Skip to content

Conversation

valeriosetti
Copy link
Contributor

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.

Copy link
Contributor

@tomi-font tomi-font left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to update tests/lib/uuid

lib/uuid/uuid.c Outdated
struct uuid *out)
{
uint8_t sha_result[PSA_HASH_LENGTH(PSA_ALG_SHA_1)];
size_t sha_len = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
size_t sha_len = 0;
size_t sha_len;

lib/uuid/uuid.c Outdated
return ret;
psa_hash_abort(&hash_operation);

return (status == PSA_SUCCESS) ? 0 : -EINVAL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return something else than EINVAL? Also have a look at (and maybe update) the documentation for this function in include/zephyr/sys/uuid.h.

int uuid_generate_v5(const struct uuid *ns, const void *data, size_t data_size,
struct uuid *out)
{
uint8_t sha_result[PSA_HASH_LENGTH(PSA_ALG_SHA_1)];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we directly use out->val instead of having yet another buffer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and it turned out that sample.subsys.uuid test started to fail due to the fact that out->val is too small in size to contain a SHA1 result. out->val is UUID_SIZE, i.e. 16 bytes while SHA-1 is 20 bytes.
This is also explained in the official documentation for UUID.

I will revert this change

@valeriosetti valeriosetti force-pushed the uuid-remove-legacy-crypto branch from 8f8076d to 23d05dc Compare October 14, 2025 22:17
@zephyrbot zephyrbot added the area: Base OS Base OS Library (lib/os) label Oct 14, 2025
@valeriosetti valeriosetti force-pushed the uuid-remove-legacy-crypto branch 2 times, most recently from 0427905 to 1cc7324 Compare October 15, 2025 09:20
@zephyrbot zephyrbot added the area: Tests Issues related to a particular existing or missing test label Oct 15, 2025
Copy link

@@ -75,7 +75,8 @@ int uuid_generate_v4(struct uuid *out);
* @retval 0 The UUID has been correctly generated and stored in @p out
* @retval -EINVAL @p out is not acceptable
* @retval -ENOMEM Memory allocation failed
* @retval -ENOTSUP mbedTLS returned an unrecognized error
* @retval -ENOTSUP Mbed TLS returned an unrecognized error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSA Crypto? 🙃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you are returning -ENOTSUP only if you receive PSA_ERROR_NOT_SUPPORTED. This is not quite aligned.
IMO we could just return a generic error code. Trying to translate between PSA Crypto and libc error codes is... not easy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you proposing to use a catch-all error? Which one would be a good candidate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are returning -ENOTSUP only if you receive PSA_ERROR_NOT_SUPPORTED

According to the PSA specs PSA_ERROR_NOT_SUPPORTED is returned by psa_hash_setup is the alg is not supported and this can happen. To me the translation to -ENOTSUP is reasonable. What would you suggest instead?

lib/uuid/uuid.c Outdated
case PSA_SUCCESS:
return 0;
case PSA_ERROR_INSUFFICIENT_MEMORY:
case PSA_ERROR_BUFFER_TOO_SMALL:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one doesn't seem accurate or even relevant to have?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the output buffer is on the stack, so I agree this can never happen. Thanks

lib/uuid/uuid.c Outdated
Comment on lines 130 to 131
case PSA_ERROR_INVALID_ARGUMENT:
return -EINVAL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not expecting this either? just thinking about code size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, according to the documentation this can only be returned by psa_hash_init if the algorithm is invalid, but this is not possible since it's hardcoded. I will remove it, thanks

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]>
@valeriosetti valeriosetti force-pushed the uuid-remove-legacy-crypto branch from 1cc7324 to f90c906 Compare October 16, 2025 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Base OS Base OS Library (lib/os) area: Samples Samples area: Tests Issues related to a particular existing or missing test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants