hash_info.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Hash information that's independent from the crypto implementation.
  3. *
  4. * (See the corresponding header file for usage notes.)
  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. #include "hash_info.h"
  23. #include "mbedtls/legacy_or_psa.h"
  24. #include "mbedtls/error.h"
  25. typedef struct {
  26. psa_algorithm_t psa_alg;
  27. mbedtls_md_type_t md_type;
  28. unsigned char size;
  29. unsigned char block_size;
  30. } hash_entry;
  31. static const hash_entry hash_table[] = {
  32. #if defined(MBEDTLS_HAS_ALG_MD5_VIA_LOWLEVEL_OR_PSA)
  33. { PSA_ALG_MD5, MBEDTLS_MD_MD5, 16, 64 },
  34. #endif
  35. #if defined(MBEDTLS_HAS_ALG_RIPEMD160_VIA_LOWLEVEL_OR_PSA)
  36. { PSA_ALG_RIPEMD160, MBEDTLS_MD_RIPEMD160, 20, 64 },
  37. #endif
  38. #if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
  39. { PSA_ALG_SHA_1, MBEDTLS_MD_SHA1, 20, 64 },
  40. #endif
  41. #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA)
  42. { PSA_ALG_SHA_224, MBEDTLS_MD_SHA224, 28, 64 },
  43. #endif
  44. #if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA)
  45. { PSA_ALG_SHA_256, MBEDTLS_MD_SHA256, 32, 64 },
  46. #endif
  47. #if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
  48. { PSA_ALG_SHA_384, MBEDTLS_MD_SHA384, 48, 128 },
  49. #endif
  50. #if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_LOWLEVEL_OR_PSA)
  51. { PSA_ALG_SHA_512, MBEDTLS_MD_SHA512, 64, 128 },
  52. #endif
  53. { PSA_ALG_NONE, MBEDTLS_MD_NONE, 0, 0 },
  54. };
  55. /* Get size from MD type */
  56. unsigned char mbedtls_hash_info_get_size(mbedtls_md_type_t md_type)
  57. {
  58. const hash_entry *entry = hash_table;
  59. while (entry->md_type != MBEDTLS_MD_NONE &&
  60. entry->md_type != md_type) {
  61. entry++;
  62. }
  63. return entry->size;
  64. }
  65. /* Get block size from MD type */
  66. unsigned char mbedtls_hash_info_get_block_size(mbedtls_md_type_t md_type)
  67. {
  68. const hash_entry *entry = hash_table;
  69. while (entry->md_type != MBEDTLS_MD_NONE &&
  70. entry->md_type != md_type) {
  71. entry++;
  72. }
  73. return entry->block_size;
  74. }
  75. /* Get PSA from MD */
  76. psa_algorithm_t mbedtls_hash_info_psa_from_md(mbedtls_md_type_t md_type)
  77. {
  78. const hash_entry *entry = hash_table;
  79. while (entry->md_type != MBEDTLS_MD_NONE &&
  80. entry->md_type != md_type) {
  81. entry++;
  82. }
  83. return entry->psa_alg;
  84. }
  85. /* Get MD from PSA */
  86. mbedtls_md_type_t mbedtls_hash_info_md_from_psa(psa_algorithm_t psa_alg)
  87. {
  88. const hash_entry *entry = hash_table;
  89. while (entry->md_type != MBEDTLS_MD_NONE &&
  90. entry->psa_alg != psa_alg) {
  91. entry++;
  92. }
  93. return entry->md_type;
  94. }
  95. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  96. int mbedtls_md_error_from_psa(psa_status_t status)
  97. {
  98. switch (status) {
  99. case PSA_SUCCESS:
  100. return 0;
  101. case PSA_ERROR_NOT_SUPPORTED:
  102. return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
  103. case PSA_ERROR_INVALID_ARGUMENT:
  104. return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  105. case PSA_ERROR_INSUFFICIENT_MEMORY:
  106. return MBEDTLS_ERR_MD_ALLOC_FAILED;
  107. default:
  108. return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
  109. }
  110. }
  111. #endif /* !MBEDTLS_DEPRECATED_REMOVED */