/src/openssl/crypto/bn/bn_add.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/cryptlib.h" |
11 | | #include "bn_lcl.h" |
12 | | |
13 | | /* signed add of b to a. */ |
14 | | int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
15 | 0 | { |
16 | 0 | int ret, r_neg, cmp_res; |
17 | 0 |
|
18 | 0 | bn_check_top(a); |
19 | 0 | bn_check_top(b); |
20 | 0 |
|
21 | 0 | if (a->neg == b->neg) { |
22 | 0 | r_neg = a->neg; |
23 | 0 | ret = BN_uadd(r, a, b); |
24 | 0 | } else { |
25 | 0 | cmp_res = BN_ucmp(a, b); |
26 | 0 | if (cmp_res > 0) { |
27 | 0 | r_neg = a->neg; |
28 | 0 | ret = BN_usub(r, a, b); |
29 | 0 | } else if (cmp_res < 0) { |
30 | 0 | r_neg = b->neg; |
31 | 0 | ret = BN_usub(r, b, a); |
32 | 0 | } else { |
33 | 0 | r_neg = 0; |
34 | 0 | BN_zero(r); |
35 | 0 | ret = 1; |
36 | 0 | } |
37 | 0 | } |
38 | 0 |
|
39 | 0 | r->neg = r_neg; |
40 | 0 | bn_check_top(r); |
41 | 0 | return ret; |
42 | 0 | } |
43 | | |
44 | | /* signed sub of b from a. */ |
45 | | int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
46 | 0 | { |
47 | 0 | int ret, r_neg, cmp_res; |
48 | 0 |
|
49 | 0 | bn_check_top(a); |
50 | 0 | bn_check_top(b); |
51 | 0 |
|
52 | 0 | if (a->neg != b->neg) { |
53 | 0 | r_neg = a->neg; |
54 | 0 | ret = BN_uadd(r, a, b); |
55 | 0 | } else { |
56 | 0 | cmp_res = BN_ucmp(a, b); |
57 | 0 | if (cmp_res > 0) { |
58 | 0 | r_neg = a->neg; |
59 | 0 | ret = BN_usub(r, a, b); |
60 | 0 | } else if (cmp_res < 0) { |
61 | 0 | r_neg = !b->neg; |
62 | 0 | ret = BN_usub(r, b, a); |
63 | 0 | } else { |
64 | 0 | r_neg = 0; |
65 | 0 | BN_zero(r); |
66 | 0 | ret = 1; |
67 | 0 | } |
68 | 0 | } |
69 | 0 |
|
70 | 0 | r->neg = r_neg; |
71 | 0 | bn_check_top(r); |
72 | 0 | return ret; |
73 | 0 | } |
74 | | |
75 | | /* unsigned add of b to a, r can be equal to a or b. */ |
76 | | int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
77 | 0 | { |
78 | 0 | int max, min, dif; |
79 | 0 | const BN_ULONG *ap, *bp; |
80 | 0 | BN_ULONG *rp, carry, t1, t2; |
81 | 0 |
|
82 | 0 | bn_check_top(a); |
83 | 0 | bn_check_top(b); |
84 | 0 |
|
85 | 0 | if (a->top < b->top) { |
86 | 0 | const BIGNUM *tmp; |
87 | 0 |
|
88 | 0 | tmp = a; |
89 | 0 | a = b; |
90 | 0 | b = tmp; |
91 | 0 | } |
92 | 0 | max = a->top; |
93 | 0 | min = b->top; |
94 | 0 | dif = max - min; |
95 | 0 |
|
96 | 0 | if (bn_wexpand(r, max + 1) == NULL) |
97 | 0 | return 0; |
98 | 0 | |
99 | 0 | r->top = max; |
100 | 0 |
|
101 | 0 | ap = a->d; |
102 | 0 | bp = b->d; |
103 | 0 | rp = r->d; |
104 | 0 |
|
105 | 0 | carry = bn_add_words(rp, ap, bp, min); |
106 | 0 | rp += min; |
107 | 0 | ap += min; |
108 | 0 |
|
109 | 0 | while (dif) { |
110 | 0 | dif--; |
111 | 0 | t1 = *(ap++); |
112 | 0 | t2 = (t1 + carry) & BN_MASK2; |
113 | 0 | *(rp++) = t2; |
114 | 0 | carry &= (t2 == 0); |
115 | 0 | } |
116 | 0 | *rp = carry; |
117 | 0 | r->top += carry; |
118 | 0 |
|
119 | 0 | r->neg = 0; |
120 | 0 | bn_check_top(r); |
121 | 0 | return 1; |
122 | 0 | } |
123 | | |
124 | | /* unsigned subtraction of b from a, a must be larger than b. */ |
125 | | int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) |
126 | 0 | { |
127 | 0 | int max, min, dif; |
128 | 0 | BN_ULONG t1, t2, borrow, *rp; |
129 | 0 | const BN_ULONG *ap, *bp; |
130 | 0 |
|
131 | 0 | bn_check_top(a); |
132 | 0 | bn_check_top(b); |
133 | 0 |
|
134 | 0 | max = a->top; |
135 | 0 | min = b->top; |
136 | 0 | dif = max - min; |
137 | 0 |
|
138 | 0 | if (dif < 0) { /* hmm... should not be happening */ |
139 | 0 | BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3); |
140 | 0 | return 0; |
141 | 0 | } |
142 | 0 |
|
143 | 0 | if (bn_wexpand(r, max) == NULL) |
144 | 0 | return 0; |
145 | 0 | |
146 | 0 | ap = a->d; |
147 | 0 | bp = b->d; |
148 | 0 | rp = r->d; |
149 | 0 |
|
150 | 0 | borrow = bn_sub_words(rp, ap, bp, min); |
151 | 0 | ap += min; |
152 | 0 | rp += min; |
153 | 0 |
|
154 | 0 | while (dif) { |
155 | 0 | dif--; |
156 | 0 | t1 = *(ap++); |
157 | 0 | t2 = (t1 - borrow) & BN_MASK2; |
158 | 0 | *(rp++) = t2; |
159 | 0 | borrow &= (t1 == 0); |
160 | 0 | } |
161 | 0 |
|
162 | 0 | while (max && *--rp == 0) |
163 | 0 | max--; |
164 | 0 |
|
165 | 0 | r->top = max; |
166 | 0 | r->neg = 0; |
167 | 0 | bn_pollute(r); |
168 | 0 |
|
169 | 0 | return 1; |
170 | 0 | } |
171 | | |