| // |
| 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 <openssl/err.h> |
| 18 |
| 19 | #include "internal.h" |
| 20 |
| 21 |
| 22 | int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, |
| 23 | const BIGNUM *n, BN_CTX *ctx) { |
| 24 | *out_no_inverse = 0; |
| 25 |
| 26 | if (!BN_is_odd(n)) { |
| 27 | OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS); |
| 28 | return 0; |
| 29 | } |
| 30 |
| 31 | if (BN_is_negative(a) || BN_cmp(a, n) >= 0) { |
| 32 | OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED); |
| 33 | return 0; |
| 34 | } |
| 35 |
| 36 | BIGNUM *A, *B, *X, *Y; |
| 37 | int ret = 0; |
| 38 | int sign; |
| 39 |
| 40 | BN_CTX_start(ctx); |
| 41 | A = BN_CTX_get(ctx); |
| 42 | B = BN_CTX_get(ctx); |
| 43 | X = BN_CTX_get(ctx); |
| 44 | Y = BN_CTX_get(ctx); |
| 45 | BIGNUM *R = out; |
| 46 | if (Y == NULL) { |
| 47 | goto err; |
| 48 | } |
| 49 |
| 50 | BN_zero(Y); |
| 51 | if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) { |
| 52 | goto err; |
| 53 | } |
| 54 | A->neg = 0; |
| 55 | sign = -1; |
| 56 | // From B = a mod |n|, A = |n| it follows that |
| 57 | // |
| 58 | // 0 <= B < A, |
| 59 | // -sign*X*a == B (mod |n|), |
| 60 | // sign*Y*a == A (mod |n|). |
| 61 |
| 62 | // Binary inversion algorithm; requires odd modulus. This is faster than the |
| 63 | // general algorithm if the modulus is sufficiently small (about 400 .. 500 |
| 64 | // bits on 32-bit systems, but much more on 64-bit systems) |
| 65 | int shift; |
| 66 |
| 67 | while (!BN_is_zero(B)) { |
| 68 | // 0 < B < |n|, |
| 69 | // 0 < A <= |n|, |
| 70 | // (1) -sign*X*a == B (mod |n|), |
| 71 | // (2) sign*Y*a == A (mod |n|) |
| 72 |
| 73 | // Now divide B by the maximum possible power of two in the integers, |
| 74 | // and divide X by the same value mod |n|. |
| 75 | // When we're done, (1) still holds. |
| 76 | shift = 0; |
| 77 | while (!BN_is_bit_set(B, shift)) { |
| 78 | // note that 0 < B |
| 79 | shift++; |
| 80 |
| 81 | if (BN_is_odd(X)) { |
| 82 | if (!BN_uadd(X, X, n)) { |
| 83 | goto err; |
| 84 | } |
| 85 | } |
| 86 | // now X is even, so we can easily divide it by two |
| 87 | if (!BN_rshift1(X, X)) { |
| 88 | goto err; |
| 89 | } |
| 90 | } |
| 91 | if (shift > 0) { |
| 92 | if (!BN_rshift(B, B, shift)) { |
| 93 | goto err; |
| 94 | } |
| 95 | } |
| 96 |
| 97 | // Same for A and Y. Afterwards, (2) still holds. |
| 98 | shift = 0; |
| 99 | while (!BN_is_bit_set(A, shift)) { |
| 100 | // note that 0 < A |
| 101 | shift++; |
| 102 |
| 103 | if (BN_is_odd(Y)) { |
| 104 | if (!BN_uadd(Y, Y, n)) { |
| 105 | goto err; |
| 106 | } |
| 107 | } |
| 108 | // now Y is even |
| 109 | if (!BN_rshift1(Y, Y)) { |
| 110 | goto err; |
| 111 | } |
| 112 | } |
| 113 | if (shift > 0) { |
| 114 | if (!BN_rshift(A, A, shift)) { |
| 115 | goto err; |
| 116 | } |
| 117 | } |
| 118 |
| 119 | // We still have (1) and (2). |
| 120 | // Both A and B are odd. |
| 121 | // The following computations ensure that |
| 122 | // |
| 123 | // 0 <= B < |n|, |
| 124 | // 0 < A < |n|, |
| 125 | // (1) -sign*X*a == B (mod |n|), |
| 126 | // (2) sign*Y*a == A (mod |n|), |
| 127 | // |
| 128 | // and that either A or B is even in the next iteration. |
| 129 | if (BN_ucmp(B, A) >= 0) { |
| 130 | // -sign*(X + Y)*a == B - A (mod |n|) |
| 131 | if (!BN_uadd(X, X, Y)) { |
| 132 | goto err; |
| 133 | } |
| 134 | // NB: we could use BN_mod_add_quick(X, X, Y, n), but that |
| 135 | // actually makes the algorithm slower |
| 136 | if (!BN_usub(B, B, A)) { |
| 137 | goto err; |
| 138 | } |
| 139 | } else { |
| 140 | // sign*(X + Y)*a == A - B (mod |n|) |
| 141 | if (!BN_uadd(Y, Y, X)) { |
| 142 | goto err; |
| 143 | } |
| 144 | // as above, BN_mod_add_quick(Y, Y, X, n) would slow things down |
| 145 | if (!BN_usub(A, A, B)) { |
| 146 | goto err; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 |
| 151 | if (!BN_is_one(A)) { |
| 152 | *out_no_inverse = 1; |
| 153 | OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE); |
| 154 | goto err; |
| 155 | } |
| 156 |
| 157 | // The while loop (Euclid's algorithm) ends when |
| 158 | // A == gcd(a,n); |
| 159 | // we have |
| 160 | // sign*Y*a == A (mod |n|), |
| 161 | // where Y is non-negative. |
| 162 |
| 163 | if (sign < 0) { |
| 164 | if (!BN_sub(Y, n, Y)) { |
| 165 | goto err; |
| 166 | } |
| 167 | } |
| 168 | // Now Y*a == A (mod |n|). |
| 169 |
| 170 | // Y*a == 1 (mod |n|) |
| 171 | if (Y->neg || BN_ucmp(Y, n) >= 0) { |
| 172 | if (!BN_nnmod(Y, Y, n, ctx)) { |
| 173 | goto err; |
| 174 | } |
| 175 | } |
| 176 | if (!BN_copy(R, Y)) { |
| 177 | goto err; |
| 178 | } |
| 179 |
| 180 | ret = 1; |
| 181 |
| 182 | err: |
| 183 | BN_CTX_end(ctx); |
| 184 | return ret; |
| 185 | } |
| 186 |
| 187 | BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n, |
| 188 | BN_CTX *ctx) { |
| 189 | BIGNUM *new_out = NULL; |
| 190 | if (out == NULL) { |
| 191 | new_out = BN_new(); |
| 192 | if (new_out == NULL) { |
| 193 | return NULL; |
| 194 | } |
| 195 | out = new_out; |
| 196 | } |
| 197 |
| 198 | int ok = 0; |
| 199 | BIGNUM *a_reduced = NULL; |
| 200 | if (a->neg || BN_ucmp(a, n) >= 0) { |
| 201 | a_reduced = BN_dup(a); |
| 202 | if (a_reduced == NULL) { |
| 203 | goto err; |
| 204 | } |
| 205 | if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) { |
| 206 | goto err; |
| 207 | } |
| 208 | a = a_reduced; |
| 209 | } |
| 210 |
| 211 | int no_inverse; |
| 212 | if (!BN_is_odd(n)) { |
| 213 | if (!bn_mod_inverse_consttime(out, &no_inverse, a, n, ctx)) { |
| 214 | goto err; |
| 215 | } |
| 216 | } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) { |
| 217 | goto err; |
| 218 | } |
| 219 |
| 220 | ok = 1; |
| 221 |
| 222 | err: |
| 223 | if (!ok) { |
| 224 | BN_free(new_out); |
| 225 | out = NULL; |
| 226 | } |
| 227 | BN_free(a_reduced); |
| 228 | return out; |
| 229 | } |
| 230 |
| 231 | int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a, |
| 232 | const BN_MONT_CTX *mont, BN_CTX *ctx) { |
| 233 | *out_no_inverse = 0; |
| 234 |
| 235 | // |a| is secret, but it is required to be in range, so these comparisons may |
| 236 | // be leaked. |
| 237 | if (BN_is_negative(a) || |
| 238 | constant_time_declassify_int(BN_cmp(a, &mont->N) >= 0)) { |
| 239 | OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED); |
| 240 | return 0; |
| 241 | } |
| 242 |
| 243 | int ret = 0; |
| 244 | BIGNUM blinding_factor; |
| 245 | BN_init(&blinding_factor); |
| 246 |
| 247 | // |BN_mod_inverse_odd| is leaky, so generate a secret blinding factor and |
| 248 | // blind |a|. This works because (ar)^-1 * r = a^-1, supposing r is |
| 249 | // invertible. If r is not invertible, this function will fail. However, we |
| 250 | // only use this in RSA, where stumbling on an uninvertible element means |
| 251 | // stumbling on the key's factorization. That is, if this function fails, the |
| 252 | // RSA key was not actually a product of two large primes. |
| 253 | // |
| 254 | // TODO(crbug.com/boringssl/677): When the PRNG output is marked secret by |
| 255 | // default, the explicit |bn_secret| call can be removed. |
| 256 | if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N)) { |
| 257 | goto err; |
| 258 | } |
| 259 | bn_secret(&blinding_factor); |
| 260 | if (!BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx)) { |
| 261 | goto err; |
| 262 | } |
| 263 |
| 264 | // Once blinded, |out| is no longer secret, so it may be passed to a leaky |
| 265 | // mod inverse function. Note |blinding_factor| is secret, so |out| will be |
| 266 | // secret again after multiplying. |
| 267 | bn_declassify(out); |
| 268 | if (!BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) || |
| 269 | !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) { |
| 270 | goto err; |
| 271 | } |
| 272 |
| 273 | ret = 1; |
| 274 |
| 275 | err: |
| 276 | BN_free(&blinding_factor); |
| 277 | return ret; |
| 278 | } |
| 279 |
| 280 | int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p, |
| 281 | BN_CTX *ctx, const BN_MONT_CTX *mont_p) { |
| 282 | BN_CTX_start(ctx); |
| 283 | BIGNUM *p_minus_2 = BN_CTX_get(ctx); |
| 284 | int ok = p_minus_2 != NULL && BN_copy(p_minus_2, p) && |
| 285 | BN_sub_word(p_minus_2, 2) && |
| 286 | BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p); |
| 287 | BN_CTX_end(ctx); |
| 288 | return ok; |
| 289 | } |
| 290 |
| 291 | int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p, |
| 292 | BN_CTX *ctx, const BN_MONT_CTX *mont_p) { |
| 293 | BN_CTX_start(ctx); |
| 294 | BIGNUM *p_minus_2 = BN_CTX_get(ctx); |
| 295 | int ok = p_minus_2 != NULL && BN_copy(p_minus_2, p) && |
| 296 | BN_sub_word(p_minus_2, 2) && |
| 297 | BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p); |
| 298 | BN_CTX_end(ctx); |
| 299 | return ok; |
| 300 | } |
| 301 |