blob: 6a504f1deea1bbd4b7867444be3b9ceecf159cbf [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.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010057 *
58 * modp may be NULL; pbits will not be used in this case.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010059 */
60typedef struct
61{
62 mpi P; /*!< prime modulus of the base field */
63 mpi B; /*!< constant term in the equation */
64 ecp_point G; /*!< generator of the subgroup used */
65 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010066 int (*modp)(mpi *); /*!< function for fast reduction mod P */
67 unsigned pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010068}
69ecp_group;
70
71/**
72 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
73 *
74 * These also are the NIST-recommended ECP groups, are the random ECP groups
75 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010076 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010077 *
78 * \warning This library does not support validation of arbitrary domain
79 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +010080 * sources (such as the ones below) should be used. See ecp_use_known_dp().
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010081 */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010082#define POLARSSL_ECP_DP_SECP192R1 0
83#define POLARSSL_ECP_DP_SECP224R1 1
84#define POLARSSL_ECP_DP_SECP256R1 2
85#define POLARSSL_ECP_DP_SECP384R1 3
86#define POLARSSL_ECP_DP_SECP521R1 4
87
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010088#ifdef __cplusplus
89extern "C" {
90#endif
91
92/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +010093 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010094 */
95void ecp_point_init( ecp_point *pt );
96
97/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +010098 * \brief Initialize a group (to something meaningless)
99 */
100void ecp_group_init( ecp_group *grp );
101
102/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100103 * \brief Free the components of a point
104 */
105void ecp_point_free( ecp_point *pt );
106
107/**
108 * \brief Free the components of an ECP group
109 */
110void ecp_group_free( ecp_group *grp );
111
112/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100113 * \brief Set a point to zero
114 */
115void ecp_set_zero( ecp_point *pt );
116
117/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100118 * \brief Copy the contents of point Q into P
119 *
120 * \param P Destination point
121 * \param Q Source point
122 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100123 * \return 0 if successful,
124 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100125 */
126int ecp_copy( ecp_point *P, const ecp_point *Q );
127
128/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100129 * \brief Import a non-zero point from two ASCII strings
130 *
131 * \param P Destination point
132 * \param radix Input numeric base
133 * \param x First affine coordinate as a null-terminated string
134 * \param y Second affine coordinate as a null-terminated string
135 *
136 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
137 */
138int ecp_point_read_string( ecp_point *P, int radix,
139 const char *x, const char *y );
140
141/**
142 * \brief Import an ECP group from null-terminated ASCII strings
143 *
144 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100145 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100146 * \param p Prime modulus of the base field
147 * \param b Constant term in the equation
148 * \param gx The generator's X coordinate
149 * \param gy The generator's Y coordinate
150 * \param n The generator's order
151 *
152 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
153 */
154int ecp_group_read_string( ecp_group *grp, int radix,
155 const char *p, const char *b,
156 const char *gx, const char *gy, const char *n);
157
158/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100159 * \brief Set a group using well-known domain parameters
160 *
161 * \param grp Destination group
162 * \param index Index in the list of well-known domain parameters
163 *
164 * \return O if successul,
165 * POLARSSL_ERR_MPI_XXX if initialization failed
166 * POLARSSL_ERR_ECP_GENERIC if index is out of range
167 *
168 * \note Index should be a POLARSSL_ECP_DP_XXX macro.
169 */
170int ecp_use_known_dp( ecp_group *grp, size_t index );
171
172/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100173 * \brief Addition: R = P + Q
174 *
175 * \param grp ECP group
176 * \param R Destination point
177 * \param P Left-hand point
178 * \param Q Right-hand point
179 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100180 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100181 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100182 */
183int ecp_add( const ecp_group *grp, ecp_point *R,
184 const ecp_point *P, const ecp_point *Q );
185
186/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100187 * \brief Multiplication by an integer: R = m * P
188 *
189 * \param grp ECP group
190 * \param R Destination point
191 * \param m Integer by which to multiply
192 * \param P Point to multiply
193 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100194 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100195 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100196 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100197int ecp_mul( const ecp_group *grp, ecp_point *R,
198 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100199
200/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100201 * \brief Checkup routine
202 *
203 * \return 0 if successful, or 1 if the test failed
204 */
205int ecp_self_test( int verbose );
206
207#ifdef __cplusplus
208}
209#endif
210
211#endif