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