mbed TLS v2.16.1
ecdsa.h
Go to the documentation of this file.
1 
13 /*
14  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
15  * SPDX-License-Identifier: GPL-2.0
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, write to the Free Software Foundation, Inc.,
29  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30  *
31  * This file is part of Mbed TLS (https://tls.mbed.org)
32  */
33 
34 #ifndef MBEDTLS_ECDSA_H
35 #define MBEDTLS_ECDSA_H
36 
37 #if !defined(MBEDTLS_CONFIG_FILE)
38 #include "config.h"
39 #else
40 #include MBEDTLS_CONFIG_FILE
41 #endif
42 
43 #include "ecp.h"
44 #include "md.h"
45 
46 /*
47  * RFC-4492 page 20:
48  *
49  * Ecdsa-Sig-Value ::= SEQUENCE {
50  * r INTEGER,
51  * s INTEGER
52  * }
53  *
54  * Size is at most
55  * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s,
56  * twice that + 1 (tag) + 2 (len) for the sequence
57  * (assuming ECP_MAX_BYTES is less than 126 for r and s,
58  * and less than 124 (total len <= 255) for the sequence)
59  */
60 #if MBEDTLS_ECP_MAX_BYTES > 124
61 #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
62 #endif
63 
64 #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
78 
79 #if defined(MBEDTLS_ECP_RESTARTABLE)
80 
86 typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
87 
93 typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
94 
95 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
96 
101 typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
102 #endif
103 
107 typedef struct
108 {
111  mbedtls_ecdsa_restart_ver_ctx *ver;
112  mbedtls_ecdsa_restart_sig_ctx *sig;
113 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
114  mbedtls_ecdsa_restart_det_ctx *det;
115 #endif
117 
118 #else /* MBEDTLS_ECP_RESTARTABLE */
119 
120 /* Now we can declare functions that take a pointer to that */
122 
123 #endif /* MBEDTLS_ECP_RESTARTABLE */
124 
162  const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
163  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
164 
165 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
166 
202  mbedtls_mpi *s, const mbedtls_mpi *d,
203  const unsigned char *buf, size_t blen,
204  mbedtls_md_type_t md_alg );
205 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
206 
240  const unsigned char *buf, size_t blen,
241  const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
242  const mbedtls_mpi *s);
243 
291 int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
292  mbedtls_md_type_t md_alg,
293  const unsigned char *hash, size_t hlen,
294  unsigned char *sig, size_t *slen,
295  int (*f_rng)(void *, unsigned char *, size_t),
296  void *p_rng );
297 
337 int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
338  mbedtls_md_type_t md_alg,
339  const unsigned char *hash, size_t hlen,
340  unsigned char *sig, size_t *slen,
341  int (*f_rng)(void *, unsigned char *, size_t),
342  void *p_rng,
343  mbedtls_ecdsa_restart_ctx *rs_ctx );
344 
345 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
346 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
347 #if defined(MBEDTLS_DEPRECATED_WARNING)
348 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
349 #else
350 #define MBEDTLS_DEPRECATED
351 #endif
352 
395 int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
396  const unsigned char *hash, size_t hlen,
397  unsigned char *sig, size_t *slen,
399 #undef MBEDTLS_DEPRECATED
400 #endif /* MBEDTLS_DEPRECATED_REMOVED */
401 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
402 
430 int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
431  const unsigned char *hash, size_t hlen,
432  const unsigned char *sig, size_t slen );
433 
465 int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
466  const unsigned char *hash, size_t hlen,
467  const unsigned char *sig, size_t slen,
468  mbedtls_ecdsa_restart_ctx *rs_ctx );
469 
486 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
487  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
488 
504 int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
505  const mbedtls_ecp_keypair *key );
506 
513 void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
514 
522 void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
523 
524 #if defined(MBEDTLS_ECP_RESTARTABLE)
525 
531 void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
532 
540 void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
541 #endif /* MBEDTLS_ECP_RESTARTABLE */
542 
543 #ifdef __cplusplus
544 }
545 #endif
546 
547 #endif /* ecdsa.h */
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
This function frees an ECDSA context.
This file provides an API for Elliptic Curves over GF(P) (ECP).
The ECP key-pair structure.
Definition: ecp.h:334
Configuration options (set of defines)
void mbedtls_ecdsa_restart_ctx
Definition: ecdsa.h:121
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
This function initializes an ECDSA context.
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.
The ECP group structure.
Definition: ecp.h:175
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.
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_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...
void mbedtls_ecp_restart_ctx
Definition: ecp.h:322
int mbedtls_ecdsa_sign_det(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, const mbedtls_mpi *d, const unsigned char *buf, size_t blen, mbedtls_md_type_t md_alg)
This function computes the ECDSA signature of a previously-hashed message, deterministic version...
mbedtls_ecp_keypair mbedtls_ecdsa_context
The ECDSA context structure.
Definition: ecdsa.h:77
mbedtls_ecp_group_id
Definition: ecp.h:79
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_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.
This file contains the generic message-digest wrapper.
int mbedtls_ecdsa_write_signature_det(mbedtls_ecdsa_context *ctx, const unsigned char *hash, size_t hlen, unsigned char *sig, size_t *slen, mbedtls_md_type_t md_alg) MBEDTLS_DEPRECATED
This function computes an ECDSA signature and writes it to a buffer, serialized as defined in RFC-449...
MPI structure.
Definition: bignum.h:182
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.
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:126
mbedtls_md_type_t
Supported message digests.
Definition: md.h:60
#define MBEDTLS_DEPRECATED
Definition: ecdsa.h:350
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...