blob: 6f099943e4f66d2ca8628e48e3fc60e31d27a44e [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/*
2 * Elliptic curve DSA
3 *
4 * Copyright (C) 2006-2013, 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 *
29 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
30 */
31
32#include "polarssl/config.h"
33
34#if defined(POLARSSL_ECDSA_C)
35
36#include "polarssl/ecdsa.h"
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020037#include "polarssl/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010038
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010039/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010040 * Derive a suitable integer for group grp from a buffer of length len
41 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
42 */
43static int derive_mpi( const ecp_group *grp, mpi *x,
44 const unsigned char *buf, size_t blen )
45{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010046 int ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010047 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010048 size_t use_size = blen > n_size ? n_size : blen;
49
50 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
51 if( use_size * 8 > grp->nbits )
52 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
53
54cleanup:
55 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010056}
57
58/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010059 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
60 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
61 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020062int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010063 const mpi *d, const unsigned char *buf, size_t blen,
64 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
65{
66 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010067 ecp_point R;
68 mpi k, e;
69
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +010070 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
71 if( grp->N.p == NULL )
72 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
73
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010074 ecp_point_init( &R );
75 mpi_init( &k );
76 mpi_init( &e );
77
78 sign_tries = 0;
79 do
80 {
81 /*
82 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +010083 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010084 */
85 key_tries = 0;
86 do
87 {
88 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +010089 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010090
91 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +020092 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +020093 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +020094 goto cleanup;
95 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010096 }
97 while( mpi_cmp_int( r, 0 ) == 0 );
98
99 /*
100 * Step 5: derive MPI from hashed message
101 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100102 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100103
104 /*
105 * Step 6: compute s = (e + r * d) / k mod n
106 */
107 MPI_CHK( mpi_mul_mpi( s, r, d ) );
108 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
109 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
110 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
111 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
112
113 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200114 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200115 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200116 goto cleanup;
117 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100118 }
119 while( mpi_cmp_int( s, 0 ) == 0 );
120
121cleanup:
122 ecp_point_free( &R );
123 mpi_free( &k );
124 mpi_free( &e );
125
126 return( ret );
127}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100128
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100129/*
130 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
131 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
132 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200133int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100134 const unsigned char *buf, size_t blen,
135 const ecp_point *Q, const mpi *r, const mpi *s)
136{
137 int ret;
138 mpi e, s_inv, u1, u2;
139 ecp_point R, P;
140
141 ecp_point_init( &R ); ecp_point_init( &P );
142 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
143
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100144 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
145 if( grp->N.p == NULL )
146 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
147
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100148 /*
149 * Step 1: make sure r and s are in range 1..n-1
150 */
151 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
152 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
153 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200154 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200155 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100156 }
157
158 /*
159 * Additional precaution: make sure Q is valid
160 */
161 MPI_CHK( ecp_check_pubkey( grp, Q ) );
162
163 /*
164 * Step 3: derive MPI from hashed message
165 */
166 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
167
168 /*
169 * Step 4: u1 = e / s mod n, u2 = r / s mod n
170 */
171 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
172
173 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
174 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
175
176 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
177 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
178
179 /*
180 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200181 *
182 * Since we're not using any secret data, no need to pass a RNG to
183 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100184 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200185 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
186 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100187 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
188
189 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200190 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200191 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200192 goto cleanup;
193 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100194
195 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100196 * Step 6: convert xR to an integer (no-op)
197 * Step 7: reduce xR mod n (gives v)
198 */
199 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
200
201 /*
202 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100203 */
204 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200205 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200206 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200207 goto cleanup;
208 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100209
210cleanup:
211 ecp_point_free( &R ); ecp_point_free( &P );
212 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
213
214 return( ret );
215}
216
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200217/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200218 * RFC 4492 page 20:
219 *
220 * Ecdsa-Sig-Value ::= SEQUENCE {
221 * r INTEGER,
222 * s INTEGER
223 * }
224 *
225 * Size is at most
226 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
227 * twice that + 1 (tag) + 2 (len) for the sequence
228 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
229 * and less than 124 (total len <= 255) for the sequence)
230 */
231#if POLARSSL_ECP_MAX_BYTES > 124
232#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
233#endif
234#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
235
236/*
237 * Compute and write signature
238 */
239int ecdsa_write_signature( ecdsa_context *ctx,
240 const unsigned char *hash, size_t hlen,
241 unsigned char *sig, size_t *slen,
242 int (*f_rng)(void *, unsigned char *, size_t),
243 void *p_rng )
244{
245 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200246 unsigned char buf[MAX_SIG_LEN];
247 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200248 size_t len = 0;
249
250 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
251 hash, hlen, f_rng, p_rng ) ) != 0 )
252 {
253 return( ret );
254 }
255
256 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
257 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
258
259 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
260 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
261 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
262
263 memcpy( sig, p, len );
264 *slen = len;
265
266 return( 0 );
267}
268
269/*
270 * Read and check signature
271 */
272int ecdsa_read_signature( ecdsa_context *ctx,
273 const unsigned char *hash, size_t hlen,
274 const unsigned char *sig, size_t slen )
275{
276 int ret;
277 unsigned char *p = (unsigned char *) sig;
278 const unsigned char *end = sig + slen;
279 size_t len;
280
281 if( ( ret = asn1_get_tag( &p, end, &len,
282 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
283 {
284 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
285 }
286
287 if( p + len != end )
288 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
289 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
290
291 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
292 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
293 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
294
295 if( p != end )
296 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
297 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
298
299 return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
300}
301
302/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200303 * Generate key pair
304 */
305int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
306 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
307{
308 return( ecp_use_known_dp( &ctx->grp, gid ) ||
309 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
310}
311
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200312/*
313 * Set context from an ecp_keypair
314 */
315int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
316{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100317 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200318
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100319 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
320 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
321 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
322 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200323 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100324 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200325
326 return( ret );
327}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200328
329/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200330 * Initialize context
331 */
332void ecdsa_init( ecdsa_context *ctx )
333{
334 ecp_group_init( &ctx->grp );
335 mpi_init( &ctx->d );
336 ecp_point_init( &ctx->Q );
337 mpi_init( &ctx->r );
338 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200339}
340
341/*
342 * Free context
343 */
344void ecdsa_free( ecdsa_context *ctx )
345{
346 ecp_group_free( &ctx->grp );
347 mpi_free( &ctx->d );
348 ecp_point_free( &ctx->Q );
349 mpi_free( &ctx->r );
350 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200351}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100352
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100353#if defined(POLARSSL_SELF_TEST)
354
355/*
356 * Checkup routine
357 */
358int ecdsa_self_test( int verbose )
359{
360 return( verbose++ );
361}
362
363#endif
364
365#endif /* defined(POLARSSL_ECDSA_C) */