blob: c5290f6a60dff0f01c68dc244ec227f94f2b01a0 [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é-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.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 *
55 * \param grp ECP group
56 * \param r First output integer
57 * \param s Second output integer
58 * \param d Private signing key
59 * \param buf Message hash
60 * \param blen Length of buf
61 * \param f_rng RNG function
62 * \param p_rng RNG parameter
63 *
64 * \return 0 if successful,
65 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
66 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020067int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010068 const mpi *d, const unsigned char *buf, size_t blen,
69 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
70
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010071#if defined(POLARSSL_ECDSA_DETERMINISTIC)
72/**
73 * \brief Compute ECDSA signature of a previously hashed message
74 * (deterministic version)
75 *
76 * \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 md_alg MD algorithm used to hash the message
83 *
84 * \return 0 if successful,
85 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
86 */
87int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
88 const mpi *d, const unsigned char *buf, size_t blen,
89 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +020090#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010091
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010092/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +010093 * \brief Verify ECDSA signature of a previously hashed message
94 *
95 * \param grp ECP group
96 * \param buf Message hash
97 * \param blen Length of buf
98 * \param Q Public key to use for verification
99 * \param r First integer of the signature
100 * \param s Second integer of the signature
101 *
102 * \return 0 if successful,
103 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
104 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
105 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200106int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100107 const unsigned char *buf, size_t blen,
108 const ecp_point *Q, const mpi *r, const mpi *s);
109
110/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200111 * \brief Compute ECDSA signature and write it to buffer,
112 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200113 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200114 *
115 * \param ctx ECDSA context
116 * \param hash Message hash
117 * \param hlen Length of hash
118 * \param sig Buffer that will hold the signature
119 * \param slen Length of the signature written
120 * \param f_rng RNG function
121 * \param p_rng RNG parameter
122 *
123 * \note The "sig" buffer must be at least as large as twice the
124 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
125 * curve is used).
126 *
127 * \return 0 if successful,
128 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
129 * POLARSSL_ERR_ASN1 error code
130 */
131int ecdsa_write_signature( ecdsa_context *ctx,
132 const unsigned char *hash, size_t hlen,
133 unsigned char *sig, size_t *slen,
134 int (*f_rng)(void *, unsigned char *, size_t),
135 void *p_rng );
136
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100137#if defined(POLARSSL_ECDSA_DETERMINISTIC)
138/**
139 * \brief Compute ECDSA signature and write it to buffer,
140 * serialized as defined in RFC 4492 page 20.
141 * Deterministic version, RFC 6979.
142 * (Not thread-safe to use same context in multiple threads)
143 *
144 * \param ctx ECDSA context
145 * \param hash Message hash
146 * \param hlen Length of hash
147 * \param sig Buffer that will hold the signature
148 * \param slen Length of the signature written
149 * \param md_alg MD algorithm used to hash the message
150 *
151 * \note The "sig" buffer must be at least as large as twice the
152 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
153 * curve is used).
154 *
155 * \return 0 if successful,
156 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
157 * POLARSSL_ERR_ASN1 error code
158 */
159int ecdsa_write_signature_det( ecdsa_context *ctx,
160 const unsigned char *hash, size_t hlen,
161 unsigned char *sig, size_t *slen,
162 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200163#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100164
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200165/**
166 * \brief Read and verify an ECDSA signature
167 *
168 * \param ctx ECDSA context
169 * \param hash Message hash
170 * \param hlen Size of hash
171 * \param sig Signature to read and verify
172 * \param slen Size of sig
173 *
174 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200175 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
176 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
177 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200178 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
179 */
180int ecdsa_read_signature( ecdsa_context *ctx,
181 const unsigned char *hash, size_t hlen,
182 const unsigned char *sig, size_t slen );
183
184/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200185 * \brief Generate an ECDSA keypair on the given curve
186 *
187 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200188 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200189 * POLARSSL_ECP_DP_XXX macros depending on configuration.
190 * \param f_rng RNG function
191 * \param p_rng RNG parameter
192 *
193 * \return 0 on success, or a POLARSSL_ERR_ECP code.
194 */
195int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
196 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
197
198/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200199 * \brief Set an ECDSA context from an EC key pair
200 *
201 * \param ctx ECDSA context to set
202 * \param key EC key to use
203 *
204 * \return 0 on success, or a POLARSSL_ERR_ECP code.
205 */
206int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
207
208/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200209 * \brief Initialize context
210 *
211 * \param ctx Context to initialize
212 */
213void ecdsa_init( ecdsa_context *ctx );
214
215/**
216 * \brief Free context
217 *
218 * \param ctx Context to free
219 */
220void ecdsa_free( ecdsa_context *ctx );
221
222/**
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100223 * \brief Checkup routine
224 *
225 * \return 0 if successful, or 1 if the test failed
226 */
227int ecdsa_self_test( int verbose );
228
229#ifdef __cplusplus
230}
231#endif
232
Paul Bakker9af723c2014-05-01 13:03:14 +0200233#endif /* ecdsa.h */