blob: e467e37b3b8a570521b76e9845b30e0e4e9bbc5f [file] [log] [blame] [raw]
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010033#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010037
38#if defined(POLARSSL_ECDSA_C)
39
40#include "polarssl/ecdsa.h"
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020041#include "polarssl/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010042
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010043#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010044#include "polarssl/hmac_drbg.h"
45#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010046
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010047#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010048/*
49 * This a hopefully temporary compatibility function.
50 *
51 * Since we can't ensure the caller will pass a valid md_alg before the next
52 * interface change, try to pick up a decent md by size.
53 *
54 * Argument is the minimum size in bytes of the MD output.
55 */
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010056static const md_info_t *md_info_by_size( size_t min_size )
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010057{
58 const md_info_t *md_cur, *md_picked = NULL;
59 const int *md_alg;
60
61 for( md_alg = md_list(); *md_alg != 0; md_alg++ )
62 {
63 if( ( md_cur = md_info_from_type( *md_alg ) ) == NULL ||
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010064 (size_t) md_cur->size < min_size ||
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010065 ( md_picked != NULL && md_cur->size > md_picked->size ) )
66 continue;
67
68 md_picked = md_cur;
69 }
70
71 return( md_picked );
72}
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010073#endif
74
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010075/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010076 * Derive a suitable integer for group grp from a buffer of length len
77 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
78 */
79static int derive_mpi( const ecp_group *grp, mpi *x,
80 const unsigned char *buf, size_t blen )
81{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010082 int ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010083 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010084 size_t use_size = blen > n_size ? n_size : blen;
85
86 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
87 if( use_size * 8 > grp->nbits )
88 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
Manuel Pégourié-Gonnarde7072f82014-01-03 12:55:15 +010089
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010090 /* While at it, reduce modulo N */
91 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
92 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010093
94cleanup:
95 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010096}
97
98/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010099 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
100 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
101 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200102int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100103 const mpi *d, const unsigned char *buf, size_t blen,
104 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
105{
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200106 int ret, key_tries, sign_tries, blind_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100107 ecp_point R;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200108 mpi k, e, t;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100109
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100110 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
111 if( grp->N.p == NULL )
112 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
113
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100114 ecp_point_init( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200115 mpi_init( &k ); mpi_init( &e ); mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100116
117 sign_tries = 0;
118 do
119 {
120 /*
121 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100122 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100123 */
124 key_tries = 0;
125 do
126 {
127 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100128 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100129
130 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200131 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200132 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200133 goto cleanup;
134 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100135 }
136 while( mpi_cmp_int( r, 0 ) == 0 );
137
138 /*
139 * Step 5: derive MPI from hashed message
140 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100141 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100142
143 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200144 * Generate a random value to blind inv_mod in next step,
145 * avoiding a potential timing leak.
146 */
147 blind_tries = 0;
148 do
149 {
150 size_t n_size = (grp->nbits + 7) / 8;
151 MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) );
152 MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
153
154 /* See ecp_gen_keypair() */
155 if( ++blind_tries > 30 )
156 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
157 }
158 while( mpi_cmp_int( &t, 1 ) < 0 ||
159 mpi_cmp_mpi( &t, &grp->N ) >= 0 );
160
161 /*
162 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100163 */
164 MPI_CHK( mpi_mul_mpi( s, r, d ) );
165 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200166 MPI_CHK( mpi_mul_mpi( &e, &e, &t ) );
167 MPI_CHK( mpi_mul_mpi( &k, &k, &t ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100168 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
169 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
170 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
171
172 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200173 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200174 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200175 goto cleanup;
176 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100177 }
178 while( mpi_cmp_int( s, 0 ) == 0 );
179
180cleanup:
181 ecp_point_free( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200182 mpi_free( &k ); mpi_free( &e ); mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100183
184 return( ret );
185}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100186
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100187#if defined(POLARSSL_ECDSA_DETERMINISTIC)
188/*
189 * Deterministic signature wrapper
190 */
191int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
192 const mpi *d, const unsigned char *buf, size_t blen,
193 md_type_t md_alg )
194{
195 int ret;
196 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100197 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100198 size_t grp_len = ( grp->nbits + 7 ) / 8;
199 const md_info_t *md_info;
200 mpi h;
201
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100202 /* Temporary fallback */
203 if( md_alg == POLARSSL_MD_NONE )
204 md_info = md_info_by_size( blen );
205 else
206 md_info = md_info_from_type( md_alg );
207
208 if( md_info == NULL )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100209 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
210
211 mpi_init( &h );
212 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
213
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100214 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
215 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100216 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100217 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100218 hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100219
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100220 ret = ecdsa_sign( grp, r, s, d, buf, blen,
221 hmac_drbg_random, &rng_ctx );
222
223cleanup:
224 hmac_drbg_free( &rng_ctx );
225 mpi_free( &h );
226
227 return( ret );
228}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100229#endif /* POLARSSL_ECDSA_DETERMINISTIC */
230
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100231/*
232 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
233 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
234 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200235int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100236 const unsigned char *buf, size_t blen,
237 const ecp_point *Q, const mpi *r, const mpi *s)
238{
239 int ret;
240 mpi e, s_inv, u1, u2;
241 ecp_point R, P;
242
243 ecp_point_init( &R ); ecp_point_init( &P );
244 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
245
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100246 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
247 if( grp->N.p == NULL )
248 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
249
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100250 /*
251 * Step 1: make sure r and s are in range 1..n-1
252 */
253 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
254 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
255 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200256 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200257 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100258 }
259
260 /*
261 * Additional precaution: make sure Q is valid
262 */
263 MPI_CHK( ecp_check_pubkey( grp, Q ) );
264
265 /*
266 * Step 3: derive MPI from hashed message
267 */
268 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
269
270 /*
271 * Step 4: u1 = e / s mod n, u2 = r / s mod n
272 */
273 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
274
275 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
276 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
277
278 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
279 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
280
281 /*
282 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200283 *
284 * Since we're not using any secret data, no need to pass a RNG to
285 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100286 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200287 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
288 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100289 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
290
291 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200292 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200293 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200294 goto cleanup;
295 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100296
297 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100298 * Step 6: convert xR to an integer (no-op)
299 * Step 7: reduce xR mod n (gives v)
300 */
301 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
302
303 /*
304 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100305 */
306 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200307 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200308 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200309 goto cleanup;
310 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100311
312cleanup:
313 ecp_point_free( &R ); ecp_point_free( &P );
314 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
315
316 return( ret );
317}
318
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200319/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200320 * RFC 4492 page 20:
321 *
322 * Ecdsa-Sig-Value ::= SEQUENCE {
323 * r INTEGER,
324 * s INTEGER
325 * }
326 *
327 * Size is at most
328 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
329 * twice that + 1 (tag) + 2 (len) for the sequence
330 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
331 * and less than 124 (total len <= 255) for the sequence)
332 */
333#if POLARSSL_ECP_MAX_BYTES > 124
334#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
335#endif
336#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
337
338/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100339 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200340 */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100341static int ecdsa_signature_to_asn1( ecdsa_context *ctx,
342 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200343{
344 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200345 unsigned char buf[MAX_SIG_LEN];
346 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200347 size_t len = 0;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200348
349 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
350 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
351
352 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
353 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
354 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
355
356 memcpy( sig, p, len );
357 *slen = len;
358
359 return( 0 );
360}
361
362/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100363 * Compute and write signature
364 */
365int ecdsa_write_signature( ecdsa_context *ctx,
366 const unsigned char *hash, size_t hlen,
367 unsigned char *sig, size_t *slen,
368 int (*f_rng)(void *, unsigned char *, size_t),
369 void *p_rng )
370{
371 int ret;
372
373 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
374 hash, hlen, f_rng, p_rng ) ) != 0 )
375 {
376 return( ret );
377 }
378
379 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
380}
381
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100382#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100383/*
384 * Compute and write signature deterministically
385 */
386int ecdsa_write_signature_det( ecdsa_context *ctx,
387 const unsigned char *hash, size_t hlen,
388 unsigned char *sig, size_t *slen,
389 md_type_t md_alg )
390{
391 int ret;
392
393 if( ( ret = ecdsa_sign_det( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
394 hash, hlen, md_alg ) ) != 0 )
395 {
396 return( ret );
397 }
398
399 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
400}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100401#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100402
403/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200404 * Read and check signature
405 */
406int ecdsa_read_signature( ecdsa_context *ctx,
407 const unsigned char *hash, size_t hlen,
408 const unsigned char *sig, size_t slen )
409{
410 int ret;
411 unsigned char *p = (unsigned char *) sig;
412 const unsigned char *end = sig + slen;
413 size_t len;
414
415 if( ( ret = asn1_get_tag( &p, end, &len,
416 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
417 {
418 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
419 }
420
421 if( p + len != end )
422 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
423 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
424
425 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
426 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
427 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
428
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200429 if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen,
430 &ctx->Q, &ctx->r, &ctx->s ) ) != 0 )
431 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200432
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200433 if( p != end )
434 return( POLARSSL_ERR_ECP_SIG_LEN_MISMATCH );
435
436 return( 0 );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200437}
438
439/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200440 * Generate key pair
441 */
442int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
443 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
444{
445 return( ecp_use_known_dp( &ctx->grp, gid ) ||
446 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
447}
448
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200449/*
450 * Set context from an ecp_keypair
451 */
452int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
453{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100454 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200455
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100456 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
457 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
458 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
459 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200460 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100461 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200462
463 return( ret );
464}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200465
466/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200467 * Initialize context
468 */
469void ecdsa_init( ecdsa_context *ctx )
470{
471 ecp_group_init( &ctx->grp );
472 mpi_init( &ctx->d );
473 ecp_point_init( &ctx->Q );
474 mpi_init( &ctx->r );
475 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200476}
477
478/*
479 * Free context
480 */
481void ecdsa_free( ecdsa_context *ctx )
482{
483 ecp_group_free( &ctx->grp );
484 mpi_free( &ctx->d );
485 ecp_point_free( &ctx->Q );
486 mpi_free( &ctx->r );
487 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200488}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100489
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100490#if defined(POLARSSL_SELF_TEST)
491
492/*
493 * Checkup routine
494 */
495int ecdsa_self_test( int verbose )
496{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100497 ((void) verbose );
498 return( 0 );
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100499}
500
501#endif
502
503#endif /* defined(POLARSSL_ECDSA_C) */