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