mbed TLS v3.1.0
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 #include "mbedtls/private_access.h"
26 
27 #include "mbedtls/build_info.h"
28 
29 #include "mbedtls/md.h"
30 
31 #if defined(MBEDTLS_RSA_C)
32 #include "mbedtls/rsa.h"
33 #endif
34 
35 #if defined(MBEDTLS_ECP_C)
36 #include "mbedtls/ecp.h"
37 #endif
38 
39 #if defined(MBEDTLS_ECDSA_C)
40 #include "mbedtls/ecdsa.h"
41 #endif
42 
43 #if defined(MBEDTLS_USE_PSA_CRYPTO)
44 #include "psa/crypto.h"
45 #endif
46 
47 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
48  !defined(inline) && !defined(__cplusplus)
49 #define inline __inline
50 #endif
51 
53 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
54 
55 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
56 
57 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
58 
59 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
60 
61 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
62 
63 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
64 
65 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
66 
67 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
68 
69 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
70 
71 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
72 
73 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
74 
75 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
76 
77 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
78 
79 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
80 
81 #define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL -0x3880
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
90 typedef enum {
100 
106 {
108  int MBEDTLS_PRIVATE(expected_salt_len);
109 
111 
115 /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
116  * size among the supported signature types. Do it by starting at 0,
117  * then incrementally increasing to be large enough for each supported
118  * signature mechanism.
119  *
120  * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
121  * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
122  * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
123  */
124 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
125 
126 #if ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT) ) && \
127  MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
128 /* For RSA, the signature can be as large as the bignum module allows.
129  * For RSA_ALT, the signature size is not necessarily tied to what the
130  * bignum module can do, but in the absence of any specific setting,
131  * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
132 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
133 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
134 #endif
135 
136 #if defined(MBEDTLS_ECDSA_C) && \
137  MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
138 /* For ECDSA, the ecdsa module exports a constant for the maximum
139  * signature size. */
140 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
141 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
142 #endif
143 
144 #if defined(MBEDTLS_USE_PSA_CRYPTO)
145 #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
146 /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
147  * through the PSA API in the PSA representation. */
148 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
149 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
150 #endif
151 
152 #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
153 /* The Mbed TLS representation is different for ECDSA signatures:
154  * PSA uses the raw concatenation of r and s,
155  * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
156  * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
157  * types, lengths (represented by up to 2 bytes), and potential leading
158  * zeros of the INTEGERs and the SEQUENCE. */
159 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
160 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE ( PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 )
161 #endif
162 #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
163 
167 typedef enum
168 {
173 
177 typedef struct mbedtls_pk_debug_item
178 {
180  const char *MBEDTLS_PRIVATE(name);
181  void *MBEDTLS_PRIVATE(value);
183 
185 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
186 
195 
199 typedef struct mbedtls_pk_context
200 {
202  void * MBEDTLS_PRIVATE(pk_ctx);
204 
205 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
206 
209 typedef struct
210 {
212  void * MBEDTLS_PRIVATE(rs_ctx);
214 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
215 /* Now we can declare functions that take a pointer to that */
216 typedef void mbedtls_pk_restart_ctx;
217 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
218 
219 #if defined(MBEDTLS_RSA_C)
220 
226 static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
227 {
228  return( (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) );
229 }
230 #endif /* MBEDTLS_RSA_C */
231 
232 #if defined(MBEDTLS_ECP_C)
233 
240 {
241  return( (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) );
242 }
243 #endif /* MBEDTLS_ECP_C */
244 
245 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
246 
249 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, size_t *olen,
250  const unsigned char *input, unsigned char *output,
251  size_t output_max_len );
252 typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
253  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
254  mbedtls_md_type_t md_alg, unsigned int hashlen,
255  const unsigned char *hash, unsigned char *sig );
256 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
257 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
258 
267 
275 
288 
289 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
290 
296 void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
297 
304 void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
305 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
306 
322 int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
323 
324 #if defined(MBEDTLS_USE_PSA_CRYPTO)
325 
354  const psa_key_id_t key );
355 #endif /* MBEDTLS_USE_PSA_CRYPTO */
356 
357 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
358 
373 int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
374  mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
376  mbedtls_pk_rsa_alt_key_len_func key_len_func );
377 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
378 
386 size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
387 
395 static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
396 {
397  return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
398 }
399 
413 
442  const unsigned char *hash, size_t hash_len,
443  const unsigned char *sig, size_t sig_len );
444 
466  mbedtls_md_type_t md_alg,
467  const unsigned char *hash, size_t hash_len,
468  const unsigned char *sig, size_t sig_len,
469  mbedtls_pk_restart_ctx *rs_ctx );
470 
500 int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
502  const unsigned char *hash, size_t hash_len,
503  const unsigned char *sig, size_t sig_len );
504 
534  const unsigned char *hash, size_t hash_len,
535  unsigned char *sig, size_t sig_size, size_t *sig_len,
536  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
537 
568  mbedtls_md_type_t md_alg,
569  const unsigned char *hash, size_t hash_len,
570  unsigned char *sig, size_t sig_size, size_t *sig_len,
571  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
572  mbedtls_pk_restart_ctx *rs_ctx );
573 
592  const unsigned char *input, size_t ilen,
593  unsigned char *output, size_t *olen, size_t osize,
594  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
595 
615  const unsigned char *input, size_t ilen,
616  unsigned char *output, size_t *olen, size_t osize,
617  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
618 
634  const mbedtls_pk_context *prv,
635  int (*f_rng)(void *, unsigned char *, size_t),
636  void *p_rng );
637 
647 
655 const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
656 
666 
667 #if defined(MBEDTLS_PK_PARSE_C)
668 
700  const unsigned char *key, size_t keylen,
701  const unsigned char *pwd, size_t pwdlen,
702  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
703 
727  const unsigned char *key, size_t keylen );
728 
729 #if defined(MBEDTLS_FS_IO)
730 
754  const char *path, const char *password,
755  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
756 
774 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
775 #endif /* MBEDTLS_FS_IO */
776 #endif /* MBEDTLS_PK_PARSE_C */
777 
778 #if defined(MBEDTLS_PK_WRITE_C)
779 
792 int mbedtls_pk_write_key_der( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
793 
807 int mbedtls_pk_write_pubkey_der( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
808 
809 #if defined(MBEDTLS_PEM_WRITE_C)
810 
820 int mbedtls_pk_write_pubkey_pem( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
821 
832 int mbedtls_pk_write_key_pem( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
833 #endif /* MBEDTLS_PEM_WRITE_C */
834 #endif /* MBEDTLS_PK_WRITE_C */
835 
836 /*
837  * WARNING: Low-level functions. You probably do not want to use these unless
838  * you are certain you do ;)
839  */
840 
841 #if defined(MBEDTLS_PK_PARSE_C)
842 
852 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
853  mbedtls_pk_context *pk );
854 #endif /* MBEDTLS_PK_PARSE_C */
855 
856 #if defined(MBEDTLS_PK_WRITE_C)
857 
867 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
868  const mbedtls_pk_context *key );
869 #endif /* MBEDTLS_PK_WRITE_C */
870 
871 /*
872  * Internal module functions. You probably do not want to use these unless you
873  * know you do.
874  */
875 #if defined(MBEDTLS_FS_IO)
876 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
877 #endif
878 
879 #if defined(MBEDTLS_USE_PSA_CRYPTO)
880 
901  psa_key_id_t *key,
902  psa_algorithm_t hash_alg );
903 #endif /* MBEDTLS_USE_PSA_CRYPTO */
904 
905 #ifdef __cplusplus
906 }
907 #endif
908 
909 #endif /* MBEDTLS_PK_H */
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext() ...
Definition: pk.h:105
int(* mbedtls_pk_rsa_alt_sign_func)(void *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, mbedtls_md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Definition: pk.h:252
Public key container.
Definition: pk.h:199
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_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:226
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:167
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_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Check if a public-private pair of keys matches.
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(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...
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, const char *path, const char *password, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Load and parse a private key.
The ECP key-pair structure.
Definition: ecp.h:421
This file contains ECDSA definitions and functions.
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_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, 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_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.
#define MBEDTLS_PRIVATE(member)
mbedtls_pk_type_t
Public key types.
Definition: pk.h:90
uint32_t psa_key_id_t
Definition: crypto_types.h:225
int mbedtls_pk_parse_key(mbedtls_pk_context *ctx, const unsigned char *key, size_t keylen, const unsigned char *pwd, size_t pwdlen, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Parse a private key in PEM or DER format.
static size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
Get the length in bytes of the underlying key.
Definition: pk.h:395
void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Initialize a restart context.
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Export debug information.
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_setup_opaque(mbedtls_pk_context *ctx, const psa_key_id_t key)
Initialize a PK context to wrap a PSA key.
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_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.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:106
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
Macro wrapper for struct's memebrs.
Build-time configuration info.
size_t(* mbedtls_pk_rsa_alt_key_len_func)(void *ctx)
Definition: pk.h:256
struct mbedtls_pk_info_t mbedtls_pk_info_t
Public key information and operations.
Definition: pk.h:194
This file contains the generic message-digest wrapper.
This file provides an API for the RSA public-key cryptosystem.
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 (including padding if relevant).
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
const char * mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Access the type name.
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:239
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_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, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature, including padding if relevant.
Context for resuming operations.
Definition: pk.h:209
struct mbedtls_pk_context mbedtls_pk_context
Public key container.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:55
struct mbedtls_pk_debug_item mbedtls_pk_debug_item
Item to send to the debug module.
Item to send to the debug module.
Definition: pk.h:177
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...
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).
int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk, psa_key_id_t *key, psa_algorithm_t hash_alg)
Turn an EC key into an opaque one.
int(* mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Types for RSA-alt abstraction.
Definition: pk.h:249