Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecdsa.h |
| 3 | * |
| 4 | * \brief Elliptic curve DSA |
| 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * 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] | 22 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_ECDSA_H |
| 24 | #define MBEDTLS_ECDSA_H |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 26 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 887aa5b | 2014-04-04 13:57:20 +0200 | [diff] [blame] | 27 | #include "md.h" |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 63e9319 | 2015-03-31 11:15:48 +0200 | [diff] [blame] | 29 | /* |
| 30 | * RFC 4492 page 20: |
| 31 | * |
| 32 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 33 | * r INTEGER, |
| 34 | * s INTEGER |
| 35 | * } |
| 36 | * |
| 37 | * Size is at most |
| 38 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 39 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 40 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 41 | * and less than 124 (total len <= 255) for the sequence) |
| 42 | */ |
Gilles Peskine | 9a8bb67 | 2017-11-02 17:09:49 +0100 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * \brief Maximum ECDSA signature size for a given curve bit size |
| 46 | * |
| 47 | * \param bits Curve size in bits |
| 48 | * \return Maximum signature size in bytes |
| 49 | * |
| 50 | * \note This macro returns a compile-time constant if its argument |
| 51 | * is one. It may evaluate its argument multiple times; if |
| 52 | * this is a problem, call the function |
| 53 | * mbedtls_ecdsa_max_sig_len instead. |
| 54 | */ |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 55 | #if MBEDTLS_ECP_MAX_BYTES > 124 |
| 56 | #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN" |
| 57 | #endif |
| 58 | |
Gilles Peskine | 9a8bb67 | 2017-11-02 17:09:49 +0100 | [diff] [blame] | 59 | #define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \ |
| 60 | ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \ |
| 61 | /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \ |
| 62 | /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) ) |
| 63 | |
| 64 | /** |
| 65 | * \brief Maximum ECDSA signature size for a given curve bit size |
| 66 | * |
| 67 | * \param bits Curve size in bits |
| 68 | * \return Maximum signature size in bytes |
| 69 | * |
| 70 | * \note If you need a compile-time constant, call the macro |
| 71 | * MBEDTLS_ECDSA_MAX_SIG_LEN instead. |
| 72 | */ |
| 73 | static inline size_t mbedtls_ecdsa_max_sig_len( size_t bits ) |
| 74 | { |
| 75 | return( MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) ); |
| 76 | } |
| 77 | |
Manuel Pégourié-Gonnard | 5bf262d | 2015-03-31 11:46:01 +0200 | [diff] [blame] | 78 | /** Maximum size of an ECDSA signature in bytes */ |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 79 | #define MBEDTLS_ECDSA_MAX_LEN (MBEDTLS_ECDSA_MAX_SIG_LEN( \ |
| 80 | 8 * MBEDTLS_ECP_MAX_BYTES ) ) |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 81 | /** |
| 82 | * \brief ECDSA context structure |
| 83 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 85 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 86 | #ifdef __cplusplus |
| 87 | extern "C" { |
| 88 | #endif |
| 89 | |
| 90 | /** |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 91 | * \brief Compute ECDSA signature of a previously hashed message |
| 92 | * |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 93 | * \note The deterministic version is usually prefered. |
| 94 | * |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 95 | * \param grp ECP group |
| 96 | * \param r First output integer |
| 97 | * \param s Second output integer |
| 98 | * \param d Private signing key |
| 99 | * \param buf Message hash |
| 100 | * \param blen Length of buf |
| 101 | * \param f_rng RNG function |
| 102 | * \param p_rng RNG parameter |
| 103 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 104 | * \note If the bitlength of the message hash is larger than the |
| 105 | * bitlength of the group order, then the hash is truncated as |
| 106 | * prescribed by SEC1 4.1.3 step 5. |
| 107 | * |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 108 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 110 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 112 | 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] | 113 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 116 | /** |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 117 | * \brief Compute ECDSA signature of a previously hashed message, |
| 118 | * deterministic version (RFC 6979). |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 119 | * |
| 120 | * \param grp ECP group |
| 121 | * \param r First output integer |
| 122 | * \param s Second output integer |
| 123 | * \param d Private signing key |
| 124 | * \param buf Message hash |
| 125 | * \param blen Length of buf |
| 126 | * \param md_alg MD algorithm used to hash the message |
| 127 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 128 | * \note If the bitlength of the message hash is larger than the |
| 129 | * bitlength of the group order, then the hash is truncated as |
| 130 | * prescribed by SEC1 4.1.3 step 5. |
| 131 | * |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 132 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 134 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 136 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 137 | mbedtls_md_type_t md_alg ); |
| 138 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 140 | /** |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 141 | * \brief Verify ECDSA signature of a previously hashed message |
| 142 | * |
| 143 | * \param grp ECP group |
| 144 | * \param buf Message hash |
| 145 | * \param blen Length of buf |
| 146 | * \param Q Public key to use for verification |
| 147 | * \param r First integer of the signature |
| 148 | * \param s Second integer of the signature |
| 149 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 150 | * \note If the bitlength of the message hash is larger than the |
| 151 | * bitlength of the group order, then the hash is truncated as |
| 152 | * prescribed by SEC1 4.1.4 step 3. |
| 153 | * |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 154 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid |
| 156 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 157 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 159 | const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | 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] | 161 | |
| 162 | /** |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 163 | * \brief Compute ECDSA signature and write it to buffer, |
| 164 | * serialized as defined in RFC 4492 page 20. |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 165 | * (Not thread-safe to use same context in multiple threads) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 166 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 167 | * \note The deterministic version (RFC 6979) is used if |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | * MBEDTLS_ECDSA_DETERMINISTIC is defined. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 169 | * |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 170 | * \param ctx ECDSA context |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 171 | * \param md_alg Algorithm that was used to hash the message |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 172 | * \param hash Message hash |
| 173 | * \param hlen Length of hash |
| 174 | * \param sig Buffer that will hold the signature |
| 175 | * \param slen Length of the signature written |
| 176 | * \param f_rng RNG function |
| 177 | * \param p_rng RNG parameter |
| 178 | * |
Gilles Peskine | 9a8bb67 | 2017-11-02 17:09:49 +0100 | [diff] [blame] | 179 | * \note The \c sig buffer must be at least |
| 180 | * `MBEDTLS_ECDSA_MAX_SIG_LEN(ctx->grp.pbits)` bytes long. |
| 181 | * MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 182 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 183 | * \note If the bitlength of the message hash is larger than the |
| 184 | * bitlength of the group order, then the hash is truncated as |
| 185 | * prescribed by SEC1 4.1.3 step 5. |
| 186 | * |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 187 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or |
| 189 | * MBEDTLS_ERR_ASN1_XXX error code |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 190 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | 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] | 192 | const unsigned char *hash, size_t hlen, |
| 193 | unsigned char *sig, size_t *slen, |
| 194 | int (*f_rng)(void *, unsigned char *, size_t), |
| 195 | void *p_rng ); |
| 196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 198 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 199 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 200 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 201 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 203 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 204 | /** |
| 205 | * \brief Compute ECDSA signature and write it to buffer, |
| 206 | * serialized as defined in RFC 4492 page 20. |
| 207 | * Deterministic version, RFC 6979. |
| 208 | * (Not thread-safe to use same context in multiple threads) |
| 209 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | * \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] | 211 | * |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 212 | * \param ctx ECDSA context |
| 213 | * \param hash Message hash |
| 214 | * \param hlen Length of hash |
| 215 | * \param sig Buffer that will hold the signature |
| 216 | * \param slen Length of the signature written |
| 217 | * \param md_alg MD algorithm used to hash the message |
| 218 | * |
Gilles Peskine | 9a8bb67 | 2017-11-02 17:09:49 +0100 | [diff] [blame] | 219 | * \note The \c sig buffer must be at least |
| 220 | * `MBEDTLS_ECDSA_MAX_SIG_LEN(ctx->grp.pbits)` bytes long. |
| 221 | * MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 222 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 223 | * \note If the bitlength of the message hash is larger than the |
| 224 | * bitlength of the group order, then the hash is truncated as |
| 225 | * prescribed by SEC1 4.1.3 step 5. |
| 226 | * |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 227 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or |
| 229 | * MBEDTLS_ERR_ASN1_XXX error code |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 230 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 232 | const unsigned char *hash, size_t hlen, |
| 233 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| 235 | #undef MBEDTLS_DEPRECATED |
| 236 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 237 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 239 | /** |
Unknown | 60b25f0 | 2018-02-06 03:17:59 -0500 | [diff] [blame] | 240 | * \brief Convert a signature from numbers to ASN.1 INTEGER's, |
| 241 | * then both packed together as parts of an ASN.1 SEQUENCE |
Gilles Peskine | bce41d3 | 2017-11-02 17:14:18 +0100 | [diff] [blame] | 242 | * |
| 243 | * \param r First number of the signature |
| 244 | * \param s Second number of the signature |
| 245 | * \param sig Buffer that will hold the signature |
| 246 | * \param slen Length of the signature written |
| 247 | * \param ssize Size of the sig buffer |
| 248 | * |
| 249 | * \note The size of the buffer \c ssize should be at least |
| 250 | * `MBEDTLS_ECDSA_MAX_SIG_LEN(grp->pbits)` bytes long if |
| 251 | * the signature was produced from curve \c grp, |
| 252 | * otherwise this function will return an error. |
| 253 | * |
| 254 | * \return 0 if successful, |
| 255 | * or a MBEDTLS_ERR_MPI_XXX or MBEDTLS_ERR_ASN1_XXX error code |
| 256 | * |
| 257 | */ |
Unknown | a2c4062 | 2018-02-06 03:24:02 -0500 | [diff] [blame] | 258 | 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] | 259 | unsigned char *sig, size_t *slen, |
| 260 | size_t ssize ); |
| 261 | |
| 262 | /** |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 263 | * \brief Read and verify an ECDSA signature |
| 264 | * |
| 265 | * \param ctx ECDSA context |
| 266 | * \param hash Message hash |
| 267 | * \param hlen Size of hash |
| 268 | * \param sig Signature to read and verify |
| 269 | * \param slen Size of sig |
| 270 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 271 | * \note If the bitlength of the message hash is larger than the |
| 272 | * bitlength of the group order, then the hash is truncated as |
| 273 | * prescribed by SEC1 4.1.4 step 3. |
| 274 | * |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 275 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 277 | * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 278 | * valid but its actual length is less than siglen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 280 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 282 | const unsigned char *hash, size_t hlen, |
| 283 | const unsigned char *sig, size_t slen ); |
| 284 | |
| 285 | /** |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 286 | * \brief Generate an ECDSA keypair on the given curve |
| 287 | * |
| 288 | * \param ctx ECDSA context in which the keypair should be stored |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 289 | * \param gid Group (elliptic curve) to use. One of the various |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | * MBEDTLS_ECP_DP_XXX macros depending on configuration. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 291 | * \param f_rng RNG function |
| 292 | * \param p_rng RNG parameter |
| 293 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 295 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | 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] | 297 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 298 | |
| 299 | /** |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 300 | * \brief Set an ECDSA context from an EC key pair |
| 301 | * |
| 302 | * \param ctx ECDSA context to set |
| 303 | * \param key EC key to use |
| 304 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code. |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 306 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | 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] | 308 | |
| 309 | /** |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 310 | * \brief Initialize context |
| 311 | * |
| 312 | * \param ctx Context to initialize |
| 313 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * \brief Free context |
| 318 | * |
| 319 | * \param ctx Context to free |
| 320 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 323 | #ifdef __cplusplus |
| 324 | } |
| 325 | #endif |
| 326 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 327 | #endif /* ecdsa.h */ |