blob: e95b80245e2eba6d22596b5c9932efb1005d2529 [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é-Gonnard2aea1412013-01-26 16:33:44 +010030#include "polarssl/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
37#include "polarssl/ecdsa.h"
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020038#include "polarssl/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é-Gonnard7845fc02014-01-27 14:24:03 +010043#include "polarssl/hmac_drbg.h"
44#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010045
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010046#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010047/*
48 * This a hopefully temporary compatibility function.
49 *
50 * Since we can't ensure the caller will pass a valid md_alg before the next
51 * interface change, try to pick up a decent md by size.
52 *
53 * Argument is the minimum size in bytes of the MD output.
54 */
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010055static const md_info_t *md_info_by_size( size_t min_size )
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010056{
57 const md_info_t *md_cur, *md_picked = NULL;
58 const int *md_alg;
59
60 for( md_alg = md_list(); *md_alg != 0; md_alg++ )
61 {
Manuel Pégourié-Gonnarda2733712015-02-10 17:32:14 +010062 if( ( md_cur = md_info_from_type( (md_type_t) *md_alg ) ) == NULL ||
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010063 (size_t) md_cur->size < min_size ||
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010064 ( md_picked != NULL && md_cur->size > md_picked->size ) )
65 continue;
66
67 md_picked = md_cur;
68 }
69
70 return( md_picked );
71}
Paul Bakker9af723c2014-05-01 13:03:14 +020072#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010073
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010074/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010075 * Derive a suitable integer for group grp from a buffer of length len
76 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
77 */
78static int derive_mpi( const ecp_group *grp, mpi *x,
79 const unsigned char *buf, size_t blen )
80{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010081 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +020082 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010083 size_t use_size = blen > n_size ? n_size : blen;
84
85 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
86 if( use_size * 8 > grp->nbits )
87 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
88
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010089 /* While at it, reduce modulo N */
90 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
91 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
92
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010093cleanup:
94 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010095}
96
97/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010098 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
99 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
100 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200101int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100102 const mpi *d, const unsigned char *buf, size_t blen,
103 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
104{
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200105 int ret, key_tries, sign_tries, blind_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100106 ecp_point R;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200107 mpi k, e, t;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100108
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100109 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
110 if( grp->N.p == NULL )
111 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
112
Darryl Greenfdac76f2017-11-20 15:53:43 +0000113 /* Make sure d is in range 1..n-1 */
114 if( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 )
115 return( POLARSSL_ERR_ECP_INVALID_KEY );
116
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100117 ecp_point_init( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200118 mpi_init( &k ); mpi_init( &e ); mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100119
120 sign_tries = 0;
121 do
122 {
123 /*
124 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100125 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100126 */
127 key_tries = 0;
128 do
129 {
130 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100131 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100132
133 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200134 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200135 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200136 goto cleanup;
137 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100138 }
139 while( mpi_cmp_int( r, 0 ) == 0 );
140
141 /*
142 * Step 5: derive MPI from hashed message
143 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100144 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100145
146 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200147 * Generate a random value to blind inv_mod in next step,
148 * avoiding a potential timing leak.
149 */
150 blind_tries = 0;
151 do
152 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200153 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200154 MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) );
155 MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
156
157 /* See ecp_gen_keypair() */
158 if( ++blind_tries > 30 )
159 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
160 }
161 while( mpi_cmp_int( &t, 1 ) < 0 ||
162 mpi_cmp_mpi( &t, &grp->N ) >= 0 );
163
164 /*
165 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100166 */
167 MPI_CHK( mpi_mul_mpi( s, r, d ) );
168 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200169 MPI_CHK( mpi_mul_mpi( &e, &e, &t ) );
170 MPI_CHK( mpi_mul_mpi( &k, &k, &t ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100171 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
172 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
173 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
174
175 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200176 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200177 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200178 goto cleanup;
179 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100180 }
181 while( mpi_cmp_int( s, 0 ) == 0 );
182
183cleanup:
184 ecp_point_free( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200185 mpi_free( &k ); mpi_free( &e ); mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100186
187 return( ret );
188}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100189
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100190#if defined(POLARSSL_ECDSA_DETERMINISTIC)
191/*
192 * Deterministic signature wrapper
193 */
194int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
195 const mpi *d, const unsigned char *buf, size_t blen,
196 md_type_t md_alg )
197{
198 int ret;
199 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100200 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100201 size_t grp_len = ( grp->nbits + 7 ) / 8;
202 const md_info_t *md_info;
203 mpi h;
204
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100205 /* Temporary fallback */
206 if( md_alg == POLARSSL_MD_NONE )
207 md_info = md_info_by_size( blen );
208 else
209 md_info = md_info_from_type( md_alg );
210
211 if( md_info == NULL )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100212 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
213
214 mpi_init( &h );
215 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
216
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100217 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
218 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100219 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100220 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100221 hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100222
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100223 ret = ecdsa_sign( grp, r, s, d, buf, blen,
224 hmac_drbg_random, &rng_ctx );
225
226cleanup:
227 hmac_drbg_free( &rng_ctx );
228 mpi_free( &h );
229
230 return( ret );
231}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100232#endif /* POLARSSL_ECDSA_DETERMINISTIC */
233
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100234/*
235 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
236 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
237 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200238int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100239 const unsigned char *buf, size_t blen,
240 const ecp_point *Q, const mpi *r, const mpi *s)
241{
242 int ret;
243 mpi e, s_inv, u1, u2;
244 ecp_point R, P;
245
246 ecp_point_init( &R ); ecp_point_init( &P );
247 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
248
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100249 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
250 if( grp->N.p == NULL )
251 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
252
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100253 /*
254 * Step 1: make sure r and s are in range 1..n-1
255 */
256 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
257 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
258 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200259 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200260 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100261 }
262
263 /*
264 * Additional precaution: make sure Q is valid
265 */
266 MPI_CHK( ecp_check_pubkey( grp, Q ) );
267
268 /*
269 * Step 3: derive MPI from hashed message
270 */
271 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
272
273 /*
274 * Step 4: u1 = e / s mod n, u2 = r / s mod n
275 */
276 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
277
278 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
279 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
280
281 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
282 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
283
284 /*
285 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200286 *
287 * Since we're not using any secret data, no need to pass a RNG to
288 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100289 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200290 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
291 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100292 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
293
294 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200295 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200296 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200297 goto cleanup;
298 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100299
300 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100301 * Step 6: convert xR to an integer (no-op)
302 * Step 7: reduce xR mod n (gives v)
303 */
304 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
305
306 /*
307 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100308 */
309 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200310 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200311 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200312 goto cleanup;
313 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100314
315cleanup:
316 ecp_point_free( &R ); ecp_point_free( &P );
317 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
318
319 return( ret );
320}
321
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200322/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200323 * RFC 4492 page 20:
324 *
325 * Ecdsa-Sig-Value ::= SEQUENCE {
326 * r INTEGER,
327 * s INTEGER
328 * }
329 *
330 * Size is at most
331 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
332 * twice that + 1 (tag) + 2 (len) for the sequence
333 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
334 * and less than 124 (total len <= 255) for the sequence)
335 */
336#if POLARSSL_ECP_MAX_BYTES > 124
337#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
338#endif
Manuel Pégourié-Gonnarde9599792014-11-10 13:43:55 +0100339#define MAX_SIG_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200340
341/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100342 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200343 */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100344static int ecdsa_signature_to_asn1( ecdsa_context *ctx,
345 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200346{
347 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200348 unsigned char buf[MAX_SIG_LEN];
349 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200350 size_t len = 0;
351
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200352 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
353 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
354
355 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
356 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
357 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
358
359 memcpy( sig, p, len );
360 *slen = len;
361
362 return( 0 );
363}
364
365/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100366 * Compute and write signature
367 */
368int ecdsa_write_signature( ecdsa_context *ctx,
369 const unsigned char *hash, size_t hlen,
370 unsigned char *sig, size_t *slen,
371 int (*f_rng)(void *, unsigned char *, size_t),
372 void *p_rng )
373{
374 int ret;
375
376 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
377 hash, hlen, f_rng, p_rng ) ) != 0 )
378 {
379 return( ret );
380 }
381
382 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
383}
384
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100385#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100386/*
387 * Compute and write signature deterministically
388 */
389int ecdsa_write_signature_det( ecdsa_context *ctx,
390 const unsigned char *hash, size_t hlen,
391 unsigned char *sig, size_t *slen,
392 md_type_t md_alg )
393{
394 int ret;
395
396 if( ( ret = ecdsa_sign_det( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
397 hash, hlen, md_alg ) ) != 0 )
398 {
399 return( ret );
400 }
401
402 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
403}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100404#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100405
406/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200407 * Read and check signature
408 */
409int ecdsa_read_signature( ecdsa_context *ctx,
410 const unsigned char *hash, size_t hlen,
411 const unsigned char *sig, size_t slen )
412{
413 int ret;
414 unsigned char *p = (unsigned char *) sig;
415 const unsigned char *end = sig + slen;
416 size_t len;
417
418 if( ( ret = asn1_get_tag( &p, end, &len,
419 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
420 {
421 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
422 }
423
424 if( p + len != end )
425 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
426 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
427
428 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
429 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
430 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
431
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200432 if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen,
433 &ctx->Q, &ctx->r, &ctx->s ) ) != 0 )
434 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200435
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200436 if( p != end )
437 return( POLARSSL_ERR_ECP_SIG_LEN_MISMATCH );
438
439 return( 0 );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200440}
441
442/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200443 * Generate key pair
444 */
445int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
446 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
447{
448 return( ecp_use_known_dp( &ctx->grp, gid ) ||
449 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
450}
451
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200452/*
453 * Set context from an ecp_keypair
454 */
455int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
456{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100457 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200458
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100459 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
460 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
461 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
462 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200463 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100464 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200465
466 return( ret );
467}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200468
469/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200470 * Initialize context
471 */
472void ecdsa_init( ecdsa_context *ctx )
473{
474 ecp_group_init( &ctx->grp );
475 mpi_init( &ctx->d );
476 ecp_point_init( &ctx->Q );
477 mpi_init( &ctx->r );
478 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200479}
480
481/*
482 * Free context
483 */
484void ecdsa_free( ecdsa_context *ctx )
485{
486 ecp_group_free( &ctx->grp );
487 mpi_free( &ctx->d );
488 ecp_point_free( &ctx->Q );
489 mpi_free( &ctx->r );
490 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200491}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100492
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100493#if defined(POLARSSL_SELF_TEST)
494
495/*
496 * Checkup routine
497 */
498int ecdsa_self_test( int verbose )
499{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100500 ((void) verbose );
501 return( 0 );
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100502}
503
Paul Bakker9af723c2014-05-01 13:03:14 +0200504#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100505
Paul Bakker9af723c2014-05-01 13:03:14 +0200506#endif /* POLARSSL_ECDSA_C */