xref: /dragonfly/sys/dev/crypto/aesni/aesni.h (revision 7ff0fc30)
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  * $FreeBSD: src/sys/crypto/aesni/aesni.h,v 1.2 2010/09/23 11:57:25 pjd Exp $
27  */
28 
29 #ifndef _AESNI_H_
30 #define _AESNI_H_
31 
32 #include <sys/types.h>
33 #include <sys/queue.h>
34 
35 #include <opencrypto/cryptodev.h>
36 
37 #if defined(__x86_64__)
38 #include <machine/cpufunc.h>
39 #include <machine/cputypes.h>
40 #include <machine/md_var.h>
41 #include <machine/specialreg.h>
42 #endif
43 
44 #define AESNI_ALIGN	16
45 
46 #define	AES128_ROUNDS	10
47 #define	AES192_ROUNDS	12
48 #define	AES256_ROUNDS	14
49 #define	AES_SCHED_LEN	((AES256_ROUNDS + 1) * AES_BLOCK_LEN)
50 
51 struct aesni_session {
52 	uint8_t enc_schedule[AES_SCHED_LEN] __aligned(16);
53 	uint8_t dec_schedule[AES_SCHED_LEN] __aligned(16);
54 	uint8_t xts_schedule[AES_SCHED_LEN] __aligned(16);
55 	uint8_t iv[AES_BLOCK_LEN];
56 	int algo;
57 	int rounds;
58 	/* uint8_t *ses_ictx; */
59 	/* uint8_t *ses_octx; */
60 	/* int ses_mlen; */
61 	int used;
62 	uint32_t id;
63 	TAILQ_ENTRY(aesni_session) next;
64 #if 0
65 	struct fpu_kern_ctx fpu_ctx;
66 #endif
67 };
68 
69 /*
70  * Internal functions, implemented in assembler.
71  */
72 void aesni_enc(int rounds, const uint8_t *key_schedule,
73     const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN],
74     const uint8_t iv[AES_BLOCK_LEN]);
75 void aesni_dec(int rounds, const uint8_t *key_schedule,
76     const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN],
77     const uint8_t iv[AES_BLOCK_LEN]);
78 void aesni_set_enckey(const uint8_t *userkey, uint8_t *encrypt_schedule,
79     int number_of_rounds);
80 void aesni_set_deckey(const uint8_t *encrypt_schedule,
81     uint8_t *decrypt_schedule, int number_of_rounds);
82 
83 /*
84  * Slightly more public interfaces.
85  */
86 void aesni_encrypt_cbc(int rounds, const void *key_schedule, size_t len,
87     const uint8_t *from, uint8_t *to, const uint8_t iv[AES_BLOCK_LEN]);
88 void aesni_decrypt_cbc(int rounds, const void *key_schedule, size_t len,
89     const uint8_t *from, const uint8_t iv[AES_BLOCK_LEN]);
90 void aesni_encrypt_ecb(int rounds, const void *key_schedule, size_t len,
91     const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN]);
92 void aesni_decrypt_ecb(int rounds, const void *key_schedule, size_t len,
93     const uint8_t from[AES_BLOCK_LEN], uint8_t to[AES_BLOCK_LEN]);
94 
95 int aesni_cipher_setup(struct aesni_session *ses,
96     struct cryptoini *encini);
97 int aesni_cipher_process(struct aesni_session *ses,
98     struct cryptodesc *enccrd, struct cryptop *crp);
99 
100 uint8_t *aesni_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
101     int *allocated);
102 
103 #endif
104