aboutsummaryrefslogtreecommitdiff
path: root/rsa.c
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2015-07-14 00:20:03 -0400
committerRob Austein <sra@hactrn.net>2015-07-14 00:20:03 -0400
commit0915302b7ffa0757be1859d73f14037f338c0253 (patch)
tree80f047113c5a290c13be95758ab58a0dd0f1ab1e /rsa.c
parent0ababd7dc7ab6a080971e669598a862ad23d2fa1 (diff)
Changes to support Pavel's ModExpS6 core.
Diffstat (limited to 'rsa.c')
-rw-r--r--rsa.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/rsa.c b/rsa.c
index b05dec7..2e950b8 100644
--- a/rsa.c
+++ b/rsa.c
@@ -55,7 +55,7 @@
*/
#ifndef HAL_RSA_USE_MODEXP
-#define HAL_RSA_USE_MODEXP 0
+#define HAL_RSA_USE_MODEXP 1
#endif
/*
@@ -170,13 +170,21 @@ static hal_error_t modexp(fp_int *msg, fp_int *exp, fp_int *mod, fp_int *res)
assert(msg != NULL && exp != NULL && mod != NULL && res != NULL);
- const size_t msg_len = fp_unsigned_bin_size(msg);
- const size_t exp_len = fp_unsigned_bin_size(exp);
- const size_t mod_len = fp_unsigned_bin_size(mod);
+ fp_int reduced_msg;
- const size_t len = (MAX(MAX(msg_len, exp_len), mod_len) + 3) & ~3;
+ if (fp_cmp_mag(msg, mod) != FP_LT) {
+ fp_init(&reduced_msg);
+ fp_mod(msg, mod, &reduced_msg);
+ msg = &reduced_msg;
+ }
+
+ const size_t exp_len = (fp_unsigned_bin_size(exp) + 3) & ~3;
+ const size_t mod_len = (fp_unsigned_bin_size(mod) + 3) & ~3;
- uint8_t msgbuf[len], expbuf[len], modbuf[len], resbuf[len];
+ uint8_t msgbuf[mod_len];
+ uint8_t expbuf[exp_len];
+ uint8_t modbuf[mod_len];
+ uint8_t resbuf[mod_len];
if ((err = unpack_fp(msg, msgbuf, sizeof(msgbuf))) != HAL_OK ||
(err = unpack_fp(exp, expbuf, sizeof(expbuf))) != HAL_OK ||