2 * Universally Unique IDentifier (UUID)
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/sha256.h"
15 int uuid_str2bin(const char *str, u8 *bin)
23 if (hexstr2bin(pos, opos, 4))
28 if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
33 if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
38 if (*pos++ != '-' || hexstr2bin(pos, opos, 2))
43 if (*pos++ != '-' || hexstr2bin(pos, opos, 6))
50 int uuid_bin2str(const u8 *bin, char *str, size_t max_len)
53 len = os_snprintf(str, max_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
54 "%02x%02x-%02x%02x%02x%02x%02x%02x",
55 bin[0], bin[1], bin[2], bin[3],
56 bin[4], bin[5], bin[6], bin[7],
57 bin[8], bin[9], bin[10], bin[11],
58 bin[12], bin[13], bin[14], bin[15]);
59 if (os_snprintf_error(max_len, len))
65 int is_nil_uuid(const u8 *uuid)
68 for (i = 0; i < UUID_LEN; i++)
75 int uuid_random(u8 *uuid)
78 u8 hash[SHA256_MAC_LEN];
80 /* Use HMAC-SHA256 and timestamp as context to avoid exposing direct
81 * os_get_random() output in the UUID field. */
83 if (os_get_random(uuid, UUID_LEN) < 0 ||
84 hmac_sha256(uuid, UUID_LEN, (const u8 *) &t, sizeof(t), hash) < 0)
87 os_memcpy(uuid, hash, UUID_LEN);
89 /* Version: 4 = random */
90 uuid[6] = (4 << 4) | (uuid[6] & 0x0f);
92 /* Variant specified in RFC 4122 */
93 uuid[8] = 0x80 | (uuid[8] & 0x3f);