Skip to content

Commit 4675d05

Browse files
committed
Add behavior to throw away collected entropy on occurrence of a noise alarm.
1 parent d2020f5 commit 4675d05

File tree

1 file changed

+16
-8
lines changed
  • targets/TARGET_Silicon_Labs/TARGET_EFM32/trng

1 file changed

+16
-8
lines changed

targets/TARGET_Silicon_Labs/TARGET_EFM32/trng/sl_trng.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ int sl_trng_poll( TRNG_TypeDef *device,
218218
size_t *olen )
219219
{
220220
size_t output_len = 0;
221-
size_t count = 0;
221+
size_t chunk_len = 0;
222222
size_t available;
223223
int ret = 0;
224224

225-
while (len > 0)
225+
while (output_len < len)
226226
{
227227
available = device->FIFOLEVEL * 4;
228228
if (available == 0)
@@ -241,7 +241,17 @@ int sl_trng_poll( TRNG_TypeDef *device,
241241
(ret == SL_TRNG_ERR_NOISE_ALARM) )
242242
{
243243
ret = 0;
244-
break;
244+
continue;
245+
}
246+
#else
247+
/* Noise alarms trigger a FIFO clearing, and we need to throw
248+
* away the collected entropy. */
249+
if ( (ret == SL_TRNG_ERR_PRELIMINARY_NOISE_ALARM) ||
250+
(ret == SL_TRNG_ERR_NOISE_ALARM) )
251+
{
252+
ret = 0;
253+
output_len = 0;
254+
continue;
245255
}
246256
#endif
247257
/* Alarm has been signaled so we throw the generated data away. */
@@ -252,11 +262,9 @@ int sl_trng_poll( TRNG_TypeDef *device,
252262
}
253263
#endif
254264

255-
count = SL_MIN(len, available);
256-
sl_trng_read_chunk(device, output, count);
257-
output += count;
258-
output_len += count;
259-
len -= count;
265+
chunk_len = SL_MIN(len - output_len, available);
266+
sl_trng_read_chunk(device, output + output_len, chunk_len);
267+
output_len += chunk_len;
260268
}
261269

262270
*olen = output_len;

0 commit comments

Comments
 (0)