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     int dma_op_num = ( ilen / (SOC_SHA_DMA_MAX_BUFFER_SIZE + 1) ) + 1;
221 
222     if (buf_len > block_length(sha_type)) {
223         ESP_LOGE(TAG, "SHA DMA buf_len cannot exceed max size for a single block");
224         return -1;
225     }
226 
227     /* DMA cannot access memory in flash, hash block by block instead of using DMA */
228     if (!s_check_dma_capable(input) && (ilen != 0)) {
229         esp_sha_block_mode(sha_type, input, ilen, buf, buf_len, is_first_block);
230         return 0;
231     }
232 
233 #if (CONFIG_SPIRAM && SOC_PSRAM_DMA_CAPABLE)
234     if (esp_ptr_external_ram(input)) {
235         Cache_WriteBack_Addr((uint32_t)input, ilen);
236     }
237     if (esp_ptr_external_ram(buf)) {
238         Cache_WriteBack_Addr((uint32_t)buf, buf_len);
239     }
240 #endif
241 
242     /* Copy to internal buf if buf is in non DMA capable memory */
243     if (!s_check_dma_capable(buf) && (buf_len != 0)) {
244         dma_cap_buf = heap_caps_malloc(sizeof(unsigned char) * buf_len, MALLOC_CAP_8BIT|MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
245         if (dma_cap_buf == NULL) {
246             ESP_LOGE(TAG, "Failed to allocate buf memory");
247             ret = -1;
248             goto cleanup;
249         }
250         memcpy(dma_cap_buf, buf, buf_len);
251         buf = dma_cap_buf;
252     }
253 
254 
255     /* The max amount of blocks in a single hardware operation is 2^6 - 1 = 63
256        Thus we only do a single DMA input list + dma buf list,
257        which is max 3968/64 + 64/64 = 63 blocks */
258     for (int i = 0; i < dma_op_num; i++) {
259 
260         int dma_chunk_len = MIN(ilen, SOC_SHA_DMA_MAX_BUFFER_SIZE);
261 
262         ret = esp_sha_dma_process(sha_type, input, dma_chunk_len, buf, buf_len, is_first_block);
263 
264         if (ret != 0) {
265             goto cleanup;
266         }
267 
268         ilen -= dma_chunk_len;
269         input = (uint8_t *)input + dma_chunk_len;
270 
271         // Only append buf to the first operation
272         buf_len = 0;
273         is_first_block = false;
274     }
275 
276 cleanup:
277     free(dma_cap_buf);
278     return ret;
279 }
280 
281 
282 /* 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)283 static esp_err_t esp_sha_dma_process(esp_sha_type sha_type, const void *input, uint32_t ilen,
284                                      const void *buf, uint32_t buf_len, bool is_first_block)
285 {
286     int ret = 0;
287     lldesc_t *dma_descr_head;
288     size_t num_blks = (ilen + buf_len) / block_length(sha_type);
289 
290     memset(&s_dma_descr_input, 0, sizeof(lldesc_t));
291     memset(&s_dma_descr_buf, 0, sizeof(lldesc_t));
292 
293     /* DMA descriptor for Memory to DMA-SHA transfer */
294     if (ilen) {
295         s_dma_descr_input.length = ilen;
296         s_dma_descr_input.size = ilen;
297         s_dma_descr_input.owner = 1;
298         s_dma_descr_input.eof = 1;
299         s_dma_descr_input.buf = (uint8_t *)input;
300         dma_descr_head = &s_dma_descr_input;
301     }
302     /* Check after input to overide head if there is any buf*/
303     if (buf_len) {
304         s_dma_descr_buf.length = buf_len;
305         s_dma_descr_buf.size = buf_len;
306         s_dma_descr_buf.owner = 1;
307         s_dma_descr_buf.eof = 1;
308         s_dma_descr_buf.buf = (uint8_t *)buf;
309         dma_descr_head = &s_dma_descr_buf;
310     }
311 
312     /* Link DMA lists */
313     if (buf_len && ilen) {
314         s_dma_descr_buf.eof = 0;
315         s_dma_descr_buf.empty = (uint32_t)(&s_dma_descr_input);
316     }
317 
318     if (esp_sha_dma_start(dma_descr_head) != ESP_OK) {
319         ESP_LOGE(TAG, "esp_sha_dma_start failed, no DMA channel available");
320         return -1;
321     }
322 
323     sha_hal_hash_dma(sha_type, num_blks, is_first_block);
324 
325     sha_hal_wait_idle();
326 
327     return ret;
328 }
329 
s_check_dma_capable(const void * p)330 static bool s_check_dma_capable(const void *p)
331 {
332     bool is_capable = false;
333 #if CONFIG_SPIRAM
334     is_capable |= esp_ptr_dma_ext_capable(p);
335 #endif
336     is_capable |= esp_ptr_dma_capable(p);
337 
338     return is_capable;
339 }
340