Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecdsa.h |
| 3 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 4 | * \brief The Elliptic Curve Digital Signature Algorithm (ECDSA). |
| 5 | * |
| 6 | * ECDSA is defined in <em>Standards for Efficient Cryptography Group (SECG): |
| 7 | * SEC1 Elliptic Curve Cryptography</em>. |
| 8 | * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve |
| 9 | * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>. |
| 10 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 11 | */ |
| 12 | /* |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame^] | 13 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 15 | * |
| 16 | * This file is provided under the Apache License 2.0, or the |
| 17 | * GNU General Public License v2.0 or later. |
| 18 | * |
| 19 | * ********** |
| 20 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 21 | * |
| 22 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 23 | * not use this file except in compliance with the License. |
| 24 | * You may obtain a copy of the License at |
| 25 | * |
| 26 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | * |
| 28 | * Unless required by applicable law or agreed to in writing, software |
| 29 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 30 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | * See the License for the specific language governing permissions and |
| 32 | * limitations under the License. |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 33 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 34 | * ********** |
| 35 | * |
| 36 | * ********** |
| 37 | * GNU General Public License v2.0 or later: |
| 38 | * |
| 39 | * This program is free software; you can redistribute it and/or modify |
| 40 | * it under the terms of the GNU General Public License as published by |
| 41 | * the Free Software Foundation; either version 2 of the License, or |
| 42 | * (at your option) any later version. |
| 43 | * |
| 44 | * This program is distributed in the hope that it will be useful, |
| 45 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 46 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 47 | * GNU General Public License for more details. |
| 48 | * |
| 49 | * You should have received a copy of the GNU General Public License along |
| 50 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 51 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 52 | * |
| 53 | * ********** |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 54 | */ |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #ifndef MBEDTLS_ECDSA_H |
| 57 | #define MBEDTLS_ECDSA_H |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 58 | |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 59 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 60 | #include "config.h" |
| 61 | #else |
| 62 | #include MBEDTLS_CONFIG_FILE |
| 63 | #endif |
| 64 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 65 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 887aa5b | 2014-04-04 13:57:20 +0200 | [diff] [blame] | 66 | #include "md.h" |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 67 | |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 68 | /* |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 69 | * RFC-4492 page 20: |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 70 | * |
| 71 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 72 | * r INTEGER, |
| 73 | * s INTEGER |
| 74 | * } |
| 75 | * |
| 76 | * Size is at most |
| 77 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 78 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 79 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 80 | * and less than 124 (total len <= 255) for the sequence) |
| 81 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | #if MBEDTLS_ECP_MAX_BYTES > 124 |
| 83 | #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN" |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 84 | #endif |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 85 | /** The maximal size of an ECDSA signature in Bytes. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) ) |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 88 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 89 | * \brief The ECDSA context structure. |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 90 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 93 | #ifdef __cplusplus |
| 94 | extern "C" { |
| 95 | #endif |
| 96 | |
| 97 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 98 | * \brief This function computes the ECDSA signature of a |
| 99 | * previously-hashed message. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 100 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 101 | * \note The deterministic version is usually preferred. |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 102 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 103 | * \param grp The ECP group. |
| 104 | * \param r The first output integer. |
| 105 | * \param s The second output integer. |
| 106 | * \param d The private signing key. |
| 107 | * \param buf The message hash. |
| 108 | * \param blen The length of \p buf. |
| 109 | * \param f_rng The RNG function. |
| 110 | * \param p_rng The RNG parameter. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 111 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 112 | * \note If the bitlength of the message hash is larger than the |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 113 | * bitlength of the group order, then the hash is truncated |
| 114 | * as defined in <em>Standards for Efficient Cryptography Group |
| 115 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 116 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 117 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 118 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX |
| 119 | * or \c MBEDTLS_MPI_XXX error code on failure. |
| 120 | * |
| 121 | * \see ecp.h |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 122 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 124 | 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] | 125 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 128 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 129 | * \brief This function computes the ECDSA signature of a |
| 130 | * previously-hashed message, deterministic version. |
| 131 | * For more information, see <em>RFC-6979: Deterministic |
| 132 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 133 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 134 | * |
Janos Follath | 2934c32 | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 135 | * |
| 136 | * \warning Since the output of the internal RNG is always the same for |
| 137 | * the same key and message, this limits the efficiency of |
| 138 | * blinding and leaks information through side channels. For |
| 139 | * secure behavior use mbedtls_ecdsa_sign_det_ext() instead. |
| 140 | * |
| 141 | * (Optimally the blinding is a random value that is different |
| 142 | * on every execution. In this case the blinding is still |
| 143 | * random from the attackers perspective, but is the same on |
| 144 | * each execution. This means that this blinding does not |
| 145 | * prevent attackers from recovering secrets by combining |
| 146 | * several measurement traces, but may prevent some attacks |
| 147 | * that exploit relationships between secret data.) |
| 148 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 149 | * \param grp The ECP group. |
| 150 | * \param r The first output integer. |
| 151 | * \param s The second output integer. |
| 152 | * \param d The private signing key. |
| 153 | * \param buf The message hash. |
| 154 | * \param blen The length of \p buf. |
| 155 | * \param md_alg The MD algorithm used to hash the message. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 156 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 157 | * \note If the bitlength of the message hash is larger than the |
| 158 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 159 | * defined in <em>Standards for Efficient Cryptography Group |
| 160 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 161 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 162 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 163 | * \return \c 0 on success, |
| 164 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 165 | * error code on failure. |
| 166 | * |
| 167 | * \see ecp.h |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 168 | */ |
Janos Follath | 2934c32 | 2019-01-04 14:32:30 +0000 | [diff] [blame] | 169 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, |
| 170 | mbedtls_mpi *s, const mbedtls_mpi *d, |
| 171 | const unsigned char *buf, size_t blen, |
| 172 | mbedtls_md_type_t md_alg ); |
| 173 | /** |
| 174 | * \brief This function computes the ECDSA signature of a |
| 175 | * previously-hashed message, deterministic version. |
| 176 | * |
| 177 | * For more information, see <em>RFC-6979: Deterministic |
| 178 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 179 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
| 180 | * |
| 181 | * \note If the bitlength of the message hash is larger than the |
| 182 | * bitlength of the group order, then the hash is truncated as |
| 183 | * defined in <em>Standards for Efficient Cryptography Group |
| 184 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 185 | * 4.1.3, step 5. |
| 186 | * |
| 187 | * \see ecp.h |
| 188 | * |
| 189 | * \param grp The context for the elliptic curve to use. |
| 190 | * This must be initialized and have group parameters |
| 191 | * set, for example through mbedtls_ecp_group_load(). |
| 192 | * \param r The MPI context in which to store the first part |
| 193 | * the signature. This must be initialized. |
| 194 | * \param s The MPI context in which to store the second part |
| 195 | * the signature. This must be initialized. |
| 196 | * \param d The private signing key. This must be initialized |
| 197 | * and setup, for example through mbedtls_ecp_gen_privkey(). |
| 198 | * \param buf The hashed content to be signed. This must be a readable |
| 199 | * buffer of length \p blen Bytes. It may be \c NULL if |
| 200 | * \p blen is zero. |
| 201 | * \param blen The length of \p buf in Bytes. |
| 202 | * \param md_alg The hash algorithm used to hash the original data. |
| 203 | * \param f_rng_blind The RNG function used for blinding. This must not be |
| 204 | * \c NULL. |
| 205 | * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be |
| 206 | * \c NULL if \p f_rng doesn't need a context parameter. |
| 207 | * |
| 208 | * \return \c 0 on success. |
| 209 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 210 | * error code on failure. |
| 211 | */ |
| 212 | int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r, |
| 213 | mbedtls_mpi *s, const mbedtls_mpi *d, |
| 214 | const unsigned char *buf, size_t blen, |
| 215 | mbedtls_md_type_t md_alg, |
| 216 | int (*f_rng_blind)(void *, unsigned char *, |
| 217 | size_t), |
| 218 | void *p_rng_blind ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 220 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 221 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 222 | * \brief This function verifies the ECDSA signature of a |
| 223 | * previously-hashed message. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 224 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 225 | * \param grp The ECP group. |
| 226 | * \param buf The message hash. |
| 227 | * \param blen The length of \p buf. |
| 228 | * \param Q The public key to use for verification. |
| 229 | * \param r The first integer of the signature. |
| 230 | * \param s The second integer of the signature. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 231 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 232 | * \note If the bitlength of the message hash is larger than the |
| 233 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 234 | * defined in <em>Standards for Efficient Cryptography Group |
| 235 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 236 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 237 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 238 | * \return \c 0 on success, |
| 239 | * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 240 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 241 | * error code on failure for any other reason. |
| 242 | * |
| 243 | * \see ecp.h |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 244 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 246 | const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s); |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 248 | |
| 249 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 250 | * \brief This function computes the ECDSA signature and writes it |
| 251 | * to a buffer, serialized as defined in <em>RFC-4492: |
| 252 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 253 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 254 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 255 | * \warning It is not thread-safe to use the same context in |
| 256 | * multiple threads. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 257 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 258 | * \note The deterministic version is used if |
| 259 | * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more |
| 260 | * information, see <em>RFC-6979: Deterministic Usage |
| 261 | * of the Digital Signature Algorithm (DSA) and Elliptic |
| 262 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 263 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 264 | * \param ctx The ECDSA context. |
| 265 | * \param md_alg The message digest that was used to hash the message. |
| 266 | * \param hash The message hash. |
| 267 | * \param hlen The length of the hash. |
| 268 | * \param sig The buffer that holds the signature. |
| 269 | * \param slen The length of the signature written. |
| 270 | * \param f_rng The RNG function. |
| 271 | * \param p_rng The RNG parameter. |
| 272 | * |
| 273 | * \note The \p sig buffer must be at least twice as large as the |
| 274 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 275 | * a 256-bit curve is used. A buffer length of |
| 276 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 277 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 278 | * \note If the bitlength of the message hash is larger than the |
| 279 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 280 | * defined in <em>Standards for Efficient Cryptography Group |
| 281 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 282 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 283 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 284 | * \return \c 0 on success, |
| 285 | * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 286 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 287 | * |
| 288 | * \see ecp.h |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 289 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 291 | const unsigned char *hash, size_t hlen, |
| 292 | unsigned char *sig, size_t *slen, |
| 293 | int (*f_rng)(void *, unsigned char *, size_t), |
| 294 | void *p_rng ); |
| 295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 297 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 298 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 299 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 300 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 302 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 303 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 304 | * \brief This function computes an ECDSA signature and writes it to a buffer, |
| 305 | * serialized as defined in <em>RFC-4492: Elliptic Curve Cryptography |
| 306 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>. |
| 307 | * |
| 308 | * The deterministic version is defined in <em>RFC-6979: |
| 309 | * Deterministic Usage of the Digital Signature Algorithm (DSA) and |
| 310 | * Elliptic Curve Digital Signature Algorithm (ECDSA)</em>. |
| 311 | * |
| 312 | * \warning It is not thread-safe to use the same context in |
| 313 | * multiple threads. |
| 314 | |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 315 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0 |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 317 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 318 | * \param ctx The ECDSA context. |
| 319 | * \param hash The Message hash. |
| 320 | * \param hlen The length of the hash. |
| 321 | * \param sig The buffer that holds the signature. |
| 322 | * \param slen The length of the signature written. |
| 323 | * \param md_alg The MD algorithm used to hash the message. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 324 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 325 | * \note The \p sig buffer must be at least twice as large as the |
| 326 | * size of the curve used, plus 9. For example, 73 Bytes if a |
| 327 | * 256-bit curve is used. A buffer length of |
| 328 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 329 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 330 | * \note If the bitlength of the message hash is larger than the |
| 331 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 332 | * defined in <em>Standards for Efficient Cryptography Group |
| 333 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 334 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 335 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 336 | * \return \c 0 on success, |
| 337 | * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 338 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 339 | * |
| 340 | * \see ecp.h |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 341 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 343 | const unsigned char *hash, size_t hlen, |
| 344 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| 346 | #undef MBEDTLS_DEPRECATED |
| 347 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 348 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 349 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 350 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 351 | * \brief This function reads and verifies an ECDSA signature. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 352 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 353 | * \param ctx The ECDSA context. |
| 354 | * \param hash The message hash. |
| 355 | * \param hlen The size of the hash. |
| 356 | * \param sig The signature to read and verify. |
| 357 | * \param slen The size of \p sig. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [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 | bff87d9 | 2018-01-25 21:58:53 +0000 | [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 | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 365 | * \return \c 0 on success, |
| 366 | * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 367 | * #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 368 | * signature in sig but its length is less than \p siglen, |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 369 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 370 | * error code on failure for any other reason. |
| 371 | * |
| 372 | * \see ecp.h |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 373 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 375 | const unsigned char *hash, size_t hlen, |
| 376 | const unsigned char *sig, size_t slen ); |
| 377 | |
| 378 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 379 | * \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] | 380 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 381 | * \param ctx The ECDSA context to store the keypair in. |
| 382 | * \param gid The elliptic curve to use. One of the various |
| 383 | * \c MBEDTLS_ECP_DP_XXX macros depending on configuration. |
| 384 | * \param f_rng The RNG function. |
| 385 | * \param p_rng The RNG parameter. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 386 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 387 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on |
| 388 | * failure. |
| 389 | * |
| 390 | * \see ecp.h |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 391 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | 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] | 393 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 394 | |
| 395 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 396 | * \brief This function sets an ECDSA context from an EC key pair. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 397 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 398 | * \param ctx The ECDSA context to set. |
| 399 | * \param key The EC key to use. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 400 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 401 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on |
| 402 | * failure. |
| 403 | * |
| 404 | * \see ecp.h |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 405 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 407 | |
| 408 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 409 | * \brief This function initializes an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 410 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 411 | * \param ctx The ECDSA context to initialize. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 412 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 414 | |
| 415 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 416 | * \brief This function frees an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 417 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 418 | * \param ctx The ECDSA context to free. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 419 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 421 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 422 | #ifdef __cplusplus |
| 423 | } |
| 424 | #endif |
| 425 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 426 | #endif /* ecdsa.h */ |