blob: e516ff6f7fc34b7416ed780f60643154b71579a6 [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é-Gonnard63e93192015-03-31 11:15:48 +020033/*
34 * RFC 4492 page 20:
35 *
36 * Ecdsa-Sig-Value ::= SEQUENCE {
37 * r INTEGER,
38 * s INTEGER
39 * }
40 *
41 * Size is at most
42 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
43 * twice that + 1 (tag) + 2 (len) for the sequence
44 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
45 * and less than 124 (total len <= 255) for the sequence)
46 */
47#if POLARSSL_ECP_MAX_BYTES > 124
48#error "POLARSSL_ECP_MAX_BYTES bigger than expected, please fix POLARSSL_ECDSA_MAX_LEN"
49#endif
50#define POLARSSL_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) )
51
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020052/**
53 * \brief ECDSA context structure
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020054 *
55 * \note Purposefully begins with the same members as struct ecp_keypair.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020056 */
57typedef struct
58{
Paul Bakker237a8472014-06-25 14:45:24 +020059 ecp_group grp; /*!< elliptic curve used */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020060 mpi d; /*!< secret signature key */
61 ecp_point Q; /*!< public signature key */
62 mpi r; /*!< first integer from signature */
63 mpi s; /*!< second integer from signature */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020064}
65ecdsa_context;
66
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010067#ifdef __cplusplus
68extern "C" {
69#endif
70
71/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010072 * \brief Compute ECDSA signature of a previously hashed message
73 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020074 * \note The deterministic version is usually prefered.
75 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010076 * \param grp ECP group
77 * \param r First output integer
78 * \param s Second output integer
79 * \param d Private signing key
80 * \param buf Message hash
81 * \param blen Length of buf
82 * \param f_rng RNG function
83 * \param p_rng RNG parameter
84 *
85 * \return 0 if successful,
86 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
87 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020088int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010089 const mpi *d, const unsigned char *buf, size_t blen,
90 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
91
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010092#if defined(POLARSSL_ECDSA_DETERMINISTIC)
93/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020094 * \brief Compute ECDSA signature of a previously hashed message,
95 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010096 *
97 * \param grp ECP group
98 * \param r First output integer
99 * \param s Second output integer
100 * \param d Private signing key
101 * \param buf Message hash
102 * \param blen Length of buf
103 * \param md_alg MD algorithm used to hash the message
104 *
105 * \return 0 if successful,
106 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
107 */
108int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
109 const mpi *d, const unsigned char *buf, size_t blen,
110 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200111#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100112
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100113/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100114 * \brief Verify ECDSA signature of a previously hashed message
115 *
116 * \param grp ECP group
117 * \param buf Message hash
118 * \param blen Length of buf
119 * \param Q Public key to use for verification
120 * \param r First integer of the signature
121 * \param s Second integer of the signature
122 *
123 * \return 0 if successful,
124 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
125 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
126 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200127int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100128 const unsigned char *buf, size_t blen,
129 const ecp_point *Q, const mpi *r, const mpi *s);
130
131/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200132 * \brief Compute ECDSA signature and write it to buffer,
133 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200134 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200135 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200136 * \note The deterministice version (RFC 6979) is used if
137 * POLARSSL_ECDSA_DETERMINISTIC is defined.
138 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200139 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200140 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200141 * \param hash Message hash
142 * \param hlen Length of hash
143 * \param sig Buffer that will hold the signature
144 * \param slen Length of the signature written
145 * \param f_rng RNG function
146 * \param p_rng RNG parameter
147 *
148 * \note The "sig" buffer must be at least as large as twice the
149 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +0200150 * curve is used). POLARSSL_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200151 *
152 * \return 0 if successful,
153 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
154 * POLARSSL_ERR_ASN1 error code
155 */
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200156int ecdsa_write_signature( ecdsa_context *ctx, md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200157 const unsigned char *hash, size_t hlen,
158 unsigned char *sig, size_t *slen,
159 int (*f_rng)(void *, unsigned char *, size_t),
160 void *p_rng );
161
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100162#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200163#if ! defined(POLARSSL_DEPRECATED_REMOVED)
164#if defined(POLARSSL_DEPRECATED_WARNING)
165#define DEPRECATED __attribute__((deprecated))
166#else
167#define DEPRECATED
168#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100169/**
170 * \brief Compute ECDSA signature and write it to buffer,
171 * serialized as defined in RFC 4492 page 20.
172 * Deterministic version, RFC 6979.
173 * (Not thread-safe to use same context in multiple threads)
174 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200175 * \deprecated Superseded by ecdsa_write_signature() in 2.0.0
176 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100177 * \param ctx ECDSA context
178 * \param hash Message hash
179 * \param hlen Length of hash
180 * \param sig Buffer that will hold the signature
181 * \param slen Length of the signature written
182 * \param md_alg MD algorithm used to hash the message
183 *
184 * \note The "sig" buffer must be at least as large as twice the
185 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
186 * curve is used).
187 *
188 * \return 0 if successful,
189 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
190 * POLARSSL_ERR_ASN1 error code
191 */
192int ecdsa_write_signature_det( ecdsa_context *ctx,
193 const unsigned char *hash, size_t hlen,
194 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200195 md_type_t md_alg ) DEPRECATED;
196#undef DEPRECATED
197#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker9af723c2014-05-01 13:03:14 +0200198#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100199
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200200/**
201 * \brief Read and verify an ECDSA signature
202 *
203 * \param ctx ECDSA context
204 * \param hash Message hash
205 * \param hlen Size of hash
206 * \param sig Signature to read and verify
207 * \param slen Size of sig
208 *
209 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200210 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
211 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
212 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200213 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
214 */
215int ecdsa_read_signature( ecdsa_context *ctx,
216 const unsigned char *hash, size_t hlen,
217 const unsigned char *sig, size_t slen );
218
219/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200220 * \brief Generate an ECDSA keypair on the given curve
221 *
222 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200223 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200224 * POLARSSL_ECP_DP_XXX macros depending on configuration.
225 * \param f_rng RNG function
226 * \param p_rng RNG parameter
227 *
228 * \return 0 on success, or a POLARSSL_ERR_ECP code.
229 */
230int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
231 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
232
233/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200234 * \brief Set an ECDSA context from an EC key pair
235 *
236 * \param ctx ECDSA context to set
237 * \param key EC key to use
238 *
239 * \return 0 on success, or a POLARSSL_ERR_ECP code.
240 */
241int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
242
243/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200244 * \brief Initialize context
245 *
246 * \param ctx Context to initialize
247 */
248void ecdsa_init( ecdsa_context *ctx );
249
250/**
251 * \brief Free context
252 *
253 * \param ctx Context to free
254 */
255void ecdsa_free( ecdsa_context *ctx );
256
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100257#ifdef __cplusplus
258}
259#endif
260
Paul Bakker9af723c2014-05-01 13:03:14 +0200261#endif /* ecdsa.h */