diff options
Diffstat (limited to 'src/crypto/fipsmodule/bn/mul.c')
-rw-r--r-- | src/crypto/fipsmodule/bn/mul.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/src/crypto/fipsmodule/bn/mul.c b/src/crypto/fipsmodule/bn/mul.c index 352b7e5f..4a0711d8 100644 --- a/src/crypto/fipsmodule/bn/mul.c +++ b/src/crypto/fipsmodule/bn/mul.c @@ -306,6 +306,24 @@ static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a, return borrow; } +int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, + BN_CTX *ctx) { + int cl = a->width < b->width ? a->width : b->width; + int dl = a->width - b->width; + int r_len = a->width < b->width ? b->width : a->width; + BN_CTX_start(ctx); + BIGNUM *tmp = BN_CTX_get(ctx); + int ok = tmp != NULL && + bn_wexpand(r, r_len) && + bn_wexpand(tmp, r_len); + if (ok) { + bn_abs_sub_part_words(r->d, a->d, b->d, cl, dl, tmp->d); + r->width = r_len; + } + BN_CTX_end(ctx); + return ok; +} + // Karatsuba recursive multiplication algorithm // (cf. Knuth, The Art of Computer Programming, Vol. 2) @@ -523,9 +541,9 @@ static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a, assert(c == 0); } -// bn_mul_impl implements |BN_mul| and |bn_mul_fixed|. Note this function breaks -// |BIGNUM| invariants and may return a negative zero. This is handled by the -// callers. +// bn_mul_impl implements |BN_mul| and |bn_mul_consttime|. Note this function +// breaks |BIGNUM| invariants and may return a negative zero. This is handled by +// the callers. static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { int al = a->width; @@ -628,7 +646,7 @@ int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { return 1; } -int bn_mul_fixed(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { +int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { // Prevent negative zeros. if (a->neg || b->neg) { OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); @@ -773,7 +791,7 @@ int BN_mul_word(BIGNUM *bn, BN_ULONG w) { return 1; } -int bn_sqr_fixed(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { +int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { int al = a->width; if (al <= 0) { r->width = 0; @@ -832,7 +850,7 @@ err: } int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { - if (!bn_sqr_fixed(r, a, ctx)) { + if (!bn_sqr_consttime(r, a, ctx)) { return 0; } |