1
6
7
8
9
15
16
17
18
19
20
21
22
40
41
42
43
44
46
47
50
52
53
54
56
57
58
62
63
65
66
67
68
69
70
71
84
85
86
102
103
104
109
110
111
112
113
114
115
116
117
118
119
120
121
124
129
130
131
132
133
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* ... */
#include "hal/sha_hal.h"
#include "hal/sha_types.h"
#include "hal/sha_ll.h"
#include "soc/soc_caps.h"
#include <stdlib.h>
#include <stdio.h>6 includes
#define SHA1_STATE_LEN_WORDS (160 / 32)
#define SHA256_STATE_LEN_WORDS (256 / 32)
#define SHA512_STATE_LEN_WORDS (512 / 32)
#if CONFIG_IDF_TARGET_ESP32
inline static size_t state_length(esp_sha_type type)
{
switch (type) {
case SHA1:
return SHA1_STATE_LEN_WORDS;...
case SHA2_256:
return SHA256_STATE_LEN_WORDS;...
case SHA2_384:
case SHA2_512:
return SHA512_STATE_LEN_WORDS;...
default:
return 0;...
}{...}
}{ ... }
/* ... */#else
inline static size_t state_length(esp_sha_type type)
{
switch (type) {
case SHA1:
return SHA1_STATE_LEN_WORDS;...
case SHA2_224:
case SHA2_256:
return SHA256_STATE_LEN_WORDS;
#if SOC_SHA_SUPPORT_SHA384...
case SHA2_384:
return SHA512_STATE_LEN_WORDS;/* ... */
#endif
#if SOC_SHA_SUPPORT_SHA512
case SHA2_512:
return SHA512_STATE_LEN_WORDS;/* ... */
#endif
#if SOC_SHA_SUPPORT_SHA512_T
case SHA2_512224:
case SHA2_512256:
case SHA2_512T:
return SHA512_STATE_LEN_WORDS;/* ... */
#endif
default:
return 0;...
}{...}
}{...}
/* ... */#endif
void sha_hal_hash_block(esp_sha_type sha_type, const void *data_block, size_t block_word_len, bool first_block)
{
sha_hal_wait_idle();
sha_ll_fill_text_block(data_block, block_word_len);
if (first_block) {
sha_ll_start_block(sha_type);
}{...} else {
sha_ll_continue_block(sha_type);
}{...}
}{ ... }
#if SOC_SHA_SUPPORT_DMA
void sha_hal_hash_dma(esp_sha_type sha_type, size_t num_blocks, bool first_block)
{
sha_hal_wait_idle();
sha_ll_set_block_num(num_blocks);
if (first_block) {
sha_ll_start_dma(sha_type);
}{...} else {
sha_ll_continue_dma(sha_type);
}{...}
}{...}
/* ... */
#endif
void sha_hal_wait_idle()
{
while (sha_ll_busy()) {
}{...}
}{ ... }
void sha_hal_read_digest(esp_sha_type sha_type, void *digest_state)
{
uint32_t *digest_state_words = (uint32_t *)digest_state;
sha_ll_load(sha_type);
uint32_t word_len = state_length(sha_type);
sha_hal_wait_idle();
sha_ll_read_digest(sha_type, digest_state, word_len);
/* ... */
for (size_t i = 0; i < word_len; i++) {
if (digest_state_words[i] != 0) {
return;
}{...}
}{...}
abort();
}{ ... }
#if SOC_SHA_SUPPORT_RESUME
void sha_hal_write_digest(esp_sha_type sha_type, void *digest_state)
{
sha_ll_write_digest(sha_type, digest_state, state_length(sha_type));
}{...}
/* ... */#endif
#if SOC_SHA_SUPPORT_SHA512_T
void sha_hal_sha512_init_hash(uint32_t t_string, uint8_t t_len)
{
sha_ll_t_string_set(t_string);
sha_ll_t_len_set(t_len);
sha_ll_start_block(SHA2_512T);
sha_hal_wait_idle();
}{...}
/* ... */#endif