sha_hal.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // The HAL layer for SHA
  15. #include "hal/sha_hal.h"
  16. #include "hal/sha_types.h"
  17. #include "hal/sha_ll.h"
  18. #include "soc/soc_caps.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #if SOC_SHA_CRYPTO_DMA
  22. #include "soc/crypto_dma_reg.h"
  23. #include "hal/crypto_dma_ll.h"
  24. #elif SOC_SHA_GENERAL_DMA
  25. #include "hal/gdma_ll.h"
  26. #endif
  27. #define SHA1_STATE_LEN_WORDS (160 / 32)
  28. #define SHA256_STATE_LEN_WORDS (256 / 32)
  29. #define SHA512_STATE_LEN_WORDS (512 / 32)
  30. #if CONFIG_IDF_TARGET_ESP32
  31. /* Return state size (in words) for a given SHA type */
  32. inline static size_t state_length(esp_sha_type type)
  33. {
  34. switch (type) {
  35. case SHA1:
  36. return SHA1_STATE_LEN_WORDS;
  37. case SHA2_256:
  38. return SHA256_STATE_LEN_WORDS;
  39. case SHA2_384:
  40. case SHA2_512:
  41. return SHA512_STATE_LEN_WORDS;
  42. default:
  43. return 0;
  44. }
  45. }
  46. #else
  47. /* Return state size (in words) for a given SHA type */
  48. inline static size_t state_length(esp_sha_type type)
  49. {
  50. switch (type) {
  51. case SHA1:
  52. return SHA1_STATE_LEN_WORDS;
  53. case SHA2_224:
  54. case SHA2_256:
  55. return SHA256_STATE_LEN_WORDS;
  56. case SHA2_384:
  57. case SHA2_512:
  58. case SHA2_512224:
  59. case SHA2_512256:
  60. case SHA2_512T:
  61. return SHA512_STATE_LEN_WORDS;
  62. default:
  63. return 0;
  64. }
  65. }
  66. #endif
  67. /* Hash a single block */
  68. void sha_hal_hash_block(esp_sha_type sha_type, const void *data_block, size_t block_word_len, bool first_block)
  69. {
  70. sha_hal_wait_idle();
  71. sha_ll_fill_text_block(data_block, block_word_len);
  72. /* Start hashing */
  73. if (first_block) {
  74. sha_ll_start_block(sha_type);
  75. } else {
  76. sha_ll_continue_block(sha_type);
  77. }
  78. }
  79. #if SOC_SHA_SUPPORT_DMA
  80. #if SOC_SHA_GENERAL_DMA
  81. static inline void sha_hal_dma_init(lldesc_t *input)
  82. {
  83. /* Update driver when centralized DMA interface implemented, IDF-2192 */
  84. gdma_ll_tx_enable_descriptor_burst(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, false);
  85. gdma_ll_tx_enable_data_burst(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, false);
  86. gdma_ll_tx_enable_auto_write_back(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, false);
  87. gdma_ll_tx_connect_to_periph(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, GDMA_LL_PERIPH_ID_SHA);
  88. #if SOC_GDMA_SUPPORT_EXTMEM
  89. /* Atleast 40 bytes when accessing external RAM */
  90. gdma_ll_tx_extend_fifo_size_to(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, 40);
  91. gdma_ll_tx_set_block_size_psram(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, GDMA_OUT_EXT_MEM_BK_SIZE_16B);
  92. #endif //SOC_GDMA_SUPPORT_EXTMEM
  93. /* Set descriptors */
  94. gdma_ll_tx_set_desc_addr(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, (uint32_t)input);
  95. gdma_ll_rx_reset_channel(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL);
  96. gdma_ll_tx_reset_channel(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL);
  97. /* Start transfer */
  98. gdma_ll_tx_start(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL);
  99. }
  100. #endif //SOC_SHA_GENERAL_DMA
  101. #if SOC_SHA_CRYPTO_DMA
  102. static inline void sha_hal_dma_init(lldesc_t *input)
  103. {
  104. crypto_dma_ll_set_mode(CRYPTO_DMA_SHA);
  105. crypto_dma_ll_reset();
  106. crypto_dma_ll_outlink_set((uint32_t)input);
  107. crypto_dma_ll_outlink_start();
  108. }
  109. #endif
  110. /* Hashes a number of message blocks using DMA */
  111. void sha_hal_hash_dma(esp_sha_type sha_type, lldesc_t *input, size_t num_blocks, bool first_block)
  112. {
  113. sha_hal_wait_idle();
  114. sha_hal_dma_init(input);
  115. sha_ll_set_block_num(num_blocks);
  116. /* Start hashing */
  117. if (first_block) {
  118. sha_ll_start_dma(sha_type);
  119. } else {
  120. sha_ll_continue_dma(sha_type);
  121. }
  122. }
  123. #endif //SOC_SHA_SUPPORT_DMA
  124. void sha_hal_wait_idle()
  125. {
  126. while (sha_ll_busy()) {
  127. }
  128. }
  129. /* Reads the current message digest from the SHA engine */
  130. void sha_hal_read_digest(esp_sha_type sha_type, void *digest_state)
  131. {
  132. uint32_t *digest_state_words = (uint32_t *)digest_state;
  133. sha_ll_load(sha_type);
  134. uint32_t word_len = state_length(sha_type);
  135. sha_hal_wait_idle();
  136. sha_ll_read_digest(sha_type, digest_state, word_len);
  137. /* Fault injection check: verify SHA engine actually ran,
  138. state is not all zeroes.
  139. */
  140. for (int i = 0; i < word_len; i++) {
  141. if (digest_state_words[i] != 0) {
  142. return;
  143. }
  144. }
  145. abort(); // SHA peripheral returned all zero state, probably due to fault injection
  146. }
  147. #if SOC_SHA_SUPPORT_RESUME
  148. /* Writes the message digest to the SHA engine */
  149. void sha_hal_write_digest(esp_sha_type sha_type, void *digest_state)
  150. {
  151. sha_ll_write_digest(sha_type, digest_state, state_length(sha_type));
  152. }
  153. #endif //SOC_SHA_SUPPORT_RESUME
  154. #if SOC_SHA_SUPPORT_SHA512_T
  155. /* Calculates and sets the initial digiest for SHA512_t */
  156. void sha_hal_sha512_init_hash(uint32_t t_string, uint8_t t_len)
  157. {
  158. sha_ll_t_string_set(t_string);
  159. sha_ll_t_len_set(t_len);
  160. sha_ll_start_block(SHA2_512T);
  161. sha_hal_wait_idle();
  162. }
  163. #endif //SOC_SHA_SUPPORT_SHA512_T