Botan 3.0.0-alpha0
Crypto and TLS for C&
ecgdsa.cpp
Go to the documentation of this file.
1/*
2* ECGDSA (BSI-TR-03111, version 2.0)
3* (C) 2016 René Korthaus
4* (C) 2018 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/ecgdsa.h>
10#include <botan/internal/keypair.h>
11#include <botan/reducer.h>
12#include <botan/internal/pk_ops_impl.h>
13#include <botan/internal/point_mul.h>
14
15namespace Botan {
16
17std::unique_ptr<Public_Key> ECGDSA_PrivateKey::public_key() const
18 {
19 return std::make_unique<ECGDSA_PublicKey>(domain(), public_point());
20 }
21
23 bool strong) const
24 {
25 if(!public_point().on_the_curve())
26 return false;
27
28 if(!strong)
29 return true;
30
31 return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-256)");
32 }
33
34namespace {
35
36/**
37* ECGDSA signature operation
38*/
39class ECGDSA_Signature_Operation final : public PK_Ops::Signature_with_EMSA
40 {
41 public:
42
43 ECGDSA_Signature_Operation(const ECGDSA_PrivateKey& ecgdsa,
44 const std::string& emsa) :
45 PK_Ops::Signature_with_EMSA(emsa),
46 m_group(ecgdsa.domain()),
47 m_x(ecgdsa.private_value())
48 {
49 }
50
51 secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len,
52 RandomNumberGenerator& rng) override;
53
54 size_t signature_length() const override { return 2*m_group.get_order_bytes(); }
55
56 size_t max_input_bits() const override { return m_group.get_order_bits(); }
57
58 private:
59 const EC_Group m_group;
60 const BigInt& m_x;
61 std::vector<BigInt> m_ws;
62 };
63
64secure_vector<uint8_t>
65ECGDSA_Signature_Operation::raw_sign(const uint8_t msg[], size_t msg_len,
66 RandomNumberGenerator& rng)
67 {
68 const BigInt m = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
69
70 const BigInt k = m_group.random_scalar(rng);
71
72 const BigInt r = m_group.mod_order(
73 m_group.blinded_base_point_multiply_x(k, rng, m_ws));
74
75 const BigInt kr = m_group.multiply_mod_order(k, r);
76
77 const BigInt s = m_group.multiply_mod_order(m_x, kr - m);
78
79 // With overwhelming probability, a bug rather than actual zero r/s
80 if(r.is_zero() || s.is_zero())
81 throw Internal_Error("During ECGDSA signature generated zero r/s");
82
83 return BigInt::encode_fixed_length_int_pair(r, s, m_group.get_order_bytes());
84 }
85
86/**
87* ECGDSA verification operation
88*/
89class ECGDSA_Verification_Operation final : public PK_Ops::Verification_with_EMSA
90 {
91 public:
92
93 ECGDSA_Verification_Operation(const ECGDSA_PublicKey& ecgdsa,
94 const std::string& emsa) :
95 PK_Ops::Verification_with_EMSA(emsa),
96 m_group(ecgdsa.domain()),
97 m_gy_mul(m_group.get_base_point(), ecgdsa.public_point())
98 {
99 }
100
101 size_t max_input_bits() const override { return m_group.get_order_bits(); }
102
103 bool with_recovery() const override { return false; }
104
105 bool verify(const uint8_t msg[], size_t msg_len,
106 const uint8_t sig[], size_t sig_len) override;
107 private:
108 const EC_Group m_group;
109 const PointGFp_Multi_Point_Precompute m_gy_mul;
110 };
111
112bool ECGDSA_Verification_Operation::verify(const uint8_t msg[], size_t msg_len,
113 const uint8_t sig[], size_t sig_len)
114 {
115 if(sig_len != m_group.get_order_bytes() * 2)
116 return false;
117
118 const BigInt e = BigInt::from_bytes_with_max_bits(msg, msg_len, m_group.get_order_bits());
119
120 const BigInt r(sig, sig_len / 2);
121 const BigInt s(sig + sig_len / 2, sig_len / 2);
122
123 if(r <= 0 || r >= m_group.get_order() || s <= 0 || s >= m_group.get_order())
124 return false;
125
126 const BigInt w = m_group.inverse_mod_order(r);
127
128 const BigInt u1 = m_group.multiply_mod_order(e, w);
129 const BigInt u2 = m_group.multiply_mod_order(s, w);
130 const PointGFp R = m_gy_mul.multi_exp(u1, u2);
131
132 if(R.is_zero())
133 return false;
134
135 const BigInt v = m_group.mod_order(R.get_affine_x());
136 return (v == r);
137 }
138
139}
140
141std::unique_ptr<PK_Ops::Verification>
143 const std::string& provider) const
144 {
145 if(provider == "base" || provider.empty())
146 return std::make_unique<ECGDSA_Verification_Operation>(*this, params);
147 throw Provider_Not_Found(algo_name(), provider);
148 }
149
150std::unique_ptr<PK_Ops::Signature>
152 const std::string& params,
153 const std::string& provider) const
154 {
155 if(provider == "base" || provider.empty())
156 return std::make_unique<ECGDSA_Signature_Operation>(*this, params);
157 throw Provider_Not_Found(algo_name(), provider);
158 }
159
160}
static secure_vector< uint8_t > encode_fixed_length_int_pair(const BigInt &n1, const BigInt &n2, size_t bytes)
Definition: big_code.cpp:128
static BigInt from_bytes_with_max_bits(const uint8_t buf[], size_t length, size_t max_bits)
Definition: bigint.cpp:110
std::unique_ptr< Public_Key > public_key() const override
Definition: ecgdsa.cpp:17
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: ecgdsa.cpp:151
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: ecgdsa.cpp:22
std::string algo_name() const override
Definition: ecgdsa.h:44
std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const override
Definition: ecgdsa.cpp:142
const EC_Group & domain() const
Definition: ecc_key.h:56
const PointGFp & public_point() const
Definition: ecc_key.h:41
PointGFp multi_exp(const BigInt &k1, const BigInt &k2) const
Definition: point_mul.cpp:377
int(* final)(unsigned char *, CTX *)
Polynomial v
Definition: kyber.cpp:822
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:47
Definition: alg_id.cpp:13