2 * Copyright (c) 2010 Konstantin Belousov <kib@FreeBSD.org>
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 * 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.
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
26 * FreeBSD: src/sys/crypto/aesni/aesni_wrap.c,v 1.1 2010/07/23 11:00:46 kib Exp
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/libkern.h>
33 #include <sys/malloc.h>
35 #include <sys/systm.h>
36 #include <crypto/aesni/aesni.h>
38 MALLOC_DECLARE(M_AESNI);
42 ps_len(const char *string, const uint8_t *data, int length)
46 printf("%-12s[0x", string);
47 for(i = 0; i < length; i++) {
48 if (i % AES_BLOCK_LEN == 0 && i > 0)
50 printf("%02x", data[i]);
57 aesni_encrypt_cbc(int rounds, const void *key_schedule, size_t len,
58 const uint8_t *from, uint8_t *to, const uint8_t iv[AES_BLOCK_LEN])
64 ps_len("AES CBC encrypt iv:", iv, AES_BLOCK_LEN);
65 ps_len("from:", from, len);
70 for (i = 0; i < len; i++) {
71 aesni_enc(rounds - 1, key_schedule, from, to, ivp);
73 from += AES_BLOCK_LEN;
77 ps_len("to:", to - len * AES_BLOCK_LEN, len * AES_BLOCK_LEN);
82 aesni_encrypt_ecb(int rounds, const void *key_schedule, size_t len,
83 const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN])
88 for (i = 0; i < len; i++) {
89 aesni_enc(rounds - 1, key_schedule, from, to, NULL);
90 from += AES_BLOCK_LEN;
96 aesni_decrypt_ecb(int rounds, const void *key_schedule, size_t len,
97 const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN])
101 len /= AES_BLOCK_LEN;
102 for (i = 0; i < len; i++) {
103 aesni_dec(rounds - 1, key_schedule, from, to, NULL);
104 from += AES_BLOCK_LEN;
110 aesni_cipher_setup(struct aesni_session *ses, struct cryptoini *encini)
115 switch (encini->cri_klen) {
117 ses->rounds = AES128_ROUNDS;
120 ses->rounds = AES192_ROUNDS;
123 ses->rounds = AES256_ROUNDS;
131 error = fpu_kern_enter(td, &ses->fpu_ctx, FPU_KERN_NORMAL);
134 aesni_set_enckey(encini->cri_key, ses->enc_schedule,
136 aesni_set_deckey(ses->enc_schedule, ses->dec_schedule,
138 karc4rand(ses->iv, sizeof(ses->iv));
140 fpu_kern_leave(td, &ses->fpu_ctx);
147 aesni_cipher_process(struct aesni_session *ses, struct cryptodesc *enccrd,
152 int error = 0, allocated;
154 buf = aesni_cipher_alloc(enccrd, crp, &allocated);
162 error = fpu_kern_enter(td, &ses->fpu_ctx, FPU_KERN_NORMAL);
167 if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
168 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
169 bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN);
171 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0)
172 crypto_copyback(crp->crp_flags, crp->crp_buf,
173 enccrd->crd_inject, AES_BLOCK_LEN, ses->iv);
175 aesni_encrypt_cbc(ses->rounds, ses->enc_schedule,
176 enccrd->crd_len, buf, buf, ses->iv);
178 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
179 bcopy(enccrd->crd_iv, ses->iv, AES_BLOCK_LEN);
181 crypto_copydata(crp->crp_flags, crp->crp_buf,
182 enccrd->crd_inject, AES_BLOCK_LEN, ses->iv);
183 aesni_decrypt_cbc(ses->rounds, ses->dec_schedule,
184 enccrd->crd_len, buf, ses->iv);
187 fpu_kern_leave(td, &ses->fpu_ctx);
190 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
191 enccrd->crd_len, buf);
192 if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0)
193 crypto_copydata(crp->crp_flags, crp->crp_buf,
194 enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN,
195 AES_BLOCK_LEN, ses->iv);
198 bzero(buf, enccrd->crd_len);