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