blob: c7b9f27da4f417cf6b941a62cd719dd6d3f4b704 [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if MBEDTLS_ECP_MAX_BYTES > 124
44#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020045#endif
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +020046/** Maximum size of an ECDSA signature in bytes */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020048
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020053/**
54 * \brief ECDSA context structure
55 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020057
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020058#if defined(MBEDTLS_ECP_RESTARTABLE)
59
60/**
61 * \brief General context for resuming ECDSA operations
62 */
63typedef struct
64{
Manuel Pégourié-Gonnard722e5152017-04-21 11:04:47 +020065 mbedtls_ecp_restart_ctx ecp; /*!< base context (admin+ecp info) */
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020066} mbedtls_ecdsa_restart_ctx;
67
68#else /* MBEDTLS_ECP_RESTARTABLE */
69
70/* Now we can declare functions that take a pointer to that */
71typedef void mbedtls_ecdsa_restart_ctx;
72
73#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010074
75/**
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010076 * \brief Compute ECDSA signature of a previously hashed message
77 *
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +020078 * \note The deterministic version is usually prefered.
79 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010080 * \param grp ECP group
81 * \param r First output integer
82 * \param s Second output integer
83 * \param d Private signing key
84 * \param buf Message hash
85 * \param blen Length of buf
86 * \param f_rng RNG function
87 * \param p_rng RNG parameter
88 *
Janos Follath0a5154b2017-03-10 11:31:41 +000089 * \note If the bitlength of the message hash is larger than the
90 * bitlength of the group order, then the hash is truncated as
91 * prescribed by SEC1 4.1.3 step 5.
92 *
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010093 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
97 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +010098 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100101/**
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +0200102 * \brief Compute ECDSA signature of a previously hashed message,
103 * deterministic version (RFC 6979).
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100104 *
105 * \param grp ECP group
106 * \param r First output integer
107 * \param s Second output integer
108 * \param d Private signing key
109 * \param buf Message hash
110 * \param blen Length of buf
111 * \param md_alg MD algorithm used to hash the message
112 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000113 * \note If the bitlength of the message hash is larger than the
114 * bitlength of the group order, then the hash is truncated as
115 * prescribed by SEC1 4.1.3 step 5.
116 *
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100117 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
121 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
122 mbedtls_md_type_t md_alg );
123#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100124
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100125/**
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100126 * \brief Verify ECDSA signature of a previously hashed message
127 *
128 * \param grp ECP group
129 * \param buf Message hash
130 * \param blen Length of buf
131 * \param Q Public key to use for verification
132 * \param r First integer of the signature
133 * \param s Second integer of the signature
134 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000135 * \note If the bitlength of the message hash is larger than the
136 * bitlength of the group order, then the hash is truncated as
137 * prescribed by SEC1 4.1.4 step 3.
138 *
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100139 * \return 0 if successful,
Manuel Pégourié-Gonnard1ed25052017-04-21 10:04:02 +0200140 * MBEDTLS_ERR_ECP_VERIFY_FAILED if signature is invalid
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100144 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100146
147/**
Manuel Pégourié-Gonnardaa431612013-08-09 17:10:27 +0200148 * \brief Compute ECDSA signature and write it to buffer,
149 * serialized as defined in RFC 4492 page 20.
Paul Bakker6838bd12013-09-30 13:56:38 +0200150 * (Not thread-safe to use same context in multiple threads)
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200151 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000152 * \note The deterministic version (RFC 6979) is used if
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 * MBEDTLS_ECDSA_DETERMINISTIC is defined.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200154 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200155 * \param ctx ECDSA context
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200156 * \param md_alg Algorithm that was used to hash the message
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200157 * \param hash Message hash
158 * \param hlen Length of hash
159 * \param sig Buffer that will hold the signature
160 * \param slen Length of the signature written
161 * \param f_rng RNG function
162 * \param p_rng RNG parameter
163 *
164 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200165 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200167 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000168 * \note If the bitlength of the message hash is larger than the
169 * bitlength of the group order, then the hash is truncated as
170 * prescribed by SEC1 4.1.3 step 5.
171 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200172 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
174 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200177 const unsigned char *hash, size_t hlen,
178 unsigned char *sig, size_t *slen,
179 int (*f_rng)(void *, unsigned char *, size_t),
180 void *p_rng );
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
183#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
184#if defined(MBEDTLS_DEPRECATED_WARNING)
185#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200186#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200188#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100189/**
190 * \brief Compute ECDSA signature and write it to buffer,
191 * serialized as defined in RFC 4492 page 20.
192 * Deterministic version, RFC 6979.
193 * (Not thread-safe to use same context in multiple threads)
194 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200196 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100197 * \param ctx ECDSA context
198 * \param hash Message hash
199 * \param hlen Length of hash
200 * \param sig Buffer that will hold the signature
201 * \param slen Length of the signature written
202 * \param md_alg MD algorithm used to hash the message
203 *
204 * \note The "sig" buffer must be at least as large as twice the
Manuel Pégourié-Gonnard5bf262d2015-03-31 11:46:01 +0200205 * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100207 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000208 * \note If the bitlength of the message hash is larger than the
209 * bitlength of the group order, then the hash is truncated as
210 * prescribed by SEC1 4.1.3 step 5.
211 *
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100212 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or
214 * MBEDTLS_ERR_ASN1_XXX error code
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100217 const unsigned char *hash, size_t hlen,
218 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
220#undef MBEDTLS_DEPRECATED
221#endif /* MBEDTLS_DEPRECATED_REMOVED */
222#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100223
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200224/**
225 * \brief Read and verify an ECDSA signature
226 *
227 * \param ctx ECDSA context
228 * \param hash Message hash
229 * \param hlen Size of hash
230 * \param sig Signature to read and verify
231 * \param slen Size of sig
232 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000233 * \note If the bitlength of the message hash is larger than the
234 * bitlength of the group order, then the hash is truncated as
235 * prescribed by SEC1 4.1.4 step 3.
236 *
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200237 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
239 * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
Manuel Pégourié-Gonnard35e95dd2014-04-08 12:17:41 +0200240 * valid but its actual length is less than siglen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200244 const unsigned char *hash, size_t hlen,
245 const unsigned char *sig, size_t slen );
246
247/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200248 * \brief Restartable version of \c mbedtls_ecdsa_read_signature()
249 *
250 * \note Performs the same job as \c mbedtls_ecdsa_read_signature()
251 * but can return early and restart according to the limit
252 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
253 *
254 * \param ctx ECDSA context
255 * \param hash Message hash
256 * \param hlen Size of hash
257 * \param sig Signature to read and verify
258 * \param slen Size of sig
259 * \param rs_ctx Restart context
260 *
261 * \return See \c mbedtls_ecdsa_read_signature(), or
262 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
263 * operations was reached: see \c mbedtls_ecp_set_max_ops().
264 */
265int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
266 const unsigned char *hash, size_t hlen,
267 const unsigned char *sig, size_t slen,
268 mbedtls_ecdsa_restart_ctx *rs_ctx );
269
270/**
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200271 * \brief Generate an ECDSA keypair on the given curve
272 *
273 * \param ctx ECDSA context in which the keypair should be stored
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200274 * \param gid Group (elliptic curve) to use. One of the various
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 * MBEDTLS_ECP_DP_XXX macros depending on configuration.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200276 * \param f_rng RNG function
277 * \param p_rng RNG parameter
278 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200282 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
283
284/**
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200285 * \brief Set an ECDSA context from an EC key pair
286 *
287 * \param ctx ECDSA context to set
288 * \param key EC key to use
289 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200293
294/**
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200295 * \brief Initialize context
296 *
297 * \param ctx Context to initialize
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200300
301/**
302 * \brief Free context
303 *
304 * \param ctx Context to free
305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200307
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200308#if defined(MBEDTLS_ECP_RESTARTABLE)
309/**
310 * \brief Initialize a restart context
311 */
312void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
313
314/**
315 * \brief Free the components of a restart context
316 */
317void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
318#endif /* MBEDTLS_ECP_RESTARTABLE */
319
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100320#ifdef __cplusplus
321}
322#endif
323
Paul Bakker9af723c2014-05-01 13:03:14 +0200324#endif /* ecdsa.h */