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 | |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 237 | #if !defined(MBEDTLS_ECDSA_SIGN_ALT) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 238 | /* |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 239 | * Compute ECDSA signature of a hashed message (SEC1 4.1.3) |
| 240 | * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message) |
| 241 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | static int ecdsa_sign_restartable(mbedtls_ecp_group *grp, |
| 243 | mbedtls_mpi *r, mbedtls_mpi *s, |
| 244 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 245 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 246 | int (*f_rng_blind)(void *, unsigned char *, size_t), |
| 247 | void *p_rng_blind, |
| 248 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 249 | { |
Manuel Pégourié-Gonnard | 50b63ba | 2017-04-25 12:57:22 +0200 | [diff] [blame] | 250 | int ret, key_tries, sign_tries; |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 251 | 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] | 252 | mbedtls_ecp_point R; |
| 253 | mbedtls_mpi k, e, t; |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 254 | mbedtls_mpi *pk = &k, *pr = r; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 255 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 256 | /* 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] | 257 | if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) { |
| 258 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 259 | } |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 260 | |
Darryl Green | c64a48b | 2017-11-17 17:09:17 +0000 | [diff] [blame] | 261 | /* Make sure d is in range 1..n-1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) { |
| 263 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 264 | } |
Darryl Green | c64a48b | 2017-11-17 17:09:17 +0000 | [diff] [blame] | 265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | mbedtls_ecp_point_init(&R); |
| 267 | 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] | 268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | ECDSA_RS_ENTER(sig); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 270 | |
| 271 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 273 | /* redirect to our context */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 274 | p_sign_tries = &rs_ctx->sig->sign_tries; |
| 275 | p_key_tries = &rs_ctx->sig->key_tries; |
| 276 | pk = &rs_ctx->sig->k; |
| 277 | pr = &rs_ctx->sig->r; |
| 278 | |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 279 | /* jump to current step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | if (rs_ctx->sig->state == ecdsa_sig_mul) { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 281 | goto mul; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | } |
| 283 | if (rs_ctx->sig->state == ecdsa_sig_modn) { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 284 | goto modn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | } |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 286 | } |
| 287 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 288 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 289 | *p_sign_tries = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | do { |
| 291 | if ((*p_sign_tries)++ > 10) { |
Manuel Pégourié-Gonnard | 6754396 | 2017-04-21 13:19:43 +0200 | [diff] [blame] | 292 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 293 | goto cleanup; |
| 294 | } |
| 295 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 296 | /* |
| 297 | * Steps 1-3: generate a suitable ephemeral keypair |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 298 | * and set r = xR mod n |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 299 | */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 300 | *p_key_tries = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | do { |
| 302 | if ((*p_key_tries)++ > 10) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 304 | goto cleanup; |
| 305 | } |
Manuel Pégourié-Gonnard | 6754396 | 2017-04-21 13:19:43 +0200 | [diff] [blame] | 306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | 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] | 308 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 309 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 311 | rs_ctx->sig->state = ecdsa_sig_mul; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 313 | |
| 314 | mul: |
| 315 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G, |
| 317 | f_rng_blind, |
| 318 | p_rng_blind, |
| 319 | ECDSA_RS_ECP)); |
| 320 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N)); |
| 321 | } while (mbedtls_mpi_cmp_int(pr, 0) == 0); |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 323 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 325 | rs_ctx->sig->state = ecdsa_sig_modn; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 327 | |
| 328 | modn: |
| 329 | #endif |
| 330 | /* |
| 331 | * Accounting for everything up to the end of the loop |
| 332 | * (step 6, but checking now avoids saving e and t) |
| 333 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * Step 5: derive MPI from hashed message |
| 338 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen)); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 340 | |
| 341 | /* |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 342 | * Generate a random value to blind inv_mod in next step, |
| 343 | * avoiding a potential timing leak. |
| 344 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind, |
| 346 | p_rng_blind)); |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * 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] | 350 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d)); |
| 352 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s)); |
| 353 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&e, &e, &t)); |
| 354 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pk, pk, &t)); |
| 355 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N)); |
| 356 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N)); |
| 357 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e)); |
| 358 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N)); |
| 359 | } while (mbedtls_mpi_cmp_int(s, 0) == 0); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 360 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 361 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | if (rs_ctx != NULL && rs_ctx->sig != NULL) { |
| 363 | mbedtls_mpi_copy(r, pr); |
| 364 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 365 | #endif |
| 366 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 367 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | mbedtls_ecp_point_free(&R); |
| 369 | 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] | 370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | ECDSA_RS_LEAVE(sig); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | return ret; |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 374 | } |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid) |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 377 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | switch (gid) { |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 379 | #ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED |
Christoph M. Wintersteiger | 3ff60bc | 2019-02-15 12:59:59 +0000 | [diff] [blame] | 380 | case MBEDTLS_ECP_DP_CURVE25519: return 0; |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 381 | #endif |
| 382 | #ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED |
Christoph M. Wintersteiger | 3ff60bc | 2019-02-15 12:59:59 +0000 | [diff] [blame] | 383 | case MBEDTLS_ECP_DP_CURVE448: return 0; |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 384 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | default: return 1; |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 389 | /* |
| 390 | * Compute ECDSA signature of a hashed message |
| 391 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 393 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 394 | 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] | 395 | { |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 396 | /* Use the same RNG for both blinding and ephemeral key generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | return ecdsa_sign_restartable(grp, r, s, d, buf, blen, |
| 398 | f_rng, p_rng, f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 399 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 400 | #endif /* !MBEDTLS_ECDSA_SIGN_ALT */ |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 401 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 403 | /* |
| 404 | * Deterministic signature wrapper |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 405 | * |
TRodziewicz | 7e9422d | 2021-04-30 10:32:58 +0200 | [diff] [blame] | 406 | * note: The f_rng_blind parameter must not be NULL. |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 407 | * |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 408 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | static int ecdsa_sign_det_restartable(mbedtls_ecp_group *grp, |
| 410 | mbedtls_mpi *r, mbedtls_mpi *s, |
| 411 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 412 | mbedtls_md_type_t md_alg, |
| 413 | int (*f_rng_blind)(void *, unsigned char *, size_t), |
| 414 | void *p_rng_blind, |
| 415 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 416 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 417 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 418 | mbedtls_hmac_drbg_context rng_ctx; |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 419 | mbedtls_hmac_drbg_context *p_rng = &rng_ctx; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | size_t grp_len = (grp->nbits + 7) / 8; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | const mbedtls_md_info_t *md_info; |
| 423 | mbedtls_mpi h; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) { |
| 426 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 427 | } |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | mbedtls_mpi_init(&h); |
| 430 | mbedtls_hmac_drbg_init(&rng_ctx); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 431 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | ECDSA_RS_ENTER(det); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 433 | |
| 434 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | if (rs_ctx != NULL && rs_ctx->det != NULL) { |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 436 | /* redirect to our context */ |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 437 | p_rng = &rs_ctx->det->rng_ctx; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 438 | |
| 439 | /* jump to current step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if (rs_ctx->det->state == ecdsa_det_sign) { |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 441 | goto sign; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | } |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 443 | } |
| 444 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 445 | |
Manuel Pégourié-Gonnard | f42bca6 | 2014-01-06 15:05:01 +0100 | [diff] [blame] | 446 | /* Use private key and message hash (reduced) to initialize HMAC_DRBG */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len)); |
| 448 | MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen)); |
| 449 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len)); |
| 450 | 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] | 451 | |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 452 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | if (rs_ctx != NULL && rs_ctx->det != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 454 | rs_ctx->det->state = ecdsa_det_sign; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | } |
Manuel Pégourié-Gonnard | af081f5 | 2017-04-25 13:44:19 +0200 | [diff] [blame] | 456 | |
| 457 | sign: |
| 458 | #endif |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 459 | #if defined(MBEDTLS_ECDSA_SIGN_ALT) |
Steven Cooreman | 6dce4bb | 2021-02-10 17:07:20 +0100 | [diff] [blame] | 460 | (void) f_rng_blind; |
| 461 | (void) p_rng_blind; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen, |
| 463 | mbedtls_hmac_drbg_random, p_rng); |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 464 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | ret = ecdsa_sign_restartable(grp, r, s, d, buf, blen, |
| 466 | mbedtls_hmac_drbg_random, p_rng, |
| 467 | f_rng_blind, p_rng_blind, rs_ctx); |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 468 | #endif /* MBEDTLS_ECDSA_SIGN_ALT */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 469 | |
| 470 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | mbedtls_hmac_drbg_free(&rng_ctx); |
| 472 | mbedtls_mpi_free(&h); |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | ECDSA_RS_LEAVE(det); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | return ret; |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 477 | } |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 478 | |
| 479 | /* |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 480 | * Deterministic signature wrapper |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 481 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r, |
| 483 | mbedtls_mpi *s, const mbedtls_mpi *d, |
| 484 | const unsigned char *buf, size_t blen, |
| 485 | mbedtls_md_type_t md_alg, |
| 486 | int (*f_rng_blind)(void *, unsigned char *, |
| 487 | size_t), |
| 488 | void *p_rng_blind) |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 489 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | return ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg, |
| 491 | f_rng_blind, p_rng_blind, NULL); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 492 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Paul Bakker | 9f3c7d7 | 2014-01-23 16:11:14 +0100 | [diff] [blame] | 494 | |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 495 | #if !defined(MBEDTLS_ECDSA_VERIFY_ALT) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 496 | /* |
| 497 | * Verify ECDSA signature of hashed message (SEC1 4.1.4) |
| 498 | * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message) |
| 499 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | static int ecdsa_verify_restartable(mbedtls_ecp_group *grp, |
| 501 | const unsigned char *buf, size_t blen, |
| 502 | const mbedtls_ecp_point *Q, |
| 503 | const mbedtls_mpi *r, const mbedtls_mpi *s, |
| 504 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 505 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 506 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | mbedtls_mpi e, s_inv, u1, u2; |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 508 | mbedtls_ecp_point R; |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 509 | mbedtls_mpi *pu1 = &u1, *pu2 = &u2; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | mbedtls_ecp_point_init(&R); |
| 512 | mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv); |
| 513 | mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 514 | |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 515 | /* 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] | 516 | if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) { |
| 517 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 518 | } |
Manuel Pégourié-Gonnard | 97871ef | 2013-12-04 20:52:04 +0100 | [diff] [blame] | 519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | ECDSA_RS_ENTER(ver); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 521 | |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 522 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | if (rs_ctx != NULL && rs_ctx->ver != NULL) { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 524 | /* redirect to our context */ |
| 525 | pu1 = &rs_ctx->ver->u1; |
| 526 | pu2 = &rs_ctx->ver->u2; |
| 527 | |
| 528 | /* jump to current step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (rs_ctx->ver->state == ecdsa_ver_muladd) { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 530 | goto muladd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 531 | } |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 532 | } |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 533 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 534 | |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 535 | /* |
| 536 | * Step 1: make sure r and s are in range 1..n-1 |
| 537 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 || |
| 539 | 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] | 540 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 541 | goto cleanup; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 545 | * Step 3: derive MPI from hashed message |
| 546 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen)); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 548 | |
| 549 | /* |
| 550 | * Step 4: u1 = e / s mod n, u2 = r / s mod n |
| 551 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | 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] | 553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | 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] | 555 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv)); |
| 557 | 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] | 558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv)); |
| 560 | 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] | 561 | |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 562 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | if (rs_ctx != NULL && rs_ctx->ver != NULL) { |
Manuel Pégourié-Gonnard | 6348181 | 2017-08-24 11:16:01 +0200 | [diff] [blame] | 564 | rs_ctx->ver->state = ecdsa_ver_muladd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | } |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 566 | |
| 567 | muladd: |
| 568 | #endif |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 569 | /* |
| 570 | * Step 5: R = u1 G + u2 Q |
| 571 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp, |
| 573 | &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP)); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | if (mbedtls_ecp_is_zero(&R)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 577 | goto cleanup; |
| 578 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 579 | |
| 580 | /* |
Manuel Pégourié-Gonnard | 178d9ba | 2013-10-29 10:45:28 +0100 | [diff] [blame] | 581 | * Step 6: convert xR to an integer (no-op) |
| 582 | * Step 7: reduce xR mod n (gives v) |
| 583 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | 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] | 585 | |
| 586 | /* |
| 587 | * 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] | 588 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
Paul Bakker | cca998a | 2013-07-26 14:20:53 +0200 | [diff] [blame] | 591 | goto cleanup; |
| 592 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 593 | |
| 594 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | mbedtls_ecp_point_free(&R); |
| 596 | mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv); |
| 597 | mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | ECDSA_RS_LEAVE(ver); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | return ret; |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 604 | /* |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 605 | * Verify ECDSA signature of hashed message |
| 606 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp, |
| 608 | const unsigned char *buf, size_t blen, |
| 609 | const mbedtls_ecp_point *Q, |
| 610 | const mbedtls_mpi *r, |
| 611 | const mbedtls_mpi *s) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 612 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | return ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL); |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 614 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 615 | #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */ |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 616 | |
| 617 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 618 | * Convert a signature (given by context) to ASN.1 |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 619 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s, |
| 621 | unsigned char *sig, size_t sig_size, |
| 622 | size_t *slen) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 623 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 624 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 }; |
| 626 | unsigned char *p = buf + sizeof(buf); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 627 | size_t len = 0; |
| 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, s)); |
| 630 | 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] | 631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); |
| 633 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf, |
| 634 | MBEDTLS_ASN1_CONSTRUCTED | |
| 635 | MBEDTLS_ASN1_SEQUENCE)); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | if (len > sig_size) { |
| 638 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 639 | } |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | memcpy(sig, p, len); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 642 | *slen = len; |
| 643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | return 0; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 648 | * Compute and write signature |
| 649 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx, |
| 651 | mbedtls_md_type_t md_alg, |
| 652 | const unsigned char *hash, size_t hlen, |
| 653 | unsigned char *sig, size_t sig_size, size_t *slen, |
| 654 | int (*f_rng)(void *, unsigned char *, size_t), |
| 655 | void *p_rng, |
| 656 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 657 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 658 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 659 | mbedtls_mpi r, s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | if (f_rng == NULL) { |
| 661 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 662 | } |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | mbedtls_mpi_init(&r); |
| 665 | mbedtls_mpi_init(&s); |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 666 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | MBEDTLS_MPI_CHK(ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d, |
| 669 | hash, hlen, md_alg, f_rng, |
| 670 | p_rng, rs_ctx)); |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 671 | #else |
| 672 | (void) md_alg; |
| 673 | |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 674 | #if defined(MBEDTLS_ECDSA_SIGN_ALT) |
Steven Cooreman | fa6641b | 2021-01-11 17:11:39 +0100 | [diff] [blame] | 675 | (void) rs_ctx; |
| 676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d, |
| 678 | hash, hlen, f_rng, p_rng)); |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 679 | #else |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 680 | /* Use the same RNG for both blinding and ephemeral key generation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | MBEDTLS_MPI_CHK(ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d, |
| 682 | hash, hlen, f_rng, p_rng, f_rng, |
| 683 | p_rng, rs_ctx)); |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 684 | #endif /* MBEDTLS_ECDSA_SIGN_ALT */ |
Ron Eldor | 5ed8c1e | 2018-11-05 14:04:26 +0200 | [diff] [blame] | 685 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 686 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | 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] | 688 | |
| 689 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | mbedtls_mpi_free(&r); |
| 691 | mbedtls_mpi_free(&s); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | return ret; |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 696 | /* |
| 697 | * Compute and write signature |
| 698 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, |
| 700 | mbedtls_md_type_t md_alg, |
| 701 | const unsigned char *hash, size_t hlen, |
| 702 | unsigned char *sig, size_t sig_size, size_t *slen, |
| 703 | int (*f_rng)(void *, unsigned char *, size_t), |
| 704 | void *p_rng) |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 705 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | return mbedtls_ecdsa_write_signature_restartable( |
| 707 | ctx, md_alg, hash, hlen, sig, sig_size, slen, |
| 708 | f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 711 | /* |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 712 | * Read and check signature |
| 713 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 714 | int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx, |
| 715 | const unsigned char *hash, size_t hlen, |
| 716 | const unsigned char *sig, size_t slen) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 717 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | return mbedtls_ecdsa_read_signature_restartable( |
| 719 | ctx, hash, hlen, sig, slen, NULL); |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Restartable read and check signature |
| 724 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx, |
| 726 | const unsigned char *hash, size_t hlen, |
| 727 | const unsigned char *sig, size_t slen, |
| 728 | mbedtls_ecdsa_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 729 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 730 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 731 | unsigned char *p = (unsigned char *) sig; |
| 732 | const unsigned char *end = sig + slen; |
| 733 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | mbedtls_mpi r, s; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | mbedtls_mpi_init(&r); |
| 736 | mbedtls_mpi_init(&s); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, |
| 739 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 741 | goto cleanup; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | if (p + len != end) { |
| 745 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA, |
| 746 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 747 | goto cleanup; |
| 748 | } |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 || |
| 751 | (ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 752 | ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 753 | goto cleanup; |
| 754 | } |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 755 | #if defined(MBEDTLS_ECDSA_VERIFY_ALT) |
Steven Cooreman | fa6641b | 2021-01-11 17:11:39 +0100 | [diff] [blame] | 756 | (void) rs_ctx; |
| 757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | if ((ret = mbedtls_ecdsa_verify(&ctx->grp, hash, hlen, |
| 759 | &ctx->Q, &r, &s)) != 0) { |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 760 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | } |
Ron Eldor | 8493f80 | 2018-11-01 11:32:15 +0200 | [diff] [blame] | 762 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | if ((ret = ecdsa_verify_restartable(&ctx->grp, hash, hlen, |
| 764 | &ctx->Q, &r, &s, rs_ctx)) != 0) { |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 765 | goto cleanup; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 766 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 767 | #endif /* MBEDTLS_ECDSA_VERIFY_ALT */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 768 | |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 769 | /* At this point we know that the buffer starts with a valid signature. |
| 770 | * Return 0 if the buffer just contains the signature, and a specific |
| 771 | * error code if the valid signature is followed by more data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | if (p != end) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | } |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 775 | |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 776 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | mbedtls_mpi_free(&r); |
| 778 | mbedtls_mpi_free(&s); |
Manuel Pégourié-Gonnard | 8fce937 | 2015-03-31 13:06:41 +0200 | [diff] [blame] | 779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | return ret; |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Ron Eldor | 314adb6 | 2017-10-10 18:28:25 +0300 | [diff] [blame] | 783 | #if !defined(MBEDTLS_ECDSA_GENKEY_ALT) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 784 | /* |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 785 | * Generate key pair |
| 786 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
| 788 | 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] | 789 | { |
Ron Eldor | adb5234 | 2018-12-17 10:06:12 +0200 | [diff] [blame] | 790 | int ret = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 791 | ret = mbedtls_ecp_group_load(&ctx->grp, gid); |
| 792 | if (ret != 0) { |
| 793 | return ret; |
| 794 | } |
Ron Eldor | adb5234 | 2018-12-17 10:06:12 +0200 | [diff] [blame] | 795 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 796 | return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d, |
| 797 | &ctx->Q, f_rng, p_rng); |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 798 | } |
Ron Eldor | 936d284 | 2018-11-01 13:05:52 +0200 | [diff] [blame] | 799 | #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */ |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 800 | |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 801 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 802 | * Set context from an mbedtls_ecp_keypair |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 803 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | 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] | 805 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 806 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 || |
| 808 | (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 || |
| 809 | (ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) { |
| 810 | mbedtls_ecdsa_free(ctx); |
Manuel Pégourié-Gonnard | 1001e32 | 2013-10-27 14:53:48 +0100 | [diff] [blame] | 811 | } |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 812 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 813 | return ret; |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 814 | } |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 815 | |
| 816 | /* |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 817 | * Initialize context |
| 818 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx) |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 820 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | mbedtls_ecp_keypair_init(ctx); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* |
| 825 | * Free context |
| 826 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx) |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 828 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | if (ctx == NULL) { |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 830 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | } |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | mbedtls_ecp_keypair_free(ctx); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 834 | } |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 835 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 836 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 837 | /* |
| 838 | * Initialize a restart context |
| 839 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 841 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | mbedtls_ecp_restart_init(&ctx->ecp); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 843 | |
| 844 | ctx->ver = NULL; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 845 | ctx->sig = NULL; |
| 846 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 847 | ctx->det = NULL; |
| 848 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Free the components of a restart context |
| 853 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 854 | void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 855 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | if (ctx == NULL) { |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 857 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | } |
Hanno Becker | 319ae11 | 2018-12-14 16:43:29 +0000 | [diff] [blame] | 859 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | mbedtls_ecp_restart_free(&ctx->ecp); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 862 | ecdsa_restart_ver_free(ctx->ver); |
| 863 | mbedtls_free(ctx->ver); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 864 | ctx->ver = NULL; |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | ecdsa_restart_sig_free(ctx->sig); |
| 867 | mbedtls_free(ctx->sig); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 868 | ctx->sig = NULL; |
| 869 | |
| 870 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | ecdsa_restart_det_free(ctx->det); |
| 872 | mbedtls_free(ctx->det); |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 873 | ctx->det = NULL; |
| 874 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 875 | } |
| 876 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | #endif /* MBEDTLS_ECDSA_C */ |