@@ -73,19 +73,28 @@ static const unsigned int MAX_GENS = UINT32_MAX;
73
73
static const unsigned int MAX_OUT = (1 << 19 );
74
74
75
75
/*
76
- * Assumes: prng != NULL, e != NULL, len >= 0.
76
+ * Assumes: prng != NULL
77
77
*/
78
- static void update (TCHmacPrng_t prng , const uint8_t * e , unsigned int len )
78
+ static void update (TCHmacPrng_t prng , const uint8_t * data , unsigned int datalen , const uint8_t * additional_data , unsigned int additional_datalen )
79
79
{
80
80
const uint8_t separator0 = 0x00 ;
81
81
const uint8_t separator1 = 0x01 ;
82
82
83
+ /* configure the new prng key into the prng's instance of hmac */
84
+ tc_hmac_set_key (& prng -> h , prng -> key , sizeof (prng -> key ));
85
+
83
86
/* use current state, e and separator 0 to compute a new prng key: */
84
87
(void )tc_hmac_init (& prng -> h );
85
88
(void )tc_hmac_update (& prng -> h , prng -> v , sizeof (prng -> v ));
86
89
(void )tc_hmac_update (& prng -> h , & separator0 , sizeof (separator0 ));
87
- (void )tc_hmac_update (& prng -> h , e , len );
90
+
91
+ if (data && datalen )
92
+ (void )tc_hmac_update (& prng -> h , data , datalen );
93
+ if (additional_data && additional_datalen )
94
+ (void )tc_hmac_update (& prng -> h , additional_data , additional_datalen );
95
+
88
96
(void )tc_hmac_final (prng -> key , sizeof (prng -> key ), & prng -> h );
97
+
89
98
/* configure the new prng key into the prng's instance of hmac */
90
99
(void )tc_hmac_set_key (& prng -> h , prng -> key , sizeof (prng -> key ));
91
100
@@ -94,12 +103,21 @@ static void update(TCHmacPrng_t prng, const uint8_t *e, unsigned int len)
94
103
(void )tc_hmac_update (& prng -> h , prng -> v , sizeof (prng -> v ));
95
104
(void )tc_hmac_final (prng -> v , sizeof (prng -> v ), & prng -> h );
96
105
106
+ if (data == 0 || datalen == 0 )
107
+ return ;
108
+
109
+ /* configure the new prng key into the prng's instance of hmac */
110
+ tc_hmac_set_key (& prng -> h , prng -> key , sizeof (prng -> key ));
111
+
97
112
/* use current state, e and separator 1 to compute a new prng key: */
98
113
(void )tc_hmac_init (& prng -> h );
99
114
(void )tc_hmac_update (& prng -> h , prng -> v , sizeof (prng -> v ));
100
115
(void )tc_hmac_update (& prng -> h , & separator1 , sizeof (separator1 ));
101
- (void )tc_hmac_update (& prng -> h , e , len );
116
+ (void )tc_hmac_update (& prng -> h , data , datalen );
117
+ if (additional_data && additional_datalen )
118
+ (void )tc_hmac_update (& prng -> h , additional_data , additional_datalen );
102
119
(void )tc_hmac_final (prng -> key , sizeof (prng -> key ), & prng -> h );
120
+
103
121
/* configure the new prng key into the prng's instance of hmac */
104
122
(void )tc_hmac_set_key (& prng -> h , prng -> key , sizeof (prng -> key ));
105
123
@@ -124,10 +142,8 @@ int tc_hmac_prng_init(TCHmacPrng_t prng,
124
142
/* put the generator into a known state: */
125
143
_set (prng -> key , 0x00 , sizeof (prng -> key ));
126
144
_set (prng -> v , 0x01 , sizeof (prng -> v ));
127
- tc_hmac_set_key (& prng -> h , prng -> key , sizeof (prng -> key ));
128
- /* update assumes SOME key has been configured into HMAC */
129
145
130
- update (prng , personalization , plen );
146
+ update (prng , personalization , plen , 0 , 0 );
131
147
132
148
/* force a reseed before allowing tc_hmac_prng_generate to succeed: */
133
149
prng -> countdown = 0 ;
@@ -159,13 +175,12 @@ int tc_hmac_prng_reseed(TCHmacPrng_t prng,
159
175
additionallen > MAX_ALEN ) {
160
176
return TC_CRYPTO_FAIL ;
161
177
} else {
162
- /* call update for the seed and additional_input */
163
- update (prng , seed , seedlen );
164
- update (prng , additional_input , additionallen );
178
+ /* call update for the seed and additional_input */
179
+ update (prng , seed , seedlen , additional_input , additionallen );
165
180
}
166
181
} else {
167
182
/* call update only for the seed */
168
- update (prng , seed , seedlen );
183
+ update (prng , seed , seedlen , 0 , 0 );
169
184
}
170
185
171
186
/* ... and enable hmac_prng_generate */
@@ -191,6 +206,9 @@ int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng)
191
206
prng -> countdown -- ;
192
207
193
208
while (outlen != 0 ) {
209
+ /* configure the new prng key into the prng's instance of hmac */
210
+ tc_hmac_set_key (& prng -> h , prng -> key , sizeof (prng -> key ));
211
+
194
212
/* operate HMAC in OFB mode to create "random" outputs */
195
213
(void )tc_hmac_init (& prng -> h );
196
214
(void )tc_hmac_update (& prng -> h , prng -> v , sizeof (prng -> v ));
@@ -206,7 +224,7 @@ int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng)
206
224
}
207
225
208
226
/* block future PRNG compromises from revealing past state */
209
- update (prng , prng -> v , TC_SHA256_DIGEST_SIZE );
227
+ update (prng , 0 , 0 , 0 , 0 );
210
228
211
229
return TC_CRYPTO_SUCCESS ;
212
230
}
0 commit comments