blob: b8aef5d30be4cee15b0efc074b23802b0ab4420b [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/*
2 * Elliptic curve DSA
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010020 */
21
22/*
23 * References:
24 *
25 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
26 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecdsa.h"
37#include "mbedtls/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010043#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010044
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020045#if defined(MBEDTLS_PLATFORM_C)
46#include "mbedtls/platform.h"
47#else
48#include <stdlib.h>
49#define mbedtls_calloc calloc
50#define mbedtls_free free
51#endif
52
53#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020054
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020055/*
56 * Sub-contect for ecdsa_verify()
57 */
58struct mbedtls_ecdsa_restart_ver
59{
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020060 mbedtls_mpi u1, u2; /* intermediate values */
61 enum { /* what to do next? */
62 ecdsa_ver_init = 0, /* getting started */
63 ecdsa_ver_muladd, /* muladd step */
64 } state;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020065};
66
67/*
68 * Init verify restart sub-context
69 */
70static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx )
71{
72 memset( ctx, 0, sizeof( *ctx ) );
73}
74
75/*
76 * Free the components of a verify restart sub-context
77 */
78static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx )
79{
80 if( ctx == NULL )
81 return;
82
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020083 mbedtls_mpi_free( &ctx->u1 );
84 mbedtls_mpi_free( &ctx->u2 );
85
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020086 memset( ctx, 0, sizeof( *ctx ) );
87}
88
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020089/*
90 * Sub-contect for ecdsa_sign()
91 */
92struct mbedtls_ecdsa_restart_sig
93{
94 enum { /* what to do next? */
95 ecdsa_sig_init = 0, /* getting started */
96 } state;
97};
98
99/*
100 * Init verify sign sub-context
101 */
102static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx )
103{
104 memset( ctx, 0, sizeof( *ctx ) );
105}
106
107/*
108 * Free the components of a sign restart sub-context
109 */
110static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx )
111{
112 if( ctx == NULL )
113 return;
114
115 memset( ctx, 0, sizeof( *ctx ) );
116}
117
118#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
119/*
120 * Sub-contect for ecdsa_sign_det()
121 */
122struct mbedtls_ecdsa_restart_det
123{
124 enum { /* what to do next? */
125 ecdsa_det_init = 0, /* getting started */
126 } state;
127};
128
129/*
130 * Init verify sign_det sub-context
131 */
132static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
133{
134 memset( ctx, 0, sizeof( *ctx ) );
135}
136
137/*
138 * Free the components of a sign_det restart sub-context
139 */
140static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx )
141{
142 if( ctx == NULL )
143 return;
144
145 memset( ctx, 0, sizeof( *ctx ) );
146}
147#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
148
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200149#define ECDSA_RS_ECP &rs_ctx->ecp
150
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200151/* Utility macro for checking and updating ops budget */
152#define ECDSA_BUDGET( ops ) \
153 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, &rs_ctx->ecp, ops ) );
154
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200155#define ECDSA_RS_ENTER( SUB ) do { \
156 /* reset ops count for this call if top-level */ \
157 if( rs_ctx != NULL && rs_ctx->ecp.depth++ == 0 ) \
158 rs_ctx->ecp.ops_done = 0; \
159 \
160 /* set up our own sub-context if needed */ \
161 if( mbedtls_ecp_restart_enabled() && \
162 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
163 { \
164 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
165 if( rs_ctx->SUB == NULL ) \
166 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
167 \
168 ecdsa_restart_## SUB ##_init( rs_ctx->SUB ); \
169 } \
170} while( 0 )
171
172#define ECDSA_RS_LEAVE( SUB ) do { \
173 /* clear our sub-context when not in progress (done or error) */ \
174 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) { \
175 ecdsa_restart_## SUB ##_free( rs_ctx->SUB ); \
176 mbedtls_free( rs_ctx->SUB ); \
177 rs_ctx->SUB = NULL; \
178 } \
179 \
180 if( rs_ctx != NULL ) \
181 rs_ctx->ecp.depth--; \
182} while( 0 )
183
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200184#else /* MBEDTLS_ECP_RESTARTABLE */
185
186#define ECDSA_RS_ECP NULL
187
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200188#define ECDSA_BUDGET( ops ) /* no-op; for compatibility */
189
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200190#define ECDSA_RS_ENTER( SUB ) (void) rs_ctx
191#define ECDSA_RS_LEAVE( SUB ) (void) rs_ctx
192
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200193#endif /* MBEDTLS_ECP_RESTARTABLE */
194
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100195/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100196 * Derive a suitable integer for group grp from a buffer of length len
197 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100200 const unsigned char *buf, size_t blen )
201{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100202 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +0200203 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100204 size_t use_size = blen > n_size ? n_size : blen;
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100207 if( use_size * 8 > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100209
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100210 /* While at it, reduce modulo N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
212 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100213
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100214cleanup:
215 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100216}
217
218/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100219 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
220 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
221 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200222static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
223 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200225 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
226 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100227{
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200228 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_ecp_point R;
230 mbedtls_mpi k, e, t;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100231
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100232 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
233 if( grp->N.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_ecp_point_init( &R );
237 mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100238
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200239 ECDSA_RS_ENTER( sig );
240
241#if defined(MBEDTLS_ECP_RESTARTABLE)
242 if( rs_ctx != NULL && rs_ctx->sig != NULL )
243 {
244 /* redirect to our context */
245 // TODO
246
247 /* jump to current step */
248 // TODO
249 }
250#endif /* MBEDTLS_ECP_RESTARTABLE */
251
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100252 sign_tries = 0;
253 do
254 {
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200255 if( sign_tries++ > 10 )
256 {
257 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
258 goto cleanup;
259 }
260
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100261 /*
262 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100263 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100264 */
265 key_tries = 0;
266 do
267 {
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100268 if( key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200271 goto cleanup;
272 }
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200273
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200274 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &k, f_rng, p_rng ) );
275
276 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &R, &k, &grp->G,
277 f_rng, p_rng ) );
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200278 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100281
282 /*
283 * Step 5: derive MPI from hashed message
284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100286
287 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200288 * Generate a random value to blind inv_mod in next step,
289 * avoiding a potential timing leak.
290 */
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200291 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng, p_rng ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200292
293 /*
294 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
297 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
298 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
299 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
300 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
301 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
302 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100305
306cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_ecp_point_free( &R );
308 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100309
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200310 ECDSA_RS_LEAVE( sig );
311
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100312 return( ret );
313}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100314
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200315/*
316 * Compute ECDSA signature of a hashed message
317 */
318int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
319 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
320 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
321{
322 return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
323 f_rng, p_rng, NULL ) );
324}
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100327/*
328 * Deterministic signature wrapper
329 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200330static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
331 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200333 mbedtls_md_type_t md_alg,
334 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100335{
336 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_hmac_drbg_context rng_ctx;
338 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100339 size_t grp_len = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 const mbedtls_md_info_t *md_info;
341 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
344 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_mpi_init( &h );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200347 mbedtls_hmac_drbg_init( &rng_ctx );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100348
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200349 ECDSA_RS_ENTER( det );
350
351#if defined(MBEDTLS_ECP_RESTARTABLE)
352 if( rs_ctx != NULL && rs_ctx->det != NULL )
353 {
354 /* redirect to our context */
355 // TODO
356
357 /* jump to current step */
358 // TODO
359 }
360#endif /* MBEDTLS_ECP_RESTARTABLE */
361
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100362 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
364 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
365 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200366 mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100367
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200368 ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
369 mbedtls_hmac_drbg_random, &rng_ctx, rs_ctx );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100370
371cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_hmac_drbg_free( &rng_ctx );
373 mbedtls_mpi_free( &h );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100374
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200375 ECDSA_RS_LEAVE( det );
376
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100377 return( ret );
378}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200379
380/*
381 * Deterministic signature wrapper
382 */
383int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
384 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
385 mbedtls_md_type_t md_alg )
386{
387 return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg, NULL ) );
388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100390
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100391/*
392 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
393 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
394 */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200395static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
396 const unsigned char *buf, size_t blen,
397 const mbedtls_ecp_point *Q,
398 const mbedtls_mpi *r, const mbedtls_mpi *s,
399 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100400{
401 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200403 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200404 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100405
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200406 mbedtls_ecp_point_init( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200407 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
408 mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100409
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100410 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
411 if( grp->N.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100413
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200414 ECDSA_RS_ENTER( ver );
415
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200416#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200417 if( rs_ctx != NULL && rs_ctx->ver != NULL )
418 {
419 /* redirect to our context */
420 pu1 = &rs_ctx->ver->u1;
421 pu2 = &rs_ctx->ver->u2;
422
423 /* jump to current step */
424 if( rs_ctx->ver->state == ecdsa_ver_muladd )
425 goto muladd;
426 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200427#endif /* MBEDTLS_ECP_RESTARTABLE */
428
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100429 /*
430 * Step 1: make sure r and s are in range 1..n-1
431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
433 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200436 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100437 }
438
439 /*
440 * Additional precaution: make sure Q is valid
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200441 * For ops count, group that together with step 4
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100442 */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200443 ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100445
446 /*
447 * Step 3: derive MPI from hashed message
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100450
451 /*
452 * Step 4: u1 = e / s mod n, u2 = r / s mod n
453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100455
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200456 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
457 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100458
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200459 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
460 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100461
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200462#if defined(MBEDTLS_ECP_RESTARTABLE)
463 if( rs_ctx != NULL && rs_ctx->ver != NULL )
464 rs_ctx->ver->state++;
465
466muladd:
467#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100468 /*
469 * Step 5: R = u1 G + u2 Q
470 */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200471 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200472 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 if( mbedtls_ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200477 goto cleanup;
478 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100479
480 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100481 * Step 6: convert xR to an integer (no-op)
482 * Step 7: reduce xR mod n (gives v)
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100485
486 /*
487 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200492 goto cleanup;
493 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100494
495cleanup:
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200496 mbedtls_ecp_point_free( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200497 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
498 mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100499
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200500 ECDSA_RS_LEAVE( ver );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200501
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100502 return( ret );
503}
504
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200505/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200506 * Verify ECDSA signature of hashed message
507 */
508int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
509 const unsigned char *buf, size_t blen,
510 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
511{
512 return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
513}
514
515/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100516 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100519 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200520{
521 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200523 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200524 size_t len = 0;
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
527 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
530 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
531 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200532
533 memcpy( sig, p, len );
534 *slen = len;
535
536 return( 0 );
537}
538
539/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100540 * Compute and write signature
541 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200542int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
543 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100544 const unsigned char *hash, size_t hlen,
545 unsigned char *sig, size_t *slen,
546 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200547 void *p_rng,
548 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100549{
550 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_mpi r, s;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_mpi_init( &r );
554 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200557 (void) f_rng;
558 (void) p_rng;
559
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200560 MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
561 hash, hlen, md_alg, rs_ctx ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200562#else
563 (void) md_alg;
564
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200565 MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
566 hash, hlen, f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200567#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200570
571cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_mpi_free( &r );
573 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200574
575 return( ret );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100576}
577
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200578/*
579 * Compute and write signature
580 */
581int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
582 const unsigned char *hash, size_t hlen,
583 unsigned char *sig, size_t *slen,
584 int (*f_rng)(void *, unsigned char *, size_t),
585 void *p_rng )
586{
587 return( mbedtls_ecdsa_write_signature_restartable(
588 ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
589}
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
592 defined(MBEDTLS_ECDSA_DETERMINISTIC)
593int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100594 const unsigned char *hash, size_t hlen,
595 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_type_t md_alg )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100597{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200599 NULL, NULL ) );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100600}
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200601#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100602
603/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200604 * Read and check signature
605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200607 const unsigned char *hash, size_t hlen,
608 const unsigned char *sig, size_t slen )
609{
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200610 return( mbedtls_ecdsa_read_signature_restartable(
611 ctx, hash, hlen, sig, slen, NULL ) );
612}
613
614/*
615 * Restartable read and check signature
616 */
617int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
618 const unsigned char *hash, size_t hlen,
619 const unsigned char *sig, size_t slen,
620 mbedtls_ecdsa_restart_ctx *rs_ctx )
621{
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200622 int ret;
623 unsigned char *p = (unsigned char *) sig;
624 const unsigned char *end = sig + slen;
625 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_mpi r, s;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_mpi_init( &r );
629 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
632 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200635 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200636 }
637
638 if( p + len != end )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
641 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200642 goto cleanup;
643 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
646 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200649 goto cleanup;
650 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200651
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200652 if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
653 &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200654 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200655
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200656 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200658
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200659cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_mpi_free( &r );
661 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200662
663 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200664}
665
666/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200667 * Generate key pair
668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200670 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
671{
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200672 return( mbedtls_ecp_group_load( &ctx->grp, gid ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200674}
675
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200676/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200680{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100681 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
684 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
685 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100688 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200689
690 return( ret );
691}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200692
693/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200694 * Initialize context
695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200697{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200699}
700
701/*
702 * Free context
703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200705{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_ecp_keypair_free( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200707}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100708
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200709#if defined(MBEDTLS_ECP_RESTARTABLE)
710/*
711 * Initialize a restart context
712 */
713void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
714{
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200715 mbedtls_ecp_restart_init( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200716
717 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200718 ctx->sig = NULL;
719#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
720 ctx->det = NULL;
721#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200722}
723
724/*
725 * Free the components of a restart context
726 */
727void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
728{
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200729 mbedtls_ecp_restart_free( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200730
731 ecdsa_restart_ver_free( ctx->ver );
732 mbedtls_free( ctx->ver );
733 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200734
735 ecdsa_restart_sig_free( ctx->sig );
736 mbedtls_free( ctx->sig );
737 ctx->sig = NULL;
738
739#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
740 ecdsa_restart_det_free( ctx->det );
741 mbedtls_free( ctx->det );
742 ctx->det = NULL;
743#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200744}
745#endif /* MBEDTLS_ECP_RESTARTABLE */
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747#endif /* MBEDTLS_ECDSA_C */