sha1_alt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "sha1_alt.h"
  8. #if defined(MBEDTLS_SHA1_C)
  9. #include "hpm_l1c_drv.h"
  10. #include "mbedtls/platform_util.h"
  11. #include "mbedtls/sha1.h"
  12. #include <string.h>
  13. #if defined(MBEDTLS_PADLOCK_C)
  14. #include "mbedtls/padlock.h"
  15. #endif
  16. #if defined(MBEDTLS_AESNI_C)
  17. #include "mbedtls/aesni.h"
  18. #endif
  19. #if defined(MBEDTLS_THREADING_C)
  20. #include "ksdk_mbedtls.h"
  21. #include "mbedtls/threading.h"
  22. #endif
  23. /******************************************************************************/
  24. /*************************** SHA1**********************************************/
  25. /******************************************************************************/
  26. #if defined(MBEDTLS_HPM_SDP_SHA1)
  27. #include "mbedtls/sha1.h"
  28. #include "board.h"
  29. void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
  30. {
  31. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  32. memset(ctx, 0, sizeof(mbedtls_sha1_context));
  33. hpm_sdp_api_init();
  34. (void)rom_sdp_hash_init(p_sys_sdp_ctx, sdp_hash_alg_sha1);
  35. }
  36. void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
  37. {
  38. if (ctx == NULL)
  39. return;
  40. mbedtls_zeroize(ctx, sizeof(mbedtls_sha1_context));
  41. }
  42. void mbedtls_sha1_clone(mbedtls_sha1_context *dst, const mbedtls_sha1_context *src)
  43. {
  44. memcpy(dst, src, sizeof(mbedtls_sha1_context));
  45. }
  46. /*
  47. * SHA-1 context setup
  48. */
  49. int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
  50. {
  51. hpm_stat_t ret = status_fail;
  52. (void)ctx;
  53. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  54. ret = rom_sdp_hash_init(p_sys_sdp_ctx, sdp_hash_alg_sha1);
  55. if (ret != status_success)
  56. {
  57. return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED;
  58. }
  59. return 0;
  60. }
  61. int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
  62. {
  63. static uint8_t ATTR_PLACE_AT_NONCACHEABLE local[64];
  64. hpm_stat_t ret = status_fail;
  65. (void)ctx;
  66. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  67. memcpy(local, data, 64);
  68. ret = rom_sdp_hash_update(p_sys_sdp_ctx, local, 64);
  69. if (ret != status_success)
  70. {
  71. return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * SHA-1 process buffer
  77. */
  78. int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen)
  79. {
  80. static uint8_t ATTR_PLACE_AT_NONCACHEABLE local[64];
  81. hpm_stat_t ret = status_fail;
  82. for (uint32_t i = 0; i < ilen; i += 64)
  83. {
  84. size_t len = ilen - i > 64 ? 64 : ilen - i;
  85. if (len < 64)
  86. {
  87. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  88. memcpy(local, input + i, len);
  89. ret = rom_sdp_hash_update(p_sys_sdp_ctx, local, len);
  90. if (ret != status_success)
  91. {
  92. return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED;
  93. }
  94. return 0;
  95. }
  96. else
  97. {
  98. ret = mbedtls_internal_sha1_process(ctx, input + i);
  99. if (ret != status_success)
  100. {
  101. return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED;
  102. }
  103. }
  104. }
  105. return 0;
  106. }
  107. /*
  108. * SHA-1 final digest
  109. */
  110. int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx, unsigned char output[20])
  111. {
  112. static uint8_t ATTR_PLACE_AT_NONCACHEABLE local[20];
  113. hpm_stat_t ret = status_fail;
  114. (void)ctx;
  115. sdp_hash_ctx_t *p_sys_sdp_ctx = (sdp_hash_ctx_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)&s_hash_ctx);
  116. uint8_t *pout = (uint8_t *)core_local_mem_to_sys_address(BOARD_RUNNING_CORE, (uint32_t)local);
  117. ret = rom_sdp_hash_finish(p_sys_sdp_ctx, pout);
  118. if (ret != status_success)
  119. {
  120. return MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED;
  121. }
  122. memcpy(output, pout, 20);
  123. return 0;
  124. }
  125. #endif /* MBEDTLS_HPM_SDP_SHA1 */
  126. #endif /* MBEDTLS_SHA1_C */