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