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. 8280297Sjkim * 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). 15280297Sjkim * 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. 22280297Sjkim * 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 :-). 37280297Sjkim * 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)" 40280297Sjkim * 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. 52280297Sjkim * 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) 65280297Sjkim{ 66280297Sjkim const BIGNUM *tmp; 67280297Sjkim int a_neg = a->neg, ret; 6855714Skris 69280297Sjkim bn_check_top(a); 70280297Sjkim bn_check_top(b); 7155714Skris 72280297Sjkim /*- 73280297Sjkim * a + b a+b 74280297Sjkim * a + -b a-b 75280297Sjkim * -a + b b-a 76280297Sjkim * -a + -b -(a+b) 77280297Sjkim */ 78280297Sjkim if (a_neg ^ b->neg) { 79280297Sjkim /* only one is negative */ 80280297Sjkim if (a_neg) { 81280297Sjkim tmp = a; 82280297Sjkim a = b; 83280297Sjkim b = tmp; 84280297Sjkim } 8555714Skris 86280297Sjkim /* we are now a - b */ 8755714Skris 88280297Sjkim if (BN_ucmp(a, b) < 0) { 89280297Sjkim if (!BN_usub(r, b, a)) 90280297Sjkim return (0); 91280297Sjkim r->neg = 1; 92280297Sjkim } else { 93280297Sjkim if (!BN_usub(r, a, b)) 94280297Sjkim return (0); 95280297Sjkim r->neg = 0; 96280297Sjkim } 97280297Sjkim return (1); 98280297Sjkim } 9955714Skris 100280297Sjkim ret = BN_uadd(r, a, b); 101280297Sjkim r->neg = a_neg; 102280297Sjkim bn_check_top(r); 103280297Sjkim return ret; 104280297Sjkim} 10555714Skris 106160814Ssimon/* unsigned add of b to a */ 10755714Skrisint BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 108280297Sjkim{ 109280297Sjkim int max, min, dif; 110280297Sjkim BN_ULONG *ap, *bp, *rp, carry, t1, t2; 111280297Sjkim const BIGNUM *tmp; 11255714Skris 113280297Sjkim bn_check_top(a); 114280297Sjkim bn_check_top(b); 11555714Skris 116280297Sjkim if (a->top < b->top) { 117280297Sjkim tmp = a; 118280297Sjkim a = b; 119280297Sjkim b = tmp; 120280297Sjkim } 121280297Sjkim max = a->top; 122280297Sjkim min = b->top; 123280297Sjkim dif = max - min; 12455714Skris 125280297Sjkim if (bn_wexpand(r, max + 1) == NULL) 126280297Sjkim return 0; 12755714Skris 128280297Sjkim r->top = max; 12955714Skris 130280297Sjkim ap = a->d; 131280297Sjkim bp = b->d; 132280297Sjkim rp = r->d; 13355714Skris 134280297Sjkim carry = bn_add_words(rp, ap, bp, min); 135280297Sjkim rp += min; 136280297Sjkim ap += min; 137280297Sjkim bp += min; 13855714Skris 139280297Sjkim if (carry) { 140280297Sjkim while (dif) { 141280297Sjkim dif--; 142280297Sjkim t1 = *(ap++); 143280297Sjkim t2 = (t1 + 1) & BN_MASK2; 144280297Sjkim *(rp++) = t2; 145280297Sjkim if (t2) { 146280297Sjkim carry = 0; 147280297Sjkim break; 148280297Sjkim } 149280297Sjkim } 150280297Sjkim if (carry) { 151280297Sjkim /* carry != 0 => dif == 0 */ 152280297Sjkim *rp = 1; 153280297Sjkim r->top++; 154280297Sjkim } 155280297Sjkim } 156280297Sjkim if (dif && rp != ap) 157280297Sjkim while (dif--) 158280297Sjkim /* copy remaining words if ap != rp */ 159280297Sjkim *(rp++) = *(ap++); 160280297Sjkim r->neg = 0; 161280297Sjkim bn_check_top(r); 162280297Sjkim return 1; 163280297Sjkim} 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) 167280297Sjkim{ 168280297Sjkim int max, min, dif; 169280297Sjkim register BN_ULONG t1, t2, *ap, *bp, *rp; 170280297Sjkim int i, carry; 17155714Skris#if defined(IRIX_CC_BUG) && !defined(LINT) 172280297Sjkim int dummy; 17355714Skris#endif 17455714Skris 175280297Sjkim bn_check_top(a); 176280297Sjkim bn_check_top(b); 17755714Skris 178280297Sjkim max = a->top; 179280297Sjkim min = b->top; 180280297Sjkim dif = max - min; 181160814Ssimon 182280297Sjkim if (dif < 0) { /* hmm... should not be happening */ 183280297Sjkim BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3); 184280297Sjkim return (0); 185280297Sjkim } 18655714Skris 187280297Sjkim if (bn_wexpand(r, max) == NULL) 188280297Sjkim return (0); 18955714Skris 190280297Sjkim ap = a->d; 191280297Sjkim bp = b->d; 192280297Sjkim rp = r->d; 19355714Skris 19455714Skris#if 1 195280297Sjkim carry = 0; 196280297Sjkim for (i = min; i != 0; i--) { 197280297Sjkim t1 = *(ap++); 198280297Sjkim t2 = *(bp++); 199280297Sjkim if (carry) { 200280297Sjkim carry = (t1 <= t2); 201280297Sjkim t1 = (t1 - t2 - 1) & BN_MASK2; 202280297Sjkim } else { 203280297Sjkim carry = (t1 < t2); 204280297Sjkim t1 = (t1 - t2) & BN_MASK2; 205280297Sjkim } 206280297Sjkim# if defined(IRIX_CC_BUG) && !defined(LINT) 207280297Sjkim dummy = t1; 208280297Sjkim# endif 209280297Sjkim *(rp++) = t1 & BN_MASK2; 210280297Sjkim } 21155714Skris#else 212280297Sjkim carry = bn_sub_words(rp, ap, bp, min); 213280297Sjkim ap += min; 214280297Sjkim bp += min; 215280297Sjkim rp += min; 21655714Skris#endif 217280297Sjkim if (carry) { /* subtracted */ 218280297Sjkim if (!dif) 219280297Sjkim /* error: a < b */ 220280297Sjkim return 0; 221280297Sjkim while (dif) { 222280297Sjkim dif--; 223280297Sjkim t1 = *(ap++); 224280297Sjkim t2 = (t1 - 1) & BN_MASK2; 225280297Sjkim *(rp++) = t2; 226280297Sjkim if (t1) 227280297Sjkim break; 228280297Sjkim } 229280297Sjkim } 23055714Skris#if 0 231280297Sjkim memcpy(rp, ap, sizeof(*rp) * (max - i)); 23255714Skris#else 233280297Sjkim if (rp != ap) { 234280297Sjkim for (;;) { 235280297Sjkim if (!dif--) 236280297Sjkim break; 237280297Sjkim rp[0] = ap[0]; 238280297Sjkim if (!dif--) 239280297Sjkim break; 240280297Sjkim rp[1] = ap[1]; 241280297Sjkim if (!dif--) 242280297Sjkim break; 243280297Sjkim rp[2] = ap[2]; 244280297Sjkim if (!dif--) 245280297Sjkim break; 246280297Sjkim rp[3] = ap[3]; 247280297Sjkim rp += 4; 248280297Sjkim ap += 4; 249280297Sjkim } 250280297Sjkim } 25155714Skris#endif 25255714Skris 253280297Sjkim r->top = max; 254280297Sjkim r->neg = 0; 255280297Sjkim bn_correct_top(r); 256280297Sjkim return (1); 257280297Sjkim} 25855714Skris 25955714Skrisint BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 260280297Sjkim{ 261280297Sjkim int max; 262280297Sjkim int add = 0, neg = 0; 263280297Sjkim const BIGNUM *tmp; 26455714Skris 265280297Sjkim bn_check_top(a); 266280297Sjkim bn_check_top(b); 26755714Skris 268280297Sjkim /*- 269280297Sjkim * a - b a-b 270280297Sjkim * a - -b a+b 271280297Sjkim * -a - b -(a+b) 272280297Sjkim * -a - -b b-a 273280297Sjkim */ 274280297Sjkim if (a->neg) { 275280297Sjkim if (b->neg) { 276280297Sjkim tmp = a; 277280297Sjkim a = b; 278280297Sjkim b = tmp; 279280297Sjkim } else { 280280297Sjkim add = 1; 281280297Sjkim neg = 1; 282280297Sjkim } 283280297Sjkim } else { 284280297Sjkim if (b->neg) { 285280297Sjkim add = 1; 286280297Sjkim neg = 0; 287280297Sjkim } 288280297Sjkim } 28955714Skris 290280297Sjkim if (add) { 291280297Sjkim if (!BN_uadd(r, a, b)) 292280297Sjkim return (0); 293280297Sjkim r->neg = neg; 294280297Sjkim return (1); 295280297Sjkim } 29655714Skris 297280297Sjkim /* We are actually doing a - b :-) */ 29855714Skris 299280297Sjkim max = (a->top > b->top) ? a->top : b->top; 300280297Sjkim if (bn_wexpand(r, max) == NULL) 301280297Sjkim return (0); 302280297Sjkim if (BN_ucmp(a, b) < 0) { 303280297Sjkim if (!BN_usub(r, b, a)) 304280297Sjkim return (0); 305280297Sjkim r->neg = 1; 306280297Sjkim } else { 307280297Sjkim if (!BN_usub(r, a, b)) 308280297Sjkim return (0); 309280297Sjkim r->neg = 0; 310280297Sjkim } 311280297Sjkim bn_check_top(r); 312280297Sjkim return (1); 313280297Sjkim} 314