mbed TLS v3.1.0
ecdsa.h
Go to the documentation of this file.
1 
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 #include "mbedtls/private_access.h"
33 
34 #include "mbedtls/build_info.h"
35 
36 #include "mbedtls/ecp.h"
37 #include "mbedtls/md.h"
38 
48 /*
49  * Ecdsa-Sig-Value ::= SEQUENCE {
50  * r INTEGER,
51  * s INTEGER
52  * }
53  *
54  * For each of r and s, the value (V) may include an extra initial "0" bit.
55  */
56 #define MBEDTLS_ECDSA_MAX_SIG_LEN( bits ) \
57  ( /*T,L of SEQUENCE*/ ( ( bits ) >= 61 * 8 ? 3 : 2 ) + \
58  /*T,L of r,s*/ 2 * ( ( ( bits ) >= 127 * 8 ? 3 : 2 ) + \
59  /*V of r,s*/ ( ( bits ) + 8 ) / 8 ) )
60 
62 #define MBEDTLS_ECDSA_MAX_LEN MBEDTLS_ECDSA_MAX_SIG_LEN( MBEDTLS_ECP_MAX_BITS )
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
76 
77 #if defined(MBEDTLS_ECP_RESTARTABLE)
78 
84 typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
85 
91 typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
92 
93 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
94 
99 typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
100 #endif
101 
105 typedef struct
106 {
111 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
113 #endif
115 
116 #else /* MBEDTLS_ECP_RESTARTABLE */
117 
118 /* Now we can declare functions that take a pointer to that */
119 typedef void mbedtls_ecdsa_restart_ctx;
120 
121 #endif /* MBEDTLS_ECP_RESTARTABLE */
122 
132 
169 int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
170  const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
171  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
172 
173 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
174 
213 int mbedtls_ecdsa_sign_det_ext( mbedtls_ecp_group *grp, mbedtls_mpi *r,
214  mbedtls_mpi *s, const mbedtls_mpi *d,
215  const unsigned char *buf, size_t blen,
216  mbedtls_md_type_t md_alg,
217  int (*f_rng_blind)(void *, unsigned char *, size_t),
218  void *p_rng_blind );
219 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
220 
253 int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
254  const unsigned char *buf, size_t blen,
255  const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
256  const mbedtls_mpi *s);
257 
307 int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx,
308  mbedtls_md_type_t md_alg,
309  const unsigned char *hash, size_t hlen,
310  unsigned char *sig, size_t sig_size, size_t *slen,
311  int (*f_rng)(void *, unsigned char *, size_t),
312  void *p_rng );
313 
354 int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
355  mbedtls_md_type_t md_alg,
356  const unsigned char *hash, size_t hlen,
357  unsigned char *sig, size_t sig_size, size_t *slen,
358  int (*f_rng)(void *, unsigned char *, size_t),
359  void *p_rng,
360  mbedtls_ecdsa_restart_ctx *rs_ctx );
361 
389 int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
390  const unsigned char *hash, size_t hlen,
391  const unsigned char *sig, size_t slen );
392 
424 int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
425  const unsigned char *hash, size_t hlen,
426  const unsigned char *sig, size_t slen,
427  mbedtls_ecdsa_restart_ctx *rs_ctx );
428 
445 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
446  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
447 
463 int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx,
464  const mbedtls_ecp_keypair *key );
465 
472 void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
473 
481 void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
482 
483 #if defined(MBEDTLS_ECP_RESTARTABLE)
484 
490 void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
491 
499 void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
500 #endif /* MBEDTLS_ECP_RESTARTABLE */
501 
502 #ifdef __cplusplus
503 }
504 #endif
505 
506 #endif /* ecdsa.h */
struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx
Internal restart context for ecdsa_sign()
Definition: ecdsa.h:91
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:421
struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx
Internal restart context for ecdsa_verify()
Definition: ecdsa.h:84
#define MBEDTLS_PRIVATE(member)
void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx)
Free the components of a restart context.
General context for resuming ECDSA operations.
Definition: ecdsa.h:105
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.
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.
struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx
Internal restart context for ecdsa_sign_det()
Definition: ecdsa.h:99
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.
Macro wrapper for struct's memebrs.
int mbedtls_ecdsa_sign_det_ext(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, int(*f_rng_blind)(void *, unsigned char *, size_t), void *p_rng_blind)
This function computes the ECDSA signature of a previously-hashed message, deterministic version...
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_restart_init(mbedtls_ecdsa_restart_ctx *ctx)
Initialize a restart context.
mbedtls_ecp_keypair mbedtls_ecdsa_context
The ECDSA context structure.
Definition: ecdsa.h:75
mbedtls_ecp_group_id
Definition: ecp.h:113
Build-time configuration info.
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.
MPI structure.
Definition: bignum.h:189
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.
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 sig_size, 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_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 sig_size, 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...
General context for resuming ECC operations.
Definition: ecp.h:368
The ECP point structure, in Jacobian coordinates.
Definition: ecp.h:172
mbedtls_md_type_t
Supported message digests.
Definition: md.h:55