2 * Copyright (c) 2004 Mark R V Murray
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
34 #include <sys/mutex.h>
35 #include <sys/selinfo.h>
36 #include <sys/systm.h>
38 #include <dev/random/randomdev.h>
40 #define RANDOM_BLOCK_SIZE 256
41 #define CIPHER_BLOCK_SIZE 16
43 static void random_nehemiah_init(void);
44 static void random_nehemiah_deinit(void);
45 static int random_nehemiah_read(void *, int);
47 struct random_systat random_nehemiah = {
48 .ident = "Hardware, VIA Nehemiah",
49 .init = random_nehemiah_init,
50 .deinit = random_nehemiah_deinit,
51 .read = random_nehemiah_read,
52 .write = (random_write_func_t *)random_null_func,
53 .reseed = (random_reseed_func_t *)random_null_func,
60 u_int round_count : 4;
61 u_int algorithm_type : 3;
62 u_int key_generation_type : 1;
63 u_int intermediate : 1;
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.
78 static uint8_t key[CIPHER_BLOCK_SIZE+7] __aligned(16);
79 static uint8_t iv[CIPHER_BLOCK_SIZE+7] __aligned(16);
80 static uint8_t in[RANDOM_BLOCK_SIZE+7] __aligned(16);
81 static uint8_t out[RANDOM_BLOCK_SIZE+7] __aligned(16);
83 static union VIA_ACE_CW acw __aligned(16);
85 static struct mtx random_nehemiah_mtx;
88 static __inline size_t
89 VIA_RNG_store(void *buf)
95 /* The .byte line is really VIA C3 "xstore" instruction */
98 ".byte 0x0f, 0xa7, 0xc0"
99 : "=a" (retval), "+d" (rate), "+D" (buf)
104 return (retval&0x1f);
111 VIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv)
113 #ifdef __GNUCLIKE_ASM
114 /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
119 ".byte 0x0f, 0xa7, 0xc8"
120 : "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
121 : "b" (key), "d" (cw)
128 random_nehemiah_init(void)
131 acw.field.round_count = 12;
133 mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF);
137 random_nehemiah_deinit(void)
139 mtx_destroy(&random_nehemiah_mtx);
143 random_nehemiah_read(void *buf, int c)
149 mtx_lock(&random_nehemiah_mtx);
151 /* Get a random AES key */
155 ret = VIA_RNG_store(p);
158 } while (count < CIPHER_BLOCK_SIZE);
160 /* Get a random AES IV */
164 ret = VIA_RNG_store(p);
167 } while (count < CIPHER_BLOCK_SIZE);
169 /* Get a block of random bytes */
173 ret = VIA_RNG_store(p);
176 } while (count < RANDOM_BLOCK_SIZE);
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.
182 VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE,
184 for (i = 0; i < RANDOM_BLOCK_SIZE; i++)
187 c = MIN(RANDOM_BLOCK_SIZE, c);
188 memcpy(buf, out, (size_t)c);
190 mtx_unlock(&random_nehemiah_mtx);