Mbed TLS v2.28.9
Loading...
Searching...
No Matches
pk.h
Go to the documentation of this file.
1
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 */
10
11#ifndef MBEDTLS_PK_H
12#define MBEDTLS_PK_H
13
14#if !defined(MBEDTLS_CONFIG_FILE)
15#include "mbedtls/config.h"
16#else
17#include MBEDTLS_CONFIG_FILE
18#endif
19
20#include "mbedtls/md.h"
21
22#if defined(MBEDTLS_RSA_C)
23#include "mbedtls/rsa.h"
24#endif
25
26#if defined(MBEDTLS_ECP_C)
27#include "mbedtls/ecp.h"
28#endif
29
30#if defined(MBEDTLS_ECDSA_C)
31#include "mbedtls/ecdsa.h"
32#endif
33
34#if defined(MBEDTLS_USE_PSA_CRYPTO)
35#include "psa/crypto.h"
36#endif
37
38#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
39 !defined(inline) && !defined(__cplusplus)
40#define inline __inline
41#endif
42
44#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
46#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
48#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
50#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
52#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
54#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
56#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
58#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
60#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
62#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
64#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
66#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
68#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
70#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
71
72/* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
74#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880
75
76#ifdef __cplusplus
77extern "C" {
78#endif
79
93
103
107/* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
108 * size among the supported signature types. Do it by starting at 0,
109 * then incrementally increasing to be large enough for each supported
110 * signature mechanism.
111 *
112 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
113 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
114 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
115 */
116#define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
117
118#if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
119 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
120/* For RSA, the signature can be as large as the bignum module allows.
121 * For RSA_ALT, the signature size is not necessarily tied to what the
122 * bignum module can do, but in the absence of any specific setting,
123 * we use that (rsa_alt_sign_wrap in pk_wrap will check). */
124#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
125#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
126#endif
127
128#if defined(MBEDTLS_ECDSA_C) && \
129 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
130/* For ECDSA, the ecdsa module exports a constant for the maximum
131 * signature size. */
132#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
133#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
134#endif
135
136#if defined(MBEDTLS_USE_PSA_CRYPTO)
137#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
138/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
139 * through the PSA API in the PSA representation. */
140#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
141#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
142#endif
143
144#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
145/* The Mbed TLS representation is different for ECDSA signatures:
146 * PSA uses the raw concatenation of r and s,
147 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
148 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
149 * types, lengths (represented by up to 2 bytes), and potential leading
150 * zeros of the INTEGERs and the SEQUENCE. */
151#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
152#define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
153#endif
154#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
155
164
173
175#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
176
181
189
190#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
194typedef struct {
195 const mbedtls_pk_info_t *pk_info;
196 void *rs_ctx;
198#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
199/* Now we can declare functions that take a pointer to that */
201#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
202
203#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
207typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen,
208 const unsigned char *input, unsigned char *output,
209 size_t output_max_len);
210typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
211 int (*f_rng)(void *, unsigned char *, size_t),
212 void *p_rng,
213 int mode, mbedtls_md_type_t md_alg,
214 unsigned int hashlen,
215 const unsigned char *hash, unsigned char *sig);
216typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
217#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
218
227
235
248
249#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
256void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
257
264void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
265#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
266
283
284#if defined(MBEDTLS_USE_PSA_CRYPTO)
313int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
314 const psa_key_id_t key);
315#endif /* MBEDTLS_USE_PSA_CRYPTO */
316
317#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
337#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
338
347
355static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
356{
357 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
358}
359
373
404 const unsigned char *hash, size_t hash_len,
405 const unsigned char *sig, size_t sig_len);
406
428 mbedtls_md_type_t md_alg,
429 const unsigned char *hash, size_t hash_len,
430 const unsigned char *sig, size_t sig_len,
431 mbedtls_pk_restart_ctx *rs_ctx);
432
462int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
464 const unsigned char *hash, size_t hash_len,
465 const unsigned char *sig, size_t sig_len);
466
498 const unsigned char *hash, size_t hash_len,
499 unsigned char *sig, size_t *sig_len,
500 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
501
531 mbedtls_md_type_t md_alg,
532 const unsigned char *hash, size_t hash_len,
533 unsigned char *sig, size_t *sig_len,
534 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
535 mbedtls_pk_restart_ctx *rs_ctx);
536
555 const unsigned char *input, size_t ilen,
556 unsigned char *output, size_t *olen, size_t osize,
557 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
558
576 const unsigned char *input, size_t ilen,
577 unsigned char *output, size_t *olen, size_t osize,
578 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
579
593
603
612
622
623#if defined(MBEDTLS_RSA_C)
635{
636 switch (mbedtls_pk_get_type(&pk)) {
637 case MBEDTLS_PK_RSA:
638 return (mbedtls_rsa_context *) (pk).pk_ctx;
639 default:
640 return NULL;
641 }
642}
643#endif /* MBEDTLS_RSA_C */
644
645#if defined(MBEDTLS_ECP_C)
658{
659 switch (mbedtls_pk_get_type(&pk)) {
660 case MBEDTLS_PK_ECKEY:
662 case MBEDTLS_PK_ECDSA:
663 return (mbedtls_ecp_keypair *) (pk).pk_ctx;
664 default:
665 return NULL;
666 }
667}
668#endif /* MBEDTLS_ECP_C */
669
670#if defined(MBEDTLS_PK_PARSE_C)
701 const unsigned char *key, size_t keylen,
702 const unsigned char *pwd, size_t pwdlen);
703
727 const unsigned char *key, size_t keylen);
728
729#if defined(MBEDTLS_FS_IO)
752 const char *path, const char *password);
753
772#endif /* MBEDTLS_FS_IO */
773#endif /* MBEDTLS_PK_PARSE_C */
774
775#if defined(MBEDTLS_PK_WRITE_C)
789int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
790
804int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
805
806#if defined(MBEDTLS_PEM_WRITE_C)
817int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
818
829int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
830#endif /* MBEDTLS_PEM_WRITE_C */
831#endif /* MBEDTLS_PK_WRITE_C */
832
833/*
834 * WARNING: Low-level functions. You probably do not want to use these unless
835 * you are certain you do ;)
836 */
837
838#if defined(MBEDTLS_PK_PARSE_C)
849int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
851#endif /* MBEDTLS_PK_PARSE_C */
852
853#if defined(MBEDTLS_PK_WRITE_C)
864int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
865 const mbedtls_pk_context *key);
866#endif /* MBEDTLS_PK_WRITE_C */
867
868/*
869 * Internal module functions. You probably do not want to use these unless you
870 * know you do.
871 */
872#if defined(MBEDTLS_FS_IO)
873int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
874#endif
875
876#if defined(MBEDTLS_USE_PSA_CRYPTO)
897int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
898 psa_key_id_t *key,
899 psa_algorithm_t hash_alg);
900#endif /* MBEDTLS_USE_PSA_CRYPTO */
901
902#ifdef __cplusplus
903}
904#endif
905
906#endif /* MBEDTLS_PK_H */
Configuration options (set of defines)
Platform Security Architecture cryptography module.
This file contains ECDSA definitions and functions.
This file provides an API for Elliptic Curves over GF(P) (ECP).
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
uint32_t psa_key_id_t
This file contains the generic message-digest wrapper.
mbedtls_md_type_t
Supported message digests.
Definition md.h:50
int(* mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Types for RSA-alt abstraction.
Definition pk.h:207
int mbedtls_pk_parse_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen)
Parse a private key in PEM or DER format.
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Export debug information.
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition pk.h:355
int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_pk_restart_ctx *rs_ctx)
Restartable version of mbedtls_pk_sign()
int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Tell if a context can do the operation given by type.
mbedtls_pk_type_t
Public key types.
Definition pk.h:83
@ MBEDTLS_PK_NONE
Definition pk.h:84
@ MBEDTLS_PK_OPAQUE
Definition pk.h:91
@ MBEDTLS_PK_ECDSA
Definition pk.h:88
@ MBEDTLS_PK_RSASSA_PSS
Definition pk.h:90
@ MBEDTLS_PK_RSA_ALT
Definition pk.h:89
@ MBEDTLS_PK_RSA
Definition pk.h:85
@ MBEDTLS_PK_ECKEY_DH
Definition pk.h:87
@ MBEDTLS_PK_ECKEY
Definition pk.h:86
int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Decrypt message (including padding if relevant).
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Get the size in bits of the underlying key.
struct mbedtls_pk_info_t mbedtls_pk_info_t
Public key information and operations.
Definition pk.h:180
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
Definition pk.h:634
int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t *sig_len, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature, including padding if relevant.
int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 DER structure Note: data is written at the end of the buffer!...
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
static mbedtls_ecp_keypair * mbedtls_pk_ec(const mbedtls_pk_context pk)
Definition pk.h:657
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Initialize a PK context with the information given and allocates the type-specific PK subcontext.
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature, with options. (Includes verification of the padding depending on type....
struct mbedtls_pk_context mbedtls_pk_context
Public key container.
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
Check if a public-private pair of keys matches.
struct mbedtls_pk_rsassa_pss_options mbedtls_pk_rsassa_pss_options
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext()
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition pk.h:159
@ MBEDTLS_PK_DEBUG_MPI
Definition pk.h:161
@ MBEDTLS_PK_DEBUG_ECP
Definition pk.h:162
@ MBEDTLS_PK_DEBUG_NONE
Definition pk.h:160
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE).
int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
const mbedtls_pk_info_t * mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Return information associated with the given PK type.
int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len, mbedtls_pk_restart_ctx *rs_ctx)
Restartable version of mbedtls_pk_verify()
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
struct mbedtls_pk_debug_item mbedtls_pk_debug_item
Item to send to the debug module.
int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, mbedtls_pk_rsa_alt_decrypt_func decrypt_func, mbedtls_pk_rsa_alt_sign_func sign_func, mbedtls_pk_rsa_alt_key_len_func key_len_func)
Initialize an RSA-alt context.
int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature (including padding if relevant).
size_t(* mbedtls_pk_rsa_alt_key_len_func)(void *ctx)
Definition pk.h:216
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
int(* mbedtls_pk_rsa_alt_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Definition pk.h:210
int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start, const mbedtls_pk_context *key)
Write a subjectPublicKey to ASN.1 data Note: function works backwards in data buffer.
void mbedtls_pk_restart_ctx
Definition pk.h:200
int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen)
Parse a public key in PEM or DER format.
int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Encrypt message (including padding if relevant).
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Load and parse a public key.
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a SubjectPublicKeyInfo DER structure Note: data is written at the end of the bu...
This file provides an API for the RSA public-key cryptosystem.
The ECP key-pair structure.
Definition ecp.h:443
Public key container.
Definition pk.h:185
const mbedtls_pk_info_t * pk_info
Definition pk.h:186
void * pk_ctx
Definition pk.h:187
Item to send to the debug module.
Definition pk.h:168
const char * name
Definition pk.h:170
mbedtls_pk_debug_type type
Definition pk.h:169
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext()
Definition pk.h:98
mbedtls_md_type_t mgf1_hash_id
Definition pk.h:99
The RSA context structure.
Definition rsa.h:97