1 /*- 2 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/libkern.h> 32 #include <sys/malloc.h> 33 #include <sys/proc.h> 34 #include <sys/systm.h> 35 #include <crypto/aesni/aesni.h> 36 37 MALLOC_DECLARE(M_AESNI); 38 39 #ifdef DEBUG 40 static void 41 ps_len(const char *string, const uint8_t *data, int length) 42 { 43 int i; 44 45 printf("%-12s[0x", string); 46 for(i = 0; i < length; i++) { 47 if (i % AES_BLOCK_LEN == 0 && i > 0) 48 printf("+"); 49 printf("%02x", data[i]); 50 } 51 printf("]\n"); 52 } 53 #endif 54 55 void 56 aesni_encrypt_cbc(int rounds, const void *key_schedule, size_t len, 57 const uint8_t *from, uint8_t *to, const uint8_t iv[AES_BLOCK_LEN]) 58 { 59 const uint8_t *ivp; 60 size_t i; 61 62 #ifdef DEBUG 63 ps_len("AES CBC encrypt iv:", iv, AES_BLOCK_LEN); 64 ps_len("from:", from, len); 65 #endif 66 67 len /= AES_BLOCK_LEN; 68 ivp = iv; 69 for (i = 0; i < len; i++) { 70 aesni_enc(rounds - 1, key_schedule, from, to, ivp); 71 ivp = to; 72 from += AES_BLOCK_LEN; 73 to += AES_BLOCK_LEN; 74 } 75 #ifdef DEBUG 76 ps_len("to:", to - len * AES_BLOCK_LEN, len * AES_BLOCK_LEN); 77 #endif 78 } 79 80 void 81 aesni_encrypt_ecb(int rounds, const void *key_schedule, size_t len, 82 const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN]) 83 { 84 size_t i; 85 86 len /= AES_BLOCK_LEN; 87 for (i = 0; i < len; i++) { 88 aesni_enc(rounds - 1, key_schedule, from, to, NULL); 89 from += AES_BLOCK_LEN; 90 to += AES_BLOCK_LEN; 91 } 92 } 93 94 void 95 aesni_decrypt_ecb(int rounds, const void *key_schedule, size_t len, 96 const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN]) 97 { 98 size_t i; 99 100 len /= AES_BLOCK_LEN; 101 for (i = 0; i < len; i++) { 102 aesni_dec(rounds - 1, key_schedule, from, to, NULL); 103 from += AES_BLOCK_LEN; 104 to += AES_BLOCK_LEN; 105 } 106 } 107 108 int 109 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini) 110 { 111 struct thread *td; 112 int error; 113 114 switch (encini->cri_klen) { 115 case 128: 116 ses->rounds = AES128_ROUNDS; 117 break; 118 case 192: 119 ses->rounds = AES192_ROUNDS; 120 break; 121 case 256: 122 ses->rounds = AES256_ROUNDS; 123 break; 124 default: 125 return (EINVAL); 126 } 127 128 td = curthread; 129 error = fpu_kern_enter(td, &ses->fpu_ctx, FPU_KERN_NORMAL); 130 if (error == 0) { 131 aesni_set_enckey(encini->cri_key, ses->enc_schedule, 132 ses->rounds); 133 aesni_set_deckey(ses->enc_schedule, ses->dec_schedule, 134 ses->rounds); 135 arc4rand(ses->iv, sizeof(ses->iv), 0); 136 fpu_kern_leave(td, &ses->fpu_ctx); 137 } 138 return (error); 139 } 140 141 int 142 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd, 143 struct cryptop *crp) 144 { 145 struct thread *td; 146 uint8_t *buf; 147 int error, allocated; 148 149 buf = aesni_cipher_alloc(enccrd, crp, &allocated); 150 if (buf == NULL) { 151 error = ENOMEM; 152 goto out; 153 } 154 155 td = curthread; 156 error = fpu_kern_enter(td, &ses->fpu_ctx, FPU_KERN_NORMAL); 157 if (error != 0) 158 goto out1; 159 160 if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) { 161 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0) 162 bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN); 163 164 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) 165 crypto_copyback(crp->crp_flags, crp->crp_buf, 166 enccrd->crd_inject, AES_BLOCK_LEN, ses->iv); 167 168 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule, 169 enccrd->crd_len, buf, buf, ses->iv); 170 } else { 171 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0) 172 bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN); 173 else 174 crypto_copydata(crp->crp_flags, crp->crp_buf, 175 enccrd->crd_inject, AES_BLOCK_LEN, ses->iv); 176 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule, 177 enccrd->crd_len, buf, ses->iv); 178 } 179 fpu_kern_leave(td, &ses->fpu_ctx); 180 if (allocated) 181 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip, 182 enccrd->crd_len, buf); 183 if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) 184 crypto_copydata(crp->crp_flags, crp->crp_buf, 185 enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN, 186 AES_BLOCK_LEN, ses->iv); 187 out1: 188 if (allocated) { 189 bzero(buf, enccrd->crd_len); 190 free(buf, M_AESNI); 191 } 192 out: 193 return (error); 194 } 195