Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve DSA |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | */ |
| 19 | |
| 20 | /* |
| 21 | * References: |
| 22 | * |
| 23 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
| 24 | */ |
| 25 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 26 | #include "common.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/ecdsa.h" |
| 31 | #include "mbedtls/asn1write.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/hmac_drbg.h" |
Manuel Pégourié-Gonnard | 7845fc0 | 2014-01-27 14:24:03 +0100 | [diff] [blame] | 37 | #endif |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 39 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 40 | |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 41 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 42 | #include "mbedtls/error.h" |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 46 | /* |
Manuel Pégourié-Gonnard | a4dd783 | 2017-09-07 11:11:39 +0200 | [diff] [blame] | 47 | * Sub-context for ecdsa_verify() |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 48 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | struct mbedtls_ecdsa_restart_ver { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 50 | mbedtls_mpi u1, u2; /* intermediate values */ |
| 51 | enum { /* what to do next? */ |
| 52 | ecdsa_ver_init = 0, /* getting started */ |
| 53 | ecdsa_ver_muladd, /* muladd step */ |
| 54 | } state; |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * Init verify restart sub-context |
| 59 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | static void ecdsa_restart_ver_init(mbedtls_ecdsa_restart_ver_ctx *ctx) |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 61 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | mbedtls_mpi_init(&ctx->u1); |
| 63 | mbedtls_mpi_init(&ctx->u2); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 64 | ctx->state = ecdsa_ver_init; |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Free the components of a verify restart sub-context |
| 69 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | static void ecdsa_restart_ver_free(mbedtls_ecdsa_restart_ver_ctx *ctx) |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 71 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 73 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | } |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | mbedtls_mpi_free(&ctx->u1); |
| 77 | mbedtls_mpi_free(&ctx->u2); |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 78 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | ecdsa_restart_ver_init(ctx); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 82 | /* |
Manuel Pégourié-Gonnard | a4dd783 | 2017-09-07 11:11:39 +0200 | [diff] [blame] | 83 | * Sub-context for ecdsa_sign() |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 84 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | struct mbedtls_ecdsa_restart_sig { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 86 | int sign_tries; |
| 87 | int key_tries; |
| 88 | mbedtls_mpi k; /* per-signature random */ |
| 89 | mbedtls_mpi r; /* r value */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 90 | enum { /* what to do next? */ |
| 91 | ecdsa_sig_init = 0, /* getting started */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 92 | ecdsa_sig_mul, /* doing ecp_mul() */ |
| 93 | ecdsa_sig_modn, /* mod N computations */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 94 | } state; |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Init verify sign sub-context |
| 99 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | static void ecdsa_restart_sig_init(mbedtls_ecdsa_restart_sig_ctx *ctx) |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 101 | { |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 102 | ctx->sign_tries = 0; |
| 103 | ctx->key_tries = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | mbedtls_mpi_init(&ctx->k); |
| 105 | mbedtls_mpi_init(&ctx->r); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 106 | ctx->state = ecdsa_sig_init; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Free the components of a sign restart sub-context |
| 111 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | static void ecdsa_restart_sig_free(mbedtls_ecdsa_restart_sig_ctx *ctx) |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 113 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 115 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | } |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | mbedtls_mpi_free(&ctx->k); |
| 119 | mbedtls_mpi_free(&ctx->r); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 123 | /* |
Manuel Pégourié-Gonnard | a4dd783 | 2017-09-07 11:11:39 +0200 | [diff] [blame] | 124 | * Sub-context for ecdsa_sign_det() |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 125 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | struct mbedtls_ecdsa_restart_det { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 127 | mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 128 | enum { /* what to do next? */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 129 | ecdsa_det_init = 0, /* getting started */ |
| 130 | ecdsa_det_sign, /* make signature */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 131 | } state; |
| 132 | }; |
| 133 | |
| 134 | /* |
| 135 | * Init verify sign_det sub-context |
| 136 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | static void ecdsa_restart_det_init(mbedtls_ecdsa_restart_det_ctx *ctx) |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 138 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | mbedtls_hmac_drbg_init(&ctx->rng_ctx); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 140 | ctx->state = ecdsa_det_init; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Free the components of a sign_det restart sub-context |
| 145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | static void ecdsa_restart_det_free(mbedtls_ecdsa_restart_det_ctx *ctx) |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 147 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 149 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | } |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | mbedtls_hmac_drbg_free(&ctx->rng_ctx); |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | ecdsa_restart_det_init(ctx); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 155 | } |
| 156 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
| 157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | #define ECDSA_RS_ECP (rs_ctx == NULL ? NULL : &rs_ctx->ecp) |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 159 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 160 | /* Utility macro for checking and updating ops budget */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | #define ECDSA_BUDGET(ops) \ |
| 162 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, ECDSA_RS_ECP, ops)); |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 163 | |
Manuel Pégourié-Gonnard | b948f7d | 2017-08-23 17:58:40 +0200 | [diff] [blame] | 164 | /* Call this when entering a function that needs its own sub-context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | #define ECDSA_RS_ENTER(SUB) do { \ |
| 166 | /* reset ops count for this call if top-level */ \ |
| 167 | if (rs_ctx != NULL && rs_ctx->ecp.depth++ == 0) \ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 168 | rs_ctx->ecp.ops_done = 0; \ |
| 169 | \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | /* set up our own sub-context if needed */ \ |
| 171 | if (mbedtls_ecp_restart_is_enabled() && \ |
| 172 | rs_ctx != NULL && rs_ctx->SUB == NULL) \ |
| 173 | { \ |
| 174 | rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \ |
| 175 | if (rs_ctx->SUB == NULL) \ |
| 176 | return MBEDTLS_ERR_ECP_ALLOC_FAILED; \ |
| 177 | \ |
| 178 | ecdsa_restart_## SUB ##_init(rs_ctx->SUB); \ |
| 179 | } \ |
| 180 | } while (0) |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 181 | |
Manuel Pégourié-Gonnard | b948f7d | 2017-08-23 17:58:40 +0200 | [diff] [blame] | 182 | /* Call this when leaving a function that needs its own sub-context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | #define ECDSA_RS_LEAVE(SUB) do { \ |
| 184 | /* clear our sub-context when not in progress (done or error) */ \ |
| 185 | if (rs_ctx != NULL && rs_ctx->SUB != NULL && \ |
| 186 | ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \ |
| 187 | { \ |
| 188 | ecdsa_restart_## SUB ##_free(rs_ctx->SUB); \ |
| 189 | mbedtls_free(rs_ctx->SUB); \ |
| 190 | rs_ctx->SUB = NULL; \ |
| 191 | } \ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 192 | \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if (rs_ctx != NULL) \ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 194 | rs_ctx->ecp.depth--; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | } while (0) |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 196 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 197 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 198 | |
| 199 | #define ECDSA_RS_ECP NULL |
| 200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | #define ECDSA_BUDGET(ops) /* no-op; for compatibility */ |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | #define ECDSA_RS_ENTER(SUB) (void) rs_ctx |
| 204 | #define ECDSA_RS_LEAVE(SUB) (void) rs_ctx |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 206 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 207 | |
Steven Cooreman | fa6641b | 2021-01-11 17:11:39 +0100 | [diff] [blame] | 208 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) || \ |
Steven Cooreman | 107409f | 2021-01-26 12:01:22 +0100 | [diff] [blame] | 209 | !defined(MBEDTLS_ECDSA_SIGN_ALT) || \ |
| 210 | !defined(MBEDTLS_ECDSA_VERIFY_ALT) |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 211 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 212 | * Derive a suitable integer for group grp from a buffer of length len |
| 213 | * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3 |
| 214 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x, |
| 216 | const unsigned char *buf, size_t blen) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 217 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 218 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | size_t n_size = (grp->nbits + 7) / 8; |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 220 | size_t use_size = blen > n_size ? n_size : blen; |
| 221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(x, buf, use_size)); |
| 223 | if (use_size * 8 > grp->nbits) { |
| 224 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(x, use_size * 8 - grp->nbits)); |
| 225 | } |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 227 | /* While at it, reduce modulo N */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | if (mbedtls_mpi_cmp_mpi(x, &grp->N) >= 0) { |
| 229 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(x, x, &grp->N)); |
| 230 | } |
Manuel Pégourié-Gonnard | 461d416 | 2014-01-06 10:16:28 +0100 | [diff] [blame] | 231 | |
Manuel Pégourié-Gonnard | 5304812 | 2014-01-03 12:55:15 +0100 | [diff] [blame] | 232 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | return ret; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 234 | } |
Steven Cooreman | fa6641b | 2021-01-11 17:11:39 +0100 | [diff] [blame] | 235 | #endif /* ECDSA_DETERMINISTIC || !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */ |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 236 | |
JonathanWitthoeft | 405ec94 | 2023-04-26 10:24:12 -0500 | [diff] [blame^] | 237 | #if !defined(MBEDTLS_ECDSA_SIGN_ALT) || \ |
| 238 | !defined(MBEDTLS_ECDSA_VERIFY_ALT) |
| 239 | |
| 240 | int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid) |
| 241 | { |
| 242 | switch (gid) { |
| 243 | #ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED |
| 244 | case MBEDTLS_ECP_DP_CURVE25519: return 0; |
| 245 | #endif |
| 246 | #ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED |
| 247 | case MBEDTLS_ECP_DP_CURVE448: return 0; |
| 248 | #endif |
| 249 | default: return 1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | #endif /* !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */ |
| 254 | |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 255 | #if !defined(MBEDTLS_ECDSA_SIGN_ALT) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 256 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 257 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 258 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 259 | */ |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 260 | int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp, |
| 261 | mbedtls_mpi *r, mbedtls_mpi *s, |
| 262 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 263 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 264 | int (*f_rng_blind)(void *, unsigned char *, size_t), |
| 265 | void *p_rng_blind, |
| 266 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 267 | { |
Manuel Pégourié-Gonnard | 50b63ba | 2017-04-25 12:57:22 +0200 | [diff] [blame] | 268 | int ret, key_tries, sign_tries; |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 269 | int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_ecp_point R; |
| 271 | mbedtls_mpi k, e, t; |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 272 | mbedtls_mpi *pk = &k, *pr = r; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 273 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 274 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) { |
| 276 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 277 | } |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 278 | |
Darryl Green | c64a48b | 2017-11-17 17:09:17 +0000 | [diff] [blame] | 279 | /* Make sure d is in range 1..n-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) { |
| 281 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 282 | } |
Darryl Green | c64a48b | 2017-11-17 17:09:17 +0000 | [diff] [blame] | 283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_ecp_point_init(&R); |
| 285 | 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] | 286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | ECDSA_RS_ENTER(sig); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 288 | |
| 289 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 291 | /* redirect to our context */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 292 | p_sign_tries = &rs_ctx->sig->sign_tries; |
| 293 | p_key_tries = &rs_ctx->sig->key_tries; |
| 294 | pk = &rs_ctx->sig->k; |
| 295 | pr = &rs_ctx->sig->r; |
| 296 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 297 | /* jump to current step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | if (rs_ctx->sig->state == ecdsa_sig_mul) { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 299 | goto mul; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | } |
| 301 | if (rs_ctx->sig->state == ecdsa_sig_modn) { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 302 | goto modn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | } |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 304 | } |
| 305 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 306 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 307 | *p_sign_tries = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | do { |
| 309 | if ((*p_sign_tries)++ > 10) { |
Manuel Pégourié-Gonnard | 6754396 | 2017-04-21 13:19:43 +0200 | [diff] [blame] | 310 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 311 | goto cleanup; |
| 312 | } |
| 313 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 314 | /* |
| 315 | * Steps 1-3: generate a suitable ephemeral keypair |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 316 | * and set r = xR mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 317 | */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 318 | *p_key_tries = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | do { |
| 320 | if ((*p_key_tries)++ > 10) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 322 | goto cleanup; |
| 323 | } |
Manuel Pégourié-Gonnard | 6754396 | 2017-04-21 13:19:43 +0200 | [diff] [blame] | 324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, pk, f_rng, p_rng)); |
Manuel Pégourié-Gonnard | 50b63ba | 2017-04-25 12:57:22 +0200 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 327 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 329 | rs_ctx->sig->state = ecdsa_sig_mul; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 331 | |
| 332 | mul: |
| 333 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G, |
| 335 | f_rng_blind, |
| 336 | p_rng_blind, |
| 337 | ECDSA_RS_ECP)); |
| 338 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N)); |
| 339 | } while (mbedtls_mpi_cmp_int(pr, 0) == 0); |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 340 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 341 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 343 | rs_ctx->sig->state = ecdsa_sig_modn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 345 | |
| 346 | modn: |
| 347 | #endif |
| 348 | /* |
| 349 | * Accounting for everything up to the end of the loop |
| 350 | * (step 6, but checking now avoids saving e and t) |
| 351 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * Step 5: derive MPI from hashed message |
| 356 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen)); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 358 | |
| 359 | /* |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 360 | * Generate a random value to blind inv_mod in next step, |
| 361 | * avoiding a potential timing leak. |
| 362 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind, |
| 364 | p_rng_blind)); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * 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] | 368 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d)); |
| 370 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s)); |
| 371 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&e, &e, &t)); |
| 372 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pk, pk, &t)); |
| 373 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N)); |
| 374 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N)); |
| 375 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e)); |
| 376 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N)); |
| 377 | } while (mbedtls_mpi_cmp_int(s, 0) == 0); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 378 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 379 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
| 381 | mbedtls_mpi_copy(r, pr); |
| 382 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 383 | #endif |
| 384 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 385 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | mbedtls_ecp_point_free(&R); |
| 387 | 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] | 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | ECDSA_RS_LEAVE(sig); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | return ret; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 392 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 393 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 394 | /* |
| 395 | * Compute ECDSA signature of a hashed message |
| 396 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 398 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 399 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 400 | { |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 401 | /* Use the same RNG for both blinding and ephemeral key generation */ |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 402 | return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen, |
| 403 | f_rng, p_rng, f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 404 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 405 | #endif /* !MBEDTLS_ECDSA_SIGN_ALT */ |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 408 | /* |
| 409 | * Deterministic signature wrapper |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 410 | * |
TRodziewicz | 7e9422d | 2021-04-30 10:32:58 +0200 | [diff] [blame] | 411 | * note: The f_rng_blind parameter must not be NULL. |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 412 | * |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 413 | */ |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 414 | int mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp, |
| 415 | mbedtls_mpi *r, mbedtls_mpi *s, |
| 416 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 417 | mbedtls_md_type_t md_alg, |
| 418 | int (*f_rng_blind)(void *, unsigned char *, size_t), |
| 419 | void *p_rng_blind, |
| 420 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 421 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 422 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | mbedtls_hmac_drbg_context rng_ctx; |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 424 | mbedtls_hmac_drbg_context *p_rng = &rng_ctx; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 425 | unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | size_t grp_len = (grp->nbits + 7) / 8; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 427 | const mbedtls_md_info_t *md_info; |
| 428 | mbedtls_mpi h; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) { |
| 431 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 432 | } |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | mbedtls_mpi_init(&h); |
| 435 | mbedtls_hmac_drbg_init(&rng_ctx); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | ECDSA_RS_ENTER(det); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 438 | |
| 439 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if (rs_ctx != NULL && rs_ctx->det != NULL) { |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 441 | /* redirect to our context */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 442 | p_rng = &rs_ctx->det->rng_ctx; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 443 | |
| 444 | /* jump to current step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | if (rs_ctx->det->state == ecdsa_det_sign) { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 446 | goto sign; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | } |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 448 | } |
| 449 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 450 | |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 451 | /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len)); |
| 453 | MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen)); |
| 454 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len)); |
| 455 | mbedtls_hmac_drbg_seed_buf(p_rng, md_info, data, 2 * grp_len); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 456 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 457 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | if (rs_ctx != NULL && rs_ctx->det != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 459 | rs_ctx->det->state = ecdsa_det_sign; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 461 | |
| 462 | sign: |
| 463 | #endif |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 464 | #if defined(MBEDTLS_ECDSA_SIGN_ALT) |
Steven Cooreman | 6dce4bb | 2021-02-10 17:07:20 +0100 | [diff] [blame] | 465 | (void) f_rng_blind; |
| 466 | (void) p_rng_blind; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen, |
| 468 | mbedtls_hmac_drbg_random, p_rng); |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 469 | #else |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 470 | ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen, |
| 471 | mbedtls_hmac_drbg_random, p_rng, |
| 472 | f_rng_blind, p_rng_blind, rs_ctx); |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 473 | #endif /* MBEDTLS_ECDSA_SIGN_ALT */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 474 | |
| 475 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | mbedtls_hmac_drbg_free(&rng_ctx); |
| 477 | mbedtls_mpi_free(&h); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | ECDSA_RS_LEAVE(det); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | return ret; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 482 | } |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 483 | |
| 484 | /* |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 485 | * Deterministic signature wrapper |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 486 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r, |
| 488 | mbedtls_mpi *s, const mbedtls_mpi *d, |
| 489 | const unsigned char *buf, size_t blen, |
| 490 | mbedtls_md_type_t md_alg, |
| 491 | int (*f_rng_blind)(void *, unsigned char *, |
| 492 | size_t), |
| 493 | void *p_rng_blind) |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 494 | { |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 495 | return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg, |
| 496 | f_rng_blind, p_rng_blind, NULL); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 497 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 499 | |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 500 | #if !defined(MBEDTLS_ECDSA_VERIFY_ALT) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 501 | /* |
| 502 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 503 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 504 | */ |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 505 | int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp, |
| 506 | const unsigned char *buf, size_t blen, |
| 507 | const mbedtls_ecp_point *Q, |
| 508 | const mbedtls_mpi *r, |
| 509 | const mbedtls_mpi *s, |
| 510 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 511 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 512 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | mbedtls_mpi e, s_inv, u1, u2; |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 514 | mbedtls_ecp_point R; |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 515 | mbedtls_mpi *pu1 = &u1, *pu2 = &u2; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 516 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | mbedtls_ecp_point_init(&R); |
| 518 | mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv); |
| 519 | mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 520 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 521 | /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) { |
| 523 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 524 | } |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | ECDSA_RS_ENTER(ver); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 527 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 528 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (rs_ctx != NULL && rs_ctx->ver != NULL) { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 530 | /* redirect to our context */ |
| 531 | pu1 = &rs_ctx->ver->u1; |
| 532 | pu2 = &rs_ctx->ver->u2; |
| 533 | |
| 534 | /* jump to current step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | if (rs_ctx->ver->state == ecdsa_ver_muladd) { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 536 | goto muladd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | } |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 538 | } |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 539 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 540 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 541 | /* |
| 542 | * Step 1: make sure r and s are in range 1..n-1 |
| 543 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 || |
| 545 | mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 547 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 551 | * Step 3: derive MPI from hashed message |
| 552 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen)); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 554 | |
| 555 | /* |
| 556 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 557 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | ECDSA_BUDGET(MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2); |
Manuel Pégourié-Gonnard | bfa1972 | 2017-08-23 17:39:18 +0200 | [diff] [blame] | 559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | 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] | 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv)); |
| 563 | 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] | 564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv)); |
| 566 | 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] | 567 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 568 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | if (rs_ctx != NULL && rs_ctx->ver != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 570 | rs_ctx->ver->state = ecdsa_ver_muladd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | } |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 572 | |
| 573 | muladd: |
| 574 | #endif |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 575 | /* |
| 576 | * Step 5: R = u1 G + u2 Q |
| 577 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp, |
| 579 | &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP)); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | if (mbedtls_ecp_is_zero(&R)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 583 | goto cleanup; |
| 584 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 585 | |
| 586 | /* |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 587 | * Step 6: convert xR to an integer (no-op) |
| 588 | * Step 7: reduce xR mod n (gives v) |
| 589 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | 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] | 591 | |
| 592 | /* |
| 593 | * 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] | 594 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 597 | goto cleanup; |
| 598 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 599 | |
| 600 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | mbedtls_ecp_point_free(&R); |
| 602 | mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv); |
| 603 | mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | ECDSA_RS_LEAVE(ver); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | return ret; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 608 | } |
| 609 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 610 | /* |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 611 | * Verify ECDSA signature of hashed message |
| 612 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp, |
| 614 | const unsigned char *buf, size_t blen, |
| 615 | const mbedtls_ecp_point *Q, |
| 616 | const mbedtls_mpi *r, |
| 617 | const mbedtls_mpi *s) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 618 | { |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 619 | return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL); |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 620 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 621 | #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */ |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 622 | |
| 623 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 624 | * Convert a signature (given by context) to ASN.1 |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 625 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s, |
| 627 | unsigned char *sig, size_t sig_size, |
| 628 | size_t *slen) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 629 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 630 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 }; |
| 632 | unsigned char *p = buf + sizeof(buf); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 633 | size_t len = 0; |
| 634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 635 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, s)); |
| 636 | 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] | 637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); |
| 639 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf, |
| 640 | MBEDTLS_ASN1_CONSTRUCTED | |
| 641 | MBEDTLS_ASN1_SEQUENCE)); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | if (len > sig_size) { |
| 644 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 645 | } |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 646 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | memcpy(sig, p, len); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 648 | *slen = len; |
| 649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | return 0; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 654 | * Compute and write signature |
| 655 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx, |
| 657 | mbedtls_md_type_t md_alg, |
| 658 | const unsigned char *hash, size_t hlen, |
| 659 | unsigned char *sig, size_t sig_size, size_t *slen, |
| 660 | int (*f_rng)(void *, unsigned char *, size_t), |
| 661 | void *p_rng, |
| 662 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 663 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 664 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_mpi r, s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 666 | if (f_rng == NULL) { |
| 667 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 668 | } |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | mbedtls_mpi_init(&r); |
| 671 | mbedtls_mpi_init(&s); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 672 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 674 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d, |
| 675 | hash, hlen, md_alg, f_rng, |
| 676 | p_rng, rs_ctx)); |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 677 | #else |
| 678 | (void) md_alg; |
| 679 | |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 680 | #if defined(MBEDTLS_ECDSA_SIGN_ALT) |
Steven Cooreman | fa6641b | 2021-01-11 17:11:39 +0100 | [diff] [blame] | 681 | (void) rs_ctx; |
| 682 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d, |
| 684 | hash, hlen, f_rng, p_rng)); |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 685 | #else |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 686 | /* Use the same RNG for both blinding and ephemeral key generation */ |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 687 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d, |
| 688 | hash, hlen, f_rng, p_rng, f_rng, |
| 689 | p_rng, rs_ctx)); |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 690 | #endif /* MBEDTLS_ECDSA_SIGN_ALT */ |
Ron Eldor | 5ed8c1e | 2018-11-05 14:04:26 +0200 | [diff] [blame] | 691 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | MBEDTLS_MPI_CHK(ecdsa_signature_to_asn1(&r, &s, sig, sig_size, slen)); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 694 | |
| 695 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | mbedtls_mpi_free(&r); |
| 697 | mbedtls_mpi_free(&s); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | return ret; |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 702 | /* |
| 703 | * Compute and write signature |
| 704 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, |
| 706 | mbedtls_md_type_t md_alg, |
| 707 | const unsigned char *hash, size_t hlen, |
| 708 | unsigned char *sig, size_t sig_size, size_t *slen, |
| 709 | int (*f_rng)(void *, unsigned char *, size_t), |
| 710 | void *p_rng) |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 711 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | return mbedtls_ecdsa_write_signature_restartable( |
| 713 | ctx, md_alg, hash, hlen, sig, sig_size, slen, |
| 714 | f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 717 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 718 | * Read and check signature |
| 719 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 720 | int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx, |
| 721 | const unsigned char *hash, size_t hlen, |
| 722 | const unsigned char *sig, size_t slen) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 723 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | return mbedtls_ecdsa_read_signature_restartable( |
| 725 | ctx, hash, hlen, sig, slen, NULL); |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Restartable read and check signature |
| 730 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx, |
| 732 | const unsigned char *hash, size_t hlen, |
| 733 | const unsigned char *sig, size_t slen, |
| 734 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 735 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 736 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 737 | unsigned char *p = (unsigned char *) sig; |
| 738 | const unsigned char *end = sig + slen; |
| 739 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | mbedtls_mpi r, s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 741 | mbedtls_mpi_init(&r); |
| 742 | mbedtls_mpi_init(&s); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 745 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 746 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 747 | goto cleanup; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | if (p + len != end) { |
| 751 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 752 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 753 | goto cleanup; |
| 754 | } |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 755 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 756 | if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 || |
| 757 | (ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 759 | goto cleanup; |
| 760 | } |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 761 | #if defined(MBEDTLS_ECDSA_VERIFY_ALT) |
Steven Cooreman | fa6641b | 2021-01-11 17:11:39 +0100 | [diff] [blame] | 762 | (void) rs_ctx; |
| 763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | if ((ret = mbedtls_ecdsa_verify(&ctx->grp, hash, hlen, |
| 765 | &ctx->Q, &r, &s)) != 0) { |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 766 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | } |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 768 | #else |
Paul Elliott | 2ba002c | 2022-12-09 18:59:26 +0000 | [diff] [blame] | 769 | if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen, |
| 770 | &ctx->Q, &r, &s, rs_ctx)) != 0) { |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 771 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 773 | #endif /* MBEDTLS_ECDSA_VERIFY_ALT */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 774 | |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 775 | /* At this point we know that the buffer starts with a valid signature. |
| 776 | * Return 0 if the buffer just contains the signature, and a specific |
| 777 | * error code if the valid signature is followed by more data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | if (p != end) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | } |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 781 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 782 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | mbedtls_mpi_free(&r); |
| 784 | mbedtls_mpi_free(&s); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 785 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | return ret; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 787 | } |
| 788 | |
Ron Eldor | 314adb6 | 2017-10-10 18:28:25 +0300 | [diff] [blame] | 789 | #if !defined(MBEDTLS_ECDSA_GENKEY_ALT) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 790 | /* |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 791 | * Generate key pair |
| 792 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 793 | int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
| 794 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 795 | { |
Ron Eldor | adb5234 | 2018-12-17 10:06:12 +0200 | [diff] [blame] | 796 | int ret = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | ret = mbedtls_ecp_group_load(&ctx->grp, gid); |
| 798 | if (ret != 0) { |
| 799 | return ret; |
| 800 | } |
Ron Eldor | adb5234 | 2018-12-17 10:06:12 +0200 | [diff] [blame] | 801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d, |
| 803 | &ctx->Q, f_rng, p_rng); |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 804 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 805 | #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */ |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 806 | |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 807 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | * Set context from an mbedtls_ecp_keypair |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 809 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | 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] | 811 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 812 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 813 | if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 || |
| 814 | (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 || |
| 815 | (ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) { |
| 816 | mbedtls_ecdsa_free(ctx); |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 817 | } |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | return ret; |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 820 | } |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 821 | |
| 822 | /* |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 823 | * Initialize context |
| 824 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx) |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 826 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | mbedtls_ecp_keypair_init(ctx); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Free context |
| 832 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx) |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 834 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | if (ctx == NULL) { |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 836 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | } |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | mbedtls_ecp_keypair_free(ctx); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 840 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 841 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 842 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 843 | /* |
| 844 | * Initialize a restart context |
| 845 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 846 | void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 847 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | mbedtls_ecp_restart_init(&ctx->ecp); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 849 | |
| 850 | ctx->ver = NULL; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 851 | ctx->sig = NULL; |
| 852 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 853 | ctx->det = NULL; |
| 854 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Free the components of a restart context |
| 859 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 861 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | if (ctx == NULL) { |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 863 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 864 | } |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | mbedtls_ecp_restart_free(&ctx->ecp); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 867 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 868 | ecdsa_restart_ver_free(ctx->ver); |
| 869 | mbedtls_free(ctx->ver); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 870 | ctx->ver = NULL; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 871 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 872 | ecdsa_restart_sig_free(ctx->sig); |
| 873 | mbedtls_free(ctx->sig); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 874 | ctx->sig = NULL; |
| 875 | |
| 876 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 877 | ecdsa_restart_det_free(ctx->det); |
| 878 | mbedtls_free(ctx->det); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 879 | ctx->det = NULL; |
| 880 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 881 | } |
| 882 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | #endif /* MBEDTLS_ECDSA_C */ |