1 /*
2  *  SHA-256 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 _SHA256_ALT_H_
24 #define _SHA256_ALT_H_
25 
26 #if defined(MBEDTLS_SHA256_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 typedef enum {
37     ESP_MBEDTLS_SHA256_UNUSED, /* first block hasn't been processed yet */
38     ESP_MBEDTLS_SHA256_HARDWARE, /* using hardware SHA engine */
39     ESP_MBEDTLS_SHA256_SOFTWARE, /* using software SHA */
40 } esp_mbedtls_sha256_mode;
41 
42 /**
43  * \brief          SHA-256 context structure
44  */
45 typedef struct {
46     uint32_t total[2];          /*!< number of bytes processed  */
47     uint32_t state[8];          /*!< intermediate digest state  */
48     unsigned char buffer[64];   /*!< data block being processed */
49     int is224;                  /*!< 0 => SHA-256, else SHA-224 */
50     esp_mbedtls_sha256_mode mode;
51 } mbedtls_sha256_context;
52 
53 #elif SOC_SHA_SUPPORT_DMA
54 typedef enum {
55     ESP_SHA256_STATE_INIT,
56     ESP_SHA256_STATE_IN_PROCESS
57 } esp_sha256_state;
58 
59 /**
60  * \brief          SHA-256 context structure
61  */
62 typedef struct {
63     uint32_t total[2];          /*!< number of bytes processed  */
64     uint32_t state[8];          /*!< intermediate digest state  */
65     unsigned char buffer[64];   /*!< data block being processed */
66     int first_block;           /*!< if first then true, else false */
67     esp_sha_type mode;
68     esp_sha256_state sha_state;
69 } mbedtls_sha256_context;
70 
71 #endif
72 
73 #endif
74 
75 #ifdef __cplusplus
76 }
77 #endif
78 
79 #endif
80