mbed TLS v3.3.0
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
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
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
48#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80
50#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00
52#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80
54#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00
56#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80
58#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00
60#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80
62#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00
64#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80
66#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00
68#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80
70#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00
72#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980
74#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900
76#define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL -0x3880
77
78#ifdef __cplusplus
79extern "C" {
80#endif
81
85typedef enum {
95
101{
103 int MBEDTLS_PRIVATE(expected_salt_len);
104
106
110/* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature
111 * size among the supported signature types. Do it by starting at 0,
112 * then incrementally increasing to be large enough for each supported
113 * signature mechanism.
114 *
115 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled
116 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C
117 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT).
118 */
119#define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0
120
121#if ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT) ) && \
122 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
123/* For RSA, the signature can be as large as the bignum module allows.
124 * For RSA_ALT, the signature size is not necessarily tied to what the
125 * bignum module can do, but in the absence of any specific setting,
126 * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */
127#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
128#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
129#endif
130
131#if defined(MBEDTLS_ECDSA_C) && \
132 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE
133/* For ECDSA, the ecdsa module exports a constant for the maximum
134 * signature size. */
135#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
136#define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
137#endif
138
139#if defined(MBEDTLS_USE_PSA_CRYPTO)
140#if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE
141/* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made
142 * through the PSA API in the PSA representation. */
143#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
144#define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE
145#endif
146
147#if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE
148/* The Mbed TLS representation is different for ECDSA signatures:
149 * PSA uses the raw concatenation of r and s,
150 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs).
151 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the
152 * types, lengths (represented by up to 2 bytes), and potential leading
153 * zeros of the INTEGERs and the SEQUENCE. */
154#undef MBEDTLS_PK_SIGNATURE_MAX_SIZE
155#define MBEDTLS_PK_SIGNATURE_MAX_SIZE ( PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 )
156#endif
157#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
158
162typedef enum
163{
168
173{
175 const char *MBEDTLS_PRIVATE(name);
176 void *MBEDTLS_PRIVATE(value);
178
180#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
181
190
194typedef struct mbedtls_pk_context
195{
197 void * MBEDTLS_PRIVATE(pk_ctx);
199
200#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
204typedef struct
205{
206 const mbedtls_pk_info_t * MBEDTLS_PRIVATE(pk_info);
207 void * MBEDTLS_PRIVATE(rs_ctx);
209#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
210/* Now we can declare functions that take a pointer to that */
212#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
213
214#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
218typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, size_t *olen,
219 const unsigned char *input, unsigned char *output,
220 size_t output_max_len );
221typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
222 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
223 mbedtls_md_type_t md_alg, unsigned int hashlen,
224 const unsigned char *hash, unsigned char *sig );
225typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
226#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
227
236
244
257
258#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
265void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
266
273void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
274#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
275
292
293#if defined(MBEDTLS_USE_PSA_CRYPTO)
322int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx,
323 const mbedtls_svc_key_id_t key );
324#endif /* MBEDTLS_USE_PSA_CRYPTO */
325
326#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
345 mbedtls_pk_rsa_alt_key_len_func key_len_func );
346#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
347
356
364static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
365{
366 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
367}
368
382
383#if defined(MBEDTLS_USE_PSA_CRYPTO)
411int mbedtls_pk_can_do_ext( const mbedtls_pk_context *ctx, psa_algorithm_t alg,
412 psa_key_usage_t usage );
413#endif /* MBEDTLS_USE_PSA_CRYPTO */
414
443 const unsigned char *hash, size_t hash_len,
444 const unsigned char *sig, size_t sig_len );
445
467 mbedtls_md_type_t md_alg,
468 const unsigned char *hash, size_t hash_len,
469 const unsigned char *sig, size_t sig_len,
470 mbedtls_pk_restart_ctx *rs_ctx );
471
501int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
503 const unsigned char *hash, size_t hash_len,
504 const unsigned char *sig, size_t sig_len );
505
535 const unsigned char *hash, size_t hash_len,
536 unsigned char *sig, size_t sig_size, size_t *sig_len,
537 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
538
539#if defined(MBEDTLS_PSA_CRYPTO_C)
571 mbedtls_md_type_t md_alg,
572 const unsigned char *hash, size_t hash_len,
573 unsigned char *sig, size_t sig_size, size_t *sig_len,
574 int (*f_rng)(void *, unsigned char *, size_t),
575 void *p_rng );
576#endif /* MBEDTLS_PSA_CRYPTO_C */
577
608 mbedtls_md_type_t md_alg,
609 const unsigned char *hash, size_t hash_len,
610 unsigned char *sig, size_t sig_size, size_t *sig_len,
611 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
612 mbedtls_pk_restart_ctx *rs_ctx );
613
632 const unsigned char *input, size_t ilen,
633 unsigned char *output, size_t *olen, size_t osize,
634 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
635
655 const unsigned char *input, size_t ilen,
656 unsigned char *output, size_t *olen, size_t osize,
657 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
658
674 const mbedtls_pk_context *prv,
675 int (*f_rng)(void *, unsigned char *, size_t),
676 void *p_rng );
677
687
695const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
696
706
707#if defined(MBEDTLS_RSA_C)
719{
720 switch( mbedtls_pk_get_type( &pk ) )
721 {
722 case MBEDTLS_PK_RSA:
723 return( (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx) );
724 default:
725 return( NULL );
726 }
727}
728#endif /* MBEDTLS_RSA_C */
729
730#if defined(MBEDTLS_ECP_C)
743{
744 switch( mbedtls_pk_get_type( &pk ) )
745 {
746 case MBEDTLS_PK_ECKEY:
748 case MBEDTLS_PK_ECDSA:
749 return( (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx) );
750 default:
751 return( NULL );
752 }
753}
754#endif /* MBEDTLS_ECP_C */
755
756#if defined(MBEDTLS_PK_PARSE_C)
789 const unsigned char *key, size_t keylen,
790 const unsigned char *pwd, size_t pwdlen,
791 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
792
816 const unsigned char *key, size_t keylen );
817
818#if defined(MBEDTLS_FS_IO)
843 const char *path, const char *password,
844 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
845
864#endif /* MBEDTLS_FS_IO */
865#endif /* MBEDTLS_PK_PARSE_C */
866
867#if defined(MBEDTLS_PK_WRITE_C)
881int mbedtls_pk_write_key_der( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
882
896int mbedtls_pk_write_pubkey_der( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
897
898#if defined(MBEDTLS_PEM_WRITE_C)
909int mbedtls_pk_write_pubkey_pem( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
910
921int mbedtls_pk_write_key_pem( const mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
922#endif /* MBEDTLS_PEM_WRITE_C */
923#endif /* MBEDTLS_PK_WRITE_C */
924
925/*
926 * WARNING: Low-level functions. You probably do not want to use these unless
927 * you are certain you do ;)
928 */
929
930#if defined(MBEDTLS_PK_PARSE_C)
941int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
942 mbedtls_pk_context *pk );
943#endif /* MBEDTLS_PK_PARSE_C */
944
945#if defined(MBEDTLS_PK_WRITE_C)
956int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
957 const mbedtls_pk_context *key );
958#endif /* MBEDTLS_PK_WRITE_C */
959
960/*
961 * Internal module functions. You probably do not want to use these unless you
962 * know you do.
963 */
964#if defined(MBEDTLS_FS_IO)
965int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
966#endif
967
968#if defined(MBEDTLS_USE_PSA_CRYPTO)
988int mbedtls_pk_wrap_as_opaque( mbedtls_pk_context *pk,
990 psa_algorithm_t alg,
991 psa_key_usage_t usage,
992 psa_algorithm_t alg2 );
993#endif /* MBEDTLS_USE_PSA_CRYPTO */
994
995#ifdef __cplusplus
996}
997#endif
998
999#endif /* MBEDTLS_PK_H */
Build-time configuration info.
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.
Definition: crypto_types.h:138
psa_key_id_t mbedtls_svc_key_id_t
Definition: crypto_types.h:296
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:328
This file contains the generic message-digest wrapper.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:55
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_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:364
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_parse_subpubkey(unsigned char **p, const unsigned char *end, mbedtls_pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
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_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_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:221
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:218
mbedtls_pk_type_t
Public key types.
Definition: pk.h:85
@ MBEDTLS_PK_NONE
Definition: pk.h:86
@ MBEDTLS_PK_OPAQUE
Definition: pk.h:93
@ MBEDTLS_PK_ECDSA
Definition: pk.h:90
@ MBEDTLS_PK_RSASSA_PSS
Definition: pk.h:92
@ MBEDTLS_PK_RSA_ALT
Definition: pk.h:91
@ MBEDTLS_PK_RSA
Definition: pk.h:87
@ MBEDTLS_PK_ECKEY_DH
Definition: pk.h:89
@ MBEDTLS_PK_ECKEY
Definition: pk.h:88
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:189
static mbedtls_rsa_context * mbedtls_pk_rsa(const mbedtls_pk_context pk)
Definition: pk.h:718
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.
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_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:742
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....
int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_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, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Make signature given a signature type.
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()
mbedtls_pk_debug_type
Types for interfacing with the debug module.
Definition: pk.h:163
@ MBEDTLS_PK_DEBUG_MPI
Definition: pk.h:165
@ MBEDTLS_PK_DEBUG_ECP
Definition: pk.h:166
@ MBEDTLS_PK_DEBUG_NONE
Definition: pk.h:164
void mbedtls_pk_init(mbedtls_pk_context *ctx)
Initialize a mbedtls_pk_context (as NONE).
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()
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.
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Get the key type.
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:225
void mbedtls_pk_free(mbedtls_pk_context *ctx)
Free the components of a mbedtls_pk_context.
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:211
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).
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.
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_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.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
This file provides an API for the RSA public-key cryptosystem.
The ECP key-pair structure.
Definition: ecp.h:422
Public key container.
Definition: pk.h:195
Item to send to the debug module.
Definition: pk.h:173
Options for RSASSA-PSS signature verification. See mbedtls_rsa_rsassa_pss_verify_ext()
Definition: pk.h:101
The RSA context structure.
Definition: rsa.h:92