diff options
author | beck <> | 2017-01-21 09:38:59 +0000 |
---|---|---|
committer | beck <> | 2017-01-21 09:38:59 +0000 |
commit | ba7dab5b77b1e4dd797dbe7a4c31b5f4cbea0cd7 (patch) | |
tree | a68beae7892dad13fd8d76ba1fc45e6570e3360b /src/lib | |
parent | 0c45e4e4d42eacefe309063241d5a7f6de6674e7 (diff) | |
download | openbsd-ba7dab5b77b1e4dd797dbe7a4c31b5f4cbea0cd7.tar.gz openbsd-ba7dab5b77b1e4dd797dbe7a4c31b5f4cbea0cd7.tar.bz2 openbsd-ba7dab5b77b1e4dd797dbe7a4c31b5f4cbea0cd7.zip |
Make explicit _ct and _nonct versions of bn_mod_exp funcitons that
matter for constant time, and make the public interface only used
external to the library.
This moves us to a model where the important things are constant time
versions unless you ask for them not to be, rather than the opposite.
I'll continue with this method by method.
Add regress tests for same.
ok jsing@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/Makefile | 5 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn.h | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_blind.c | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_exp.c | 66 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_lcl.h | 12 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_prime.c | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/bn/bn_sqrt.c | 10 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_key.c | 6 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_ameth.c | 6 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_gen.c | 5 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_key.c | 5 | ||||
-rw-r--r-- | src/lib/libcrypto/dsa/dsa_ossl.c | 6 | ||||
-rw-r--r-- | src/lib/libcrypto/pem/pvkfmt.c | 6 | ||||
-rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 6 |
14 files changed, 106 insertions, 39 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile index 29b6669a12..3fb904b470 100644 --- a/src/lib/libcrypto/Makefile +++ b/src/lib/libcrypto/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.13 2016/12/21 15:49:29 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.14 2017/01/21 09:38:58 beck Exp $ |
2 | 2 | ||
3 | LIB= crypto | 3 | LIB= crypto |
4 | 4 | ||
@@ -24,7 +24,8 @@ CFLAGS+= -DLIBRESSL_INTERNAL | |||
24 | CFLAGS+= -DOPENSSL_NO_HW_PADLOCK # XXX enable this? | 24 | CFLAGS+= -DOPENSSL_NO_HW_PADLOCK # XXX enable this? |
25 | 25 | ||
26 | CFLAGS+= -I${LCRYPTO_SRC} | 26 | CFLAGS+= -I${LCRYPTO_SRC} |
27 | CFLAGS+= -I${LCRYPTO_SRC}/modes -I${LCRYPTO_SRC}/asn1 -I${LCRYPTO_SRC}/evp | 27 | CFLAGS+= -I${LCRYPTO_SRC}/asn1 -I${LCRYPTO_SRC}/bn -I${LCRYPTO_SRC}/evp |
28 | CFLAGS+= -I${LCRYPTO_SRC}/modes | ||
28 | 29 | ||
29 | VERSION_SCRIPT= Symbols.map | 30 | VERSION_SCRIPT= Symbols.map |
30 | SYMBOL_LIST= ${.CURDIR}/Symbols.list | 31 | SYMBOL_LIST= ${.CURDIR}/Symbols.list |
diff --git a/src/lib/libcrypto/bn/bn.h b/src/lib/libcrypto/bn/bn.h index 5f8278faa8..16ba8ae981 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.32 2016/12/21 15:49:29 jsing Exp $ */ | 1 | /* $OpenBSD: bn.h,v 1.33 2017/01/21 09:38:58 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 | * |
@@ -418,10 +418,12 @@ int BN_lshift(BIGNUM *r, const BIGNUM *a, int n); | |||
418 | int BN_lshift1(BIGNUM *r, const BIGNUM *a); | 418 | int BN_lshift1(BIGNUM *r, const BIGNUM *a); |
419 | int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); | 419 | int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); |
420 | 420 | ||
421 | #ifndef LIBRESSL_INTERNAL | ||
421 | int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 422 | int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
422 | const BIGNUM *m, BN_CTX *ctx); | 423 | const BIGNUM *m, BN_CTX *ctx); |
423 | int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 424 | int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
424 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | 425 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); |
426 | #endif | ||
425 | int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, | 427 | int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, |
426 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont); | 428 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont); |
427 | int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, | 429 | int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p, |
diff --git a/src/lib/libcrypto/bn/bn_blind.c b/src/lib/libcrypto/bn/bn_blind.c index c842f76c6f..01874f6208 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.14 2014/07/12 16:03:36 miod Exp $ */ | 1 | /* $OpenBSD: bn_blind.c,v 1.15 2017/01/21 09:38:58 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 | * |
@@ -372,7 +372,7 @@ BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, | |||
372 | ctx, ret->m_ctx)) | 372 | ctx, ret->m_ctx)) |
373 | goto err; | 373 | goto err; |
374 | } else { | 374 | } else { |
375 | if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx)) | 375 | if (!BN_mod_exp_ct(ret->A, ret->A, ret->e, ret->mod, ctx)) |
376 | goto err; | 376 | goto err; |
377 | } | 377 | } |
378 | 378 | ||
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c index 83c62be25a..ed4bc666bf 100644 --- a/src/lib/libcrypto/bn/bn_exp.c +++ b/src/lib/libcrypto/bn/bn_exp.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_exp.c,v 1.27 2017/01/21 04:34:16 beck Exp $ */ | 1 | /* $OpenBSD: bn_exp.c,v 1.28 2017/01/21 09:38:58 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 | * |
@@ -172,9 +172,9 @@ err: | |||
172 | return (ret); | 172 | return (ret); |
173 | } | 173 | } |
174 | 174 | ||
175 | int | 175 | static int |
176 | BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | 176 | BN_mod_exp_internal(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, |
177 | BN_CTX *ctx) | 177 | BN_CTX *ctx, int ct) |
178 | { | 178 | { |
179 | int ret; | 179 | int ret; |
180 | 180 | ||
@@ -213,12 +213,11 @@ BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
213 | */ | 213 | */ |
214 | 214 | ||
215 | if (BN_is_odd(m)) { | 215 | if (BN_is_odd(m)) { |
216 | if (a->top == 1 && !a->neg && | 216 | if (a->top == 1 && !a->neg && !ct) { |
217 | (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) { | ||
218 | BN_ULONG A = a->d[0]; | 217 | BN_ULONG A = a->d[0]; |
219 | ret = BN_mod_exp_mont_word(r, A,p, m,ctx, NULL); | 218 | ret = BN_mod_exp_mont_word(r, A,p, m,ctx, NULL); |
220 | } else | 219 | } else |
221 | ret = BN_mod_exp_mont(r, a,p, m,ctx, NULL); | 220 | ret = BN_mod_exp_mont_ct(r, a,p, m,ctx, NULL); |
222 | } else { | 221 | } else { |
223 | ret = BN_mod_exp_recp(r, a,p, m, ctx); | 222 | ret = BN_mod_exp_recp(r, a,p, m, ctx); |
224 | } | 223 | } |
@@ -228,6 +227,30 @@ BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
228 | } | 227 | } |
229 | 228 | ||
230 | int | 229 | int |
230 | BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | ||
231 | BN_CTX *ctx) | ||
232 | { | ||
233 | return BN_mod_exp_internal(r, a, p, m, ctx, | ||
234 | (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)); | ||
235 | } | ||
236 | |||
237 | int | ||
238 | BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | ||
239 | BN_CTX *ctx) | ||
240 | { | ||
241 | return BN_mod_exp_internal(r, a, p, m, ctx, 1); | ||
242 | } | ||
243 | |||
244 | |||
245 | int | ||
246 | BN_mod_exp_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | ||
247 | BN_CTX *ctx) | ||
248 | { | ||
249 | return BN_mod_exp_internal(r, a, p, m, ctx, 0); | ||
250 | } | ||
251 | |||
252 | |||
253 | int | ||
231 | BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | 254 | BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, |
232 | BN_CTX *ctx) | 255 | BN_CTX *ctx) |
233 | { | 256 | { |
@@ -361,9 +384,9 @@ err: | |||
361 | return (ret); | 384 | return (ret); |
362 | } | 385 | } |
363 | 386 | ||
364 | int | 387 | static int |
365 | BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | 388 | BN_mod_exp_mont_internal(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, |
366 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | 389 | BN_CTX *ctx, BN_MONT_CTX *in_mont, int ct) |
367 | { | 390 | { |
368 | int i, j, bits, ret = 0, wstart, wend, window, wvalue; | 391 | int i, j, bits, ret = 0, wstart, wend, window, wvalue; |
369 | int start = 1; | 392 | int start = 1; |
@@ -373,7 +396,7 @@ BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | |||
373 | BIGNUM *val[TABLE_SIZE]; | 396 | BIGNUM *val[TABLE_SIZE]; |
374 | BN_MONT_CTX *mont = NULL; | 397 | BN_MONT_CTX *mont = NULL; |
375 | 398 | ||
376 | if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) { | 399 | if (ct) { |
377 | return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont); | 400 | return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont); |
378 | } | 401 | } |
379 | 402 | ||
@@ -513,6 +536,27 @@ err: | |||
513 | return (ret); | 536 | return (ret); |
514 | } | 537 | } |
515 | 538 | ||
539 | int | ||
540 | BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | ||
541 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
542 | { | ||
543 | return BN_mod_exp_mont_internal(rr, a, p, m, ctx, in_mont, | ||
544 | (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)); | ||
545 | } | ||
546 | |||
547 | int | ||
548 | BN_mod_exp_mont_ct(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | ||
549 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
550 | { | ||
551 | return BN_mod_exp_mont_internal(rr, a, p, m, ctx, in_mont, 1); | ||
552 | } | ||
553 | |||
554 | int | ||
555 | BN_mod_exp_mont_nonct(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, | ||
556 | BN_CTX *ctx, BN_MONT_CTX *in_mont) | ||
557 | { | ||
558 | return BN_mod_exp_mont_internal(rr, a, p, m, ctx, in_mont, 0); | ||
559 | } | ||
516 | 560 | ||
517 | /* BN_mod_exp_mont_consttime() stores the precomputed powers in a specific layout | 561 | /* BN_mod_exp_mont_consttime() stores the precomputed powers in a specific layout |
518 | * so that accessing any of these table values shows the same access pattern as far | 562 | * so that accessing any of these table values shows the same access pattern as far |
diff --git a/src/lib/libcrypto/bn/bn_lcl.h b/src/lib/libcrypto/bn/bn_lcl.h index ca130a63cb..f8ce4bdc51 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.23 2016/12/21 15:49:29 jsing Exp $ */ | 1 | /* $OpenBSD: bn_lcl.h,v 1.24 2017/01/21 09:38:58 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 | * |
@@ -584,6 +584,16 @@ BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, int | |||
584 | 584 | ||
585 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); | 585 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom); |
586 | 586 | ||
587 | /* Explicitly const time / non-const time versions for internal use */ | ||
588 | int BN_mod_exp_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
589 | const BIGNUM *m, BN_CTX *ctx); | ||
590 | int BN_mod_exp_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
591 | const BIGNUM *m, BN_CTX *ctx); | ||
592 | int BN_mod_exp_mont_ct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
593 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
594 | int BN_mod_exp_mont_nonct(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
595 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | ||
596 | |||
587 | __END_HIDDEN_DECLS | 597 | __END_HIDDEN_DECLS |
588 | 598 | ||
589 | #endif | 599 | #endif |
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c index fb39756de2..b2f32684e4 100644 --- a/src/lib/libcrypto/bn/bn_prime.c +++ b/src/lib/libcrypto/bn/bn_prime.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_prime.c,v 1.15 2016/07/05 02:54:35 bcook Exp $ */ | 1 | /* $OpenBSD: bn_prime.c,v 1.16 2017/01/21 09:38:58 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 | * |
@@ -369,7 +369,7 @@ static int | |||
369 | witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1, const BIGNUM *a1_odd, | 369 | witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1, const BIGNUM *a1_odd, |
370 | int k, BN_CTX *ctx, BN_MONT_CTX *mont) | 370 | int k, BN_CTX *ctx, BN_MONT_CTX *mont) |
371 | { | 371 | { |
372 | if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont)) | 372 | if (!BN_mod_exp_mont_ct(w, w, a1_odd, a, ctx, mont)) |
373 | /* w := w^a1_odd mod a */ | 373 | /* w := w^a1_odd mod a */ |
374 | return -1; | 374 | return -1; |
375 | if (BN_is_one(w)) | 375 | if (BN_is_one(w)) |
diff --git a/src/lib/libcrypto/bn/bn_sqrt.c b/src/lib/libcrypto/bn/bn_sqrt.c index e5231d2a95..5928dfc79d 100644 --- a/src/lib/libcrypto/bn/bn_sqrt.c +++ b/src/lib/libcrypto/bn/bn_sqrt.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_sqrt.c,v 1.7 2016/11/08 01:40:22 guenther Exp $ */ | 1 | /* $OpenBSD: bn_sqrt.c,v 1.8 2017/01/21 09:38:58 beck Exp $ */ |
2 | /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> | 2 | /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> |
3 | * and Bodo Moeller for the OpenSSL project. */ | 3 | * and Bodo Moeller for the OpenSSL project. */ |
4 | /* ==================================================================== | 4 | /* ==================================================================== |
@@ -149,7 +149,7 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | |||
149 | q->neg = 0; | 149 | q->neg = 0; |
150 | if (!BN_add_word(q, 1)) | 150 | if (!BN_add_word(q, 1)) |
151 | goto end; | 151 | goto end; |
152 | if (!BN_mod_exp(ret, A, q, p, ctx)) | 152 | if (!BN_mod_exp_ct(ret, A, q, p, ctx)) |
153 | goto end; | 153 | goto end; |
154 | err = 0; | 154 | err = 0; |
155 | goto vrfy; | 155 | goto vrfy; |
@@ -190,7 +190,7 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | |||
190 | if (!BN_rshift(q, p, 3)) | 190 | if (!BN_rshift(q, p, 3)) |
191 | goto end; | 191 | goto end; |
192 | q->neg = 0; | 192 | q->neg = 0; |
193 | if (!BN_mod_exp(b, t, q, p, ctx)) | 193 | if (!BN_mod_exp_ct(b, t, q, p, ctx)) |
194 | goto end; | 194 | goto end; |
195 | 195 | ||
196 | /* y := b^2 */ | 196 | /* y := b^2 */ |
@@ -272,7 +272,7 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | |||
272 | 272 | ||
273 | /* Now that we have some non-square, we can find an element | 273 | /* Now that we have some non-square, we can find an element |
274 | * of order 2^e by computing its q'th power. */ | 274 | * of order 2^e by computing its q'th power. */ |
275 | if (!BN_mod_exp(y, y, q, p, ctx)) | 275 | if (!BN_mod_exp_ct(y, y, q, p, ctx)) |
276 | goto end; | 276 | goto end; |
277 | if (BN_is_one(y)) { | 277 | if (BN_is_one(y)) { |
278 | BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); | 278 | BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); |
@@ -314,7 +314,7 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) | |||
314 | } else if (!BN_one(x)) | 314 | } else if (!BN_one(x)) |
315 | goto end; | 315 | goto end; |
316 | } else { | 316 | } else { |
317 | if (!BN_mod_exp(x, A, t, p, ctx)) | 317 | if (!BN_mod_exp_ct(x, A, t, p, ctx)) |
318 | goto end; | 318 | goto end; |
319 | if (BN_is_zero(x)) { | 319 | if (BN_is_zero(x)) { |
320 | /* special case: a == 0 (mod p) */ | 320 | /* special case: a == 0 (mod p) */ |
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c index 6eb1365bf6..5b365cdd06 100644 --- a/src/lib/libcrypto/dh/dh_key.c +++ b/src/lib/libcrypto/dh/dh_key.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dh_key.c,v 1.25 2016/07/07 11:53:12 bcook Exp $ */ | 1 | /* $OpenBSD: dh_key.c,v 1.26 2017/01/21 09:38:58 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 | * |
@@ -62,6 +62,8 @@ | |||
62 | #include <openssl/dh.h> | 62 | #include <openssl/dh.h> |
63 | #include <openssl/err.h> | 63 | #include <openssl/err.h> |
64 | 64 | ||
65 | #include "bn_lcl.h" | ||
66 | |||
65 | static int generate_key(DH *dh); | 67 | static int generate_key(DH *dh); |
66 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); | 68 | static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh); |
67 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a, | 69 | static int dh_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a, |
@@ -233,7 +235,7 @@ static int | |||
233 | dh_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 235 | dh_bn_mod_exp(const DH *dh, BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
234 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | 236 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) |
235 | { | 237 | { |
236 | return BN_mod_exp_mont(r, a, p, m, ctx, m_ctx); | 238 | return BN_mod_exp_mont_ct(r, a, p, m, ctx, m_ctx); |
237 | } | 239 | } |
238 | 240 | ||
239 | static int | 241 | static int |
diff --git a/src/lib/libcrypto/dsa/dsa_ameth.c b/src/lib/libcrypto/dsa/dsa_ameth.c index 3e434701aa..92ad02e187 100644 --- a/src/lib/libcrypto/dsa/dsa_ameth.c +++ b/src/lib/libcrypto/dsa/dsa_ameth.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_ameth.c,v 1.20 2016/10/19 16:49:11 jsing Exp $ */ | 1 | /* $OpenBSD: dsa_ameth.c,v 1.21 2017/01/21 09:38:59 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 2006. | 3 | * project 2006. |
4 | */ | 4 | */ |
@@ -66,8 +66,8 @@ | |||
66 | #include <openssl/err.h> | 66 | #include <openssl/err.h> |
67 | #include <openssl/x509.h> | 67 | #include <openssl/x509.h> |
68 | 68 | ||
69 | |||
70 | #include "asn1_locl.h" | 69 | #include "asn1_locl.h" |
70 | #include "bn_lcl.h" | ||
71 | 71 | ||
72 | static int | 72 | static int |
73 | dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | 73 | dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) |
@@ -224,7 +224,7 @@ dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) | |||
224 | goto dsaerr; | 224 | goto dsaerr; |
225 | } | 225 | } |
226 | 226 | ||
227 | if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { | 227 | if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { |
228 | DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR); | 228 | DSAerr(DSA_F_DSA_PRIV_DECODE,DSA_R_BN_ERROR); |
229 | goto dsaerr; | 229 | goto dsaerr; |
230 | } | 230 | } |
diff --git a/src/lib/libcrypto/dsa/dsa_gen.c b/src/lib/libcrypto/dsa/dsa_gen.c index 7ddb7fec5e..d627e5ae9c 100644 --- a/src/lib/libcrypto/dsa/dsa_gen.c +++ b/src/lib/libcrypto/dsa/dsa_gen.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_gen.c,v 1.22 2015/07/15 18:34:37 miod Exp $ */ | 1 | /* $OpenBSD: dsa_gen.c,v 1.23 2017/01/21 09:38:59 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 | * |
@@ -68,6 +68,7 @@ | |||
68 | #include <openssl/evp.h> | 68 | #include <openssl/evp.h> |
69 | #include <openssl/sha.h> | 69 | #include <openssl/sha.h> |
70 | 70 | ||
71 | #include "bn_lcl.h" | ||
71 | #include "dsa_locl.h" | 72 | #include "dsa_locl.h" |
72 | 73 | ||
73 | int | 74 | int |
@@ -315,7 +316,7 @@ end: | |||
315 | 316 | ||
316 | for (;;) { | 317 | for (;;) { |
317 | /* g=test^r0%p */ | 318 | /* g=test^r0%p */ |
318 | if (!BN_mod_exp_mont(g, test, r0, p, ctx, mont)) | 319 | if (!BN_mod_exp_mont_ct(g, test, r0, p, ctx, mont)) |
319 | goto err; | 320 | goto err; |
320 | if (!BN_is_one(g)) | 321 | if (!BN_is_one(g)) |
321 | break; | 322 | break; |
diff --git a/src/lib/libcrypto/dsa/dsa_key.c b/src/lib/libcrypto/dsa/dsa_key.c index fc4eb9c433..cb95e1369c 100644 --- a/src/lib/libcrypto/dsa/dsa_key.c +++ b/src/lib/libcrypto/dsa/dsa_key.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dsa_key.c,v 1.22 2016/06/30 02:02:06 bcook Exp $ */ | 1 | /* $OpenBSD: dsa_key.c,v 1.23 2017/01/21 09:38:59 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 | * |
@@ -65,6 +65,7 @@ | |||
65 | 65 | ||
66 | #include <openssl/bn.h> | 66 | #include <openssl/bn.h> |
67 | #include <openssl/dsa.h> | 67 | #include <openssl/dsa.h> |
68 | #include "bn_lcl.h" | ||
68 | 69 | ||
69 | static int dsa_builtin_keygen(DSA *dsa); | 70 | static int dsa_builtin_keygen(DSA *dsa); |
70 | 71 | ||
@@ -108,7 +109,7 @@ dsa_builtin_keygen(DSA *dsa) | |||
108 | 109 | ||
109 | BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME); | 110 | BN_with_flags(&prk, priv_key, BN_FLG_CONSTTIME); |
110 | 111 | ||
111 | if (!BN_mod_exp(pub_key, dsa->g, &prk, dsa->p, ctx)) | 112 | if (!BN_mod_exp_ct(pub_key, dsa->g, &prk, dsa->p, ctx)) |
112 | goto err; | 113 | goto err; |
113 | } | 114 | } |
114 | 115 | ||
diff --git a/src/lib/libcrypto/dsa/dsa_ossl.c b/src/lib/libcrypto/dsa/dsa_ossl.c index a28d3e9d1a..3f01a83a44 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.26 2016/06/21 04:16:53 bcook Exp $ */ | 1 | /* $OpenBSD: dsa_ossl.c,v 1.27 2017/01/21 09:38:59 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/err.h> | 66 | #include <openssl/err.h> |
67 | #include <openssl/sha.h> | 67 | #include <openssl/sha.h> |
68 | 68 | ||
69 | #include "bn_lcl.h" | ||
70 | |||
69 | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); | 71 | static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); |
70 | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, | 72 | static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, |
71 | BIGNUM **rp); | 73 | BIGNUM **rp); |
@@ -238,7 +240,7 @@ dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp) | |||
238 | dsa->method_mont_p)) | 240 | dsa->method_mont_p)) |
239 | goto err; | 241 | goto err; |
240 | } else { | 242 | } else { |
241 | if (!BN_mod_exp_mont(r, dsa->g, &k, dsa->p, ctx, dsa->method_mont_p)) | 243 | if (!BN_mod_exp_mont_ct(r, dsa->g, &k, dsa->p, ctx, dsa->method_mont_p)) |
242 | goto err; | 244 | goto err; |
243 | } | 245 | } |
244 | 246 | ||
diff --git a/src/lib/libcrypto/pem/pvkfmt.c b/src/lib/libcrypto/pem/pvkfmt.c index 7a9045396c..0a33a24b9f 100644 --- a/src/lib/libcrypto/pem/pvkfmt.c +++ b/src/lib/libcrypto/pem/pvkfmt.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: pvkfmt.c,v 1.16 2016/03/02 14:28:14 beck Exp $ */ | 1 | /* $OpenBSD: pvkfmt.c,v 1.17 2017/01/21 09:38:59 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 | */ |
@@ -73,6 +73,8 @@ | |||
73 | #include <openssl/dsa.h> | 73 | #include <openssl/dsa.h> |
74 | #include <openssl/rsa.h> | 74 | #include <openssl/rsa.h> |
75 | 75 | ||
76 | #include "bn_lcl.h" | ||
77 | |||
76 | /* Utility function: read a DWORD (4 byte unsigned integer) in little endian | 78 | /* Utility function: read a DWORD (4 byte unsigned integer) in little endian |
77 | * format | 79 | * format |
78 | */ | 80 | */ |
@@ -340,7 +342,7 @@ b2i_dss(const unsigned char **in, unsigned int length, unsigned int bitlen, | |||
340 | goto memerr; | 342 | goto memerr; |
341 | if (!(ctx = BN_CTX_new())) | 343 | if (!(ctx = BN_CTX_new())) |
342 | goto memerr; | 344 | goto memerr; |
343 | if (!BN_mod_exp(dsa->pub_key, dsa->g, | 345 | if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, |
344 | dsa->priv_key, dsa->p, ctx)) | 346 | dsa->priv_key, dsa->p, ctx)) |
345 | goto memerr; | 347 | goto memerr; |
346 | BN_CTX_free(ctx); | 348 | BN_CTX_free(ctx); |
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c index af76541c28..640ed9a0d6 100644 --- a/src/lib/libcrypto/rsa/rsa_eay.c +++ b/src/lib/libcrypto/rsa/rsa_eay.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: rsa_eay.c,v 1.43 2016/09/09 11:39:11 tb Exp $ */ | 1 | /* $OpenBSD: rsa_eay.c,v 1.44 2017/01/21 09:38:59 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 | * |
@@ -118,6 +118,8 @@ | |||
118 | #include <openssl/err.h> | 118 | #include <openssl/err.h> |
119 | #include <openssl/rsa.h> | 119 | #include <openssl/rsa.h> |
120 | 120 | ||
121 | #include "bn_lcl.h" | ||
122 | |||
121 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | 123 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, |
122 | unsigned char *to, RSA *rsa, int padding); | 124 | unsigned char *to, RSA *rsa, int padding); |
123 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | 125 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, |
@@ -137,7 +139,7 @@ static RSA_METHOD rsa_pkcs1_eay_meth = { | |||
137 | .rsa_priv_enc = RSA_eay_private_encrypt, /* signing */ | 139 | .rsa_priv_enc = RSA_eay_private_encrypt, /* signing */ |
138 | .rsa_priv_dec = RSA_eay_private_decrypt, | 140 | .rsa_priv_dec = RSA_eay_private_decrypt, |
139 | .rsa_mod_exp = RSA_eay_mod_exp, | 141 | .rsa_mod_exp = RSA_eay_mod_exp, |
140 | .bn_mod_exp = BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ | 142 | .bn_mod_exp = BN_mod_exp_mont_ct, /* XXX probably we should not use Montgomery if e == 3 */ |
141 | .init = RSA_eay_init, | 143 | .init = RSA_eay_init, |
142 | .finish = RSA_eay_finish, | 144 | .finish = RSA_eay_finish, |
143 | }; | 145 | }; |