Mbed TLS v2.28.5
pk.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright The Mbed TLS Contributors
8  * SPDX-License-Identifier: Apache-2.0
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License"); you may
11  * not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #ifndef MBEDTLS_PK_H
24 #define MBEDTLS_PK_H
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #include "mbedtls/md.h"
33 
34 #if defined(MBEDTLS_RSA_C)
35 #include "mbedtls/rsa.h"
36 #endif
37 
38 #if defined(MBEDTLS_ECP_C)
39 #include "mbedtls/ecp.h"
40 #endif
41 
42 #if defined(MBEDTLS_ECDSA_C)
43 #include "mbedtls/ecdsa.h"
44 #endif
45 
46 #if defined(MBEDTLS_USE_PSA_CRYPTO)
47 #include "psa/crypto.h"
48 #endif
49 
50 #if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && \
51  !defined(inline) && !defined(__cplusplus)
52 #define inline __inline
53 #endif
54 
56 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
57 
58 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
59 
60 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
61 
62 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
63 
64 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
65 
66 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
67 
68 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
69 
70 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
71 
72 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
73 
74 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
75 
76 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
77 
78 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
79 
80 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
81 
82 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
83 
84 /* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
86 #define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880
87 
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
91 
95 typedef enum {
105 
113 
115 
119 /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
120  * size among the supported signature types. Do it by starting at 0,
121  * then incrementally increasing to be large enough for each supported
122  * signature mechanism.
123  *
124  * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
125  * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
126  * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
127  */
128 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
129 
130 #if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \
131  MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
132 /* For RSA, the signature can be as large as the bignum module allows.
133  * For RSA_ALT, the signature size is not necessarily tied to what the
134  * bignum module can do, but in the absence of any specific setting,
135  * we use that (rsa_alt_sign_wrap in pk_wrap will check). */
136 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
137 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
138 #endif
139 
140 #if defined(MBEDTLS_ECDSA_C) && \
141  MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
142 /* For ECDSA, the ecdsa module exports a constant for the maximum
143  * signature size. */
144 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
145 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
146 #endif
147 
148 #if defined(MBEDTLS_USE_PSA_CRYPTO)
149 #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
150 /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
151  * through the PSA API in the PSA representation. */
152 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
153 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
154 #endif
155 
156 #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
157 /* The Mbed TLS representation is different for ECDSA signatures:
158  * PSA uses the raw concatenation of r and s,
159  * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
160  * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
161  * types, lengths (represented by up to 2 bytes), and potential leading
162  * zeros of the INTEGERs and the SEQUENCE. */
163 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
164 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11)
165 #endif
166 #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
167 
171 typedef enum {
176 
180 typedef struct mbedtls_pk_debug_item {
182  const char *name;
183  void *value;
185 
187 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
188 
193 
197 typedef struct mbedtls_pk_context {
199  void *pk_ctx;
201 
202 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
203 
206 typedef struct {
207  const mbedtls_pk_info_t *pk_info;
208  void *rs_ctx;
210 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
211 /* Now we can declare functions that take a pointer to that */
213 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
214 
215 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
216 
219 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, int mode, size_t *olen,
220  const unsigned char *input, unsigned char *output,
221  size_t output_max_len);
222 typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx,
223  int (*f_rng)(void *, unsigned char *, size_t),
224  void *p_rng,
225  int mode, mbedtls_md_type_t md_alg,
226  unsigned int hashlen,
227  const unsigned char *hash, unsigned char *sig);
228 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx);
229 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
230 
239 
247 
260 
261 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
262 
268 void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx);
269 
276 void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx);
277 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
278 
295 
296 #if defined(MBEDTLS_USE_PSA_CRYPTO)
297 
325 int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
326  const psa_key_id_t key);
327 #endif /* MBEDTLS_USE_PSA_CRYPTO */
328 
329 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
330 
345 int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
346  mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
348  mbedtls_pk_rsa_alt_key_len_func key_len_func);
349 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
350 
358 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
359 
367 static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
368 {
369  return (mbedtls_pk_get_bitlen(ctx) + 7) / 8;
370 }
371 
385 
416  const unsigned char *hash, size_t hash_len,
417  const unsigned char *sig, size_t sig_len);
418 
440  mbedtls_md_type_t md_alg,
441  const unsigned char *hash, size_t hash_len,
442  const unsigned char *sig, size_t sig_len,
443  mbedtls_pk_restart_ctx *rs_ctx);
444 
474 int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
476  const unsigned char *hash, size_t hash_len,
477  const unsigned char *sig, size_t sig_len);
478 
510  const unsigned char *hash, size_t hash_len,
511  unsigned char *sig, size_t *sig_len,
512  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
513 
543  mbedtls_md_type_t md_alg,
544  const unsigned char *hash, size_t hash_len,
545  unsigned char *sig, size_t *sig_len,
546  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
547  mbedtls_pk_restart_ctx *rs_ctx);
548 
567  const unsigned char *input, size_t ilen,
568  unsigned char *output, size_t *olen, size_t osize,
569  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
570 
588  const unsigned char *input, size_t ilen,
589  unsigned char *output, size_t *olen, size_t osize,
590  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
591 
605 
615 
623 const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx);
624 
634 
635 #if defined(MBEDTLS_RSA_C)
636 
647 {
648  switch (mbedtls_pk_get_type(&pk)) {
649  case MBEDTLS_PK_RSA:
650  return (mbedtls_rsa_context *) (pk).pk_ctx;
651  default:
652  return NULL;
653  }
654 }
655 #endif /* MBEDTLS_RSA_C */
656 
657 #if defined(MBEDTLS_ECP_C)
658 
670 {
671  switch (mbedtls_pk_get_type(&pk)) {
672  case MBEDTLS_PK_ECKEY:
673  case MBEDTLS_PK_ECKEY_DH:
674  case MBEDTLS_PK_ECDSA:
675  return (mbedtls_ecp_keypair *) (pk).pk_ctx;
676  default:
677  return NULL;
678  }
679 }
680 #endif /* MBEDTLS_ECP_C */
681 
682 #if defined(MBEDTLS_PK_PARSE_C)
683 
713  const unsigned char *key, size_t keylen,
714  const unsigned char *pwd, size_t pwdlen);
715 
739  const unsigned char *key, size_t keylen);
740 
741 #if defined(MBEDTLS_FS_IO)
742 
764  const char *path, const char *password);
765 
783 int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path);
784 #endif /* MBEDTLS_FS_IO */
785 #endif /* MBEDTLS_PK_PARSE_C */
786 
787 #if defined(MBEDTLS_PK_WRITE_C)
788 
801 int mbedtls_pk_write_key_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
802 
816 int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
817 
818 #if defined(MBEDTLS_PEM_WRITE_C)
819 
829 int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
830 
841 int mbedtls_pk_write_key_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size);
842 #endif /* MBEDTLS_PEM_WRITE_C */
843 #endif /* MBEDTLS_PK_WRITE_C */
844 
845 /*
846  * WARNING: Low-level functions. You probably do not want to use these unless
847  * you are certain you do ;)
848  */
849 
850 #if defined(MBEDTLS_PK_PARSE_C)
851 
861 int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
862  mbedtls_pk_context *pk);
863 #endif /* MBEDTLS_PK_PARSE_C */
864 
865 #if defined(MBEDTLS_PK_WRITE_C)
866 
876 int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
877  const mbedtls_pk_context *key);
878 #endif /* MBEDTLS_PK_WRITE_C */
879 
880 /*
881  * Internal module functions. You probably do not want to use these unless you
882  * know you do.
883  */
884 #if defined(MBEDTLS_FS_IO)
885 int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n);
886 #endif
887 
888 #if defined(MBEDTLS_USE_PSA_CRYPTO)
889 
909 int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
910  psa_key_id_t *key,
911  psa_algorithm_t hash_alg);
912 #endif /* MBEDTLS_USE_PSA_CRYPTO */
913 
914 #ifdef __cplusplus
915 }
916 #endif
917 
918 #endif /* MBEDTLS_PK_H */
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *ctx, unsigned char *buf, size_t size)
Write a public key to a PEM string.
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext() ...
Definition: pk.h:110
Public key container.
Definition: pk.h:197
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_parse_subpubkey(unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE).
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
Definition: pk.h:646
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:171
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()
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_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Tell if a context can do the operation given by type.
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...
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.
This file provides an API for Elliptic Curves over GF(P) (ECP).
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...
The ECP key-pair structure.
Definition: ecp.h:455
This file contains ECDSA definitions and functions.
const mbedtls_pk_info_t * pk_info
Definition: pk.h:198
Platform Security Architecture cryptography module.
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).
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.
mbedtls_pk_type_t
Public key types.
Definition: pk.h:95
Configuration options (set of defines)
uint32_t psa_key_id_t
Definition: crypto_types.h:278
void * value
Definition: pk.h:183
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:367
size_t(* mbedtls_pk_rsa_alt_key_len_func)(void *ctx)
Definition: pk.h:228
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.
void mbedtls_pk_restart_ctx
Definition: pk.h:212
const char * name
Definition: pk.h:182
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Export debug information.
void * pk_ctx
Definition: pk.h:199
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_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.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:137
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
mbedtls_md_type_t mgf1_hash_id
Definition: pk.h:111
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
Public key information and operations.
Definition: pk.h:192
This file contains the generic message-digest wrapper.
This file provides an API for the RSA public-key cryptosystem.
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).
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
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.
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!...
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
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:222
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:219
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
static mbedtls_ecp_keypair * mbedtls_pk_ec(const mbedtls_pk_context pk)
Definition: pk.h:669
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...
struct mbedtls_pk_context mbedtls_pk_context
Public key container.
mbedtls_pk_debug_type type
Definition: pk.h:181
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:62
struct mbedtls_pk_debug_item mbedtls_pk_debug_item
Item to send to the debug module.
The RSA context structure.
Definition: rsa.h:109
Item to send to the debug module.
Definition: pk.h:180
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
Load and parse a public key.
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_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).