| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 |
| 19 | #include <openssl/err.h> |
| 20 |
| 21 | #include "../fipsmodule/bn/internal.h" |
| 22 |
| 23 |
| 24 | int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { |
| 25 | bssl::BN_CTXScope scope(ctx); |
| 26 | BIGNUM *rr; |
| 27 | if (r == a || r == p) { |
| 28 | rr = BN_CTX_get(ctx); |
| 29 | } else { |
| 30 | rr = r; |
| 31 | } |
| 32 |
| 33 | BIGNUM *v = BN_CTX_get(ctx); |
| 34 | if (rr == NULL || v == NULL) { |
| 35 | return 0; |
| 36 | } |
| 37 |
| 38 | if (BN_copy(v, a) == NULL) { |
| 39 | return 0; |
| 40 | } |
| 41 | int bits = BN_num_bits(p); |
| 42 |
| 43 | if (BN_is_odd(p)) { |
| 44 | if (BN_copy(rr, a) == NULL) { |
| 45 | return 0; |
| 46 | } |
| 47 | } else { |
| 48 | if (!BN_one(rr)) { |
| 49 | return 0; |
| 50 | } |
| 51 | } |
| 52 |
| 53 | for (int i = 1; i < bits; i++) { |
| 54 | if (!BN_sqr(v, v, ctx)) { |
| 55 | return 0; |
| 56 | } |
| 57 | if (BN_is_bit_set(p, i)) { |
| 58 | if (!BN_mul(rr, rr, v, ctx)) { |
| 59 | return 0; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 |
| 64 | if (r != rr && !BN_copy(r, rr)) { |
| 65 | return 0; |
| 66 | } |
| 67 | return 1; |
| 68 | } |
| 69 |
| 70 | static int mod_exp_even(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
| 71 | const BIGNUM *m, BN_CTX *ctx) { |
| 72 | // No cryptographic operations require modular exponentiation with an even |
| 73 | // modulus. We support it for backwards compatibility with any applications |
| 74 | // that may have relied on the operation, but optimize for simplicity over |
| 75 | // performance with straightforward square-and-multiply routine. |
| 76 | int bits = BN_num_bits(p); |
| 77 | if (bits == 0) { |
| 78 | return BN_one(r); |
| 79 | } |
| 80 |
| 81 | // Make a copy of |a|, in case it aliases |r|. |
| 82 | bssl::BN_CTXScope scope(ctx); |
| 83 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 84 | if (tmp == nullptr || !BN_copy(tmp, a)) { |
| 85 | return 0; |
| 86 | } |
| 87 |
| 88 | assert(BN_is_bit_set(p, bits - 1)); |
| 89 | if (!BN_copy(r, tmp)) { |
| 90 | return 0; |
| 91 | } |
| 92 |
| 93 | for (int i = bits - 2; i >= 0; i--) { |
| 94 | if (!BN_mod_sqr(r, r, m, ctx) || |
| 95 | (BN_is_bit_set(p, i) && !BN_mod_mul(r, r, tmp, m, ctx))) { |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 |
| 100 | return 1; |
| 101 | } |
| 102 |
| 103 | int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, |
| 104 | BN_CTX *ctx) { |
| 105 | if (m->neg) { |
| 106 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 107 | return 0; |
| 108 | } |
| 109 | if (a->neg || BN_ucmp(a, m) >= 0) { |
| 110 | if (!BN_nnmod(r, a, m, ctx)) { |
| 111 | return 0; |
| 112 | } |
| 113 | a = r; |
| 114 | } |
| 115 |
| 116 | if (BN_is_odd(m)) { |
| 117 | return BN_mod_exp_mont(r, a, p, m, ctx, NULL); |
| 118 | } |
| 119 |
| 120 | return mod_exp_even(r, a, p, m, ctx); |
| 121 | } |
| 122 |
| 123 | int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, |
| 124 | const BIGNUM *m, BN_CTX *ctx, |
| 125 | const BN_MONT_CTX *mont) { |
| 126 | BIGNUM a_bignum; |
| 127 | BN_init(&a_bignum); |
| 128 |
| 129 | int ret = 0; |
| 130 |
| 131 | // BN_mod_exp_mont requires reduced inputs. |
| 132 | if (bn_minimal_width(m) == 1) { |
| 133 | a %= m->d[0]; |
| 134 | } |
| 135 |
| 136 | if (!BN_set_word(&a_bignum, a)) { |
| 137 | OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR); |
| 138 | goto err; |
| 139 | } |
| 140 |
| 141 | ret = BN_mod_exp_mont(rr, &a_bignum, p, m, ctx, mont); |
| 142 |
| 143 | err: |
| 144 | BN_free(&a_bignum); |
| 145 |
| 146 | return ret; |
| 147 | } |
| 148 |
| 149 | int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1, |
| 150 | const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, |
| 151 | BN_CTX *ctx, const BN_MONT_CTX *mont) { |
| 152 | BIGNUM tmp; |
| 153 | BN_init(&tmp); |
| 154 |
| 155 | int ret = 0; |
| 156 | BN_MONT_CTX *new_mont = NULL; |
| 157 |
| 158 | // Allocate a montgomery context if it was not supplied by the caller. |
| 159 | if (mont == NULL) { |
| 160 | new_mont = BN_MONT_CTX_new_for_modulus(m, ctx); |
| 161 | if (new_mont == NULL) { |
| 162 | goto err; |
| 163 | } |
| 164 | mont = new_mont; |
| 165 | } |
| 166 |
| 167 | // BN_mod_mul_montgomery removes one Montgomery factor, so passing one |
| 168 | // Montgomery-encoded and one non-Montgomery-encoded value gives a |
| 169 | // non-Montgomery-encoded result. |
| 170 | if (!BN_mod_exp_mont(rr, a1, p1, m, ctx, mont) || |
| 171 | !BN_mod_exp_mont(&tmp, a2, p2, m, ctx, mont) || |
| 172 | !BN_to_montgomery(rr, rr, mont, ctx) || |
| 173 | !BN_mod_mul_montgomery(rr, rr, &tmp, mont, ctx)) { |
| 174 | goto err; |
| 175 | } |
| 176 |
| 177 | ret = 1; |
| 178 |
| 179 | err: |
| 180 | BN_MONT_CTX_free(new_mont); |
| 181 | BN_free(&tmp); |
| 182 |
| 183 | return ret; |
| 184 | } |
| 185 |