1 /*
2 * ESP hardware accelerated SHA1/256/512 implementation
3 * based on mbedTLS FIPS-197 compliant version.
4 *
5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6 * Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE Ltd
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 */
22 /*
23 * The SHA-1 standard was published by NIST in 1993.
24 *
25 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
26 */
27
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/lock.h>
31
32 #include "esp_log.h"
33 #include "esp_memory_utils.h"
34 #include "esp_crypto_lock.h"
35 #include "esp_attr.h"
36 #include "soc/lldesc.h"
37 #include "soc/ext_mem_defs.h"
38 #include "soc/periph_defs.h"
39
40 #include "freertos/FreeRTOS.h"
41 #include "freertos/semphr.h"
42
43 #include "esp_private/periph_ctrl.h"
44 #include "sys/param.h"
45
46 #include "sha/sha_dma.h"
47 #include "hal/sha_hal.h"
48 #include "soc/soc_caps.h"
49 #include "esp_sha_dma_priv.h"
50
51 #if CONFIG_IDF_TARGET_ESP32S2
52 #include "esp32s2/rom/cache.h"
53 #elif CONFIG_IDF_TARGET_ESP32S3
54 #include "esp32s3/rom/cache.h"
55 #elif CONFIG_IDF_TARGET_ESP32C3
56 #include "esp32s3/rom/cache.h"
57 #elif CONFIG_IDF_TARGET_ESP32C2
58 #include "esp32c2/rom/cache.h"
59 #endif
60
61 #if SOC_SHA_GDMA
62 #define SHA_LOCK() esp_crypto_sha_aes_lock_acquire()
63 #define SHA_RELEASE() esp_crypto_sha_aes_lock_release()
64 #elif SOC_SHA_CRYPTO_DMA
65 #define SHA_LOCK() esp_crypto_dma_lock_acquire()
66 #define SHA_RELEASE() esp_crypto_dma_lock_release()
67 #endif
68
69 const static char *TAG = "esp-sha";
70 static bool s_check_dma_capable(const void *p);
71
72 /* These are static due to:
73 * * Must be in DMA capable memory, so stack is not a safe place to put them
74 * * To avoid having to malloc/free them for every DMA operation
75 */
76 static DRAM_ATTR lldesc_t s_dma_descr_input;
77 static DRAM_ATTR lldesc_t s_dma_descr_buf;
78
esp_sha_write_digest_state(esp_sha_type sha_type,void * digest_state)79 void esp_sha_write_digest_state(esp_sha_type sha_type, void *digest_state)
80 {
81 sha_hal_write_digest(sha_type, digest_state);
82 }
83
esp_sha_read_digest_state(esp_sha_type sha_type,void * digest_state)84 void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state)
85 {
86 sha_hal_read_digest(sha_type, digest_state);
87 }
88
89 /* Return block size (in bytes) for a given SHA type */
block_length(esp_sha_type type)90 inline static size_t block_length(esp_sha_type type)
91 {
92 switch (type) {
93 case SHA1:
94 case SHA2_224:
95 case SHA2_256:
96 return 64;
97 #if SOC_SHA_SUPPORT_SHA384
98 case SHA2_384:
99 #endif
100 #if SOC_SHA_SUPPORT_SHA512
101 case SHA2_512:
102 #endif
103 #if SOC_SHA_SUPPORT_SHA512_T
104 case SHA2_512224:
105 case SHA2_512256:
106 case SHA2_512T:
107 #endif
108 return 128;
109 default:
110 return 0;
111 }
112 }
113
114
115 /* Enable SHA peripheral and then lock it */
esp_sha_acquire_hardware()116 void esp_sha_acquire_hardware()
117 {
118 SHA_LOCK(); /* Released when releasing hw with esp_sha_release_hardware() */
119
120 /* Enable SHA and DMA hardware */
121 #if SOC_SHA_CRYPTO_DMA
122 periph_module_enable(PERIPH_SHA_DMA_MODULE);
123 #elif SOC_SHA_GDMA
124 periph_module_enable(PERIPH_SHA_MODULE);
125 #endif
126 }
127
128 /* Disable SHA peripheral block and then release it */
esp_sha_release_hardware()129 void esp_sha_release_hardware()
130 {
131 /* Disable SHA and DMA hardware */
132 #if SOC_SHA_CRYPTO_DMA
133 periph_module_disable(PERIPH_SHA_DMA_MODULE);
134 #elif SOC_SHA_GDMA
135 periph_module_disable(PERIPH_SHA_MODULE);
136 #endif
137
138 SHA_RELEASE();
139 }
140
141 #if SOC_SHA_SUPPORT_SHA512_T
142 /* The initial hash value for SHA512/t is generated according to the
143 algorithm described in the TRM, chapter SHA-Accelerator
144 */
esp_sha_512_t_init_hash(uint16_t t)145 int esp_sha_512_t_init_hash(uint16_t t)
146 {
147 uint32_t t_string = 0;
148 uint8_t t0, t1, t2, t_len;
149
150 if (t == 384) {
151 ESP_LOGE(TAG, "Invalid t for SHA512/t, t = %u,cannot be 384", t);
152 return -1;
153 }
154
155 if (t <= 9) {
156 t_string = (uint32_t)((1 << 23) | ((0x30 + t) << 24));
157 t_len = 0x48;
158 } else if (t <= 99) {
159 t0 = t % 10;
160 t1 = (t / 10) % 10;
161 t_string = (uint32_t)((1 << 15) | ((0x30 + t0) << 16) |
162 (((0x30 + t1) << 24)));
163 t_len = 0x50;
164 } else if (t <= 512) {
165 t0 = t % 10;
166 t1 = (t / 10) % 10;
167 t2 = t / 100;
168 t_string = (uint32_t)((1 << 7) | ((0x30 + t0) << 8) |
169 (((0x30 + t1) << 16) + ((0x30 + t2) << 24)));
170 t_len = 0x58;
171 } else {
172 ESP_LOGE(TAG, "Invalid t for SHA512/t, t = %u, must equal or less than 512", t);
173 return -1;
174 }
175
176 sha_hal_sha512_init_hash(t_string, t_len);
177
178 return 0;
179 }
180
181 #endif //SOC_SHA_SUPPORT_SHA512_T
182
183
184 /* Hash the input block by block, using non-DMA mode */
esp_sha_block_mode(esp_sha_type sha_type,const uint8_t * input,uint32_t ilen,const uint8_t * buf,uint32_t buf_len,bool is_first_block)185 static void esp_sha_block_mode(esp_sha_type sha_type, const uint8_t *input, uint32_t ilen,
186 const uint8_t *buf, uint32_t buf_len, bool is_first_block)
187 {
188 size_t blk_len = 0;
189 size_t blk_word_len = 0;
190 int num_block = 0;
191
192 blk_len = block_length(sha_type);
193 blk_word_len = blk_len / 4;
194 num_block = ilen / blk_len;
195
196 if (buf_len != 0) {
197 sha_hal_hash_block(sha_type, buf, blk_word_len, is_first_block);
198 is_first_block = false;
199 }
200
201 for (int i = 0; i < num_block; i++) {
202 sha_hal_hash_block(sha_type, input + blk_len * i, blk_word_len, is_first_block);
203 is_first_block = false;
204 }
205 }
206
207
208
209 static int esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen,
210 const void *buf, uint32_t buf_len, bool is_first_block);
211
212 /* Performs SHA on multiple blocks at a time using DMA
213 splits up into smaller operations for inputs that exceed a single DMA list
214 */
esp_sha_dma(esp_sha_type sha_type,const void * input,uint32_t ilen,const void * buf,uint32_t buf_len,bool is_first_block)215 int esp_sha_dma(esp_sha_type sha_type, const void *input, uint32_t ilen,
216 const void *buf, uint32_t buf_len, bool is_first_block)
217 {
218 int ret = 0;
219 unsigned char *dma_cap_buf = NULL;
220
221 if (buf_len > block_length(sha_type)) {
222 ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block");
223 return -1;
224 }
225
226 /* DMA cannot access memory in flash, hash block by block instead of using DMA */
227 if (!s_check_dma_capable(input) && (ilen != 0)) {
228 esp_sha_block_mode(sha_type, input, ilen, buf, buf_len, is_first_block);
229 return 0;
230 }
231
232 #if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE)
233 if (esp_ptr_external_ram(input)) {
234 Cache_WriteBack_Addr((uint32_t)input, ilen);
235 }
236 if (esp_ptr_external_ram(buf)) {
237 Cache_WriteBack_Addr((uint32_t)buf, buf_len);
238 }
239 #endif
240
241 /* Copy to internal buf if buf is in non DMA capable memory */
242 if (!s_check_dma_capable(buf) && (buf_len != 0)) {
243 dma_cap_buf = heap_caps_malloc(sizeof(unsigned char) * buf_len, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
244 if (dma_cap_buf == NULL) {
245 ESP_LOGE(TAG, "Failed to allocate buf memory");
246 ret = -1;
247 goto cleanup;
248 }
249 memcpy(dma_cap_buf, buf, buf_len);
250 buf = dma_cap_buf;
251 }
252
253 uint32_t dma_op_num;
254
255 if (ilen > 0) {
256 /* Number of DMA operations based on maximum chunk size in single operation */
257 dma_op_num = (ilen + SOC_SHA_DMA_MAX_BUFFER_SIZE - 1) / SOC_SHA_DMA_MAX_BUFFER_SIZE;
258 } else {
259 /* For zero input length, we must allow at-least 1 DMA operation to see
260 * if there is any pending data that is yet to be copied out */
261 dma_op_num = 1;
262 }
263
264 /* The max amount of blocks in a single hardware operation is 2^6 - 1 = 63
265 Thus we only do a single DMA input list + dma buf list,
266 which is max 3968/64 + 64/64 = 63 blocks */
267 for (int i = 0; i < dma_op_num; i++) {
268
269 int dma_chunk_len = MIN(ilen, SOC_SHA_DMA_MAX_BUFFER_SIZE);
270
271 ret = esp_sha_dma_process(sha_type, input, dma_chunk_len, buf, buf_len, is_first_block);
272
273 if (ret != 0) {
274 goto cleanup;
275 }
276
277 ilen -= dma_chunk_len;
278 input = (uint8_t *)input + dma_chunk_len;
279
280 // Only append buf to the first operation
281 buf_len = 0;
282 is_first_block = false;
283 }
284
285 cleanup:
286 free(dma_cap_buf);
287 return ret;
288 }
289
290
291 /* Performs SHA on multiple blocks at a time */
esp_sha_dma_process(esp_sha_type sha_type,const void * input,uint32_t ilen,const void * buf,uint32_t buf_len,bool is_first_block)292 static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen,
293 const void *buf, uint32_t buf_len, bool is_first_block)
294 {
295 int ret = 0;
296 lldesc_t *dma_descr_head;
297 size_t num_blks = (ilen + buf_len) / block_length(sha_type);
298
299 memset(&s_dma_descr_input, 0, sizeof(lldesc_t));
300 memset(&s_dma_descr_buf, 0, sizeof(lldesc_t));
301
302 /* DMA descriptor for Memory to DMA-SHA transfer */
303 if (ilen) {
304 s_dma_descr_input.length = ilen;
305 s_dma_descr_input.size = ilen;
306 s_dma_descr_input.owner = 1;
307 s_dma_descr_input.eof = 1;
308 s_dma_descr_input.buf = (uint8_t *)input;
309 dma_descr_head = &s_dma_descr_input;
310 }
311 /* Check after input to overide head if there is any buf*/
312 if (buf_len) {
313 s_dma_descr_buf.length = buf_len;
314 s_dma_descr_buf.size = buf_len;
315 s_dma_descr_buf.owner = 1;
316 s_dma_descr_buf.eof = 1;
317 s_dma_descr_buf.buf = (uint8_t *)buf;
318 dma_descr_head = &s_dma_descr_buf;
319 }
320
321 /* Link DMA lists */
322 if (buf_len && ilen) {
323 s_dma_descr_buf.eof = 0;
324 s_dma_descr_buf.empty = (uint32_t)(&s_dma_descr_input);
325 }
326
327 if (esp_sha_dma_start(dma_descr_head) != ESP_OK) {
328 ESP_LOGE(TAG, "esp_sha_dma_start failed, no DMA channel available");
329 return -1;
330 }
331
332 sha_hal_hash_dma(sha_type, num_blks, is_first_block);
333
334 sha_hal_wait_idle();
335
336 return ret;
337 }
338
s_check_dma_capable(const void * p)339 static bool s_check_dma_capable(const void *p)
340 {
341 bool is_capable = false;
342 #if CONFIG_SPIRAM
343 is_capable |= esp_ptr_dma_ext_capable(p);
344 #endif
345 is_capable |= esp_ptr_dma_capable(p);
346
347 return is_capable;
348 }
349