Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ecc.h « ECC « Inc « cryptographic « ble « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6edb39140357770017e8433fbc7297d83d27bce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
  ******************************************************************************
  * @file    ecc.h
  * @author  MCD Application Team
  * @brief   Provides Elliptic Curve Cryptography (ECC) primitives
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2015 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Image license SLA0044,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        http://www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CRL_ECC_H__
#define __CRL_ECC_H__

#ifdef __cplusplus
extern "C"
{
#endif

  /* Includes ------------------------------------------------------------------*/
#include "../Common_ecc_rsa/MATH/math.h"

  /** @addtogroup ECC
    * @{
    */

  /**
    * @brief  Structure continaing the BigNum_stt that describes the parameters of an Elliptic Curve and its generator
    */
  typedef struct
  {
    BigNum_stt *pmA;          /*!< Parameter a of the curve equation. */
    BigNum_stt *pmB;          /*!< Parameter b of the curve equation. */
    BigNum_stt *pmP;          /*!< Modulus p. */
    BigNum_stt *pmN;          /*!< Order of the curve n. */
    BigNum_stt *pmGx;         /*!< Coordinate pmX of curve's generator */
    BigNum_stt *pmGy;         /*!< Coordinate pmY of curve's generator*/
  }
  intEC_stt;


  /**
    * @brief  Structure that keeps the Elliptic Curve Parameter
    */
  typedef struct
  {
    const uint8_t *pmA;  /*!< pointer to paramter "a" */

    int32_t mAsize;      /*!< size of paramter "a" */

    const uint8_t *pmB;  /*!< pointer to paramter "b" */

    int32_t mBsize;      /*!< size of paramter "b" */

    const uint8_t *pmP;  /*!<pointer to paramter "p" */

    int32_t mPsize;      /*!<size of paramter "p" */

    const uint8_t *pmN;  /*!< pointer to paramter "n" */

    int32_t mNsize;      /*!< size of paramter "n" */

    const uint8_t *pmGx; /*!< pointer to x coordinate of generator point */

    int32_t mGxsize;     /*!< size of x coordinate of generator point */

    const uint8_t *pmGy; /*!< pointer to y coordinate of generator point */

    int32_t mGysize;     /*!< size of y coordinate of generator point */

    intEC_stt *pmInternalEC;  /*!< Pointer to internal structure for handling the parameters */
  }
  EC_stt;


  /**
    * @brief  Enumeration to specify the possible flags for an Elliptic Curve Point
    */
  typedef enum ECPntFlags_e
  {

    E_POINT_GENERAL = 0,    /*!< The point is not normalized (Coordinate Z != 1) */

    E_POINT_NORMALIZED = 1, /*!< The point is normalized (Coordinate Z == 1)*/

    E_POINT_INFINITY = 2,   /*!< The point is the O point */

    E_POINT_MONTY = 4       /*!< The point's coordinates are expressed in Montgomery domain */
  } ECPntFlags_et;

  /**
    * @brief  Object used to store an elliptic curve point.
    */
  typedef struct
  {

    BigNum_stt *pmX ;     /*!< pmX coordinate. */

    BigNum_stt *pmY ;     /*!< pmY coordinate. */

    BigNum_stt *pmZ ;     /*!< pmZ coordinate, used in projective representations. */

    ECPntFlags_et mFlag;  /*!< Point Flag, allowed values are: \n
                             * - flag=CRL_EPOINT_GENERAL for a point which may have pmZ different from 1
                             * - flag=CRL_EPOINT_NORMALIZED for a point which has pmZ equal to 1
                             * - flag=CRL_EPOINT_INFINITY to denote the infinity point
                            */
  }
  ECpoint_stt;

  /**
    * @brief   Enumeration for the coordinates of an elliptic curve point
    */
  typedef enum ECcoordinate_e
  {
    E_ECC_POINT_COORDINATE_X = 0,  /*!< Coordinate X */

    E_ECC_POINT_COORDINATE_Y = 1,   /*!< Coordinate Y */

    E_ECC_POINT_COORDINATE_Z = 2,   /*!< Coordinate Z */
  } ECcoordinate_et;


  /**
    * @brief   Object used to store an ECC private key
    */
  typedef struct
  {
    BigNum_stt *pmD;   /*!<  BigNum Representing the Private Key */
  }
  ECCprivKey_stt;

  /**
    * @brief   Object used to store an ECDSA signature
    */
  typedef struct
  {
    /** R */
    BigNum_stt *pmR ;  /*!< pointer to paramter R*/
    /** S */
    BigNum_stt *pmS ; /*!< pointer to paramter S*/
  }
  ECDSAsignature_stt;


  /**
    * @brief  Enumeration for the values inside the ECDSA signature
    */
  typedef enum ECDSAsignValues_e
  {
    E_ECDSA_SIGNATURE_R_VALUE = 0,  /*!<  Value R  */
    E_ECDSA_SIGNATURE_S_VALUE = 1,  /*!<  Value S */
  } ECDSAsignValues_et;

  /**
    * @brief  Structure used in ECDSA signature verification function
    */
  typedef struct
  {

    ECpoint_stt *pmPubKey;  /*!<  Pointer to the ECC Public Key used in the verification */

    EC_stt      *pmEC;      /*!<  Pointer to Elliptic Curve parameters */
  }
  ECDSAverifyCtx_stt;


  /**
    * @brief  Structure used in ECDSA signature generation function
    */
  typedef struct
  {

    ECCprivKey_stt *pmPrivKey;  /*!<  Pointer to the ECC Private Key used in the verification */

    EC_stt         *pmEC;       /*!<  Pointer to Elliptic Curve parameters */

    RNGstate_stt   *pmRNG;      /*!<  Pointer to an Initialized Random Engine Status */
  }
  ECDSAsignCtx_stt;

  /* Exported functions ------------------------------------------------------- */
  int32_t ECCinitEC(EC_stt *P_pECctx, membuf_stt *P_pMemBuf);
  int32_t ECCfreeEC(EC_stt *P_pECctx, membuf_stt *P_pMemBuf);
  int32_t ECCinitPoint(ECpoint_stt **P_ppECPnt, const EC_stt *P_pECctx, membuf_stt *P_pMemBuf);
  int32_t ECCfreePoint(ECpoint_stt **P_pECPnt, membuf_stt *P_pMemBuf);
  int32_t ECCsetPointCoordinate(ECpoint_stt *P_pECPnt,
                                ECcoordinate_et P_Coordinate,
                                const uint8_t *P_pCoordinateValue,
                                int32_t P_coordinateSize);
  int32_t ECCgetPointCoordinate(const ECpoint_stt *P_pECPnt,
                                ECcoordinate_et P_Coordinate,
                                uint8_t *P_pCoordinateValue,
                                int32_t *P_pCoordinateSize);
  int32_t ECCgetPointFlag(const ECpoint_stt *P_pECPnt);
    void ECCsetPointFlag(ECpoint_stt *P_pECPnt,
                         ECPntFlags_et P_newFlag);
  int32_t ECCsetPointGenerator(ECpoint_stt *P_pPoint, const EC_stt *P_pECctx);
  int32_t ECCcopyPoint(const ECpoint_stt *P_pOriginalPoint, ECpoint_stt *P_pCopyPoint);
  int32_t ECCinitPrivKey(ECCprivKey_stt **P_ppECCprivKey, const EC_stt *P_pECctx, membuf_stt *P_pMemBuf);
  int32_t ECCfreePrivKey(ECCprivKey_stt **P_pECCprivKey, membuf_stt *P_pMemBuf);
  int32_t ECCsetPrivKeyValue(ECCprivKey_stt *P_pECCprivKey,
                             const uint8_t *P_pPrivateKey,
                             int32_t P_privateKeySize);
  int32_t ECCgetPrivKeyValue(const ECCprivKey_stt *P_pECCprivKey,
                             uint8_t *P_pPrivateKey,
                             int32_t *P_pPrivateKeySize);
  int32_t ECCscalarMul(const ECpoint_stt *P_pECbasePnt,
                       const ECCprivKey_stt *P_pECCprivKey,
                       ECpoint_stt *P_pECresultPnt,
                       const EC_stt *P_pECctx,
                       membuf_stt *P_pMemBuf);
  int32_t ECDSAinitSign(ECDSAsignature_stt **P_ppSignature, const EC_stt *P_pECctx, membuf_stt *P_pMemBuf);
  int32_t ECDSAfreeSign(ECDSAsignature_stt **P_pSignature, membuf_stt *P_pMemBuf);
  int32_t ECDSAsetSignature(ECDSAsignature_stt *P_pSignature,
                            ECDSAsignValues_et P_RorS,
                            const uint8_t *P_pValue,
                            int32_t P_valueSize);
  int32_t ECDSAgetSignature(const ECDSAsignature_stt *P_pSignature,
                            ECDSAsignValues_et P_RorS,
                            uint8_t *P_pValue,
                            int32_t *P_pValueSize);
  int32_t ECDSAverify(const uint8_t      *P_pDigest,
                      int32_t             P_digestSize,
                      const ECDSAsignature_stt   *P_pSignature,
                      const ECDSAverifyCtx_stt *P_pVerifyCtx,
                      membuf_stt *P_pMemBuf);
  int32_t ECCvalidatePubKey(const ECpoint_stt *pECCpubKey, const EC_stt *P_pECctx, membuf_stt *P_pMemBuf);
  int32_t ECCkeyGen(ECCprivKey_stt *P_pPrivKey,
                    ECpoint_stt    *P_pPubKey,
                    RNGstate_stt * P_pRandomState,
                    const EC_stt    *P_pECctx,
                    membuf_stt *P_pMemBuf);
  int32_t ECDSAsign(const uint8_t         *P_pDigest,
                    int32_t                P_digestSize,
                    const ECDSAsignature_stt *P_pSignature,
                    const ECDSAsignCtx_stt *P_pSignCtx,
                    membuf_stt *P_pMemBuf);

  /**
    * @}
    */

#ifdef __cplusplus
}
#endif

#endif /* __ECC_H */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/