blob: 2a3b2150679c0adaa6b38abeb77f9f0be74d13d7 [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
55 */
Manuel Pégourié-Gonnard8fce9372015-03-31 13:06:41 +020056typedef ecp_keypair ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020057
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010058#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010063 * \brief Compute ECDSA signature of a previously hashed message
64 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020065 * \note The deterministic version is usually prefered.
66 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010067 * \param grp ECP group
68 * \param r First output integer
69 * \param s Second output integer
70 * \param d Private signing key
71 * \param buf Message hash
72 * \param blen Length of buf
73 * \param f_rng RNG function
74 * \param p_rng RNG parameter
75 *
76 * \return 0 if successful,
77 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
78 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020079int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010080 const mpi *d, const unsigned char *buf, size_t blen,
81 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
82
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010083#if defined(POLARSSL_ECDSA_DETERMINISTIC)
84/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020085 * \brief Compute ECDSA signature of a previously hashed message,
86 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010087 *
88 * \param grp ECP group
89 * \param r First output integer
90 * \param s Second output integer
91 * \param d Private signing key
92 * \param buf Message hash
93 * \param blen Length of buf
94 * \param md_alg MD algorithm used to hash the message
95 *
96 * \return 0 if successful,
97 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
98 */
99int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
100 const mpi *d, const unsigned char *buf, size_t blen,
101 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200102#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100103
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100104/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100105 * \brief Verify ECDSA signature of a previously hashed message
106 *
107 * \param grp ECP group
108 * \param buf Message hash
109 * \param blen Length of buf
110 * \param Q Public key to use for verification
111 * \param r First integer of the signature
112 * \param s Second integer of the signature
113 *
114 * \return 0 if successful,
115 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
116 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
117 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200118int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100119 const unsigned char *buf, size_t blen,
120 const ecp_point *Q, const mpi *r, const mpi *s);
121
122/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200123 * \brief Compute ECDSA signature and write it to buffer,
124 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200125 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200126 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200127 * \note The deterministice version (RFC 6979) is used if
128 * POLARSSL_ECDSA_DETERMINISTIC is defined.
129 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200130 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200131 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200132 * \param hash Message hash
133 * \param hlen Length of hash
134 * \param sig Buffer that will hold the signature
135 * \param slen Length of the signature written
136 * \param f_rng RNG function
137 * \param p_rng RNG parameter
138 *
139 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200140 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +0200141 * curve is used). POLARSSL_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200142 *
143 * \return 0 if successful,
144 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
145 * POLARSSL_ERR_ASN1 error code
146 */
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200147int ecdsa_write_signature( ecdsa_context *ctx, md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200148 const unsigned char *hash, size_t hlen,
149 unsigned char *sig, size_t *slen,
150 int (*f_rng)(void *, unsigned char *, size_t),
151 void *p_rng );
152
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100153#if defined(POLARSSL_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200154#if ! defined(POLARSSL_DEPRECATED_REMOVED)
155#if defined(POLARSSL_DEPRECATED_WARNING)
156#define DEPRECATED __attribute__((deprecated))
157#else
158#define DEPRECATED
159#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100160/**
161 * \brief Compute ECDSA signature and write it to buffer,
162 * serialized as defined in RFC 4492 page 20.
163 * Deterministic version, RFC 6979.
164 * (Not thread-safe to use same context in multiple threads)
165 *
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200166 * \deprecated Superseded by ecdsa_write_signature() in 2.0.0
167 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100168 * \param ctx ECDSA context
169 * \param hash Message hash
170 * \param hlen Length of hash
171 * \param sig Buffer that will hold the signature
172 * \param slen Length of the signature written
173 * \param md_alg MD algorithm used to hash the message
174 *
175 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200176 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
177 * curve is used). POLARSSL_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100178 *
179 * \return 0 if successful,
180 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
181 * POLARSSL_ERR_ASN1 error code
182 */
183int ecdsa_write_signature_det( ecdsa_context *ctx,
184 const unsigned char *hash, size_t hlen,
185 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200186 md_type_t md_alg ) DEPRECATED;
187#undef DEPRECATED
188#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker9af723c2014-05-01 13:03:14 +0200189#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100190
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200191/**
192 * \brief Read and verify an ECDSA signature
193 *
194 * \param ctx ECDSA context
195 * \param hash Message hash
196 * \param hlen Size of hash
197 * \param sig Signature to read and verify
198 * \param slen Size of sig
199 *
200 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200201 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
202 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
203 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200204 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
205 */
206int ecdsa_read_signature( ecdsa_context *ctx,
207 const unsigned char *hash, size_t hlen,
208 const unsigned char *sig, size_t slen );
209
210/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200211 * \brief Generate an ECDSA keypair on the given curve
212 *
213 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200214 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200215 * POLARSSL_ECP_DP_XXX macros depending on configuration.
216 * \param f_rng RNG function
217 * \param p_rng RNG parameter
218 *
219 * \return 0 on success, or a POLARSSL_ERR_ECP code.
220 */
221int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
222 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
223
224/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200225 * \brief Set an ECDSA context from an EC key pair
226 *
227 * \param ctx ECDSA context to set
228 * \param key EC key to use
229 *
230 * \return 0 on success, or a POLARSSL_ERR_ECP code.
231 */
232int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
233
234/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200235 * \brief Initialize context
236 *
237 * \param ctx Context to initialize
238 */
239void ecdsa_init( ecdsa_context *ctx );
240
241/**
242 * \brief Free context
243 *
244 * \param ctx Context to free
245 */
246void ecdsa_free( ecdsa_context *ctx );
247
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100248#ifdef __cplusplus
249}
250#endif
251
Paul Bakker9af723c2014-05-01 13:03:14 +0200252#endif /* ecdsa.h */