|
15 | 15 | #endif |
16 | 16 |
|
17 | 17 | #if defined(CONFIG_UUID_V5) |
18 | | -#include <mbedtls/md.h> |
| 18 | +#include <psa/crypto.h> |
19 | 19 | #endif |
20 | 20 |
|
21 | 21 | #if defined(CONFIG_UUID_BASE64) |
@@ -82,67 +82,53 @@ int uuid_generate_v4(struct uuid *out) |
82 | 82 | int uuid_generate_v5(const struct uuid *ns, const void *data, size_t data_size, |
83 | 83 | struct uuid *out) |
84 | 84 | { |
| 85 | + psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT; |
| 86 | + size_t sha_len; |
| 87 | + psa_status_t status; |
| 88 | + |
85 | 89 | if (out == NULL) { |
86 | 90 | return -EINVAL; |
87 | 91 | } |
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) { |
115 | 95 | goto exit; |
116 | 96 | } |
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) { |
121 | 100 | goto exit; |
122 | 101 | } |
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) { |
127 | 105 | goto exit; |
128 | 106 | } |
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) { |
133 | 110 | goto exit; |
134 | 111 | } |
135 | 112 |
|
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 | | - } |
140 | 113 | /* Update version and variant */ |
141 | 114 | overwrite_uuid_version_and_variant(UUID_V5_VERSION, UUID_V5_VARIANT, out); |
142 | 115 |
|
143 | 116 | 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 | + } |
146 | 132 | } |
147 | 133 | #endif |
148 | 134 |
|
|
0 commit comments