blob: e76cd9eb0e29266bfd200cd415c95449bc8bb2c0 [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/*
33 * ECP Error codes
34 */
35
36/**
37 * \brief ECP point structure (affine coordinates)
38 */
39typedef struct
40{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010041 char is_zero; /*!< true if point at infinity */
42 mpi X; /*!< the point's X coordinate */
43 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010044}
45ecp_point;
46
47/**
48 * \brief ECP group structure
49 *
50 * The curves we consider are defined by y^2 = x^3 - 3x + b mod p,
51 * and a generator for a large subgroup is fixed.
52 */
53typedef struct
54{
55 mpi P; /*!< prime modulus of the base field */
56 mpi B; /*!< constant term in the equation */
57 ecp_point G; /*!< generator of the subgroup used */
58 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010059}
60ecp_group;
61
62/**
63 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
64 *
65 * These also are the NIST-recommended ECP groups, are the random ECP groups
66 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010067 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010068 *
69 * \warning This library does not support validation of arbitrary domain
70 * parameters. Therefore, only well-known domain parameters from trusted
71 * sources (such as the ones below) should be used.
72 */
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010073#define POLARSSL_ECP_SECP192R1_P \
74 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"
75#define POLARSSL_ECP_SECP192R1_B \
76 "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"
77#define POLARSSL_ECP_SECP192R1_GX \
78 "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"
79#define POLARSSL_ECP_SECP192R1_GY \
80 "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"
81#define POLARSSL_ECP_SECP192R1_N \
82 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"
83
84#define POLARSSL_ECP_SECP224R1_P \
85 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"
86#define POLARSSL_ECP_SECP224R1_B \
87 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"
88#define POLARSSL_ECP_SECP224R1_GX \
89 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"
90#define POLARSSL_ECP_SECP224R1_GY \
91 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"
92#define POLARSSL_ECP_SECP224R1_N \
93 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"
94
95#define POLARSSL_ECP_SECP256R1_P \
96 "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"
97#define POLARSSL_ECP_SECP256R1_B \
98 "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"
99#define POLARSSL_ECP_SECP256R1_GX \
100 "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"
101#define POLARSSL_ECP_SECP256R1_GY \
102 "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"
103#define POLARSSL_ECP_SECP256R1_N \
104 "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"
105
106#define POLARSSL_ECP_SECP384R1_P \
107 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
108 "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"
109#define POLARSSL_ECP_SECP384R1_B \
110 "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \
111 "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"
112#define POLARSSL_ECP_SECP384R1_GX \
113 "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \
114 "59F741E082542A385502F25DBF55296C3A545E3872760AB7"
115#define POLARSSL_ECP_SECP384R1_GY \
116 "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \
117 "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"
118#define POLARSSL_ECP_SECP384R1_N \
119 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
120 "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"
121
122#define POLARSSL_ECP_SECP521R1_P \
123 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
124 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
125 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
126#define POLARSSL_ECP_SECP521R1_B \
127 "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \
128 "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \
129 "3BB1BF073573DF883D2C34F1EF451FD46B503F00"
130#define POLARSSL_ECP_SECP521R1_GX \
131 "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \
132 "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \
133 "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66"
134#define POLARSSL_ECP_SECP521R1_GY \
135 "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \
136 "579B446817AFBD17273E662C97EE72995EF42640C550B901" \
137 "3FAD0761353C7086A272C24088BE94769FD16650"
138#define POLARSSL_ECP_SECP521R1_N \
139 "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \
140 "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \
141 "F709A5D03BB5C9B8899C47AEBB6FB71E91386409"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100142
143#ifdef __cplusplus
144extern "C" {
145#endif
146
147/**
148 * \brief Addition: R = P + Q
149 *
150 * \param grp ECP group
151 * \param R Destination point
152 * \param P Left-hand point
153 * \param Q Right-hand point
154 *
155 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
156 */
157int ecp_add( const ecp_group *grp, ecp_point *R,
158 const ecp_point *P, const ecp_point *Q );
159
160/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100161 * \brief Multiplication by an integer: R = m * P
162 *
163 * \param grp ECP group
164 * \param R Destination point
165 * \param m Integer by which to multiply
166 * \param P Point to multiply
167 *
168 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
169 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100170int ecp_mul( const ecp_group *grp, ecp_point *R,
171 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100172
173/**
174 * \brief Free the components of a point
175 */
176void ecp_point_free( ecp_point *pt );
177
178/**
179 * \brief Free the components of an ECP group
180 */
181void ecp_group_free( ecp_group *grp );
182
183/**
184 * \brief Checkup routine
185 *
186 * \return 0 if successful, or 1 if the test failed
187 */
188int ecp_self_test( int verbose );
189
190#ifdef __cplusplus
191}
192#endif
193
194#endif