psa_crypto_helpers.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /** \file psa_crypto_helpers.c
  2. *
  3. * \brief Helper functions to test PSA crypto functionality.
  4. */
  5. /*
  6. * Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. #include <test/helpers.h>
  22. #include <test/macros.h>
  23. #include <psa_crypto_slot_management.h>
  24. #include <test/psa_crypto_helpers.h>
  25. #if defined(MBEDTLS_PSA_CRYPTO_C)
  26. #include <psa/crypto.h>
  27. #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
  28. #include <psa_crypto_storage.h>
  29. static mbedtls_svc_key_id_t key_ids_used_in_test[9];
  30. static size_t num_key_ids_used;
  31. int mbedtls_test_uses_key_id(mbedtls_svc_key_id_t key_id)
  32. {
  33. size_t i;
  34. if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(key_id) >
  35. PSA_MAX_PERSISTENT_KEY_IDENTIFIER) {
  36. /* Don't touch key id values that designate non-key files. */
  37. return 1;
  38. }
  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. }
  43. }
  44. if (num_key_ids_used == ARRAY_LENGTH(key_ids_used_in_test)) {
  45. return 0;
  46. }
  47. key_ids_used_in_test[num_key_ids_used] = key_id;
  48. ++num_key_ids_used;
  49. return 1;
  50. }
  51. void mbedtls_test_psa_purge_key_storage(void)
  52. {
  53. size_t i;
  54. for (i = 0; i < num_key_ids_used; i++) {
  55. psa_destroy_persistent_key(key_ids_used_in_test[i]);
  56. }
  57. num_key_ids_used = 0;
  58. }
  59. void mbedtls_test_psa_purge_key_cache(void)
  60. {
  61. size_t i;
  62. for (i = 0; i < num_key_ids_used; i++) {
  63. psa_purge_key(key_ids_used_in_test[i]);
  64. }
  65. }
  66. #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
  67. const char *mbedtls_test_helper_is_psa_leaking(void)
  68. {
  69. mbedtls_psa_stats_t stats;
  70. mbedtls_psa_get_stats(&stats);
  71. if (stats.volatile_slots != 0) {
  72. return "A volatile slot has not been closed properly.";
  73. }
  74. if (stats.persistent_slots != 0) {
  75. return "A persistent slot has not been closed properly.";
  76. }
  77. if (stats.external_slots != 0) {
  78. return "An external slot has not been closed properly.";
  79. }
  80. if (stats.half_filled_slots != 0) {
  81. return "A half-filled slot has not been cleared properly.";
  82. }
  83. if (stats.locked_slots != 0) {
  84. return "Some slots are still marked as locked.";
  85. }
  86. return NULL;
  87. }
  88. #if defined(RECORD_PSA_STATUS_COVERAGE_LOG)
  89. /** Name of the file where return statuses are logged by #RECORD_STATUS. */
  90. #define STATUS_LOG_FILE_NAME "statuses.log"
  91. psa_status_t mbedtls_test_record_status(psa_status_t status,
  92. const char *func,
  93. const char *file, int line,
  94. const char *expr)
  95. {
  96. /* We open the log file on first use.
  97. * We never close the log file, so the record_status feature is not
  98. * compatible with resource leak detectors such as Asan.
  99. */
  100. static FILE *log;
  101. if (log == NULL) {
  102. log = fopen(STATUS_LOG_FILE_NAME, "a");
  103. }
  104. fprintf(log, "%d:%s:%s:%d:%s\n", (int) status, func, file, line, expr);
  105. return status;
  106. }
  107. #endif /* defined(RECORD_PSA_STATUS_COVERAGE_LOG) */
  108. psa_key_usage_t mbedtls_test_update_key_usage_flags(psa_key_usage_t usage_flags)
  109. {
  110. psa_key_usage_t updated_usage = usage_flags;
  111. if (usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
  112. updated_usage |= PSA_KEY_USAGE_SIGN_MESSAGE;
  113. }
  114. if (usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
  115. updated_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
  116. }
  117. return updated_usage;
  118. }
  119. int mbedtls_test_fail_if_psa_leaking(int line_no, const char *filename)
  120. {
  121. const char *msg = mbedtls_test_helper_is_psa_leaking();
  122. if (msg == NULL) {
  123. return 0;
  124. } else {
  125. mbedtls_test_fail(msg, line_no, filename);
  126. return 1;
  127. }
  128. }
  129. #endif /* MBEDTLS_PSA_CRYPTO_C */