diff options
author | Rob Austein <sra@hactrn.net> | 2015-09-08 12:45:01 -0400 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2015-09-08 12:45:01 -0400 |
commit | c334ad5442340a6f6ba22d3d56970b7fe376df6b (patch) | |
tree | dafab59be410e976cd64e14236d0130fe5b6e779 /rsa.c | |
parent | 53bff0b94832da75e37bea3e94e051f24fdec560 (diff) | |
parent | 2d37968bdcfb97281386ca56cea59d39c0b98f8b (diff) |
Merge branch 'modexps6'
Diffstat (limited to 'rsa.c')
-rw-r--r-- | rsa.c | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -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 || |