diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/bn/bn_convert.c | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/src/lib/libcrypto/bn/bn_convert.c b/src/lib/libcrypto/bn/bn_convert.c index 0bfb00e958..7c5ba85305 100644 --- a/src/lib/libcrypto/bn/bn_convert.c +++ b/src/lib/libcrypto/bn/bn_convert.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bn_convert.c,v 1.19 2024/04/17 14:45:46 jsing Exp $ */ | 1 | /* $OpenBSD: bn_convert.c,v 1.20 2024/04/17 14:47:17 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 | * |
@@ -720,45 +720,42 @@ BN_bn2mpi(const BIGNUM *a, unsigned char *d) | |||
720 | LCRYPTO_ALIAS(BN_bn2mpi); | 720 | LCRYPTO_ALIAS(BN_bn2mpi); |
721 | 721 | ||
722 | BIGNUM * | 722 | BIGNUM * |
723 | BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain) | 723 | BN_mpi2bn(const unsigned char *d, int n, BIGNUM *bn_in) |
724 | { | 724 | { |
725 | BIGNUM *a = ain; | 725 | BIGNUM *bn = bn_in; |
726 | long len; | 726 | uint32_t mpi_len; |
727 | uint8_t v; | ||
727 | int neg = 0; | 728 | int neg = 0; |
729 | CBS cbs; | ||
730 | |||
731 | if (n < 0) | ||
732 | return NULL; | ||
733 | |||
734 | CBS_init(&cbs, d, n); | ||
728 | 735 | ||
729 | if (n < 4) { | 736 | if (!CBS_get_u32(&cbs, &mpi_len)) { |
730 | BNerror(BN_R_INVALID_LENGTH); | 737 | BNerror(BN_R_INVALID_LENGTH); |
731 | return (NULL); | 738 | return NULL; |
739 | |||
732 | } | 740 | } |
733 | len = ((long)d[0] << 24) | ((long)d[1] << 16) | ((int)d[2] << 8) | | 741 | if (CBS_len(&cbs) != mpi_len) { |
734 | (int)d[3]; | ||
735 | if ((len + 4) != n) { | ||
736 | BNerror(BN_R_ENCODING_ERROR); | 742 | BNerror(BN_R_ENCODING_ERROR); |
737 | return (NULL); | 743 | return NULL; |
744 | } | ||
745 | if (CBS_len(&cbs) > 0) { | ||
746 | if (!CBS_peek_u8(&cbs, &v)) | ||
747 | return NULL; | ||
748 | neg = (v >> 7) & 1; | ||
738 | } | 749 | } |
739 | 750 | ||
740 | if (a == NULL) | 751 | if (!bn_bin2bn_cbs(&bn, &cbs, 0)) |
741 | a = BN_new(); | 752 | return NULL; |
742 | if (a == NULL) | ||
743 | return (NULL); | ||
744 | 753 | ||
745 | if (len == 0) { | 754 | if (neg) |
746 | a->neg = 0; | 755 | BN_clear_bit(bn, BN_num_bits(bn) - 1); |
747 | a->top = 0; | 756 | |
748 | return (a); | 757 | BN_set_negative(bn, neg); |
749 | } | 758 | |
750 | d += 4; | 759 | return bn; |
751 | if ((*d) & 0x80) | ||
752 | neg = 1; | ||
753 | if (BN_bin2bn(d, (int)len, a) == NULL) { | ||
754 | if (ain == NULL) | ||
755 | BN_free(a); | ||
756 | return (NULL); | ||
757 | } | ||
758 | BN_set_negative(a, neg); | ||
759 | if (neg) { | ||
760 | BN_clear_bit(a, BN_num_bits(a) - 1); | ||
761 | } | ||
762 | return (a); | ||
763 | } | 760 | } |
764 | LCRYPTO_ALIAS(BN_mpi2bn); | 761 | LCRYPTO_ALIAS(BN_mpi2bn); |