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 | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 32 | /** |
| 33 | * \brief ECDSA context structure |
Manuel Pégourié-Gonnard | 211a64c | 2013-08-09 15:04:26 +0200 | [diff] [blame] | 34 | * |
| 35 | * \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] | 36 | */ |
| 37 | typedef struct |
| 38 | { |
| 39 | ecp_group grp; /*!< ellipitic curve used */ |
| 40 | mpi d; /*!< secret signature key */ |
| 41 | ecp_point Q; /*!< public signature key */ |
| 42 | mpi r; /*!< first integer from signature */ |
| 43 | mpi s; /*!< second integer from signature */ |
Manuel Pégourié-Gonnard | bec2f45 | 2013-06-27 10:17:07 +0200 | [diff] [blame] | 44 | } |
| 45 | ecdsa_context; |
| 46 | |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | /** |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 52 | * \brief Compute ECDSA signature of a previously hashed message |
| 53 | * |
| 54 | * \param grp ECP group |
| 55 | * \param r First output integer |
| 56 | * \param s Second output integer |
| 57 | * \param d Private signing key |
| 58 | * \param buf Message hash |
| 59 | * \param blen Length of buf |
| 60 | * \param f_rng RNG function |
| 61 | * \param p_rng RNG parameter |
| 62 | * |
| 63 | * \return 0 if successful, |
| 64 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 65 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 66 | int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s, |
Manuel Pégourié-Gonnard | b309ab2 | 2013-01-26 17:24:59 +0100 | [diff] [blame] | 67 | const mpi *d, const unsigned char *buf, size_t blen, |
| 68 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 69 | |
| 70 | /** |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 71 | * \brief Verify ECDSA signature of a previously hashed message |
| 72 | * |
| 73 | * \param grp ECP group |
| 74 | * \param buf Message hash |
| 75 | * \param blen Length of buf |
| 76 | * \param Q Public key to use for verification |
| 77 | * \param r First integer of the signature |
| 78 | * \param s Second integer of the signature |
| 79 | * |
| 80 | * \return 0 if successful, |
| 81 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid |
| 82 | * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code |
| 83 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 84 | int ecdsa_verify( ecp_group *grp, |
Manuel Pégourié-Gonnard | 3aeb5a7 | 2013-01-26 18:05:50 +0100 | [diff] [blame] | 85 | const unsigned char *buf, size_t blen, |
| 86 | const ecp_point *Q, const mpi *r, const mpi *s); |
| 87 | |
| 88 | /** |
Manuel Pégourié-Gonnard | aa43161 | 2013-08-09 17:10:27 +0200 | [diff] [blame] | 89 | * \brief Compute ECDSA signature and write it to buffer, |
| 90 | * serialized as defined in RFC 4492 page 20. |
Paul Bakker | 6838bd1 | 2013-09-30 13:56:38 +0200 | [diff] [blame] | 91 | * (Not thread-safe to use same context in multiple threads) |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 92 | * |
| 93 | * \param ctx ECDSA context |
| 94 | * \param hash Message hash |
| 95 | * \param hlen Length of hash |
| 96 | * \param sig Buffer that will hold the signature |
| 97 | * \param slen Length of the signature written |
| 98 | * \param f_rng RNG function |
| 99 | * \param p_rng RNG parameter |
| 100 | * |
| 101 | * \note The "sig" buffer must be at least as large as twice the |
| 102 | * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit |
| 103 | * curve is used). |
| 104 | * |
| 105 | * \return 0 if successful, |
| 106 | * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or |
| 107 | * POLARSSL_ERR_ASN1 error code |
| 108 | */ |
| 109 | int ecdsa_write_signature( ecdsa_context *ctx, |
| 110 | const unsigned char *hash, size_t hlen, |
| 111 | unsigned char *sig, size_t *slen, |
| 112 | int (*f_rng)(void *, unsigned char *, size_t), |
| 113 | void *p_rng ); |
| 114 | |
| 115 | /** |
| 116 | * \brief Read and verify an ECDSA signature |
| 117 | * |
| 118 | * \param ctx ECDSA context |
| 119 | * \param hash Message hash |
| 120 | * \param hlen Size of hash |
| 121 | * \param sig Signature to read and verify |
| 122 | * \param slen Size of sig |
| 123 | * |
| 124 | * \return 0 if successful, |
| 125 | * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid |
| 126 | * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code |
| 127 | */ |
| 128 | int ecdsa_read_signature( ecdsa_context *ctx, |
| 129 | const unsigned char *hash, size_t hlen, |
| 130 | const unsigned char *sig, size_t slen ); |
| 131 | |
| 132 | /** |
Manuel Pégourié-Gonnard | 8eebd01 | 2013-08-09 16:21:34 +0200 | [diff] [blame] | 133 | * \brief Generate an ECDSA keypair on the given curve |
| 134 | * |
| 135 | * \param ctx ECDSA context in which the keypair should be stored |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 136 | * \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] | 137 | * POLARSSL_ECP_DP_XXX macros depending on configuration. |
| 138 | * \param f_rng RNG function |
| 139 | * \param p_rng RNG parameter |
| 140 | * |
| 141 | * \return 0 on success, or a POLARSSL_ERR_ECP code. |
| 142 | */ |
| 143 | int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid, |
| 144 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 145 | |
| 146 | /** |
Manuel Pégourié-Gonnard | f499993 | 2013-08-12 17:02:59 +0200 | [diff] [blame] | 147 | * \brief Set an ECDSA context from an EC key pair |
| 148 | * |
| 149 | * \param ctx ECDSA context to set |
| 150 | * \param key EC key to use |
| 151 | * |
| 152 | * \return 0 on success, or a POLARSSL_ERR_ECP code. |
| 153 | */ |
| 154 | int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key ); |
| 155 | |
| 156 | /** |
Manuel Pégourié-Gonnard | 7c8934e | 2013-06-27 12:54:02 +0200 | [diff] [blame] | 157 | * \brief Initialize context |
| 158 | * |
| 159 | * \param ctx Context to initialize |
| 160 | */ |
| 161 | void ecdsa_init( ecdsa_context *ctx ); |
| 162 | |
| 163 | /** |
| 164 | * \brief Free context |
| 165 | * |
| 166 | * \param ctx Context to free |
| 167 | */ |
| 168 | void ecdsa_free( ecdsa_context *ctx ); |
| 169 | |
| 170 | /** |
Manuel Pégourié-Gonnard | 2aea141 | 2013-01-26 16:33:44 +0100 | [diff] [blame] | 171 | * \brief Checkup routine |
| 172 | * |
| 173 | * \return 0 if successful, or 1 if the test failed |
| 174 | */ |
| 175 | int ecdsa_self_test( int verbose ); |
| 176 | |
| 177 | #ifdef __cplusplus |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #endif |