diff options
Diffstat (limited to 'src/lib/libcrypto/bn')
| -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 |
6 files changed, 78 insertions, 22 deletions
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) */ |
