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