1 /*
2  * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include "hal/sha_types.h"
11 #include "soc/soc_caps.h"
12 #include "esp_log.h"
13 
14 #include <mbedtls/sha1.h>
15 #include <mbedtls/sha256.h>
16 #include <mbedtls/sha512.h>
17 
18 #if SOC_SHA_SUPPORT_PARALLEL_ENG
19 #include "sha/sha_parallel_engine.h"
20 #elif SOC_SHA_SUPPORT_DMA
21 #include "sha/sha_dma.h"
22 #else
23 #include "sha/sha_block.h"
24 #endif
25 
26 static const char *TAG = "esp_sha";
27 
esp_sha(esp_sha_type sha_type,const unsigned char * input,size_t ilen,unsigned char * output)28 void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, unsigned char *output)
29 {
30     union {
31 #if SOC_SHA_SUPPORT_SHA1
32         mbedtls_sha1_context sha1;
33 #endif
34 #if SOC_SHA_SUPPORT_SHA256
35         mbedtls_sha256_context sha256;
36 #endif
37 #if SOC_SHA_SUPPORT_SHA384 || SOC_SHA_SUPPORT_SHA512
38         mbedtls_sha512_context sha512;
39 #endif
40     } ctx;
41 
42     int ret __attribute__((unused));
43     assert(input != NULL && output != NULL);
44 
45 #if SOC_SHA_SUPPORT_SHA1
46     if (sha_type == SHA1) {
47         mbedtls_sha1_init(&ctx.sha1);
48         mbedtls_sha1_starts(&ctx.sha1);
49         ret = mbedtls_sha1_update(&ctx.sha1, input, ilen);
50         assert(ret == 0);
51         ret = mbedtls_sha1_finish(&ctx.sha1, output);
52         assert(ret == 0);
53         mbedtls_sha1_free(&ctx.sha1);
54         return;
55     }
56 #endif //SOC_SHA_SUPPORT_SHA1
57 
58 #if SOC_SHA_SUPPORT_SHA256
59     if (sha_type == SHA2_256) {
60         mbedtls_sha256_init(&ctx.sha256);
61         mbedtls_sha256_starts(&ctx.sha256, 0);
62         ret = mbedtls_sha256_update(&ctx.sha256, input, ilen);
63         assert(ret == 0);
64         ret = mbedtls_sha256_finish(&ctx.sha256, output);
65         assert(ret == 0);
66         mbedtls_sha256_free(&ctx.sha256);
67         return;
68     }
69 #endif //SOC_SHA_SUPPORT_SHA256
70 
71 #if SOC_SHA_SUPPORT_SHA384
72     if (sha_type == SHA2_384) {
73         mbedtls_sha512_init(&ctx.sha512);
74         mbedtls_sha512_starts(&ctx.sha512, 1);
75         ret = mbedtls_sha512_update(&ctx.sha512, input, ilen);
76         assert(ret == 0);
77         ret = mbedtls_sha512_finish(&ctx.sha512, output);
78         assert(ret == 0);
79         mbedtls_sha512_free(&ctx.sha512);
80         return;
81     }
82 #endif //SOC_SHA_SUPPORT_SHA384
83 
84 #if SOC_SHA_SUPPORT_SHA512
85     if (sha_type == SHA2_512) {
86         mbedtls_sha512_init(&ctx.sha512);
87         mbedtls_sha512_starts(&ctx.sha512, 0);
88         ret = mbedtls_sha512_update(&ctx.sha512, input, ilen);
89         assert(ret == 0);
90         ret = mbedtls_sha512_finish(&ctx.sha512, output);
91         assert(ret == 0);
92         mbedtls_sha512_free(&ctx.sha512);
93         return;
94     }
95 #endif //SOC_SHA_SUPPORT_SHA512
96 
97     ESP_LOGE(TAG, "SHA type %d not supported", (int)sha_type);
98     abort();
99 }
100