blob: a249ac565dbd6f9ba27f1fb2241cb7391107e483 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
2 * Elliptic curves over GF(p)
3 *
4 * Copyright (C) 2012, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (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 along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010030 * Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
31 */
32
33#include "polarssl/config.h"
34
35#if defined(POLARSSL_ECP_C)
36
37#include "polarssl/ecp.h"
38
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010039/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010040 * Initialize a point
41 */
42void ecp_point_init( ecp_point *pt )
43{
44 if( pt == NULL )
45 return;
46
47 pt->is_zero = 1;
48 mpi_init( &( pt->X ) );
49 mpi_init( &( pt->Y ) );
50}
51
52/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010053 * Unallocate (the components of) a point
54 */
55void ecp_point_free( ecp_point *pt )
56{
57 if( pt == NULL )
58 return;
59
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010060 pt->is_zero = 1;
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010061 mpi_free( &( pt->X ) );
62 mpi_free( &( pt->Y ) );
63}
64
65/*
66 * Unallocate (the components of) a group
67 */
68void ecp_group_free( ecp_group *grp )
69{
70 if( grp == NULL )
71 return;
72
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010073 mpi_free( &grp->P );
74 mpi_free( &grp->B );
75 ecp_point_free( &grp->G );
76 mpi_free( &grp->N );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +010077}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010078
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010079/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010080 * Set point to zero
81 */
82void ecp_set_zero( ecp_point *pt )
83{
84 pt->is_zero = 1;
85 mpi_free( &( pt->X ) );
86 mpi_free( &( pt->Y ) );
87}
88
89/*
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010090 * Copy the contents of Q into P
91 */
92int ecp_copy( ecp_point *P, const ecp_point *Q )
93{
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010094 int ret = 0;
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010095
96 P->is_zero = Q->is_zero;
97 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
98 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
99
100cleanup:
101 return( ret );
102}
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100103
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100104/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100105 * Import a non-zero point from ASCII strings
106 */
107int ecp_point_read_string( ecp_point *P, int radix,
108 const char *x, const char *y )
109{
110 int ret = 0;
111
112 P->is_zero = 0;
113 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
114 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
115
116cleanup:
117 return( ret );
118}
119
120/*
121 * Import an ECP group from ASCII strings
122 */
123int ecp_group_read_string( ecp_group *grp, int radix,
124 const char *p, const char *b,
125 const char *gx, const char *gy, const char *n)
126{
127 int ret = 0;
128
129 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
130 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
131 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
132 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
133
134cleanup:
135 return( ret );
136}
137
138/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100139 * Addition: R = P + Q, generic case (P != Q, P != 0, Q != 0, R != 0)
140 * Cf SEC1 v2 p. 7, item 4
141 */
142static int ecp_add_generic( const ecp_group *grp, ecp_point *R,
143 const ecp_point *P, const ecp_point *Q )
144{
145 int ret = 0;
146 mpi DX, DY, K, L, LL, M, X, Y;
147
148 mpi_init( &DX ); mpi_init( &DY ); mpi_init( &K ); mpi_init( &L );
149 mpi_init( &LL ); mpi_init( &M ); mpi_init( &X ); mpi_init( &Y );
150
151 /*
152 * L = (Q.Y - P.Y) / (Q.X - P.X) mod p
153 */
154 MPI_CHK( mpi_sub_mpi( &DY, &Q->Y, &P->Y ) );
155 MPI_CHK( mpi_sub_mpi( &DX, &Q->X, &P->X ) );
156 MPI_CHK( mpi_inv_mod( &K, &DX, &grp->P ) );
157 MPI_CHK( mpi_mul_mpi( &K, &K, &DY ) );
158 MPI_CHK( mpi_mod_mpi( &L, &K, &grp->P ) );
159
160 /*
161 * LL = L^2 mod p
162 * M = L^2 - Q.X
163 * X = L^2 - P.X - Q.X
164 */
165 MPI_CHK( mpi_mul_mpi( &LL, &L, &L ) );
166 MPI_CHK( mpi_mod_mpi( &LL, &LL, &grp->P ) );
167 MPI_CHK( mpi_sub_mpi( &M, &LL, &Q->X ) );
168 MPI_CHK( mpi_sub_mpi( &X, &M, &P->X ) );
169
170 /*
171 * Y = L * (P.X - X) - P.Y = L * (-M) - P.Y
172 */
173 MPI_CHK( mpi_copy( &Y, &M ) );
174 Y.s = - Y.s;
175 MPI_CHK( mpi_mul_mpi( &Y, &Y, &L ) );
176 MPI_CHK( mpi_sub_mpi( &Y, &Y, &P->Y ) );
177
178 /*
179 * R = (X mod p, Y mod p)
180 */
181 R->is_zero = 0;
182 MPI_CHK( mpi_mod_mpi( &R->X, &X, &grp->P ) );
183 MPI_CHK( mpi_mod_mpi( &R->Y, &Y, &grp->P ) );
184
185cleanup:
186
187 mpi_free( &DX ); mpi_free( &DY ); mpi_free( &K ); mpi_free( &L );
188 mpi_free( &LL ); mpi_free( &M ); mpi_free( &X ); mpi_free( &Y );
189
190 return( ret );
191}
192
193/*
194 * Doubling: R = 2 * P, generic case (P != 0, R != 0)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100195 */
196static int ecp_double_generic( const ecp_group *grp, ecp_point *R,
197 const ecp_point *P )
198{
199 int ret = 0;
200
201 (void) grp;
202 (void) R;
203 (void) P;
204
205 return( ret );
206}
207
208/*
209 * Addition: R = P + Q, cf p. 7 of SEC1 v2
210 */
211int ecp_add( const ecp_group *grp, ecp_point *R,
212 const ecp_point *P, const ecp_point *Q )
213{
214 int ret = 0;
215
216 if( P->is_zero )
217 {
218 ret = ecp_copy( R, Q );
219 }
220 else if( Q->is_zero )
221 {
222 ret = ecp_copy( R, P );
223 }
224 else if( mpi_cmp_mpi( &P->X, &Q->X ) != 0 )
225 {
226 ret = ecp_add_generic( grp, R, P, Q );
227 }
228 else if( mpi_cmp_int( &P->Y, 0 ) == 0 ||
229 mpi_cmp_mpi( &P->Y, &Q->Y ) != 0 )
230 {
231 ecp_set_zero( R );
232 }
233 else
234 {
235 /*
236 * P == Q
237 */
238 ret = ecp_double_generic( grp, R, P );
239 }
240
241 return ret;
242}
243
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100244
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100245#if defined(POLARSSL_SELF_TEST)
246
247/*
Manuel Pégourié-Gonnardd0dc6312012-11-05 16:28:33 +0100248 * Return true iff P and Q are the same point
249 */
250static int ecp_point_eq( const ecp_point *P, const ecp_point *Q )
251{
252 if( P->is_zero || Q->is_zero )
253 return( P->is_zero && Q->is_zero );
254
255 return( mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
256 mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 );
257}
258
259/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100260 * Checkup routine
Manuel Pégourié-Gonnardd0dc6312012-11-05 16:28:33 +0100261 *
262 * Data gathered from http://danher6.100webspace.net/ecc/#EFp_interactivo
263 * and double-checked using Pari-GP
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100264 */
265int ecp_self_test( int verbose )
266{
Manuel Pégourié-Gonnardd0dc6312012-11-05 16:28:33 +0100267 int ret = 0;
268 size_t i;
269 ecp_group grp;
270 ecp_point O, A, B, C, D, E, F, G, TMP;
271 ecp_point add_table[][3] =
272 {
273 {O, O, O}, {O, A, A}, {A, O, A},
274 {A, A, O}, {B, C, O}, {C, B, O},
275 {A, D, E}, {D, A, E}, {B, D, F}, {D, B, F},
276 {D, D, G},
277 };
278
279 ecp_set_zero( &O );
280 MPI_CHK( ecp_group_read_string( &grp, 10, "47", "4", "17", "42", "13" ) );
281 MPI_CHK( ecp_point_read_string( &A, 10, "13", "0" ) );
282 MPI_CHK( ecp_point_read_string( &B, 10, "14", "11" ) );
283 MPI_CHK( ecp_point_read_string( &C, 10, "14", "36" ) );
284 MPI_CHK( ecp_point_read_string( &D, 10, "37", "31" ) );
285 MPI_CHK( ecp_point_read_string( &E, 10, "34", "14" ) );
286 MPI_CHK( ecp_point_read_string( &F, 10, "45", "7" ) );
287 MPI_CHK( ecp_point_read_string( &E, 10, "21", "32" ) );
288
289 if( verbose != 0 )
290 printf( " ECP test #1 (ecp_add): " );
291
292 for( i = 0; i < sizeof( add_table ) / sizeof( add_table[0] ); i++ )
293 {
294 MPI_CHK( ecp_add( &grp, &TMP, &add_table[i][0], &add_table[i][1] ) );
295 if( ! ecp_point_eq( &TMP, &add_table[i][2] ) )
296 {
297 if( verbose != 0 )
298 printf(" failed (%zu)\n", i);
299
300 return( 1 );
301 }
302 }
303
304cleanup:
305
306 if( ret != 0 && verbose != 0 )
307 printf( "Unexpected error, return code = %08X\n", ret );
308
309 ecp_group_free( &grp );
310 ecp_point_free( &O ); ecp_point_free( &A ); ecp_point_free( &B );
311 ecp_point_free( &C ); ecp_point_free( &D ); ecp_point_free( &E );
312 ecp_point_free( &F ); ecp_point_free( &G );
313
314 if( verbose != 0 )
315 printf( "\n" );
316
317 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100318}
319
320#endif
321
322#endif