bootloader_sha.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "esp32/rom/sha.h"
  19. #include "soc/dport_reg.h"
  20. #include "soc/hwcrypto_periph.h"
  21. static uint32_t words_hashed;
  22. // Words per SHA256 block
  23. static const size_t BLOCK_WORDS = (64 / sizeof(uint32_t));
  24. // Words in final SHA256 digest
  25. static const size_t DIGEST_WORDS = (32 / sizeof(uint32_t));
  26. bootloader_sha256_handle_t bootloader_sha256_start(void)
  27. {
  28. // Enable SHA hardware
  29. ets_sha_enable();
  30. words_hashed = 0;
  31. return (bootloader_sha256_handle_t)&words_hashed; // Meaningless non-NULL value
  32. }
  33. void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
  34. {
  35. assert(handle != NULL);
  36. assert(data_len % 4 == 0);
  37. const uint32_t *w = (const uint32_t *)data;
  38. size_t word_len = data_len / 4;
  39. uint32_t *sha_text_reg = (uint32_t *)(SHA_TEXT_BASE);
  40. while (word_len > 0) {
  41. size_t block_count = words_hashed % BLOCK_WORDS;
  42. size_t copy_words = (BLOCK_WORDS - block_count);
  43. copy_words = MIN(word_len, copy_words);
  44. // Wait for SHA engine idle
  45. while (REG_READ(SHA_256_BUSY_REG) != 0) { }
  46. // Copy to memory block
  47. for (size_t i = 0; i < copy_words; i++) {
  48. sha_text_reg[block_count + i] = __builtin_bswap32(w[i]);
  49. }
  50. asm volatile ("memw");
  51. // Update counters
  52. words_hashed += copy_words;
  53. block_count += copy_words;
  54. word_len -= copy_words;
  55. w += copy_words;
  56. // If we loaded a full block, run the SHA engine
  57. if (block_count == BLOCK_WORDS) {
  58. if (words_hashed == BLOCK_WORDS) {
  59. REG_WRITE(SHA_256_START_REG, 1);
  60. } else {
  61. REG_WRITE(SHA_256_CONTINUE_REG, 1);
  62. }
  63. block_count = 0;
  64. }
  65. }
  66. }
  67. void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
  68. {
  69. assert(handle != NULL);
  70. if (digest == NULL) {
  71. return; // We'd free resources here, but there are none to free
  72. }
  73. uint32_t data_words = words_hashed;
  74. // Pad to a 55 byte long block loaded in the engine
  75. // (leaving 1 byte 0x80 plus variable padding plus 8 bytes of length,
  76. // to fill a 64 byte block.)
  77. int block_bytes = (words_hashed % BLOCK_WORDS) * 4;
  78. int pad_bytes = 55 - block_bytes;
  79. if (pad_bytes < 0) {
  80. pad_bytes += 64;
  81. }
  82. static const uint8_t padding[64] = { 0x80, 0, };
  83. pad_bytes += 5; // 1 byte for 0x80 plus first 4 bytes of the 64-bit length
  84. assert(pad_bytes % 4 == 0); // should be, as (block_bytes % 4 == 0)
  85. bootloader_sha256_data(handle, padding, pad_bytes);
  86. assert(words_hashed % BLOCK_WORDS == 60 / 4); // 32-bits left in block
  87. // Calculate 32-bit length for final 32 bits of data
  88. uint32_t bit_count = __builtin_bswap32( data_words * 32 );
  89. bootloader_sha256_data(handle, &bit_count, sizeof(bit_count));
  90. assert(words_hashed % BLOCK_WORDS == 0);
  91. while (REG_READ(SHA_256_BUSY_REG) == 1) { }
  92. REG_WRITE(SHA_256_LOAD_REG, 1);
  93. while (REG_READ(SHA_256_BUSY_REG) == 1) { }
  94. uint32_t *digest_words = (uint32_t *)digest;
  95. uint32_t *sha_text_reg = (uint32_t *)(SHA_TEXT_BASE);
  96. for (size_t i = 0; i < DIGEST_WORDS; i++) {
  97. digest_words[i] = __builtin_bswap32(sha_text_reg[i]);
  98. }
  99. asm volatile ("memw");
  100. }