psa_util_internal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * \file psa_util_internal.h
  3. *
  4. * \brief Internal utility functions for use of PSA Crypto.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_PSA_UTIL_INTERNAL_H
  11. #define MBEDTLS_PSA_UTIL_INTERNAL_H
  12. /* Include the public header so that users only need one include. */
  13. #include "mbedtls/psa_util.h"
  14. #include "psa/crypto.h"
  15. #if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
  16. /*************************************************************************
  17. * FFDH
  18. ************************************************************************/
  19. #define MBEDTLS_PSA_MAX_FFDH_PUBKEY_LENGTH \
  20. PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
  21. /*************************************************************************
  22. * ECC
  23. ************************************************************************/
  24. #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \
  25. PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
  26. #define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \
  27. PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
  28. /*************************************************************************
  29. * Error translation
  30. ************************************************************************/
  31. typedef struct {
  32. /* Error codes used by PSA crypto are in -255..-128, fitting in 16 bits. */
  33. int16_t psa_status;
  34. /* Error codes used by Mbed TLS are in one of the ranges
  35. * -127..-1 (low-level) or -32767..-4096 (high-level with a low-level
  36. * code optionally added), fitting in 16 bits. */
  37. int16_t mbedtls_error;
  38. } mbedtls_error_pair_t;
  39. #if defined(MBEDTLS_MD_LIGHT)
  40. extern const mbedtls_error_pair_t psa_to_md_errors[4];
  41. #endif
  42. #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
  43. extern const mbedtls_error_pair_t psa_to_cipher_errors[4];
  44. #endif
  45. #if defined(MBEDTLS_LMS_C)
  46. extern const mbedtls_error_pair_t psa_to_lms_errors[3];
  47. #endif
  48. #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
  49. extern const mbedtls_error_pair_t psa_to_ssl_errors[7];
  50. #endif
  51. #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
  52. defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
  53. extern const mbedtls_error_pair_t psa_to_pk_rsa_errors[8];
  54. #endif
  55. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  56. defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
  57. extern const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[7];
  58. #endif
  59. /* Generic fallback function for error translation,
  60. * when the received state was not module-specific. */
  61. int psa_generic_status_to_mbedtls(psa_status_t status);
  62. /* This function iterates over provided local error translations,
  63. * and if no match was found - calls the fallback error translation function. */
  64. int psa_status_to_mbedtls(psa_status_t status,
  65. const mbedtls_error_pair_t *local_translations,
  66. size_t local_errors_num,
  67. int (*fallback_f)(psa_status_t));
  68. /* The second out of three-stage error handling functions of the pk module,
  69. * acts as a fallback after RSA / ECDSA error translation, and if no match
  70. * is found, it itself calls psa_generic_status_to_mbedtls. */
  71. int psa_pk_status_to_mbedtls(psa_status_t status);
  72. /* Utility macro to shorten the defines of error translator in modules. */
  73. #define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \
  74. psa_status_to_mbedtls(status, error_list, \
  75. sizeof(error_list)/sizeof(error_list[0]), \
  76. fallback_f)
  77. #endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
  78. #endif /* MBEDTLS_PSA_UTIL_INTERNAL_H */