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