Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file pk.h |
| 3 | * |
| 4 | * \brief Public Key abstraction layer |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 9 | */ |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 10 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #ifndef MBEDTLS_PK_H |
| 12 | #define MBEDTLS_PK_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 13 | #include "mbedtls/private_access.h" |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 14 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 15 | #include "mbedtls/build_info.h" |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 16 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 17 | #include "mbedtls/md.h" |
Manuel Pégourié-Gonnard | f73da02 | 2013-08-17 14:36:32 +0200 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_RSA_C) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 20 | #include "mbedtls/rsa.h" |
Manuel Pégourié-Gonnard | 244569f | 2013-07-10 09:46:30 +0200 | [diff] [blame] | 21 | #endif |
| 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_ECP_C) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 24 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 25 | #endif |
| 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_ECDSA_C) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 28 | #include "mbedtls/ecdsa.h" |
Manuel Pégourié-Gonnard | 211a64c | 2013-08-09 15:04:26 +0200 | [diff] [blame] | 29 | #endif |
| 30 | |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 31 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 32 | #include "psa/crypto.h" |
| 33 | #endif |
| 34 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 35 | /** Memory allocation failed. */ |
| 36 | #define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 |
| 37 | /** Type mismatch, eg attempt to encrypt with an ECDSA key */ |
| 38 | #define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 |
| 39 | /** Bad input parameters to function. */ |
| 40 | #define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 |
| 41 | /** Read/write of file failed. */ |
| 42 | #define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 |
| 43 | /** Unsupported key version */ |
| 44 | #define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 |
| 45 | /** Invalid key tag or value. */ |
| 46 | #define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 |
| 47 | /** Key algorithm is unsupported (only RSA and EC are supported). */ |
| 48 | #define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 |
| 49 | /** Private key password can't be empty. */ |
| 50 | #define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 |
| 51 | /** Given private key password does not allow for correct decryption. */ |
| 52 | #define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 |
| 53 | /** The pubkey tag or value is invalid (only RSA and EC are supported). */ |
| 54 | #define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 |
| 55 | /** The algorithm tag or value is invalid. */ |
| 56 | #define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 |
| 57 | /** Elliptic curve is unsupported (only NIST curves are supported). */ |
| 58 | #define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 |
| 59 | /** Unavailable feature, e.g. RSA disabled for RSA key. */ |
| 60 | #define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 |
| 61 | /** The buffer contains a valid signature followed by more data. */ |
| 62 | #define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 |
| 63 | /** The output buffer is too small. */ |
| 64 | #define MBEDTLS_ERR_PK_BUFFER_TOO_SMALL -0x3880 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 65 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 66 | #ifdef __cplusplus |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
| 70 | /** |
| 71 | * \brief Public key types |
| 72 | */ |
| 73 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | MBEDTLS_PK_NONE=0, |
| 75 | MBEDTLS_PK_RSA, |
| 76 | MBEDTLS_PK_ECKEY, |
| 77 | MBEDTLS_PK_ECKEY_DH, |
| 78 | MBEDTLS_PK_ECDSA, |
| 79 | MBEDTLS_PK_RSA_ALT, |
| 80 | MBEDTLS_PK_RSASSA_PSS, |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 81 | MBEDTLS_PK_OPAQUE, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | } mbedtls_pk_type_t; |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 84 | /** |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 85 | * \brief Options for RSASSA-PSS signature verification. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | * See \c mbedtls_rsa_rsassa_pss_verify_ext() |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 87 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | typedef struct mbedtls_pk_rsassa_pss_options { |
Gilles Peskine | 34c43a8 | 2023-02-02 23:06:37 +0100 | [diff] [blame] | 89 | /** The digest to use for MGF1 in PSS. |
| 90 | * |
| 91 | * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled and #MBEDTLS_RSA_C is |
| 92 | * disabled, this must be equal to the \c md_alg argument passed |
| 93 | * to mbedtls_pk_verify_ext(). In a future version of the library, |
| 94 | * this constraint may apply whenever #MBEDTLS_USE_PSA_CRYPTO is |
| 95 | * enabled regardless of the status of #MBEDTLS_RSA_C. |
| 96 | */ |
| 97 | mbedtls_md_type_t mgf1_hash_id; |
| 98 | |
| 99 | /** The expected length of the salt, in bytes. This may be |
| 100 | * #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length. |
| 101 | * |
| 102 | * \note When #MBEDTLS_USE_PSA_CRYPTO is enabled, only |
| 103 | * #MBEDTLS_RSA_SALT_LEN_ANY is valid. Any other value may be |
| 104 | * ignored (allowing any salt length). |
| 105 | */ |
| 106 | int expected_salt_len; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 107 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | } mbedtls_pk_rsassa_pss_options; |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 109 | |
| 110 | /** |
Gilles Peskine | da252be | 2019-11-05 16:23:49 +0100 | [diff] [blame] | 111 | * \brief Maximum size of a signature made by mbedtls_pk_sign(). |
| 112 | */ |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 113 | /* We need to set MBEDTLS_PK_SIGNATURE_MAX_SIZE to the maximum signature |
| 114 | * size among the supported signature types. Do it by starting at 0, |
| 115 | * then incrementally increasing to be large enough for each supported |
| 116 | * signature mechanism. |
| 117 | * |
| 118 | * The resulting value can be 0, for example if MBEDTLS_ECDH_C is enabled |
| 119 | * (which allows the pk module to be included) but neither MBEDTLS_ECDSA_C |
| 120 | * nor MBEDTLS_RSA_C nor any opaque signature mechanism (PSA or RSA_ALT). |
| 121 | */ |
| 122 | #define MBEDTLS_PK_SIGNATURE_MAX_SIZE 0 |
| 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | #if (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_RSA_ALT_SUPPORT)) && \ |
Gilles Peskine | b22a24b | 2019-11-05 16:56:39 +0100 | [diff] [blame] | 125 | MBEDTLS_MPI_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 126 | /* For RSA, the signature can be as large as the bignum module allows. |
| 127 | * For RSA_ALT, the signature size is not necessarily tied to what the |
| 128 | * bignum module can do, but in the absence of any specific setting, |
Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 129 | * we use that (rsa_alt_sign_wrap in library/pk_wrap.h will check). */ |
Gilles Peskine | b22a24b | 2019-11-05 16:56:39 +0100 | [diff] [blame] | 130 | #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE |
| 131 | #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE |
| 132 | #endif |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 133 | |
Gilles Peskine | b22a24b | 2019-11-05 16:56:39 +0100 | [diff] [blame] | 134 | #if defined(MBEDTLS_ECDSA_C) && \ |
| 135 | MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_PK_SIGNATURE_MAX_SIZE |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 136 | /* For ECDSA, the ecdsa module exports a constant for the maximum |
| 137 | * signature size. */ |
Gilles Peskine | b22a24b | 2019-11-05 16:56:39 +0100 | [diff] [blame] | 138 | #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE |
| 139 | #define MBEDTLS_PK_SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN |
| 140 | #endif |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 141 | |
| 142 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 143 | #if PSA_SIGNATURE_MAX_SIZE > MBEDTLS_PK_SIGNATURE_MAX_SIZE |
| 144 | /* PSA_SIGNATURE_MAX_SIZE is the maximum size of a signature made |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 145 | * through the PSA API in the PSA representation. */ |
| 146 | #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 147 | #define MBEDTLS_PK_SIGNATURE_MAX_SIZE PSA_SIGNATURE_MAX_SIZE |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | #if PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11 > MBEDTLS_PK_SIGNATURE_MAX_SIZE |
| 151 | /* The Mbed TLS representation is different for ECDSA signatures: |
Gilles Peskine | b22a24b | 2019-11-05 16:56:39 +0100 | [diff] [blame] | 152 | * PSA uses the raw concatenation of r and s, |
| 153 | * whereas Mbed TLS uses the ASN.1 representation (SEQUENCE of two INTEGERs). |
| 154 | * Add the overhead of ASN.1: up to (1+2) + 2 * (1+2+1) for the |
| 155 | * types, lengths (represented by up to 2 bytes), and potential leading |
| 156 | * zeros of the INTEGERs and the SEQUENCE. */ |
| 157 | #undef MBEDTLS_PK_SIGNATURE_MAX_SIZE |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | #define MBEDTLS_PK_SIGNATURE_MAX_SIZE (PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE + 11) |
Gilles Peskine | b22a24b | 2019-11-05 16:56:39 +0100 | [diff] [blame] | 159 | #endif |
Gilles Peskine | 5460565 | 2019-11-08 16:24:16 +0100 | [diff] [blame] | 160 | #endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */ |
Gilles Peskine | da252be | 2019-11-05 16:23:49 +0100 | [diff] [blame] | 161 | |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 162 | /* Internal helper to define which fields in the pk_context structure below |
| 163 | * should be used for EC keys: legacy ecp_keypair or the raw (PSA friendly) |
Manuel Pégourié-Gonnard | f7298cd | 2023-09-18 09:55:24 +0200 | [diff] [blame] | 164 | * format. It should be noted that this only affects how data is stored, not |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 165 | * which functions are used for various operations. The overall picture looks |
| 166 | * like this: |
Manuel Pégourié-Gonnard | f7298cd | 2023-09-18 09:55:24 +0200 | [diff] [blame] | 167 | * - if USE_PSA is not defined and ECP_C is defined then use ecp_keypair data |
| 168 | * structure and legacy functions |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 169 | * - if USE_PSA is defined and |
| 170 | * - if ECP_C then use ecp_keypair structure, convert data to a PSA friendly |
| 171 | * format and use PSA functions |
| 172 | * - if !ECP_C then use new raw data and PSA functions directly. |
| 173 | * |
| 174 | * The main reason for the "intermediate" (USE_PSA + ECP_C) above is that as long |
| 175 | * as ECP_C is defined mbedtls_pk_ec() gives the user a read/write access to the |
Manuel Pégourié-Gonnard | f7298cd | 2023-09-18 09:55:24 +0200 | [diff] [blame] | 176 | * ecp_keypair structure inside the pk_context so they can modify it using |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 177 | * ECP functions which are not under PK module's control. |
| 178 | */ |
| 179 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) && \ |
| 180 | !defined(MBEDTLS_ECP_C) |
| 181 | #define MBEDTLS_PK_USE_PSA_EC_DATA |
Manuel Pégourié-Gonnard | f7298cd | 2023-09-18 09:55:24 +0200 | [diff] [blame] | 182 | #endif |
Valerio Setti | a9aab1a | 2023-06-19 13:39:54 +0200 | [diff] [blame] | 183 | |
Valerio Setti | cf084ae | 2023-01-26 14:30:12 +0100 | [diff] [blame] | 184 | /** |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 185 | * \brief Types for interfacing with the debug module |
| 186 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | MBEDTLS_PK_DEBUG_NONE = 0, |
| 189 | MBEDTLS_PK_DEBUG_MPI, |
| 190 | MBEDTLS_PK_DEBUG_ECP, |
Valerio Setti | 92c3f36 | 2023-05-17 15:36:44 +0200 | [diff] [blame] | 191 | MBEDTLS_PK_DEBUG_PSA_EC, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | } mbedtls_pk_debug_type; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * \brief Item to send to the debug module |
| 196 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | typedef struct mbedtls_pk_debug_item { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 198 | mbedtls_pk_debug_type MBEDTLS_PRIVATE(type); |
| 199 | const char *MBEDTLS_PRIVATE(name); |
| 200 | void *MBEDTLS_PRIVATE(value); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | } mbedtls_pk_debug_item; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 202 | |
| 203 | /** Maximum number of item send for debugging, plus 1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #define MBEDTLS_PK_DEBUG_MAX_ITEMS 3 |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 205 | |
| 206 | /** |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 207 | * \brief Public key information and operations |
Gilles Peskine | 2e9d65f | 2021-08-31 23:05:19 +0200 | [diff] [blame] | 208 | * |
| 209 | * \note The library does not support custom pk info structures, |
| 210 | * only built-in structures returned by |
| 211 | * mbedtls_cipher_info_from_type(). |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 212 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | typedef struct mbedtls_pk_info_t mbedtls_pk_info_t; |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 214 | |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 215 | #define MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN \ |
| 216 | PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) |
Manuel Pégourié-Gonnard | d73b3c1 | 2013-08-12 17:06:05 +0200 | [diff] [blame] | 217 | /** |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 218 | * \brief Public key container |
| 219 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | typedef struct mbedtls_pk_context { |
| 221 | const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */ |
| 222 | void *MBEDTLS_PRIVATE(pk_ctx); /**< Underlying public key context */ |
Valerio Setti | 7ef8a8d | 2023-05-23 18:39:54 +0200 | [diff] [blame] | 223 | /* The following field is used to store the ID of a private key in the |
| 224 | * following cases: |
Tomi Fontanilles | bad170e | 2023-12-14 22:03:12 +0200 | [diff] [blame] | 225 | * - opaque key when MBEDTLS_USE_PSA_CRYPTO is defined |
Valerio Setti | 7ef8a8d | 2023-05-23 18:39:54 +0200 | [diff] [blame] | 226 | * - normal key when MBEDTLS_PK_USE_PSA_EC_DATA is defined. In this case: |
| 227 | * - the pk_ctx above is not not used to store the private key anymore. |
| 228 | * Actually that field not populated at all in this case because also |
| 229 | * the public key will be stored in raw format as explained below |
| 230 | * - this ID is used for all private key operations (ex: sign, check |
| 231 | * key pair, key write, etc) using PSA functions |
| 232 | * |
| 233 | * Note: this private key storing solution only affects EC keys, not the |
| 234 | * other ones. The latters still use the pk_ctx to store their own |
Tomi Fontanilles | bad170e | 2023-12-14 22:03:12 +0200 | [diff] [blame] | 235 | * context. */ |
| 236 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 4f387ef | 2023-05-02 14:15:59 +0200 | [diff] [blame] | 237 | mbedtls_svc_key_id_t MBEDTLS_PRIVATE(priv_id); /**< Key ID for opaque keys */ |
Tomi Fontanilles | bad170e | 2023-12-14 22:03:12 +0200 | [diff] [blame] | 238 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 239 | /* The following fields are meant for storing the public key in raw format |
| 240 | * which is handy for: |
| 241 | * - easily importing it into the PSA context |
| 242 | * - reducing the ECP module dependencies in the PK one. |
| 243 | * |
| 244 | * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled: |
| 245 | * - the pk_ctx above is not used anymore for storing the public key |
Valerio Setti | 7ef8a8d | 2023-05-23 18:39:54 +0200 | [diff] [blame] | 246 | * inside the ecp_keypair structure |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 247 | * - the following fields are used for all public key operations: signature |
| 248 | * verify, key pair check and key write. |
Gilles Peskine | cb3b4ca | 2024-02-02 13:12:39 +0100 | [diff] [blame] | 249 | * - For a key pair, priv_id contains the private key. For a public key, |
| 250 | * priv_id is null. |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 251 | * Of course, when MBEDTLS_PK_USE_PSA_EC_DATA is not enabled, the legacy |
Valerio Setti | 016264b | 2023-05-22 18:40:35 +0200 | [diff] [blame] | 252 | * ecp_keypair structure is used for storing the public key and performing |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 253 | * all the operations. |
| 254 | * |
| 255 | * Note: This new public key storing solution only works for EC keys, not |
Valerio Setti | f57007d | 2023-05-19 13:54:39 +0200 | [diff] [blame] | 256 | * other ones. The latters still use pk_ctx to store their own |
Valerio Setti | 722f8f7 | 2023-05-17 15:31:21 +0200 | [diff] [blame] | 257 | * context. |
| 258 | */ |
| 259 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 260 | uint8_t MBEDTLS_PRIVATE(pub_raw)[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; /**< Raw public key */ |
| 261 | size_t MBEDTLS_PRIVATE(pub_raw_len); /**< Valid bytes in "pub_raw" */ |
| 262 | psa_ecc_family_t MBEDTLS_PRIVATE(ec_family); /**< EC family of pk */ |
| 263 | size_t MBEDTLS_PRIVATE(ec_bits); /**< Curve's bits of pk */ |
Valerio Setti | c1541cb | 2023-05-17 15:49:55 +0200 | [diff] [blame] | 264 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | } mbedtls_pk_context; |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 266 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 267 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 268 | /** |
| 269 | * \brief Context for resuming operations |
| 270 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | typedef struct { |
| 272 | const mbedtls_pk_info_t *MBEDTLS_PRIVATE(pk_info); /**< Public key information */ |
| 273 | void *MBEDTLS_PRIVATE(rs_ctx); /**< Underlying restart context */ |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 274 | } mbedtls_pk_restart_ctx; |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 275 | #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 276 | /* Now we can declare functions that take a pointer to that */ |
| 277 | typedef void mbedtls_pk_restart_ctx; |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 278 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 281 | /** |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 282 | * \brief Types for RSA-alt abstraction |
| 283 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | typedef int (*mbedtls_pk_rsa_alt_decrypt_func)(void *ctx, size_t *olen, |
| 285 | const unsigned char *input, unsigned char *output, |
| 286 | size_t output_max_len); |
| 287 | typedef int (*mbedtls_pk_rsa_alt_sign_func)(void *ctx, |
| 288 | int (*f_rng)(void *, unsigned char *, size_t), |
| 289 | void *p_rng, |
| 290 | mbedtls_md_type_t md_alg, unsigned int hashlen, |
| 291 | const unsigned char *hash, unsigned char *sig); |
| 292 | typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)(void *ctx); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 294 | |
| 295 | /** |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 296 | * \brief Return information associated with the given PK type |
| 297 | * |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 298 | * \param pk_type PK type to search for. |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 299 | * |
| 300 | * \return The PK info associated with the type or NULL if not found. |
| 301 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type); |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 303 | |
| 304 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 305 | * \brief Initialize a #mbedtls_pk_context (as NONE). |
| 306 | * |
| 307 | * \param ctx The context to initialize. |
| 308 | * This must not be \c NULL. |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 309 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | void mbedtls_pk_init(mbedtls_pk_context *ctx); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 311 | |
| 312 | /** |
Andrzej Kurek | b274f27 | 2019-02-05 05:06:35 -0500 | [diff] [blame] | 313 | * \brief Free the components of a #mbedtls_pk_context. |
Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 314 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 315 | * \param ctx The context to clear. It must have been initialized. |
| 316 | * If this is \c NULL, this function does nothing. |
| 317 | * |
Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 318 | * \note For contexts that have been set up with |
Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 319 | * mbedtls_pk_setup_opaque(), this does not free the underlying |
Gilles Peskine | 1139249 | 2019-05-27 14:53:19 +0200 | [diff] [blame] | 320 | * PSA key and you still need to call psa_destroy_key() |
Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 321 | * independently if you want to destroy that key. |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 322 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | void mbedtls_pk_free(mbedtls_pk_context *ctx); |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 324 | |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 325 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 326 | /** |
| 327 | * \brief Initialize a restart context |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 328 | * |
| 329 | * \param ctx The context to initialize. |
| 330 | * This must not be \c NULL. |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 331 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx); |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 333 | |
| 334 | /** |
| 335 | * \brief Free the components of a restart context |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 336 | * |
| 337 | * \param ctx The context to clear. It must have been initialized. |
| 338 | * If this is \c NULL, this function does nothing. |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 339 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx); |
Manuel Pégourié-Gonnard | aaa9814 | 2017-08-18 17:30:37 +0200 | [diff] [blame] | 341 | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 342 | |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 343 | /** |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 344 | * \brief Initialize a PK context with the information given |
| 345 | * and allocates the type-specific PK subcontext. |
| 346 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 347 | * \param ctx Context to initialize. It must not have been set |
| 348 | * up yet (type #MBEDTLS_PK_NONE). |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 349 | * \param info Information to use |
| 350 | * |
| 351 | * \return 0 on success, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 353 | * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure. |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 354 | * |
Paul Bakker | 42099c3 | 2014-01-27 11:45:49 +0100 | [diff] [blame] | 355 | * \note For contexts holding an RSA-alt key, use |
Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 356 | * \c mbedtls_pk_setup_rsa_alt() instead. |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 357 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info); |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 359 | |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 360 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 361 | /** |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 362 | * \brief Initialize a PK context to wrap a PSA key. |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 363 | * |
Valerio Setti | f9f63ed | 2024-03-25 09:37:47 +0100 | [diff] [blame] | 364 | * This function creates a PK context which wraps a PSA key. The PSA wrapped |
| 365 | * key must be an EC or RSA key pair (DH is not suported in the PK module). |
Valerio Setti | f5a6e22 | 2024-03-18 11:06:44 +0100 | [diff] [blame] | 366 | * |
Valerio Setti | f9f63ed | 2024-03-25 09:37:47 +0100 | [diff] [blame] | 367 | * Under the hood PSA functions will be used to perform the required |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 368 | * operations and, based on the key type, used algorithms will be: |
| 369 | * * EC: |
| 370 | * * verify, verify_ext, sign, sign_ext: ECDSA. |
| 371 | * * RSA: |
Valerio Setti | 42a3954 | 2024-03-21 16:22:24 +0100 | [diff] [blame] | 372 | * * sign, decrypt: use the primary algorithm in the wrapped PSA key; |
| 373 | * * sign_ext: RSA PSS if the pk_type is #MBEDTLS_PK_RSASSA_PSS, otherwise |
| 374 | * it falls back to the sign() case; |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 375 | * * verify, verify_ext, encrypt: not supported. |
Valerio Setti | 80cd479 | 2024-03-20 15:58:54 +0100 | [diff] [blame] | 376 | * |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 377 | * In order for the above operations to succeed, the policy of the wrapped PSA |
| 378 | * key must allow the specified algorithm. |
Manuel Pégourié-Gonnard | 392dc04 | 2018-11-13 10:48:23 +0100 | [diff] [blame] | 379 | * |
Valerio Setti | ac81e23 | 2024-03-21 16:49:02 +0100 | [diff] [blame] | 380 | * Opaque PK contexts wrapping an EC keys also support \c mbedtls_pk_check_pair(), |
| 381 | * whereas RSA ones do not. |
| 382 | * |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 383 | * \warning The PSA wrapped key must remain valid as long as the wrapping PK |
| 384 | * context is in use, that is at least between the point this function |
| 385 | * is called and the point mbedtls_pk_free() is called on this context. |
Valerio Setti | 80cd479 | 2024-03-20 15:58:54 +0100 | [diff] [blame] | 386 | * |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 387 | * \param ctx The context to initialize. It must be empty (type NONE). |
| 388 | * \param key The PSA key to wrap, which must hold an ECC or RSA key pair. |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 389 | * |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 390 | * \return \c 0 on success. |
| 391 | * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input (context already |
| 392 | * used, invalid key identifier). |
Valerio Setti | 1c7f5de | 2024-04-04 09:39:12 +0200 | [diff] [blame] | 393 | * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the key is not an ECC or |
| 394 | * RSA key pair. |
Valerio Setti | fc6b22c | 2024-03-20 16:08:08 +0100 | [diff] [blame] | 395 | * \return #MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure. |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 396 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx, |
| 398 | const mbedtls_svc_key_id_t key); |
Manuel Pégourié-Gonnard | 20678b2 | 2018-10-22 12:11:15 +0200 | [diff] [blame] | 399 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
Manuel Pégourié-Gonnard | ab46694 | 2013-08-15 11:30:27 +0200 | [diff] [blame] | 402 | /** |
Paul Bakker | 4fc090a | 2013-09-18 15:43:25 +0200 | [diff] [blame] | 403 | * \brief Initialize an RSA-alt context |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 404 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 405 | * \param ctx Context to initialize. It must not have been set |
| 406 | * up yet (type #MBEDTLS_PK_NONE). |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 407 | * \param key RSA key pointer |
| 408 | * \param decrypt_func Decryption function |
| 409 | * \param sign_func Signing function |
Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 410 | * \param key_len_func Function returning key length in bytes |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 411 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the |
Manuel Pégourié-Gonnard | 12c1ff0 | 2013-08-21 12:28:31 +0200 | [diff] [blame] | 413 | * context wasn't already initialized as RSA_ALT. |
| 414 | * |
Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 415 | * \note This function replaces \c mbedtls_pk_setup() for RSA-alt. |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 416 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key, |
| 418 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func, |
| 419 | mbedtls_pk_rsa_alt_sign_func sign_func, |
| 420 | mbedtls_pk_rsa_alt_key_len_func key_len_func); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 422 | |
| 423 | /** |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 424 | * \brief Get the size in bits of the underlying key |
| 425 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 426 | * \param ctx The context to query. It must have been initialized. |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 427 | * |
| 428 | * \return Key size in bits, or 0 on error |
| 429 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 431 | |
| 432 | /** |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 433 | * \brief Get the length in bytes of the underlying key |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 434 | * |
| 435 | * \param ctx The context to query. It must have been initialized. |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 436 | * |
Paul Bakker | 4fc090a | 2013-09-18 15:43:25 +0200 | [diff] [blame] | 437 | * \return Key length in bytes, or 0 on error |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 438 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 440 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | return (mbedtls_pk_get_bitlen(ctx) + 7) / 8; |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 445 | * \brief Tell if a context can do the operation given by type |
| 446 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 447 | * \param ctx The context to query. It must have been initialized. |
| 448 | * \param type The desired type. |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 449 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 450 | * \return 1 if the context can do operations on the given type. |
| 451 | * \return 0 if the context cannot do the operations on the given |
| 452 | * type. This is always the case for a context that has |
| 453 | * been initialized but not set up, or that has been |
| 454 | * cleared with mbedtls_pk_free(). |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 455 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 457 | |
Neil Armstrong | 0b52958 | 2022-05-11 10:10:20 +0200 | [diff] [blame] | 458 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 459 | /** |
Neil Armstrong | cec133a | 2022-05-17 11:56:01 +0200 | [diff] [blame] | 460 | * \brief Tell if context can do the operation given by PSA algorithm |
Neil Armstrong | 0b52958 | 2022-05-11 10:10:20 +0200 | [diff] [blame] | 461 | * |
| 462 | * \param ctx The context to query. It must have been initialized. |
| 463 | * \param alg PSA algorithm to check against, the following are allowed: |
| 464 | * PSA_ALG_RSA_PKCS1V15_SIGN(hash), |
| 465 | * PSA_ALG_RSA_PSS(hash), |
| 466 | * PSA_ALG_RSA_PKCS1V15_CRYPT, |
| 467 | * PSA_ALG_ECDSA(hash), |
| 468 | * PSA_ALG_ECDH, where hash is a specific hash. |
Valerio Setti | c4c1d3a | 2024-03-12 13:09:23 +0100 | [diff] [blame] | 469 | * \param usage PSA usage flag to check against, must be composed of: |
Neil Armstrong | 408f6a6 | 2022-05-17 14:23:20 +0200 | [diff] [blame] | 470 | * PSA_KEY_USAGE_SIGN_HASH |
| 471 | * PSA_KEY_USAGE_DECRYPT |
| 472 | * PSA_KEY_USAGE_DERIVE. |
| 473 | * Context key must match all passed usage flags. |
Neil Armstrong | 0b52958 | 2022-05-11 10:10:20 +0200 | [diff] [blame] | 474 | * |
Neil Armstrong | b2f2b02 | 2022-05-20 12:00:56 +0200 | [diff] [blame] | 475 | * \warning Since the set of allowed algorithms and usage flags may be |
| 476 | * expanded in the future, the return value \c 0 should not |
| 477 | * be taken in account for non-allowed algorithms and usage |
| 478 | * flags. |
| 479 | * |
Neil Armstrong | 0b52958 | 2022-05-11 10:10:20 +0200 | [diff] [blame] | 480 | * \return 1 if the context can do operations on the given type. |
| 481 | * \return 0 if the context cannot do the operations on the given |
Neil Armstrong | b2f2b02 | 2022-05-20 12:00:56 +0200 | [diff] [blame] | 482 | * type, for non-allowed algorithms and usage flags, or |
| 483 | * for a context that has been initialized but not set up |
| 484 | * or that has been cleared with mbedtls_pk_free(). |
Neil Armstrong | 0b52958 | 2022-05-11 10:10:20 +0200 | [diff] [blame] | 485 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg, |
| 487 | psa_key_usage_t usage); |
Neil Armstrong | 0b52958 | 2022-05-11 10:10:20 +0200 | [diff] [blame] | 488 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 489 | |
Valerio Setti | c4c1d3a | 2024-03-12 13:09:23 +0100 | [diff] [blame] | 490 | #if defined(MBEDTLS_PSA_CRYPTO_CLIENT) |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 491 | /** |
| 492 | * \brief Determine valid PSA attributes that can be used to |
| 493 | * import a key into PSA. |
| 494 | * |
Gilles Peskine | ddbe4ae | 2024-03-04 18:30:09 +0100 | [diff] [blame] | 495 | * The attributes determined by this function are suitable |
| 496 | * for calling mbedtls_pk_import_into_psa() to create |
| 497 | * a PSA key with the same key material. |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 498 | * |
Gilles Peskine | ddbe4ae | 2024-03-04 18:30:09 +0100 | [diff] [blame] | 499 | * The typical flow of operations involving this function is |
| 500 | * ``` |
| 501 | * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 502 | * int ret = mbedtls_pk_get_psa_attributes(pk, &attributes); |
| 503 | * if (ret != 0) ...; // error handling omitted |
| 504 | * // Tweak attributes if desired |
| 505 | * psa_key_id_t key_id = 0; |
| 506 | * ret = mbedtls_pk_import_into_psa(pk, &attributes, &key_id); |
| 507 | * if (ret != 0) ...; // error handling omitted |
| 508 | * ``` |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 509 | * |
| 510 | * \note This function does not support RSA-alt contexts |
| 511 | * (set up with mbedtls_pk_setup_rsa_alt()). |
| 512 | * |
| 513 | * \param[in] pk The PK context to use. It must have been set up. |
| 514 | * It can either contain a key pair or just a public key. |
| 515 | * \param usage A single `PSA_KEY_USAGE_xxx` flag among the following: |
| 516 | * - #PSA_KEY_USAGE_DECRYPT: \p pk must contain a |
| 517 | * key pair. The output \p attributes will contain a |
| 518 | * key pair type, and the usage policy will allow |
| 519 | * #PSA_KEY_USAGE_ENCRYPT as well as |
| 520 | * #PSA_KEY_USAGE_DECRYPT. |
| 521 | * - #PSA_KEY_USAGE_DERIVE: \p pk must contain a |
| 522 | * key pair. The output \p attributes will contain a |
| 523 | * key pair type. |
| 524 | * - #PSA_KEY_USAGE_ENCRYPT: The output |
| 525 | * \p attributes will contain a public key type. |
| 526 | * - #PSA_KEY_USAGE_SIGN_HASH: \p pk must contain a |
| 527 | * key pair. The output \p attributes will contain a |
| 528 | * key pair type, and the usage policy will allow |
| 529 | * #PSA_KEY_USAGE_VERIFY_HASH as well as |
| 530 | * #PSA_KEY_USAGE_SIGN_HASH. |
| 531 | * - #PSA_KEY_USAGE_SIGN_MESSAGE: \p pk must contain a |
| 532 | * key pair. The output \p attributes will contain a |
| 533 | * key pair type, and the usage policy will allow |
| 534 | * #PSA_KEY_USAGE_VERIFY_MESSAGE as well as |
| 535 | * #PSA_KEY_USAGE_SIGN_MESSAGE. |
| 536 | * - #PSA_KEY_USAGE_VERIFY_HASH: The output |
| 537 | * \p attributes will contain a public key type. |
| 538 | * - #PSA_KEY_USAGE_VERIFY_MESSAGE: The output |
| 539 | * \p attributes will contain a public key type. |
| 540 | * \param[out] attributes |
| 541 | * On success, valid attributes to import the key into PSA. |
| 542 | * - The lifetime and key identifier are unchanged. If the |
| 543 | * attribute structure was initialized or reset before |
| 544 | * calling this function, this will result in a volatile |
| 545 | * key. Call psa_set_key_identifier() before or after this |
| 546 | * function if you wish to create a persistent key. Call |
| 547 | * psa_set_key_lifetime() before or after this function if |
| 548 | * you wish to import the key in a secure element. |
| 549 | * - The key type and bit-size are determined by the contents |
| 550 | * of the PK context. If the PK context contains a key |
| 551 | * pair, the key type can be either a key pair type or |
| 552 | * the corresponding public key type, depending on |
| 553 | * \p usage. If the PK context contains a public key, |
| 554 | * the key type is a public key type. |
| 555 | * - The key's policy is determined by the key type and |
| 556 | * the \p usage parameter. The usage always allows |
| 557 | * \p usage, exporting and copying the key, and |
| 558 | * possibly other permissions as documented for the |
| 559 | * \p usage parameter. |
Gilles Peskine | e208b25 | 2024-02-01 20:42:21 +0100 | [diff] [blame] | 560 | * The permitted algorithm policy is determined as follows |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 561 | * based on the #mbedtls_pk_type_t type of \p pk, |
| 562 | * the chosen \p usage and other factors: |
Gilles Peskine | e208b25 | 2024-02-01 20:42:21 +0100 | [diff] [blame] | 563 | * - #MBEDTLS_PK_RSA whose underlying |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 564 | * #mbedtls_rsa_context has the padding mode |
| 565 | * #MBEDTLS_RSA_PKCS_V15: |
| 566 | * #PSA_ALG_RSA_PKCS1V15_SIGN(#PSA_ALG_ANY_HASH) |
| 567 | * if \p usage is SIGN/VERIFY, and |
| 568 | * #PSA_ALG_RSA_PKCS1V15_CRYPT |
| 569 | * if \p usage is ENCRYPT/DECRYPT. |
Gilles Peskine | e208b25 | 2024-02-01 20:42:21 +0100 | [diff] [blame] | 570 | * - #MBEDTLS_PK_RSA whose underlying |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 571 | * #mbedtls_rsa_context has the padding mode |
| 572 | * #MBEDTLS_RSA_PKCS_V21 and the digest type |
| 573 | * corresponding to the PSA algorithm \c hash: |
| 574 | * #PSA_ALG_RSA_PSS_ANY_SALT(#PSA_ALG_ANY_HASH) |
| 575 | * if \p usage is SIGN/VERIFY, and |
| 576 | * #PSA_ALG_RSA_OAEP(\c hash) |
| 577 | * if \p usage is ENCRYPT/DECRYPT. |
| 578 | * - #MBEDTLS_PK_RSA_ALT: not supported. |
| 579 | * - #MBEDTLS_PK_ECDSA or #MBEDTLS_PK_ECKEY |
| 580 | * if \p usage is SIGN/VERIFY: |
| 581 | * #PSA_ALG_DETERMINISTIC_ECDSA(#PSA_ALG_ANY_HASH) |
| 582 | * if #MBEDTLS_ECDSA_DETERMINISTIC is enabled, |
| 583 | * otherwise #PSA_ALG_ECDSA(#PSA_ALG_ANY_HASH). |
| 584 | * - #MBEDTLS_PK_ECKEY_DH or #MBEDTLS_PK_ECKEY |
| 585 | * if \p usage is DERIVE: |
| 586 | * #PSA_ALG_ECDH. |
Gilles Peskine | e208b25 | 2024-02-01 20:42:21 +0100 | [diff] [blame] | 587 | * - #MBEDTLS_PK_OPAQUE: same as the primary algorithm |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 588 | * set for the underlying PSA key, except that |
| 589 | * sign/decrypt flags are removed if the type is |
| 590 | * set to a public key type. |
Gilles Peskine | 793920c | 2024-02-01 21:26:54 +0100 | [diff] [blame] | 591 | * The underlying key must allow \p usage. |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 592 | * Note that the enrollment algorithm set with |
| 593 | * psa_set_key_enrollment_algorithm() is not copied. |
| 594 | * |
| 595 | * \return 0 on success. |
| 596 | * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain |
| 597 | * a key of the type identified in \p attributes. |
| 598 | * Another error code on other failures. |
| 599 | */ |
| 600 | int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk, |
| 601 | psa_key_usage_t usage, |
| 602 | psa_key_attributes_t *attributes); |
Gilles Peskine | 05ee3fb | 2024-02-07 18:58:10 +0100 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * \brief Import a key into the PSA key store. |
| 606 | * |
Gilles Peskine | ddbe4ae | 2024-03-04 18:30:09 +0100 | [diff] [blame] | 607 | * This function is equivalent to calling psa_import_key() |
| 608 | * with the key material from \p pk. |
Gilles Peskine | 05ee3fb | 2024-02-07 18:58:10 +0100 | [diff] [blame] | 609 | * |
Gilles Peskine | ddbe4ae | 2024-03-04 18:30:09 +0100 | [diff] [blame] | 610 | * The typical way to use this function is: |
| 611 | * -# Call mbedtls_pk_get_psa_attributes() to obtain |
| 612 | * attributes for the given key. |
| 613 | * -# If desired, modify the attributes, for example: |
| 614 | * - To create a persistent key, call |
| 615 | * psa_set_key_identifier() and optionally |
| 616 | * psa_set_key_lifetime(). |
| 617 | * - To import only the public part of a key pair: |
| 618 | * |
| 619 | * psa_set_key_type(&attributes, |
| 620 | * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( |
| 621 | * psa_get_key_type(&attributes))); |
| 622 | * - Restrict the key usage if desired. |
| 623 | * -# Call mbedtls_pk_import_into_psa(). |
Gilles Peskine | 05ee3fb | 2024-02-07 18:58:10 +0100 | [diff] [blame] | 624 | * |
| 625 | * \note This function does not support RSA-alt contexts |
| 626 | * (set up with mbedtls_pk_setup_rsa_alt()). |
| 627 | * |
| 628 | * \param[in] pk The PK context to use. It must have been set up. |
| 629 | * It can either contain a key pair or just a public key. |
| 630 | * \param[in] attributes |
| 631 | * The attributes to use for the new key. They must be |
| 632 | * compatible with \p pk. In particular, the key type |
| 633 | * must match the content of \p pk. |
| 634 | * If \p pk contains a key pair, the key type in |
| 635 | * attributes can be either the key pair type or the |
| 636 | * corresponding public key type (to import only the |
| 637 | * public part). |
| 638 | * \param[out] key_id |
| 639 | * On success, the identifier of the newly created key. |
| 640 | * On error, this is #MBEDTLS_SVC_KEY_ID_INIT. |
| 641 | * |
| 642 | * \return 0 on success. |
| 643 | * #MBEDTLS_ERR_PK_TYPE_MISMATCH if \p pk does not contain |
| 644 | * a key of the type identified in \p attributes. |
| 645 | * Another error code on other failures. |
| 646 | */ |
| 647 | int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk, |
| 648 | const psa_key_attributes_t *attributes, |
| 649 | mbedtls_svc_key_id_t *key_id); |
Valerio Setti | c4c1d3a | 2024-03-12 13:09:23 +0100 | [diff] [blame] | 650 | |
| 651 | /** |
| 652 | * \brief Create a PK context starting from a key stored in PSA. |
| 653 | * This key: |
| 654 | * - must be exportable and |
| 655 | * - must be an RSA or EC key pair or public key (FFDH is not supported in PK). |
| 656 | * |
| 657 | * The resulting PK object will be a transparent type: |
| 658 | * - #MBEDTLS_PK_RSA for RSA keys or |
| 659 | * - #MBEDTLS_PK_ECKEY for EC keys. |
| 660 | * |
| 661 | * Once this functions returns the PK object will be completely |
| 662 | * independent from the original PSA key that it was generated |
| 663 | * from. |
| 664 | * Calling mbedtls_pk_sign(), mbedtls_pk_verify(), |
| 665 | * mbedtls_pk_encrypt(), mbedtls_pk_decrypt() on the resulting |
| 666 | * PK context will perform the corresponding algorithm for that |
| 667 | * PK context type. |
| 668 | * * For ECDSA, the choice of deterministic vs randomized will |
| 669 | * be based on the compile-time setting #MBEDTLS_ECDSA_DETERMINISTIC. |
| 670 | * * For an RSA key, the output PK context will allow both |
| 671 | * encrypt/decrypt and sign/verify regardless of the original |
| 672 | * key's policy. |
| 673 | * The original key's policy determines the output key's padding |
| 674 | * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS, |
| 675 | * otherwise PKCS1 v1.5 is set. |
| 676 | * |
| 677 | * \param key_id The key identifier of the key stored in PSA. |
| 678 | * \param pk The PK context that will be filled. It must be initialized, |
| 679 | * but not set up. |
| 680 | * |
| 681 | * \return 0 on success. |
| 682 | * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input |
| 683 | * parameters are not correct. |
| 684 | */ |
| 685 | int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk); |
| 686 | |
| 687 | /** |
| 688 | * \brief Create a PK context for the public key of a PSA key. |
| 689 | * |
| 690 | * The key must be an RSA or ECC key. It can be either a |
| 691 | * public key or a key pair, and only the public key is copied. |
| 692 | * The resulting PK object will be a transparent type: |
| 693 | * - #MBEDTLS_PK_RSA for RSA keys or |
| 694 | * - #MBEDTLS_PK_ECKEY for EC keys. |
| 695 | * |
| 696 | * Once this functions returns the PK object will be completely |
| 697 | * independent from the original PSA key that it was generated |
| 698 | * from. |
| 699 | * Calling mbedtls_pk_verify() or |
| 700 | * mbedtls_pk_encrypt() on the resulting |
| 701 | * PK context will perform the corresponding algorithm for that |
| 702 | * PK context type. |
| 703 | * |
| 704 | * For an RSA key, the output PK context will allow both |
| 705 | * encrypt and verify regardless of the original key's policy. |
| 706 | * The original key's policy determines the output key's padding |
| 707 | * mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS, |
| 708 | * otherwise PKCS1 v1.5 is set. |
| 709 | * |
| 710 | * \param key_id The key identifier of the key stored in PSA. |
| 711 | * \param pk The PK context that will be filled. It must be initialized, |
| 712 | * but not set up. |
| 713 | * |
| 714 | * \return 0 on success. |
| 715 | * \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input |
| 716 | * parameters are not correct. |
| 717 | */ |
| 718 | int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk); |
| 719 | #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */ |
Gilles Peskine | 0b17255 | 2024-01-18 14:11:26 +0100 | [diff] [blame] | 720 | |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 721 | /** |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 722 | * \brief Verify signature (including padding if relevant). |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 723 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 724 | * \param ctx The PK context to use. It must have been set up. |
Gilles Peskine | 9dbbc29 | 2021-06-22 18:28:13 +0200 | [diff] [blame] | 725 | * \param md_alg Hash algorithm used. |
| 726 | * This can be #MBEDTLS_MD_NONE if the signature algorithm |
| 727 | * does not rely on a hash algorithm (non-deterministic |
| 728 | * ECDSA, RSA PKCS#1 v1.5). |
| 729 | * For PKCS#1 v1.5, if \p md_alg is #MBEDTLS_MD_NONE, then |
| 730 | * \p hash is the DigestInfo structure used by RFC 8017 |
| 731 | * §9.2 steps 3–6. If \p md_alg is a valid hash |
| 732 | * algorithm then \p hash is the digest itself, and this |
| 733 | * function calculates the DigestInfo encoding internally. |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 734 | * \param hash Hash of the message to sign |
Gilles Peskine | 9dbbc29 | 2021-06-22 18:28:13 +0200 | [diff] [blame] | 735 | * \param hash_len Hash length |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 736 | * \param sig Signature to verify |
| 737 | * \param sig_len Signature length |
| 738 | * |
Valerio Setti | 85e568c | 2024-02-19 15:45:00 +0100 | [diff] [blame] | 739 | * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is |
| 740 | * either PKCS#1 v1.5 or PSS (accepting any salt length), |
| 741 | * depending on the padding mode in the underlying RSA context. |
| 742 | * For a pk object constructed by parsing, this is PKCS#1 v1.5 |
| 743 | * by default. Use mbedtls_pk_verify_ext() to explicitly select |
| 744 | * a different algorithm. |
| 745 | * |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 746 | * \return 0 on success (signature is valid), |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 747 | * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid |
Andrzej Kurek | 3bedb5b | 2022-02-17 14:39:00 -0500 | [diff] [blame] | 748 | * signature in \p sig but its length is less than \p sig_len, |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 749 | * or a specific error code. |
| 750 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 752 | const unsigned char *hash, size_t hash_len, |
| 753 | const unsigned char *sig, size_t sig_len); |
Manuel Pégourié-Gonnard | b3d9187 | 2013-08-14 15:56:19 +0200 | [diff] [blame] | 754 | |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 755 | /** |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 756 | * \brief Restartable version of \c mbedtls_pk_verify() |
| 757 | * |
| 758 | * \note Performs the same job as \c mbedtls_pk_verify(), but can |
| 759 | * return early and restart according to the limit set with |
| 760 | * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC |
| 761 | * operations. For RSA, same as \c mbedtls_pk_verify(). |
| 762 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 763 | * \param ctx The PK context to use. It must have been set up. |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 764 | * \param md_alg Hash algorithm used (see notes) |
| 765 | * \param hash Hash of the message to sign |
| 766 | * \param hash_len Hash length or 0 (see notes) |
| 767 | * \param sig Signature to verify |
| 768 | * \param sig_len Signature length |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 769 | * \param rs_ctx Restart context (NULL to disable restart) |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 770 | * |
| 771 | * \return See \c mbedtls_pk_verify(), or |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 772 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 773 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 774 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx, |
| 776 | mbedtls_md_type_t md_alg, |
| 777 | const unsigned char *hash, size_t hash_len, |
| 778 | const unsigned char *sig, size_t sig_len, |
| 779 | mbedtls_pk_restart_ctx *rs_ctx); |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 780 | |
| 781 | /** |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 782 | * \brief Verify signature, with options. |
| 783 | * (Includes verification of the padding depending on type.) |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 784 | * |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 785 | * \param type Signature type (inc. possible padding type) to verify |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 786 | * \param options Pointer to type-specific options, or NULL |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 787 | * \param ctx The PK context to use. It must have been set up. |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 788 | * \param md_alg Hash algorithm used (see notes) |
| 789 | * \param hash Hash of the message to sign |
| 790 | * \param hash_len Hash length or 0 (see notes) |
| 791 | * \param sig Signature to verify |
| 792 | * \param sig_len Signature length |
| 793 | * |
| 794 | * \return 0 on success (signature is valid), |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 795 | * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 796 | * used for this type of signatures, |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 797 | * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid |
Andrzej Kurek | 3bedb5b | 2022-02-17 14:39:00 -0500 | [diff] [blame] | 798 | * signature in \p sig but its length is less than \p sig_len, |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 799 | * or a specific error code. |
| 800 | * |
| 801 | * \note If hash_len is 0, then the length associated with md_alg |
| 802 | * is used instead, or an error returned if it is invalid. |
| 803 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 804 | * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0 |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 805 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point |
| 807 | * to a mbedtls_pk_rsassa_pss_options structure, |
Manuel Pégourié-Gonnard | a6e0291 | 2022-12-21 09:59:33 +0100 | [diff] [blame] | 808 | * otherwise it must be NULL. Note that if |
| 809 | * #MBEDTLS_USE_PSA_CRYPTO is defined, the salt length is not |
| 810 | * verified as PSA_ALG_RSA_PSS_ANY_SALT is used. |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 811 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options, |
| 813 | mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 814 | const unsigned char *hash, size_t hash_len, |
| 815 | const unsigned char *sig, size_t sig_len); |
Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 816 | |
| 817 | /** |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 818 | * \brief Make signature, including padding if relevant. |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 819 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 820 | * \param ctx The PK context to use. It must have been set up |
| 821 | * with a private key. |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 822 | * \param md_alg Hash algorithm used (see notes) |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 823 | * \param hash Hash of the message to sign |
Gilles Peskine | 9dbbc29 | 2021-06-22 18:28:13 +0200 | [diff] [blame] | 824 | * \param hash_len Hash length |
Gilles Peskine | da252be | 2019-11-05 16:23:49 +0100 | [diff] [blame] | 825 | * \param sig Place to write the signature. |
| 826 | * It must have enough room for the signature. |
| 827 | * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. |
| 828 | * You may use a smaller buffer if it is large enough |
| 829 | * given the key type. |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 830 | * \param sig_size The size of the \p sig buffer in bytes. |
Gilles Peskine | da252be | 2019-11-05 16:23:49 +0100 | [diff] [blame] | 831 | * \param sig_len On successful return, |
| 832 | * the number of bytes written to \p sig. |
Manuel Pégourié-Gonnard | 34d3756 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 833 | * \param f_rng RNG function, must not be \c NULL. |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 834 | * \param p_rng RNG parameter |
| 835 | * |
Valerio Setti | 85e568c | 2024-02-19 15:45:00 +0100 | [diff] [blame] | 836 | * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is |
| 837 | * either PKCS#1 v1.5 or PSS (using the largest possible salt |
| 838 | * length up to the hash length), depending on the padding mode |
| 839 | * in the underlying RSA context. For a pk object constructed |
| 840 | * by parsing, this is PKCS#1 v1.5 by default. Use |
| 841 | * mbedtls_pk_verify_ext() to explicitly select a different |
| 842 | * algorithm. |
Manuel Pégourié-Gonnard | bfe32ef | 2013-08-22 14:55:30 +0200 | [diff] [blame] | 843 | * |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 844 | * \return 0 on success, or a specific error code. |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 845 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 846 | * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0. |
| 847 | * For ECDSA, md_alg may never be MBEDTLS_MD_NONE. |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 848 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
| 850 | const unsigned char *hash, size_t hash_len, |
| 851 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 852 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Manuel Pégourié-Gonnard | 8df2769 | 2013-08-21 10:34:38 +0200 | [diff] [blame] | 853 | |
| 854 | /** |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 855 | * \brief Make signature given a signature type. |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 856 | * |
Jerry Yu | 8beb9e1 | 2022-03-12 16:23:53 +0800 | [diff] [blame] | 857 | * \param pk_type Signature type. |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 858 | * \param ctx The PK context to use. It must have been set up |
| 859 | * with a private key. |
| 860 | * \param md_alg Hash algorithm used (see notes) |
| 861 | * \param hash Hash of the message to sign |
| 862 | * \param hash_len Hash length |
| 863 | * \param sig Place to write the signature. |
| 864 | * It must have enough room for the signature. |
| 865 | * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. |
| 866 | * You may use a smaller buffer if it is large enough |
| 867 | * given the key type. |
| 868 | * \param sig_size The size of the \p sig buffer in bytes. |
| 869 | * \param sig_len On successful return, |
| 870 | * the number of bytes written to \p sig. |
| 871 | * \param f_rng RNG function, must not be \c NULL. |
| 872 | * \param p_rng RNG parameter |
| 873 | * |
| 874 | * \return 0 on success, or a specific error code. |
| 875 | * |
Jerry Yu | e6e73d6 | 2022-03-24 13:05:08 +0800 | [diff] [blame] | 876 | * \note When \p pk_type is #MBEDTLS_PK_RSASSA_PSS, |
| 877 | * see #PSA_ALG_RSA_PSS for a description of PSS options used. |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 878 | * |
| 879 | * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0. |
| 880 | * For ECDSA, md_alg may never be MBEDTLS_MD_NONE. |
| 881 | * |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 882 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type, |
| 884 | mbedtls_pk_context *ctx, |
| 885 | mbedtls_md_type_t md_alg, |
| 886 | const unsigned char *hash, size_t hash_len, |
| 887 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 888 | int (*f_rng)(void *, unsigned char *, size_t), |
| 889 | void *p_rng); |
Jerry Yu | d69439a | 2022-02-24 15:52:15 +0800 | [diff] [blame] | 890 | |
| 891 | /** |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 892 | * \brief Restartable version of \c mbedtls_pk_sign() |
| 893 | * |
| 894 | * \note Performs the same job as \c mbedtls_pk_sign(), but can |
| 895 | * return early and restart according to the limit set with |
| 896 | * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC |
| 897 | * operations. For RSA, same as \c mbedtls_pk_sign(). |
| 898 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 899 | * \param ctx The PK context to use. It must have been set up |
| 900 | * with a private key. |
Gilles Peskine | 9db14fa | 2019-11-08 18:37:19 +0100 | [diff] [blame] | 901 | * \param md_alg Hash algorithm used (see notes for mbedtls_pk_sign()) |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 902 | * \param hash Hash of the message to sign |
Gilles Peskine | 9dbbc29 | 2021-06-22 18:28:13 +0200 | [diff] [blame] | 903 | * \param hash_len Hash length |
Gilles Peskine | 9db14fa | 2019-11-08 18:37:19 +0100 | [diff] [blame] | 904 | * \param sig Place to write the signature. |
| 905 | * It must have enough room for the signature. |
| 906 | * #MBEDTLS_PK_SIGNATURE_MAX_SIZE is always enough. |
| 907 | * You may use a smaller buffer if it is large enough |
| 908 | * given the key type. |
Gilles Peskine | f00f152 | 2021-06-22 00:09:00 +0200 | [diff] [blame] | 909 | * \param sig_size The size of the \p sig buffer in bytes. |
Gilles Peskine | 9db14fa | 2019-11-08 18:37:19 +0100 | [diff] [blame] | 910 | * \param sig_len On successful return, |
| 911 | * the number of bytes written to \p sig. |
Manuel Pégourié-Gonnard | 34d3756 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 912 | * \param f_rng RNG function, must not be \c NULL. |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 913 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 914 | * \param rs_ctx Restart context (NULL to disable restart) |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 915 | * |
Gilles Peskine | 9db14fa | 2019-11-08 18:37:19 +0100 | [diff] [blame] | 916 | * \return See \c mbedtls_pk_sign(). |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 917 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 918 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
| 919 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 920 | int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx, |
| 921 | mbedtls_md_type_t md_alg, |
| 922 | const unsigned char *hash, size_t hash_len, |
| 923 | unsigned char *sig, size_t sig_size, size_t *sig_len, |
| 924 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 925 | mbedtls_pk_restart_ctx *rs_ctx); |
Manuel Pégourié-Gonnard | 82cb27b | 2017-05-03 10:59:45 +0200 | [diff] [blame] | 926 | |
| 927 | /** |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 928 | * \brief Decrypt message (including padding if relevant). |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 929 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 930 | * \param ctx The PK context to use. It must have been set up |
| 931 | * with a private key. |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 932 | * \param input Input to decrypt |
| 933 | * \param ilen Input size |
| 934 | * \param output Decrypted output |
Paul Bakker | 4fc090a | 2013-09-18 15:43:25 +0200 | [diff] [blame] | 935 | * \param olen Decrypted message length |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 936 | * \param osize Size of the output buffer |
Manuel Pégourié-Gonnard | 34d3756 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 937 | * \param f_rng RNG function, must not be \c NULL. |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 938 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 939 | * |
Valerio Setti | 85e568c | 2024-02-19 15:45:00 +0100 | [diff] [blame] | 940 | * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is |
| 941 | * either PKCS#1 v1.5 or OAEP, depending on the padding mode in |
| 942 | * the underlying RSA context. For a pk object constructed by |
| 943 | * parsing, this is PKCS#1 v1.5 by default. |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 944 | * |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 945 | * \return 0 on success, or a specific error code. |
| 946 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | int mbedtls_pk_decrypt(mbedtls_pk_context *ctx, |
| 948 | const unsigned char *input, size_t ilen, |
| 949 | unsigned char *output, size_t *olen, size_t osize, |
| 950 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 951 | |
| 952 | /** |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 953 | * \brief Encrypt message (including padding if relevant). |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 954 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 955 | * \param ctx The PK context to use. It must have been set up. |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 956 | * \param input Message to encrypt |
| 957 | * \param ilen Message size |
| 958 | * \param output Encrypted output |
| 959 | * \param olen Encrypted output length |
| 960 | * \param osize Size of the output buffer |
Manuel Pégourié-Gonnard | 34d3756 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 961 | * \param f_rng RNG function, must not be \c NULL. |
Paul Bakker | dcbfdcc | 2013-09-10 16:16:50 +0200 | [diff] [blame] | 962 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 963 | * |
Valerio Setti | 85e568c | 2024-02-19 15:45:00 +0100 | [diff] [blame] | 964 | * \note For keys of type #MBEDTLS_PK_RSA, the signature algorithm is |
| 965 | * either PKCS#1 v1.5 or OAEP, depending on the padding mode in |
| 966 | * the underlying RSA context. For a pk object constructed by |
| 967 | * parsing, this is PKCS#1 v1.5 by default. |
Manuel Pégourié-Gonnard | 34d3756 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 968 | * |
Manuel Pégourié-Gonnard | d543a58 | 2014-06-25 14:04:36 +0200 | [diff] [blame] | 969 | * \note \p f_rng is used for padding generation. |
| 970 | * |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 971 | * \return 0 on success, or a specific error code. |
| 972 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | int mbedtls_pk_encrypt(mbedtls_pk_context *ctx, |
| 974 | const unsigned char *input, size_t ilen, |
| 975 | unsigned char *output, size_t *olen, size_t osize, |
| 976 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
Manuel Pégourié-Gonnard | a2d3f22 | 2013-08-21 11:51:08 +0200 | [diff] [blame] | 977 | |
| 978 | /** |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 979 | * \brief Check if a public-private pair of keys matches. |
| 980 | * |
| 981 | * \param pub Context holding a public key. |
| 982 | * \param prv Context holding a private (and public) key. |
Manuel Pégourié-Gonnard | 39be141 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 983 | * \param f_rng RNG function, must not be \c NULL. |
| 984 | * \param p_rng RNG parameter |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 985 | * |
Manuel Pégourié-Gonnard | eaeb7b2 | 2018-10-24 12:37:44 +0200 | [diff] [blame] | 986 | * \return \c 0 on success (keys were checked and match each other). |
| 987 | * \return #MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE if the keys could not |
| 988 | * be checked - in that case they may or may not match. |
| 989 | * \return #MBEDTLS_ERR_PK_BAD_INPUT_DATA if a context is invalid. |
| 990 | * \return Another non-zero value if the keys do not match. |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 991 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 992 | int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, |
| 993 | const mbedtls_pk_context *prv, |
| 994 | int (*f_rng)(void *, unsigned char *, size_t), |
| 995 | void *p_rng); |
Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 996 | |
| 997 | /** |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 998 | * \brief Export debug information |
| 999 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1000 | * \param ctx The PK context to use. It must have been initialized. |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1001 | * \param items Place to write debug items |
| 1002 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1003 | * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1004 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 1006 | |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1007 | /** |
| 1008 | * \brief Access the type name |
| 1009 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1010 | * \param ctx The PK context to use. It must have been initialized. |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1011 | * |
| 1012 | * \return Type name on success, or "invalid PK" |
| 1013 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx); |
Manuel Pégourié-Gonnard | 3fb5c5e | 2013-08-14 18:26:41 +0200 | [diff] [blame] | 1015 | |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1016 | /** |
Paul Bakker | 4fc090a | 2013-09-18 15:43:25 +0200 | [diff] [blame] | 1017 | * \brief Get the key type |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1018 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1019 | * \param ctx The PK context to use. It must have been initialized. |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1020 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1021 | * \return Type on success. |
| 1022 | * \return #MBEDTLS_PK_NONE for a context that has not been set up. |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1023 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx); |
Manuel Pégourié-Gonnard | 8053da4 | 2013-09-11 22:28:30 +0200 | [diff] [blame] | 1025 | |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1037 | static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk) |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 1038 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1039 | switch (mbedtls_pk_get_type(&pk)) { |
Manuel Pégourié-Gonnard | 4cfaae5 | 2022-06-23 09:43:39 +0200 | [diff] [blame] | 1040 | case MBEDTLS_PK_RSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1041 | return (mbedtls_rsa_context *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
Manuel Pégourié-Gonnard | 4cfaae5 | 2022-06-23 09:43:39 +0200 | [diff] [blame] | 1042 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | return NULL; |
Manuel Pégourié-Gonnard | 4cfaae5 | 2022-06-23 09:43:39 +0200 | [diff] [blame] | 1044 | } |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 1045 | } |
| 1046 | #endif /* MBEDTLS_RSA_C */ |
| 1047 | |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 1048 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk) |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 1061 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1062 | switch (mbedtls_pk_get_type(&pk)) { |
Manuel Pégourié-Gonnard | 4cfaae5 | 2022-06-23 09:43:39 +0200 | [diff] [blame] | 1063 | case MBEDTLS_PK_ECKEY: |
| 1064 | case MBEDTLS_PK_ECKEY_DH: |
| 1065 | case MBEDTLS_PK_ECDSA: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | return (mbedtls_ecp_keypair *) (pk).MBEDTLS_PRIVATE(pk_ctx); |
Manuel Pégourié-Gonnard | 4cfaae5 | 2022-06-23 09:43:39 +0200 | [diff] [blame] | 1067 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | return NULL; |
Manuel Pégourié-Gonnard | 4cfaae5 | 2022-06-23 09:43:39 +0200 | [diff] [blame] | 1069 | } |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 1070 | } |
Valerio Setti | 229bf10 | 2023-05-15 11:13:55 +0200 | [diff] [blame] | 1071 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 22e84de | 2022-06-10 09:48:38 +0200 | [diff] [blame] | 1072 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1073 | #if defined(MBEDTLS_PK_PARSE_C) |
Paul Bakker | ff3a518 | 2013-09-16 22:42:19 +0200 | [diff] [blame] | 1074 | /** \ingroup pk_module */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1075 | /** |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1076 | * \brief Parse a private key in PEM or DER format |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1077 | * |
Gilles Peskine | 5b7e164 | 2022-08-04 23:44:59 +0200 | [diff] [blame] | 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 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1098 | * \param f_rng RNG function, must not be \c NULL. Used for blinding. |
| 1099 | * \param p_rng RNG parameter |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1100 | * |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1101 | * \note On entry, ctx must be empty, either freshly initialised |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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(). |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1104 | * |
| 1105 | * \note The key is also checked for correctness. |
| 1106 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1107 | * \return 0 if successful, or a specific PK or PEM error code |
| 1108 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1113 | |
Paul Bakker | ff3a518 | 2013-09-16 22:42:19 +0200 | [diff] [blame] | 1114 | /** \ingroup pk_module */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1115 | /** |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1116 | * \brief Parse a public key in PEM or DER format |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1117 | * |
Gilles Peskine | 5b7e164 | 2022-08-04 23:44:59 +0200 | [diff] [blame] | 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 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 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`. |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1131 | * |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1132 | * \note On entry, ctx must be empty, either freshly initialised |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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(). |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1135 | * |
Valerio Setti | 78f79d3 | 2023-02-07 08:33:26 +0100 | [diff] [blame] | 1136 | * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for |
| 1137 | * limitations. |
| 1138 | * |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1139 | * \note The key is also checked for correctness. |
| 1140 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1141 | * \return 0 if successful, or a specific PK or PEM error code |
| 1142 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx, |
| 1144 | const unsigned char *key, size_t keylen); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1145 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | ff3a518 | 2013-09-16 22:42:19 +0200 | [diff] [blame] | 1147 | /** \ingroup pk_module */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1148 | /** |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1149 | * \brief Load and parse a private key |
| 1150 | * |
Gilles Peskine | 5b7e164 | 2022-08-04 23:44:59 +0200 | [diff] [blame] | 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 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1155 | * \param ctx The PK context to fill. It must have been initialized |
| 1156 | * but not set up. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1157 | * \param path filename to read the private key from |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 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. |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1163 | * \param f_rng RNG function, must not be \c NULL. Used for blinding. |
| 1164 | * \param p_rng RNG parameter |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1165 | * |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1166 | * \note On entry, ctx must be empty, either freshly initialised |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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(). |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1169 | * |
| 1170 | * \note The key is also checked for correctness. |
| 1171 | * |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1172 | * \return 0 if successful, or a specific PK or PEM error code |
| 1173 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1177 | |
Paul Bakker | ff3a518 | 2013-09-16 22:42:19 +0200 | [diff] [blame] | 1178 | /** \ingroup pk_module */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1179 | /** |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1180 | * \brief Load and parse a public key |
| 1181 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1182 | * \param ctx The PK context to fill. It must have been initialized |
| 1183 | * but not set up. |
Simon Butcher | 295639b | 2016-05-10 19:39:36 +0100 | [diff] [blame] | 1184 | * \param path filename to read the public key from |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1185 | * |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1186 | * \note On entry, ctx must be empty, either freshly initialised |
Simon Butcher | 295639b | 2016-05-10 19:39:36 +0100 | [diff] [blame] | 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(). |
Manuel Pégourié-Gonnard | e3b3d19 | 2014-03-13 19:21:49 +0100 | [diff] [blame] | 1190 | * |
| 1191 | * \note The key is also checked for correctness. |
| 1192 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1193 | * \return 0 if successful, or a specific PK or PEM error code |
| 1194 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1195 | int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | #endif /* MBEDTLS_FS_IO */ |
| 1197 | #endif /* MBEDTLS_PK_PARSE_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1198 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | #if defined(MBEDTLS_PK_WRITE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1200 | /** |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 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 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1206 | * \param ctx PK context which must contain a valid private key. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1213 | int mbedtls_pk_write_key_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 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 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1221 | * \param ctx PK context which must contain a valid public or private key. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 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 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1228 | int mbedtls_pk_write_pubkey_der(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1231 | /** |
| 1232 | * \brief Write a public key to a PEM string |
| 1233 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 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. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1238 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 1239 | * \return 0 if successful, or a specific error code |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1240 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1241 | int mbedtls_pk_write_pubkey_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1242 | |
| 1243 | /** |
| 1244 | * \brief Write a private key to a PKCS#1 or SEC1 PEM string |
| 1245 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 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. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1250 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 1251 | * \return 0 if successful, or a specific error code |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1252 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1253 | int mbedtls_pk_write_key_pem(const mbedtls_pk_context *ctx, unsigned char *buf, size_t size); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | #endif /* MBEDTLS_PEM_WRITE_C */ |
| 1255 | #endif /* MBEDTLS_PK_WRITE_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1256 | |
| 1257 | /* |
| 1258 | * WARNING: Low-level functions. You probably do not want to use these unless |
| 1259 | * you are certain you do ;) |
| 1260 | */ |
| 1261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | #if defined(MBEDTLS_PK_PARSE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1263 | /** |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 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 |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1268 | * \param pk The PK context to fill. It must have been initialized |
| 1269 | * but not set up. |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1270 | * |
| 1271 | * \return 0 if successful, or a specific PK error code |
| 1272 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1273 | int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end, |
| 1274 | mbedtls_pk_context *pk); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1275 | #endif /* MBEDTLS_PK_PARSE_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | #if defined(MBEDTLS_PK_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 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) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1284 | * \param key PK context which must contain a valid public or private key. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1285 | * |
| 1286 | * \return the length written or a negative error code |
| 1287 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start, |
| 1289 | const mbedtls_pk_context *key); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1290 | #endif /* MBEDTLS_PK_WRITE_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1291 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 1292 | #ifdef __cplusplus |
| 1293 | } |
| 1294 | #endif |
| 1295 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | #endif /* MBEDTLS_PK_H */ |