OPTIGA Trust M  1.1.0
C++ library for Optiga Trust M Chip Security Controller
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: 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  * This file is part of Mbed TLS (https://tls.mbed.org)
30  */
31 
32 #ifndef MBEDTLS_ECDSA_H
33 #define MBEDTLS_ECDSA_H
34 
35 #include "ecp.h"
36 #include "md.h"
37 
38 /*
39  * RFC-4492 page 20:
40  *
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  */
52 #if MBEDTLS_ECP_MAX_BYTES > 124
53 #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN"
54 #endif
55 
56 #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) )
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
70 
71 #if defined(MBEDTLS_ECP_RESTARTABLE)
72 
78 typedef struct mbedtls_ecdsa_restart_ver mbedtls_ecdsa_restart_ver_ctx;
79 
85 typedef struct mbedtls_ecdsa_restart_sig mbedtls_ecdsa_restart_sig_ctx;
86 
87 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
88 
93 typedef struct mbedtls_ecdsa_restart_det mbedtls_ecdsa_restart_det_ctx;
94 #endif
95 
99 typedef struct
100 {
103  mbedtls_ecdsa_restart_ver_ctx *ver;
104  mbedtls_ecdsa_restart_sig_ctx *sig;
105 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
106  mbedtls_ecdsa_restart_det_ctx *det;
107 #endif
109 
110 #else /* MBEDTLS_ECP_RESTARTABLE */
111 
112 /* Now we can declare functions that take a pointer to that */
114 
115 #endif /* MBEDTLS_ECP_RESTARTABLE */
116 
154  const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
155  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
156 
157 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
158 
193 int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r,
194  mbedtls_mpi *s, const mbedtls_mpi *d,
195  const unsigned char *buf, size_t blen,
196  mbedtls_md_type_t md_alg );
197 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
198 
232  const unsigned char *buf, size_t blen,
233  const mbedtls_ecp_point *Q, const mbedtls_mpi *r,
234  const mbedtls_mpi *s);
235 
284  mbedtls_md_type_t md_alg,
285  const unsigned char *hash, size_t hlen,
286  unsigned char *sig, size_t *slen,
287  int (*f_rng)(void *, unsigned char *, size_t),
288  void *p_rng );
289 
330  mbedtls_md_type_t md_alg,
331  const unsigned char *hash, size_t hlen,
332  unsigned char *sig, size_t *slen,
333  int (*f_rng)(void *, unsigned char *, size_t),
334  void *p_rng,
335  mbedtls_ecdsa_restart_ctx *rs_ctx );
336 
337 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
338 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
339 #if defined(MBEDTLS_DEPRECATED_WARNING)
340 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
341 #else
342 #define MBEDTLS_DEPRECATED
343 #endif
344 
387 int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
388  const unsigned char *hash, size_t hlen,
389  unsigned char *sig, size_t *slen,
391 #undef MBEDTLS_DEPRECATED
392 #endif /* MBEDTLS_DEPRECATED_REMOVED */
393 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
394 
423  const unsigned char *hash, size_t hlen,
424  const unsigned char *sig, size_t slen );
425 
458  const unsigned char *hash, size_t hlen,
459  const unsigned char *sig, size_t slen,
460  mbedtls_ecdsa_restart_ctx *rs_ctx );
461 
479  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
480 
497  const mbedtls_ecp_keypair *key );
498 
506 
515 
516 #if defined(MBEDTLS_ECP_RESTARTABLE)
517 
523 void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
524 
532 void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
533 #endif /* MBEDTLS_ECP_RESTARTABLE */
534 
535 #ifdef __cplusplus
536 }
537 #endif
538 
539 #endif /* ecdsa.h */
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
This function frees an ECDSA context.
#define MBEDTLS_DEPRECATED
Definition: aes.h:625
This file provides an API for Elliptic Curves over GF(P) (ECP).
The ECP key-pair structure.
Definition: ecp.h:326
void mbedtls_ecdsa_restart_ctx
Definition: ecdsa.h:113
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:167
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:252
mbedtls_ecp_keypair mbedtls_ecdsa_context
The ECDSA context structure.
Definition: ecdsa.h:69
mbedtls_ecp_group_id
Definition: ecp.h:71
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:180
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:118
mbedtls_md_type_t
Supported message digests.
Definition: md.h:58
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...