aes_hal.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. #pragma once
  20. #include <stddef.h>
  21. #include <stdbool.h>
  22. #include "soc/soc_caps.h"
  23. #include "hal/aes_types.h"
  24. #include "hal/aes_ll.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * @brief Sets the key used for AES encryption/decryption
  30. *
  31. * @param key pointer to the key
  32. * @param key_bytes number of bytes in key
  33. * @param mode key mode, 0 : decrypt, 1: encrypt
  34. *
  35. * @return uint8_t number of key bytes written to hardware, used for fault injection check
  36. */
  37. uint8_t aes_hal_setkey(const uint8_t *key, size_t key_bytes, int mode);
  38. /**
  39. * @brief encrypts/decrypts a single block
  40. *
  41. * @param input_block input block, size of AES_BLOCK_BYTES
  42. * @param output_block output block, size of AES_BLOCK_BYTES
  43. */
  44. void aes_hal_transform_block(const void *input_block, void *output_block);
  45. #if SOC_AES_SUPPORT_DMA
  46. /**
  47. * @brief Inits the AES mode of operation
  48. *
  49. * @param mode mode of operation, e.g. CTR or CBC
  50. */
  51. void aes_hal_mode_init(esp_aes_mode_t mode);
  52. /**
  53. * @brief Sets the initialization vector for the transform
  54. *
  55. * @note The same IV must never be reused with the same key
  56. *
  57. * @param iv the initialization vector, length = IV_BYTES (16 bytes)
  58. */
  59. void aes_hal_set_iv(const uint8_t *iv);
  60. /**
  61. * @brief Reads the initialization vector
  62. *
  63. * @param iv initialization vector read from HW, length = IV_BYTES (16 bytes)
  64. */
  65. void aes_hal_read_iv(uint8_t *iv);
  66. /**
  67. * @brief Busy waits until the AES operation is done
  68. *
  69. * @param output pointer to inlink descriptor
  70. */
  71. void aes_hal_wait_done(void);
  72. /**
  73. * @brief Starts an already configured AES DMA transform
  74. *
  75. * @param num_blocks Number of blocks to transform
  76. */
  77. void aes_hal_transform_dma_start(size_t num_blocks);
  78. /**
  79. * @brief Finish up a AES DMA conversion, release DMA
  80. *
  81. */
  82. void aes_hal_transform_dma_finish(void);
  83. /**
  84. * @brief Enable or disable transform completed interrupt
  85. *
  86. * @param enable true to enable, false to disable.
  87. */
  88. #define aes_hal_interrupt_enable(enable) aes_ll_interrupt_enable(enable)
  89. /**
  90. * @brief Clears the interrupt
  91. *
  92. */
  93. #define aes_hal_interrupt_clear() aes_ll_interrupt_clear()
  94. #if SOC_AES_SUPPORT_GCM
  95. /**
  96. * @brief Calculates the Hash sub-key H0 needed to start AES-GCM
  97. *
  98. * @param gcm_hash the Hash sub-key H0 output
  99. */
  100. void aes_hal_gcm_calc_hash(uint8_t *gcm_hash);
  101. /**
  102. * @brief Initializes the AES hardware for AES-GCM
  103. *
  104. * @param aad_num_blocks the number of Additional Authenticated Data (AAD) blocks
  105. * @param num_valid_bit the number of effective bits of incomplete blocks in plaintext/cipertext
  106. */
  107. void aes_hal_gcm_init(size_t aad_num_blocks, size_t num_valid_bit);
  108. /**
  109. * @brief Starts a AES-GCM transform
  110. *
  111. * @param num_blocks Number of blocks to transform
  112. */
  113. void aes_hal_transform_dma_gcm_start(size_t num_blocks);
  114. /**
  115. * @brief Sets the J0 value, for more information see the GCM subchapter in the TRM
  116. *
  117. * @note Only affects AES-GCM
  118. *
  119. * @param j0 J0 value
  120. */
  121. #define aes_hal_gcm_set_j0(j0) aes_ll_gcm_set_j0(j0)
  122. /**
  123. * @brief Read the tag after a AES-GCM transform
  124. *
  125. * @param tag Pointer to where to store the result
  126. * @param tag_length number of bytes to read into tag
  127. */
  128. void aes_hal_gcm_read_tag(uint8_t *tag, size_t tag_len);
  129. #endif //SOC_AES_SUPPORT_GCM
  130. #endif //SOC_AES_SUPPORT_DMA
  131. #ifdef __cplusplus
  132. }
  133. #endif