139beb93cSSam Leffler /*
239beb93cSSam Leffler * Universally Unique IDentifier (UUID)
339beb93cSSam Leffler * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
739beb93cSSam Leffler */
839beb93cSSam Leffler
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler
1139beb93cSSam Leffler #include "common.h"
12*85732ac8SCy Schubert #include "crypto/sha256.h"
1339beb93cSSam Leffler #include "uuid.h"
1439beb93cSSam Leffler
uuid_str2bin(const char * str,u8 * bin)1539beb93cSSam Leffler int uuid_str2bin(const char *str, u8 *bin)
1639beb93cSSam Leffler {
1739beb93cSSam Leffler const char *pos;
1839beb93cSSam Leffler u8 *opos;
1939beb93cSSam Leffler
2039beb93cSSam Leffler pos = str;
2139beb93cSSam Leffler opos = bin;
2239beb93cSSam Leffler
2339beb93cSSam Leffler if (hexstr2bin(pos, opos, 4))
2439beb93cSSam Leffler return -1;
2539beb93cSSam Leffler pos += 8;
2639beb93cSSam Leffler opos += 4;
2739beb93cSSam Leffler
2839beb93cSSam Leffler if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
2939beb93cSSam Leffler return -1;
3039beb93cSSam Leffler pos += 4;
3139beb93cSSam Leffler opos += 2;
3239beb93cSSam Leffler
3339beb93cSSam Leffler if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
3439beb93cSSam Leffler return -1;
3539beb93cSSam Leffler pos += 4;
3639beb93cSSam Leffler opos += 2;
3739beb93cSSam Leffler
3839beb93cSSam Leffler if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
3939beb93cSSam Leffler return -1;
4039beb93cSSam Leffler pos += 4;
4139beb93cSSam Leffler opos += 2;
4239beb93cSSam Leffler
4339beb93cSSam Leffler if (*pos++ != '-' || hexstr2bin(pos, opos, 6))
4439beb93cSSam Leffler return -1;
4539beb93cSSam Leffler
4639beb93cSSam Leffler return 0;
4739beb93cSSam Leffler }
4839beb93cSSam Leffler
4939beb93cSSam Leffler
uuid_bin2str(const u8 * bin,char * str,size_t max_len)5039beb93cSSam Leffler int uuid_bin2str(const u8 *bin, char *str, size_t max_len)
5139beb93cSSam Leffler {
5239beb93cSSam Leffler int len;
5339beb93cSSam Leffler len = os_snprintf(str, max_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
5439beb93cSSam Leffler "%02x%02x-%02x%02x%02x%02x%02x%02x",
5539beb93cSSam Leffler bin[0], bin[1], bin[2], bin[3],
5639beb93cSSam Leffler bin[4], bin[5], bin[6], bin[7],
5739beb93cSSam Leffler bin[8], bin[9], bin[10], bin[11],
5839beb93cSSam Leffler bin[12], bin[13], bin[14], bin[15]);
595b9c547cSRui Paulo if (os_snprintf_error(max_len, len))
6039beb93cSSam Leffler return -1;
6139beb93cSSam Leffler return 0;
6239beb93cSSam Leffler }
6339beb93cSSam Leffler
6439beb93cSSam Leffler
is_nil_uuid(const u8 * uuid)6539beb93cSSam Leffler int is_nil_uuid(const u8 *uuid)
6639beb93cSSam Leffler {
6739beb93cSSam Leffler int i;
6839beb93cSSam Leffler for (i = 0; i < UUID_LEN; i++)
6939beb93cSSam Leffler if (uuid[i])
7039beb93cSSam Leffler return 0;
7139beb93cSSam Leffler return 1;
7239beb93cSSam Leffler }
73*85732ac8SCy Schubert
74*85732ac8SCy Schubert
uuid_random(u8 * uuid)75*85732ac8SCy Schubert int uuid_random(u8 *uuid)
76*85732ac8SCy Schubert {
77*85732ac8SCy Schubert struct os_time t;
78*85732ac8SCy Schubert u8 hash[SHA256_MAC_LEN];
79*85732ac8SCy Schubert
80*85732ac8SCy Schubert /* Use HMAC-SHA256 and timestamp as context to avoid exposing direct
81*85732ac8SCy Schubert * os_get_random() output in the UUID field. */
82*85732ac8SCy Schubert os_get_time(&t);
83*85732ac8SCy Schubert if (os_get_random(uuid, UUID_LEN) < 0 ||
84*85732ac8SCy Schubert hmac_sha256(uuid, UUID_LEN, (const u8 *) &t, sizeof(t), hash) < 0)
85*85732ac8SCy Schubert return -1;
86*85732ac8SCy Schubert
87*85732ac8SCy Schubert os_memcpy(uuid, hash, UUID_LEN);
88*85732ac8SCy Schubert
89*85732ac8SCy Schubert /* Version: 4 = random */
90*85732ac8SCy Schubert uuid[6] = (4 << 4) | (uuid[6] & 0x0f);
91*85732ac8SCy Schubert
92*85732ac8SCy Schubert /* Variant specified in RFC 4122 */
93*85732ac8SCy Schubert uuid[8] = 0x80 | (uuid[8] & 0x3f);
94*85732ac8SCy Schubert
95*85732ac8SCy Schubert return 0;
96*85732ac8SCy Schubert }
97