Mbed OS Reference
Loading...
Searching...
No Matches
ecdsa.h
Go to the documentation of this file.
1/**
2 * \file ecdsa.h
3 *
4 * \brief This file contains ECDSA definitions and functions.
5 *
6 * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in
7 * <em>Standards for Efficient Cryptography Group (SECG):
8 * 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 *
12 */
13/*
14 * Copyright The Mbed TLS Contributors
15 * 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.
28 */
29
30#ifndef MBEDTLS_ECDSA_H
31#define MBEDTLS_ECDSA_H
32
33#if !defined(MBEDTLS_CONFIG_FILE)
34#include "mbedtls/config.h"
35#else
36#include MBEDTLS_CONFIG_FILE
37#endif
38
39#include "mbedtls/ecp.h"
40#include "mbedtls/md.h"
41
42/**
43 * \addtogroup mbedtls
44 * \{
45 * \defgroup mbedtls_ecdsa_module ECDSA
46 * \{
47 */
48
49/**
50 * \brief Maximum ECDSA signature size for a given curve bit size
51 *
52 * \param bits Curve size in bits
53 * \return Maximum signature size in bytes
54 *
55 * \note This macro returns a compile-time constant if its argument
56 * is one. It may evaluate its argument multiple times.
57 */
58/*
59 * Ecdsa-Sig-Value ::= SEQUENCE {
60 * r INTEGER,
61 * s INTEGER
62 * }
63 *
64 * For each of r and s, the value (V) may include an extra initial "0" bit.
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/** The maximal size of an ECDSA signature in Bytes. */
72#define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78/**
79 * \brief The ECDSA context structure.
80 *
81 * \warning Performing multiple operations concurrently on the same
82 * ECDSA context is not supported; objects of this type
83 * should not be shared between multiple threads.
84 */
86
87#if defined(MBEDTLS_ECP_RESTARTABLE)
88
89/**
90 * \brief Internal restart context for ecdsa_verify()
91 *
92 * \note Opaque struct, defined in ecdsa.c
93 */
94typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
95
96/**
97 * \brief Internal restart context for ecdsa_sign()
98 *
99 * \note Opaque struct, defined in ecdsa.c
100 */
101typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
102
103#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
104/**
105 * \brief Internal restart context for ecdsa_sign_det()
106 *
107 * \note Opaque struct, defined in ecdsa.c
108 */
109typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
110#endif
111
112/**
113 * \brief General context for resuming ECDSA operations
114 */
115typedef struct
116{
117 mbedtls_ecp_restart_ctx ecp; /*!< base context for ECP restart and
118 shared administrative info */
119 mbedtls_ecdsa_restart_ver_ctx *ver; /*!< ecdsa_verify() sub-context */
120 mbedtls_ecdsa_restart_sig_ctx *sig; /*!< ecdsa_sign() sub-context */
121#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
122 mbedtls_ecdsa_restart_det_ctx *det; /*!< ecdsa_sign_det() sub-context */
123#endif
124} mbedtls_ecdsa_restart_ctx;
125
126#else /* MBEDTLS_ECP_RESTARTABLE */
127
128/* Now we can declare functions that take a pointer to that */
129typedef void mbedtls_ecdsa_restart_ctx;
130
131#endif /* MBEDTLS_ECP_RESTARTABLE */
132
133/**
134 * \brief This function checks whether a given group can be used
135 * for ECDSA.
136 *
137 * \param gid The ECP group ID to check.
138 *
139 * \return \c 1 if the group can be used, \c 0 otherwise
140 */
142
143/**
144 * \brief This function computes the ECDSA signature of a
145 * previously-hashed message.
146 *
147 * \note The deterministic version implemented in
148 * mbedtls_ecdsa_sign_det() is usually preferred.
149 *
150 * \note If the bitlength of the message hash is larger than the
151 * bitlength of the group order, then the hash is truncated
152 * as defined in <em>Standards for Efficient Cryptography Group
153 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
154 * 4.1.3, step 5.
155 *
156 * \see ecp.h
157 *
158 * \param grp The context for the elliptic curve to use.
159 * This must be initialized and have group parameters
160 * set, for example through mbedtls_ecp_group_load().
161 * \param r The MPI context in which to store the first part
162 * the signature. This must be initialized.
163 * \param s The MPI context in which to store the second part
164 * the signature. This must be initialized.
165 * \param d The private signing key. This must be initialized.
166 * \param buf The content to be signed. This is usually the hash of
167 * the original data to be signed. This must be a readable
168 * buffer of length \p blen Bytes. It may be \c NULL if
169 * \p blen is zero.
170 * \param blen The length of \p buf in Bytes.
171 * \param f_rng The RNG function. This must not be \c NULL.
172 * \param p_rng The RNG context to be passed to \p f_rng. This may be
173 * \c NULL if \p f_rng doesn't need a context parameter.
174 *
175 * \return \c 0 on success.
176 * \return An \c MBEDTLS_ERR_ECP_XXX
177 * or \c MBEDTLS_MPI_XXX error code on failure.
178 */
180 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
181 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
182
183#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
184#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
185#if defined(MBEDTLS_DEPRECATED_WARNING)
186#define MBEDTLS_DEPRECATED __attribute__((deprecated))
187#else
188#define MBEDTLS_DEPRECATED
189#endif
190/**
191 * \brief This function computes the ECDSA signature of a
192 * previously-hashed message, deterministic version.
193 *
194 * For more information, see <em>RFC-6979: Deterministic
195 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
196 * Curve Digital Signature Algorithm (ECDSA)</em>.
197 *
198 * \note If the bitlength of the message hash is larger than the
199 * bitlength of the group order, then the hash is truncated as
200 * defined in <em>Standards for Efficient Cryptography Group
201 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
202 * 4.1.3, step 5.
203 *
204 * \warning Since the output of the internal RNG is always the same for
205 * the same key and message, this limits the efficiency of
206 * blinding and leaks information through side channels. For
207 * secure behavior use mbedtls_ecdsa_sign_det_ext() instead.
208 *
209 * (Optimally the blinding is a random value that is different
210 * on every execution. In this case the blinding is still
211 * random from the attackers perspective, but is the same on
212 * each execution. This means that this blinding does not
213 * prevent attackers from recovering secrets by combining
214 * several measurement traces, but may prevent some attacks
215 * that exploit relationships between secret data.)
216 *
217 * \see ecp.h
218 *
219 * \param grp The context for the elliptic curve to use.
220 * This must be initialized and have group parameters
221 * set, for example through mbedtls_ecp_group_load().
222 * \param r The MPI context in which to store the first part
223 * the signature. This must be initialized.
224 * \param s The MPI context in which to store the second part
225 * the signature. This must be initialized.
226 * \param d The private signing key. This must be initialized
227 * and setup, for example through mbedtls_ecp_gen_privkey().
228 * \param buf The hashed content to be signed. This must be a readable
229 * buffer of length \p blen Bytes. It may be \c NULL if
230 * \p blen is zero.
231 * \param blen The length of \p buf in Bytes.
232 * \param md_alg The hash algorithm used to hash the original data.
233 *
234 * \return \c 0 on success.
235 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
236 * error code on failure.
237 */
238int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
239 mbedtls_mpi *s, const mbedtls_mpi *d,
240 const unsigned char *buf, size_t blen,
241 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
242#undef MBEDTLS_DEPRECATED
243#endif /* MBEDTLS_DEPRECATED_REMOVED */
244
245/**
246 * \brief This function computes the ECDSA signature of a
247 * previously-hashed message, deterministic version.
248 *
249 * For more information, see <em>RFC-6979: Deterministic
250 * Usage of the Digital Signature Algorithm (DSA) and Elliptic
251 * Curve Digital Signature Algorithm (ECDSA)</em>.
252 *
253 * \note If the bitlength of the message hash is larger than the
254 * bitlength of the group order, then the hash is truncated as
255 * defined in <em>Standards for Efficient Cryptography Group
256 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
257 * 4.1.3, step 5.
258 *
259 * \see ecp.h
260 *
261 * \param grp The context for the elliptic curve to use.
262 * This must be initialized and have group parameters
263 * set, for example through mbedtls_ecp_group_load().
264 * \param r The MPI context in which to store the first part
265 * the signature. This must be initialized.
266 * \param s The MPI context in which to store the second part
267 * the signature. This must be initialized.
268 * \param d The private signing key. This must be initialized
269 * and setup, for example through mbedtls_ecp_gen_privkey().
270 * \param buf The hashed content to be signed. This must be a readable
271 * buffer of length \p blen Bytes. It may be \c NULL if
272 * \p blen is zero.
273 * \param blen The length of \p buf in Bytes.
274 * \param md_alg The hash algorithm used to hash the original data.
275 * \param f_rng_blind The RNG function used for blinding. This must not be
276 * \c NULL.
277 * \param p_rng_blind The RNG context to be passed to \p f_rng. This may be
278 * \c NULL if \p f_rng doesn't need a context parameter.
279 *
280 * \return \c 0 on success.
281 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
282 * error code on failure.
283 */
284int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
285 mbedtls_mpi *s, const mbedtls_mpi *d,
286 const unsigned char *buf, size_t blen,
287 mbedtls_md_type_t md_alg,
288 int (*f_rng_blind)(void *, unsigned char *, size_t),
289 void *p_rng_blind );
290#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
291
292/**
293 * \brief This function verifies the ECDSA signature of a
294 * previously-hashed message.
295 *
296 * \note If the bitlength of the message hash is larger than the
297 * bitlength of the group order, then the hash is truncated as
298 * defined in <em>Standards for Efficient Cryptography Group
299 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
300 * 4.1.4, step 3.
301 *
302 * \see ecp.h
303 *
304 * \param grp The ECP group to use.
305 * This must be initialized and have group parameters
306 * set, for example through mbedtls_ecp_group_load().
307 * \param buf The hashed content that was signed. This must be a readable
308 * buffer of length \p blen Bytes. It may be \c NULL if
309 * \p blen is zero.
310 * \param blen The length of \p buf in Bytes.
311 * \param Q The public key to use for verification. This must be
312 * initialized and setup.
313 * \param r The first integer of the signature.
314 * This must be initialized.
315 * \param s The second integer of the signature.
316 * This must be initialized.
317 *
318 * \return \c 0 on success.
319 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature
320 * is invalid.
321 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX
322 * error code on failure for any other reason.
323 */
325 const unsigned char *buf, size_t blen,
326 const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
327 const mbedtls_mpi *s);
328
329/**
330 * \brief This function computes the ECDSA signature and writes it
331 * to a buffer, serialized as defined in <em>RFC-4492:
332 * Elliptic Curve Cryptography (ECC) Cipher Suites for
333 * Transport Layer Security (TLS)</em>.
334 *
335 * \warning It is not thread-safe to use the same context in
336 * multiple threads.
337 *
338 * \note The deterministic version is used if
339 * MBEDTLS_ECDSA_DETERMINISTIC is defined. For more
340 * information, see <em>RFC-6979: Deterministic Usage
341 * of the Digital Signature Algorithm (DSA) and Elliptic
342 * Curve Digital Signature Algorithm (ECDSA)</em>.
343 *
344 * \note If the bitlength of the message hash is larger than the
345 * bitlength of the group order, then the hash is truncated as
346 * defined in <em>Standards for Efficient Cryptography Group
347 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
348 * 4.1.3, step 5.
349 *
350 * \see ecp.h
351 *
352 * \param ctx The ECDSA context to use. This must be initialized
353 * and have a group and private key bound to it, for example
354 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
355 * \param md_alg The message digest that was used to hash the message.
356 * \param hash The message hash to be signed. This must be a readable
357 * buffer of length \p blen Bytes.
358 * \param hlen The length of the hash \p hash in Bytes.
359 * \param sig The buffer to which to write the signature. This must be a
360 * writable buffer of length at least twice as large as the
361 * size of the curve used, plus 9. For example, 73 Bytes if
362 * a 256-bit curve is used. A buffer length of
363 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
364 * \param slen The address at which to store the actual length of
365 * the signature written. Must not be \c NULL.
366 * \param f_rng The RNG function. This must not be \c NULL if
367 * MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
368 * it is used only for blinding and may be set to \c NULL, but
369 * doing so is DEPRECATED.
370 * \param p_rng The RNG context to be passed to \p f_rng. This may be
371 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
372 *
373 * \return \c 0 on success.
374 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
375 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
376 */
378 mbedtls_md_type_t md_alg,
379 const unsigned char *hash, size_t hlen,
380 unsigned char *sig, size_t *slen,
381 int (*f_rng)(void *, unsigned char *, size_t),
382 void *p_rng );
383
384/**
385 * \brief This function computes the ECDSA signature and writes it
386 * to a buffer, in a restartable way.
387 *
388 * \see \c mbedtls_ecdsa_write_signature()
389 *
390 * \note This function is like \c mbedtls_ecdsa_write_signature()
391 * but it can return early and restart according to the limit
392 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
393 *
394 * \param ctx The ECDSA context to use. This must be initialized
395 * and have a group and private key bound to it, for example
396 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
397 * \param md_alg The message digest that was used to hash the message.
398 * \param hash The message hash to be signed. This must be a readable
399 * buffer of length \p blen Bytes.
400 * \param hlen The length of the hash \p hash in Bytes.
401 * \param sig The buffer to which to write the signature. This must be a
402 * writable buffer of length at least twice as large as the
403 * size of the curve used, plus 9. For example, 73 Bytes if
404 * a 256-bit curve is used. A buffer length of
405 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
406 * \param slen The address at which to store the actual length of
407 * the signature written. Must not be \c NULL.
408 * \param f_rng The RNG function. This must not be \c NULL if
409 * MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise,
410 * it is unused and may be set to \c NULL.
411 * \param p_rng The RNG context to be passed to \p f_rng. This may be
412 * \c NULL if \p f_rng is \c NULL or doesn't use a context.
413 * \param rs_ctx The restart context to use. This may be \c NULL to disable
414 * restarting. If it is not \c NULL, it must point to an
415 * initialized restart context.
416 *
417 * \return \c 0 on success.
418 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
419 * operations was reached: see \c mbedtls_ecp_set_max_ops().
420 * \return Another \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
421 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
422 */
424 mbedtls_md_type_t md_alg,
425 const unsigned char *hash, size_t hlen,
426 unsigned char *sig, size_t *slen,
427 int (*f_rng)(void *, unsigned char *, size_t),
428 void *p_rng,
429 mbedtls_ecdsa_restart_ctx *rs_ctx );
430
431#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
432#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
433#if defined(MBEDTLS_DEPRECATED_WARNING)
434#define MBEDTLS_DEPRECATED __attribute__((deprecated))
435#else
436#define MBEDTLS_DEPRECATED
437#endif
438/**
439 * \brief This function computes an ECDSA signature and writes
440 * it to a buffer, serialized as defined in <em>RFC-4492:
441 * Elliptic Curve Cryptography (ECC) Cipher Suites for
442 * Transport Layer Security (TLS)</em>.
443 *
444 * The deterministic version is defined in <em>RFC-6979:
445 * Deterministic Usage of the Digital Signature Algorithm (DSA)
446 * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>.
447 *
448 * \warning It is not thread-safe to use the same context in
449 * multiple threads.
450 *
451 * \note If the bitlength of the message hash is larger than the
452 * bitlength of the group order, then the hash is truncated as
453 * defined in <em>Standards for Efficient Cryptography Group
454 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
455 * 4.1.3, step 5.
456 *
457 * \see ecp.h
458 *
459 * \deprecated Superseded by mbedtls_ecdsa_write_signature() in
460 * Mbed TLS version 2.0 and later.
461 *
462 * \param ctx The ECDSA context to use. This must be initialized
463 * and have a group and private key bound to it, for example
464 * via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair().
465 * \param hash The message hash to be signed. This must be a readable
466 * buffer of length \p blen Bytes.
467 * \param hlen The length of the hash \p hash in Bytes.
468 * \param sig The buffer to which to write the signature. This must be a
469 * writable buffer of length at least twice as large as the
470 * size of the curve used, plus 9. For example, 73 Bytes if
471 * a 256-bit curve is used. A buffer length of
472 * #MBEDTLS_ECDSA_MAX_LEN is always safe.
473 * \param slen The address at which to store the actual length of
474 * the signature written. Must not be \c NULL.
475 * \param md_alg The message digest that was used to hash the message.
476 *
477 * \return \c 0 on success.
478 * \return An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or
479 * \c MBEDTLS_ERR_ASN1_XXX error code on failure.
480 */
481int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
482 const unsigned char *hash, size_t hlen,
483 unsigned char *sig, size_t *slen,
484 mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED;
485#undef MBEDTLS_DEPRECATED
486#endif /* MBEDTLS_DEPRECATED_REMOVED */
487#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
488
489/**
490 * \brief This function reads and verifies an ECDSA signature.
491 *
492 * \note If the bitlength of the message hash is larger than the
493 * bitlength of the group order, then the hash is truncated as
494 * defined in <em>Standards for Efficient Cryptography Group
495 * (SECG): SEC1 Elliptic Curve Cryptography</em>, section
496 * 4.1.4, step 3.
497 *
498 * \see ecp.h
499 *
500 * \param ctx The ECDSA context to use. This must be initialized
501 * and have a group and public key bound to it.
502 * \param hash The message hash that was signed. This must be a readable
503 * buffer of length \p size Bytes.
504 * \param hlen The size of the hash \p hash.
505 * \param sig The signature to read and verify. This must be a readable
506 * buffer of length \p slen Bytes.
507 * \param slen The size of \p sig in Bytes.
508 *
509 * \return \c 0 on success.
510 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
511 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
512 * signature in \p sig, but its length is less than \p siglen.
513 * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
514 * error code on failure for any other reason.
515 */
517 const unsigned char *hash, size_t hlen,
518 const unsigned char *sig, size_t slen );
519
520/**
521 * \brief This function reads and verifies an ECDSA signature,
522 * in a restartable way.
523 *
524 * \see \c mbedtls_ecdsa_read_signature()
525 *
526 * \note This function is like \c mbedtls_ecdsa_read_signature()
527 * but it can return early and restart according to the limit
528 * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
529 *
530 * \param ctx The ECDSA context to use. This must be initialized
531 * and have a group and public key bound to it.
532 * \param hash The message hash that was signed. This must be a readable
533 * buffer of length \p size Bytes.
534 * \param hlen The size of the hash \p hash.
535 * \param sig The signature to read and verify. This must be a readable
536 * buffer of length \p slen Bytes.
537 * \param slen The size of \p sig in Bytes.
538 * \param rs_ctx The restart context to use. This may be \c NULL to disable
539 * restarting. If it is not \c NULL, it must point to an
540 * initialized restart context.
541 *
542 * \return \c 0 on success.
543 * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid.
544 * \return #MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if there is a valid
545 * signature in \p sig, but its length is less than \p siglen.
546 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
547 * operations was reached: see \c mbedtls_ecp_set_max_ops().
548 * \return Another \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_ERR_MPI_XXX
549 * error code on failure for any other reason.
550 */
552 const unsigned char *hash, size_t hlen,
553 const unsigned char *sig, size_t slen,
554 mbedtls_ecdsa_restart_ctx *rs_ctx );
555
556/**
557 * \brief This function generates an ECDSA keypair on the given curve.
558 *
559 * \see ecp.h
560 *
561 * \param ctx The ECDSA context to store the keypair in.
562 * This must be initialized.
563 * \param gid The elliptic curve to use. One of the various
564 * \c MBEDTLS_ECP_DP_XXX macros depending on configuration.
565 * \param f_rng The RNG function to use. This must not be \c NULL.
566 * \param p_rng The RNG context to be passed to \p f_rng. This may be
567 * \c NULL if \p f_rng doesn't need a context argument.
568 *
569 * \return \c 0 on success.
570 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
571 */
573 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
574
575/**
576 * \brief This function sets up an ECDSA context from an EC key pair.
577 *
578 * \see ecp.h
579 *
580 * \param ctx The ECDSA context to setup. This must be initialized.
581 * \param key The EC key to use. This must be initialized and hold
582 * a private-public key pair or a public key. In the former
583 * case, the ECDSA context may be used for signature creation
584 * and verification after this call. In the latter case, it
585 * may be used for signature verification.
586 *
587 * \return \c 0 on success.
588 * \return An \c MBEDTLS_ERR_ECP_XXX code on failure.
589 */
591 const mbedtls_ecp_keypair *key );
592
593/**
594 * \brief This function initializes an ECDSA context.
595 *
596 * \param ctx The ECDSA context to initialize.
597 * This must not be \c NULL.
598 */
600
601/**
602 * \brief This function frees an ECDSA context.
603 *
604 * \param ctx The ECDSA context to free. This may be \c NULL,
605 * in which case this function does nothing. If it
606 * is not \c NULL, it must be initialized.
607 */
609
610#if defined(MBEDTLS_ECP_RESTARTABLE)
611/**
612 * \brief Initialize a restart context.
613 *
614 * \param ctx The restart context to initialize.
615 * This must not be \c NULL.
616 */
617void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
618
619/**
620 * \brief Free the components of a restart context.
621 *
622 * \param ctx The restart context to free. This may be \c NULL,
623 * in which case this function does nothing. If it
624 * is not \c NULL, it must be initialized.
625 */
626void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
627#endif /* MBEDTLS_ECP_RESTARTABLE */
628
629/// \}
630/// \}
631
632#ifdef __cplusplus
633}
634#endif
635
636#endif /* ecdsa.h */
Configuration options (set of defines)
int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hlen, unsigned char *sig, size_t *slen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_ecdsa_restart_ctx *rs_ctx)
This function computes the ECDSA signature and writes it to a buffer, in a restartable way.
int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp, const unsigned char *buf, size_t blen, const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
This function verifies the ECDSA signature of a previously-hashed message.
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
This function frees an ECDSA context.
int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
This function sets up an ECDSA context from an EC key pair.
int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hlen, unsigned char *sig, size_t *slen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the ECDSA signature and writes it to a buffer, serialized as defined in RFC-44...
int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx, const unsigned char *hash, size_t hlen, const unsigned char *sig, size_t slen, mbedtls_ecdsa_restart_ctx *rs_ctx)
This function reads and verifies an ECDSA signature, in a restartable way.
int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx, const unsigned char *hash, size_t hlen, const unsigned char *sig, size_t slen)
This function reads and verifies an ECDSA signature.
mbedtls_ecp_keypair mbedtls_ecdsa_context
The ECDSA context structure.
Definition: ecdsa.h:85
int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function generates an ECDSA keypair on the given curve.
int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
This function checks whether a given group can be used for ECDSA.
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
This function initializes an ECDSA context.
int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, const mbedtls_mpi *d, const unsigned char *buf, size_t blen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
This function computes the ECDSA signature of a previously-hashed message.
mbedtls_ecp_group_id
Domain-parameter identifiers: curve, subgroup, and generator.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:64
This file provides an API for Elliptic Curves over GF(P) (ECP).
This file contains the generic message-digest wrapper.
The ECP group structure.
The ECP key-pair structure.
The ECP point structure, in Jacobian coordinates.
MPI structure.
Definition: bignum.h:185