aboutsummaryrefslogtreecommitdiff
path: root/modexp.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 /modexp.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 'modexp.c')
-rw-r--r--modexp.c20
1 files changed, 19 insertions, 1 deletions
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