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