blob: f9914703491aa2ece8d7d24265e5b91cb421a948 [file] [log] [blame]
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +01001/**
2 * \file ecdsa.h
3 *
Rose Zadikbff87d92018-01-25 21:58:53 +00004 * \brief The Elliptic Curve Digital Signature Algorithm (ECDSA).
5 *
6 * ECDSA is defined in <em>Standards for Efficient Cryptography Group (SECG):
7 * SEC1 Elliptic Curve Cryptography</em>.
8 * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve
9 * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
10 *
Darryl Greena40a1012018-01-05 15:33:17 +000011 */
12/*
Rose Zadikbff87d92018-01-25 21:58:53 +000013 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020014 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010027 *
Rose Zadikbff87d92018-01-25 21:58:53 +000028 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010029 */
Rose Zadikbff87d92018-01-25 21:58:53 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#ifndef MBEDTLS_ECDSA_H
32#define MBEDTLS_ECDSA_H
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010033
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020034#include "ecp.h"
Manuel Pégourié-Gonnard887aa5b2014-04-04 13:57:20 +020035#include "md.h"
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +010036
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020037/*
Rose Zadikbff87d92018-01-25 21:58:53 +000038 * RFC-4492 page 20:
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020039 *
40 * Ecdsa-Sig-Value ::= SEQUENCE {
41 * r INTEGER,
42 * s INTEGER
43 * }
44 *
45 * Size is at most
46 * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
47 * twice that + 1 (tag) + 2 (len) for the sequence
48 * (assuming ECP_MAX_BYTES is less than 126 for r and s,
49 * and less than 124 (total len <= 255) for the sequence)
50 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if MBEDTLS_ECP_MAX_BYTES > 124
52#error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020053#endif
Manuel Pégourié-Gonnard63e93192015-03-31 11:15:48 +020054
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020055/**
Gilles Peskine9a8bb672017-11-02 17:09:49 +010056 * \brief Maximum ECDSA signature size for a given curve bit size
57 *
58 * \param bits Curve size in bits
59 * \return Maximum signature size in bytes
60 *
61 * \note This macro returns a compile-time constant if its argument
62 * is one. It may evaluate its argument multiple times; if
63 * this is a problem, call the function
64 * mbedtls_ecdsa_max_sig_len instead.
65 */
66#define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
67 ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
68 /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
69 /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
70
71/**
72 * \brief Maximum ECDSA signature size for a given curve bit size
73 *
74 * \param bits Curve size in bits
75 * \return Maximum signature size in bytes
76 *
77 * \note If you need a compile-time constant, call the macro
78 * MBEDTLS_ECDSA_MAX_SIG_LEN instead.
79 */
80static inline size_t mbedtls_ecdsa_max_sig_len( size_t bits )
81{
82 return( MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) );
83}
84
Andrzej Kurek49241632018-02-08 09:03:21 -050085/** The maximal size of an ECDSA signature in Bytes. */
Andrzej Kurekbba09272018-02-14 07:16:27 -050086#define MBEDTLS_ECDSA_MAX_LEN \
87 ( MBEDTLS_ECDSA_MAX_SIG_LEN( 8 * MBEDTLS_ECP_MAX_BYTES ) )
Andrzej Kurek024ab062018-02-12 09:34:39 -050088
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020089/**
Rose Zadikbff87d92018-01-25 21:58:53 +000090 * \brief The ECDSA context structure.
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092typedef mbedtls_ecp_keypair mbedtls_ecdsa_context;
Manuel Pégourié-Gonnardbec2f452013-06-27 10:17:07 +020093
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010094#ifdef __cplusplus
95extern "C" {
96#endif
97
98/**
Rose Zadikbff87d92018-01-25 21:58:53 +000099 * \brief This function computes the ECDSA signature of a
Andrzej Kurekbba09272018-02-14 07:16:27 -0500100 * previously-hashed message. The signature is in
101 * ASN.1 SEQUENCE format, as described in <em>Standards
102 * for Efficient Cryptography Group (SECG): SEC1 Elliptic
103 * Curve Cryptography</em>, section C.5.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100104 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000105 * \note The deterministic version is usually preferred.
Manuel Pégourié-Gonnardb8cfe3f2015-03-31 11:04:45 +0200106 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000107 * \param grp The ECP group.
108 * \param r The first output integer.
109 * \param s The second output integer.
110 * \param d The private signing key.
111 * \param buf The message hash.
112 * \param blen The length of \p buf.
113 * \param f_rng The RNG function.
114 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100115 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000116 * \note If the bitlength of the message hash is larger than the
Rose Zadikbff87d92018-01-25 21:58:53 +0000117 * bitlength of the group order, then the hash is truncated
118 * as defined in <em>Standards for Efficient Cryptography Group
119 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
120 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000121 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000122 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX
123 * or \c MBEDTLS_MPI_XXX error code on failure.
124 *
125 * \see ecp.h
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
128 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100129 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100132/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000133 * \brief This function computes the ECDSA signature of a
134 * previously-hashed message, deterministic version.
135 * For more information, see <em>RFC-6979: Deterministic
136 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
137 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100138 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000139 * \param grp The ECP group.
140 * \param r The first output integer.
141 * \param s The second output integer.
142 * \param d The private signing key.
143 * \param buf The message hash.
144 * \param blen The length of \p buf.
145 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100146 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000147 * \note If the bitlength of the message hash is larger than the
148 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000149 * defined in <em>Standards for Efficient Cryptography Group
150 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
151 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000152 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000153 * \return \c 0 on success,
154 * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
155 * error code on failure.
156 *
157 * \see ecp.h
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
160 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
161 mbedtls_md_type_t md_alg );
162#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard4daaef72014-01-06 14:25:56 +0100163
Manuel Pégourié-Gonnardb309ab22013-01-26 17:24:59 +0100164/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000165 * \brief This function verifies the ECDSA signature of a
166 * previously-hashed message.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100167 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000168 * \param grp The ECP group.
169 * \param buf The message hash.
170 * \param blen The length of \p buf.
171 * \param Q The public key to use for verification.
172 * \param r The first integer of the signature.
173 * \param s The second integer of the signature.
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100174 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000175 * \note If the bitlength of the message hash is larger than the
176 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000177 * defined in <em>Standards for Efficient Cryptography Group
178 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
179 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000180 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000181 * \return \c 0 on success,
182 * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
183 * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
184 * error code on failure for any other reason.
185 *
186 * \see ecp.h
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100189 const unsigned char *buf, size_t blen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
Manuel Pégourié-Gonnard3aeb5a72013-01-26 18:05:50 +0100191
192/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000193 * \brief This function computes the ECDSA signature and writes it
194 * to a buffer, serialized as defined in <em>RFC-4492:
195 * Elliptic Curve Cryptography (ECC) Cipher Suites for
196 * Transport Layer Security (TLS)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200197 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000198 * \warning It is not thread-safe to use the same context in
199 * multiple threads.
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200200 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000201 * \note The deterministic version is used if
202 * #MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
203 * information, see <em>RFC-6979: Deterministic Usage
204 * of the Digital Signature Algorithm (DSA) and Elliptic
205 * Curve Digital Signature Algorithm (ECDSA)</em>.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200206 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000207 * \param ctx The ECDSA context.
208 * \param md_alg The message digest that was used to hash the message.
209 * \param hash The message hash.
210 * \param hlen The length of the hash.
211 * \param sig The buffer that holds the signature.
212 * \param slen The length of the signature written.
213 * \param f_rng The RNG function.
214 * \param p_rng The RNG parameter.
215 *
Andrzej Kurek0044ab12018-02-20 11:18:21 -0500216 * \note The signature \p sig is expected to in be ASN.1 SEQUENCE
Andrzej Kurekbba09272018-02-14 07:16:27 -0500217 * format, as described in <em>Standards for Efficient
218 * Cryptography Group (SECG): SEC1 Elliptic Curve
219 * Cryptography</em>, section C.5.
220 *
221 * \note A \p sig buffer length of #MBEDTLS_ECDSA_MAX_LEN is
222 * always safe.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200223 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000224 * \note If the bitlength of the message hash is larger than the
225 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000226 * defined in <em>Standards for Efficient Cryptography Group
227 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
228 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000229 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000230 * \return \c 0 on success,
231 * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
232 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
233 *
234 * \see ecp.h
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200237 const unsigned char *hash, size_t hlen,
238 unsigned char *sig, size_t *slen,
239 int (*f_rng)(void *, unsigned char *, size_t),
240 void *p_rng );
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
243#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
244#if defined(MBEDTLS_DEPRECATED_WARNING)
245#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200246#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200248#endif
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100249/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000250 * \brief This function computes an ECDSA signature and writes it to a buffer,
251 * serialized as defined in <em>RFC-4492: Elliptic Curve Cryptography
252 * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>.
253 *
254 * The deterministic version is defined in <em>RFC-6979:
255 * Deterministic Usage of the Digital Signature Algorithm (DSA) and
256 * Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
257 *
258 * \warning It is not thread-safe to use the same context in
259 * multiple threads.
260
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100261 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0
Manuel Pégourié-Gonnarddfdcac92015-03-31 11:41:42 +0200263 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000264 * \param ctx The ECDSA context.
265 * \param hash The Message hash.
266 * \param hlen The length of the hash.
267 * \param sig The buffer that holds the signature.
268 * \param slen The length of the signature written.
269 * \param md_alg The MD algorithm used to hash the message.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100270 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000271 * \note The \p sig buffer must be at least twice as large as the
272 * size of the curve used, plus 9. For example, 73 Bytes if a
273 * 256-bit curve is used. A buffer length of
274 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100275 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000276 * \note If the bitlength of the message hash is larger than the
277 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000278 * defined in <em>Standards for Efficient Cryptography Group
279 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
280 * 4.1.3, step 5.
Janos Follath0a5154b2017-03-10 11:31:41 +0000281 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000282 * \return \c 0 on success,
283 * or an \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
284 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
285 *
286 * \see ecp.h
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100289 const unsigned char *hash, size_t hlen,
290 unsigned char *sig, size_t *slen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
292#undef MBEDTLS_DEPRECATED
293#endif /* MBEDTLS_DEPRECATED_REMOVED */
294#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Manuel Pégourié-Gonnard937340b2014-01-06 10:27:16 +0100295
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200296/**
Andrzej Kurek024ab062018-02-12 09:34:39 -0500297 * \brief Convert an ECDSA signature from number pair format to ASN.1
Gilles Peskinebce41d32017-11-02 17:14:18 +0100298 *
299 * \param r First number of the signature
300 * \param s Second number of the signature
301 * \param sig Buffer that will hold the signature
302 * \param slen Length of the signature written
303 * \param ssize Size of the sig buffer
304 *
305 * \note The size of the buffer \c ssize should be at least
Andrzej Kurekbba09272018-02-14 07:16:27 -0500306 * `MBEDTLS_ECDSA_MAX_SIG_LEN(grp->pbits)` bytes long if the
307 * signature was produced from curve \c grp, otherwise
308 * this function may fail with the error
309 * MBEDTLS_ERR_ASN1_BUF_TOO_SMALL.
Unknown6f21aed2018-02-07 08:02:31 -0500310 * The output ASN.1 SEQUENCE format is as follows:
311 * Ecdsa-Sig-Value ::= SEQUENCE {
312 * r INTEGER,
313 * s INTEGER
314 * }
Andrzej Kurekbba09272018-02-14 07:16:27 -0500315 * This format is expected by \c mbedtls_ecdsa_verify.
Gilles Peskinebce41d32017-11-02 17:14:18 +0100316 *
317 * \return 0 if successful,
318 * or a MBEDTLS_ERR_MPI_XXX or MBEDTLS_ERR_ASN1_XXX error code
319 *
320 */
Unknowna2c40622018-02-06 03:24:02 -0500321int mbedtls_ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
Gilles Peskinebce41d32017-11-02 17:14:18 +0100322 unsigned char *sig, size_t *slen,
323 size_t ssize );
324
325/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000326 * \brief This function reads and verifies an ECDSA signature.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200327 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000328 * \param ctx The ECDSA context.
329 * \param hash The message hash.
330 * \param hlen The size of the hash.
331 * \param sig The signature to read and verify.
332 * \param slen The size of \p sig.
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200333 *
Janos Follath0a5154b2017-03-10 11:31:41 +0000334 * \note If the bitlength of the message hash is larger than the
335 * bitlength of the group order, then the hash is truncated as
Rose Zadikbff87d92018-01-25 21:58:53 +0000336 * defined in <em>Standards for Efficient Cryptography Group
337 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
338 * 4.1.4, step 3.
Janos Follath0a5154b2017-03-10 11:31:41 +0000339 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000340 * \return \c 0 on success,
341 * #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
342 * #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is
343 * valid but its actual length is less than \p siglen,
344 * or an \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
345 * error code on failure for any other reason.
346 *
347 * \see ecp.h
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200350 const unsigned char *hash, size_t hlen,
351 const unsigned char *sig, size_t slen );
352
353/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000354 * \brief This function generates an ECDSA keypair on the given curve.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200355 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000356 * \param ctx The ECDSA context to store the keypair in.
357 * \param gid The elliptic curve to use. One of the various
358 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
359 * \param f_rng The RNG function.
360 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200361 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000362 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on
363 * failure.
364 *
365 * \see ecp.h
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
Manuel Pégourié-Gonnard8eebd012013-08-09 16:21:34 +0200368 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
369
370/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000371 * \brief This function sets an ECDSA context from an EC key pair.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200372 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000373 * \param ctx The ECDSA context to set.
374 * \param key The EC key to use.
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200375 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000376 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX code on
377 * failure.
378 *
379 * \see ecp.h
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
Manuel Pégourié-Gonnardf4999932013-08-12 17:02:59 +0200382
383/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000384 * \brief This function initializes an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200385 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000386 * \param ctx The ECDSA context to initialize.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200389
390/**
Rose Zadikbff87d92018-01-25 21:58:53 +0000391 * \brief This function frees an ECDSA context.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200392 *
Rose Zadikbff87d92018-01-25 21:58:53 +0000393 * \param ctx The ECDSA context to free.
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
Manuel Pégourié-Gonnard7c8934e2013-06-27 12:54:02 +0200396
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100397#ifdef __cplusplus
398}
399#endif
400
Paul Bakker9af723c2014-05-01 13:03:14 +0200401#endif /* ecdsa.h */