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-2019 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 1.9.6
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 Elliptic curve point
49  **/
50 
51 typedef struct
52 {
53  Mpi x; ///<x-coordinate
54  Mpi y; ///<y-coordinate
55  Mpi z; ///<z-coordinate
56 } EcPoint;
57 
58 
59 /**
60  * @brief EC domain parameters
61  **/
62 
63 typedef struct
64 {
65  const char_t *name; ///<Curve name
66  EcCurveType type; ///<Curve type
67  Mpi p; ///<Prime
68  Mpi a; ///<Curve parameter a
69  Mpi b; ///<Curve parameter b
70  EcPoint g; ///<Base point G
71  Mpi q; ///<Order of the point G
72  uint32_t h; ///<Cofactor h
73  EcFastModAlgo mod; ///<Fast modular reduction
75 
76 
77 //EC related constants
78 extern const uint8_t EC_PUBLIC_KEY_OID[7];
79 
80 //EC related functions
83 
85  const EcCurveInfo *curveInfo);
86 
87 void ecInit(EcPoint *r);
88 void ecFree(EcPoint *r);
89 
90 error_t ecCopy(EcPoint *r, const EcPoint *s);
91 
93  const uint8_t *data, size_t length);
94 
95 error_t ecExport(const EcDomainParameters *params, const EcPoint *a,
96  uint8_t *data, size_t *length);
97 
99  const EcPoint *s);
100 
102  const EcPoint *s);
103 
104 bool_t ecIsPointAffine(const EcDomainParameters *params, const EcPoint *s);
105 
107  const EcPoint *s);
108 
109 error_t ecAdd(const EcDomainParameters *params, EcPoint *r,
110  const EcPoint *s, const EcPoint *t);
111 
113  const EcPoint *s, const EcPoint *t);
114 
116  const EcPoint *s, const EcPoint *t);
117 
118 error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d,
119  const EcPoint *s);
120 
122  const Mpi *d0, const EcPoint *s, const Mpi *d1, const EcPoint *t);
123 
124 error_t ecAddMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
125  const Mpi *b);
126 
127 error_t ecSubMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
128  const Mpi *b);
129 
130 error_t ecMulMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
131  const Mpi *b);
132 
133 error_t ecSqrMod(const EcDomainParameters *params, Mpi *r, const Mpi *a);
134 
135 //C++ guard
136 #ifdef __cplusplus
137 }
138 #endif
139 
140 #endif
error_t ecAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition (helper routine)
Definition: ec.c:588
uint8_t length
Definition: dtls_misc.h:149
error_t ecAddMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular addition.
Definition: ec.c:1154
int bool_t
Definition: compiler_port.h:49
Mpi p
Prime.
Definition: ec.h:67
error_t ecFullAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition.
Definition: ec.c:748
uint8_t a
Definition: ndp.h:410
Arbitrary precision integer.
Definition: mpi.h:69
error_t ecLoadDomainParameters(EcDomainParameters *params, const EcCurveInfo *curveInfo)
Load EC domain parameters.
Definition: ec.c:93
uint8_t b[6]
Definition: dtls_misc.h:139
bool_t ecIsPointAffine(const EcDomainParameters *params, const EcPoint *s)
Check whether the affine point S is on the curve.
Definition: ec.c:414
error_t ecProjectify(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Compute projective representation.
Definition: ec.c:344
EcPoint g
Base point G.
Definition: ec.h:70
uint32_t h
Cofactor h.
Definition: ec.h:72
void ecInit(EcPoint *r)
Initialize elliptic curve point.
Definition: ec.c:156
Mpi y
y-coordinate
Definition: ec.h:54
error_t ecAffinify(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Recover affine representation.
Definition: ec.c:368
error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d, const EcPoint *s)
Scalar multiplication.
Definition: ec.c:835
EC domain parameters.
Definition: ec.h:63
Mpi a
Curve parameter a.
Definition: ec.h:68
uint32_t r
Definition: ndp.h:345
uint8_t t
Definition: llmnr_common.h:81
void ecInitDomainParameters(EcDomainParameters *params)
Initialize EC domain parameters.
Definition: ec.c:55
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:214
Elliptic curve parameters.
Definition: ec_curves.h:292
error_t
Error codes.
Definition: error.h:42
void ecFree(EcPoint *r)
Release an elliptic curve point.
Definition: ec.c:170
General definitions for cryptographic algorithms.
Mpi x
x-coordinate
Definition: ec.h:53
const char_t * name
Curve name.
Definition: ec.h:65
error_t ecSubMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular subtraction.
Definition: ec.c:1183
Elliptic curve point.
Definition: ec.h:51
error_t ecMulMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular multiplication.
Definition: ec.c:1212
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:287
char char_t
Definition: compiler_port.h:43
void ecFreeDomainParameters(EcDomainParameters *params)
Release EC domain parameters.
Definition: ec.c:75
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:984
EcFastModAlgo mod
Fast modular reduction.
Definition: ec.h:73
const uint8_t EC_PUBLIC_KEY_OID[7]
Definition: ec.c:47
error_t ecSqrMod(const EcDomainParameters *params, Mpi *r, const Mpi *a)
Fast modular squaring.
Definition: ec.c:1244
EcCurveType type
Curve type.
Definition: ec.h:66
error_t ecFullSub(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point subtraction.
Definition: ec.c:799
Mpi b
Curve parameter b.
Definition: ec.h:69
error_t(* EcFastModAlgo)(Mpi *a, const Mpi *p)
Fast modular reduction.
Definition: ec_curves.h:285
Elliptic curves.
uint8_t data[]
Definition: dtls_misc.h:176
error_t ecDouble(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Point doubling.
Definition: ec.c:456
EcCurveType
Elliptic curve type.
Definition: ec_curves.h:264
Mpi z
z-coordinate
Definition: ec.h:55
Mpi q
Order of the point G.
Definition: ec.h:71
error_t ecCopy(EcPoint *r, const EcPoint *s)
Copy EC point.
Definition: ec.c:186