sha.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _ESP_SHA_H_
  14. #define _ESP_SHA_H_
  15. #include "rom/sha.h"
  16. #include "esp_types.h"
  17. /** @brief Low-level support functions for the hardware SHA engine
  18. *
  19. * @note If you're looking for a SHA API to use, try mbedtls component
  20. * mbedtls/shaXX.h. That API supports hardware acceleration.
  21. *
  22. * The API in this header provides some building blocks for implementing a
  23. * full SHA API such as the one in mbedtls, and also a basic SHA function esp_sha().
  24. *
  25. * Some technical details about the hardware SHA engine:
  26. *
  27. * - SHA accelerator engine calculates one digest at a time, per SHA
  28. * algorithm type. It initialises and maintains the digest state
  29. * internally. It is possible to read out an in-progress SHA digest
  30. * state, but it is not possible to restore a SHA digest state
  31. * into the engine.
  32. *
  33. * - The memory block SHA_TEXT_BASE is shared between all SHA digest
  34. * engines, so all engines must be idle before this memory block is
  35. * modified.
  36. *
  37. */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Defined in rom/sha.h */
  42. typedef enum SHA_TYPE esp_sha_type;
  43. /** @brief Calculate SHA1 or SHA2 sum of some data, using hardware SHA engine
  44. *
  45. * @note For more versatile SHA calculations, where data doesn't need
  46. * to be passed all at once, try the mbedTLS mbedtls/shaX.h APIs. The
  47. * hardware-accelerated mbedTLS implementation is also faster when
  48. * hashing large amounts of data.
  49. *
  50. * @note It is not necessary to lock any SHA hardware before calling
  51. * this function, thread safety is managed internally.
  52. *
  53. * @note If a TLS connection is open then this function may block
  54. * indefinitely waiting for a SHA engine to become available. Use the
  55. * mbedTLS SHA API to avoid this problem.
  56. *
  57. * @param sha_type SHA algorithm to use.
  58. *
  59. * @param input Input data buffer.
  60. *
  61. * @param ilen Length of input data in bytes.
  62. *
  63. * @param output Buffer for output SHA digest. Output is 20 bytes for
  64. * sha_type SHA1, 32 bytes for sha_type SHA2_256, 48 bytes for
  65. * sha_type SHA2_384, 64 bytes for sha_type SHA2_512.
  66. */
  67. void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output);
  68. /* @brief Begin to execute a single SHA block operation
  69. *
  70. * @note This is a piece of a SHA algorithm, rather than an entire SHA
  71. * algorithm.
  72. *
  73. * @note Call esp_sha_try_lock_engine() before calling this
  74. * function. Do not call esp_sha_lock_memory_block() beforehand, this
  75. * is done inside the function.
  76. *
  77. * @param sha_type SHA algorithm to use.
  78. *
  79. * @param data_block Pointer to block of data. Block size is
  80. * determined by algorithm (SHA1/SHA2_256 = 64 bytes,
  81. * SHA2_384/SHA2_512 = 128 bytes)
  82. *
  83. * @param is_first_block If this parameter is true, the SHA state will
  84. * be initialised (with the initial state of the given SHA algorithm)
  85. * before the block is calculated. If false, the existing state of the
  86. * SHA engine will be used.
  87. *
  88. * @return As a performance optimisation, this function returns before
  89. * the SHA block operation is complete. Both this function and
  90. * esp_sha_read_state() will automatically wait for any previous
  91. * operation to complete before they begin. If using the SHA registers
  92. * directly in another way, call esp_sha_wait_idle() after calling this
  93. * function but before accessing the SHA registers.
  94. */
  95. void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block);
  96. /** @brief Read out the current state of the SHA digest loaded in the engine.
  97. *
  98. * @note This is a piece of a SHA algorithm, rather than an entire SHA algorithm.
  99. *
  100. * @note Call esp_sha_try_lock_engine() before calling this
  101. * function. Do not call esp_sha_lock_memory_block() beforehand, this
  102. * is done inside the function.
  103. *
  104. * If the SHA suffix padding block has been executed already, the
  105. * value that is read is the SHA digest (in big endian
  106. * format). Otherwise, the value that is read is an interim SHA state.
  107. *
  108. * @note If sha_type is SHA2_384, only 48 bytes of state will be read.
  109. * This is enough for the final SHA2_384 digest, but if you want the
  110. * interim SHA-384 state (to continue digesting) then pass SHA2_512 instead.
  111. *
  112. * @param sha_type SHA algorithm in use.
  113. *
  114. * @param state Pointer to a memory buffer to hold the SHA state. Size
  115. * is 20 bytes (SHA1), 32 bytes (SHA2_256), 48 bytes (SHA2_384) or 64 bytes (SHA2_512).
  116. *
  117. */
  118. void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state);
  119. /**
  120. * @brief Obtain exclusive access to a particular SHA engine
  121. *
  122. * @param sha_type Type of SHA engine to use.
  123. *
  124. * Blocks until engine is available. Note: Can block indefinitely
  125. * while a TLS connection is open, suggest using
  126. * esp_sha_try_lock_engine() and failing over to software SHA.
  127. */
  128. void esp_sha_lock_engine(esp_sha_type sha_type);
  129. /**
  130. * @brief Try and obtain exclusive access to a particular SHA engine
  131. *
  132. * @param sha_type Type of SHA engine to use.
  133. *
  134. * @return Returns true if the SHA engine is locked for exclusive
  135. * use. Call esp_sha_unlock_sha_engine() when done. Returns false if
  136. * the SHA engine is already in use, caller should use software SHA
  137. * algorithm for this digest.
  138. */
  139. bool esp_sha_try_lock_engine(esp_sha_type sha_type);
  140. /**
  141. * @brief Unlock an engine previously locked with esp_sha_lock_engine() or esp_sha_try_lock_engine()
  142. *
  143. * @param sha_type Type of engine to release.
  144. */
  145. void esp_sha_unlock_engine(esp_sha_type sha_type);
  146. /**
  147. * @brief Acquire exclusive access to the SHA shared memory block at SHA_TEXT_BASE
  148. *
  149. * This memory block is shared across all the SHA algorithm types.
  150. *
  151. * Caller should have already locked a SHA engine before calling this function.
  152. *
  153. * Note that it is possible to obtain exclusive access to the memory block even
  154. * while it is in use by the SHA engine. Caller should use esp_sha_wait_idle()
  155. * to ensure the SHA engine is not reading from the memory block in hardware.
  156. *
  157. * @note You do not need to lock the memory block before calling esp_sha_block() or esp_sha_read_digest_state(), these functions handle memory block locking internally.
  158. *
  159. * Call esp_sha_unlock_memory_block() when done.
  160. */
  161. void esp_sha_lock_memory_block(void);
  162. /**
  163. * @brief Release exclusive access to the SHA register memory block at SHA_TEXT_BASE
  164. *
  165. * Caller should have already locked a SHA engine before calling this function.
  166. *
  167. * Call following esp_sha_lock_memory_block().
  168. */
  169. void esp_sha_unlock_memory_block(void);
  170. /** @brief Wait for the SHA engine to finish any current operation
  171. *
  172. * @note This function does not ensure exclusive access to any SHA
  173. * engine. Caller should use esp_sha_try_lock_engine() and
  174. * esp_sha_lock_memory_block() as required.
  175. *
  176. * @note Functions declared in this header file wait for SHA engine
  177. * completion automatically, so you don't need to use this API for
  178. * these. However if accessing SHA registers directly, you will need
  179. * to call this before accessing SHA registers if using the
  180. * esp_sha_block() function.
  181. *
  182. * @note This function busy-waits, so wastes CPU resources.
  183. * Best to delay calling until you are about to need it.
  184. *
  185. */
  186. void esp_sha_wait_idle(void);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif