blob: 8f58d4dc1634cb3be646d831e0f3b3d46d895cca [file] [log] [blame]
Gilles Peskine514a8fd2020-11-13 17:41:53 +01001/** \file psa_crypto_helpers.c
2 *
3 * \brief Helper functions to test PSA crypto functionality.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include <test/helpers.h>
24#include <test/macros.h>
Przemyslaw Stekiel53de2622021-11-03 09:35:35 +010025#include <psa_crypto_slot_management.h>
Gilles Peskined4008d52020-11-24 17:34:30 +010026#include <test/psa_crypto_helpers.h>
Andrzej Kurekb22b9772023-05-30 09:44:20 -040027#include "common.h"
Gilles Peskine514a8fd2020-11-13 17:41:53 +010028
29#if defined(MBEDTLS_PSA_CRYPTO_C)
30
31#include <psa/crypto.h>
32
Gilles Peskine313ffb82021-02-14 12:51:14 +010033#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
34
35#include <psa_crypto_storage.h>
36
37static mbedtls_svc_key_id_t key_ids_used_in_test[9];
38static size_t num_key_ids_used;
39
Gilles Peskine449bd832023-01-11 14:50:10 +010040int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)
Gilles Peskine313ffb82021-02-14 12:51:14 +010041{
42 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010043 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id) >
44 PSA_MAX_PERSISTENT_KEY_IDENTIFIER) {
Gilles Peskine313ffb82021-02-14 12:51:14 +010045 /* Don't touch key id values that designate non-key files. */
Gilles Peskine449bd832023-01-11 14:50:10 +010046 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010047 }
Gilles Peskine449bd832023-01-11 14:50:10 +010048 for (i = 0; i < num_key_ids_used; i++) {
49 if (mbedtls_svc_key_id_equal(key_id, key_ids_used_in_test[i])) {
50 return 1;
51 }
Gilles Peskine313ffb82021-02-14 12:51:14 +010052 }
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if (num_key_ids_used == ARRAY_LENGTH(key_ids_used_in_test)) {
54 return 0;
55 }
Gilles Peskine313ffb82021-02-14 12:51:14 +010056 key_ids_used_in_test[num_key_ids_used] = key_id;
57 ++num_key_ids_used;
Gilles Peskine449bd832023-01-11 14:50:10 +010058 return 1;
Gilles Peskine313ffb82021-02-14 12:51:14 +010059}
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061void mbedtls_test_psa_purge_key_storage(void)
Gilles Peskine313ffb82021-02-14 12:51:14 +010062{
63 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010064 for (i = 0; i < num_key_ids_used; i++) {
65 psa_destroy_persistent_key(key_ids_used_in_test[i]);
66 }
Gilles Peskine313ffb82021-02-14 12:51:14 +010067 num_key_ids_used = 0;
68}
Gilles Peskineaae718c2021-02-14 13:46:39 +010069
Gilles Peskine449bd832023-01-11 14:50:10 +010070void mbedtls_test_psa_purge_key_cache(void)
Gilles Peskineaae718c2021-02-14 13:46:39 +010071{
72 size_t i;
Gilles Peskine449bd832023-01-11 14:50:10 +010073 for (i = 0; i < num_key_ids_used; i++) {
74 psa_purge_key(key_ids_used_in_test[i]);
75 }
Gilles Peskineaae718c2021-02-14 13:46:39 +010076}
77
Gilles Peskine313ffb82021-02-14 12:51:14 +010078#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080const char *mbedtls_test_helper_is_psa_leaking(void)
Gilles Peskined4008d52020-11-24 17:34:30 +010081{
82 mbedtls_psa_stats_t stats;
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_psa_get_stats(&stats);
Gilles Peskined4008d52020-11-24 17:34:30 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (stats.volatile_slots != 0) {
87 return "A volatile slot has not been closed properly.";
88 }
89 if (stats.persistent_slots != 0) {
90 return "A persistent slot has not been closed properly.";
91 }
92 if (stats.external_slots != 0) {
93 return "An external slot has not been closed properly.";
94 }
95 if (stats.half_filled_slots != 0) {
96 return "A half-filled slot has not been cleared properly.";
97 }
98 if (stats.locked_slots != 0) {
99 return "Some slots are still marked as locked.";
100 }
Gilles Peskined4008d52020-11-24 17:34:30 +0100101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return NULL;
Gilles Peskined4008d52020-11-24 17:34:30 +0100103}
104
105#if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
106/** Name of the file where return statuses are logged by #RECORD_STATUS. */
107#define STATUS_LOG_FILE_NAME "statuses.log"
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109psa_status_t mbedtls_test_record_status(psa_status_t status,
110 const char *func,
111 const char *file, int line,
112 const char *expr)
Gilles Peskined4008d52020-11-24 17:34:30 +0100113{
114 /* We open the log file on first use.
115 * We never close the log file, so the record_status feature is not
116 * compatible with resource leak detectors such as Asan.
117 */
118 static FILE *log;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (log == NULL) {
120 log = fopen(STATUS_LOG_FILE_NAME, "a");
121 }
122 fprintf(log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr);
123 return status;
Gilles Peskined4008d52020-11-24 17:34:30 +0100124}
125#endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200128{
129 psa_key_usage_t updated_usage = usage_flags;
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200132 updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 }
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200136 updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 return updated_usage;
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200140}
141
Yanray Wange64b4052022-10-28 18:12:01 +0800142int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename)
143{
144 const char *msg = mbedtls_test_helper_is_psa_leaking();
145 if (msg == NULL) {
146 return 0;
147 } else {
148 mbedtls_test_fail(msg, line_no, filename);
149 return 1;
150 }
151}
152
Gilles Peskine514a8fd2020-11-13 17:41:53 +0100153#endif /* MBEDTLS_PSA_CRYPTO_C */