sha.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * ESP32 hardware accelerated SHA1/256/512 implementation
  3. * based on mbedTLS FIPS-197 compliant version.
  4. *
  5. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6. * Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. */
  22. /*
  23. * The SHA-1 standard was published by NIST in 1993.
  24. *
  25. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  26. */
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <sys/lock.h>
  30. #include <byteswap.h>
  31. #include <assert.h>
  32. #include "hwcrypto/sha.h"
  33. #include "rom/ets_sys.h"
  34. #include "soc/dport_reg.h"
  35. #include "soc/hwcrypto_reg.h"
  36. inline static uint32_t SHA_LOAD_REG(esp_sha_type sha_type) {
  37. return SHA_1_LOAD_REG + sha_type * 0x10;
  38. }
  39. inline static uint32_t SHA_BUSY_REG(esp_sha_type sha_type) {
  40. return SHA_1_BUSY_REG + sha_type * 0x10;
  41. }
  42. inline static uint32_t SHA_START_REG(esp_sha_type sha_type) {
  43. return SHA_1_START_REG + sha_type * 0x10;
  44. }
  45. inline static uint32_t SHA_CONTINUE_REG(esp_sha_type sha_type) {
  46. return SHA_1_CONTINUE_REG + sha_type * 0x10;
  47. }
  48. /* Single lock for SHA engine memory block
  49. */
  50. static _lock_t memory_block_lock;
  51. typedef struct {
  52. _lock_t lock;
  53. bool in_use;
  54. } sha_engine_state;
  55. /* Pointer to state of each concurrent SHA engine.
  56. Indexes:
  57. 0 = SHA1
  58. 1 = SHA2_256
  59. 2 = SHA2_384 or SHA2_512
  60. */
  61. static sha_engine_state engine_states[3];
  62. /* Index into the sha_engine_state array */
  63. inline static size_t sha_engine_index(esp_sha_type type) {
  64. switch(type) {
  65. case SHA1:
  66. return 0;
  67. case SHA2_256:
  68. return 1;
  69. default:
  70. return 2;
  71. }
  72. }
  73. /* Return digest length (in bytes) for a given SHA type */
  74. inline static size_t sha_length(esp_sha_type type) {
  75. switch(type) {
  76. case SHA1:
  77. return 20;
  78. case SHA2_256:
  79. return 32;
  80. case SHA2_384:
  81. return 48;
  82. case SHA2_512:
  83. return 64;
  84. default:
  85. return 0;
  86. }
  87. }
  88. /* Return block size (in bytes) for a given SHA type */
  89. inline static size_t block_length(esp_sha_type type) {
  90. switch(type) {
  91. case SHA1:
  92. case SHA2_256:
  93. return 64;
  94. case SHA2_384:
  95. case SHA2_512:
  96. return 128;
  97. default:
  98. return 0;
  99. }
  100. }
  101. void esp_sha_lock_memory_block(void)
  102. {
  103. _lock_acquire(&memory_block_lock);
  104. }
  105. void esp_sha_unlock_memory_block(void)
  106. {
  107. _lock_release(&memory_block_lock);
  108. }
  109. /* Lock to hold when changing SHA engine state,
  110. allows checking of sha_engines_all_idle()
  111. */
  112. static _lock_t state_change_lock;
  113. inline static bool sha_engines_all_idle() {
  114. return !engine_states[0].in_use
  115. && !engine_states[1].in_use
  116. && !engine_states[2].in_use;
  117. }
  118. static void esp_sha_lock_engine_inner(sha_engine_state *engine);
  119. bool esp_sha_try_lock_engine(esp_sha_type sha_type)
  120. {
  121. sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)];
  122. if(_lock_try_acquire(&engine->lock) != 0) {
  123. /* This SHA engine is already in use */
  124. return false;
  125. } else {
  126. esp_sha_lock_engine_inner(engine);
  127. return true;
  128. }
  129. }
  130. void esp_sha_lock_engine(esp_sha_type sha_type)
  131. {
  132. sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)];
  133. _lock_acquire(&engine->lock);
  134. esp_sha_lock_engine_inner(engine);
  135. }
  136. static void esp_sha_lock_engine_inner(sha_engine_state *engine)
  137. {
  138. _lock_acquire(&state_change_lock);
  139. if (sha_engines_all_idle()) {
  140. /* Enable SHA hardware */
  141. REG_SET_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_SHA);
  142. /* also clear reset on secure boot, otherwise SHA is held in reset */
  143. REG_CLR_BIT(DPORT_PERI_RST_EN_REG,
  144. DPORT_PERI_EN_SHA
  145. | DPORT_PERI_EN_SECUREBOOT);
  146. ets_sha_enable();
  147. }
  148. _lock_release(&state_change_lock);
  149. assert( !engine->in_use && "in_use flag should be cleared" );
  150. engine->in_use = true;
  151. }
  152. void esp_sha_unlock_engine(esp_sha_type sha_type)
  153. {
  154. sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)];
  155. _lock_acquire(&state_change_lock);
  156. assert( engine->in_use && "in_use flag should be set" );
  157. engine->in_use = false;
  158. if (sha_engines_all_idle()) {
  159. /* Disable SHA hardware */
  160. /* Don't assert reset on secure boot, otherwise AES is held in reset */
  161. REG_SET_BIT(DPORT_PERI_RST_EN_REG, DPORT_PERI_EN_SHA);
  162. REG_CLR_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_SHA);
  163. }
  164. _lock_release(&state_change_lock);
  165. _lock_release(&engine->lock);
  166. }
  167. void esp_sha_wait_idle(void)
  168. {
  169. while(REG_READ(SHA_1_BUSY_REG) == 1) {}
  170. while(REG_READ(SHA_256_BUSY_REG) == 1) {}
  171. while(REG_READ(SHA_384_BUSY_REG) == 1) {}
  172. while(REG_READ(SHA_512_BUSY_REG) == 1) {}
  173. }
  174. void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
  175. {
  176. sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)];
  177. assert(engine->in_use && "SHA engine should be locked" );
  178. esp_sha_lock_memory_block();
  179. esp_sha_wait_idle();
  180. REG_WRITE(SHA_LOAD_REG(sha_type), 1);
  181. while(REG_READ(SHA_BUSY_REG(sha_type)) == 1) { }
  182. uint32_t *digest_state_words = (uint32_t *)digest_state;
  183. uint32_t *reg_addr_buf = (uint32_t *)(SHA_TEXT_BASE);
  184. if(sha_type == SHA2_384 || sha_type == SHA2_512) {
  185. /* for these ciphers using 64-bit states, swap each pair of words */
  186. for(int i = 0; i < sha_length(sha_type)/4; i += 2) {
  187. digest_state_words[i+1] = reg_addr_buf[i];
  188. digest_state_words[i]= reg_addr_buf[i+1];
  189. }
  190. } else {
  191. memcpy(digest_state_words, reg_addr_buf, sha_length(sha_type));
  192. }
  193. asm volatile ("memw");
  194. esp_sha_unlock_memory_block();
  195. }
  196. void esp_sha_block(esp_sha_type sha_type, const void *data_block, bool is_first_block)
  197. {
  198. sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)];
  199. assert(engine->in_use && "SHA engine should be locked" );
  200. esp_sha_lock_memory_block();
  201. esp_sha_wait_idle();
  202. /* Fill the data block */
  203. uint32_t *reg_addr_buf = (uint32_t *)(SHA_TEXT_BASE);
  204. uint32_t *data_words = (uint32_t *)data_block;
  205. for (int i = 0; i < block_length(sha_type) / 4; i++) {
  206. reg_addr_buf[i] = __bswap_32(data_words[i]);
  207. }
  208. asm volatile ("memw");
  209. if(is_first_block) {
  210. REG_WRITE(SHA_START_REG(sha_type), 1);
  211. } else {
  212. REG_WRITE(SHA_CONTINUE_REG(sha_type), 1);
  213. }
  214. esp_sha_unlock_memory_block();
  215. /* Note: deliberately not waiting for this operation to complete,
  216. as a performance tweak - delay waiting until the next time we need the SHA
  217. unit, instead.
  218. */
  219. }
  220. void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output)
  221. {
  222. size_t block_len = block_length(sha_type);
  223. esp_sha_lock_engine(sha_type);
  224. SHA_CTX ctx;
  225. ets_sha_init(&ctx);
  226. while(ilen > 0) {
  227. size_t chunk_len = (ilen > block_len) ? block_len : ilen;
  228. esp_sha_lock_memory_block();
  229. esp_sha_wait_idle();
  230. ets_sha_update(&ctx, sha_type, input, chunk_len * 8);
  231. esp_sha_unlock_memory_block();
  232. input += chunk_len;
  233. ilen -= chunk_len;
  234. }
  235. esp_sha_lock_memory_block();
  236. esp_sha_wait_idle();
  237. ets_sha_finish(&ctx, sha_type, output);
  238. esp_sha_unlock_memory_block();
  239. esp_sha_unlock_engine(sha_type);
  240. }