nehemiah.c revision 153575
1128059Smarkm/*-
2128059Smarkm * Copyright (c) 2004 Mark R V Murray
3128059Smarkm * All rights reserved.
4128059Smarkm *
5128059Smarkm * Redistribution and use in source and binary forms, with or without
6128059Smarkm * modification, are permitted provided that the following conditions
7128059Smarkm * are met:
8128059Smarkm * 1. Redistributions of source code must retain the above copyright
9128059Smarkm *    notice, this list of conditions and the following disclaimer
10128059Smarkm *    in this position and unchanged.
11128059Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12128059Smarkm *    notice, this list of conditions and the following disclaimer in the
13128059Smarkm *    documentation and/or other materials provided with the distribution.
14128059Smarkm *
15128059Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16128059Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17128059Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18128059Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19128059Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20128059Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21128059Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22128059Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23128059Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24128059Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25128059Smarkm *
26128059Smarkm */
27128059Smarkm
28128059Smarkm#include <sys/cdefs.h>
29128059Smarkm__FBSDID("$FreeBSD: head/sys/dev/random/nehemiah.c 153575 2005-12-20 21:41:52Z ps $");
30128059Smarkm
31128368Smarkm#include <sys/param.h>
32128059Smarkm#include <sys/time.h>
33128059Smarkm#include <sys/lock.h>
34128059Smarkm#include <sys/mutex.h>
35128059Smarkm#include <sys/selinfo.h>
36128368Smarkm#include <sys/systm.h>
37128059Smarkm
38128059Smarkm#include <dev/random/randomdev.h>
39128059Smarkm
40128368Smarkm#define RANDOM_BLOCK_SIZE	256
41128368Smarkm#define CIPHER_BLOCK_SIZE	16
42128368Smarkm
43128368Smarkmstatic void random_nehemiah_init(void);
44153575Spsstatic void random_nehemiah_deinit(void);
45128059Smarkmstatic int random_nehemiah_read(void *, int);
46128059Smarkm
47128059Smarkmstruct random_systat random_nehemiah = {
48128059Smarkm	.ident = "Hardware, VIA Nehemiah",
49128368Smarkm	.init = random_nehemiah_init,
50153575Sps	.deinit = random_nehemiah_deinit,
51128059Smarkm	.read = random_nehemiah_read,
52128059Smarkm	.write = (random_write_func_t *)random_null_func,
53128059Smarkm	.reseed = (random_reseed_func_t *)random_null_func,
54128059Smarkm	.seeded = 1,
55128059Smarkm};
56128059Smarkm
57128368Smarkmunion VIA_ACE_CW {
58128368Smarkm	uint64_t raw;
59128368Smarkm	struct {
60128368Smarkm		u_int round_count : 4;
61128368Smarkm		u_int algorithm_type : 3;
62128368Smarkm		u_int key_generation_type : 1;
63128368Smarkm		u_int intermediate : 1;
64128368Smarkm		u_int decrypt : 1;
65128368Smarkm		u_int key_size : 2;
66128368Smarkm		u_int filler0 : 20;
67128368Smarkm		u_int filler1 : 32;
68128368Smarkm		u_int filler2 : 32;
69128368Smarkm		u_int filler3 : 32;
70128368Smarkm	} field;
71128368Smarkm};
72128368Smarkm
73128368Smarkm/* The extra 7 is to allow an 8-byte write on the last byte of the
74128368Smarkm * arrays.  The ACE wants the AES data 16-byte/128-bit aligned, and
75128368Smarkm * it _always_ writes n*64 bits. The RNG does not care about alignment,
76128368Smarkm * and it always writes n*32 bits or n*64 bits.
77128368Smarkm */
78128368Smarkmstatic uint8_t key[CIPHER_BLOCK_SIZE+7]	__aligned(16);
79128368Smarkmstatic uint8_t iv[CIPHER_BLOCK_SIZE+7]	__aligned(16);
80128368Smarkmstatic uint8_t in[RANDOM_BLOCK_SIZE+7]	__aligned(16);
81128368Smarkmstatic uint8_t out[RANDOM_BLOCK_SIZE+7]	__aligned(16);
82128368Smarkm
83128368Smarkmstatic union VIA_ACE_CW acw		__aligned(16);
84128368Smarkm
85153575Spsstatic struct mtx random_nehemiah_mtx;
86153575Sps
87128059Smarkm/* ARGSUSED */
88128368Smarkmstatic __inline size_t
89128368SmarkmVIA_RNG_store(void *buf)
90128368Smarkm{
91143063Sjoerg#ifdef __GNUCLIKE_ASM
92128368Smarkm	uint32_t retval = 0;
93128368Smarkm	uint32_t rate = 0;
94128368Smarkm
95128368Smarkm	/* The .byte line is really VIA C3 "xstore" instruction */
96128368Smarkm	__asm __volatile(
97128368Smarkm		"movl	$0,%%edx		\n\t"
98128368Smarkm		".byte	0x0f, 0xa7, 0xc0"
99128368Smarkm			: "=a" (retval), "+d" (rate), "+D" (buf)
100128368Smarkm			:
101128368Smarkm			: "memory"
102128368Smarkm	);
103128368Smarkm	if (rate == 0)
104128368Smarkm		return (retval&0x1f);
105128368Smarkm#endif
106128368Smarkm	return (0);
107128368Smarkm}
108128368Smarkm
109128368Smarkm/* ARGSUSED */
110128368Smarkmstatic __inline void
111128368SmarkmVIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv)
112128368Smarkm{
113143063Sjoerg#ifdef __GNUCLIKE_ASM
114128368Smarkm	/* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
115128368Smarkm	__asm __volatile(
116128368Smarkm		"pushf				\n\t"
117128368Smarkm		"popf				\n\t"
118128368Smarkm		"rep				\n\t"
119128368Smarkm		".byte	0x0f, 0xa7, 0xc8"
120128368Smarkm			: "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
121128368Smarkm			: "b" (key), "d" (cw)
122128368Smarkm			: "cc", "memory"
123128368Smarkm		);
124128368Smarkm#endif
125128368Smarkm}
126128368Smarkm
127128368Smarkmstatic void
128128368Smarkmrandom_nehemiah_init(void)
129128368Smarkm{
130128368Smarkm	acw.raw = 0ULL;
131128368Smarkm	acw.field.round_count = 12;
132153575Sps
133153575Sps	mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF);
134128368Smarkm}
135128368Smarkm
136153575Spsvoid
137153575Spsrandom_nehemiah_deinit(void)
138153575Sps{
139153575Sps	mtx_destroy(&random_nehemiah_mtx);
140153575Sps}
141153575Sps
142128059Smarkmstatic int
143128059Smarkmrandom_nehemiah_read(void *buf, int c)
144128059Smarkm{
145128368Smarkm	int i;
146128368Smarkm	size_t count, ret;
147128368Smarkm	uint8_t *p;
148128059Smarkm
149153575Sps	mtx_lock(&random_nehemiah_mtx);
150153575Sps
151128368Smarkm	/* Get a random AES key */
152128368Smarkm	count = 0;
153128368Smarkm	p = key;
154128368Smarkm	do {
155128368Smarkm		ret = VIA_RNG_store(p);
156128368Smarkm		p += ret;
157128368Smarkm		count += ret;
158128368Smarkm	} while (count < CIPHER_BLOCK_SIZE);
159128368Smarkm
160128368Smarkm	/* Get a random AES IV */
161128368Smarkm	count = 0;
162128368Smarkm	p = iv;
163128368Smarkm	do {
164128368Smarkm		ret = VIA_RNG_store(p);
165128368Smarkm		p += ret;
166128368Smarkm		count += ret;
167128368Smarkm	} while (count < CIPHER_BLOCK_SIZE);
168128368Smarkm
169128368Smarkm	/* Get a block of random bytes */
170128368Smarkm	count = 0;
171128368Smarkm	p = in;
172128368Smarkm	do {
173128368Smarkm		ret = VIA_RNG_store(p);
174128368Smarkm		p += ret;
175128368Smarkm		count += ret;
176128368Smarkm	} while (count < RANDOM_BLOCK_SIZE);
177128368Smarkm
178128368Smarkm	/* This is a Davies-Meyer hash of the most paranoid variety; the
179128368Smarkm	 * key, IV and the data are all read directly from the hardware RNG.
180128368Smarkm	 * All of these are used precisely once.
181128368Smarkm	 */
182128368Smarkm	VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE,
183128368Smarkm	    key, &acw, iv);
184128368Smarkm	for (i = 0; i < RANDOM_BLOCK_SIZE; i++)
185128368Smarkm		out[i] ^= in[i];
186128368Smarkm
187128368Smarkm	c = MIN(RANDOM_BLOCK_SIZE, c);
188128368Smarkm	memcpy(buf, out, (size_t)c);
189128368Smarkm
190153575Sps	mtx_unlock(&random_nehemiah_mtx);
191128059Smarkm	return (c);
192128059Smarkm}
193