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