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 */
1616#endif /* MBEDTLS_ALLOW_PRIVATE_ACCESS */
1717#endif /* !(MBEDTLS_VERSION_NUMBER < 0x03000000) */
1818
19+ /* For MbedTLS 4.x support using PSA Crypto */
20+ #if (MBEDTLS_VERSION_NUMBER >= 0x04000000 )
21+ #define MBEDTLS_PSA_CRYPTO
22+ #endif
23+
1924#include "crypto_hash_sha256.h"
20- #include "mbedtls/sha256.h"
2125#include <string.h>
2226
27+ #ifdef MBEDTLS_PSA_CRYPTO
28+ #include "psa/crypto.h"
29+ #else
30+ #include "mbedtls/sha256.h"
31+ #endif
32+
33+ #ifndef MBEDTLS_PSA_CRYPTO
2334#ifdef MBEDTLS_SHA256_ALT
2435/* Wrapper only works if the libsodium context structure can be mapped
2536 directly to the mbedTLS context structure.
@@ -61,10 +72,42 @@ static void sha256_libsodium_to_mbedtls(mbedtls_sha256_context *mb_ctx, crypto_h
6172 memcpy (mb_ctx -> buffer , ls_state -> buf , sizeof (mb_ctx -> buffer ));
6273 mb_ctx -> is224 = 0 ;
6374}
75+ #endif /* !MBEDTLS_PSA_CRYPTO */
6476
6577int
6678crypto_hash_sha256_init (crypto_hash_sha256_state * state )
6779{
80+ if (state == NULL ) {
81+ return -1 ;
82+ }
83+ #ifdef MBEDTLS_PSA_CRYPTO
84+ psa_status_t status ;
85+
86+ status = psa_crypto_init ();
87+ if (status != PSA_SUCCESS ) {
88+ return -1 ;
89+ }
90+
91+ psa_hash_operation_t * operation ;
92+
93+ /* Store PSA hash operation in the state buffer
94+ * The libsodium state structure is large enough to hold psa_hash_operation_t.
95+ * Ensure this is safe with respect to both size and alignment.
96+ */
97+ _Static_assert (sizeof (crypto_hash_sha256_state ) >= sizeof (psa_hash_operation_t ),
98+ "crypto_hash_sha256_state too small for psa_hash_operation_t" );
99+ _Static_assert (_Alignof(crypto_hash_sha256_state ) >= _Alignof(psa_hash_operation_t ),
100+ "crypto_hash_sha256_state alignment insufficient for psa_hash_operation_t" );
101+ memset (state , 0 , sizeof (* state ));
102+ operation = (psa_hash_operation_t * )state ;
103+ * operation = psa_hash_operation_init ();
104+
105+ status = psa_hash_setup (operation , PSA_ALG_SHA_256 );
106+ if (status != PSA_SUCCESS ) {
107+ return -1 ;
108+ }
109+ return 0 ;
110+ #else
68111 mbedtls_sha256_context ctx ;
69112 mbedtls_sha256_init (& ctx );
70113#ifdef MBEDTLS_2_X_COMPAT
@@ -77,12 +120,27 @@ crypto_hash_sha256_init(crypto_hash_sha256_state *state)
77120 }
78121 sha256_mbedtls_to_libsodium (state , & ctx );
79122 return 0 ;
123+ #endif /* !MBEDTLS_PSA_CRYPTO */
80124}
81125
82126int
83127crypto_hash_sha256_update (crypto_hash_sha256_state * state ,
84128 const unsigned char * in , unsigned long long inlen )
85129{
130+ if (state == NULL || (in == NULL && inlen > 0 )) {
131+ return -1 ;
132+ }
133+ #ifdef MBEDTLS_PSA_CRYPTO
134+ psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
135+ psa_status_t status ;
136+
137+ status = psa_hash_update (operation , in , inlen );
138+ if (status != PSA_SUCCESS ) {
139+ psa_hash_abort (operation );
140+ return -1 ;
141+ }
142+ return 0 ;
143+ #else
86144 mbedtls_sha256_context ctx ;
87145 sha256_libsodium_to_mbedtls (& ctx , state );
88146#ifdef MBEDTLS_2_X_COMPAT
@@ -95,27 +153,59 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
95153 }
96154 sha256_mbedtls_to_libsodium (state , & ctx );
97155 return 0 ;
156+ #endif /* !MBEDTLS_PSA_CRYPTO */
98157}
99158
100159int
101160crypto_hash_sha256_final (crypto_hash_sha256_state * state , unsigned char * out )
102161{
162+ if (state == NULL || out == NULL ) {
163+ return -1 ;
164+ }
165+ #ifdef MBEDTLS_PSA_CRYPTO
166+ psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
167+ psa_status_t status ;
168+ size_t hash_len ;
169+
170+ status = psa_hash_finish (operation , out , crypto_hash_sha256_BYTES , & hash_len );
171+ if (status != PSA_SUCCESS || hash_len != crypto_hash_sha256_BYTES ) {
172+ psa_hash_abort (operation );
173+ return -1 ;
174+ }
175+ return 0 ;
176+ #else
103177 mbedtls_sha256_context ctx ;
104178 sha256_libsodium_to_mbedtls (& ctx , state );
105179#ifdef MBEDTLS_2_X_COMPAT
106180 return mbedtls_sha256_finish_ret (& ctx , out );
107181#else
108182 return mbedtls_sha256_finish (& ctx , out );
109183#endif /* MBEDTLS_2_X_COMPAT */
184+ #endif /* !MBEDTLS_PSA_CRYPTO */
110185}
111186
112187int
113188crypto_hash_sha256 (unsigned char * out , const unsigned char * in ,
114189 unsigned long long inlen )
115190{
191+ if (out == NULL || (in == NULL && inlen > 0 )) {
192+ return -1 ;
193+ }
194+ #ifdef MBEDTLS_PSA_CRYPTO
195+ psa_status_t status ;
196+ size_t hash_len ;
197+
198+ status = psa_hash_compute (PSA_ALG_SHA_256 , in , inlen , out ,
199+ crypto_hash_sha256_BYTES , & hash_len );
200+ if (status != PSA_SUCCESS || hash_len != crypto_hash_sha256_BYTES ) {
201+ return -1 ;
202+ }
203+ return 0 ;
204+ #else
116205#ifdef MBEDTLS_2_X_COMPAT
117206 return mbedtls_sha256_ret (in , inlen , out , 0 );
118207#else
119208 return mbedtls_sha256 (in , inlen , out , 0 );
120209#endif /* MBEDTLS_2_X_COMPAT */
210+ #endif /* !MBEDTLS_PSA_CRYPTO */
121211}
0 commit comments