aes_hal.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // The HAL layer for AES
  7. #include "hal/aes_hal.h"
  8. #include "hal/aes_ll.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "soc/soc_caps.h"
  12. uint8_t aes_hal_setkey(const uint8_t *key, size_t key_bytes, int mode)
  13. {
  14. aes_ll_set_mode(mode, key_bytes);
  15. uint8_t key_bytes_in_hardware = aes_ll_write_key(key, key_bytes / 4);
  16. /* Used for fault injection check: all words of key data should have been written to hardware */
  17. return key_bytes_in_hardware;
  18. }
  19. /**
  20. * @brief Busy wait until the AES accelerator is idle
  21. *
  22. */
  23. static inline void aes_hal_wait_idle(void)
  24. {
  25. while (aes_ll_get_state() != ESP_AES_STATE_IDLE) {
  26. }
  27. }
  28. void aes_hal_transform_block(const void *input_block, void *output_block)
  29. {
  30. aes_ll_write_block(input_block);
  31. aes_ll_start_transform();
  32. aes_hal_wait_idle();
  33. aes_ll_read_block(output_block);
  34. }
  35. #if SOC_AES_SUPPORT_DMA
  36. void aes_hal_transform_dma_start(size_t num_blocks)
  37. {
  38. aes_ll_dma_enable(true);
  39. /* Write the number of blocks */
  40. aes_ll_set_num_blocks(num_blocks);
  41. /* Start encrypting/decrypting */
  42. aes_ll_start_transform();
  43. }
  44. void aes_hal_transform_dma_finish(void)
  45. {
  46. aes_ll_dma_exit();
  47. aes_ll_dma_enable(false);
  48. }
  49. void aes_hal_mode_init(esp_aes_mode_t mode)
  50. {
  51. /* Set the algorith mode CBC, CFB ... */
  52. aes_ll_set_block_mode(mode);
  53. /* Presently hard-coding the INC function to 32 bit */
  54. if (mode == ESP_AES_BLOCK_MODE_CTR) {
  55. aes_ll_set_inc();
  56. }
  57. }
  58. void aes_hal_set_iv(const uint8_t *iv)
  59. {
  60. aes_ll_set_iv(iv);
  61. }
  62. void aes_hal_read_iv(uint8_t *iv)
  63. {
  64. aes_ll_read_iv(iv);
  65. }
  66. void aes_hal_wait_done()
  67. {
  68. while (aes_ll_get_state() != ESP_AES_STATE_DONE) {}
  69. }
  70. #endif //SOC_AES_SUPPORT_DMA
  71. #if SOC_AES_SUPPORT_GCM
  72. void aes_hal_gcm_calc_hash(uint8_t *gcm_hash)
  73. {
  74. aes_ll_dma_enable(true);
  75. aes_ll_start_transform();
  76. aes_hal_wait_idle();
  77. aes_ll_gcm_read_hash(gcm_hash);
  78. }
  79. void aes_hal_transform_dma_gcm_start(size_t num_blocks)
  80. {
  81. /* Write the number of blocks */
  82. aes_ll_set_num_blocks(num_blocks);
  83. /* Start encrypting/decrypting */
  84. aes_ll_cont_transform();
  85. }
  86. void aes_hal_gcm_init(size_t aad_num_blocks, size_t num_valid_bit)
  87. {
  88. aes_ll_gcm_set_aad_num_blocks(aad_num_blocks);
  89. aes_ll_gcm_set_num_valid_bit(num_valid_bit);
  90. }
  91. void aes_hal_gcm_read_tag(uint8_t *tag, size_t tag_len)
  92. {
  93. uint8_t tag_res[TAG_BYTES];
  94. aes_ll_gcm_read_tag(tag_res);
  95. memcpy(tag, tag_res, tag_len);
  96. }
  97. #endif //SOC_AES_SUPPORT_GCM