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$"); 30128059Smarkm 31240950Skib#include "opt_cpu.h" 32240950Skib 33240950Skib#ifdef PADLOCK_RNG 34240950Skib 35128368Smarkm#include <sys/param.h> 36128059Smarkm#include <sys/time.h> 37128059Smarkm#include <sys/lock.h> 38128059Smarkm#include <sys/mutex.h> 39128059Smarkm#include <sys/selinfo.h> 40128368Smarkm#include <sys/systm.h> 41128059Smarkm 42208834Skib#include <machine/pcb.h> 43208834Skib 44128059Smarkm#include <dev/random/randomdev.h> 45128059Smarkm 46128368Smarkm#define RANDOM_BLOCK_SIZE 256 47128368Smarkm#define CIPHER_BLOCK_SIZE 16 48128368Smarkm 49128368Smarkmstatic void random_nehemiah_init(void); 50153575Spsstatic void random_nehemiah_deinit(void); 51128059Smarkmstatic int random_nehemiah_read(void *, int); 52128059Smarkm 53128059Smarkmstruct random_systat random_nehemiah = { 54128059Smarkm .ident = "Hardware, VIA Nehemiah", 55128368Smarkm .init = random_nehemiah_init, 56153575Sps .deinit = random_nehemiah_deinit, 57128059Smarkm .read = random_nehemiah_read, 58128059Smarkm .write = (random_write_func_t *)random_null_func, 59128059Smarkm .reseed = (random_reseed_func_t *)random_null_func, 60128059Smarkm .seeded = 1, 61128059Smarkm}; 62128059Smarkm 63128368Smarkmunion VIA_ACE_CW { 64128368Smarkm uint64_t raw; 65128368Smarkm struct { 66128368Smarkm u_int round_count : 4; 67128368Smarkm u_int algorithm_type : 3; 68128368Smarkm u_int key_generation_type : 1; 69128368Smarkm u_int intermediate : 1; 70128368Smarkm u_int decrypt : 1; 71128368Smarkm u_int key_size : 2; 72128368Smarkm u_int filler0 : 20; 73128368Smarkm u_int filler1 : 32; 74128368Smarkm u_int filler2 : 32; 75128368Smarkm u_int filler3 : 32; 76128368Smarkm } field; 77128368Smarkm}; 78128368Smarkm 79128368Smarkm/* The extra 7 is to allow an 8-byte write on the last byte of the 80128368Smarkm * arrays. The ACE wants the AES data 16-byte/128-bit aligned, and 81128368Smarkm * it _always_ writes n*64 bits. The RNG does not care about alignment, 82128368Smarkm * and it always writes n*32 bits or n*64 bits. 83128368Smarkm */ 84128368Smarkmstatic uint8_t key[CIPHER_BLOCK_SIZE+7] __aligned(16); 85128368Smarkmstatic uint8_t iv[CIPHER_BLOCK_SIZE+7] __aligned(16); 86128368Smarkmstatic uint8_t in[RANDOM_BLOCK_SIZE+7] __aligned(16); 87128368Smarkmstatic uint8_t out[RANDOM_BLOCK_SIZE+7] __aligned(16); 88128368Smarkm 89128368Smarkmstatic union VIA_ACE_CW acw __aligned(16); 90128368Smarkm 91231979Skibstatic struct fpu_kern_ctx *fpu_ctx_save; 92208834Skib 93153575Spsstatic struct mtx random_nehemiah_mtx; 94153575Sps 95128059Smarkm/* ARGSUSED */ 96128368Smarkmstatic __inline size_t 97128368SmarkmVIA_RNG_store(void *buf) 98128368Smarkm{ 99143063Sjoerg#ifdef __GNUCLIKE_ASM 100128368Smarkm uint32_t retval = 0; 101128368Smarkm uint32_t rate = 0; 102128368Smarkm 103128368Smarkm /* The .byte line is really VIA C3 "xstore" instruction */ 104128368Smarkm __asm __volatile( 105128368Smarkm "movl $0,%%edx \n\t" 106128368Smarkm ".byte 0x0f, 0xa7, 0xc0" 107128368Smarkm : "=a" (retval), "+d" (rate), "+D" (buf) 108128368Smarkm : 109128368Smarkm : "memory" 110128368Smarkm ); 111128368Smarkm if (rate == 0) 112128368Smarkm return (retval&0x1f); 113128368Smarkm#endif 114128368Smarkm return (0); 115128368Smarkm} 116128368Smarkm 117128368Smarkm/* ARGSUSED */ 118128368Smarkmstatic __inline void 119128368SmarkmVIA_ACE_cbc(void *in, void *out, size_t count, void *key, union VIA_ACE_CW *cw, void *iv) 120128368Smarkm{ 121143063Sjoerg#ifdef __GNUCLIKE_ASM 122128368Smarkm /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */ 123128368Smarkm __asm __volatile( 124128368Smarkm "pushf \n\t" 125128368Smarkm "popf \n\t" 126128368Smarkm "rep \n\t" 127128368Smarkm ".byte 0x0f, 0xa7, 0xc8" 128128368Smarkm : "+a" (iv), "+c" (count), "+D" (out), "+S" (in) 129128368Smarkm : "b" (key), "d" (cw) 130128368Smarkm : "cc", "memory" 131128368Smarkm ); 132128368Smarkm#endif 133128368Smarkm} 134128368Smarkm 135128368Smarkmstatic void 136128368Smarkmrandom_nehemiah_init(void) 137128368Smarkm{ 138128368Smarkm acw.raw = 0ULL; 139128368Smarkm acw.field.round_count = 12; 140192774Smarkm 141153575Sps mtx_init(&random_nehemiah_mtx, "random nehemiah", NULL, MTX_DEF); 142231979Skib fpu_ctx_save = fpu_kern_alloc_ctx(FPU_KERN_NORMAL); 143128368Smarkm} 144128368Smarkm 145153575Spsvoid 146153575Spsrandom_nehemiah_deinit(void) 147153575Sps{ 148231979Skib 149231979Skib fpu_kern_free_ctx(fpu_ctx_save); 150153575Sps mtx_destroy(&random_nehemiah_mtx); 151153575Sps} 152153575Sps 153128059Smarkmstatic int 154128059Smarkmrandom_nehemiah_read(void *buf, int c) 155128059Smarkm{ 156208834Skib int i, error; 157128368Smarkm size_t count, ret; 158128368Smarkm uint8_t *p; 159128059Smarkm 160153575Sps mtx_lock(&random_nehemiah_mtx); 161231979Skib error = fpu_kern_enter(curthread, fpu_ctx_save, FPU_KERN_NORMAL); 162208834Skib if (error != 0) { 163208834Skib mtx_unlock(&random_nehemiah_mtx); 164208834Skib return (0); 165208834Skib } 166153575Sps 167128368Smarkm /* Get a random AES key */ 168128368Smarkm count = 0; 169128368Smarkm p = key; 170128368Smarkm do { 171128368Smarkm ret = VIA_RNG_store(p); 172128368Smarkm p += ret; 173128368Smarkm count += ret; 174128368Smarkm } while (count < CIPHER_BLOCK_SIZE); 175128368Smarkm 176128368Smarkm /* Get a random AES IV */ 177128368Smarkm count = 0; 178128368Smarkm p = iv; 179128368Smarkm do { 180128368Smarkm ret = VIA_RNG_store(p); 181128368Smarkm p += ret; 182128368Smarkm count += ret; 183128368Smarkm } while (count < CIPHER_BLOCK_SIZE); 184128368Smarkm 185128368Smarkm /* Get a block of random bytes */ 186128368Smarkm count = 0; 187128368Smarkm p = in; 188128368Smarkm do { 189128368Smarkm ret = VIA_RNG_store(p); 190128368Smarkm p += ret; 191128368Smarkm count += ret; 192128368Smarkm } while (count < RANDOM_BLOCK_SIZE); 193128368Smarkm 194128368Smarkm /* This is a Davies-Meyer hash of the most paranoid variety; the 195128368Smarkm * key, IV and the data are all read directly from the hardware RNG. 196128368Smarkm * All of these are used precisely once. 197128368Smarkm */ 198128368Smarkm VIA_ACE_cbc(in, out, RANDOM_BLOCK_SIZE/CIPHER_BLOCK_SIZE, 199128368Smarkm key, &acw, iv); 200128368Smarkm for (i = 0; i < RANDOM_BLOCK_SIZE; i++) 201128368Smarkm out[i] ^= in[i]; 202128368Smarkm 203128368Smarkm c = MIN(RANDOM_BLOCK_SIZE, c); 204128368Smarkm memcpy(buf, out, (size_t)c); 205128368Smarkm 206231979Skib fpu_kern_leave(curthread, fpu_ctx_save); 207153575Sps mtx_unlock(&random_nehemiah_mtx); 208128059Smarkm return (c); 209128059Smarkm} 210240950Skib 211240950Skib#endif 212