blob: 0bd0e97f7b3fc3f31a80e7f5a77d0fb7e485bf15 [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}
134#endif
135
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100136/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100137 * Derive a suitable integer for group grp from a buffer of length len
138 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
139 */
140static int derive_mpi( const ecp_group *grp, mpi *x,
141 const unsigned char *buf, size_t blen )
142{
Manuel Pégourié-Gonnarde7072f82014-01-03 12:55:15 +0100143 int ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100144 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnarde7072f82014-01-03 12:55:15 +0100145 size_t use_size = blen > n_size ? n_size : blen;
146
147 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
148 if( use_size * 8 > grp->nbits )
149 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
150
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100151 /* While at it, reduce modulo N */
152 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
153 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
154
Manuel Pégourié-Gonnarde7072f82014-01-03 12:55:15 +0100155cleanup:
156 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100157}
158
159/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100160 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
161 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
162 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200163int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100164 const mpi *d, const unsigned char *buf, size_t blen,
165 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
166{
167 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100168 ecp_point R;
169 mpi k, e;
170
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100171 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
172 if( grp->N.p == NULL )
173 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
174
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100175 ecp_point_init( &R );
176 mpi_init( &k );
177 mpi_init( &e );
178
179 sign_tries = 0;
180 do
181 {
182 /*
183 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100184 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100185 */
186 key_tries = 0;
187 do
188 {
189 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100190 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100191
192 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200193 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200194 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200195 goto cleanup;
196 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100197 }
198 while( mpi_cmp_int( r, 0 ) == 0 );
199
200 /*
201 * Step 5: derive MPI from hashed message
202 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100203 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100204
205 /*
206 * Step 6: compute s = (e + r * d) / k mod n
207 */
208 MPI_CHK( mpi_mul_mpi( s, r, d ) );
209 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
210 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
211 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
212 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
213
214 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200215 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200216 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200217 goto cleanup;
218 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100219 }
220 while( mpi_cmp_int( s, 0 ) == 0 );
221
222cleanup:
223 ecp_point_free( &R );
224 mpi_free( &k );
225 mpi_free( &e );
226
227 return( ret );
228}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100229
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100230#if defined(POLARSSL_ECDSA_DETERMINISTIC)
231/*
232 * Deterministic signature wrapper
233 */
234int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
235 const mpi *d, const unsigned char *buf, size_t blen,
236 md_type_t md_alg )
237{
238 int ret;
239 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100240 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100241 size_t grp_len = ( grp->nbits + 7 ) / 8;
242 const md_info_t *md_info;
243 mpi h;
244
245 if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
246 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
247
248 mpi_init( &h );
249 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
250
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100251 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
252 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100253 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100254 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
255 hmac_drbg_init( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100256
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100257 ret = ecdsa_sign( grp, r, s, d, buf, blen,
258 hmac_drbg_random, &rng_ctx );
259
260cleanup:
261 hmac_drbg_free( &rng_ctx );
262 mpi_free( &h );
263
264 return( ret );
265}
266#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100267/*
268 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
269 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
270 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200271int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100272 const unsigned char *buf, size_t blen,
273 const ecp_point *Q, const mpi *r, const mpi *s)
274{
275 int ret;
276 mpi e, s_inv, u1, u2;
277 ecp_point R, P;
278
279 ecp_point_init( &R ); ecp_point_init( &P );
280 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
281
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100282 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
283 if( grp->N.p == NULL )
284 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
285
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100286 /*
287 * Step 1: make sure r and s are in range 1..n-1
288 */
289 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
290 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
291 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200292 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200293 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100294 }
295
296 /*
297 * Additional precaution: make sure Q is valid
298 */
299 MPI_CHK( ecp_check_pubkey( grp, Q ) );
300
301 /*
302 * Step 3: derive MPI from hashed message
303 */
304 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
305
306 /*
307 * Step 4: u1 = e / s mod n, u2 = r / s mod n
308 */
309 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
310
311 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
312 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
313
314 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
315 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
316
317 /*
318 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200319 *
320 * Since we're not using any secret data, no need to pass a RNG to
321 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100322 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200323 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
324 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100325 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
326
327 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200328 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200329 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200330 goto cleanup;
331 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100332
333 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100334 * Step 6: convert xR to an integer (no-op)
335 * Step 7: reduce xR mod n (gives v)
336 */
337 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
338
339 /*
340 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100341 */
342 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200343 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200344 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200345 goto cleanup;
346 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100347
348cleanup:
349 ecp_point_free( &R ); ecp_point_free( &P );
350 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
351
352 return( ret );
353}
354
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200355/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200356 * RFC 4492 page 20:
357 *
358 * Ecdsa-Sig-Value ::= SEQUENCE {
359 * r INTEGER,
360 * s INTEGER
361 * }
362 *
363 * Size is at most
364 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
365 * twice that + 1 (tag) + 2 (len) for the sequence
366 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
367 * and less than 124 (total len <= 255) for the sequence)
368 */
369#if POLARSSL_ECP_MAX_BYTES > 124
370#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
371#endif
372#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
373
374/*
375 * Compute and write signature
376 */
377int ecdsa_write_signature( ecdsa_context *ctx,
378 const unsigned char *hash, size_t hlen,
379 unsigned char *sig, size_t *slen,
380 int (*f_rng)(void *, unsigned char *, size_t),
381 void *p_rng )
382{
383 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200384 unsigned char buf[MAX_SIG_LEN];
385 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200386 size_t len = 0;
387
388 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
389 hash, hlen, f_rng, p_rng ) ) != 0 )
390 {
391 return( ret );
392 }
393
394 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
395 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
396
397 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
398 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
399 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
400
401 memcpy( sig, p, len );
402 *slen = len;
403
404 return( 0 );
405}
406
407/*
408 * Read and check signature
409 */
410int ecdsa_read_signature( ecdsa_context *ctx,
411 const unsigned char *hash, size_t hlen,
412 const unsigned char *sig, size_t slen )
413{
414 int ret;
415 unsigned char *p = (unsigned char *) sig;
416 const unsigned char *end = sig + slen;
417 size_t len;
418
419 if( ( ret = asn1_get_tag( &p, end, &len,
420 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
421 {
422 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
423 }
424
425 if( p + len != end )
426 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
427 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
428
429 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
430 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
431 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
432
433 if( p != end )
434 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
435 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
436
437 return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
438}
439
440/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200441 * Generate key pair
442 */
443int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
444 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
445{
446 return( ecp_use_known_dp( &ctx->grp, gid ) ||
447 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
448}
449
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200450/*
451 * Set context from an ecp_keypair
452 */
453int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
454{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100455 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200456
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100457 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
458 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
459 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
460 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200461 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100462 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200463
464 return( ret );
465}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200466
467/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200468 * Initialize context
469 */
470void ecdsa_init( ecdsa_context *ctx )
471{
472 ecp_group_init( &ctx->grp );
473 mpi_init( &ctx->d );
474 ecp_point_init( &ctx->Q );
475 mpi_init( &ctx->r );
476 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200477}
478
479/*
480 * Free context
481 */
482void ecdsa_free( ecdsa_context *ctx )
483{
484 ecp_group_free( &ctx->grp );
485 mpi_free( &ctx->d );
486 ecp_point_free( &ctx->Q );
487 mpi_free( &ctx->r );
488 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200489}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100490
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100491#if defined(POLARSSL_SELF_TEST)
492
493/*
494 * Checkup routine
495 */
496int ecdsa_self_test( int verbose )
497{
498 return( verbose++ );
499}
500
501#endif
502
503#endif /* defined(POLARSSL_ECDSA_C) */