blob: 13f394bc89d16f0c89bcf1418f21f0a754c6589c [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{
46 size_t n_size = (grp->nbits + 7) / 8;
47 return( mpi_read_binary( x, buf, blen > n_size ? n_size : blen ) );
48}
49
50/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010051 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
52 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
53 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020054int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010055 const mpi *d, const unsigned char *buf, size_t blen,
56 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
57{
58 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010059 ecp_point R;
60 mpi k, e;
61
62 ecp_point_init( &R );
63 mpi_init( &k );
64 mpi_init( &e );
65
66 sign_tries = 0;
67 do
68 {
69 /*
70 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +010071 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010072 */
73 key_tries = 0;
74 do
75 {
76 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +010077 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010078
79 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +020080 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +020081 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +020082 goto cleanup;
83 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010084 }
85 while( mpi_cmp_int( r, 0 ) == 0 );
86
87 /*
88 * Step 5: derive MPI from hashed message
89 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010090 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010091
92 /*
93 * Step 6: compute s = (e + r * d) / k mod n
94 */
95 MPI_CHK( mpi_mul_mpi( s, r, d ) );
96 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
97 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
98 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
99 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
100
101 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200102 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200103 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200104 goto cleanup;
105 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100106 }
107 while( mpi_cmp_int( s, 0 ) == 0 );
108
109cleanup:
110 ecp_point_free( &R );
111 mpi_free( &k );
112 mpi_free( &e );
113
114 return( ret );
115}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100116
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100117/*
118 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
119 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
120 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200121int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100122 const unsigned char *buf, size_t blen,
123 const ecp_point *Q, const mpi *r, const mpi *s)
124{
125 int ret;
126 mpi e, s_inv, u1, u2;
127 ecp_point R, P;
128
129 ecp_point_init( &R ); ecp_point_init( &P );
130 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
131
132 /*
133 * Step 1: make sure r and s are in range 1..n-1
134 */
135 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
136 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
137 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200138 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200139 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100140 }
141
142 /*
143 * Additional precaution: make sure Q is valid
144 */
145 MPI_CHK( ecp_check_pubkey( grp, Q ) );
146
147 /*
148 * Step 3: derive MPI from hashed message
149 */
150 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
151
152 /*
153 * Step 4: u1 = e / s mod n, u2 = r / s mod n
154 */
155 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
156
157 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
158 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
159
160 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
161 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
162
163 /*
164 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200165 *
166 * Since we're not using any secret data, no need to pass a RNG to
167 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100168 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200169 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
170 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100171 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
172
173 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200174 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200175 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200176 goto cleanup;
177 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100178
179 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100180 * Step 6: convert xR to an integer (no-op)
181 * Step 7: reduce xR mod n (gives v)
182 */
183 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
184
185 /*
186 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100187 */
188 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200189 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200190 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200191 goto cleanup;
192 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100193
194cleanup:
195 ecp_point_free( &R ); ecp_point_free( &P );
196 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
197
198 return( ret );
199}
200
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200201/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200202 * RFC 4492 page 20:
203 *
204 * Ecdsa-Sig-Value ::= SEQUENCE {
205 * r INTEGER,
206 * s INTEGER
207 * }
208 *
209 * Size is at most
210 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
211 * twice that + 1 (tag) + 2 (len) for the sequence
212 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
213 * and less than 124 (total len <= 255) for the sequence)
214 */
215#if POLARSSL_ECP_MAX_BYTES > 124
216#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
217#endif
218#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
219
220/*
221 * Compute and write signature
222 */
223int ecdsa_write_signature( ecdsa_context *ctx,
224 const unsigned char *hash, size_t hlen,
225 unsigned char *sig, size_t *slen,
226 int (*f_rng)(void *, unsigned char *, size_t),
227 void *p_rng )
228{
229 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200230 unsigned char buf[MAX_SIG_LEN];
231 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200232 size_t len = 0;
233
234 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
235 hash, hlen, f_rng, p_rng ) ) != 0 )
236 {
237 return( ret );
238 }
239
240 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
241 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
242
243 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
244 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
245 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
246
247 memcpy( sig, p, len );
248 *slen = len;
249
250 return( 0 );
251}
252
253/*
254 * Read and check signature
255 */
256int ecdsa_read_signature( ecdsa_context *ctx,
257 const unsigned char *hash, size_t hlen,
258 const unsigned char *sig, size_t slen )
259{
260 int ret;
261 unsigned char *p = (unsigned char *) sig;
262 const unsigned char *end = sig + slen;
263 size_t len;
264
265 if( ( ret = asn1_get_tag( &p, end, &len,
266 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
267 {
268 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
269 }
270
271 if( p + len != end )
272 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
273 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
274
275 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
276 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
277 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
278
279 if( p != end )
280 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
281 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
282
283 return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
284}
285
286/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200287 * Generate key pair
288 */
289int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
290 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
291{
292 return( ecp_use_known_dp( &ctx->grp, gid ) ||
293 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
294}
295
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200296/*
297 * Set context from an ecp_keypair
298 */
299int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
300{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100301 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200302
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100303 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
304 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
305 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
306 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200307 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100308 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200309
310 return( ret );
311}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200312
313/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200314 * Initialize context
315 */
316void ecdsa_init( ecdsa_context *ctx )
317{
318 ecp_group_init( &ctx->grp );
319 mpi_init( &ctx->d );
320 ecp_point_init( &ctx->Q );
321 mpi_init( &ctx->r );
322 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200323}
324
325/*
326 * Free context
327 */
328void ecdsa_free( ecdsa_context *ctx )
329{
330 ecp_group_free( &ctx->grp );
331 mpi_free( &ctx->d );
332 ecp_point_free( &ctx->Q );
333 mpi_free( &ctx->r );
334 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200335}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100336
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100337#if defined(POLARSSL_SELF_TEST)
338
339/*
340 * Checkup routine
341 */
342int ecdsa_self_test( int verbose )
343{
344 return( verbose++ );
345}
346
347#endif
348
349#endif /* defined(POLARSSL_ECDSA_C) */