2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2020-11-26 22:18:57 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:22:12 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "bn_local.h"
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2018-04-18 04:57:22 +08:00
|
|
|
/* signed add of b to a. */
|
2000-01-24 06:06:24 +08:00
|
|
|
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
1998-12-21 18:52:47 +08:00
|
|
|
{
|
2018-04-18 04:57:22 +08:00
|
|
|
int ret, r_neg, cmp_res;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
bn_check_top(a);
|
|
|
|
bn_check_top(b);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-04-18 04:57:22 +08:00
|
|
|
if (a->neg == b->neg) {
|
|
|
|
r_neg = a->neg;
|
|
|
|
ret = BN_uadd(r, a, b);
|
|
|
|
} else {
|
|
|
|
cmp_res = BN_ucmp(a, b);
|
|
|
|
if (cmp_res > 0) {
|
|
|
|
r_neg = a->neg;
|
|
|
|
ret = BN_usub(r, a, b);
|
|
|
|
} else if (cmp_res < 0) {
|
|
|
|
r_neg = b->neg;
|
|
|
|
ret = BN_usub(r, b, a);
|
|
|
|
} else {
|
|
|
|
r_neg = 0;
|
|
|
|
BN_zero(r);
|
|
|
|
ret = 1;
|
1998-12-21 18:52:47 +08:00
|
|
|
}
|
2018-04-18 04:57:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
r->neg = r_neg;
|
|
|
|
bn_check_top(r);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* signed sub of b from a. */
|
|
|
|
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
|
|
|
{
|
|
|
|
int ret, r_neg, cmp_res;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-04-18 04:57:22 +08:00
|
|
|
bn_check_top(a);
|
|
|
|
bn_check_top(b);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-04-18 04:57:22 +08:00
|
|
|
if (a->neg != b->neg) {
|
|
|
|
r_neg = a->neg;
|
|
|
|
ret = BN_uadd(r, a, b);
|
|
|
|
} else {
|
|
|
|
cmp_res = BN_ucmp(a, b);
|
|
|
|
if (cmp_res > 0) {
|
|
|
|
r_neg = a->neg;
|
|
|
|
ret = BN_usub(r, a, b);
|
|
|
|
} else if (cmp_res < 0) {
|
|
|
|
r_neg = !b->neg;
|
|
|
|
ret = BN_usub(r, b, a);
|
1998-12-21 18:52:47 +08:00
|
|
|
} else {
|
2018-04-18 04:57:22 +08:00
|
|
|
r_neg = 0;
|
|
|
|
BN_zero(r);
|
|
|
|
ret = 1;
|
1998-12-21 18:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2018-04-18 04:57:22 +08:00
|
|
|
r->neg = r_neg;
|
2003-11-05 06:54:49 +08:00
|
|
|
bn_check_top(r);
|
2003-12-06 19:55:46 +08:00
|
|
|
return ret;
|
1998-12-21 18:52:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-18 04:57:22 +08:00
|
|
|
/* unsigned add of b to a, r can be equal to a or b. */
|
1999-06-05 06:23:10 +08:00
|
|
|
int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
1998-12-21 18:52:47 +08:00
|
|
|
{
|
2008-11-12 11:58:08 +08:00
|
|
|
int max, min, dif;
|
2014-06-30 04:05:23 +08:00
|
|
|
const BN_ULONG *ap, *bp;
|
|
|
|
BN_ULONG *rp, carry, t1, t2;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
bn_check_top(a);
|
|
|
|
bn_check_top(b);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
if (a->top < b->top) {
|
2016-06-12 18:34:46 +08:00
|
|
|
const BIGNUM *tmp;
|
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
tmp = a;
|
|
|
|
a = b;
|
|
|
|
b = tmp;
|
|
|
|
}
|
2003-12-06 19:55:46 +08:00
|
|
|
max = a->top;
|
|
|
|
min = b->top;
|
|
|
|
dif = max - min;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
if (bn_wexpand(r, max + 1) == NULL)
|
2003-12-06 19:55:46 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
r->top = max;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 18:52:47 +08:00
|
|
|
ap = a->d;
|
|
|
|
bp = b->d;
|
|
|
|
rp = r->d;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
1998-12-21 18:56:39 +08:00
|
|
|
carry = bn_add_words(rp, ap, bp, min);
|
|
|
|
rp += min;
|
|
|
|
ap += min;
|
2016-06-12 18:34:46 +08:00
|
|
|
|
|
|
|
while (dif) {
|
|
|
|
dif--;
|
|
|
|
t1 = *(ap++);
|
|
|
|
t2 = (t1 + carry) & BN_MASK2;
|
|
|
|
*(rp++) = t2;
|
|
|
|
carry &= (t2 == 0);
|
1998-12-21 18:52:47 +08:00
|
|
|
}
|
2016-06-12 18:34:46 +08:00
|
|
|
*rp = carry;
|
|
|
|
r->top += carry;
|
|
|
|
|
2000-12-08 06:06:09 +08:00
|
|
|
r->neg = 0;
|
2003-11-05 06:54:49 +08:00
|
|
|
bn_check_top(r);
|
2003-12-06 19:55:46 +08:00
|
|
|
return 1;
|
1998-12-21 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unsigned subtraction of b from a, a must be larger than b. */
|
1999-06-05 06:23:10 +08:00
|
|
|
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
|
1998-12-21 19:00:56 +08:00
|
|
|
{
|
2008-11-12 11:58:08 +08:00
|
|
|
int max, min, dif;
|
2016-06-12 18:34:46 +08:00
|
|
|
BN_ULONG t1, t2, borrow, *rp;
|
|
|
|
const BN_ULONG *ap, *bp;
|
1998-12-21 19:00:56 +08:00
|
|
|
|
|
|
|
bn_check_top(a);
|
|
|
|
bn_check_top(b);
|
|
|
|
|
2003-12-06 19:55:46 +08:00
|
|
|
max = a->top;
|
|
|
|
min = b->top;
|
|
|
|
dif = max - min;
|
|
|
|
|
|
|
|
if (dif < 0) { /* hmm... should not be happening */
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_BN, BN_R_ARG2_LT_ARG3);
|
2016-06-12 18:34:46 +08:00
|
|
|
return 0;
|
1998-12-21 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bn_wexpand(r, max) == NULL)
|
2016-06-12 18:34:46 +08:00
|
|
|
return 0;
|
1998-12-21 19:00:56 +08:00
|
|
|
|
|
|
|
ap = a->d;
|
|
|
|
bp = b->d;
|
|
|
|
rp = r->d;
|
|
|
|
|
2016-06-12 18:34:46 +08:00
|
|
|
borrow = bn_sub_words(rp, ap, bp, min);
|
1998-12-21 19:00:56 +08:00
|
|
|
ap += min;
|
|
|
|
rp += min;
|
2016-06-12 18:34:46 +08:00
|
|
|
|
|
|
|
while (dif) {
|
|
|
|
dif--;
|
|
|
|
t1 = *(ap++);
|
|
|
|
t2 = (t1 - borrow) & BN_MASK2;
|
|
|
|
*(rp++) = t2;
|
|
|
|
borrow &= (t1 == 0);
|
1998-12-21 19:00:56 +08:00
|
|
|
}
|
|
|
|
|
2017-11-15 19:25:02 +08:00
|
|
|
while (max && *--rp == 0)
|
|
|
|
max--;
|
|
|
|
|
1998-12-21 19:00:56 +08:00
|
|
|
r->top = max;
|
2000-12-08 06:06:09 +08:00
|
|
|
r->neg = 0;
|
2017-11-15 19:25:02 +08:00
|
|
|
bn_pollute(r);
|
|
|
|
|
2016-06-12 18:34:46 +08:00
|
|
|
return 1;
|
1998-12-21 19:00:56 +08:00
|
|
|
}
|
|
|
|
|