fe1ca8
From f942ba2c4c14b6bf7720e8316afe1971231553bc Mon Sep 17 00:00:00 2001
fe1ca8
From: Karel Zak <kzak@redhat.com>
fe1ca8
Date: Fri, 31 Aug 2018 12:27:32 +0200
fe1ca8
Subject: [PATCH 7/8] libuuid: fix name-based UUIDs
fe1ca8
fe1ca8
The current version is not fully compatible with RFC4122. It
fe1ca8
incorrectly encodes UUID variant
fe1ca8
fe1ca8
	xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
fe1ca8
fe1ca8
where M is UUID version and N is UUID variant.
fe1ca8
fe1ca8
 $ python -c "import uuid ; print(uuid.uuid5(uuid.UUID(int=0), 'foo'))"
fe1ca8
 aa752cea-8222-5bc8-acd9-555b090c0ccb
fe1ca8
                    ^^
fe1ca8
fe1ca8
Old version:
fe1ca8
fe1ca8
 $ uuidgen --namespace 00000000-0000-0000-0000-000000000000 --name 'foo' --sha1
fe1ca8
 aa752cea-8222-5bc8-8cd9-555b090c0ccb
fe1ca8
                    ^^
fe1ca8
fe1ca8
Fixed version:
fe1ca8
 ./uuidgen --namespace 00000000-0000-0000-0000-000000000000 --name 'foo' --sha1;
fe1ca8
 aa752cea-8222-5bc8-acd9-555b090c0ccb
fe1ca8
                    ^^
fe1ca8
fe1ca8
The patch uses uuid_unpack and uuid_pack. It makes code more readable
fe1ca8
and allow to access proper octens. The same way we already use for
fe1ca8
time and random based UUIDs.
fe1ca8
fe1ca8
Upstream: http://github.com/karelzak/util-linux/commit/d6ddf07d31dfdc894eb8e7e6842aa856342c526e
fe1ca8
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1624877
fe1ca8
Addresses: https://github.com/karelzak/util-linux/issues/683
fe1ca8
Signed-off-by: Karel Zak <kzak@redhat.com>
fe1ca8
---
fe1ca8
 libuuid/src/gen_uuid.c | 34 ++++++++++++++++------------------
fe1ca8
 1 file changed, 16 insertions(+), 18 deletions(-)
fe1ca8
fe1ca8
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
fe1ca8
index a374e75c9..27c135db5 100644
fe1ca8
--- a/libuuid/src/gen_uuid.c
fe1ca8
+++ b/libuuid/src/gen_uuid.c
fe1ca8
@@ -96,9 +96,6 @@
fe1ca8
 #define THREAD_LOCAL static
fe1ca8
 #endif
fe1ca8
 
fe1ca8
-/* index with UUID_VARIANT_xxx and shift 5 bits */
fe1ca8
-static unsigned char variant_bits[] = { 0x00, 0x04, 0x06, 0x07 };
fe1ca8
-
fe1ca8
 #ifdef _WIN32
fe1ca8
 static void gettimeofday (struct timeval *tv, void *dummy)
fe1ca8
 {
fe1ca8
@@ -566,21 +563,22 @@ void uuid_generate_md5(uuid_t out, const uuid_t ns, const char *name, size_t len
fe1ca8
 {
fe1ca8
 	UL_MD5_CTX ctx;
fe1ca8
 	char hash[UL_MD5LENGTH];
fe1ca8
+	uuid_t buf;
fe1ca8
+	struct uuid uu;
fe1ca8
 
fe1ca8
 	ul_MD5Init(&ctx;;
fe1ca8
-	/* hash concatenation of well-known UUID with name */
fe1ca8
 	ul_MD5Update(&ctx, ns, sizeof(uuid_t));
fe1ca8
 	ul_MD5Update(&ctx, (const unsigned char *)name, len);
fe1ca8
-
fe1ca8
 	ul_MD5Final((unsigned char *)hash, &ctx;;
fe1ca8
 
fe1ca8
-	memcpy(out, hash, sizeof(uuid_t));
fe1ca8
+	assert(sizeof(buf) <= sizeof(hash));
fe1ca8
 
fe1ca8
-	out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT);
fe1ca8
-	out[6] |= (UUID_TYPE_DCE_MD5 << UUID_TYPE_SHIFT);
fe1ca8
+	memcpy(buf, hash, sizeof(buf));
fe1ca8
+	uuid_unpack(buf, &uu);
fe1ca8
 
fe1ca8
-	out[8] &= ~(UUID_VARIANT_MASK << UUID_VARIANT_SHIFT);
fe1ca8
-	out[8] |= (variant_bits[UUID_VARIANT_DCE] << UUID_VARIANT_SHIFT);
fe1ca8
+	uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
fe1ca8
+	uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x3000;
fe1ca8
+	uuid_pack(&uu, out);
fe1ca8
 }
fe1ca8
 
fe1ca8
 /*
fe1ca8
@@ -591,20 +589,20 @@ void uuid_generate_sha1(uuid_t out, const uuid_t ns, const char *name, size_t le
fe1ca8
 {
fe1ca8
 	UL_SHA1_CTX ctx;
fe1ca8
 	char hash[UL_SHA1LENGTH];
fe1ca8
+	uuid_t buf;
fe1ca8
+	struct uuid uu;
fe1ca8
 
fe1ca8
 	ul_SHA1Init(&ctx;;
fe1ca8
-	/* hash concatenation of well-known UUID with name */
fe1ca8
 	ul_SHA1Update(&ctx, ns, sizeof(uuid_t));
fe1ca8
 	ul_SHA1Update(&ctx, (const unsigned char *)name, len);
fe1ca8
-
fe1ca8
 	ul_SHA1Final((unsigned char *)hash, &ctx;;
fe1ca8
 
fe1ca8
-	memcpy(out, hash, sizeof(uuid_t));
fe1ca8
+	assert(sizeof(buf) <= sizeof(hash));
fe1ca8
 
fe1ca8
-	out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT);
fe1ca8
-	out[6] |= (UUID_TYPE_DCE_SHA1 << UUID_TYPE_SHIFT);
fe1ca8
+	memcpy(buf, hash, sizeof(buf));
fe1ca8
+	uuid_unpack(buf, &uu);
fe1ca8
 
fe1ca8
-	out[8] &= ~(UUID_VARIANT_MASK << UUID_VARIANT_SHIFT);
fe1ca8
-	out[8] |= (variant_bits[UUID_VARIANT_DCE] << UUID_VARIANT_SHIFT);
fe1ca8
+	uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
fe1ca8
+	uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x5000;
fe1ca8
+	uuid_pack(&uu, out);
fe1ca8
 }
fe1ca8
-
fe1ca8
-- 
fe1ca8
2.14.4
fe1ca8