Skip to content

Commit 0427905

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 0427905

File tree

4 files changed

+38
-51
lines changed

4 files changed

+38
-51
lines changed

include/zephyr/sys/uuid.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ int uuid_generate_v4(struct uuid *out);
7575
* @retval 0 The UUID has been correctly generated and stored in @p out
7676
* @retval -EINVAL @p out is not acceptable
7777
* @retval -ENOMEM Memory allocation failed
78-
* @retval -ENOTSUP mbedTLS returned an unrecognized error
78+
* @retval -ENOTSUP Mbed TLS returned an unrecognized error
79+
* @retval -EIO Generic error
7980
*/
8081
int uuid_generate_v5(const struct uuid *ns, const void *data, size_t data_size,
8182
struct uuid *out);

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: 32 additions & 46 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,67 +82,53 @@ 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+
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
86+
size_t sha_len;
87+
psa_status_t status;
88+
8589
if (out == NULL) {
8690
return -EINVAL;
8791
}
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;
92+
93+
status = psa_hash_setup(&hash_operation, PSA_ALG_SHA_1);
94+
if (status != PSA_SUCCESS) {
11595
goto exit;
11696
}
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;
97+
98+
status = psa_hash_update(&hash_operation, ns->val, UUID_SIZE);
99+
if (status != PSA_SUCCESS) {
121100
goto exit;
122101
}
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;
102+
103+
status = psa_hash_update(&hash_operation, data, data_size);
104+
if (status != PSA_SUCCESS) {
127105
goto exit;
128106
}
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;
107+
108+
status = psa_hash_finish(&hash_operation, out->val, UUID_SIZE, &sha_len);
109+
if (status != PSA_SUCCESS) {
133110
goto exit;
134111
}
135112

136-
/* Store the computed SHA1 in the out struct */
137-
for (unsigned int i = 0; i < UUID_SIZE; i++) {
138-
out->val[i] = sha_result[i];
139-
}
140113
/* Update version and variant */
141114
overwrite_uuid_version_and_variant(UUID_V5_VERSION, UUID_V5_VARIANT, out);
142115

143116
exit:
144-
mbedtls_md_free(&ctx);
145-
return ret;
117+
psa_hash_abort(&hash_operation);
118+
119+
switch (status) {
120+
case PSA_SUCCESS:
121+
return 0;
122+
case PSA_ERROR_INSUFFICIENT_MEMORY:
123+
case PSA_ERROR_BUFFER_TOO_SMALL:
124+
return -ENOMEM;
125+
case PSA_ERROR_INVALID_ARGUMENT:
126+
return -EINVAL;
127+
case PSA_ERROR_NOT_SUPPORTED:
128+
return -ENOTSUP;
129+
default:
130+
return -EIO;
131+
}
146132
}
147133
#endif
148134

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)