psa_util.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * PSA hashing layer on top of Mbed TLS software crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include <psa/crypto.h>
  23. #include "psa_crypto_core.h"
  24. #include <mbedtls/psa_util.h>
  25. #include <mbedtls/error.h>
  26. #include <mbedtls/lms.h>
  27. #include <mbedtls/ssl.h>
  28. #include <mbedtls/rsa.h>
  29. /* PSA_SUCCESS is kept at the top of each error table since
  30. * it's the most common status when everything functions properly. */
  31. #if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_MD5_C) || defined(MBEDTLS_USE_PSA_CRYPTO)
  32. const mbedtls_error_pair_t psa_to_md_errors[] =
  33. {
  34. { PSA_SUCCESS, 0 },
  35. { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
  36. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
  37. { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
  38. };
  39. #endif
  40. #if defined(MBEDTLS_LMS_C)
  41. const mbedtls_error_pair_t psa_to_lms_errors[] =
  42. {
  43. { PSA_SUCCESS, 0 },
  44. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
  45. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
  46. };
  47. #endif
  48. #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
  49. const mbedtls_error_pair_t psa_to_ssl_errors[] =
  50. {
  51. { PSA_SUCCESS, 0 },
  52. { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
  53. { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
  54. { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
  55. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
  56. { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
  57. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
  58. };
  59. #endif
  60. #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
  61. defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
  62. const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
  63. {
  64. { PSA_SUCCESS, 0 },
  65. { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
  66. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
  67. { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
  68. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
  69. { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
  70. { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
  71. { PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
  72. };
  73. #endif
  74. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  75. defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  76. const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
  77. {
  78. { PSA_SUCCESS, 0 },
  79. { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
  80. { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
  81. { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
  82. { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
  83. { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
  84. { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
  85. };
  86. #endif
  87. int psa_generic_status_to_mbedtls(psa_status_t status)
  88. {
  89. switch (status) {
  90. case PSA_SUCCESS:
  91. return 0;
  92. case PSA_ERROR_NOT_SUPPORTED:
  93. return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
  94. case PSA_ERROR_CORRUPTION_DETECTED:
  95. return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  96. case PSA_ERROR_COMMUNICATION_FAILURE:
  97. case PSA_ERROR_HARDWARE_FAILURE:
  98. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  99. case PSA_ERROR_NOT_PERMITTED:
  100. default:
  101. return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
  102. }
  103. }
  104. int psa_status_to_mbedtls(psa_status_t status,
  105. const mbedtls_error_pair_t *local_translations,
  106. size_t local_errors_num,
  107. int (*fallback_f)(psa_status_t))
  108. {
  109. for (size_t i = 0; i < local_errors_num; i++) {
  110. if (status == local_translations[i].psa_status) {
  111. return local_translations[i].mbedtls_error;
  112. }
  113. }
  114. return fallback_f(status);
  115. }
  116. int psa_pk_status_to_mbedtls(psa_status_t status)
  117. {
  118. switch (status) {
  119. case PSA_ERROR_INVALID_HANDLE:
  120. return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
  121. case PSA_ERROR_BUFFER_TOO_SMALL:
  122. return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
  123. case PSA_ERROR_NOT_SUPPORTED:
  124. return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
  125. case PSA_ERROR_INVALID_ARGUMENT:
  126. return MBEDTLS_ERR_PK_INVALID_ALG;
  127. case PSA_ERROR_INSUFFICIENT_MEMORY:
  128. return MBEDTLS_ERR_PK_ALLOC_FAILED;
  129. case PSA_ERROR_BAD_STATE:
  130. return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
  131. case PSA_ERROR_DATA_CORRUPT:
  132. case PSA_ERROR_DATA_INVALID:
  133. case PSA_ERROR_STORAGE_FAILURE:
  134. return MBEDTLS_ERR_PK_FILE_IO_ERROR;
  135. default:
  136. return psa_generic_status_to_mbedtls(status);
  137. }
  138. }
  139. #endif /* MBEDTLS_PSA_CRYPTO_C */