1313#include <mgmt/mcumgr/grp/fs_mgmt/fs_mgmt_config.h>
1414#include <mgmt/mcumgr/grp/fs_mgmt/fs_mgmt_hash_checksum_sha256.h>
1515
16- #if defined(CONFIG_TINYCRYPT_SHA256 )
17- #include <tinycrypt/constants.h>
18- #include <tinycrypt/sha256.h>
16+ #ifdef CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
17+ #include <psa/crypto.h>
18+ typedef psa_hash_operation_t hash_ctx_t ;
19+ #define SUCCESS_VALUE PSA_SUCCESS
20+
1921#else
20- #include <mbedtls/md.h>
2122#include <mbedtls/sha256.h>
22- #endif
23-
24- #define SHA256_DIGEST_SIZE 32
25-
26- #if defined(CONFIG_TINYCRYPT_SHA256 )
27- /* Tinycrypt SHA256 implementation */
28- static int fs_mgmt_hash_checksum_sha256 (struct fs_file_t * file , uint8_t * output ,
29- size_t * out_len , size_t len )
30- {
31- int rc = 0 ;
32- ssize_t bytes_read = 0 ;
33- size_t read_size = CONFIG_MCUMGR_GRP_FS_CHECKSUM_HASH_CHUNK_SIZE ;
34- uint8_t buffer [CONFIG_MCUMGR_GRP_FS_CHECKSUM_HASH_CHUNK_SIZE ];
35- struct tc_sha256_state_struct sha ;
36-
37- /* Clear variables prior to calculation */
38- * out_len = 0 ;
39- memset (output , 0 , SHA256_DIGEST_SIZE );
40-
41- if (tc_sha256_init (& sha ) != TC_CRYPTO_SUCCESS ) {
42- return MGMT_ERR_EUNKNOWN ;
43- }
23+ typedef mbedtls_sha256_context hash_ctx_t ;
24+ #define SUCCESS_VALUE 0
4425
45- /* Read all data from file and add to SHA256 hash calculation */
46- do {
47- if ((read_size + * out_len ) >= len ) {
48- /* Limit read size to size of requested data */
49- read_size = len - * out_len ;
50- }
51-
52- bytes_read = fs_read (file , buffer , read_size );
26+ #endif /* CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT */
5327
54- if (bytes_read < 0 ) {
55- /* Failed to read file data, pass generic unknown error back */
56- return MGMT_ERR_EUNKNOWN ;
57- } else if (bytes_read > 0 ) {
58- if (tc_sha256_update (& sha , buffer , bytes_read ) != TC_CRYPTO_SUCCESS ) {
59- return MGMT_ERR_EUNKNOWN ;
60- }
28+ #define SHA256_DIGEST_SIZE 32
6129
62- * out_len += bytes_read ;
63- }
64- } while (bytes_read > 0 && * out_len < len );
30+ /* The API that the different hash implementations provide further down. */
31+ static int hash_setup (hash_ctx_t * );
32+ static int hash_update (hash_ctx_t * , const uint8_t * input , size_t ilen );
33+ static int hash_finish (hash_ctx_t * , uint8_t * output );
34+ static void hash_teardown (hash_ctx_t * );
6535
66- /* Finalise SHA256 hash calculation and store output in provided output buffer */
67- if (tc_sha256_final (output , & sha ) != TC_CRYPTO_SUCCESS ) {
68- rc = MGMT_ERR_EUNKNOWN ;
69- }
70-
71- return rc ;
72- }
73- #else
74- /* mbedtls SHA256 implementation */
7536static int fs_mgmt_hash_checksum_sha256 (struct fs_file_t * file , uint8_t * output ,
7637 size_t * out_len , size_t len )
7738{
78- int rc = 0 ;
39+ int rc = MGMT_ERR_EUNKNOWN ;
7940 ssize_t bytes_read = 0 ;
8041 size_t read_size = CONFIG_MCUMGR_GRP_FS_CHECKSUM_HASH_CHUNK_SIZE ;
8142 uint8_t buffer [CONFIG_MCUMGR_GRP_FS_CHECKSUM_HASH_CHUNK_SIZE ];
82- mbedtls_md_context_t mbed_hash_ctx ;
83- const mbedtls_md_info_t * mbed_hash_info ;
43+ hash_ctx_t hash_ctx ;
8444
8545 /* Clear variables prior to calculation */
8646 * out_len = 0 ;
8747 memset (output , 0 , SHA256_DIGEST_SIZE );
8848
89- mbed_hash_info = mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 );
90- mbedtls_md_init (& mbed_hash_ctx );
91-
92- if (mbedtls_md_setup (& mbed_hash_ctx , mbed_hash_info , 0 ) != 0 ) {
93- return MGMT_ERR_EUNKNOWN ;
94- }
95-
96- if (mbedtls_md_starts (& mbed_hash_ctx )) {
97- rc = MGMT_ERR_EUNKNOWN ;
98- goto error ;
49+ if (hash_setup (& hash_ctx ) != SUCCESS_VALUE ) {
50+ goto teardown ;
9951 }
10052
10153 /* Read all data from file and add to SHA256 hash calculation */
@@ -108,30 +60,27 @@ static int fs_mgmt_hash_checksum_sha256(struct fs_file_t *file, uint8_t *output,
10860 bytes_read = fs_read (file , buffer , read_size );
10961
11062 if (bytes_read < 0 ) {
111- /* Failed to read file data, pass generic unknown error back */
112- rc = MGMT_ERR_EUNKNOWN ;
113- goto error ;
63+ /* Failed to read file data */
64+ goto teardown ;
11465 } else if (bytes_read > 0 ) {
115- if (mbedtls_md_update (& mbed_hash_ctx , buffer , bytes_read ) != 0 ) {
116- rc = MGMT_ERR_EUNKNOWN ;
117- goto error ;
66+ if (hash_update (& hash_ctx , buffer , bytes_read ) != SUCCESS_VALUE ) {
67+ goto teardown ;
11868 }
11969
12070 * out_len += bytes_read ;
12171 }
12272 } while (bytes_read > 0 && * out_len < len );
12373
12474 /* Finalise SHA256 hash calculation and store output in provided output buffer */
125- if (mbedtls_md_finish ( & mbed_hash_ctx , output ) != 0 ) {
126- rc = MGMT_ERR_EUNKNOWN ;
75+ if (hash_finish ( & hash_ctx , output ) == SUCCESS_VALUE ) {
76+ rc = 0 ;
12777 }
12878
129- error :
130- mbedtls_md_free ( & mbed_hash_ctx );
79+ teardown :
80+ hash_teardown ( & hash_ctx );
13181
13282 return rc ;
13383}
134- #endif
13584
13685static struct fs_mgmt_hash_checksum_group sha256 = {
13786 .group_name = "sha256" ,
@@ -149,3 +98,47 @@ void fs_mgmt_hash_checksum_unregister_sha256(void)
14998{
15099 fs_mgmt_hash_checksum_unregister_group (& sha256 );
151100}
101+
102+ #ifdef CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
103+
104+ static int hash_setup (psa_hash_operation_t * ctx )
105+ {
106+ * ctx = psa_hash_operation_init ();
107+ return psa_hash_setup (ctx , PSA_ALG_SHA_256 );
108+ }
109+ static int hash_update (psa_hash_operation_t * ctx , const uint8_t * input , size_t ilen )
110+ {
111+ return psa_hash_update (ctx , input , ilen );
112+ }
113+ static int hash_finish (psa_hash_operation_t * ctx , uint8_t * output )
114+ {
115+ size_t output_length ;
116+
117+ return psa_hash_finish (ctx , output , SHA256_DIGEST_SIZE , & output_length );
118+ }
119+ static void hash_teardown (psa_hash_operation_t * ctx )
120+ {
121+ psa_hash_abort (ctx );
122+ }
123+
124+ #else
125+
126+ static int hash_setup (mbedtls_sha256_context * ctx )
127+ {
128+ mbedtls_sha256_init (ctx );
129+ return mbedtls_sha256_starts (ctx , false);
130+ }
131+ static int hash_update (mbedtls_sha256_context * ctx , const uint8_t * input , size_t ilen )
132+ {
133+ return mbedtls_sha256_update (ctx , input , ilen );
134+ }
135+ static int hash_finish (mbedtls_sha256_context * ctx , uint8_t * output )
136+ {
137+ return mbedtls_sha256_finish (ctx , output );
138+ }
139+ static void hash_teardown (mbedtls_sha256_context * ctx )
140+ {
141+ mbedtls_sha256_free (ctx );
142+ }
143+
144+ #endif /* CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT */
0 commit comments