155714Skris/* crypto/bn/bn_add.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include "bn_lcl.h" 6255714Skris 6355714Skris/* r can == a or b */ 6459191Skrisint BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 65280304Sjkim{ 66280304Sjkim const BIGNUM *tmp; 67280304Sjkim int a_neg = a->neg, ret; 6855714Skris 69280304Sjkim bn_check_top(a); 70280304Sjkim bn_check_top(b); 7155714Skris 72280304Sjkim /*- 73280304Sjkim * a + b a+b 74280304Sjkim * a + -b a-b 75280304Sjkim * -a + b b-a 76280304Sjkim * -a + -b -(a+b) 77280304Sjkim */ 78280304Sjkim if (a_neg ^ b->neg) { 79280304Sjkim /* only one is negative */ 80280304Sjkim if (a_neg) { 81280304Sjkim tmp = a; 82280304Sjkim a = b; 83280304Sjkim b = tmp; 84280304Sjkim } 8555714Skris 86280304Sjkim /* we are now a - b */ 8755714Skris 88280304Sjkim if (BN_ucmp(a, b) < 0) { 89280304Sjkim if (!BN_usub(r, b, a)) 90280304Sjkim return (0); 91280304Sjkim r->neg = 1; 92280304Sjkim } else { 93280304Sjkim if (!BN_usub(r, a, b)) 94280304Sjkim return (0); 95280304Sjkim r->neg = 0; 96280304Sjkim } 97280304Sjkim return (1); 98280304Sjkim } 9955714Skris 100280304Sjkim ret = BN_uadd(r, a, b); 101280304Sjkim r->neg = a_neg; 102280304Sjkim bn_check_top(r); 103280304Sjkim return ret; 104280304Sjkim} 10555714Skris 106160814Ssimon/* unsigned add of b to a */ 10755714Skrisint BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 108280304Sjkim{ 109280304Sjkim int max, min, dif; 110280304Sjkim BN_ULONG *ap, *bp, *rp, carry, t1, t2; 111280304Sjkim const BIGNUM *tmp; 11255714Skris 113280304Sjkim bn_check_top(a); 114280304Sjkim bn_check_top(b); 11555714Skris 116280304Sjkim if (a->top < b->top) { 117280304Sjkim tmp = a; 118280304Sjkim a = b; 119280304Sjkim b = tmp; 120280304Sjkim } 121280304Sjkim max = a->top; 122280304Sjkim min = b->top; 123280304Sjkim dif = max - min; 12455714Skris 125280304Sjkim if (bn_wexpand(r, max + 1) == NULL) 126280304Sjkim return 0; 12755714Skris 128280304Sjkim r->top = max; 12955714Skris 130280304Sjkim ap = a->d; 131280304Sjkim bp = b->d; 132280304Sjkim rp = r->d; 13355714Skris 134280304Sjkim carry = bn_add_words(rp, ap, bp, min); 135280304Sjkim rp += min; 136280304Sjkim ap += min; 137280304Sjkim bp += min; 13855714Skris 139280304Sjkim if (carry) { 140280304Sjkim while (dif) { 141280304Sjkim dif--; 142280304Sjkim t1 = *(ap++); 143280304Sjkim t2 = (t1 + 1) & BN_MASK2; 144280304Sjkim *(rp++) = t2; 145280304Sjkim if (t2) { 146280304Sjkim carry = 0; 147280304Sjkim break; 148280304Sjkim } 149280304Sjkim } 150280304Sjkim if (carry) { 151280304Sjkim /* carry != 0 => dif == 0 */ 152280304Sjkim *rp = 1; 153280304Sjkim r->top++; 154280304Sjkim } 155280304Sjkim } 156280304Sjkim if (dif && rp != ap) 157280304Sjkim while (dif--) 158280304Sjkim /* copy remaining words if ap != rp */ 159280304Sjkim *(rp++) = *(ap++); 160280304Sjkim r->neg = 0; 161280304Sjkim bn_check_top(r); 162280304Sjkim return 1; 163280304Sjkim} 16455714Skris 16555714Skris/* unsigned subtraction of b from a, a must be larger than b. */ 16655714Skrisint BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 167280304Sjkim{ 168280304Sjkim int max, min, dif; 169280304Sjkim register BN_ULONG t1, t2, *ap, *bp, *rp; 170280304Sjkim int i, carry; 17155714Skris#if defined(IRIX_CC_BUG) && !defined(LINT) 172280304Sjkim int dummy; 17355714Skris#endif 17455714Skris 175280304Sjkim bn_check_top(a); 176280304Sjkim bn_check_top(b); 17755714Skris 178280304Sjkim max = a->top; 179280304Sjkim min = b->top; 180280304Sjkim dif = max - min; 181160814Ssimon 182280304Sjkim if (dif < 0) { /* hmm... should not be happening */ 183280304Sjkim BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3); 184280304Sjkim return (0); 185280304Sjkim } 18655714Skris 187280304Sjkim if (bn_wexpand(r, max) == NULL) 188280304Sjkim return (0); 18955714Skris 190280304Sjkim ap = a->d; 191280304Sjkim bp = b->d; 192280304Sjkim rp = r->d; 19355714Skris 19455714Skris#if 1 195280304Sjkim carry = 0; 196280304Sjkim for (i = min; i != 0; i--) { 197280304Sjkim t1 = *(ap++); 198280304Sjkim t2 = *(bp++); 199280304Sjkim if (carry) { 200280304Sjkim carry = (t1 <= t2); 201280304Sjkim t1 = (t1 - t2 - 1) & BN_MASK2; 202280304Sjkim } else { 203280304Sjkim carry = (t1 < t2); 204280304Sjkim t1 = (t1 - t2) & BN_MASK2; 205280304Sjkim } 206280304Sjkim# if defined(IRIX_CC_BUG) && !defined(LINT) 207280304Sjkim dummy = t1; 208280304Sjkim# endif 209280304Sjkim *(rp++) = t1 & BN_MASK2; 210280304Sjkim } 21155714Skris#else 212280304Sjkim carry = bn_sub_words(rp, ap, bp, min); 213280304Sjkim ap += min; 214280304Sjkim bp += min; 215280304Sjkim rp += min; 21655714Skris#endif 217280304Sjkim if (carry) { /* subtracted */ 218280304Sjkim if (!dif) 219280304Sjkim /* error: a < b */ 220280304Sjkim return 0; 221280304Sjkim while (dif) { 222280304Sjkim dif--; 223280304Sjkim t1 = *(ap++); 224280304Sjkim t2 = (t1 - 1) & BN_MASK2; 225280304Sjkim *(rp++) = t2; 226280304Sjkim if (t1) 227280304Sjkim break; 228280304Sjkim } 229280304Sjkim } 23055714Skris#if 0 231280304Sjkim memcpy(rp, ap, sizeof(*rp) * (max - i)); 23255714Skris#else 233280304Sjkim if (rp != ap) { 234280304Sjkim for (;;) { 235280304Sjkim if (!dif--) 236280304Sjkim break; 237280304Sjkim rp[0] = ap[0]; 238280304Sjkim if (!dif--) 239280304Sjkim break; 240280304Sjkim rp[1] = ap[1]; 241280304Sjkim if (!dif--) 242280304Sjkim break; 243280304Sjkim rp[2] = ap[2]; 244280304Sjkim if (!dif--) 245280304Sjkim break; 246280304Sjkim rp[3] = ap[3]; 247280304Sjkim rp += 4; 248280304Sjkim ap += 4; 249280304Sjkim } 250280304Sjkim } 25155714Skris#endif 25255714Skris 253280304Sjkim r->top = max; 254280304Sjkim r->neg = 0; 255280304Sjkim bn_correct_top(r); 256280304Sjkim return (1); 257280304Sjkim} 25855714Skris 25955714Skrisint BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 260280304Sjkim{ 261280304Sjkim int max; 262280304Sjkim int add = 0, neg = 0; 263280304Sjkim const BIGNUM *tmp; 26455714Skris 265280304Sjkim bn_check_top(a); 266280304Sjkim bn_check_top(b); 26755714Skris 268280304Sjkim /*- 269280304Sjkim * a - b a-b 270280304Sjkim * a - -b a+b 271280304Sjkim * -a - b -(a+b) 272280304Sjkim * -a - -b b-a 273280304Sjkim */ 274280304Sjkim if (a->neg) { 275280304Sjkim if (b->neg) { 276280304Sjkim tmp = a; 277280304Sjkim a = b; 278280304Sjkim b = tmp; 279280304Sjkim } else { 280280304Sjkim add = 1; 281280304Sjkim neg = 1; 282280304Sjkim } 283280304Sjkim } else { 284280304Sjkim if (b->neg) { 285280304Sjkim add = 1; 286280304Sjkim neg = 0; 287280304Sjkim } 288280304Sjkim } 28955714Skris 290280304Sjkim if (add) { 291280304Sjkim if (!BN_uadd(r, a, b)) 292280304Sjkim return (0); 293280304Sjkim r->neg = neg; 294280304Sjkim return (1); 295280304Sjkim } 29655714Skris 297280304Sjkim /* We are actually doing a - b :-) */ 29855714Skris 299280304Sjkim max = (a->top > b->top) ? a->top : b->top; 300280304Sjkim if (bn_wexpand(r, max) == NULL) 301280304Sjkim return (0); 302280304Sjkim if (BN_ucmp(a, b) < 0) { 303280304Sjkim if (!BN_usub(r, b, a)) 304280304Sjkim return (0); 305280304Sjkim r->neg = 1; 306280304Sjkim } else { 307280304Sjkim if (!BN_usub(r, a, b)) 308280304Sjkim return (0); 309280304Sjkim r->neg = 0; 310280304Sjkim } 311280304Sjkim bn_check_top(r); 312280304Sjkim return (1); 313280304Sjkim} 314