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 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if MBEDTLS_ECP_MAX_BYTES > 124 |
| 44 | #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] | 45 | #endif |
Manuel Pégourié-Gonnard | 5bf262d | 2015-03-31 11:46:01 +0200 | [diff] [blame] | 46 | /** Maximum size of an ECDSA signature in bytes */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #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] | 48 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 49 | #ifdef __cplusplus |
| 50 | extern "C" { |
| 51 | #endif |
| 52 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 53 | /** |
| 54 | * \brief ECDSA context structure |
| 55 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 59 | |
| 60 | /** |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 61 | * \brief Internal restart context for ecdsa_verify() |
| 62 | * |
| 63 | * \note Opaque struct |
| 64 | */ |
| 65 | typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx; |
| 66 | |
| 67 | /** |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 68 | * \brief Internal restart context for ecdsa_sign() |
| 69 | * |
| 70 | * \note Opaque struct, defined in ecdsa.c |
| 71 | */ |
| 72 | typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx; |
| 73 | |
| 74 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 75 | /** |
| 76 | * \brief Internal restart context for ecdsa_sign_det() |
| 77 | * |
| 78 | * \note Opaque struct, defined in ecdsa.c |
| 79 | */ |
| 80 | typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx; |
| 81 | #endif |
| 82 | |
| 83 | /** |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 84 | * \brief General context for resuming ECDSA operations |
| 85 | */ |
| 86 | typedef struct |
| 87 | { |
Manuel Pégourié-Gonnard | 722e515 | 2017-04-21 11:04:47 +0200 | [diff] [blame] | 88 | mbedtls_ecp_restart_ctx ecp; /*!< base context (admin+ecp info) */ |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 89 | mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */ |
Manuel Pégourié-Gonnard | b90883d | 2017-04-25 11:33:10 +0200 | [diff] [blame] | 90 | mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */ |
| 91 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 92 | mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */ |
| 93 | #endif |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 94 | } mbedtls_ecdsa_restart_ctx; |
| 95 | |
| 96 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 97 | |
| 98 | /* Now we can declare functions that take a pointer to that */ |
| 99 | typedef void mbedtls_ecdsa_restart_ctx; |
| 100 | |
| 101 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 102 | |
| 103 | /** |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 104 | * \brief Compute ECDSA signature of a previously hashed message |
| 105 | * |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 106 | * \note The deterministic version is usually prefered. |
| 107 | * |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 108 | * \param grp ECP group |
| 109 | * \param r First output integer |
| 110 | * \param s Second output integer |
| 111 | * \param d Private signing key |
| 112 | * \param buf Message hash |
| 113 | * \param blen Length of buf |
| 114 | * \param f_rng RNG function |
| 115 | * \param p_rng RNG parameter |
| 116 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 117 | * \note If the bitlength of the message hash is larger than the |
| 118 | * bitlength of the group order, then the hash is truncated as |
| 119 | * prescribed by SEC1 4.1.3 step 5. |
| 120 | * |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 121 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | * 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] | 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 | /** |
Manuel Pégourié-Gonnard | b8cfe3f | 2015-03-31 11:04:45 +0200 | [diff] [blame] | 130 | * \brief Compute ECDSA signature of a previously hashed message, |
| 131 | * deterministic version (RFC 6979). |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 132 | * |
| 133 | * \param grp ECP group |
| 134 | * \param r First output integer |
| 135 | * \param s Second output integer |
| 136 | * \param d Private signing key |
| 137 | * \param buf Message hash |
| 138 | * \param blen Length of buf |
| 139 | * \param md_alg MD algorithm used to hash the message |
| 140 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 141 | * \note If the bitlength of the message hash is larger than the |
| 142 | * bitlength of the group order, then the hash is truncated as |
| 143 | * prescribed by SEC1 4.1.3 step 5. |
| 144 | * |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 145 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | * 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] | 147 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| 149 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| 150 | mbedtls_md_type_t md_alg ); |
| 151 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 152 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 153 | /** |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 154 | * \brief Verify ECDSA signature of a previously hashed message |
| 155 | * |
| 156 | * \param grp ECP group |
| 157 | * \param buf Message hash |
| 158 | * \param blen Length of buf |
| 159 | * \param Q Public key to use for verification |
| 160 | * \param r First integer of the signature |
| 161 | * \param s Second integer of the signature |
| 162 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 163 | * \note If the bitlength of the message hash is larger than the |
| 164 | * bitlength of the group order, then the hash is truncated as |
| 165 | * prescribed by SEC1 4.1.4 step 3. |
| 166 | * |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 167 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 1ed2505 | 2017-04-21 10:04:02 +0200 | [diff] [blame] | 168 | * MBEDTLS_ERR_ECP_VERIFY_FAILED if signature is invalid |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | * 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] | 170 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 172 | const unsigned char *buf, size_t blen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | 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] | 174 | |
| 175 | /** |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 176 | * \brief Compute ECDSA signature and write it to buffer, |
| 177 | * serialized as defined in RFC 4492 page 20. |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 178 | * (Not thread-safe to use same context in multiple threads) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 179 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 180 | * \note The deterministic version (RFC 6979) is used if |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | * MBEDTLS_ECDSA_DETERMINISTIC is defined. |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 182 | * |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 183 | * \param ctx ECDSA context |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 184 | * \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] | 185 | * \param hash Message hash |
| 186 | * \param hlen Length of hash |
| 187 | * \param sig Buffer that will hold the signature |
| 188 | * \param slen Length of the signature written |
| 189 | * \param f_rng RNG function |
| 190 | * \param p_rng RNG parameter |
| 191 | * |
| 192 | * \note The "sig" buffer must be at least as large as twice the |
Manuel Pégourié-Gonnard | 5bf262d | 2015-03-31 11:46:01 +0200 | [diff] [blame] | 193 | * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 195 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 196 | * \note If the bitlength of the message hash is larger than the |
| 197 | * bitlength of the group order, then the hash is truncated as |
| 198 | * prescribed by SEC1 4.1.3 step 5. |
| 199 | * |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 200 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or |
| 202 | * MBEDTLS_ERR_ASN1_XXX error code |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 203 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 205 | const unsigned char *hash, size_t hlen, |
| 206 | unsigned char *sig, size_t *slen, |
| 207 | int (*f_rng)(void *, unsigned char *, size_t), |
| 208 | void *p_rng ); |
| 209 | |
Manuel Pégourié-Gonnard | addb10e | 2017-04-21 12:54:46 +0200 | [diff] [blame] | 210 | /** |
| 211 | * \brief Restartable version of \c mbedtls_ecdsa_write_signature() |
| 212 | * |
| 213 | * \note Performs the same job as \c mbedtls_ecdsa_write_signature() |
| 214 | * but can return early and restart according to the limit |
| 215 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 216 | * |
| 217 | * \param ctx ECDSA context |
| 218 | * \param md_alg Algorithm that was used to hash the message |
| 219 | * \param hash Message hash |
| 220 | * \param hlen Length of hash |
| 221 | * \param sig Buffer that will hold the signature |
| 222 | * \param slen Length of the signature written |
| 223 | * \param f_rng RNG function |
| 224 | * \param p_rng RNG parameter |
| 225 | * \param rs_ctx Restart context |
| 226 | * |
| 227 | * \return See \c mbedtls_ecdsa_write_signature(), or |
| 228 | * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
| 229 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 230 | */ |
| 231 | int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx, |
| 232 | mbedtls_md_type_t md_alg, |
| 233 | const unsigned char *hash, size_t hlen, |
| 234 | unsigned char *sig, size_t *slen, |
| 235 | int (*f_rng)(void *, unsigned char *, size_t), |
| 236 | void *p_rng, |
| 237 | mbedtls_ecdsa_restart_ctx *rs_ctx ); |
| 238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| 240 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 241 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 242 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 243 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | dfdcac9 | 2015-03-31 11:41:42 +0200 | [diff] [blame] | 245 | #endif |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 246 | /** |
| 247 | * \brief Compute ECDSA signature and write it to buffer, |
| 248 | * serialized as defined in RFC 4492 page 20. |
| 249 | * Deterministic version, RFC 6979. |
| 250 | * (Not thread-safe to use same context in multiple threads) |
| 251 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | * \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] | 253 | * |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 254 | * \param ctx ECDSA context |
| 255 | * \param hash Message hash |
| 256 | * \param hlen Length of hash |
| 257 | * \param sig Buffer that will hold the signature |
| 258 | * \param slen Length of the signature written |
| 259 | * \param md_alg MD algorithm used to hash the message |
| 260 | * |
| 261 | * \note The "sig" buffer must be at least as large as twice the |
Manuel Pégourié-Gonnard | 5bf262d | 2015-03-31 11:46:01 +0200 | [diff] [blame] | 262 | * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe. |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 264 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 265 | * \note If the bitlength of the message hash is larger than the |
| 266 | * bitlength of the group order, then the hash is truncated as |
| 267 | * prescribed by SEC1 4.1.3 step 5. |
| 268 | * |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 269 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or |
| 271 | * MBEDTLS_ERR_ASN1_XXX error code |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 272 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 274 | const unsigned char *hash, size_t hlen, |
| 275 | unsigned char *sig, size_t *slen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| 277 | #undef MBEDTLS_DEPRECATED |
| 278 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| 279 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 280 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 281 | /** |
| 282 | * \brief Read and verify an ECDSA signature |
| 283 | * |
| 284 | * \param ctx ECDSA context |
| 285 | * \param hash Message hash |
| 286 | * \param hlen Size of hash |
| 287 | * \param sig Signature to read and verify |
| 288 | * \param slen Size of sig |
| 289 | * |
Janos Follath | 0a5154b | 2017-03-10 11:31:41 +0000 | [diff] [blame] | 290 | * \note If the bitlength of the message hash is larger than the |
| 291 | * bitlength of the group order, then the hash is truncated as |
| 292 | * prescribed by SEC1 4.1.4 step 3. |
| 293 | * |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 294 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 296 | * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 297 | * valid but its actual length is less than siglen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | * 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] | 299 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 301 | const unsigned char *hash, size_t hlen, |
| 302 | const unsigned char *sig, size_t slen ); |
| 303 | |
| 304 | /** |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 305 | * \brief Restartable version of \c mbedtls_ecdsa_read_signature() |
| 306 | * |
| 307 | * \note Performs the same job as \c mbedtls_ecdsa_read_signature() |
| 308 | * but can return early and restart according to the limit |
| 309 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 310 | * |
| 311 | * \param ctx ECDSA context |
| 312 | * \param hash Message hash |
| 313 | * \param hlen Size of hash |
| 314 | * \param sig Signature to read and verify |
| 315 | * \param slen Size of sig |
| 316 | * \param rs_ctx Restart context |
| 317 | * |
| 318 | * \return See \c mbedtls_ecdsa_read_signature(), or |
| 319 | * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
| 320 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 321 | */ |
| 322 | int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx, |
| 323 | const unsigned char *hash, size_t hlen, |
| 324 | const unsigned char *sig, size_t slen, |
| 325 | mbedtls_ecdsa_restart_ctx *rs_ctx ); |
| 326 | |
| 327 | /** |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 328 | * \brief Generate an ECDSA keypair on the given curve |
| 329 | * |
| 330 | * \param ctx ECDSA context in which the keypair should be stored |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 331 | * \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] | 332 | * MBEDTLS_ECP_DP_XXX macros depending on configuration. |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 333 | * \param f_rng RNG function |
| 334 | * \param p_rng RNG parameter |
| 335 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | * \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] | 337 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | 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] | 339 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 340 | |
| 341 | /** |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 342 | * \brief Set an ECDSA context from an EC key pair |
| 343 | * |
| 344 | * \param ctx ECDSA context to set |
| 345 | * \param key EC key to use |
| 346 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | * \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] | 348 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | 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] | 350 | |
| 351 | /** |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 352 | * \brief Initialize context |
| 353 | * |
| 354 | * \param ctx Context to initialize |
| 355 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 356 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 357 | |
| 358 | /** |
| 359 | * \brief Free context |
| 360 | * |
| 361 | * \param ctx Context to free |
| 362 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 364 | |
Manuel Pégourié-Gonnard | 32aa437 | 2017-04-21 10:29:13 +0200 | [diff] [blame] | 365 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 366 | /** |
| 367 | * \brief Initialize a restart context |
| 368 | */ |
| 369 | void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx ); |
| 370 | |
| 371 | /** |
| 372 | * \brief Free the components of a restart context |
| 373 | */ |
| 374 | void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx ); |
| 375 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
| 376 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 377 | #ifdef __cplusplus |
| 378 | } |
| 379 | #endif |
| 380 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 381 | #endif /* ecdsa.h */ |