Skip to content

Commit

Permalink
Merge pull request #3929 from Rohde-Schwarz/test/ensure_bigint_padding
Browse files Browse the repository at this point in the history
Test: make sure that BigInt::binary_encode() writes padding
  • Loading branch information
reneme committed Mar 6, 2024
2 parents 3fed83d + fb802dd commit b5e694a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/lib/math/bigint/bigint.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,10 @@ class BOTAN_PUBLIC_API(2, 0) BigInt final {
* If len exactly equals this->bytes(), this function behaves identically
* to binary_encode.
*
* Zero-padding the binary encoding is useful to ensure that other
* applications correctly parse the encoded value as "positive integer",
* as a leading 1-bit may be interpreted as a sign bit.
*
* @param buf destination byte array for the integer value
* @param len how many bytes to write
*/
Expand Down
57 changes: 55 additions & 2 deletions src/tests/test_bigint.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* (C) 2009,2015,2016 Jack Lloyd
* (C) 2024 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
Expand Down Expand Up @@ -562,8 +563,6 @@ Test::Result test_const_time_left_shift() {
return result;
}

BOTAN_REGISTER_TEST_FN("math", "bn_ct_lshift", test_const_time_left_shift);

class BigInt_Powmod_Test final : public Text_Based_Test {
public:
BigInt_Powmod_Test() : Text_Based_Test("bn/powmod.vec", "Base,Exponent,Modulus,Output") {}
Expand Down Expand Up @@ -774,6 +773,60 @@ class DSA_ParamGen_Test final : public Text_Based_Test {

BOTAN_REGISTER_TEST("math", "dsa_param", DSA_ParamGen_Test);

std::vector<Test::Result> test_bigint_serialization() {
auto rng = Test::new_rng("test_bigint_serialization");

return {
Botan_Tests::CHECK("BigInt binary serialization",
[](Test::Result& res) {
Botan::BigInt a(0x1234567890ABCDEF);
auto enc = Botan::BigInt::encode(a);
res.test_eq("BigInt::encode()", enc, Botan::hex_decode("1234567890ABCDEF"));

auto enc_locked = Botan::BigInt::encode_locked(a);
res.test_eq(
"BigInt::encode_locked()", enc_locked, Botan::hex_decode_locked("1234567890ABCDEF"));
std::vector<uint8_t> enc2(a.bytes());
a.binary_encode(enc2.data(), enc2.size());
res.test_eq("BigInt::binary_encode", enc2, Botan::hex_decode("1234567890ABCDEF"));
}),

Botan_Tests::CHECK("BigInt truncated/padded binary serialization",
[&](Test::Result& res) {
Botan::BigInt a(0xFEDCBA9876543210);

std::vector<uint8_t> enc1(a.bytes() - 1);
a.binary_encode(enc1.data(), enc1.size());
res.test_eq("BigInt::binary_encode", enc1, Botan::hex_decode("DCBA9876543210"));

std::vector<uint8_t> enc2(a.bytes() - 3);
a.binary_encode(enc2.data(), enc2.size());
res.test_eq("BigInt::binary_encode", enc2, Botan::hex_decode("9876543210"));

std::vector<uint8_t> enc3(a.bytes() + 1);
a.binary_encode(enc3.data(), enc3.size());
res.test_eq("BigInt::binary_encode", enc3, Botan::hex_decode("00FEDCBA9876543210"));

// make sure that the padding is actually written
std::vector<uint8_t> enc4(a.bytes() + 3);
rng->randomize(enc4);
a.binary_encode(enc4.data(), enc4.size());
res.test_eq("BigInt::binary_encode", enc4, Botan::hex_decode("000000FEDCBA9876543210"));

Botan::BigInt b(Botan::hex_decode("FEDCBA9876543210BAADC0FFEE"));

std::vector<uint8_t> enc5(b.bytes() + 12);
rng->randomize(enc5);
b.binary_encode(enc5.data(), enc5.size());
res.test_eq("BigInt::binary_encode",
enc5,
Botan::hex_decode("000000000000000000000000FEDCBA9876543210BAADC0FFEE"));
}),
};
}

BOTAN_REGISTER_TEST_FN("math", "bignum_auxiliary", test_const_time_left_shift, test_bigint_serialization);

#endif

} // namespace
Expand Down

0 comments on commit b5e694a

Please sign in to comment.