Botan  2.6.0
Crypto and TLS for C++11
eckcdsa.cpp
Go to the documentation of this file.
1 /*
2 * ECKCDSA (ISO/IEC 14888-3:2006/Cor.2:2009)
3 * (C) 2016 RenĂ© Korthaus, Sirrix AG
4 * (C) 2018 Jack Lloyd
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/eckcdsa.h>
10 #include <botan/internal/pk_ops_impl.h>
11 #include <botan/keypair.h>
12 #include <botan/reducer.h>
13 #include <botan/emsa.h>
14 #include <botan/hash.h>
15 #include <botan/rng.h>
16 
17 namespace Botan {
18 
20  bool strong) const
21  {
22  if(!public_point().on_the_curve())
23  {
24  return false;
25  }
26 
27  if(!strong)
28  {
29  return true;
30  }
31 
32  return KeyPair::signature_consistency_check(rng, *this, "EMSA1(SHA-256)");
33  }
34 
35 namespace {
36 
37 /**
38 * ECKCDSA signature operation
39 */
40 class ECKCDSA_Signature_Operation final : public PK_Ops::Signature_with_EMSA
41  {
42  public:
43 
44  ECKCDSA_Signature_Operation(const ECKCDSA_PrivateKey& eckcdsa,
45  const std::string& emsa) :
46  PK_Ops::Signature_with_EMSA(emsa),
47  m_group(eckcdsa.domain()),
48  m_x(eckcdsa.private_value()),
49  m_prefix()
50  {
51  const BigInt public_point_x = eckcdsa.public_point().get_affine_x();
52  const BigInt public_point_y = eckcdsa.public_point().get_affine_y();
53 
54  m_prefix.resize(public_point_x.bytes() + public_point_y.bytes());
55  public_point_x.binary_encode(m_prefix.data());
56  public_point_y.binary_encode(&m_prefix[public_point_x.bytes()]);
57  m_prefix.resize(HashFunction::create(hash_for_signature())->hash_block_size()); // use only the "hash input block size" leftmost bits
58  }
59 
60  secure_vector<uint8_t> raw_sign(const uint8_t msg[], size_t msg_len,
61  RandomNumberGenerator& rng) override;
62 
63  size_t max_input_bits() const override { return m_group.get_order_bits(); }
64 
65  bool has_prefix() override { return true; }
66  secure_vector<uint8_t> message_prefix() const override { return m_prefix; }
67 
68  private:
69  const EC_Group m_group;
70  const BigInt& m_x;
71  secure_vector<uint8_t> m_prefix;
72  std::vector<BigInt> m_ws;
73  };
74 
75 secure_vector<uint8_t>
76 ECKCDSA_Signature_Operation::raw_sign(const uint8_t msg[], size_t,
77  RandomNumberGenerator& rng)
78  {
79  const BigInt k = m_group.random_scalar(rng);
80  const BigInt k_times_P_x = m_group.blinded_base_point_multiply_x(k, rng, m_ws);
81 
82  secure_vector<uint8_t> to_be_hashed(k_times_P_x.bytes());
83  k_times_P_x.binary_encode(to_be_hashed.data());
84 
85  std::unique_ptr<EMSA> emsa = this->clone_emsa();
86  emsa->update(to_be_hashed.data(), to_be_hashed.size());
87  secure_vector<uint8_t> c = emsa->raw_data();
88  c = emsa->encoding_of(c, max_input_bits(), rng);
89 
90  const BigInt r(c.data(), c.size());
91 
92  xor_buf(c, msg, c.size());
93  BigInt w(c.data(), c.size());
94  w = m_group.mod_order(w);
95 
96  const BigInt s = m_group.multiply_mod_order(m_x, k - w);
97  if(s.is_zero())
98  throw Internal_Error("During ECKCDSA signature generation created zero s");
99 
100  secure_vector<uint8_t> output = BigInt::encode_1363(r, c.size());
101  output += BigInt::encode_1363(s, m_group.get_order_bytes());
102  return output;
103  }
104 
105 /**
106 * ECKCDSA verification operation
107 */
108 class ECKCDSA_Verification_Operation final : public PK_Ops::Verification_with_EMSA
109  {
110  public:
111 
112  ECKCDSA_Verification_Operation(const ECKCDSA_PublicKey& eckcdsa,
113  const std::string& emsa) :
114  PK_Ops::Verification_with_EMSA(emsa),
115  m_group(eckcdsa.domain()),
116  m_public_point(eckcdsa.public_point()),
117  m_prefix()
118  {
119  const BigInt public_point_x = m_public_point.get_affine_x();
120  const BigInt public_point_y = m_public_point.get_affine_y();
121 
122  m_prefix.resize(public_point_x.bytes() + public_point_y.bytes());
123  public_point_x.binary_encode(&m_prefix[0]);
124  public_point_y.binary_encode(&m_prefix[public_point_x.bytes()]);
125  m_prefix.resize(HashFunction::create(hash_for_signature())->hash_block_size()); // use only the "hash input block size" leftmost bits
126  }
127 
128  bool has_prefix() override { return true; }
129  secure_vector<uint8_t> message_prefix() const override { return m_prefix; }
130 
131  size_t max_input_bits() const override { return m_group.get_order_bits(); }
132 
133  bool with_recovery() const override { return false; }
134 
135  bool verify(const uint8_t msg[], size_t msg_len,
136  const uint8_t sig[], size_t sig_len) override;
137  private:
138  const EC_Group m_group;
139  const PointGFp& m_public_point;
140  secure_vector<uint8_t> m_prefix;
141  };
142 
143 bool ECKCDSA_Verification_Operation::verify(const uint8_t msg[], size_t,
144  const uint8_t sig[], size_t sig_len)
145  {
146  const std::unique_ptr<HashFunction> hash = HashFunction::create(hash_for_signature());
147  //calculate size of r
148 
149  const size_t order_bytes = m_group.get_order_bytes();
150 
151  const size_t size_r = std::min(hash -> output_length(), order_bytes);
152  if(sig_len != size_r + order_bytes)
153  {
154  return false;
155  }
156 
157  secure_vector<uint8_t> r(sig, sig + size_r);
158 
159  // check that 0 < s < q
160  const BigInt s(sig + size_r, order_bytes);
161 
162  if(s <= 0 || s >= m_group.get_order())
163  {
164  return false;
165  }
166 
167  secure_vector<uint8_t> r_xor_e(r);
168  xor_buf(r_xor_e, msg, r.size());
169  BigInt w(r_xor_e.data(), r_xor_e.size());
170  w = m_group.mod_order(w);
171 
172  const PointGFp q = m_group.point_multiply(w, m_public_point, s);
173  const BigInt q_x = q.get_affine_x();
174  secure_vector<uint8_t> c(q_x.bytes());
175  q_x.binary_encode(c.data());
176  std::unique_ptr<EMSA> emsa = this->clone_emsa();
177  emsa->update(c.data(), c.size());
178  secure_vector<uint8_t> v = emsa->raw_data();
179  Null_RNG rng;
180  v = emsa->encoding_of(v, max_input_bits(), rng);
181 
182  return (v == r);
183  }
184 
185 }
186 
187 std::unique_ptr<PK_Ops::Verification>
189  const std::string& provider) const
190  {
191  if(provider == "base" || provider.empty())
192  return std::unique_ptr<PK_Ops::Verification>(new ECKCDSA_Verification_Operation(*this, params));
193  throw Provider_Not_Found(algo_name(), provider);
194  }
195 
196 std::unique_ptr<PK_Ops::Signature>
198  const std::string& params,
199  const std::string& provider) const
200  {
201  if(provider == "base" || provider.empty())
202  return std::unique_ptr<PK_Ops::Signature>(new ECKCDSA_Signature_Operation(*this, params));
203  throw Provider_Not_Found(algo_name(), provider);
204  }
205 
206 }
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: eckcdsa.cpp:19
const PointGFp & public_point() const
Definition: ecc_key.h:57
void binary_encode(uint8_t buf[]) const
Definition: bigint.cpp:304
BigInt get_affine_x() const
Definition: point_gfp.cpp:491
BigInt get_affine_y() const
Definition: point_gfp.cpp:510
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const override
Definition: eckcdsa.cpp:197
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &private_key, const Public_Key &public_key, const std::string &padding)
Definition: keypair.cpp:49
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:174
std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const override
Definition: eckcdsa.cpp:188
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
Definition: alg_id.cpp:13
size_t bytes() const
Definition: bigint.cpp:208
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
MechanismType hash
std::string algo_name() const override
Definition: eckcdsa.h:44