1 /*
2  *  SHA-1 implementation with hardware ESP32 support added.
3  *  Uses mbedTLS software implementation for failover when concurrent
4  *  SHA operations are in use.
5  *
6  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  *  Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE LTD
8  *  SPDX-License-Identifier: Apache-2.0
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11  *  not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  *
22  */
23 #ifndef _SHA1_ALT_H_
24 #define _SHA1_ALT_H_
25 
26 #if defined(MBEDTLS_SHA1_ALT)
27 
28 #include "hal/sha_types.h"
29 #include "soc/soc_caps.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #if SOC_SHA_SUPPORT_PARALLEL_ENG
36 
37 typedef enum {
38     ESP_MBEDTLS_SHA1_UNUSED, /* first block hasn't been processed yet */
39     ESP_MBEDTLS_SHA1_HARDWARE, /* using hardware SHA engine */
40     ESP_MBEDTLS_SHA1_SOFTWARE, /* using software SHA */
41 } esp_mbedtls_sha1_mode;
42 
43 /**
44  * \brief          SHA-1 context structure
45  */
46 typedef struct {
47     uint32_t total[2];          /*!< number of bytes processed  */
48     uint32_t state[5];          /*!< intermediate digest state  */
49     unsigned char buffer[64];   /*!< data block being processed */
50     esp_mbedtls_sha1_mode mode;
51 } mbedtls_sha1_context;
52 
53 /**
54  * \brief          Set the SHA-1 mode for a mbedtls_sha1_context.
55  *
56  * \param ctx      The SHA-1 context structure.
57  * \param mode     The SHA-1 mode to be set. It can be one of the following:
58  *                  - ESP_MBEDTLS_SHA1_UNUSED: Indicates that the first block hasn't been processed yet.
59  *                  - ESP_MBEDTLS_SHA1_HARDWARE: Specifies the use of hardware SHA engine for SHA-1 calculations.
60  *                  - ESP_MBEDTLS_SHA1_SOFTWARE: Specifies the use of software-based SHA-1 calculations.
61  *
62  * \return         None.
63  */
esp_mbedtls_set_sha1_mode(mbedtls_sha1_context * ctx,esp_mbedtls_sha1_mode mode)64 static inline void esp_mbedtls_set_sha1_mode(mbedtls_sha1_context *ctx, esp_mbedtls_sha1_mode mode)
65 {
66     if (ctx) {
67         ctx->mode = mode;
68     }
69 }
70 
71 #elif SOC_SHA_SUPPORT_DMA || SOC_SHA_SUPPORT_RESUME
72 
73 typedef enum {
74     ESP_SHA1_STATE_INIT,
75     ESP_SHA1_STATE_IN_PROCESS
76 } esp_sha1_state;
77 
78 /**
79  * \brief          SHA-1 context structure
80  */
81 typedef struct {
82     uint32_t total[2];          /*!< number of bytes processed  */
83     uint32_t state[5];          /*!< intermediate digest state  */
84     unsigned char buffer[64];   /*!< data block being processed */
85     int first_block;            /*!< if first then true else false */
86     esp_sha_type mode;
87     esp_sha1_state sha_state;
88 } mbedtls_sha1_context;
89 
90 #endif
91 
92 #endif
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif
99