xref: /freebsd/sys/crypto/aesni/aesni.h (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
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 #ifndef _AESNI_H_
28 #define _AESNI_H_
29 
30 #include <sys/types.h>
31 #include <sys/malloc.h>
32 #include <sys/queue.h>
33 
34 #include <opencrypto/cryptodev.h>
35 
36 #if defined(__amd64__) || defined(__i386__)
37 #include <machine/cpufunc.h>
38 #include <machine/cputypes.h>
39 #include <machine/md_var.h>
40 #include <machine/specialreg.h>
41 #include <machine/fpu.h>
42 #endif
43 
44 #define	AES128_ROUNDS	10
45 #define	AES192_ROUNDS	12
46 #define	AES256_ROUNDS	14
47 #define	AES_SCHED_LEN	((AES256_ROUNDS + 1) * AES_BLOCK_LEN)
48 #define	AES_SCHED_ALIGN	16
49 
50 struct aesni_session {
51 	uint8_t schedules[3 * AES_SCHED_LEN + AES_SCHED_ALIGN];
52 	uint8_t *enc_schedule;
53 	uint8_t *dec_schedule;
54 	uint8_t *xts_schedule;
55 	int rounds;
56 	/* uint8_t *ses_ictx; */
57 	/* uint8_t *ses_octx; */
58 	int used;
59 	int mlen;
60 	int hash_len;
61 	void (*hash_init)(void *);
62 	int (*hash_update)(void *, const void *, u_int);
63 	void (*hash_finalize)(void *, void *);
64 	bool hmac;
65 };
66 
67 /*
68  * Internal functions, implemented in assembler.
69  */
70 void aesni_set_enckey(const uint8_t *userkey,
71     uint8_t *encrypt_schedule /*__aligned(16)*/, int number_of_rounds);
72 void aesni_set_deckey(const uint8_t *encrypt_schedule /*__aligned(16)*/,
73     uint8_t *decrypt_schedule /*__aligned(16)*/, int number_of_rounds);
74 
75 /*
76  * Slightly more public interfaces.
77  */
78 void aesni_encrypt_cbc(int rounds, const void *key_schedule /*__aligned(16)*/,
79     size_t len, const uint8_t *from, uint8_t *to,
80     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
81 void aesni_decrypt_cbc(int rounds, const void *key_schedule /*__aligned(16)*/,
82     size_t len, uint8_t *buf, const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
83 void aesni_encrypt_ecb(int rounds, const void *key_schedule /*__aligned(16)*/,
84     size_t len, const uint8_t *from, uint8_t *to);
85 void aesni_decrypt_ecb(int rounds, const void *key_schedule /*__aligned(16)*/,
86     size_t len, const uint8_t *from, uint8_t *to);
87 void aesni_encrypt_icm(int rounds, const void *key_schedule /*__aligned(16)*/,
88     size_t len, const uint8_t *from, uint8_t *to,
89     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
90 
91 void aesni_encrypt_xts(int rounds, const void *data_schedule /*__aligned(16)*/,
92     const void *tweak_schedule /*__aligned(16)*/, size_t len,
93     const uint8_t *from, uint8_t *to,
94     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
95 void aesni_decrypt_xts(int rounds, const void *data_schedule /*__aligned(16)*/,
96     const void *tweak_schedule /*__aligned(16)*/, size_t len,
97     const uint8_t *from, uint8_t *to,
98     const uint8_t iv[__min_size(AES_BLOCK_LEN)]);
99 
100 /* GCM & GHASH functions */
101 void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
102     const unsigned char *addt, const unsigned char *ivec,
103     unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
104     const unsigned char *key, int nr);
105 int AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
106     const unsigned char *addt, const unsigned char *ivec,
107     const unsigned char *tag, uint32_t nbytes, uint32_t abytes, int ibytes,
108     const unsigned char *key, int nr);
109 
110 /* CCM + CBC-MAC functions */
111 void AES_CCM_encrypt(const unsigned char *in, unsigned char *out,
112     const unsigned char *addt, const unsigned char *ivec,
113     unsigned char *tag, uint32_t nbytes, uint32_t abytes, int nlen,
114     int tag_length, const unsigned char *key, int nr);
115 int AES_CCM_decrypt(const unsigned char *in, unsigned char *out,
116     const unsigned char *addt, const unsigned char *ivec,
117     const unsigned char *tag, uint32_t nbytes, uint32_t abytes, int nlen,
118     int tag_length, const unsigned char *key, int nr);
119 void aesni_cipher_setup_common(struct aesni_session *ses,
120     const struct crypto_session_params *csp, const uint8_t *key, int keylen);
121 
122 #endif /* _AESNI_H_ */
123