Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 1 | /** |
Hanno Becker | afebf5a | 2018-11-13 21:01:41 +0000 | [diff] [blame] | 2 | * \file psa_util.h |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 3 | * |
| 4 | * \brief Utility functions for the use of the PSA Crypto library. |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +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 |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Hanno Becker | 186b65a | 2018-11-19 15:14:21 +0000 | [diff] [blame] | 11 | #ifndef MBEDTLS_PSA_UTIL_H |
| 12 | #define MBEDTLS_PSA_UTIL_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 13 | #include "mbedtls/private_access.h" |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 14 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 15 | #include "mbedtls/build_info.h" |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 16 | |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 17 | #include "psa/crypto.h" |
| 18 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 19 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 20 | |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 21 | /* Expose whatever RNG the PSA subsystem uses to applications using the |
Gilles Peskine | 996f216 | 2021-02-16 16:50:00 +0100 | [diff] [blame] | 22 | * mbedtls_xxx API. The declarations and definitions here need to be |
| 23 | * consistent with the implementation in library/psa_crypto_random_impl.h. |
| 24 | * See that file for implementation documentation. */ |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 25 | |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 26 | |
| 27 | /* The type of a `f_rng` random generator function that many library functions |
| 28 | * take. |
| 29 | * |
| 30 | * This type name is not part of the Mbed TLS stable API. It may be renamed |
| 31 | * or moved without warning. |
| 32 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size); |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 34 | |
| 35 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 36 | |
| 37 | /** The random generator function for the PSA subsystem. |
| 38 | * |
| 39 | * This function is suitable as the `f_rng` random generator function |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 40 | * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE |
| 41 | * to obtain the \p p_rng parameter. |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 42 | * |
| 43 | * The implementation of this function depends on the configuration of the |
| 44 | * library. |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 45 | * |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 46 | * \note Depending on the configuration, this may be a function or |
| 47 | * a pointer to a function. |
| 48 | * |
| 49 | * \note This function may only be used if the PSA crypto subsystem is active. |
| 50 | * This means that you must call psa_crypto_init() before any call to |
| 51 | * this function, and you must not call this function after calling |
| 52 | * mbedtls_psa_crypto_free(). |
| 53 | * |
| 54 | * \param p_rng The random generator context. This must be |
| 55 | * #MBEDTLS_PSA_RANDOM_STATE. No other state is |
| 56 | * supported. |
| 57 | * \param output The buffer to fill. It must have room for |
| 58 | * \c output_size bytes. |
| 59 | * \param output_size The number of bytes to write to \p output. |
| 60 | * This function may fail if \p output_size is too |
| 61 | * large. It is guaranteed to accept any output size |
| 62 | * requested by Mbed TLS library functions. The |
| 63 | * maximum request size depends on the library |
| 64 | * configuration. |
| 65 | * |
| 66 | * \return \c 0 on success. |
| 67 | * \return An `MBEDTLS_ERR_ENTROPY_xxx`, |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 68 | * `MBEDTLS_ERR_PLATFORM_xxx, |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 69 | * `MBEDTLS_ERR_CTR_DRBG_xxx` or |
| 70 | * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error. |
| 71 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | int mbedtls_psa_get_random(void *p_rng, |
| 73 | unsigned char *output, |
| 74 | size_t output_size); |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 75 | |
| 76 | /** The random generator state for the PSA subsystem. |
| 77 | * |
| 78 | * This macro expands to an expression which is suitable as the `p_rng` |
| 79 | * random generator state parameter of many `mbedtls_xxx` functions. |
| 80 | * It must be used in combination with the random generator function |
| 81 | * mbedtls_psa_get_random(). |
| 82 | * |
| 83 | * The implementation of this macro depends on the configuration of the |
| 84 | * library. Do not make any assumption on its nature. |
| 85 | */ |
| 86 | #define MBEDTLS_PSA_RANDOM_STATE NULL |
| 87 | |
| 88 | #else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
| 89 | |
| 90 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 91 | #include "mbedtls/ctr_drbg.h" |
| 92 | typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t; |
| 93 | static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random; |
| 94 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 95 | #include "mbedtls/hmac_drbg.h" |
| 96 | typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t; |
| 97 | static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random; |
| 98 | #endif |
| 99 | extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state; |
| 100 | |
| 101 | #define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state |
| 102 | |
| 103 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
| 104 | |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 105 | /** \defgroup psa_tls_helpers TLS helper functions |
| 106 | * @{ |
| 107 | */ |
| 108 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
| 109 | #include <mbedtls/ecp.h> |
| 110 | |
| 111 | /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA. |
| 112 | * |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 113 | * \param grpid An Mbed TLS elliptic curve identifier |
| 114 | * (`MBEDTLS_ECP_DP_xxx`). |
Valerio Setti | eca0714 | 2024-01-04 13:17:31 +0100 | [diff] [blame] | 115 | * \param[out] bits On success the bit size of the curve; 0 on failure. |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 116 | * |
Valerio Setti | d0aa9c1 | 2024-01-09 09:10:44 +0100 | [diff] [blame] | 117 | * \return If the curve is supported in the PSA API, this function |
| 118 | * returns the proper PSA curve identifier |
| 119 | * (`PSA_ECC_FAMILY_xxx`). This holds even if the curve is |
| 120 | * not supported by the ECP module. |
| 121 | * \return \c 0 if the curve is not supported in the PSA API. |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 122 | */ |
| 123 | psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid, |
| 124 | size_t *bits); |
| 125 | |
| 126 | /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS. |
| 127 | * |
Valerio Setti | 39faa9c | 2024-01-09 09:11:22 +0100 | [diff] [blame] | 128 | * \param family A PSA elliptic curve family identifier |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 129 | * (`PSA_ECC_FAMILY_xxx`). |
| 130 | * \param bits The bit-length of a private key on \p curve. |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 131 | * |
Valerio Setti | d0aa9c1 | 2024-01-09 09:10:44 +0100 | [diff] [blame] | 132 | * \return If the curve is supported in the PSA API, this function |
| 133 | * returns the corresponding Mbed TLS elliptic curve |
Valerio Setti | eca0714 | 2024-01-04 13:17:31 +0100 | [diff] [blame] | 134 | * identifier (`MBEDTLS_ECP_DP_xxx`). |
Valerio Setti | 0e60880 | 2023-12-29 11:46:44 +0100 | [diff] [blame] | 135 | * \return #MBEDTLS_ECP_DP_NONE if the combination of \c curve |
Valerio Setti | d0aa9c1 | 2024-01-09 09:10:44 +0100 | [diff] [blame] | 136 | * and \p bits is not supported. |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 137 | */ |
Valerio Setti | 39faa9c | 2024-01-09 09:11:22 +0100 | [diff] [blame] | 138 | mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family, |
Valerio Setti | d36c313 | 2023-12-21 14:03:51 +0100 | [diff] [blame] | 139 | size_t bits); |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 140 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
| 141 | |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 142 | /** |
| 143 | * \brief This function returns the PSA algorithm identifier |
| 144 | * associated with the given digest type. |
| 145 | * |
Valerio Setti | dd2afcd | 2024-01-09 08:41:29 +0100 | [diff] [blame] | 146 | * \param md_type The type of digest to search for. Must not be NONE. |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 147 | * |
Valerio Setti | dd2afcd | 2024-01-09 08:41:29 +0100 | [diff] [blame] | 148 | * \warning If \p md_type is \c MBEDTLS_MD_NONE, this function will |
| 149 | * not return \c PSA_ALG_NONE, but an invalid algorithm. |
| 150 | * |
| 151 | * \warning This function does not check if the algorithm is |
| 152 | * supported, it always returns the corresponding identifier. |
| 153 | * |
| 154 | * \return The PSA algorithm identifier associated with \p md_type, |
| 155 | * regardless of whether it is supported or not. |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 156 | */ |
Valerio Setti | dd2afcd | 2024-01-09 08:41:29 +0100 | [diff] [blame] | 157 | static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type) |
| 158 | { |
| 159 | return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type; |
| 160 | } |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * \brief This function returns the given digest type |
| 164 | * associated with the PSA algorithm identifier. |
| 165 | * |
| 166 | * \param psa_alg The PSA algorithm identifier to search for. |
| 167 | * |
Valerio Setti | dd2afcd | 2024-01-09 08:41:29 +0100 | [diff] [blame] | 168 | * \warning This function does not check if the algorithm is |
| 169 | * supported, it always returns the corresponding identifier. |
| 170 | * |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 171 | * \return The MD type associated with \p psa_alg, |
Valerio Setti | dd2afcd | 2024-01-09 08:41:29 +0100 | [diff] [blame] | 172 | * regardless of whether it is supported or not. |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 173 | */ |
Valerio Setti | dd2afcd | 2024-01-09 08:41:29 +0100 | [diff] [blame] | 174 | static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg) |
| 175 | { |
| 176 | return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK); |
| 177 | } |
Valerio Setti | 45c3cae | 2024-01-02 13:26:04 +0100 | [diff] [blame] | 178 | |
Valerio Setti | 75501f5 | 2024-01-08 16:49:17 +0100 | [diff] [blame] | 179 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Valerio Setti | 84890c9 | 2024-01-09 14:20:23 +0100 | [diff] [blame] | 180 | /** Convert an ECDSA signature from raw format (used by PSA APIs) to DER ASN.1 |
| 181 | * format (used by legacy crypto APIs). |
Valerio Setti | 75501f5 | 2024-01-08 16:49:17 +0100 | [diff] [blame] | 182 | * |
| 183 | * \param raw Buffer that contains the signature in raw format. |
| 184 | * \param raw_len Length of raw buffer in bytes |
| 185 | * \param[out] der Buffer that will be filled with the converted DER |
| 186 | * output. It can overlap with raw buffer. |
| 187 | * \param der_size Size of the output der buffer in bytes. |
| 188 | * \param[out] der_len On success it contains the amount of valid data |
| 189 | * (in bytes) written to der buffer. It's undefined |
| 190 | * in case of failure. |
| 191 | * \param bits Size of each raw coordinate in bits. |
| 192 | */ |
| 193 | int mbedtls_ecdsa_raw_to_der(const unsigned char *raw, size_t raw_len, |
| 194 | unsigned char *der, size_t der_size, size_t *der_len, |
| 195 | size_t bits); |
| 196 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
| 197 | |
| 198 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Valerio Setti | 84890c9 | 2024-01-09 14:20:23 +0100 | [diff] [blame] | 199 | /** Convert an ECDSA signature from DER ASN.1 format (used by legacy crypto |
| 200 | * APIs) to raw format (used by PSA APIs). |
Valerio Setti | 75501f5 | 2024-01-08 16:49:17 +0100 | [diff] [blame] | 201 | * |
| 202 | * \param der Buffer that contains the signature in DER format. |
| 203 | * \param der_len Size of the der buffer in bytes. |
| 204 | * \param[out] raw Buffer that will be filled with the converted raw |
| 205 | * signature. It can overlap with der buffer. |
| 206 | * \param raw_size Size of the raw buffer in bytes. |
| 207 | * \param[out] raw_len On success it is updated with the amount of valid |
| 208 | * data (in bytes) written to raw buffer. It's undefined |
| 209 | * in case of failure. |
| 210 | * \param bits Size of each raw coordinate in bits. |
| 211 | */ |
| 212 | int mbedtls_ecdsa_der_to_raw(const unsigned char *der, size_t der_len, |
| 213 | unsigned char *raw, size_t raw_size, size_t *raw_len, |
| 214 | size_t bits); |
| 215 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
| 216 | |
Joakim Andersson | b349108 | 2023-12-11 21:29:19 +0100 | [diff] [blame] | 217 | /**@}*/ |
| 218 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 219 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Hanno Becker | 186b65a | 2018-11-19 15:14:21 +0000 | [diff] [blame] | 220 | #endif /* MBEDTLS_PSA_UTIL_H */ |