blob: 0de84aae9603989809df6a375a51090346048509 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/**
2 * \file ecp.h
3 *
4 * \brief Elliptic curves over GF(p)
5 *
6 * Copyright (C) 2012, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (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 along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_ECP_H
28#define POLARSSL_ECP_H
29
30#include "bignum.h"
31
32/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010033 * ECP error codes
34 *
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010035 * (Only one error code available...)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010036 */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010037#define POLARSSL_ERR_ECP_GENERIC -0x007E /**< Generic ECP error */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010038
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010039/**
40 * \brief ECP point structure (affine coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010041 *
42 * Note: if the point is zero, X and Y are irrelevant and should be freed.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010043 */
44typedef struct
45{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010046 char is_zero; /*!< true if point at infinity */
47 mpi X; /*!< the point's X coordinate */
48 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010049}
50ecp_point;
51
52/**
53 * \brief ECP group structure
54 *
55 * The curves we consider are defined by y^2 = x^3 - 3x + b mod p,
56 * and a generator for a large subgroup is fixed.
57 */
58typedef struct
59{
60 mpi P; /*!< prime modulus of the base field */
61 mpi B; /*!< constant term in the equation */
62 ecp_point G; /*!< generator of the subgroup used */
63 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010064}
65ecp_group;
66
67/**
68 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
69 *
70 * These also are the NIST-recommended ECP groups, are the random ECP groups
71 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010072 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010073 *
74 * \warning This library does not support validation of arbitrary domain
75 * parameters. Therefore, only well-known domain parameters from trusted
76 * sources (such as the ones below) should be used.
77 */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010078#define POLARSSL_ECP_DP_SECP192R1 0
79#define POLARSSL_ECP_DP_SECP224R1 1
80#define POLARSSL_ECP_DP_SECP256R1 2
81#define POLARSSL_ECP_DP_SECP384R1 3
82#define POLARSSL_ECP_DP_SECP521R1 4
83
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010084#define POLARSSL_ECP_SECP192R1_P \
85 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
86#define POLARSSL_ECP_SECP192R1_B \
87 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
88#define POLARSSL_ECP_SECP192R1_GX \
89 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
90#define POLARSSL_ECP_SECP192R1_GY \
91 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
92#define POLARSSL_ECP_SECP192R1_N \
93 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
94
95#define POLARSSL_ECP_SECP224R1_P \
96 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
97#define POLARSSL_ECP_SECP224R1_B \
98 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
99#define POLARSSL_ECP_SECP224R1_GX \
100 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
101#define POLARSSL_ECP_SECP224R1_GY \
102 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
103#define POLARSSL_ECP_SECP224R1_N \
104 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
105
106#define POLARSSL_ECP_SECP256R1_P \
107 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
108#define POLARSSL_ECP_SECP256R1_B \
109 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
110#define POLARSSL_ECP_SECP256R1_GX \
111 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
112#define POLARSSL_ECP_SECP256R1_GY \
113 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
114#define POLARSSL_ECP_SECP256R1_N \
115 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
116
117#define POLARSSL_ECP_SECP384R1_P \
118 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
119 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
120#define POLARSSL_ECP_SECP384R1_B \
121 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
122 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
123#define POLARSSL_ECP_SECP384R1_GX \
124 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
125 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
126#define POLARSSL_ECP_SECP384R1_GY \
127 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
128 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
129#define POLARSSL_ECP_SECP384R1_N \
130 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
131 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
132
133#define POLARSSL_ECP_SECP521R1_P \
134 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
135 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
136 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
137#define POLARSSL_ECP_SECP521R1_B \
138 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
139 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
140 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
141#define POLARSSL_ECP_SECP521R1_GX \
142 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
143 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
144 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
145#define POLARSSL_ECP_SECP521R1_GY \
146 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
147 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
148 "3FAD0761353C7086A272C24088BE94769FD16650"
149#define POLARSSL_ECP_SECP521R1_N \
150 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
151 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
152 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100153
154#ifdef __cplusplus
155extern "C" {
156#endif
157
158/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100159 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100160 */
161void ecp_point_init( ecp_point *pt );
162
163/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100164 * \brief Initialize a group (to something meaningless)
165 */
166void ecp_group_init( ecp_group *grp );
167
168/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100169 * \brief Free the components of a point
170 */
171void ecp_point_free( ecp_point *pt );
172
173/**
174 * \brief Free the components of an ECP group
175 */
176void ecp_group_free( ecp_group *grp );
177
178/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100179 * \brief Set a point to zero
180 */
181void ecp_set_zero( ecp_point *pt );
182
183/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100184 * \brief Copy the contents of point Q into P
185 *
186 * \param P Destination point
187 * \param Q Source point
188 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100189 * \return 0 if successful,
190 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100191 */
192int ecp_copy( ecp_point *P, const ecp_point *Q );
193
194/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100195 * \brief Import a non-zero point from two ASCII strings
196 *
197 * \param P Destination point
198 * \param radix Input numeric base
199 * \param x First affine coordinate as a null-terminated string
200 * \param y Second affine coordinate as a null-terminated string
201 *
202 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
203 */
204int ecp_point_read_string( ecp_point *P, int radix,
205 const char *x, const char *y );
206
207/**
208 * \brief Import an ECP group from null-terminated ASCII strings
209 *
210 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100211 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100212 * \param p Prime modulus of the base field
213 * \param b Constant term in the equation
214 * \param gx The generator's X coordinate
215 * \param gy The generator's Y coordinate
216 * \param n The generator's order
217 *
218 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
219 */
220int ecp_group_read_string( ecp_group *grp, int radix,
221 const char *p, const char *b,
222 const char *gx, const char *gy, const char *n);
223
224/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100225 * \brief Set a group using well-known domain parameters
226 *
227 * \param grp Destination group
228 * \param index Index in the list of well-known domain parameters
229 *
230 * \return O if successul,
231 * POLARSSL_ERR_MPI_XXX if initialization failed
232 * POLARSSL_ERR_ECP_GENERIC if index is out of range
233 *
234 * \note Index should be a POLARSSL_ECP_DP_XXX macro.
235 */
236int ecp_use_known_dp( ecp_group *grp, size_t index );
237
238/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100239 * \brief Addition: R = P + Q
240 *
241 * \param grp ECP group
242 * \param R Destination point
243 * \param P Left-hand point
244 * \param Q Right-hand point
245 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100246 * \return 0 if successful,
247 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100248 */
249int ecp_add( const ecp_group *grp, ecp_point *R,
250 const ecp_point *P, const ecp_point *Q );
251
252/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100253 * \brief Multiplication by an integer: R = m * P
254 *
255 * \param grp ECP group
256 * \param R Destination point
257 * \param m Integer by which to multiply
258 * \param P Point to multiply
259 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100260 * \return 0 if successful,
261 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100262 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100263int ecp_mul( const ecp_group *grp, ecp_point *R,
264 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100265
266/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100267 * \brief Checkup routine
268 *
269 * \return 0 if successful, or 1 if the test failed
270 */
271int ecp_self_test( int verbose );
272
273#ifdef __cplusplus
274}
275#endif
276
277#endif