scclib
Stable Cloud Computing C++ Library
ecc.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_CRYPTO_ECC_H
32 #define _SCC_CRYPTO_ECC_H
33 
34 #include <memory>
35 #include <vector>
36 #include <crypto/bignum.h>
37 
38 namespace scc::crypto {
39 
58 struct EccGfpBase;
59 struct EccGfpPointBase;
60 class EccGfpPoint;
61 
76 class EccGfp
77 {
78  friend class EccGfpPoint;
79 
80  std::shared_ptr<EccGfpBase> m_ctx;
81 
82 public:
83 
85  enum class Type
86  {
87  std_p192r1,
88  std_p224r1,
89  std_p256r1,
90  std_p384r1,
91  std_p521r1,
92  std_p256sm2,
93  //curve_25519, ///< X25519 algorithm (128 bit security level)
94  //curve_448, ///< X448 algorithm (224 bit security level)
95  };
96 
98  virtual ~EccGfp();
99 
101  void reset(Type type);
102 
104  static bool valid(const EccGfp&);
105  bool valid() const
106  {
107  return valid(*this);
108  }
109 
111  int bit_width() const;
112 
117  void generate_key_pair(Bignum& priv_key, EccGfpPoint& pub_key)
118  {
119  private_key(priv_key);
120  public_key(priv_key, pub_key);
121  }
122 
130  static bool validate_key_pair(const Bignum&, const EccGfpPoint&);
131 
136 
141  void public_key(const Bignum&, EccGfpPoint&);
142 
146 
165 
166  static void sign_ecdsa(const void* loc, int len, const EccGfp::Type& t, const scc::crypto::Bignum& rk, scc::crypto::Bignum& tk, scc::crypto::Bignum& x, scc::crypto::Bignum& y)
167  {
168  EccGfp curve(t);
169  sign_ecdsa(loc, len, curve, rk, tk, x, y);
170  }
171 
182  static bool verify_ecdsa(const void*, int, const EccGfpPoint&, const scc::crypto::Bignum&, const scc::crypto::Bignum&);
183 
194 };
195 
197 {
198  friend class EccGfp;
199  friend class EcdsaSignature;
200 
201  std::shared_ptr<EccGfpPointBase> m_ctx;
202 
203 public:
207  EccGfpPoint(const EccGfp& curve)
208  {
209  reset(curve);
210  }
211  EccGfpPoint(const EccGfp::Type& curve_type)
212  {
213  EccGfp curve(curve_type);
214  reset(curve);
215  }
216 
217  virtual ~EccGfpPoint();
218 
220  void reset()
221  {
222  m_ctx.reset();
223  }
225  void reset(const EccGfp&);
226 
234 
239  void set(const scc::crypto::Bignum& x, const scc::crypto::Bignum& y, EccGfp& curve)
240  {
241  reset(curve);
242  set(x, y);
243  }
248  void set(const void*, int);
249  void set(const std::vector<char>& v)
250  {
251  set(v.data(), v.size());
252  }
253 
258  void set(const void* loc, int len, EccGfp& curve)
259  {
260  reset(curve);
261  set(loc, len);
262  }
263  void set(const std::vector<char>& v, EccGfp& curve)
264  {
265  set(v.data(), v.size(), curve);
266  }
267 
272  void set();
273 
285  void get(std::vector<char>&) const;
286 
288  static bool infinite(const EccGfpPoint&, const EccGfp&);
289  bool infinite(const EccGfp& curve) const
290  {
291  return infinite(*this, curve);
292  }
293 
295  static bool infinite(const EccGfpPoint&);
296  bool infinite() const
297  {
298  return infinite(*this);
299  }
300 
303  static bool valid(const EccGfpPoint&, const EccGfp&);
304  bool valid(const EccGfp& curve) const
305  {
306  return valid(*this, curve);
307  }
308 
311  static bool valid(const EccGfpPoint&);
312  bool valid() const
313  {
314  return valid(*this);
315  }
316 
318  bool equal(const EccGfpPoint&) const;
319 
320  bool operator==(const EccGfpPoint& b) const
321  {
322  return equal(b);
323  }
324  bool operator!=(const EccGfpPoint& b) const
325  {
326  return !equal(b);
327  }
328 };
329 
333 } // namespace
334 
335 #endif
Big number arithmetic.
Big number.
Definition: bignum.h:59
static bool infinite(const EccGfpPoint &)
Test the point for infinity.
EccGfpPoint(const EccGfp &curve)
Construct a point on the elliptic curve.
Definition: ecc.h:207
bool equal(const EccGfpPoint &) const
Test for equality with a second point.
void reset()
Reset the point to invalid.
Definition: ecc.h:220
void get(scc::crypto::Bignum &, scc::crypto::Bignum &) const
Get the coordinates.
void set(const void *, int)
Set the point from a data string.
EccGfpPoint()
Construct an invalid point.
Definition: ecc.h:205
void get(std::vector< char > &) const
Get the coordinates to a data string.
static bool valid(const EccGfpPoint &, const EccGfp &)
Verify the point on a curve.
void set(const scc::crypto::Bignum &x, const scc::crypto::Bignum &y, EccGfp &curve)
Set point to a new curve.
Definition: ecc.h:239
void set()
Set point to infinity.
void reset(const EccGfp &)
Reset the point to fall on the elliptic curve.
void set(const void *loc, int len, EccGfp &curve)
Set the point from a data string on a new curve.
Definition: ecc.h:258
static bool infinite(const EccGfpPoint &, const EccGfp &)
Test the point for infinity on a curve.
void set(const scc::crypto::Bignum &, const scc::crypto::Bignum &)
Set point.
static bool valid(const EccGfpPoint &)
Verify the point.
Elliptic curve cryptography over Galois prime field GF(p) curve.
Definition: ecc.h:77
static void sign_ecdsa(const void *, int, const EccGfp &, const scc::crypto::Bignum &, scc::crypto::Bignum &, scc::crypto::Bignum &, scc::crypto::Bignum &)
Sign a message using ECDSA.
static void dh_shared_secret(const scc::crypto::Bignum &, const EccGfpPoint &, scc::crypto::Bignum &)
Calculate a shared secret using the Diffie-Hellman scheme.
static bool verify_ecdsa(const void *, int, const EccGfpPoint &, const scc::crypto::Bignum &, const scc::crypto::Bignum &)
Verify a message using the ECDSA.
void generate_key_pair(Bignum &priv_key, EccGfpPoint &pub_key)
Generate a private and public key pair for this curve.
Definition: ecc.h:117
int bit_width() const
Elliptic curve ordinal bit width.
static bool validate_key_pair(const Bignum &, const EccGfpPoint &)
Validate a key pair.
void generate_public_key(const Bignum &, EccGfpPoint &)
Generate a public key from the private key.
void reset(Type type)
Reset the curve to a new standard type.
void private_key(Bignum &)
Generate a private key.
static bool valid(const EccGfp &)
Verify the curve.
void public_key(const Bignum &, EccGfpPoint &)
Generate a public key corresponding to a private key.
Type
Standard field type.
Definition: ecc.h:86
@ std_p256sm2
standard curve secp256sm2
@ std_p224r1
standard curve secp224r1 (112 bit security level)
@ std_p192r1
standard curve secp192r1 (96 bit security level)
@ std_p256r1
standard curve secp256r1 (128 bit security level)
@ std_p521r1
standard curve secp521r1 (256 bit security level)
@ std_p384r1
standard curve secp384r1 (192 bit security level)