blob: 14854ffac02cc6d0b871663f54b1f4e0e3a4a621 [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é-Gonnarddd75c312014-03-31 11:55:42 +0200228 int ret, key_tries, sign_tries, blind_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
274 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
275 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100276 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100278
279 /*
280 * Step 5: derive MPI from hashed message
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100283
284 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200285 * Generate a random value to blind inv_mod in next step,
286 * avoiding a potential timing leak.
287 */
288 blind_tries = 0;
289 do
290 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200291 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) );
293 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 /* See mbedtls_ecp_gen_keypair() */
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200296 if( ++blind_tries > 30 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 ||
300 mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200301
302 /*
303 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
306 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
307 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
308 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
309 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
310 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
311 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100314
315cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_ecp_point_free( &R );
317 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100318
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200319 ECDSA_RS_LEAVE( sig );
320
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100321 return( ret );
322}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100323
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200324/*
325 * Compute ECDSA signature of a hashed message
326 */
327int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
328 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
329 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
330{
331 return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
332 f_rng, p_rng, NULL ) );
333}
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100336/*
337 * Deterministic signature wrapper
338 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200339static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
340 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200342 mbedtls_md_type_t md_alg,
343 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100344{
345 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 mbedtls_hmac_drbg_context rng_ctx;
347 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100348 size_t grp_len = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 const mbedtls_md_info_t *md_info;
350 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
353 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_mpi_init( &h );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200356 mbedtls_hmac_drbg_init( &rng_ctx );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100357
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200358 ECDSA_RS_ENTER( det );
359
360#if defined(MBEDTLS_ECP_RESTARTABLE)
361 if( rs_ctx != NULL && rs_ctx->det != NULL )
362 {
363 /* redirect to our context */
364 // TODO
365
366 /* jump to current step */
367 // TODO
368 }
369#endif /* MBEDTLS_ECP_RESTARTABLE */
370
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100371 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
373 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
374 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200375 mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100376
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200377 ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
378 mbedtls_hmac_drbg_random, &rng_ctx, rs_ctx );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100379
380cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_hmac_drbg_free( &rng_ctx );
382 mbedtls_mpi_free( &h );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100383
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200384 ECDSA_RS_LEAVE( det );
385
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100386 return( ret );
387}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200388
389/*
390 * Deterministic signature wrapper
391 */
392int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
393 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
394 mbedtls_md_type_t md_alg )
395{
396 return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg, NULL ) );
397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100399
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100400/*
401 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
402 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
403 */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200404static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
405 const unsigned char *buf, size_t blen,
406 const mbedtls_ecp_point *Q,
407 const mbedtls_mpi *r, const mbedtls_mpi *s,
408 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100409{
410 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200412 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200413 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100414
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200415 mbedtls_ecp_point_init( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200416 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
417 mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100418
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100419 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
420 if( grp->N.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100422
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200423 ECDSA_RS_ENTER( ver );
424
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200425#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200426 if( rs_ctx != NULL && rs_ctx->ver != NULL )
427 {
428 /* redirect to our context */
429 pu1 = &rs_ctx->ver->u1;
430 pu2 = &rs_ctx->ver->u2;
431
432 /* jump to current step */
433 if( rs_ctx->ver->state == ecdsa_ver_muladd )
434 goto muladd;
435 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200436#endif /* MBEDTLS_ECP_RESTARTABLE */
437
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100438 /*
439 * Step 1: make sure r and s are in range 1..n-1
440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
442 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200445 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100446 }
447
448 /*
449 * Additional precaution: make sure Q is valid
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200450 * For ops count, group that together with step 4
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100451 */
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200452 ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100454
455 /*
456 * Step 3: derive MPI from hashed message
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100459
460 /*
461 * Step 4: u1 = e / s mod n, u2 = r / s mod n
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100464
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200465 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
466 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100467
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200468 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
469 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100470
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200471#if defined(MBEDTLS_ECP_RESTARTABLE)
472 if( rs_ctx != NULL && rs_ctx->ver != NULL )
473 rs_ctx->ver->state++;
474
475muladd:
476#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100477 /*
478 * Step 5: R = u1 G + u2 Q
479 */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200480 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200481 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( mbedtls_ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200486 goto cleanup;
487 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100488
489 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100490 * Step 6: convert xR to an integer (no-op)
491 * Step 7: reduce xR mod n (gives v)
492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100494
495 /*
496 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200501 goto cleanup;
502 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100503
504cleanup:
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200505 mbedtls_ecp_point_free( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200506 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
507 mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100508
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200509 ECDSA_RS_LEAVE( ver );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200510
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100511 return( ret );
512}
513
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200514/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200515 * Verify ECDSA signature of hashed message
516 */
517int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
518 const unsigned char *buf, size_t blen,
519 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
520{
521 return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
522}
523
524/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100525 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100528 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200529{
530 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200532 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200533 size_t len = 0;
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
536 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
539 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
540 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200541
542 memcpy( sig, p, len );
543 *slen = len;
544
545 return( 0 );
546}
547
548/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100549 * Compute and write signature
550 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200551int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
552 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100553 const unsigned char *hash, size_t hlen,
554 unsigned char *sig, size_t *slen,
555 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200556 void *p_rng,
557 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100558{
559 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_mpi r, s;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_mpi_init( &r );
563 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200566 (void) f_rng;
567 (void) p_rng;
568
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200569 MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
570 hash, hlen, md_alg, rs_ctx ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200571#else
572 (void) md_alg;
573
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200574 MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
575 hash, hlen, f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200576#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200579
580cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_mpi_free( &r );
582 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200583
584 return( ret );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100585}
586
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200587/*
588 * Compute and write signature
589 */
590int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
591 const unsigned char *hash, size_t hlen,
592 unsigned char *sig, size_t *slen,
593 int (*f_rng)(void *, unsigned char *, size_t),
594 void *p_rng )
595{
596 return( mbedtls_ecdsa_write_signature_restartable(
597 ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
598}
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
601 defined(MBEDTLS_ECDSA_DETERMINISTIC)
602int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100603 const unsigned char *hash, size_t hlen,
604 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_md_type_t md_alg )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200608 NULL, NULL ) );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100609}
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200610#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100611
612/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200613 * Read and check signature
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200616 const unsigned char *hash, size_t hlen,
617 const unsigned char *sig, size_t slen )
618{
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200619 return( mbedtls_ecdsa_read_signature_restartable(
620 ctx, hash, hlen, sig, slen, NULL ) );
621}
622
623/*
624 * Restartable read and check signature
625 */
626int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
627 const unsigned char *hash, size_t hlen,
628 const unsigned char *sig, size_t slen,
629 mbedtls_ecdsa_restart_ctx *rs_ctx )
630{
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200631 int ret;
632 unsigned char *p = (unsigned char *) sig;
633 const unsigned char *end = sig + slen;
634 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_mpi r, s;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_mpi_init( &r );
638 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
641 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200644 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200645 }
646
647 if( p + len != end )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
650 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200651 goto cleanup;
652 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
655 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200658 goto cleanup;
659 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200660
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200661 if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
662 &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200663 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200664
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200665 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200667
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200668cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_mpi_free( &r );
670 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200671
672 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200673}
674
675/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200676 * Generate key pair
677 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200679 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
680{
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200681 return( mbedtls_ecp_group_load( &ctx->grp, gid ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200683}
684
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200685/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200689{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100690 int ret;
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
693 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
694 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100697 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200698
699 return( ret );
700}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200701
702/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200703 * Initialize context
704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200708}
709
710/*
711 * Free context
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200714{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_ecp_keypair_free( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200716}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100717
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200718#if defined(MBEDTLS_ECP_RESTARTABLE)
719/*
720 * Initialize a restart context
721 */
722void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
723{
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200724 mbedtls_ecp_restart_init( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200725
726 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200727 ctx->sig = NULL;
728#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
729 ctx->det = NULL;
730#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200731}
732
733/*
734 * Free the components of a restart context
735 */
736void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
737{
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200738 mbedtls_ecp_restart_free( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200739
740 ecdsa_restart_ver_free( ctx->ver );
741 mbedtls_free( ctx->ver );
742 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200743
744 ecdsa_restart_sig_free( ctx->sig );
745 mbedtls_free( ctx->sig );
746 ctx->sig = NULL;
747
748#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
749 ecdsa_restart_det_free( ctx->det );
750 mbedtls_free( ctx->det );
751 ctx->det = NULL;
752#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200753}
754#endif /* MBEDTLS_ECP_RESTARTABLE */
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#endif /* MBEDTLS_ECDSA_C */