hash_info.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Hash information that's independent from the crypto implementation.
  3. *
  4. * This can be used by:
  5. * - code based on PSA
  6. * - code based on the legacy API
  7. * - code based on either of them depending on MBEDTLS_USE_PSA_CRYPTO
  8. * - code based on either of them depending on what's available
  9. *
  10. * Note: this internal module will go away when everything becomes based on
  11. * PSA Crypto; it is a helper for the transition while hash algorithms are
  12. * still represented using mbedtls_md_type_t in most places even when PSA is
  13. * used for the actual crypto computations.
  14. *
  15. * Copyright The Mbed TLS Contributors
  16. * SPDX-License-Identifier: Apache-2.0
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  19. * not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  26. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30. #ifndef MBEDTLS_HASH_INFO_H
  31. #define MBEDTLS_HASH_INFO_H
  32. #include "common.h"
  33. #include "mbedtls/md.h"
  34. #include "psa/crypto.h"
  35. #include "mbedtls/platform_util.h"
  36. /** \def MBEDTLS_HASH_MAX_SIZE
  37. *
  38. * Maximum size of a hash based on configuration.
  39. */
  40. #if defined(MBEDTLS_MD_C) && ( \
  41. !defined(MBEDTLS_PSA_CRYPTO_C) || \
  42. MBEDTLS_MD_MAX_SIZE >= PSA_HASH_MAX_SIZE)
  43. #define MBEDTLS_HASH_MAX_SIZE MBEDTLS_MD_MAX_SIZE
  44. #elif defined(MBEDTLS_PSA_CRYPTO_C) && ( \
  45. !defined(MBEDTLS_MD_C) || \
  46. PSA_HASH_MAX_SIZE >= MBEDTLS_MD_MAX_SIZE)
  47. #define MBEDTLS_HASH_MAX_SIZE PSA_HASH_MAX_SIZE
  48. #endif
  49. /** Get the output length of the given hash type from its MD type.
  50. *
  51. * \note To get the output length from the PSA alg, use \c PSA_HASH_LENGTH().
  52. *
  53. * \param md_type The hash MD type.
  54. *
  55. * \return The output length in bytes, or 0 if not known.
  56. */
  57. unsigned char mbedtls_hash_info_get_size(mbedtls_md_type_t md_type);
  58. /** Get the block size of the given hash type from its MD type.
  59. *
  60. * \note To get the output length from the PSA alg, use
  61. * \c PSA_HASH_BLOCK_LENGTH().
  62. *
  63. * \param md_type The hash MD type.
  64. *
  65. * \return The block size in bytes, or 0 if not known.
  66. */
  67. unsigned char mbedtls_hash_info_get_block_size(mbedtls_md_type_t md_type);
  68. /** Get the PSA alg from the MD type.
  69. *
  70. * \param md_type The hash MD type.
  71. *
  72. * \return The corresponding PSA algorithm identifier,
  73. * or PSA_ALG_NONE if not known.
  74. */
  75. psa_algorithm_t mbedtls_hash_info_psa_from_md(mbedtls_md_type_t md_type);
  76. /** Get the MD type alg from the PSA algorithm identifier.
  77. *
  78. * \param psa_alg The PSA hash algorithm.
  79. *
  80. * \return The corresponding MD type,
  81. * or MBEDTLS_MD_NONE if not known.
  82. */
  83. mbedtls_md_type_t mbedtls_hash_info_md_from_psa(psa_algorithm_t psa_alg);
  84. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  85. /** Convert PSA status to MD error code.
  86. *
  87. * \param status PSA status.
  88. *
  89. * \return The corresponding MD error code,
  90. */
  91. int MBEDTLS_DEPRECATED mbedtls_md_error_from_psa(psa_status_t status);
  92. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  93. #endif /* MBEDTLS_HASH_INFO_H */