diff options
author | Rob Austein <sra@hactrn.net> | 2017-03-02 20:34:19 -0500 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2017-03-02 20:34:19 -0500 |
commit | 91bb0444d8830569bfc09900d599c9ae39953f01 (patch) | |
tree | 3d4030a905aa3c6865b7fad0943f813a17fc4087 /csprng.c | |
parent | c2f499e0a301d4ad0f92d58f436d01087ceae1bb (diff) | |
parent | a33470b2f899a94b50304a73721ba6d0d6d447b4 (diff) |
Merge branch 'pymux' into hw_ecdsa_p256
Diffstat (limited to 'csprng.c')
-rw-r--r-- | csprng.c | 23 |
1 files changed, 11 insertions, 12 deletions
@@ -45,35 +45,34 @@ hal_error_t hal_get_random(hal_core_t *core, void *buffer, const size_t length) { - uint8_t temp[4], *buf = buffer; + uint8_t temp[4], ior = 0, * const buf = buffer; hal_error_t err; - size_t i; if ((err = hal_core_alloc(CSPRNG_NAME, &core)) != HAL_OK) return err; - for (i = 0; i < length; i += 4) { + for (size_t i = 0; i < length; i += 4) { const int last = (length - i) < 4; if (WAIT_FOR_CSPRNG_VALID && (err = hal_io_wait_valid(core)) != HAL_OK) - goto out; + break; if ((err = hal_io_read(core, CSPRNG_ADDR_RANDOM, (last ? temp : &buf[i]), 4)) != HAL_OK) - goto out; + break; if (last) for (; i < length; i++) buf[i] = temp[i&3]; } - for (i = 0, buf = buffer; i < length; i++, buf++) - if (*buf != 0) { - err = HAL_OK; - goto out; - } - err = HAL_ERROR_CSPRNG_BROKEN; + if (err == HAL_OK) { + for (size_t i = 0; i < length; i++) + ior |= buf[i]; + + if (ior == 0 && length > 0) + err = HAL_ERROR_CSPRNG_BROKEN; + } -out: hal_core_free(core); return err; } |