blob: 57f9b4849c09ff7c7a226a7e81c10fc632c7ff18 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
4 * \brief Elliptic curve DSA
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01009 *
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010010 * 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é-Gonnardbdc96762013-10-03 11:50:39 +020027#include "ecp.h"
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010028
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010029#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020030#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010031#endif
32
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020033/**
34 * \brief ECDSA context structure
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020035 *
36 * \note Purposefully begins with the same members as struct ecp_keypair.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020037 */
38typedef struct
39{
Paul Bakker237a8472014-06-25 14:45:24 +020040 ecp_group grp; /*!< elliptic curve used */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020041 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é-Gonnardbec2f452013-06-27 10:17:07 +020045}
46ecdsa_context;
47
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010048#ifdef __cplusplus
49extern "C" {
50#endif
51
52/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010053 * \brief Compute ECDSA signature of a previously hashed message
54 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020055 * \note The deterministic version is usually prefered.
56 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010057 * \param grp ECP group
58 * \param r First output integer
59 * \param s Second output integer
60 * \param d Private signing key
61 * \param buf Message hash
62 * \param blen Length of buf
63 * \param f_rng RNG function
64 * \param p_rng RNG parameter
65 *
66 * \return 0 if successful,
67 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
68 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020069int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010070 const mpi *d, const unsigned char *buf, size_t blen,
71 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
72
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010073#if defined(POLARSSL_ECDSA_DETERMINISTIC)
74/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020075 * \brief Compute ECDSA signature of a previously hashed message,
76 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010077 *
78 * \param grp ECP group
79 * \param r First output integer
80 * \param s Second output integer
81 * \param d Private signing key
82 * \param buf Message hash
83 * \param blen Length of buf
84 * \param md_alg MD algorithm used to hash the message
85 *
86 * \return 0 if successful,
87 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
88 */
89int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
90 const mpi *d, const unsigned char *buf, size_t blen,
91 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +020092#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010093
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010094/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010095 * \brief Verify ECDSA signature of a previously hashed message
96 *
97 * \param grp ECP group
98 * \param buf Message hash
99 * \param blen Length of buf
100 * \param Q Public key to use for verification
101 * \param r First integer of the signature
102 * \param s Second integer of the signature
103 *
104 * \return 0 if successful,
105 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
106 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
107 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200108int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100109 const unsigned char *buf, size_t blen,
110 const ecp_point *Q, const mpi *r, const mpi *s);
111
112/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200113 * \brief Compute ECDSA signature and write it to buffer,
114 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200115 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200116 *
117 * \param ctx ECDSA context
118 * \param hash Message hash
119 * \param hlen Length of hash
120 * \param sig Buffer that will hold the signature
121 * \param slen Length of the signature written
122 * \param f_rng RNG function
123 * \param p_rng RNG parameter
124 *
125 * \note The "sig" buffer must be at least as large as twice the
126 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
127 * curve is used).
128 *
129 * \return 0 if successful,
130 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
131 * POLARSSL_ERR_ASN1 error code
132 */
133int ecdsa_write_signature( ecdsa_context *ctx,
134 const unsigned char *hash, size_t hlen,
135 unsigned char *sig, size_t *slen,
136 int (*f_rng)(void *, unsigned char *, size_t),
137 void *p_rng );
138
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100139#if defined(POLARSSL_ECDSA_DETERMINISTIC)
140/**
141 * \brief Compute ECDSA signature and write it to buffer,
142 * serialized as defined in RFC 4492 page 20.
143 * Deterministic version, RFC 6979.
144 * (Not thread-safe to use same context in multiple threads)
145 *
146 * \param ctx ECDSA context
147 * \param hash Message hash
148 * \param hlen Length of hash
149 * \param sig Buffer that will hold the signature
150 * \param slen Length of the signature written
151 * \param md_alg MD algorithm used to hash the message
152 *
153 * \note The "sig" buffer must be at least as large as twice the
154 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
155 * curve is used).
156 *
157 * \return 0 if successful,
158 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
159 * POLARSSL_ERR_ASN1 error code
160 */
161int ecdsa_write_signature_det( ecdsa_context *ctx,
162 const unsigned char *hash, size_t hlen,
163 unsigned char *sig, size_t *slen,
164 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200165#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100166
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200167/**
168 * \brief Read and verify an ECDSA signature
169 *
170 * \param ctx ECDSA context
171 * \param hash Message hash
172 * \param hlen Size of hash
173 * \param sig Signature to read and verify
174 * \param slen Size of sig
175 *
176 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200177 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
178 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
179 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200180 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
181 */
182int ecdsa_read_signature( ecdsa_context *ctx,
183 const unsigned char *hash, size_t hlen,
184 const unsigned char *sig, size_t slen );
185
186/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200187 * \brief Generate an ECDSA keypair on the given curve
188 *
189 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200190 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200191 * POLARSSL_ECP_DP_XXX macros depending on configuration.
192 * \param f_rng RNG function
193 * \param p_rng RNG parameter
194 *
195 * \return 0 on success, or a POLARSSL_ERR_ECP code.
196 */
197int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
198 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
199
200/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200201 * \brief Set an ECDSA context from an EC key pair
202 *
203 * \param ctx ECDSA context to set
204 * \param key EC key to use
205 *
206 * \return 0 on success, or a POLARSSL_ERR_ECP code.
207 */
208int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
209
210/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200211 * \brief Initialize context
212 *
213 * \param ctx Context to initialize
214 */
215void ecdsa_init( ecdsa_context *ctx );
216
217/**
218 * \brief Free context
219 *
220 * \param ctx Context to free
221 */
222void ecdsa_free( ecdsa_context *ctx );
223
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100224#ifdef __cplusplus
225}
226#endif
227
Paul Bakker9af723c2014-05-01 13:03:14 +0200228#endif /* ecdsa.h */