Skip to content

Commit 8f8076d

Browse files
committed
lib: uuid: replace legacy crypto support with PSA API
Legacy crypto support is going to be removed in the next Mbed TLS release (which will be named TF-PSA-Crypto for the crypto support) so this commit transitions UUID library from legacy crypto to PSA API. Signed-off-by: Valerio Setti <[email protected]>
1 parent 169cf86 commit 8f8076d

File tree

3 files changed

+25
-46
lines changed

3 files changed

+25
-46
lines changed

lib/uuid/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ config UUID_V5
2323
select EXPERIMENTAL
2424
depends on UUID
2525
depends on MBEDTLS
26-
depends on MBEDTLS_MD
27-
depends on MBEDTLS_SHA1
26+
depends on MBEDTLS_PSA_CRYPTO_C
27+
depends on PSA_WANT_ALG_SHA_1
2828
# When TF-M is enabled, Mbed TLS's MD module (which is used to generate
2929
# v5 UUIDs) will dispacth hash operations to TF-M. Unfortunately TF-M
3030
# does not support SHA-1 (because it's a weak algorithm) so the

lib/uuid/uuid.c

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#endif
1616

1717
#if defined(CONFIG_UUID_V5)
18-
#include <mbedtls/md.h>
18+
#include <psa/crypto.h>
1919
#endif
2020

2121
#if defined(CONFIG_UUID_BASE64)
@@ -82,54 +82,32 @@ int uuid_generate_v4(struct uuid *out)
8282
int uuid_generate_v5(const struct uuid *ns, const void *data, size_t data_size,
8383
struct uuid *out)
8484
{
85+
uint8_t sha_result[PSA_HASH_LENGTH(PSA_ALG_SHA_1)];
86+
size_t sha_len = 0;
87+
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
88+
psa_status_t status;
89+
8590
if (out == NULL) {
8691
return -EINVAL;
8792
}
88-
int ret = 0;
89-
int mbedtls_err = 0;
90-
mbedtls_md_context_t ctx = {0};
91-
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
92-
const size_t sha_1_bytes = 20;
93-
uint8_t sha_result[sha_1_bytes];
94-
95-
mbedtls_md_init(&ctx);
96-
mbedtls_err = mbedtls_md_setup(&ctx, md_info, 0);
97-
/* Might return: MBEDTLS_ERR_MD_BAD_INPUT_DATA or MBEDTLS_ERR_MD_ALLOC_FAILED */
98-
switch (mbedtls_err) {
99-
case 0:
100-
break;
101-
case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
102-
ret = -EINVAL;
103-
goto exit;
104-
case MBEDTLS_ERR_MD_ALLOC_FAILED:
105-
ret = -ENOMEM;
106-
goto exit;
107-
default:
108-
ret = -ENOTSUP;
109-
goto exit;
110-
}
111-
mbedtls_err = mbedtls_md_starts(&ctx);
112-
if (mbedtls_err != 0) {
113-
/* Might return MBEDTLS_ERR_MD_BAD_INPUT_DATA */
114-
ret = -EINVAL;
93+
94+
status = psa_hash_setup(&hash_operation, PSA_ALG_SHA_1);
95+
if (status != PSA_SUCCESS) {
11596
goto exit;
11697
}
117-
mbedtls_err = mbedtls_md_update(&ctx, ns->val, UUID_SIZE);
118-
if (mbedtls_err != 0) {
119-
/* Might return MBEDTLS_ERR_MD_BAD_INPUT_DATA */
120-
ret = -EINVAL;
98+
99+
status = psa_hash_update(&hash_operation, ns->val, UUID_SIZE);
100+
if (status != PSA_SUCCESS) {
121101
goto exit;
122102
}
123-
mbedtls_err = mbedtls_md_update(&ctx, data, data_size);
124-
if (mbedtls_err != 0) {
125-
/* Might return MBEDTLS_ERR_MD_BAD_INPUT_DATA */
126-
ret = -EINVAL;
103+
104+
status = psa_hash_update(&hash_operation, data, data_size);
105+
if (status != PSA_SUCCESS) {
127106
goto exit;
128107
}
129-
mbedtls_err = mbedtls_md_finish(&ctx, sha_result);
130-
if (mbedtls_err != 0) {
131-
/* Might return MBEDTLS_ERR_MD_BAD_INPUT_DATA */
132-
ret = -EINVAL;
108+
109+
status = psa_hash_finish(&hash_operation, sha_result, sizeof(sha_result), &sha_len);
110+
if (status != PSA_SUCCESS) {
133111
goto exit;
134112
}
135113

@@ -141,8 +119,9 @@ int uuid_generate_v5(const struct uuid *ns, const void *data, size_t data_size,
141119
overwrite_uuid_version_and_variant(UUID_V5_VERSION, UUID_V5_VARIANT, out);
142120

143121
exit:
144-
mbedtls_md_free(&ctx);
145-
return ret;
122+
psa_hash_abort(&hash_operation);
123+
124+
return (status == PSA_SUCCESS) ? 0 : -EINVAL;
146125
}
147126
#endif
148127

samples/subsys/uuid/prj.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ CONFIG_UUID_BASE64=y
66
CONFIG_ENTROPY_GENERATOR=y
77

88
CONFIG_MBEDTLS=y
9-
CONFIG_MBEDTLS_MD=y
10-
CONFIG_MBEDTLS_SHA1=y
9+
CONFIG_MBEDTLS_PSA_CRYPTO_C=y
10+
CONFIG_PSA_WANT_ALG_SHA_1=y
1111
CONFIG_BASE64=y
1212

1313
CONFIG_LOG=y

0 commit comments

Comments
 (0)