Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecdsa.h |
| 3 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 4 | * \brief This file contains ECDSA definitions and functions. |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 5 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 6 | * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in |
| 7 | * <em>Standards for Efficient Cryptography Group (SECG): |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 8 | * SEC1 Elliptic Curve Cryptography</em>. |
| 9 | * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve |
| 10 | * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>. |
| 11 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 12 | */ |
| 13 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 14 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 15 | * 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] | 16 | */ |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | #ifndef MBEDTLS_ECDSA_H |
| 19 | #define MBEDTLS_ECDSA_H |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 20 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 21 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 22 | #include "mbedtls/config.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 23 | #else |
| 24 | #include MBEDTLS_CONFIG_FILE |
| 25 | #endif |
| 26 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 27 | #include "mbedtls/ecp.h" |
| 28 | #include "mbedtls/md.h" |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2f2b396 | 2018-11-12 15:06:57 +0100 | [diff] [blame] | 30 | /** |
| 31 | * \brief Maximum ECDSA signature size for a given curve bit size |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 32 | * |
Manuel Pégourié-Gonnard | 2f2b396 | 2018-11-12 15:06:57 +0100 | [diff] [blame] | 33 | * \param bits Curve size in bits |
| 34 | * \return Maximum signature size in bytes |
| 35 | * |
| 36 | * \note This macro returns a compile-time constant if its argument |
| 37 | * is one. It may evaluate its argument multiple times. |
| 38 | */ |
| 39 | /* |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 40 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 41 | * r INTEGER, |
| 42 | * s INTEGER |
| 43 | * } |
| 44 | * |
Manuel Pégourié-Gonnard | 2f2b396 | 2018-11-12 15:06:57 +0100 | [diff] [blame] | 45 | * For each of r and s, the value (V) may include an extra initial "0" bit. |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 46 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | #define MBEDTLS_ECDSA_MAX_SIG_LEN(bits) \ |
| 48 | (/*T,L of SEQUENCE*/ ((bits) >= 61 * 8 ? 3 : 2) + \ |
| 49 | /*T,L of r,s*/ 2 * (((bits) >= 127 * 8 ? 3 : 2) + \ |
| 50 | /*V of r,s*/ ((bits) + 8) / 8)) |
Manuel Pégourié-Gonnard | 2f2b396 | 2018-11-12 15:06:57 +0100 | [diff] [blame] | 51 | |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 52 | /** The maximal size of an ECDSA signature in Bytes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | #define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN(MBEDTLS_ECP_MAX_BITS) |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 55 | #ifdef __cplusplus |
| 56 | extern "C" { |
| 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 59 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 60 | * \brief The ECDSA context structure. |
Manuel Pégourié-Gonnard | eaf55be | 2017-08-23 14:40:21 +0200 | [diff] [blame] | 61 | * |
| 62 | * \warning Performing multiple operations concurrently on the same |
| 63 | * ECDSA context is not supported; objects of this type |
| 64 | * should not be shared between multiple threads. |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 65 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 67 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 68 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 69 | |
| 70 | /** |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 71 | * \brief Internal restart context for ecdsa_verify() |
| 72 | * |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 73 | * \note Opaque struct, defined in ecdsa.c |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 74 | */ |
| 75 | typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx; |
| 76 | |
| 77 | /** |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 78 | * \brief Internal restart context for ecdsa_sign() |
| 79 | * |
| 80 | * \note Opaque struct, defined in ecdsa.c |
| 81 | */ |
| 82 | typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx; |
| 83 | |
| 84 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 85 | /** |
| 86 | * \brief Internal restart context for ecdsa_sign_det() |
| 87 | * |
| 88 | * \note Opaque struct, defined in ecdsa.c |
| 89 | */ |
| 90 | typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx; |
| 91 | #endif |
| 92 | |
| 93 | /** |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 94 | * \brief General context for resuming ECDSA operations |
| 95 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | typedef struct { |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 97 | mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and |
| 98 | shared administrative info */ |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 99 | mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 100 | mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */ |
| 101 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 102 | mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */ |
| 103 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 104 | } mbedtls_ecdsa_restart_ctx; |
| 105 | |
| 106 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 107 | |
| 108 | /* Now we can declare functions that take a pointer to that */ |
| 109 | typedef void mbedtls_ecdsa_restart_ctx; |
| 110 | |
| 111 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 112 | |
| 113 | /** |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 114 | * \brief This function checks whether a given group can be used |
| 115 | * for ECDSA. |
| 116 | * |
| 117 | * \param gid The ECP group ID to check. |
| 118 | * |
| 119 | * \return \c 1 if the group can be used, \c 0 otherwise |
| 120 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid); |
Christoph M. Wintersteiger | 0082f9d | 2019-01-07 13:47:30 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 124 | * \brief This function computes the ECDSA signature of a |
| 125 | * previously-hashed message. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 126 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 127 | * \note The deterministic version implemented in |
| 128 | * mbedtls_ecdsa_sign_det() is usually preferred. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 129 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 130 | * \note If the bitlength of the message hash is larger than the |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 131 | * bitlength of the group order, then the hash is truncated |
| 132 | * as defined in <em>Standards for Efficient Cryptography Group |
| 133 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 134 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 135 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 136 | * \see ecp.h |
| 137 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 138 | * \param grp The context for the elliptic curve to use. |
| 139 | * This must be initialized and have group parameters |
| 140 | * set, for example through mbedtls_ecp_group_load(). |
| 141 | * \param r The MPI context in which to store the first part |
| 142 | * the signature. This must be initialized. |
| 143 | * \param s The MPI context in which to store the second part |
| 144 | * the signature. This must be initialized. |
| 145 | * \param d The private signing key. This must be initialized. |
| 146 | * \param buf The content to be signed. This is usually the hash of |
| 147 | * the original data to be signed. This must be a readable |
| 148 | * buffer of length \p blen Bytes. It may be \c NULL if |
| 149 | * \p blen is zero. |
| 150 | * \param blen The length of \p buf in Bytes. |
| 151 | * \param f_rng The RNG function. This must not be \c NULL. |
| 152 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 153 | * \c NULL if \p f_rng doesn't need a context parameter. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 154 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 155 | * \return \c 0 on success. |
| 156 | * \return An \c MBEDTLS_ERR_ECP_XXX |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 157 | * or \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 158 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 160 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 161 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 162 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Janos Follath | e65e059 | 2019-01-04 15:55:43 +0000 | [diff] [blame] | 165 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 166 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 167 | #else |
| 168 | #define MBEDTLS_DEPRECATED |
| 169 | #endif |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 170 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 171 | * \brief This function computes the ECDSA signature of a |
| 172 | * previously-hashed message, deterministic version. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 173 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 174 | * For more information, see <em>RFC-6979: Deterministic |
| 175 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 176 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 177 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 178 | * \note If the bitlength of the message hash is larger than the |
| 179 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 180 | * defined in <em>Standards for Efficient Cryptography Group |
| 181 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 182 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 183 | * |
Janos Follath | 75f2c20 | 2019-01-15 11:44:31 +0000 | [diff] [blame] | 184 | * \warning Since the output of the internal RNG is always the same for |
| 185 | * the same key and message, this limits the efficiency of |
| 186 | * blinding and leaks information through side channels. For |
| 187 | * secure behavior use mbedtls_ecdsa_sign_det_ext() instead. |
| 188 | * |
| 189 | * (Optimally the blinding is a random value that is different |
| 190 | * on every execution. In this case the blinding is still |
| 191 | * random from the attackers perspective, but is the same on |
| 192 | * each execution. This means that this blinding does not |
| 193 | * prevent attackers from recovering secrets by combining |
| 194 | * several measurement traces, but may prevent some attacks |
| 195 | * that exploit relationships between secret data.) |
| 196 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 197 | * \see ecp.h |
| 198 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 199 | * \param grp The context for the elliptic curve to use. |
| 200 | * This must be initialized and have group parameters |
| 201 | * set, for example through mbedtls_ecp_group_load(). |
| 202 | * \param r The MPI context in which to store the first part |
| 203 | * the signature. This must be initialized. |
| 204 | * \param s The MPI context in which to store the second part |
| 205 | * the signature. This must be initialized. |
| 206 | * \param d The private signing key. This must be initialized |
| 207 | * and setup, for example through mbedtls_ecp_gen_privkey(). |
| 208 | * \param buf The hashed content to be signed. This must be a readable |
| 209 | * buffer of length \p blen Bytes. It may be \c NULL if |
| 210 | * \p blen is zero. |
| 211 | * \param blen The length of \p buf in Bytes. |
| 212 | * \param md_alg The hash algorithm used to hash the original data. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 213 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 214 | * \return \c 0 on success. |
Rose Zadik | 14d0d57 | 2018-04-16 16:09:30 +0100 | [diff] [blame] | 215 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 216 | * error code on failure. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 217 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | int mbedtls_ecdsa_sign_det(mbedtls_ecp_group *grp, mbedtls_mpi *r, |
| 219 | mbedtls_mpi *s, const mbedtls_mpi *d, |
| 220 | const unsigned char *buf, size_t blen, |
| 221 | mbedtls_md_type_t md_alg) MBEDTLS_DEPRECATED; |
Janos Follath | e65e059 | 2019-01-04 15:55:43 +0000 | [diff] [blame] | 222 | #undef MBEDTLS_DEPRECATED |
| 223 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 224 | |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 225 | /** |
| 226 | * \brief This function computes the ECDSA signature of a |
| 227 | * previously-hashed message, deterministic version. |
| 228 | * |
| 229 | * For more information, see <em>RFC-6979: Deterministic |
| 230 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 231 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
| 232 | * |
| 233 | * \note If the bitlength of the message hash is larger than the |
| 234 | * bitlength of the group order, then the hash is truncated as |
| 235 | * defined in <em>Standards for Efficient Cryptography Group |
| 236 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 237 | * 4.1.3, step 5. |
| 238 | * |
| 239 | * \see ecp.h |
| 240 | * |
| 241 | * \param grp The context for the elliptic curve to use. |
| 242 | * This must be initialized and have group parameters |
| 243 | * set, for example through mbedtls_ecp_group_load(). |
| 244 | * \param r The MPI context in which to store the first part |
| 245 | * the signature. This must be initialized. |
| 246 | * \param s The MPI context in which to store the second part |
| 247 | * the signature. This must be initialized. |
| 248 | * \param d The private signing key. This must be initialized |
| 249 | * and setup, for example through mbedtls_ecp_gen_privkey(). |
| 250 | * \param buf The hashed content to be signed. This must be a readable |
| 251 | * buffer of length \p blen Bytes. It may be \c NULL if |
| 252 | * \p blen is zero. |
| 253 | * \param blen The length of \p buf in Bytes. |
| 254 | * \param md_alg The hash algorithm used to hash the original data. |
| 255 | * \param f_rng_blind The RNG function used for blinding. This must not be |
| 256 | * \c NULL. |
Andrzej Kurek | c00fb7f | 2023-07-14 05:29:20 -0400 | [diff] [blame] | 257 | * \param p_rng_blind The RNG context to be passed to \p f_rng_blind. This |
| 258 | * may be \c NULL if \p f_rng_blind doesn't need |
| 259 | * a context parameter. |
Janos Follath | dca667a | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 260 | * |
| 261 | * \return \c 0 on success. |
| 262 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 263 | * error code on failure. |
| 264 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r, |
| 266 | mbedtls_mpi *s, const mbedtls_mpi *d, |
| 267 | const unsigned char *buf, size_t blen, |
| 268 | mbedtls_md_type_t md_alg, |
| 269 | int (*f_rng_blind)(void *, unsigned char *, size_t), |
| 270 | void *p_rng_blind); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 272 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 273 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 274 | * \brief This function verifies the ECDSA signature of a |
| 275 | * previously-hashed message. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 276 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 277 | * \note If the bitlength of the message hash is larger than the |
| 278 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 279 | * defined in <em>Standards for Efficient Cryptography Group |
| 280 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 281 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 282 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 283 | * \see ecp.h |
| 284 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 285 | * \param grp The ECP group to use. |
| 286 | * This must be initialized and have group parameters |
| 287 | * set, for example through mbedtls_ecp_group_load(). |
| 288 | * \param buf The hashed content that was signed. This must be a readable |
| 289 | * buffer of length \p blen Bytes. It may be \c NULL if |
| 290 | * \p blen is zero. |
| 291 | * \param blen The length of \p buf in Bytes. |
| 292 | * \param Q The public key to use for verification. This must be |
| 293 | * initialized and setup. |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 294 | * \param r The first integer of the signature. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 295 | * This must be initialized. |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 296 | * \param s The second integer of the signature. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 297 | * This must be initialized. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 298 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 299 | * \return \c 0 on success. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 300 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
Dave Rodgman | c60b10a | 2022-08-19 11:08:07 +0100 | [diff] [blame] | 301 | * error code on failure. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 302 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp, |
| 304 | const unsigned char *buf, size_t blen, |
| 305 | const mbedtls_ecp_point *Q, const mbedtls_mpi *r, |
| 306 | const mbedtls_mpi *s); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 307 | |
| 308 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 309 | * \brief This function computes the ECDSA signature and writes it |
| 310 | * to a buffer, serialized as defined in <em>RFC-4492: |
| 311 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 312 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 313 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 314 | * \warning It is not thread-safe to use the same context in |
| 315 | * multiple threads. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 316 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 317 | * \note The deterministic version is used if |
| 318 | * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more |
| 319 | * information, see <em>RFC-6979: Deterministic Usage |
| 320 | * of the Digital Signature Algorithm (DSA) and Elliptic |
| 321 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 322 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 323 | * \note If the bitlength of the message hash is larger than the |
| 324 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 325 | * defined in <em>Standards for Efficient Cryptography Group |
| 326 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 327 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 328 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 329 | * \see ecp.h |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 330 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 331 | * \param ctx The ECDSA context to use. This must be initialized |
| 332 | * and have a group and private key bound to it, for example |
| 333 | * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair(). |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 334 | * \param md_alg The message digest that was used to hash the message. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 335 | * \param hash The message hash to be signed. This must be a readable |
Andrzej Kurek | 96ce1b0 | 2023-07-14 05:22:42 -0400 | [diff] [blame] | 336 | * buffer of length \p hlen Bytes. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 337 | * \param hlen The length of the hash \p hash in Bytes. |
| 338 | * \param sig The buffer to which to write the signature. This must be a |
| 339 | * writable buffer of length at least twice as large as the |
| 340 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 341 | * a 256-bit curve is used. A buffer length of |
| 342 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
| 343 | * \param slen The address at which to store the actual length of |
| 344 | * the signature written. Must not be \c NULL. |
| 345 | * \param f_rng The RNG function. This must not be \c NULL if |
| 346 | * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise, |
Janos Follath | e65e059 | 2019-01-04 15:55:43 +0000 | [diff] [blame] | 347 | * it is used only for blinding and may be set to \c NULL, but |
| 348 | * doing so is DEPRECATED. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 349 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 350 | * \c NULL if \p f_rng is \c NULL or doesn't use a context. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 351 | * |
| 352 | * \return \c 0 on success. |
| 353 | * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 354 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 355 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 356 | int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, |
| 357 | mbedtls_md_type_t md_alg, |
| 358 | const unsigned char *hash, size_t hlen, |
| 359 | unsigned char *sig, size_t *slen, |
| 360 | int (*f_rng)(void *, unsigned char *, size_t), |
| 361 | void *p_rng); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 362 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 363 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 364 | * \brief This function computes the ECDSA signature and writes it |
| 365 | * to a buffer, in a restartable way. |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 366 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 367 | * \see \c mbedtls_ecdsa_write_signature() |
| 368 | * |
| 369 | * \note This function is like \c mbedtls_ecdsa_write_signature() |
| 370 | * but it can return early and restart according to the limit |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 371 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 372 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 373 | * \param ctx The ECDSA context to use. This must be initialized |
| 374 | * and have a group and private key bound to it, for example |
| 375 | * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair(). |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 376 | * \param md_alg The message digest that was used to hash the message. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 377 | * \param hash The message hash to be signed. This must be a readable |
Andrzej Kurek | 96ce1b0 | 2023-07-14 05:22:42 -0400 | [diff] [blame] | 378 | * buffer of length \p hlen Bytes. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 379 | * \param hlen The length of the hash \p hash in Bytes. |
| 380 | * \param sig The buffer to which to write the signature. This must be a |
| 381 | * writable buffer of length at least twice as large as the |
| 382 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 383 | * a 256-bit curve is used. A buffer length of |
| 384 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
| 385 | * \param slen The address at which to store the actual length of |
| 386 | * the signature written. Must not be \c NULL. |
| 387 | * \param f_rng The RNG function. This must not be \c NULL if |
| 388 | * #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise, |
| 389 | * it is unused and may be set to \c NULL. |
| 390 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 391 | * \c NULL if \p f_rng is \c NULL or doesn't use a context. |
| 392 | * \param rs_ctx The restart context to use. This may be \c NULL to disable |
| 393 | * restarting. If it is not \c NULL, it must point to an |
| 394 | * initialized restart context. |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 395 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 396 | * \return \c 0 on success. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 397 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 398 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 399 | * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 400 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 401 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx, |
| 403 | mbedtls_md_type_t md_alg, |
| 404 | const unsigned char *hash, size_t hlen, |
| 405 | unsigned char *sig, size_t *slen, |
| 406 | int (*f_rng)(void *, unsigned char *, size_t), |
| 407 | void *p_rng, |
| 408 | mbedtls_ecdsa_restart_ctx *rs_ctx); |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 409 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 413 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 414 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 416 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 417 | /** |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 418 | * \brief This function computes an ECDSA signature and writes |
| 419 | * it to a buffer, serialized as defined in <em>RFC-4492: |
| 420 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 421 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 422 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 423 | * The deterministic version is defined in <em>RFC-6979: |
| 424 | * Deterministic Usage of the Digital Signature Algorithm (DSA) |
| 425 | * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 426 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 427 | * \warning It is not thread-safe to use the same context in |
| 428 | * multiple threads. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 429 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 430 | * \note If the bitlength of the message hash is larger than the |
| 431 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 432 | * defined in <em>Standards for Efficient Cryptography Group |
| 433 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 434 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 435 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 436 | * \see ecp.h |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 437 | * |
Rose Zadik | ec5d416 | 2018-04-17 15:55:28 +0100 | [diff] [blame] | 438 | * \deprecated Superseded by mbedtls_ecdsa_write_signature() in |
| 439 | * Mbed TLS version 2.0 and later. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 440 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 441 | * \param ctx The ECDSA context to use. This must be initialized |
| 442 | * and have a group and private key bound to it, for example |
| 443 | * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair(). |
| 444 | * \param hash The message hash to be signed. This must be a readable |
Andrzej Kurek | db54dbd | 2023-07-14 05:35:38 -0400 | [diff] [blame] | 445 | * buffer of length \p hlen Bytes. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 446 | * \param hlen The length of the hash \p hash in Bytes. |
| 447 | * \param sig The buffer to which to write the signature. This must be a |
| 448 | * writable buffer of length at least twice as large as the |
| 449 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 450 | * a 256-bit curve is used. A buffer length of |
| 451 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
| 452 | * \param slen The address at which to store the actual length of |
| 453 | * the signature written. Must not be \c NULL. |
| 454 | * \param md_alg The message digest that was used to hash the message. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 455 | * |
| 456 | * \return \c 0 on success. |
| 457 | * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 458 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 459 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | int mbedtls_ecdsa_write_signature_det(mbedtls_ecdsa_context *ctx, |
| 461 | const unsigned char *hash, size_t hlen, |
| 462 | unsigned char *sig, size_t *slen, |
| 463 | mbedtls_md_type_t md_alg) MBEDTLS_DEPRECATED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | #undef MBEDTLS_DEPRECATED |
| 465 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 466 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 467 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 468 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 469 | * \brief This function reads and verifies an ECDSA signature. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 470 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 471 | * \note If the bitlength of the message hash is larger than the |
| 472 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 473 | * defined in <em>Standards for Efficient Cryptography Group |
| 474 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 475 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 476 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 477 | * \see ecp.h |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 478 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 479 | * \param ctx The ECDSA context to use. This must be initialized |
| 480 | * and have a group and public key bound to it. |
| 481 | * \param hash The message hash that was signed. This must be a readable |
Andrzej Kurek | 96ce1b0 | 2023-07-14 05:22:42 -0400 | [diff] [blame] | 482 | * buffer of length \p hlen Bytes. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 483 | * \param hlen The size of the hash \p hash. |
| 484 | * \param sig The signature to read and verify. This must be a readable |
| 485 | * buffer of length \p slen Bytes. |
| 486 | * \param slen The size of \p sig in Bytes. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 487 | * |
| 488 | * \return \c 0 on success. |
| 489 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid. |
Rose Zadik | abc9ec7 | 2018-04-23 06:16:40 +0100 | [diff] [blame] | 490 | * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 491 | * signature in \p sig, but its length is less than \p siglen. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 492 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 493 | * error code on failure for any other reason. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 494 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 495 | int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx, |
| 496 | const unsigned char *hash, size_t hlen, |
| 497 | const unsigned char *sig, size_t slen); |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 498 | |
| 499 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 500 | * \brief This function reads and verifies an ECDSA signature, |
| 501 | * in a restartable way. |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 502 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 503 | * \see \c mbedtls_ecdsa_read_signature() |
| 504 | * |
| 505 | * \note This function is like \c mbedtls_ecdsa_read_signature() |
| 506 | * but it can return early and restart according to the limit |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 507 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 508 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 509 | * \param ctx The ECDSA context to use. This must be initialized |
| 510 | * and have a group and public key bound to it. |
| 511 | * \param hash The message hash that was signed. This must be a readable |
Andrzej Kurek | 96ce1b0 | 2023-07-14 05:22:42 -0400 | [diff] [blame] | 512 | * buffer of length \p hlen Bytes. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 513 | * \param hlen The size of the hash \p hash. |
| 514 | * \param sig The signature to read and verify. This must be a readable |
| 515 | * buffer of length \p slen Bytes. |
| 516 | * \param slen The size of \p sig in Bytes. |
| 517 | * \param rs_ctx The restart context to use. This may be \c NULL to disable |
| 518 | * restarting. If it is not \c NULL, it must point to an |
| 519 | * initialized restart context. |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 520 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 521 | * \return \c 0 on success. |
| 522 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid. |
| 523 | * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 524 | * signature in \p sig, but its length is less than \p siglen. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 525 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 526 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 527 | * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 528 | * error code on failure for any other reason. |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 529 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 530 | int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx, |
| 531 | const unsigned char *hash, size_t hlen, |
| 532 | const unsigned char *sig, size_t slen, |
| 533 | mbedtls_ecdsa_restart_ctx *rs_ctx); |
Christoph M. Wintersteiger | ef17e3b | 2019-02-15 12:52:09 +0000 | [diff] [blame] | 534 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 535 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 536 | * \brief This function generates an ECDSA keypair on the given curve. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 537 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 538 | * \see ecp.h |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 539 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 540 | * \param ctx The ECDSA context to store the keypair in. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 541 | * This must be initialized. |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 542 | * \param gid The elliptic curve to use. One of the various |
| 543 | * \c MBEDTLS_ECP_DP_XXX macros depending on configuration. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 544 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 545 | * \param p_rng The RNG context to be passed to \p f_rng. This may be |
| 546 | * \c NULL if \p f_rng doesn't need a context argument. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 547 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 548 | * \return \c 0 on success. |
| 549 | * \return An \c MBEDTLS_ERR_ECP_XXX code on failure. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 550 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 551 | int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
| 552 | 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] | 553 | |
| 554 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 555 | * \brief This function sets up an ECDSA context from an EC key pair. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 556 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 557 | * \see ecp.h |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 558 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 559 | * \param ctx The ECDSA context to setup. This must be initialized. |
| 560 | * \param key The EC key to use. This must be initialized and hold |
| 561 | * a private-public key pair or a public key. In the former |
| 562 | * case, the ECDSA context may be used for signature creation |
| 563 | * and verification after this call. In the latter case, it |
| 564 | * may be used for signature verification. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 565 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 566 | * \return \c 0 on success. |
| 567 | * \return An \c MBEDTLS_ERR_ECP_XXX code on failure. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 568 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 569 | int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, |
| 570 | const mbedtls_ecp_keypair *key); |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 571 | |
| 572 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 573 | * \brief This function initializes an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 574 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 575 | * \param ctx The ECDSA context to initialize. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 576 | * This must not be \c NULL. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 577 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 578 | void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 579 | |
| 580 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 581 | * \brief This function frees an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 582 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 583 | * \param ctx The ECDSA context to free. This may be \c NULL, |
| 584 | * in which case this function does nothing. If it |
| 585 | * is not \c NULL, it must be initialized. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 586 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 587 | void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 588 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 589 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 590 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 591 | * \brief Initialize a restart context. |
| 592 | * |
| 593 | * \param ctx The restart context to initialize. |
| 594 | * This must not be \c NULL. |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 595 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 596 | void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx); |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 597 | |
| 598 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 599 | * \brief Free the components of a restart context. |
| 600 | * |
| 601 | * \param ctx The restart context to free. This may be \c NULL, |
| 602 | * in which case this function does nothing. If it |
| 603 | * is not \c NULL, it must be initialized. |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 604 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 605 | void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx); |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 606 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 607 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 608 | #ifdef __cplusplus |
| 609 | } |
| 610 | #endif |
| 611 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 612 | #endif /* ecdsa.h */ |