blob: a5a535a42398703a3f6045672414095fd9b6a125 [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
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020050/** Maximum size of an ECDSA signature in bytes */
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020051#define POLARSSL_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) )
52
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020053/**
54 * \brief ECDSA context structure
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020055 *
56 * \note Purposefully begins with the same members as struct ecp_keypair.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020057 */
58typedef struct
59{
Paul Bakker237a8472014-06-25 14:45:24 +020060 ecp_group grp; /*!< elliptic curve used */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020061 mpi d; /*!< secret signature key */
62 ecp_point Q; /*!< public signature key */
63 mpi r; /*!< first integer from signature */
64 mpi s; /*!< second integer from signature */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020065}
66ecdsa_context;
67
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010068#ifdef __cplusplus
69extern "C" {
70#endif
71
72/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010073 * \brief Compute ECDSA signature of a previously hashed message
74 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020075 * \note The deterministic version is usually prefered.
76 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010077 * \param grp ECP group
78 * \param r First output integer
79 * \param s Second output integer
80 * \param d Private signing key
81 * \param buf Message hash
82 * \param blen Length of buf
83 * \param f_rng RNG function
84 * \param p_rng RNG parameter
85 *
86 * \return 0 if successful,
87 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
88 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020089int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010090 const mpi *d, const unsigned char *buf, size_t blen,
91 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
92
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010093#if defined(POLARSSL_ECDSA_DETERMINISTIC)
94/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020095 * \brief Compute ECDSA signature of a previously hashed message,
96 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010097 *
98 * \param grp ECP group
99 * \param r First output integer
100 * \param s Second output integer
101 * \param d Private signing key
102 * \param buf Message hash
103 * \param blen Length of buf
104 * \param md_alg MD algorithm used to hash the message
105 *
106 * \return 0 if successful,
107 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
108 */
109int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
110 const mpi *d, const unsigned char *buf, size_t blen,
111 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200112#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100113
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100114/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100115 * \brief Verify ECDSA signature of a previously hashed message
116 *
117 * \param grp ECP group
118 * \param buf Message hash
119 * \param blen Length of buf
120 * \param Q Public key to use for verification
121 * \param r First integer of the signature
122 * \param s Second integer of the signature
123 *
124 * \return 0 if successful,
125 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
126 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
127 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200128int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100129 const unsigned char *buf, size_t blen,
130 const ecp_point *Q, const mpi *r, const mpi *s);
131
132/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200133 * \brief Compute ECDSA signature and write it to buffer,
134 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200135 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200136 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200137 * \note The deterministice version (RFC 6979) is used if
138 * POLARSSL_ECDSA_DETERMINISTIC is defined.
139 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200140 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200141 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200142 * \param hash Message hash
143 * \param hlen Length of hash
144 * \param sig Buffer that will hold the signature
145 * \param slen Length of the signature written
146 * \param f_rng RNG function
147 * \param p_rng RNG parameter
148 *
149 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200150 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +0200151 * curve is used). POLARSSL_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200152 *
153 * \return 0 if successful,
154 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
155 * POLARSSL_ERR_ASN1 error code
156 */
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200157int ecdsa_write_signature( ecdsa_context *ctx, md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200158 const unsigned char *hash, size_t hlen,
159 unsigned char *sig, size_t *slen,
160 int (*f_rng)(void *, unsigned char *, size_t),
161 void *p_rng );
162
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100163#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200164#if ! defined(POLARSSL_DEPRECATED_REMOVED)
165#if defined(POLARSSL_DEPRECATED_WARNING)
166#define DEPRECATED __attribute__((deprecated))
167#else
168#define DEPRECATED
169#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100170/**
171 * \brief Compute ECDSA signature and write it to buffer,
172 * serialized as defined in RFC 4492 page 20.
173 * Deterministic version, RFC 6979.
174 * (Not thread-safe to use same context in multiple threads)
175 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200176 * \deprecated Superseded by ecdsa_write_signature() in 2.0.0
177 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100178 * \param ctx ECDSA context
179 * \param hash Message hash
180 * \param hlen Length of hash
181 * \param sig Buffer that will hold the signature
182 * \param slen Length of the signature written
183 * \param md_alg MD algorithm used to hash the message
184 *
185 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200186 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
187 * curve is used). POLARSSL_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100188 *
189 * \return 0 if successful,
190 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
191 * POLARSSL_ERR_ASN1 error code
192 */
193int ecdsa_write_signature_det( ecdsa_context *ctx,
194 const unsigned char *hash, size_t hlen,
195 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200196 md_type_t md_alg ) DEPRECATED;
197#undef DEPRECATED
198#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker9af723c2014-05-01 13:03:14 +0200199#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100200
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200201/**
202 * \brief Read and verify an ECDSA signature
203 *
204 * \param ctx ECDSA context
205 * \param hash Message hash
206 * \param hlen Size of hash
207 * \param sig Signature to read and verify
208 * \param slen Size of sig
209 *
210 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200211 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
212 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
213 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200214 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
215 */
216int ecdsa_read_signature( ecdsa_context *ctx,
217 const unsigned char *hash, size_t hlen,
218 const unsigned char *sig, size_t slen );
219
220/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200221 * \brief Generate an ECDSA keypair on the given curve
222 *
223 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200224 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200225 * POLARSSL_ECP_DP_XXX macros depending on configuration.
226 * \param f_rng RNG function
227 * \param p_rng RNG parameter
228 *
229 * \return 0 on success, or a POLARSSL_ERR_ECP code.
230 */
231int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
232 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
233
234/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200235 * \brief Set an ECDSA context from an EC key pair
236 *
237 * \param ctx ECDSA context to set
238 * \param key EC key to use
239 *
240 * \return 0 on success, or a POLARSSL_ERR_ECP code.
241 */
242int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
243
244/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200245 * \brief Initialize context
246 *
247 * \param ctx Context to initialize
248 */
249void ecdsa_init( ecdsa_context *ctx );
250
251/**
252 * \brief Free context
253 *
254 * \param ctx Context to free
255 */
256void ecdsa_free( ecdsa_context *ctx );
257
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100258#ifdef __cplusplus
259}
260#endif
261
Paul Bakker9af723c2014-05-01 13:03:14 +0200262#endif /* ecdsa.h */