ec.h
Go to the documentation of this file.
1 /**
2  * @file ec.h
3  * @brief ECC (Elliptic Curve Cryptography)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 #ifndef _EC_H
32 #define _EC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //secp112r1 elliptic curve support
38 #ifndef SECP112R1_SUPPORT
39  #define SECP112R1_SUPPORT DISABLED
40 #elif (SECP112R1_SUPPORT != ENABLED && SECP112R1_SUPPORT != DISABLED)
41  #error SECP112R1_SUPPORT parameter is not valid
42 #endif
43 
44 //secp112r2 elliptic curve support
45 #ifndef SECP112R2_SUPPORT
46  #define SECP112R2_SUPPORT DISABLED
47 #elif (SECP112R2_SUPPORT != ENABLED && SECP112R2_SUPPORT != DISABLED)
48  #error SECP112R2_SUPPORT parameter is not valid
49 #endif
50 
51 //secp128r1 elliptic curve support
52 #ifndef SECP128R1_SUPPORT
53  #define SECP128R1_SUPPORT DISABLED
54 #elif (SECP128R1_SUPPORT != ENABLED && SECP128R1_SUPPORT != DISABLED)
55  #error SECP128R1_SUPPORT parameter is not valid
56 #endif
57 
58 //secp128r2 elliptic curve support
59 #ifndef SECP128R2_SUPPORT
60  #define SECP128R2_SUPPORT DISABLED
61 #elif (SECP128R2_SUPPORT != ENABLED && SECP128R2_SUPPORT != DISABLED)
62  #error SECP128R2_SUPPORT parameter is not valid
63 #endif
64 
65 //secp160k1 elliptic curve support
66 #ifndef SECP160K1_SUPPORT
67  #define SECP160K1_SUPPORT DISABLED
68 #elif (SECP160K1_SUPPORT != ENABLED && SECP160K1_SUPPORT != DISABLED)
69  #error SECP160K1_SUPPORT parameter is not valid
70 #endif
71 
72 //secp160r1 elliptic curve support
73 #ifndef SECP160R1_SUPPORT
74  #define SECP160R1_SUPPORT DISABLED
75 #elif (SECP160R1_SUPPORT != ENABLED && SECP160R1_SUPPORT != DISABLED)
76  #error SECP160R1_SUPPORT parameter is not valid
77 #endif
78 
79 //secp160r2 elliptic curve support
80 #ifndef SECP160R2_SUPPORT
81  #define SECP160R2_SUPPORT DISABLED
82 #elif (SECP160R2_SUPPORT != ENABLED && SECP160R2_SUPPORT != DISABLED)
83  #error SECP160R2_SUPPORT parameter is not valid
84 #endif
85 
86 //secp192k1 elliptic curve support
87 #ifndef SECP192K1_SUPPORT
88  #define SECP192K1_SUPPORT DISABLED
89 #elif (SECP192K1_SUPPORT != ENABLED && SECP192K1_SUPPORT != DISABLED)
90  #error SECP192K1_SUPPORT parameter is not valid
91 #endif
92 
93 //secp192r1 elliptic curve support (NIST P-192)
94 #ifndef SECP192R1_SUPPORT
95  #define SECP192R1_SUPPORT DISABLED
96 #elif (SECP192R1_SUPPORT != ENABLED && SECP192R1_SUPPORT != DISABLED)
97  #error SECP192R1_SUPPORT parameter is not valid
98 #endif
99 
100 //secp224k1 elliptic curve support
101 #ifndef SECP224K1_SUPPORT
102  #define SECP224K1_SUPPORT DISABLED
103 #elif (SECP224K1_SUPPORT != ENABLED && SECP224K1_SUPPORT != DISABLED)
104  #error SECP224K1_SUPPORT parameter is not valid
105 #endif
106 
107 //secp224r1 elliptic curve support (NIST P-224)
108 #ifndef SECP224R1_SUPPORT
109  #define SECP224R1_SUPPORT ENABLED
110 #elif (SECP224R1_SUPPORT != ENABLED && SECP224R1_SUPPORT != DISABLED)
111  #error SECP224R1_SUPPORT parameter is not valid
112 #endif
113 
114 //secp256k1 elliptic curve support
115 #ifndef SECP256K1_SUPPORT
116  #define SECP256K1_SUPPORT DISABLED
117 #elif (SECP256K1_SUPPORT != ENABLED && SECP256K1_SUPPORT != DISABLED)
118  #error SECP256K1_SUPPORT parameter is not valid
119 #endif
120 
121 //secp256r1 elliptic curve support (NIST P-256)
122 #ifndef SECP256R1_SUPPORT
123  #define SECP256R1_SUPPORT ENABLED
124 #elif (SECP256R1_SUPPORT != ENABLED && SECP256R1_SUPPORT != DISABLED)
125  #error SECP256R1_SUPPORT parameter is not valid
126 #endif
127 
128 //secp384r1 elliptic curve support (NIST P-384)
129 #ifndef SECP384R1_SUPPORT
130  #define SECP384R1_SUPPORT ENABLED
131 #elif (SECP384R1_SUPPORT != ENABLED && SECP384R1_SUPPORT != DISABLED)
132  #error SECP384R1_SUPPORT parameter is not valid
133 #endif
134 
135 //secp521r1 elliptic curve support (NIST P-521)
136 #ifndef SECP521R1_SUPPORT
137  #define SECP521R1_SUPPORT ENABLED
138 #elif (SECP521R1_SUPPORT != ENABLED && SECP521R1_SUPPORT != DISABLED)
139  #error SECP521R1_SUPPORT parameter is not valid
140 #endif
141 
142 //brainpoolP160r1 elliptic curve support
143 #ifndef BRAINPOOLP160R1_SUPPORT
144  #define BRAINPOOLP160R1_SUPPORT DISABLED
145 #elif (BRAINPOOLP160R1_SUPPORT != ENABLED && BRAINPOOLP160R1_SUPPORT != DISABLED)
146  #error BRAINPOOLP160R1_SUPPORT parameter is not valid
147 #endif
148 
149 //brainpoolP160t1 elliptic curve support
150 #ifndef BRAINPOOLP160T1_SUPPORT
151  #define BRAINPOOLP160T1_SUPPORT DISABLED
152 #elif (BRAINPOOLP160T1_SUPPORT != ENABLED && BRAINPOOLP160T1_SUPPORT != DISABLED)
153  #error BRAINPOOLP160T1_SUPPORT parameter is not valid
154 #endif
155 
156 //brainpoolP192r1 elliptic curve support
157 #ifndef BRAINPOOLP192R1_SUPPORT
158  #define BRAINPOOLP192R1_SUPPORT DISABLED
159 #elif (BRAINPOOLP192R1_SUPPORT != ENABLED && BRAINPOOLP192R1_SUPPORT != DISABLED)
160  #error BRAINPOOLP192R1_SUPPORT parameter is not valid
161 #endif
162 
163 //brainpoolP192t1 elliptic curve support
164 #ifndef BRAINPOOLP192T1_SUPPORT
165  #define BRAINPOOLP192T1_SUPPORT DISABLED
166 #elif (BRAINPOOLP192T1_SUPPORT != ENABLED && BRAINPOOLP192T1_SUPPORT != DISABLED)
167  #error BRAINPOOLP192T1_SUPPORT parameter is not valid
168 #endif
169 
170 //brainpoolP224r1 elliptic curve support
171 #ifndef BRAINPOOLP224R1_SUPPORT
172  #define BRAINPOOLP224R1_SUPPORT DISABLED
173 #elif (BRAINPOOLP224R1_SUPPORT != ENABLED && BRAINPOOLP224R1_SUPPORT != DISABLED)
174  #error BRAINPOOLP224R1_SUPPORT parameter is not valid
175 #endif
176 
177 //brainpoolP224t1 elliptic curve support
178 #ifndef BRAINPOOLP224T1_SUPPORT
179  #define BRAINPOOLP224T1_SUPPORT DISABLED
180 #elif (BRAINPOOLP224T1_SUPPORT != ENABLED && BRAINPOOLP224T1_SUPPORT != DISABLED)
181  #error BRAINPOOLP224T1_SUPPORT parameter is not valid
182 #endif
183 
184 //brainpoolP256r1 elliptic curve support
185 #ifndef BRAINPOOLP256R1_SUPPORT
186  #define BRAINPOOLP256R1_SUPPORT DISABLED
187 #elif (BRAINPOOLP256R1_SUPPORT != ENABLED && BRAINPOOLP256R1_SUPPORT != DISABLED)
188  #error BRAINPOOLP256R1_SUPPORT parameter is not valid
189 #endif
190 
191 //brainpoolP256t1 elliptic curve support
192 #ifndef BRAINPOOLP256T1_SUPPORT
193  #define BRAINPOOLP256T1_SUPPORT DISABLED
194 #elif (BRAINPOOLP256T1_SUPPORT != ENABLED && BRAINPOOLP256T1_SUPPORT != DISABLED)
195  #error BRAINPOOLP256T1_SUPPORT parameter is not valid
196 #endif
197 
198 //brainpoolP320r1 elliptic curve support
199 #ifndef BRAINPOOLP320R1_SUPPORT
200  #define BRAINPOOLP320R1_SUPPORT DISABLED
201 #elif (BRAINPOOLP320R1_SUPPORT != ENABLED && BRAINPOOLP320R1_SUPPORT != DISABLED)
202  #error BRAINPOOLP320R1_SUPPORT parameter is not valid
203 #endif
204 
205 //brainpoolP320t1 elliptic curve support
206 #ifndef BRAINPOOLP320T1_SUPPORT
207  #define BRAINPOOLP320T1_SUPPORT DISABLED
208 #elif (BRAINPOOLP320T1_SUPPORT != ENABLED && BRAINPOOLP320T1_SUPPORT != DISABLED)
209  #error BRAINPOOLP320T1_SUPPORT parameter is not valid
210 #endif
211 
212 //brainpoolP384r1 elliptic curve support
213 #ifndef BRAINPOOLP384R1_SUPPORT
214  #define BRAINPOOLP384R1_SUPPORT DISABLED
215 #elif (BRAINPOOLP384R1_SUPPORT != ENABLED && BRAINPOOLP384R1_SUPPORT != DISABLED)
216  #error BRAINPOOLP384R1_SUPPORT parameter is not valid
217 #endif
218 
219 //brainpoolP384t1 elliptic curve support
220 #ifndef BRAINPOOLP384T1_SUPPORT
221  #define BRAINPOOLP384T1_SUPPORT DISABLED
222 #elif (BRAINPOOLP384T1_SUPPORT != ENABLED && BRAINPOOLP384T1_SUPPORT != DISABLED)
223  #error BRAINPOOLP384T1_SUPPORT parameter is not valid
224 #endif
225 
226 //brainpoolP512r1 elliptic curve support
227 #ifndef BRAINPOOLP512R1_SUPPORT
228  #define BRAINPOOLP512R1_SUPPORT DISABLED
229 #elif (BRAINPOOLP512R1_SUPPORT != ENABLED && BRAINPOOLP512R1_SUPPORT != DISABLED)
230  #error BRAINPOOLP512R1_SUPPORT parameter is not valid
231 #endif
232 
233 //brainpoolP512t1 elliptic curve support
234 #ifndef BRAINPOOLP512T1_SUPPORT
235  #define BRAINPOOLP512T1_SUPPORT DISABLED
236 #elif (BRAINPOOLP512T1_SUPPORT != ENABLED && BRAINPOOLP512T1_SUPPORT != DISABLED)
237  #error BRAINPOOLP512T1_SUPPORT parameter is not valid
238 #endif
239 
240 //FRP256v1 elliptic curve support
241 #ifndef FRP256V1_SUPPORT
242  #define FRP256V1_SUPPORT DISABLED
243 #elif (FRP256V1_SUPPORT != ENABLED && FRP256V1_SUPPORT != DISABLED)
244  #error FRP256V1_SUPPORT parameter is not valid
245 #endif
246 
247 //SM2 elliptic curve support
248 #ifndef SM2_SUPPORT
249  #define SM2_SUPPORT DISABLED
250 #elif (SM2_SUPPORT != ENABLED && SM2_SUPPORT != DISABLED)
251  #error SM2_SUPPORT parameter is not valid
252 #endif
253 
254 //Curve25519 elliptic curve support
255 #ifndef X25519_SUPPORT
256  #define X25519_SUPPORT DISABLED
257 #elif (X25519_SUPPORT != ENABLED && X25519_SUPPORT != DISABLED)
258  #error X25519_SUPPORT parameter is not valid
259 #endif
260 
261 //Curve448 elliptic curve support
262 #ifndef X448_SUPPORT
263  #define X448_SUPPORT DISABLED
264 #elif (X448_SUPPORT != ENABLED && X448_SUPPORT != DISABLED)
265  #error X448_SUPPORT parameter is not valid
266 #endif
267 
268 //Ed25519 elliptic curve support
269 #ifndef ED25519_SUPPORT
270  #define ED25519_SUPPORT DISABLED
271 #elif (ED25519_SUPPORT != ENABLED && ED25519_SUPPORT != DISABLED)
272  #error ED25519_SUPPORT parameter is not valid
273 #endif
274 
275 //Ed448 elliptic curve support
276 #ifndef ED448_SUPPORT
277  #define ED448_SUPPORT DISABLED
278 #elif (ED448_SUPPORT != ENABLED && ED448_SUPPORT != DISABLED)
279  #error ED448_SUPPORT parameter is not valid
280 #endif
281 
282 //Maximum size of prime modulus p
283 #if (SECP521R1_SUPPORT == ENABLED)
284  #define EC_MAX_MODULUS_SIZE 17
285 #elif (BRAINPOOLP512R1_SUPPORT == ENABLED || BRAINPOOLP512T1_SUPPORT == ENABLED)
286  #define EC_MAX_MODULUS_SIZE 16
287 #elif (X448_SUPPORT == ENABLED)
288  #define EC_MAX_MODULUS_SIZE 14
289 #elif (SECP384R1_SUPPORT == ENABLED || BRAINPOOLP384R1_SUPPORT == ENABLED || \
290  BRAINPOOLP384T1_SUPPORT == ENABLED)
291  #define EC_MAX_MODULUS_SIZE 12
292 #elif (BRAINPOOLP320R1_SUPPORT == ENABLED)
293  #define EC_MAX_MODULUS_SIZE 10
294 #elif (SECP256K1_SUPPORT == ENABLED || SECP256R1_SUPPORT == ENABLED || \
295  BRAINPOOLP256R1_SUPPORT == ENABLED || BRAINPOOLP256T1_SUPPORT == ENABLED || \
296  FRP256V1_SUPPORT == ENABLED || SM2_SUPPORT == ENABLED || \
297  X25519_SUPPORT == ENABLED)
298  #define EC_MAX_MODULUS_SIZE 8
299 #elif (SECP224K1_SUPPORT == ENABLED || SECP224R1_SUPPORT == ENABLED || \
300  BRAINPOOLP224R1_SUPPORT == ENABLED || BRAINPOOLP224T1_SUPPORT == ENABLED)
301  #define EC_MAX_MODULUS_SIZE 7
302 #elif (SECP192K1_SUPPORT == ENABLED || SECP192R1_SUPPORT == ENABLED || \
303  BRAINPOOLP192R1_SUPPORT == ENABLED || BRAINPOOLP192T1_SUPPORT == ENABLED)
304  #define EC_MAX_MODULUS_SIZE 6
305 #elif (SECP160K1_SUPPORT == ENABLED || SECP160R1_SUPPORT == ENABLED || \
306  SECP160R2_SUPPORT == ENABLED || BRAINPOOLP160R1_SUPPORT == ENABLED || \
307  BRAINPOOLP160T1_SUPPORT == ENABLED)
308  #define EC_MAX_MODULUS_SIZE 5
309 #else
310  #define EC_MAX_MODULUS_SIZE 4
311 #endif
312 
313 //Maximum size of order q
314 #if (SECP521R1_SUPPORT == ENABLED)
315  #define EC_MAX_ORDER_SIZE 17
316 #elif (BRAINPOOLP512R1_SUPPORT == ENABLED || BRAINPOOLP512T1_SUPPORT == ENABLED)
317  #define EC_MAX_ORDER_SIZE 16
318 #elif (X448_SUPPORT == ENABLED)
319  #define EC_MAX_ORDER_SIZE 14
320 #elif (SECP384R1_SUPPORT == ENABLED || BRAINPOOLP384R1_SUPPORT == ENABLED || \
321  BRAINPOOLP384T1_SUPPORT == ENABLED)
322  #define EC_MAX_ORDER_SIZE 12
323 #elif (BRAINPOOLP320R1_SUPPORT == ENABLED || BRAINPOOLP320T1_SUPPORT == ENABLED)
324  #define EC_MAX_ORDER_SIZE 10
325 #elif (SECP224K1_SUPPORT == ENABLED || SECP256K1_SUPPORT == ENABLED || \
326  SECP256R1_SUPPORT == ENABLED || BRAINPOOLP256R1_SUPPORT == ENABLED || \
327  BRAINPOOLP256T1_SUPPORT == ENABLED || FRP256V1_SUPPORT == ENABLED || \
328  SM2_SUPPORT == ENABLED || X25519_SUPPORT == ENABLED)
329  #define EC_MAX_ORDER_SIZE 8
330 #elif (SECP224R1_SUPPORT == ENABLED || BRAINPOOLP224R1_SUPPORT == ENABLED || \
331  BRAINPOOLP224T1_SUPPORT == ENABLED)
332  #define EC_MAX_ORDER_SIZE 7
333 #elif (SECP160K1_SUPPORT == ENABLED || SECP160R1_SUPPORT == ENABLED || \
334  SECP160R2_SUPPORT == ENABLED || SECP192K1_SUPPORT == ENABLED || \
335  SECP192R1_SUPPORT == ENABLED || BRAINPOOLP192R1_SUPPORT == ENABLED || \
336  BRAINPOOLP192T1_SUPPORT == ENABLED)
337  #define EC_MAX_ORDER_SIZE 6
338 #elif (BRAINPOOLP160R1_SUPPORT == ENABLED || BRAINPOOLP160T1_SUPPORT == ENABLED)
339  #define EC_MAX_ORDER_SIZE 5
340 #else
341  #define EC_MAX_ORDER_SIZE 4
342 #endif
343 
344 //Forward declaration of EcCurve structure
345 struct _EcCurve;
346 #define EcCurve struct _EcCurve
347 
348 //C++ guard
349 #ifdef __cplusplus
350 extern "C" {
351 #endif
352 
353 
354 /**
355  * @brief Elliptic curve type
356  **/
357 
358 typedef enum
359 {
366 
367 
368 /**
369  * @brief EC point format
370  **/
371 
372 typedef enum
373 {
378 
379 
380 /**
381  * @brief EC public key format
382  **/
383 
384 typedef enum
385 {
391 
392 
393 /**
394  * @brief EC point (affine coordinates)
395  **/
396 
397 typedef struct
398 {
399  uint32_t x[EC_MAX_MODULUS_SIZE]; ///<x-coordinate
400  uint32_t y[EC_MAX_MODULUS_SIZE]; ///<y-coordinate
401 } EcPoint;
402 
403 
404 /**
405  * @brief EC point (projective coordinates)
406  **/
407 
408 typedef struct
409 {
410  uint32_t x[EC_MAX_MODULUS_SIZE]; ///<x-coordinate
411  uint32_t y[EC_MAX_MODULUS_SIZE]; ///<y-coordinate
412  uint32_t z[EC_MAX_MODULUS_SIZE]; ///<z-coordinate
413 } EcPoint3;
414 
415 
416 /**
417  * @brief EC public key
418  **/
419 
420 typedef struct
421 {
422  const EcCurve *curve; ///<Elliptic curve parameters
423  EcPoint q; ///<Public key
424 } EcPublicKey;
425 
426 
427 /**
428  * @brief EC private key
429  **/
430 
431 typedef struct
432 {
433  const EcCurve *curve; ///<Elliptic curve parameters
434  uint32_t d[EC_MAX_ORDER_SIZE]; ///<Private key
435  int_t slot; ///<Private key slot
436  EcPublicKey q; ///<Public key
437 } EcPrivateKey;
438 
439 
440 /**
441  * @brief Working state (point addition/subtraction/doubling)
442  **/
443 
444 typedef struct
445 {
446  const EcCurve *curve;
447  uint32_t t0[EC_MAX_MODULUS_SIZE];
450  uint32_t t3[EC_MAX_MODULUS_SIZE];
451  uint32_t t4[EC_MAX_MODULUS_SIZE];
452  uint32_t t5[EC_MAX_MODULUS_SIZE];
453  uint32_t t6[EC_MAX_MODULUS_SIZE];
454  uint32_t t7[EC_MAX_MODULUS_SIZE];
455 } EcState;
456 
457 
458 /**
459  * @brief Working state (fast scalar multiplication)
460  **/
461 
462 typedef struct
463 {
464  uint32_t k[EC_MAX_ORDER_SIZE + 1];
465  uint32_t h[EC_MAX_ORDER_SIZE + 1];
468 
469 
470 /**
471  * @brief Working state (regular scalar multiplication)
472  **/
473 
474 typedef struct
475 {
476  uint32_t init;
483 
484 
485 /**
486  * @brief Working state (twin multiplication)
487  **/
488 
489 typedef struct
490 {
495 
496 
497 /**
498  * @brief Modular reduction
499  **/
500 
501 typedef void (*EcModAlgo)(const EcCurve *curve, uint32_t *r,
502  const uint32_t *a);
503 
504 
505 /**
506  * @brief Modular inverse
507  **/
508 
509 typedef void (*EcInvModAlgo)(const EcCurve *curve, uint32_t *r,
510  const uint32_t *a);
511 
512 
513 /**
514  * @brief Elliptic curve parameters
515  **/
516 
517 struct _EcCurve
518 {
519  const char_t *name; ///<Curve name
520  const uint8_t *oid; ///<Object identifier
521  size_t oidSize; ///<OID size
522  EcCurveType type; ///<Curve type
523  uint_t fieldSize; ///<Field size, in bits
524  uint_t orderSize; ///<Order size, in bits
525  const uint32_t p[EC_MAX_MODULUS_SIZE + 1]; ///<Prime modulus p
526  const uint32_t pmu[EC_MAX_MODULUS_SIZE + 1]; ///<Pre-computed value mu
527  const uint32_t a[EC_MAX_MODULUS_SIZE]; ///<Curve parameter a
528  const uint32_t b[EC_MAX_MODULUS_SIZE]; ///<Curve parameter b
529  EcPoint3 g; ///<Base point G
530  const uint32_t q[EC_MAX_ORDER_SIZE + 1]; ///<Order of the base point G
531  const uint32_t qmu[EC_MAX_ORDER_SIZE + 1]; ///<Pre-computed value mu
532  uint32_t h; ///<Cofactor h
533  EcModAlgo fieldMod; ///<Field modular reduction
534  EcInvModAlgo fieldInv; ///<Field modular inversion
535  EcModAlgo scalarMod; ///<Scalar modular reduction
536  EcInvModAlgo scalarInv; ///<Scalar modular inversion
537 };
538 
539 
540 //EC related constants
541 extern const uint8_t EC_PUBLIC_KEY_OID[7];
542 
543 //EC related functions
544 void ecInitPublicKey(EcPublicKey *key);
545 void ecFreePublicKey(EcPublicKey *key);
546 
547 void ecInitPrivateKey(EcPrivateKey *key);
548 void ecFreePrivateKey(EcPrivateKey *key);
549 
550 error_t ecGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext,
551  const EcCurve *curve, EcPrivateKey *privateKey, EcPublicKey *publicKey);
552 
553 error_t ecGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext,
554  const EcCurve *curve, EcPrivateKey *privateKey);
555 
556 error_t ecGeneratePublicKey(const EcPrivateKey *privateKey,
557  EcPublicKey *publicKey);
558 
559 error_t ecImportPublicKey(EcPublicKey *key, const EcCurve *curve,
560  const uint8_t *data, size_t length, EcPublicKeyFormat format);
561 
562 error_t ecExportPublicKey(const EcPublicKey *key, uint8_t *data,
563  size_t *length, EcPublicKeyFormat format);
564 
566  const uint8_t *data, size_t length);
567 
568 error_t ecExportPrivateKey(const EcPrivateKey *key, uint8_t *data,
569  size_t *length);
570 
571 error_t ecImportPoint(const EcCurve *curve, EcPoint *r, const uint8_t *data,
572  size_t length);
573 
574 error_t ecExportPoint(const EcCurve *curve, const EcPoint *a, uint8_t *data,
575  size_t *length);
576 
577 void ecProjectify(const EcCurve *curve, EcPoint3 *r, const EcPoint *s);
578 error_t ecAffinify(const EcCurve *curve, EcPoint3 *r, const EcPoint3 *s);
579 bool_t ecIsPointAffine(const EcCurve *curve, const EcPoint *s);
580 
581 void ecDouble(EcState *state, EcPoint3 *r, const EcPoint3 *s);
582 
583 void ecAdd(EcState *state, EcPoint3 *r, const EcPoint3 *s,
584  const EcPoint3 *t);
585 
586 void ecFullAdd(EcState *state, EcPoint3 *r, const EcPoint3 *s,
587  const EcPoint3 *t);
588 
589 void ecFullSub(EcState *state, EcPoint3 *r, const EcPoint3 *s,
590  const EcPoint3 *t);
591 
592 error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d,
593  const EcPoint3 *s);
594 
595 error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d,
596  const EcPoint3 *s);
597 
598 error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0,
599  const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t);
600 
601 //C++ guard
602 #ifdef __cplusplus
603 }
604 #endif
605 
606 #endif
error_t ecGeneratePublicKey(const EcPrivateKey *privateKey, EcPublicKey *publicKey)
Derive the public key from an EC private key.
Definition: ec.c:200
Elliptic curve parameters.
Definition: ec.h:518
const char_t * name
Curve name.
Definition: ec.h:519
@ EC_CURVE_TYPE_WEIERSTRASS_A3
Definition: ec.h:362
@ EC_CURVE_TYPE_MONTGOMERY
Definition: ec.h:363
@ EC_CURVE_TYPE_EDWARDS
Definition: ec.h:364
int bool_t
Definition: compiler_port.h:61
@ EC_PUBLIC_KEY_FORMAT_RAW_Y
Definition: ec.h:389
@ EC_POINT_FORMAT_UNCOMPRESSED
Definition: ec.h:376
void ecFullSub(EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
Point subtraction.
Definition: ec.c:1151
error_t ecGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext, const EcCurve *curve, EcPrivateKey *privateKey, EcPublicKey *publicKey)
EC key pair generation.
Definition: ec.c:117
uint8_t a
Definition: ndp.h:411
EcInvModAlgo fieldInv
Field modular inversion.
Definition: ec.h:534
signed int int_t
Definition: compiler_port.h:56
#define PrngAlgo
Definition: crypto.h:973
EcPoint3 spt
Definition: ec.h:491
uint8_t x
Definition: lldp_ext_med.h:211
const EcCurve * curve
Elliptic curve parameters.
Definition: ec.h:433
uint8_t t
Definition: lldp_ext_med.h:212
int_t slot
Private key slot.
Definition: ec.h:435
Working state (fast scalar multiplication)
Definition: ec.h:463
uint8_t data[]
Definition: ethernet.h:222
#define EC_MAX_ORDER_SIZE
Definition: ec.h:315
EcPoint3 p0
Definition: ec.h:477
const uint32_t b[EC_MAX_MODULUS_SIZE]
Curve parameter b.
Definition: ec.h:528
@ EC_PUBLIC_KEY_FORMAT_X963
Definition: ec.h:386
@ EC_POINT_FORMAT_COMPRESSED_ODD
Definition: ec.h:375
EcPoint3 smt
Definition: ec.h:492
Working state (regular scalar multiplication)
Definition: ec.h:475
error_t ecImportPrivateKey(EcPrivateKey *key, const EcCurve *curve, const uint8_t *data, size_t length)
Import an EC private key.
Definition: ec.c:490
Working state (twin multiplication)
Definition: ec.h:490
error_t ecImportPublicKey(EcPublicKey *key, const EcCurve *curve, const uint8_t *data, size_t length, EcPublicKeyFormat format)
Import an EC public key.
Definition: ec.c:263
error_t ecMulFast(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (fast calculation)
Definition: ec.c:1181
uint8_t r
Definition: ndp.h:346
const uint32_t pmu[EC_MAX_MODULUS_SIZE+1]
Pre-computed value mu.
Definition: ec.h:526
EcState subState
Definition: ec.h:493
void ecAdd(EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
Point addition (helper routine)
Definition: ec.c:957
void ecInitPrivateKey(EcPrivateKey *key)
Initialize an EC private key.
Definition: ec.c:80
uint8_t h
Definition: ndp.h:302
EcPoint3 p
Definition: ec.h:479
uint32_t init
Definition: ec.h:476
void ecFullAdd(EcState *state, EcPoint3 *r, const EcPoint3 *s, const EcPoint3 *t)
Point addition.
Definition: ec.c:1094
EcPoint3 q
Definition: ec.h:480
error_t
Error codes.
Definition: error.h:43
void ecFreePublicKey(EcPublicKey *key)
Release an EC public key.
Definition: ec.c:68
bool_t ecIsPointAffine(const EcCurve *curve, const EcPoint *s)
Check whether the affine point S is on the curve.
Definition: ec.c:798
error_t ecGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext, const EcCurve *curve, EcPrivateKey *privateKey)
EC private key generation.
Definition: ec.c:158
uint32_t h
Cofactor h.
Definition: ec.h:532
EcPublicKeyFormat
EC public key format.
Definition: ec.h:385
General definitions for cryptographic algorithms.
EcState subState
Definition: ec.h:481
EcPublicKey q
Public key.
Definition: ec.h:436
const uint32_t p[EC_MAX_MODULUS_SIZE+1]
Prime modulus p.
Definition: ec.h:525
@ EC_PUBLIC_KEY_FORMAT_RAW
Definition: ec.h:387
EC private key.
Definition: ec.h:432
const uint8_t * oid
Object identifier.
Definition: ec.h:520
EcInvModAlgo scalarInv
Scalar modular inversion.
Definition: ec.h:536
EcPoint3 q0
Definition: ec.h:478
uint32_t t2
EC point (affine coordinates)
Definition: ec.h:398
void ecProjectify(const EcCurve *curve, EcPoint3 *r, const EcPoint *s)
Compute projective representation.
Definition: ec.c:720
uint8_t length
Definition: tcp.h:375
uint_t orderSize
Order size, in bits.
Definition: ec.h:524
@ EC_POINT_FORMAT_COMPRESSED_EVEN
Definition: ec.h:374
EcCurveType type
Curve type.
Definition: ec.h:522
error_t ecImportPoint(const EcCurve *curve, EcPoint *r, const uint8_t *data, size_t length)
Convert an octet string to an EC point.
Definition: ec.c:626
uint8_t z
Definition: dns_common.h:191
EC public key.
Definition: ec.h:421
char char_t
Definition: compiler_port.h:55
const EcCurve * curve
Definition: ec.h:446
error_t ecAffinify(const EcCurve *curve, EcPoint3 *r, const EcPoint3 *s)
Recover affine representation.
Definition: ec.c:749
uint32_t t1
Working state (point addition/subtraction/doubling)
Definition: ec.h:445
const uint32_t q[EC_MAX_ORDER_SIZE+1]
Order of the base point G.
Definition: ec.h:530
EC point (projective coordinates)
Definition: ec.h:409
void(* EcModAlgo)(const EcCurve *curve, uint32_t *r, const uint32_t *a)
Modular reduction.
Definition: ec.h:501
const uint8_t EC_PUBLIC_KEY_OID[7]
Definition: ec.c:44
size_t oidSize
OID size.
Definition: ec.h:521
EcCurveType
Elliptic curve type.
Definition: ec.h:359
const uint32_t a[EC_MAX_MODULUS_SIZE]
Curve parameter a.
Definition: ec.h:527
EcPoint3 g
Base point G.
Definition: ec.h:529
@ EC_PUBLIC_KEY_FORMAT_RAW_X
Definition: ec.h:388
EcPoint q
Public key.
Definition: ec.h:423
EcState subState
Definition: ec.h:466
void(* EcInvModAlgo)(const EcCurve *curve, uint32_t *r, const uint32_t *a)
Modular inverse.
Definition: ec.h:509
error_t ecExportPrivateKey(const EcPrivateKey *key, uint8_t *data, size_t *length)
Export an EC private key.
Definition: ec.c:554
void ecInitPublicKey(EcPublicKey *key)
Initialize an EC public key.
Definition: ec.c:52
uint8_t s
Definition: igmp_common.h:234
@ EC_CURVE_TYPE_WEIERSTRASS
Definition: ec.h:360
void ecDouble(EcState *state, EcPoint3 *r, const EcPoint3 *s)
Point doubling.
Definition: ec.c:848
error_t ecExportPublicKey(const EcPublicKey *key, uint8_t *data, size_t *length, EcPublicKeyFormat format)
Export an EC public key.
Definition: ec.c:378
#define EcCurve
Definition: ec.h:346
const uint32_t qmu[EC_MAX_ORDER_SIZE+1]
Pre-computed value mu.
Definition: ec.h:531
uint_t fieldSize
Field size, in bits.
Definition: ec.h:523
unsigned int uint_t
Definition: compiler_port.h:57
error_t ecExportPoint(const EcCurve *curve, const EcPoint *a, uint8_t *data, size_t *length)
Convert an EC point to an octet string.
Definition: ec.c:675
EcModAlgo scalarMod
Scalar modular reduction.
Definition: ec.h:535
const EcCurve * curve
Elliptic curve parameters.
Definition: ec.h:422
@ EC_CURVE_TYPE_WEIERSTRASS_A0
Definition: ec.h:361
error_t ecTwinMul(const EcCurve *curve, EcPoint3 *r, const uint32_t *d0, const EcPoint3 *s, const uint32_t *d1, const EcPoint3 *t)
Twin multiplication.
Definition: ec.c:1418
void ecFreePrivateKey(EcPrivateKey *key)
Release an EC private key.
Definition: ec.c:100
#define EC_MAX_MODULUS_SIZE
Definition: ec.h:284
error_t ecMulRegular(const EcCurve *curve, EcPoint3 *r, const uint32_t *d, const EcPoint3 *s)
Scalar multiplication (regular calculation)
Definition: ec.c:1312
EcModAlgo fieldMod
Field modular reduction.
Definition: ec.h:533
EcPointFormat
EC point format.
Definition: ec.h:373