2424#include "crypto_hash_sha256.h"
2525#include <string.h>
2626
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
34- #ifdef MBEDTLS_SHA256_ALT
35- /* Wrapper only works if the libsodium context structure can be mapped
36- directly to the mbedTLS context structure.
37-
38- See extended comments in crypto_hash_sha512_mbedtls.c
39- */
40- #error "This wrapper only support standard software mbedTLS SHA"
41- #endif
42-
43- /* Sanity check that all the context fields have identical sizes
44- (this should be more or less given from the SHA256 algorithm)
45-
46- Note that the meaning of the fields is *not* all the same. In libsodium, SHA256 'count' is a 64-bit *bit* count. In
47- mbedTLS, 'total' is a 2x32-bit *byte* count (count[0] == MSB).
48-
49- For this implementation, we don't convert so the libsodium state structure actually holds a binary copy of the
50- mbedTLS totals. This doesn't matter inside libsodium's documented API, but would matter if any callers try to use
51- the state's bit count.
52- */
53- _Static_assert (sizeof (((crypto_hash_sha256_state * )0 )-> state ) == sizeof (((mbedtls_sha256_context * )0 )-> state ), "state mismatch" );
54- _Static_assert (sizeof (((crypto_hash_sha256_state * )0 )-> count ) == sizeof (((mbedtls_sha256_context * )0 )-> total ), "count mismatch" );
55- _Static_assert (sizeof (((crypto_hash_sha256_state * )0 )-> buf ) == sizeof (((mbedtls_sha256_context * )0 )-> buffer ), "buf mismatch" );
56-
57- /* Inline functions to convert between mbedTLS & libsodium
58- context structures
59- */
60-
61- static void sha256_mbedtls_to_libsodium (crypto_hash_sha256_state * ls_state , const mbedtls_sha256_context * mb_ctx )
62- {
63- memcpy (& ls_state -> count , mb_ctx -> total , sizeof (ls_state -> count ));
64- memcpy (ls_state -> state , mb_ctx -> state , sizeof (ls_state -> state ));
65- memcpy (ls_state -> buf , mb_ctx -> buffer , sizeof (ls_state -> buf ));
66- }
67-
68- static void sha256_libsodium_to_mbedtls (mbedtls_sha256_context * mb_ctx , crypto_hash_sha256_state * ls_state )
69- {
70- memcpy (mb_ctx -> total , & ls_state -> count , sizeof (mb_ctx -> total ));
71- memcpy (mb_ctx -> state , ls_state -> state , sizeof (mb_ctx -> state ));
72- memcpy (mb_ctx -> buffer , ls_state -> buf , sizeof (mb_ctx -> buffer ));
73- mb_ctx -> is224 = 0 ;
74- }
75- #endif /* !MBEDTLS_PSA_CRYPTO */
76-
7727int
7828crypto_hash_sha256_init (crypto_hash_sha256_state * state )
7929{
80- if (state == NULL ) {
81- return -1 ;
82- }
8330#ifdef MBEDTLS_PSA_CRYPTO
8431 psa_status_t status ;
8532
@@ -88,37 +35,23 @@ crypto_hash_sha256_init(crypto_hash_sha256_state *state)
8835 return -1 ;
8936 }
9037
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 ();
38+ state -> _psa_op = psa_hash_operation_init ();
10439
105- status = psa_hash_setup (operation , PSA_ALG_SHA_256 );
40+ status = psa_hash_setup (& state -> _psa_op , PSA_ALG_SHA_256 );
10641 if (status != PSA_SUCCESS ) {
10742 return -1 ;
10843 }
10944 return 0 ;
11045#else
111- mbedtls_sha256_context ctx ;
112- mbedtls_sha256_init (& ctx );
46+ mbedtls_sha256_init (& state -> ctx );
11347#ifdef MBEDTLS_2_X_COMPAT
114- int ret = mbedtls_sha256_starts_ret (& ctx , 0 );
48+ int ret = mbedtls_sha256_starts_ret (& state -> ctx , 0 );
11549#else
116- int ret = mbedtls_sha256_starts (& ctx , 0 );
50+ int ret = mbedtls_sha256_starts (& state -> ctx , 0 );
11751#endif /* MBEDTLS_2_X_COMPAT */
11852 if (ret != 0 ) {
11953 return ret ;
12054 }
121- sha256_mbedtls_to_libsodium (state , & ctx );
12255 return 0 ;
12356#endif /* !MBEDTLS_PSA_CRYPTO */
12457}
12760crypto_hash_sha256_update (crypto_hash_sha256_state * state ,
12861 const unsigned char * in , unsigned long long inlen )
12962{
130- if (state == NULL || ( in == NULL && inlen > 0 ) ) {
63+ if (in == NULL && inlen > 0 ) {
13164 return -1 ;
13265 }
13366#ifdef MBEDTLS_PSA_CRYPTO
134- psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
13567 psa_status_t status ;
13668
137- status = psa_hash_update (operation , in , inlen );
69+ status = psa_hash_update (& state -> _psa_op , in , inlen );
13870 if (status != PSA_SUCCESS ) {
139- psa_hash_abort (operation );
71+ psa_hash_abort (& state -> _psa_op );
14072 return -1 ;
14173 }
14274 return 0 ;
14375#else
144- mbedtls_sha256_context ctx ;
145- sha256_libsodium_to_mbedtls (& ctx , state );
14676#ifdef MBEDTLS_2_X_COMPAT
147- int ret = mbedtls_sha256_update_ret (& ctx , in , inlen );
77+ int ret = mbedtls_sha256_update_ret (& state -> ctx , in , inlen );
14878#else
149- int ret = mbedtls_sha256_update (& ctx , in , inlen );
79+ int ret = mbedtls_sha256_update (& state -> ctx , in , inlen );
15080#endif /* MBEDTLS_2_X_COMPAT */
15181 if (ret != 0 ) {
15282 return ret ;
15383 }
154- sha256_mbedtls_to_libsodium (state , & ctx );
15584 return 0 ;
15685#endif /* !MBEDTLS_PSA_CRYPTO */
15786}
15887
15988int
16089crypto_hash_sha256_final (crypto_hash_sha256_state * state , unsigned char * out )
16190{
162- if (state == NULL || out == NULL ) {
163- return -1 ;
164- }
16591#ifdef MBEDTLS_PSA_CRYPTO
166- psa_hash_operation_t * operation = (psa_hash_operation_t * )state ;
16792 psa_status_t status ;
16893 size_t hash_len ;
16994
170- status = psa_hash_finish (operation , out , crypto_hash_sha256_BYTES , & hash_len );
95+ status = psa_hash_finish (& state -> _psa_op , out , crypto_hash_sha256_BYTES , & hash_len );
17196 if (status != PSA_SUCCESS || hash_len != crypto_hash_sha256_BYTES ) {
172- psa_hash_abort (operation );
97+ psa_hash_abort (& state -> _psa_op );
17398 return -1 ;
17499 }
175100 return 0 ;
176101#else
177- mbedtls_sha256_context ctx ;
178- sha256_libsodium_to_mbedtls (& ctx , state );
179102#ifdef MBEDTLS_2_X_COMPAT
180- return mbedtls_sha256_finish_ret (& ctx , out );
103+ return mbedtls_sha256_finish_ret (& state -> ctx , out );
181104#else
182- return mbedtls_sha256_finish (& ctx , out );
105+ return mbedtls_sha256_finish (& state -> ctx , out );
183106#endif /* MBEDTLS_2_X_COMPAT */
184107#endif /* !MBEDTLS_PSA_CRYPTO */
185108}
188111crypto_hash_sha256 (unsigned char * out , const unsigned char * in ,
189112 unsigned long long inlen )
190113{
191- if (out == NULL || ( in == NULL && inlen > 0 ) ) {
114+ if (in == NULL && inlen > 0 ) {
192115 return -1 ;
193116 }
194117#ifdef MBEDTLS_PSA_CRYPTO
0 commit comments