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"
25+ #include <assert.h>
2126#include <string.h>
2227
28+ #ifdef MBEDTLS_PSA_CRYPTO
29+ #include "psa/crypto.h"
30+ #else
31+ #include "mbedtls/sha256.h"
32+ #endif
33+
34+ #ifndef MBEDTLS_PSA_CRYPTO
2335#ifdef MBEDTLS_SHA256_ALT
2436/* Wrapper only works if the libsodium context structure can be mapped
2537 directly to the mbedTLS context structure.
@@ -61,10 +73,33 @@ static void sha256_libsodium_to_mbedtls(mbedtls_sha256_context *mb_ctx, crypto_h
6173 memcpy (mb_ctx -> buffer , ls_state -> buf , sizeof (mb_ctx -> buffer ));
6274 mb_ctx -> is224 = 0 ;
6375}
76+ #endif /* !MBEDTLS_PSA_CRYPTO */
6477
6578int
6679crypto_hash_sha256_init (crypto_hash_sha256_state * state )
6780{
81+ #ifdef MBEDTLS_PSA_CRYPTO
82+ psa_status_t status ;
83+ psa_hash_operation_t * operation ;
84+
85+ /* Store PSA hash operation in the state buffer
86+ * The libsodium state structure is large enough to hold psa_hash_operation_t.
87+ * Ensure this is safe with respect to both size and alignment.
88+ */
89+ _Static_assert (sizeof (crypto_hash_sha256_state ) >= sizeof (psa_hash_operation_t ),
90+ "crypto_hash_sha256_state too small for psa_hash_operation_t" );
91+ _Static_assert (_Alignof(crypto_hash_sha256_state ) >= _Alignof(psa_hash_operation_t ),
92+ "crypto_hash_sha256_state alignment insufficient for psa_hash_operation_t" );
93+ memset (state , 0 , sizeof (* state ));
94+ operation = (psa_hash_operation_t * )state ;
95+ * operation = psa_hash_operation_init ();
96+
97+ status = psa_hash_setup (operation , PSA_ALG_SHA_256 );
98+ if (status != PSA_SUCCESS ) {
99+ return -1 ;
100+ }
101+ return 0 ;
102+ #else
68103 mbedtls_sha256_context ctx ;
69104 mbedtls_sha256_init (& ctx );
70105#ifdef MBEDTLS_2_X_COMPAT
@@ -77,12 +112,24 @@ crypto_hash_sha256_init(crypto_hash_sha256_state *state)
77112 }
78113 sha256_mbedtls_to_libsodium (state , & ctx );
79114 return 0 ;
115+ #endif /* !MBEDTLS_PSA_CRYPTO */
80116}
81117
82118int
83119crypto_hash_sha256_update (crypto_hash_sha256_state * state ,
84120 const unsigned char * in , unsigned long long inlen )
85121{
122+ #ifdef MBEDTLS_PSA_CRYPTO
123+ psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
124+ psa_status_t status ;
125+
126+ status = psa_hash_update (operation , in , inlen );
127+ if (status != PSA_SUCCESS ) {
128+ psa_hash_abort (operation );
129+ return -1 ;
130+ }
131+ return 0 ;
132+ #else
86133 mbedtls_sha256_context ctx ;
87134 sha256_libsodium_to_mbedtls (& ctx , state );
88135#ifdef MBEDTLS_2_X_COMPAT
@@ -95,27 +142,62 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
95142 }
96143 sha256_mbedtls_to_libsodium (state , & ctx );
97144 return 0 ;
145+ #endif /* !MBEDTLS_PSA_CRYPTO */
98146}
99147
100148int
101149crypto_hash_sha256_final (crypto_hash_sha256_state * state , unsigned char * out )
102150{
151+ #ifdef MBEDTLS_PSA_CRYPTO
152+ psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
153+ psa_status_t status ;
154+ size_t hash_len ;
155+
156+ status = psa_hash_finish (operation , out , crypto_hash_sha256_BYTES , & hash_len );
157+ if (status != PSA_SUCCESS ) {
158+ psa_hash_abort (operation );
159+ return -1 ;
160+ }
161+ return 0 ;
162+ #else
103163 mbedtls_sha256_context ctx ;
104164 sha256_libsodium_to_mbedtls (& ctx , state );
105165#ifdef MBEDTLS_2_X_COMPAT
106166 return mbedtls_sha256_finish_ret (& ctx , out );
107167#else
108168 return mbedtls_sha256_finish (& ctx , out );
109169#endif /* MBEDTLS_2_X_COMPAT */
170+ #endif /* !MBEDTLS_PSA_CRYPTO */
110171}
111172
112173int
113174crypto_hash_sha256 (unsigned char * out , const unsigned char * in ,
114175 unsigned long long inlen )
115176{
177+ #ifdef MBEDTLS_PSA_CRYPTO
178+ psa_status_t status ;
179+ size_t hash_len ;
180+
181+ status = psa_hash_compute (PSA_ALG_SHA_256 , in , inlen , out ,
182+ crypto_hash_sha256_BYTES , & hash_len );
183+ if (status != PSA_SUCCESS ) {
184+ return -1 ;
185+ }
186+ return 0 ;
187+ #else
116188#ifdef MBEDTLS_2_X_COMPAT
117189 return mbedtls_sha256_ret (in , inlen , out , 0 );
118190#else
119191 return mbedtls_sha256 (in , inlen , out , 0 );
120192#endif /* MBEDTLS_2_X_COMPAT */
193+ #endif /* !MBEDTLS_PSA_CRYPTO */
121194}
195+
196+ #ifdef MBEDTLS_PSA_CRYPTO
197+ __attribute__((constructor )) static void crypto_psa_init_sha256 (void )
198+ {
199+ /* Ensure PSA is initialized */
200+ psa_status_t status = psa_crypto_init ();
201+ assert (status == PSA_SUCCESS );
202+ }
203+ #endif
0 commit comments