aboutsummaryrefslogtreecommitdiff
path: root/csprng.c
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2016-07-05 22:45:35 -0400
committerPaul Selkirk <paul@psgd.org>2016-07-05 22:45:35 -0400
commit30f8e4e85b6a337291b09d55d8edc15e422b6341 (patch)
tree19199dd47bb98e18a96281d34e35d1971565fc72 /csprng.c
parente1c57eff41a57b8a3f16e5d652b5598d75887a21 (diff)
Attempt to add resource management, for multiple cores of the same type.
Find a suitable core, and mark it busy. Don't forget to release it as soon as you're done. This has a knock-on effect of un-const'ing core arguments and struct fields in a lot of places, and it moves some core checks around.
Diffstat (limited to 'csprng.c')
-rw-r--r--csprng.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/csprng.c b/csprng.c
index c6ae4e1..7ff6c69 100644
--- a/csprng.c
+++ b/csprng.c
@@ -43,23 +43,23 @@
#define WAIT_FOR_CSPRNG_VALID 1
#endif
-hal_error_t hal_get_random(const hal_core_t *core, void *buffer, const size_t length)
+hal_error_t hal_get_random(hal_core_t *core, void *buffer, const size_t length)
{
uint8_t temp[4], *buf = buffer;
hal_error_t err;
size_t i;
- if ((err = hal_core_check_name(&core, CSPRNG_NAME)) != HAL_OK)
+ if ((err = hal_core_alloc(CSPRNG_NAME, &core)) != HAL_OK)
return err;
for (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)
- return err;
+ goto out;
if ((err = hal_io_read(core, CSPRNG_ADDR_RANDOM, (last ? temp : &buf[i]), 4)) != HAL_OK)
- return err;
+ goto out;
if (last)
for (; i < length; i++)
@@ -67,10 +67,15 @@ hal_error_t hal_get_random(const hal_core_t *core, void *buffer, const size_t le
}
for (i = 0, buf = buffer; i < length; i++, buf++)
- if (*buf != 0)
- return HAL_OK;
+ if (*buf != 0) {
+ err = HAL_OK;
+ goto out;
+ }
+ err = HAL_ERROR_CSPRNG_BROKEN;
- return HAL_ERROR_CSPRNG_BROKEN;
+out:
+ hal_core_free(core);
+ return err;
}
/*