summaryrefslogtreecommitdiffstats
path: root/crypto/ecdsa
diff options
context:
space:
mode:
authorAndy Polyakov <appro@openssl.org>2014-09-12 00:13:20 +0200
committerAndy Polyakov <appro@openssl.org>2014-09-22 00:07:44 +0200
commit8aed2a7548362e88e84a7feb795a3a97e8395008 (patch)
tree63b547c414c388873b7955303ae2ad51bd0c1eb2 /crypto/ecdsa
parentf7835e1c20836f286f00d6bcc69f154493e01475 (diff)
Reserve option to use BN_mod_exp_mont_consttime in ECDSA.
Submitted by Shay Gueron, Intel Corp. RT: 3149 Reviewed-by: Rich Salz <rsalz@openssl.org> (cherry picked from commit f54be179aa4cbbd944728771d7d59ed588158a12)
Diffstat (limited to 'crypto/ecdsa')
-rw-r--r--crypto/ecdsa/ecs_ossl.c36
1 files changed, 31 insertions, 5 deletions
diff --git a/crypto/ecdsa/ecs_ossl.c b/crypto/ecdsa/ecs_ossl.c
index 7725935610..c23343b64d 100644
--- a/crypto/ecdsa/ecs_ossl.c
+++ b/crypto/ecdsa/ecs_ossl.c
@@ -187,11 +187,37 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
while (BN_is_zero(r));
/* compute the inverse of k */
- if (!BN_mod_inverse(k, k, order, ctx))
- {
- ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
- goto err;
- }
+ if (EC_GROUP_get_mont_data(group) != NULL)
+ {
+ /* We want inverse in constant time, therefore we utilize the
+ * fact order must be prime and use Fermats Little Theorem
+ * instead. */
+ if (!BN_set_word(X, 2) )
+ {
+ ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
+ goto err;
+ }
+ if (!BN_mod_sub(X, order, X, order, ctx))
+ {
+ ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
+ goto err;
+ }
+ BN_set_flags(X, BN_FLG_CONSTTIME);
+ if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx, EC_GROUP_get_mont_data(group)))
+ {
+ ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
+ goto err;
+ }
+ }
+ else
+ {
+ if (!BN_mod_inverse(k, k, order, ctx))
+ {
+ ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
+ goto err;
+ }
+ }
+
/* clear old values if necessary */
if (*rp != NULL)
BN_clear_free(*rp);