blob: 908fe5cb933374c49727055353fd8dafb79e75b6 [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é-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ECDSA_H
24#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010025
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020026#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020027#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010028
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020029/*
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 */
Unknown6f21aed2018-02-07 08:02:31 -050043#if MBEDTLS_ECP_MAX_BYTES > 124
44#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
45#endif
Gilles Peskine9a8bb672017-11-02 17:09:49 +010046
47/**
48 * \brief Maximum ECDSA signature size for a given curve bit size
49 *
50 * \param bits Curve size in bits
51 * \return Maximum signature size in bytes
52 *
53 * \note This macro returns a compile-time constant if its argument
54 * is one. It may evaluate its argument multiple times; if
55 * this is a problem, call the function
56 * mbedtls_ecdsa_max_sig_len instead.
57 */
58#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
59 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
60 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
61 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
62
63/**
64 * \brief Maximum ECDSA signature size for a given curve bit size
65 *
66 * \param bits Curve size in bits
67 * \return Maximum signature size in bytes
68 *
69 * \note If you need a compile-time constant, call the macro
70 * MBEDTLS_ECDSA_MAX_SIG_LEN instead.
71 */
72static inline size_t mbedtls_ecdsa_max_sig_len( size_t bits )
73{
74 return( MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) );
75}
76
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020077/** Maximum size of an ECDSA signature in bytes */
Unknown60b25f02018-02-06 03:17:59 -050078#define MBEDTLS_ECDSA_MAX_LEN (MBEDTLS_ECDSA_MAX_SIG_LEN( \
79 8 * MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020080/**
81 * \brief ECDSA context structure
82 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020084
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010085#ifdef __cplusplus
86extern "C" {
87#endif
88
89/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010090 * \brief Compute ECDSA signature of a previously hashed message
91 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020092 * \note The deterministic version is usually prefered.
93 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010094 * \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 f_rng RNG function
101 * \param p_rng RNG parameter
102 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000103 * \note If the bitlength of the message hash is larger than the
104 * bitlength of the group order, then the hash is truncated as
105 * prescribed by SEC1 4.1.3 step 5.
106 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100107 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
111 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100112 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100115/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +0200116 * \brief Compute ECDSA signature of a previously hashed message,
117 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100118 *
119 * \param grp ECP group
120 * \param r First output integer
121 * \param s Second output integer
122 * \param d Private signing key
123 * \param buf Message hash
124 * \param blen Length of buf
125 * \param md_alg MD algorithm used to hash the message
126 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000127 * \note If the bitlength of the message hash is larger than the
128 * bitlength of the group order, then the hash is truncated as
129 * prescribed by SEC1 4.1.3 step 5.
130 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100131 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
135 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
136 mbedtls_md_type_t md_alg );
137#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100138
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100139/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100140 * \brief Verify ECDSA signature of a previously hashed message
141 *
142 * \param grp ECP group
143 * \param buf Message hash
144 * \param blen Length of buf
145 * \param Q Public key to use for verification
146 * \param r First integer of the signature
147 * \param s Second integer of the signature
148 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000149 * \note If the bitlength of the message hash is larger than the
150 * bitlength of the group order, then the hash is truncated as
151 * prescribed by SEC1 4.1.4 step 3.
152 *
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100153 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
155 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100158 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100160
161/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200162 * \brief Compute ECDSA signature and write it to buffer,
163 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200164 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200165 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000166 * \note The deterministic version (RFC 6979) is used if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 * MBEDTLS_ECDSA_DETERMINISTIC is defined.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200168 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200169 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200170 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200171 * \param hash Message hash
172 * \param hlen Length of hash
173 * \param sig Buffer that will hold the signature
174 * \param slen Length of the signature written
175 * \param f_rng RNG function
176 * \param p_rng RNG parameter
177 *
Gilles Peskine9a8bb672017-11-02 17:09:49 +0100178 * \note The \c sig buffer must be at least
179 * `MBEDTLS_ECDSA_MAX_SIG_LEN(ctx->grp.pbits)` bytes long.
180 * MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200181 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000182 * \note If the bitlength of the message hash is larger than the
183 * bitlength of the group order, then the hash is truncated as
184 * prescribed by SEC1 4.1.3 step 5.
185 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200186 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
188 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200191 const unsigned char *hash, size_t hlen,
192 unsigned char *sig, size_t *slen,
193 int (*f_rng)(void *, unsigned char *, size_t),
194 void *p_rng );
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
197#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
198#if defined(MBEDTLS_DEPRECATED_WARNING)
199#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200200#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200202#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100203/**
204 * \brief Compute ECDSA signature and write it to buffer,
205 * serialized as defined in RFC 4492 page 20.
206 * Deterministic version, RFC 6979.
207 * (Not thread-safe to use same context in multiple threads)
208 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200210 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100211 * \param ctx ECDSA context
212 * \param hash Message hash
213 * \param hlen Length of hash
214 * \param sig Buffer that will hold the signature
215 * \param slen Length of the signature written
216 * \param md_alg MD algorithm used to hash the message
217 *
Gilles Peskine9a8bb672017-11-02 17:09:49 +0100218 * \note The \c sig buffer must be at least
219 * `MBEDTLS_ECDSA_MAX_SIG_LEN(ctx->grp.pbits)` bytes long.
220 * MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100221 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000222 * \note If the bitlength of the message hash is larger than the
223 * bitlength of the group order, then the hash is truncated as
224 * prescribed by SEC1 4.1.3 step 5.
225 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100226 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
228 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100231 const unsigned char *hash, size_t hlen,
232 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
234#undef MBEDTLS_DEPRECATED
235#endif /* MBEDTLS_DEPRECATED_REMOVED */
236#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100237
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200238/**
Unknown6f21aed2018-02-07 08:02:31 -0500239 * \brief Convert a signature from numbers to ASN.1
Gilles Peskinebce41d32017-11-02 17:14:18 +0100240 *
241 * \param r First number of the signature
242 * \param s Second number of the signature
243 * \param sig Buffer that will hold the signature
244 * \param slen Length of the signature written
245 * \param ssize Size of the sig buffer
246 *
247 * \note The size of the buffer \c ssize should be at least
248 * `MBEDTLS_ECDSA_MAX_SIG_LEN(grp->pbits)` bytes long if
249 * the signature was produced from curve \c grp,
250 * otherwise this function will return an error.
Unknown6f21aed2018-02-07 08:02:31 -0500251 * The output ASN.1 SEQUENCE format is as follows:
252 * Ecdsa-Sig-Value ::= SEQUENCE {
253 * r INTEGER,
254 * s INTEGER
255 * }
Gilles Peskinebce41d32017-11-02 17:14:18 +0100256 *
257 * \return 0 if successful,
258 * or a MBEDTLS_ERR_MPI_XXX or MBEDTLS_ERR_ASN1_XXX error code
259 *
260 */
Unknowna2c40622018-02-06 03:24:02 -0500261int mbedtls_ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
Gilles Peskinebce41d32017-11-02 17:14:18 +0100262 unsigned char *sig, size_t *slen,
263 size_t ssize );
264
265/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200266 * \brief Read and verify an ECDSA signature
267 *
268 * \param ctx ECDSA context
269 * \param hash Message hash
270 * \param hlen Size of hash
271 * \param sig Signature to read and verify
272 * \param slen Size of sig
273 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000274 * \note If the bitlength of the message hash is larger than the
275 * bitlength of the group order, then the hash is truncated as
276 * prescribed by SEC1 4.1.4 step 3.
277 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200278 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
280 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200281 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200285 const unsigned char *hash, size_t hlen,
286 const unsigned char *sig, size_t slen );
287
288/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200289 * \brief Generate an ECDSA keypair on the given curve
290 *
291 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200292 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200294 * \param f_rng RNG function
295 * \param p_rng RNG parameter
296 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200300 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
301
302/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200303 * \brief Set an ECDSA context from an EC key pair
304 *
305 * \param ctx ECDSA context to set
306 * \param key EC key to use
307 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200311
312/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200313 * \brief Initialize context
314 *
315 * \param ctx Context to initialize
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200318
319/**
320 * \brief Free context
321 *
322 * \param ctx Context to free
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200325
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100326#ifdef __cplusplus
327}
328#endif
329
Paul Bakker9af723c2014-05-01 13:03:14 +0200330#endif /* ecdsa.h */