nehemiah.c revision 208834
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 208834 2010-06-05 16:00:53Z kib $"); 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 38208834Skib#include <machine/pcb.h> 39208834Skib 40128059Smarkm#include <dev/random/randomdev.h> 41128059Smarkm 42128368Smarkm#define RANDOM_BLOCK_SIZE 256 43128368Smarkm#define CIPHER_BLOCK_SIZE 16 44128368Smarkm 45128368Smarkmstatic void random_nehemiah_init(void); 46153575Spsstatic void random_nehemiah_deinit(void); 47128059Smarkmstatic int random_nehemiah_read(void *, int); 48128059Smarkm 49128059Smarkmstruct random_systat random_nehemiah = { 50128059Smarkm .ident = "Hardware, VIA Nehemiah", 51128368Smarkm .init = random_nehemiah_init, 52153575Sps .deinit = random_nehemiah_deinit, 53128059Smarkm .read = random_nehemiah_read, 54128059Smarkm .write = (random_write_func_t *)random_null_func, 55128059Smarkm .reseed = (random_reseed_func_t *)random_null_func, 56128059Smarkm .seeded = 1, 57128059Smarkm}; 58128059Smarkm 59128368Smarkmunion VIA_ACE_CW { 60128368Smarkm uint64_t raw; 61128368Smarkm struct { 62128368Smarkm u_int round_count : 4; 63128368Smarkm u_int algorithm_type : 3; 64128368Smarkm u_int key_generation_type : 1; 65128368Smarkm u_int intermediate : 1; 66128368Smarkm u_int decrypt : 1; 67128368Smarkm u_int key_size : 2; 68128368Smarkm u_int filler0 : 20; 69128368Smarkm u_int filler1 : 32; 70128368Smarkm u_int filler2 : 32; 71128368Smarkm u_int filler3 : 32; 72128368Smarkm } field; 73128368Smarkm}; 74128368Smarkm 75128368Smarkm/* The extra 7 is to allow an 8-byte write on the last byte of the 76128368Smarkm * arrays. The ACE wants the AES data 16-byte/128-bit aligned, and 77128368Smarkm * it _always_ writes n*64 bits. The RNG does not care about alignment, 78128368Smarkm * and it always writes n*32 bits or n*64 bits. 79128368Smarkm */ 80128368Smarkmstatic uint8_t key[CIPHER_BLOCK_SIZE+7] __aligned(16); 81128368Smarkmstatic uint8_t iv[CIPHER_BLOCK_SIZE+7] __aligned(16); 82128368Smarkmstatic uint8_t in[RANDOM_BLOCK_SIZE+7] __aligned(16); 83128368Smarkmstatic uint8_t out[RANDOM_BLOCK_SIZE+7] __aligned(16); 84128368Smarkm 85128368Smarkmstatic union VIA_ACE_CW acw __aligned(16); 86128368Smarkm 87208834Skibstatic struct fpu_kern_ctx fpu_ctx_save; 88208834Skib 89153575Spsstatic struct mtx random_nehemiah_mtx; 90153575Sps 91128059Smarkm/* ARGSUSED */ 92128368Smarkmstatic __inline size_t 93128368SmarkmVIA_RNG_store(void *buf) 94128368Smarkm{ 95143063Sjoerg#ifdef __GNUCLIKE_ASM 96128368Smarkm uint32_t retval = 0; 97128368Smarkm uint32_t rate = 0; 98128368Smarkm 99128368Smarkm /* The .byte line is really VIA C3 "xstore" instruction */ 100128368Smarkm __asm __volatile( 101128368Smarkm "movl $0,%%edx \n\t" 102128368Smarkm ".byte 0x0f, 0xa7, 0xc0" 103128368Smarkm : "=a" (retval), "+d" (rate), "+D" (buf) 104128368Smarkm : 105128368Smarkm : "memory" 106128368Smarkm ); 107128368Smarkm if (rate == 0) 108128368Smarkm return (retval&0x1f); 109128368Smarkm#endif 110128368Smarkm return (0); 111128368Smarkm} 112128368Smarkm 113128368Smarkm/* ARGSUSED */ 114128368Smarkmstatic __inline void 115128368SmarkmVIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv) 116128368Smarkm{ 117143063Sjoerg#ifdef __GNUCLIKE_ASM 118128368Smarkm /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */ 119128368Smarkm __asm __volatile( 120128368Smarkm "pushf \n\t" 121128368Smarkm "popf \n\t" 122128368Smarkm "rep \n\t" 123128368Smarkm ".byte 0x0f, 0xa7, 0xc8" 124128368Smarkm : "+a" (iv), "+c" (count), "+D" (out), "+S" (in) 125128368Smarkm : "b" (key), "d" (cw) 126128368Smarkm : "cc", "memory" 127128368Smarkm ); 128128368Smarkm#endif 129128368Smarkm} 130128368Smarkm 131128368Smarkmstatic void 132128368Smarkmrandom_nehemiah_init(void) 133128368Smarkm{ 134128368Smarkm acw.raw = 0ULL; 135128368Smarkm acw.field.round_count = 12; 136192774Smarkm 137153575Sps mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF); 138128368Smarkm} 139128368Smarkm 140153575Spsvoid 141153575Spsrandom_nehemiah_deinit(void) 142153575Sps{ 143153575Sps mtx_destroy(&random_nehemiah_mtx); 144153575Sps} 145153575Sps 146128059Smarkmstatic int 147128059Smarkmrandom_nehemiah_read(void *buf, int c) 148128059Smarkm{ 149208834Skib int i, error; 150128368Smarkm size_t count, ret; 151128368Smarkm uint8_t *p; 152128059Smarkm 153153575Sps mtx_lock(&random_nehemiah_mtx); 154208834Skib error = fpu_kern_enter(curthread, &fpu_ctx_save, FPU_KERN_NORMAL); 155208834Skib if (error != 0) { 156208834Skib mtx_unlock(&random_nehemiah_mtx); 157208834Skib return (0); 158208834Skib } 159153575Sps 160128368Smarkm /* Get a random AES key */ 161128368Smarkm count = 0; 162128368Smarkm p = key; 163128368Smarkm do { 164128368Smarkm ret = VIA_RNG_store(p); 165128368Smarkm p += ret; 166128368Smarkm count += ret; 167128368Smarkm } while (count < CIPHER_BLOCK_SIZE); 168128368Smarkm 169128368Smarkm /* Get a random AES IV */ 170128368Smarkm count = 0; 171128368Smarkm p = iv; 172128368Smarkm do { 173128368Smarkm ret = VIA_RNG_store(p); 174128368Smarkm p += ret; 175128368Smarkm count += ret; 176128368Smarkm } while (count < CIPHER_BLOCK_SIZE); 177128368Smarkm 178128368Smarkm /* Get a block of random bytes */ 179128368Smarkm count = 0; 180128368Smarkm p = in; 181128368Smarkm do { 182128368Smarkm ret = VIA_RNG_store(p); 183128368Smarkm p += ret; 184128368Smarkm count += ret; 185128368Smarkm } while (count < RANDOM_BLOCK_SIZE); 186128368Smarkm 187128368Smarkm /* This is a Davies-Meyer hash of the most paranoid variety; the 188128368Smarkm * key, IV and the data are all read directly from the hardware RNG. 189128368Smarkm * All of these are used precisely once. 190128368Smarkm */ 191128368Smarkm VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE, 192128368Smarkm key, &acw, iv); 193128368Smarkm for (i = 0; i < RANDOM_BLOCK_SIZE; i++) 194128368Smarkm out[i] ^= in[i]; 195128368Smarkm 196128368Smarkm c = MIN(RANDOM_BLOCK_SIZE, c); 197128368Smarkm memcpy(buf, out, (size_t)c); 198128368Smarkm 199208834Skib fpu_kern_leave(curthread, &fpu_ctx_save); 200153575Sps mtx_unlock(&random_nehemiah_mtx); 201128059Smarkm return (c); 202128059Smarkm} 203