Skip to content

Commit b15d896

Browse files
tomi-fontnordicjm
authored andcommitted
samples: net: download: migrate to PSA Crypto
Replace usage of legacy `mbedtls_sha256` by the PSA Crypto equivalent. The explicit setting of `CONFIG_TFM_PROFILE_TYPE_MINIMAL=y` is removed from the 91 devices so that the `NOT_SET` profile is used instead because the `MINIMAL` profile does not support SHA-256. Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
1 parent a2eed96 commit b15d896

5 files changed

Lines changed: 25 additions & 20 deletions

File tree

samples/net/download/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ config SAMPLE_FILE_URL
4747

4848
config SAMPLE_COMPUTE_HASH
4949
bool "Compute sha256 hash"
50-
select MBEDTLS
50+
select PSA_CRYPTO
51+
select PSA_WANT_ALG_SHA_256
5152

5253
config SAMPLE_COMPARE_HASH
5354
bool "Compare hash"

samples/net/download/boards/nrf9151dk_nrf9151_ns.conf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
# This file is merged with prj.conf in the application folder, and options
99
# set here will take precedence if they are present in both files.
1010

11-
# TF-M
12-
CONFIG_TFM_PROFILE_TYPE_MINIMAL=y
1311

1412
# Disable Duplicate Address Detection (DAD)
1513
# due to not being properly implemented for offloaded interfaces.

samples/net/download/boards/nrf9160dk_nrf9160_ns.conf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
# TF-M
8-
CONFIG_TFM_PROFILE_TYPE_MINIMAL=y
9-
107
# Configuration file for nRF9160 DK
118
# This file is merged with prj.conf in the application folder, and options
129
# set here will take precedence if they are present in both files.

samples/net/download/boards/nrf9161dk_nrf9161_ns.conf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
66

7-
# TF-M
8-
CONFIG_TFM_PROFILE_TYPE_MINIMAL=y
9-
107
# Configuration file for nRF9160 DK
118
# This file is merged with prj.conf in the application folder, and options
129
# set here will take precedence if they are present in both files.

samples/net/download/src/main.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ static struct downloader_host_cfg host_dl_cfg = {
7474
};
7575

7676
#if CONFIG_SAMPLE_COMPUTE_HASH
77-
#include <mbedtls/sha256.h>
78-
static mbedtls_sha256_context sha256_ctx;
77+
#include <psa/crypto.h>
78+
static psa_hash_operation_t hash_ctx;
7979
#endif
8080

8181
static int64_t ref_time;
@@ -215,7 +215,9 @@ static int callback(const struct downloader_evt *event)
215215
static size_t file_size;
216216
uint32_t speed;
217217
int64_t ms_elapsed;
218-
218+
#if CONFIG_SAMPLE_COMPUTE_HASH
219+
psa_status_t status;
220+
#endif
219221
if (downloaded == 0) {
220222
downloader_file_size_get(&downloader, &file_size);
221223
downloaded += STARTING_OFFSET;
@@ -231,8 +233,11 @@ static int callback(const struct downloader_evt *event)
231233
}
232234

233235
#if CONFIG_SAMPLE_COMPUTE_HASH
234-
mbedtls_sha256_update(&sha256_ctx,
235-
event->fragment.buf, event->fragment.len);
236+
status = psa_hash_update(&hash_ctx, event->fragment.buf, event->fragment.len);
237+
if (status != PSA_SUCCESS) {
238+
printk("Error during hash update: %d\n", status);
239+
return status;
240+
}
236241
#endif
237242
return 0;
238243

@@ -245,9 +250,13 @@ static int callback(const struct downloader_evt *event)
245250
#if CONFIG_SAMPLE_COMPUTE_HASH
246251
uint8_t hash[32];
247252
uint8_t hash_str[64 + 1];
253+
size_t hash_length;
248254

249-
mbedtls_sha256_finish(&sha256_ctx, hash);
250-
mbedtls_sha256_free(&sha256_ctx);
255+
status = psa_hash_finish(&hash_ctx, hash, sizeof(hash), &hash_length);
256+
if (status != PSA_SUCCESS) {
257+
printk("Error during hash finish: %d\n", status);
258+
return status;
259+
}
251260

252261
bin2hex(hash, sizeof(hash), hash_str, sizeof(hash_str));
253262

@@ -313,7 +322,14 @@ int main(void)
313322
return 0;
314323
}
315324
#endif
325+
#if CONFIG_SAMPLE_COMPUTE_HASH
326+
psa_status_t status = psa_hash_setup(&hash_ctx, PSA_ALG_SHA_256);
316327

328+
if (status != PSA_SUCCESS) {
329+
printk("psa_hash_setup, error: %d\n", status);
330+
return status;
331+
}
332+
#endif
317333
printk("Connecting to network\n");
318334

319335
err = conn_mgr_all_if_connect(true);
@@ -342,10 +358,6 @@ int main(void)
342358
return 0;
343359
}
344360

345-
#if CONFIG_SAMPLE_COMPUTE_HASH
346-
mbedtls_sha256_init(&sha256_ctx);
347-
mbedtls_sha256_starts(&sha256_ctx, false);
348-
#endif
349361

350362
ref_time = k_uptime_get();
351363

0 commit comments

Comments
 (0)