blob: b0a54831e82b44d1f44ece6edabf6da8164b2527 [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 *
136 * \param ctx ECDSA context
137 * \param hash Message hash
138 * \param hlen Length of hash
139 * \param sig Buffer that will hold the signature
140 * \param slen Length of the signature written
141 * \param f_rng RNG function
142 * \param p_rng RNG parameter
143 *
144 * \note The "sig" buffer must be at least as large as twice the
145 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +0200146 * curve is used). POLARSSL_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200147 *
148 * \return 0 if successful,
149 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
150 * POLARSSL_ERR_ASN1 error code
151 */
152int ecdsa_write_signature( ecdsa_context *ctx,
153 const unsigned char *hash, size_t hlen,
154 unsigned char *sig, size_t *slen,
155 int (*f_rng)(void *, unsigned char *, size_t),
156 void *p_rng );
157
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100158#if defined(POLARSSL_ECDSA_DETERMINISTIC)
159/**
160 * \brief Compute ECDSA signature and write it to buffer,
161 * serialized as defined in RFC 4492 page 20.
162 * Deterministic version, RFC 6979.
163 * (Not thread-safe to use same context in multiple threads)
164 *
165 * \param ctx ECDSA context
166 * \param hash Message hash
167 * \param hlen Length of hash
168 * \param sig Buffer that will hold the signature
169 * \param slen Length of the signature written
170 * \param md_alg MD algorithm used to hash the message
171 *
172 * \note The "sig" buffer must be at least as large as twice the
173 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
174 * curve is used).
175 *
176 * \return 0 if successful,
177 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
178 * POLARSSL_ERR_ASN1 error code
179 */
180int ecdsa_write_signature_det( ecdsa_context *ctx,
181 const unsigned char *hash, size_t hlen,
182 unsigned char *sig, size_t *slen,
183 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200184#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100185
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200186/**
187 * \brief Read and verify an ECDSA signature
188 *
189 * \param ctx ECDSA context
190 * \param hash Message hash
191 * \param hlen Size of hash
192 * \param sig Signature to read and verify
193 * \param slen Size of sig
194 *
195 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200196 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
197 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
198 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200199 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
200 */
201int ecdsa_read_signature( ecdsa_context *ctx,
202 const unsigned char *hash, size_t hlen,
203 const unsigned char *sig, size_t slen );
204
205/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200206 * \brief Generate an ECDSA keypair on the given curve
207 *
208 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200209 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200210 * POLARSSL_ECP_DP_XXX macros depending on configuration.
211 * \param f_rng RNG function
212 * \param p_rng RNG parameter
213 *
214 * \return 0 on success, or a POLARSSL_ERR_ECP code.
215 */
216int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
217 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
218
219/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200220 * \brief Set an ECDSA context from an EC key pair
221 *
222 * \param ctx ECDSA context to set
223 * \param key EC key to use
224 *
225 * \return 0 on success, or a POLARSSL_ERR_ECP code.
226 */
227int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
228
229/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200230 * \brief Initialize context
231 *
232 * \param ctx Context to initialize
233 */
234void ecdsa_init( ecdsa_context *ctx );
235
236/**
237 * \brief Free context
238 *
239 * \param ctx Context to free
240 */
241void ecdsa_free( ecdsa_context *ctx );
242
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100243#ifdef __cplusplus
244}
245#endif
246
Paul Bakker9af723c2014-05-01 13:03:14 +0200247#endif /* ecdsa.h */