diff options
| author | beck <> | 2017-01-21 11:00:47 +0000 |
|---|---|---|
| committer | beck <> | 2017-01-21 11:00:47 +0000 |
| commit | 48df727a3547375dae8622c34fb55bdf5ef2c44c (patch) | |
| tree | 7ad0f2808a2ac42a510bf2f280dba63a3dfc83e3 /src/lib/libcrypto/bn | |
| parent | 55a172a1ed5b0cd8f7de3628fcc2e56df6716d59 (diff) | |
| download | openbsd-48df727a3547375dae8622c34fb55bdf5ef2c44c.tar.gz openbsd-48df727a3547375dae8622c34fb55bdf5ef2c44c.tar.bz2 openbsd-48df727a3547375dae8622c34fb55bdf5ef2c44c.zip | |
Add ct and nonct versions of BN_mod_inverse for internal use
ok jsing@
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_gcd.c | 30 | ||||
| -rw-r--r-- | src/lib/libcrypto/bn/bn_lcl.h | 6 | ||||
| -rw-r--r-- | src/lib/libcrypto/bn/bn_mont.c | 8 | ||||
| -rw-r--r-- | src/lib/libcrypto/bn/bn_x931p.c | 8 |
6 files changed, 43 insertions, 17 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); | |||
| 454 | int BN_asc2bn(BIGNUM **a, const char *str); | 454 | int BN_asc2bn(BIGNUM **a, const char *str); |
| 455 | int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); | 455 | int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); |
| 456 | int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */ | 456 | int BN_kronecker(const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx); /* returns -2 for error */ |
| 457 | #ifndef LIBRESSL_INTERNAL | ||
| 457 | BIGNUM *BN_mod_inverse(BIGNUM *ret, | 458 | BIGNUM *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 | ||
| 459 | BIGNUM *BN_mod_sqrt(BIGNUM *ret, | 461 | BIGNUM *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: | |||
| 231 | static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, | 231 | static 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 | ||
| 234 | BIGNUM * | 234 | static BIGNUM * |
| 235 | BN_mod_inverse(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) | 235 | BN_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 | ||
| 526 | BIGNUM * | ||
| 527 | BN_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 | |||
| 534 | BIGNUM * | ||
| 535 | BN_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 | |||
| 540 | BIGNUM * | ||
| 541 | BN_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)) |
| 602 | BIGNUM *BN_mod_inverse_ct(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, | ||
| 603 | BN_CTX *ctx); | ||
| 604 | BIGNUM *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)) |
