File tree Expand file tree Collapse file tree 2 files changed +44
-26
lines changed Expand file tree Collapse file tree 2 files changed +44
-26
lines changed Original file line number Diff line number Diff line change 25
25
/*
26
26
* Get Random number generator.
27
27
*/
28
+
29
+ #define PRNG_KEY_SIZE (0x20UL)
30
+
28
31
static volatile int g_PRNG_done ;
29
32
volatile int g_AES_done ;
30
33
34
+ /* Implementation that should never be optimized out by the compiler */
35
+ static void trng_zeroize ( void * v , size_t n ) {
36
+ volatile unsigned char * p = (unsigned char * )v ; while ( n -- ) * p ++ = 0 ;
37
+ }
38
+
31
39
void CRYPTO_IRQHandler ()
32
40
{
33
41
if (PRNG_GET_INT_FLAG ()) {
@@ -77,21 +85,22 @@ void trng_free(trng_t *obj)
77
85
int trng_get_bytes (trng_t * obj , uint8_t * output , size_t length , size_t * output_length )
78
86
{
79
87
(void )obj ;
80
-
81
- * output_length = 0 ;
82
- if (length < 32 ) {
83
- unsigned char tmpBuff [32 ];
88
+ unsigned char tmpBuff [PRNG_KEY_SIZE ];
89
+ size_t cur_length = 0 ;
90
+
91
+ while (length >= sizeof (tmpBuff )) {
92
+ trng_get (output );
93
+ output += sizeof (tmpBuff );
94
+ cur_length += sizeof (tmpBuff );
95
+ length -= sizeof (tmpBuff );
96
+ }
97
+ if (length > 0 ) {
84
98
trng_get (tmpBuff );
85
- memcpy (output , & tmpBuff , length );
86
- * output_length = length ;
87
- } else {
88
- for (unsigned i = 0 ; i < (length /32 ); i ++ ) {
89
- trng_get (output );
90
- * output_length += 32 ;
91
- output += 32 ;
92
- }
99
+ memcpy (output , tmpBuff , length );
100
+ cur_length += length ;
101
+ trng_zeroize (tmpBuff , sizeof (tmpBuff ));
93
102
}
94
-
103
+ * output_length = cur_length ;
95
104
return 0 ;
96
105
}
97
106
Original file line number Diff line number Diff line change 30
30
/*
31
31
* Get Random number generator.
32
32
*/
33
+
34
+ #define PRNG_KEY_SIZE (0x20UL)
35
+
33
36
static volatile int g_PRNG_done ;
34
37
volatile int g_AES_done ;
35
38
39
+ /* Implementation that should never be optimized out by the compiler */
40
+ static void trng_zeroize ( void * v , size_t n ) {
41
+ volatile unsigned char * p = (unsigned char * )v ; while ( n -- ) * p ++ = 0 ;
42
+ }
43
+
36
44
void CRYPTO_IRQHandler ()
37
45
{
38
46
if (PRNG_GET_INT_FLAG ()) {
@@ -82,21 +90,22 @@ void trng_free(trng_t *obj)
82
90
int trng_get_bytes (trng_t * obj , uint8_t * output , size_t length , size_t * output_length )
83
91
{
84
92
(void )obj ;
85
-
86
- * output_length = 0 ;
87
- if (length < 32 ) {
88
- unsigned char tmpBuff [32 ];
93
+ unsigned char tmpBuff [PRNG_KEY_SIZE ];
94
+ size_t cur_length = 0 ;
95
+
96
+ while (length >= sizeof (tmpBuff )) {
97
+ trng_get (output );
98
+ output += sizeof (tmpBuff );
99
+ cur_length += sizeof (tmpBuff );
100
+ length -= sizeof (tmpBuff );
101
+ }
102
+ if (length > 0 ) {
89
103
trng_get (tmpBuff );
90
- memcpy (output , & tmpBuff , length );
91
- * output_length = length ;
92
- } else {
93
- for (int i = 0 ; i < (length /32 ); i ++ ) {
94
- trng_get (output );
95
- * output_length += 32 ;
96
- output += 32 ;
97
- }
104
+ memcpy (output , tmpBuff , length );
105
+ cur_length += length ;
106
+ trng_zeroize (tmpBuff , sizeof (tmpBuff ));
98
107
}
99
-
108
+ * output_length = cur_length ;
100
109
return 0 ;
101
110
}
102
111
You can’t perform that action at this time.
0 commit comments