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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _EC_H
30 #define _EC_H
31 
32 //Dependencies
33 #include "core/crypto.h"
34 #include "ecc/ec_curves.h"
35 
36 //Error code checking
37 #define EC_CHECK(f) if((error = f) != NO_ERROR) goto end
38 
39 //C++ guard
40 #ifdef __cplusplus
41  extern "C" {
42 #endif
43 
44 
45 /**
46  * @brief Elliptic curve point
47  **/
48 
49 typedef struct
50 {
51  Mpi x; ///<x-coordinate
52  Mpi y; ///<y-coordinate
53  Mpi z; ///<z-coordinate
54 } EcPoint;
55 
56 
57 /**
58  * @brief EC domain parameters
59  **/
60 
61 typedef struct
62 {
63  const char_t *name; ///<Curve name
64  EcCurveType type; ///<Curve type
65  Mpi p; ///<Prime
66  Mpi a; ///<Curve parameter a
67  Mpi b; ///<Curve parameter b
68  EcPoint g; ///<Base point G
69  Mpi q; ///<Order of the point G
70  uint32_t h; ///<Cofactor h
71  EcFastModAlgo mod; ///<Fast modular reduction
73 
74 
75 //EC related constants
76 extern const uint8_t EC_PUBLIC_KEY_OID[7];
77 
78 //EC related functions
81 
83  const EcCurveInfo *curveInfo);
84 
85 void ecInit(EcPoint *r);
86 void ecFree(EcPoint *r);
87 
88 error_t ecCopy(EcPoint *r, const EcPoint *s);
89 
91  const uint8_t *data, size_t length);
92 
93 error_t ecExport(const EcDomainParameters *params, const EcPoint *a,
94  uint8_t *data, size_t *length);
95 
97  const EcPoint *s);
98 
100  const EcPoint *s);
101 
102 bool_t ecIsPointAffine(const EcDomainParameters *params, const EcPoint *s);
103 
105  const EcPoint *s);
106 
107 error_t ecAdd(const EcDomainParameters *params, EcPoint *r,
108  const EcPoint *s, const EcPoint *t);
109 
111  const EcPoint *s, const EcPoint *t);
112 
114  const EcPoint *s, const EcPoint *t);
115 
116 error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d,
117  const EcPoint *s);
118 
120  const Mpi *d0, const EcPoint *s, const Mpi *d1, const EcPoint *t);
121 
122 error_t ecAddMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
123  const Mpi *b);
124 
125 error_t ecSubMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
126  const Mpi *b);
127 
128 error_t ecMulMod(const EcDomainParameters *params, Mpi *r, const Mpi *a,
129  const Mpi *b);
130 
131 error_t ecSqrMod(const EcDomainParameters *params, Mpi *r, const Mpi *a);
132 
133 //C++ guard
134 #ifdef __cplusplus
135  }
136 #endif
137 
138 #endif
EC domain parameters.
Definition: ec.h:61
Arbitrary precision integer.
Definition: mpi.h:67
char char_t
Definition: compiler_port.h:41
void ecInit(EcPoint *r)
Initialize elliptic curve point.
Definition: ec.c:150
General definitions for cryptographic algorithms.
Elliptic curve parameters.
Definition: ec_curves.h:290
error_t ecAddMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular addition.
Definition: ec.c:1148
EcFastModAlgo mod
Fast modular reduction.
Definition: ec.h:71
error_t ecFullAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition.
Definition: ec.c:742
Mpi x
x-coordinate
Definition: ec.h:51
void ecFree(EcPoint *r)
Release an elliptic curve point.
Definition: ec.c:164
uint8_t a
Definition: ndp.h:407
Mpi b
Curve parameter b.
Definition: ec.h:67
Elliptic curves.
EcCurveType type
Curve type.
Definition: ec.h:64
Elliptic curve point.
Definition: ec.h:49
error_t ecMulMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular multiplication.
Definition: ec.c:1206
error_t ecAffinify(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Recover affine representation.
Definition: ec.c:362
error_t ecMult(const EcDomainParameters *params, EcPoint *r, const Mpi *d, const EcPoint *s)
Scalar multiplication.
Definition: ec.c:829
error_t(* EcFastModAlgo)(Mpi *a, const Mpi *p)
Fast modular reduction.
Definition: ec_curves.h:283
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:281
Mpi y
y-coordinate
Definition: ec.h:52
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:208
Mpi q
Order of the point G.
Definition: ec.h:69
Mpi p
Prime.
Definition: ec.h:65
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:978
void ecInitDomainParameters(EcDomainParameters *params)
Initialize EC domain parameters.
Definition: ec.c:53
const uint8_t EC_PUBLIC_KEY_OID[7]
Definition: ec.c:45
EcPoint g
Base point G.
Definition: ec.h:68
uint32_t h
Cofactor h.
Definition: ec.h:70
error_t
Error codes.
Definition: error.h:40
error_t ecFullSub(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point subtraction.
Definition: ec.c:793
EcCurveType
Elliptic curve type.
Definition: ec_curves.h:262
error_t ecSubMod(const EcDomainParameters *params, Mpi *r, const Mpi *a, const Mpi *b)
Fast modular subtraction.
Definition: ec.c:1177
uint8_t data[]
Definition: dtls_misc.h:167
error_t ecDouble(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Point doubling.
Definition: ec.c:450
error_t ecCopy(EcPoint *r, const EcPoint *s)
Copy EC point.
Definition: ec.c:180
uint32_t r
Definition: ndp.h:342
Mpi a
Curve parameter a.
Definition: ec.h:66
Mpi z
z-coordinate
Definition: ec.h:53
error_t ecAdd(const EcDomainParameters *params, EcPoint *r, const EcPoint *s, const EcPoint *t)
Point addition (helper routine)
Definition: ec.c:582
void ecFreeDomainParameters(EcDomainParameters *params)
Release EC domain parameters.
Definition: ec.c:73
error_t ecLoadDomainParameters(EcDomainParameters *params, const EcCurveInfo *curveInfo)
Load EC domain parameters.
Definition: ec.c:91
uint8_t length
Definition: dtls_misc.h:140
bool_t ecIsPointAffine(const EcDomainParameters *params, const EcPoint *s)
Check whether the affine point S is on the curve.
Definition: ec.c:408
int bool_t
Definition: compiler_port.h:47
error_t ecSqrMod(const EcDomainParameters *params, Mpi *r, const Mpi *a)
Fast modular squaring.
Definition: ec.c:1238
const char_t * name
Curve name.
Definition: ec.h:63
uint8_t b[6]
Definition: dtls_misc.h:130
error_t ecProjectify(const EcDomainParameters *params, EcPoint *r, const EcPoint *s)
Compute projective representation.
Definition: ec.c:338