xref: /linux/lib/uuid.c (revision 24c776355f4097316a763005434ffff716aa21a8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Unified UUID/GUID definition
4  *
5  * Copyright (C) 2009, 2016 Intel Corp.
6  *	Huang Ying <ying.huang@intel.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/ctype.h>
11 #include <linux/errno.h>
12 #include <linux/export.h>
13 #include <linux/hex.h>
14 #include <linux/uuid.h>
15 #include <linux/random.h>
16 
17 const guid_t guid_null;
18 EXPORT_SYMBOL(guid_null);
19 const uuid_t uuid_null;
20 EXPORT_SYMBOL(uuid_null);
21 
22 const u8 guid_index[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
23 const u8 uuid_index[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
24 
25 /**
26  * generate_random_uuid - generate a random UUID
27  * @uuid: where to put the generated UUID
28  *
29  * Random UUID interface
30  *
31  * Used to create a Boot ID or a filesystem UUID/GUID, but can be
32  * useful for other kernel drivers.
33  */
34 void generate_random_uuid(unsigned char uuid[16])
35 {
36 	get_random_bytes(uuid, 16);
37 	/* Set UUID version to 4 --- truly random generation */
38 	uuid[6] = (uuid[6] & 0x0F) | 0x40;
39 	/* Set the UUID variant to DCE */
40 	uuid[8] = (uuid[8] & 0x3F) | 0x80;
41 }
42 EXPORT_SYMBOL(generate_random_uuid);
43 
44 void generate_random_guid(unsigned char guid[16])
45 {
46 	get_random_bytes(guid, 16);
47 	/* Set GUID version to 4 --- truly random generation */
48 	guid[7] = (guid[7] & 0x0F) | 0x40;
49 	/* Set the GUID variant to DCE */
50 	guid[8] = (guid[8] & 0x3F) | 0x80;
51 }
52 EXPORT_SYMBOL(generate_random_guid);
53 
54 static void __uuid_gen_common(__u8 b[16])
55 {
56 	get_random_bytes(b, 16);
57 	/* reversion 0b10 */
58 	b[8] = (b[8] & 0x3F) | 0x80;
59 }
60 
61 void guid_gen(guid_t *lu)
62 {
63 	__uuid_gen_common(lu->b);
64 	/* version 4 : random generation */
65 	lu->b[7] = (lu->b[7] & 0x0F) | 0x40;
66 }
67 EXPORT_SYMBOL_GPL(guid_gen);
68 
69 void uuid_gen(uuid_t *bu)
70 {
71 	__uuid_gen_common(bu->b);
72 	/* version 4 : random generation */
73 	bu->b[6] = (bu->b[6] & 0x0F) | 0x40;
74 }
75 EXPORT_SYMBOL_GPL(uuid_gen);
76 
77 /**
78  * uuid_is_valid - checks if a UUID string is valid
79  * @uuid:	UUID string to check
80  *
81  * Description:
82  * It checks if the UUID string is following the format:
83  *	xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
84  *
85  * where x is a hex digit.
86  *
87  * Return: true if input is valid UUID string.
88  */
89 bool uuid_is_valid(const char *uuid)
90 {
91 	unsigned int i;
92 
93 	for (i = 0; i < UUID_STRING_LEN; i++) {
94 		if (i == 8 || i == 13 || i == 18 || i == 23) {
95 			if (uuid[i] != '-')
96 				return false;
97 		} else if (!isxdigit(uuid[i])) {
98 			return false;
99 		}
100 	}
101 
102 	return true;
103 }
104 EXPORT_SYMBOL(uuid_is_valid);
105 
106 static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])
107 {
108 	static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
109 	unsigned int i;
110 
111 	if (!uuid_is_valid(uuid))
112 		return -EINVAL;
113 
114 	for (i = 0; i < 16; i++) {
115 		int hi = hex_to_bin(uuid[si[i] + 0]);
116 		int lo = hex_to_bin(uuid[si[i] + 1]);
117 
118 		b[ei[i]] = (hi << 4) | lo;
119 	}
120 
121 	return 0;
122 }
123 
124 int guid_parse(const char *uuid, guid_t *u)
125 {
126 	return __uuid_parse(uuid, u->b, guid_index);
127 }
128 EXPORT_SYMBOL(guid_parse);
129 
130 int uuid_parse(const char *uuid, uuid_t *u)
131 {
132 	return __uuid_parse(uuid, u->b, uuid_index);
133 }
134 EXPORT_SYMBOL(uuid_parse);
135