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>
chromium-stable
David Benjamin 1 year ago committed by Boringssl LUCI CQ
parent e4f60679ca
commit 004317217f
  1. 8
      crypto/bn_extra/convert.c
  2. 8
      crypto/fipsmodule/bn/bn_test.cc
  3. 6
      crypto/fipsmodule/bn/bytes.c
  4. 13
      include/openssl/bn.h

@ -455,3 +455,11 @@ int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len) {
}
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;
}

@ -1158,8 +1158,8 @@ TEST_F(BNTest, LittleEndian) {
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 @@ TEST_F(BNTest, LittleEndian) {
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());
}
}

@ -116,7 +116,7 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
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 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
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,

@ -254,11 +254,11 @@ OPENSSL_EXPORT BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret);
// |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 @@ OPENSSL_EXPORT int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod,
// 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 @@ OPENSSL_EXPORT int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len);
// 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

Loading…
Cancel
Save