bn_mul.c revision 1.37
1/* $OpenBSD: bn_mul.c,v 1.37 2023/04/19 10:51:22 jsing Exp $ */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59#include <assert.h> 60#include <stdio.h> 61#include <string.h> 62 63#include <openssl/opensslconf.h> 64 65#include "bn_arch.h" 66#include "bn_internal.h" 67#include "bn_local.h" 68 69/* 70 * bn_mul_comba4() computes r[] = a[] * b[] using Comba multiplication 71 * (https://everything2.com/title/Comba+multiplication), where a and b are both 72 * four word arrays, producing an eight word array result. 73 */ 74#ifndef HAVE_BN_MUL_COMBA4 75void 76bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) 77{ 78 BN_ULONG c0, c1, c2; 79 80 bn_mulw_addtw(a[0], b[0], 0, 0, 0, &c2, &c1, &r[0]); 81 82 bn_mulw_addtw(a[0], b[1], 0, c2, c1, &c2, &c1, &c0); 83 bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]); 84 85 bn_mulw_addtw(a[2], b[0], 0, c2, c1, &c2, &c1, &c0); 86 bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0); 87 bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]); 88 89 bn_mulw_addtw(a[0], b[3], 0, c2, c1, &c2, &c1, &c0); 90 bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0); 91 bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0); 92 bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]); 93 94 bn_mulw_addtw(a[3], b[1], 0, c2, c1, &c2, &c1, &c0); 95 bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0); 96 bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &r[4]); 97 98 bn_mulw_addtw(a[2], b[3], 0, c2, c1, &c2, &c1, &c0); 99 bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &r[5]); 100 101 bn_mulw_addtw(a[3], b[3], 0, c2, c1, &c2, &r[7], &r[6]); 102} 103#endif 104 105/* 106 * bn_mul_comba8() computes r[] = a[] * b[] using Comba multiplication 107 * (https://everything2.com/title/Comba+multiplication), where a and b are both 108 * eight word arrays, producing a 16 word array result. 109 */ 110#ifndef HAVE_BN_MUL_COMBA8 111void 112bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) 113{ 114 BN_ULONG c0, c1, c2; 115 116 bn_mulw_addtw(a[0], b[0], 0, 0, 0, &c2, &c1, &r[0]); 117 118 bn_mulw_addtw(a[0], b[1], 0, c2, c1, &c2, &c1, &c0); 119 bn_mulw_addtw(a[1], b[0], c2, c1, c0, &c2, &c1, &r[1]); 120 121 bn_mulw_addtw(a[2], b[0], 0, c2, c1, &c2, &c1, &c0); 122 bn_mulw_addtw(a[1], b[1], c2, c1, c0, &c2, &c1, &c0); 123 bn_mulw_addtw(a[0], b[2], c2, c1, c0, &c2, &c1, &r[2]); 124 125 bn_mulw_addtw(a[0], b[3], 0, c2, c1, &c2, &c1, &c0); 126 bn_mulw_addtw(a[1], b[2], c2, c1, c0, &c2, &c1, &c0); 127 bn_mulw_addtw(a[2], b[1], c2, c1, c0, &c2, &c1, &c0); 128 bn_mulw_addtw(a[3], b[0], c2, c1, c0, &c2, &c1, &r[3]); 129 130 bn_mulw_addtw(a[4], b[0], 0, c2, c1, &c2, &c1, &c0); 131 bn_mulw_addtw(a[3], b[1], c2, c1, c0, &c2, &c1, &c0); 132 bn_mulw_addtw(a[2], b[2], c2, c1, c0, &c2, &c1, &c0); 133 bn_mulw_addtw(a[1], b[3], c2, c1, c0, &c2, &c1, &c0); 134 bn_mulw_addtw(a[0], b[4], c2, c1, c0, &c2, &c1, &r[4]); 135 136 bn_mulw_addtw(a[0], b[5], 0, c2, c1, &c2, &c1, &c0); 137 bn_mulw_addtw(a[1], b[4], c2, c1, c0, &c2, &c1, &c0); 138 bn_mulw_addtw(a[2], b[3], c2, c1, c0, &c2, &c1, &c0); 139 bn_mulw_addtw(a[3], b[2], c2, c1, c0, &c2, &c1, &c0); 140 bn_mulw_addtw(a[4], b[1], c2, c1, c0, &c2, &c1, &c0); 141 bn_mulw_addtw(a[5], b[0], c2, c1, c0, &c2, &c1, &r[5]); 142 143 bn_mulw_addtw(a[6], b[0], 0, c2, c1, &c2, &c1, &c0); 144 bn_mulw_addtw(a[5], b[1], c2, c1, c0, &c2, &c1, &c0); 145 bn_mulw_addtw(a[4], b[2], c2, c1, c0, &c2, &c1, &c0); 146 bn_mulw_addtw(a[3], b[3], c2, c1, c0, &c2, &c1, &c0); 147 bn_mulw_addtw(a[2], b[4], c2, c1, c0, &c2, &c1, &c0); 148 bn_mulw_addtw(a[1], b[5], c2, c1, c0, &c2, &c1, &c0); 149 bn_mulw_addtw(a[0], b[6], c2, c1, c0, &c2, &c1, &r[6]); 150 151 bn_mulw_addtw(a[0], b[7], 0, c2, c1, &c2, &c1, &c0); 152 bn_mulw_addtw(a[1], b[6], c2, c1, c0, &c2, &c1, &c0); 153 bn_mulw_addtw(a[2], b[5], c2, c1, c0, &c2, &c1, &c0); 154 bn_mulw_addtw(a[3], b[4], c2, c1, c0, &c2, &c1, &c0); 155 bn_mulw_addtw(a[4], b[3], c2, c1, c0, &c2, &c1, &c0); 156 bn_mulw_addtw(a[5], b[2], c2, c1, c0, &c2, &c1, &c0); 157 bn_mulw_addtw(a[6], b[1], c2, c1, c0, &c2, &c1, &c0); 158 bn_mulw_addtw(a[7], b[0], c2, c1, c0, &c2, &c1, &r[7]); 159 160 bn_mulw_addtw(a[7], b[1], 0, c2, c1, &c2, &c1, &c0); 161 bn_mulw_addtw(a[6], b[2], c2, c1, c0, &c2, &c1, &c0); 162 bn_mulw_addtw(a[5], b[3], c2, c1, c0, &c2, &c1, &c0); 163 bn_mulw_addtw(a[4], b[4], c2, c1, c0, &c2, &c1, &c0); 164 bn_mulw_addtw(a[3], b[5], c2, c1, c0, &c2, &c1, &c0); 165 bn_mulw_addtw(a[2], b[6], c2, c1, c0, &c2, &c1, &c0); 166 bn_mulw_addtw(a[1], b[7], c2, c1, c0, &c2, &c1, &r[8]); 167 168 bn_mulw_addtw(a[2], b[7], 0, c2, c1, &c2, &c1, &c0); 169 bn_mulw_addtw(a[3], b[6], c2, c1, c0, &c2, &c1, &c0); 170 bn_mulw_addtw(a[4], b[5], c2, c1, c0, &c2, &c1, &c0); 171 bn_mulw_addtw(a[5], b[4], c2, c1, c0, &c2, &c1, &c0); 172 bn_mulw_addtw(a[6], b[3], c2, c1, c0, &c2, &c1, &c0); 173 bn_mulw_addtw(a[7], b[2], c2, c1, c0, &c2, &c1, &r[9]); 174 175 bn_mulw_addtw(a[7], b[3], 0, c2, c1, &c2, &c1, &c0); 176 bn_mulw_addtw(a[6], b[4], c2, c1, c0, &c2, &c1, &c0); 177 bn_mulw_addtw(a[5], b[5], c2, c1, c0, &c2, &c1, &c0); 178 bn_mulw_addtw(a[4], b[6], c2, c1, c0, &c2, &c1, &c0); 179 bn_mulw_addtw(a[3], b[7], c2, c1, c0, &c2, &c1, &r[10]); 180 181 bn_mulw_addtw(a[4], b[7], 0, c2, c1, &c2, &c1, &c0); 182 bn_mulw_addtw(a[5], b[6], c2, c1, c0, &c2, &c1, &c0); 183 bn_mulw_addtw(a[6], b[5], c2, c1, c0, &c2, &c1, &c0); 184 bn_mulw_addtw(a[7], b[4], c2, c1, c0, &c2, &c1, &r[11]); 185 186 bn_mulw_addtw(a[7], b[5], 0, c2, c1, &c2, &c1, &c0); 187 bn_mulw_addtw(a[6], b[6], c2, c1, c0, &c2, &c1, &c0); 188 bn_mulw_addtw(a[5], b[7], c2, c1, c0, &c2, &c1, &r[12]); 189 190 bn_mulw_addtw(a[6], b[7], 0, c2, c1, &c2, &c1, &c0); 191 bn_mulw_addtw(a[7], b[6], c2, c1, c0, &c2, &c1, &r[13]); 192 193 bn_mulw_addtw(a[7], b[7], 0, c2, c1, &c2, &r[15], &r[14]); 194} 195#endif 196 197/* 198 * bn_mul_words() computes (carry:r[i]) = a[i] * w + carry, where a is an array 199 * of words and w is a single word. This should really be called bn_mulw_words() 200 * since only one input is an array. This is used as a step in the multiplication 201 * of word arrays. 202 */ 203#ifndef HAVE_BN_MUL_WORDS 204BN_ULONG 205bn_mul_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) 206{ 207 BN_ULONG carry = 0; 208 209 assert(num >= 0); 210 if (num <= 0) 211 return 0; 212 213#ifndef OPENSSL_SMALL_FOOTPRINT 214 while (num & ~3) { 215 bn_mulw_addw(a[0], w, carry, &carry, &r[0]); 216 bn_mulw_addw(a[1], w, carry, &carry, &r[1]); 217 bn_mulw_addw(a[2], w, carry, &carry, &r[2]); 218 bn_mulw_addw(a[3], w, carry, &carry, &r[3]); 219 a += 4; 220 r += 4; 221 num -= 4; 222 } 223#endif 224 while (num) { 225 bn_mulw_addw(a[0], w, carry, &carry, &r[0]); 226 a++; 227 r++; 228 num--; 229 } 230 return carry; 231} 232#endif 233 234/* 235 * bn_mul_add_words() computes (carry:r[i]) = a[i] * w + r[i] + carry, where 236 * a is an array of words and w is a single word. This should really be called 237 * bn_mulw_add_words() since only one input is an array. This is used as a step 238 * in the multiplication of word arrays. 239 */ 240#ifndef HAVE_BN_MUL_ADD_WORDS 241BN_ULONG 242bn_mul_add_words(BN_ULONG *r, const BN_ULONG *a, int num, BN_ULONG w) 243{ 244 BN_ULONG carry = 0; 245 246 assert(num >= 0); 247 if (num <= 0) 248 return 0; 249 250#ifndef OPENSSL_SMALL_FOOTPRINT 251 while (num & ~3) { 252 bn_mulw_addw_addw(a[0], w, r[0], carry, &carry, &r[0]); 253 bn_mulw_addw_addw(a[1], w, r[1], carry, &carry, &r[1]); 254 bn_mulw_addw_addw(a[2], w, r[2], carry, &carry, &r[2]); 255 bn_mulw_addw_addw(a[3], w, r[3], carry, &carry, &r[3]); 256 a += 4; 257 r += 4; 258 num -= 4; 259 } 260#endif 261 while (num) { 262 bn_mulw_addw_addw(a[0], w, r[0], carry, &carry, &r[0]); 263 a++; 264 r++; 265 num--; 266 } 267 268 return carry; 269} 270#endif 271 272void 273bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) 274{ 275 BN_ULONG *rr; 276 277 278 if (na < nb) { 279 int itmp; 280 BN_ULONG *ltmp; 281 282 itmp = na; 283 na = nb; 284 nb = itmp; 285 ltmp = a; 286 a = b; 287 b = ltmp; 288 289 } 290 rr = &(r[na]); 291 if (nb <= 0) { 292 (void)bn_mul_words(r, a, na, 0); 293 return; 294 } else 295 rr[0] = bn_mul_words(r, a, na, b[0]); 296 297 for (;;) { 298 if (--nb <= 0) 299 return; 300 rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); 301 if (--nb <= 0) 302 return; 303 rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); 304 if (--nb <= 0) 305 return; 306 rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); 307 if (--nb <= 0) 308 return; 309 rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); 310 rr += 4; 311 r += 4; 312 b += 4; 313 } 314} 315 316 317#ifndef HAVE_BN_MUL 318int 319bn_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, int rn, BN_CTX *ctx) 320{ 321 bn_mul_normal(r->d, a->d, a->top, b->d, b->top); 322 323 return 1; 324} 325 326#endif /* HAVE_BN_MUL */ 327 328int 329BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 330{ 331 BIGNUM *rr; 332 int rn; 333 int ret = 0; 334 335 BN_CTX_start(ctx); 336 337 if (BN_is_zero(a) || BN_is_zero(b)) { 338 BN_zero(r); 339 goto done; 340 } 341 342 rr = r; 343 if (rr == a || rr == b) 344 rr = BN_CTX_get(ctx); 345 if (rr == NULL) 346 goto err; 347 348 rn = a->top + b->top; 349 if (rn < a->top) 350 goto err; 351 if (!bn_wexpand(rr, rn)) 352 goto err; 353 354 if (a->top == 4 && b->top == 4) { 355 bn_mul_comba4(rr->d, a->d, b->d); 356 } else if (a->top == 8 && b->top == 8) { 357 bn_mul_comba8(rr->d, a->d, b->d); 358 } else { 359 if (!bn_mul(rr, a, b, rn, ctx)) 360 goto err; 361 } 362 363 rr->top = rn; 364 bn_correct_top(rr); 365 366 BN_set_negative(rr, a->neg ^ b->neg); 367 368 if (!bn_copy(r, rr)) 369 goto err; 370 done: 371 ret = 1; 372 err: 373 BN_CTX_end(ctx); 374 375 return ret; 376} 377