blob: d5f7985cd27127b6bfc4639aae88a8137e1ffa2d [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é-Gonnard63e93192015-03-31 11:15:48 +020038/*
Rose Zadikbff87d92018-01-25 21:58:53 +000039 * RFC-4492 page 20:
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020040 *
41 * Ecdsa-Sig-Value ::= SEQUENCE {
42 * r INTEGER,
43 * s INTEGER
44 * }
45 *
46 * Size is at most
47 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
48 * twice that + 1 (tag) + 2 (len) for the sequence
49 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
50 * and less than 124 (total len <= 255) for the sequence)
51 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if MBEDTLS_ECP_MAX_BYTES > 124
53#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020054#endif
Rose Zadikbff87d92018-01-25 21:58:53 +000055/** The maximal size of an ECDSA signature in Bytes. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020057
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020058#ifdef __cplusplus
59extern "C" {
60#endif
61
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020062/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020063 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020064 *
65 * \warning Performing multiple operations concurrently on the same
66 * ECDSA context is not supported; objects of this type
67 * should not be shared between multiple threads.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020070
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020071#if defined(MBEDTLS_ECP_RESTARTABLE)
72
73/**
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +020074 * \brief Internal restart context for ecdsa_verify()
75 *
76 * \note Opaque struct
77 */
78typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
79
80/**
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +020081 * \brief Internal restart context for ecdsa_sign()
82 *
83 * \note Opaque struct, defined in ecdsa.c
84 */
85typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
86
87#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
88/**
89 * \brief Internal restart context for ecdsa_sign_det()
90 *
91 * \note Opaque struct, defined in ecdsa.c
92 */
93typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
94#endif
95
96/**
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +020097 * \brief General context for resuming ECDSA operations
98 */
99typedef struct
100{
Manuel Pégourié-Gonnard12e4a8b2018-09-12 10:55:15 +0200101 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
102 shared administrative info */
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200103 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
Manuel Pégourié-Gonnardb90883d2017-04-25 11:33:10 +0200104 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
105#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
106 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
107#endif
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200108} mbedtls_ecdsa_restart_ctx;
109
110#else /* MBEDTLS_ECP_RESTARTABLE */
111
112/* Now we can declare functions that take a pointer to that */
113typedef void mbedtls_ecdsa_restart_ctx;
114
115#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100116
117/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000118 * \brief This function computes the ECDSA signature of a
119 * previously-hashed message.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100120 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000121 * \note The deterministic version is usually preferred.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100122 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000123 * \note If the bitlength of the message hash is larger than the
Rose Zadik817297f2018-03-27 11:30:14 +0100124 * bitlength of the group order, then the hash is truncated
125 * as defined in <em>Standards for Efficient Cryptography Group
126 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
127 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000128 *
Rose Zadik817297f2018-03-27 11:30:14 +0100129 * \see ecp.h
130 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000131 * \param grp The ECP group.
132 * \param r The first output integer.
133 * \param s The second output integer.
134 * \param d The private signing key.
135 * \param buf The message hash.
136 * \param blen The length of \p buf.
137 * \param f_rng The RNG function.
Rose Zadik817297f2018-03-27 11:30:14 +0100138 * \param p_rng The RNG context.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100139 *
Rose Zadik817297f2018-03-27 11:30:14 +0100140 * \return \c 0 on success.
141 * \return An \c MBEDTLS_ERR_ECP_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000142 * or \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
145 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100146 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100149/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000150 * \brief This function computes the ECDSA signature of a
151 * previously-hashed message, deterministic version.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100152 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000153 * For more information, see <em>RFC-6979: Deterministic
154 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
155 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100156 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000157 * \note If the bitlength of the message hash is larger than the
158 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100159 * defined in <em>Standards for Efficient Cryptography Group
160 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
161 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000162 *
Rose Zadik817297f2018-03-27 11:30:14 +0100163 * \see ecp.h
164 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000165 * \param grp The ECP group.
166 * \param r The first output integer.
167 * \param s The second output integer.
168 * \param d The private signing key.
169 * \param buf The message hash.
170 * \param blen The length of \p buf.
171 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100172 *
Rose Zadik817297f2018-03-27 11:30:14 +0100173 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100174 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000175 * error code on failure.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
178 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
179 mbedtls_md_type_t md_alg );
180#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100181
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100182/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000183 * \brief This function verifies the ECDSA signature of a
184 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100185 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000186 * \note If the bitlength of the message hash is larger than the
187 * bitlength of the group order, then the hash is truncated as
Rose Zadik817297f2018-03-27 11:30:14 +0100188 * defined in <em>Standards for Efficient Cryptography Group
189 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
190 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000191 *
Rose Zadik817297f2018-03-27 11:30:14 +0100192 * \see ecp.h
193 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000194 * \param grp The ECP group.
195 * \param buf The message hash.
196 * \param blen The length of \p buf.
197 * \param Q The public key to use for verification.
198 * \param r The first integer of the signature.
199 * \param s The second integer of the signature.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100200 *
Rose Zadik817297f2018-03-27 11:30:14 +0100201 * \return \c 0 on success.
Rose Zadik14d0d572018-04-16 16:09:30 +0100202 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
203 * is invalid.
Rose Zadik817297f2018-03-27 11:30:14 +0100204 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
Rose Zadikbff87d92018-01-25 21:58:53 +0000205 * error code on failure for any other reason.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100208 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100210
211/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000212 * \brief This function computes the ECDSA signature and writes it
213 * to a buffer, serialized as defined in <em>RFC-4492:
214 * Elliptic Curve Cryptography (ECC) Cipher Suites for
215 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200216 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000217 * \warning It is not thread-safe to use the same context in
218 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200219 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000220 * \note The deterministic version is used if
221 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
222 * information, see <em>RFC-6979: Deterministic Usage
223 * of the Digital Signature Algorithm (DSA) and Elliptic
224 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200225 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000226 * \note The \p sig buffer must be at least twice as large as the
227 * size of the curve used, plus 9. For example, 73 Bytes if
228 * a 256-bit curve is used. A buffer length of
229 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200230 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000231 * \note If the bitlength of the message hash is larger than the
232 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000233 * defined in <em>Standards for Efficient Cryptography Group
234 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
235 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000236 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000237 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100238 *
239 * \param ctx The ECDSA context.
240 * \param md_alg The message digest that was used to hash the message.
241 * \param hash The message hash.
242 * \param hlen The length of the hash.
243 * \param sig The buffer that holds the signature.
244 * \param slen The length of the signature written.
245 * \param f_rng The RNG function.
246 * \param p_rng The RNG context.
247 *
248 * \return \c 0 on success.
249 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
250 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200253 const unsigned char *hash, size_t hlen,
254 unsigned char *sig, size_t *slen,
255 int (*f_rng)(void *, unsigned char *, size_t),
256 void *p_rng );
257
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200258/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200259 * \brief This function computes the ECDSA signature and writes it
260 * to a buffer, in a restartable way.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200261 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200262 * \see \c mbedtls_ecdsa_write_signature()
263 *
264 * \note This function is like \c mbedtls_ecdsa_write_signature()
265 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200266 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
267 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200268 * \param ctx The ECDSA context.
269 * \param md_alg The message digest that was used to hash the message.
270 * \param hash The message hash.
271 * \param hlen The length of the hash.
272 * \param sig The buffer that holds the signature.
273 * \param slen The length of the signature written.
274 * \param f_rng The RNG function.
275 * \param p_rng The RNG context.
276 * \param rs_ctx The restart context.
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200277 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200278 * \return \c 0 on success.
279 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
280 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
281 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardaddb10e2017-04-21 12:54:46 +0200282 * operations was reached: see \c mbedtls_ecp_set_max_ops().
283 */
284int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
285 mbedtls_md_type_t md_alg,
286 const unsigned char *hash, size_t hlen,
287 unsigned char *sig, size_t *slen,
288 int (*f_rng)(void *, unsigned char *, size_t),
289 void *p_rng,
290 mbedtls_ecdsa_restart_ctx *rs_ctx );
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
293#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
294#if defined(MBEDTLS_DEPRECATED_WARNING)
295#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200296#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200298#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100299/**
Rose Zadik817297f2018-03-27 11:30:14 +0100300 * \brief This function computes an ECDSA signature and writes
301 * it to a buffer, serialized as defined in <em>RFC-4492:
302 * Elliptic Curve Cryptography (ECC) Cipher Suites for
303 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100304 *
Rose Zadik817297f2018-03-27 11:30:14 +0100305 * The deterministic version is defined in <em>RFC-6979:
306 * Deterministic Usage of the Digital Signature Algorithm (DSA)
307 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200308 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000309 * \warning It is not thread-safe to use the same context in
310 * multiple threads.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100311 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000312 * \note The \p sig buffer must be at least twice as large as the
313 * size of the curve used, plus 9. For example, 73 Bytes if a
314 * 256-bit curve is used. A buffer length of
315 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100316 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000317 * \note If the bitlength of the message hash is larger than the
318 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000319 * defined in <em>Standards for Efficient Cryptography Group
320 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
321 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000322 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000323 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100324 *
Rose Zadikec5d4162018-04-17 15:55:28 +0100325 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
326 * Mbed TLS version 2.0 and later.
Rose Zadik817297f2018-03-27 11:30:14 +0100327 *
328 * \param ctx The ECDSA context.
Rose Zadik14d0d572018-04-16 16:09:30 +0100329 * \param hash The message hash.
Rose Zadik817297f2018-03-27 11:30:14 +0100330 * \param hlen The length of the hash.
331 * \param sig The buffer that holds the signature.
332 * \param slen The length of the signature written.
333 * \param md_alg The MD algorithm used to hash the message.
334 *
335 * \return \c 0 on success.
336 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
337 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100340 const unsigned char *hash, size_t hlen,
341 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
343#undef MBEDTLS_DEPRECATED
344#endif /* MBEDTLS_DEPRECATED_REMOVED */
345#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100346
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200347/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000348 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200349 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000350 * \note If the bitlength of the message hash is larger than the
351 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000352 * defined in <em>Standards for Efficient Cryptography Group
353 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
354 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000355 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000356 * \see ecp.h
Rose Zadik817297f2018-03-27 11:30:14 +0100357 *
358 * \param ctx The ECDSA context.
359 * \param hash The message hash.
360 * \param hlen The size of the hash.
361 * \param sig The signature to read and verify.
362 * \param slen The size of \p sig.
363 *
364 * \return \c 0 on success.
365 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
Rose Zadikabc9ec72018-04-23 06:16:40 +0100366 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
367 * signature in \p sig, but its length is less than \p siglen.
Rose Zadik817297f2018-03-27 11:30:14 +0100368 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
369 * error code on failure for any other reason.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200372 const unsigned char *hash, size_t hlen,
373 const unsigned char *sig, size_t slen );
374
375/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200376 * \brief This function reads and verifies an ECDSA signature,
377 * in a restartable way.
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200378 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200379 * \see \c mbedtls_ecdsa_read_signature()
380 *
381 * \note This function is like \c mbedtls_ecdsa_read_signature()
382 * but it can return early and restart according to the limit
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200383 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
384 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200385 * \param ctx The ECDSA context.
386 * \param hash The message hash.
387 * \param hlen The size of the hash.
388 * \param sig The signature to read and verify.
389 * \param slen The size of \p sig.
390 * \param rs_ctx The restart context
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200391 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200392 * \return \c 0 on success.
393 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
394 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
395 * signature in \p sig, but its length is less than \p siglen.
396 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
397 * error code on failure for any other reason.
398 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200399 * operations was reached: see \c mbedtls_ecp_set_max_ops().
400 */
401int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
402 const unsigned char *hash, size_t hlen,
403 const unsigned char *sig, size_t slen,
404 mbedtls_ecdsa_restart_ctx *rs_ctx );
405
406/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000407 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200408 *
Rose Zadik817297f2018-03-27 11:30:14 +0100409 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200410 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000411 * \param ctx The ECDSA context to store the keypair in.
412 * \param gid The elliptic curve to use. One of the various
413 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
414 * \param f_rng The RNG function.
Rose Zadik817297f2018-03-27 11:30:14 +0100415 * \param p_rng The RNG context.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200416 *
Rose Zadik817297f2018-03-27 11:30:14 +0100417 * \return \c 0 on success.
418 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200421 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
422
423/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000424 * \brief This function sets an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200425 *
Rose Zadik817297f2018-03-27 11:30:14 +0100426 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200427 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000428 * \param ctx The ECDSA context to set.
429 * \param key The EC key to use.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200430 *
Rose Zadik817297f2018-03-27 11:30:14 +0100431 * \return \c 0 on success.
432 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200435
436/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000437 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200438 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000439 * \param ctx The ECDSA context to initialize.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200442
443/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000444 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200445 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000446 * \param ctx The ECDSA context to free.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200449
Manuel Pégourié-Gonnard32aa4372017-04-21 10:29:13 +0200450#if defined(MBEDTLS_ECP_RESTARTABLE)
451/**
452 * \brief Initialize a restart context
453 */
454void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
455
456/**
457 * \brief Free the components of a restart context
458 */
459void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
460#endif /* MBEDTLS_ECP_RESTARTABLE */
461
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100462#ifdef __cplusplus
463}
464#endif
465
Paul Bakker9af723c2014-05-01 13:03:14 +0200466#endif /* ecdsa.h */