10#include <botan/eckcdsa.h>
12#include <botan/hash.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/keypair.h>
16#include <botan/internal/parsing.h>
17#include <botan/internal/pk_ops_impl.h>
18#include <botan/internal/scan_name.h>
19#include <botan/internal/stl_util.h>
41std::unique_ptr<HashFunction> eckcdsa_signature_hash(std::string_view padding) {
46 SCAN_Name req(padding);
48 if(req.algo_name() ==
"EMSA1" && req.arg_count() == 1) {
57 throw Algorithm_Not_Found(padding);
60std::unique_ptr<HashFunction> eckcdsa_signature_hash(
const AlgorithmIdentifier& alg_id) {
61 const auto oid_info =
split_on(alg_id.oid().to_formatted_string(),
'/');
63 if(oid_info.size() != 2 || oid_info[0] !=
"ECKCDSA") {
64 throw Decoding_Error(
fmt(
"Unexpected AlgorithmIdentifier OID {} in association with ECKCDSA key", alg_id.oid()));
67 if(!alg_id.parameters_are_empty()) {
68 throw Decoding_Error(
"Unexpected non-empty AlgorithmIdentifier parameters for ECKCDSA");
74std::vector<uint8_t> eckcdsa_prefix(
const EC_AffinePoint& point,
size_t hash_block_size) {
75 auto prefix = point.xy_bytes<std::vector<uint8_t>>();
78 prefix.resize(hash_block_size);
106void truncate_hash_if_needed(std::vector<uint8_t>& digest,
size_t group_order_bytes) {
107 if(digest.size() > group_order_bytes) {
108 const size_t bytes_to_truncate = digest.size() - group_order_bytes;
109 digest.erase(digest.begin(), digest.begin() + bytes_to_truncate);
116class ECKCDSA_Signature_Operation
final :
public PK_Ops::Signature {
118 ECKCDSA_Signature_Operation(
const ECKCDSA_PrivateKey& eckcdsa, std::string_view padding) :
119 m_group(eckcdsa.domain()),
120 m_x(eckcdsa._private_key()),
121 m_hash(eckcdsa_signature_hash(padding)),
122 m_prefix(eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size())),
123 m_prefix_used(false) {}
125 void update(std::span<const uint8_t> input)
override {
127 m_hash->update(m_prefix);
128 m_prefix_used =
true;
130 m_hash->update(input);
133 std::vector<uint8_t> sign(RandomNumberGenerator& rng)
override {
134 m_prefix_used =
false;
135 std::vector<uint8_t> digest = m_hash->final_stdvec();
136 truncate_hash_if_needed(digest, m_group.get_order_bytes());
137 return raw_sign(digest, rng);
140 size_t signature_length()
const override {
return 2 * m_group.get_order_bytes(); }
142 AlgorithmIdentifier algorithm_identifier()
const override;
144 std::string hash_function()
const override {
return m_hash->name(); }
147 std::vector<uint8_t> raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng);
149 const EC_Group m_group;
151 std::unique_ptr<HashFunction> m_hash;
152 std::vector<uint8_t> m_prefix;
153 std::vector<BigInt> m_ws;
157AlgorithmIdentifier ECKCDSA_Signature_Operation::algorithm_identifier()
const {
158 const std::string full_name =
"ECKCDSA/" + m_hash->name();
159 const OID oid = OID::from_string(full_name);
160 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_EMPTY_PARAM);
163std::vector<uint8_t> ECKCDSA_Signature_Operation::raw_sign(std::span<const uint8_t> msg, RandomNumberGenerator& rng) {
164 const auto k = EC_Scalar::random(m_group, rng);
166 m_hash->update(EC_AffinePoint::g_mul(k, rng, m_ws).x_bytes());
167 auto c = m_hash->final_stdvec();
173 const auto w = EC_Scalar::from_bytes_mod_order(m_group, c);
175 const auto s = m_x * (k - w);
177 throw Internal_Error(
"During ECKCDSA signature generation created zero s");
180 return concat(r, s.serialize());
186class ECKCDSA_Verification_Operation
final :
public PK_Ops::Verification {
188 ECKCDSA_Verification_Operation(
const ECKCDSA_PublicKey& eckcdsa, std::string_view padding) :
189 m_group(eckcdsa.domain()),
190 m_gy_mul(eckcdsa._public_key()),
191 m_hash(eckcdsa_signature_hash(padding)),
192 m_prefix(eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size())),
193 m_prefix_used(false) {}
195 ECKCDSA_Verification_Operation(
const ECKCDSA_PublicKey& eckcdsa,
const AlgorithmIdentifier& alg_id) :
196 m_group(eckcdsa.domain()),
197 m_gy_mul(eckcdsa._public_key()),
198 m_hash(eckcdsa_signature_hash(alg_id)),
199 m_prefix(eckcdsa_prefix(eckcdsa._public_key(), m_hash->hash_block_size())),
200 m_prefix_used(false) {}
202 void update(std::span<const uint8_t> msg)
override;
204 bool is_valid_signature(std::span<const uint8_t> sig)
override;
206 std::string hash_function()
const override {
return m_hash->name(); }
209 bool verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig);
211 const EC_Group m_group;
212 const EC_Group::Mul2Table m_gy_mul;
213 std::unique_ptr<HashFunction> m_hash;
214 std::vector<uint8_t> m_prefix;
218void ECKCDSA_Verification_Operation::update(std::span<const uint8_t> msg) {
220 m_prefix_used =
true;
221 m_hash->update(m_prefix.data(), m_prefix.size());
226bool ECKCDSA_Verification_Operation::is_valid_signature(std::span<const uint8_t> sig) {
227 m_prefix_used =
false;
228 std::vector<uint8_t> digest = m_hash->final_stdvec();
230 return verify(digest, sig);
233bool ECKCDSA_Verification_Operation::verify(std::span<const uint8_t> msg, std::span<const uint8_t> sig) {
236 const size_t size_r = std::min(msg.size(), order_bytes);
237 if(sig.size() != size_r + order_bytes) {
241 auto r = sig.first(size_r);
243 if(
auto s = EC_Scalar::deserialize(m_group, sig.last(order_bytes))) {
244 std::vector<uint8_t> r_xor_e(r.size());
245 xor_buf(r_xor_e, r, msg.first(size_r));
247 const auto w = EC_Scalar::from_bytes_mod_order(m_group, r_xor_e);
250 std::vector<uint8_t> v = m_hash->process<std::vector<uint8_t>>(q->x_bytes());
262 return std::make_unique<ECKCDSA_PrivateKey>(rng, domain());
266 std::string_view provider)
const {
267 if(provider ==
"base" || provider.empty()) {
268 return std::make_unique<ECKCDSA_Verification_Operation>(*
this, params);
275 if(provider ==
"base" || provider.empty()) {
276 return std::make_unique<ECKCDSA_Verification_Operation>(*
this, signature_algorithm);
283 std::string_view params,
284 std::string_view provider)
const {
285 if(provider ==
"base" || provider.empty()) {
286 return std::make_unique<ECKCDSA_Signature_Operation>(*
this, params);
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
std::unique_ptr< Public_Key > public_key() const override
bool check_key(RandomNumberGenerator &rng, bool) const override
std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const override
std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::optional< EC_AffinePoint > mul2_vartime(const EC_Scalar &x, const EC_Scalar &y) const
size_t get_order_bytes() const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
const EC_Group & domain() const
const EC_Point & public_point() const
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, std::string_view padding)
std::string fmt(std::string_view format, const T &... args)
std::vector< std::string > split_on(std::string_view str, char delim)
constexpr auto concat(Rs &&... ranges)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)