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 |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 55 | /** |
Gilles Peskine | 9a8bb67 | 2017-11-02 17:09:49 +0100 | [diff] [blame] | 56 | * \brief Maximum ECDSA signature size for a given curve bit size |
| 57 | * |
| 58 | * \param bits Curve size in bits |
| 59 | * \return Maximum signature size in bytes |
| 60 | * |
| 61 | * \note This macro returns a compile-time constant if its argument |
| 62 | * is one. It may evaluate its argument multiple times; if |
| 63 | * this is a problem, call the function |
| 64 | * mbedtls_ecdsa_max_sig_len instead. |
| 65 | */ |
| 66 | #define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \ |
| 67 | ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \ |
| 68 | /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \ |
| 69 | /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) ) |
| 70 | |
| 71 | /** |
| 72 | * \brief Maximum ECDSA signature size for a given curve bit size |
| 73 | * |
| 74 | * \param bits Curve size in bits |
| 75 | * \return Maximum signature size in bytes |
| 76 | * |
| 77 | * \note If you need a compile-time constant, call the macro |
| 78 | * MBEDTLS_ECDSA_MAX_SIG_LEN instead. |
| 79 | */ |
| 80 | static inline size_t mbedtls_ecdsa_max_sig_len( size_t bits ) |
| 81 | { |
| 82 | return( MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) ); |
| 83 | } |
| 84 | |
Andrzej Kurek | 4924163 | 2018-02-08 09:03:21 -0500 | [diff] [blame] | 85 | /** The maximal size of an ECDSA signature in Bytes. */ |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 86 | #define MBEDTLS_ECDSA_MAX_LEN (MBEDTLS_ECDSA_MAX_SIG_LEN( \ |
| 87 | 8 * MBEDTLS_ECP_MAX_BYTES ) ) |
Andrzej Kurek | 024ab06 | 2018-02-12 09:34:39 -0500 | [diff] [blame^] | 88 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 89 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 90 | * \brief The ECDSA context structure. |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 91 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 93 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 94 | #ifdef __cplusplus |
| 95 | extern "C" { |
| 96 | #endif |
| 97 | |
| 98 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 99 | * \brief This function computes the ECDSA signature of a |
| 100 | * previously-hashed message. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 101 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 102 | * \note The deterministic version is usually preferred. |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 103 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 104 | * \param grp The ECP group. |
| 105 | * \param r The first output integer. |
| 106 | * \param s The second output integer. |
| 107 | * \param d The private signing key. |
| 108 | * \param buf The message hash. |
| 109 | * \param blen The length of \p buf. |
| 110 | * \param f_rng The RNG function. |
| 111 | * \param p_rng The RNG parameter. |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 112 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 113 | * \note If the bitlength of the message hash is larger than the |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 114 | * bitlength of the group order, then the hash is truncated |
| 115 | * as defined in <em>Standards for Efficient Cryptography Group |
| 116 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 117 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 118 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 119 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX |
| 120 | * or \c MBEDTLS_MPI_XXX error code on failure. |
| 121 | * |
| 122 | * \see ecp.h |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 123 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 125 | 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] | 126 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 129 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 130 | * \brief This function computes the ECDSA signature of a |
| 131 | * previously-hashed message, deterministic version. |
| 132 | * For more information, see <em>RFC-6979: Deterministic |
| 133 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 134 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 135 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 136 | * \param grp The ECP group. |
| 137 | * \param r The first output integer. |
| 138 | * \param s The second output integer. |
| 139 | * \param d The private signing key. |
| 140 | * \param buf The message hash. |
| 141 | * \param blen The length of \p buf. |
| 142 | * \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] | 143 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 144 | * \note If the bitlength of the message hash is larger than the |
| 145 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 146 | * defined in <em>Standards for Efficient Cryptography Group |
| 147 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 148 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 149 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 150 | * \return \c 0 on success, |
| 151 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 152 | * error code on failure. |
| 153 | * |
| 154 | * \see ecp.h |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 155 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 157 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 158 | mbedtls_md_type_t md_alg ); |
| 159 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 160 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 161 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 162 | * \brief This function verifies the ECDSA signature of a |
| 163 | * previously-hashed message. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 164 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 165 | * \param grp The ECP group. |
| 166 | * \param buf The message hash. |
| 167 | * \param blen The length of \p buf. |
| 168 | * \param Q The public key to use for verification. |
| 169 | * \param r The first integer of the signature. |
| 170 | * \param s The second integer of the signature. |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 171 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 172 | * \note If the bitlength of the message hash is larger than the |
| 173 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 174 | * defined in <em>Standards for Efficient Cryptography Group |
| 175 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 176 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 177 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 178 | * \return \c 0 on success, |
| 179 | * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 180 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX |
| 181 | * error code on failure for any other reason. |
| 182 | * |
| 183 | * \see ecp.h |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 184 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 186 | const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | 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] | 188 | |
| 189 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 190 | * \brief This function computes the ECDSA signature and writes it |
| 191 | * to a buffer, serialized as defined in <em>RFC-4492: |
| 192 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 193 | * Transport Layer Security (TLS)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 194 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 195 | * \warning It is not thread-safe to use the same context in |
| 196 | * multiple threads. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 197 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 198 | * \note The deterministic version is used if |
| 199 | * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more |
| 200 | * information, see <em>RFC-6979: Deterministic Usage |
| 201 | * of the Digital Signature Algorithm (DSA) and Elliptic |
| 202 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 203 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 204 | * \param ctx The ECDSA context. |
| 205 | * \param md_alg The message digest that was used to hash the message. |
| 206 | * \param hash The message hash. |
| 207 | * \param hlen The length of the hash. |
| 208 | * \param sig The buffer that holds the signature. |
| 209 | * \param slen The length of the signature written. |
| 210 | * \param f_rng The RNG function. |
| 211 | * \param p_rng The RNG parameter. |
| 212 | * |
| 213 | * \note The \p sig buffer must be at least twice as large as the |
| 214 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 215 | * a 256-bit curve is used. A buffer length of |
| 216 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 217 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 218 | * \note If the bitlength of the message hash is larger than the |
| 219 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 220 | * defined in <em>Standards for Efficient Cryptography Group |
| 221 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 222 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 223 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 224 | * \return \c 0 on success, |
| 225 | * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 226 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 227 | * |
| 228 | * \see ecp.h |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 229 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | 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] | 231 | const unsigned char *hash, size_t hlen, |
| 232 | unsigned char *sig, size_t *slen, |
| 233 | int (*f_rng)(void *, unsigned char *, size_t), |
| 234 | void *p_rng ); |
| 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 237 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 238 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 239 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 240 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 242 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 243 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 244 | * \brief This function computes an ECDSA signature and writes it to a buffer, |
| 245 | * serialized as defined in <em>RFC-4492: Elliptic Curve Cryptography |
| 246 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>. |
| 247 | * |
| 248 | * The deterministic version is defined in <em>RFC-6979: |
| 249 | * Deterministic Usage of the Digital Signature Algorithm (DSA) and |
| 250 | * Elliptic Curve Digital Signature Algorithm (ECDSA)</em>. |
| 251 | * |
| 252 | * \warning It is not thread-safe to use the same context in |
| 253 | * multiple threads. |
| 254 | |
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 | * \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] | 257 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 258 | * \param ctx The ECDSA context. |
| 259 | * \param hash The Message hash. |
| 260 | * \param hlen The length of the hash. |
| 261 | * \param sig The buffer that holds the signature. |
| 262 | * \param slen The length of the signature written. |
| 263 | * \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] | 264 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 265 | * \note The \p sig buffer must be at least twice as large as the |
| 266 | * size of the curve used, plus 9. For example, 73 Bytes if a |
| 267 | * 256-bit curve is used. A buffer length of |
| 268 | * #MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 269 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 270 | * \note If the bitlength of the message hash is larger than the |
| 271 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 272 | * defined in <em>Standards for Efficient Cryptography Group |
| 273 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 274 | * 4.1.3, step 5. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 275 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 276 | * \return \c 0 on success, |
| 277 | * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or |
| 278 | * \c MBEDTLS_ERR_ASN1_XXX error code on failure. |
| 279 | * |
| 280 | * \see ecp.h |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 283 | const unsigned char *hash, size_t hlen, |
| 284 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| 286 | #undef MBEDTLS_DEPRECATED |
| 287 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 288 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 289 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 290 | /** |
Andrzej Kurek | 024ab06 | 2018-02-12 09:34:39 -0500 | [diff] [blame^] | 291 | * \brief Convert an ECDSA signature from number pair format to ASN.1 |
Gilles Peskine | bce41d3 | 2017-11-02 17:14:18 +0100 | [diff] [blame] | 292 | * |
| 293 | * \param r First number of the signature |
| 294 | * \param s Second number of the signature |
| 295 | * \param sig Buffer that will hold the signature |
| 296 | * \param slen Length of the signature written |
| 297 | * \param ssize Size of the sig buffer |
| 298 | * |
| 299 | * \note The size of the buffer \c ssize should be at least |
| 300 | * `MBEDTLS_ECDSA_MAX_SIG_LEN(grp->pbits)` bytes long if |
| 301 | * the signature was produced from curve \c grp, |
| 302 | * otherwise this function will return an error. |
Unknown | 6f21aed | 2018-02-07 08:02:31 -0500 | [diff] [blame] | 303 | * The output ASN.1 SEQUENCE format is as follows: |
| 304 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 305 | * r INTEGER, |
| 306 | * s INTEGER |
| 307 | * } |
Gilles Peskine | bce41d3 | 2017-11-02 17:14:18 +0100 | [diff] [blame] | 308 | * |
| 309 | * \return 0 if successful, |
| 310 | * or a MBEDTLS_ERR_MPI_XXX or MBEDTLS_ERR_ASN1_XXX error code |
| 311 | * |
| 312 | */ |
Unknown | a2c4062 | 2018-02-06 03:24:02 -0500 | [diff] [blame] | 313 | int mbedtls_ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s, |
Gilles Peskine | bce41d3 | 2017-11-02 17:14:18 +0100 | [diff] [blame] | 314 | unsigned char *sig, size_t *slen, |
| 315 | size_t ssize ); |
| 316 | |
| 317 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 318 | * \brief This function reads and verifies an ECDSA signature. |
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 | * \param ctx The ECDSA context. |
| 321 | * \param hash The message hash. |
| 322 | * \param hlen The size of the hash. |
| 323 | * \param sig The signature to read and verify. |
| 324 | * \param slen The size of \p sig. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 325 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 326 | * \note If the bitlength of the message hash is larger than the |
| 327 | * bitlength of the group order, then the hash is truncated as |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 328 | * defined in <em>Standards for Efficient Cryptography Group |
| 329 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 330 | * 4.1.4, step 3. |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 331 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 332 | * \return \c 0 on success, |
| 333 | * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 334 | * #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is |
| 335 | * valid but its actual length is less than \p siglen, |
| 336 | * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX |
| 337 | * error code on failure for any other reason. |
| 338 | * |
| 339 | * \see ecp.h |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 340 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 342 | const unsigned char *hash, size_t hlen, |
| 343 | const unsigned char *sig, size_t slen ); |
| 344 | |
| 345 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 346 | * \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] | 347 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 348 | * \param ctx The ECDSA context to store the keypair in. |
| 349 | * \param gid The elliptic curve to use. One of the various |
| 350 | * \c MBEDTLS_ECP_DP_XXX macros depending on configuration. |
| 351 | * \param f_rng The RNG function. |
| 352 | * \param p_rng The RNG parameter. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 353 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 354 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on |
| 355 | * failure. |
| 356 | * |
| 357 | * \see ecp.h |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 358 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | 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] | 360 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 361 | |
| 362 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 363 | * \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] | 364 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 365 | * \param ctx The ECDSA context to set. |
| 366 | * \param key The EC key to use. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 367 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 368 | * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on |
| 369 | * failure. |
| 370 | * |
| 371 | * \see ecp.h |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 372 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | 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] | 374 | |
| 375 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 376 | * \brief This function initializes an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 377 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 378 | * \param ctx The ECDSA context to initialize. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 379 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 381 | |
| 382 | /** |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 383 | * \brief This function frees an ECDSA context. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 384 | * |
Rose Zadik | bff87d9 | 2018-01-25 21:58:53 +0000 | [diff] [blame] | 385 | * \param ctx The ECDSA context to free. |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 386 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 387 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 388 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 389 | #ifdef __cplusplus |
| 390 | } |
| 391 | #endif |
| 392 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 393 | #endif /* ecdsa.h */ |