bootloader_sha.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2017 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. #include "bootloader_sha.h"
  14. #include <stdbool.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <sys/param.h>
  18. #ifndef BOOTLOADER_BUILD
  19. // App version is a wrapper around mbedTLS SHA API
  20. #include <mbedtls/sha256.h>
  21. bootloader_sha256_handle_t bootloader_sha256_start()
  22. {
  23. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)malloc(sizeof(mbedtls_sha256_context));
  24. if (!ctx) {
  25. return NULL;
  26. }
  27. mbedtls_sha256_init(ctx);
  28. int ret = mbedtls_sha256_starts_ret(ctx, false);
  29. if (ret != 0) {
  30. return NULL;
  31. }
  32. return ctx;
  33. }
  34. void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
  35. {
  36. assert(handle != NULL);
  37. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
  38. int ret = mbedtls_sha256_update_ret(ctx, data, data_len);
  39. assert(ret == 0);
  40. }
  41. void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
  42. {
  43. assert(handle != NULL);
  44. mbedtls_sha256_context *ctx = (mbedtls_sha256_context *)handle;
  45. if (digest != NULL) {
  46. int ret = mbedtls_sha256_finish_ret(ctx, digest);
  47. assert(ret == 0);
  48. }
  49. mbedtls_sha256_free(ctx);
  50. free(handle);
  51. }
  52. #else // Bootloader version
  53. #include "rom/sha.h"
  54. #include "soc/dport_reg.h"
  55. #include "soc/hwcrypto_reg.h"
  56. #include "rom/ets_sys.h" // TO REMOVE
  57. static uint32_t words_hashed;
  58. // Words per SHA256 block
  59. static const size_t BLOCK_WORDS = (64/sizeof(uint32_t));
  60. // Words in final SHA256 digest
  61. static const size_t DIGEST_WORDS = (32/sizeof(uint32_t));
  62. bootloader_sha256_handle_t bootloader_sha256_start()
  63. {
  64. // Enable SHA hardware
  65. ets_sha_enable();
  66. words_hashed = 0;
  67. return (bootloader_sha256_handle_t)&words_hashed; // Meaningless non-NULL value
  68. }
  69. void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
  70. {
  71. assert(handle != NULL);
  72. assert(data_len % 4 == 0);
  73. const uint32_t *w = (const uint32_t *)data;
  74. size_t word_len = data_len / 4;
  75. uint32_t *sha_text_reg = (uint32_t *)(SHA_TEXT_BASE);
  76. //ets_printf("word_len %d so far %d\n", word_len, words_hashed);
  77. while (word_len > 0) {
  78. size_t block_count = words_hashed % BLOCK_WORDS;
  79. size_t copy_words = (BLOCK_WORDS - block_count);
  80. copy_words = MIN(word_len, copy_words);
  81. // Wait for SHA engine idle
  82. while(REG_READ(SHA_256_BUSY_REG) != 0) { }
  83. // Copy to memory block
  84. //ets_printf("block_count %d copy_words %d\n", block_count, copy_words);
  85. for (int i = 0; i < copy_words; i++) {
  86. sha_text_reg[block_count + i] = __builtin_bswap32(w[i]);
  87. }
  88. asm volatile ("memw");
  89. // Update counters
  90. words_hashed += copy_words;
  91. block_count += copy_words;
  92. word_len -= copy_words;
  93. w += copy_words;
  94. // If we loaded a full block, run the SHA engine
  95. if (block_count == BLOCK_WORDS) {
  96. //ets_printf("running engine @ count %d\n", words_hashed);
  97. if (words_hashed == BLOCK_WORDS) {
  98. REG_WRITE(SHA_256_START_REG, 1);
  99. } else {
  100. REG_WRITE(SHA_256_CONTINUE_REG, 1);
  101. }
  102. block_count = 0;
  103. }
  104. }
  105. }
  106. void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
  107. {
  108. assert(handle != NULL);
  109. if (digest == NULL) {
  110. return; // We'd free resources here, but there are none to free
  111. }
  112. uint32_t data_words = words_hashed;
  113. // Pad to a 55 byte long block loaded in the engine
  114. // (leaving 1 byte 0x80 plus variable padding plus 8 bytes of length,
  115. // to fill a 64 byte block.)
  116. int block_bytes = (words_hashed % BLOCK_WORDS) * 4;
  117. int pad_bytes = 55 - block_bytes;
  118. if (pad_bytes < 0) {
  119. pad_bytes += 64;
  120. }
  121. static const uint8_t padding[64] = { 0x80, 0, };
  122. pad_bytes += 5; // 1 byte for 0x80 plus first 4 bytes of the 64-bit length
  123. assert(pad_bytes % 4 == 0); // should be, as (block_bytes % 4 == 0)
  124. bootloader_sha256_data(handle, padding, pad_bytes);
  125. assert(words_hashed % BLOCK_WORDS == 60/4); // 32-bits left in block
  126. // Calculate 32-bit length for final 32 bits of data
  127. uint32_t bit_count = __builtin_bswap32( data_words * 32 );
  128. bootloader_sha256_data(handle, &bit_count, sizeof(bit_count));
  129. assert(words_hashed % BLOCK_WORDS == 0);
  130. while(REG_READ(SHA_256_BUSY_REG) == 1) { }
  131. REG_WRITE(SHA_256_LOAD_REG, 1);
  132. while(REG_READ(SHA_256_BUSY_REG) == 1) { }
  133. uint32_t *digest_words = (uint32_t *)digest;
  134. uint32_t *sha_text_reg = (uint32_t *)(SHA_TEXT_BASE);
  135. for (int i = 0; i < DIGEST_WORDS; i++) {
  136. digest_words[i] = __builtin_bswap32(sha_text_reg[i]);
  137. }
  138. asm volatile ("memw");
  139. }
  140. #endif
  141. esp_err_t bootloader_sha256_hex_to_str(char *out_str, const uint8_t *in_array_hex, size_t len)
  142. {
  143. if (out_str == NULL || in_array_hex == NULL || len == 0) {
  144. return ESP_ERR_INVALID_ARG;
  145. }
  146. for (int i = 0; i < len; i++) {
  147. for (int shift = 0; shift < 2; shift++) {
  148. uint8_t nibble = (in_array_hex[i] >> (shift ? 0 : 4)) & 0x0F;
  149. if (nibble < 10) {
  150. out_str[i*2+shift] = '0' + nibble;
  151. } else {
  152. out_str[i*2+shift] = 'a' + nibble - 10;
  153. }
  154. }
  155. }
  156. return ESP_OK;
  157. }