Debian Codegraph @

external/boringssl/crypto/bn/div.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson ([email protected]).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young ([email protected])"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson ([email protected])"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/bn.h>
58
59 #include <assert.h>
60 #include <limits.h>
61 #include <openssl/err.h>
62
63 #include "internal.h"
64
65
66 #if !defined(BN_ULLONG)
67 /* bn_div_words divides a double-width |h|,|l| by |d| and returns the result,
68 * which must fit in a |BN_ULONG|. */
69 static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) {
70 BN_ULONG dh, dl, q, ret = 0, th, tl, t;
71 int i, count = 2;
72
73 if (d == 0) {
74 return BN_MASK2;
75 }
76
77 i = BN_num_bits_word(d);
78 assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
79
80 i = BN_BITS2 - i;
81 if (h >= d) {
82 h -= d;
83 }
84
85 if (i) {
86 d <<= i;
87 h = (h << i) | (l >> (BN_BITS2 - i));
88 l <<= i;
89 }
90 dh = (d & BN_MASK2h) >> BN_BITS4;
91 dl = (d & BN_MASK2l);
92 for (;;) {
93 if ((h >> BN_BITS4) == dh) {
94 q = BN_MASK2l;
95 } else {
96 q = h / dh;
97 }
98
99 th = q * dh;
100 tl = dl * q;
101 for (;;) {
102 t = h - th;
103 if ((t & BN_MASK2h) ||
104 ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) {
105 break;
106 }
107 q--;
108 th -= dh;
109 tl -= dl;
110 }
111 t = (tl >> BN_BITS4);
112 tl = (tl << BN_BITS4) & BN_MASK2h;
113 th += t;
114
115 if (l < tl) {
116 th++;
117 }
118 l -= tl;
119 if (h < th) {
120 h += d;
121 q--;
122 }
123 h -= th;
124
125 if (--count == 0) {
126 break;
127 }
128
129 ret = q << BN_BITS4;
130 h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
131 l = (l & BN_MASK2l) << BN_BITS4;
132 }
133
134 ret |= q;
135 return ret;
136 }
137 #endif /* !defined(BN_ULLONG) */
138
139 static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
140 BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) {
141 /* GCC and Clang generate function calls to |__udivdi3| and |__umoddi3| when
142 * the |BN_ULLONG|-based C code is used.
143 *
144 * GCC bugs:
145 * * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
146 * * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721
147 * * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54183
148 * * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58897
149 * * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65668
150 *
151 * Clang bugs:
152 * * https://llvm.org/bugs/show_bug.cgi?id=6397
153 * * https://llvm.org/bugs/show_bug.cgi?id=12418
154 *
155 * These issues aren't specific to x86 and x86_64, so it might be worthwhile
156 * to add more assembly language implementations. */
157 #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__GNUC__)
158 __asm__ volatile (
159 "divl %4"
160 : "=a"(*quotient_out), "=d"(*rem_out)
161 : "a"(n1), "d"(n0), "g"(d0)
162 : "cc" );
163 #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__GNUC__)
164 __asm__ volatile (
165 "divq %4"
166 : "=a"(*quotient_out), "=d"(*rem_out)
167 : "a"(n1), "d"(n0), "g"(d0)
168 : "cc" );
169 #else
170 #if defined(BN_ULLONG)
171 BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
172 *quotient_out = (BN_ULONG)(n / d0);
173 #else
174 *quotient_out = bn_div_words(n0, n1, d0);
175 #endif
176 *rem_out = n1 - (*quotient_out * d0);
177 #endif
178 }
179
180 /* BN_div computes dv := num / divisor, rounding towards
181 * zero, and sets up rm such that dv*divisor + rm = num holds.
182 * Thus:
183 * dv->neg == num->neg ^ divisor->neg (unless the result is zero)
184 * rm->neg == num->neg (unless the remainder is zero)
185 * If 'dv' or 'rm' is NULL, the respective value is not returned. */
186 int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
187 BN_CTX *ctx) {
188 int norm_shift, i, loop;
189 BIGNUM *tmp, wnum, *snum, *sdiv, *res;
190 BN_ULONG *resp, *wnump;
191 BN_ULONG d0, d1;
192 int num_n, div_n;
193 int no_branch = 0;
194
195 /* Invalid zero-padding would have particularly bad consequences
196 * so don't just rely on bn_check_top() here */
197 if ((num->top > 0 && num->d[num->top - 1] == 0) ||
198 (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
199 OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
200 return 0;
201 }
202
203 if ((num->flags & BN_FLG_CONSTTIME) != 0 ||
204 (divisor->flags & BN_FLG_CONSTTIME) != 0) {
205 no_branch = 1;
206 }
207
208 if (BN_is_zero(divisor)) {
209 OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
210 return 0;
211 }
212
213 if (!no_branch && BN_ucmp(num, divisor) < 0) {
214 if (rm != NULL) {
215 if (BN_copy(rm, num) == NULL) {
216 return 0;
217 }
218 }
219 if (dv != NULL) {
220 BN_zero(dv);
221 }
222 return 1;
223 }
224
225 BN_CTX_start(ctx);
226 tmp = BN_CTX_get(ctx);
227 snum = BN_CTX_get(ctx);
228 sdiv = BN_CTX_get(ctx);
229 if (dv == NULL) {
230 res = BN_CTX_get(ctx);
231 } else {
232 res = dv;
233 }
234 if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL) {
235 goto err;
236 }
237
238 /* First we normalise the numbers */
239 norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
240 if (!(BN_lshift(sdiv, divisor, norm_shift))) {
241 goto err;
242 }
243 sdiv->neg = 0;
244 norm_shift += BN_BITS2;
245 if (!(BN_lshift(snum, num, norm_shift))) {
246 goto err;
247 }
248 snum->neg = 0;
249
250 if (no_branch) {
251 /* Since we don't know whether snum is larger than sdiv,
252 * we pad snum with enough zeroes without changing its
253 * value.
254 */
255 if (snum->top <= sdiv->top + 1) {
256 if (bn_wexpand(snum, sdiv->top + 2) == NULL) {
257 goto err;
258 }
259 for (i = snum->top; i < sdiv->top + 2; i++) {
260 snum->d[i] = 0;
261 }
262 snum->top = sdiv->top + 2;
263 } else {
264 if (bn_wexpand(snum, snum->top + 1) == NULL) {
265 goto err;
266 }
267 snum->d[snum->top] = 0;
268 snum->top++;
269 }
270 }
271
272 div_n = sdiv->top;
273 num_n = snum->top;
274 loop = num_n - div_n;
275 /* Lets setup a 'window' into snum
276 * This is the part that corresponds to the current
277 * 'area' being divided */
278 wnum.neg = 0;
279 wnum.d = &(snum->d[loop]);
280 wnum.top = div_n;
281 /* only needed when BN_ucmp messes up the values between top and max */
282 wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
283
284 /* Get the top 2 words of sdiv */
285 /* div_n=sdiv->top; */
286 d0 = sdiv->d[div_n - 1];
287 d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
288
289 /* pointer to the 'top' of snum */
290 wnump = &(snum->d[num_n - 1]);
291
292 /* Setup to 'res' */
293 res->neg = (num->neg ^ divisor->neg);
294 if (!bn_wexpand(res, (loop + 1))) {
295 goto err;
296 }
297 res->top = loop - no_branch;
298 resp = &(res->d[loop - 1]);
299
300 /* space for temp */
301 if (!bn_wexpand(tmp, (div_n + 1))) {
302 goto err;
303 }
304
305 if (!no_branch) {
306 if (BN_ucmp(&wnum, sdiv) >= 0) {
307 bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
308 *resp = 1;
309 } else {
310 res->top--;
311 }
312 }
313
314 /* if res->top == 0 then clear the neg value otherwise decrease
315 * the resp pointer */
316 if (res->top == 0) {
317 res->neg = 0;
318 } else {
319 resp--;
320 }
321
322 for (i = 0; i < loop - 1; i++, wnump--, resp--) {
323 BN_ULONG q, l0;
324 /* the first part of the loop uses the top two words of snum and sdiv to
325 * calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv */
326 BN_ULONG n0, n1, rem = 0;
327
328 n0 = wnump[0];
329 n1 = wnump[-1];
330 if (n0 == d0) {
331 q = BN_MASK2;
332 } else {
333 /* n0 < d0 */
334 bn_div_rem_words(&q, &rem, n0, n1, d0);
335
336 #ifdef BN_ULLONG
337 BN_ULLONG t2 = (BN_ULLONG)d1 * q;
338 for (;;) {
339 if (t2 <= ((((BN_ULLONG)rem) << BN_BITS2) | wnump[-2])) {
340 break;
341 }
342 q--;
343 rem += d0;
344 if (rem < d0) {
345 break; /* don't let rem overflow */
346 }
347 t2 -= d1;
348 }
349 #else /* !BN_ULLONG */
350 BN_ULONG t2l, t2h;
351 BN_UMULT_LOHI(t2l, t2h, d1, q);
352 for (;;) {
353 if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2]))) {
354 break;
355 }
356 q--;
357 rem += d0;
358 if (rem < d0) {
359 break; /* don't let rem overflow */
360 }
361 if (t2l < d1) {
362 t2h--;
363 }
364 t2l -= d1;
365 }
366 #endif /* !BN_ULLONG */
367 }
368
369 l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
370 tmp->d[div_n] = l0;
371 wnum.d--;
372 /* ingore top values of the bignums just sub the two
373 * BN_ULONG arrays with bn_sub_words */
374 if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
375 /* Note: As we have considered only the leading
376 * two BN_ULONGs in the calculation of q, sdiv * q
377 * might be greater than wnum (but then (q-1) * sdiv
378 * is less or equal than wnum)
379 */
380 q--;
381 if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) {
382 /* we can't have an overflow here (assuming
383 * that q != 0, but if q == 0 then tmp is
384 * zero anyway) */
385 (*wnump)++;
386 }
387 }
388 /* store part of the result */
389 *resp = q;
390 }
391 bn_correct_top(snum);
392 if (rm != NULL) {
393 /* Keep a copy of the neg flag in num because if rm==num
394 * BN_rshift() will overwrite it.
395 */
396 int neg = num->neg;
397 if (!BN_rshift(rm, snum, norm_shift)) {
398 goto err;
399 }
400 if (!BN_is_zero(rm)) {
401 rm->neg = neg;
402 }
403 }
404 if (no_branch) {
405 bn_correct_top(res);
406 }
407 BN_CTX_end(ctx);
408 return 1;
409
410 err:
411 BN_CTX_end(ctx);
412 return 0;
413 }
414
415 int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
416 if (!(BN_mod(r, m, d, ctx))) {
417 return 0;
418 }
419 if (!r->neg) {
420 return 1;
421 }
422
423 /* now -|d| < r < 0, so we have to set r := r + |d|. */
424 return (d->neg ? BN_sub : BN_add)(r, r, d);
425 }
426
427 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
428 BN_CTX *ctx) {
429 if (!BN_add(r, a, b)) {
430 return 0;
431 }
432 return BN_nnmod(r, r, m, ctx);
433 }
434
435 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
436 const BIGNUM *m) {
437 if (!BN_uadd(r, a, b)) {
438 return 0;
439 }
440 if (BN_ucmp(r, m) >= 0) {
441 return BN_usub(r, r, m);
442 }
443 return 1;
444 }
445
446 int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
447 BN_CTX *ctx) {
448 if (!BN_sub(r, a, b)) {
449 return 0;
450 }
451 return BN_nnmod(r, r, m, ctx);
452 }
453
454 /* BN_mod_sub variant that may be used if both a and b are non-negative
455 * and less than m */
456 int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
457 const BIGNUM *m) {
458 if (!BN_sub(r, a, b)) {
459 return 0;
460 }
461 if (r->neg) {
462 return BN_add(r, r, m);
463 }
464 return 1;
465 }
466
467 int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
468 BN_CTX *ctx) {
469 BIGNUM *t;
470 int ret = 0;
471
472 BN_CTX_start(ctx);
473 t = BN_CTX_get(ctx);
474 if (t == NULL) {
475 goto err;
476 }
477
478 if (a == b) {
479 if (!BN_sqr(t, a, ctx)) {
480 goto err;
481 }
482 } else {
483 if (!BN_mul(t, a, b, ctx)) {
484 goto err;
485 }
486 }
487
488 if (!BN_nnmod(r, t, m, ctx)) {
489 goto err;
490 }
491
492 ret = 1;
493
494 err:
495 BN_CTX_end(ctx);
496 return ret;
497 }
498
499 int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
500 if (!BN_sqr(r, a, ctx)) {
501 return 0;
502 }
503
504 /* r->neg == 0, thus we don't need BN_nnmod */
505 return BN_mod(r, r, m, ctx);
506 }
507
508 int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
509 BN_CTX *ctx) {
510 BIGNUM *abs_m = NULL;
511 int ret;
512
513 if (!BN_nnmod(r, a, m, ctx)) {
514 return 0;
515 }
516
517 if (m->neg) {
518 abs_m = BN_dup(m);
519 if (abs_m == NULL) {
520 return 0;
521 }
522 abs_m->neg = 0;
523 }
524
525 ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
526
527 BN_free(abs_m);
528 return ret;
529 }
530
531 int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
532 if (r != a) {
533 if (BN_copy(r, a) == NULL) {
534 return 0;
535 }
536 }
537
538 while (n > 0) {
539 int max_shift;
540
541 /* 0 < r < m */
542 max_shift = BN_num_bits(m) - BN_num_bits(r);
543 /* max_shift >= 0 */
544
545 if (max_shift < 0) {
546 OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
547 return 0;
548 }
549
550 if (max_shift > n) {
551 max_shift = n;
552 }
553
554 if (max_shift) {
555 if (!BN_lshift(r, r, max_shift)) {
556 return 0;
557 }
558 n -= max_shift;
559 } else {
560 if (!BN_lshift1(r, r)) {
561 return 0;
562 }
563 --n;
564 }
565
566 /* BN_num_bits(r) <= BN_num_bits(m) */
567 if (BN_cmp(r, m) >= 0) {
568 if (!BN_sub(r, r, m)) {
569 return 0;
570 }
571 }
572 }
573
574 return 1;
575 }
576
577 int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
578 if (!BN_lshift1(r, a)) {
579 return 0;
580 }
581
582 return BN_nnmod(r, r, m, ctx);
583 }
584
585 int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
586 if (!BN_lshift1(r, a)) {
587 return 0;
588 }
589 if (BN_cmp(r, m) >= 0) {
590 return BN_sub(r, r, m);
591 }
592
593 return 1;
594 }
595
596 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
597 BN_ULONG ret = 0;
598 int i, j;
599
600 w &= BN_MASK2;
601
602 if (!w) {
603 /* actually this an error (division by zero) */
604 return (BN_ULONG) - 1;
605 }
606
607 if (a->top == 0) {
608 return 0;
609 }
610
611 /* normalize input for |bn_div_rem_words|. */
612 j = BN_BITS2 - BN_num_bits_word(w);
613 w <<= j;
614 if (!BN_lshift(a, a, j)) {
615 return (BN_ULONG) - 1;
616 }
617
618 for (i = a->top - 1; i >= 0; i--) {
619 BN_ULONG l = a->d[i];
620 BN_ULONG d;
621 BN_ULONG unused_rem;
622 bn_div_rem_words(&d, &unused_rem, ret, l, w);
623 ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2;
624 a->d[i] = d;
625 }
626
627 if ((a->top > 0) && (a->d[a->top - 1] == 0)) {
628 a->top--;
629 }
630
631 ret >>= j;
632 return ret;
633 }
634
635 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
636 #ifndef BN_ULLONG
637 BN_ULONG ret = 0;
638 #else
639 BN_ULLONG ret = 0;
640 #endif
641 int i;
642
643 if (w == 0) {
644 return (BN_ULONG) -1;
645 }
646
647 #ifndef BN_ULLONG
648 /* If |w| is too long and we don't have |BN_ULLONG| then we need to fall back
649 * to using |BN_div_word|. */
650 if (w > ((BN_ULONG)1 << BN_BITS4)) {
651 BIGNUM *tmp = BN_dup(a);
652 if (tmp == NULL) {
653 return (BN_ULONG)-1;
654 }
655 ret = BN_div_word(tmp, w);
656 BN_free(tmp);
657 return ret;
658 }
659 #endif
660
661 w &= BN_MASK2;
662 for (i = a->top - 1; i >= 0; i--) {
663 #ifndef BN_ULLONG
664 ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
665 ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
666 #else
667 ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w);
668 #endif
669 }
670 return (BN_ULONG)ret;
671 }