blob: c0088db5eb196716568de54b5776c13089e37061 [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 */
Gilles Peskine9a8bb672017-11-02 17:09:49 +010043
44/**
45 * \brief Maximum ECDSA signature size for a given curve bit size
46 *
47 * \param bits Curve size in bits
48 * \return Maximum signature size in bytes
49 *
50 * \note This macro returns a compile-time constant if its argument
51 * is one. It may evaluate its argument multiple times; if
52 * this is a problem, call the function
53 * mbedtls_ecdsa_max_sig_len instead.
54 */
55#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
56 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
57 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
58 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
59
60/**
61 * \brief Maximum ECDSA signature size for a given curve bit size
62 *
63 * \param bits Curve size in bits
64 * \return Maximum signature size in bytes
65 *
66 * \note If you need a compile-time constant, call the macro
67 * MBEDTLS_ECDSA_MAX_SIG_LEN instead.
68 */
69static inline size_t mbedtls_ecdsa_max_sig_len( size_t bits )
70{
71 return( MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) );
72}
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if MBEDTLS_ECP_MAX_BYTES > 124
75#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020076#endif
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020077/** Maximum size of an ECDSA signature in bytes */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020079
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/**
Gilles Peskinebce41d32017-11-02 17:14:18 +0100239 * \brief Convert a signature from numbers to ASN.1
240 *
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.
251 *
252 * \return 0 if successful,
253 * or a MBEDTLS_ERR_MPI_XXX or MBEDTLS_ERR_ASN1_XXX error code
254 *
255 */
256int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
257 unsigned char *sig, size_t *slen,
258 size_t ssize );
259
260/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200261 * \brief Read and verify an ECDSA signature
262 *
263 * \param ctx ECDSA context
264 * \param hash Message hash
265 * \param hlen Size of hash
266 * \param sig Signature to read and verify
267 * \param slen Size of sig
268 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000269 * \note If the bitlength of the message hash is larger than the
270 * bitlength of the group order, then the hash is truncated as
271 * prescribed by SEC1 4.1.4 step 3.
272 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200273 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
275 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200276 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200280 const unsigned char *hash, size_t hlen,
281 const unsigned char *sig, size_t slen );
282
283/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200284 * \brief Generate an ECDSA keypair on the given curve
285 *
286 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200287 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200289 * \param f_rng RNG function
290 * \param p_rng RNG parameter
291 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200295 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
296
297/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200298 * \brief Set an ECDSA context from an EC key pair
299 *
300 * \param ctx ECDSA context to set
301 * \param key EC key to use
302 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200306
307/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200308 * \brief Initialize context
309 *
310 * \param ctx Context to initialize
311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200313
314/**
315 * \brief Free context
316 *
317 * \param ctx Context to free
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200320
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100321#ifdef __cplusplus
322}
323#endif
324
Paul Bakker9af723c2014-05-01 13:03:14 +0200325#endif /* ecdsa.h */