Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
| 57 | #include <openssl/bn.h> |
| 58 | |
| 59 | #include <assert.h> |
| 60 | #include <string.h> |
| 61 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 62 | #include <openssl/err.h> |
| 63 | #include <openssl/mem.h> |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 64 | #include <openssl/type_check.h> |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 65 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 66 | #include "internal.h" |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 67 | #include "../../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 68 | |
| 69 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 70 | #define BN_MUL_RECURSIVE_SIZE_NORMAL 16 |
| 71 | #define BN_SQR_RECURSIVE_SIZE_NORMAL BN_MUL_RECURSIVE_SIZE_NORMAL |
| 72 | |
| 73 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 74 | static void bn_abs_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 75 | size_t num, BN_ULONG *tmp) { |
| 76 | BN_ULONG borrow = bn_sub_words(tmp, a, b, num); |
| 77 | bn_sub_words(r, b, a, num); |
| 78 | bn_select_words(r, 0 - borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, num); |
| 79 | } |
| 80 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 81 | static void bn_mul_normal(BN_ULONG *r, const BN_ULONG *a, size_t na, |
| 82 | const BN_ULONG *b, size_t nb) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 83 | if (na < nb) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 84 | size_t itmp = na; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 85 | na = nb; |
| 86 | nb = itmp; |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 87 | const BN_ULONG *ltmp = a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 88 | a = b; |
| 89 | b = ltmp; |
| 90 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 91 | BN_ULONG *rr = &(r[na]); |
| 92 | if (nb == 0) { |
| 93 | OPENSSL_memset(r, 0, na * sizeof(BN_ULONG)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 94 | return; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 95 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 96 | rr[0] = bn_mul_words(r, a, na, b[0]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 97 | |
| 98 | for (;;) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 99 | if (--nb == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 103 | if (--nb == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 107 | if (--nb == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 111 | if (--nb == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); |
| 115 | rr += 4; |
| 116 | r += 4; |
| 117 | b += 4; |
| 118 | } |
| 119 | } |
| 120 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 121 | #if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM) |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 122 | // Here follows specialised variants of bn_add_words() and bn_sub_words(). They |
| 123 | // have the property performing operations on arrays of different sizes. The |
| 124 | // sizes of those arrays is expressed through cl, which is the common length ( |
| 125 | // basicall, min(len(a),len(b)) ), and dl, which is the delta between the two |
| 126 | // lengths, calculated as len(a)-len(b). All lengths are the number of |
| 127 | // BN_ULONGs... For the operations that require a result array as parameter, |
| 128 | // it must have the length cl+abs(dl). These functions should probably end up |
| 129 | // in bn_asm.c as soon as there are assembler counterparts for the systems that |
| 130 | // use assembler files. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 131 | |
| 132 | static BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, |
| 133 | const BN_ULONG *b, int cl, int dl) { |
| 134 | BN_ULONG c, t; |
| 135 | |
| 136 | assert(cl >= 0); |
| 137 | c = bn_sub_words(r, a, b, cl); |
| 138 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 139 | if (dl == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 140 | return c; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 141 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 142 | |
| 143 | r += cl; |
| 144 | a += cl; |
| 145 | b += cl; |
| 146 | |
| 147 | if (dl < 0) { |
| 148 | for (;;) { |
| 149 | t = b[0]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 150 | r[0] = 0 - t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 151 | if (t != 0) { |
| 152 | c = 1; |
| 153 | } |
| 154 | if (++dl >= 0) { |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | t = b[1]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 159 | r[1] = 0 - t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 160 | if (t != 0) { |
| 161 | c = 1; |
| 162 | } |
| 163 | if (++dl >= 0) { |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | t = b[2]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 168 | r[2] = 0 - t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | if (t != 0) { |
| 170 | c = 1; |
| 171 | } |
| 172 | if (++dl >= 0) { |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | t = b[3]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 177 | r[3] = 0 - t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 178 | if (t != 0) { |
| 179 | c = 1; |
| 180 | } |
| 181 | if (++dl >= 0) { |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | b += 4; |
| 186 | r += 4; |
| 187 | } |
| 188 | } else { |
| 189 | int save_dl = dl; |
| 190 | while (c) { |
| 191 | t = a[0]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 192 | r[0] = t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 193 | if (t != 0) { |
| 194 | c = 0; |
| 195 | } |
| 196 | if (--dl <= 0) { |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | t = a[1]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 201 | r[1] = t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 202 | if (t != 0) { |
| 203 | c = 0; |
| 204 | } |
| 205 | if (--dl <= 0) { |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | t = a[2]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 210 | r[2] = t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 211 | if (t != 0) { |
| 212 | c = 0; |
| 213 | } |
| 214 | if (--dl <= 0) { |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | t = a[3]; |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 219 | r[3] = t - c; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | if (t != 0) { |
| 221 | c = 0; |
| 222 | } |
| 223 | if (--dl <= 0) { |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | save_dl = dl; |
| 228 | a += 4; |
| 229 | r += 4; |
| 230 | } |
| 231 | if (dl > 0) { |
| 232 | if (save_dl > dl) { |
| 233 | switch (save_dl - dl) { |
| 234 | case 1: |
| 235 | r[1] = a[1]; |
| 236 | if (--dl <= 0) { |
| 237 | break; |
| 238 | } |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 239 | OPENSSL_FALLTHROUGH; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | case 2: |
| 241 | r[2] = a[2]; |
| 242 | if (--dl <= 0) { |
| 243 | break; |
| 244 | } |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 245 | OPENSSL_FALLTHROUGH; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | case 3: |
| 247 | r[3] = a[3]; |
| 248 | if (--dl <= 0) { |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | a += 4; |
| 253 | r += 4; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (dl > 0) { |
| 258 | for (;;) { |
| 259 | r[0] = a[0]; |
| 260 | if (--dl <= 0) { |
| 261 | break; |
| 262 | } |
| 263 | r[1] = a[1]; |
| 264 | if (--dl <= 0) { |
| 265 | break; |
| 266 | } |
| 267 | r[2] = a[2]; |
| 268 | if (--dl <= 0) { |
| 269 | break; |
| 270 | } |
| 271 | r[3] = a[3]; |
| 272 | if (--dl <= 0) { |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | a += 4; |
| 277 | r += 4; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return c; |
| 283 | } |
| 284 | #else |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 285 | // On other platforms the function is defined in asm. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 286 | BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 287 | int cl, int dl); |
| 288 | #endif |
| 289 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 290 | // bn_abs_sub_part_words computes |r| = |a| - |b|, storing the absolute value |
| 291 | // and returning a mask of all ones if the result was negative and all zeros if |
| 292 | // the result was positive. |cl| and |dl| follow the |bn_sub_part_words| calling |
| 293 | // convention. |
| 294 | // |
| 295 | // TODO(davidben): Make this take |size_t|. The |cl| + |dl| calling convention |
| 296 | // is confusing. The trouble is 32-bit x86 implements |bn_sub_part_words| in |
| 297 | // assembly, but we can probably just delete it? |
| 298 | static BN_ULONG bn_abs_sub_part_words(BN_ULONG *r, const BN_ULONG *a, |
| 299 | const BN_ULONG *b, int cl, int dl, |
| 300 | BN_ULONG *tmp) { |
| 301 | BN_ULONG borrow = bn_sub_part_words(tmp, a, b, cl, dl); |
| 302 | bn_sub_part_words(r, b, a, cl, -dl); |
| 303 | int r_len = cl + (dl < 0 ? -dl : dl); |
| 304 | borrow = 0 - borrow; |
| 305 | bn_select_words(r, borrow, r /* tmp < 0 */, tmp /* tmp >= 0 */, r_len); |
| 306 | return borrow; |
| 307 | } |
| 308 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 309 | // Karatsuba recursive multiplication algorithm |
| 310 | // (cf. Knuth, The Art of Computer Programming, Vol. 2) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 311 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 312 | // bn_mul_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r| has |
| 313 | // length 2*|n2|, |a| has length |n2| + |dna|, |b| has length |n2| + |dnb|, and |
| 314 | // |t| has length 4*|n2|. |n2| must be a power of two. Finally, we must have |
| 315 | // -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dna| <= 0 and |
| 316 | // -|BN_MUL_RECURSIVE_SIZE_NORMAL|/2 <= |dnb| <= 0. |
| 317 | // |
| 318 | // TODO(davidben): Simplify and |size_t| the calling convention around lengths |
| 319 | // here. |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 320 | static void bn_mul_recursive(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 321 | int n2, int dna, int dnb, BN_ULONG *t) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 322 | // |n2| is a power of two. |
| 323 | assert(n2 != 0 && (n2 & (n2 - 1)) == 0); |
| 324 | // Check |dna| and |dnb| are in range. |
| 325 | assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dna && dna <= 0); |
| 326 | assert(-BN_MUL_RECURSIVE_SIZE_NORMAL/2 <= dnb && dnb <= 0); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 327 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 328 | // Only call bn_mul_comba 8 if n2 == 8 and the |
| 329 | // two arrays are complete [steve] |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 330 | if (n2 == 8 && dna == 0 && dnb == 0) { |
| 331 | bn_mul_comba8(r, a, b); |
| 332 | return; |
| 333 | } |
| 334 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 335 | // Else do normal multiply |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 336 | if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) { |
| 337 | bn_mul_normal(r, a, n2 + dna, b, n2 + dnb); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 338 | if (dna + dnb < 0) { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 339 | OPENSSL_memset(&r[2 * n2 + dna + dnb], 0, |
| 340 | sizeof(BN_ULONG) * -(dna + dnb)); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 341 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 345 | // Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|. |
| 346 | // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used |
| 347 | // for recursive calls. |
| 348 | // Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1 |
| 349 | // to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as: |
| 350 | // |
| 351 | // a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0 |
| 352 | // |
| 353 | // Note that we know |n| >= |BN_MUL_RECURSIVE_SIZE_NORMAL|/2 above, so |
| 354 | // |tna| and |tnb| are non-negative. |
| 355 | int n = n2 / 2, tna = n + dna, tnb = n + dnb; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 356 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 357 | // t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR |
| 358 | // their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1 |
| 359 | // themselves store the absolute value. |
| 360 | BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]); |
| 361 | neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]); |
| 362 | |
| 363 | // Compute: |
| 364 | // t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)| |
| 365 | // r0,r1 = a0 * b0 |
| 366 | // r2,r3 = a1 * b1 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 367 | if (n == 4 && dna == 0 && dnb == 0) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 368 | bn_mul_comba4(&t[n2], t, &t[n]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 369 | |
| 370 | bn_mul_comba4(r, a, b); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 371 | bn_mul_comba4(&r[n2], &a[n], &b[n]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 372 | } else if (n == 8 && dna == 0 && dnb == 0) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 373 | bn_mul_comba8(&t[n2], t, &t[n]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 374 | |
| 375 | bn_mul_comba8(r, a, b); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 376 | bn_mul_comba8(&r[n2], &a[n], &b[n]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 377 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 378 | BN_ULONG *p = &t[n2 * 2]; |
| 379 | bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 380 | bn_mul_recursive(r, a, b, n, 0, 0, p); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 381 | bn_mul_recursive(&r[n2], &a[n], &b[n], n, dna, dnb, p); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 384 | // t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1 |
| 385 | BN_ULONG c = bn_add_words(t, r, &r[n2], n2); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 386 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 387 | // t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0. |
| 388 | // The second term is stored as the absolute value, so we do this with a |
| 389 | // constant-time select. |
| 390 | BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2); |
| 391 | BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2); |
| 392 | bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2); |
| 393 | OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 394 | crypto_word_t_too_small); |
| 395 | c = constant_time_select_w(neg, c_neg, c_pos); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 396 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 397 | // We now have our three components. Add them together. |
| 398 | // r1,r2,c = r1,r2 + t2,t3,c |
| 399 | c += bn_add_words(&r[n], &r[n], &t[n2], n2); |
| 400 | |
| 401 | // Propagate the carry bit to the end. |
| 402 | for (int i = n + n2; i < n2 + n2; i++) { |
| 403 | BN_ULONG old = r[i]; |
| 404 | r[i] = old + c; |
| 405 | c = r[i] < old; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 408 | // The product should fit without carries. |
| 409 | assert(c == 0); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 412 | // bn_mul_part_recursive sets |r| to |a| * |b|, using |t| as scratch space. |r| |
| 413 | // has length 4*|n|, |a| has length |n| + |tna|, |b| has length |n| + |tnb|, and |
| 414 | // |t| has length 8*|n|. |n| must be a power of two. Additionally, we must have |
| 415 | // 0 <= tna < n and 0 <= tnb < n, and |tna| and |tnb| must differ by at most |
| 416 | // one. |
| 417 | // |
| 418 | // TODO(davidben): Make this take |size_t| and perhaps the actual lengths of |a| |
| 419 | // and |b|. |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 420 | static void bn_mul_part_recursive(BN_ULONG *r, const BN_ULONG *a, |
| 421 | const BN_ULONG *b, int n, int tna, int tnb, |
| 422 | BN_ULONG *t) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 423 | // |n| is a power of two. |
| 424 | assert(n != 0 && (n & (n - 1)) == 0); |
| 425 | // Check |tna| and |tnb| are in range. |
| 426 | assert(0 <= tna && tna < n); |
| 427 | assert(0 <= tnb && tnb < n); |
| 428 | assert(-1 <= tna - tnb && tna - tnb <= 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 429 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 430 | int n2 = n * 2; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 431 | if (n < 8) { |
| 432 | bn_mul_normal(r, a, n + tna, b, n + tnb); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 433 | OPENSSL_memset(r + n2 + tna + tnb, 0, n2 - tna - tnb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 437 | // Split |a| and |b| into a0,a1 and b0,b1, where a0 and b0 have size |n|. |a1| |
| 438 | // and |b1| have size |tna| and |tnb|, respectively. |
| 439 | // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used |
| 440 | // for recursive calls. |
| 441 | // Split |r| into r0,r1,r2,r3. We must contribute a0*b0 to r0,r1, a0*a1+b0*b1 |
| 442 | // to r1,r2, and a1*b1 to r2,r3. The middle term we will compute as: |
| 443 | // |
| 444 | // a0*a1 + b0*b1 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 445 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 446 | // t0 = a0 - a1 and t1 = b1 - b0. The result will be multiplied, so we XOR |
| 447 | // their sign masks, giving the sign of (a0 - a1)*(b1 - b0). t0 and t1 |
| 448 | // themselves store the absolute value. |
| 449 | BN_ULONG neg = bn_abs_sub_part_words(t, a, &a[n], tna, n - tna, &t[n2]); |
| 450 | neg ^= bn_abs_sub_part_words(&t[n], &b[n], b, tnb, tnb - n, &t[n2]); |
| 451 | |
| 452 | // Compute: |
| 453 | // t2,t3 = t0 * t1 = |(a0 - a1)*(b1 - b0)| |
| 454 | // r0,r1 = a0 * b0 |
| 455 | // r2,r3 = a1 * b1 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 456 | if (n == 8) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 457 | bn_mul_comba8(&t[n2], t, &t[n]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 458 | bn_mul_comba8(r, a, b); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 459 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 460 | bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb); |
| 461 | // |bn_mul_normal| only writes |tna| + |tna| words. Zero the rest. |
| 462 | OPENSSL_memset(&r[n2 + tna + tnb], 0, sizeof(BN_ULONG) * (n2 - tna - tnb)); |
| 463 | } else { |
| 464 | BN_ULONG *p = &t[n2 * 2]; |
| 465 | bn_mul_recursive(&t[n2], t, &t[n], n, 0, 0, p); |
| 466 | bn_mul_recursive(r, a, b, n, 0, 0, p); |
| 467 | |
| 468 | OPENSSL_memset(&r[n2], 0, sizeof(BN_ULONG) * n2); |
| 469 | if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL && |
| 470 | tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) { |
| 471 | bn_mul_normal(&r[n2], &a[n], tna, &b[n], tnb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 472 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 473 | int i = n; |
| 474 | for (;;) { |
| 475 | i /= 2; |
| 476 | if (i < tna || i < tnb) { |
| 477 | // E.g., n == 16, i == 8 and tna == 11. |tna| and |tnb| are within one |
| 478 | // of each other, so if |tna| is larger and tna > i, then we know |
| 479 | // tnb >= i, and this call is valid. |
| 480 | bn_mul_part_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p); |
| 481 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 482 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 483 | if (i == tna || i == tnb) { |
| 484 | // If there is only a bottom half to the number, just do it. We know |
| 485 | // the larger of |tna - i| and |tnb - i| is zero. The other is zero or |
| 486 | // -1 by because of |tna| and |tnb| differ by at most one. |
| 487 | bn_mul_recursive(&r[n2], &a[n], &b[n], i, tna - i, tnb - i, p); |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | // This loop will eventually terminate when |i| falls below |
| 492 | // |BN_MUL_RECURSIVE_SIZE_NORMAL| because we know one of |tna| and |tnb| |
| 493 | // exceeds that. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 498 | // t0,t1,c = r0,r1 + r2,r3 = a0*b0 + a1*b1 |
| 499 | BN_ULONG c = bn_add_words(t, r, &r[n2], n2); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 500 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 501 | // t2,t3,c = t0,t1,c + neg*t2,t3 = (a0 - a1)*(b1 - b0) + a1*b1 + a0*b0. |
| 502 | // The second term is stored as the absolute value, so we do this with a |
| 503 | // constant-time select. |
| 504 | BN_ULONG c_neg = c - bn_sub_words(&t[n2 * 2], t, &t[n2], n2); |
| 505 | BN_ULONG c_pos = c + bn_add_words(&t[n2], t, &t[n2], n2); |
| 506 | bn_select_words(&t[n2], neg, &t[n2 * 2], &t[n2], n2); |
| 507 | OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t), |
| 508 | crypto_word_t_too_small); |
| 509 | c = constant_time_select_w(neg, c_neg, c_pos); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 510 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 511 | // We now have our three components. Add them together. |
| 512 | // r1,r2,c = r1,r2 + t2,t3,c |
| 513 | c += bn_add_words(&r[n], &r[n], &t[n2], n2); |
| 514 | |
| 515 | // Propagate the carry bit to the end. |
| 516 | for (int i = n + n2; i < n2 + n2; i++) { |
| 517 | BN_ULONG old = r[i]; |
| 518 | r[i] = old + c; |
| 519 | c = r[i] < old; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 522 | // The product should fit without carries. |
| 523 | assert(c == 0); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 526 | // bn_mul_impl implements |BN_mul| and |bn_mul_fixed|. Note this function breaks |
| 527 | // |BIGNUM| invariants and may return a negative zero. This is handled by the |
| 528 | // callers. |
| 529 | static int bn_mul_impl(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 530 | BN_CTX *ctx) { |
| 531 | int al = a->width; |
| 532 | int bl = b->width; |
| 533 | if (al == 0 || bl == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 534 | BN_zero(r); |
| 535 | return 1; |
| 536 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 537 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 538 | int ret = 0; |
| 539 | BIGNUM *rr; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 540 | BN_CTX_start(ctx); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 541 | if (r == a || r == b) { |
| 542 | rr = BN_CTX_get(ctx); |
| 543 | if (r == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 544 | goto err; |
| 545 | } |
| 546 | } else { |
| 547 | rr = r; |
| 548 | } |
| 549 | rr->neg = a->neg ^ b->neg; |
| 550 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 551 | int i = al - bl; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 552 | if (i == 0) { |
| 553 | if (al == 8) { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 554 | if (!bn_wexpand(rr, 16)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 555 | goto err; |
| 556 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 557 | rr->width = 16; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 558 | bn_mul_comba8(rr->d, a->d, b->d); |
| 559 | goto end; |
| 560 | } |
| 561 | } |
| 562 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 563 | int top = al + bl; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 564 | static const int kMulNormalSize = 16; |
| 565 | if (al >= kMulNormalSize && bl >= kMulNormalSize) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 566 | if (-1 <= i && i <= 1) { |
| 567 | // Find the larger power of two less than or equal to the larger length. |
| 568 | int j; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 569 | if (i >= 0) { |
| 570 | j = BN_num_bits_word((BN_ULONG)al); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 571 | } else { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 572 | j = BN_num_bits_word((BN_ULONG)bl); |
| 573 | } |
| 574 | j = 1 << (j - 1); |
| 575 | assert(j <= al || j <= bl); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 576 | BIGNUM *t = BN_CTX_get(ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 577 | if (t == NULL) { |
| 578 | goto err; |
| 579 | } |
| 580 | if (al > j || bl > j) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 581 | // We know |al| and |bl| are at most one from each other, so if al > j, |
| 582 | // bl >= j, and vice versa. Thus we can use |bn_mul_part_recursive|. |
| 583 | assert(al >= j && bl >= j); |
| 584 | if (!bn_wexpand(t, j * 8) || |
| 585 | !bn_wexpand(rr, j * 4)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 586 | goto err; |
| 587 | } |
| 588 | bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d); |
| 589 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 590 | // al <= j && bl <= j. Additionally, we know j <= al or j <= bl, so one |
| 591 | // of al - j or bl - j is zero. The other, by the bound on |i| above, is |
| 592 | // zero or -1. Thus, we can use |bn_mul_recursive|. |
| 593 | if (!bn_wexpand(t, j * 4) || |
| 594 | !bn_wexpand(rr, j * 2)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 595 | goto err; |
| 596 | } |
| 597 | bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d); |
| 598 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 599 | rr->width = top; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 600 | goto end; |
| 601 | } |
| 602 | } |
| 603 | |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 604 | if (!bn_wexpand(rr, top)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 605 | goto err; |
| 606 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 607 | rr->width = top; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 608 | bn_mul_normal(rr->d, a->d, al, b->d, bl); |
| 609 | |
| 610 | end: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 611 | if (r != rr && !BN_copy(r, rr)) { |
| 612 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 613 | } |
| 614 | ret = 1; |
| 615 | |
| 616 | err: |
| 617 | BN_CTX_end(ctx); |
| 618 | return ret; |
| 619 | } |
| 620 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 621 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { |
| 622 | if (!bn_mul_impl(r, a, b, ctx)) { |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | // This additionally fixes any negative zeros created by |bn_mul_impl|. |
| 627 | bn_set_minimal_width(r); |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | int bn_mul_fixed(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { |
| 632 | // Prevent negative zeros. |
| 633 | if (a->neg || b->neg) { |
| 634 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | return bn_mul_impl(r, a, b, ctx); |
| 639 | } |
| 640 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 641 | int bn_mul_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a, |
| 642 | const BN_ULONG *b, size_t num_b) { |
| 643 | if (num_r != num_a + num_b) { |
| 644 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 645 | return 0; |
| 646 | } |
| 647 | // TODO(davidben): Should this call |bn_mul_comba4| too? |BN_mul| does not |
| 648 | // hit that code. |
| 649 | if (num_a == 8 && num_b == 8) { |
| 650 | bn_mul_comba8(r, a, b); |
| 651 | } else { |
| 652 | bn_mul_normal(r, a, num_a, b, num_b); |
| 653 | } |
| 654 | return 1; |
| 655 | } |
| 656 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 657 | // tmp must have 2*n words |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 658 | static void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, size_t n, |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 659 | BN_ULONG *tmp) { |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 660 | if (n == 0) { |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | size_t max = n * 2; |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 665 | const BN_ULONG *ap = a; |
| 666 | BN_ULONG *rp = r; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 667 | rp[0] = rp[max - 1] = 0; |
| 668 | rp++; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 669 | |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 670 | // Compute the contribution of a[i] * a[j] for all i < j. |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 671 | if (n > 1) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 672 | ap++; |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 673 | rp[n - 1] = bn_mul_words(rp, ap, n - 1, ap[-1]); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 674 | rp += 2; |
| 675 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 676 | if (n > 2) { |
| 677 | for (size_t i = n - 2; i > 0; i--) { |
| 678 | ap++; |
| 679 | rp[i] = bn_mul_add_words(rp, ap, i, ap[-1]); |
| 680 | rp += 2; |
| 681 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 684 | // The final result fits in |max| words, so none of the following operations |
| 685 | // will overflow. |
| 686 | |
| 687 | // Double |r|, giving the contribution of a[i] * a[j] for all i != j. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 688 | bn_add_words(r, r, r, max); |
| 689 | |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 690 | // Add in the contribution of a[i] * a[i] for all i. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 691 | bn_sqr_words(tmp, a, n); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 692 | bn_add_words(r, r, tmp, max); |
| 693 | } |
| 694 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 695 | // bn_sqr_recursive sets |r| to |a|^2, using |t| as scratch space. |r| has |
| 696 | // length 2*|n2|, |a| has length |n2|, and |t| has length 4*|n2|. |n2| must be |
| 697 | // a power of two. |
| 698 | static void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, size_t n2, |
Robert Sloan | d5c2215 | 2017-11-13 09:22:12 -0800 | [diff] [blame] | 699 | BN_ULONG *t) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 700 | // |n2| is a power of two. |
| 701 | assert(n2 != 0 && (n2 & (n2 - 1)) == 0); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 702 | |
| 703 | if (n2 == 4) { |
| 704 | bn_sqr_comba4(r, a); |
| 705 | return; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 706 | } |
| 707 | if (n2 == 8) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 708 | bn_sqr_comba8(r, a); |
| 709 | return; |
| 710 | } |
| 711 | if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { |
| 712 | bn_sqr_normal(r, a, n2, t); |
| 713 | return; |
| 714 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 715 | |
| 716 | // Split |a| into a0,a1, each of size |n|. |
| 717 | // Split |t| into t0,t1,t2,t3, each of size |n|, with the remaining 4*|n| used |
| 718 | // for recursive calls. |
| 719 | // Split |r| into r0,r1,r2,r3. We must contribute a0^2 to r0,r1, 2*a0*a1 to |
| 720 | // r1,r2, and a1^2 to r2,r3. |
| 721 | size_t n = n2 / 2; |
| 722 | BN_ULONG *t_recursive = &t[n2 * 2]; |
| 723 | |
| 724 | // t0 = |a0 - a1|. |
| 725 | bn_abs_sub_words(t, a, &a[n], n, &t[n]); |
| 726 | // t2,t3 = t0^2 = |a0 - a1|^2 = a0^2 - 2*a0*a1 + a1^2 |
| 727 | bn_sqr_recursive(&t[n2], t, n, t_recursive); |
| 728 | |
| 729 | // r0,r1 = a0^2 |
| 730 | bn_sqr_recursive(r, a, n, t_recursive); |
| 731 | |
| 732 | // r2,r3 = a1^2 |
| 733 | bn_sqr_recursive(&r[n2], &a[n], n, t_recursive); |
| 734 | |
| 735 | // t0,t1,c = r0,r1 + r2,r3 = a0^2 + a1^2 |
| 736 | BN_ULONG c = bn_add_words(t, r, &r[n2], n2); |
| 737 | // t2,t3,c = t0,t1,c - t2,t3 = 2*a0*a1 |
| 738 | c -= bn_sub_words(&t[n2], t, &t[n2], n2); |
| 739 | |
| 740 | // We now have our three components. Add them together. |
| 741 | // r1,r2,c = r1,r2 + t2,t3,c |
| 742 | c += bn_add_words(&r[n], &r[n], &t[n2], n2); |
| 743 | |
| 744 | // Propagate the carry bit to the end. |
| 745 | for (size_t i = n + n2; i < n2 + n2; i++) { |
| 746 | BN_ULONG old = r[i]; |
| 747 | r[i] = old + c; |
| 748 | c = r[i] < old; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 751 | // The square should fit without carries. |
| 752 | assert(c == 0); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | int BN_mul_word(BIGNUM *bn, BN_ULONG w) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 756 | if (!bn->width) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 757 | return 1; |
| 758 | } |
| 759 | |
| 760 | if (w == 0) { |
| 761 | BN_zero(bn); |
| 762 | return 1; |
| 763 | } |
| 764 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 765 | BN_ULONG ll = bn_mul_words(bn->d, bn->d, bn->width, w); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 766 | if (ll) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 767 | if (!bn_wexpand(bn, bn->width + 1)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 768 | return 0; |
| 769 | } |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 770 | bn->d[bn->width++] = ll; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | return 1; |
| 774 | } |
| 775 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 776 | int bn_sqr_fixed(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { |
| 777 | int al = a->width; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 778 | if (al <= 0) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 779 | r->width = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 780 | r->neg = 0; |
| 781 | return 1; |
| 782 | } |
| 783 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 784 | int ret = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 785 | BN_CTX_start(ctx); |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 786 | BIGNUM *rr = (a != r) ? r : BN_CTX_get(ctx); |
| 787 | BIGNUM *tmp = BN_CTX_get(ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 788 | if (!rr || !tmp) { |
| 789 | goto err; |
| 790 | } |
| 791 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 792 | int max = 2 * al; // Non-zero (from above) |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 793 | if (!bn_wexpand(rr, max)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 794 | goto err; |
| 795 | } |
| 796 | |
| 797 | if (al == 4) { |
| 798 | bn_sqr_comba4(rr->d, a->d); |
| 799 | } else if (al == 8) { |
| 800 | bn_sqr_comba8(rr->d, a->d); |
| 801 | } else { |
| 802 | if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) { |
| 803 | BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2]; |
| 804 | bn_sqr_normal(rr->d, a->d, al, t); |
| 805 | } else { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 806 | // If |al| is a power of two, we can use |bn_sqr_recursive|. |
| 807 | if (al != 0 && (al & (al - 1)) == 0) { |
| 808 | if (!bn_wexpand(tmp, al * 4)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 809 | goto err; |
| 810 | } |
| 811 | bn_sqr_recursive(rr->d, a->d, al, tmp->d); |
| 812 | } else { |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 813 | if (!bn_wexpand(tmp, max)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 814 | goto err; |
| 815 | } |
| 816 | bn_sqr_normal(rr->d, a->d, al, tmp->d); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | rr->neg = 0; |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 822 | rr->width = max; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 823 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 824 | if (rr != r && !BN_copy(r, rr)) { |
| 825 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 826 | } |
| 827 | ret = 1; |
| 828 | |
| 829 | err: |
| 830 | BN_CTX_end(ctx); |
| 831 | return ret; |
| 832 | } |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 833 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 834 | int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { |
| 835 | if (!bn_sqr_fixed(r, a, ctx)) { |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | bn_set_minimal_width(r); |
| 840 | return 1; |
| 841 | } |
| 842 | |
Robert Sloan | 99319a1 | 2017-11-27 10:32:46 -0800 | [diff] [blame] | 843 | int bn_sqr_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a, size_t num_a) { |
| 844 | if (num_r != 2 * num_a || num_a > BN_SMALL_MAX_WORDS) { |
| 845 | OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 846 | return 0; |
| 847 | } |
| 848 | if (num_a == 4) { |
| 849 | bn_sqr_comba4(r, a); |
| 850 | } else if (num_a == 8) { |
| 851 | bn_sqr_comba8(r, a); |
| 852 | } else { |
| 853 | BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS]; |
| 854 | bn_sqr_normal(r, a, num_a, tmp); |
| 855 | OPENSSL_cleanse(tmp, 2 * num_a * sizeof(BN_ULONG)); |
| 856 | } |
| 857 | return 1; |
| 858 | } |