From bb1038290e62c3c60b877e70e5a3618b6adc4979 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Wed, 29 May 2024 03:35:52 -0400 Subject: [PATCH] Apply some suggestions from review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: René Meusel --- src/lib/math/bigint/big_code.cpp | 10 +++++++--- src/lib/pubkey/ec_group/ec_point.cpp | 25 +++++++++++++------------ src/lib/pubkey/pubkey.cpp | 5 ++++- src/lib/pubkey/rfc6979/rfc6979.cpp | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/lib/math/bigint/big_code.cpp b/src/lib/math/bigint/big_code.cpp index b6f8f8b8e77..852a0629b69 100644 --- a/src/lib/math/bigint/big_code.cpp +++ b/src/lib/math/bigint/big_code.cpp @@ -9,6 +9,7 @@ #include #include +#include namespace Botan { @@ -110,8 +111,9 @@ secure_vector BigInt::encode_fixed_length_int_pair(const BigInt& n1, co throw Encoding_Error("encode_fixed_length_int_pair: values too large to encode properly"); } secure_vector output(2 * bytes); - n1.serialize_to(std::span{output}.subspan(0, bytes)); - n2.serialize_to(std::span{output}.subspan(bytes, bytes)); + BufferStuffer stuffer(output); + n1.serialize_to(stuffer.next(bytes)); + n2.serialize_to(stuffer.next(bytes)); return output; } @@ -138,7 +140,9 @@ BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base) { binary = hex_decode_locked(buf0_with_leading_0, 2); - binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false); + if(length > 1) { + binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]), length - 1, false); + } } else { binary = hex_decode_locked(cast_uint8_ptr_to_char(buf), length, false); } diff --git a/src/lib/pubkey/ec_group/ec_point.cpp b/src/lib/pubkey/ec_group/ec_point.cpp index 4a802603ac2..f25f1d2a72d 100644 --- a/src/lib/pubkey/ec_group/ec_point.cpp +++ b/src/lib/pubkey/ec_group/ec_point.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Botan { @@ -595,22 +596,22 @@ std::vector EC_Point::encode(EC_Point_Format format) const { const BigInt x = get_affine_x(); const BigInt y = get_affine_y(); - std::vector result; + const size_t fe = (format == EC_Point_Format::Compressed) ? 1 : 2; + + std::vector result(1 + fe * p_bytes); + BufferStuffer stuffer(result); if(format == EC_Point_Format::Uncompressed) { - result.resize(1 + 2 * p_bytes); - result[0] = 0x04; - x.serialize_to(std::span{result}.subspan(1, p_bytes)); - y.serialize_to(std::span{result}.subspan(1 + p_bytes, p_bytes)); + stuffer.append(0x04); + x.serialize_to(stuffer.next(p_bytes)); + y.serialize_to(stuffer.next(p_bytes)); } else if(format == EC_Point_Format::Compressed) { - result.resize(1 + p_bytes); - result[0] = 0x02 | static_cast(y.get_bit(0)); - x.serialize_to(std::span{result}.subspan(1, p_bytes)); + stuffer.append(0x02 | static_cast(y.get_bit(0))); + x.serialize_to(stuffer.next(p_bytes)); } else if(format == EC_Point_Format::Hybrid) { - result.resize(1 + 2 * p_bytes); - result[0] = 0x06 | static_cast(y.get_bit(0)); - x.serialize_to(std::span{result}.subspan(1, p_bytes)); - y.serialize_to(std::span{result}.subspan(1 + p_bytes, p_bytes)); + stuffer.append(0x06 | static_cast(y.get_bit(0))); + x.serialize_to(stuffer.next(p_bytes)); + y.serialize_to(stuffer.next(p_bytes)); } else { throw Invalid_Argument("EC2OSP illegal point encoding"); } diff --git a/src/lib/pubkey/pubkey.cpp b/src/lib/pubkey/pubkey.cpp index ff84f13d6fb..870d9a17619 100644 --- a/src/lib/pubkey/pubkey.cpp +++ b/src/lib/pubkey/pubkey.cpp @@ -16,6 +16,7 @@ #include #include #include +#include namespace Botan { @@ -286,9 +287,11 @@ std::vector der_encode_signature(std::span sig, size_t p throw Encoding_Error("Unexpected size for DER signature"); } + BufferSlicer bs_sig(sig); std::vector sig_parts; + sig_parts.reserve(parts); for(size_t i = 0; i != parts; ++i) { - sig_parts.push_back(BigInt::from_bytes(sig.subspan(part_size * i, part_size))); + sig_parts.emplace_back(BigInt::from_bytes(bs_sig.take(part_size))); } std::vector output; diff --git a/src/lib/pubkey/rfc6979/rfc6979.cpp b/src/lib/pubkey/rfc6979/rfc6979.cpp index 9b5243d3e35..805760c4520 100644 --- a/src/lib/pubkey/rfc6979/rfc6979.cpp +++ b/src/lib/pubkey/rfc6979/rfc6979.cpp @@ -21,7 +21,7 @@ RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(std::string_view hash, const Bi m_rng_out(m_rlen) { m_hmac_drbg = std::make_unique(MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", hash))); - x.serialize_to(std::span{m_rng_in}.subspan(0, m_rlen)); + x.serialize_to(std::span{m_rng_in}.first(m_rlen)); } RFC6979_Nonce_Generator::~RFC6979_Nonce_Generator() = default;