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 | |
Ron Eldor | 0559c66 | 2018-02-14 16:02:41 +0200 | [diff] [blame^] | 34 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 35 | #include "config.h" |
| 36 | #else |
| 37 | #include MBEDTLS_CONFIG_FILE |
| 38 | #endif |
| 39 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 40 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 887aa5b | 2014-04-04 13:57:20 +0200 | [diff] [blame] | 41 | #include "md.h" |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 42 | |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 43 | /* |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 44 | * RFC-4492 page 20: |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 45 | * |
| 46 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 47 | * r INTEGER, |
| 48 | * s INTEGER |
| 49 | * } |
| 50 | * |
| 51 | * Size is at most |
| 52 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 53 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 54 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 55 | * and less than 124 (total len <= 255) for the sequence) |
| 56 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #if MBEDTLS_ECP_MAX_BYTES > 124 |
| 58 | #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] | 59 | #endif |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 60 | /** The maximal size of an ECDSA signature in Bytes. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #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] | 62 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 63 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 64 | * \brief The ECDSA context structure. |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 65 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 67 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 68 | #ifdef __cplusplus |
| 69 | extern "C" { |
| 70 | #endif |
| 71 | |
| 72 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 73 | * \brief This function computes the ECDSA signature of a |
| 74 | * previously-hashed message. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 75 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 76 | * \note The deterministic version is usually preferred. |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 77 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 78 | * \param grp The ECP group. |
| 79 | * \param r The first output integer. |
| 80 | * \param s The second output integer. |
| 81 | * \param d The private signing key. |
| 82 | * \param buf The message hash. |
| 83 | * \param blen The length of \p buf. |
| 84 | * \param f_rng The RNG function. |
| 85 | * \param p_rng The RNG parameter. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 86 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 87 | * \note If the bitlength of the message hash is larger than the |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 88 | * bitlength of the group order, then the hash is truncated |
| 89 | * as defined in <em>Standards for Efficient Cryptography Group |
| 90 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 91 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 92 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 93 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX |
| 94 | * or \c MBEDTLS_MPI_XXX error code on failure. |
| 95 | * |
| 96 | * \see ecp.h |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 97 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 99 | 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] | 100 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 103 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 104 | * \brief This function computes the ECDSA signature of a |
| 105 | * previously-hashed message, deterministic version. |
| 106 | * For more information, see <em>RFC-6979: Deterministic |
| 107 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 108 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 109 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 110 | * \param grp The ECP group. |
| 111 | * \param r The first output integer. |
| 112 | * \param s The second output integer. |
| 113 | * \param d The private signing key. |
| 114 | * \param buf The message hash. |
| 115 | * \param blen The length of \p buf. |
| 116 | * \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] | 117 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 118 | * \note If the bitlength of the message hash is larger than the |
| 119 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 120 | * defined in <em>Standards for Efficient Cryptography Group |
| 121 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 122 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 123 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 124 | * \return \c 0 on success, |
| 125 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 126 | * error code on failure. |
| 127 | * |
| 128 | * \see ecp.h |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 129 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 131 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 132 | mbedtls_md_type_t md_alg ); |
| 133 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 134 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 135 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 136 | * \brief This function verifies the ECDSA signature of a |
| 137 | * previously-hashed message. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 138 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 139 | * \param grp The ECP group. |
| 140 | * \param buf The message hash. |
| 141 | * \param blen The length of \p buf. |
| 142 | * \param Q The public key to use for verification. |
| 143 | * \param r The first integer of the signature. |
| 144 | * \param s The second integer of the signature. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 145 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 146 | * \note If the bitlength of the message hash is larger than the |
| 147 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 148 | * defined in <em>Standards for Efficient Cryptography Group |
| 149 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 150 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 151 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 152 | * \return \c 0 on success, |
| 153 | * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 154 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 155 | * error code on failure for any other reason. |
| 156 | * |
| 157 | * \see ecp.h |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 160 | const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | 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] | 162 | |
| 163 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 164 | * \brief This function computes the ECDSA signature and writes it |
| 165 | * to a buffer, serialized as defined in <em>RFC-4492: |
| 166 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 167 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 168 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 169 | * \warning It is not thread-safe to use the same context in |
| 170 | * multiple threads. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 171 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 172 | * \note The deterministic version is used if |
| 173 | * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more |
| 174 | * information, see <em>RFC-6979: Deterministic Usage |
| 175 | * of the Digital Signature Algorithm (DSA) and Elliptic |
| 176 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 177 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 178 | * \param ctx The ECDSA context. |
| 179 | * \param md_alg The message digest that was used to hash the message. |
| 180 | * \param hash The message hash. |
| 181 | * \param hlen The length of the hash. |
| 182 | * \param sig The buffer that holds the signature. |
| 183 | * \param slen The length of the signature written. |
| 184 | * \param f_rng The RNG function. |
| 185 | * \param p_rng The RNG parameter. |
| 186 | * |
| 187 | * \note The \p sig buffer must be at least twice as large as the |
| 188 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 189 | * a 256-bit curve is used. A buffer length of |
| 190 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 191 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 192 | * \note If the bitlength of the message hash is larger than the |
| 193 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 194 | * defined in <em>Standards for Efficient Cryptography Group |
| 195 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 196 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 197 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 198 | * \return \c 0 on success, |
| 199 | * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 200 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 201 | * |
| 202 | * \see ecp.h |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 203 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | 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] | 205 | const unsigned char *hash, size_t hlen, |
| 206 | unsigned char *sig, size_t *slen, |
| 207 | int (*f_rng)(void *, unsigned char *, size_t), |
| 208 | void *p_rng ); |
| 209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 211 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 212 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 213 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 214 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 216 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 217 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 218 | * \brief This function computes an ECDSA signature and writes it to a buffer, |
| 219 | * serialized as defined in <em>RFC-4492: Elliptic Curve Cryptography |
| 220 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>. |
| 221 | * |
| 222 | * The deterministic version is defined in <em>RFC-6979: |
| 223 | * Deterministic Usage of the Digital Signature Algorithm (DSA) and |
| 224 | * Elliptic Curve Digital Signature Algorithm (ECDSA)</em>. |
| 225 | * |
| 226 | * \warning It is not thread-safe to use the same context in |
| 227 | * multiple threads. |
| 228 | |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 229 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | * \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] | 231 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 232 | * \param ctx The ECDSA context. |
| 233 | * \param hash The Message hash. |
| 234 | * \param hlen The length of the hash. |
| 235 | * \param sig The buffer that holds the signature. |
| 236 | * \param slen The length of the signature written. |
| 237 | * \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] | 238 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 239 | * \note The \p sig buffer must be at least twice as large as the |
| 240 | * size of the curve used, plus 9. For example, 73 Bytes if a |
| 241 | * 256-bit curve is used. A buffer length of |
| 242 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 243 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 244 | * \note If the bitlength of the message hash is larger than the |
| 245 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 246 | * defined in <em>Standards for Efficient Cryptography Group |
| 247 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 248 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 249 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 250 | * \return \c 0 on success, |
| 251 | * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 252 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 253 | * |
| 254 | * \see ecp.h |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 255 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 257 | const unsigned char *hash, size_t hlen, |
| 258 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| 260 | #undef MBEDTLS_DEPRECATED |
| 261 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 262 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 263 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 264 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 265 | * \brief This function reads and verifies an ECDSA signature. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 266 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 267 | * \param ctx The ECDSA context. |
| 268 | * \param hash The message hash. |
| 269 | * \param hlen The size of the hash. |
| 270 | * \param sig The signature to read and verify. |
| 271 | * \param slen The size of \p sig. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 272 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 273 | * \note If the bitlength of the message hash is larger than the |
| 274 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 275 | * defined in <em>Standards for Efficient Cryptography Group |
| 276 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 277 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 278 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 279 | * \return \c 0 on success, |
| 280 | * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 281 | * #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 282 | * signature in sig but its length is less than \p siglen, |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 283 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 284 | * error code on failure for any other reason. |
| 285 | * |
| 286 | * \see ecp.h |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 287 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 289 | const unsigned char *hash, size_t hlen, |
| 290 | const unsigned char *sig, size_t slen ); |
| 291 | |
| 292 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 293 | * \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] | 294 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 295 | * \param ctx The ECDSA context to store the keypair in. |
| 296 | * \param gid The elliptic curve to use. One of the various |
| 297 | * \c MBEDTLS_ECP_DP_XXX macros depending on configuration. |
| 298 | * \param f_rng The RNG function. |
| 299 | * \param p_rng The RNG parameter. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 300 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 301 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on |
| 302 | * failure. |
| 303 | * |
| 304 | * \see ecp.h |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 305 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | 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] | 307 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 308 | |
| 309 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 310 | * \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] | 311 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 312 | * \param ctx The ECDSA context to set. |
| 313 | * \param key The EC key to use. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 314 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 315 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on |
| 316 | * failure. |
| 317 | * |
| 318 | * \see ecp.h |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 319 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | 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] | 321 | |
| 322 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 323 | * \brief This function initializes an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 324 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 325 | * \param ctx The ECDSA context to initialize. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 326 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 328 | |
| 329 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 330 | * \brief This function frees an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 331 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 332 | * \param ctx The ECDSA context to free. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 333 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 335 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 336 | #ifdef __cplusplus |
| 337 | } |
| 338 | #endif |
| 339 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 340 | #endif /* ecdsa.h */ |