xref: /dragonfly/crypto/libressl/crypto/bn/bn_lib.c (revision cca6fc52)
1 /* $OpenBSD: bn_lib.c,v 1.47 2019/06/17 17:11:48 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
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
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
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.
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.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
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:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
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
51  * SUCH DAMAGE.
52  *
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
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #ifndef BN_DEBUG
60 # undef NDEBUG /* avoid conflicting definitions */
61 # define NDEBUG
62 #endif
63 
64 #include <assert.h>
65 #include <limits.h>
66 #include <stdio.h>
67 #include <string.h>
68 
69 #include <openssl/opensslconf.h>
70 
71 #include <openssl/err.h>
72 
73 #include "bn_lcl.h"
74 
75 /* This stuff appears to be completely unused, so is deprecated */
76 #ifndef OPENSSL_NO_DEPRECATED
77 /* For a 32 bit machine
78  * 2 -   4 ==  128
79  * 3 -   8 ==  256
80  * 4 -  16 ==  512
81  * 5 -  32 == 1024
82  * 6 -  64 == 2048
83  * 7 - 128 == 4096
84  * 8 - 256 == 8192
85  */
86 static int bn_limit_bits = 0;
87 static int bn_limit_num = 8;        /* (1<<bn_limit_bits) */
88 static int bn_limit_bits_low = 0;
89 static int bn_limit_num_low = 8;    /* (1<<bn_limit_bits_low) */
90 static int bn_limit_bits_high = 0;
91 static int bn_limit_num_high = 8;   /* (1<<bn_limit_bits_high) */
92 static int bn_limit_bits_mont = 0;
93 static int bn_limit_num_mont = 8;   /* (1<<bn_limit_bits_mont) */
94 
95 void
96 BN_set_params(int mult, int high, int low, int mont)
97 {
98 	if (mult >= 0) {
99 		if (mult > (int)(sizeof(int) * 8) - 1)
100 			mult = sizeof(int) * 8 - 1;
101 		bn_limit_bits = mult;
102 		bn_limit_num = 1 << mult;
103 	}
104 	if (high >= 0) {
105 		if (high > (int)(sizeof(int) * 8) - 1)
106 			high = sizeof(int) * 8 - 1;
107 		bn_limit_bits_high = high;
108 		bn_limit_num_high = 1 << high;
109 	}
110 	if (low >= 0) {
111 		if (low > (int)(sizeof(int) * 8) - 1)
112 			low = sizeof(int) * 8 - 1;
113 		bn_limit_bits_low = low;
114 		bn_limit_num_low = 1 << low;
115 	}
116 	if (mont >= 0) {
117 		if (mont > (int)(sizeof(int) * 8) - 1)
118 			mont = sizeof(int) * 8 - 1;
119 		bn_limit_bits_mont = mont;
120 		bn_limit_num_mont = 1 << mont;
121 	}
122 }
123 
124 int
125 BN_get_params(int which)
126 {
127 	if (which == 0)
128 		return (bn_limit_bits);
129 	else if (which == 1)
130 		return (bn_limit_bits_high);
131 	else if (which == 2)
132 		return (bn_limit_bits_low);
133 	else if (which == 3)
134 		return (bn_limit_bits_mont);
135 	else
136 		return (0);
137 }
138 #endif
139 
140 const BIGNUM *
141 BN_value_one(void)
142 {
143 	static const BN_ULONG data_one = 1L;
144 	static const BIGNUM const_one = {
145 		(BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA
146 	};
147 
148 	return (&const_one);
149 }
150 
151 int
152 BN_num_bits_word(BN_ULONG l)
153 {
154 	BN_ULONG x, mask;
155 	int bits;
156 	unsigned int shift;
157 
158 	/* Constant time calculation of floor(log2(l)) + 1. */
159 	bits = (l != 0);
160 	shift = BN_BITS4;	/* On _LP64 this is 32, otherwise 16. */
161 	do {
162 		x = l >> shift;
163 		/* If x is 0, set mask to 0, otherwise set it to all 1s. */
164 		mask = ((~x & (x - 1)) >> (BN_BITS2 - 1)) - 1;
165 		bits += shift & mask;
166 		/* If x is 0, leave l alone, otherwise set l = x. */
167 		l ^= (x ^ l) & mask;
168 	} while ((shift /= 2) != 0);
169 
170 	return bits;
171 }
172 
173 int
174 BN_num_bits(const BIGNUM *a)
175 {
176 	int i = a->top - 1;
177 
178 	bn_check_top(a);
179 
180 	if (BN_is_zero(a))
181 		return 0;
182 	return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
183 }
184 
185 void
186 BN_clear_free(BIGNUM *a)
187 {
188 	int i;
189 
190 	if (a == NULL)
191 		return;
192 	bn_check_top(a);
193 	if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA)))
194 		freezero(a->d, a->dmax * sizeof(a->d[0]));
195 	i = BN_get_flags(a, BN_FLG_MALLOCED);
196 	explicit_bzero(a, sizeof(BIGNUM));
197 	if (i)
198 		free(a);
199 }
200 
201 void
202 BN_free(BIGNUM *a)
203 {
204 	BN_clear_free(a);
205 }
206 
207 void
208 BN_init(BIGNUM *a)
209 {
210 	memset(a, 0, sizeof(BIGNUM));
211 	bn_check_top(a);
212 }
213 
214 BIGNUM *
215 BN_new(void)
216 {
217 	BIGNUM *ret;
218 
219 	if ((ret = malloc(sizeof(BIGNUM))) == NULL) {
220 		BNerror(ERR_R_MALLOC_FAILURE);
221 		return (NULL);
222 	}
223 	ret->flags = BN_FLG_MALLOCED;
224 	ret->top = 0;
225 	ret->neg = 0;
226 	ret->dmax = 0;
227 	ret->d = NULL;
228 	bn_check_top(ret);
229 	return (ret);
230 }
231 
232 /* This is used both by bn_expand2() and bn_dup_expand() */
233 /* The caller MUST check that words > b->dmax before calling this */
234 static BN_ULONG *
235 bn_expand_internal(const BIGNUM *b, int words)
236 {
237 	BN_ULONG *A, *a = NULL;
238 	const BN_ULONG *B;
239 	int i;
240 
241 	bn_check_top(b);
242 
243 	if (words > (INT_MAX/(4*BN_BITS2))) {
244 		BNerror(BN_R_BIGNUM_TOO_LONG);
245 		return NULL;
246 	}
247 	if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
248 		BNerror(BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
249 		return (NULL);
250 	}
251 	a = A = reallocarray(NULL, words, sizeof(BN_ULONG));
252 	if (A == NULL) {
253 		BNerror(ERR_R_MALLOC_FAILURE);
254 		return (NULL);
255 	}
256 #if 1
257 	B = b->d;
258 	/* Check if the previous number needs to be copied */
259 	if (B != NULL) {
260 		for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
261 			/*
262 			 * The fact that the loop is unrolled
263 			 * 4-wise is a tribute to Intel. It's
264 			 * the one that doesn't have enough
265 			 * registers to accommodate more data.
266 			 * I'd unroll it 8-wise otherwise:-)
267 			 *
268 			 *		<appro@fy.chalmers.se>
269 			 */
270 			BN_ULONG a0, a1, a2, a3;
271 			a0 = B[0];
272 			a1 = B[1];
273 			a2 = B[2];
274 			a3 = B[3];
275 			A[0] = a0;
276 			A[1] = a1;
277 			A[2] = a2;
278 			A[3] = a3;
279 		}
280 		switch (b->top & 3) {
281 		case 3:
282 			A[2] = B[2];
283 		case 2:
284 			A[1] = B[1];
285 		case 1:
286 			A[0] = B[0];
287 		}
288 	}
289 
290 #else
291 	memset(A, 0, sizeof(BN_ULONG) * words);
292 	memcpy(A, b->d, sizeof(b->d[0]) * b->top);
293 #endif
294 
295 	return (a);
296 }
297 
298 /* This is an internal function that can be used instead of bn_expand2()
299  * when there is a need to copy BIGNUMs instead of only expanding the
300  * data part, while still expanding them.
301  * Especially useful when needing to expand BIGNUMs that are declared
302  * 'const' and should therefore not be changed.
303  * The reason to use this instead of a BN_dup() followed by a bn_expand2()
304  * is memory allocation overhead.  A BN_dup() followed by a bn_expand2()
305  * will allocate new memory for the BIGNUM data twice, and free it once,
306  * while bn_dup_expand() makes sure allocation is made only once.
307  */
308 
309 #ifndef OPENSSL_NO_DEPRECATED
310 BIGNUM *
311 bn_dup_expand(const BIGNUM *b, int words)
312 {
313 	BIGNUM *r = NULL;
314 
315 	bn_check_top(b);
316 
317 	/* This function does not work if
318 	 *      words <= b->dmax && top < words
319 	 * because BN_dup() does not preserve 'dmax'!
320 	 * (But bn_dup_expand() is not used anywhere yet.)
321 	 */
322 
323 	if (words > b->dmax) {
324 		BN_ULONG *a = bn_expand_internal(b, words);
325 
326 		if (a) {
327 			r = BN_new();
328 			if (r) {
329 				r->top = b->top;
330 				r->dmax = words;
331 				r->neg = b->neg;
332 				r->d = a;
333 			} else {
334 				/* r == NULL, BN_new failure */
335 				free(a);
336 			}
337 		}
338 		/* If a == NULL, there was an error in allocation in
339 		   bn_expand_internal(), and NULL should be returned */
340 	} else {
341 		r = BN_dup(b);
342 	}
343 
344 	bn_check_top(r);
345 	return r;
346 }
347 #endif
348 
349 /* This is an internal function that should not be used in applications.
350  * It ensures that 'b' has enough room for a 'words' word number
351  * and initialises any unused part of b->d with leading zeros.
352  * It is mostly used by the various BIGNUM routines. If there is an error,
353  * NULL is returned. If not, 'b' is returned. */
354 
355 BIGNUM *
356 bn_expand2(BIGNUM *b, int words)
357 {
358 	bn_check_top(b);
359 
360 	if (words > b->dmax) {
361 		BN_ULONG *a = bn_expand_internal(b, words);
362 		if (!a)
363 			return NULL;
364 		if (b->d)
365 			freezero(b->d, b->dmax * sizeof(b->d[0]));
366 		b->d = a;
367 		b->dmax = words;
368 	}
369 
370 /* None of this should be necessary because of what b->top means! */
371 #if 0
372 	/* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */
373 	if (b->top < b->dmax) {
374 		int i;
375 		BN_ULONG *A = &(b->d[b->top]);
376 		for (i = (b->dmax - b->top) >> 3; i > 0; i--, A += 8) {
377 			A[0] = 0;
378 			A[1] = 0;
379 			A[2] = 0;
380 			A[3] = 0;
381 			A[4] = 0;
382 			A[5] = 0;
383 			A[6] = 0;
384 			A[7] = 0;
385 		}
386 		for (i = (b->dmax - b->top)&7; i > 0; i--, A++)
387 			A[0] = 0;
388 		assert(A == &(b->d[b->dmax]));
389 	}
390 #endif
391 	bn_check_top(b);
392 	return b;
393 }
394 
395 BIGNUM *
396 BN_dup(const BIGNUM *a)
397 {
398 	BIGNUM *t;
399 
400 	if (a == NULL)
401 		return NULL;
402 	bn_check_top(a);
403 
404 	t = BN_new();
405 	if (t == NULL)
406 		return NULL;
407 	if (!BN_copy(t, a)) {
408 		BN_free(t);
409 		return NULL;
410 	}
411 	bn_check_top(t);
412 	return t;
413 }
414 
415 BIGNUM *
416 BN_copy(BIGNUM *a, const BIGNUM *b)
417 {
418 	int i;
419 	BN_ULONG *A;
420 	const BN_ULONG *B;
421 
422 	bn_check_top(b);
423 
424 	if (a == b)
425 		return (a);
426 	if (bn_wexpand(a, b->top) == NULL)
427 		return (NULL);
428 
429 #if 1
430 	A = a->d;
431 	B = b->d;
432 	for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
433 		BN_ULONG a0, a1, a2, a3;
434 		a0 = B[0];
435 		a1 = B[1];
436 		a2 = B[2];
437 		a3 = B[3];
438 		A[0] = a0;
439 		A[1] = a1;
440 		A[2] = a2;
441 		A[3] = a3;
442 	}
443 	switch (b->top & 3) {
444 	case 3:
445 		A[2] = B[2];
446 	case 2:
447 		A[1] = B[1];
448 	case 1:
449 		A[0] = B[0];
450 	}
451 #else
452 	memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
453 #endif
454 
455 	a->top = b->top;
456 	a->neg = b->neg;
457 	bn_check_top(a);
458 	return (a);
459 }
460 
461 void
462 BN_swap(BIGNUM *a, BIGNUM *b)
463 {
464 	int flags_old_a, flags_old_b;
465 	BN_ULONG *tmp_d;
466 	int tmp_top, tmp_dmax, tmp_neg;
467 
468 	bn_check_top(a);
469 	bn_check_top(b);
470 
471 	flags_old_a = a->flags;
472 	flags_old_b = b->flags;
473 
474 	tmp_d = a->d;
475 	tmp_top = a->top;
476 	tmp_dmax = a->dmax;
477 	tmp_neg = a->neg;
478 
479 	a->d = b->d;
480 	a->top = b->top;
481 	a->dmax = b->dmax;
482 	a->neg = b->neg;
483 
484 	b->d = tmp_d;
485 	b->top = tmp_top;
486 	b->dmax = tmp_dmax;
487 	b->neg = tmp_neg;
488 
489 	a->flags = (flags_old_a & BN_FLG_MALLOCED) |
490 	    (flags_old_b & BN_FLG_STATIC_DATA);
491 	b->flags = (flags_old_b & BN_FLG_MALLOCED) |
492 	    (flags_old_a & BN_FLG_STATIC_DATA);
493 	bn_check_top(a);
494 	bn_check_top(b);
495 }
496 
497 void
498 BN_clear(BIGNUM *a)
499 {
500 	bn_check_top(a);
501 	if (a->d != NULL)
502 		explicit_bzero(a->d, a->dmax * sizeof(a->d[0]));
503 	a->top = 0;
504 	a->neg = 0;
505 }
506 
507 BN_ULONG
508 BN_get_word(const BIGNUM *a)
509 {
510 	if (a->top > 1)
511 		return BN_MASK2;
512 	else if (a->top == 1)
513 		return a->d[0];
514 	/* a->top == 0 */
515 	return 0;
516 }
517 
518 BIGNUM *
519 bn_expand(BIGNUM *a, int bits)
520 {
521 	if (bits > (INT_MAX - BN_BITS2 + 1))
522 		return (NULL);
523 
524 	if (((bits + BN_BITS2 - 1) / BN_BITS2) <= a->dmax)
525 		return (a);
526 
527 	return bn_expand2(a, (bits + BN_BITS2 - 1) / BN_BITS2);
528 }
529 
530 int
531 BN_set_word(BIGNUM *a, BN_ULONG w)
532 {
533 	bn_check_top(a);
534 	if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
535 		return (0);
536 	a->neg = 0;
537 	a->d[0] = w;
538 	a->top = (w ? 1 : 0);
539 	bn_check_top(a);
540 	return (1);
541 }
542 
543 BIGNUM *
544 BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
545 {
546 	unsigned int i, m;
547 	unsigned int n;
548 	BN_ULONG l;
549 	BIGNUM *bn = NULL;
550 
551 	if (len < 0)
552 		return (NULL);
553 	if (ret == NULL)
554 		ret = bn = BN_new();
555 	if (ret == NULL)
556 		return (NULL);
557 	bn_check_top(ret);
558 	l = 0;
559 	n = len;
560 	if (n == 0) {
561 		ret->top = 0;
562 		return (ret);
563 	}
564 	i = ((n - 1) / BN_BYTES) + 1;
565 	m = ((n - 1) % (BN_BYTES));
566 	if (bn_wexpand(ret, (int)i) == NULL) {
567 		BN_free(bn);
568 		return NULL;
569 	}
570 	ret->top = i;
571 	ret->neg = 0;
572 	while (n--) {
573 		l = (l << 8L) | *(s++);
574 		if (m-- == 0) {
575 			ret->d[--i] = l;
576 			l = 0;
577 			m = BN_BYTES - 1;
578 		}
579 	}
580 	/* need to call this due to clear byte at top if avoiding
581 	 * having the top bit set (-ve number) */
582 	bn_correct_top(ret);
583 	return (ret);
584 }
585 
586 /* ignore negative */
587 int
588 BN_bn2bin(const BIGNUM *a, unsigned char *to)
589 {
590 	int n, i;
591 	BN_ULONG l;
592 
593 	bn_check_top(a);
594 	n = i=BN_num_bytes(a);
595 	while (i--) {
596 		l = a->d[i / BN_BYTES];
597 		*(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
598 	}
599 	return (n);
600 }
601 
602 int
603 BN_ucmp(const BIGNUM *a, const BIGNUM *b)
604 {
605 	int i;
606 	BN_ULONG t1, t2, *ap, *bp;
607 
608 	bn_check_top(a);
609 	bn_check_top(b);
610 
611 	i = a->top - b->top;
612 	if (i != 0)
613 		return (i);
614 	ap = a->d;
615 	bp = b->d;
616 	for (i = a->top - 1; i >= 0; i--) {
617 		t1 = ap[i];
618 		t2 = bp[i];
619 		if (t1 != t2)
620 			return ((t1 > t2) ? 1 : -1);
621 	}
622 	return (0);
623 }
624 
625 int
626 BN_cmp(const BIGNUM *a, const BIGNUM *b)
627 {
628 	int i;
629 	int gt, lt;
630 	BN_ULONG t1, t2;
631 
632 	if ((a == NULL) || (b == NULL)) {
633 		if (a != NULL)
634 			return (-1);
635 		else if (b != NULL)
636 			return (1);
637 		else
638 			return (0);
639 	}
640 
641 	bn_check_top(a);
642 	bn_check_top(b);
643 
644 	if (a->neg != b->neg) {
645 		if (a->neg)
646 			return (-1);
647 		else
648 			return (1);
649 	}
650 	if (a->neg == 0) {
651 		gt = 1;
652 		lt = -1;
653 	} else {
654 		gt = -1;
655 		lt = 1;
656 	}
657 
658 	if (a->top > b->top)
659 		return (gt);
660 	if (a->top < b->top)
661 		return (lt);
662 	for (i = a->top - 1; i >= 0; i--) {
663 		t1 = a->d[i];
664 		t2 = b->d[i];
665 		if (t1 > t2)
666 			return (gt);
667 		if (t1 < t2)
668 			return (lt);
669 	}
670 	return (0);
671 }
672 
673 int
674 BN_set_bit(BIGNUM *a, int n)
675 {
676 	int i, j, k;
677 
678 	if (n < 0)
679 		return 0;
680 
681 	i = n / BN_BITS2;
682 	j = n % BN_BITS2;
683 	if (a->top <= i) {
684 		if (bn_wexpand(a, i + 1) == NULL)
685 			return (0);
686 		for (k = a->top; k < i + 1; k++)
687 			a->d[k] = 0;
688 		a->top = i + 1;
689 	}
690 
691 	a->d[i] |= (((BN_ULONG)1) << j);
692 	bn_check_top(a);
693 	return (1);
694 }
695 
696 int
697 BN_clear_bit(BIGNUM *a, int n)
698 {
699 	int i, j;
700 
701 	bn_check_top(a);
702 	if (n < 0)
703 		return 0;
704 
705 	i = n / BN_BITS2;
706 	j = n % BN_BITS2;
707 	if (a->top <= i)
708 		return (0);
709 
710 	a->d[i] &= (~(((BN_ULONG)1) << j));
711 	bn_correct_top(a);
712 	return (1);
713 }
714 
715 int
716 BN_is_bit_set(const BIGNUM *a, int n)
717 {
718 	int i, j;
719 
720 	bn_check_top(a);
721 	if (n < 0)
722 		return 0;
723 	i = n / BN_BITS2;
724 	j = n % BN_BITS2;
725 	if (a->top <= i)
726 		return 0;
727 	return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
728 }
729 
730 int
731 BN_mask_bits(BIGNUM *a, int n)
732 {
733 	int b, w;
734 
735 	bn_check_top(a);
736 	if (n < 0)
737 		return 0;
738 
739 	w = n / BN_BITS2;
740 	b = n % BN_BITS2;
741 	if (w >= a->top)
742 		return 0;
743 	if (b == 0)
744 		a->top = w;
745 	else {
746 		a->top = w + 1;
747 		a->d[w] &= ~(BN_MASK2 << b);
748 	}
749 	bn_correct_top(a);
750 	return (1);
751 }
752 
753 void
754 BN_set_negative(BIGNUM *a, int b)
755 {
756 	if (b && !BN_is_zero(a))
757 		a->neg = 1;
758 	else
759 		a->neg = 0;
760 }
761 
762 int
763 bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
764 {
765 	int i;
766 	BN_ULONG aa, bb;
767 
768 	aa = a[n - 1];
769 	bb = b[n - 1];
770 	if (aa != bb)
771 		return ((aa > bb) ? 1 : -1);
772 	for (i = n - 2; i >= 0; i--) {
773 		aa = a[i];
774 		bb = b[i];
775 		if (aa != bb)
776 			return ((aa > bb) ? 1 : -1);
777 	}
778 	return (0);
779 }
780 
781 /* Here follows a specialised variants of bn_cmp_words().  It has the
782    property of performing the operation on arrays of different sizes.
783    The sizes of those arrays is expressed through cl, which is the
784    common length ( basicall, min(len(a),len(b)) ), and dl, which is the
785    delta between the two lengths, calculated as len(a)-len(b).
786    All lengths are the number of BN_ULONGs...  */
787 
788 int
789 bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
790 {
791 	int n, i;
792 
793 	n = cl - 1;
794 
795 	if (dl < 0) {
796 		for (i = dl; i < 0; i++) {
797 			if (b[n - i] != 0)
798 				return -1; /* a < b */
799 		}
800 	}
801 	if (dl > 0) {
802 		for (i = dl; i > 0; i--) {
803 			if (a[n + i] != 0)
804 				return 1; /* a > b */
805 		}
806 	}
807 	return bn_cmp_words(a, b, cl);
808 }
809 
810 /*
811  * Constant-time conditional swap of a and b.
812  * a and b are swapped if condition is not 0.
813  * The code assumes that at most one bit of condition is set.
814  * nwords is the number of words to swap.
815  * The code assumes that at least nwords are allocated in both a and b,
816  * and that no more than nwords are used by either a or b.
817  * a and b cannot be the same number
818  */
819 void
820 BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
821 {
822 	BN_ULONG t;
823 	int i;
824 
825 	bn_wcheck_size(a, nwords);
826 	bn_wcheck_size(b, nwords);
827 
828 	assert(a != b);
829 	assert((condition & (condition - 1)) == 0);
830 	assert(sizeof(BN_ULONG) >= sizeof(int));
831 
832 	condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
833 
834 	t = (a->top^b->top) & condition;
835 	a->top ^= t;
836 	b->top ^= t;
837 
838 #define BN_CONSTTIME_SWAP(ind) \
839 	do { \
840 		t = (a->d[ind] ^ b->d[ind]) & condition; \
841 		a->d[ind] ^= t; \
842 		b->d[ind] ^= t; \
843 	} while (0)
844 
845 
846 	switch (nwords) {
847 	default:
848 		for (i = 10; i < nwords; i++)
849 			BN_CONSTTIME_SWAP(i);
850 		/* Fallthrough */
851 	case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */
852 	case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */
853 	case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */
854 	case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */
855 	case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */
856 	case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */
857 	case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */
858 	case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */
859 	case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */
860 	case 1:
861 		BN_CONSTTIME_SWAP(0);
862 	}
863 #undef BN_CONSTTIME_SWAP
864 }
865 
866 /*
867  * Constant-time conditional swap of a and b.
868  * a and b are swapped if condition is not 0.
869  * nwords is the number of words to swap.
870  */
871 int
872 BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords)
873 {
874 	BN_ULONG t;
875 	int i, words;
876 
877 	if (a == b)
878 		return 1;
879 	if (nwords > INT_MAX)
880 		return 0;
881 	words = (int)nwords;
882 	if (bn_wexpand(a, words) == NULL || bn_wexpand(b, words) == NULL)
883 		return 0;
884 	if (a->top > words || b->top > words) {
885 		BNerror(BN_R_INVALID_LENGTH);
886 		return 0;
887 	}
888 
889 	/* Set condition to 0 (if it was zero) or all 1s otherwise. */
890 	condition = ((~condition & (condition - 1)) >> (BN_BITS2 - 1)) - 1;
891 
892 	/* swap top field */
893 	t = (a->top ^ b->top) & condition;
894 	a->top ^= t;
895 	b->top ^= t;
896 
897 	/* swap neg field */
898 	t = (a->neg ^ b->neg) & condition;
899 	a->neg ^= t;
900 	b->neg ^= t;
901 
902 	/* swap BN_FLG_CONSTTIME from flag field */
903 	t = ((a->flags ^ b->flags) & BN_FLG_CONSTTIME) & condition;
904 	a->flags ^= t;
905 	b->flags ^= t;
906 
907 	/* swap the data */
908 	for (i = 0; i < words; i++) {
909 		t = (a->d[i] ^ b->d[i]) & condition;
910 		a->d[i] ^= t;
911 		b->d[i] ^= t;
912 	}
913 
914 	return 1;
915 }
916 
917 BN_GENCB *
918 BN_GENCB_new(void)
919 {
920 	BN_GENCB *cb;
921 
922 	if ((cb = calloc(1, sizeof(*cb))) == NULL)
923 		return NULL;
924 
925 	return cb;
926 }
927 
928 void
929 BN_GENCB_free(BN_GENCB *cb)
930 {
931 	if (cb == NULL)
932 		return;
933 	free(cb);
934 }
935 
936 void *
937 BN_GENCB_get_arg(BN_GENCB *cb)
938 {
939 	return cb->arg;
940 }
941