1 /* $OpenBSD: bn_lib.c,v 1.52 2021/12/04 16:02:44 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 void 141 BN_set_flags(BIGNUM *b, int n) 142 { 143 b->flags |= n; 144 } 145 146 int 147 BN_get_flags(const BIGNUM *b, int n) 148 { 149 return b->flags & n; 150 } 151 152 void 153 BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags) 154 { 155 int dest_flags; 156 157 dest_flags = (dest->flags & BN_FLG_MALLOCED) | 158 (b->flags & ~BN_FLG_MALLOCED) | BN_FLG_STATIC_DATA | flags; 159 160 *dest = *b; 161 dest->flags = dest_flags; 162 } 163 164 const BIGNUM * 165 BN_value_one(void) 166 { 167 static const BN_ULONG data_one = 1L; 168 static const BIGNUM const_one = { 169 (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA 170 }; 171 172 return (&const_one); 173 } 174 175 int 176 BN_num_bits_word(BN_ULONG l) 177 { 178 BN_ULONG x, mask; 179 int bits; 180 unsigned int shift; 181 182 /* Constant time calculation of floor(log2(l)) + 1. */ 183 bits = (l != 0); 184 shift = BN_BITS4; /* On _LP64 this is 32, otherwise 16. */ 185 do { 186 x = l >> shift; 187 /* If x is 0, set mask to 0, otherwise set it to all 1s. */ 188 mask = ((~x & (x - 1)) >> (BN_BITS2 - 1)) - 1; 189 bits += shift & mask; 190 /* If x is 0, leave l alone, otherwise set l = x. */ 191 l ^= (x ^ l) & mask; 192 } while ((shift /= 2) != 0); 193 194 return bits; 195 } 196 197 int 198 BN_num_bits(const BIGNUM *a) 199 { 200 int i = a->top - 1; 201 202 bn_check_top(a); 203 204 if (BN_is_zero(a)) 205 return 0; 206 return ((i * BN_BITS2) + BN_num_bits_word(a->d[i])); 207 } 208 209 void 210 BN_clear_free(BIGNUM *a) 211 { 212 int i; 213 214 if (a == NULL) 215 return; 216 bn_check_top(a); 217 if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) 218 freezero(a->d, a->dmax * sizeof(a->d[0])); 219 i = BN_get_flags(a, BN_FLG_MALLOCED); 220 explicit_bzero(a, sizeof(BIGNUM)); 221 if (i) 222 free(a); 223 } 224 225 void 226 BN_free(BIGNUM *a) 227 { 228 BN_clear_free(a); 229 } 230 231 void 232 BN_init(BIGNUM *a) 233 { 234 memset(a, 0, sizeof(BIGNUM)); 235 bn_check_top(a); 236 } 237 238 BIGNUM * 239 BN_new(void) 240 { 241 BIGNUM *ret; 242 243 if ((ret = malloc(sizeof(BIGNUM))) == NULL) { 244 BNerror(ERR_R_MALLOC_FAILURE); 245 return (NULL); 246 } 247 ret->flags = BN_FLG_MALLOCED; 248 ret->top = 0; 249 ret->neg = 0; 250 ret->dmax = 0; 251 ret->d = NULL; 252 bn_check_top(ret); 253 return (ret); 254 } 255 256 /* This is used both by bn_expand2() and bn_dup_expand() */ 257 /* The caller MUST check that words > b->dmax before calling this */ 258 static BN_ULONG * 259 bn_expand_internal(const BIGNUM *b, int words) 260 { 261 BN_ULONG *A, *a = NULL; 262 const BN_ULONG *B; 263 int i; 264 265 bn_check_top(b); 266 267 if (words > (INT_MAX/(4*BN_BITS2))) { 268 BNerror(BN_R_BIGNUM_TOO_LONG); 269 return NULL; 270 } 271 if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { 272 BNerror(BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); 273 return (NULL); 274 } 275 a = A = reallocarray(NULL, words, sizeof(BN_ULONG)); 276 if (A == NULL) { 277 BNerror(ERR_R_MALLOC_FAILURE); 278 return (NULL); 279 } 280 #if 1 281 B = b->d; 282 /* Check if the previous number needs to be copied */ 283 if (B != NULL) { 284 for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { 285 /* 286 * The fact that the loop is unrolled 287 * 4-wise is a tribute to Intel. It's 288 * the one that doesn't have enough 289 * registers to accommodate more data. 290 * I'd unroll it 8-wise otherwise:-) 291 * 292 * <appro@fy.chalmers.se> 293 */ 294 BN_ULONG a0, a1, a2, a3; 295 a0 = B[0]; 296 a1 = B[1]; 297 a2 = B[2]; 298 a3 = B[3]; 299 A[0] = a0; 300 A[1] = a1; 301 A[2] = a2; 302 A[3] = a3; 303 } 304 switch (b->top & 3) { 305 case 3: 306 A[2] = B[2]; 307 case 2: 308 A[1] = B[1]; 309 case 1: 310 A[0] = B[0]; 311 } 312 } 313 314 #else 315 memset(A, 0, sizeof(BN_ULONG) * words); 316 memcpy(A, b->d, sizeof(b->d[0]) * b->top); 317 #endif 318 319 return (a); 320 } 321 322 /* This is an internal function that can be used instead of bn_expand2() 323 * when there is a need to copy BIGNUMs instead of only expanding the 324 * data part, while still expanding them. 325 * Especially useful when needing to expand BIGNUMs that are declared 326 * 'const' and should therefore not be changed. 327 * The reason to use this instead of a BN_dup() followed by a bn_expand2() 328 * is memory allocation overhead. A BN_dup() followed by a bn_expand2() 329 * will allocate new memory for the BIGNUM data twice, and free it once, 330 * while bn_dup_expand() makes sure allocation is made only once. 331 */ 332 333 #ifndef OPENSSL_NO_DEPRECATED 334 BIGNUM * 335 bn_dup_expand(const BIGNUM *b, int words) 336 { 337 BIGNUM *r = NULL; 338 339 bn_check_top(b); 340 341 /* This function does not work if 342 * words <= b->dmax && top < words 343 * because BN_dup() does not preserve 'dmax'! 344 * (But bn_dup_expand() is not used anywhere yet.) 345 */ 346 347 if (words > b->dmax) { 348 BN_ULONG *a = bn_expand_internal(b, words); 349 350 if (a) { 351 r = BN_new(); 352 if (r) { 353 r->top = b->top; 354 r->dmax = words; 355 r->neg = b->neg; 356 r->d = a; 357 } else { 358 /* r == NULL, BN_new failure */ 359 free(a); 360 } 361 } 362 /* If a == NULL, there was an error in allocation in 363 bn_expand_internal(), and NULL should be returned */ 364 } else { 365 r = BN_dup(b); 366 } 367 368 bn_check_top(r); 369 return r; 370 } 371 #endif 372 373 /* This is an internal function that should not be used in applications. 374 * It ensures that 'b' has enough room for a 'words' word number 375 * and initialises any unused part of b->d with leading zeros. 376 * It is mostly used by the various BIGNUM routines. If there is an error, 377 * NULL is returned. If not, 'b' is returned. */ 378 379 BIGNUM * 380 bn_expand2(BIGNUM *b, int words) 381 { 382 bn_check_top(b); 383 384 if (words > b->dmax) { 385 BN_ULONG *a = bn_expand_internal(b, words); 386 if (!a) 387 return NULL; 388 if (b->d) 389 freezero(b->d, b->dmax * sizeof(b->d[0])); 390 b->d = a; 391 b->dmax = words; 392 } 393 394 /* None of this should be necessary because of what b->top means! */ 395 #if 0 396 /* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */ 397 if (b->top < b->dmax) { 398 int i; 399 BN_ULONG *A = &(b->d[b->top]); 400 for (i = (b->dmax - b->top) >> 3; i > 0; i--, A += 8) { 401 A[0] = 0; 402 A[1] = 0; 403 A[2] = 0; 404 A[3] = 0; 405 A[4] = 0; 406 A[5] = 0; 407 A[6] = 0; 408 A[7] = 0; 409 } 410 for (i = (b->dmax - b->top)&7; i > 0; i--, A++) 411 A[0] = 0; 412 assert(A == &(b->d[b->dmax])); 413 } 414 #endif 415 bn_check_top(b); 416 return b; 417 } 418 419 BIGNUM * 420 BN_dup(const BIGNUM *a) 421 { 422 BIGNUM *t; 423 424 if (a == NULL) 425 return NULL; 426 bn_check_top(a); 427 428 t = BN_new(); 429 if (t == NULL) 430 return NULL; 431 if (!BN_copy(t, a)) { 432 BN_free(t); 433 return NULL; 434 } 435 bn_check_top(t); 436 return t; 437 } 438 439 BIGNUM * 440 BN_copy(BIGNUM *a, const BIGNUM *b) 441 { 442 int i; 443 BN_ULONG *A; 444 const BN_ULONG *B; 445 446 bn_check_top(b); 447 448 if (a == b) 449 return (a); 450 if (bn_wexpand(a, b->top) == NULL) 451 return (NULL); 452 453 #if 1 454 A = a->d; 455 B = b->d; 456 for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { 457 BN_ULONG a0, a1, a2, a3; 458 a0 = B[0]; 459 a1 = B[1]; 460 a2 = B[2]; 461 a3 = B[3]; 462 A[0] = a0; 463 A[1] = a1; 464 A[2] = a2; 465 A[3] = a3; 466 } 467 switch (b->top & 3) { 468 case 3: 469 A[2] = B[2]; 470 case 2: 471 A[1] = B[1]; 472 case 1: 473 A[0] = B[0]; 474 } 475 #else 476 memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); 477 #endif 478 479 a->top = b->top; 480 a->neg = b->neg; 481 bn_check_top(a); 482 return (a); 483 } 484 485 void 486 BN_swap(BIGNUM *a, BIGNUM *b) 487 { 488 int flags_old_a, flags_old_b; 489 BN_ULONG *tmp_d; 490 int tmp_top, tmp_dmax, tmp_neg; 491 492 bn_check_top(a); 493 bn_check_top(b); 494 495 flags_old_a = a->flags; 496 flags_old_b = b->flags; 497 498 tmp_d = a->d; 499 tmp_top = a->top; 500 tmp_dmax = a->dmax; 501 tmp_neg = a->neg; 502 503 a->d = b->d; 504 a->top = b->top; 505 a->dmax = b->dmax; 506 a->neg = b->neg; 507 508 b->d = tmp_d; 509 b->top = tmp_top; 510 b->dmax = tmp_dmax; 511 b->neg = tmp_neg; 512 513 a->flags = (flags_old_a & BN_FLG_MALLOCED) | 514 (flags_old_b & BN_FLG_STATIC_DATA); 515 b->flags = (flags_old_b & BN_FLG_MALLOCED) | 516 (flags_old_a & BN_FLG_STATIC_DATA); 517 bn_check_top(a); 518 bn_check_top(b); 519 } 520 521 void 522 BN_clear(BIGNUM *a) 523 { 524 bn_check_top(a); 525 if (a->d != NULL) 526 explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); 527 a->top = 0; 528 a->neg = 0; 529 } 530 531 BN_ULONG 532 BN_get_word(const BIGNUM *a) 533 { 534 if (a->top > 1) 535 return BN_MASK2; 536 else if (a->top == 1) 537 return a->d[0]; 538 /* a->top == 0 */ 539 return 0; 540 } 541 542 BIGNUM * 543 bn_expand(BIGNUM *a, int bits) 544 { 545 if (bits > (INT_MAX - BN_BITS2 + 1)) 546 return (NULL); 547 548 if (((bits + BN_BITS2 - 1) / BN_BITS2) <= a->dmax) 549 return (a); 550 551 return bn_expand2(a, (bits + BN_BITS2 - 1) / BN_BITS2); 552 } 553 554 int 555 BN_set_word(BIGNUM *a, BN_ULONG w) 556 { 557 bn_check_top(a); 558 if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) 559 return (0); 560 a->neg = 0; 561 a->d[0] = w; 562 a->top = (w ? 1 : 0); 563 bn_check_top(a); 564 return (1); 565 } 566 567 BIGNUM * 568 BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) 569 { 570 unsigned int i, m; 571 unsigned int n; 572 BN_ULONG l; 573 BIGNUM *bn = NULL; 574 575 if (len < 0) 576 return (NULL); 577 if (ret == NULL) 578 ret = bn = BN_new(); 579 if (ret == NULL) 580 return (NULL); 581 bn_check_top(ret); 582 l = 0; 583 n = len; 584 if (n == 0) { 585 ret->top = 0; 586 return (ret); 587 } 588 i = ((n - 1) / BN_BYTES) + 1; 589 m = ((n - 1) % (BN_BYTES)); 590 if (bn_wexpand(ret, (int)i) == NULL) { 591 BN_free(bn); 592 return NULL; 593 } 594 ret->top = i; 595 ret->neg = 0; 596 while (n--) { 597 l = (l << 8L) | *(s++); 598 if (m-- == 0) { 599 ret->d[--i] = l; 600 l = 0; 601 m = BN_BYTES - 1; 602 } 603 } 604 /* need to call this due to clear byte at top if avoiding 605 * having the top bit set (-ve number) */ 606 bn_correct_top(ret); 607 return (ret); 608 } 609 610 typedef enum { 611 big, 612 little, 613 } endianness_t; 614 615 /* ignore negative */ 616 static int 617 bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianness_t endianness) 618 { 619 int n; 620 size_t i, lasti, j, atop, mask; 621 BN_ULONG l; 622 623 /* 624 * In case |a| is fixed-top, BN_num_bytes can return bogus length, 625 * but it's assumed that fixed-top inputs ought to be "nominated" 626 * even for padded output, so it works out... 627 */ 628 n = BN_num_bytes(a); 629 if (tolen == -1) 630 tolen = n; 631 else if (tolen < n) { /* uncommon/unlike case */ 632 BIGNUM temp = *a; 633 634 bn_correct_top(&temp); 635 636 n = BN_num_bytes(&temp); 637 if (tolen < n) 638 return -1; 639 } 640 641 /* Swipe through whole available data and don't give away padded zero. */ 642 atop = a->dmax * BN_BYTES; 643 if (atop == 0) { 644 explicit_bzero(to, tolen); 645 return tolen; 646 } 647 648 lasti = atop - 1; 649 atop = a->top * BN_BYTES; 650 651 if (endianness == big) 652 to += tolen; /* start from the end of the buffer */ 653 654 for (i = 0, j = 0; j < (size_t)tolen; j++) { 655 unsigned char val; 656 657 l = a->d[i / BN_BYTES]; 658 mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1)); 659 val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask); 660 661 if (endianness == big) 662 *--to = val; 663 else 664 *to++ = val; 665 666 i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */ 667 } 668 669 return tolen; 670 } 671 672 int 673 BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) 674 { 675 if (tolen < 0) 676 return -1; 677 return bn2binpad(a, to, tolen, big); 678 } 679 680 int 681 BN_bn2bin(const BIGNUM *a, unsigned char *to) 682 { 683 return bn2binpad(a, to, -1, big); 684 } 685 686 BIGNUM * 687 BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret) 688 { 689 unsigned int i, m, n; 690 BN_ULONG l; 691 BIGNUM *bn = NULL; 692 693 if (ret == NULL) 694 ret = bn = BN_new(); 695 if (ret == NULL) 696 return NULL; 697 698 bn_check_top(ret); 699 700 s += len; 701 /* Skip trailing zeroes. */ 702 for (; len > 0 && s[-1] == 0; s--, len--) 703 continue; 704 705 n = len; 706 if (n == 0) { 707 ret->top = 0; 708 return ret; 709 } 710 711 i = ((n - 1) / BN_BYTES) + 1; 712 m = (n - 1) % BN_BYTES; 713 if (bn_wexpand(ret, (int)i) == NULL) { 714 BN_free(bn); 715 return NULL; 716 } 717 718 ret->top = i; 719 ret->neg = 0; 720 l = 0; 721 while (n-- > 0) { 722 s--; 723 l = (l << 8L) | *s; 724 if (m-- == 0) { 725 ret->d[--i] = l; 726 l = 0; 727 m = BN_BYTES - 1; 728 } 729 } 730 731 /* 732 * need to call this due to clear byte at top if avoiding having the 733 * top bit set (-ve number) 734 */ 735 bn_correct_top(ret); 736 737 return ret; 738 } 739 740 int 741 BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen) 742 { 743 if (tolen < 0) 744 return -1; 745 746 return bn2binpad(a, to, tolen, little); 747 } 748 749 int 750 BN_ucmp(const BIGNUM *a, const BIGNUM *b) 751 { 752 int i; 753 BN_ULONG t1, t2, *ap, *bp; 754 755 bn_check_top(a); 756 bn_check_top(b); 757 758 i = a->top - b->top; 759 if (i != 0) 760 return (i); 761 ap = a->d; 762 bp = b->d; 763 for (i = a->top - 1; i >= 0; i--) { 764 t1 = ap[i]; 765 t2 = bp[i]; 766 if (t1 != t2) 767 return ((t1 > t2) ? 1 : -1); 768 } 769 return (0); 770 } 771 772 int 773 BN_cmp(const BIGNUM *a, const BIGNUM *b) 774 { 775 int i; 776 int gt, lt; 777 BN_ULONG t1, t2; 778 779 if ((a == NULL) || (b == NULL)) { 780 if (a != NULL) 781 return (-1); 782 else if (b != NULL) 783 return (1); 784 else 785 return (0); 786 } 787 788 bn_check_top(a); 789 bn_check_top(b); 790 791 if (a->neg != b->neg) { 792 if (a->neg) 793 return (-1); 794 else 795 return (1); 796 } 797 if (a->neg == 0) { 798 gt = 1; 799 lt = -1; 800 } else { 801 gt = -1; 802 lt = 1; 803 } 804 805 if (a->top > b->top) 806 return (gt); 807 if (a->top < b->top) 808 return (lt); 809 for (i = a->top - 1; i >= 0; i--) { 810 t1 = a->d[i]; 811 t2 = b->d[i]; 812 if (t1 > t2) 813 return (gt); 814 if (t1 < t2) 815 return (lt); 816 } 817 return (0); 818 } 819 820 int 821 BN_set_bit(BIGNUM *a, int n) 822 { 823 int i, j, k; 824 825 if (n < 0) 826 return 0; 827 828 i = n / BN_BITS2; 829 j = n % BN_BITS2; 830 if (a->top <= i) { 831 if (bn_wexpand(a, i + 1) == NULL) 832 return (0); 833 for (k = a->top; k < i + 1; k++) 834 a->d[k] = 0; 835 a->top = i + 1; 836 } 837 838 a->d[i] |= (((BN_ULONG)1) << j); 839 bn_check_top(a); 840 return (1); 841 } 842 843 int 844 BN_clear_bit(BIGNUM *a, int n) 845 { 846 int i, j; 847 848 bn_check_top(a); 849 if (n < 0) 850 return 0; 851 852 i = n / BN_BITS2; 853 j = n % BN_BITS2; 854 if (a->top <= i) 855 return (0); 856 857 a->d[i] &= (~(((BN_ULONG)1) << j)); 858 bn_correct_top(a); 859 return (1); 860 } 861 862 int 863 BN_is_bit_set(const BIGNUM *a, int n) 864 { 865 int i, j; 866 867 bn_check_top(a); 868 if (n < 0) 869 return 0; 870 i = n / BN_BITS2; 871 j = n % BN_BITS2; 872 if (a->top <= i) 873 return 0; 874 return (int)(((a->d[i]) >> j) & ((BN_ULONG)1)); 875 } 876 877 int 878 BN_mask_bits(BIGNUM *a, int n) 879 { 880 int b, w; 881 882 bn_check_top(a); 883 if (n < 0) 884 return 0; 885 886 w = n / BN_BITS2; 887 b = n % BN_BITS2; 888 if (w >= a->top) 889 return 0; 890 if (b == 0) 891 a->top = w; 892 else { 893 a->top = w + 1; 894 a->d[w] &= ~(BN_MASK2 << b); 895 } 896 bn_correct_top(a); 897 return (1); 898 } 899 900 void 901 BN_set_negative(BIGNUM *a, int b) 902 { 903 if (b && !BN_is_zero(a)) 904 a->neg = 1; 905 else 906 a->neg = 0; 907 } 908 909 int 910 bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) 911 { 912 int i; 913 BN_ULONG aa, bb; 914 915 aa = a[n - 1]; 916 bb = b[n - 1]; 917 if (aa != bb) 918 return ((aa > bb) ? 1 : -1); 919 for (i = n - 2; i >= 0; i--) { 920 aa = a[i]; 921 bb = b[i]; 922 if (aa != bb) 923 return ((aa > bb) ? 1 : -1); 924 } 925 return (0); 926 } 927 928 /* Here follows a specialised variants of bn_cmp_words(). It has the 929 property of performing the operation on arrays of different sizes. 930 The sizes of those arrays is expressed through cl, which is the 931 common length ( basicall, min(len(a),len(b)) ), and dl, which is the 932 delta between the two lengths, calculated as len(a)-len(b). 933 All lengths are the number of BN_ULONGs... */ 934 935 int 936 bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) 937 { 938 int n, i; 939 940 n = cl - 1; 941 942 if (dl < 0) { 943 for (i = dl; i < 0; i++) { 944 if (b[n - i] != 0) 945 return -1; /* a < b */ 946 } 947 } 948 if (dl > 0) { 949 for (i = dl; i > 0; i--) { 950 if (a[n + i] != 0) 951 return 1; /* a > b */ 952 } 953 } 954 return bn_cmp_words(a, b, cl); 955 } 956 957 /* 958 * Constant-time conditional swap of a and b. 959 * a and b are swapped if condition is not 0. 960 * The code assumes that at most one bit of condition is set. 961 * nwords is the number of words to swap. 962 * The code assumes that at least nwords are allocated in both a and b, 963 * and that no more than nwords are used by either a or b. 964 * a and b cannot be the same number 965 */ 966 void 967 BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) 968 { 969 BN_ULONG t; 970 int i; 971 972 bn_wcheck_size(a, nwords); 973 bn_wcheck_size(b, nwords); 974 975 assert(a != b); 976 assert((condition & (condition - 1)) == 0); 977 assert(sizeof(BN_ULONG) >= sizeof(int)); 978 979 condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1; 980 981 t = (a->top^b->top) & condition; 982 a->top ^= t; 983 b->top ^= t; 984 985 #define BN_CONSTTIME_SWAP(ind) \ 986 do { \ 987 t = (a->d[ind] ^ b->d[ind]) & condition; \ 988 a->d[ind] ^= t; \ 989 b->d[ind] ^= t; \ 990 } while (0) 991 992 993 switch (nwords) { 994 default: 995 for (i = 10; i < nwords; i++) 996 BN_CONSTTIME_SWAP(i); 997 /* Fallthrough */ 998 case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */ 999 case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */ 1000 case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */ 1001 case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */ 1002 case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */ 1003 case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */ 1004 case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */ 1005 case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */ 1006 case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */ 1007 case 1: 1008 BN_CONSTTIME_SWAP(0); 1009 } 1010 #undef BN_CONSTTIME_SWAP 1011 } 1012 1013 /* 1014 * Constant-time conditional swap of a and b. 1015 * a and b are swapped if condition is not 0. 1016 * nwords is the number of words to swap. 1017 */ 1018 int 1019 BN_swap_ct(BN_ULONG condition, BIGNUM *a, BIGNUM *b, size_t nwords) 1020 { 1021 BN_ULONG t; 1022 int i, words; 1023 1024 if (a == b) 1025 return 1; 1026 if (nwords > INT_MAX) 1027 return 0; 1028 words = (int)nwords; 1029 if (bn_wexpand(a, words) == NULL || bn_wexpand(b, words) == NULL) 1030 return 0; 1031 if (a->top > words || b->top > words) { 1032 BNerror(BN_R_INVALID_LENGTH); 1033 return 0; 1034 } 1035 1036 /* Set condition to 0 (if it was zero) or all 1s otherwise. */ 1037 condition = ((~condition & (condition - 1)) >> (BN_BITS2 - 1)) - 1; 1038 1039 /* swap top field */ 1040 t = (a->top ^ b->top) & condition; 1041 a->top ^= t; 1042 b->top ^= t; 1043 1044 /* swap neg field */ 1045 t = (a->neg ^ b->neg) & condition; 1046 a->neg ^= t; 1047 b->neg ^= t; 1048 1049 /* swap BN_FLG_CONSTTIME from flag field */ 1050 t = ((a->flags ^ b->flags) & BN_FLG_CONSTTIME) & condition; 1051 a->flags ^= t; 1052 b->flags ^= t; 1053 1054 /* swap the data */ 1055 for (i = 0; i < words; i++) { 1056 t = (a->d[i] ^ b->d[i]) & condition; 1057 a->d[i] ^= t; 1058 b->d[i] ^= t; 1059 } 1060 1061 return 1; 1062 } 1063 1064 void 1065 BN_zero_ex(BIGNUM *a) 1066 { 1067 a->neg = 0; 1068 a->top = 0; 1069 /* XXX: a->flags &= ~BN_FIXED_TOP */ 1070 } 1071 1072 int 1073 BN_abs_is_word(const BIGNUM *a, const BN_ULONG w) 1074 { 1075 return (a->top == 1 && a->d[0] == w) || (w == 0 && a->top == 0); 1076 } 1077 1078 int 1079 BN_is_zero(const BIGNUM *a) 1080 { 1081 return a->top == 0; 1082 } 1083 1084 int 1085 BN_is_one(const BIGNUM *a) 1086 { 1087 return BN_abs_is_word(a, 1) && !a->neg; 1088 } 1089 1090 int 1091 BN_is_word(const BIGNUM *a, const BN_ULONG w) 1092 { 1093 return BN_abs_is_word(a, w) && (w == 0 || !a->neg); 1094 } 1095 1096 int 1097 BN_is_odd(const BIGNUM *a) 1098 { 1099 return a->top > 0 && (a->d[0] & 1); 1100 } 1101 1102 int 1103 BN_is_negative(const BIGNUM *a) 1104 { 1105 return a->neg != 0; 1106 } 1107 1108 BN_GENCB * 1109 BN_GENCB_new(void) 1110 { 1111 BN_GENCB *cb; 1112 1113 if ((cb = calloc(1, sizeof(*cb))) == NULL) 1114 return NULL; 1115 1116 return cb; 1117 } 1118 1119 void 1120 BN_GENCB_free(BN_GENCB *cb) 1121 { 1122 if (cb == NULL) 1123 return; 1124 free(cb); 1125 } 1126 1127 /* Populate a BN_GENCB structure with an "old"-style callback */ 1128 void 1129 BN_GENCB_set_old(BN_GENCB *gencb, void (*cb)(int, int, void *), void *cb_arg) 1130 { 1131 gencb->ver = 1; 1132 gencb->cb.cb_1 = cb; 1133 gencb->arg = cb_arg; 1134 } 1135 1136 /* Populate a BN_GENCB structure with a "new"-style callback */ 1137 void 1138 BN_GENCB_set(BN_GENCB *gencb, int (*cb)(int, int, BN_GENCB *), void *cb_arg) 1139 { 1140 gencb->ver = 2; 1141 gencb->cb.cb_2 = cb; 1142 gencb->arg = cb_arg; 1143 } 1144 1145 void * 1146 BN_GENCB_get_arg(BN_GENCB *cb) 1147 { 1148 return cb->arg; 1149 } 1150