![]() |
|
|||
File indexing completed on 2025-08-27 09:37:32
0001 /** 0002 * \file pk.h 0003 * 0004 * \brief Public Key abstraction layer 0005 */ 0006 /* 0007 * Copyright The Mbed TLS Contributors 0008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 0009 */ 0010 0011 #ifndef MBEDTLS_PK_H 0012 #define MBEDTLS_PK_H 0013 #include "mbedtls/private_access.h" 0014 0015 #include "mbedtls/build_info.h" 0016 0017 #include "mbedtls/md.h" 0018 0019 #if defined(MBEDTLS_RSA_C) 0020 #include "mbedtls/rsa.h" 0021 #endif 0022 0023 #if defined(MBEDTLS_ECP_C) 0024 #include "mbedtls/ecp.h" 0025 #endif 0026 0027 #if defined(MBEDTLS_ECDSA_C) 0028 #include "mbedtls/ecdsa.h" 0029 #endif 0030 0031 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) 0032 #include "psa/crypto.h" 0033 #endif 0034 0035 /** Memory allocation failed. */ 0036 #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 0037 /** Type mismatch, eg attempt to encrypt with an ECDSA key */ 0038 #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 0039 /** Bad input parameters to function. */ 0040 #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 0041 /** Read/write of file failed. */ 0042 #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 0043 /** Unsupported key version */ 0044 #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 0045 /** Invalid key tag or value. */ 0046 #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 0047 /** Key algorithm is unsupported (only RSA and EC are supported). */ 0048 #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 0049 /** Private key password can't be empty. */ 0050 #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 0051 /** Given private key password does not allow for correct decryption. */ 0052 #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 0053 /** The pubkey tag or value is invalid (only RSA and EC are supported). */ 0054 #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 0055 /** The algorithm tag or value is invalid. */ 0056 #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 0057 /** Elliptic curve is unsupported (only NIST curves are supported). */ 0058 #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 0059 /** Unavailable feature, e.g. RSA disabled for RSA key. */ 0060 #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 0061 /** The buffer contains a valid signature followed by more data. */ 0062 #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 0063 /** The output buffer is too small. */ 0064 #define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL -0x3880 0065 0066 #ifdef __cplusplus 0067 extern "C" { 0068 #endif 0069 0070 /** 0071 * \brief Public key types 0072 */ 0073 typedef enum { 0074 MBEDTLS_PK_NONE=0, 0075 MBEDTLS_PK_RSA, 0076 MBEDTLS_PK_ECKEY, 0077 MBEDTLS_PK_ECKEY_DH, 0078 MBEDTLS_PK_ECDSA, 0079 MBEDTLS_PK_RSA_ALT, 0080 MBEDTLS_PK_RSASSA_PSS, 0081 MBEDTLS_PK_OPAQUE, 0082 } mbedtls_pk_type_t; 0083 0084 /** 0085 * \brief Options for RSASSA-PSS signature verification. 0086 * See \c mbedtls_rsa_rsassa_pss_verify_ext() 0087 */ 0088 typedef struct mbedtls_pk_rsassa_pss_options { 0089 /** The digest to use for MGF1 in PSS. 0090 * 0091 * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled and #MBEDTLS_RSA_C is 0092 * disabled, this must be equal to the \c md_alg argument passed 0093 * to mbedtls_pk_verify_ext(). In a future version of the library, 0094 * this constraint may apply whenever #MBEDTLS_USE_PSA_CRYPTO is 0095 * enabled regardless of the status of #MBEDTLS_RSA_C. 0096 */ 0097 mbedtls_md_type_t mgf1_hash_id; 0098 0099 /** The expected length of the salt, in bytes. This may be 0100 * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length. 0101 * 0102 * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled, only 0103 * #MBEDTLS_RSA_SALT_LEN_ANY is valid. Any other value may be 0104 * ignored (allowing any salt length). 0105 */ 0106 int expected_salt_len; 0107 0108 } mbedtls_pk_rsassa_pss_options; 0109 0110 /** 0111 * \brief Maximum size of a signature made by mbedtls_pk_sign(). 0112 */ 0113 /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature 0114 * size among the supported signature types. Do it by starting at 0, 0115 * then incrementally increasing to be large enough for each supported 0116 * signature mechanism. 0117 * 0118 * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled 0119 * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C 0120 * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT). 0121 */ 0122 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0 0123 0124 #if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \ 0125 MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE 0126 /* For RSA, the signature can be as large as the bignum module allows. 0127 * For RSA_ALT, the signature size is not necessarily tied to what the 0128 * bignum module can do, but in the absence of any specific setting, 0129 * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */ 0130 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE 0131 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE 0132 #endif 0133 0134 #if defined(MBEDTLS_ECDSA_C) && \ 0135 MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE 0136 /* For ECDSA, the ecdsa module exports a constant for the maximum 0137 * signature size. */ 0138 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE 0139 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN 0140 #endif 0141 0142 #if defined(MBEDTLS_USE_PSA_CRYPTO) 0143 #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE 0144 /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made 0145 * through the PSA API in the PSA representation. */ 0146 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE 0147 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE 0148 #endif 0149 0150 #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE 0151 /* The Mbed TLS representation is different for ECDSA signatures: 0152 * PSA uses the raw concatenation of r and s, 0153 * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs). 0154 * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the 0155 * types, lengths (represented by up to 2 bytes), and potential leading 0156 * zeros of the INTEGERs and the SEQUENCE. */ 0157 #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE 0158 #define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11) 0159 #endif 0160 #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */ 0161 0162 /* Internal helper to define which fields in the pk_context structure below 0163 * should be used for EC keys: legacy ecp_keypair or the raw (PSA friendly) 0164 * format. It should be noted that this only affects how data is stored, not 0165 * which functions are used for various operations. The overall picture looks 0166 * like this: 0167 * - if USE_PSA is not defined and ECP_C is defined then use ecp_keypair data 0168 * structure and legacy functions 0169 * - if USE_PSA is defined and 0170 * - if ECP_C then use ecp_keypair structure, convert data to a PSA friendly 0171 * format and use PSA functions 0172 * - if !ECP_C then use new raw data and PSA functions directly. 0173 * 0174 * The main reason for the "intermediate" (USE_PSA + ECP_C) above is that as long 0175 * as ECP_C is defined mbedtls_pk_ec() gives the user a read/write access to the 0176 * ecp_keypair structure inside the pk_context so they can modify it using 0177 * ECP functions which are not under PK module's control. 0178 */ 0179 #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \ 0180 !defined(MBEDTLS_ECP_C) 0181 #define MBEDTLS_PK_USE_PSA_EC_DATA 0182 #endif 0183 0184 /** 0185 * \brief Types for interfacing with the debug module 0186 */ 0187 typedef enum { 0188 MBEDTLS_PK_DEBUG_NONE = 0, 0189 MBEDTLS_PK_DEBUG_MPI, 0190 MBEDTLS_PK_DEBUG_ECP, 0191 MBEDTLS_PK_DEBUG_PSA_EC, 0192 } mbedtls_pk_debug_type; 0193 0194 /** 0195 * \brief Item to send to the debug module 0196 */ 0197 typedef struct mbedtls_pk_debug_item { 0198 mbedtls_pk_debug_type MBEDTLS_PRIVATE(type); 0199 const char *MBEDTLS_PRIVATE(name); 0200 void *MBEDTLS_PRIVATE(value); 0201 } mbedtls_pk_debug_item; 0202 0203 /** Maximum number of item send for debugging, plus 1 */ 0204 #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3 0205 0206 /** 0207 * \brief Public key information and operations 0208 * 0209 * \note The library does not support custom pk info structures, 0210 * only built-in structures returned by 0211 * mbedtls_cipher_info_from_type(). 0212 */ 0213 typedef struct mbedtls_pk_info_t mbedtls_pk_info_t; 0214 0215 #define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \ 0216 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) 0217 /** 0218 * \brief Public key container 0219 */ 0220 typedef struct mbedtls_pk_context { 0221 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */ 0222 void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */ 0223 /* The following field is used to store the ID of a private key in the 0224 * following cases: 0225 * - opaque key when MBEDTLS_USE_PSA_CRYPTO is defined 0226 * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case: 0227 * - the pk_ctx above is not not used to store the private key anymore. 0228 * Actually that field not populated at all in this case because also 0229 * the public key will be stored in raw format as explained below 0230 * - this ID is used for all private key operations (ex: sign, check 0231 * key pair, key write, etc) using PSA functions 0232 * 0233 * Note: this private key storing solution only affects EC keys, not the 0234 * other ones. The latters still use the pk_ctx to store their own 0235 * context. */ 0236 #if defined(MBEDTLS_USE_PSA_CRYPTO) 0237 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */ 0238 #endif /* MBEDTLS_USE_PSA_CRYPTO */ 0239 /* The following fields are meant for storing the public key in raw format 0240 * which is handy for: 0241 * - easily importing it into the PSA context 0242 * - reducing the ECP module dependencies in the PK one. 0243 * 0244 * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled: 0245 * - the pk_ctx above is not used anymore for storing the public key 0246 * inside the ecp_keypair structure 0247 * - the following fields are used for all public key operations: signature 0248 * verify, key pair check and key write. 0249 * - For a key pair, priv_id contains the private key. For a public key, 0250 * priv_id is null. 0251 * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy 0252 * ecp_keypair structure is used for storing the public key and performing 0253 * all the operations. 0254 * 0255 * Note: This new public key storing solution only works for EC keys, not 0256 * other ones. The latters still use pk_ctx to store their own 0257 * context. 0258 */ 0259 #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) 0260 uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key */ 0261 size_t MBEDTLS_PRIVATE(pub_raw_len); /**< Valid bytes in "pub_raw" */ 0262 psa_ecc_family_t MBEDTLS_PRIVATE(ec_family); /**< EC family of pk */ 0263 size_t MBEDTLS_PRIVATE(ec_bits); /**< Curve's bits of pk */ 0264 #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ 0265 } mbedtls_pk_context; 0266 0267 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 0268 /** 0269 * \brief Context for resuming operations 0270 */ 0271 typedef struct { 0272 const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */ 0273 void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */ 0274 } mbedtls_pk_restart_ctx; 0275 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 0276 /* Now we can declare functions that take a pointer to that */ 0277 typedef void mbedtls_pk_restart_ctx; 0278 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 0279 0280 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 0281 /** 0282 * \brief Types for RSA-alt abstraction 0283 */ 0284 typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen, 0285 const unsigned char *input, unsigned char *output, 0286 size_t output_max_len); 0287 typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx, 0288 int (*f_rng)(void *, unsigned char *, size_t), 0289 void *p_rng, 0290 mbedtls_md_type_t md_alg, unsigned int hashlen, 0291 const unsigned char *hash, unsigned char *sig); 0292 typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx); 0293 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ 0294 0295 /** 0296 * \brief Return information associated with the given PK type 0297 * 0298 * \param pk_type PK type to search for. 0299 * 0300 * \return The PK info associated with the type or NULL if not found. 0301 */ 0302 const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type); 0303 0304 /** 0305 * \brief Initialize a #mbedtls_pk_context (as NONE). 0306 * 0307 * \param ctx The context to initialize. 0308 * This must not be \c NULL. 0309 */ 0310 void mbedtls_pk_init(mbedtls_pk_context *ctx); 0311 0312 /** 0313 * \brief Free the components of a #mbedtls_pk_context. 0314 * 0315 * \param ctx The context to clear. It must have been initialized. 0316 * If this is \c NULL, this function does nothing. 0317 * 0318 * \note For contexts that have been set up with 0319 * mbedtls_pk_setup_opaque(), this does not free the underlying 0320 * PSA key and you still need to call psa_destroy_key() 0321 * independently if you want to destroy that key. 0322 */ 0323 void mbedtls_pk_free(mbedtls_pk_context *ctx); 0324 0325 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) 0326 /** 0327 * \brief Initialize a restart context 0328 * 0329 * \param ctx The context to initialize. 0330 * This must not be \c NULL. 0331 */ 0332 void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx); 0333 0334 /** 0335 * \brief Free the components of a restart context 0336 * 0337 * \param ctx The context to clear. It must have been initialized. 0338 * If this is \c NULL, this function does nothing. 0339 */ 0340 void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx); 0341 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ 0342 0343 /** 0344 * \brief Initialize a PK context with the information given 0345 * and allocates the type-specific PK subcontext. 0346 * 0347 * \param ctx Context to initialize. It must not have been set 0348 * up yet (type #MBEDTLS_PK_NONE). 0349 * \param info Information to use 0350 * 0351 * \return 0 on success, 0352 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input, 0353 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure. 0354 * 0355 * \note For contexts holding an RSA-alt key, use 0356 * \c mbedtls_pk_setup_rsa_alt() instead. 0357 */ 0358 int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info); 0359 0360 #if defined(MBEDTLS_USE_PSA_CRYPTO) 0361 /** 0362 * \brief Initialize a PK context to wrap a PSA key. 0363 * 0364 * This function creates a PK context which wraps a PSA key. The PSA wrapped 0365 * key must be an EC or RSA key pair (DH is not suported in the PK module). 0366 * 0367 * Under the hood PSA functions will be used to perform the required 0368 * operations and, based on the key type, used algorithms will be: 0369 * * EC: 0370 * * verify, verify_ext, sign, sign_ext: ECDSA. 0371 * * RSA: 0372 * * sign, decrypt: use the primary algorithm in the wrapped PSA key; 0373 * * sign_ext: RSA PSS if the pk_type is #MBEDTLS_PK_RSASSA_PSS, otherwise 0374 * it falls back to the sign() case; 0375 * * verify, verify_ext, encrypt: not supported. 0376 * 0377 * In order for the above operations to succeed, the policy of the wrapped PSA 0378 * key must allow the specified algorithm. 0379 * 0380 * Opaque PK contexts wrapping an EC keys also support \c mbedtls_pk_check_pair(), 0381 * whereas RSA ones do not. 0382 * 0383 * \warning The PSA wrapped key must remain valid as long as the wrapping PK 0384 * context is in use, that is at least between the point this function 0385 * is called and the point mbedtls_pk_free() is called on this context. 0386 * 0387 * \param ctx The context to initialize. It must be empty (type NONE). 0388 * \param key The PSA key to wrap, which must hold an ECC or RSA key pair. 0389 * 0390 * \return \c 0 on success. 0391 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input (context already 0392 * used, invalid key identifier). 0393 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an ECC or 0394 * RSA key pair. 0395 * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure. 0396 */ 0397 int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx, 0398 const mbedtls_svc_key_id_t key); 0399 #endif /* MBEDTLS_USE_PSA_CRYPTO */ 0400 0401 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 0402 /** 0403 * \brief Initialize an RSA-alt context 0404 * 0405 * \param ctx Context to initialize. It must not have been set 0406 * up yet (type #MBEDTLS_PK_NONE). 0407 * \param key RSA key pointer 0408 * \param decrypt_func Decryption function 0409 * \param sign_func Signing function 0410 * \param key_len_func Function returning key length in bytes 0411 * 0412 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the 0413 * context wasn't already initialized as RSA_ALT. 0414 * 0415 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt. 0416 */ 0417 int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, 0418 mbedtls_pk_rsa_alt_decrypt_func decrypt_func, 0419 mbedtls_pk_rsa_alt_sign_func sign_func, 0420 mbedtls_pk_rsa_alt_key_len_func key_len_func); 0421 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ 0422 0423 /** 0424 * \brief Get the size in bits of the underlying key 0425 * 0426 * \param ctx The context to query. It must have been initialized. 0427 * 0428 * \return Key size in bits, or 0 on error 0429 */ 0430 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx); 0431 0432 /** 0433 * \brief Get the length in bytes of the underlying key 0434 * 0435 * \param ctx The context to query. It must have been initialized. 0436 * 0437 * \return Key length in bytes, or 0 on error 0438 */ 0439 static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx) 0440 { 0441 return (mbedtls_pk_get_bitlen(ctx) + 7) / 8; 0442 } 0443 0444 /** 0445 * \brief Tell if a context can do the operation given by type 0446 * 0447 * \param ctx The context to query. It must have been initialized. 0448 * \param type The desired type. 0449 * 0450 * \return 1 if the context can do operations on the given type. 0451 * \return 0 if the context cannot do the operations on the given 0452 * type. This is always the case for a context that has 0453 * been initialized but not set up, or that has been 0454 * cleared with mbedtls_pk_free(). 0455 */ 0456 int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type); 0457 0458 #if defined(MBEDTLS_USE_PSA_CRYPTO) 0459 /** 0460 * \brief Tell if context can do the operation given by PSA algorithm 0461 * 0462 * \param ctx The context to query. It must have been initialized. 0463 * \param alg PSA algorithm to check against, the following are allowed: 0464 * PSA_ALG_RSA_PKCS1V15_SIGN(hash), 0465 * PSA_ALG_RSA_PSS(hash), 0466 * PSA_ALG_RSA_PKCS1V15_CRYPT, 0467 * PSA_ALG_ECDSA(hash), 0468 * PSA_ALG_ECDH, where hash is a specific hash. 0469 * \param usage PSA usage flag to check against, must be composed of: 0470 * PSA_KEY_USAGE_SIGN_HASH 0471 * PSA_KEY_USAGE_DECRYPT 0472 * PSA_KEY_USAGE_DERIVE. 0473 * Context key must match all passed usage flags. 0474 * 0475 * \warning Since the set of allowed algorithms and usage flags may be 0476 * expanded in the future, the return value \c 0 should not 0477 * be taken in account for non-allowed algorithms and usage 0478 * flags. 0479 * 0480 * \return 1 if the context can do operations on the given type. 0481 * \return 0 if the context cannot do the operations on the given 0482 * type, for non-allowed algorithms and usage flags, or 0483 * for a context that has been initialized but not set up 0484 * or that has been cleared with mbedtls_pk_free(). 0485 */ 0486 int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg, 0487 psa_key_usage_t usage); 0488 #endif /* MBEDTLS_USE_PSA_CRYPTO */ 0489 0490 #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) 0491 /** 0492 * \brief Determine valid PSA attributes that can be used to 0493 * import a key into PSA. 0494 * 0495 * The attributes determined by this function are suitable 0496 * for calling mbedtls_pk_import_into_psa() to create 0497 * a PSA key with the same key material. 0498 * 0499 * The typical flow of operations involving this function is 0500 * ``` 0501 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; 0502 * int ret = mbedtls_pk_get_psa_attributes(pk, &attributes); 0503 * if (ret != 0) ...; // error handling omitted 0504 * // Tweak attributes if desired 0505 * psa_key_id_t key_id = 0; 0506 * ret = mbedtls_pk_import_into_psa(pk, &attributes, &key_id); 0507 * if (ret != 0) ...; // error handling omitted 0508 * ``` 0509 * 0510 * \note This function does not support RSA-alt contexts 0511 * (set up with mbedtls_pk_setup_rsa_alt()). 0512 * 0513 * \param[in] pk The PK context to use. It must have been set up. 0514 * It can either contain a key pair or just a public key. 0515 * \param usage A single `PSA_KEY_USAGE_xxx` flag among the following: 0516 * - #PSA_KEY_USAGE_DECRYPT: \p pk must contain a 0517 * key pair. The output \p attributes will contain a 0518 * key pair type, and the usage policy will allow 0519 * #PSA_KEY_USAGE_ENCRYPT as well as 0520 * #PSA_KEY_USAGE_DECRYPT. 0521 * - #PSA_KEY_USAGE_DERIVE: \p pk must contain a 0522 * key pair. The output \p attributes will contain a 0523 * key pair type. 0524 * - #PSA_KEY_USAGE_ENCRYPT: The output 0525 * \p attributes will contain a public key type. 0526 * - #PSA_KEY_USAGE_SIGN_HASH: \p pk must contain a 0527 * key pair. The output \p attributes will contain a 0528 * key pair type, and the usage policy will allow 0529 * #PSA_KEY_USAGE_VERIFY_HASH as well as 0530 * #PSA_KEY_USAGE_SIGN_HASH. 0531 * - #PSA_KEY_USAGE_SIGN_MESSAGE: \p pk must contain a 0532 * key pair. The output \p attributes will contain a 0533 * key pair type, and the usage policy will allow 0534 * #PSA_KEY_USAGE_VERIFY_MESSAGE as well as 0535 * #PSA_KEY_USAGE_SIGN_MESSAGE. 0536 * - #PSA_KEY_USAGE_VERIFY_HASH: The output 0537 * \p attributes will contain a public key type. 0538 * - #PSA_KEY_USAGE_VERIFY_MESSAGE: The output 0539 * \p attributes will contain a public key type. 0540 * \param[out] attributes 0541 * On success, valid attributes to import the key into PSA. 0542 * - The lifetime and key identifier are unchanged. If the 0543 * attribute structure was initialized or reset before 0544 * calling this function, this will result in a volatile 0545 * key. Call psa_set_key_identifier() before or after this 0546 * function if you wish to create a persistent key. Call 0547 * psa_set_key_lifetime() before or after this function if 0548 * you wish to import the key in a secure element. 0549 * - The key type and bit-size are determined by the contents 0550 * of the PK context. If the PK context contains a key 0551 * pair, the key type can be either a key pair type or 0552 * the corresponding public key type, depending on 0553 * \p usage. If the PK context contains a public key, 0554 * the key type is a public key type. 0555 * - The key's policy is determined by the key type and 0556 * the \p usage parameter. The usage always allows 0557 * \p usage, exporting and copying the key, and 0558 * possibly other permissions as documented for the 0559 * \p usage parameter. 0560 * The permitted algorithm policy is determined as follows 0561 * based on the #mbedtls_pk_type_t type of \p pk, 0562 * the chosen \p usage and other factors: 0563 * - #MBEDTLS_PK_RSA whose underlying 0564 * #mbedtls_rsa_context has the padding mode 0565 * #MBEDTLS_RSA_PKCS_V15: 0566 * #PSA_ALG_RSA_PKCS1V15_SIGN(#PSA_ALG_ANY_HASH) 0567 * if \p usage is SIGN/VERIFY, and 0568 * #PSA_ALG_RSA_PKCS1V15_CRYPT 0569 * if \p usage is ENCRYPT/DECRYPT. 0570 * - #MBEDTLS_PK_RSA whose underlying 0571 * #mbedtls_rsa_context has the padding mode 0572 * #MBEDTLS_RSA_PKCS_V21 and the digest type 0573 * corresponding to the PSA algorithm \c hash: 0574 * #PSA_ALG_RSA_PSS_ANY_SALT(#PSA_ALG_ANY_HASH) 0575 * if \p usage is SIGN/VERIFY, and 0576 * #PSA_ALG_RSA_OAEP(\c hash) 0577 * if \p usage is ENCRYPT/DECRYPT. 0578 * - #MBEDTLS_PK_RSA_ALT: not supported. 0579 * - #MBEDTLS_PK_ECDSA or #MBEDTLS_PK_ECKEY 0580 * if \p usage is SIGN/VERIFY: 0581 * #PSA_ALG_DETERMINISTIC_ECDSA(#PSA_ALG_ANY_HASH) 0582 * if #MBEDTLS_ECDSA_DETERMINISTIC is enabled, 0583 * otherwise #PSA_ALG_ECDSA(#PSA_ALG_ANY_HASH). 0584 * - #MBEDTLS_PK_ECKEY_DH or #MBEDTLS_PK_ECKEY 0585 * if \p usage is DERIVE: 0586 * #PSA_ALG_ECDH. 0587 * - #MBEDTLS_PK_OPAQUE: same as the primary algorithm 0588 * set for the underlying PSA key, except that 0589 * sign/decrypt flags are removed if the type is 0590 * set to a public key type. 0591 * The underlying key must allow \p usage. 0592 * Note that the enrollment algorithm set with 0593 * psa_set_key_enrollment_algorithm() is not copied. 0594 * 0595 * \return 0 on success. 0596 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain 0597 * a key of the type identified in \p attributes. 0598 * Another error code on other failures. 0599 */ 0600 int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk, 0601 psa_key_usage_t usage, 0602 psa_key_attributes_t *attributes); 0603 0604 /** 0605 * \brief Import a key into the PSA key store. 0606 * 0607 * This function is equivalent to calling psa_import_key() 0608 * with the key material from \p pk. 0609 * 0610 * The typical way to use this function is: 0611 * -# Call mbedtls_pk_get_psa_attributes() to obtain 0612 * attributes for the given key. 0613 * -# If desired, modify the attributes, for example: 0614 * - To create a persistent key, call 0615 * psa_set_key_identifier() and optionally 0616 * psa_set_key_lifetime(). 0617 * - To import only the public part of a key pair: 0618 * 0619 * psa_set_key_type(&attributes, 0620 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( 0621 * psa_get_key_type(&attributes))); 0622 * - Restrict the key usage if desired. 0623 * -# Call mbedtls_pk_import_into_psa(). 0624 * 0625 * \note This function does not support RSA-alt contexts 0626 * (set up with mbedtls_pk_setup_rsa_alt()). 0627 * 0628 * \param[in] pk The PK context to use. It must have been set up. 0629 * It can either contain a key pair or just a public key. 0630 * \param[in] attributes 0631 * The attributes to use for the new key. They must be 0632 * compatible with \p pk. In particular, the key type 0633 * must match the content of \p pk. 0634 * If \p pk contains a key pair, the key type in 0635 * attributes can be either the key pair type or the 0636 * corresponding public key type (to import only the 0637 * public part). 0638 * \param[out] key_id 0639 * On success, the identifier of the newly created key. 0640 * On error, this is #MBEDTLS_SVC_KEY_ID_INIT. 0641 * 0642 * \return 0 on success. 0643 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain 0644 * a key of the type identified in \p attributes. 0645 * Another error code on other failures. 0646 */ 0647 int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk, 0648 const psa_key_attributes_t *attributes, 0649 mbedtls_svc_key_id_t *key_id); 0650 0651 /** 0652 * \brief Create a PK context starting from a key stored in PSA. 0653 * This key: 0654 * - must be exportable and 0655 * - must be an RSA or EC key pair or public key (FFDH is not supported in PK). 0656 * 0657 * The resulting PK object will be a transparent type: 0658 * - #MBEDTLS_PK_RSA for RSA keys or 0659 * - #MBEDTLS_PK_ECKEY for EC keys. 0660 * 0661 * Once this functions returns the PK object will be completely 0662 * independent from the original PSA key that it was generated 0663 * from. 0664 * Calling mbedtls_pk_sign(), mbedtls_pk_verify(), 0665 * mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting 0666 * PK context will perform the corresponding algorithm for that 0667 * PK context type. 0668 * * For ECDSA, the choice of deterministic vs randomized will 0669 * be based on the compile-time setting #MBEDTLS_ECDSA_DETERMINISTIC. 0670 * * For an RSA key, the output PK context will allow both 0671 * encrypt/decrypt and sign/verify regardless of the original 0672 * key's policy. 0673 * The original key's policy determines the output key's padding 0674 * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS, 0675 * otherwise PKCS1 v1.5 is set. 0676 * 0677 * \param key_id The key identifier of the key stored in PSA. 0678 * \param pk The PK context that will be filled. It must be initialized, 0679 * but not set up. 0680 * 0681 * \return 0 on success. 0682 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input 0683 * parameters are not correct. 0684 */ 0685 int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk); 0686 0687 /** 0688 * \brief Create a PK context for the public key of a PSA key. 0689 * 0690 * The key must be an RSA or ECC key. It can be either a 0691 * public key or a key pair, and only the public key is copied. 0692 * The resulting PK object will be a transparent type: 0693 * - #MBEDTLS_PK_RSA for RSA keys or 0694 * - #MBEDTLS_PK_ECKEY for EC keys. 0695 * 0696 * Once this functions returns the PK object will be completely 0697 * independent from the original PSA key that it was generated 0698 * from. 0699 * Calling mbedtls_pk_verify() or 0700 * mbedtls_pk_encrypt() on the resulting 0701 * PK context will perform the corresponding algorithm for that 0702 * PK context type. 0703 * 0704 * For an RSA key, the output PK context will allow both 0705 * encrypt and verify regardless of the original key's policy. 0706 * The original key's policy determines the output key's padding 0707 * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS, 0708 * otherwise PKCS1 v1.5 is set. 0709 * 0710 * \param key_id The key identifier of the key stored in PSA. 0711 * \param pk The PK context that will be filled. It must be initialized, 0712 * but not set up. 0713 * 0714 * \return 0 on success. 0715 * \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input 0716 * parameters are not correct. 0717 */ 0718 int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk); 0719 #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ 0720 0721 /** 0722 * \brief Verify signature (including padding if relevant). 0723 * 0724 * \param ctx The PK context to use. It must have been set up. 0725 * \param md_alg Hash algorithm used. 0726 * This can be #MBEDTLS_MD_NONE if the signature algorithm 0727 * does not rely on a hash algorithm (non-deterministic 0728 * ECDSA, RSA PKCS#1 v1.5). 0729 * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then 0730 * \p hash is the DigestInfo structure used by RFC 8017 0731 * §9.2 steps 3–6. If \p md_alg is a valid hash 0732 * algorithm then \p hash is the digest itself, and this 0733 * function calculates the DigestInfo encoding internally. 0734 * \param hash Hash of the message to sign 0735 * \param hash_len Hash length 0736 * \param sig Signature to verify 0737 * \param sig_len Signature length 0738 * 0739 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is 0740 * either PKCS#1 v1.5 or PSS (accepting any salt length), 0741 * depending on the padding mode in the underlying RSA context. 0742 * For a pk object constructed by parsing, this is PKCS#1 v1.5 0743 * by default. Use mbedtls_pk_verify_ext() to explicitly select 0744 * a different algorithm. 0745 * 0746 * \return 0 on success (signature is valid), 0747 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid 0748 * signature in \p sig but its length is less than \p sig_len, 0749 * or a specific error code. 0750 */ 0751 int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, 0752 const unsigned char *hash, size_t hash_len, 0753 const unsigned char *sig, size_t sig_len); 0754 0755 /** 0756 * \brief Restartable version of \c mbedtls_pk_verify() 0757 * 0758 * \note Performs the same job as \c mbedtls_pk_verify(), but can 0759 * return early and restart according to the limit set with 0760 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC 0761 * operations. For RSA, same as \c mbedtls_pk_verify(). 0762 * 0763 * \param ctx The PK context to use. It must have been set up. 0764 * \param md_alg Hash algorithm used (see notes) 0765 * \param hash Hash of the message to sign 0766 * \param hash_len Hash length or 0 (see notes) 0767 * \param sig Signature to verify 0768 * \param sig_len Signature length 0769 * \param rs_ctx Restart context (NULL to disable restart) 0770 * 0771 * \return See \c mbedtls_pk_verify(), or 0772 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of 0773 * operations was reached: see \c mbedtls_ecp_set_max_ops(). 0774 */ 0775 int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, 0776 mbedtls_md_type_t md_alg, 0777 const unsigned char *hash, size_t hash_len, 0778 const unsigned char *sig, size_t sig_len, 0779 mbedtls_pk_restart_ctx *rs_ctx); 0780 0781 /** 0782 * \brief Verify signature, with options. 0783 * (Includes verification of the padding depending on type.) 0784 * 0785 * \param type Signature type (inc. possible padding type) to verify 0786 * \param options Pointer to type-specific options, or NULL 0787 * \param ctx The PK context to use. It must have been set up. 0788 * \param md_alg Hash algorithm used (see notes) 0789 * \param hash Hash of the message to sign 0790 * \param hash_len Hash length or 0 (see notes) 0791 * \param sig Signature to verify 0792 * \param sig_len Signature length 0793 * 0794 * \return 0 on success (signature is valid), 0795 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be 0796 * used for this type of signatures, 0797 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid 0798 * signature in \p sig but its length is less than \p sig_len, 0799 * or a specific error code. 0800 * 0801 * \note If hash_len is 0, then the length associated with md_alg 0802 * is used instead, or an error returned if it is invalid. 0803 * 0804 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0 0805 * 0806 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point 0807 * to a mbedtls_pk_rsassa_pss_options structure, 0808 * otherwise it must be NULL. Note that if 0809 * #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not 0810 * verified as PSA_ALG_RSA_PSS_ANY_SALT is used. 0811 */ 0812 int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, 0813 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, 0814 const unsigned char *hash, size_t hash_len, 0815 const unsigned char *sig, size_t sig_len); 0816 0817 /** 0818 * \brief Make signature, including padding if relevant. 0819 * 0820 * \param ctx The PK context to use. It must have been set up 0821 * with a private key. 0822 * \param md_alg Hash algorithm used (see notes) 0823 * \param hash Hash of the message to sign 0824 * \param hash_len Hash length 0825 * \param sig Place to write the signature. 0826 * It must have enough room for the signature. 0827 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. 0828 * You may use a smaller buffer if it is large enough 0829 * given the key type. 0830 * \param sig_size The size of the \p sig buffer in bytes. 0831 * \param sig_len On successful return, 0832 * the number of bytes written to \p sig. 0833 * \param f_rng RNG function, must not be \c NULL. 0834 * \param p_rng RNG parameter 0835 * 0836 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is 0837 * either PKCS#1 v1.5 or PSS (using the largest possible salt 0838 * length up to the hash length), depending on the padding mode 0839 * in the underlying RSA context. For a pk object constructed 0840 * by parsing, this is PKCS#1 v1.5 by default. Use 0841 * mbedtls_pk_verify_ext() to explicitly select a different 0842 * algorithm. 0843 * 0844 * \return 0 on success, or a specific error code. 0845 * 0846 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0. 0847 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE. 0848 */ 0849 int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, 0850 const unsigned char *hash, size_t hash_len, 0851 unsigned char *sig, size_t sig_size, size_t *sig_len, 0852 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 0853 0854 /** 0855 * \brief Make signature given a signature type. 0856 * 0857 * \param pk_type Signature type. 0858 * \param ctx The PK context to use. It must have been set up 0859 * with a private key. 0860 * \param md_alg Hash algorithm used (see notes) 0861 * \param hash Hash of the message to sign 0862 * \param hash_len Hash length 0863 * \param sig Place to write the signature. 0864 * It must have enough room for the signature. 0865 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. 0866 * You may use a smaller buffer if it is large enough 0867 * given the key type. 0868 * \param sig_size The size of the \p sig buffer in bytes. 0869 * \param sig_len On successful return, 0870 * the number of bytes written to \p sig. 0871 * \param f_rng RNG function, must not be \c NULL. 0872 * \param p_rng RNG parameter 0873 * 0874 * \return 0 on success, or a specific error code. 0875 * 0876 * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS, 0877 * see #PSA_ALG_RSA_PSS for a description of PSS options used. 0878 * 0879 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0. 0880 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE. 0881 * 0882 */ 0883 int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type, 0884 mbedtls_pk_context *ctx, 0885 mbedtls_md_type_t md_alg, 0886 const unsigned char *hash, size_t hash_len, 0887 unsigned char *sig, size_t sig_size, size_t *sig_len, 0888 int (*f_rng)(void *, unsigned char *, size_t), 0889 void *p_rng); 0890 0891 /** 0892 * \brief Restartable version of \c mbedtls_pk_sign() 0893 * 0894 * \note Performs the same job as \c mbedtls_pk_sign(), but can 0895 * return early and restart according to the limit set with 0896 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC 0897 * operations. For RSA, same as \c mbedtls_pk_sign(). 0898 * 0899 * \param ctx The PK context to use. It must have been set up 0900 * with a private key. 0901 * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign()) 0902 * \param hash Hash of the message to sign 0903 * \param hash_len Hash length 0904 * \param sig Place to write the signature. 0905 * It must have enough room for the signature. 0906 * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. 0907 * You may use a smaller buffer if it is large enough 0908 * given the key type. 0909 * \param sig_size The size of the \p sig buffer in bytes. 0910 * \param sig_len On successful return, 0911 * the number of bytes written to \p sig. 0912 * \param f_rng RNG function, must not be \c NULL. 0913 * \param p_rng RNG parameter 0914 * \param rs_ctx Restart context (NULL to disable restart) 0915 * 0916 * \return See \c mbedtls_pk_sign(). 0917 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of 0918 * operations was reached: see \c mbedtls_ecp_set_max_ops(). 0919 */ 0920 int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, 0921 mbedtls_md_type_t md_alg, 0922 const unsigned char *hash, size_t hash_len, 0923 unsigned char *sig, size_t sig_size, size_t *sig_len, 0924 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 0925 mbedtls_pk_restart_ctx *rs_ctx); 0926 0927 /** 0928 * \brief Decrypt message (including padding if relevant). 0929 * 0930 * \param ctx The PK context to use. It must have been set up 0931 * with a private key. 0932 * \param input Input to decrypt 0933 * \param ilen Input size 0934 * \param output Decrypted output 0935 * \param olen Decrypted message length 0936 * \param osize Size of the output buffer 0937 * \param f_rng RNG function, must not be \c NULL. 0938 * \param p_rng RNG parameter 0939 * 0940 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is 0941 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in 0942 * the underlying RSA context. For a pk object constructed by 0943 * parsing, this is PKCS#1 v1.5 by default. 0944 * 0945 * \return 0 on success, or a specific error code. 0946 */ 0947 int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, 0948 const unsigned char *input, size_t ilen, 0949 unsigned char *output, size_t *olen, size_t osize, 0950 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 0951 0952 /** 0953 * \brief Encrypt message (including padding if relevant). 0954 * 0955 * \param ctx The PK context to use. It must have been set up. 0956 * \param input Message to encrypt 0957 * \param ilen Message size 0958 * \param output Encrypted output 0959 * \param olen Encrypted output length 0960 * \param osize Size of the output buffer 0961 * \param f_rng RNG function, must not be \c NULL. 0962 * \param p_rng RNG parameter 0963 * 0964 * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is 0965 * either PKCS#1 v1.5 or OAEP, depending on the padding mode in 0966 * the underlying RSA context. For a pk object constructed by 0967 * parsing, this is PKCS#1 v1.5 by default. 0968 * 0969 * \note \p f_rng is used for padding generation. 0970 * 0971 * \return 0 on success, or a specific error code. 0972 */ 0973 int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, 0974 const unsigned char *input, size_t ilen, 0975 unsigned char *output, size_t *olen, size_t osize, 0976 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 0977 0978 /** 0979 * \brief Check if a public-private pair of keys matches. 0980 * 0981 * \param pub Context holding a public key. 0982 * \param prv Context holding a private (and public) key. 0983 * \param f_rng RNG function, must not be \c NULL. 0984 * \param p_rng RNG parameter 0985 * 0986 * \return \c 0 on success (keys were checked and match each other). 0987 * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not 0988 * be checked - in that case they may or may not match. 0989 * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid. 0990 * \return Another non-zero value if the keys do not match. 0991 */ 0992 int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, 0993 const mbedtls_pk_context *prv, 0994 int (*f_rng)(void *, unsigned char *, size_t), 0995 void *p_rng); 0996 0997 /** 0998 * \brief Export debug information 0999 * 1000 * \param ctx The PK context to use. It must have been initialized. 1001 * \param items Place to write debug items 1002 * 1003 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA 1004 */ 1005 int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items); 1006 1007 /** 1008 * \brief Access the type name 1009 * 1010 * \param ctx The PK context to use. It must have been initialized. 1011 * 1012 * \return Type name on success, or "invalid PK" 1013 */ 1014 const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx); 1015 1016 /** 1017 * \brief Get the key type 1018 * 1019 * \param ctx The PK context to use. It must have been initialized. 1020 * 1021 * \return Type on success. 1022 * \return #MBEDTLS_PK_NONE for a context that has not been set up. 1023 */ 1024 mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx); 1025 1026 #if defined(MBEDTLS_RSA_C) 1027 /** 1028 * Quick access to an RSA context inside a PK context. 1029 * 1030 * \warning This function can only be used when the type of the context, as 1031 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_RSA. 1032 * Ensuring that is the caller's responsibility. 1033 * Alternatively, you can check whether this function returns NULL. 1034 * 1035 * \return The internal RSA context held by the PK context, or NULL. 1036 */ 1037 static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk) 1038 { 1039 switch (mbedtls_pk_get_type(&pk)) { 1040 case MBEDTLS_PK_RSA: 1041 return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx); 1042 default: 1043 return NULL; 1044 } 1045 } 1046 #endif /* MBEDTLS_RSA_C */ 1047 1048 #if defined(MBEDTLS_ECP_C) 1049 /** 1050 * Quick access to an EC context inside a PK context. 1051 * 1052 * \warning This function can only be used when the type of the context, as 1053 * returned by mbedtls_pk_get_type(), is #MBEDTLS_PK_ECKEY, 1054 * #MBEDTLS_PK_ECKEY_DH, or #MBEDTLS_PK_ECDSA. 1055 * Ensuring that is the caller's responsibility. 1056 * Alternatively, you can check whether this function returns NULL. 1057 * 1058 * \return The internal EC context held by the PK context, or NULL. 1059 */ 1060 static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk) 1061 { 1062 switch (mbedtls_pk_get_type(&pk)) { 1063 case MBEDTLS_PK_ECKEY: 1064 case MBEDTLS_PK_ECKEY_DH: 1065 case MBEDTLS_PK_ECDSA: 1066 return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); 1067 default: 1068 return NULL; 1069 } 1070 } 1071 #endif /* MBEDTLS_ECP_C */ 1072 1073 #if defined(MBEDTLS_PK_PARSE_C) 1074 /** \ingroup pk_module */ 1075 /** 1076 * \brief Parse a private key in PEM or DER format 1077 * 1078 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto 1079 * subsystem must have been initialized by calling 1080 * psa_crypto_init() before calling this function. 1081 * 1082 * \param ctx The PK context to fill. It must have been initialized 1083 * but not set up. 1084 * \param key Input buffer to parse. 1085 * The buffer must contain the input exactly, with no 1086 * extra trailing material. For PEM, the buffer must 1087 * contain a null-terminated string. 1088 * \param keylen Size of \b key in bytes. 1089 * For PEM data, this includes the terminating null byte, 1090 * so \p keylen must be equal to `strlen(key) + 1`. 1091 * \param pwd Optional password for decryption. 1092 * Pass \c NULL if expecting a non-encrypted key. 1093 * Pass a string of \p pwdlen bytes if expecting an encrypted 1094 * key; a non-encrypted key will also be accepted. 1095 * The empty password is not supported. 1096 * \param pwdlen Size of the password in bytes. 1097 * Ignored if \p pwd is \c NULL. 1098 * \param f_rng RNG function, must not be \c NULL. Used for blinding. 1099 * \param p_rng RNG parameter 1100 * 1101 * \note On entry, ctx must be empty, either freshly initialised 1102 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a 1103 * specific key type, check the result with mbedtls_pk_can_do(). 1104 * 1105 * \note The key is also checked for correctness. 1106 * 1107 * \return 0 if successful, or a specific PK or PEM error code 1108 */ 1109 int mbedtls_pk_parse_key(mbedtls_pk_context *ctx, 1110 const unsigned char *key, size_t keylen, 1111 const unsigned char *pwd, size_t pwdlen, 1112 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 1113 1114 /** \ingroup pk_module */ 1115 /** 1116 * \brief Parse a public key in PEM or DER format 1117 * 1118 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto 1119 * subsystem must have been initialized by calling 1120 * psa_crypto_init() before calling this function. 1121 * 1122 * \param ctx The PK context to fill. It must have been initialized 1123 * but not set up. 1124 * \param key Input buffer to parse. 1125 * The buffer must contain the input exactly, with no 1126 * extra trailing material. For PEM, the buffer must 1127 * contain a null-terminated string. 1128 * \param keylen Size of \b key in bytes. 1129 * For PEM data, this includes the terminating null byte, 1130 * so \p keylen must be equal to `strlen(key) + 1`. 1131 * 1132 * \note On entry, ctx must be empty, either freshly initialised 1133 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a 1134 * specific key type, check the result with mbedtls_pk_can_do(). 1135 * 1136 * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for 1137 * limitations. 1138 * 1139 * \note The key is also checked for correctness. 1140 * 1141 * \return 0 if successful, or a specific PK or PEM error code 1142 */ 1143 int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, 1144 const unsigned char *key, size_t keylen); 1145 1146 #if defined(MBEDTLS_FS_IO) 1147 /** \ingroup pk_module */ 1148 /** 1149 * \brief Load and parse a private key 1150 * 1151 * \note If #MBEDTLS_USE_PSA_CRYPTO is enabled, the PSA crypto 1152 * subsystem must have been initialized by calling 1153 * psa_crypto_init() before calling this function. 1154 * 1155 * \param ctx The PK context to fill. It must have been initialized 1156 * but not set up. 1157 * \param path filename to read the private key from 1158 * \param password Optional password to decrypt the file. 1159 * Pass \c NULL if expecting a non-encrypted key. 1160 * Pass a null-terminated string if expecting an encrypted 1161 * key; a non-encrypted key will also be accepted. 1162 * The empty password is not supported. 1163 * \param f_rng RNG function, must not be \c NULL. Used for blinding. 1164 * \param p_rng RNG parameter 1165 * 1166 * \note On entry, ctx must be empty, either freshly initialised 1167 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a 1168 * specific key type, check the result with mbedtls_pk_can_do(). 1169 * 1170 * \note The key is also checked for correctness. 1171 * 1172 * \return 0 if successful, or a specific PK or PEM error code 1173 */ 1174 int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx, 1175 const char *path, const char *password, 1176 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); 1177 1178 /** \ingroup pk_module */ 1179 /** 1180 * \brief Load and parse a public key 1181 * 1182 * \param ctx The PK context to fill. It must have been initialized 1183 * but not set up. 1184 * \param path filename to read the public key from 1185 * 1186 * \note On entry, ctx must be empty, either freshly initialised 1187 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If 1188 * you need a specific key type, check the result with 1189 * mbedtls_pk_can_do(). 1190 * 1191 * \note The key is also checked for correctness. 1192 * 1193 * \return 0 if successful, or a specific PK or PEM error code 1194 */ 1195 int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path); 1196 #endif /* MBEDTLS_FS_IO */ 1197 #endif /* MBEDTLS_PK_PARSE_C */ 1198 1199 #if defined(MBEDTLS_PK_WRITE_C) 1200 /** 1201 * \brief Write a private key to a PKCS#1 or SEC1 DER structure 1202 * Note: data is written at the end of the buffer! Use the 1203 * return value to determine where you should start 1204 * using the buffer 1205 * 1206 * \param ctx PK context which must contain a valid private key. 1207 * \param buf buffer to write to 1208 * \param size size of the buffer 1209 * 1210 * \return length of data written if successful, or a specific 1211 * error code 1212 */ 1213 int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); 1214 1215 /** 1216 * \brief Write a public key to a SubjectPublicKeyInfo DER structure 1217 * Note: data is written at the end of the buffer! Use the 1218 * return value to determine where you should start 1219 * using the buffer 1220 * 1221 * \param ctx PK context which must contain a valid public or private key. 1222 * \param buf buffer to write to 1223 * \param size size of the buffer 1224 * 1225 * \return length of data written if successful, or a specific 1226 * error code 1227 */ 1228 int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); 1229 1230 #if defined(MBEDTLS_PEM_WRITE_C) 1231 /** 1232 * \brief Write a public key to a PEM string 1233 * 1234 * \param ctx PK context which must contain a valid public or private key. 1235 * \param buf Buffer to write to. The output includes a 1236 * terminating null byte. 1237 * \param size Size of the buffer in bytes. 1238 * 1239 * \return 0 if successful, or a specific error code 1240 */ 1241 int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); 1242 1243 /** 1244 * \brief Write a private key to a PKCS#1 or SEC1 PEM string 1245 * 1246 * \param ctx PK context which must contain a valid private key. 1247 * \param buf Buffer to write to. The output includes a 1248 * terminating null byte. 1249 * \param size Size of the buffer in bytes. 1250 * 1251 * \return 0 if successful, or a specific error code 1252 */ 1253 int mbedtls_pk_write_key_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); 1254 #endif /* MBEDTLS_PEM_WRITE_C */ 1255 #endif /* MBEDTLS_PK_WRITE_C */ 1256 1257 /* 1258 * WARNING: Low-level functions. You probably do not want to use these unless 1259 * you are certain you do ;) 1260 */ 1261 1262 #if defined(MBEDTLS_PK_PARSE_C) 1263 /** 1264 * \brief Parse a SubjectPublicKeyInfo DER structure 1265 * 1266 * \param p the position in the ASN.1 data 1267 * \param end end of the buffer 1268 * \param pk The PK context to fill. It must have been initialized 1269 * but not set up. 1270 * 1271 * \return 0 if successful, or a specific PK error code 1272 */ 1273 int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, 1274 mbedtls_pk_context *pk); 1275 #endif /* MBEDTLS_PK_PARSE_C */ 1276 1277 #if defined(MBEDTLS_PK_WRITE_C) 1278 /** 1279 * \brief Write a subjectPublicKey to ASN.1 data 1280 * Note: function works backwards in data buffer 1281 * 1282 * \param p reference to current position pointer 1283 * \param start start of the buffer (for bounds-checking) 1284 * \param key PK context which must contain a valid public or private key. 1285 * 1286 * \return the length written or a negative error code 1287 */ 1288 int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start, 1289 const mbedtls_pk_context *key); 1290 #endif /* MBEDTLS_PK_WRITE_C */ 1291 1292 #ifdef __cplusplus 1293 } 1294 #endif 1295 1296 #endif /* MBEDTLS_PK_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |