sha_hal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #if SOC_SHA_SUPPORT_SHA384
  57. case SHA2_384:
  58. return SHA512_STATE_LEN_WORDS;
  59. #endif
  60. #if SOC_SHA_SUPPORT_SHA512
  61. case SHA2_512:
  62. return SHA512_STATE_LEN_WORDS;
  63. #endif
  64. #if SOC_SHA_SUPPORT_SHA512_T
  65. case SHA2_512224:
  66. case SHA2_512256:
  67. case SHA2_512T:
  68. return SHA512_STATE_LEN_WORDS;
  69. #endif
  70. default:
  71. return 0;
  72. }
  73. }
  74. #endif
  75. /* Hash a single block */
  76. void sha_hal_hash_block(esp_sha_type sha_type, const void *data_block, size_t block_word_len, bool first_block)
  77. {
  78. sha_hal_wait_idle();
  79. sha_ll_fill_text_block(data_block, block_word_len);
  80. /* Start hashing */
  81. if (first_block) {
  82. sha_ll_start_block(sha_type);
  83. } else {
  84. sha_ll_continue_block(sha_type);
  85. }
  86. }
  87. #if SOC_SHA_SUPPORT_DMA
  88. #if SOC_SHA_GENERAL_DMA
  89. static inline void sha_hal_dma_init(lldesc_t *input)
  90. {
  91. /* Update driver when centralized DMA interface implemented, IDF-2192 */
  92. gdma_ll_tx_enable_descriptor_burst(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, false);
  93. gdma_ll_tx_enable_data_burst(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, false);
  94. gdma_ll_tx_enable_auto_write_back(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, false);
  95. gdma_ll_tx_connect_to_periph(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, GDMA_LL_PERIPH_ID_SHA);
  96. #if SOC_GDMA_SUPPORT_EXTMEM
  97. /* Atleast 40 bytes when accessing external RAM */
  98. gdma_ll_tx_extend_fifo_size_to(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, 40);
  99. gdma_ll_tx_set_block_size_psram(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, GDMA_OUT_EXT_MEM_BK_SIZE_16B);
  100. #endif //SOC_GDMA_SUPPORT_EXTMEM
  101. /* Set descriptors */
  102. gdma_ll_tx_set_desc_addr(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL, (uint32_t)input);
  103. gdma_ll_rx_reset_channel(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL);
  104. gdma_ll_tx_reset_channel(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL);
  105. /* Start transfer */
  106. gdma_ll_tx_start(&GDMA, SOC_GDMA_SHA_DMA_CHANNEL);
  107. }
  108. #endif //SOC_SHA_GENERAL_DMA
  109. #if SOC_SHA_CRYPTO_DMA
  110. static inline void sha_hal_dma_init(lldesc_t *input)
  111. {
  112. crypto_dma_ll_set_mode(CRYPTO_DMA_SHA);
  113. crypto_dma_ll_reset();
  114. crypto_dma_ll_outlink_set((uint32_t)input);
  115. crypto_dma_ll_outlink_start();
  116. }
  117. #endif
  118. /* Hashes a number of message blocks using DMA */
  119. void sha_hal_hash_dma(esp_sha_type sha_type, lldesc_t *input, size_t num_blocks, bool first_block)
  120. {
  121. sha_hal_wait_idle();
  122. sha_hal_dma_init(input);
  123. sha_ll_set_block_num(num_blocks);
  124. /* Start hashing */
  125. if (first_block) {
  126. sha_ll_start_dma(sha_type);
  127. } else {
  128. sha_ll_continue_dma(sha_type);
  129. }
  130. }
  131. #endif //SOC_SHA_SUPPORT_DMA
  132. void sha_hal_wait_idle()
  133. {
  134. while (sha_ll_busy()) {
  135. }
  136. }
  137. /* Reads the current message digest from the SHA engine */
  138. void sha_hal_read_digest(esp_sha_type sha_type, void *digest_state)
  139. {
  140. uint32_t *digest_state_words = (uint32_t *)digest_state;
  141. sha_ll_load(sha_type);
  142. uint32_t word_len = state_length(sha_type);
  143. sha_hal_wait_idle();
  144. sha_ll_read_digest(sha_type, digest_state, word_len);
  145. /* Fault injection check: verify SHA engine actually ran,
  146. state is not all zeroes.
  147. */
  148. for (int i = 0; i < word_len; i++) {
  149. if (digest_state_words[i] != 0) {
  150. return;
  151. }
  152. }
  153. abort(); // SHA peripheral returned all zero state, probably due to fault injection
  154. }
  155. #if SOC_SHA_SUPPORT_RESUME
  156. /* Writes the message digest to the SHA engine */
  157. void sha_hal_write_digest(esp_sha_type sha_type, void *digest_state)
  158. {
  159. sha_ll_write_digest(sha_type, digest_state, state_length(sha_type));
  160. }
  161. #endif //SOC_SHA_SUPPORT_RESUME
  162. #if SOC_SHA_SUPPORT_SHA512_T
  163. /* Calculates and sets the initial digiest for SHA512_t */
  164. void sha_hal_sha512_init_hash(uint32_t t_string, uint8_t t_len)
  165. {
  166. sha_ll_t_string_set(t_string);
  167. sha_ll_t_len_set(t_len);
  168. sha_ll_start_block(SHA2_512T);
  169. sha_hal_wait_idle();
  170. }
  171. #endif //SOC_SHA_SUPPORT_SHA512_T