| // |
| 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 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 |
| 24 | #include "../../internal.h" |
| 25 | #include "internal.h" |
| 26 |
| 27 |
| 28 | #define BN_SQR_STACK_WORDS 16 |
| 29 |
| 30 | static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na, |
| 31 | const BN_ULONG *b, size_t nb) { |
| 32 | if (na < nb) { |
| 33 | size_t itmp = na; |
| 34 | na = nb; |
| 35 | nb = itmp; |
| 36 | const BN_ULONG *ltmp = a; |
| 37 | a = b; |
| 38 | b = ltmp; |
| 39 | } |
| 40 | BN_ULONG *rr = &(r[na]); |
| 41 | if (nb == 0) { |
| 42 | OPENSSL_memset(r, 0, na * sizeof(BN_ULONG)); |
| 43 | return; |
| 44 | } |
| 45 | rr[0] = bn_mul_words(r, a, na, b[0]); |
| 46 |
| 47 | for (;;) { |
| 48 | if (--nb == 0) { |
| 49 | return; |
| 50 | } |
| 51 | rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); |
| 52 | if (--nb == 0) { |
| 53 | return; |
| 54 | } |
| 55 | rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); |
| 56 | if (--nb == 0) { |
| 57 | return; |
| 58 | } |
| 59 | rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); |
| 60 | if (--nb == 0) { |
| 61 | return; |
| 62 | } |
| 63 | rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); |
| 64 | rr += 4; |
| 65 | r += 4; |
| 66 | b += 4; |
| 67 | } |
| 68 | } |
| 69 |
| 70 | // bn_sub_part_words sets |r| to |a| - |b|. It returns the borrow bit, which is |
| 71 | // one if the operation underflowed and zero otherwise. |cl| is the common |
| 72 | // length, that is, the shorter of len(a) or len(b). |dl| is the delta length, |
| 73 | // that is, len(a) - len(b). |r|'s length matches the larger of |a| and |b|, or |
| 74 | // cl + abs(dl). |
| 75 | // |
| 76 | // TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention |
| 77 | // is confusing. |
| 78 | static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, |
| 79 | const BN_ULONG *b, int cl, int dl) { |
| 80 | assert(cl >= 0); |
| 81 | BN_ULONG borrow = bn_sub_words(r, a, b, cl); |
| 82 | if (dl == 0) { |
| 83 | return borrow; |
| 84 | } |
| 85 |
| 86 | r += cl; |
| 87 | a += cl; |
| 88 | b += cl; |
| 89 |
| 90 | if (dl < 0) { |
| 91 | // |a| is shorter than |b|. Complete the subtraction as if the excess words |
| 92 | // in |a| were zeros. |
| 93 | dl = -dl; |
| 94 | for (int i = 0; i < dl; i++) { |
| 95 | r[i] = CRYPTO_subc_w(0, b[i], borrow, &borrow); |
| 96 | } |
| 97 | } else { |
| 98 | // |b| is shorter than |a|. Complete the subtraction as if the excess words |
| 99 | // in |b| were zeros. |
| 100 | for (int i = 0; i < dl; i++) { |
| 101 | r[i] = CRYPTO_subc_w(a[i], 0, borrow, &borrow); |
| 102 | } |
| 103 | } |
| 104 |
| 105 | return borrow; |
| 106 | } |
| 107 |
| 108 | // bn_abs_sub_part_words computes |r| = |a| - |b|, storing the absolute value |
| 109 | // and returning a mask of all ones if the result was negative and all zeros if |
| 110 | // the result was positive. |cl| and |dl| follow the |bn_sub_part_words| calling |
| 111 | // convention. |
| 112 | // |
| 113 | // TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention |
| 114 | // is confusing. |
| 115 | // |
| 116 | // TODO(davidben): This function used to be used as part of a general Karatsuba |
| 117 | // multiplication implementation, which had to account for differently-sized |
| 118 | // inputs. Now it is only used as part of RSA key generation, which does not |
| 119 | // need all this. |
| 120 | static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a, |
| 121 | const BN_ULONG *b, int cl, int dl, |
| 122 | BN_ULONG *tmp) { |
| 123 | BN_ULONG borrow = bn_sub_part_words(tmp, a, b, cl, dl); |
| 124 | bn_sub_part_words(r, b, a, cl, -dl); |
| 125 | int r_len = cl + (dl < 0 ? -dl : dl); |
| 126 | borrow = 0 - borrow; |
| 127 | bn_select_words(r, borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, r_len); |
| 128 | return borrow; |
| 129 | } |
| 130 |
| 131 | int bn_abs_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 132 | BN_CTX *ctx) { |
| 133 | int cl = a->width < b->width ? a->width : b->width; |
| 134 | int dl = a->width - b->width; |
| 135 | int r_len = a->width < b->width ? b->width : a->width; |
| 136 | BN_CTX_start(ctx); |
| 137 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 138 | int ok = tmp != NULL && bn_wexpand(r, r_len) && bn_wexpand(tmp, r_len); |
| 139 | if (ok) { |
| 140 | bn_abs_sub_part_words(r->d, a->d, b->d, cl, dl, tmp->d); |
| 141 | r->width = r_len; |
| 142 | } |
| 143 | BN_CTX_end(ctx); |
| 144 | return ok; |
| 145 | } |
| 146 |
| 147 | // bn_mul_impl implements |BN_mul| and |bn_mul_consttime|. Note this function |
| 148 | // breaks |BIGNUM| invariants and may return a negative zero. This is handled by |
| 149 | // the callers. |
| 150 | static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 151 | BN_CTX *ctx) { |
| 152 | int al = a->width; |
| 153 | int bl = b->width; |
| 154 | if (al == 0 || bl == 0) { |
| 155 | BN_zero(r); |
| 156 | return 1; |
| 157 | } |
| 158 |
| 159 | int ret = 0, i, top; |
| 160 | BIGNUM *rr; |
| 161 | BN_CTX_start(ctx); |
| 162 | if (r == a || r == b) { |
| 163 | rr = BN_CTX_get(ctx); |
| 164 | if (rr == NULL) { |
| 165 | goto err; |
| 166 | } |
| 167 | } else { |
| 168 | rr = r; |
| 169 | } |
| 170 | rr->neg = a->neg ^ b->neg; |
| 171 |
| 172 | i = al - bl; |
| 173 | if (i == 0) { |
| 174 | if (al == 8) { |
| 175 | if (!bn_wexpand(rr, 16)) { |
| 176 | goto err; |
| 177 | } |
| 178 | rr->width = 16; |
| 179 | bn_mul_comba8(rr->d, a->d, b->d); |
| 180 | goto end; |
| 181 | } |
| 182 | } |
| 183 |
| 184 | top = al + bl; |
| 185 | if (!bn_wexpand(rr, top)) { |
| 186 | goto err; |
| 187 | } |
| 188 | rr->width = top; |
| 189 | bn_mul_normal(rr->d, a->d, al, b->d, bl); |
| 190 |
| 191 | end: |
| 192 | if (r != rr && !BN_copy(r, rr)) { |
| 193 | goto err; |
| 194 | } |
| 195 | ret = 1; |
| 196 |
| 197 | err: |
| 198 | BN_CTX_end(ctx); |
| 199 | return ret; |
| 200 | } |
| 201 |
| 202 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { |
| 203 | if (!bn_mul_impl(r, a, b, ctx)) { |
| 204 | return 0; |
| 205 | } |
| 206 |
| 207 | // This additionally fixes any negative zeros created by |bn_mul_impl|. |
| 208 | bn_set_minimal_width(r); |
| 209 | return 1; |
| 210 | } |
| 211 |
| 212 | int bn_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { |
| 213 | // Prevent negative zeros. |
| 214 | if (a->neg || b->neg) { |
| 215 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 216 | return 0; |
| 217 | } |
| 218 |
| 219 | return bn_mul_impl(r, a, b, ctx); |
| 220 | } |
| 221 |
| 222 | void bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a, |
| 223 | const BN_ULONG *b, size_t num_b) { |
| 224 | if (num_r != num_a + num_b) { |
| 225 | abort(); |
| 226 | } |
| 227 | // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not |
| 228 | // hit that code. |
| 229 | if (num_a == 8 && num_b == 8) { |
| 230 | bn_mul_comba8(r, a, b); |
| 231 | } else { |
| 232 | bn_mul_normal(r, a, num_a, b, num_b); |
| 233 | } |
| 234 | } |
| 235 |
| 236 | // tmp must have 2*n words |
| 237 | static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n, |
| 238 | BN_ULONG *tmp) { |
| 239 | if (n == 0) { |
| 240 | return; |
| 241 | } |
| 242 |
| 243 | size_t max = n * 2; |
| 244 | const BN_ULONG *ap = a; |
| 245 | BN_ULONG *rp = r; |
| 246 | rp[0] = rp[max - 1] = 0; |
| 247 | rp++; |
| 248 |
| 249 | // Compute the contribution of a[i] * a[j] for all i < j. |
| 250 | if (n > 1) { |
| 251 | ap++; |
| 252 | rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]); |
| 253 | rp += 2; |
| 254 | } |
| 255 | if (n > 2) { |
| 256 | for (size_t i = n - 2; i > 0; i--) { |
| 257 | ap++; |
| 258 | rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]); |
| 259 | rp += 2; |
| 260 | } |
| 261 | } |
| 262 |
| 263 | // The final result fits in |max| words, so none of the following operations |
| 264 | // will overflow. |
| 265 |
| 266 | // Double |r|, giving the contribution of a[i] * a[j] for all i != j. |
| 267 | bn_add_words(r, r, r, max); |
| 268 |
| 269 | // Add in the contribution of a[i] * a[i] for all i. |
| 270 | bn_sqr_words(tmp, a, n); |
| 271 | bn_add_words(r, r, tmp, max); |
| 272 | } |
| 273 |
| 274 | int BN_mul_word(BIGNUM *bn, BN_ULONG w) { |
| 275 | if (!bn->width) { |
| 276 | return 1; |
| 277 | } |
| 278 |
| 279 | if (w == 0) { |
| 280 | BN_zero(bn); |
| 281 | return 1; |
| 282 | } |
| 283 |
| 284 | BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->width, w); |
| 285 | if (ll) { |
| 286 | if (!bn_wexpand(bn, bn->width + 1)) { |
| 287 | return 0; |
| 288 | } |
| 289 | bn->d[bn->width++] = ll; |
| 290 | } |
| 291 |
| 292 | return 1; |
| 293 | } |
| 294 |
| 295 | int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { |
| 296 | int al = a->width; |
| 297 | if (al <= 0) { |
| 298 | r->width = 0; |
| 299 | r->neg = 0; |
| 300 | return 1; |
| 301 | } |
| 302 |
| 303 | int ret = 0, max; |
| 304 | BN_CTX_start(ctx); |
| 305 | BIGNUM *rr = (a != r) ? r : BN_CTX_get(ctx); |
| 306 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 307 | if (!rr || !tmp) { |
| 308 | goto err; |
| 309 | } |
| 310 |
| 311 | max = 2 * al; // Non-zero (from above) |
| 312 | if (!bn_wexpand(rr, max)) { |
| 313 | goto err; |
| 314 | } |
| 315 |
| 316 | if (al == 4) { |
| 317 | bn_sqr_comba4(rr->d, a->d); |
| 318 | } else if (al == 8) { |
| 319 | bn_sqr_comba8(rr->d, a->d); |
| 320 | } else { |
| 321 | if (al < BN_SQR_STACK_WORDS) { |
| 322 | BN_ULONG t[BN_SQR_STACK_WORDS * 2]; |
| 323 | bn_sqr_normal(rr->d, a->d, al, t); |
| 324 | } else { |
| 325 | if (!bn_wexpand(tmp, max)) { |
| 326 | goto err; |
| 327 | } |
| 328 | bn_sqr_normal(rr->d, a->d, al, tmp->d); |
| 329 | } |
| 330 | } |
| 331 |
| 332 | rr->neg = 0; |
| 333 | rr->width = max; |
| 334 |
| 335 | if (rr != r && !BN_copy(r, rr)) { |
| 336 | goto err; |
| 337 | } |
| 338 | ret = 1; |
| 339 |
| 340 | err: |
| 341 | BN_CTX_end(ctx); |
| 342 | return ret; |
| 343 | } |
| 344 |
| 345 | int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { |
| 346 | if (!bn_sqr_consttime(r, a, ctx)) { |
| 347 | return 0; |
| 348 | } |
| 349 |
| 350 | bn_set_minimal_width(r); |
| 351 | return 1; |
| 352 | } |
| 353 |
| 354 | void bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) { |
| 355 | if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) { |
| 356 | abort(); |
| 357 | } |
| 358 | if (num_a == 4) { |
| 359 | bn_sqr_comba4(r, a); |
| 360 | } else if (num_a == 8) { |
| 361 | bn_sqr_comba8(r, a); |
| 362 | } else { |
| 363 | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; |
| 364 | bn_sqr_normal(r, a, num_a, tmp); |
| 365 | OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG)); |
| 366 | } |
| 367 | } |
| 368 |