bn_add.c revision 1.23
1/* $OpenBSD: bn_add.c,v 1.23 2023/02/16 04:42:20 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#include "bn_internal.h" 68 69BN_ULONG bn_add(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b); 70BN_ULONG bn_sub(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b); 71 72/* 73 * bn_add_words() computes (carry:r[i]) = a[i] + b[i] + carry, where a and b 74 * are both arrays of words. Any carry resulting from the addition is returned. 75 */ 76#ifndef HAVE_BN_ADD_WORDS 77BN_ULONG 78bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) 79{ 80 BN_ULONG carry = 0; 81 82 assert(n >= 0); 83 if (n <= 0) 84 return 0; 85 86#ifndef OPENSSL_SMALL_FOOTPRINT 87 while (n & ~3) { 88 bn_addw_addw(a[0], b[0], carry, &carry, &r[0]); 89 bn_addw_addw(a[1], b[1], carry, &carry, &r[1]); 90 bn_addw_addw(a[2], b[2], carry, &carry, &r[2]); 91 bn_addw_addw(a[3], b[3], carry, &carry, &r[3]); 92 a += 4; 93 b += 4; 94 r += 4; 95 n -= 4; 96 } 97#endif 98 while (n) { 99 bn_addw_addw(a[0], b[0], carry, &carry, &r[0]); 100 a++; 101 b++; 102 r++; 103 n--; 104 } 105 return carry; 106} 107#endif 108 109/* 110 * bn_sub_words() computes (borrow:r[i]) = a[i] - b[i] - borrow, where a and b 111 * are both arrays of words. Any borrow resulting from the subtraction is 112 * returned. 113 */ 114#ifndef HAVE_BN_SUB_WORDS 115BN_ULONG 116bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) 117{ 118 BN_ULONG borrow = 0; 119 120 assert(n >= 0); 121 if (n <= 0) 122 return 0; 123 124#ifndef OPENSSL_SMALL_FOOTPRINT 125 while (n & ~3) { 126 bn_subw_subw(a[0], b[0], borrow, &borrow, &r[0]); 127 bn_subw_subw(a[1], b[1], borrow, &borrow, &r[1]); 128 bn_subw_subw(a[2], b[2], borrow, &borrow, &r[2]); 129 bn_subw_subw(a[3], b[3], borrow, &borrow, &r[3]); 130 a += 4; 131 b += 4; 132 r += 4; 133 n -= 4; 134 } 135#endif 136 while (n) { 137 bn_subw_subw(a[0], b[0], borrow, &borrow, &r[0]); 138 a++; 139 b++; 140 r++; 141 n--; 142 } 143 return borrow; 144} 145#endif 146 147/* 148 * bn_add() computes a + b, storing the result in r (which may be the same as a 149 * or b). The caller must ensure that r has been expanded to max(a->top, b->top) 150 * words. Any carry resulting from the addition is returned. 151 */ 152#ifndef HAVE_BN_ADD 153BN_ULONG 154bn_add(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b) 155{ 156 BN_ULONG *rp, carry, t1, t2; 157 const BN_ULONG *ap, *bp; 158 int max, min, dif; 159 160 if (a->top < b->top) { 161 const BIGNUM *tmp; 162 163 tmp = a; 164 a = b; 165 b = tmp; 166 } 167 max = a->top; 168 min = b->top; 169 dif = max - min; 170 171 ap = a->d; 172 bp = b->d; 173 rp = r->d; 174 175 carry = bn_add_words(rp, ap, bp, min); 176 rp += min; 177 ap += min; 178 179 while (dif) { 180 dif--; 181 t1 = *(ap++); 182 t2 = (t1 + carry) & BN_MASK2; 183 *(rp++) = t2; 184 carry &= (t2 == 0); 185 } 186 187 return carry; 188} 189#endif 190 191/* 192 * bn_sub() computes a - b, storing the result in r (which may be the same as a 193 * or b). The caller must ensure that the number of words in a is greater than 194 * or equal to the number of words in b and that r has been expanded to 195 * a->top words. Any borrow resulting from the subtraction is returned. 196 */ 197#ifndef HAVE_BN_SUB 198BN_ULONG 199bn_sub(BIGNUM *r, int rn, const BIGNUM *a, const BIGNUM *b) 200{ 201 BN_ULONG t1, t2, borrow, *rp; 202 const BN_ULONG *ap, *bp; 203 int max, min, dif; 204 205 max = a->top; 206 min = b->top; 207 dif = max - min; 208 209 ap = a->d; 210 bp = b->d; 211 rp = r->d; 212 213 borrow = bn_sub_words(rp, ap, bp, min); 214 ap += min; 215 rp += min; 216 217 while (dif) { 218 dif--; 219 t1 = *(ap++); 220 t2 = (t1 - borrow) & BN_MASK2; 221 *(rp++) = t2; 222 borrow &= (t1 == 0); 223 } 224 225 return borrow; 226} 227#endif 228 229int 230BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 231{ 232 BN_ULONG carry; 233 int rn; 234 235 if ((rn = a->top) < b->top) 236 rn = b->top; 237 if (rn == INT_MAX) 238 return 0; 239 if (!bn_wexpand(r, rn + 1)) 240 return 0; 241 242 carry = bn_add(r, rn, a, b); 243 r->d[rn] = carry; 244 245 r->top = rn + (carry & 1); 246 r->neg = 0; 247 248 return 1; 249} 250 251int 252BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 253{ 254 BN_ULONG borrow; 255 int rn; 256 257 if (a->top < b->top) { 258 BNerror(BN_R_ARG2_LT_ARG3); 259 return 0; 260 } 261 rn = a->top; 262 263 if (!bn_wexpand(r, rn)) 264 return 0; 265 266 borrow = bn_sub(r, rn, a, b); 267 if (borrow > 0) { 268 BNerror(BN_R_ARG2_LT_ARG3); 269 return 0; 270 } 271 272 r->top = rn; 273 r->neg = 0; 274 275 bn_correct_top(r); 276 277 return 1; 278} 279 280int 281BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 282{ 283 int ret, r_neg; 284 285 if (a->neg == b->neg) { 286 r_neg = a->neg; 287 ret = BN_uadd(r, a, b); 288 } else { 289 int cmp = BN_ucmp(a, b); 290 291 if (cmp > 0) { 292 r_neg = a->neg; 293 ret = BN_usub(r, a, b); 294 } else if (cmp < 0) { 295 r_neg = b->neg; 296 ret = BN_usub(r, b, a); 297 } else { 298 r_neg = 0; 299 BN_zero(r); 300 ret = 1; 301 } 302 } 303 304 BN_set_negative(r, r_neg); 305 306 return ret; 307} 308 309int 310BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 311{ 312 int ret, r_neg; 313 314 if (a->neg != b->neg) { 315 r_neg = a->neg; 316 ret = BN_uadd(r, a, b); 317 } else { 318 int cmp = BN_ucmp(a, b); 319 320 if (cmp > 0) { 321 r_neg = a->neg; 322 ret = BN_usub(r, a, b); 323 } else if (cmp < 0) { 324 r_neg = !b->neg; 325 ret = BN_usub(r, b, a); 326 } else { 327 r_neg = 0; 328 BN_zero(r); 329 ret = 1; 330 } 331 } 332 333 BN_set_negative(r, r_neg); 334 335 return ret; 336} 337