2017-06-28 02:46:34 -04:00
|
|
|
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
|
2017-06-27 03:25:30 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
#include "bootloader_sha.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/param.h>
|
2019-05-27 02:29:43 -04:00
|
|
|
|
2019-03-14 05:29:32 -04:00
|
|
|
#include "esp32/rom/sha.h"
|
2019-05-27 02:29:43 -04:00
|
|
|
#include "soc/dport_reg.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/hwcrypto_periph.h"
|
2017-06-27 03:25:30 -04:00
|
|
|
|
|
|
|
static uint32_t words_hashed;
|
|
|
|
|
|
|
|
// Words per SHA256 block
|
2019-04-16 05:01:31 -04:00
|
|
|
static const size_t BLOCK_WORDS = (64 / sizeof(uint32_t));
|
2017-06-28 02:46:34 -04:00
|
|
|
// Words in final SHA256 digest
|
2019-04-16 05:01:31 -04:00
|
|
|
static const size_t DIGEST_WORDS = (32 / sizeof(uint32_t));
|
2017-06-27 03:25:30 -04:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
bootloader_sha256_handle_t bootloader_sha256_start(void)
|
2017-06-27 03:25:30 -04:00
|
|
|
{
|
|
|
|
// Enable SHA hardware
|
|
|
|
ets_sha_enable();
|
|
|
|
words_hashed = 0;
|
|
|
|
return (bootloader_sha256_handle_t)&words_hashed; // Meaningless non-NULL value
|
|
|
|
}
|
|
|
|
|
|
|
|
void bootloader_sha256_data(bootloader_sha256_handle_t handle, const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
assert(handle != NULL);
|
|
|
|
assert(data_len % 4 == 0);
|
|
|
|
|
|
|
|
const uint32_t *w = (const uint32_t *)data;
|
|
|
|
size_t word_len = data_len / 4;
|
|
|
|
uint32_t *sha_text_reg = (uint32_t *)(SHA_TEXT_BASE);
|
|
|
|
|
|
|
|
while (word_len > 0) {
|
|
|
|
size_t block_count = words_hashed % BLOCK_WORDS;
|
|
|
|
size_t copy_words = (BLOCK_WORDS - block_count);
|
|
|
|
|
|
|
|
copy_words = MIN(word_len, copy_words);
|
|
|
|
|
|
|
|
// Wait for SHA engine idle
|
2019-04-16 05:01:31 -04:00
|
|
|
while (REG_READ(SHA_256_BUSY_REG) != 0) { }
|
2017-06-27 03:25:30 -04:00
|
|
|
|
|
|
|
// Copy to memory block
|
2020-11-16 23:48:35 -05:00
|
|
|
for (size_t i = 0; i < copy_words; i++) {
|
2017-06-27 03:25:30 -04:00
|
|
|
sha_text_reg[block_count + i] = __builtin_bswap32(w[i]);
|
|
|
|
}
|
|
|
|
asm volatile ("memw");
|
|
|
|
|
|
|
|
// Update counters
|
|
|
|
words_hashed += copy_words;
|
|
|
|
block_count += copy_words;
|
|
|
|
word_len -= copy_words;
|
|
|
|
w += copy_words;
|
|
|
|
|
|
|
|
// If we loaded a full block, run the SHA engine
|
|
|
|
if (block_count == BLOCK_WORDS) {
|
|
|
|
if (words_hashed == BLOCK_WORDS) {
|
|
|
|
REG_WRITE(SHA_256_START_REG, 1);
|
|
|
|
} else {
|
|
|
|
REG_WRITE(SHA_256_CONTINUE_REG, 1);
|
|
|
|
}
|
|
|
|
block_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest)
|
|
|
|
{
|
|
|
|
assert(handle != NULL);
|
|
|
|
|
2017-06-28 02:46:34 -04:00
|
|
|
if (digest == NULL) {
|
|
|
|
return; // We'd free resources here, but there are none to free
|
|
|
|
}
|
|
|
|
|
2017-06-27 03:25:30 -04:00
|
|
|
uint32_t data_words = words_hashed;
|
|
|
|
|
2017-06-28 02:46:34 -04:00
|
|
|
// Pad to a 55 byte long block loaded in the engine
|
|
|
|
// (leaving 1 byte 0x80 plus variable padding plus 8 bytes of length,
|
|
|
|
// to fill a 64 byte block.)
|
2017-06-27 03:25:30 -04:00
|
|
|
int block_bytes = (words_hashed % BLOCK_WORDS) * 4;
|
2017-06-28 02:46:34 -04:00
|
|
|
int pad_bytes = 55 - block_bytes;
|
2017-06-27 03:25:30 -04:00
|
|
|
if (pad_bytes < 0) {
|
|
|
|
pad_bytes += 64;
|
|
|
|
}
|
|
|
|
static const uint8_t padding[64] = { 0x80, 0, };
|
|
|
|
|
2017-06-28 02:46:34 -04:00
|
|
|
pad_bytes += 5; // 1 byte for 0x80 plus first 4 bytes of the 64-bit length
|
|
|
|
assert(pad_bytes % 4 == 0); // should be, as (block_bytes % 4 == 0)
|
|
|
|
|
2017-06-27 03:25:30 -04:00
|
|
|
bootloader_sha256_data(handle, padding, pad_bytes);
|
|
|
|
|
2019-04-16 05:01:31 -04:00
|
|
|
assert(words_hashed % BLOCK_WORDS == 60 / 4); // 32-bits left in block
|
2017-06-27 03:25:30 -04:00
|
|
|
|
|
|
|
// Calculate 32-bit length for final 32 bits of data
|
|
|
|
uint32_t bit_count = __builtin_bswap32( data_words * 32 );
|
|
|
|
bootloader_sha256_data(handle, &bit_count, sizeof(bit_count));
|
|
|
|
|
|
|
|
assert(words_hashed % BLOCK_WORDS == 0);
|
|
|
|
|
2019-04-16 05:01:31 -04:00
|
|
|
while (REG_READ(SHA_256_BUSY_REG) == 1) { }
|
2017-06-27 03:25:30 -04:00
|
|
|
REG_WRITE(SHA_256_LOAD_REG, 1);
|
2019-04-16 05:01:31 -04:00
|
|
|
while (REG_READ(SHA_256_BUSY_REG) == 1) { }
|
2017-06-27 03:25:30 -04:00
|
|
|
|
|
|
|
uint32_t *digest_words = (uint32_t *)digest;
|
|
|
|
uint32_t *sha_text_reg = (uint32_t *)(SHA_TEXT_BASE);
|
2020-11-16 23:48:35 -05:00
|
|
|
for (size_t i = 0; i < DIGEST_WORDS; i++) {
|
2017-06-27 03:25:30 -04:00
|
|
|
digest_words[i] = __builtin_bswap32(sha_text_reg[i]);
|
|
|
|
}
|
|
|
|
asm volatile ("memw");
|
|
|
|
}
|