mbed TLS v2.14.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: 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 
145  const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
146  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
147 
148 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
149 
178  const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
179  mbedtls_md_type_t md_alg );
180 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
181 
208  const unsigned char *buf, size_t blen,
209  const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s);
210 
252 int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
253  const unsigned char *hash, size_t hlen,
254  unsigned char *sig, size_t *slen,
255  int (*f_rng)(void *, unsigned char *, size_t),
256  void *p_rng );
257 
284 int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx,
285  mbedtls_md_type_t md_alg,
286  const unsigned char *hash, size_t hlen,
287  unsigned char *sig, size_t *slen,
288  int (*f_rng)(void *, unsigned char *, size_t),
289  void *p_rng,
290  mbedtls_ecdsa_restart_ctx *rs_ctx );
291 
292 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
293 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
294 #if defined(MBEDTLS_DEPRECATED_WARNING)
295 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
296 #else
297 #define MBEDTLS_DEPRECATED
298 #endif
299 
339 int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
340  const unsigned char *hash, size_t hlen,
341  unsigned char *sig, size_t *slen,
343 #undef MBEDTLS_DEPRECATED
344 #endif /* MBEDTLS_DEPRECATED_REMOVED */
345 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
346 
371 int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
372  const unsigned char *hash, size_t hlen,
373  const unsigned char *sig, size_t slen );
374 
401 int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx,
402  const unsigned char *hash, size_t hlen,
403  const unsigned char *sig, size_t slen,
404  mbedtls_ecdsa_restart_ctx *rs_ctx );
405 
420 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
421  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
422 
434 int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key );
435 
441 void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx );
442 
448 void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx );
449 
450 #if defined(MBEDTLS_ECP_RESTARTABLE)
451 
454 void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx );
455 
459 void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx );
460 #endif /* MBEDTLS_ECP_RESTARTABLE */
461 
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif /* ecdsa.h */