Skip to content

Commit

Permalink
Address some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed May 24, 2024
1 parent 15488e7 commit af9b85f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
19 changes: 9 additions & 10 deletions src/lib/pubkey/ec_group/ec_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,14 @@ std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(const char* p_str,
}

//static
std::pair<std::shared_ptr<EC_Group_Data>, bool> EC_Group::BER_decode_EC_group(const uint8_t bits[],
size_t len,
std::pair<std::shared_ptr<EC_Group_Data>, bool> EC_Group::BER_decode_EC_group(std::span<const uint8_t> 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);
}

Expand All @@ -339,7 +338,7 @@ std::pair<std::shared_ptr<EC_Group_Data>, bool> EC_Group::BER_decode_EC_group(co
std::vector<uint8_t> base_pt;
std::vector<uint8_t> seed;

BER_Decoder(bits, len)
BER_Decoder(bits)
.start_sequence()
.decode_and_check<size_t>(1, "Unknown ECC param version code")
.start_sequence()
Expand Down Expand Up @@ -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<uint8_t> 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;
}
Expand All @@ -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,
Expand Down Expand Up @@ -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<const uint8_t> ber) {
auto data = BER_decode_EC_group(ber, EC_Group_Source::ExternalSource);
m_data = data.first;
m_explicit_encoding = data.second;
}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/pubkey/ec_group/ec_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const uint8_t> ber);

template <typename Alloc>
EC_Group(const std::vector<uint8_t, Alloc>& 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)
Expand Down Expand Up @@ -449,8 +448,7 @@ class BOTAN_PUBLIC_API(2, 0) EC_Group final {

EC_Group(std::shared_ptr<EC_Group_Data>&& data);

static std::pair<std::shared_ptr<EC_Group_Data>, bool> BER_decode_EC_group(const uint8_t bits[],
size_t len,
static std::pair<std::shared_ptr<EC_Group_Data>, bool> BER_decode_EC_group(std::span<const uint8_t> ber,
EC_Group_Source source);

static std::shared_ptr<EC_Group_Data> load_EC_group_info(const char* p,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/ec_group/ec_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ EC_Point EC_Point::mul(const BigInt& scalar) const {
}

//static
void EC_Point::force_all_affine(std::vector<EC_Point>& points, secure_vector<word>& ws) {
void EC_Point::force_all_affine(std::span<EC_Point> points, secure_vector<word>& ws) {
if(points.size() <= 1) {
for(auto& point : points) {
point.force_affine();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pubkey/ec_group/ec_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<EC_Point>& points, secure_vector<word>& ws);
static void force_all_affine(std::span<EC_Point> points, secure_vector<word>& ws);

bool is_affine() const;

Expand Down
5 changes: 4 additions & 1 deletion src/lib/pubkey/ecc_key/ecc_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <botan/ec_point.h>
#include <botan/numthry.h>
#include <botan/secmem.h>
#include <botan/internal/fmt.h>
#include <botan/internal/workfactor.h>

namespace Botan {
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 2 additions & 6 deletions src/lib/pubkey/eckcdsa/eckcdsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <botan/internal/pk_ops_impl.h>
#include <botan/internal/point_mul.h>
#include <botan/internal/scan_name.h>
#include <botan/internal/stl_util.h>

namespace Botan {

Expand Down Expand Up @@ -73,12 +74,7 @@ std::unique_ptr<HashFunction> eckcdsa_signature_hash(const AlgorithmIdentifier&
}

std::vector<uint8_t> 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<uint8_t> 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<std::vector<uint8_t>>(point.x_bytes(), point.y_bytes());

// Either truncate or zero-extend to match the hash block size
prefix.resize(hash_block_size);
Expand Down

0 comments on commit af9b85f

Please sign in to comment.