bn_add.c revision 1.21
1/* $OpenBSD: bn_add.c,v 1.21 2023/02/02 18:39:26 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 <limits.h> 61#include <stdio.h> 62 63#include <openssl/err.h> 64 65#include "bn_arch.h" 66#include "bn_local.h" 67 68BN_ULONG bn_add(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b); 69BN_ULONG bn_sub(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b); 70 71#ifndef HAVE_BN_ADD_WORDS 72#ifdef BN_LLONG 73BN_ULONG 74bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) 75{ 76 BN_ULLONG ll = 0; 77 78 assert(n >= 0); 79 if (n <= 0) 80 return ((BN_ULONG)0); 81 82#ifndef OPENSSL_SMALL_FOOTPRINT 83 while (n & ~3) { 84 ll += (BN_ULLONG)a[0] + b[0]; 85 r[0] = (BN_ULONG)ll & BN_MASK2; 86 ll >>= BN_BITS2; 87 ll += (BN_ULLONG)a[1] + b[1]; 88 r[1] = (BN_ULONG)ll & BN_MASK2; 89 ll >>= BN_BITS2; 90 ll += (BN_ULLONG)a[2] + b[2]; 91 r[2] = (BN_ULONG)ll & BN_MASK2; 92 ll >>= BN_BITS2; 93 ll += (BN_ULLONG)a[3] + b[3]; 94 r[3] = (BN_ULONG)ll & BN_MASK2; 95 ll >>= BN_BITS2; 96 a += 4; 97 b += 4; 98 r += 4; 99 n -= 4; 100 } 101#endif 102 while (n) { 103 ll += (BN_ULLONG)a[0] + b[0]; 104 r[0] = (BN_ULONG)ll & BN_MASK2; 105 ll >>= BN_BITS2; 106 a++; 107 b++; 108 r++; 109 n--; 110 } 111 return ((BN_ULONG)ll); 112} 113#else /* !BN_LLONG */ 114BN_ULONG 115bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) 116{ 117 BN_ULONG c, l, t; 118 119 assert(n >= 0); 120 if (n <= 0) 121 return ((BN_ULONG)0); 122 123 c = 0; 124#ifndef OPENSSL_SMALL_FOOTPRINT 125 while (n & ~3) { 126 t = a[0]; 127 t = (t + c) & BN_MASK2; 128 c = (t < c); 129 l = (t + b[0]) & BN_MASK2; 130 c += (l < t); 131 r[0] = l; 132 t = a[1]; 133 t = (t + c) & BN_MASK2; 134 c = (t < c); 135 l = (t + b[1]) & BN_MASK2; 136 c += (l < t); 137 r[1] = l; 138 t = a[2]; 139 t = (t + c) & BN_MASK2; 140 c = (t < c); 141 l = (t + b[2]) & BN_MASK2; 142 c += (l < t); 143 r[2] = l; 144 t = a[3]; 145 t = (t + c) & BN_MASK2; 146 c = (t < c); 147 l = (t + b[3]) & BN_MASK2; 148 c += (l < t); 149 r[3] = l; 150 a += 4; 151 b += 4; 152 r += 4; 153 n -= 4; 154 } 155#endif 156 while (n) { 157 t = a[0]; 158 t = (t + c) & BN_MASK2; 159 c = (t < c); 160 l = (t + b[0]) & BN_MASK2; 161 c += (l < t); 162 r[0] = l; 163 a++; 164 b++; 165 r++; 166 n--; 167 } 168 return ((BN_ULONG)c); 169} 170#endif /* !BN_LLONG */ 171#endif 172 173#ifndef HAVE_BN_SUB_WORDS 174BN_ULONG 175bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) 176{ 177 BN_ULONG t1, t2; 178 int c = 0; 179 180 assert(n >= 0); 181 if (n <= 0) 182 return ((BN_ULONG)0); 183 184#ifndef OPENSSL_SMALL_FOOTPRINT 185 while (n&~3) { 186 t1 = a[0]; 187 t2 = b[0]; 188 r[0] = (t1 - t2 - c) & BN_MASK2; 189 if (t1 != t2) 190 c = (t1 < t2); 191 t1 = a[1]; 192 t2 = b[1]; 193 r[1] = (t1 - t2 - c) & BN_MASK2; 194 if (t1 != t2) 195 c = (t1 < t2); 196 t1 = a[2]; 197 t2 = b[2]; 198 r[2] = (t1 - t2 - c) & BN_MASK2; 199 if (t1 != t2) 200 c = (t1 < t2); 201 t1 = a[3]; 202 t2 = b[3]; 203 r[3] = (t1 - t2 - c) & BN_MASK2; 204 if (t1 != t2) 205 c = (t1 < t2); 206 a += 4; 207 b += 4; 208 r += 4; 209 n -= 4; 210 } 211#endif 212 while (n) { 213 t1 = a[0]; 214 t2 = b[0]; 215 r[0] = (t1 - t2 - c) & BN_MASK2; 216 if (t1 != t2) 217 c = (t1 < t2); 218 a++; 219 b++; 220 r++; 221 n--; 222 } 223 return (c); 224} 225#endif 226 227#ifndef HAVE_BN_ADD 228/* 229 * bn_add() computes a + b, storing the result in r (which may be the same as a 230 * or b). The caller must ensure that r has been expanded to max(a->top, b->top) 231 * words. Any carry resulting from the addition is returned. 232 */ 233BN_ULONG 234bn_add(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b) 235{ 236 BN_ULONG *rp, carry, t1, t2; 237 const BN_ULONG *ap, *bp; 238 int max, min, dif; 239 240 if (a->top < b->top) { 241 const BIGNUM *tmp; 242 243 tmp = a; 244 a = b; 245 b = tmp; 246 } 247 max = a->top; 248 min = b->top; 249 dif = max - min; 250 251 ap = a->d; 252 bp = b->d; 253 rp = r->d; 254 255 carry = bn_add_words(rp, ap, bp, min); 256 rp += min; 257 ap += min; 258 259 while (dif) { 260 dif--; 261 t1 = *(ap++); 262 t2 = (t1 + carry) & BN_MASK2; 263 *(rp++) = t2; 264 carry &= (t2 == 0); 265 } 266 267 return carry; 268} 269#endif 270 271#ifndef HAVE_BN_SUB 272/* 273 * bn_sub() computes a - b, storing the result in r (which may be the same as a 274 * or b). The caller must ensure that the number of words in a is greater than 275 * or equal to the number of words in b and that r has been expanded to 276 * a->top words. Any borrow resulting from the subtraction is returned. 277 */ 278BN_ULONG 279bn_sub(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b) 280{ 281 BN_ULONG t1, t2, borrow, *rp; 282 const BN_ULONG *ap, *bp; 283 int max, min, dif; 284 285 max = a->top; 286 min = b->top; 287 dif = max - min; 288 289 ap = a->d; 290 bp = b->d; 291 rp = r->d; 292 293 borrow = bn_sub_words(rp, ap, bp, min); 294 ap += min; 295 rp += min; 296 297 while (dif) { 298 dif--; 299 t1 = *(ap++); 300 t2 = (t1 - borrow) & BN_MASK2; 301 *(rp++) = t2; 302 borrow &= (t1 == 0); 303 } 304 305 return borrow; 306} 307#endif 308 309int 310BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 311{ 312 BN_ULONG carry; 313 int rn; 314 315 if ((rn = a->top) < b->top) 316 rn = b->top; 317 if (rn == INT_MAX) 318 return 0; 319 if (!bn_wexpand(r, rn + 1)) 320 return 0; 321 322 carry = bn_add(r, rn, a, b); 323 r->d[rn] = carry; 324 325 r->top = rn + (carry & 1); 326 r->neg = 0; 327 328 return 1; 329} 330 331int 332BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 333{ 334 BN_ULONG borrow; 335 int rn; 336 337 if (a->top < b->top) { 338 BNerror(BN_R_ARG2_LT_ARG3); 339 return 0; 340 } 341 rn = a->top; 342 343 if (!bn_wexpand(r, rn)) 344 return 0; 345 346 borrow = bn_sub(r, rn, a, b); 347 if (borrow > 0) { 348 BNerror(BN_R_ARG2_LT_ARG3); 349 return 0; 350 } 351 352 r->top = rn; 353 r->neg = 0; 354 355 bn_correct_top(r); 356 357 return 1; 358} 359 360int 361BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 362{ 363 int ret, r_neg; 364 365 if (a->neg == b->neg) { 366 r_neg = a->neg; 367 ret = BN_uadd(r, a, b); 368 } else { 369 int cmp = BN_ucmp(a, b); 370 371 if (cmp > 0) { 372 r_neg = a->neg; 373 ret = BN_usub(r, a, b); 374 } else if (cmp < 0) { 375 r_neg = b->neg; 376 ret = BN_usub(r, b, a); 377 } else { 378 r_neg = 0; 379 BN_zero(r); 380 ret = 1; 381 } 382 } 383 384 r->neg = r_neg; 385 return ret; 386} 387 388int 389BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 390{ 391 int ret, r_neg; 392 393 if (a->neg != b->neg) { 394 r_neg = a->neg; 395 ret = BN_uadd(r, a, b); 396 } else { 397 int cmp = BN_ucmp(a, b); 398 399 if (cmp > 0) { 400 r_neg = a->neg; 401 ret = BN_usub(r, a, b); 402 } else if (cmp < 0) { 403 r_neg = !b->neg; 404 ret = BN_usub(r, b, a); 405 } else { 406 r_neg = 0; 407 BN_zero(r); 408 ret = 1; 409 } 410 } 411 412 r->neg = r_neg; 413 return ret; 414} 415