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