Skip to content

Commit 6002abe

Browse files
tomi-fontnashif
authored andcommitted
mgmt: mcumgr: replace Tinycrypt by PSA
As part of ongoing work to move away from TinyCrypt and towards PSA (#43712), make fs_mgmt use either PSA (when available) or MbedTLS (as a fallback) for SHA-256. The use of PSA is guarded by CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT which requires a locally-built PSA core for devices without TF-M. Signed-off-by: Tomi Fontanilles <[email protected]>
1 parent 2d61db9 commit 6002abe

File tree

6 files changed

+85
-84
lines changed

6 files changed

+85
-84
lines changed

doc/releases/migration-guide-3.7.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ LoRaWAN
370370
MCUmgr
371371
======
372372

373+
* The support for SHA-256 (when using checksum/hash functions), previously provided
374+
by either TinyCrypt or MbedTLS, is now provided by either PSA or MbedTLS.
375+
PSA is the recommended API going forward, however, if it is not already enabled
376+
(:kconfig:option:`CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT`) and you have tight code size
377+
constraints, you may be able to save 1.3 KB by using MbedTLS instead.
378+
373379
Modem
374380
=====
375381

subsys/mgmt/mcumgr/grp/fs_mgmt/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ config MCUMGR_GRP_FS_CHECKSUM_IEEE_CRC32
125125

126126
config MCUMGR_GRP_FS_HASH_SHA256
127127
bool "SHA256 hash support"
128-
depends on TINYCRYPT_SHA256 || MBEDTLS_MAC_SHA256_ENABLED
128+
depends on BUILD_WITH_TFM || MBEDTLS_MAC_SHA256_ENABLED
129+
select PSA_WANT_ALG_SHA_256 if BUILD_WITH_TFM
129130
help
130131
Enable SHA256 hash support for MCUmgr.
131132

subsys/mgmt/mcumgr/grp/fs_mgmt/src/fs_mgmt_hash_checksum_sha256.c

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,89 +13,41 @@
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 */
7536
static 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

13685
static 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 */

tests/subsys/mgmt/mcumgr/all_options/prj.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66
CONFIG_ZTEST=y
7-
CONFIG_TINYCRYPT=y
8-
CONFIG_TINYCRYPT_SHA256=y
7+
CONFIG_MBEDTLS=y
8+
CONFIG_MBEDTLS_MAC_SHA256_ENABLED=y
99
CONFIG_FILE_SYSTEM=y
1010
CONFIG_BASE64=y
1111
CONFIG_NET_BUF=y

tests/subsys/mgmt/mcumgr/fs_mgmt_hash_supported/configuration/all.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# SPDX-License-Identifier: Apache-2.0
55
#
6-
CONFIG_TINYCRYPT=y
7-
CONFIG_TINYCRYPT_SHA256=y
86
CONFIG_MCUMGR_GRP_FS_CHECKSUM_IEEE_CRC32=y
97
CONFIG_MCUMGR_GRP_FS_HASH_SHA256=y
8+
CONFIG_MBEDTLS=y
9+
CONFIG_MBEDTLS_MAC_SHA256_ENABLED=y

tests/subsys/mgmt/mcumgr/fs_mgmt_hash_supported/configuration/sha256.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44
# SPDX-License-Identifier: Apache-2.0
55
#
6-
CONFIG_TINYCRYPT=y
7-
CONFIG_TINYCRYPT_SHA256=y
6+
CONFIG_MCUMGR_GRP_FS_CHECKSUM_IEEE_CRC32=n
87
CONFIG_MCUMGR_GRP_FS_HASH_SHA256=y
8+
CONFIG_MBEDTLS=y
9+
CONFIG_MBEDTLS_MAC_SHA256_ENABLED=y

0 commit comments

Comments
 (0)