Add BN_bn2lebinpad and BN_lebin2bn These are OpenSSL names for BN_bn2le_padded and BN_le2bn. We can just replace BN_le2bn with BN_lebin2bn. BN_bn2lebinpad is not size_t-clean, so handle it as a separate function like we did BN_bn2binpad. Change-Id: I6999ca06140a0c8c25942362dc79d1821971d679 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/62665 Commit-Queue: Adam Langley <agl@google.com> Auto-Submit: David Benjamin <davidben@google.com> Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/crypto/bn_extra/convert.c b/crypto/bn_extra/convert.c index 29234ef..c9161fa 100644 --- a/crypto/bn_extra/convert.c +++ b/crypto/bn_extra/convert.c
@@ -455,3 +455,11 @@ } return len; } + +int BN_bn2lebinpad(const BIGNUM *in, uint8_t *out, int len) { + if (len < 0 || + !BN_bn2le_padded(out, (size_t)len, in)) { + return -1; + } + return len; +}
diff --git a/crypto/fipsmodule/bn/bn_test.cc b/crypto/fipsmodule/bn/bn_test.cc index 5bf2e13..08c4719 100644 --- a/crypto/fipsmodule/bn/bn_test.cc +++ b/crypto/fipsmodule/bn/bn_test.cc
@@ -1158,8 +1158,8 @@ ASSERT_TRUE(BN_bn2le_padded(out, sizeof(out), x.get())); EXPECT_EQ(Bytes(zeros), Bytes(out)); - ASSERT_TRUE(BN_le2bn(out, sizeof(out), y.get())); - EXPECT_BIGNUMS_EQUAL("BN_le2bn round-trip", x.get(), y.get()); + ASSERT_TRUE(BN_lebin2bn(out, sizeof(out), y.get())); + EXPECT_BIGNUMS_EQUAL("BN_lebin2bn round-trip", x.get(), y.get()); // Test random numbers at various byte lengths. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) { @@ -1182,8 +1182,8 @@ EXPECT_EQ(Bytes(out), Bytes(expected)); // Make sure the decoding produces the same BIGNUM. - ASSERT_TRUE(BN_le2bn(out, bytes, y.get())); - EXPECT_BIGNUMS_EQUAL("BN_le2bn round-trip", x.get(), y.get()); + ASSERT_TRUE(BN_lebin2bn(out, bytes, y.get())); + EXPECT_BIGNUMS_EQUAL("BN_lebin2bn round-trip", x.get(), y.get()); } }
diff --git a/crypto/fipsmodule/bn/bytes.c b/crypto/fipsmodule/bn/bytes.c index 331e085..aca0e38 100644 --- a/crypto/fipsmodule/bn/bytes.c +++ b/crypto/fipsmodule/bn/bytes.c
@@ -116,7 +116,7 @@ return ret; } -BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) { +BIGNUM *BN_lebin2bn(const uint8_t *in, size_t len, BIGNUM *ret) { BIGNUM *bn = NULL; if (ret == NULL) { bn = BN_new(); @@ -149,6 +149,10 @@ return ret; } +BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) { + return BN_lebin2bn(in, len, ret); +} + // fits_in_bytes returns one if the |num_words| words in |words| can be // represented in |num_bytes| bytes. static int fits_in_bytes(const BN_ULONG *words, size_t num_words,
diff --git a/include/openssl/bn.h b/include/openssl/bn.h index 0361645..92ff7a1 100644 --- a/include/openssl/bn.h +++ b/include/openssl/bn.h
@@ -254,11 +254,11 @@ // |in| is secret, use |BN_bn2bin_padded| instead. OPENSSL_EXPORT size_t BN_bn2bin(const BIGNUM *in, uint8_t *out); -// BN_le2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as +// BN_lebin2bn sets |*ret| to the value of |len| bytes from |in|, interpreted as // a little-endian number, and returns |ret|. If |ret| is NULL then a fresh // |BIGNUM| is allocated and returned. It returns NULL on allocation // failure. -OPENSSL_EXPORT BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret); +OPENSSL_EXPORT BIGNUM *BN_lebin2bn(const uint8_t *in, size_t len, BIGNUM *ret); // BN_bn2le_padded serialises the absolute value of |in| to |out| as a // little-endian integer, which must have |len| of space available, padding @@ -972,6 +972,12 @@ // Use |BN_bn2bin_padded| instead. It is |size_t|-clean. OPENSSL_EXPORT int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len); +// BN_bn2lebinpad behaves like |BN_bn2le_padded|, but it returns |len| on +// success and -1 on error. +// +// Use |BN_bn2le_padded| instead. It is |size_t|-clean. +OPENSSL_EXPORT int BN_bn2lebinpad(const BIGNUM *in, uint8_t *out, int len); + // BN_prime_checks is a deprecated alias for |BN_prime_checks_for_validation|. // Use |BN_prime_checks_for_generation| or |BN_prime_checks_for_validation| // instead. (This defaults to the |_for_validation| value in order to be @@ -981,6 +987,9 @@ // BN_secure_new calls |BN_new|. OPENSSL_EXPORT BIGNUM *BN_secure_new(void); +// BN_le2bn calls |BN_lebin2bn|. +OPENSSL_EXPORT BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret); + // Private functions