blob: 5245c6ee36f2cca814393549afcb69ae71c35bc0 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
Rose Zadik817297f2018-03-27 11:30:14 +01004 * \brief This file contains ECDSA definitions and functions.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01005 *
Rose Zadik817297f2018-03-27 11:30:14 +01006 * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in
7 * <em>Standards for Efficient Cryptography Group (SECG):
Rose Zadikbff87d92018-01-25 21:58:53 +00008 * SEC1 Elliptic Curve Cryptography</em>.
9 * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve
10 * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
11 *
Darryl Greena40a1012018-01-05 15:33:17 +000012 */
13/*
Rose Zadikbff87d92018-01-25 21:58:53 +000014 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010028 *
Rose Zadikbff87d92018-01-25 21:58:53 +000029 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010030 */
Rose Zadikbff87d92018-01-25 21:58:53 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#ifndef MBEDTLS_ECDSA_H
33#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010034
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020035#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020036#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010037
Manuel Pégourié-Gonnardfe860732018-11-12 15:06:57 +010038/**
39 * \brief Maximum ECDSA signature size for a given curve bit size
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020040 *
Manuel Pégourié-Gonnardfe860732018-11-12 15:06:57 +010041 * \param bits Curve size in bits
42 * \return Maximum signature size in bytes
43 *
44 * \note This macro returns a compile-time constant if its argument
45 * is one. It may evaluate its argument multiple times.
46 */
47/*
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020048 * Ecdsa-Sig-Value ::= SEQUENCE {
49 * r INTEGER,
50 * s INTEGER
51 * }
52 *
Manuel Pégourié-Gonnardfe860732018-11-12 15:06:57 +010053 * For each of r and s, the value (V) may include an extra initial "0" bit.
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020054 */
Manuel Pégourié-Gonnardfe860732018-11-12 15:06:57 +010055#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
Rose Zadikbff87d92018-01-25 21:58:53 +000060/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnardfe860732018-11-12 15:06:57 +010061#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020062
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020063#ifdef __cplusplus
64extern "C" {
65#endif
66
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020067/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020068 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020069 *
70 * \warning Performing multiple operations concurrently on the same
71 * ECDSA context is not supported; objects of this type
72 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020075
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020076#if defined(MBEDTLS_ECP_RESTARTABLE)
77
78/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020079 * \brief Internal restart context for ecdsa_verify()
80 *
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020081 * \note Opaque struct, defined in ecdsa.c
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020082 */
83typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
84
85/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020086 * \brief Internal restart context for ecdsa_sign()
87 *
88 * \note Opaque struct, defined in ecdsa.c
89 */
90typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
91
92#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
93/**
94 * \brief Internal restart context for ecdsa_sign_det()
95 *
96 * \note Opaque struct, defined in ecdsa.c
97 */
98typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
99#endif
100
101/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200102 * \brief General context for resuming ECDSA operations
103 */
104typedef struct
105{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200106 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
107 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200108 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200109 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
110#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
111 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
112#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200113} mbedtls_ecdsa_restart_ctx;
114
115#else /* MBEDTLS_ECP_RESTARTABLE */
116
117/* Now we can declare functions that take a pointer to that */
118typedef void mbedtls_ecdsa_restart_ctx;
119
120#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100121
122/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000123 * \brief This function computes the ECDSA signature of a
124 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100125 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000126 * \note The deterministic version is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100127 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000128 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100129 * bitlength of the group order, then the hash is truncated
130 * as defined in <em>Standards for Efficient Cryptography Group
131 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
132 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000133 *
Rose Zadik817297f2018-03-27 11:30:14 +0100134 * \see ecp.h
135 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000136 * \param grp The ECP group.
137 * \param r The first output integer.
138 * \param s The second output integer.
139 * \param d The private signing key.
140 * \param buf The message hash.
141 * \param blen The length of \p buf.
142 * \param f_rng The RNG function.
Rose Zadik817297f2018-03-27 11:30:14 +0100143 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100144 *
Rose Zadik817297f2018-03-27 11:30:14 +0100145 * \return \c 0 on success.
146 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000147 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
150 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100151 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100154/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000155 * \brief This function computes the ECDSA signature of a
156 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100157 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000158 * For more information, see <em>RFC-6979: Deterministic
159 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
160 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100161 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000162 * \note If the bitlength of the message hash is larger than the
163 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100164 * defined in <em>Standards for Efficient Cryptography Group
165 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
166 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000167 *
Rose Zadik817297f2018-03-27 11:30:14 +0100168 * \see ecp.h
169 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000170 * \param grp The ECP group.
171 * \param r The first output integer.
172 * \param s The second output integer.
173 * \param d The private signing key.
174 * \param buf The message hash.
175 * \param blen The length of \p buf.
176 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100177 *
Rose Zadik817297f2018-03-27 11:30:14 +0100178 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100179 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000180 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
183 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
184 mbedtls_md_type_t md_alg );
185#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100186
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100187/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000188 * \brief This function verifies the ECDSA signature of a
189 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100190 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000191 * \note If the bitlength of the message hash is larger than the
192 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100193 * defined in <em>Standards for Efficient Cryptography Group
194 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
195 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000196 *
Rose Zadik817297f2018-03-27 11:30:14 +0100197 * \see ecp.h
198 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000199 * \param grp The ECP group.
200 * \param buf The message hash.
201 * \param blen The length of \p buf.
202 * \param Q The public key to use for verification.
203 * \param r The first integer of the signature.
204 * \param s The second integer of the signature.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100205 *
Rose Zadik817297f2018-03-27 11:30:14 +0100206 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100207 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
208 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100209 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000210 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100213 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100215
216/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000217 * \brief This function computes the ECDSA signature and writes it
218 * to a buffer, serialized as defined in <em>RFC-4492:
219 * Elliptic Curve Cryptography (ECC) Cipher Suites for
220 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200221 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000222 * \warning It is not thread-safe to use the same context in
223 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200224 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000225 * \note The deterministic version is used if
226 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
227 * information, see <em>RFC-6979: Deterministic Usage
228 * of the Digital Signature Algorithm (DSA) and Elliptic
229 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200230 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000231 * \note The \p sig buffer must be at least twice as large as the
232 * size of the curve used, plus 9. For example, 73 Bytes if
233 * a 256-bit curve is used. A buffer length of
234 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200235 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000236 * \note If the bitlength of the message hash is larger than the
237 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000238 * defined in <em>Standards for Efficient Cryptography Group
239 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
240 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000241 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000242 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100243 *
244 * \param ctx The ECDSA context.
245 * \param md_alg The message digest that was used to hash the message.
246 * \param hash The message hash.
247 * \param hlen The length of the hash.
248 * \param sig The buffer that holds the signature.
249 * \param slen The length of the signature written.
250 * \param f_rng The RNG function.
251 * \param p_rng The RNG context.
252 *
253 * \return \c 0 on success.
254 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
255 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200258 const unsigned char *hash, size_t hlen,
259 unsigned char *sig, size_t *slen,
260 int (*f_rng)(void *, unsigned char *, size_t),
261 void *p_rng );
262
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200263/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200264 * \brief This function computes the ECDSA signature and writes it
265 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200266 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200267 * \see \c mbedtls_ecdsa_write_signature()
268 *
269 * \note This function is like \c mbedtls_ecdsa_write_signature()
270 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200271 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
272 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200273 * \param ctx The ECDSA context.
274 * \param md_alg The message digest that was used to hash the message.
275 * \param hash The message hash.
276 * \param hlen The length of the hash.
277 * \param sig The buffer that holds the signature.
278 * \param slen The length of the signature written.
279 * \param f_rng The RNG function.
280 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200281 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200282 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200283 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200284 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200285 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200286 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
287 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200288 */
289int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
290 mbedtls_md_type_t md_alg,
291 const unsigned char *hash, size_t hlen,
292 unsigned char *sig, size_t *slen,
293 int (*f_rng)(void *, unsigned char *, size_t),
294 void *p_rng,
295 mbedtls_ecdsa_restart_ctx *rs_ctx );
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
298#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
299#if defined(MBEDTLS_DEPRECATED_WARNING)
300#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200301#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200303#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100304/**
Rose Zadik817297f2018-03-27 11:30:14 +0100305 * \brief This function computes an ECDSA signature and writes
306 * it to a buffer, serialized as defined in <em>RFC-4492:
307 * Elliptic Curve Cryptography (ECC) Cipher Suites for
308 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100309 *
Rose Zadik817297f2018-03-27 11:30:14 +0100310 * The deterministic version is defined in <em>RFC-6979:
311 * Deterministic Usage of the Digital Signature Algorithm (DSA)
312 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200313 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000314 * \warning It is not thread-safe to use the same context in
315 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100316 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000317 * \note The \p sig buffer must be at least twice as large as the
318 * size of the curve used, plus 9. For example, 73 Bytes if a
319 * 256-bit curve is used. A buffer length of
320 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100321 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000322 * \note If the bitlength of the message hash is larger than the
323 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000324 * defined in <em>Standards for Efficient Cryptography Group
325 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
326 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000327 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000328 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100329 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100330 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
331 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100332 *
333 * \param ctx The ECDSA context.
Rose Zadik14d0d572018-04-16 16:09:30 +0100334 * \param hash The message hash.
Rose Zadik817297f2018-03-27 11:30:14 +0100335 * \param hlen The length of the hash.
336 * \param sig The buffer that holds the signature.
337 * \param slen The length of the signature written.
338 * \param md_alg The MD algorithm used to hash the message.
339 *
340 * \return \c 0 on success.
341 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
342 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100345 const unsigned char *hash, size_t hlen,
346 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
348#undef MBEDTLS_DEPRECATED
349#endif /* MBEDTLS_DEPRECATED_REMOVED */
350#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100351
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200352/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000353 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200354 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000355 * \note If the bitlength of the message hash is larger than the
356 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000357 * defined in <em>Standards for Efficient Cryptography Group
358 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
359 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000360 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000361 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100362 *
363 * \param ctx The ECDSA context.
364 * \param hash The message hash.
365 * \param hlen The size of the hash.
366 * \param sig The signature to read and verify.
367 * \param slen The size of \p sig.
368 *
369 * \return \c 0 on success.
370 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100371 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
372 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100373 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
374 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200377 const unsigned char *hash, size_t hlen,
378 const unsigned char *sig, size_t slen );
379
380/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200381 * \brief This function reads and verifies an ECDSA signature,
382 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200383 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200384 * \see \c mbedtls_ecdsa_read_signature()
385 *
386 * \note This function is like \c mbedtls_ecdsa_read_signature()
387 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200388 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
389 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200390 * \param ctx The ECDSA context.
391 * \param hash The message hash.
392 * \param hlen The size of the hash.
393 * \param sig The signature to read and verify.
394 * \param slen The size of \p sig.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200395 * \param rs_ctx The restart context (NULL disables restart).
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200396 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200397 * \return \c 0 on success.
398 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
399 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
400 * signature in \p sig, but its length is less than \p siglen.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200401 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200402 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200403 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
404 * error code on failure for any other reason.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200405 */
406int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
407 const unsigned char *hash, size_t hlen,
408 const unsigned char *sig, size_t slen,
409 mbedtls_ecdsa_restart_ctx *rs_ctx );
410
411/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000412 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200413 *
Rose Zadik817297f2018-03-27 11:30:14 +0100414 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200415 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000416 * \param ctx The ECDSA context to store the keypair in.
417 * \param gid The elliptic curve to use. One of the various
418 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
419 * \param f_rng The RNG function.
Rose Zadik817297f2018-03-27 11:30:14 +0100420 * \param p_rng The RNG context.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200421 *
Rose Zadik817297f2018-03-27 11:30:14 +0100422 * \return \c 0 on success.
423 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200426 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
427
428/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000429 * \brief This function sets an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200430 *
Rose Zadik817297f2018-03-27 11:30:14 +0100431 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200432 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000433 * \param ctx The ECDSA context to set.
434 * \param key The EC key to use.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200435 *
Rose Zadik817297f2018-03-27 11:30:14 +0100436 * \return \c 0 on success.
437 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200440
441/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000442 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200443 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000444 * \param ctx The ECDSA context to initialize.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200447
448/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000449 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200450 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000451 * \param ctx The ECDSA context to free.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200454
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200455#if defined(MBEDTLS_ECP_RESTARTABLE)
456/**
457 * \brief Initialize a restart context
458 */
459void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
460
461/**
462 * \brief Free the components of a restart context
463 */
464void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
465#endif /* MBEDTLS_ECP_RESTARTABLE */
466
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100467#ifdef __cplusplus
468}
469#endif
470
Paul Bakker9af723c2014-05-01 13:03:14 +0200471#endif /* ecdsa.h */