blob: 4415cef3dfe12943221fab0bdbeed563a032f34b [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24/*
25 * References:
26 *
27 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
28 */
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010035
36#if defined(POLARSSL_ECDSA_C)
37
38#include "polarssl/ecdsa.h"
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020039#include "polarssl/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010040
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010041#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010042#include "polarssl/hmac_drbg.h"
43#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010044
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010045#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010046/*
47 * This a hopefully temporary compatibility function.
48 *
49 * Since we can't ensure the caller will pass a valid md_alg before the next
50 * interface change, try to pick up a decent md by size.
51 *
52 * Argument is the minimum size in bytes of the MD output.
53 */
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010054static const md_info_t *md_info_by_size( size_t min_size )
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010055{
56 const md_info_t *md_cur, *md_picked = NULL;
57 const int *md_alg;
58
59 for( md_alg = md_list(); *md_alg != 0; md_alg++ )
60 {
61 if( ( md_cur = md_info_from_type( *md_alg ) ) == NULL ||
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010062 (size_t) md_cur->size < min_size ||
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010063 ( md_picked != NULL && md_cur->size > md_picked->size ) )
64 continue;
65
66 md_picked = md_cur;
67 }
68
69 return( md_picked );
70}
Paul Bakker9af723c2014-05-01 13:03:14 +020071#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010072
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010073/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010074 * Derive a suitable integer for group grp from a buffer of length len
75 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
76 */
77static int derive_mpi( const ecp_group *grp, mpi *x,
78 const unsigned char *buf, size_t blen )
79{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010080 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +020081 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010082 size_t use_size = blen > n_size ? n_size : blen;
83
84 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
85 if( use_size * 8 > grp->nbits )
86 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
87
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010088 /* While at it, reduce modulo N */
89 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
90 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
91
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010092cleanup:
93 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010094}
95
96/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010097 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
98 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
99 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200100int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100101 const mpi *d, const unsigned char *buf, size_t blen,
102 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
103{
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200104 int ret, key_tries, sign_tries, blind_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100105 ecp_point R;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200106 mpi k, e, t;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100107
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100108 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
109 if( grp->N.p == NULL )
110 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
111
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100112 ecp_point_init( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200113 mpi_init( &k ); mpi_init( &e ); mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100114
115 sign_tries = 0;
116 do
117 {
118 /*
119 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100120 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100121 */
122 key_tries = 0;
123 do
124 {
125 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100126 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100127
128 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200129 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200130 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200131 goto cleanup;
132 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100133 }
134 while( mpi_cmp_int( r, 0 ) == 0 );
135
136 /*
137 * Step 5: derive MPI from hashed message
138 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100139 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100140
141 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200142 * Generate a random value to blind inv_mod in next step,
143 * avoiding a potential timing leak.
144 */
145 blind_tries = 0;
146 do
147 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200148 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200149 MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) );
150 MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
151
152 /* See ecp_gen_keypair() */
153 if( ++blind_tries > 30 )
154 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
155 }
156 while( mpi_cmp_int( &t, 1 ) < 0 ||
157 mpi_cmp_mpi( &t, &grp->N ) >= 0 );
158
159 /*
160 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100161 */
162 MPI_CHK( mpi_mul_mpi( s, r, d ) );
163 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200164 MPI_CHK( mpi_mul_mpi( &e, &e, &t ) );
165 MPI_CHK( mpi_mul_mpi( &k, &k, &t ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100166 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
167 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
168 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
169
170 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200171 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200172 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200173 goto cleanup;
174 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100175 }
176 while( mpi_cmp_int( s, 0 ) == 0 );
177
178cleanup:
179 ecp_point_free( &R );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200180 mpi_free( &k ); mpi_free( &e ); mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100181
182 return( ret );
183}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100184
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100185#if defined(POLARSSL_ECDSA_DETERMINISTIC)
186/*
187 * Deterministic signature wrapper
188 */
189int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
190 const mpi *d, const unsigned char *buf, size_t blen,
191 md_type_t md_alg )
192{
193 int ret;
194 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100195 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100196 size_t grp_len = ( grp->nbits + 7 ) / 8;
197 const md_info_t *md_info;
198 mpi h;
199
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100200 /* Temporary fallback */
201 if( md_alg == POLARSSL_MD_NONE )
202 md_info = md_info_by_size( blen );
203 else
204 md_info = md_info_from_type( md_alg );
205
206 if( md_info == NULL )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100207 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
208
209 mpi_init( &h );
210 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
211
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100212 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
213 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100214 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100215 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100216 hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100217
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100218 ret = ecdsa_sign( grp, r, s, d, buf, blen,
219 hmac_drbg_random, &rng_ctx );
220
221cleanup:
222 hmac_drbg_free( &rng_ctx );
223 mpi_free( &h );
224
225 return( ret );
226}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100227#endif /* POLARSSL_ECDSA_DETERMINISTIC */
228
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100229/*
230 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
231 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
232 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200233int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100234 const unsigned char *buf, size_t blen,
235 const ecp_point *Q, const mpi *r, const mpi *s)
236{
237 int ret;
238 mpi e, s_inv, u1, u2;
239 ecp_point R, P;
240
241 ecp_point_init( &R ); ecp_point_init( &P );
242 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
243
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100244 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
245 if( grp->N.p == NULL )
246 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
247
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100248 /*
249 * Step 1: make sure r and s are in range 1..n-1
250 */
251 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
252 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
253 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200254 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200255 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100256 }
257
258 /*
259 * Additional precaution: make sure Q is valid
260 */
261 MPI_CHK( ecp_check_pubkey( grp, Q ) );
262
263 /*
264 * Step 3: derive MPI from hashed message
265 */
266 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
267
268 /*
269 * Step 4: u1 = e / s mod n, u2 = r / s mod n
270 */
271 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
272
273 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
274 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
275
276 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
277 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
278
279 /*
280 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200281 *
282 * Since we're not using any secret data, no need to pass a RNG to
283 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100284 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200285 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
286 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100287 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
288
289 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200290 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200291 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200292 goto cleanup;
293 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100294
295 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100296 * Step 6: convert xR to an integer (no-op)
297 * Step 7: reduce xR mod n (gives v)
298 */
299 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
300
301 /*
302 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100303 */
304 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200305 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200306 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200307 goto cleanup;
308 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100309
310cleanup:
311 ecp_point_free( &R ); ecp_point_free( &P );
312 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
313
314 return( ret );
315}
316
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200317/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200318 * RFC 4492 page 20:
319 *
320 * Ecdsa-Sig-Value ::= SEQUENCE {
321 * r INTEGER,
322 * s INTEGER
323 * }
324 *
325 * Size is at most
326 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
327 * twice that + 1 (tag) + 2 (len) for the sequence
328 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
329 * and less than 124 (total len <= 255) for the sequence)
330 */
331#if POLARSSL_ECP_MAX_BYTES > 124
332#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
333#endif
Manuel Pégourié-Gonnarde9599792014-11-10 13:43:55 +0100334#define MAX_SIG_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200335
336/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100337 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200338 */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100339static int ecdsa_signature_to_asn1( ecdsa_context *ctx,
340 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200341{
342 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200343 unsigned char buf[MAX_SIG_LEN];
344 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200345 size_t len = 0;
346
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200347 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
348 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
349
350 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
351 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
352 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
353
354 memcpy( sig, p, len );
355 *slen = len;
356
357 return( 0 );
358}
359
360/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100361 * Compute and write signature
362 */
363int ecdsa_write_signature( ecdsa_context *ctx,
364 const unsigned char *hash, size_t hlen,
365 unsigned char *sig, size_t *slen,
366 int (*f_rng)(void *, unsigned char *, size_t),
367 void *p_rng )
368{
369 int ret;
370
371 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
372 hash, hlen, f_rng, p_rng ) ) != 0 )
373 {
374 return( ret );
375 }
376
377 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
378}
379
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100380#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100381/*
382 * Compute and write signature deterministically
383 */
384int ecdsa_write_signature_det( ecdsa_context *ctx,
385 const unsigned char *hash, size_t hlen,
386 unsigned char *sig, size_t *slen,
387 md_type_t md_alg )
388{
389 int ret;
390
391 if( ( ret = ecdsa_sign_det( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
392 hash, hlen, md_alg ) ) != 0 )
393 {
394 return( ret );
395 }
396
397 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
398}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100399#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100400
401/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200402 * Read and check signature
403 */
404int ecdsa_read_signature( ecdsa_context *ctx,
405 const unsigned char *hash, size_t hlen,
406 const unsigned char *sig, size_t slen )
407{
408 int ret;
409 unsigned char *p = (unsigned char *) sig;
410 const unsigned char *end = sig + slen;
411 size_t len;
412
413 if( ( ret = asn1_get_tag( &p, end, &len,
414 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
415 {
416 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
417 }
418
419 if( p + len != end )
420 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
421 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
422
423 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
424 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
425 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
426
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200427 if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen,
428 &ctx->Q, &ctx->r, &ctx->s ) ) != 0 )
429 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200430
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200431 if( p != end )
432 return( POLARSSL_ERR_ECP_SIG_LEN_MISMATCH );
433
434 return( 0 );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200435}
436
437/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200438 * Generate key pair
439 */
440int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
441 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
442{
443 return( ecp_use_known_dp( &ctx->grp, gid ) ||
444 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
445}
446
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200447/*
448 * Set context from an ecp_keypair
449 */
450int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
451{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100452 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200453
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100454 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
455 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
456 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
457 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200458 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100459 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200460
461 return( ret );
462}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200463
464/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200465 * Initialize context
466 */
467void ecdsa_init( ecdsa_context *ctx )
468{
469 ecp_group_init( &ctx->grp );
470 mpi_init( &ctx->d );
471 ecp_point_init( &ctx->Q );
472 mpi_init( &ctx->r );
473 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200474}
475
476/*
477 * Free context
478 */
479void ecdsa_free( ecdsa_context *ctx )
480{
481 ecp_group_free( &ctx->grp );
482 mpi_free( &ctx->d );
483 ecp_point_free( &ctx->Q );
484 mpi_free( &ctx->r );
485 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200486}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100487
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100488#if defined(POLARSSL_SELF_TEST)
489
490/*
491 * Checkup routine
492 */
493int ecdsa_self_test( int verbose )
494{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100495 ((void) verbose );
496 return( 0 );
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100497}
498
Paul Bakker9af723c2014-05-01 13:03:14 +0200499#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100500
Paul Bakker9af723c2014-05-01 13:03:14 +0200501#endif /* POLARSSL_ECDSA_C */