18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Unified UUID/GUID definition
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2009, 2016 Intel Corp.
68c2ecf20Sopenharmony_ci *	Huang Ying <ying.huang@intel.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/ctype.h>
118c2ecf20Sopenharmony_ci#include <linux/errno.h>
128c2ecf20Sopenharmony_ci#include <linux/export.h>
138c2ecf20Sopenharmony_ci#include <linux/uuid.h>
148c2ecf20Sopenharmony_ci#include <linux/random.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciconst guid_t guid_null;
178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(guid_null);
188c2ecf20Sopenharmony_ciconst uuid_t uuid_null;
198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(uuid_null);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciconst u8 guid_index[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
228c2ecf20Sopenharmony_ciconst u8 uuid_index[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/**
258c2ecf20Sopenharmony_ci * generate_random_uuid - generate a random UUID
268c2ecf20Sopenharmony_ci * @uuid: where to put the generated UUID
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * Random UUID interface
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Used to create a Boot ID or a filesystem UUID/GUID, but can be
318c2ecf20Sopenharmony_ci * useful for other kernel drivers.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_civoid generate_random_uuid(unsigned char uuid[16])
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	get_random_bytes(uuid, 16);
368c2ecf20Sopenharmony_ci	/* Set UUID version to 4 --- truly random generation */
378c2ecf20Sopenharmony_ci	uuid[6] = (uuid[6] & 0x0F) | 0x40;
388c2ecf20Sopenharmony_ci	/* Set the UUID variant to DCE */
398c2ecf20Sopenharmony_ci	uuid[8] = (uuid[8] & 0x3F) | 0x80;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generate_random_uuid);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_civoid generate_random_guid(unsigned char guid[16])
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	get_random_bytes(guid, 16);
468c2ecf20Sopenharmony_ci	/* Set GUID version to 4 --- truly random generation */
478c2ecf20Sopenharmony_ci	guid[7] = (guid[7] & 0x0F) | 0x40;
488c2ecf20Sopenharmony_ci	/* Set the GUID variant to DCE */
498c2ecf20Sopenharmony_ci	guid[8] = (guid[8] & 0x3F) | 0x80;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generate_random_guid);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void __uuid_gen_common(__u8 b[16])
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	prandom_bytes(b, 16);
568c2ecf20Sopenharmony_ci	/* reversion 0b10 */
578c2ecf20Sopenharmony_ci	b[8] = (b[8] & 0x3F) | 0x80;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_civoid guid_gen(guid_t *lu)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	__uuid_gen_common(lu->b);
638c2ecf20Sopenharmony_ci	/* version 4 : random generation */
648c2ecf20Sopenharmony_ci	lu->b[7] = (lu->b[7] & 0x0F) | 0x40;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(guid_gen);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_civoid uuid_gen(uuid_t *bu)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	__uuid_gen_common(bu->b);
718c2ecf20Sopenharmony_ci	/* version 4 : random generation */
728c2ecf20Sopenharmony_ci	bu->b[6] = (bu->b[6] & 0x0F) | 0x40;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(uuid_gen);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/**
778c2ecf20Sopenharmony_ci * uuid_is_valid - checks if a UUID string is valid
788c2ecf20Sopenharmony_ci * @uuid:	UUID string to check
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * Description:
818c2ecf20Sopenharmony_ci * It checks if the UUID string is following the format:
828c2ecf20Sopenharmony_ci *	xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * where x is a hex digit.
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci * Return: true if input is valid UUID string.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cibool uuid_is_valid(const char *uuid)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	unsigned int i;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	for (i = 0; i < UUID_STRING_LEN; i++) {
938c2ecf20Sopenharmony_ci		if (i == 8 || i == 13 || i == 18 || i == 23) {
948c2ecf20Sopenharmony_ci			if (uuid[i] != '-')
958c2ecf20Sopenharmony_ci				return false;
968c2ecf20Sopenharmony_ci		} else if (!isxdigit(uuid[i])) {
978c2ecf20Sopenharmony_ci			return false;
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return true;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(uuid_is_valid);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
1088c2ecf20Sopenharmony_ci	unsigned int i;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (!uuid_is_valid(uuid))
1118c2ecf20Sopenharmony_ci		return -EINVAL;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++) {
1148c2ecf20Sopenharmony_ci		int hi = hex_to_bin(uuid[si[i] + 0]);
1158c2ecf20Sopenharmony_ci		int lo = hex_to_bin(uuid[si[i] + 1]);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		b[ei[i]] = (hi << 4) | lo;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return 0;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciint guid_parse(const char *uuid, guid_t *u)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return __uuid_parse(uuid, u->b, guid_index);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(guid_parse);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciint uuid_parse(const char *uuid, uuid_t *u)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	return __uuid_parse(uuid, u->b, uuid_index);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(uuid_parse);
134