blob: 94522a4a6635c1f8352538f87fea9a0a0494516a [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é-Gonnard7845fc02014-01-27 14:24:03 +010040#include "polarssl/hmac_drbg.h"
41#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010042
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010043#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010044/*
45 * This a hopefully temporary compatibility function.
46 *
47 * Since we can't ensure the caller will pass a valid md_alg before the next
48 * interface change, try to pick up a decent md by size.
49 *
50 * Argument is the minimum size in bytes of the MD output.
51 */
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010052static const md_info_t *md_info_by_size( size_t min_size )
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010053{
54 const md_info_t *md_cur, *md_picked = NULL;
55 const int *md_alg;
56
57 for( md_alg = md_list(); *md_alg != 0; md_alg++ )
58 {
59 if( ( md_cur = md_info_from_type( *md_alg ) ) == NULL ||
Manuel Pégourié-Gonnard95924852014-03-21 10:54:55 +010060 (size_t) md_cur->size < min_size ||
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +010061 ( md_picked != NULL && md_cur->size > md_picked->size ) )
62 continue;
63
64 md_picked = md_cur;
65 }
66
67 return( md_picked );
68}
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010069#endif
70
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010071/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010072 * Derive a suitable integer for group grp from a buffer of length len
73 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
74 */
75static int derive_mpi( const ecp_group *grp, mpi *x,
76 const unsigned char *buf, size_t blen )
77{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010078 int ret;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010079 size_t n_size = (grp->nbits + 7) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010080 size_t use_size = blen > n_size ? n_size : blen;
81
82 MPI_CHK( mpi_read_binary( x, buf, use_size ) );
83 if( use_size * 8 > grp->nbits )
84 MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
85
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010086 /* While at it, reduce modulo N */
87 if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
88 MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
89
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +010090cleanup:
91 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010092}
93
94/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010095 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
96 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
97 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020098int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010099 const mpi *d, const unsigned char *buf, size_t blen,
100 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
101{
102 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100103 ecp_point R;
104 mpi k, e;
105
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100106 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
107 if( grp->N.p == NULL )
108 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
109
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100110 ecp_point_init( &R );
111 mpi_init( &k );
112 mpi_init( &e );
113
114 sign_tries = 0;
115 do
116 {
117 /*
118 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100119 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100120 */
121 key_tries = 0;
122 do
123 {
124 MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100125 MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100126
127 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200128 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200129 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200130 goto cleanup;
131 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100132 }
133 while( mpi_cmp_int( r, 0 ) == 0 );
134
135 /*
136 * Step 5: derive MPI from hashed message
137 */
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100138 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100139
140 /*
141 * Step 6: compute s = (e + r * d) / k mod n
142 */
143 MPI_CHK( mpi_mul_mpi( s, r, d ) );
144 MPI_CHK( mpi_add_mpi( &e, &e, s ) );
145 MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
146 MPI_CHK( mpi_mul_mpi( s, s, &e ) );
147 MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
148
149 if( sign_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200150 {
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200151 ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200152 goto cleanup;
153 }
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100154 }
155 while( mpi_cmp_int( s, 0 ) == 0 );
156
157cleanup:
158 ecp_point_free( &R );
159 mpi_free( &k );
160 mpi_free( &e );
161
162 return( ret );
163}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100164
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100165#if defined(POLARSSL_ECDSA_DETERMINISTIC)
166/*
167 * Deterministic signature wrapper
168 */
169int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
170 const mpi *d, const unsigned char *buf, size_t blen,
171 md_type_t md_alg )
172{
173 int ret;
174 hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100175 unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100176 size_t grp_len = ( grp->nbits + 7 ) / 8;
177 const md_info_t *md_info;
178 mpi h;
179
Manuel Pégourié-Gonnard5e6edcf2014-01-07 16:17:53 +0100180 /* Temporary fallback */
181 if( md_alg == POLARSSL_MD_NONE )
182 md_info = md_info_by_size( blen );
183 else
184 md_info = md_info_from_type( md_alg );
185
186 if( md_info == NULL )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100187 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
188
189 mpi_init( &h );
190 memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
191
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100192 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
193 MPI_CHK( mpi_write_binary( d, data, grp_len ) );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100194 MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100195 MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100196 hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100197
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100198 ret = ecdsa_sign( grp, r, s, d, buf, blen,
199 hmac_drbg_random, &rng_ctx );
200
201cleanup:
202 hmac_drbg_free( &rng_ctx );
203 mpi_free( &h );
204
205 return( ret );
206}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100207#endif /* POLARSSL_ECDSA_DETERMINISTIC */
208
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100209/*
210 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
211 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
212 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200213int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100214 const unsigned char *buf, size_t blen,
215 const ecp_point *Q, const mpi *r, const mpi *s)
216{
217 int ret;
218 mpi e, s_inv, u1, u2;
219 ecp_point R, P;
220
221 ecp_point_init( &R ); ecp_point_init( &P );
222 mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
223
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100224 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
225 if( grp->N.p == NULL )
226 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
227
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100228 /*
229 * Step 1: make sure r and s are in range 1..n-1
230 */
231 if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
232 mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
233 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200234 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200235 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100236 }
237
238 /*
239 * Additional precaution: make sure Q is valid
240 */
241 MPI_CHK( ecp_check_pubkey( grp, Q ) );
242
243 /*
244 * Step 3: derive MPI from hashed message
245 */
246 MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
247
248 /*
249 * Step 4: u1 = e / s mod n, u2 = r / s mod n
250 */
251 MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
252
253 MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
254 MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
255
256 MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
257 MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
258
259 /*
260 * Step 5: R = u1 G + u2 Q
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200261 *
262 * Since we're not using any secret data, no need to pass a RNG to
263 * ecp_mul() for countermesures.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100264 */
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200265 MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
266 MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100267 MPI_CHK( ecp_add( grp, &R, &R, &P ) );
268
269 if( ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200270 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200271 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200272 goto cleanup;
273 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100274
275 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100276 * Step 6: convert xR to an integer (no-op)
277 * Step 7: reduce xR mod n (gives v)
278 */
279 MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
280
281 /*
282 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100283 */
284 if( mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200285 {
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +0200286 ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200287 goto cleanup;
288 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100289
290cleanup:
291 ecp_point_free( &R ); ecp_point_free( &P );
292 mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
293
294 return( ret );
295}
296
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200297/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200298 * RFC 4492 page 20:
299 *
300 * Ecdsa-Sig-Value ::= SEQUENCE {
301 * r INTEGER,
302 * s INTEGER
303 * }
304 *
305 * Size is at most
306 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
307 * twice that + 1 (tag) + 2 (len) for the sequence
308 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
309 * and less than 124 (total len <= 255) for the sequence)
310 */
311#if POLARSSL_ECP_MAX_BYTES > 124
312#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix MAX_SIG_LEN"
313#endif
314#define MAX_SIG_LEN ( 3 + 2 * ( 2 + POLARSSL_ECP_MAX_BYTES ) )
315
316/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100317 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200318 */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100319static int ecdsa_signature_to_asn1( ecdsa_context *ctx,
320 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200321{
322 int ret;
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200323 unsigned char buf[MAX_SIG_LEN];
324 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200325 size_t len = 0;
326
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200327 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->s ) );
328 ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, &ctx->r ) );
329
330 ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
331 ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
332 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
333
334 memcpy( sig, p, len );
335 *slen = len;
336
337 return( 0 );
338}
339
340/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100341 * Compute and write signature
342 */
343int ecdsa_write_signature( ecdsa_context *ctx,
344 const unsigned char *hash, size_t hlen,
345 unsigned char *sig, size_t *slen,
346 int (*f_rng)(void *, unsigned char *, size_t),
347 void *p_rng )
348{
349 int ret;
350
351 if( ( ret = ecdsa_sign( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
352 hash, hlen, f_rng, p_rng ) ) != 0 )
353 {
354 return( ret );
355 }
356
357 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
358}
359
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100360#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100361/*
362 * Compute and write signature deterministically
363 */
364int ecdsa_write_signature_det( ecdsa_context *ctx,
365 const unsigned char *hash, size_t hlen,
366 unsigned char *sig, size_t *slen,
367 md_type_t md_alg )
368{
369 int ret;
370
371 if( ( ret = ecdsa_sign_det( &ctx->grp, &ctx->r, &ctx->s, &ctx->d,
372 hash, hlen, md_alg ) ) != 0 )
373 {
374 return( ret );
375 }
376
377 return( ecdsa_signature_to_asn1( ctx, sig, slen ) );
378}
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100379#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100380
381/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200382 * Read and check signature
383 */
384int ecdsa_read_signature( ecdsa_context *ctx,
385 const unsigned char *hash, size_t hlen,
386 const unsigned char *sig, size_t slen )
387{
388 int ret;
389 unsigned char *p = (unsigned char *) sig;
390 const unsigned char *end = sig + slen;
391 size_t len;
392
393 if( ( ret = asn1_get_tag( &p, end, &len,
394 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
395 {
396 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
397 }
398
399 if( p + len != end )
400 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
401 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
402
403 if( ( ret = asn1_get_mpi( &p, end, &ctx->r ) ) != 0 ||
404 ( ret = asn1_get_mpi( &p, end, &ctx->s ) ) != 0 )
405 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA + ret );
406
407 if( p != end )
408 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA +
409 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
410
411 return( ecdsa_verify( &ctx->grp, hash, hlen, &ctx->Q, &ctx->r, &ctx->s ) );
412}
413
414/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200415 * Generate key pair
416 */
417int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
418 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
419{
420 return( ecp_use_known_dp( &ctx->grp, gid ) ||
421 ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
422}
423
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200424/*
425 * Set context from an ecp_keypair
426 */
427int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
428{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100429 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200430
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100431 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
432 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
433 ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
434 {
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200435 ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100436 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200437
438 return( ret );
439}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200440
441/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200442 * Initialize context
443 */
444void ecdsa_init( ecdsa_context *ctx )
445{
446 ecp_group_init( &ctx->grp );
447 mpi_init( &ctx->d );
448 ecp_point_init( &ctx->Q );
449 mpi_init( &ctx->r );
450 mpi_init( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200451}
452
453/*
454 * Free context
455 */
456void ecdsa_free( ecdsa_context *ctx )
457{
458 ecp_group_free( &ctx->grp );
459 mpi_free( &ctx->d );
460 ecp_point_free( &ctx->Q );
461 mpi_free( &ctx->r );
462 mpi_free( &ctx->s );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200463}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100464
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100465#if defined(POLARSSL_SELF_TEST)
466
467/*
468 * Checkup routine
469 */
470int ecdsa_self_test( int verbose )
471{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100472 ((void) verbose );
473 return( 0 );
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100474}
475
476#endif
477
478#endif /* defined(POLARSSL_ECDSA_C) */