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 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 35 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 887aa5b | 2014-04-04 13:57:20 +0200 | [diff] [blame] | 36 | #include "md.h" |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 38 | /* |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 39 | * RFC-4492 page 20: |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 40 | * |
| 41 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 42 | * r INTEGER, |
| 43 | * s INTEGER |
| 44 | * } |
| 45 | * |
| 46 | * Size is at most |
| 47 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 48 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 49 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 50 | * and less than 124 (total len <= 255) for the sequence) |
| 51 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if MBEDTLS_ECP_MAX_BYTES > 124 |
| 53 | #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] | 54 | #endif |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 55 | /** The maximal size of an ECDSA signature in Bytes. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #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] | 57 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 62 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 63 | * \brief The ECDSA context structure. |
Manuel Pégourié-Gonnard | eaf55be | 2017-08-23 14:40:21 +0200 | [diff] [blame] | 64 | * |
| 65 | * \warning Performing multiple operations concurrently on the same |
| 66 | * ECDSA context is not supported; objects of this type |
| 67 | * should not be shared between multiple threads. |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 68 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 72 | |
| 73 | /** |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 74 | * \brief Internal restart context for ecdsa_verify() |
| 75 | * |
| 76 | * \note Opaque struct |
| 77 | */ |
| 78 | typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx; |
| 79 | |
| 80 | /** |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 81 | * \brief Internal restart context for ecdsa_sign() |
| 82 | * |
| 83 | * \note Opaque struct, defined in ecdsa.c |
| 84 | */ |
| 85 | typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx; |
| 86 | |
| 87 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 88 | /** |
| 89 | * \brief Internal restart context for ecdsa_sign_det() |
| 90 | * |
| 91 | * \note Opaque struct, defined in ecdsa.c |
| 92 | */ |
| 93 | typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx; |
| 94 | #endif |
| 95 | |
| 96 | /** |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 97 | * \brief General context for resuming ECDSA operations |
| 98 | */ |
| 99 | typedef struct |
| 100 | { |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 101 | mbedtls_ecp_restart_ctx ecp; /*!< base context (admin+ecp info) */ |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 102 | mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 103 | mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */ |
| 104 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 105 | mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */ |
| 106 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 107 | } mbedtls_ecdsa_restart_ctx; |
| 108 | |
| 109 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 110 | |
| 111 | /* Now we can declare functions that take a pointer to that */ |
| 112 | typedef void mbedtls_ecdsa_restart_ctx; |
| 113 | |
| 114 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 115 | |
| 116 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 117 | * \brief This function computes the ECDSA signature of a |
| 118 | * previously-hashed message. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 119 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 120 | * \note The deterministic version is usually preferred. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 121 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 122 | * \note If the bitlength of the message hash is larger than the |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 123 | * bitlength of the group order, then the hash is truncated |
| 124 | * as defined in <em>Standards for Efficient Cryptography Group |
| 125 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 126 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 127 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 128 | * \see ecp.h |
| 129 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 130 | * \param grp The ECP group. |
| 131 | * \param r The first output integer. |
| 132 | * \param s The second output integer. |
| 133 | * \param d The private signing key. |
| 134 | * \param buf The message hash. |
| 135 | * \param blen The length of \p buf. |
| 136 | * \param f_rng The RNG function. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 137 | * \param p_rng The RNG context. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 138 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 139 | * \return \c 0 on success. |
| 140 | * \return An \c MBEDTLS_ERR_ECP_XXX |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 141 | * or \c MBEDTLS_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 142 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 144 | 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] | 145 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 148 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 149 | * \brief This function computes the ECDSA signature of a |
| 150 | * previously-hashed message, deterministic version. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 151 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 152 | * For more information, see <em>RFC-6979: Deterministic |
| 153 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 154 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 155 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 156 | * \note If the bitlength of the message hash is larger than the |
| 157 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 158 | * defined in <em>Standards for Efficient Cryptography Group |
| 159 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 160 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 161 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 162 | * \see ecp.h |
| 163 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 164 | * \param grp The ECP group. |
| 165 | * \param r The first output integer. |
| 166 | * \param s The second output integer. |
| 167 | * \param d The private signing key. |
| 168 | * \param buf The message hash. |
| 169 | * \param blen The length of \p buf. |
| 170 | * \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] | 171 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 172 | * \return \c 0 on success. |
Rose Zadik | 14d0d57 | 2018-04-16 16:09:30 +0100 | [diff] [blame] | 173 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 174 | * error code on failure. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 175 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 177 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 178 | mbedtls_md_type_t md_alg ); |
| 179 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 180 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 181 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 182 | * \brief This function verifies the ECDSA signature of a |
| 183 | * previously-hashed message. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 184 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 185 | * \note If the bitlength of the message hash is larger than the |
| 186 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 187 | * defined in <em>Standards for Efficient Cryptography Group |
| 188 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 189 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 190 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 191 | * \see ecp.h |
| 192 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 193 | * \param grp The ECP group. |
| 194 | * \param buf The message hash. |
| 195 | * \param blen The length of \p buf. |
| 196 | * \param Q The public key to use for verification. |
| 197 | * \param r The first integer of the signature. |
| 198 | * \param s The second integer of the signature. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 199 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 200 | * \return \c 0 on success. |
Rose Zadik | 14d0d57 | 2018-04-16 16:09:30 +0100 | [diff] [blame] | 201 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature |
| 202 | * is invalid. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 203 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 204 | * error code on failure for any other reason. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 205 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 207 | const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | 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] | 209 | |
| 210 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 211 | * \brief This function computes the ECDSA signature and writes it |
| 212 | * to a buffer, serialized as defined in <em>RFC-4492: |
| 213 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 214 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 215 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 216 | * \warning It is not thread-safe to use the same context in |
| 217 | * multiple threads. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 218 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 219 | * \note The deterministic version is used if |
| 220 | * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more |
| 221 | * information, see <em>RFC-6979: Deterministic Usage |
| 222 | * of the Digital Signature Algorithm (DSA) and Elliptic |
| 223 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 224 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 225 | * \note The \p sig buffer must be at least twice as large as the |
| 226 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 227 | * a 256-bit curve is used. A buffer length of |
| 228 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 229 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 230 | * \note If the bitlength of the message hash is larger than the |
| 231 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 232 | * defined in <em>Standards for Efficient Cryptography Group |
| 233 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 234 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 235 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 236 | * \see ecp.h |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 237 | * |
| 238 | * \param ctx The ECDSA context. |
| 239 | * \param md_alg The message digest that was used to hash the message. |
| 240 | * \param hash The message hash. |
| 241 | * \param hlen The length of the hash. |
| 242 | * \param sig The buffer that holds the signature. |
| 243 | * \param slen The length of the signature written. |
| 244 | * \param f_rng The RNG function. |
| 245 | * \param p_rng The RNG context. |
| 246 | * |
| 247 | * \return \c 0 on success. |
| 248 | * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 249 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 250 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | 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] | 252 | const unsigned char *hash, size_t hlen, |
| 253 | unsigned char *sig, size_t *slen, |
| 254 | int (*f_rng)(void *, unsigned char *, size_t), |
| 255 | void *p_rng ); |
| 256 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 257 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 258 | * \brief This function computes the ECDSA signature and writes it |
| 259 | * to a buffer, in a restartable way. |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 260 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 261 | * \see \c mbedtls_ecdsa_write_signature() |
| 262 | * |
| 263 | * \note This function is like \c mbedtls_ecdsa_write_signature() |
| 264 | * 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] | 265 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 266 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 267 | * \param ctx The ECDSA context. |
| 268 | * \param md_alg The message digest that was used to hash the message. |
| 269 | * \param hash The message hash. |
| 270 | * \param hlen The length of the hash. |
| 271 | * \param sig The buffer that holds the signature. |
| 272 | * \param slen The length of the signature written. |
| 273 | * \param f_rng The RNG function. |
| 274 | * \param p_rng The RNG context. |
| 275 | * \param rs_ctx The restart context. |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 276 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 277 | * \return \c 0 on success. |
| 278 | * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 279 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 280 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 281 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 282 | */ |
| 283 | int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx, |
| 284 | mbedtls_md_type_t md_alg, |
| 285 | const unsigned char *hash, size_t hlen, |
| 286 | unsigned char *sig, size_t *slen, |
| 287 | int (*f_rng)(void *, unsigned char *, size_t), |
| 288 | void *p_rng, |
| 289 | mbedtls_ecdsa_restart_ctx *rs_ctx ); |
| 290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 292 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 293 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 294 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 295 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 297 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 298 | /** |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 299 | * \brief This function computes an ECDSA signature and writes |
| 300 | * it to a buffer, serialized as defined in <em>RFC-4492: |
| 301 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 302 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 303 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 304 | * The deterministic version is defined in <em>RFC-6979: |
| 305 | * Deterministic Usage of the Digital Signature Algorithm (DSA) |
| 306 | * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +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 | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 310 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 311 | * \note The \p sig buffer must be at least twice as large as the |
| 312 | * size of the curve used, plus 9. For example, 73 Bytes if a |
| 313 | * 256-bit curve is used. A buffer length of |
| 314 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 315 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 316 | * \note If the bitlength of the message hash is larger than the |
| 317 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 318 | * defined in <em>Standards for Efficient Cryptography Group |
| 319 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 320 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 321 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 322 | * \see ecp.h |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 323 | * |
Rose Zadik | ec5d416 | 2018-04-17 15:55:28 +0100 | [diff] [blame] | 324 | * \deprecated Superseded by mbedtls_ecdsa_write_signature() in |
| 325 | * Mbed TLS version 2.0 and later. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 326 | * |
| 327 | * \param ctx The ECDSA context. |
Rose Zadik | 14d0d57 | 2018-04-16 16:09:30 +0100 | [diff] [blame] | 328 | * \param hash The message hash. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 329 | * \param hlen The length of the hash. |
| 330 | * \param sig The buffer that holds the signature. |
| 331 | * \param slen The length of the signature written. |
| 332 | * \param md_alg The MD algorithm used to hash the message. |
| 333 | * |
| 334 | * \return \c 0 on success. |
| 335 | * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 336 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 337 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 339 | const unsigned char *hash, size_t hlen, |
| 340 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| 342 | #undef MBEDTLS_DEPRECATED |
| 343 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 344 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 345 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 346 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 347 | * \brief This function reads and verifies an ECDSA signature. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 348 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 349 | * \note If the bitlength of the message hash is larger than the |
| 350 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 351 | * defined in <em>Standards for Efficient Cryptography Group |
| 352 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 353 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 354 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 355 | * \see ecp.h |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 356 | * |
| 357 | * \param ctx The ECDSA context. |
| 358 | * \param hash The message hash. |
| 359 | * \param hlen The size of the hash. |
| 360 | * \param sig The signature to read and verify. |
| 361 | * \param slen The size of \p sig. |
| 362 | * |
| 363 | * \return \c 0 on success. |
| 364 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid. |
Rose Zadik | abc9ec7 | 2018-04-23 06:16:40 +0100 | [diff] [blame] | 365 | * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 366 | * signature in \p sig, but its length is less than \p siglen. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 367 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 368 | * error code on failure for any other reason. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 369 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 370 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 371 | const unsigned char *hash, size_t hlen, |
| 372 | const unsigned char *sig, size_t slen ); |
| 373 | |
| 374 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 375 | * \brief This function reads and verifies an ECDSA signature, |
| 376 | * in a restartable way. |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 377 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 378 | * \see \c mbedtls_ecdsa_read_signature() |
| 379 | * |
| 380 | * \note This function is like \c mbedtls_ecdsa_read_signature() |
| 381 | * 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] | 382 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 383 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 384 | * \param ctx The ECDSA context. |
| 385 | * \param hash The message hash. |
| 386 | * \param hlen The size of the hash. |
| 387 | * \param sig The signature to read and verify. |
| 388 | * \param slen The size of \p sig. |
| 389 | * \param rs_ctx The restart context |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 390 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame^] | 391 | * \return \c 0 on success. |
| 392 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid. |
| 393 | * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 394 | * signature in \p sig, but its length is less than \p siglen. |
| 395 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 396 | * error code on failure for any other reason. |
| 397 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 398 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 399 | */ |
| 400 | int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx, |
| 401 | const unsigned char *hash, size_t hlen, |
| 402 | const unsigned char *sig, size_t slen, |
| 403 | mbedtls_ecdsa_restart_ctx *rs_ctx ); |
| 404 | |
| 405 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 406 | * \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] | 407 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 408 | * \see ecp.h |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 409 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 410 | * \param ctx The ECDSA context to store the keypair in. |
| 411 | * \param gid The elliptic curve to use. One of the various |
| 412 | * \c MBEDTLS_ECP_DP_XXX macros depending on configuration. |
| 413 | * \param f_rng The RNG function. |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 414 | * \param p_rng The RNG context. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 415 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 416 | * \return \c 0 on success. |
| 417 | * \return An \c MBEDTLS_ERR_ECP_XXX code on failure. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 418 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | 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] | 420 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 421 | |
| 422 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 423 | * \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] | 424 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 425 | * \see ecp.h |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 426 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 427 | * \param ctx The ECDSA context to set. |
| 428 | * \param key The EC key to use. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 429 | * |
Rose Zadik | 817297f | 2018-03-27 11:30:14 +0100 | [diff] [blame] | 430 | * \return \c 0 on success. |
| 431 | * \return An \c MBEDTLS_ERR_ECP_XXX code on failure. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 432 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | 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] | 434 | |
| 435 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 436 | * \brief This function initializes an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 437 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 438 | * \param ctx The ECDSA context to initialize. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 439 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 441 | |
| 442 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 443 | * \brief This function frees an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 444 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 445 | * \param ctx The ECDSA context to free. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 446 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 449 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 450 | /** |
| 451 | * \brief Initialize a restart context |
| 452 | */ |
| 453 | void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx ); |
| 454 | |
| 455 | /** |
| 456 | * \brief Free the components of a restart context |
| 457 | */ |
| 458 | void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx ); |
| 459 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 460 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 461 | #ifdef __cplusplus |
| 462 | } |
| 463 | #endif |
| 464 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 465 | #endif /* ecdsa.h */ |