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