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