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-2024 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.4.4
29  **/
30 
31 #ifndef _EC_H
32 #define _EC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "ecc/ec_curves.h"
37 
38 //Error code checking
39 #define EC_CHECK(f) if((error = f) != NO_ERROR) goto end
40 
41 //C++ guard
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
47 /**
48  * @brief EC point format
49  **/
50 
51 typedef enum
52 {
57 
58 
59 /**
60  * @brief EC point
61  **/
62 
63 typedef struct
64 {
65  Mpi x; ///<x-coordinate
66  Mpi y; ///<y-coordinate
67  Mpi z; ///<z-coordinate
68 } EcPoint;
69 
70 
71 /**
72  * @brief EC domain parameters
73  **/
74 
75 typedef struct
76 {
77  const char_t *name; ///<Curve name
78  EcCurveType type; ///<Curve type
79  Mpi p; ///<Prime
80  Mpi a; ///<Curve parameter a
81  Mpi b; ///<Curve parameter b
82  EcPoint g; ///<Base point G
83  Mpi q; ///<Order of the point G
84  uint32_t h; ///<Cofactor h
85  EcFastModAlgo mod; ///<Fast modular reduction
87 
88 
89 /**
90  * @brief EC public key
91  **/
92 
93 typedef struct
94 {
95  EcPoint q; ///<Public key
96 } EcPublicKey;
97 
98 
99 /**
100  * @brief EC private key
101  **/
102 
103 typedef struct
104 {
105  Mpi d; ///<Private key
106  int_t slot; ///<Private key slot
107 } EcPrivateKey;
108 
109 
110 //EC related constants
111 extern const uint8_t EC_PUBLIC_KEY_OID[7];
112 
113 //EC related functions
116 
118  const EcCurveInfo *curveInfo);
119 
120 void ecInitPublicKey(EcPublicKey *key);
121 void ecFreePublicKey(EcPublicKey *key);
122 
123 void ecInitPrivateKey(EcPrivateKey *key);
124 void ecFreePrivateKey(EcPrivateKey *key);
125 
126 error_t ecGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext,
127  const EcDomainParameters *params, EcPrivateKey *privateKey,
128  EcPublicKey *publicKey);
129 
130 error_t ecGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext,
131  const EcDomainParameters *params, EcPrivateKey *privateKey);
132 
134  const EcPrivateKey *privateKey, EcPublicKey *publicKey);
135 
136 void ecInit(EcPoint *r);
137 void ecFree(EcPoint *r);
138 
139 error_t ecCopy(EcPoint *r, const EcPoint *s);
140 
142  const uint8_t *data, size_t length);
143 
144 error_t ecExport(const EcDomainParameters *params, const EcPoint *a,
145  uint8_t *data, size_t *length);
146 
148  const EcPoint *s);
149 
151  const EcPoint *s);
152 
153 bool_t ecIsPointAffine(const EcDomainParameters *params, const EcPoint *s);
154 
156  const EcPoint *s);
157 
158 error_t ecAdd(const EcDomainParameters *params, EcPoint *r,
159  const EcPoint *s, const EcPoint *t);
160 
162  const EcPoint *s, const EcPoint *t);
163 
165  const EcPoint *s, const EcPoint *t);
166 
167 error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d,
168  const EcPoint *s);
169 
171  const Mpi *d0, const EcPoint *s, const Mpi *d1, const EcPoint *t);
172 
173 error_t ecAddMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
174  const Mpi *b);
175 
176 error_t ecSubMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
177  const Mpi *b);
178 
179 error_t ecMulMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
180  const Mpi *b);
181 
182 error_t ecSqrMod(const EcDomainParameters *params, Mpi *r, const Mpi *a);
183 
184 //C++ guard
185 #ifdef __cplusplus
186 }
187 #endif
188 
189 #endif
error_t ecAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition (helper routine)
Definition: ec.c:739
error_t ecAddMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular addition.
Definition: ec.c:1317
int bool_t
Definition: compiler_port.h:53
uint8_t b
Definition: nbns_common.h:104
Mpi p
Prime.
Definition: ec.h:79
@ EC_POINT_FORMAT_UNCOMPRESSED
Definition: ec.h:55
error_t ecFullAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition.
Definition: ec.c:899
uint8_t a
Definition: ndp.h:411
Arbitrary precision integer.
Definition: mpi.h:80
signed int int_t
Definition: compiler_port.h:49
#define PrngAlgo
Definition: crypto.h:938
error_t ecLoadDomainParameters(EcDomainParameters *params, const EcCurveInfo *curveInfo)
Load EC domain parameters.
Definition: ec.c:90
uint8_t t
Definition: lldp_ext_med.h:212
int_t slot
Private key slot.
Definition: ec.h:106
bool_t ecIsPointAffine(const EcDomainParameters *params, const EcPoint *s)
Check whether the affine point S is on the curve.
Definition: ec.c:565
error_t ecProjectify(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Compute projective representation.
Definition: ec.c:495
uint8_t data[]
Definition: ethernet.h:222
Mpi d
Private key.
Definition: ec.h:105
EcPoint g
Base point G.
Definition: ec.h:82
uint32_t h
Cofactor h.
Definition: ec.h:84
void ecInit(EcPoint *r)
Initialize elliptic curve point.
Definition: ec.c:307
Mpi y
y-coordinate
Definition: ec.h:66
error_t ecAffinify(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Recover affine representation.
Definition: ec.c:519
error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d, const EcPoint *s)
Scalar multiplication.
Definition: ec.c:998
error_t ecGeneratePrivateKey(const PrngAlgo *prngAlgo, void *prngContext, const EcDomainParameters *params, EcPrivateKey *privateKey)
EC private key generation.
Definition: ec.c:239
EC domain parameters.
Definition: ec.h:76
@ EC_POINT_FORMAT_COMPRESSED_ODD
Definition: ec.h:54
Mpi a
Curve parameter a.
Definition: ec.h:80
uint8_t r
Definition: ndp.h:346
void ecInitDomainParameters(EcDomainParameters *params)
Initialize EC domain parameters.
Definition: ec.c:51
error_t ecImport(const EcDomainParameters *params, EcPoint *r, const uint8_t *data, size_t length)
Convert an octet string to an EC point.
Definition: ec.c:365
error_t ecGeneratePublicKey(const EcDomainParameters *params, const EcPrivateKey *privateKey, EcPublicKey *publicKey)
Derive the public key from an EC private key.
Definition: ec.c:275
Elliptic curve parameters.
Definition: ec_curves.h:302
void ecInitPrivateKey(EcPrivateKey *key)
Initialize an EC private key.
Definition: ec.c:177
error_t
Error codes.
Definition: error.h:43
void ecFreePublicKey(EcPublicKey *key)
Release an EC public key.
Definition: ec.c:165
void ecFree(EcPoint *r)
Release an elliptic curve point.
Definition: ec.c:321
General definitions for cryptographic algorithms.
Mpi x
x-coordinate
Definition: ec.h:65
EC private key.
Definition: ec.h:104
const char_t * name
Curve name.
Definition: ec.h:77
error_t ecSubMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular subtraction.
Definition: ec.c:1346
EC point.
Definition: ec.h:64
error_t ecMulMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular multiplication.
Definition: ec.c:1375
uint8_t length
Definition: tcp.h:368
error_t ecExport(const EcDomainParameters *params, const EcPoint *a, uint8_t *data, size_t *length)
Convert an EC point to an octet string.
Definition: ec.c:438
@ EC_POINT_FORMAT_COMPRESSED_EVEN
Definition: ec.h:53
EC public key.
Definition: ec.h:94
char char_t
Definition: compiler_port.h:48
void ecFreeDomainParameters(EcDomainParameters *params)
Release EC domain parameters.
Definition: ec.c:72
error_t ecTwinMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d0, const EcPoint *s, const Mpi *d1, const EcPoint *t)
Twin multiplication.
Definition: ec.c:1147
EcFastModAlgo mod
Fast modular reduction.
Definition: ec.h:85
const uint8_t EC_PUBLIC_KEY_OID[7]
Definition: ec.c:43
EcPoint q
Public key.
Definition: ec.h:95
error_t ecSqrMod(const EcDomainParameters *params, Mpi *r, const Mpi *a)
Fast modular squaring.
Definition: ec.c:1407
void ecInitPublicKey(EcPublicKey *key)
Initialize an EC public key.
Definition: ec.c:153
uint8_t s
Definition: igmp_common.h:234
EcCurveType type
Curve type.
Definition: ec.h:78
error_t ecFullSub(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point subtraction.
Definition: ec.c:962
error_t ecGenerateKeyPair(const PrngAlgo *prngAlgo, void *prngContext, const EcDomainParameters *params, EcPrivateKey *privateKey, EcPublicKey *publicKey)
EC key pair generation.
Definition: ec.c:209
Mpi b
Curve parameter b.
Definition: ec.h:81
error_t(* EcFastModAlgo)(Mpi *a, const Mpi *p)
Fast modular reduction.
Definition: ec_curves.h:294
Elliptic curves.
error_t ecDouble(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Point doubling.
Definition: ec.c:607
EcCurveType
Elliptic curve type.
Definition: ec_curves.h:274
Mpi z
z-coordinate
Definition: ec.h:67
Mpi q
Order of the point G.
Definition: ec.h:83
void ecFreePrivateKey(EcPrivateKey *key)
Release an EdDSA private key.
Definition: ec.c:192
EcPointFormat
EC point format.
Definition: ec.h:52
error_t ecCopy(EcPoint *r, const EcPoint *s)
Copy EC point.
Definition: ec.c:337