Skip to content

Commit bb39048

Browse files
sorru94kartben
authored andcommitted
uuid: Add UUID utilities
Add UUID generation and parsing utilities compliant with RFC9562. Signed-off-by: Simone Orru <[email protected]>
1 parent d453fb9 commit bb39048

File tree

6 files changed

+484
-0
lines changed

6 files changed

+484
-0
lines changed

include/zephyr/sys/uuid.h

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 2025, SECO Mind Srl
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_SYS_UUID_H_
8+
#define ZEPHYR_INCLUDE_SYS_UUID_H_
9+
10+
/**
11+
* @file
12+
*
13+
* @brief Utility functions for the generation and parsing of Universal Unique Identifier.
14+
* @details This driver is compliant with RFC9562: https://datatracker.ietf.org/doc/rfc9562/
15+
*/
16+
17+
#include <zephyr/kernel.h>
18+
#include <zephyr/types.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
/**
25+
* @defgroup uuid UUID
26+
* @since 4.0
27+
* @version 0.1.0
28+
* @ingroup utilities
29+
* @{
30+
*/
31+
32+
/** @brief Number of bytes in the binary representation of a UUID. */
33+
#define UUID_SIZE 16U
34+
35+
/** @brief Length of the UUID canonical string representation, including the NULL terminator. */
36+
#define UUID_STR_LEN 37U
37+
38+
/** @brief Length of the UUID base64 string representation, including the NULL terminator. */
39+
#define UUID_BASE64_LEN 25U
40+
41+
/**
42+
* @brief Length of the UUID base64 URL and filename safe string representation, including the
43+
* NULL terminator.
44+
*/
45+
#define UUID_BASE64URL_LEN 23U
46+
47+
/** @brief Binary representation of a UUID. */
48+
struct uuid {
49+
/** @cond INTERNAL_HIDDEN */
50+
uint8_t val[UUID_SIZE];
51+
/** @endcond */
52+
};
53+
54+
/**
55+
* @brief Generate a UUIDv4.
56+
*
57+
* @param out The UUID where the result will be written.
58+
*
59+
* @retval 0 The UUID has been correctly generated and stored in @p out
60+
* @retval -EINVAL @p out is not acceptable
61+
*/
62+
int uuid_generate_v4(struct uuid *out);
63+
64+
/**
65+
* @brief Generate a UUIDv5.
66+
*
67+
* @details This function computes a deterministic UUID starting from a namespace UUID and binary
68+
* data.
69+
*
70+
* @param namespace A pointer to an UUID to be used as namespace.
71+
* @param data A pointer to the data that will be hashed to produce the UUID.
72+
* @param data_size The size of the data buffer.
73+
* @param out The UUID where the result will be written.
74+
*
75+
* @retval 0 The UUID has been correctly generated and stored in @p out
76+
* @retval -EINVAL @p out is not acceptable
77+
* @retval -ENOMEM Memory allocation failed
78+
* @retval -ENOTSUP mbedTLS returned an unrecognized error
79+
*/
80+
int uuid_generate_v5(const struct uuid *namespace, const void *data, size_t data_size,
81+
struct uuid *out);
82+
83+
/**
84+
* @brief Copy an UUID into another UUID.
85+
*
86+
* @param data Input data to copy.
87+
* @param out Destination for the copy.
88+
*
89+
* @retval 0 The UUID has been correctly copied in @p dst
90+
* @retval -EINVAL @p dst is not acceptable
91+
*/
92+
int uuid_copy(const struct uuid *data, struct uuid *out);
93+
94+
/**
95+
* @brief Create a uuid_t from a binary (big-endian) formatted UUID.
96+
*
97+
* @param data The buffer where the binary UUID is stored in a big-endian order.
98+
* @param out The UUID where the result will be written.
99+
*
100+
* @retval 0 The UUID has been correctly parsed and stored in @p out
101+
* @retval -EINVAL @p data or @p out are not acceptable
102+
*/
103+
int uuid_from_buffer(const uint8_t data[UUID_SIZE], struct uuid *out);
104+
105+
/**
106+
* @brief Parse a UUID from its canonical (RFC9562) string representation.
107+
*
108+
* @param data A pointer to the string to be parsed.
109+
* @param out The UUID where the result will be written.
110+
*
111+
* @retval 0 The UUID has been correctly parsed and stored in @p out
112+
* @retval -EINVAL @p input or @p out are not acceptable
113+
*/
114+
int uuid_from_string(const char data[UUID_STR_LEN], struct uuid *out);
115+
116+
/**
117+
* @brief Create a uuid_t from a binary (big-endian) formatted UUID.
118+
*
119+
* @param data The input UUID to store in the buffer.
120+
* @param out The buffer where the binary UUID is stored in a big-endian order.
121+
*
122+
* @retval 0 The UUID has been correctly parsed and stored in @p buff
123+
* @retval -EINVAL @p buff is not acceptable
124+
*/
125+
int uuid_to_buffer(const struct uuid *data, uint8_t out[UUID_SIZE]);
126+
127+
/**
128+
* @brief Convert a UUID to its canonical (RFC9562) string representation.
129+
*
130+
* @param data The UUID to convert to string.
131+
* @param out A pointer to a previously allocated buffer where the result will be written.
132+
*
133+
* @retval 0 The UUID has been converted and written in @p out
134+
* @retval -EINVAL @p out is not acceptable
135+
*/
136+
int uuid_to_string(const struct uuid *data, char out[UUID_STR_LEN]);
137+
138+
/**
139+
* @brief Convert a UUID to its base 64 (RFC 3548, RFC 4648) string representation.
140+
*
141+
* @param data The UUID to convert to string.
142+
* @param out A pointer to a previously allocated buffer where the result will be written.
143+
*
144+
* @retval 0 The UUID has been converted and written in @p out
145+
* @retval -EINVAL @p out is not acceptable
146+
*/
147+
int uuid_to_base64(const struct uuid *data, char out[UUID_BASE64_LEN]);
148+
149+
/**
150+
* @brief Convert a UUID to its base 64 (RFC 4648 sec. 5) URL and filename safe string
151+
* representation.
152+
*
153+
* @param data The UUID to convert to string.
154+
* @param out A pointer to a previously allocated buffer where the result will be written.
155+
*
156+
* @retval 0 The UUID has been converted and written in @p out
157+
* @retval -EINVAL @p out is not acceptable
158+
*/
159+
int uuid_to_base64url(const struct uuid *data, char out[UUID_BASE64URL_LEN]);
160+
161+
/**
162+
* @}
163+
*/
164+
165+
#ifdef __cplusplus
166+
}
167+
#endif
168+
169+
#endif /* ZEPHYR_INCLUDE_SYS_UUID_H_ */

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ add_subdirectory(utils)
2020
add_subdirectory_ifdef(CONFIG_SMF smf)
2121
add_subdirectory_ifdef(CONFIG_OPENAMP open-amp)
2222
add_subdirectory_ifdef(CONFIG_ACPI acpi)
23+
add_subdirectory(uuid)

lib/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ source "lib/acpi/Kconfig"
3030
source "lib/runtime/Kconfig"
3131

3232
source "lib/utils/Kconfig"
33+
34+
source "lib/uuid/Kconfig"
3335
endmenu

lib/uuid/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_sources_ifdef(CONFIG_UUID uuid.c)
4+
5+
zephyr_library_link_libraries_ifdef(CONFIG_UUID_V5 mbedTLS)

lib/uuid/Kconfig

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright (c) 2025, SECO Mind Srl
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
menu "Universally Unique Identifier (UUID)"
6+
7+
config UUID
8+
bool "UUID support [EXPERIMENTAL]"
9+
select EXPERIMENTAL
10+
help
11+
Enable use of the UUID library.
12+
13+
config UUID_V4
14+
bool "UUID version 4 generation support [EXPERIMENTAL]"
15+
select EXPERIMENTAL
16+
depends on UUID
17+
depends on ENTROPY_GENERATOR
18+
help
19+
Enable generation of UUID v4.
20+
21+
config UUID_V5
22+
bool "UUID version 5 generation support [EXPERIMENTAL]"
23+
select EXPERIMENTAL
24+
depends on UUID
25+
depends on MBEDTLS
26+
depends on MBEDTLS_MD
27+
depends on MBEDTLS_SHA1
28+
help
29+
Enable generation of UUID v5.
30+
31+
config UUID_BASE64
32+
bool "UUID Base64 support [EXPERIMENTAL]"
33+
select EXPERIMENTAL
34+
depends on UUID
35+
depends on BASE64
36+
help
37+
Enable conversion functions to write UUIDs in base 64
38+
formats.
39+
40+
endmenu

0 commit comments

Comments
 (0)