From 30f8e4e85b6a337291b09d55d8edc15e422b6341 Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 5 Jul 2016 22:45:35 -0400 Subject: 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. --- modexp.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'modexp.c') diff --git a/modexp.c b/modexp.c index b84c51b..bad492b 100644 --- a/modexp.c +++ b/modexp.c @@ -138,12 +138,14 @@ static hal_error_t set_buffer(const hal_core_t *core, * Run one modexp operation. */ -hal_error_t hal_modexp(const hal_core_t *core, +hal_error_t hal_modexp(hal_core_t *core, const uint8_t * const msg, const size_t msg_len, /* Message */ const uint8_t * const exp, const size_t exp_len, /* Exponent */ const uint8_t * const mod, const size_t mod_len, /* Modulus */ uint8_t *result, const size_t result_len) { + hal_error_t err; + /* * All pointers must be set, neither message nor exponent may be * longer than modulus, result buffer must not be shorter than @@ -160,6 +162,22 @@ hal_error_t hal_modexp(const hal_core_t *core, ((msg_len | exp_len | mod_len) & 3) != 0) return HAL_ERROR_BAD_ARGUMENTS; + if (((err = hal_core_alloc(MODEXPS6_NAME, &core)) == HAL_ERROR_CORE_NOT_FOUND) && + ((err = hal_core_alloc(MODEXPA7_NAME, &core)) != HAL_OK)) + return err; + +#undef check +#define check(_expr_) \ + do { \ + hal_error_t _err = (_expr_); \ + if (_err != HAL_OK && debug) \ + printf("%s failed: %s\n", #_expr_, hal_error_string(_err)); \ + if (_err != HAL_OK) { \ + hal_core_free(core); \ + return _err; \ + } \ + } while (0) + /* * We probably ought to take the mode (fast vs constant-time) as an * argument, but for the moment we just guess that really short -- cgit v1.2.3