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