Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve DSA |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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é-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * References: |
| 24 | * |
| 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
| 26 | */ |
| 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #endif |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/ecdsa.h" |
| 37 | #include "mbedtls/asn1write.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 38 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 39 | #include <string.h> |
| 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/hmac_drbg.h" |
Manuel Pégourié-Gonnard | 7845fc0 | 2014-01-27 14:24:03 +0100 | [diff] [blame] | 43 | #endif |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_PLATFORM_C) |
| 46 | #include "mbedtls/platform.h" |
| 47 | #else |
| 48 | #include <stdlib.h> |
| 49 | #define mbedtls_calloc calloc |
| 50 | #define mbedtls_free free |
| 51 | #endif |
| 52 | |
| 53 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Sub-contect for ecdsa_verify() |
| 57 | */ |
| 58 | struct mbedtls_ecdsa_restart_ver |
| 59 | { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 60 | mbedtls_mpi u1, u2; /* intermediate values */ |
| 61 | enum { /* what to do next? */ |
| 62 | ecdsa_ver_init = 0, /* getting started */ |
| 63 | ecdsa_ver_muladd, /* muladd step */ |
| 64 | } state; |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Init verify restart sub-context |
| 69 | */ |
| 70 | static void ecdsa_restart_ver_init( mbedtls_ecdsa_restart_ver_ctx *ctx ) |
| 71 | { |
| 72 | memset( ctx, 0, sizeof( *ctx ) ); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Free the components of a verify restart sub-context |
| 77 | */ |
| 78 | static void ecdsa_restart_ver_free( mbedtls_ecdsa_restart_ver_ctx *ctx ) |
| 79 | { |
| 80 | if( ctx == NULL ) |
| 81 | return; |
| 82 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 83 | mbedtls_mpi_free( &ctx->u1 ); |
| 84 | mbedtls_mpi_free( &ctx->u2 ); |
| 85 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 86 | memset( ctx, 0, sizeof( *ctx ) ); |
| 87 | } |
| 88 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 89 | /* |
| 90 | * Sub-contect for ecdsa_sign() |
| 91 | */ |
| 92 | struct mbedtls_ecdsa_restart_sig |
| 93 | { |
| 94 | enum { /* what to do next? */ |
| 95 | ecdsa_sig_init = 0, /* getting started */ |
| 96 | } state; |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Init verify sign sub-context |
| 101 | */ |
| 102 | static void ecdsa_restart_sig_init( mbedtls_ecdsa_restart_sig_ctx *ctx ) |
| 103 | { |
| 104 | memset( ctx, 0, sizeof( *ctx ) ); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Free the components of a sign restart sub-context |
| 109 | */ |
| 110 | static void ecdsa_restart_sig_free( mbedtls_ecdsa_restart_sig_ctx *ctx ) |
| 111 | { |
| 112 | if( ctx == NULL ) |
| 113 | return; |
| 114 | |
| 115 | memset( ctx, 0, sizeof( *ctx ) ); |
| 116 | } |
| 117 | |
| 118 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 119 | /* |
| 120 | * Sub-contect for ecdsa_sign_det() |
| 121 | */ |
| 122 | struct mbedtls_ecdsa_restart_det |
| 123 | { |
| 124 | enum { /* what to do next? */ |
| 125 | ecdsa_det_init = 0, /* getting started */ |
| 126 | } state; |
| 127 | }; |
| 128 | |
| 129 | /* |
| 130 | * Init verify sign_det sub-context |
| 131 | */ |
| 132 | static void ecdsa_restart_det_init( mbedtls_ecdsa_restart_det_ctx *ctx ) |
| 133 | { |
| 134 | memset( ctx, 0, sizeof( *ctx ) ); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Free the components of a sign_det restart sub-context |
| 139 | */ |
| 140 | static void ecdsa_restart_det_free( mbedtls_ecdsa_restart_det_ctx *ctx ) |
| 141 | { |
| 142 | if( ctx == NULL ) |
| 143 | return; |
| 144 | |
| 145 | memset( ctx, 0, sizeof( *ctx ) ); |
| 146 | } |
| 147 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
| 148 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 149 | #define ECDSA_RS_ECP &rs_ctx->ecp |
| 150 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 151 | /* Utility macro for checking and updating ops budget */ |
| 152 | #define ECDSA_BUDGET( ops ) \ |
| 153 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, &rs_ctx->ecp, ops ) ); |
| 154 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 155 | #define ECDSA_RS_ENTER( SUB ) do { \ |
| 156 | /* reset ops count for this call if top-level */ \ |
| 157 | if( rs_ctx != NULL && rs_ctx->ecp.depth++ == 0 ) \ |
| 158 | rs_ctx->ecp.ops_done = 0; \ |
| 159 | \ |
| 160 | /* set up our own sub-context if needed */ \ |
| 161 | if( mbedtls_ecp_restart_enabled() && \ |
| 162 | rs_ctx != NULL && rs_ctx->SUB == NULL ) \ |
| 163 | { \ |
| 164 | rs_ctx->SUB = mbedtls_calloc( 1, sizeof( *rs_ctx->SUB ) ); \ |
| 165 | if( rs_ctx->SUB == NULL ) \ |
| 166 | return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); \ |
| 167 | \ |
| 168 | ecdsa_restart_## SUB ##_init( rs_ctx->SUB ); \ |
| 169 | } \ |
| 170 | } while( 0 ) |
| 171 | |
| 172 | #define ECDSA_RS_LEAVE( SUB ) do { \ |
| 173 | /* clear our sub-context when not in progress (done or error) */ \ |
| 174 | if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS ) { \ |
| 175 | ecdsa_restart_## SUB ##_free( rs_ctx->SUB ); \ |
| 176 | mbedtls_free( rs_ctx->SUB ); \ |
| 177 | rs_ctx->SUB = NULL; \ |
| 178 | } \ |
| 179 | \ |
| 180 | if( rs_ctx != NULL ) \ |
| 181 | rs_ctx->ecp.depth--; \ |
| 182 | } while( 0 ) |
| 183 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 184 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 185 | |
| 186 | #define ECDSA_RS_ECP NULL |
| 187 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 188 | #define ECDSA_BUDGET( ops ) /* no-op; for compatibility */ |
| 189 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 190 | #define ECDSA_RS_ENTER( SUB ) (void) rs_ctx |
| 191 | #define ECDSA_RS_LEAVE( SUB ) (void) rs_ctx |
| 192 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 193 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 194 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 195 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 196 | * Derive a suitable integer for group grp from a buffer of length len |
| 197 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
| 198 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 200 | const unsigned char *buf, size_t blen ) |
| 201 | { |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 202 | int ret; |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 203 | size_t n_size = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 204 | size_t use_size = blen > n_size ? n_size : blen; |
| 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) ); |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 207 | if( use_size * 8 > grp->nbits ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) ); |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 209 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 210 | /* While at it, reduce modulo N */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 ) |
| 212 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) ); |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 213 | |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 214 | cleanup: |
| 215 | return( ret ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 219 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 220 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 221 | */ |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 222 | static int ecdsa_sign_restartable( mbedtls_ecp_group *grp, |
| 223 | mbedtls_mpi *r, mbedtls_mpi *s, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 225 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 226 | mbedtls_ecdsa_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 227 | { |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 228 | int ret, key_tries, sign_tries, blind_tries; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | mbedtls_ecp_point R; |
| 230 | mbedtls_mpi k, e, t; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 231 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 232 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 233 | if( grp->N.p == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | mbedtls_ecp_point_init( &R ); |
| 237 | mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 239 | ECDSA_RS_ENTER( sig ); |
| 240 | |
| 241 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 242 | if( rs_ctx != NULL && rs_ctx->sig != NULL ) |
| 243 | { |
| 244 | /* redirect to our context */ |
| 245 | // TODO |
| 246 | |
| 247 | /* jump to current step */ |
| 248 | // TODO |
| 249 | } |
| 250 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 251 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 252 | sign_tries = 0; |
| 253 | do |
| 254 | { |
| 255 | /* |
| 256 | * Steps 1-3: generate a suitable ephemeral keypair |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 257 | * and set r = xR mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 258 | */ |
| 259 | key_tries = 0; |
| 260 | do |
| 261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) ); |
| 263 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 264 | |
| 265 | if( key_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 266 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 267 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 268 | goto cleanup; |
| 269 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 270 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | while( mbedtls_mpi_cmp_int( r, 0 ) == 0 ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * Step 5: derive MPI from hashed message |
| 275 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 277 | |
| 278 | /* |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 279 | * Generate a random value to blind inv_mod in next step, |
| 280 | * avoiding a potential timing leak. |
| 281 | */ |
| 282 | blind_tries = 0; |
| 283 | do |
| 284 | { |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 285 | size_t n_size = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) ); |
| 287 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 288 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | /* See mbedtls_ecp_gen_keypair() */ |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 290 | if( ++blind_tries > 30 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 292 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 || |
| 294 | mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 ); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 298 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) ); |
| 300 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) ); |
| 301 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) ); |
| 302 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) ); |
| 303 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) ); |
| 304 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) ); |
| 305 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 306 | |
| 307 | if( sign_tries++ > 10 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 310 | goto cleanup; |
| 311 | } |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 312 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 313 | while( mbedtls_mpi_cmp_int( s, 0 ) == 0 ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 314 | |
| 315 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | mbedtls_ecp_point_free( &R ); |
| 317 | mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t ); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 318 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 319 | ECDSA_RS_LEAVE( sig ); |
| 320 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 321 | return( ret ); |
| 322 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 323 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 324 | /* |
| 325 | * Compute ECDSA signature of a hashed message |
| 326 | */ |
| 327 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 328 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 329 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 330 | { |
| 331 | return( ecdsa_sign_restartable( grp, r, s, d, buf, blen, |
| 332 | f_rng, p_rng, NULL ) ); |
| 333 | } |
| 334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 336 | /* |
| 337 | * Deterministic signature wrapper |
| 338 | */ |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 339 | static int ecdsa_sign_det_restartable( mbedtls_ecp_group *grp, |
| 340 | mbedtls_mpi *r, mbedtls_mpi *s, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 342 | mbedtls_md_type_t md_alg, |
| 343 | mbedtls_ecdsa_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 344 | { |
| 345 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | mbedtls_hmac_drbg_context rng_ctx; |
| 347 | unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES]; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 348 | size_t grp_len = ( grp->nbits + 7 ) / 8; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | const mbedtls_md_info_t *md_info; |
| 350 | mbedtls_mpi h; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 351 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL ) |
| 353 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | mbedtls_mpi_init( &h ); |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 356 | mbedtls_hmac_drbg_init( &rng_ctx ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 357 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 358 | ECDSA_RS_ENTER( det ); |
| 359 | |
| 360 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 361 | if( rs_ctx != NULL && rs_ctx->det != NULL ) |
| 362 | { |
| 363 | /* redirect to our context */ |
| 364 | // TODO |
| 365 | |
| 366 | /* jump to current step */ |
| 367 | // TODO |
| 368 | } |
| 369 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 370 | |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 371 | /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) ); |
| 373 | MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) ); |
| 374 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) ); |
Manuel Pégourié-Gonnard | f9e9481 | 2015-04-28 22:07:14 +0200 | [diff] [blame] | 375 | mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 377 | ret = ecdsa_sign_restartable( grp, r, s, d, buf, blen, |
| 378 | mbedtls_hmac_drbg_random, &rng_ctx, rs_ctx ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 379 | |
| 380 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | mbedtls_hmac_drbg_free( &rng_ctx ); |
| 382 | mbedtls_mpi_free( &h ); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 383 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 384 | ECDSA_RS_LEAVE( det ); |
| 385 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 386 | return( ret ); |
| 387 | } |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 388 | |
| 389 | /* |
| 390 | * Deterministic signature wrapper |
| 391 | */ |
| 392 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 393 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 394 | mbedtls_md_type_t md_alg ) |
| 395 | { |
| 396 | return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg, NULL ) ); |
| 397 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 399 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 400 | /* |
| 401 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 402 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 403 | */ |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 404 | static int ecdsa_verify_restartable( mbedtls_ecp_group *grp, |
| 405 | const unsigned char *buf, size_t blen, |
| 406 | const mbedtls_ecp_point *Q, |
| 407 | const mbedtls_mpi *r, const mbedtls_mpi *s, |
| 408 | mbedtls_ecdsa_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 409 | { |
| 410 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | mbedtls_mpi e, s_inv, u1, u2; |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 412 | mbedtls_ecp_point R; |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 413 | mbedtls_mpi *pu1 = &u1, *pu2 = &u2; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 414 | |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 415 | mbedtls_ecp_point_init( &R ); |
Manuel Pégourié-Gonnard | 411079f | 2017-04-20 15:41:08 +0200 | [diff] [blame] | 416 | mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); |
| 417 | mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 418 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 419 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
| 420 | if( grp->N.p == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 422 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 423 | ECDSA_RS_ENTER( ver ); |
| 424 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 425 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 426 | if( rs_ctx != NULL && rs_ctx->ver != NULL ) |
| 427 | { |
| 428 | /* redirect to our context */ |
| 429 | pu1 = &rs_ctx->ver->u1; |
| 430 | pu2 = &rs_ctx->ver->u2; |
| 431 | |
| 432 | /* jump to current step */ |
| 433 | if( rs_ctx->ver->state == ecdsa_ver_muladd ) |
| 434 | goto muladd; |
| 435 | } |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 436 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 437 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 438 | /* |
| 439 | * Step 1: make sure r and s are in range 1..n-1 |
| 440 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 441 | if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 || |
| 442 | mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 ) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 443 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 445 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Additional precaution: make sure Q is valid |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 450 | * For ops count, group that together with step 4 |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 451 | */ |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 452 | ECDSA_BUDGET( MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 453 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Step 3: derive MPI from hashed message |
| 457 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 458 | MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 459 | |
| 460 | /* |
| 461 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 464 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 465 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu1, &e, &s_inv ) ); |
| 466 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu1, pu1, &grp->N ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 467 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 468 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( pu2, r, &s_inv ) ); |
| 469 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( pu2, pu2, &grp->N ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 470 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 471 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 472 | if( rs_ctx != NULL && rs_ctx->ver != NULL ) |
| 473 | rs_ctx->ver->state++; |
| 474 | |
| 475 | muladd: |
| 476 | #endif |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 477 | /* |
| 478 | * Step 5: R = u1 G + u2 Q |
| 479 | */ |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 480 | MBEDTLS_MPI_CHK( mbedtls_ecp_muladd_restartable( grp, |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 481 | &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP ) ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | if( mbedtls_ecp_is_zero( &R ) ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 485 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 486 | goto cleanup; |
| 487 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 488 | |
| 489 | /* |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 490 | * Step 6: convert xR to an integer (no-op) |
| 491 | * Step 7: reduce xR mod n (gives v) |
| 492 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) ); |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * Step 8: check if v (that is, R.X) is equal to r |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 497 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 ) |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 499 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 500 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 501 | goto cleanup; |
| 502 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 503 | |
| 504 | cleanup: |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 505 | mbedtls_ecp_point_free( &R ); |
Manuel Pégourié-Gonnard | 411079f | 2017-04-20 15:41:08 +0200 | [diff] [blame] | 506 | mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); |
| 507 | mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 ); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 508 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 509 | ECDSA_RS_LEAVE( ver ); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 510 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 511 | return( ret ); |
| 512 | } |
| 513 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 514 | /* |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 515 | * Verify ECDSA signature of hashed message |
| 516 | */ |
| 517 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
| 518 | const unsigned char *buf, size_t blen, |
| 519 | const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) |
| 520 | { |
| 521 | return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) ); |
| 522 | } |
| 523 | |
| 524 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 525 | * Convert a signature (given by context) to ASN.1 |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 526 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 528 | unsigned char *sig, size_t *slen ) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 529 | { |
| 530 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | unsigned char buf[MBEDTLS_ECDSA_MAX_LEN]; |
Manuel Pégourié-Gonnard | 4cf0686 | 2013-09-16 12:07:45 +0200 | [diff] [blame] | 532 | unsigned char *p = buf + sizeof( buf ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 533 | size_t len = 0; |
| 534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 535 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) ); |
| 536 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) ); |
| 539 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf, |
| 540 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 541 | |
| 542 | memcpy( sig, p, len ); |
| 543 | *slen = len; |
| 544 | |
| 545 | return( 0 ); |
| 546 | } |
| 547 | |
| 548 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 549 | * Compute and write signature |
| 550 | */ |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 551 | int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx, |
| 552 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 553 | const unsigned char *hash, size_t hlen, |
| 554 | unsigned char *sig, size_t *slen, |
| 555 | int (*f_rng)(void *, unsigned char *, size_t), |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 556 | void *p_rng, |
| 557 | mbedtls_ecdsa_restart_ctx *rs_ctx ) |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 558 | { |
| 559 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | mbedtls_mpi r, s; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | mbedtls_mpi_init( &r ); |
| 563 | mbedtls_mpi_init( &s ); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 564 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 566 | (void) f_rng; |
| 567 | (void) p_rng; |
| 568 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 569 | MBEDTLS_MPI_CHK( ecdsa_sign_det_restartable( &ctx->grp, &r, &s, &ctx->d, |
| 570 | hash, hlen, md_alg, rs_ctx ) ); |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 571 | #else |
| 572 | (void) md_alg; |
| 573 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 574 | MBEDTLS_MPI_CHK( ecdsa_sign_restartable( &ctx->grp, &r, &s, &ctx->d, |
| 575 | hash, hlen, f_rng, p_rng, rs_ctx ) ); |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 576 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) ); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 579 | |
| 580 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | mbedtls_mpi_free( &r ); |
| 582 | mbedtls_mpi_free( &s ); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 583 | |
| 584 | return( ret ); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 587 | /* |
| 588 | * Compute and write signature |
| 589 | */ |
| 590 | int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, |
| 591 | const unsigned char *hash, size_t hlen, |
| 592 | unsigned char *sig, size_t *slen, |
| 593 | int (*f_rng)(void *, unsigned char *, size_t), |
| 594 | void *p_rng ) |
| 595 | { |
| 596 | return( mbedtls_ecdsa_write_signature_restartable( |
| 597 | ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) ); |
| 598 | } |
| 599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \ |
| 601 | defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 602 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 603 | const unsigned char *hash, size_t hlen, |
| 604 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | mbedtls_md_type_t md_alg ) |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen, |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 608 | NULL, NULL ) ); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 609 | } |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 610 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 611 | |
| 612 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 613 | * Read and check signature |
| 614 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 615 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 616 | const unsigned char *hash, size_t hlen, |
| 617 | const unsigned char *sig, size_t slen ) |
| 618 | { |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 619 | return( mbedtls_ecdsa_read_signature_restartable( |
| 620 | ctx, hash, hlen, sig, slen, NULL ) ); |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Restartable read and check signature |
| 625 | */ |
| 626 | int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx, |
| 627 | const unsigned char *hash, size_t hlen, |
| 628 | const unsigned char *sig, size_t slen, |
| 629 | mbedtls_ecdsa_restart_ctx *rs_ctx ) |
| 630 | { |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 631 | int ret; |
| 632 | unsigned char *p = (unsigned char *) sig; |
| 633 | const unsigned char *end = sig + slen; |
| 634 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | mbedtls_mpi r, s; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 636 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | mbedtls_mpi_init( &r ); |
| 638 | mbedtls_mpi_init( &s ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 641 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 642 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 644 | goto cleanup; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if( p + len != end ) |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 648 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA + |
| 650 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 651 | goto cleanup; |
| 652 | } |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 || |
| 655 | ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 656 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 658 | goto cleanup; |
| 659 | } |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 660 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 661 | if( ( ret = ecdsa_verify_restartable( &ctx->grp, hash, hlen, |
| 662 | &ctx->Q, &r, &s, rs_ctx ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 663 | goto cleanup; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 664 | |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 665 | if( p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 666 | ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH; |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 667 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 668 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 669 | mbedtls_mpi_free( &r ); |
| 670 | mbedtls_mpi_free( &s ); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 671 | |
| 672 | return( ret ); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 676 | * Generate key pair |
| 677 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 678 | int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 679 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 680 | { |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 681 | return( mbedtls_ecp_group_load( &ctx->grp, gid ) || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 685 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | * Set context from an mbedtls_ecp_keypair |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 687 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ) |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 689 | { |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 690 | int ret; |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 692 | if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || |
| 693 | ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 || |
| 694 | ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 695 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | mbedtls_ecdsa_free( ctx ); |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 697 | } |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 698 | |
| 699 | return( ret ); |
| 700 | } |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 701 | |
| 702 | /* |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 703 | * Initialize context |
| 704 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ) |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 707 | mbedtls_ecp_keypair_init( ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
| 711 | * Free context |
| 712 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ) |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 714 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | mbedtls_ecp_keypair_free( ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 716 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 719 | /* |
| 720 | * Initialize a restart context |
| 721 | */ |
| 722 | void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx ) |
| 723 | { |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 724 | mbedtls_ecp_restart_init( &ctx->ecp ); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 725 | |
| 726 | ctx->ver = NULL; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 727 | ctx->sig = NULL; |
| 728 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 729 | ctx->det = NULL; |
| 730 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Free the components of a restart context |
| 735 | */ |
| 736 | void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx ) |
| 737 | { |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 738 | mbedtls_ecp_restart_free( &ctx->ecp ); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 739 | |
| 740 | ecdsa_restart_ver_free( ctx->ver ); |
| 741 | mbedtls_free( ctx->ver ); |
| 742 | ctx->ver = NULL; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame^] | 743 | |
| 744 | ecdsa_restart_sig_free( ctx->sig ); |
| 745 | mbedtls_free( ctx->sig ); |
| 746 | ctx->sig = NULL; |
| 747 | |
| 748 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 749 | ecdsa_restart_det_free( ctx->det ); |
| 750 | mbedtls_free( ctx->det ); |
| 751 | ctx->det = NULL; |
| 752 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 753 | } |
| 754 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 756 | #endif /* MBEDTLS_ECDSA_C */ |