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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://polarssl.org) |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 9 | * |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | #ifndef POLARSSL_ECDSA_H |
| 25 | #define POLARSSL_ECDSA_H |
| 26 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 27 | #include "ecp.h" |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 29 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
Manuel Pégourié-Gonnard | 887aa5b | 2014-04-04 13:57:20 +0200 | [diff] [blame] | 30 | #include "md.h" |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 31 | #endif |
| 32 | |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 33 | /** |
| 34 | * \brief ECDSA context structure |
Manuel Pégourié-Gonnard | 211a64c | 2013-08-09 15:04:26 +0200 | [diff] [blame] | 35 | * |
| 36 | * \note Purposefully begins with the same members as struct ecp_keypair. |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 37 | */ |
| 38 | typedef struct |
| 39 | { |
Paul Bakker | 237a847 | 2014-06-25 14:45:24 +0200 | [diff] [blame] | 40 | ecp_group grp; /*!< elliptic curve used */ |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 41 | mpi d; /*!< secret signature key */ |
| 42 | ecp_point Q; /*!< public signature key */ |
| 43 | mpi r; /*!< first integer from signature */ |
| 44 | mpi s; /*!< second integer from signature */ |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 45 | } |
| 46 | ecdsa_context; |
| 47 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
| 52 | /** |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 53 | * \brief Compute ECDSA signature of a previously hashed message |
| 54 | * |
| 55 | * \param grp ECP group |
| 56 | * \param r First output integer |
| 57 | * \param s Second output integer |
| 58 | * \param d Private signing key |
| 59 | * \param buf Message hash |
| 60 | * \param blen Length of buf |
| 61 | * \param f_rng RNG function |
| 62 | * \param p_rng RNG parameter |
| 63 | * |
| 64 | * \return 0 if successful, |
| 65 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 66 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 67 | int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s, |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 68 | const mpi *d, const unsigned char *buf, size_t blen, |
| 69 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 70 | |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 71 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 72 | /** |
| 73 | * \brief Compute ECDSA signature of a previously hashed message |
| 74 | * (deterministic version) |
| 75 | * |
| 76 | * \param grp ECP group |
| 77 | * \param r First output integer |
| 78 | * \param s Second output integer |
| 79 | * \param d Private signing key |
| 80 | * \param buf Message hash |
| 81 | * \param blen Length of buf |
| 82 | * \param md_alg MD algorithm used to hash the message |
| 83 | * |
| 84 | * \return 0 if successful, |
| 85 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 86 | */ |
| 87 | int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s, |
| 88 | const mpi *d, const unsigned char *buf, size_t blen, |
| 89 | md_type_t md_alg ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 90 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 4daaef7 | 2014-01-06 14:25:56 +0100 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 92 | /** |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 93 | * \brief Verify ECDSA signature of a previously hashed message |
| 94 | * |
| 95 | * \param grp ECP group |
| 96 | * \param buf Message hash |
| 97 | * \param blen Length of buf |
| 98 | * \param Q Public key to use for verification |
| 99 | * \param r First integer of the signature |
| 100 | * \param s Second integer of the signature |
| 101 | * |
| 102 | * \return 0 if successful, |
| 103 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid |
| 104 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 105 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 106 | int ecdsa_verify( ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 107 | const unsigned char *buf, size_t blen, |
| 108 | const ecp_point *Q, const mpi *r, const mpi *s); |
| 109 | |
| 110 | /** |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 111 | * \brief Compute ECDSA signature and write it to buffer, |
| 112 | * serialized as defined in RFC 4492 page 20. |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 113 | * (Not thread-safe to use same context in multiple threads) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 114 | * |
| 115 | * \param ctx ECDSA context |
| 116 | * \param hash Message hash |
| 117 | * \param hlen Length of hash |
| 118 | * \param sig Buffer that will hold the signature |
| 119 | * \param slen Length of the signature written |
| 120 | * \param f_rng RNG function |
| 121 | * \param p_rng RNG parameter |
| 122 | * |
| 123 | * \note The "sig" buffer must be at least as large as twice the |
| 124 | * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit |
| 125 | * curve is used). |
| 126 | * |
| 127 | * \return 0 if successful, |
| 128 | * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or |
| 129 | * POLARSSL_ERR_ASN1 error code |
| 130 | */ |
| 131 | int ecdsa_write_signature( ecdsa_context *ctx, |
| 132 | const unsigned char *hash, size_t hlen, |
| 133 | unsigned char *sig, size_t *slen, |
| 134 | int (*f_rng)(void *, unsigned char *, size_t), |
| 135 | void *p_rng ); |
| 136 | |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 137 | #if defined(POLARSSL_ECDSA_DETERMINISTIC) |
| 138 | /** |
| 139 | * \brief Compute ECDSA signature and write it to buffer, |
| 140 | * serialized as defined in RFC 4492 page 20. |
| 141 | * Deterministic version, RFC 6979. |
| 142 | * (Not thread-safe to use same context in multiple threads) |
| 143 | * |
| 144 | * \param ctx ECDSA context |
| 145 | * \param hash Message hash |
| 146 | * \param hlen Length of hash |
| 147 | * \param sig Buffer that will hold the signature |
| 148 | * \param slen Length of the signature written |
| 149 | * \param md_alg MD algorithm used to hash the message |
| 150 | * |
| 151 | * \note The "sig" buffer must be at least as large as twice the |
| 152 | * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit |
| 153 | * curve is used). |
| 154 | * |
| 155 | * \return 0 if successful, |
| 156 | * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or |
| 157 | * POLARSSL_ERR_ASN1 error code |
| 158 | */ |
| 159 | int ecdsa_write_signature_det( ecdsa_context *ctx, |
| 160 | const unsigned char *hash, size_t hlen, |
| 161 | unsigned char *sig, size_t *slen, |
| 162 | md_type_t md_alg ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 163 | #endif /* POLARSSL_ECDSA_DETERMINISTIC */ |
Manuel Pégourié-Gonnard | 937340b | 2014-01-06 10:27:16 +0100 | [diff] [blame] | 164 | |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 165 | /** |
| 166 | * \brief Read and verify an ECDSA signature |
| 167 | * |
| 168 | * \param ctx ECDSA context |
| 169 | * \param hash Message hash |
| 170 | * \param hlen Size of hash |
| 171 | * \param sig Signature to read and verify |
| 172 | * \param slen Size of sig |
| 173 | * |
| 174 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 35e95dd | 2014-04-08 12:17:41 +0200 | [diff] [blame] | 175 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| 176 | * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is |
| 177 | * valid but its actual length is less than siglen, |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 178 | * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code |
| 179 | */ |
| 180 | int ecdsa_read_signature( ecdsa_context *ctx, |
| 181 | const unsigned char *hash, size_t hlen, |
| 182 | const unsigned char *sig, size_t slen ); |
| 183 | |
| 184 | /** |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 185 | * \brief Generate an ECDSA keypair on the given curve |
| 186 | * |
| 187 | * \param ctx ECDSA context in which the keypair should be stored |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 188 | * \param gid Group (elliptic curve) to use. One of the various |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 189 | * POLARSSL_ECP_DP_XXX macros depending on configuration. |
| 190 | * \param f_rng RNG function |
| 191 | * \param p_rng RNG parameter |
| 192 | * |
| 193 | * \return 0 on success, or a POLARSSL_ERR_ECP code. |
| 194 | */ |
| 195 | int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, |
| 196 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 197 | |
| 198 | /** |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 199 | * \brief Set an ECDSA context from an EC key pair |
| 200 | * |
| 201 | * \param ctx ECDSA context to set |
| 202 | * \param key EC key to use |
| 203 | * |
| 204 | * \return 0 on success, or a POLARSSL_ERR_ECP code. |
| 205 | */ |
| 206 | int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key ); |
| 207 | |
| 208 | /** |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 209 | * \brief Initialize context |
| 210 | * |
| 211 | * \param ctx Context to initialize |
| 212 | */ |
| 213 | void ecdsa_init( ecdsa_context *ctx ); |
| 214 | |
| 215 | /** |
| 216 | * \brief Free context |
| 217 | * |
| 218 | * \param ctx Context to free |
| 219 | */ |
| 220 | void ecdsa_free( ecdsa_context *ctx ); |
| 221 | |
| 222 | /** |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 223 | * \brief Checkup routine |
| 224 | * |
| 225 | * \return 0 if successful, or 1 if the test failed |
| 226 | */ |
| 227 | int ecdsa_self_test( int verbose ); |
| 228 | |
| 229 | #ifdef __cplusplus |
| 230 | } |
| 231 | #endif |
| 232 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 233 | #endif /* ecdsa.h */ |