summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bn/bn_lib.c')
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c182
1 files changed, 1 insertions, 181 deletions
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index cd06563a5d..78410e2133 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lib.c,v 1.83 2023/04/19 10:54:49 jsing Exp $ */ 1/* $OpenBSD: bn_lib.c,v 1.84 2023/04/19 11:12:43 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -435,186 +435,6 @@ BN_set_word(BIGNUM *a, BN_ULONG w)
435 return (1); 435 return (1);
436} 436}
437 437
438BIGNUM *
439BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
440{
441 unsigned int i, m;
442 unsigned int n;
443 BN_ULONG l;
444 BIGNUM *bn = NULL;
445
446 if (len < 0)
447 return (NULL);
448 if (ret == NULL)
449 ret = bn = BN_new();
450 if (ret == NULL)
451 return (NULL);
452 l = 0;
453 n = len;
454 if (n == 0) {
455 ret->top = 0;
456 return (ret);
457 }
458 i = ((n - 1) / BN_BYTES) + 1;
459 m = ((n - 1) % (BN_BYTES));
460 if (!bn_wexpand(ret, (int)i)) {
461 BN_free(bn);
462 return NULL;
463 }
464 ret->top = i;
465 ret->neg = 0;
466 while (n--) {
467 l = (l << 8L) | *(s++);
468 if (m-- == 0) {
469 ret->d[--i] = l;
470 l = 0;
471 m = BN_BYTES - 1;
472 }
473 }
474 /* need to call this due to clear byte at top if avoiding
475 * having the top bit set (-ve number) */
476 bn_correct_top(ret);
477 return (ret);
478}
479
480typedef enum {
481 big,
482 little,
483} endianness_t;
484
485/* ignore negative */
486static int
487bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianness_t endianness)
488{
489 int n;
490 size_t i, lasti, j, atop, mask;
491 BN_ULONG l;
492
493 /*
494 * In case |a| is fixed-top, BN_num_bytes can return bogus length,
495 * but it's assumed that fixed-top inputs ought to be "nominated"
496 * even for padded output, so it works out...
497 */
498 n = BN_num_bytes(a);
499 if (tolen == -1)
500 tolen = n;
501 else if (tolen < n) { /* uncommon/unlike case */
502 BIGNUM temp = *a;
503
504 bn_correct_top(&temp);
505
506 n = BN_num_bytes(&temp);
507 if (tolen < n)
508 return -1;
509 }
510
511 /* Swipe through whole available data and don't give away padded zero. */
512 atop = a->dmax * BN_BYTES;
513 if (atop == 0) {
514 explicit_bzero(to, tolen);
515 return tolen;
516 }
517
518 lasti = atop - 1;
519 atop = a->top * BN_BYTES;
520
521 if (endianness == big)
522 to += tolen; /* start from the end of the buffer */
523
524 for (i = 0, j = 0; j < (size_t)tolen; j++) {
525 unsigned char val;
526
527 l = a->d[i / BN_BYTES];
528 mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
529 val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
530
531 if (endianness == big)
532 *--to = val;
533 else
534 *to++ = val;
535
536 i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
537 }
538
539 return tolen;
540}
541
542int
543BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
544{
545 if (tolen < 0)
546 return -1;
547 return bn2binpad(a, to, tolen, big);
548}
549
550int
551BN_bn2bin(const BIGNUM *a, unsigned char *to)
552{
553 return bn2binpad(a, to, -1, big);
554}
555
556BIGNUM *
557BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
558{
559 unsigned int i, m, n;
560 BN_ULONG l;
561 BIGNUM *bn = NULL;
562
563 if (ret == NULL)
564 ret = bn = BN_new();
565 if (ret == NULL)
566 return NULL;
567
568
569 s += len;
570 /* Skip trailing zeroes. */
571 for (; len > 0 && s[-1] == 0; s--, len--)
572 continue;
573
574 n = len;
575 if (n == 0) {
576 ret->top = 0;
577 return ret;
578 }
579
580 i = ((n - 1) / BN_BYTES) + 1;
581 m = (n - 1) % BN_BYTES;
582 if (!bn_wexpand(ret, (int)i)) {
583 BN_free(bn);
584 return NULL;
585 }
586
587 ret->top = i;
588 ret->neg = 0;
589 l = 0;
590 while (n-- > 0) {
591 s--;
592 l = (l << 8L) | *s;
593 if (m-- == 0) {
594 ret->d[--i] = l;
595 l = 0;
596 m = BN_BYTES - 1;
597 }
598 }
599
600 /*
601 * need to call this due to clear byte at top if avoiding having the
602 * top bit set (-ve number)
603 */
604 bn_correct_top(ret);
605
606 return ret;
607}
608
609int
610BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
611{
612 if (tolen < 0)
613 return -1;
614
615 return bn2binpad(a, to, tolen, little);
616}
617
618int 438int
619BN_ucmp(const BIGNUM *a, const BIGNUM *b) 439BN_ucmp(const BIGNUM *a, const BIGNUM *b)
620{ 440{