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
32 #include <sys/types.h>
33 #include <sys/malloc.h>
34 #include <sys/queue.h>
36 #include <opencrypto/cryptodev.h>
38 #if defined(__amd64__) || defined(__i386__)
39 #include <machine/cpufunc.h>
40 #include <machine/cputypes.h>
41 #include <machine/md_var.h>
42 #include <machine/specialreg.h>
45 #include <machine/npx.h>
46 #elif defined(__amd64__)
47 #include <machine/fpu.h>
50 #define AES128_ROUNDS 10
51 #define AES192_ROUNDS 12
52 #define AES256_ROUNDS 14
53 #define AES_SCHED_LEN ((AES256_ROUNDS + 1) * AES_BLOCK_LEN)
55 /* SHA1, SHA2-224 and SHA2-256 only. */
56 #define AESNI_SHA_BLOCK_LEN 64
58 struct aesni_session {
59 uint8_t enc_schedule[AES_SCHED_LEN] __aligned(16);
60 uint8_t dec_schedule[AES_SCHED_LEN] __aligned(16);
61 uint8_t xts_schedule[AES_SCHED_LEN] __aligned(16);
62 uint8_t hmac_key[AESNI_SHA_BLOCK_LEN];
65 /* uint8_t *ses_ictx; */
66 /* uint8_t *ses_octx; */
74 * Internal functions, implemented in assembler.
76 void aesni_set_enckey(const uint8_t *userkey,
77 uint8_t *encrypt_schedule /*__aligned(16)*/, int number_of_rounds);
78 void aesni_set_deckey(const uint8_t *encrypt_schedule /*__aligned(16)*/,
79 uint8_t *decrypt_schedule /*__aligned(16)*/, int number_of_rounds);
82 * Slightly more public interfaces.
84 void aesni_encrypt_cbc(int rounds, const void *key_schedule /*__aligned(16)*/,
85 size_t len, const uint8_t *from, uint8_t *to,
86 const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
87 void aesni_decrypt_cbc(int rounds, const void *key_schedule /*__aligned(16)*/,
88 size_t len, uint8_t *buf, const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
89 void aesni_encrypt_ecb(int rounds, const void *key_schedule /*__aligned(16)*/,
90 size_t len, const uint8_t *from, uint8_t *to);
91 void aesni_decrypt_ecb(int rounds, const void *key_schedule /*__aligned(16)*/,
92 size_t len, const uint8_t *from, uint8_t *to);
93 void aesni_encrypt_icm(int rounds, const void *key_schedule /*__aligned(16)*/,
94 size_t len, const uint8_t *from, uint8_t *to,
95 const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
97 void aesni_encrypt_xts(int rounds, const void *data_schedule /*__aligned(16)*/,
98 const void *tweak_schedule /*__aligned(16)*/, size_t len,
99 const uint8_t *from, uint8_t *to,
100 const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
101 void aesni_decrypt_xts(int rounds, const void *data_schedule /*__aligned(16)*/,
102 const void *tweak_schedule /*__aligned(16)*/, size_t len,
103 const uint8_t *from, uint8_t *to,
104 const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
106 /* GCM & GHASH functions */
107 void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
108 const unsigned char *addt, const unsigned char *ivec,
109 unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
110 const unsigned char *key, int nr);
111 int AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
112 const unsigned char *addt, const unsigned char *ivec,
113 const unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
114 const unsigned char *key, int nr);
116 /* CCM + CBC-MAC functions */
117 void AES_CCM_encrypt(const unsigned char *in, unsigned char *out,
118 const unsigned char *addt, const unsigned char *ivec,
119 unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
120 const unsigned char *key, int nr);
121 int AES_CCM_decrypt(const unsigned char *in, unsigned char *out,
122 const unsigned char *addt, const unsigned char *ivec,
123 const unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
124 const unsigned char *key, int nr);
125 int aesni_cipher_setup_common(struct aesni_session *ses, const uint8_t *key,
128 #endif /* _AESNI_H_ */