uuid.c revision 189251
1189251Ssam/*
2189251Ssam * Universally Unique IDentifier (UUID)
3189251Ssam * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4189251Ssam *
5189251Ssam * This program is free software; you can redistribute it and/or modify
6189251Ssam * it under the terms of the GNU General Public License version 2 as
7189251Ssam * published by the Free Software Foundation.
8189251Ssam *
9189251Ssam * Alternatively, this software may be distributed under the terms of BSD
10189251Ssam * license.
11189251Ssam *
12189251Ssam * See README and COPYING for more details.
13189251Ssam */
14189251Ssam
15189251Ssam#include "includes.h"
16189251Ssam
17189251Ssam#include "common.h"
18189251Ssam#include "crypto.h"
19189251Ssam#include "sha1.h"
20189251Ssam#include "uuid.h"
21189251Ssam
22189251Ssamint uuid_str2bin(const char *str, u8 *bin)
23189251Ssam{
24189251Ssam	const char *pos;
25189251Ssam	u8 *opos;
26189251Ssam
27189251Ssam	pos = str;
28189251Ssam	opos = bin;
29189251Ssam
30189251Ssam	if (hexstr2bin(pos, opos, 4))
31189251Ssam		return -1;
32189251Ssam	pos += 8;
33189251Ssam	opos += 4;
34189251Ssam
35189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
36189251Ssam		return -1;
37189251Ssam	pos += 4;
38189251Ssam	opos += 2;
39189251Ssam
40189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
41189251Ssam		return -1;
42189251Ssam	pos += 4;
43189251Ssam	opos += 2;
44189251Ssam
45189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
46189251Ssam		return -1;
47189251Ssam	pos += 4;
48189251Ssam	opos += 2;
49189251Ssam
50189251Ssam	if (*pos++ != '-' || hexstr2bin(pos, opos, 6))
51189251Ssam		return -1;
52189251Ssam
53189251Ssam	return 0;
54189251Ssam}
55189251Ssam
56189251Ssam
57189251Ssamint uuid_bin2str(const u8 *bin, char *str, size_t max_len)
58189251Ssam{
59189251Ssam	int len;
60189251Ssam	len = os_snprintf(str, max_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
61189251Ssam			  "%02x%02x-%02x%02x%02x%02x%02x%02x",
62189251Ssam			  bin[0], bin[1], bin[2], bin[3],
63189251Ssam			  bin[4], bin[5], bin[6], bin[7],
64189251Ssam			  bin[8], bin[9], bin[10], bin[11],
65189251Ssam			  bin[12], bin[13], bin[14], bin[15]);
66189251Ssam	if (len < 0 || (size_t) len >= max_len)
67189251Ssam		return -1;
68189251Ssam	return 0;
69189251Ssam}
70189251Ssam
71189251Ssam
72189251Ssamint is_nil_uuid(const u8 *uuid)
73189251Ssam{
74189251Ssam	int i;
75189251Ssam	for (i = 0; i < UUID_LEN; i++)
76189251Ssam		if (uuid[i])
77189251Ssam			return 0;
78189251Ssam	return 1;
79189251Ssam}
80189251Ssam
81189251Ssam
82189251Ssamvoid uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
83189251Ssam{
84189251Ssam	const u8 *addr[2];
85189251Ssam	size_t len[2];
86189251Ssam	u8 hash[SHA1_MAC_LEN];
87189251Ssam	u8 nsid[16] = {
88189251Ssam		0x52, 0x64, 0x80, 0xf8,
89189251Ssam		0xc9, 0x9b,
90189251Ssam		0x4b, 0xe5,
91189251Ssam		0xa6, 0x55,
92189251Ssam		0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
93189251Ssam	};
94189251Ssam
95189251Ssam	addr[0] = nsid;
96189251Ssam	len[0] = sizeof(nsid);
97189251Ssam	addr[1] = mac_addr;
98189251Ssam	len[1] = 6;
99189251Ssam	sha1_vector(2, addr, len, hash);
100189251Ssam	os_memcpy(uuid, hash, 16);
101189251Ssam
102189251Ssam	/* Version: 5 = named-based version using SHA-1 */
103189251Ssam	uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
104189251Ssam
105189251Ssam	/* Variant specified in RFC 4122 */
106189251Ssam	uuid[8] = 0x80 | (uuid[8] & 0x3f);
107189251Ssam}
108