11/*
2- * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2017-2026 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ #include "sdkconfig.h"
78#include <mbedtls/version.h>
89
910/* Keep forward-compatibility with Mbed TLS 3.x */
1718#endif /* !(MBEDTLS_VERSION_NUMBER < 0x03000000) */
1819
1920#include "crypto_hash_sha256.h"
20- #include "mbedtls/sha256.h"
2121#include <string.h>
2222
23+ #if defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
24+ #include "psa/crypto.h"
25+ #else
26+ #include "mbedtls/sha256.h"
27+ #endif
28+
29+ #if !defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
2330#ifdef MBEDTLS_SHA256_ALT
2431/* Wrapper only works if the libsodium context structure can be mapped
2532 directly to the mbedTLS context structure.
@@ -61,10 +68,28 @@ static void sha256_libsodium_to_mbedtls(mbedtls_sha256_context *mb_ctx, crypto_h
6168 memcpy (mb_ctx -> buffer , ls_state -> buf , sizeof (mb_ctx -> buffer ));
6269 mb_ctx -> is224 = 0 ;
6370}
71+ #endif /* !CONFIG_MBEDTLS_VER_4_X_SUPPORT */
6472
6573int
6674crypto_hash_sha256_init (crypto_hash_sha256_state * state )
6775{
76+ #if defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
77+ psa_status_t status ;
78+ psa_hash_operation_t * operation ;
79+
80+ /* Store PSA hash operation in the state buffer
81+ * The libsodium state structure is large enough to hold psa_hash_operation_t
82+ */
83+ memset (state , 0 , sizeof (* state ));
84+ operation = (psa_hash_operation_t * )state ;
85+ * operation = psa_hash_operation_init ();
86+
87+ status = psa_hash_setup (operation , PSA_ALG_SHA_256 );
88+ if (status != PSA_SUCCESS ) {
89+ return -1 ;
90+ }
91+ return 0 ;
92+ #else
6893 mbedtls_sha256_context ctx ;
6994 mbedtls_sha256_init (& ctx );
7095#ifdef MBEDTLS_2_X_COMPAT
@@ -77,12 +102,24 @@ crypto_hash_sha256_init(crypto_hash_sha256_state *state)
77102 }
78103 sha256_mbedtls_to_libsodium (state , & ctx );
79104 return 0 ;
105+ #endif /* CONFIG_MBEDTLS_VER_4_X_SUPPORT */
80106}
81107
82108int
83109crypto_hash_sha256_update (crypto_hash_sha256_state * state ,
84110 const unsigned char * in , unsigned long long inlen )
85111{
112+ #if defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
113+ psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
114+ psa_status_t status ;
115+
116+ status = psa_hash_update (operation , in , inlen );
117+ if (status != PSA_SUCCESS ) {
118+ psa_hash_abort (operation );
119+ return -1 ;
120+ }
121+ return 0 ;
122+ #else
86123 mbedtls_sha256_context ctx ;
87124 sha256_libsodium_to_mbedtls (& ctx , state );
88125#ifdef MBEDTLS_2_X_COMPAT
@@ -95,27 +132,62 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
95132 }
96133 sha256_mbedtls_to_libsodium (state , & ctx );
97134 return 0 ;
135+ #endif /* CONFIG_MBEDTLS_VER_4_X_SUPPORT */
98136}
99137
100138int
101139crypto_hash_sha256_final (crypto_hash_sha256_state * state , unsigned char * out )
102140{
141+ #if defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
142+ psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
143+ psa_status_t status ;
144+ size_t hash_len ;
145+
146+ status = psa_hash_finish (operation , out , crypto_hash_sha256_BYTES , & hash_len );
147+ if (status != PSA_SUCCESS ) {
148+ psa_hash_abort (operation );
149+ return -1 ;
150+ }
151+ return 0 ;
152+ #else
103153 mbedtls_sha256_context ctx ;
104154 sha256_libsodium_to_mbedtls (& ctx , state );
105155#ifdef MBEDTLS_2_X_COMPAT
106156 return mbedtls_sha256_finish_ret (& ctx , out );
107157#else
108158 return mbedtls_sha256_finish (& ctx , out );
109159#endif /* MBEDTLS_2_X_COMPAT */
160+ #endif /* CONFIG_MBEDTLS_VER_4_X_SUPPORT */
110161}
111162
112163int
113164crypto_hash_sha256 (unsigned char * out , const unsigned char * in ,
114165 unsigned long long inlen )
115166{
167+ #if defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
168+ psa_status_t status ;
169+ size_t hash_len ;
170+
171+ status = psa_hash_compute (PSA_ALG_SHA_256 , in , inlen , out ,
172+ crypto_hash_sha256_BYTES , & hash_len );
173+ if (status != PSA_SUCCESS ) {
174+ return -1 ;
175+ }
176+ return 0 ;
177+ #else
116178#ifdef MBEDTLS_2_X_COMPAT
117179 return mbedtls_sha256_ret (in , inlen , out , 0 );
118180#else
119181 return mbedtls_sha256 (in , inlen , out , 0 );
120182#endif /* MBEDTLS_2_X_COMPAT */
183+ #endif /* CONFIG_MBEDTLS_VER_4_X_SUPPORT */
184+ }
185+
186+ #if defined(CONFIG_MBEDTLS_VER_4_X_SUPPORT )
187+ __attribute__((constructor )) static void crypto_psa_init (void )
188+ {
189+ /* Ensure PSA is initialized */
190+ psa_status_t status = psa_crypto_init ();
191+ assert (status == PSA_SUCCESS );
121192}
193+ #endif
0 commit comments