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