summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbeck <>2017-01-21 11:00:47 +0000
committerbeck <>2017-01-21 11:00:47 +0000
commit9a561f2327a3e6b5ee853e60427012bfe6e02043 (patch)
tree7ad0f2808a2ac42a510bf2f280dba63a3dfc83e3 /src
parentbce45cc241b51da39ead8b476c811b47d76ccc46 (diff)
downloadopenbsd-9a561f2327a3e6b5ee853e60427012bfe6e02043.tar.gz
openbsd-9a561f2327a3e6b5ee853e60427012bfe6e02043.tar.bz2
openbsd-9a561f2327a3e6b5ee853e60427012bfe6e02043.zip
Add ct and nonct versions of BN_mod_inverse for internal use
ok jsing@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/bn/bn.h4
-rw-r--r--src/lib/libcrypto/bn/bn_blind.c4
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c30
-rw-r--r--src/lib/libcrypto/bn/bn_lcl.h6
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c8
-rw-r--r--src/lib/libcrypto/bn/bn_x931p.c8
-rw-r--r--src/lib/libcrypto/dsa/dsa_ossl.c6
-rw-r--r--src/lib/libcrypto/ec/ecp_smpl.c7
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_ossl.c10
-rw-r--r--src/lib/libcrypto/gost/gostr341001.c4
-rw-r--r--src/lib/libcrypto/rsa/rsa_chk.c4
-rw-r--r--src/lib/libcrypto/rsa/rsa_crpt.c6
-rw-r--r--src/lib/libcrypto/rsa/rsa_gen.c6
13 files changed, 67 insertions, 36 deletions
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h
index fd9a62fe3f..5d5de7e43a 100644
--- a/src/lib/libcrypto/bn/bn.h
+++ b/src/lib/libcrypto/bn/bn.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn.h,v 1.34 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: bn.h,v 1.35 2017/01/21 11:00:46 beck Exp $ */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -454,8 +454,10 @@ int BN_dec2bn(BIGNUM **a, const char *str);
454int BN_asc2bn(BIGNUM **a, const char *str); 454int BN_asc2bn(BIGNUM **a, const char *str);
455int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 455int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
456int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */ 456int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */
457#ifndef LIBRESSL_INTERNAL
457BIGNUM *BN_mod_inverse(BIGNUM *ret, 458BIGNUM *BN_mod_inverse(BIGNUM *ret,
458 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 459 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
460#endif
459BIGNUM *BN_mod_sqrt(BIGNUM *ret, 461BIGNUM *BN_mod_sqrt(BIGNUM *ret,
460 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 462 const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
461 463
diff --git a/src/lib/libcrypto/bn/bn_blind.c b/src/lib/libcrypto/bn/bn_blind.c
index 01874f6208..28c6276751 100644
--- a/src/lib/libcrypto/bn/bn_blind.c
+++ b/src/lib/libcrypto/bn/bn_blind.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_blind.c,v 1.15 2017/01/21 09:38:58 beck Exp $ */ 1/* $OpenBSD: bn_blind.c,v 1.16 2017/01/21 11:00:46 beck Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -351,7 +351,7 @@ BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m,
351 do { 351 do {
352 if (!BN_rand_range(ret->A, ret->mod)) 352 if (!BN_rand_range(ret->A, ret->mod))
353 goto err; 353 goto err;
354 if (BN_mod_inverse(ret->Ai, ret->A, ret->mod, ctx) == NULL) { 354 if (BN_mod_inverse_ct(ret->Ai, ret->A, ret->mod, ctx) == NULL) {
355 /* this should almost never happen for good RSA keys */ 355 /* this should almost never happen for good RSA keys */
356 unsigned long error = ERR_peek_last_error(); 356 unsigned long error = ERR_peek_last_error();
357 if (ERR_GET_REASON(error) == BN_R_NO_INVERSE) { 357 if (ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index 3c8ff5b405..4eab1b36d2 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_gcd.c,v 1.11 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: bn_gcd.c,v 1.12 2017/01/21 11:00:46 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -231,17 +231,16 @@ err:
231static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, 231static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a,
232 const BIGNUM *n, BN_CTX *ctx); 232 const BIGNUM *n, BN_CTX *ctx);
233 233
234BIGNUM * 234static BIGNUM *
235BN_mod_inverse(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) 235BN_mod_inverse_internal(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
236 int ct)
236{ 237{
237 BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; 238 BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
238 BIGNUM *ret = NULL; 239 BIGNUM *ret = NULL;
239 int sign; 240 int sign;
240 241
241 if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || 242 if (ct)
242 (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
243 return BN_mod_inverse_no_branch(in, a, n, ctx); 243 return BN_mod_inverse_no_branch(in, a, n, ctx);
244 }
245 244
246 bn_check_top(a); 245 bn_check_top(a);
247 bn_check_top(n); 246 bn_check_top(n);
@@ -524,6 +523,25 @@ err:
524 return (ret); 523 return (ret);
525} 524}
526 525
526BIGNUM *
527BN_mod_inverse(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
528{
529 int ct = ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) ||
530 (BN_get_flags(n, BN_FLG_CONSTTIME) != 0));
531 return BN_mod_inverse_internal(in, a, n, ctx, ct);
532}
533
534BIGNUM *
535BN_mod_inverse_nonct(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
536{
537 return BN_mod_inverse_internal(in, a, n, ctx, 0);
538}
539
540BIGNUM *
541BN_mod_inverse_ct(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
542{
543 return BN_mod_inverse_internal(in, a, n, ctx, 1);
544}
527 545
528/* BN_mod_inverse_no_branch is a special version of BN_mod_inverse. 546/* BN_mod_inverse_no_branch is a special version of BN_mod_inverse.
529 * It does not contain branches that may leak sensitive information. 547 * It does not contain branches that may leak sensitive information.
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h
index 59d9036d01..75c35499a8 100644
--- a/src/lib/libcrypto/bn/bn_lcl.h
+++ b/src/lib/libcrypto/bn/bn_lcl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lcl.h,v 1.25 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: bn_lcl.h,v 1.26 2017/01/21 11:00:46 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -599,5 +599,9 @@ int BN_div_ct(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
599 BN_CTX *ctx); 599 BN_CTX *ctx);
600#define BN_mod_ct(rem,m,d,ctx) BN_div_ct(NULL,(rem),(m),(d),(ctx)) 600#define BN_mod_ct(rem,m,d,ctx) BN_div_ct(NULL,(rem),(m),(d),(ctx))
601#define BN_mod_nonct(rem,m,d,ctx) BN_div_nonct(NULL,(rem),(m),(d),(ctx)) 601#define BN_mod_nonct(rem,m,d,ctx) BN_div_nonct(NULL,(rem),(m),(d),(ctx))
602BIGNUM *BN_mod_inverse_ct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n,
603 BN_CTX *ctx);
604BIGNUM *BN_mod_inverse_nonct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n,
605 BN_CTX *ctx);
602__END_HIDDEN_DECLS 606__END_HIDDEN_DECLS
603#endif 607#endif
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index 3496502435..eeac046826 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mont.c,v 1.25 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: bn_mont.c,v 1.26 2017/01/21 11:00:46 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -400,7 +400,7 @@ BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
400 if ((buf[1] = mod->top > 1 ? mod->d[1] : 0)) 400 if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
401 tmod.top = 2; 401 tmod.top = 2;
402 402
403 if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) 403 if ((BN_mod_inverse_ct(Ri, R, &tmod, ctx)) == NULL)
404 goto err; 404 goto err;
405 if (!BN_lshift(Ri, Ri, 2 * BN_BITS2)) 405 if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
406 goto err; /* R*Ri */ 406 goto err; /* R*Ri */
@@ -433,7 +433,7 @@ BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
433 buf[1] = 0; 433 buf[1] = 0;
434 tmod.top = buf[0] != 0 ? 1 : 0; 434 tmod.top = buf[0] != 0 ? 1 : 0;
435 /* Ri = R^-1 mod N*/ 435 /* Ri = R^-1 mod N*/
436 if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) 436 if ((BN_mod_inverse_ct(Ri, R, &tmod, ctx)) == NULL)
437 goto err; 437 goto err;
438 if (!BN_lshift(Ri, Ri, BN_BITS2)) 438 if (!BN_lshift(Ri, Ri, BN_BITS2))
439 goto err; /* R*Ri */ 439 goto err; /* R*Ri */
@@ -461,7 +461,7 @@ BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
461 if (!BN_set_bit(R, mont->ri)) 461 if (!BN_set_bit(R, mont->ri))
462 goto err; /* R = 2^ri */ 462 goto err; /* R = 2^ri */
463 /* Ri = R^-1 mod N*/ 463 /* Ri = R^-1 mod N*/
464 if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL) 464 if ((BN_mod_inverse_ct(Ri, R, &mont->N, ctx)) == NULL)
465 goto err; 465 goto err;
466 if (!BN_lshift(Ri, Ri, mont->ri)) 466 if (!BN_lshift(Ri, Ri, mont->ri))
467 goto err; /* R*Ri */ 467 goto err; /* R*Ri */
diff --git a/src/lib/libcrypto/bn/bn_x931p.c b/src/lib/libcrypto/bn/bn_x931p.c
index 1948bc8e71..84c998d4e1 100644
--- a/src/lib/libcrypto/bn/bn_x931p.c
+++ b/src/lib/libcrypto/bn/bn_x931p.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_x931p.c,v 1.8 2015/04/29 00:11:12 doug Exp $ */ 1/* $OpenBSD: bn_x931p.c,v 1.9 2017/01/21 11:00:46 beck Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005. 3 * project 2005.
4 */ 4 */
@@ -59,6 +59,8 @@
59#include <stdio.h> 59#include <stdio.h>
60#include <openssl/bn.h> 60#include <openssl/bn.h>
61 61
62#include "bn_lcl.h"
63
62/* X9.31 routines for prime derivation */ 64/* X9.31 routines for prime derivation */
63 65
64/* X9.31 prime derivation. This is used to generate the primes pi 66/* X9.31 prime derivation. This is used to generate the primes pi
@@ -134,13 +136,13 @@ BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, const BIGNUM *Xp,
134 136
135 /* First set p to value of Rp */ 137 /* First set p to value of Rp */
136 138
137 if (!BN_mod_inverse(p, p2, p1, ctx)) 139 if (!BN_mod_inverse_ct(p, p2, p1, ctx))
138 goto err; 140 goto err;
139 141
140 if (!BN_mul(p, p, p2, ctx)) 142 if (!BN_mul(p, p, p2, ctx))
141 goto err; 143 goto err;
142 144
143 if (!BN_mod_inverse(t, p1, p2, ctx)) 145 if (!BN_mod_inverse_ct(t, p1, p2, ctx))
144 goto err; 146 goto err;
145 147
146 if (!BN_mul(t, t, p1, ctx)) 148 if (!BN_mul(t, t, p1, ctx))
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c
index 4177557d0e..f806cd645a 100644
--- a/src/lib/libcrypto/dsa/dsa_ossl.c
+++ b/src/lib/libcrypto/dsa/dsa_ossl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dsa_ossl.c,v 1.28 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: dsa_ossl.c,v 1.29 2017/01/21 11:00:46 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -248,7 +248,7 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
248 goto err; 248 goto err;
249 249
250 /* Compute part of 's = inv(k) (m + xr) mod q' */ 250 /* Compute part of 's = inv(k) (m + xr) mod q' */
251 if ((kinv = BN_mod_inverse(NULL, &k, dsa->q, ctx)) == NULL) 251 if ((kinv = BN_mod_inverse_ct(NULL, &k, dsa->q, ctx)) == NULL)
252 goto err; 252 goto err;
253 253
254 BN_clear_free(*kinvp); 254 BN_clear_free(*kinvp);
@@ -312,7 +312,7 @@ dsa_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, DSA *dsa)
312 312
313 /* Calculate W = inv(S) mod Q 313 /* Calculate W = inv(S) mod Q
314 * save W in u2 */ 314 * save W in u2 */
315 if ((BN_mod_inverse(&u2, sig->s, dsa->q, ctx)) == NULL) 315 if ((BN_mod_inverse_ct(&u2, sig->s, dsa->q, ctx)) == NULL)
316 goto err; 316 goto err;
317 317
318 /* save M in u1 */ 318 /* save M in u1 */
diff --git a/src/lib/libcrypto/ec/ecp_smpl.c b/src/lib/libcrypto/ec/ecp_smpl.c
index f6db4dc9b1..f497657463 100644
--- a/src/lib/libcrypto/ec/ecp_smpl.c
+++ b/src/lib/libcrypto/ec/ecp_smpl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_smpl.c,v 1.15 2015/02/09 15:49:22 jsing Exp $ */ 1/* $OpenBSD: ecp_smpl.c,v 1.16 2017/01/21 11:00:47 beck Exp $ */
2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. 3 * for the OpenSSL project.
4 * Includes code written by Bodo Moeller for the OpenSSL project. 4 * Includes code written by Bodo Moeller for the OpenSSL project.
@@ -64,6 +64,7 @@
64 64
65#include <openssl/err.h> 65#include <openssl/err.h>
66 66
67#include "bn_lcl.h"
67#include "ec_lcl.h" 68#include "ec_lcl.h"
68 69
69const EC_METHOD * 70const EC_METHOD *
@@ -581,7 +582,7 @@ ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP * group, const EC_POIN
581 } 582 }
582 } 583 }
583 } else { 584 } else {
584 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx)) { 585 if (!BN_mod_inverse_ct(Z_1, Z_, &group->field, ctx)) {
585 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_BN_LIB); 586 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_BN_LIB);
586 goto err; 587 goto err;
587 } 588 }
@@ -1311,7 +1312,7 @@ ec_GFp_simple_points_make_affine(const EC_GROUP * group, size_t num, EC_POINT *
1311 1312
1312 /* invert heap[1] */ 1313 /* invert heap[1] */
1313 if (!BN_is_zero(heap[1])) { 1314 if (!BN_is_zero(heap[1])) {
1314 if (!BN_mod_inverse(heap[1], heap[1], &group->field, ctx)) { 1315 if (!BN_mod_inverse_ct(heap[1], heap[1], &group->field, ctx)) {
1315 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB); 1316 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1316 goto err; 1317 goto err;
1317 } 1318 }
diff --git a/src/lib/libcrypto/ecdsa/ecs_ossl.c b/src/lib/libcrypto/ecdsa/ecs_ossl.c
index 26158a001b..637da6535f 100644
--- a/src/lib/libcrypto/ecdsa/ecs_ossl.c
+++ b/src/lib/libcrypto/ecdsa/ecs_ossl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_ossl.c,v 1.7 2017/01/05 13:25:52 jsing Exp $ */ 1/* $OpenBSD: ecs_ossl.c,v 1.8 2017/01/21 11:00:47 beck Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project 3 * Written by Nils Larsch for the OpenSSL project
4 */ 4 */
@@ -58,11 +58,13 @@
58 58
59#include <openssl/opensslconf.h> 59#include <openssl/opensslconf.h>
60 60
61#include "ecs_locl.h"
62#include <openssl/err.h> 61#include <openssl/err.h>
63#include <openssl/obj_mac.h> 62#include <openssl/obj_mac.h>
64#include <openssl/bn.h> 63#include <openssl/bn.h>
65 64
65#include "bn_lcl.h"
66#include "ecs_locl.h"
67
66static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, 68static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
67 const BIGNUM *, const BIGNUM *, EC_KEY *eckey); 69 const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
68static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, 70static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
@@ -176,7 +178,7 @@ ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
176 } while (BN_is_zero(r)); 178 } while (BN_is_zero(r));
177 179
178 /* compute the inverse of k */ 180 /* compute the inverse of k */
179 if (!BN_mod_inverse(k, k, order, ctx)) { 181 if (!BN_mod_inverse_ct(k, k, order, ctx)) {
180 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB); 182 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
181 goto err; 183 goto err;
182 } 184 }
@@ -360,7 +362,7 @@ ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig,
360 goto err; 362 goto err;
361 } 363 }
362 /* calculate tmp1 = inv(S) mod order */ 364 /* calculate tmp1 = inv(S) mod order */
363 if (!BN_mod_inverse(u2, sig->s, order, ctx)) { 365 if (!BN_mod_inverse_ct(u2, sig->s, order, ctx)) {
364 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); 366 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
365 goto err; 367 goto err;
366 } 368 }
diff --git a/src/lib/libcrypto/gost/gostr341001.c b/src/lib/libcrypto/gost/gostr341001.c
index 5fb494009c..39749394af 100644
--- a/src/lib/libcrypto/gost/gostr341001.c
+++ b/src/lib/libcrypto/gost/gostr341001.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gostr341001.c,v 1.5 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: gostr341001.c,v 1.6 2017/01/21 11:00:47 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> 3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4 * Copyright (c) 2005-2006 Cryptocom LTD 4 * Copyright (c) 2005-2006 Cryptocom LTD
@@ -294,7 +294,7 @@ gost2001_do_verify(BIGNUM *md, ECDSA_SIG *sig, GOST_KEY *ec)
294 goto err; 294 goto err;
295 if (BN_is_zero(e)) 295 if (BN_is_zero(e))
296 BN_one(e); 296 BN_one(e);
297 if ((v = BN_mod_inverse(v, e, order, ctx)) == NULL) 297 if ((v = BN_mod_inverse_ct(v, e, order, ctx)) == NULL)
298 goto err; 298 goto err;
299 if (BN_mod_mul(z1, sig->s, v, order, ctx) == 0) 299 if (BN_mod_mul(z1, sig->s, v, order, ctx) == 0)
300 goto err; 300 goto err;
diff --git a/src/lib/libcrypto/rsa/rsa_chk.c b/src/lib/libcrypto/rsa/rsa_chk.c
index efe9431f2d..91616d17cb 100644
--- a/src/lib/libcrypto/rsa/rsa_chk.c
+++ b/src/lib/libcrypto/rsa/rsa_chk.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_chk.c,v 1.10 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: rsa_chk.c,v 1.11 2017/01/21 11:00:47 beck Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -191,7 +191,7 @@ RSA_check_key(const RSA *key)
191 } 191 }
192 192
193 /* iqmp = q^-1 mod p? */ 193 /* iqmp = q^-1 mod p? */
194 if (!BN_mod_inverse(i, key->q, key->p, ctx)) { 194 if (!BN_mod_inverse_ct(i, key->q, key->p, ctx)) {
195 ret = -1; 195 ret = -1;
196 goto err; 196 goto err;
197 } 197 }
diff --git a/src/lib/libcrypto/rsa/rsa_crpt.c b/src/lib/libcrypto/rsa/rsa_crpt.c
index ccb677c12b..8063a83263 100644
--- a/src/lib/libcrypto/rsa/rsa_crpt.c
+++ b/src/lib/libcrypto/rsa/rsa_crpt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_crpt.c,v 1.16 2016/07/07 11:53:12 bcook Exp $ */ 1/* $OpenBSD: rsa_crpt.c,v 1.17 2017/01/21 11:00:47 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -66,6 +66,8 @@
66#include <openssl/lhash.h> 66#include <openssl/lhash.h>
67#include <openssl/rsa.h> 67#include <openssl/rsa.h>
68 68
69#include "bn_lcl.h"
70
69#ifndef OPENSSL_NO_ENGINE 71#ifndef OPENSSL_NO_ENGINE
70#include <openssl/engine.h> 72#include <openssl/engine.h>
71#endif 73#endif
@@ -160,7 +162,7 @@ rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q,
160 if (!BN_mul(r0, r1, r2, ctx)) 162 if (!BN_mul(r0, r1, r2, ctx))
161 goto err; 163 goto err;
162 164
163 ret = BN_mod_inverse(NULL, d, r0, ctx); 165 ret = BN_mod_inverse_ct(NULL, d, r0, ctx);
164err: 166err:
165 BN_CTX_end(ctx); 167 BN_CTX_end(ctx);
166 return ret; 168 return ret;
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c
index 817f177e96..300b292b7b 100644
--- a/src/lib/libcrypto/rsa/rsa_gen.c
+++ b/src/lib/libcrypto/rsa/rsa_gen.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_gen.c,v 1.19 2017/01/21 10:38:29 beck Exp $ */ 1/* $OpenBSD: rsa_gen.c,v 1.20 2017/01/21 11:00:47 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -197,7 +197,7 @@ rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
197 197
198 BN_with_flags(&pr0, r0, BN_FLG_CONSTTIME); 198 BN_with_flags(&pr0, r0, BN_FLG_CONSTTIME);
199 199
200 if (!BN_mod_inverse(rsa->d, rsa->e, &pr0, ctx)) /* d */ 200 if (!BN_mod_inverse_ct(rsa->d, rsa->e, &pr0, ctx)) /* d */
201 goto err; 201 goto err;
202 202
203 /* set up d for correct BN_FLG_CONSTTIME flag */ 203 /* set up d for correct BN_FLG_CONSTTIME flag */
@@ -213,7 +213,7 @@ rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb)
213 213
214 /* calculate inverse of q mod p */ 214 /* calculate inverse of q mod p */
215 BN_with_flags(&p, rsa->p, BN_FLG_CONSTTIME); 215 BN_with_flags(&p, rsa->p, BN_FLG_CONSTTIME);
216 if (!BN_mod_inverse(rsa->iqmp, rsa->q, &p, ctx)) 216 if (!BN_mod_inverse_ct(rsa->iqmp, rsa->q, &p, ctx))
217 goto err; 217 goto err;
218 218
219 ok = 1; 219 ok = 1;