summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_div.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_div.c')
-rw-r--r--src/lib/libcrypto/bn/bn_div.c263
1 files changed, 134 insertions, 129 deletions
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index e3e06ac054..04e4a24165 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.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
@@ -112,132 +112,134 @@
112 * rm->neg == num->neg (unless the remainder is zero) 112 * rm->neg == num->neg (unless the remainder is zero)
113 * If 'dv' or 'rm' is NULL, the respective value is not returned. 113 * If 'dv' or 'rm' is NULL, the respective value is not returned.
114 */ 114 */
115int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, 115int
116 BN_CTX *ctx) 116BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
117 { 117 BN_CTX *ctx)
118 int norm_shift,i,loop; 118{
119 BIGNUM *tmp,wnum,*snum,*sdiv,*res; 119 int norm_shift, i, loop;
120 BN_ULONG *resp,*wnump; 120 BIGNUM *tmp, wnum, *snum, *sdiv, *res;
121 BN_ULONG d0,d1; 121 BN_ULONG *resp, *wnump;
122 int num_n,div_n; 122 BN_ULONG d0, d1;
123 int no_branch=0; 123 int num_n, div_n;
124 int no_branch = 0;
124 125
125 /* Invalid zero-padding would have particularly bad consequences 126 /* Invalid zero-padding would have particularly bad consequences
126 * in the case of 'num', so don't just rely on bn_check_top() for this one 127 * in the case of 'num', so don't just rely on bn_check_top() for this one
127 * (bn_check_top() works only for BN_DEBUG builds) */ 128 * (bn_check_top() works only for BN_DEBUG builds) */
128 if (num->top > 0 && num->d[num->top - 1] == 0) 129 if (num->top > 0 && num->d[num->top - 1] == 0) {
129 { 130 BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
130 BNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);
131 return 0; 131 return 0;
132 } 132 }
133 133
134 bn_check_top(num); 134 bn_check_top(num);
135 135
136 if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) 136 if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) ||
137 { 137 (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {
138 no_branch=1; 138 no_branch = 1;
139 } 139 }
140 140
141 bn_check_top(dv); 141 bn_check_top(dv);
142 bn_check_top(rm); 142 bn_check_top(rm);
143 /* bn_check_top(num); */ /* 'num' has been checked already */ 143 /* bn_check_top(num); */ /* 'num' has been checked already */
144 bn_check_top(divisor); 144 bn_check_top(divisor);
145 145
146 if (BN_is_zero(divisor)) 146 if (BN_is_zero(divisor)) {
147 { 147 BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
148 BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO); 148 return (0);
149 return(0); 149 }
150 }
151 150
152 if (!no_branch && BN_ucmp(num,divisor) < 0) 151 if (!no_branch && BN_ucmp(num, divisor) < 0) {
153 { 152 if (rm != NULL) {
154 if (rm != NULL) 153 if (BN_copy(rm, num) == NULL)
155 { if (BN_copy(rm,num) == NULL) return(0); } 154 return (0);
156 if (dv != NULL) BN_zero(dv);
157 return(1);
158 } 155 }
156 if (dv != NULL)
157 BN_zero(dv);
158 return (1);
159 }
159 160
160 BN_CTX_start(ctx); 161 BN_CTX_start(ctx);
161 tmp=BN_CTX_get(ctx); 162 tmp = BN_CTX_get(ctx);
162 snum=BN_CTX_get(ctx); 163 snum = BN_CTX_get(ctx);
163 sdiv=BN_CTX_get(ctx); 164 sdiv = BN_CTX_get(ctx);
164 if (dv == NULL) 165 if (dv == NULL)
165 res=BN_CTX_get(ctx); 166 res = BN_CTX_get(ctx);
166 else res=dv; 167 else
168 res = dv;
167 if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL) 169 if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)
168 goto err; 170 goto err;
169 171
170 /* First we normalise the numbers */ 172 /* First we normalise the numbers */
171 norm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2); 173 norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
172 if (!(BN_lshift(sdiv,divisor,norm_shift))) goto err; 174 if (!(BN_lshift(sdiv, divisor, norm_shift)))
173 sdiv->neg=0; 175 goto err;
174 norm_shift+=BN_BITS2; 176 sdiv->neg = 0;
175 if (!(BN_lshift(snum,num,norm_shift))) goto err; 177 norm_shift += BN_BITS2;
176 snum->neg=0; 178 if (!(BN_lshift(snum, num, norm_shift)))
179 goto err;
180 snum->neg = 0;
177 181
178 if (no_branch) 182 if (no_branch) {
179 {
180 /* Since we don't know whether snum is larger than sdiv, 183 /* Since we don't know whether snum is larger than sdiv,
181 * we pad snum with enough zeroes without changing its 184 * we pad snum with enough zeroes without changing its
182 * value. 185 * value.
183 */ 186 */
184 if (snum->top <= sdiv->top+1) 187 if (snum->top <= sdiv->top + 1) {
185 { 188 if (bn_wexpand(snum, sdiv->top + 2) == NULL)
186 if (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err; 189 goto err;
187 for (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0; 190 for (i = snum->top; i < sdiv->top + 2; i++)
191 snum->d[i] = 0;
188 snum->top = sdiv->top + 2; 192 snum->top = sdiv->top + 2;
189 } 193 } else {
190 else 194 if (bn_wexpand(snum, snum->top + 1) == NULL)
191 { 195 goto err;
192 if (bn_wexpand(snum, snum->top + 1) == NULL) goto err;
193 snum->d[snum->top] = 0; 196 snum->d[snum->top] = 0;
194 snum->top ++; 197 snum->top ++;
195 }
196 } 198 }
199 }
197 200
198 div_n=sdiv->top; 201 div_n = sdiv->top;
199 num_n=snum->top; 202 num_n = snum->top;
200 loop=num_n-div_n; 203 loop = num_n - div_n;
201 /* Lets setup a 'window' into snum 204 /* Lets setup a 'window' into snum
202 * This is the part that corresponds to the current 205 * This is the part that corresponds to the current
203 * 'area' being divided */ 206 * 'area' being divided */
204 wnum.neg = 0; 207 wnum.neg = 0;
205 wnum.d = &(snum->d[loop]); 208 wnum.d = &(snum->d[loop]);
206 wnum.top = div_n; 209 wnum.top = div_n;
207 /* only needed when BN_ucmp messes up the values between top and max */ 210 /* only needed when BN_ucmp messes up the values between top and max */
208 wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */ 211 wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
209 212
210 /* Get the top 2 words of sdiv */ 213 /* Get the top 2 words of sdiv */
211 /* div_n=sdiv->top; */ 214 /* div_n=sdiv->top; */
212 d0=sdiv->d[div_n-1]; 215 d0 = sdiv->d[div_n - 1];
213 d1=(div_n == 1)?0:sdiv->d[div_n-2]; 216 d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
214 217
215 /* pointer to the 'top' of snum */ 218 /* pointer to the 'top' of snum */
216 wnump= &(snum->d[num_n-1]); 219 wnump = &(snum->d[num_n - 1]);
217 220
218 /* Setup to 'res' */ 221 /* Setup to 'res' */
219 res->neg= (num->neg^divisor->neg); 222 res->neg = (num->neg ^ divisor->neg);
220 if (!bn_wexpand(res,(loop+1))) goto err; 223 if (!bn_wexpand(res, (loop + 1)))
221 res->top=loop-no_branch; 224 goto err;
222 resp= &(res->d[loop-1]); 225 res->top = loop - no_branch;
226 resp = &(res->d[loop - 1]);
223 227
224 /* space for temp */ 228 /* space for temp */
225 if (!bn_wexpand(tmp,(div_n+1))) goto err; 229 if (!bn_wexpand(tmp, (div_n + 1)))
230 goto err;
226 231
227 if (!no_branch) 232 if (!no_branch) {
228 { 233 if (BN_ucmp(&wnum, sdiv) >= 0) {
229 if (BN_ucmp(&wnum,sdiv) >= 0)
230 {
231 /* If BN_DEBUG_RAND is defined BN_ucmp changes (via 234 /* If BN_DEBUG_RAND is defined BN_ucmp changes (via
232 * bn_pollute) the const bignum arguments => 235 * bn_pollute) the const bignum arguments =>
233 * clean the values between top and max again */ 236 * clean the values between top and max again */
234 bn_clear_top2max(&wnum); 237 bn_clear_top2max(&wnum);
235 bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n); 238 bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
236 *resp=1; 239 *resp = 1;
237 } 240 } else
238 else
239 res->top--; 241 res->top--;
240 } 242 }
241 243
242 /* if res->top == 0 then clear the neg value otherwise decrease 244 /* if res->top == 0 then clear the neg value otherwise decrease
243 * the resp pointer */ 245 * the resp pointer */
@@ -246,31 +248,30 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
246 else 248 else
247 resp--; 249 resp--;
248 250
249 for (i=0; i<loop-1; i++, wnump--, resp--) 251 for (i = 0; i < loop - 1; i++, wnump--, resp--) {
250 { 252 BN_ULONG q, l0;
251 BN_ULONG q,l0;
252 /* the first part of the loop uses the top two words of 253 /* the first part of the loop uses the top two words of
253 * snum and sdiv to calculate a BN_ULONG q such that 254 * snum and sdiv to calculate a BN_ULONG q such that
254 * | wnum - sdiv * q | < sdiv */ 255 * | wnum - sdiv * q | < sdiv */
255#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM) 256#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
256 BN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG); 257 BN_ULONG bn_div_3_words(BN_ULONG*, BN_ULONG, BN_ULONG);
257 q=bn_div_3_words(wnump,d1,d0); 258 q = bn_div_3_words(wnump, d1, d0);
258#else 259#else
259 BN_ULONG n0,n1,rem=0; 260 BN_ULONG n0, n1, rem = 0;
260 261
261 n0=wnump[0]; 262 n0 = wnump[0];
262 n1=wnump[-1]; 263 n1 = wnump[-1];
263 if (n0 == d0) 264 if (n0 == d0)
264 q=BN_MASK2; 265 q = BN_MASK2;
265 else /* n0 < d0 */ 266 else /* n0 < d0 */
266 { 267 {
267#ifdef BN_LLONG 268#ifdef BN_LLONG
268 BN_ULLONG t2; 269 BN_ULLONG t2;
269 270
270#if defined(BN_DIV2W) && !defined(bn_div_words) 271#if defined(BN_DIV2W) && !defined(bn_div_words)
271 q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0); 272 q = (BN_ULONG)(((((BN_ULLONG)n0) << BN_BITS2)|n1)/d0);
272#else 273#else
273 q=bn_div_words(n0,n1,d0); 274 q = bn_div_words(n0, n1, d0);
274#endif 275#endif
275 276
276#ifndef REMAINDER_IS_ALREADY_CALCULATED 277#ifndef REMAINDER_IS_ALREADY_CALCULATED
@@ -278,62 +279,65 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
278 * rem doesn't have to be BN_ULLONG. The least we 279 * rem doesn't have to be BN_ULLONG. The least we
279 * know it's less that d0, isn't it? 280 * know it's less that d0, isn't it?
280 */ 281 */
281 rem=(n1-q*d0)&BN_MASK2; 282 rem = (n1 - q * d0) & BN_MASK2;
282#endif 283#endif
283 t2=(BN_ULLONG)d1*q; 284 t2 = (BN_ULLONG)d1*q;
284 285
285 for (;;) 286 for (;;) {
286 { 287 if (t2 <= ((((BN_ULLONG)rem) << BN_BITS2) |
287 if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2])) 288 wnump[-2]))
288 break; 289 break;
289 q--; 290 q--;
290 rem += d0; 291 rem += d0;
291 if (rem < d0) break; /* don't let rem overflow */ 292 if (rem < d0) break; /* don't let rem overflow */
292 t2 -= d1; 293 t2 -= d1;
293 } 294 }
294#else /* !BN_LLONG */ 295#else /* !BN_LLONG */
295 BN_ULONG t2l,t2h; 296 BN_ULONG t2l, t2h;
296 297
297 q=bn_div_words(n0,n1,d0); 298 q = bn_div_words(n0, n1, d0);
298#ifndef REMAINDER_IS_ALREADY_CALCULATED 299#ifndef REMAINDER_IS_ALREADY_CALCULATED
299 rem=(n1-q*d0)&BN_MASK2; 300 rem = (n1 - q*d0)&BN_MASK2;
300#endif 301#endif
301 302
302#if defined(BN_UMULT_LOHI) 303#if defined(BN_UMULT_LOHI)
303 BN_UMULT_LOHI(t2l,t2h,d1,q); 304 BN_UMULT_LOHI(t2l, t2h, d1, q);
304#elif defined(BN_UMULT_HIGH) 305#elif defined(BN_UMULT_HIGH)
305 t2l = d1 * q; 306 t2l = d1 * q;
306 t2h = BN_UMULT_HIGH(d1,q); 307 t2h = BN_UMULT_HIGH(d1, q);
307#else 308#else
308 { 309 {
309 BN_ULONG ql, qh; 310 BN_ULONG ql, qh;
310 t2l=LBITS(d1); t2h=HBITS(d1); 311 t2l = LBITS(d1);
311 ql =LBITS(q); qh =HBITS(q); 312 t2h = HBITS(d1);
312 mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ 313 ql = LBITS(q);
314 qh = HBITS(q);
315 mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
313 } 316 }
314#endif 317#endif
315 318
316 for (;;) 319 for (;;) {
317 {
318 if ((t2h < rem) || 320 if ((t2h < rem) ||
319 ((t2h == rem) && (t2l <= wnump[-2]))) 321 ((t2h == rem) && (t2l <= wnump[-2])))
320 break; 322 break;
321 q--; 323 q--;
322 rem += d0; 324 rem += d0;
323 if (rem < d0) break; /* don't let rem overflow */ 325 if (rem < d0)
324 if (t2l < d1) t2h--; t2l -= d1; 326 break; /* don't let rem overflow */
325 } 327 if (t2l < d1)
326#endif /* !BN_LLONG */ 328 t2h--;
329 t2l -= d1;
327 } 330 }
331#endif /* !BN_LLONG */
332 }
328#endif /* !BN_DIV3W */ 333#endif /* !BN_DIV3W */
329 334
330 l0=bn_mul_words(tmp->d,sdiv->d,div_n,q); 335 l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
331 tmp->d[div_n]=l0; 336 tmp->d[div_n] = l0;
332 wnum.d--; 337 wnum.d--;
333 /* ingore top values of the bignums just sub the two 338 /* ingore top values of the bignums just sub the two
334 * BN_ULONG arrays with bn_sub_words */ 339 * BN_ULONG arrays with bn_sub_words */
335 if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1)) 340 if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
336 {
337 /* Note: As we have considered only the leading 341 /* Note: As we have considered only the leading
338 * two BN_ULONGs in the calculation of q, sdiv * q 342 * two BN_ULONGs in the calculation of q, sdiv * q
339 * might be greater than wnum (but then (q-1) * sdiv 343 * might be greater than wnum (but then (q-1) * sdiv
@@ -345,27 +349,28 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
345 * that q != 0, but if q == 0 then tmp is 349 * that q != 0, but if q == 0 then tmp is
346 * zero anyway) */ 350 * zero anyway) */
347 (*wnump)++; 351 (*wnump)++;
348 } 352 }
349 /* store part of the result */ 353 /* store part of the result */
350 *resp = q; 354 *resp = q;
351 } 355 }
352 bn_correct_top(snum); 356 bn_correct_top(snum);
353 if (rm != NULL) 357 if (rm != NULL) {
354 {
355 /* Keep a copy of the neg flag in num because if rm==num 358 /* Keep a copy of the neg flag in num because if rm==num
356 * BN_rshift() will overwrite it. 359 * BN_rshift() will overwrite it.
357 */ 360 */
358 int neg = num->neg; 361 int neg = num->neg;
359 BN_rshift(rm,snum,norm_shift); 362 BN_rshift(rm, snum, norm_shift);
360 if (!BN_is_zero(rm)) 363 if (!BN_is_zero(rm))
361 rm->neg = neg; 364 rm->neg = neg;
362 bn_check_top(rm); 365 bn_check_top(rm);
363 } 366 }
364 if (no_branch) bn_correct_top(res); 367 if (no_branch)
368 bn_correct_top(res);
365 BN_CTX_end(ctx); 369 BN_CTX_end(ctx);
366 return(1); 370 return (1);
371
367err: 372err:
368 bn_check_top(rm); 373 bn_check_top(rm);
369 BN_CTX_end(ctx); 374 BN_CTX_end(ctx);
370 return(0); 375 return (0);
371 } 376}