sha_hal.c 5.5 KB

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