Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: kern_uuid.c,v 1.21 2026/01/04 02:09:29 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Marcel Moolenaar
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  * $FreeBSD: /repoman/r/ncvs/src/sys/kern/kern_uuid.c,v 1.7 2004/01/12 13:34:11 rse Exp $
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: kern_uuid.c,v 1.21 2026/01/04 02:09:29 riastradh Exp $");
     33 
     34 #include <sys/param.h>
     35 
     36 #include <sys/cprng.h>
     37 #include <sys/endian.h>
     38 #include <sys/sdt.h>
     39 #include <sys/syscallargs.h>
     40 #include <sys/systm.h>
     41 #include <sys/uuid.h>
     42 
     43 /*
     44  * See also:
     45  *	http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
     46  *	http://www.opengroup.org/onlinepubs/009629399/apdxa.htm
     47  */
     48 
     49 CTASSERT(sizeof(struct uuid) == 16);
     50 
     51 static void
     52 uuid_generate(struct uuid *uuid)
     53 {
     54 
     55 	/* Randomly generate the content.  */
     56 	cprng_fast(uuid, sizeof(*uuid));
     57 
     58 	/* Set the version number to 4.  */
     59 	uuid->time_hi_and_version &= ~(uint32_t)0xf000;
     60 	uuid->time_hi_and_version |= 0x4000;
     61 
     62 	/* Fix the reserved bits.  */
     63 	uuid->clock_seq_hi_and_reserved &= ~(uint8_t)0x40;
     64 	uuid->clock_seq_hi_and_reserved |= 0x80;
     65 }
     66 
     67 int
     68 sys_uuidgen(struct lwp *l, const struct sys_uuidgen_args *uap, register_t *retval)
     69 {
     70 	struct uuid *store, tmp;
     71 	unsigned count;
     72 	int error;
     73 
     74 	/*
     75 	 * Limit the number of UUIDs that can be created at the same time
     76 	 * to some arbitrary number. This isn't really necessary, but I
     77 	 * like to have some sort of upper-bound that's less than 2G :-)
     78 	 * XXX needs to be tunable.
     79 	 */
     80 	if (SCARG(uap,count) < 1 || SCARG(uap,count) > 2048)
     81 		return SET_ERROR(EINVAL);
     82 
     83 	for (store = SCARG(uap,store), count = SCARG(uap,count);
     84 	     count > 0;
     85 	     store++, count--) {
     86 		uuid_generate(&tmp);
     87 		error = copyout(&tmp, store, sizeof tmp);
     88 		if (error)
     89 			return error;
     90 	}
     91 
     92 	return 0;
     93 }
     94 
     95 int
     96 uuidgen(struct uuid *store, int count)
     97 {
     98 
     99 	while (--count > 0)
    100 		uuid_generate(store++);
    101 	return 0;
    102 }
    103 
    104 int
    105 uuid_snprintf(char *buf, size_t sz, const struct uuid *uuid)
    106 {
    107 
    108 	return snprintf(buf, sz,
    109 	    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
    110 	    uuid->time_low, uuid->time_mid, uuid->time_hi_and_version,
    111 	    uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low,
    112 	    uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3],
    113 	    uuid->node[4], uuid->node[5]);
    114 }
    115 
    116 int
    117 uuid_printf(const struct uuid *uuid)
    118 {
    119 	char buf[UUID_STR_LEN];
    120 
    121 	(void) uuid_snprintf(buf, sizeof(buf), uuid);
    122 	printf("%s", buf);
    123 	return (0);
    124 }
    125 
    126 /*
    127  * Encode/Decode UUID into octet-stream.
    128  *   http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
    129  *
    130  * 0                   1                   2                   3
    131  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    132  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    133  *  |                          time_low                             |
    134  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    135  *  |       time_mid                |         time_hi_and_version   |
    136  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    137  *  |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
    138  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    139  *  |                         node (2-5)                            |
    140  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    141  */
    142 
    143 void
    144 uuid_enc_le(void *buf, const struct uuid *uuid)
    145 {
    146 	uint8_t *p = buf;
    147 	int i;
    148 
    149 	le32enc(p, uuid->time_low);
    150 	le16enc(p + 4, uuid->time_mid);
    151 	le16enc(p + 6, uuid->time_hi_and_version);
    152 	p[8] = uuid->clock_seq_hi_and_reserved;
    153 	p[9] = uuid->clock_seq_low;
    154 	for (i = 0; i < _UUID_NODE_LEN; i++)
    155 		p[10 + i] = uuid->node[i];
    156 }
    157 
    158 void
    159 uuid_dec_le(void const *buf, struct uuid *uuid)
    160 {
    161 	const uint8_t *p = buf;
    162 	int i;
    163 
    164 	uuid->time_low = le32dec(p);
    165 	uuid->time_mid = le16dec(p + 4);
    166 	uuid->time_hi_and_version = le16dec(p + 6);
    167 	uuid->clock_seq_hi_and_reserved = p[8];
    168 	uuid->clock_seq_low = p[9];
    169 	for (i = 0; i < _UUID_NODE_LEN; i++)
    170 		uuid->node[i] = p[10 + i];
    171 }
    172 
    173 void
    174 uuid_enc_be(void *buf, const struct uuid *uuid)
    175 {
    176 	uint8_t *p = buf;
    177 	int i;
    178 
    179 	be32enc(p, uuid->time_low);
    180 	be16enc(p + 4, uuid->time_mid);
    181 	be16enc(p + 6, uuid->time_hi_and_version);
    182 	p[8] = uuid->clock_seq_hi_and_reserved;
    183 	p[9] = uuid->clock_seq_low;
    184 	for (i = 0; i < _UUID_NODE_LEN; i++)
    185 		p[10 + i] = uuid->node[i];
    186 }
    187 
    188 void
    189 uuid_dec_be(void const *buf, struct uuid *uuid)
    190 {
    191 	const uint8_t *p = buf;
    192 	int i;
    193 
    194 	uuid->time_low = be32dec(p);
    195 	uuid->time_mid = be16dec(p + 4);
    196 	uuid->time_hi_and_version = be16dec(p + 6);
    197 	uuid->clock_seq_hi_and_reserved = p[8];
    198 	uuid->clock_seq_low = p[9];
    199 	for (i = 0; i < _UUID_NODE_LEN; i++)
    200 		uuid->node[i] = p[10 + i];
    201 }
    202