| 1 |
// Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
| 2 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/rsa.h> |
| 6 |
|
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/bn.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
|
| 13 |
#include "internal.h" |
| 14 |
#include "../../internal.h" |
| 15 |
|
| 16 |
|
| 17 |
#define BN_BLINDING_COUNTER 32 |
| 18 |
|
| 19 |
struct bn_blinding_st { |
| 20 |
BIGNUM *A; // The base blinding factor, Montgomery-encoded. |
| 21 |
BIGNUM *Ai; // The inverse of the blinding factor, Montgomery-encoded. |
| 22 |
unsigned counter; |
| 23 |
}; |
| 24 |
|
| 25 |
static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e, |
| 26 |
const BN_MONT_CTX *mont, BN_CTX *ctx); |
| 27 |
|
| 28 |
BN_BLINDING *BN_BLINDING_new(void) { |
| 29 |
BN_BLINDING *ret = OPENSSL_zalloc(sizeof(BN_BLINDING)); |
| 30 |
if (ret == NULL) { |
| 31 |
return NULL; |
| 32 |
} |
| 33 |
|
| 34 |
ret->A = BN_new(); |
| 35 |
if (ret->A == NULL) { |
| 36 |
goto err; |
| 37 |
} |
| 38 |
|
| 39 |
ret->Ai = BN_new(); |
| 40 |
if (ret->Ai == NULL) { |
| 41 |
goto err; |
| 42 |
} |
| 43 |
|
| 44 |
// The blinding values need to be created before this blinding can be used. |
| 45 |
ret->counter = BN_BLINDING_COUNTER - 1; |
| 46 |
|
| 47 |
return ret; |
| 48 |
|
| 49 |
err: |
| 50 |
BN_BLINDING_free(ret); |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
|
| 54 |
void BN_BLINDING_free(BN_BLINDING *r) { |
| 55 |
if (r == NULL) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
BN_free(r->A); |
| 60 |
BN_free(r->Ai); |
| 61 |
OPENSSL_free(r); |
| 62 |
} |
| 63 |
|
| 64 |
void BN_BLINDING_invalidate(BN_BLINDING *b) { |
| 65 |
b->counter = BN_BLINDING_COUNTER - 1; |
| 66 |
} |
| 67 |
|
| 68 |
static int bn_blinding_update(BN_BLINDING *b, const BIGNUM *e, |
| 69 |
const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 70 |
if (++b->counter == BN_BLINDING_COUNTER) { |
| 71 |
// re-create blinding parameters |
| 72 |
if (!bn_blinding_create_param(b, e, mont, ctx)) { |
| 73 |
goto err; |
| 74 |
} |
| 75 |
b->counter = 0; |
| 76 |
} else { |
| 77 |
if (!BN_mod_mul_montgomery(b->A, b->A, b->A, mont, ctx) || |
| 78 |
!BN_mod_mul_montgomery(b->Ai, b->Ai, b->Ai, mont, ctx)) { |
| 79 |
goto err; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return 1; |
| 84 |
|
| 85 |
err: |
| 86 |
// |A| and |Ai| may be in an inconsistent state so they both need to be |
| 87 |
// replaced the next time this blinding is used. Note that this is only |
| 88 |
// sufficient because support for |BN_BLINDING_NO_UPDATE| and |
| 89 |
// |BN_BLINDING_NO_RECREATE| was previously dropped. |
| 90 |
b->counter = BN_BLINDING_COUNTER - 1; |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, const BIGNUM *e, |
| 96 |
const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 97 |
// |n| is not Montgomery-encoded and |b->A| is. |BN_mod_mul_montgomery| |
| 98 |
// cancels one Montgomery factor, so the resulting value of |n| is unencoded. |
| 99 |
if (!bn_blinding_update(b, e, mont, ctx) || |
| 100 |
!BN_mod_mul_montgomery(n, n, b->A, mont, ctx)) { |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_MONT_CTX *mont, |
| 108 |
BN_CTX *ctx) { |
| 109 |
// |n| is not Montgomery-encoded and |b->A| is. |BN_mod_mul_montgomery| |
| 110 |
// cancels one Montgomery factor, so the resulting value of |n| is unencoded. |
| 111 |
return BN_mod_mul_montgomery(n, n, b->Ai, mont, ctx); |
| 112 |
} |
| 113 |
|
| 114 |
static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e, |
| 115 |
const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 116 |
int no_inverse; |
| 117 |
if (!BN_rand_range_ex(b->A, 1, &mont->N) || |
| 118 |
// Compute |b->A|^-1 in Montgomery form. Note |BN_from_montgomery| + |
| 119 |
// |BN_mod_inverse_blinded| is equivalent to, but more efficient than, |
| 120 |
// |BN_mod_inverse_blinded| + |BN_to_montgomery|. |
| 121 |
// |
| 122 |
// We do not retry if |b->A| has no inverse. Finding a non-invertible |
| 123 |
// value of |b->A| is equivalent to factoring |mont->N|. There is |
| 124 |
// negligible probability of stumbling on one at random. |
| 125 |
!BN_from_montgomery(b->Ai, b->A, mont, ctx) || |
| 126 |
!BN_mod_inverse_blinded(b->Ai, &no_inverse, b->Ai, mont, ctx) || |
| 127 |
// TODO(davidben): |BN_mod_exp_mont| internally computes the result in |
| 128 |
// Montgomery form. Save a pair of Montgomery reductions and a |
| 129 |
// multiplication by returning that value directly. |
| 130 |
!BN_mod_exp_mont(b->A, b->A, e, &mont->N, ctx, mont) || |
| 131 |
!BN_to_montgomery(b->A, b->A, mont, ctx)) { |
| 132 |
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
return 1; |
| 137 |
} |