summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_add.c
diff options
context:
space:
mode:
authorjsing <>2014-05-08 13:20:49 +0000
committerjsing <>2014-05-08 13:20:49 +0000
commit2e8879604fe3abbc2431ca79a4a923f1e87da75e (patch)
tree18398455223278c0cb2bd44f57e4499a4370f665 /src/lib/libcrypto/bn/bn_add.c
parentf7d9a959949e5f3918c1cf2b27fb4cd7b62d07d5 (diff)
downloadopenbsd-2e8879604fe3abbc2431ca79a4a923f1e87da75e.tar.gz
openbsd-2e8879604fe3abbc2431ca79a4a923f1e87da75e.tar.bz2
openbsd-2e8879604fe3abbc2431ca79a4a923f1e87da75e.zip
Emergency knfectomie requested by tedu@.
Diffstat (limited to 'src/lib/libcrypto/bn/bn_add.c')
-rw-r--r--src/lib/libcrypto/bn/bn_add.c288
1 files changed, 146 insertions, 142 deletions
diff --git a/src/lib/libcrypto/bn/bn_add.c b/src/lib/libcrypto/bn/bn_add.c
index b36615910c..47f7b0df5a 100644
--- a/src/lib/libcrypto/bn/bn_add.c
+++ b/src/lib/libcrypto/bn/bn_add.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -61,8 +61,9 @@
61#include "bn_lcl.h" 61#include "bn_lcl.h"
62 62
63/* r can == a or b */ 63/* r can == a or b */
64int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 64int
65 { 65BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
66{
66 const BIGNUM *tmp; 67 const BIGNUM *tmp;
67 int a_neg = a->neg, ret; 68 int a_neg = a->neg, ret;
68 69
@@ -74,85 +75,85 @@ int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
74 * -a + b b-a 75 * -a + b b-a
75 * -a + -b -(a+b) 76 * -a + -b -(a+b)
76 */ 77 */
77 if (a_neg ^ b->neg) 78 if (a_neg ^ b->neg) {
78 {
79 /* only one is negative */ 79 /* only one is negative */
80 if (a_neg) 80 if (a_neg)
81 { tmp=a; a=b; b=tmp; } 81 { tmp = a;
82 a = b;
83 b = tmp;
84 }
82 85
83 /* we are now a - b */ 86 /* we are now a - b */
84 87
85 if (BN_ucmp(a,b) < 0) 88 if (BN_ucmp(a, b) < 0) {
86 { 89 if (!BN_usub(r, b, a))
87 if (!BN_usub(r,b,a)) return(0); 90 return (0);
88 r->neg=1; 91 r->neg = 1;
89 } 92 } else {
90 else 93 if (!BN_usub(r, a, b))
91 { 94 return (0);
92 if (!BN_usub(r,a,b)) return(0); 95 r->neg = 0;
93 r->neg=0;
94 }
95 return(1);
96 } 96 }
97 return (1);
98 }
97 99
98 ret = BN_uadd(r,a,b); 100 ret = BN_uadd(r, a, b);
99 r->neg = a_neg; 101 r->neg = a_neg;
100 bn_check_top(r); 102 bn_check_top(r);
101 return ret; 103 return ret;
102 } 104}
103 105
104/* unsigned add of b to a */ 106/* unsigned add of b to a */
105int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 107int
106 { 108BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
107 int max,min,dif; 109{
108 BN_ULONG *ap,*bp,*rp,carry,t1,t2; 110 int max, min, dif;
111 BN_ULONG *ap, *bp, *rp, carry, t1, t2;
109 const BIGNUM *tmp; 112 const BIGNUM *tmp;
110 113
111 bn_check_top(a); 114 bn_check_top(a);
112 bn_check_top(b); 115 bn_check_top(b);
113 116
114 if (a->top < b->top) 117 if (a->top < b->top) {
115 { tmp=a; a=b; b=tmp; } 118 tmp = a;
119 a = b;
120 b = tmp;
121 }
116 max = a->top; 122 max = a->top;
117 min = b->top; 123 min = b->top;
118 dif = max - min; 124 dif = max - min;
119 125
120 if (bn_wexpand(r,max+1) == NULL) 126 if (bn_wexpand(r, max + 1) == NULL)
121 return 0; 127 return 0;
122 128
123 r->top=max; 129 r->top = max;
124
125 130
126 ap=a->d; 131 ap = a->d;
127 bp=b->d; 132 bp = b->d;
128 rp=r->d; 133 rp = r->d;
129 134
130 carry=bn_add_words(rp,ap,bp,min); 135 carry = bn_add_words(rp, ap, bp, min);
131 rp+=min; 136 rp += min;
132 ap+=min; 137 ap += min;
133 bp+=min; 138 bp += min;
134 139
135 if (carry) 140 if (carry) {
136 { 141 while (dif) {
137 while (dif)
138 {
139 dif--; 142 dif--;
140 t1 = *(ap++); 143 t1 = *(ap++);
141 t2 = (t1+1) & BN_MASK2; 144 t2 = (t1 + 1) & BN_MASK2;
142 *(rp++) = t2; 145 *(rp++) = t2;
143 if (t2) 146 if (t2) {
144 { 147 carry = 0;
145 carry=0;
146 break; 148 break;
147 }
148 } 149 }
149 if (carry) 150 }
150 { 151 if (carry) {
151 /* carry != 0 => dif == 0 */ 152 /* carry != 0 => dif == 0 */
152 *rp = 1; 153 *rp = 1;
153 r->top++; 154 r->top++;
154 }
155 } 155 }
156 }
156 if (dif && rp != ap) 157 if (dif && rp != ap)
157 while (dif--) 158 while (dif--)
158 /* copy remaining words if ap != rp */ 159 /* copy remaining words if ap != rp */
@@ -160,14 +161,15 @@ int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
160 r->neg = 0; 161 r->neg = 0;
161 bn_check_top(r); 162 bn_check_top(r);
162 return 1; 163 return 1;
163 } 164}
164 165
165/* unsigned subtraction of b from a, a must be larger than b. */ 166/* unsigned subtraction of b from a, a must be larger than b. */
166int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 167int
167 { 168BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
168 int max,min,dif; 169{
169 register BN_ULONG t1,t2,*ap,*bp,*rp; 170 int max, min, dif;
170 int i,carry; 171 register BN_ULONG t1, t2, *ap, *bp, *rp;
172 int i, carry;
171 173
172 bn_check_top(a); 174 bn_check_top(a);
173 bn_check_top(b); 175 bn_check_top(b);
@@ -177,87 +179,86 @@ int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
177 dif = max - min; 179 dif = max - min;
178 180
179 if (dif < 0) /* hmm... should not be happening */ 181 if (dif < 0) /* hmm... should not be happening */
180 { 182 {
181 BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3); 183 BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);
182 return(0); 184 return (0);
183 } 185 }
184 186
185 if (bn_wexpand(r,max) == NULL) return(0); 187 if (bn_wexpand(r, max) == NULL)
188 return (0);
186 189
187 ap=a->d; 190 ap = a->d;
188 bp=b->d; 191 bp = b->d;
189 rp=r->d; 192 rp = r->d;
190 193
191#if 1 194#if 1
192 carry=0; 195 carry = 0;
193 for (i = min; i != 0; i--) 196 for (i = min; i != 0; i--) {
194 {
195 t1= *(ap++); 197 t1= *(ap++);
196 t2= *(bp++); 198 t2= *(bp++);
197 if (carry) 199 if (carry) {
198 { 200 carry = (t1 <= t2);
199 carry=(t1 <= t2); 201 t1 = (t1 - t2 - 1)&BN_MASK2;
200 t1=(t1-t2-1)&BN_MASK2; 202 } else {
201 } 203 carry = (t1 < t2);
202 else 204 t1 = (t1 - t2)&BN_MASK2;
203 {
204 carry=(t1 < t2);
205 t1=(t1-t2)&BN_MASK2;
206 }
207 *(rp++)=t1&BN_MASK2;
208 } 205 }
206 *(rp++) = t1&BN_MASK2;
207 }
209#else 208#else
210 carry=bn_sub_words(rp,ap,bp,min); 209 carry = bn_sub_words(rp, ap, bp, min);
211 ap+=min; 210 ap += min;
212 bp+=min; 211 bp += min;
213 rp+=min; 212 rp += min;
214#endif 213#endif
215 if (carry) /* subtracted */ 214 if (carry) /* subtracted */
216 { 215 {
217 if (!dif) 216 if (!dif)
218 /* error: a < b */ 217 /* error: a < b */
219 return 0; 218 return 0;
220 while (dif) 219 while (dif) {
221 {
222 dif--; 220 dif--;
223 t1 = *(ap++); 221 t1 = *(ap++);
224 t2 = (t1-1)&BN_MASK2; 222 t2 = (t1 - 1)&BN_MASK2;
225 *(rp++) = t2; 223 *(rp++) = t2;
226 if (t1) 224 if (t1)
227 break; 225 break;
228 }
229 } 226 }
227 }
230#if 0 228#if 0
231 memcpy(rp,ap,sizeof(*rp)*(max-i)); 229 memcpy(rp, ap, sizeof(*rp)*(max - i));
232#else 230#else
233 if (rp != ap) 231 if (rp != ap) {
234 { 232 for (;;) {
235 for (;;) 233 if (!dif--)
236 { 234 break;
237 if (!dif--) break; 235 rp[0] = ap[0];
238 rp[0]=ap[0]; 236 if (!dif--)
239 if (!dif--) break; 237 break;
240 rp[1]=ap[1]; 238 rp[1] = ap[1];
241 if (!dif--) break; 239 if (!dif--)
242 rp[2]=ap[2]; 240 break;
243 if (!dif--) break; 241 rp[2] = ap[2];
244 rp[3]=ap[3]; 242 if (!dif--)
245 rp+=4; 243 break;
246 ap+=4; 244 rp[3] = ap[3];
247 } 245 rp += 4;
246 ap += 4;
248 } 247 }
248 }
249#endif 249#endif
250 250
251 r->top=max; 251 r->top = max;
252 r->neg=0; 252 r->neg = 0;
253 bn_correct_top(r); 253 bn_correct_top(r);
254 return(1); 254 return (1);
255 } 255}
256 256
257int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) 257int
258 { 258BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
259{
259 int max; 260 int max;
260 int add=0,neg=0; 261 int add = 0, neg = 0;
261 const BIGNUM *tmp; 262 const BIGNUM *tmp;
262 263
263 bn_check_top(a); 264 bn_check_top(a);
@@ -268,40 +269,43 @@ int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
268 * -a - b -(a+b) 269 * -a - b -(a+b)
269 * -a - -b b-a 270 * -a - -b b-a
270 */ 271 */
271 if (a->neg) 272 if (a->neg) {
272 { 273 if (b->neg) {
273 if (b->neg) 274 tmp = a;
274 { tmp=a; a=b; b=tmp; } 275 a = b;
275 else 276 b = tmp;
276 { add=1; neg=1; } 277 } else {
278 add = 1;
279 neg = 1;
277 } 280 }
278 else 281 } else {
279 { 282 if (b->neg) {
280 if (b->neg) { add=1; neg=0; } 283 add = 1;
284 neg = 0;
281 } 285 }
286 }
282 287
283 if (add) 288 if (add) {
284 { 289 if (!BN_uadd(r, a, b))
285 if (!BN_uadd(r,a,b)) return(0); 290 return (0);
286 r->neg=neg; 291 r->neg = neg;
287 return(1); 292 return (1);
288 } 293 }
289 294
290 /* We are actually doing a - b :-) */ 295 /* We are actually doing a - b :-) */
291 296
292 max=(a->top > b->top)?a->top:b->top; 297 max = (a->top > b->top) ? a->top : b->top;
293 if (bn_wexpand(r,max) == NULL) return(0); 298 if (bn_wexpand(r, max) == NULL)
294 if (BN_ucmp(a,b) < 0) 299 return (0);
295 { 300 if (BN_ucmp(a, b) < 0) {
296 if (!BN_usub(r,b,a)) return(0); 301 if (!BN_usub(r, b, a))
297 r->neg=1; 302 return (0);
298 } 303 r->neg = 1;
299 else 304 } else {
300 { 305 if (!BN_usub(r, a, b))
301 if (!BN_usub(r,a,b)) return(0); 306 return (0);
302 r->neg=0; 307 r->neg = 0;
303 }
304 bn_check_top(r);
305 return(1);
306 } 308 }
307 309 bn_check_top(r);
310 return (1);
311}