blob: da8df9cde2f9dafe5f2a52d1f643157c64126205 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/*
2 * Elliptic curve DSA
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010045 */
46
47/*
48 * References:
49 *
50 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
51 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#endif
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010060
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/ecdsa.h"
62#include "mbedtls/asn1write.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010063
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <string.h>
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard7845fc02014-01-27 14:24:03 +010068#endif
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +010069
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020070#if defined(MBEDTLS_PLATFORM_C)
71#include "mbedtls/platform.h"
72#else
73#include <stdlib.h>
74#define mbedtls_calloc calloc
75#define mbedtls_free free
76#endif
77
Hanno Becker319ae112018-12-14 16:43:29 +000078#include "mbedtls/platform_util.h"
79
80/* Parameter validation macros based on platform_util.h */
81#define ECDSA_VALIDATE_RET( cond ) \
82 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA )
83#define ECDSA_VALIDATE( cond ) \
84 MBEDTLS_INTERNAL_VALIDATE( cond )
85
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020086#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020087
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020088/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +020089 * Sub-context for ecdsa_verify()
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020090 */
91struct mbedtls_ecdsa_restart_ver
92{
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +020093 mbedtls_mpi u1, u2; /* intermediate values */
94 enum { /* what to do next? */
95 ecdsa_ver_init = 0, /* getting started */
96 ecdsa_ver_muladd, /* muladd step */
97 } state;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020098};
99
100/*
101 * Init verify restart sub-context
102 */
103static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx )
104{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200105 mbedtls_mpi_init( &ctx->u1 );
106 mbedtls_mpi_init( &ctx->u2 );
107 ctx->state = ecdsa_ver_init;
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200108}
109
110/*
111 * Free the components of a verify restart sub-context
112 */
113static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx )
114{
115 if( ctx == NULL )
116 return;
117
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200118 mbedtls_mpi_free( &ctx->u1 );
119 mbedtls_mpi_free( &ctx->u2 );
120
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200121 ecdsa_restart_ver_init( ctx );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200122}
123
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200124/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +0200125 * Sub-context for ecdsa_sign()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200126 */
127struct mbedtls_ecdsa_restart_sig
128{
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200129 int sign_tries;
130 int key_tries;
131 mbedtls_mpi k; /* per-signature random */
132 mbedtls_mpi r; /* r value */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200133 enum { /* what to do next? */
134 ecdsa_sig_init = 0, /* getting started */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200135 ecdsa_sig_mul, /* doing ecp_mul() */
136 ecdsa_sig_modn, /* mod N computations */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200137 } state;
138};
139
140/*
141 * Init verify sign sub-context
142 */
143static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx )
144{
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200145 ctx->sign_tries = 0;
146 ctx->key_tries = 0;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200147 mbedtls_mpi_init( &ctx->k );
148 mbedtls_mpi_init( &ctx->r );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200149 ctx->state = ecdsa_sig_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200150}
151
152/*
153 * Free the components of a sign restart sub-context
154 */
155static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx )
156{
157 if( ctx == NULL )
158 return;
159
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200160 mbedtls_mpi_free( &ctx->k );
161 mbedtls_mpi_free( &ctx->r );
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200162}
163
164#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
165/*
Manuel Pégourié-Gonnarda4dd7832017-09-07 11:11:39 +0200166 * Sub-context for ecdsa_sign_det()
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200167 */
168struct mbedtls_ecdsa_restart_det
169{
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200170 mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200171 enum { /* what to do next? */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200172 ecdsa_det_init = 0, /* getting started */
173 ecdsa_det_sign, /* make signature */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200174 } state;
175};
176
177/*
178 * Init verify sign_det sub-context
179 */
180static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx )
181{
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200182 mbedtls_hmac_drbg_init( &ctx->rng_ctx );
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200183 ctx->state = ecdsa_det_init;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200184}
185
186/*
187 * Free the components of a sign_det restart sub-context
188 */
189static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx )
190{
191 if( ctx == NULL )
192 return;
193
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200194 mbedtls_hmac_drbg_free( &ctx->rng_ctx );
195
Manuel Pégourié-Gonnard5bd38b12017-08-23 16:55:59 +0200196 ecdsa_restart_det_init( ctx );
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200197}
198#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
199
Hanno Becker2c5ef112019-07-19 12:42:21 +0100200#define ECDSA_RS_ECP ( rs_ctx == NULL ? NULL : &rs_ctx->ecp )
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200201
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200202/* Utility macro for checking and updating ops budget */
203#define ECDSA_BUDGET( ops ) \
Hanno Becker2c5ef112019-07-19 12:42:21 +0100204 MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, ECDSA_RS_ECP, ops ) );
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200205
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200206/* Call this when entering a function that needs its own sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200207#define ECDSA_RS_ENTER( SUB ) do { \
208 /* reset ops count for this call if top-level */ \
209 if( rs_ctx != NULL && rs_ctx->ecp.depth++ == 0 ) \
210 rs_ctx->ecp.ops_done = 0; \
211 \
212 /* set up our own sub-context if needed */ \
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200213 if( mbedtls_ecp_restart_is_enabled() && \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200214 rs_ctx != NULL && rs_ctx->SUB == NULL ) \
215 { \
216 rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \
217 if( rs_ctx->SUB == NULL ) \
218 return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \
219 \
220 ecdsa_restart_## SUB ##_init( rs_ctx->SUB ); \
221 } \
222} while( 0 )
223
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200224/* Call this when leaving a function that needs its own sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200225#define ECDSA_RS_LEAVE( SUB ) do { \
226 /* clear our sub-context when not in progress (done or error) */ \
Manuel Pégourié-Gonnardb948f7d2017-08-23 17:58:40 +0200227 if( rs_ctx != NULL && rs_ctx->SUB != NULL && \
228 ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) \
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200229 { \
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200230 ecdsa_restart_## SUB ##_free( rs_ctx->SUB ); \
231 mbedtls_free( rs_ctx->SUB ); \
232 rs_ctx->SUB = NULL; \
233 } \
234 \
235 if( rs_ctx != NULL ) \
236 rs_ctx->ecp.depth--; \
237} while( 0 )
238
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200239#else /* MBEDTLS_ECP_RESTARTABLE */
240
241#define ECDSA_RS_ECP NULL
242
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200243#define ECDSA_BUDGET( ops ) /* no-op; for compatibility */
244
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200245#define ECDSA_RS_ENTER( SUB ) (void) rs_ctx
246#define ECDSA_RS_LEAVE( SUB ) (void) rs_ctx
247
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200248#endif /* MBEDTLS_ECP_RESTARTABLE */
249
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100250/*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100251 * Derive a suitable integer for group grp from a buffer of length len
252 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100255 const unsigned char *buf, size_t blen )
256{
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100257 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +0200258 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100259 size_t use_size = blen > n_size ? n_size : blen;
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100262 if( use_size * 8 > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100264
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100265 /* While at it, reduce modulo N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
267 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
Manuel Pégourié-Gonnard461d4162014-01-06 10:16:28 +0100268
Manuel Pégourié-Gonnard53048122014-01-03 12:55:15 +0100269cleanup:
270 return( ret );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100271}
272
Ron Eldor936d2842018-11-01 13:05:52 +0200273#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100274/*
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100275 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
276 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
277 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200278static int ecdsa_sign_restartable( mbedtls_ecp_group *grp,
279 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200281 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Janos Follathf1713e92019-01-04 14:32:30 +0000282 int (*f_rng_blind)(void *, unsigned char *, size_t),
283 void *p_rng_blind,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200284 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100285{
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200286 int ret, key_tries, sign_tries;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200287 int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_ecp_point R;
289 mbedtls_mpi k, e, t;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200290 mbedtls_mpi *pk = &k, *pr = r;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100291
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100292 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
293 if( grp->N.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100295
Darryl Greenc64a48b2017-11-17 17:09:17 +0000296 /* Make sure d is in range 1..n-1 */
297 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
298 return( MBEDTLS_ERR_ECP_INVALID_KEY );
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_ecp_point_init( &R );
301 mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100302
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200303 ECDSA_RS_ENTER( sig );
304
305#if defined(MBEDTLS_ECP_RESTARTABLE)
306 if( rs_ctx != NULL && rs_ctx->sig != NULL )
307 {
308 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200309 p_sign_tries = &rs_ctx->sig->sign_tries;
310 p_key_tries = &rs_ctx->sig->key_tries;
311 pk = &rs_ctx->sig->k;
312 pr = &rs_ctx->sig->r;
313
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200314 /* jump to current step */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200315 if( rs_ctx->sig->state == ecdsa_sig_mul )
316 goto mul;
317 if( rs_ctx->sig->state == ecdsa_sig_modn )
318 goto modn;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200319 }
320#endif /* MBEDTLS_ECP_RESTARTABLE */
321
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200322 *p_sign_tries = 0;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100323 do
324 {
Manuel Pégourié-Gonnard42b81942020-01-24 12:11:56 +0100325 if( (*p_sign_tries)++ > 10 )
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200326 {
327 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
328 goto cleanup;
329 }
330
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100331 /*
332 * Steps 1-3: generate a suitable ephemeral keypair
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100333 * and set r = xR mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100334 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200335 *p_key_tries = 0;
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100336 do
337 {
Manuel Pégourié-Gonnard42b81942020-01-24 12:11:56 +0100338 if( (*p_key_tries)++ > 10 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200341 goto cleanup;
342 }
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +0200343
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200344 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, pk, f_rng, p_rng ) );
Manuel Pégourié-Gonnard50b63ba2017-04-25 12:57:22 +0200345
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200346#if defined(MBEDTLS_ECP_RESTARTABLE)
347 if( rs_ctx != NULL && rs_ctx->sig != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200348 rs_ctx->sig->state = ecdsa_sig_mul;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200349
350mul:
351#endif
352 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &R, pk, &grp->G,
Janos Follathf1713e92019-01-04 14:32:30 +0000353 f_rng_blind,
354 p_rng_blind,
355 ECDSA_RS_ECP ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200356 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pr, &R.X, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100357 }
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200358 while( mbedtls_mpi_cmp_int( pr, 0 ) == 0 );
359
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200360#if defined(MBEDTLS_ECP_RESTARTABLE)
361 if( rs_ctx != NULL && rs_ctx->sig != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200362 rs_ctx->sig->state = ecdsa_sig_modn;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200363
364modn:
365#endif
366 /*
367 * Accounting for everything up to the end of the loop
368 * (step 6, but checking now avoids saving e and t)
369 */
370 ECDSA_BUDGET( MBEDTLS_ECP_OPS_INV + 4 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100371
372 /*
373 * Step 5: derive MPI from hashed message
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100376
377 /*
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200378 * Generate a random value to blind inv_mod in next step,
379 * avoiding a potential timing leak.
380 */
Janos Follathf1713e92019-01-04 14:32:30 +0000381 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, &t, f_rng_blind,
382 p_rng_blind ) );
Manuel Pégourié-Gonnarddd75c312014-03-31 11:55:42 +0200383
384 /*
385 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100386 */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200387 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, pr, d ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
389 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200390 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pk, pk, &t ) );
Janos Follathd65df1f2019-10-17 10:18:51 +0100391 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pk, pk, &grp->N ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200392 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, pk, &grp->N ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
394 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100395 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100397
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200398#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard28d16282017-08-23 17:33:27 +0200399 if( rs_ctx != NULL && rs_ctx->sig != NULL )
400 mbedtls_mpi_copy( r, pr );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200401#endif
402
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100403cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_ecp_point_free( &R );
405 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100406
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200407 ECDSA_RS_LEAVE( sig );
408
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100409 return( ret );
410}
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100411
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200412/*
413 * Compute ECDSA signature of a hashed message
414 */
415int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
416 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
417 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
418{
Hanno Becker319ae112018-12-14 16:43:29 +0000419 ECDSA_VALIDATE_RET( grp != NULL );
420 ECDSA_VALIDATE_RET( r != NULL );
421 ECDSA_VALIDATE_RET( s != NULL );
422 ECDSA_VALIDATE_RET( d != NULL );
423 ECDSA_VALIDATE_RET( f_rng != NULL );
424 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
425
Janos Follathf1713e92019-01-04 14:32:30 +0000426 /* Use the same RNG for both blinding and ephemeral key generation */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200427 return( ecdsa_sign_restartable( grp, r, s, d, buf, blen,
Janos Follathf1713e92019-01-04 14:32:30 +0000428 f_rng, p_rng, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200429}
Ron Eldor936d2842018-11-01 13:05:52 +0200430#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100433/*
434 * Deterministic signature wrapper
435 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200436static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp,
437 mbedtls_mpi *r, mbedtls_mpi *s,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200439 mbedtls_md_type_t md_alg,
Janos Follathf1713e92019-01-04 14:32:30 +0000440 int (*f_rng_blind)(void *, unsigned char *, size_t),
441 void *p_rng_blind,
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200442 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100443{
444 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_hmac_drbg_context rng_ctx;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200446 mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100448 size_t grp_len = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 const mbedtls_md_info_t *md_info;
450 mbedtls_mpi h;
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
453 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_mpi_init( &h );
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200456 mbedtls_hmac_drbg_init( &rng_ctx );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100457
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200458 ECDSA_RS_ENTER( det );
459
460#if defined(MBEDTLS_ECP_RESTARTABLE)
461 if( rs_ctx != NULL && rs_ctx->det != NULL )
462 {
463 /* redirect to our context */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200464 p_rng = &rs_ctx->det->rng_ctx;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200465
466 /* jump to current step */
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200467 if( rs_ctx->det->state == ecdsa_det_sign )
468 goto sign;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200469 }
470#endif /* MBEDTLS_ECP_RESTARTABLE */
471
Manuel Pégourié-Gonnardf42bca62014-01-06 15:05:01 +0100472 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
474 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
475 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200476 mbedtls_hmac_drbg_seed_buf( p_rng, md_info, data, 2 * grp_len );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100477
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200478#if defined(MBEDTLS_ECP_RESTARTABLE)
479 if( rs_ctx != NULL && rs_ctx->det != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200480 rs_ctx->det->state = ecdsa_det_sign;
Manuel Pégourié-Gonnardaf081f52017-04-25 13:44:19 +0200481
482sign:
483#endif
Ron Eldor8493f802018-11-01 11:32:15 +0200484#if defined(MBEDTLS_ECDSA_SIGN_ALT)
485 ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
486 mbedtls_hmac_drbg_random, p_rng );
487#else
Janos Follathf1713e92019-01-04 14:32:30 +0000488 if( f_rng_blind != NULL )
489 ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
490 mbedtls_hmac_drbg_random, p_rng,
491 f_rng_blind, p_rng_blind, rs_ctx );
492 else
Janos Follathd73f6132019-01-07 17:27:56 +0000493 {
494 mbedtls_hmac_drbg_context *p_rng_blind_det;
495
496#if !defined(MBEDTLS_ECP_RESTARTABLE)
Janos Follathf1713e92019-01-04 14:32:30 +0000497 /*
Janos Follathd73f6132019-01-07 17:27:56 +0000498 * To avoid reusing rng_ctx and risking incorrect behavior we seed a
499 * second HMAC-DRBG with the same seed. We also apply a label to avoid
500 * reusing the bits of the ephemeral key for blinding and eliminate the
501 * risk that they leak this way.
502 */
503 const char* blind_label = "BLINDING CONTEXT";
504 mbedtls_hmac_drbg_context rng_ctx_blind;
505
506 mbedtls_hmac_drbg_init( &rng_ctx_blind );
507 p_rng_blind_det = &rng_ctx_blind;
508
509 mbedtls_hmac_drbg_seed_buf( p_rng_blind_det, md_info,
510 data, 2 * grp_len );
511 ret = mbedtls_hmac_drbg_update_ret( p_rng_blind_det,
512 (const unsigned char*) blind_label,
513 strlen( blind_label ) );
514 if( ret != 0 )
515 {
516 mbedtls_hmac_drbg_free( &rng_ctx_blind );
517 goto cleanup;
518 }
519#else
520 /*
521 * In the case of restartable computations we would either need to store
522 * the second RNG in the restart context too or set it up at every
523 * restart. The first option would penalize the correct application of
524 * the function and the second would defeat the purpose of the
525 * restartable feature.
526 *
527 * Therefore in this case we reuse the original RNG. This comes with the
528 * price that the resulting signature might not be a valid deterministic
529 * ECDSA signature with a very low probability (same magnitude as
530 * successfully guessing the private key). However even then it is still
531 * a valid ECDSA signature.
532 */
533 p_rng_blind_det = p_rng;
534#endif /* MBEDTLS_ECP_RESTARTABLE */
535
536 /*
537 * Since the output of the RNGs is always the same for the same key and
538 * message, this limits the efficiency of blinding and leaks information
539 * through side channels. After mbedtls_ecdsa_sign_det() is removed NULL
540 * won't be a valid value for f_rng_blind anymore. Therefore it should
541 * be checked by the caller and this branch and check can be removed.
Janos Follathf1713e92019-01-04 14:32:30 +0000542 */
543 ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen,
544 mbedtls_hmac_drbg_random, p_rng,
Janos Follathd73f6132019-01-07 17:27:56 +0000545 mbedtls_hmac_drbg_random, p_rng_blind_det,
546 rs_ctx );
547
548#if !defined(MBEDTLS_ECP_RESTARTABLE)
549 mbedtls_hmac_drbg_free( &rng_ctx_blind );
550#endif
551 }
Ron Eldor936d2842018-11-01 13:05:52 +0200552#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100553
554cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_hmac_drbg_free( &rng_ctx );
556 mbedtls_mpi_free( &h );
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100557
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200558 ECDSA_RS_LEAVE( det );
559
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100560 return( ret );
561}
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200562
563/*
Janos Follathf1713e92019-01-04 14:32:30 +0000564 * Deterministic signature wrappers
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200565 */
Janos Follathf1713e92019-01-04 14:32:30 +0000566int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
567 mbedtls_mpi *s, const mbedtls_mpi *d,
568 const unsigned char *buf, size_t blen,
569 mbedtls_md_type_t md_alg )
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200570{
Hanno Becker319ae112018-12-14 16:43:29 +0000571 ECDSA_VALIDATE_RET( grp != NULL );
572 ECDSA_VALIDATE_RET( r != NULL );
573 ECDSA_VALIDATE_RET( s != NULL );
574 ECDSA_VALIDATE_RET( d != NULL );
575 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
576
Janos Follathf1713e92019-01-04 14:32:30 +0000577 return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
578 NULL, NULL, NULL ) );
579}
580
581int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
582 mbedtls_mpi *s, const mbedtls_mpi *d,
583 const unsigned char *buf, size_t blen,
584 mbedtls_md_type_t md_alg,
585 int (*f_rng_blind)(void *, unsigned char *,
586 size_t),
587 void *p_rng_blind )
588{
589 ECDSA_VALIDATE_RET( grp != NULL );
590 ECDSA_VALIDATE_RET( r != NULL );
591 ECDSA_VALIDATE_RET( s != NULL );
592 ECDSA_VALIDATE_RET( d != NULL );
593 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
594 ECDSA_VALIDATE_RET( f_rng_blind != NULL );
595
596 return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg,
597 f_rng_blind, p_rng_blind, NULL ) );
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Paul Bakker9f3c7d72014-01-23 16:11:14 +0100600
Ron Eldor936d2842018-11-01 13:05:52 +0200601#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100602/*
603 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
604 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
605 */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200606static int ecdsa_verify_restartable( mbedtls_ecp_group *grp,
607 const unsigned char *buf, size_t blen,
608 const mbedtls_ecp_point *Q,
609 const mbedtls_mpi *r, const mbedtls_mpi *s,
610 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100611{
612 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_mpi e, s_inv, u1, u2;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200614 mbedtls_ecp_point R;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200615 mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100616
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200617 mbedtls_ecp_point_init( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200618 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv );
619 mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100620
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100621 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
622 if( grp->N.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100624
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200625 ECDSA_RS_ENTER( ver );
626
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200627#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200628 if( rs_ctx != NULL && rs_ctx->ver != NULL )
629 {
630 /* redirect to our context */
631 pu1 = &rs_ctx->ver->u1;
632 pu2 = &rs_ctx->ver->u2;
633
634 /* jump to current step */
635 if( rs_ctx->ver->state == ecdsa_ver_muladd )
636 goto muladd;
637 }
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200638#endif /* MBEDTLS_ECP_RESTARTABLE */
639
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100640 /*
641 * Step 1: make sure r and s are in range 1..n-1
642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
644 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200647 goto cleanup;
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100648 }
649
650 /*
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100651 * Step 3: derive MPI from hashed message
652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100654
655 /*
656 * Step 4: u1 = e / s mod n, u2 = r / s mod n
657 */
Manuel Pégourié-Gonnardbfa19722017-08-23 17:39:18 +0200658 ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 );
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100661
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200662 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) );
663 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100664
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200665 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) );
666 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100667
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200668#if defined(MBEDTLS_ECP_RESTARTABLE)
669 if( rs_ctx != NULL && rs_ctx->ver != NULL )
Manuel Pégourié-Gonnard63481812017-08-24 11:16:01 +0200670 rs_ctx->ver->state = ecdsa_ver_muladd;
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200671
672muladd:
673#endif
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100674 /*
675 * Step 5: R = u1 G + u2 Q
676 */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200677 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp,
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +0200678 &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 if( mbedtls_ecp_is_zero( &R ) )
Paul Bakkercca998a2013-07-26 14:20:53 +0200681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200683 goto cleanup;
684 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100685
686 /*
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100687 * Step 6: convert xR to an integer (no-op)
688 * Step 7: reduce xR mod n (gives v)
689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
Manuel Pégourié-Gonnard178d9ba2013-10-29 10:45:28 +0100691
692 /*
693 * Step 8: check if v (that is, R.X) is equal to r
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
Paul Bakkercca998a2013-07-26 14:20:53 +0200696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
Paul Bakkercca998a2013-07-26 14:20:53 +0200698 goto cleanup;
699 }
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100700
701cleanup:
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +0200702 mbedtls_ecp_point_free( &R );
Manuel Pégourié-Gonnard411079f2017-04-20 15:41:08 +0200703 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv );
704 mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100705
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200706 ECDSA_RS_LEAVE( ver );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200707
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100708 return( ret );
709}
710
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200711/*
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200712 * Verify ECDSA signature of hashed message
713 */
714int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Hanno Becker319ae112018-12-14 16:43:29 +0000715 const unsigned char *buf, size_t blen,
716 const mbedtls_ecp_point *Q,
717 const mbedtls_mpi *r,
718 const mbedtls_mpi *s)
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200719{
Hanno Becker319ae112018-12-14 16:43:29 +0000720 ECDSA_VALIDATE_RET( grp != NULL );
721 ECDSA_VALIDATE_RET( Q != NULL );
722 ECDSA_VALIDATE_RET( r != NULL );
723 ECDSA_VALIDATE_RET( s != NULL );
724 ECDSA_VALIDATE_RET( buf != NULL || blen == 0 );
725
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200726 return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) );
727}
Ron Eldor936d2842018-11-01 13:05:52 +0200728#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200729
730/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100731 * Convert a signature (given by context) to ASN.1
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100734 unsigned char *sig, size_t *slen )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200735{
736 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
Manuel Pégourié-Gonnard4cf06862013-09-16 12:07:45 +0200738 unsigned char *p = buf + sizeof( buf );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200739 size_t len = 0;
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
742 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
745 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
746 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200747
748 memcpy( sig, p, len );
749 *slen = len;
750
751 return( 0 );
752}
753
754/*
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100755 * Compute and write signature
756 */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200757int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
758 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100759 const unsigned char *hash, size_t hlen,
760 unsigned char *sig, size_t *slen,
761 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200762 void *p_rng,
763 mbedtls_ecdsa_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100764{
765 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 mbedtls_mpi r, s;
Hanno Becker319ae112018-12-14 16:43:29 +0000767 ECDSA_VALIDATE_RET( ctx != NULL );
768 ECDSA_VALIDATE_RET( hash != NULL );
769 ECDSA_VALIDATE_RET( sig != NULL );
770 ECDSA_VALIDATE_RET( slen != NULL );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_mpi_init( &r );
773 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200776 MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d,
Janos Follathf1713e92019-01-04 14:32:30 +0000777 hash, hlen, md_alg, f_rng,
778 p_rng, rs_ctx ) );
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200779#else
780 (void) md_alg;
781
Ron Eldor8493f802018-11-01 11:32:15 +0200782#if defined(MBEDTLS_ECDSA_SIGN_ALT)
783 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
784 hash, hlen, f_rng, p_rng ) );
785#else
Janos Follathf1713e92019-01-04 14:32:30 +0000786 /* Use the same RNG for both blinding and ephemeral key generation */
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200787 MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d,
Janos Follathf1713e92019-01-04 14:32:30 +0000788 hash, hlen, f_rng, p_rng, f_rng,
789 p_rng, rs_ctx ) );
Ron Eldor936d2842018-11-01 13:05:52 +0200790#endif /* MBEDTLS_ECDSA_SIGN_ALT */
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200791#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200794
795cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_mpi_free( &r );
797 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200798
799 return( ret );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100800}
801
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200802/*
803 * Compute and write signature
804 */
Hanno Becker319ae112018-12-14 16:43:29 +0000805int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
806 mbedtls_md_type_t md_alg,
807 const unsigned char *hash, size_t hlen,
808 unsigned char *sig, size_t *slen,
809 int (*f_rng)(void *, unsigned char *, size_t),
810 void *p_rng )
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200811{
Hanno Becker319ae112018-12-14 16:43:29 +0000812 ECDSA_VALIDATE_RET( ctx != NULL );
813 ECDSA_VALIDATE_RET( hash != NULL );
814 ECDSA_VALIDATE_RET( sig != NULL );
815 ECDSA_VALIDATE_RET( slen != NULL );
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200816 return( mbedtls_ecdsa_write_signature_restartable(
817 ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) );
818}
819
Ron Eldor5ed8c1e2018-11-05 14:04:26 +0200820#if !defined(MBEDTLS_DEPRECATED_REMOVED) && \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821 defined(MBEDTLS_ECDSA_DETERMINISTIC)
822int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100823 const unsigned char *hash, size_t hlen,
824 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_md_type_t md_alg )
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100826{
Hanno Becker319ae112018-12-14 16:43:29 +0000827 ECDSA_VALIDATE_RET( ctx != NULL );
828 ECDSA_VALIDATE_RET( hash != NULL );
829 ECDSA_VALIDATE_RET( sig != NULL );
830 ECDSA_VALIDATE_RET( slen != NULL );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200832 NULL, NULL ) );
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100833}
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200834#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100835
836/*
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200837 * Read and check signature
838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200840 const unsigned char *hash, size_t hlen,
841 const unsigned char *sig, size_t slen )
842{
Hanno Becker319ae112018-12-14 16:43:29 +0000843 ECDSA_VALIDATE_RET( ctx != NULL );
844 ECDSA_VALIDATE_RET( hash != NULL );
845 ECDSA_VALIDATE_RET( sig != NULL );
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200846 return( mbedtls_ecdsa_read_signature_restartable(
847 ctx, hash, hlen, sig, slen, NULL ) );
848}
849
850/*
851 * Restartable read and check signature
852 */
853int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
854 const unsigned char *hash, size_t hlen,
855 const unsigned char *sig, size_t slen,
856 mbedtls_ecdsa_restart_ctx *rs_ctx )
857{
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200858 int ret;
859 unsigned char *p = (unsigned char *) sig;
860 const unsigned char *end = sig + slen;
861 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_mpi r, s;
Hanno Becker319ae112018-12-14 16:43:29 +0000863 ECDSA_VALIDATE_RET( ctx != NULL );
864 ECDSA_VALIDATE_RET( hash != NULL );
865 ECDSA_VALIDATE_RET( sig != NULL );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_mpi_init( &r );
868 mbedtls_mpi_init( &s );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
871 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200874 goto cleanup;
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200875 }
876
877 if( p + len != end )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
880 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200881 goto cleanup;
882 }
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
885 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200888 goto cleanup;
889 }
Ron Eldor8493f802018-11-01 11:32:15 +0200890#if defined(MBEDTLS_ECDSA_VERIFY_ALT)
891 if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
892 &ctx->Q, &r, &s ) ) != 0 )
893 goto cleanup;
894#else
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200895 if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen,
896 &ctx->Q, &r, &s, rs_ctx ) ) != 0 )
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200897 goto cleanup;
Ron Eldor936d2842018-11-01 13:05:52 +0200898#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200899
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200900 /* At this point we know that the buffer starts with a valid signature.
901 * Return 0 if the buffer just contains the signature, and a specific
902 * error code if the valid signature is followed by more data. */
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200903 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200905
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200906cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_mpi_free( &r );
908 mbedtls_mpi_free( &s );
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +0200909
910 return( ret );
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200911}
912
Ron Eldor314adb62017-10-10 18:28:25 +0300913#if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200914/*
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200915 * Generate key pair
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200918 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
919{
Ron Eldora04efb82018-12-17 10:06:12 +0200920 int ret = 0;
Hanno Becker319ae112018-12-14 16:43:29 +0000921 ECDSA_VALIDATE_RET( ctx != NULL );
922 ECDSA_VALIDATE_RET( f_rng != NULL );
923
Ron Eldora04efb82018-12-17 10:06:12 +0200924 ret = mbedtls_ecp_group_load( &ctx->grp, gid );
925 if( ret != 0 )
926 return( ret );
927
928 return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d,
929 &ctx->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200930}
Ron Eldor936d2842018-11-01 13:05:52 +0200931#endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200932
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200933/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 * Set context from an mbedtls_ecp_keypair
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200937{
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100938 int ret;
Hanno Becker319ae112018-12-14 16:43:29 +0000939 ECDSA_VALIDATE_RET( ctx != NULL );
940 ECDSA_VALIDATE_RET( key != NULL );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
943 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
944 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_ecdsa_free( ctx );
Manuel Pégourié-Gonnard1001e322013-10-27 14:53:48 +0100947 }
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200948
949 return( ret );
950}
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200951
952/*
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200953 * Initialize context
954 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200956{
Hanno Becker319ae112018-12-14 16:43:29 +0000957 ECDSA_VALIDATE( ctx != NULL );
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 mbedtls_ecp_keypair_init( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200960}
961
962/*
963 * Free context
964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200966{
Hanno Becker319ae112018-12-14 16:43:29 +0000967 if( ctx == NULL )
968 return;
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_ecp_keypair_free( ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200971}
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100972
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200973#if defined(MBEDTLS_ECP_RESTARTABLE)
974/*
975 * Initialize a restart context
976 */
977void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx )
978{
Hanno Becker319ae112018-12-14 16:43:29 +0000979 ECDSA_VALIDATE( ctx != NULL );
980
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200981 mbedtls_ecp_restart_init( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200982
983 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200984 ctx->sig = NULL;
985#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
986 ctx->det = NULL;
987#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200988}
989
990/*
991 * Free the components of a restart context
992 */
993void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx )
994{
Hanno Becker319ae112018-12-14 16:43:29 +0000995 if( ctx == NULL )
996 return;
997
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +0200998 mbedtls_ecp_restart_free( &ctx->ecp );
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200999
1000 ecdsa_restart_ver_free( ctx->ver );
1001 mbedtls_free( ctx->ver );
1002 ctx->ver = NULL;
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +02001003
1004 ecdsa_restart_sig_free( ctx->sig );
1005 mbedtls_free( ctx->sig );
1006 ctx->sig = NULL;
1007
1008#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1009 ecdsa_restart_det_free( ctx->det );
1010 mbedtls_free( ctx->det );
1011 ctx->det = NULL;
1012#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +02001013}
1014#endif /* MBEDTLS_ECP_RESTARTABLE */
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_ECDSA_C */