nehemiah.c revision 153575
1124771Sgrehan/*-
2124771Sgrehan * Copyright (c) 2004 Mark R V Murray
3124771Sgrehan * All rights reserved.
4124771Sgrehan *
5124771Sgrehan * Redistribution and use in source and binary forms, with or without
6124771Sgrehan * modification, are permitted provided that the following conditions
7124771Sgrehan * are met:
8124771Sgrehan * 1. Redistributions of source code must retain the above copyright
9124771Sgrehan *    notice, this list of conditions and the following disclaimer
10124771Sgrehan *    in this position and unchanged.
11124771Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
12124771Sgrehan *    notice, this list of conditions and the following disclaimer in the
13124771Sgrehan *    documentation and/or other materials provided with the distribution.
14124771Sgrehan *
15124771Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16124771Sgrehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17124771Sgrehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18124771Sgrehan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19124771Sgrehan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20124771Sgrehan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21124771Sgrehan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22124771Sgrehan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23124771Sgrehan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24124771Sgrehan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25124771Sgrehan *
26124771Sgrehan */
27124771Sgrehan
28124771Sgrehan#include <sys/cdefs.h>
29124771Sgrehan__FBSDID("$FreeBSD: head/sys/dev/random/nehemiah.c 153575 2005-12-20 21:41:52Z ps $");
30124771Sgrehan
31124771Sgrehan#include <sys/param.h>
32124771Sgrehan#include <sys/time.h>
33124771Sgrehan#include <sys/lock.h>
34130585Sphk#include <sys/mutex.h>
35255904Snwhitehorn#include <sys/selinfo.h>
36124771Sgrehan#include <sys/systm.h>
37124771Sgrehan
38124771Sgrehan#include <dev/random/randomdev.h>
39124771Sgrehan
40131671Sgrehan#define RANDOM_BLOCK_SIZE	256
41124771Sgrehan#define CIPHER_BLOCK_SIZE	16
42124771Sgrehan
43124771Sgrehanstatic void random_nehemiah_init(void);
44124771Sgrehanstatic void random_nehemiah_deinit(void);
45124771Sgrehanstatic int random_nehemiah_read(void *, int);
46124771Sgrehan
47124771Sgrehanstruct random_systat random_nehemiah = {
48124771Sgrehan	.ident = "Hardware, VIA Nehemiah",
49124771Sgrehan	.init = random_nehemiah_init,
50124771Sgrehan	.deinit = random_nehemiah_deinit,
51124771Sgrehan	.read = random_nehemiah_read,
52131671Sgrehan	.write = (random_write_func_t *)random_null_func,
53131671Sgrehan	.reseed = (random_reseed_func_t *)random_null_func,
54131671Sgrehan	.seeded = 1,
55186050Snwhitehorn};
56142414Sgrehan
57142414Sgrehanunion VIA_ACE_CW {
58142414Sgrehan	uint64_t raw;
59142414Sgrehan	struct {
60142414Sgrehan		u_int round_count : 4;
61124771Sgrehan		u_int algorithm_type : 3;
62124771Sgrehan		u_int key_generation_type : 1;
63124771Sgrehan		u_int intermediate : 1;
64		u_int decrypt : 1;
65		u_int key_size : 2;
66		u_int filler0 : 20;
67		u_int filler1 : 32;
68		u_int filler2 : 32;
69		u_int filler3 : 32;
70	} field;
71};
72
73/* The extra 7 is to allow an 8-byte write on the last byte of the
74 * arrays.  The ACE wants the AES data 16-byte/128-bit aligned, and
75 * it _always_ writes n*64 bits. The RNG does not care about alignment,
76 * and it always writes n*32 bits or n*64 bits.
77 */
78static uint8_t key[CIPHER_BLOCK_SIZE+7]	__aligned(16);
79static uint8_t iv[CIPHER_BLOCK_SIZE+7]	__aligned(16);
80static uint8_t in[RANDOM_BLOCK_SIZE+7]	__aligned(16);
81static uint8_t out[RANDOM_BLOCK_SIZE+7]	__aligned(16);
82
83static union VIA_ACE_CW acw		__aligned(16);
84
85static struct mtx random_nehemiah_mtx;
86
87/* ARGSUSED */
88static __inline size_t
89VIA_RNG_store(void *buf)
90{
91#ifdef __GNUCLIKE_ASM
92	uint32_t retval = 0;
93	uint32_t rate = 0;
94
95	/* The .byte line is really VIA C3 "xstore" instruction */
96	__asm __volatile(
97		"movl	$0,%%edx		\n\t"
98		".byte	0x0f, 0xa7, 0xc0"
99			: "=a" (retval), "+d" (rate), "+D" (buf)
100			:
101			: "memory"
102	);
103	if (rate == 0)
104		return (retval&0x1f);
105#endif
106	return (0);
107}
108
109/* ARGSUSED */
110static __inline void
111VIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv)
112{
113#ifdef __GNUCLIKE_ASM
114	/* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
115	__asm __volatile(
116		"pushf				\n\t"
117		"popf				\n\t"
118		"rep				\n\t"
119		".byte	0x0f, 0xa7, 0xc8"
120			: "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
121			: "b" (key), "d" (cw)
122			: "cc", "memory"
123		);
124#endif
125}
126
127static void
128random_nehemiah_init(void)
129{
130	acw.raw = 0ULL;
131	acw.field.round_count = 12;
132
133	mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF);
134}
135
136void
137random_nehemiah_deinit(void)
138{
139	mtx_destroy(&random_nehemiah_mtx);
140}
141
142static int
143random_nehemiah_read(void *buf, int c)
144{
145	int i;
146	size_t count, ret;
147	uint8_t *p;
148
149	mtx_lock(&random_nehemiah_mtx);
150
151	/* Get a random AES key */
152	count = 0;
153	p = key;
154	do {
155		ret = VIA_RNG_store(p);
156		p += ret;
157		count += ret;
158	} while (count < CIPHER_BLOCK_SIZE);
159
160	/* Get a random AES IV */
161	count = 0;
162	p = iv;
163	do {
164		ret = VIA_RNG_store(p);
165		p += ret;
166		count += ret;
167	} while (count < CIPHER_BLOCK_SIZE);
168
169	/* Get a block of random bytes */
170	count = 0;
171	p = in;
172	do {
173		ret = VIA_RNG_store(p);
174		p += ret;
175		count += ret;
176	} while (count < RANDOM_BLOCK_SIZE);
177
178	/* This is a Davies-Meyer hash of the most paranoid variety; the
179	 * key, IV and the data are all read directly from the hardware RNG.
180	 * All of these are used precisely once.
181	 */
182	VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE,
183	    key, &acw, iv);
184	for (i = 0; i < RANDOM_BLOCK_SIZE; i++)
185		out[i] ^= in[i];
186
187	c = MIN(RANDOM_BLOCK_SIZE, c);
188	memcpy(buf, out, (size_t)c);
189
190	mtx_unlock(&random_nehemiah_mtx);
191	return (c);
192}
193