From e3895a788129f57e27716d1ab00df002c96b81a3 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sat, 9 Sep 2017 23:45:20 -0400 Subject: Far too much fun with modexpa7 operand lengths and locations. --- modexp.c | 30 ++++++++++++++++-------------- rsa.c | 2 +- verilog_constants.h | 3 ++- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/modexp.c b/modexp.c index 950455f..7ff7b21 100644 --- a/modexp.c +++ b/modexp.c @@ -195,13 +195,13 @@ hal_error_t hal_modexp(hal_core_t *core, * about 32-bit words). */ - if (mod == NULL || - msg == NULL || msg_len > mod_len * 2 || - exp == NULL || exp_len > mod_len || - result == NULL || result_len < mod_len || - coeff == NULL || coeff_len != mod_len || - mont == NULL || mont_len != mod_len || - ((msg_len | exp_len | mod_len | coeff_len | mont_len) & 3) != 0) + if (msg == NULL || msg_len > MODEXPA7_OPERAND_BYTES || msg_len > mod_len * 2 || + exp == NULL || exp_len > MODEXPA7_OPERAND_BYTES || exp_len > mod_len || + mod == NULL || mod_len > MODEXPA7_OPERAND_BYTES || + result == NULL || result_len > MODEXPA7_OPERAND_BYTES || result_len < mod_len || + coeff == NULL || coeff_len > MODEXPA7_OPERAND_BYTES || + mont == NULL || mont_len > MODEXPA7_OPERAND_BYTES || + ((msg_len | exp_len | mod_len) & 3) != 0) return HAL_ERROR_BAD_ARGUMENTS; /* @@ -221,9 +221,11 @@ hal_error_t hal_modexp(hal_core_t *core, check(get_register(core, MODEXPA7_ADDR_BUFFER_BITS, &operand_max)); operand_max /= 8; - if (msg_len > operand_max || exp_len > operand_max || mod_len > operand_max || - (coeff != NULL && coeff_len > operand_max) || - (mont != NULL && mont_len > operand_max)) { + if (msg_len > operand_max || + exp_len > operand_max || + mod_len > operand_max || + coeff_len > operand_max || + mont_len > operand_max) { hal_core_free(core); return HAL_ERROR_BAD_ARGUMENTS; } @@ -244,13 +246,13 @@ hal_error_t hal_modexp(hal_core_t *core, check(hal_io_zero(core)); check(hal_io_init(core)); check(hal_io_wait_ready(core)); - check(get_buffer(core, MODEXPA7_ADDR_MODULUS_COEFF_OUT, coeff, coeff_len)); - check(get_buffer(core, MODEXPA7_ADDR_MONTGOMERY_FACTOR_OUT, mont, mont_len)); + check(get_buffer(core, MODEXPA7_ADDR_MODULUS_COEFF_OUT, coeff, coeff_len)); + check(get_buffer(core, MODEXPA7_ADDR_MONTGOMERY_FACTOR_OUT, mont, mont_len)); } /* Load modulus-dependent speedup factors (even if we just calculated them) */ - check(set_buffer(core, MODEXPA7_ADDR_MODULUS_COEFF_IN, coeff, coeff_len)); - check(set_buffer(core, MODEXPA7_ADDR_MONTGOMERY_FACTOR_IN, mont, mont_len)); + check(set_buffer(core, MODEXPA7_ADDR_MODULUS_COEFF_IN, coeff, coeff_len)); + check(set_buffer(core, MODEXPA7_ADDR_MONTGOMERY_FACTOR_IN, mont, mont_len)); /* Select CRT mode if and only if message is longer than exponent */ check(set_register(core, MODEXPA7_ADDR_MODE, diff --git a/rsa.c b/rsa.c index d964210..9cc940c 100644 --- a/rsa.c +++ b/rsa.c @@ -98,7 +98,7 @@ */ #ifndef HAL_RSA_MAX_OPERAND_LENGTH -#define HAL_RSA_MAX_OPERAND_LENGTH (4096 / 8) +#define HAL_RSA_MAX_OPERAND_LENGTH MODEXPA7_OPERAND_BYTES #endif /* diff --git a/verilog_constants.h b/verilog_constants.h index 5307f68..7b64c46 100644 --- a/verilog_constants.h +++ b/verilog_constants.h @@ -231,9 +231,10 @@ */ #define MODEXPA7_OPERAND_BITS (4096) +#define MODEXPA7_OPERAND_BYTES (MODEXPA7_OPERAND_BITS / 8) #define MODEXPA7_OPERAND_WORDS (MODEXPA7_OPERAND_BITS / 32) #define MODEXPA7_ADDR_REGISTERS (0 * MODEXPA7_OPERAND_WORDS) -#define MODEXPA7_ADDR_OPERANDS (4 * MODEXPA7_OPERAND_WORDS) +#define MODEXPA7_ADDR_OPERANDS (8 * MODEXPA7_OPERAND_WORDS) #define MODEXPA7_ADDR_MODE (MODEXPA7_ADDR_REGISTERS + 0x10) #define MODEXPA7_ADDR_MODULUS_BITS (MODEXPA7_ADDR_REGISTERS + 0x11) #define MODEXPA7_ADDR_EXPONENT_BITS (MODEXPA7_ADDR_REGISTERS + 0x12) -- cgit v1.2.3