From fe2ff50ba5bcd04bee650a295d0ea868504c3128 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 24 May 2024 13:58:55 -0400 Subject: [PATCH] Address some review comments --- src/lib/pubkey/ec_group/ec_group.cpp | 19 +++++++++---------- src/lib/pubkey/ec_group/ec_group.h | 10 ++++------ src/lib/pubkey/ec_group/ec_point.cpp | 2 +- src/lib/pubkey/ec_group/ec_point.h | 2 +- src/lib/pubkey/ecc_key/ecc_key.cpp | 5 ++++- src/lib/pubkey/eckcdsa/eckcdsa.cpp | 8 ++------ 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/lib/pubkey/ec_group/ec_group.cpp b/src/lib/pubkey/ec_group/ec_group.cpp index c5f8ec2e95..17d71e322b 100644 --- a/src/lib/pubkey/ec_group/ec_group.cpp +++ b/src/lib/pubkey/ec_group/ec_group.cpp @@ -322,15 +322,14 @@ std::shared_ptr EC_Group::load_EC_group_info(const char* p_str, } //static -std::pair, bool> EC_Group::BER_decode_EC_group(const uint8_t bits[], - size_t len, +std::pair, bool> EC_Group::BER_decode_EC_group(std::span bits, EC_Group_Source source) { - BER_Decoder ber(bits, len); + BER_Decoder ber(bits); BER_Object obj = ber.get_next_object(); if(obj.type() == ASN1_Type::ObjectId) { OID dom_par_oid; - BER_Decoder(bits, len).decode(dom_par_oid); + BER_Decoder(bits).decode(dom_par_oid); return std::make_pair(ec_group_data().lookup(dom_par_oid), false); } @@ -339,7 +338,7 @@ std::pair, bool> EC_Group::BER_decode_EC_group(co std::vector base_pt; std::vector seed; - BER_Decoder(bits, len) + BER_Decoder(bits) .start_sequence() .decode_and_check(1, "Unknown ECC param version code") .start_sequence() @@ -447,9 +446,9 @@ EC_Group::EC_Group(std::string_view str) { if(m_data == nullptr) { if(str.size() > 30 && str.substr(0, 29) == "-----BEGIN EC PARAMETERS-----") { // OK try it as PEM ... - secure_vector ber = PEM_Code::decode_check_label(str, "EC PARAMETERS"); + const auto ber = PEM_Code::decode_check_label(str, "EC PARAMETERS"); - auto data = BER_decode_EC_group(ber.data(), ber.size(), EC_Group_Source::ExternalSource); + auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource); this->m_data = data.first; this->m_explicit_encoding = data.second; } @@ -463,7 +462,7 @@ EC_Group::EC_Group(std::string_view str) { //static EC_Group EC_Group::from_PEM(std::string_view pem) { const auto ber = PEM_Code::decode_check_label(pem, "EC PARAMETERS"); - return EC_Group(ber.data(), ber.size()); + return EC_Group(ber); } EC_Group::EC_Group(const BigInt& p, @@ -505,8 +504,8 @@ EC_Group::EC_Group(const OID& oid, ec_group_data().lookup_or_create(p, a, b, base_x, base_y, order, cofactor, oid, EC_Group_Source::ExternalSource); } -EC_Group::EC_Group(const uint8_t ber[], size_t ber_len) { - auto data = BER_decode_EC_group(ber, ber_len, EC_Group_Source::ExternalSource); +EC_Group::EC_Group(std::span ber) { + auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource); m_data = data.first; m_explicit_encoding = data.second; } diff --git a/src/lib/pubkey/ec_group/ec_group.h b/src/lib/pubkey/ec_group/ec_group.h index c1504f5bed..573cb748ec 100644 --- a/src/lib/pubkey/ec_group/ec_group.h +++ b/src/lib/pubkey/ec_group/ec_group.h @@ -111,12 +111,11 @@ class BOTAN_PUBLIC_API(2, 0) EC_Group final { /** * Decode a BER encoded ECC domain parameter set * @param ber the bytes of the BER encoding - * @param ber_len the length of ber */ - explicit EC_Group(const uint8_t ber[], size_t ber_len); + explicit EC_Group(std::span ber); - template - EC_Group(const std::vector& ber) : EC_Group(ber.data(), ber.size()) {} + BOTAN_DEPRECATED("Use EC_Group(std::span)") + EC_Group(const uint8_t ber[], size_t ber_len) : EC_Group(std::span{ber, ber_len}) {} /** * Create an EC domain by OID (or throw if unknown) @@ -449,8 +448,7 @@ class BOTAN_PUBLIC_API(2, 0) EC_Group final { EC_Group(std::shared_ptr&& data); - static std::pair, bool> BER_decode_EC_group(const uint8_t bits[], - size_t len, + static std::pair, bool> BER_decode_EC_group(std::span ber, EC_Group_Source source); static std::shared_ptr load_EC_group_info(const char* p, diff --git a/src/lib/pubkey/ec_group/ec_point.cpp b/src/lib/pubkey/ec_group/ec_point.cpp index 09ac5684dd..69ec2c4336 100644 --- a/src/lib/pubkey/ec_group/ec_point.cpp +++ b/src/lib/pubkey/ec_group/ec_point.cpp @@ -383,7 +383,7 @@ EC_Point EC_Point::mul(const BigInt& scalar) const { } //static -void EC_Point::force_all_affine(std::vector& points, secure_vector& ws) { +void EC_Point::force_all_affine(std::span points, secure_vector& ws) { if(points.size() <= 1) { for(auto& point : points) { point.force_affine(); diff --git a/src/lib/pubkey/ec_group/ec_point.h b/src/lib/pubkey/ec_group/ec_point.h index 49d6506bea..ff56c734d0 100644 --- a/src/lib/pubkey/ec_group/ec_point.h +++ b/src/lib/pubkey/ec_group/ec_point.h @@ -145,7 +145,7 @@ class BOTAN_PUBLIC_API(2, 0) EC_Point final { /** * Force all points on the list to affine coordinates */ - static void force_all_affine(std::vector& points, secure_vector& ws); + static void force_all_affine(std::span points, secure_vector& ws); bool is_affine() const; diff --git a/src/lib/pubkey/ecc_key/ecc_key.cpp b/src/lib/pubkey/ecc_key/ecc_key.cpp index 66238020ba..853bf0b686 100644 --- a/src/lib/pubkey/ecc_key/ecc_key.cpp +++ b/src/lib/pubkey/ecc_key/ecc_key.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include namespace Botan { @@ -176,7 +177,9 @@ bool EC_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const { } const BigInt& EC_PublicKey::get_int_field(std::string_view field) const { - if(field == "base_x") { + if(field == "public_x" || field == "public_y") { + throw Not_Implemented(fmt("EC_PublicKey::get_int_field no longer implements getter for {}", field)); + } else if(field == "base_x") { return this->domain().get_g_x(); } else if(field == "base_y") { return this->domain().get_g_y(); diff --git a/src/lib/pubkey/eckcdsa/eckcdsa.cpp b/src/lib/pubkey/eckcdsa/eckcdsa.cpp index 8fb56b9175..293823fd4e 100644 --- a/src/lib/pubkey/eckcdsa/eckcdsa.cpp +++ b/src/lib/pubkey/eckcdsa/eckcdsa.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace Botan { @@ -73,12 +74,7 @@ std::unique_ptr eckcdsa_signature_hash(const AlgorithmIdentifier& } std::vector eckcdsa_prefix(const EC_Point& point, size_t hash_block_size) { - const auto public_x = point.x_bytes(); - const auto public_y = point.y_bytes(); - - std::vector prefix(public_x.size() + public_y.size()); - copy_mem(&prefix[0], public_x.data(), public_x.size()); - copy_mem(&prefix[public_x.size()], public_y.data(), public_y.size()); + auto prefix = concat>(point.x_bytes(), point.y_bytes()); // Either truncate or zero-extend to match the hash block size prefix.resize(hash_block_size);