blob: 7490c65966e3ce0bcf74651d0363b560fb1bad6d [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 */
Unknown60b25f02018-02-06 03:17:59 -050055#if MBEDTLS_ECP_MAX_BYTES > 124
56#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
57#endif
58
Gilles Peskine9a8bb672017-11-02 17:09:49 +010059#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
60 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
61 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
62 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
63
64/**
65 * \brief Maximum ECDSA signature size for a given curve bit size
66 *
67 * \param bits Curve size in bits
68 * \return Maximum signature size in bytes
69 *
70 * \note If you need a compile-time constant, call the macro
71 * MBEDTLS_ECDSA_MAX_SIG_LEN instead.
72 */
73static inline size_t mbedtls_ecdsa_max_sig_len( size_t bits )
74{
75 return( MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) );
76}
77
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020078/** Maximum size of an ECDSA signature in bytes */
Unknown60b25f02018-02-06 03:17:59 -050079#define MBEDTLS_ECDSA_MAX_LEN (MBEDTLS_ECDSA_MAX_SIG_LEN( \
80 8 * MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020081/**
82 * \brief ECDSA context structure
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020085
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010086#ifdef __cplusplus
87extern "C" {
88#endif
89
90/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010091 * \brief Compute ECDSA signature of a previously hashed message
92 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020093 * \note The deterministic version is usually prefered.
94 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010095 * \param grp ECP group
96 * \param r First output integer
97 * \param s Second output integer
98 * \param d Private signing key
99 * \param buf Message hash
100 * \param blen Length of buf
101 * \param f_rng RNG function
102 * \param p_rng RNG parameter
103 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000104 * \note If the bitlength of the message hash is larger than the
105 * bitlength of the group order, then the hash is truncated as
106 * prescribed by SEC1 4.1.3 step 5.
107 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100108 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
112 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100113 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100116/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +0200117 * \brief Compute ECDSA signature of a previously hashed message,
118 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100119 *
120 * \param grp ECP group
121 * \param r First output integer
122 * \param s Second output integer
123 * \param d Private signing key
124 * \param buf Message hash
125 * \param blen Length of buf
126 * \param md_alg MD algorithm used to hash the message
127 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000128 * \note If the bitlength of the message hash is larger than the
129 * bitlength of the group order, then the hash is truncated as
130 * prescribed by SEC1 4.1.3 step 5.
131 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100132 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
136 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
137 mbedtls_md_type_t md_alg );
138#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100139
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100140/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100141 * \brief Verify ECDSA signature of a previously hashed message
142 *
143 * \param grp ECP group
144 * \param buf Message hash
145 * \param blen Length of buf
146 * \param Q Public key to use for verification
147 * \param r First integer of the signature
148 * \param s Second integer of the signature
149 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000150 * \note If the bitlength of the message hash is larger than the
151 * bitlength of the group order, then the hash is truncated as
152 * prescribed by SEC1 4.1.4 step 3.
153 *
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100154 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid
156 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100159 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100161
162/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200163 * \brief Compute ECDSA signature and write it to buffer,
164 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200165 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200166 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000167 * \note The deterministic version (RFC 6979) is used if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 * MBEDTLS_ECDSA_DETERMINISTIC is defined.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200169 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200170 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200171 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200172 * \param hash Message hash
173 * \param hlen Length of hash
174 * \param sig Buffer that will hold the signature
175 * \param slen Length of the signature written
176 * \param f_rng RNG function
177 * \param p_rng RNG parameter
178 *
Gilles Peskine9a8bb672017-11-02 17:09:49 +0100179 * \note The \c sig buffer must be at least
180 * `MBEDTLS_ECDSA_MAX_SIG_LEN(ctx->grp.pbits)` bytes long.
181 * MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200182 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000183 * \note If the bitlength of the message hash is larger than the
184 * bitlength of the group order, then the hash is truncated as
185 * prescribed by SEC1 4.1.3 step 5.
186 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200187 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
189 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200192 const unsigned char *hash, size_t hlen,
193 unsigned char *sig, size_t *slen,
194 int (*f_rng)(void *, unsigned char *, size_t),
195 void *p_rng );
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
198#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
199#if defined(MBEDTLS_DEPRECATED_WARNING)
200#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200201#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200203#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100204/**
205 * \brief Compute ECDSA signature and write it to buffer,
206 * serialized as defined in RFC 4492 page 20.
207 * Deterministic version, RFC 6979.
208 * (Not thread-safe to use same context in multiple threads)
209 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200211 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100212 * \param ctx ECDSA context
213 * \param hash Message hash
214 * \param hlen Length of hash
215 * \param sig Buffer that will hold the signature
216 * \param slen Length of the signature written
217 * \param md_alg MD algorithm used to hash the message
218 *
Gilles Peskine9a8bb672017-11-02 17:09:49 +0100219 * \note The \c sig buffer must be at least
220 * `MBEDTLS_ECDSA_MAX_SIG_LEN(ctx->grp.pbits)` bytes long.
221 * MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100222 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000223 * \note If the bitlength of the message hash is larger than the
224 * bitlength of the group order, then the hash is truncated as
225 * prescribed by SEC1 4.1.3 step 5.
226 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100227 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
229 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100232 const unsigned char *hash, size_t hlen,
233 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
235#undef MBEDTLS_DEPRECATED
236#endif /* MBEDTLS_DEPRECATED_REMOVED */
237#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100238
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200239/**
Unknown60b25f02018-02-06 03:17:59 -0500240 * \brief Convert a signature from numbers to ASN.1 INTEGER's,
241 * then both packed together as parts of an ASN.1 SEQUENCE
Gilles Peskinebce41d32017-11-02 17:14:18 +0100242 *
243 * \param r First number of the signature
244 * \param s Second number of the signature
245 * \param sig Buffer that will hold the signature
246 * \param slen Length of the signature written
247 * \param ssize Size of the sig buffer
248 *
249 * \note The size of the buffer \c ssize should be at least
250 * `MBEDTLS_ECDSA_MAX_SIG_LEN(grp->pbits)` bytes long if
251 * the signature was produced from curve \c grp,
252 * otherwise this function will return an error.
253 *
254 * \return 0 if successful,
255 * or a MBEDTLS_ERR_MPI_XXX or MBEDTLS_ERR_ASN1_XXX error code
256 *
257 */
258int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
259 unsigned char *sig, size_t *slen,
260 size_t ssize );
261
262/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200263 * \brief Read and verify an ECDSA signature
264 *
265 * \param ctx ECDSA context
266 * \param hash Message hash
267 * \param hlen Size of hash
268 * \param sig Signature to read and verify
269 * \param slen Size of sig
270 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000271 * \note If the bitlength of the message hash is larger than the
272 * bitlength of the group order, then the hash is truncated as
273 * prescribed by SEC1 4.1.4 step 3.
274 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200275 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
277 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200278 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200282 const unsigned char *hash, size_t hlen,
283 const unsigned char *sig, size_t slen );
284
285/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200286 * \brief Generate an ECDSA keypair on the given curve
287 *
288 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200289 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200291 * \param f_rng RNG function
292 * \param p_rng RNG parameter
293 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200297 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
298
299/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200300 * \brief Set an ECDSA context from an EC key pair
301 *
302 * \param ctx ECDSA context to set
303 * \param key EC key to use
304 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200308
309/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200310 * \brief Initialize context
311 *
312 * \param ctx Context to initialize
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200315
316/**
317 * \brief Free context
318 *
319 * \param ctx Context to free
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200322
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100323#ifdef __cplusplus
324}
325#endif
326
Paul Bakker9af723c2014-05-01 13:03:14 +0200327#endif /* ecdsa.h */