Skip to content

Commit

Permalink
Apply some suggestions from review
Browse files Browse the repository at this point in the history
Co-authored-by: René Meusel <[email protected]>
  • Loading branch information
randombit and reneme committed May 29, 2024
1 parent ae49fef commit bb10382
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/lib/math/bigint/big_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <botan/hex.h>
#include <botan/internal/divide.h>
#include <botan/internal/stl_util.h>

namespace Botan {

Expand Down Expand Up @@ -110,8 +111,9 @@ secure_vector<uint8_t> 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<uint8_t> 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;
}

Expand All @@ -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);
}
Expand Down
25 changes: 13 additions & 12 deletions src/lib/pubkey/ec_group/ec_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <botan/numthry.h>
#include <botan/rng.h>
#include <botan/internal/ct_utils.h>
#include <botan/internal/stl_util.h>

namespace Botan {

Expand Down Expand Up @@ -595,22 +596,22 @@ std::vector<uint8_t> EC_Point::encode(EC_Point_Format format) const {
const BigInt x = get_affine_x();
const BigInt y = get_affine_y();

std::vector<uint8_t> result;
const size_t fe = (format == EC_Point_Format::Compressed) ? 1 : 2;

std::vector<uint8_t> 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<uint8_t>(y.get_bit(0));
x.serialize_to(std::span{result}.subspan(1, p_bytes));
stuffer.append(0x02 | static_cast<uint8_t>(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<uint8_t>(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<uint8_t>(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");
}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/pubkey/pubkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <botan/internal/fmt.h>
#include <botan/internal/parsing.h>
#include <botan/internal/pss_params.h>
#include <botan/internal/stl_util.h>

namespace Botan {

Expand Down Expand Up @@ -286,9 +287,11 @@ std::vector<uint8_t> der_encode_signature(std::span<const uint8_t> sig, size_t p
throw Encoding_Error("Unexpected size for DER signature");
}

BufferSlicer bs_sig(sig);
std::vector<BigInt> 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<uint8_t> output;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/rfc6979/rfc6979.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<HMAC_DRBG>(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;
Expand Down

0 comments on commit bb10382

Please sign in to comment.