blob: a6ba75d1cec17a462c6c35388244929362229d76 [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
Hanno Becker319ae112018-12-14 16:43:29 +000053#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000054#include "mbedtls/error.h"
Hanno Becker319ae112018-12-14 16:43:29 +000055
56/* Parameter validation macros based on platform_util.h */
57#define ECDSA_VALIDATE_RET( cond ) \
58 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
59#define ECDSA_VALIDATE( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE( cond )
61
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020062#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020063
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020064/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +020065 * Sub-context for ecdsa_verify()
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020066 */
67struct mbedtls_ecdsa_restart_ver
68{
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020069 mbedtls_mpi u1, u2; /* intermediate values */
70 enum { /* what to do next? */
71 ecdsa_ver_init = 0, /* getting started */
72 ecdsa_ver_muladd, /* muladd step */
73 } state;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020074};
75
76/*
77 * Init verify restart sub-context
78 */
79static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx )
80{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +020081 mbedtls_mpi_init( &ctx->u1 );
82 mbedtls_mpi_init( &ctx->u2 );
83 ctx->state = ecdsa_ver_init;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020084}
85
86/*
87 * Free the components of a verify restart sub-context
88 */
89static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx )
90{
91 if( ctx == NULL )
92 return;
93
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020094 mbedtls_mpi_free( &ctx->u1 );
95 mbedtls_mpi_free( &ctx->u2 );
96
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +020097 ecdsa_restart_ver_init( ctx );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020098}
99
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200100/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +0200101 * Sub-context for ecdsa_sign()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200102 */
103struct mbedtls_ecdsa_restart_sig
104{
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200105 int sign_tries;
106 int key_tries;
107 mbedtls_mpi k; /* per-signature random */
108 mbedtls_mpi r; /* r value */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200109 enum { /* what to do next? */
110 ecdsa_sig_init = 0, /* getting started */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200111 ecdsa_sig_mul, /* doing ecp_mul() */
112 ecdsa_sig_modn, /* mod N computations */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200113 } state;
114};
115
116/*
117 * Init verify sign sub-context
118 */
119static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx )
120{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200121 ctx->sign_tries = 0;
122 ctx->key_tries = 0;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200123 mbedtls_mpi_init( &ctx->k );
124 mbedtls_mpi_init( &ctx->r );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200125 ctx->state = ecdsa_sig_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200126}
127
128/*
129 * Free the components of a sign restart sub-context
130 */
131static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx )
132{
133 if( ctx == NULL )
134 return;
135
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200136 mbedtls_mpi_free( &ctx->k );
137 mbedtls_mpi_free( &ctx->r );
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200138}
139
140#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
141/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +0200142 * Sub-context for ecdsa_sign_det()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200143 */
144struct mbedtls_ecdsa_restart_det
145{
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200146 mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200147 enum { /* what to do next? */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200148 ecdsa_det_init = 0, /* getting started */
149 ecdsa_det_sign, /* make signature */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200150 } state;
151};
152
153/*
154 * Init verify sign_det sub-context
155 */
156static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
157{
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200158 mbedtls_hmac_drbg_init( &ctx->rng_ctx );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200159 ctx->state = ecdsa_det_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200160}
161
162/*
163 * Free the components of a sign_det restart sub-context
164 */
165static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx )
166{
167 if( ctx == NULL )
168 return;
169
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200170 mbedtls_hmac_drbg_free( &ctx->rng_ctx );
171
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200172 ecdsa_restart_det_init( ctx );
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200173}
174#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
175
Hanno Becker59c92ed2019-07-19 12:42:21 +0100176#define ECDSA_RS_ECP ( rs_ctx == NULL ? NULL : &rs_ctx->ecp )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200177
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200178/* Utility macro for checking and updating ops budget */
179#define ECDSA_BUDGET( ops ) \
Hanno Becker59c92ed2019-07-19 12:42:21 +0100180 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, ECDSA_RS_ECP, ops ) );
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200181
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200182/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200183#define ECDSA_RS_ENTER( SUB ) do { \
184 /* reset ops count for this call if top-level */ \
185 if( rs_ctx != NULL && rs_ctx->ecp.depth++ == 0 ) \
186 rs_ctx->ecp.ops_done = 0; \
187 \
188 /* set up our own sub-context if needed */ \
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200189 if( mbedtls_ecp_restart_is_enabled() && \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200190 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
191 { \
192 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
193 if( rs_ctx->SUB == NULL ) \
194 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
195 \
196 ecdsa_restart_## SUB ##_init( rs_ctx->SUB ); \
197 } \
198} while( 0 )
199
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200200/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200201#define ECDSA_RS_LEAVE( SUB ) do { \
202 /* clear our sub-context when not in progress (done or error) */ \
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200203 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
204 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200205 { \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200206 ecdsa_restart_## SUB ##_free( rs_ctx->SUB ); \
207 mbedtls_free( rs_ctx->SUB ); \
208 rs_ctx->SUB = NULL; \
209 } \
210 \
211 if( rs_ctx != NULL ) \
212 rs_ctx->ecp.depth--; \
213} while( 0 )
214
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200215#else /* MBEDTLS_ECP_RESTARTABLE */
216
217#define ECDSA_RS_ECP NULL
218
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200219#define ECDSA_BUDGET( ops ) /* no-op; for compatibility */
220
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200221#define ECDSA_RS_ENTER( SUB ) (void) rs_ctx
222#define ECDSA_RS_LEAVE( SUB ) (void) rs_ctx
223
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200224#endif /* MBEDTLS_ECP_RESTARTABLE */
225
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100226/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100227 * Derive a suitable integer for group grp from a buffer of length len
228 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100231 const unsigned char *buf, size_t blen )
232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker66d5d072014-06-17 16:39:18 +0200234 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100235 size_t use_size = blen > n_size ? n_size : blen;
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100238 if( use_size * 8 > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100240
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100241 /* While at it, reduce modulo N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
243 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100244
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100245cleanup:
246 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100247}
248
Ron Eldor936d2842018-11-01 13:05:52 +0200249#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100250/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100251 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
252 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
253 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200254static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
255 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200257 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Janos Follathdca667a2019-01-04 14:32:30 +0000258 int (*f_rng_blind)(void *, unsigned char *, size_t),
259 void *p_rng_blind,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200260 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100261{
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200262 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200263 int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_ecp_point R;
265 mbedtls_mpi k, e, t;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200266 mbedtls_mpi *pk = &k, *pr = r;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100267
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100268 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Christoph M. Wintersteigerc25df682019-04-16 12:54:56 +0100269 if( ! mbedtls_ecdsa_can_do( grp->id ) || grp->N.p == NULL )
Christoph M. Wintersteigerfba94e92018-12-06 17:18:24 +0000270 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100271
Darryl Greenc64a48b2017-11-17 17:09:17 +0000272 /* Make sure d is in range 1..n-1 */
273 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
274 return( MBEDTLS_ERR_ECP_INVALID_KEY );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_ecp_point_init( &R );
277 mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100278
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200279 ECDSA_RS_ENTER( sig );
280
281#if defined(MBEDTLS_ECP_RESTARTABLE)
282 if( rs_ctx != NULL && rs_ctx->sig != NULL )
283 {
284 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200285 p_sign_tries = &rs_ctx->sig->sign_tries;
286 p_key_tries = &rs_ctx->sig->key_tries;
287 pk = &rs_ctx->sig->k;
288 pr = &rs_ctx->sig->r;
289
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200290 /* jump to current step */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200291 if( rs_ctx->sig->state == ecdsa_sig_mul )
292 goto mul;
293 if( rs_ctx->sig->state == ecdsa_sig_modn )
294 goto modn;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200295 }
296#endif /* MBEDTLS_ECP_RESTARTABLE */
297
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200298 *p_sign_tries = 0;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100299 do
300 {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200301 if( *p_sign_tries++ > 10 )
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200302 {
303 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
304 goto cleanup;
305 }
306
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100307 /*
308 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100309 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100310 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200311 *p_key_tries = 0;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100312 do
313 {
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200314 if( *p_key_tries++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200317 goto cleanup;
318 }
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200319
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200320 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, pk, f_rng, p_rng ) );
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200321
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200322#if defined(MBEDTLS_ECP_RESTARTABLE)
323 if( rs_ctx != NULL && rs_ctx->sig != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200324 rs_ctx->sig->state = ecdsa_sig_mul;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200325
326mul:
327#endif
328 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &R, pk, &grp->G,
Janos Follathdca667a2019-01-04 14:32:30 +0000329 f_rng_blind,
330 p_rng_blind,
331 ECDSA_RS_ECP ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200332 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pr, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100333 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200334 while( mbedtls_mpi_cmp_int( pr, 0 ) == 0 );
335
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200336#if defined(MBEDTLS_ECP_RESTARTABLE)
337 if( rs_ctx != NULL && rs_ctx->sig != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200338 rs_ctx->sig->state = ecdsa_sig_modn;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200339
340modn:
341#endif
342 /*
343 * Accounting for everything up to the end of the loop
344 * (step 6, but checking now avoids saving e and t)
345 */
346 ECDSA_BUDGET( MBEDTLS_ECP_OPS_INV + 4 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100347
348 /*
349 * Step 5: derive MPI from hashed message
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100352
353 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200354 * Generate a random value to blind inv_mod in next step,
355 * avoiding a potential timing leak.
356 */
Janos Follathdca667a2019-01-04 14:32:30 +0000357 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng_blind,
358 p_rng_blind ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200359
360 /*
361 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100362 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200363 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, pr, d ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
365 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200366 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pk, pk, &t ) );
367 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, pk, &grp->N ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
369 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100372
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200373#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +0200374 if( rs_ctx != NULL && rs_ctx->sig != NULL )
375 mbedtls_mpi_copy( r, pr );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200376#endif
377
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100378cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_ecp_point_free( &R );
380 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100381
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200382 ECDSA_RS_LEAVE( sig );
383
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100384 return( ret );
385}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100386
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000387int mbedtls_ecdsa_can_do( mbedtls_ecp_group_id gid )
388{
389 switch( gid )
390 {
391#ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000392 case MBEDTLS_ECP_DP_CURVE25519: return 0;
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000393#endif
394#ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
Christoph M. Wintersteiger3ff60bc2019-02-15 12:59:59 +0000395 case MBEDTLS_ECP_DP_CURVE448: return 0;
Christoph M. Wintersteiger0082f9d2019-01-07 13:47:30 +0000396#endif
397 default: return 1;
398 }
399}
400
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200401/*
402 * Compute ECDSA signature of a hashed message
403 */
404int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
405 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
406 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
407{
Hanno Becker319ae112018-12-14 16:43:29 +0000408 ECDSA_VALIDATE_RET( grp != NULL );
409 ECDSA_VALIDATE_RET( r != NULL );
410 ECDSA_VALIDATE_RET( s != NULL );
411 ECDSA_VALIDATE_RET( d != NULL );
412 ECDSA_VALIDATE_RET( f_rng != NULL );
413 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
414
Janos Follathdca667a2019-01-04 14:32:30 +0000415 /* Use the same RNG for both blinding and ephemeral key generation */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200416 return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
Janos Follathdca667a2019-01-04 14:32:30 +0000417 f_rng, p_rng, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200418}
Ron Eldor936d2842018-11-01 13:05:52 +0200419#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100422/*
423 * Deterministic signature wrapper
424 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200425static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
426 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200428 mbedtls_md_type_t md_alg,
Janos Follathdca667a2019-01-04 14:32:30 +0000429 int (*f_rng_blind)(void *, unsigned char *, size_t),
430 void *p_rng_blind,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200431 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100432{
Janos Follath24eed8d2019-11-22 13:21:35 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200435 mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100437 size_t grp_len = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 const mbedtls_md_info_t *md_info;
439 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
442 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_mpi_init( &h );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200445 mbedtls_hmac_drbg_init( &rng_ctx );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100446
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200447 ECDSA_RS_ENTER( det );
448
449#if defined(MBEDTLS_ECP_RESTARTABLE)
450 if( rs_ctx != NULL && rs_ctx->det != NULL )
451 {
452 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200453 p_rng = &rs_ctx->det->rng_ctx;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200454
455 /* jump to current step */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200456 if( rs_ctx->det->state == ecdsa_det_sign )
457 goto sign;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200458 }
459#endif /* MBEDTLS_ECP_RESTARTABLE */
460
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100461 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
463 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
464 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200465 mbedtls_hmac_drbg_seed_buf( p_rng, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100466
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200467#if defined(MBEDTLS_ECP_RESTARTABLE)
468 if( rs_ctx != NULL && rs_ctx->det != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200469 rs_ctx->det->state = ecdsa_det_sign;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200470
471sign:
472#endif
Ron Eldor8493f802018-11-01 11:32:15 +0200473#if defined(MBEDTLS_ECDSA_SIGN_ALT)
474 ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
475 mbedtls_hmac_drbg_random, p_rng );
476#else
Janos Follathdca667a2019-01-04 14:32:30 +0000477 if( f_rng_blind != NULL )
478 ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
479 mbedtls_hmac_drbg_random, p_rng,
480 f_rng_blind, p_rng_blind, rs_ctx );
481 else
Janos Follath896a2942019-01-07 17:27:56 +0000482 {
483 mbedtls_hmac_drbg_context *p_rng_blind_det;
484
485#if !defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follathdca667a2019-01-04 14:32:30 +0000486 /*
Janos Follath896a2942019-01-07 17:27:56 +0000487 * To avoid reusing rng_ctx and risking incorrect behavior we seed a
488 * second HMAC-DRBG with the same seed. We also apply a label to avoid
489 * reusing the bits of the ephemeral key for blinding and eliminate the
490 * risk that they leak this way.
491 */
492 const char* blind_label = "BLINDING CONTEXT";
493 mbedtls_hmac_drbg_context rng_ctx_blind;
494
495 mbedtls_hmac_drbg_init( &rng_ctx_blind );
496 p_rng_blind_det = &rng_ctx_blind;
497 mbedtls_hmac_drbg_seed_buf( p_rng_blind_det, md_info,
498 data, 2 * grp_len );
499 ret = mbedtls_hmac_drbg_update_ret( p_rng_blind_det,
500 (const unsigned char*) blind_label,
501 strlen( blind_label ) );
502 if( ret != 0 )
503 {
504 mbedtls_hmac_drbg_free( &rng_ctx_blind );
505 goto cleanup;
506 }
507#else
508 /*
509 * In the case of restartable computations we would either need to store
510 * the second RNG in the restart context too or set it up at every
511 * restart. The first option would penalize the correct application of
512 * the function and the second would defeat the purpose of the
513 * restartable feature.
514 *
515 * Therefore in this case we reuse the original RNG. This comes with the
516 * price that the resulting signature might not be a valid deterministic
517 * ECDSA signature with a very low probability (same magnitude as
518 * successfully guessing the private key). However even then it is still
519 * a valid ECDSA signature.
520 */
521 p_rng_blind_det = p_rng;
522#endif /* MBEDTLS_ECP_RESTARTABLE */
523
524 /*
525 * Since the output of the RNGs is always the same for the same key and
526 * message, this limits the efficiency of blinding and leaks information
527 * through side channels. After mbedtls_ecdsa_sign_det() is removed NULL
528 * won't be a valid value for f_rng_blind anymore. Therefore it should
529 * be checked by the caller and this branch and check can be removed.
Janos Follathdca667a2019-01-04 14:32:30 +0000530 */
531 ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
532 mbedtls_hmac_drbg_random, p_rng,
Janos Follath896a2942019-01-07 17:27:56 +0000533 mbedtls_hmac_drbg_random, p_rng_blind_det,
534 rs_ctx );
535
536#if !defined(MBEDTLS_ECP_RESTARTABLE)
537 mbedtls_hmac_drbg_free( &rng_ctx_blind );
538#endif
539 }
Ron Eldor936d2842018-11-01 13:05:52 +0200540#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100541
542cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_hmac_drbg_free( &rng_ctx );
544 mbedtls_mpi_free( &h );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100545
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200546 ECDSA_RS_LEAVE( det );
547
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100548 return( ret );
549}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200550
551/*
Janos Follathdca667a2019-01-04 14:32:30 +0000552 * Deterministic signature wrappers
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200553 */
Janos Follathe65e0592019-01-04 15:55:43 +0000554
555#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Janos Follathdca667a2019-01-04 14:32:30 +0000556int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
557 mbedtls_mpi *s, const mbedtls_mpi *d,
558 const unsigned char *buf, size_t blen,
559 mbedtls_md_type_t md_alg )
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200560{
Hanno Becker319ae112018-12-14 16:43:29 +0000561 ECDSA_VALIDATE_RET( grp != NULL );
562 ECDSA_VALIDATE_RET( r != NULL );
563 ECDSA_VALIDATE_RET( s != NULL );
564 ECDSA_VALIDATE_RET( d != NULL );
565 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
566
Janos Follathdca667a2019-01-04 14:32:30 +0000567 return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
568 NULL, NULL, NULL ) );
569}
Janos Follathe65e0592019-01-04 15:55:43 +0000570#endif /* MBEDTLS_DEPRECATED_REMOVED */
Janos Follathdca667a2019-01-04 14:32:30 +0000571
572int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
573 mbedtls_mpi *s, const mbedtls_mpi *d,
574 const unsigned char *buf, size_t blen,
575 mbedtls_md_type_t md_alg,
576 int (*f_rng_blind)(void *, unsigned char *,
577 size_t),
578 void *p_rng_blind )
579{
580 ECDSA_VALIDATE_RET( grp != NULL );
581 ECDSA_VALIDATE_RET( r != NULL );
582 ECDSA_VALIDATE_RET( s != NULL );
583 ECDSA_VALIDATE_RET( d != NULL );
584 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
585 ECDSA_VALIDATE_RET( f_rng_blind != NULL );
586
587 return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
588 f_rng_blind, p_rng_blind, NULL ) );
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100591
Ron Eldor936d2842018-11-01 13:05:52 +0200592#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100593/*
594 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
595 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
596 */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200597static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
598 const unsigned char *buf, size_t blen,
599 const mbedtls_ecp_point *Q,
600 const mbedtls_mpi *r, const mbedtls_mpi *s,
601 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100602{
Janos Follath24eed8d2019-11-22 13:21:35 +0000603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200605 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200606 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100607
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200608 mbedtls_ecp_point_init( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200609 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
610 mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100611
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100612 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
Christoph M. Wintersteigerc25df682019-04-16 12:54:56 +0100613 if( ! mbedtls_ecdsa_can_do( grp->id ) || grp->N.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100615
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200616 ECDSA_RS_ENTER( ver );
617
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200618#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200619 if( rs_ctx != NULL && rs_ctx->ver != NULL )
620 {
621 /* redirect to our context */
622 pu1 = &rs_ctx->ver->u1;
623 pu2 = &rs_ctx->ver->u2;
624
625 /* jump to current step */
626 if( rs_ctx->ver->state == ecdsa_ver_muladd )
627 goto muladd;
628 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200629#endif /* MBEDTLS_ECP_RESTARTABLE */
630
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100631 /*
632 * Step 1: make sure r and s are in range 1..n-1
633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
635 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200638 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100639 }
640
641 /*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100642 * Step 3: derive MPI from hashed message
643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100645
646 /*
647 * Step 4: u1 = e / s mod n, u2 = r / s mod n
648 */
Manuel Pégourié-Gonnardbfa19722017-08-23 17:39:18 +0200649 ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100652
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200653 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
654 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100655
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200656 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
657 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100658
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200659#if defined(MBEDTLS_ECP_RESTARTABLE)
660 if( rs_ctx != NULL && rs_ctx->ver != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200661 rs_ctx->ver->state = ecdsa_ver_muladd;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200662
663muladd:
664#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100665 /*
666 * Step 5: R = u1 G + u2 Q
667 */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200668 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200669 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 if( mbedtls_ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200674 goto cleanup;
675 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100676
677 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100678 * Step 6: convert xR to an integer (no-op)
679 * Step 7: reduce xR mod n (gives v)
680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100682
683 /*
684 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200689 goto cleanup;
690 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100691
692cleanup:
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200693 mbedtls_ecp_point_free( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200694 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
695 mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100696
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200697 ECDSA_RS_LEAVE( ver );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200698
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100699 return( ret );
700}
701
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200702/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200703 * Verify ECDSA signature of hashed message
704 */
705int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Hanno Becker319ae112018-12-14 16:43:29 +0000706 const unsigned char *buf, size_t blen,
707 const mbedtls_ecp_point *Q,
708 const mbedtls_mpi *r,
709 const mbedtls_mpi *s)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200710{
Hanno Becker319ae112018-12-14 16:43:29 +0000711 ECDSA_VALIDATE_RET( grp != NULL );
712 ECDSA_VALIDATE_RET( Q != NULL );
713 ECDSA_VALIDATE_RET( r != NULL );
714 ECDSA_VALIDATE_RET( s != NULL );
715 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
716
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200717 return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
718}
Ron Eldor936d2842018-11-01 13:05:52 +0200719#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200720
721/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100722 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100725 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200726{
Janos Follath24eed8d2019-11-22 13:21:35 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200729 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200730 size_t len = 0;
731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
733 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
736 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
737 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200738
739 memcpy( sig, p, len );
740 *slen = len;
741
742 return( 0 );
743}
744
745/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100746 * Compute and write signature
747 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200748int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
749 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100750 const unsigned char *hash, size_t hlen,
751 unsigned char *sig, size_t *slen,
752 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200753 void *p_rng,
754 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100755{
Janos Follath24eed8d2019-11-22 13:21:35 +0000756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_mpi r, s;
Hanno Becker319ae112018-12-14 16:43:29 +0000758 ECDSA_VALIDATE_RET( ctx != NULL );
759 ECDSA_VALIDATE_RET( hash != NULL );
760 ECDSA_VALIDATE_RET( sig != NULL );
761 ECDSA_VALIDATE_RET( slen != NULL );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_mpi_init( &r );
764 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200767 MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
Janos Follathdca667a2019-01-04 14:32:30 +0000768 hash, hlen, md_alg, f_rng,
769 p_rng, rs_ctx ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200770#else
771 (void) md_alg;
772
Ron Eldor8493f802018-11-01 11:32:15 +0200773#if defined(MBEDTLS_ECDSA_SIGN_ALT)
774 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
775 hash, hlen, f_rng, p_rng ) );
776#else
Janos Follathdca667a2019-01-04 14:32:30 +0000777 /* Use the same RNG for both blinding and ephemeral key generation */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200778 MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
Janos Follathdca667a2019-01-04 14:32:30 +0000779 hash, hlen, f_rng, p_rng, f_rng,
780 p_rng, rs_ctx ) );
Ron Eldor936d2842018-11-01 13:05:52 +0200781#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200782#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200785
786cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_mpi_free( &r );
788 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200789
790 return( ret );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100791}
792
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200793/*
794 * Compute and write signature
795 */
Hanno Becker319ae112018-12-14 16:43:29 +0000796int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
797 mbedtls_md_type_t md_alg,
798 const unsigned char *hash, size_t hlen,
799 unsigned char *sig, size_t *slen,
800 int (*f_rng)(void *, unsigned char *, size_t),
801 void *p_rng )
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200802{
Hanno Becker319ae112018-12-14 16:43:29 +0000803 ECDSA_VALIDATE_RET( ctx != NULL );
804 ECDSA_VALIDATE_RET( hash != NULL );
805 ECDSA_VALIDATE_RET( sig != NULL );
806 ECDSA_VALIDATE_RET( slen != NULL );
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200807 return( mbedtls_ecdsa_write_signature_restartable(
808 ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
809}
810
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200811#if !defined(MBEDTLS_DEPRECATED_REMOVED) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 defined(MBEDTLS_ECDSA_DETERMINISTIC)
813int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100814 const unsigned char *hash, size_t hlen,
815 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 mbedtls_md_type_t md_alg )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100817{
Hanno Becker319ae112018-12-14 16:43:29 +0000818 ECDSA_VALIDATE_RET( ctx != NULL );
819 ECDSA_VALIDATE_RET( hash != NULL );
820 ECDSA_VALIDATE_RET( sig != NULL );
821 ECDSA_VALIDATE_RET( slen != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200823 NULL, NULL ) );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100824}
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200825#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100826
827/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200828 * Read and check signature
829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200831 const unsigned char *hash, size_t hlen,
832 const unsigned char *sig, size_t slen )
833{
Hanno Becker319ae112018-12-14 16:43:29 +0000834 ECDSA_VALIDATE_RET( ctx != NULL );
835 ECDSA_VALIDATE_RET( hash != NULL );
836 ECDSA_VALIDATE_RET( sig != NULL );
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200837 return( mbedtls_ecdsa_read_signature_restartable(
838 ctx, hash, hlen, sig, slen, NULL ) );
839}
840
841/*
842 * Restartable read and check signature
843 */
844int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
845 const unsigned char *hash, size_t hlen,
846 const unsigned char *sig, size_t slen,
847 mbedtls_ecdsa_restart_ctx *rs_ctx )
848{
Janos Follath24eed8d2019-11-22 13:21:35 +0000849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200850 unsigned char *p = (unsigned char *) sig;
851 const unsigned char *end = sig + slen;
852 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_mpi r, s;
Hanno Becker319ae112018-12-14 16:43:29 +0000854 ECDSA_VALIDATE_RET( ctx != NULL );
855 ECDSA_VALIDATE_RET( hash != NULL );
856 ECDSA_VALIDATE_RET( sig != NULL );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_mpi_init( &r );
859 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
862 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200865 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200866 }
867
868 if( p + len != end )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
871 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200872 goto cleanup;
873 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
876 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200879 goto cleanup;
880 }
Ron Eldor8493f802018-11-01 11:32:15 +0200881#if defined(MBEDTLS_ECDSA_VERIFY_ALT)
882 if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
883 &ctx->Q, &r, &s ) ) != 0 )
884 goto cleanup;
885#else
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200886 if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
887 &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200888 goto cleanup;
Ron Eldor936d2842018-11-01 13:05:52 +0200889#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200890
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200891 /* At this point we know that the buffer starts with a valid signature.
892 * Return 0 if the buffer just contains the signature, and a specific
893 * error code if the valid signature is followed by more data. */
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200894 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200896
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200897cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_mpi_free( &r );
899 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200900
901 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200902}
903
Ron Eldor314adb62017-10-10 18:28:25 +0300904#if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200905/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200906 * Generate key pair
907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200909 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
910{
Ron Eldoradb52342018-12-17 10:06:12 +0200911 int ret = 0;
Hanno Becker319ae112018-12-14 16:43:29 +0000912 ECDSA_VALIDATE_RET( ctx != NULL );
913 ECDSA_VALIDATE_RET( f_rng != NULL );
914
Ron Eldoradb52342018-12-17 10:06:12 +0200915 ret = mbedtls_ecp_group_load( &ctx->grp, gid );
916 if( ret != 0 )
917 return( ret );
918
919 return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d,
920 &ctx->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200921}
Ron Eldor936d2842018-11-01 13:05:52 +0200922#endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200923
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200924/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200926 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200928{
Janos Follath24eed8d2019-11-22 13:21:35 +0000929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker319ae112018-12-14 16:43:29 +0000930 ECDSA_VALIDATE_RET( ctx != NULL );
931 ECDSA_VALIDATE_RET( key != NULL );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
934 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
935 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100938 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200939
940 return( ret );
941}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200942
943/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200944 * Initialize context
945 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200947{
Hanno Becker319ae112018-12-14 16:43:29 +0000948 ECDSA_VALIDATE( ctx != NULL );
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200951}
952
953/*
954 * Free context
955 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200957{
Hanno Becker319ae112018-12-14 16:43:29 +0000958 if( ctx == NULL )
959 return;
960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_ecp_keypair_free( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200962}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100963
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200964#if defined(MBEDTLS_ECP_RESTARTABLE)
965/*
966 * Initialize a restart context
967 */
968void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
969{
Hanno Becker319ae112018-12-14 16:43:29 +0000970 ECDSA_VALIDATE( ctx != NULL );
971
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200972 mbedtls_ecp_restart_init( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200973
974 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200975 ctx->sig = NULL;
976#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
977 ctx->det = NULL;
978#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200979}
980
981/*
982 * Free the components of a restart context
983 */
984void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
985{
Hanno Becker319ae112018-12-14 16:43:29 +0000986 if( ctx == NULL )
987 return;
988
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200989 mbedtls_ecp_restart_free( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200990
991 ecdsa_restart_ver_free( ctx->ver );
992 mbedtls_free( ctx->ver );
993 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200994
995 ecdsa_restart_sig_free( ctx->sig );
996 mbedtls_free( ctx->sig );
997 ctx->sig = NULL;
998
999#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1000 ecdsa_restart_det_free( ctx->det );
1001 mbedtls_free( ctx->det );
1002 ctx->det = NULL;
1003#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +02001004}
1005#endif /* MBEDTLS_ECP_RESTARTABLE */
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#endif /* MBEDTLS_ECDSA_C */