Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cECPGroup.h
Go to the documentation of this file.
1 //
5 //
6 #ifndef _INC_cECPGroup_H
7 #define _INC_cECPGroup_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 #include "cECPGroupDef.h"
12 #include "../File/cASNReader.h"
13 
14 namespace GrayLib
15 {
17 
18  struct cECPStats
19  {
20  // stats for testing
21  ULONG m_add_count;
22  ULONG m_dbl_count;
23  ULONG m_mul_count;
24 
25  void SetCounts0() noexcept
26  {
27  m_add_count = 0;
28  m_dbl_count = 0;
29  m_mul_count = 0;
30  }
31  };
32 
34  {
52 
53  typedef cECPGroupParams SUPER_t;
54 
55  public:
56 
57  // Maximum "window" size used for point multiplication.
58  // Default: 6.
59  // Minimum value: 2. Maximum value: 7.
60  //
61  // Result is an array of at most ( 1 << ( k_ECP_WINDOW_SIZE - 1 ) )
62  // points used for point multiplication. This value is directly tied to EC
63  // peak memory usage, so decreasing it by one should roughly cut memory usage
64  // by two (if large curves are in use).
65  //
66  // Reduction in size may reduce speed, but larger curves are impacted first.
67  // Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
68  // w-size: 6 5 4 3 2
69  // 521 145 141 135 120 97
70  // 384 214 209 198 177 146
71  // 256 320 320 303 262 226
72  //
73  // 224 475 475 453 398 342
74  // 192 640 640 633 587 476
75 
76  static const int k_ECP_WINDOW_SIZE = 6;
77  static cECPStats sm_ECPStats; // for testing
78 
80 
82 
83  protected:
84 
85  HRESULT DoModP(OUT cBigInteger& N) const;
86 
87  HRESULT DoMulModP(OUT cBigInteger& X, const cBigInteger& Y, const cBigInteger& Z) const
88  {
89  X.InitMultiply(Y, Z);
90  return this->DoModP(X);
91  }
92 
93  HRESULT DoAddMixed(OUT cECPPoint& R, const cECPPoint& P, const cECPPoint& Q) const;
94 
95  HRESULT Mxz_normalize(cECPPoint& P) const;
96  HRESULT Mxz_randomize(cECPPoint& P, IRandomNoise* pRandom) const;
97  HRESULT Mxz_add_double(cECPPoint& R, cECPPoint& S, const cECPPoint& P, const cECPPoint& Q, const cBigInteger& d) const;
98  HRESULT Mxz_mul(cECPPoint& R, const cBigInteger& m, const cECPPoint& P, IRandomNoise* pRandom) const;
99 
100  HRESULT Jac_normalize(cECPPoint& pt) const;
101  HRESULT Jac_normalize_many(cECPPoint* pT[], ITERATE_t t_len) const;
102  HRESULT Jac_double(cECPPoint& R, const cECPPoint& P) const;
103  HRESULT Jac_randomize(cECPPoint& pt, IRandomNoise* pRandom) const;
104  HRESULT Jac_safe_invert(cECPPoint& Q, bool inv) const;
105 
106  static void Comb_fixed(BYTE x[], size_t d, BYTE w, const cBigInteger& m);
107 
108  HRESULT Comb_precompute(cECPPoint T[], const cECPPoint& P, BYTE w, size_t d) const;
109  HRESULT Comb_mul_core(cECPPoint& R, const cECPPoint T[], BYTE t_len, const BYTE x[], size_t d, IRandomNoise* pRandom) const;
110  HRESULT Comb_mul(OUT cECPPoint& R, const cBigUnsigned& m, const cECPPoint& P, IRandomNoise* pRandom) const;
111  HRESULT Comb_select(cECPPoint& R, const cECPPoint T[], BYTE t_len, BYTE i) const;
112 
113  HRESULT DoAdd(OUT cECPPoint& R, const cECPPoint& P, const cECPPoint& Q) const;
114  HRESULT DoSub(cECPPoint& R, const cECPPoint& P, const cECPPoint& Q) const; // NOT USED
115 
116  HRESULT IsValidPublicKeyWS(const cECPPoint& pt) const;
117  HRESULT IsValidPublicKeyMX(const cECPPoint& pt) const;
118 
119  public:
120  cECPGroup() noexcept
121  : m_pECPGroupDef(nullptr)
122  {
123  }
125  {
126  SetEmptyECPGroup();
127  }
128 
129  void SetEmptyECPGroup();
130 
131  ECPGroup_TYPE get_ECPGroupId() const noexcept
132  {
133  if (m_pECPGroupDef == nullptr)
134  return ECPGroup_NONE;
135  return m_pECPGroupDef->m_eECPGroupId;
136  }
137  const cECPGroupDef* get_ECPGroupDef() const noexcept
138  {
139  return m_pECPGroupDef;
140  }
141 
142  HRESULT put_ECPGroupDef(const cECPGroupDef* pECPGroupDef);
143  HRESULT put_ECPGroupId(ECPGroup_TYPE eECPGroupId);
144  HRESULT SetECParameters(const cASNBuf& params);
145 
146  HRESULT DoMult(OUT cECPPoint& R, const cBigUnsigned& m, const cECPPoint& P, IRandomNoise* pRandom) const;
147 
148  HRESULT SetCopyECP(const cECPGroup& rSrc);
149 
150  HRESULT GenerateKeys(cBigUnsigned& d, cECPPoint& Q, IRandomNoise* pRandom) const;
151 
152  HRESULT IsValidPublicKey(const cECPPoint& pt) const;
153 
154  HRESULT MakeSignatureECDSA(OUT cBigInteger& r, OUT cBigInteger& s, const cBigInteger& d, const BYTE* buf, size_t nSizeBuffer, IRandomNoise* pRandom) const;
155  HRESULT VerifySignatureECDSA(const BYTE* buf, size_t nSizeBuffer, const cECPPoint& Q, const cBigInteger& r, const cBigInteger& s) const;
156 
158  };
159 }
160 
161 #endif
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define P(a, b, c, d, k, s, t)
#define R(t)
Definition: cASN.h:78
Definition: cBigInteger.h:18
Definition: cBigUnsigned.h:22
Definition: cECPGroupDef.h:21
ECPGroup_TYPE m_eECPGroupId
cECPGroupMgr TLS predefined NamedCurve identifier from TLS_EXT_supported_groups
Definition: cECPGroupDef.h:28
Definition: cECPGroupParams.h:105
Definition: cECPGroup.h:34
HRESULT DoMulModP(OUT cBigInteger &X, const cBigInteger &Y, const cBigInteger &Z) const
Definition: cECPGroup.h:87
UNITTEST_FRIEND(cECPGroup)
ECPGroup_TYPE get_ECPGroupId() const noexcept
Definition: cECPGroup.h:131
cArrayStruct< cECPPoint > m_aT
pre-computed points for Comb_mul()
Definition: cECPGroup.h:81
const cECPGroupDef * get_ECPGroupDef() const noexcept
Definition: cECPGroup.h:137
cECPGroup() noexcept
Definition: cECPGroup.h:120
static cECPStats sm_ECPStats
Definition: cECPGroup.h:77
~cECPGroup()
Definition: cECPGroup.h:124
const cECPGroupDef * m_pECPGroupDef
internal group identifier. predefined curve.
Definition: cECPGroup.h:79
Definition: cECPGroupParams.h:41
Definition: cArray.h:932
Definition: cMesh.h:22
ECPGroup_TYPE
Definition: cECPGroupParams.h:28
UNITTEST2_PREDEF(cQuadtree)
int ITERATE_t
like size_t but signed
Definition: Index.h:28
Definition: cECPGroup.h:19
ULONG m_dbl_count
Definition: cECPGroup.h:22
void SetCounts0() noexcept
Definition: cECPGroup.h:25
ULONG m_mul_count
Definition: cECPGroup.h:23
ULONG m_add_count
Definition: cECPGroup.h:21
Definition: cRandom.h:19