blob: b4cdfca27b1ae58a7e888a776851ee995a78bc89 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/*
2 * Elliptic curve DSA
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01007 *
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23/*
24 * References:
25 *
26 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
27 */
28
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#else
32#include POLARSSL_CONFIG_FILE
33#endif
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010034
35#if defined(POLARSSL_ECDSA_C)
36
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecdsa.h"
38#include "mbedtls/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010042#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010044#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010045
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010046/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010047 * Derive a suitable integer for group grp from a buffer of length len
48 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
49 */
50static int derive_mpi( const ecp_group *grp, mpi *x,
51 const unsigned char *buf, size_t blen )
52{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010053 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +020054 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010055 size_t use_size = blen > n_size ? n_size : blen;
56
57 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
58 if( use_size * 8 > grp->nbits )
59 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
60
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010061 /* While at it, reduce modulo N */
62 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
63 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
64
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010065cleanup:
66 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010067}
68
69/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010070 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
71 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
72 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020073int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010074 const mpi *d, const unsigned char *buf, size_t blen,
75 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
76{
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +020077 int ret, key_tries, sign_tries, blind_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010078 ecp_point R;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +020079 mpi k, e, t;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010080
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +010081 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
82 if( grp->N.p == NULL )
83 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
84
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010085 ecp_point_init( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +020086 mpi_init( &k ); mpi_init( &e ); mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010087
88 sign_tries = 0;
89 do
90 {
91 /*
92 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +010093 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010094 */
95 key_tries = 0;
96 do
97 {
98 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +010099 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100100
101 if( key_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( r, 0 ) == 0 );
108
109 /*
110 * Step 5: derive MPI from hashed message
111 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100112 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100113
114 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200115 * Generate a random value to blind inv_mod in next step,
116 * avoiding a potential timing leak.
117 */
118 blind_tries = 0;
119 do
120 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200121 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200122 MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) );
123 MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
124
125 /* See ecp_gen_keypair() */
126 if( ++blind_tries > 30 )
127 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
128 }
129 while( mpi_cmp_int( &t, 1 ) < 0 ||
130 mpi_cmp_mpi( &t, &grp->N ) >= 0 );
131
132 /*
133 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100134 */
135 MPI_CHK( mpi_mul_mpi( s, r, d ) );
136 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200137 MPI_CHK( mpi_mul_mpi( &e, &e, &t ) );
138 MPI_CHK( mpi_mul_mpi( &k, &k, &t ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100139 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
140 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
141 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
142
143 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200144 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200145 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200146 goto cleanup;
147 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100148 }
149 while( mpi_cmp_int( s, 0 ) == 0 );
150
151cleanup:
152 ecp_point_free( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200153 mpi_free( &k ); mpi_free( &e ); mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100154
155 return( ret );
156}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100157
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100158#if defined(POLARSSL_ECDSA_DETERMINISTIC)
159/*
160 * Deterministic signature wrapper
161 */
162int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
163 const mpi *d, const unsigned char *buf, size_t blen,
164 md_type_t md_alg )
165{
166 int ret;
167 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100168 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100169 size_t grp_len = ( grp->nbits + 7 ) / 8;
170 const md_info_t *md_info;
171 mpi h;
172
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +0200173 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100174 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
175
176 mpi_init( &h );
177 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
178
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100179 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
180 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100181 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100182 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100183 hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100184
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100185 ret = ecdsa_sign( grp, r, s, d, buf, blen,
186 hmac_drbg_random, &rng_ctx );
187
188cleanup:
189 hmac_drbg_free( &rng_ctx );
190 mpi_free( &h );
191
192 return( ret );
193}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100194#endif /* POLARSSL_ECDSA_DETERMINISTIC */
195
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100196/*
197 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
198 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
199 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200200int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100201 const unsigned char *buf, size_t blen,
202 const ecp_point *Q, const mpi *r, const mpi *s)
203{
204 int ret;
205 mpi e, s_inv, u1, u2;
206 ecp_point R, P;
207
208 ecp_point_init( &R ); ecp_point_init( &P );
209 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
210
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100211 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
212 if( grp->N.p == NULL )
213 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
214
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100215 /*
216 * Step 1: make sure r and s are in range 1..n-1
217 */
218 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
219 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
220 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200221 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200222 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100223 }
224
225 /*
226 * Additional precaution: make sure Q is valid
227 */
228 MPI_CHK( ecp_check_pubkey( grp, Q ) );
229
230 /*
231 * Step 3: derive MPI from hashed message
232 */
233 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
234
235 /*
236 * Step 4: u1 = e / s mod n, u2 = r / s mod n
237 */
238 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
239
240 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
241 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
242
243 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
244 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
245
246 /*
247 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200248 *
249 * Since we're not using any secret data, no need to pass a RNG to
250 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100251 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200252 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
253 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100254 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
255
256 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200257 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200258 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200259 goto cleanup;
260 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100261
262 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100263 * Step 6: convert xR to an integer (no-op)
264 * Step 7: reduce xR mod n (gives v)
265 */
266 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
267
268 /*
269 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100270 */
271 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200272 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200273 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200274 goto cleanup;
275 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100276
277cleanup:
278 ecp_point_free( &R ); ecp_point_free( &P );
279 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
280
281 return( ret );
282}
283
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200284/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100285 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200286 */
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200287static int ecdsa_signature_to_asn1( const mpi *r, const mpi *s,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100288 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200289{
290 int ret;
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +0200291 unsigned char buf[POLARSSL_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200292 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200293 size_t len = 0;
294
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200295 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, s ) );
296 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, r ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200297
298 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
299 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
300 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
301
302 memcpy( sig, p, len );
303 *slen = len;
304
305 return( 0 );
306}
307
308/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100309 * Compute and write signature
310 */
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200311int ecdsa_write_signature( ecdsa_context *ctx, md_type_t md_alg,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100312 const unsigned char *hash, size_t hlen,
313 unsigned char *sig, size_t *slen,
314 int (*f_rng)(void *, unsigned char *, size_t),
315 void *p_rng )
316{
317 int ret;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200318 mpi r, s;
319
320 mpi_init( &r );
321 mpi_init( &s );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100322
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200323#if defined(POLARSSL_ECDSA_DETERMINISTIC)
324 (void) f_rng;
325 (void) p_rng;
326
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200327 MPI_CHK( ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
328 hash, hlen, md_alg ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200329#else
330 (void) md_alg;
331
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200332 MPI_CHK( ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
333 hash, hlen, f_rng, p_rng ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200334#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100335
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200336 MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
337
338cleanup:
339 mpi_free( &r );
340 mpi_free( &s );
341
342 return( ret );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100343}
344
Manuel Pégourié-Gonnardeadda3f2015-04-03 13:14:48 +0200345#if ! defined(POLARSSL_DEPRECATED_REMOVED) && \
346 defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100347int ecdsa_write_signature_det( ecdsa_context *ctx,
348 const unsigned char *hash, size_t hlen,
349 unsigned char *sig, size_t *slen,
350 md_type_t md_alg )
351{
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200352 return( ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200353 NULL, NULL ) );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100354}
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200355#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100356
357/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200358 * Read and check signature
359 */
360int ecdsa_read_signature( ecdsa_context *ctx,
361 const unsigned char *hash, size_t hlen,
362 const unsigned char *sig, size_t slen )
363{
364 int ret;
365 unsigned char *p = (unsigned char *) sig;
366 const unsigned char *end = sig + slen;
367 size_t len;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200368 mpi r, s;
369
370 mpi_init( &r );
371 mpi_init( &s );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200372
373 if( ( ret = asn1_get_tag( &p, end, &len,
374 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
375 {
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200376 ret += POLARSSL_ERR_ECP_BAD_INPUT_DATA;
377 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200378 }
379
380 if( p + len != end )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200381 {
382 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA +
383 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
384 goto cleanup;
385 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200386
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200387 if( ( ret = asn1_get_mpi( &p, end, &r ) ) != 0 ||
388 ( ret = asn1_get_mpi( &p, end, &s ) ) != 0 )
389 {
390 ret += POLARSSL_ERR_ECP_BAD_INPUT_DATA;
391 goto cleanup;
392 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200393
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200394 if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen,
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200395 &ctx->Q, &r, &s ) ) != 0 )
396 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200397
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200398 if( p != end )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200399 ret = POLARSSL_ERR_ECP_SIG_LEN_MISMATCH;
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200400
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200401cleanup:
402 mpi_free( &r );
403 mpi_free( &s );
404
405 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200406}
407
408/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200409 * Generate key pair
410 */
411int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
412 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
413{
414 return( ecp_use_known_dp( &ctx->grp, gid ) ||
415 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
416}
417
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200418/*
419 * Set context from an ecp_keypair
420 */
421int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
422{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100423 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200424
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100425 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
426 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
427 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
428 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200429 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100430 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200431
432 return( ret );
433}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200434
435/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200436 * Initialize context
437 */
438void ecdsa_init( ecdsa_context *ctx )
439{
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200440 ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200441}
442
443/*
444 * Free context
445 */
446void ecdsa_free( ecdsa_context *ctx )
447{
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200448 ecp_keypair_free( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200449}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100450
Paul Bakker9af723c2014-05-01 13:03:14 +0200451#endif /* POLARSSL_ECDSA_C */