libuuid: add support for hash-based UUIDs

Adding V3 and V5 UUIDs per RFC-4122.

[kzak@redhat.com: - fix symbols file]

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Philip Prindeville 2017-09-05 11:19:26 +02:00 committed by Karel Zak
parent 58d57ae2d8
commit 0047626887
18 changed files with 666 additions and 3 deletions

View file

@ -24,8 +24,11 @@ libuuid_la_SOURCES = \
libuuid/src/uuidd.h \
libuuid/src/uuidP.h \
libuuid/src/uuid_time.c \
libuuid/src/predefined.c \
$(uuidinc_HEADERS) \
lib/randutils.c
lib/randutils.c \
lib/md5.c \
lib/sha1.c
libuuid_la_DEPENDENCIES = libuuid/src/libuuid.sym
libuuid_la_LIBADD = $(SOCKET_LIBS)

View file

@ -87,6 +87,8 @@
#include "randutils.h"
#include "strutils.h"
#include "c.h"
#include "md5.h"
#include "sha1.h"
#ifdef HAVE_TLS
#define THREAD_LOCAL static __thread
@ -94,6 +96,9 @@
#define THREAD_LOCAL static
#endif
/* index with UUID_VARIANT_xxx and shift 5 bits */
static unsigned char variant_bits[] = { 0x00, 0x04, 0x06, 0x07 };
#ifdef _WIN32
static void gettimeofday (struct timeval *tv, void *dummy)
{
@ -552,3 +557,54 @@ void uuid_generate(uuid_t out)
else
uuid_generate_time(out);
}
/*
* Generate an MD5 hashed (predictable) UUID based on a well-known UUID
* providing the namespace and an arbitrary binary string.
*/
void uuid_generate_md5(uuid_t out, const uuid_t ns, const char *name, size_t len)
{
MD5_CTX ctx;
char hash[MD5LENGTH];
MD5Init(&ctx);
/* hash concatenation of well-known UUID with name */
MD5Update(&ctx, ns, sizeof(uuid_t));
MD5Update(&ctx, (const unsigned char *)name, len);
MD5Final((unsigned char *)hash, &ctx);
memcpy(out, hash, sizeof(uuid_t));
out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT);
out[6] |= (UUID_TYPE_DCE_MD5 << UUID_TYPE_SHIFT);
out[8] &= ~(UUID_VARIANT_MASK << UUID_VARIANT_SHIFT);
out[8] |= (variant_bits[UUID_VARIANT_DCE] << UUID_VARIANT_SHIFT);
}
/*
* Generate a SHA1 hashed (predictable) UUID based on a well-known UUID
* providing the namespace and an arbitrary binary string.
*/
void uuid_generate_sha1(uuid_t out, const uuid_t ns, const char *name, size_t len)
{
SHA1_CTX ctx;
char hash[SHA1LENGTH];
SHA1Init(&ctx);
/* hash concatenation of well-known UUID with name */
SHA1Update(&ctx, ns, sizeof(uuid_t));
SHA1Update(&ctx, (const unsigned char *)name, len);
SHA1Final((unsigned char *)hash, &ctx);
memcpy(out, hash, sizeof(uuid_t));
out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT);
out[6] |= (UUID_TYPE_DCE_SHA1 << UUID_TYPE_SHIFT);
out[8] &= ~(UUID_VARIANT_MASK << UUID_VARIANT_SHIFT);
out[8] |= (variant_bits[UUID_VARIANT_DCE] << UUID_VARIANT_SHIFT);
}

View file

@ -6,7 +6,7 @@
* The original libuuid from e2fsprogs (<=1.41.5) does not to use
* symbol versioning -- all the original symbols are in UUID_1.0 now.
*
* Copyright (C) 2011-2014 Karel Zak <kzak@redhat.com>
* Copyright (C) 2011-2017 Kareil Zak <kzak@redhat.com>
*/
UUID_1.0 {
global:
@ -34,6 +34,15 @@ global:
uuid_generate_time_safe;
} UUID_1.0;
/*
* version(s) since util-linux.2.31
*/
UUID_2.31 {
global:
uuid_generate_md5;
uuid_generate_sha1;
uuid_get_template;
} UUID_2.20;
/*
* __uuid_* this is not part of the official API, this is

80
libuuid/src/predefined.c Normal file
View file

@ -0,0 +1,80 @@
/*
* predefined.c --- well-known UUIDs from the RFC-4122 namespace
*
* Copyright (C) 2017 Philip Prindeville
*
* %Begin-Header%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
* %End-Header%
*/
#include <string.h>
#include "uuid.h"
/*
* These are time-based UUIDs that are well-known in that they've
* been canonized as part of RFC-4122, Appendex C. They are to
* be used as the namespace (ns) argument to the uuid_generate_md5()
* and uuid_generate_sha1() functions.
*
* See Section 4.3 for the particulars of how namespace UUIDs
* are combined with seed values to generate new UUIDs.
*/
UUID_DEFINE(NameSpace_DNS,
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
UUID_DEFINE(NameSpace_URL,
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
UUID_DEFINE(NameSpace_OID,
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
UUID_DEFINE(NameSpace_X500,
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8);
const uuid_t *uuid_get_template(const char *alias)
{
if (!alias || !*alias)
return NULL;
if (!strcmp(alias, "dns"))
return &NameSpace_DNS;
else if (!strcmp(alias, "url"))
return &NameSpace_URL;
else if (!strcmp(alias, "oid"))
return &NameSpace_OID;
else if (!strcmp(alias, "x500") || !strcmp(alias, "x.500"))
return &NameSpace_X500;
else
return NULL;
}

View file

@ -49,9 +49,18 @@ typedef unsigned char uuid_t[16];
#define UUID_VARIANT_MICROSOFT 2
#define UUID_VARIANT_OTHER 3
#define UUID_VARIANT_SHIFT 5
#define UUID_VARIANT_MASK 0x7
/* UUID Type definitions */
#define UUID_TYPE_DCE_TIME 1
#define UUID_TYPE_DCE_SECURITY 2
#define UUID_TYPE_DCE_MD5 3
#define UUID_TYPE_DCE_RANDOM 4
#define UUID_TYPE_DCE_SHA1 5
#define UUID_TYPE_SHIFT 4
#define UUID_TYPE_MASK 0xf
/* Allow UUID constants to be defined */
#ifdef __GNUC__
@ -81,6 +90,9 @@ extern void uuid_generate_random(uuid_t out);
extern void uuid_generate_time(uuid_t out);
extern int uuid_generate_time_safe(uuid_t out);
extern void uuid_generate_md5(uuid_t out, const uuid_t ns, const char *name, size_t len);
extern void uuid_generate_sha1(uuid_t out, const uuid_t ns, const char *name, size_t len);
/* isnull.c */
extern int uuid_is_null(const uuid_t uu);
@ -97,6 +109,9 @@ extern time_t uuid_time(const uuid_t uu, struct timeval *ret_tv);
extern int uuid_type(const uuid_t uu);
extern int uuid_variant(const uuid_t uu);
/* predefined.c */
extern const uuid_t *uuid_get_template(const char *alias);
#ifdef __cplusplus
}
#endif