blob: 47b644ef6cade83611ce7102e100c9998604d35e [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é-Gonnard887aa5b2014-04-04 13:57:20 +020028#include "md.h"
Janos Follath5d96a3d2017-03-10 11:31:41 +000029/*
30 * RFC 4492 page 20:
31 *
32 * Ecdsa-Sig-Value ::= SEQUENCE {
33 * r INTEGER,
34 * s INTEGER
35 * }
36 *
37 * Size is at most
38 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
39 * twice that + 1 (tag) + 2 (len) for the sequence
40 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
41 * and less than 124 (total len <= 255) for the sequence)
42 *
43 */
44/** Maximum size of an ECDSA signature in bytes */
45#define POLARSSL_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + POLARSSL_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010046
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020047/**
48 * \brief ECDSA context structure
Manuel Pégourié-Gonnard211a64c2013-08-09 15:04:26 +020049 *
50 * \note Purposefully begins with the same members as struct ecp_keypair.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020051 */
52typedef struct
53{
Paul Bakker237a8472014-06-25 14:45:24 +020054 ecp_group grp; /*!< elliptic curve used */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020055 mpi d; /*!< secret signature key */
56 ecp_point Q; /*!< public signature key */
57 mpi r; /*!< first integer from signature */
58 mpi s; /*!< second integer from signature */
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020059}
60ecdsa_context;
61
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010062#ifdef __cplusplus
63extern "C" {
64#endif
65
66/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010067 * \brief Compute ECDSA signature of a previously hashed message
68 *
69 * \param grp ECP group
70 * \param r First output integer
71 * \param s Second output integer
72 * \param d Private signing key
73 * \param buf Message hash
74 * \param blen Length of buf
75 * \param f_rng RNG function
76 * \param p_rng RNG parameter
77 *
Janos Follath5d96a3d2017-03-10 11:31:41 +000078 * \note If the bitlength of the message hash is larger than the
79 * bitlength of the group order, then the hash is truncated as
80 * prescribed by SEC1 4.1.3 step 5.
81 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010082 * \return 0 if successful,
83 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
84 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020085int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010086 const mpi *d, const unsigned char *buf, size_t blen,
87 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
88
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010089#if defined(POLARSSL_ECDSA_DETERMINISTIC)
90/**
91 * \brief Compute ECDSA signature of a previously hashed message
92 * (deterministic version)
93 *
94 * \param grp ECP group
95 * \param r First output integer
96 * \param s Second output integer
97 * \param d Private signing key
98 * \param buf Message hash
99 * \param blen Length of buf
100 * \param md_alg MD algorithm used to hash the message
101 *
Janos Follath5d96a3d2017-03-10 11:31:41 +0000102 * \note If the bitlength of the message hash is larger than the
103 * bitlength of the group order, then the hash is truncated as
104 * prescribed by SEC1 4.1.3 step 5.
105 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100106 * \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 *
Janos Follath5d96a3d2017-03-10 11:31:41 +0000124 * \note If the bitlength of the message hash is larger than the
125 * bitlength of the group order, then the hash is truncated as
126 * prescribed by SEC1 4.1.4 step 3.
127 *
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100128 * \return 0 if successful,
129 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
130 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
131 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200132int ecdsa_verify( ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100133 const unsigned char *buf, size_t blen,
134 const ecp_point *Q, const mpi *r, const mpi *s);
135
136/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200137 * \brief Compute ECDSA signature and write it to buffer,
138 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200139 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200140 *
141 * \param ctx ECDSA context
142 * \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
150 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
151 * curve is used).
152 *
Janos Follath5d96a3d2017-03-10 11:31:41 +0000153 * \note If the bitlength of the message hash is larger than the
154 * bitlength of the group order, then the hash is truncated as
155 * prescribed by SEC1 4.1.3 step 5.
156 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200157 * \return 0 if successful,
158 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
159 * POLARSSL_ERR_ASN1 error code
160 */
161int ecdsa_write_signature( ecdsa_context *ctx,
162 const unsigned char *hash, size_t hlen,
163 unsigned char *sig, size_t *slen,
164 int (*f_rng)(void *, unsigned char *, size_t),
165 void *p_rng );
166
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100167#if defined(POLARSSL_ECDSA_DETERMINISTIC)
168/**
169 * \brief Compute ECDSA signature and write it to buffer,
170 * serialized as defined in RFC 4492 page 20.
171 * Deterministic version, RFC 6979.
172 * (Not thread-safe to use same context in multiple threads)
173 *
174 * \param ctx ECDSA context
175 * \param hash Message hash
176 * \param hlen Length of hash
177 * \param sig Buffer that will hold the signature
178 * \param slen Length of the signature written
179 * \param md_alg MD algorithm used to hash the message
180 *
181 * \note The "sig" buffer must be at least as large as twice the
182 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
183 * curve is used).
184 *
185 * \return 0 if successful,
186 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
187 * POLARSSL_ERR_ASN1 error code
188 */
189int ecdsa_write_signature_det( ecdsa_context *ctx,
190 const unsigned char *hash, size_t hlen,
191 unsigned char *sig, size_t *slen,
192 md_type_t md_alg );
Paul Bakker9af723c2014-05-01 13:03:14 +0200193#endif /* POLARSSL_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100194
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200195/**
196 * \brief Read and verify an ECDSA signature
197 *
198 * \param ctx ECDSA context
199 * \param hash Message hash
200 * \param hlen Size of hash
201 * \param sig Signature to read and verify
202 * \param slen Size of sig
203 *
Janos Follath5d96a3d2017-03-10 11:31:41 +0000204 * \note If the bitlength of the message hash is larger than the
205 * bitlength of the group order, then the hash is truncated as
206 * prescribed by SEC1 4.1.4 step 3.
207 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200208 * \return 0 if successful,
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200209 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
210 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
211 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200212 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
213 */
214int ecdsa_read_signature( ecdsa_context *ctx,
215 const unsigned char *hash, size_t hlen,
216 const unsigned char *sig, size_t slen );
217
218/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200219 * \brief Generate an ECDSA keypair on the given curve
220 *
221 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200222 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200223 * POLARSSL_ECP_DP_XXX macros depending on configuration.
224 * \param f_rng RNG function
225 * \param p_rng RNG parameter
226 *
227 * \return 0 on success, or a POLARSSL_ERR_ECP code.
228 */
229int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
230 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
231
232/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200233 * \brief Set an ECDSA context from an EC key pair
234 *
235 * \param ctx ECDSA context to set
236 * \param key EC key to use
237 *
238 * \return 0 on success, or a POLARSSL_ERR_ECP code.
239 */
240int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
241
242/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200243 * \brief Initialize context
244 *
245 * \param ctx Context to initialize
246 */
247void ecdsa_init( ecdsa_context *ctx );
248
249/**
250 * \brief Free context
251 *
252 * \param ctx Context to free
253 */
254void ecdsa_free( ecdsa_context *ctx );
255
256/**
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100257 * \brief Checkup routine
258 *
259 * \return 0 if successful, or 1 if the test failed
260 */
261int ecdsa_self_test( int verbose );
262
263#ifdef __cplusplus
264}
265#endif
266
Paul Bakker9af723c2014-05-01 13:03:14 +0200267#endif /* ecdsa.h */