summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2024-02-19 09:31:33 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2024-02-20 11:46:30 +0100
commit1428b9259ed5b6c83e68e073e711ea248d67adf1 (patch)
treeeb82709c2319ecb9442ada8a8115a40b43e222aa
parent081561b017ddadbef973acb3b34b9589f4f1e12d (diff)
downloadbarebox-1428b9259ed5.tar.gz
barebox-1428b9259ed5.tar.xz
uuid: implement random uuid/guid
Link: https://lore.barebox.org/20240219083140.2713047-6-s.hauer@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--lib/uuid.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/uuid.c b/lib/uuid.c
index db75be1725..60a651e330 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -8,9 +8,39 @@
#include <linux/uuid.h>
#include <module.h>
+#include <stdlib.h>
#include <linux/export.h>
const guid_t guid_null;
EXPORT_SYMBOL(guid_null);
const uuid_t uuid_null;
EXPORT_SYMBOL(uuid_null);
+
+/**
+ * generate_random_uuid - generate a random UUID
+ * @uuid: where to put the generated UUID
+ *
+ * Random UUID interface
+ *
+ * Used to create a Boot ID or a filesystem UUID/GUID, but can be
+ * useful for other kernel drivers.
+ */
+void generate_random_uuid(unsigned char uuid[16])
+{
+ get_random_bytes(uuid, 16);
+ /* Set UUID version to 4 --- truly random generation */
+ uuid[6] = (uuid[6] & 0x0F) | 0x40;
+ /* Set the UUID variant to DCE */
+ uuid[8] = (uuid[8] & 0x3F) | 0x80;
+}
+EXPORT_SYMBOL(generate_random_uuid);
+
+void generate_random_guid(unsigned char guid[16])
+{
+ get_random_bytes(guid, 16);
+ /* Set GUID version to 4 --- truly random generation */
+ guid[7] = (guid[7] & 0x0F) | 0x40;
+ /* Set the GUID variant to DCE */
+ guid[8] = (guid[8] & 0x3F) | 0x80;
+}
+EXPORT_SYMBOL(generate_random_guid);