diff --git a/src/crypto/fipsmodule/bn/bn_test.cc b/src/crypto/fipsmodule/bn/bn_test.cc index 01dcfbb16..e4720b1b3 100644 --- a/src/crypto/fipsmodule/bn/bn_test.cc +++ b/src/crypto/fipsmodule/bn/bn_test.cc @@ -74,6 +74,7 @@ #include #include +#include #include #include @@ -2796,6 +2797,27 @@ TEST_F(BNTest, MontgomeryLarge) { ctx(), nullptr)); } +TEST_F(BNTest, FormatWord) { + char buf[32]; + snprintf(buf, sizeof(buf), BN_DEC_FMT1, BN_ULONG{1234}); + EXPECT_STREQ(buf, "1234"); + snprintf(buf, sizeof(buf), BN_HEX_FMT1, BN_ULONG{1234}); + EXPECT_STREQ(buf, "4d2"); + + // |BN_HEX_FMT2| is zero-padded up to the maximum value. +#if defined(OPENSSL_64_BIT) + snprintf(buf, sizeof(buf), BN_HEX_FMT2, BN_ULONG{1234}); + EXPECT_STREQ(buf, "00000000000004d2"); + snprintf(buf, sizeof(buf), BN_HEX_FMT2, std::numeric_limits::max()); + EXPECT_STREQ(buf, "ffffffffffffffff"); +#else + snprintf(buf, sizeof(buf), BN_HEX_FMT2, BN_ULONG{1234}); + EXPECT_STREQ(buf, "000004d2"); + snprintf(buf, sizeof(buf), BN_HEX_FMT2, std::numeric_limits::max()); + EXPECT_STREQ(buf, "ffffffff"); +#endif +} + #if defined(SUPPORTS_ABI_TEST) // These functions are not always implemented in assembly, but they sometimes // are, so include ABI tests for each. diff --git a/src/include/openssl/bn.h b/src/include/openssl/bn.h index a03e41f18..0361645ab 100644 --- a/src/include/openssl/bn.h +++ b/src/include/openssl/bn.h @@ -160,14 +160,12 @@ extern "C" { typedef uint64_t BN_ULONG; #define BN_BITS2 64 #define BN_DEC_FMT1 "%" PRIu64 -#define BN_DEC_FMT2 "%019" PRIu64 #define BN_HEX_FMT1 "%" PRIx64 #define BN_HEX_FMT2 "%016" PRIx64 #elif defined(OPENSSL_32_BIT) typedef uint32_t BN_ULONG; #define BN_BITS2 32 #define BN_DEC_FMT1 "%" PRIu32 -#define BN_DEC_FMT2 "%09" PRIu32 #define BN_HEX_FMT1 "%" PRIx32 #define BN_HEX_FMT2 "%08" PRIx32 #else