Mbed TLS v4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #define MBEDTLS_PK_HAVE_PRIVATE_HEADER
14 
15 #include "mbedtls/private_access.h"
16 
19 
20 #include "mbedtls/md.h"
21 
22 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
23 #include "psa/crypto.h"
24 #endif
25 
27 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
28 
29 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
30 
31 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
32 
33 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
34 
35 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
36 
37 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
38 
39 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
40 
41 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
42 
43 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
44 
45 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
46 
47 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 typedef enum {
55  MBEDTLS_PK_SIGALG_RSA_PKCS1V15, // PSA_ALG_RSA_PKCS1V15_SIGN
56  MBEDTLS_PK_SIGALG_RSA_PSS, // PSA_ALG_RSA_PSS_ANY_SALT
57  MBEDTLS_PK_SIGALG_ECDSA, // MBEDTLS_PK_ALG_ECDSA
59 
64 /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
65  * size among the supported signature types. Do it by starting at 0,
66  * then incrementally increasing to be large enough for each supported
67  * signature mechanism.
68  *
69  * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
70  * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
71  * nor MBEDTLS_RSA_C nor any PSA signature mechanism (PSA).
72  */
73 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
74 
75 #if defined(MBEDTLS_RSA_C) && \
76  MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
77 /* For RSA, the signature can be as large as the bignum module allows.*/
78 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
79 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
80 #endif
81 
82 #if defined(MBEDTLS_ECDSA_C) && \
83  MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
84 /* For ECDSA, the ecdsa module exports a constant for the maximum
85  * signature size. */
86 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
87 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
88 #endif
89 
90 #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
91 /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
92  * through the PSA API in the PSA representation. */
93 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
94 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
95 #endif
96 
97 #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
98 /* The Mbed TLS representation is different for ECDSA signatures:
99  * PSA uses the raw concatenation of r and s,
100  * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
101  * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
102  * types, lengths (represented by up to 2 bytes), and potential leading
103  * zeros of the INTEGERs and the SEQUENCE. */
104 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
105 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
106 #endif
107 
108 /* Keep this symbol for backward compatibility. There is code in the framework
109  * which depends on this. Once 3.6 LTS branch will reach end-of-life framework's
110  * code can be adjusted and this define removed. */
111 #define MBEDTLS_PK_USE_PSA_EC_DATA
112 
113 /* This is identical to MBEDTLS_PK_USE_PSA_EC_DATA above, but for RSA keys.
114  * The main reason for having it is that framework code is shared between
115  * the develoment branch and the 3.6 LTS one and we need a way to tell from which
116  * of the two we're building.
117  * This symbol is not used in builtin driver and tests and it can be removed
118  * at the same time as MBEDTLS_PK_USE_PSA_EC_DATA. */
119 #define MBEDTLS_PK_USE_PSA_RSA_DATA
120 
121 /* Opaque internal type */
123 
124 #define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \
125  PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
126 
127 #define MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN \
128  PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
129 
130 #define MBEDTLS_PK_MAX_PUBKEY_RAW_LEN \
131  (MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN > MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN) ? \
132  MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN : MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN
133 
134 typedef enum {
138 
142 typedef struct mbedtls_pk_context {
143  /* Public key information. */
144  const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info);
145  /* Previously: underlying public key context.
146  * Now unused (by public functions at least). */
147  void *MBEDTLS_PRIVATE(pk_ctx);
148 
149  /* The following field is used to store the ID of a private key for:
150  * - EC keys (MBEDTLS_PK_ECKEY, MBEDTLS_PK_ECKEY_DH, MBEDTLS_PK_ECDSA)
151  * - Wrapped keys (EC or RSA).
152  *
153  * priv_id = MBEDTLS_SVC_KEY_ID_INIT when PK context wraps only the public
154  * key.
155  *
156  * Other keys still use the pk_ctx to store their own context. */
157  mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id);
158 
159 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
160  /* Public EC or RSA key in raw format, where raw here means the format returned
161  * by psa_export_public_key(). */
162  uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_PUBKEY_RAW_LEN];
163 
164  /* Lenght of the raw key above in bytes. */
165  size_t MBEDTLS_PRIVATE(pub_raw_len);
166 
167  /* Bits of the private/public key. */
168  size_t MBEDTLS_PRIVATE(bits);
169 #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
170 
171 #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
172  /* EC family. Only applies to EC keys. */
173  psa_ecc_family_t MBEDTLS_PRIVATE(ec_family);
174 #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
175 
176 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
177  /* Padding associated to the RSA key. It only affects RSA public key since
178  * the private one is imported into PSA with v1.5 as main algorithm and
179  * v2.1 as enrollment algorithm. */
180  mbedtls_pk_rsa_padding_t MBEDTLS_PRIVATE(rsa_padding);
181 
182  /* Hash algorithm to be used with RSA public key. */
183  psa_algorithm_t MBEDTLS_PRIVATE(rsa_hash_alg);
184 #endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
186 
187 #if defined(MBEDTLS_ECP_RESTARTABLE)
188 
191 typedef struct {
192  const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /* Public key information */
193  void *MBEDTLS_PRIVATE(rs_ctx); /* Underlying restart context */
195 
196 #else /* MBEDTLS_ECP_RESTARTABLE */
197 /* Now we can declare functions that take a pointer to that */
198 typedef void mbedtls_pk_restart_ctx;
199 #endif /* MBEDTLS_ECP_RESTARTABLE */
200 
207 #if defined(PSA_WANT_ALG_DETERMINISTIC_ECDSA)
208 #define MBEDTLS_PK_ALG_ECDSA(hash_alg) PSA_ALG_DETERMINISTIC_ECDSA(hash_alg)
209 #else
210 #define MBEDTLS_PK_ALG_ECDSA(hash_alg) PSA_ALG_ECDSA(hash_alg)
211 #endif
212 
230 
245 
246 #if defined(MBEDTLS_ECP_RESTARTABLE)
247 
253 void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
254 
261 void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
262 #endif /* MBEDTLS_ECP_RESTARTABLE */
263 
290  const mbedtls_svc_key_id_t key);
291 
299 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
300 
343  psa_key_usage_t usage);
344 
345 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
346 
433  psa_key_usage_t usage,
434  psa_key_attributes_t *attributes);
435 
477  const psa_key_attributes_t *attributes,
478  mbedtls_svc_key_id_t *key_id);
479 
510 
540 #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
541 
567  const unsigned char *hash, size_t hash_len,
568  const unsigned char *sig, size_t sig_len);
569 
591  mbedtls_md_type_t md_alg,
592  const unsigned char *hash, size_t hash_len,
593  const unsigned char *sig, size_t sig_len,
594  mbedtls_pk_restart_ctx *rs_ctx);
595 
619  const unsigned char *hash, size_t hash_len,
620  const unsigned char *sig, size_t sig_len);
621 
650  const unsigned char *hash, size_t hash_len,
651  unsigned char *sig, size_t sig_size, size_t *sig_len);
652 
688  mbedtls_md_type_t md_alg,
689  const unsigned char *hash, size_t hash_len,
690  unsigned char *sig, size_t sig_size, size_t *sig_len,
691  mbedtls_pk_restart_ctx *rs_ctx);
692 
717  mbedtls_pk_context *ctx,
718  mbedtls_md_type_t md_alg,
719  const unsigned char *hash, size_t hash_len,
720  unsigned char *sig, size_t sig_size, size_t *sig_len);
721 
738  const mbedtls_pk_context *prv);
739 
740 #if defined(MBEDTLS_PK_PARSE_C)
741 
769  const unsigned char *key, size_t keylen,
770  const unsigned char *pwd, size_t pwdlen);
771 
793  const unsigned char *key, size_t keylen);
794 
795 #if defined(MBEDTLS_FS_IO)
796 
816  const char *path, const char *password);
817 
832 int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
833 #endif /* MBEDTLS_FS_IO */
834 #endif /* MBEDTLS_PK_PARSE_C */
835 
836 #if defined(MBEDTLS_PK_WRITE_C)
837 
850 int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
851 
865 int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
866 
867 #if defined(MBEDTLS_PEM_WRITE_C)
868 
878 int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
879 
890 int mbedtls_pk_write_key_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
891 #endif /* MBEDTLS_PEM_WRITE_C */
892 #endif /* MBEDTLS_PK_WRITE_C */
893 
894 #ifdef __cplusplus
895 }
896 #endif
897 
898 #endif /* MBEDTLS_PK_H */
Public key container.
Definition: pk.h:142
int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
int mbedtls_pk_verify_ext(mbedtls_pk_sigalg_t type, 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, selecting a specific algorithm.
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as empty).
Platform Security Architecture cryptography module.
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()
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Get the size in bits of the underlying key.
int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk, const psa_key_attributes_t *attributes, mbedtls_svc_key_id_t *key_id)
Import a key into the PSA key store.
int mbedtls_pk_sign_ext(mbedtls_pk_sigalg_t sig_type, mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, const unsigned char *hash, size_t hash_len, unsigned char *sig, size_t sig_size, size_t *sig_len)
Generate a signature, selecting a specific algorithm.
void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Initialize a restart context.
mbedtls_pk_rsa_padding_t
Definition: pk.h:134
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_write_key_der(const 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_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_write_key_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a private key to a PKCS#1 or SEC1 PEM string.
#define MBEDTLS_PK_MAX_PUBKEY_RAW_LEN
Definition: pk.h:130
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_size, size_t *sig_len)
Make signature.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:127
int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx, const mbedtls_svc_key_id_t key)
Populate a PK context by wrapping a PSA key pair.
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Empty a mbedtls_pk_context. After this, the context can be re-used as if it had been freshly initiali...
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_size, size_t *sig_len, mbedtls_pk_restart_ctx *rs_ctx)
Restartable version of mbedtls_pk_sign()
Compatibility definitions for MbedTLS 3.x code built with MbedTLS 4.x or TF-PSA-Crypto 1...
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:316
int mbedtls_pk_can_do_psa(const mbedtls_pk_context *pk, psa_algorithm_t alg, psa_key_usage_t usage)
Tell if the key wrapped in the PK context is able to perform the usage operation using the alg algori...
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password)
Load and parse a private key.
struct mbedtls_pk_info_t mbedtls_pk_info_t
Definition: pk.h:122
This file contains the generic functions for message-digest (hashing) and HMAC.
void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Free the components of a restart 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.
int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk, psa_key_usage_t usage, psa_key_attributes_t *attributes)
Determine valid PSA attributes that can be used to import a key into PSA.
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk)
Populate a PK context with the public key material of a PSA key.
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk)
Populate a PK context with the key material from a PSA key.
Build-time configuration info.
Context for resuming operations.
Definition: pk.h:191
struct mbedtls_pk_context mbedtls_pk_context
Public key container.
uint8_t psa_ecc_family_t
Definition: crypto_types.h:90
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
Check if a public-private pair of keys matches.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:50
int mbedtls_pk_write_pubkey_der(const 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...
mbedtls_pk_sigalg_t
Definition: pk.h:53
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Load and parse a public key.