blob: 47382e583e3c17e763672579191529242926ee35 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
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é-Gonnardbec2f452013-06-27 10:17:07 +020032/**
33 * \brief ECDSA context structure
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020034 *
35 * \note Purposefully begins with the same members as struct ecp_keypair.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020036 */
37typedef 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é-Gonnardbec2f452013-06-27 10:17:07 +020044}
45ecdsa_context;
46
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010047#ifdef __cplusplus
48extern "C" {
49#endif
50
51/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010052 * \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 */
66int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
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é-Gonnard3aeb5a72013-01-26 18:05:50 +010071 * \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 */
84int ecdsa_verify( const ecp_group *grp,
85 const unsigned char *buf, size_t blen,
86 const ecp_point *Q, const mpi *r, const mpi *s);
87
88/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +020089 * \brief Compute ECDSA signature and write it to buffer,
90 * serialized as defined in RFC 4492 page 20.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +020091 *
92 * \param ctx ECDSA context
93 * \param hash Message hash
94 * \param hlen Length of hash
95 * \param sig Buffer that will hold the signature
96 * \param slen Length of the signature written
97 * \param f_rng RNG function
98 * \param p_rng RNG parameter
99 *
100 * \note The "sig" buffer must be at least as large as twice the
101 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
102 * curve is used).
103 *
104 * \return 0 if successful,
105 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
106 * POLARSSL_ERR_ASN1 error code
107 */
108int ecdsa_write_signature( ecdsa_context *ctx,
109 const unsigned char *hash, size_t hlen,
110 unsigned char *sig, size_t *slen,
111 int (*f_rng)(void *, unsigned char *, size_t),
112 void *p_rng );
113
114/**
115 * \brief Read and verify an ECDSA signature
116 *
117 * \param ctx ECDSA context
118 * \param hash Message hash
119 * \param hlen Size of hash
120 * \param sig Signature to read and verify
121 * \param slen Size of sig
122 *
123 * \return 0 if successful,
124 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
125 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
126 */
127int ecdsa_read_signature( ecdsa_context *ctx,
128 const unsigned char *hash, size_t hlen,
129 const unsigned char *sig, size_t slen );
130
131/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200132 * \brief Generate an ECDSA keypair on the given curve
133 *
134 * \param ctx ECDSA context in which the keypair should be stored
135 * \param grp Group (elliptic curve) to use. One of the various
136 * POLARSSL_ECP_DP_XXX macros depending on configuration.
137 * \param f_rng RNG function
138 * \param p_rng RNG parameter
139 *
140 * \return 0 on success, or a POLARSSL_ERR_ECP code.
141 */
142int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
143 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
144
145/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200146 * \brief Set an ECDSA context from an EC key pair
147 *
148 * \param ctx ECDSA context to set
149 * \param key EC key to use
150 *
151 * \return 0 on success, or a POLARSSL_ERR_ECP code.
152 */
153int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
154
155/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200156 * \brief Initialize context
157 *
158 * \param ctx Context to initialize
159 */
160void ecdsa_init( ecdsa_context *ctx );
161
162/**
163 * \brief Free context
164 *
165 * \param ctx Context to free
166 */
167void ecdsa_free( ecdsa_context *ctx );
168
169/**
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100170 * \brief Checkup routine
171 *
172 * \return 0 if successful, or 1 if the test failed
173 */
174int ecdsa_self_test( int verbose );
175
176#ifdef __cplusplus
177}
178#endif
179
180#endif