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