xref: /freebsd/sys/contrib/openzfs/module/icp/include/aes/aes_impl.h (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_AES_IMPL_H
27 #define	_AES_IMPL_H
28 
29 /*
30  * Common definitions used by AES.
31  */
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #include <sys/zfs_context.h>
38 #include <sys/crypto/common.h>
39 
40 /* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */
41 #define	IS_P2ALIGNED2(v, w, a) \
42 	((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0)
43 
44 #define	AES_BLOCK_LEN	16	/* bytes */
45 /* Round constant length, in number of 32-bit elements: */
46 #define	RC_LENGTH	(5 * ((AES_BLOCK_LEN) / 4 - 2))
47 
48 #define	AES_COPY_BLOCK(src, dst) \
49 	(dst)[0] = (src)[0]; \
50 	(dst)[1] = (src)[1]; \
51 	(dst)[2] = (src)[2]; \
52 	(dst)[3] = (src)[3]; \
53 	(dst)[4] = (src)[4]; \
54 	(dst)[5] = (src)[5]; \
55 	(dst)[6] = (src)[6]; \
56 	(dst)[7] = (src)[7]; \
57 	(dst)[8] = (src)[8]; \
58 	(dst)[9] = (src)[9]; \
59 	(dst)[10] = (src)[10]; \
60 	(dst)[11] = (src)[11]; \
61 	(dst)[12] = (src)[12]; \
62 	(dst)[13] = (src)[13]; \
63 	(dst)[14] = (src)[14]; \
64 	(dst)[15] = (src)[15]
65 
66 #define	AES_XOR_BLOCK(src, dst) \
67 	(dst)[0] ^= (src)[0]; \
68 	(dst)[1] ^= (src)[1]; \
69 	(dst)[2] ^= (src)[2]; \
70 	(dst)[3] ^= (src)[3]; \
71 	(dst)[4] ^= (src)[4]; \
72 	(dst)[5] ^= (src)[5]; \
73 	(dst)[6] ^= (src)[6]; \
74 	(dst)[7] ^= (src)[7]; \
75 	(dst)[8] ^= (src)[8]; \
76 	(dst)[9] ^= (src)[9]; \
77 	(dst)[10] ^= (src)[10]; \
78 	(dst)[11] ^= (src)[11]; \
79 	(dst)[12] ^= (src)[12]; \
80 	(dst)[13] ^= (src)[13]; \
81 	(dst)[14] ^= (src)[14]; \
82 	(dst)[15] ^= (src)[15]
83 
84 /* AES key size definitions */
85 #define	AES_MINBITS		128
86 #define	AES_MAXBITS		256
87 
88 /* AES key schedule may be implemented with 32- or 64-bit elements: */
89 #define	AES_32BIT_KS		32
90 #define	AES_64BIT_KS		64
91 
92 #define	MAX_AES_NR		14 /* Maximum number of rounds */
93 #define	MAX_AES_NB		4  /* Number of columns comprising a state */
94 
95 typedef union {
96 #ifdef	sun4u
97 	uint64_t	ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
98 #endif
99 	uint32_t	ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
100 } aes_ks_t;
101 
102 typedef struct aes_impl_ops aes_impl_ops_t;
103 
104 /*
105  * The absolute offset of the encr_ks (0) and the nr (504) fields are hard
106  * coded in aesni-gcm-x86_64, so please don't change (or adjust accordingly).
107  */
108 typedef struct aes_key aes_key_t;
109 struct aes_key {
110 	aes_ks_t	encr_ks;  /* encryption key schedule */
111 	aes_ks_t	decr_ks;  /* decryption key schedule */
112 #ifdef __amd64
113 	long double	align128; /* Align fields above for Intel AES-NI */
114 #endif	/* __amd64 */
115 	const aes_impl_ops_t	*ops;	/* ops associated with this schedule */
116 	int		nr;	  /* number of rounds (10, 12, or 14) */
117 	int		type;	  /* key schedule size (32 or 64 bits) */
118 };
119 
120 /*
121  * Core AES functions.
122  * ks and keysched are pointers to aes_key_t.
123  * They are declared void* as they are intended to be opaque types.
124  * Use function aes_alloc_keysched() to allocate memory for ks and keysched.
125  */
126 extern void *aes_alloc_keysched(size_t *size, int kmflag);
127 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,
128 	void *keysched);
129 extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);
130 extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);
131 
132 /*
133  * AES mode functions.
134  * The first 2 functions operate on 16-byte AES blocks.
135  */
136 extern void aes_copy_block(uint8_t *in, uint8_t *out);
137 extern void aes_xor_block(uint8_t *data, uint8_t *dst);
138 
139 /* Note: ctx is a pointer to aes_ctx_t defined in modes.h */
140 extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length,
141     crypto_data_t *out);
142 extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length,
143     crypto_data_t *out);
144 
145 /*
146  * The following definitions and declarations are only used by AES FIPS POST
147  */
148 #ifdef _AES_IMPL
149 
150 typedef enum aes_mech_type {
151 	AES_ECB_MECH_INFO_TYPE,		/* SUN_CKM_AES_ECB */
152 	AES_CBC_MECH_INFO_TYPE,		/* SUN_CKM_AES_CBC */
153 	AES_CBC_PAD_MECH_INFO_TYPE,	/* SUN_CKM_AES_CBC_PAD */
154 	AES_CTR_MECH_INFO_TYPE,		/* SUN_CKM_AES_CTR */
155 	AES_CCM_MECH_INFO_TYPE,		/* SUN_CKM_AES_CCM */
156 	AES_GCM_MECH_INFO_TYPE,		/* SUN_CKM_AES_GCM */
157 	AES_GMAC_MECH_INFO_TYPE		/* SUN_CKM_AES_GMAC */
158 } aes_mech_type_t;
159 
160 #endif /* _AES_IMPL */
161 
162 /*
163  * Methods used to define AES implementation
164  *
165  * @aes_gen_f Key generation
166  * @aes_enc_f Function encrypts one block
167  * @aes_dec_f Function decrypts one block
168  * @aes_will_work_f Function tests whether method will function
169  */
170 typedef void 		(*aes_generate_f)(aes_key_t *, const uint32_t *, int);
171 typedef void		(*aes_encrypt_f)(const uint32_t[], int,
172     const uint32_t[4], uint32_t[4]);
173 typedef void		(*aes_decrypt_f)(const uint32_t[], int,
174     const uint32_t[4], uint32_t[4]);
175 typedef boolean_t	(*aes_will_work_f)(void);
176 
177 #define	AES_IMPL_NAME_MAX (16)
178 
179 struct aes_impl_ops {
180 	aes_generate_f generate;
181 	aes_encrypt_f encrypt;
182 	aes_decrypt_f decrypt;
183 	aes_will_work_f is_supported;
184 	boolean_t needs_byteswap;
185 	char name[AES_IMPL_NAME_MAX];
186 };
187 
188 extern const aes_impl_ops_t aes_generic_impl;
189 #if defined(__x86_64)
190 extern const aes_impl_ops_t aes_x86_64_impl;
191 
192 /* These functions are used to execute amd64 instructions for AMD or Intel: */
193 extern int rijndael_key_setup_enc_amd64(uint32_t rk[],
194 	const uint32_t cipherKey[], int keyBits);
195 extern int rijndael_key_setup_dec_amd64(uint32_t rk[],
196 	const uint32_t cipherKey[], int keyBits);
197 extern void aes_encrypt_amd64(const uint32_t rk[], int Nr,
198 	const uint32_t pt[4], uint32_t ct[4]);
199 extern void aes_decrypt_amd64(const uint32_t rk[], int Nr,
200 	const uint32_t ct[4], uint32_t pt[4]);
201 #endif
202 #if defined(__x86_64) && defined(HAVE_AES)
203 extern const aes_impl_ops_t aes_aesni_impl;
204 #endif
205 
206 /*
207  * Initializes fastest implementation
208  */
209 void aes_impl_init(void);
210 
211 /*
212  * Returns optimal allowed AES implementation
213  */
214 const struct aes_impl_ops *aes_impl_get_ops(void);
215 
216 #ifdef	__cplusplus
217 }
218 #endif
219 
220 #endif	/* _AES_IMPL_H */
221