[Cryptech-Commits] [sw/libhal] 01/05: Clean up csprng driver.

git at cryptech.is git at cryptech.is
Wed Feb 15 01:13:16 UTC 2017


This is an automated email from the git hooks/post-receive script.

sra at hactrn.net pushed a commit to branch pymux
in repository sw/libhal.

commit a118a9b503537894fd9dc4ff021ef3b0635166eb
Author: Rob Austein <sra at hactrn.net>
AuthorDate: Fri Feb 3 00:21:02 2017 -0500

    Clean up csprng driver.
---
 csprng.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/csprng.c b/csprng.c
index 7ff6c69..8ba4fa5 100644
--- a/csprng.c
+++ b/csprng.c
@@ -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;
 }



More information about the Commits mailing list