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