Gilles Peskine | 514a8fd | 2020-11-13 17:41:53 +0100 | [diff] [blame] | 1 | /** \file psa_crypto_helpers.c |
| 2 | * |
| 3 | * \brief Helper functions to test PSA crypto functionality. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 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 |
Gilles Peskine | 514a8fd | 2020-11-13 17:41:53 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <test/helpers.h> |
| 12 | #include <test/macros.h> |
Przemyslaw Stekiel | 53de262 | 2021-11-03 09:35:35 +0100 | [diff] [blame] | 13 | #include <psa_crypto_slot_management.h> |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 14 | #include <test/psa_crypto_helpers.h> |
Gilles Peskine | 514a8fd | 2020-11-13 17:41:53 +0100 | [diff] [blame] | 15 | |
Gilles Peskine | 4804847 | 2024-06-20 21:47:31 +0200 | [diff] [blame^] | 16 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 17 | #include <mbedtls/ctr_drbg.h> |
| 18 | #endif |
| 19 | |
Gilles Peskine | 514a8fd | 2020-11-13 17:41:53 +0100 | [diff] [blame] | 20 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 21 | |
| 22 | #include <psa/crypto.h> |
| 23 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 24 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 25 | |
| 26 | #include <psa_crypto_storage.h> |
| 27 | |
| 28 | static mbedtls_svc_key_id_t key_ids_used_in_test[9]; |
| 29 | static size_t num_key_ids_used; |
| 30 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 31 | int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id) |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 32 | { |
| 33 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id) > |
| 35 | PSA_MAX_PERSISTENT_KEY_IDENTIFIER) { |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 36 | /* Don't touch key id values that designate non-key files. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | return 1; |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 38 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 39 | for (i = 0; i < num_key_ids_used; i++) { |
| 40 | if (mbedtls_svc_key_id_equal(key_id, key_ids_used_in_test[i])) { |
| 41 | return 1; |
| 42 | } |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 43 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | if (num_key_ids_used == ARRAY_LENGTH(key_ids_used_in_test)) { |
| 45 | return 0; |
| 46 | } |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 47 | key_ids_used_in_test[num_key_ids_used] = key_id; |
| 48 | ++num_key_ids_used; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | return 1; |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | void mbedtls_test_psa_purge_key_storage(void) |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 53 | { |
| 54 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | for (i = 0; i < num_key_ids_used; i++) { |
| 56 | psa_destroy_persistent_key(key_ids_used_in_test[i]); |
| 57 | } |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 58 | num_key_ids_used = 0; |
| 59 | } |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 60 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | void mbedtls_test_psa_purge_key_cache(void) |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 62 | { |
| 63 | size_t i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | for (i = 0; i < num_key_ids_used; i++) { |
| 65 | psa_purge_key(key_ids_used_in_test[i]); |
| 66 | } |
Gilles Peskine | aae718c | 2021-02-14 13:46:39 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Gilles Peskine | 313ffb8 | 2021-02-14 12:51:14 +0100 | [diff] [blame] | 69 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 70 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | const char *mbedtls_test_helper_is_psa_leaking(void) |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 72 | { |
| 73 | mbedtls_psa_stats_t stats; |
| 74 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | mbedtls_psa_get_stats(&stats); |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | 4804847 | 2024-06-20 21:47:31 +0200 | [diff] [blame^] | 77 | #if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \ |
| 78 | defined(MBEDTLS_CTR_DRBG_C) && \ |
| 79 | defined(MBEDTLS_CTR_DRBG_USE_PSA_CRYPTO) |
Valerio Setti | 7448367 | 2023-11-24 08:36:12 +0100 | [diff] [blame] | 80 | /* When AES_C is not defined and PSA does not have an external RNG, |
| 81 | * then CTR_DRBG uses PSA to perform AES-ECB. In this scenario 1 key |
| 82 | * slot is used internally from PSA to hold the AES key and it should |
| 83 | * not be taken into account when evaluating remaining open slots. */ |
| 84 | if (stats.volatile_slots > 1) { |
| 85 | return "A volatile slot has not been closed properly."; |
| 86 | } |
| 87 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (stats.volatile_slots != 0) { |
| 89 | return "A volatile slot has not been closed properly."; |
| 90 | } |
Valerio Setti | 7448367 | 2023-11-24 08:36:12 +0100 | [diff] [blame] | 91 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | if (stats.persistent_slots != 0) { |
| 93 | return "A persistent slot has not been closed properly."; |
| 94 | } |
| 95 | if (stats.external_slots != 0) { |
| 96 | return "An external slot has not been closed properly."; |
| 97 | } |
| 98 | if (stats.half_filled_slots != 0) { |
| 99 | return "A half-filled slot has not been cleared properly."; |
| 100 | } |
| 101 | if (stats.locked_slots != 0) { |
| 102 | return "Some slots are still marked as locked."; |
| 103 | } |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | return NULL; |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | #if defined(RECORD_PSA_STATUS_COVERAGE_LOG) |
| 109 | /** Name of the file where return statuses are logged by #RECORD_STATUS. */ |
| 110 | #define STATUS_LOG_FILE_NAME "statuses.log" |
| 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | psa_status_t mbedtls_test_record_status(psa_status_t status, |
| 113 | const char *func, |
| 114 | const char *file, int line, |
| 115 | const char *expr) |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 116 | { |
| 117 | /* We open the log file on first use. |
| 118 | * We never close the log file, so the record_status feature is not |
| 119 | * compatible with resource leak detectors such as Asan. |
| 120 | */ |
| 121 | static FILE *log; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | if (log == NULL) { |
| 123 | log = fopen(STATUS_LOG_FILE_NAME, "a"); |
| 124 | } |
| 125 | fprintf(log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr); |
| 126 | return status; |
Gilles Peskine | d4008d5 | 2020-11-24 17:34:30 +0100 | [diff] [blame] | 127 | } |
| 128 | #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */ |
| 129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags) |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 131 | { |
| 132 | psa_key_usage_t updated_usage = usage_flags; |
| 133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | if (usage_flags & PSA_KEY_USAGE_SIGN_HASH) { |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 135 | updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | } |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | if (usage_flags & PSA_KEY_USAGE_VERIFY_HASH) { |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 139 | updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | } |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | return updated_usage; |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Yanray Wang | e64b405 | 2022-10-28 18:12:01 +0800 | [diff] [blame] | 145 | int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename) |
| 146 | { |
| 147 | const char *msg = mbedtls_test_helper_is_psa_leaking(); |
| 148 | if (msg == NULL) { |
| 149 | return 0; |
| 150 | } else { |
| 151 | mbedtls_test_fail(msg, line_no, filename); |
| 152 | return 1; |
| 153 | } |
| 154 | } |
| 155 | |
Kusumit Ghoderao | 7c61ffc | 2023-09-05 19:29:47 +0530 | [diff] [blame] | 156 | uint64_t mbedtls_test_parse_binary_string(data_t *bin_string) |
Kusumit Ghoderao | ac7a04a | 2023-08-18 13:47:47 +0530 | [diff] [blame] | 157 | { |
| 158 | uint64_t result = 0; |
| 159 | TEST_LE_U(bin_string->len, 8); |
| 160 | for (size_t i = 0; i < bin_string->len; i++) { |
| 161 | result = result << 8 | bin_string->x[i]; |
| 162 | } |
| 163 | exit: |
| 164 | return result; /* returns 0 if len > 8 */ |
| 165 | } |
| 166 | |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 167 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 168 | |
| 169 | #include <mbedtls/entropy.h> |
| 170 | #include <psa_crypto_its.h> |
| 171 | |
| 172 | int mbedtls_test_inject_entropy_seed_read(unsigned char *buf, size_t len) |
| 173 | { |
| 174 | size_t actual_len = 0; |
| 175 | psa_status_t status = psa_its_get(PSA_CRYPTO_ITS_RANDOM_SEED_UID, |
| 176 | 0, len, buf, &actual_len); |
| 177 | if (status != 0) { |
| 178 | return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
| 179 | } |
| 180 | if (actual_len != len) { |
| 181 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int mbedtls_test_inject_entropy_seed_write(unsigned char *buf, size_t len) |
| 187 | { |
| 188 | psa_status_t status = psa_its_set(PSA_CRYPTO_ITS_RANDOM_SEED_UID, |
| 189 | len, buf, 0); |
| 190 | if (status != 0) { |
| 191 | return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 196 | int mbedtls_test_inject_entropy_restore(void) |
| 197 | { |
| 198 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 199 | for (size_t i = 0; i < sizeof(buf); i++) { |
| 200 | buf[i] = (unsigned char) i; |
| 201 | } |
| 202 | psa_status_t status = mbedtls_psa_inject_entropy(buf, sizeof(buf)); |
| 203 | /* It's ok if the file was just created, or if it already exists. */ |
| 204 | if (status != PSA_SUCCESS && status != PSA_ERROR_NOT_PERMITTED) { |
| 205 | return status; |
| 206 | } |
| 207 | return PSA_SUCCESS; |
| 208 | } |
| 209 | |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 210 | #endif /* MBEDTLS_PSA_INJECT_ENTROPY */ |
| 211 | |
Gilles Peskine | 514a8fd | 2020-11-13 17:41:53 +0100 | [diff] [blame] | 212 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |