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. |
| 5 | * |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 6 | * \warning These functions are not part of the public API and may |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 7 | * change at any time. |
| 8 | */ |
| 9 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 10 | * Copyright The Mbed TLS Contributors |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 11 | * SPDX-License-Identifier: Apache-2.0 |
| 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Hanno Becker | 186b65a | 2018-11-19 15:14:21 +0000 | [diff] [blame] | 26 | #ifndef MBEDTLS_PSA_UTIL_H |
| 27 | #define MBEDTLS_PSA_UTIL_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 28 | #include "mbedtls/private_access.h" |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 29 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 30 | #include "mbedtls/build_info.h" |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 1c32e37 | 2023-03-28 11:57:09 +0200 | [diff] [blame] | 32 | #include "mbedtls/platform_util.h" |
| 33 | |
Jerry Yu | b02ee18 | 2022-03-16 10:30:41 +0800 | [diff] [blame] | 34 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
Hanno Becker | 87837b2 | 2018-11-08 13:32:02 +0000 | [diff] [blame] | 35 | |
| 36 | #include "psa/crypto.h" |
| 37 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 38 | #include "mbedtls/ecp.h" |
| 39 | #include "mbedtls/md.h" |
| 40 | #include "mbedtls/pk.h" |
| 41 | #include "mbedtls/oid.h" |
TRodziewicz | 3408d60 | 2021-04-21 13:25:25 +0200 | [diff] [blame] | 42 | #include "mbedtls/error.h" |
Hanno Becker | f75f912 | 2019-01-07 15:36:51 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Hanno Becker | 5525126 | 2018-11-12 09:29:12 +0000 | [diff] [blame] | 45 | /* Translations for ECC. */ |
| 46 | |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 47 | static inline int mbedtls_psa_get_ecc_oid_from_id( |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 48 | psa_ecc_family_t curve, size_t bits, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | char const **oid, size_t *oid_len) |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 50 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | switch (curve) { |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 52 | case PSA_ECC_FAMILY_SECP_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | switch (bits) { |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 54 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 55 | case 192: |
| 56 | *oid = MBEDTLS_OID_EC_GRP_SECP192R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192R1); |
| 58 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 59 | #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ |
| 60 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 61 | case 224: |
| 62 | *oid = MBEDTLS_OID_EC_GRP_SECP224R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224R1); |
| 64 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 65 | #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ |
| 66 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 67 | case 256: |
| 68 | *oid = MBEDTLS_OID_EC_GRP_SECP256R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256R1); |
| 70 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 71 | #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 72 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 73 | case 384: |
| 74 | *oid = MBEDTLS_OID_EC_GRP_SECP384R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP384R1); |
| 76 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 77 | #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 78 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 79 | case 521: |
| 80 | *oid = MBEDTLS_OID_EC_GRP_SECP521R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP521R1); |
| 82 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 83 | #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 84 | } |
| 85 | break; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 86 | case PSA_ECC_FAMILY_SECP_K1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | switch (bits) { |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 88 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 89 | case 192: |
| 90 | *oid = MBEDTLS_OID_EC_GRP_SECP192K1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192K1); |
| 92 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 93 | #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ |
| 94 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 95 | case 224: |
| 96 | *oid = MBEDTLS_OID_EC_GRP_SECP224K1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224K1); |
| 98 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 99 | #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ |
| 100 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 101 | case 256: |
| 102 | *oid = MBEDTLS_OID_EC_GRP_SECP256K1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256K1); |
| 104 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 105 | #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 106 | } |
| 107 | break; |
Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 108 | case PSA_ECC_FAMILY_BRAINPOOL_P_R1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | switch (bits) { |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 110 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 111 | case 256: |
| 112 | *oid = MBEDTLS_OID_EC_GRP_BP256R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP256R1); |
| 114 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 115 | #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */ |
| 116 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 117 | case 384: |
| 118 | *oid = MBEDTLS_OID_EC_GRP_BP384R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP384R1); |
| 120 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 121 | #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */ |
| 122 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 123 | case 512: |
| 124 | *oid = MBEDTLS_OID_EC_GRP_BP512R1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP512R1); |
| 126 | return 0; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 127 | #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */ |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 128 | } |
| 129 | break; |
valerio | 64e0184 | 2023-05-31 12:02:43 +0200 | [diff] [blame] | 130 | case PSA_ECC_FAMILY_MONTGOMERY: |
| 131 | switch (bits) { |
| 132 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 133 | case 255: |
| 134 | *oid = MBEDTLS_OID_X25519; |
| 135 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_X25519); |
| 136 | return 0; |
| 137 | #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ |
| 138 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 139 | case 448: |
| 140 | *oid = MBEDTLS_OID_X448; |
| 141 | *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_X448); |
| 142 | return 0; |
| 143 | #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */ |
| 144 | } |
| 145 | break; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 146 | } |
Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 147 | (void) oid; |
| 148 | (void) oid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | return -1; |
Hanno Becker | 812e124 | 2019-02-01 10:06:51 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Manuel Pégourié-Gonnard | 5975376 | 2022-01-18 11:52:11 +0100 | [diff] [blame] | 152 | #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) |
Hanno Becker | df51dbe | 2019-02-18 16:41:55 +0000 | [diff] [blame] | 154 | |
Valerio Setti | 1337a4f | 2023-01-30 15:54:55 +0100 | [diff] [blame] | 155 | #define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \ |
| 156 | PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) |
| 157 | |
Przemek Stekiel | cceb933 | 2023-05-18 14:31:10 +0200 | [diff] [blame] | 158 | #define MBEDTLS_PSA_MAX_FFDH_PUBKEY_LENGTH \ |
| 159 | PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS) |
| 160 | |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 161 | /* Expose whatever RNG the PSA subsystem uses to applications using the |
Gilles Peskine | 996f216 | 2021-02-16 16:50:00 +0100 | [diff] [blame] | 162 | * mbedtls_xxx API. The declarations and definitions here need to be |
| 163 | * consistent with the implementation in library/psa_crypto_random_impl.h. |
| 164 | * See that file for implementation documentation. */ |
Jerry Yu | 406cf27 | 2022-03-22 11:33:42 +0800 | [diff] [blame] | 165 | |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 166 | |
| 167 | /* The type of a `f_rng` random generator function that many library functions |
| 168 | * take. |
| 169 | * |
| 170 | * This type name is not part of the Mbed TLS stable API. It may be renamed |
| 171 | * or moved without warning. |
| 172 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | 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] | 174 | |
| 175 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 176 | |
| 177 | /** The random generator function for the PSA subsystem. |
| 178 | * |
| 179 | * This function is suitable as the `f_rng` random generator function |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 180 | * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE |
| 181 | * to obtain the \p p_rng parameter. |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 182 | * |
| 183 | * The implementation of this function depends on the configuration of the |
| 184 | * library. |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 185 | * |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 186 | * \note Depending on the configuration, this may be a function or |
| 187 | * a pointer to a function. |
| 188 | * |
| 189 | * \note This function may only be used if the PSA crypto subsystem is active. |
| 190 | * This means that you must call psa_crypto_init() before any call to |
| 191 | * this function, and you must not call this function after calling |
| 192 | * mbedtls_psa_crypto_free(). |
| 193 | * |
| 194 | * \param p_rng The random generator context. This must be |
| 195 | * #MBEDTLS_PSA_RANDOM_STATE. No other state is |
| 196 | * supported. |
| 197 | * \param output The buffer to fill. It must have room for |
| 198 | * \c output_size bytes. |
| 199 | * \param output_size The number of bytes to write to \p output. |
| 200 | * This function may fail if \p output_size is too |
| 201 | * large. It is guaranteed to accept any output size |
| 202 | * requested by Mbed TLS library functions. The |
| 203 | * maximum request size depends on the library |
| 204 | * configuration. |
| 205 | * |
| 206 | * \return \c 0 on success. |
| 207 | * \return An `MBEDTLS_ERR_ENTROPY_xxx`, |
Gilles Peskine | 2cff7e2 | 2021-02-16 16:49:42 +0100 | [diff] [blame] | 208 | * `MBEDTLS_ERR_PLATFORM_xxx, |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 209 | * `MBEDTLS_ERR_CTR_DRBG_xxx` or |
| 210 | * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error. |
| 211 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | int mbedtls_psa_get_random(void *p_rng, |
| 213 | unsigned char *output, |
| 214 | size_t output_size); |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 215 | |
| 216 | /** The random generator state for the PSA subsystem. |
| 217 | * |
| 218 | * This macro expands to an expression which is suitable as the `p_rng` |
| 219 | * random generator state parameter of many `mbedtls_xxx` functions. |
| 220 | * It must be used in combination with the random generator function |
| 221 | * mbedtls_psa_get_random(). |
| 222 | * |
| 223 | * The implementation of this macro depends on the configuration of the |
| 224 | * library. Do not make any assumption on its nature. |
| 225 | */ |
| 226 | #define MBEDTLS_PSA_RANDOM_STATE NULL |
| 227 | |
| 228 | #else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
| 229 | |
| 230 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 231 | #include "mbedtls/ctr_drbg.h" |
| 232 | typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t; |
| 233 | static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random; |
| 234 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 235 | #include "mbedtls/hmac_drbg.h" |
| 236 | typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t; |
| 237 | static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random; |
| 238 | #endif |
| 239 | extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state; |
| 240 | |
| 241 | #define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state |
| 242 | |
| 243 | #endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */ |
| 244 | |
Andrzej Kurek | 747ab4e | 2023-02-28 10:32:47 -0500 | [diff] [blame] | 245 | typedef struct { |
Gilles Peskine | b567f83 | 2023-05-02 21:38:11 +0200 | [diff] [blame] | 246 | /* Error codes used by PSA crypto are in -255..-128, fitting in 16 bits. */ |
| 247 | int16_t psa_status; |
| 248 | /* Error codes used by Mbed TLS are in one of the ranges |
Gilles Peskine | 4837e9d | 2023-05-07 20:27:13 +0200 | [diff] [blame] | 249 | * -127..-1 (low-level) or -32767..-4096 (high-level with a low-level |
| 250 | * code optionally added), fitting in 16 bits. */ |
Andrzej Kurek | 747ab4e | 2023-02-28 10:32:47 -0500 | [diff] [blame] | 251 | int16_t mbedtls_error; |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 252 | } mbedtls_error_pair_t; |
Andrzej Kurek | 747ab4e | 2023-02-28 10:32:47 -0500 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 725d2e2 | 2023-03-29 12:38:37 +0200 | [diff] [blame] | 254 | #if defined(MBEDTLS_MD_LIGHT) |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 255 | extern const mbedtls_error_pair_t psa_to_md_errors[4]; |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 256 | #endif |
Gilles Peskine | e3ed802 | 2021-02-03 20:04:08 +0100 | [diff] [blame] | 257 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 258 | #if defined(MBEDTLS_LMS_C) |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 259 | extern const mbedtls_error_pair_t psa_to_lms_errors[3]; |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 260 | #endif |
| 261 | |
| 262 | #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 263 | extern const mbedtls_error_pair_t psa_to_ssl_errors[7]; |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 264 | #endif |
| 265 | |
| 266 | #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \ |
Valerio Setti | 8bb5763 | 2023-05-26 13:48:07 +0200 | [diff] [blame] | 267 | defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY) |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 268 | extern const mbedtls_error_pair_t psa_to_pk_rsa_errors[8]; |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 269 | #endif |
| 270 | |
| 271 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && \ |
| 272 | defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 273 | extern const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[7]; |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 274 | #endif |
| 275 | |
Andrzej Kurek | 138b30a | 2023-02-28 10:06:42 -0500 | [diff] [blame] | 276 | /* Generic fallback function for error translation, |
| 277 | * when the received state was not module-specific. */ |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 278 | int psa_generic_status_to_mbedtls(psa_status_t status); |
| 279 | |
Andrzej Kurek | 138b30a | 2023-02-28 10:06:42 -0500 | [diff] [blame] | 280 | /* This function iterates over provided local error translations, |
| 281 | * and if no match was found - calls the fallback error translation function. */ |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 282 | int psa_status_to_mbedtls(psa_status_t status, |
Andrzej Kurek | 270b3f9 | 2023-03-03 05:54:13 -0500 | [diff] [blame] | 283 | const mbedtls_error_pair_t *local_translations, |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 284 | size_t local_errors_num, |
| 285 | int (*fallback_f)(psa_status_t)); |
| 286 | |
Andrzej Kurek | 138b30a | 2023-02-28 10:06:42 -0500 | [diff] [blame] | 287 | /* The second out of three-stage error handling functions of the pk module, |
| 288 | * acts as a fallback after RSA / ECDSA error translation, and if no match |
| 289 | * is found, it itself calls psa_generic_status_to_mbedtls. */ |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 290 | int psa_pk_status_to_mbedtls(psa_status_t status); |
| 291 | |
Andrzej Kurek | 138b30a | 2023-02-28 10:06:42 -0500 | [diff] [blame] | 292 | /* Utility macro to shorten the defines of error translator in modules. */ |
Valerio Setti | ab9dc66 | 2023-03-27 14:02:08 +0200 | [diff] [blame] | 293 | #define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \ |
| 294 | psa_status_to_mbedtls(status, error_list, \ |
| 295 | sizeof(error_list)/sizeof(error_list[0]), \ |
| 296 | fallback_f) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 297 | |
| 298 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
Hanno Becker | 186b65a | 2018-11-19 15:14:21 +0000 | [diff] [blame] | 299 | #endif /* MBEDTLS_PSA_UTIL_H */ |