1 /*- 2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * Copyright (c) 2004 Mark R V Murray 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 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. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* $OpenBSD: via.c,v 1.3 2004/06/15 23:36:55 deraadt Exp $ */ 29 /*- 30 * Copyright (c) 2003 Jason Wright 31 * Copyright (c) 2003, 2004 Theo de Raadt 32 * All rights reserved. 33 * 34 * Permission to use, copy, modify, and distribute this software for any 35 * purpose with or without fee is hereby granted, provided that the above 36 * copyright notice and this permission notice appear in all copies. 37 * 38 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 39 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 40 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 41 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 42 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 43 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 44 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 45 */ 46 47 #include <sys/cdefs.h> 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/module.h> 52 #include <sys/malloc.h> 53 #include <sys/libkern.h> 54 #include <sys/pcpu.h> 55 #include <sys/uio.h> 56 #include <machine/fpu.h> 57 58 #include <opencrypto/cryptodev.h> 59 #include <crypto/rijndael/rijndael.h> 60 61 #include <crypto/via/padlock.h> 62 63 #define PADLOCK_ROUND_COUNT_AES128 10 64 #define PADLOCK_ROUND_COUNT_AES192 12 65 #define PADLOCK_ROUND_COUNT_AES256 14 66 67 #define PADLOCK_ALGORITHM_TYPE_AES 0 68 69 #define PADLOCK_KEY_GENERATION_HW 0 70 #define PADLOCK_KEY_GENERATION_SW 1 71 72 #define PADLOCK_DIRECTION_ENCRYPT 0 73 #define PADLOCK_DIRECTION_DECRYPT 1 74 75 #define PADLOCK_KEY_SIZE_128 0 76 #define PADLOCK_KEY_SIZE_192 1 77 #define PADLOCK_KEY_SIZE_256 2 78 79 MALLOC_DECLARE(M_PADLOCK); 80 81 static __inline void 82 padlock_cbc(void *in, void *out, size_t count, void *key, union padlock_cw *cw, 83 void *iv) 84 { 85 /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */ 86 __asm __volatile( 87 "pushf \n\t" 88 "popf \n\t" 89 "rep \n\t" 90 ".byte 0x0f, 0xa7, 0xd0" 91 : "+a" (iv), "+c" (count), "+D" (out), "+S" (in) 92 : "b" (key), "d" (cw) 93 : "cc", "memory" 94 ); 95 } 96 97 static void 98 padlock_cipher_key_setup(struct padlock_session *ses, const void *key, int klen) 99 { 100 union padlock_cw *cw; 101 int i; 102 103 cw = &ses->ses_cw; 104 if (cw->cw_key_generation == PADLOCK_KEY_GENERATION_SW) { 105 /* Build expanded keys for both directions */ 106 rijndaelKeySetupEnc(ses->ses_ekey, key, klen * 8); 107 rijndaelKeySetupDec(ses->ses_dkey, key, klen * 8); 108 for (i = 0; i < 4 * (RIJNDAEL_MAXNR + 1); i++) { 109 ses->ses_ekey[i] = ntohl(ses->ses_ekey[i]); 110 ses->ses_dkey[i] = ntohl(ses->ses_dkey[i]); 111 } 112 } else { 113 bcopy(key, ses->ses_ekey, klen); 114 bcopy(key, ses->ses_dkey, klen); 115 } 116 } 117 118 int 119 padlock_cipher_setup(struct padlock_session *ses, 120 const struct crypto_session_params *csp) 121 { 122 union padlock_cw *cw; 123 124 if (csp->csp_cipher_klen != 16 && csp->csp_cipher_klen != 24 && 125 csp->csp_cipher_klen != 32) { 126 return (EINVAL); 127 } 128 129 cw = &ses->ses_cw; 130 bzero(cw, sizeof(*cw)); 131 cw->cw_algorithm_type = PADLOCK_ALGORITHM_TYPE_AES; 132 cw->cw_key_generation = PADLOCK_KEY_GENERATION_SW; 133 cw->cw_intermediate = 0; 134 switch (csp->csp_cipher_klen * 8) { 135 case 128: 136 cw->cw_round_count = PADLOCK_ROUND_COUNT_AES128; 137 cw->cw_key_size = PADLOCK_KEY_SIZE_128; 138 #ifdef HW_KEY_GENERATION 139 /* This doesn't buy us much, that's why it is commented out. */ 140 cw->cw_key_generation = PADLOCK_KEY_GENERATION_HW; 141 #endif 142 break; 143 case 192: 144 cw->cw_round_count = PADLOCK_ROUND_COUNT_AES192; 145 cw->cw_key_size = PADLOCK_KEY_SIZE_192; 146 break; 147 case 256: 148 cw->cw_round_count = PADLOCK_ROUND_COUNT_AES256; 149 cw->cw_key_size = PADLOCK_KEY_SIZE_256; 150 break; 151 } 152 if (csp->csp_cipher_key != NULL) { 153 padlock_cipher_key_setup(ses, csp->csp_cipher_key, 154 csp->csp_cipher_klen); 155 } 156 return (0); 157 } 158 159 /* 160 * Function checks if the given buffer is already 16 bytes aligned. 161 * If it is there is no need to allocate new buffer. 162 * If it isn't, new buffer is allocated. 163 */ 164 static u_char * 165 padlock_cipher_alloc(struct cryptop *crp, int *allocated) 166 { 167 u_char *addr; 168 169 addr = crypto_contiguous_subsegment(crp, crp->crp_payload_start, 170 crp->crp_payload_length); 171 if (((uintptr_t)addr & 0xf) == 0) { /* 16 bytes aligned? */ 172 *allocated = 0; 173 return (addr); 174 } 175 176 *allocated = 1; 177 addr = malloc(crp->crp_payload_length + 16, M_PADLOCK, M_NOWAIT); 178 return (addr); 179 } 180 181 int 182 padlock_cipher_process(struct padlock_session *ses, struct cryptop *crp, 183 const struct crypto_session_params *csp) 184 { 185 union padlock_cw *cw; 186 struct thread *td; 187 u_char *buf, *abuf; 188 uint32_t *key; 189 uint8_t iv[AES_BLOCK_LEN] __aligned(16); 190 int allocated; 191 192 buf = padlock_cipher_alloc(crp, &allocated); 193 if (buf == NULL) 194 return (ENOMEM); 195 /* Buffer has to be 16 bytes aligned. */ 196 abuf = PADLOCK_ALIGN(buf); 197 198 if (crp->crp_cipher_key != NULL) { 199 padlock_cipher_key_setup(ses, crp->crp_cipher_key, 200 csp->csp_cipher_klen); 201 } 202 203 cw = &ses->ses_cw; 204 cw->cw_filler0 = 0; 205 cw->cw_filler1 = 0; 206 cw->cw_filler2 = 0; 207 cw->cw_filler3 = 0; 208 209 crypto_read_iv(crp, iv); 210 211 if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { 212 cw->cw_direction = PADLOCK_DIRECTION_ENCRYPT; 213 key = ses->ses_ekey; 214 } else { 215 cw->cw_direction = PADLOCK_DIRECTION_DECRYPT; 216 key = ses->ses_dkey; 217 } 218 219 if (allocated) { 220 crypto_copydata(crp, crp->crp_payload_start, 221 crp->crp_payload_length, abuf); 222 } 223 224 td = curthread; 225 fpu_kern_enter(td, NULL, FPU_KERN_NORMAL | FPU_KERN_NOCTX); 226 padlock_cbc(abuf, abuf, crp->crp_payload_length / AES_BLOCK_LEN, key, 227 cw, iv); 228 fpu_kern_leave(td, NULL); 229 230 if (allocated) { 231 crypto_copyback(crp, crp->crp_payload_start, 232 crp->crp_payload_length, abuf); 233 234 zfree(buf, M_PADLOCK); 235 } 236 return (0); 237 } 238