blob: 9f697a1cd587cf9f134b1982e7cbd8e9e1d6e7f0 [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é-Gonnard461d4162014-01-06 10:16:28 +010039#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010040/*
41 * Simplified HMAC_DRBG context.
42 * No reseed counter, no prediction resistance flag.
43 */
44typedef struct
45{
46 md_context_t md_ctx;
47 unsigned char V[POLARSSL_MD_MAX_SIZE];
48 unsigned char K[POLARSSL_MD_MAX_SIZE];
49} hmac_drbg_context;
50
51/*
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +010052 * Simplified HMAC_DRBG update, using optional additional data
53 */
54static void hmac_drbg_update( hmac_drbg_context *ctx,
55 const unsigned char *data, size_t data_len )
56{
57 size_t md_len = ctx->md_ctx.md_info->size;
58 unsigned char rounds = ( data != NULL && data_len != 0 ) ? 2 : 1;
59 unsigned char sep[1];
60
61 for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
62 {
63 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
64 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
65 md_hmac_update( &ctx->md_ctx, sep, 1 );
66 if( rounds == 2 )
67 md_hmac_update( &ctx->md_ctx, data, data_len );
68 md_hmac_finish( &ctx->md_ctx, ctx->K );
69
70 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
71 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
72 md_hmac_finish( &ctx->md_ctx, ctx->V );
73 }
74}
75
76/*
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010077 * Simplified HMAC_DRBG initialisation.
78 *
79 * Uses an entropy buffer rather than callback,
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +010080 * assume personalisation string is included in entropy buffer,
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010081 * assumes md_info is not NULL and valid.
82 */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010083static void hmac_drbg_init( hmac_drbg_context *ctx,
84 const md_info_t * md_info,
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +010085 const unsigned char *data, size_t data_len )
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010086{
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010087 memset( ctx, 0, sizeof( hmac_drbg_context ) );
88 md_init_ctx( &ctx->md_ctx, md_info );
89
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +010090 memset( ctx->V, 0x01, md_info->size );
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010091 /* ctx->K is already 0 */
92
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +010093 hmac_drbg_update( ctx, data, data_len );
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010094}
95
96/*
97 * Simplified HMAC_DRBG random function
98 */
99static int hmac_drbg_random( void *state,
100 unsigned char *output, size_t out_len )
101{
102 hmac_drbg_context *ctx = (hmac_drbg_context *) state;
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100103 size_t md_len = ctx->md_ctx.md_info->size;
104 size_t left = out_len;
105 unsigned char *out = output;
106
107 while( left != 0 )
108 {
109 size_t use_len = left > md_len ? md_len : left;
110
111 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
112 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
113 md_hmac_finish( &ctx->md_ctx, ctx->V );
114
115 memcpy( out, ctx->V, use_len );
116 out += use_len;
117 left -= use_len;
118 }
119
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100120 hmac_drbg_update( ctx, NULL, 0 );
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100121
122 return( 0 );
123}
124
125static void hmac_drbg_free( hmac_drbg_context *ctx )
126{
127 if( ctx == NULL )
128 return;
129
130 md_free_ctx( &ctx->md_ctx );
131
132 memset( ctx, 0, sizeof( hmac_drbg_context ) );
133}
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100134
135/*
136 * This a hopefully temporary compatibility function.
137 *
138 * Since we can't ensure the caller will pass a valid md_alg before the next
139 * interface change, try to pick up a decent md by size.
140 *
141 * Argument is the minimum size in bytes of the MD output.
142 */
Paul Bakker18e9f322014-01-23 16:08:06 +0100143static const md_info_t *md_info_by_size( int min_size )
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100144{
145 const md_info_t *md_cur, *md_picked = NULL;
146 const int *md_alg;
147
148 for( md_alg = md_list(); *md_alg != 0; md_alg++ )
149 {
150 if( ( md_cur = md_info_from_type( *md_alg ) ) == NULL ||
151 md_cur->size < min_size ||
152 ( md_picked != NULL && md_cur->size > md_picked->size ) )
153 continue;
154
155 md_picked = md_cur;
156 }
157
158 return( md_picked );
159}
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100160#endif
161
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100162/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100163 * Derive a suitable integer for group grp from a buffer of length len
164 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
165 */
166static int derive_mpi( const ecp_group *grp, mpi *x,
167 const unsigned char *buf, size_t blen )
168{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100169 int ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100170 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100171 size_t use_size = blen > n_size ? n_size : blen;
172
173 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
174 if( use_size * 8 > grp->nbits )
175 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
176
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100177 /* While at it, reduce modulo N */
178 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
179 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
180
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100181cleanup:
182 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100183}
184
185/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100186 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
187 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
188 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200189int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100190 const mpi *d, const unsigned char *buf, size_t blen,
191 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
192{
193 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100194 ecp_point R;
195 mpi k, e;
196
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100197 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
198 if( grp->N.p == NULL )
199 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
200
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100201 ecp_point_init( &R );
202 mpi_init( &k );
203 mpi_init( &e );
204
205 sign_tries = 0;
206 do
207 {
208 /*
209 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100210 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100211 */
212 key_tries = 0;
213 do
214 {
215 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100216 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100217
218 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200219 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200220 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200221 goto cleanup;
222 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100223 }
224 while( mpi_cmp_int( r, 0 ) == 0 );
225
226 /*
227 * Step 5: derive MPI from hashed message
228 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100229 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100230
231 /*
232 * Step 6: compute s = (e + r * d) / k mod n
233 */
234 MPI_CHK( mpi_mul_mpi( s, r, d ) );
235 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
236 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
237 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
238 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
239
240 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200241 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200242 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200243 goto cleanup;
244 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100245 }
246 while( mpi_cmp_int( s, 0 ) == 0 );
247
248cleanup:
249 ecp_point_free( &R );
250 mpi_free( &k );
251 mpi_free( &e );
252
253 return( ret );
254}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100255
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100256#if defined(POLARSSL_ECDSA_DETERMINISTIC)
257/*
258 * Deterministic signature wrapper
259 */
260int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
261 const mpi *d, const unsigned char *buf, size_t blen,
262 md_type_t md_alg )
263{
264 int ret;
265 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100266 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100267 size_t grp_len = ( grp->nbits + 7 ) / 8;
268 const md_info_t *md_info;
269 mpi h;
270
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100271 /* Temporary fallback */
272 if( md_alg == POLARSSL_MD_NONE )
273 md_info = md_info_by_size( blen );
274 else
275 md_info = md_info_from_type( md_alg );
276
277 if( md_info == NULL )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100278 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
279
280 mpi_init( &h );
281 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
282
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100283 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
284 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100285 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100286 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
287 hmac_drbg_init( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100288
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100289 ret = ecdsa_sign( grp, r, s, d, buf, blen,
290 hmac_drbg_random, &rng_ctx );
291
292cleanup:
293 hmac_drbg_free( &rng_ctx );
294 mpi_free( &h );
295
296 return( ret );
297}
298#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100299/*
300 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
301 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
302 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200303int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100304 const unsigned char *buf, size_t blen,
305 const ecp_point *Q, const mpi *r, const mpi *s)
306{
307 int ret;
308 mpi e, s_inv, u1, u2;
309 ecp_point R, P;
310
311 ecp_point_init( &R ); ecp_point_init( &P );
312 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
313
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100314 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
315 if( grp->N.p == NULL )
316 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
317
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100318 /*
319 * Step 1: make sure r and s are in range 1..n-1
320 */
321 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
322 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
323 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200324 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200325 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100326 }
327
328 /*
329 * Additional precaution: make sure Q is valid
330 */
331 MPI_CHK( ecp_check_pubkey( grp, Q ) );
332
333 /*
334 * Step 3: derive MPI from hashed message
335 */
336 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
337
338 /*
339 * Step 4: u1 = e / s mod n, u2 = r / s mod n
340 */
341 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
342
343 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
344 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
345
346 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
347 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
348
349 /*
350 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200351 *
352 * Since we're not using any secret data, no need to pass a RNG to
353 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100354 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200355 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
356 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100357 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
358
359 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200360 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200361 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200362 goto cleanup;
363 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100364
365 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100366 * Step 6: convert xR to an integer (no-op)
367 * Step 7: reduce xR mod n (gives v)
368 */
369 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
370
371 /*
372 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100373 */
374 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200375 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200376 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200377 goto cleanup;
378 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100379
380cleanup:
381 ecp_point_free( &R ); ecp_point_free( &P );
382 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
383
384 return( ret );
385}
386
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200387/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200388 * RFC 4492 page 20:
389 *
390 * Ecdsa-Sig-Value ::= SEQUENCE {
391 * r INTEGER,
392 * s INTEGER
393 * }
394 *
395 * Size is at most
396 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
397 * twice that + 1 (tag) + 2 (len) for the sequence
398 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
399 * and less than 124 (total len <= 255) for the sequence)
400 */
401#if POLARSSL_ECP_MAX_BYTES > 124
402#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
403#endif
404#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
405
406/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100407 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200408 */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100409static int ecdsa_signature_to_asn1( ecdsa_context *ctx,
410 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200411{
412 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200413 unsigned char buf[MAX_SIG_LEN];
414 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200415 size_t len = 0;
416
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200417 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
418 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
419
420 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
421 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
422 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
423
424 memcpy( sig, p, len );
425 *slen = len;
426
427 return( 0 );
428}
429
430/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100431 * Compute and write signature
432 */
433int ecdsa_write_signature( ecdsa_context *ctx,
434 const unsigned char *hash, size_t hlen,
435 unsigned char *sig, size_t *slen,
436 int (*f_rng)(void *, unsigned char *, size_t),
437 void *p_rng )
438{
439 int ret;
440
441 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
442 hash, hlen, f_rng, p_rng ) ) != 0 )
443 {
444 return( ret );
445 }
446
447 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
448}
449
450/*
451 * Compute and write signature deterministically
452 */
453int ecdsa_write_signature_det( ecdsa_context *ctx,
454 const unsigned char *hash, size_t hlen,
455 unsigned char *sig, size_t *slen,
456 md_type_t md_alg )
457{
458 int ret;
459
460 if( ( ret = ecdsa_sign_det( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
461 hash, hlen, md_alg ) ) != 0 )
462 {
463 return( ret );
464 }
465
466 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
467}
468
469/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200470 * Read and check signature
471 */
472int ecdsa_read_signature( ecdsa_context *ctx,
473 const unsigned char *hash, size_t hlen,
474 const unsigned char *sig, size_t slen )
475{
476 int ret;
477 unsigned char *p = (unsigned char *) sig;
478 const unsigned char *end = sig + slen;
479 size_t len;
480
481 if( ( ret = asn1_get_tag( &p, end, &len,
482 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
483 {
484 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
485 }
486
487 if( p + len != end )
488 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
489 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
490
491 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
492 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
493 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
494
495 if( p != end )
496 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
497 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
498
499 return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
500}
501
502/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200503 * Generate key pair
504 */
505int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
506 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
507{
508 return( ecp_use_known_dp( &ctx->grp, gid ) ||
509 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
510}
511
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200512/*
513 * Set context from an ecp_keypair
514 */
515int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
516{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100517 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200518
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100519 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
520 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
521 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
522 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200523 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100524 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200525
526 return( ret );
527}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200528
529/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200530 * Initialize context
531 */
532void ecdsa_init( ecdsa_context *ctx )
533{
534 ecp_group_init( &ctx->grp );
535 mpi_init( &ctx->d );
536 ecp_point_init( &ctx->Q );
537 mpi_init( &ctx->r );
538 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200539}
540
541/*
542 * Free context
543 */
544void ecdsa_free( ecdsa_context *ctx )
545{
546 ecp_group_free( &ctx->grp );
547 mpi_free( &ctx->d );
548 ecp_point_free( &ctx->Q );
549 mpi_free( &ctx->r );
550 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200551}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100552
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100553#if defined(POLARSSL_SELF_TEST)
554
555/*
556 * Checkup routine
557 */
558int ecdsa_self_test( int verbose )
559{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100560 ((void) verbose );
561 return( 0 );
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100562}
563
564#endif
565
566#endif /* defined(POLARSSL_ECDSA_C) */