xref: /freebsd/sys/contrib/openzfs/module/icp/include/aes/aes_impl.h (revision 75e1fea68aaa613a20dfdcd0c59dd403aca02c49)
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 https://opensource.org/licenses/CDDL-1.0.
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 #include <sys/asm_linkage.h>
40 
41 /* Similar to sysmacros.h IS_P2ALIGNED, but checks two pointers: */
42 #define	IS_P2ALIGNED2(v, w, a) \
43 	((((uintptr_t)(v) | (uintptr_t)(w)) & ((uintptr_t)(a) - 1)) == 0)
44 
45 #define	AES_BLOCK_LEN	16	/* bytes */
46 /* Round constant length, in number of 32-bit elements: */
47 #define	RC_LENGTH	(5 * ((AES_BLOCK_LEN) / 4 - 2))
48 
49 #define	AES_COPY_BLOCK(src, dst) \
50 	(dst)[0] = (src)[0]; \
51 	(dst)[1] = (src)[1]; \
52 	(dst)[2] = (src)[2]; \
53 	(dst)[3] = (src)[3]; \
54 	(dst)[4] = (src)[4]; \
55 	(dst)[5] = (src)[5]; \
56 	(dst)[6] = (src)[6]; \
57 	(dst)[7] = (src)[7]; \
58 	(dst)[8] = (src)[8]; \
59 	(dst)[9] = (src)[9]; \
60 	(dst)[10] = (src)[10]; \
61 	(dst)[11] = (src)[11]; \
62 	(dst)[12] = (src)[12]; \
63 	(dst)[13] = (src)[13]; \
64 	(dst)[14] = (src)[14]; \
65 	(dst)[15] = (src)[15]
66 
67 #define	AES_XOR_BLOCK(src, dst) \
68 	(dst)[0] ^= (src)[0]; \
69 	(dst)[1] ^= (src)[1]; \
70 	(dst)[2] ^= (src)[2]; \
71 	(dst)[3] ^= (src)[3]; \
72 	(dst)[4] ^= (src)[4]; \
73 	(dst)[5] ^= (src)[5]; \
74 	(dst)[6] ^= (src)[6]; \
75 	(dst)[7] ^= (src)[7]; \
76 	(dst)[8] ^= (src)[8]; \
77 	(dst)[9] ^= (src)[9]; \
78 	(dst)[10] ^= (src)[10]; \
79 	(dst)[11] ^= (src)[11]; \
80 	(dst)[12] ^= (src)[12]; \
81 	(dst)[13] ^= (src)[13]; \
82 	(dst)[14] ^= (src)[14]; \
83 	(dst)[15] ^= (src)[15]
84 
85 /* AES key size definitions */
86 #define	AES_MINBITS		128
87 #define	AES_MAXBITS		256
88 
89 /* AES key schedule may be implemented with 32- or 64-bit elements: */
90 #define	AES_32BIT_KS		32
91 #define	AES_64BIT_KS		64
92 
93 #define	MAX_AES_NR		14 /* Maximum number of rounds */
94 #define	MAX_AES_NB		4  /* Number of columns comprising a state */
95 
96 typedef union {
97 #ifdef	sun4u
98 	uint64_t	ks64[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
99 #endif
100 	uint32_t	ks32[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
101 } aes_ks_t;
102 
103 typedef struct aes_impl_ops aes_impl_ops_t;
104 
105 /*
106  * The absolute offset of the encr_ks (0) and the nr (504) fields are hard
107  * coded in aesni-gcm-x86_64, so please don't change (or adjust accordingly).
108  */
109 typedef struct aes_key aes_key_t;
110 struct aes_key {
111 	aes_ks_t	encr_ks;  /* encryption key schedule */
112 	aes_ks_t	decr_ks;  /* decryption key schedule */
113 #ifdef __amd64
114 	long double	align128; /* Align fields above for Intel AES-NI */
115 #endif	/* __amd64 */
116 	const aes_impl_ops_t	*ops;	/* ops associated with this schedule */
117 	int		nr;	  /* number of rounds (10, 12, or 14) */
118 	int		type;	  /* key schedule size (32 or 64 bits) */
119 };
120 
121 /*
122  * Core AES functions.
123  * ks and keysched are pointers to aes_key_t.
124  * They are declared void* as they are intended to be opaque types.
125  * Use function aes_alloc_keysched() to allocate memory for ks and keysched.
126  */
127 extern void *aes_alloc_keysched(size_t *size, int kmflag);
128 extern void aes_init_keysched(const uint8_t *cipherKey, uint_t keyBits,
129 	void *keysched);
130 extern int aes_encrypt_block(const void *ks, const uint8_t *pt, uint8_t *ct);
131 extern int aes_decrypt_block(const void *ks, const uint8_t *ct, uint8_t *pt);
132 
133 /*
134  * AES mode functions.
135  * The first 2 functions operate on 16-byte AES blocks.
136  */
137 extern void aes_copy_block(uint8_t *in, uint8_t *out);
138 extern void aes_xor_block(uint8_t *data, uint8_t *dst);
139 
140 /* Note: ctx is a pointer to aes_ctx_t defined in modes.h */
141 extern int aes_encrypt_contiguous_blocks(void *ctx, char *data, size_t length,
142     crypto_data_t *out);
143 extern int aes_decrypt_contiguous_blocks(void *ctx, char *data, size_t length,
144     crypto_data_t *out);
145 
146 /*
147  * The following definitions and declarations are only used by AES FIPS POST
148  */
149 #ifdef _AES_IMPL
150 
151 typedef enum aes_mech_type {
152 	AES_CCM_MECH_INFO_TYPE,		/* SUN_CKM_AES_CCM */
153 	AES_GCM_MECH_INFO_TYPE,		/* SUN_CKM_AES_GCM */
154 } aes_mech_type_t;
155 
156 #endif /* _AES_IMPL */
157 
158 /*
159  * Methods used to define AES implementation
160  *
161  * @aes_gen_f Key generation
162  * @aes_enc_f Function encrypts one block
163  * @aes_dec_f Function decrypts one block
164  * @aes_will_work_f Function tests whether method will function
165  */
166 typedef void 		(*aes_generate_f)(aes_key_t *, const uint32_t *, int);
167 typedef void		(*aes_encrypt_f)(const uint32_t[], int,
168     const uint32_t[4], uint32_t[4]);
169 typedef void		(*aes_decrypt_f)(const uint32_t[], int,
170     const uint32_t[4], uint32_t[4]);
171 typedef boolean_t	(*aes_will_work_f)(void);
172 
173 #define	AES_IMPL_NAME_MAX (16)
174 
175 struct aes_impl_ops {
176 	aes_generate_f generate;
177 	aes_encrypt_f encrypt;
178 	aes_decrypt_f decrypt;
179 	aes_will_work_f is_supported;
180 	boolean_t needs_byteswap;
181 	char name[AES_IMPL_NAME_MAX];
182 };
183 
184 extern const aes_impl_ops_t aes_generic_impl;
185 #if defined(__x86_64)
186 extern const aes_impl_ops_t aes_x86_64_impl;
187 
188 /* These functions are used to execute amd64 instructions for AMD or Intel: */
189 extern ASMABI int rijndael_key_setup_enc_amd64(uint32_t rk[],
190 	const uint32_t cipherKey[], int keyBits);
191 extern ASMABI int rijndael_key_setup_dec_amd64(uint32_t rk[],
192 	const uint32_t cipherKey[], int keyBits);
193 extern ASMABI void aes_encrypt_amd64(const uint32_t rk[], int Nr,
194 	const uint32_t pt[4], uint32_t ct[4]);
195 extern ASMABI void aes_decrypt_amd64(const uint32_t rk[], int Nr,
196 	const uint32_t ct[4], uint32_t pt[4]);
197 #endif
198 #if defined(__x86_64) && defined(HAVE_AES)
199 extern const aes_impl_ops_t aes_aesni_impl;
200 #endif
201 
202 /*
203  * Initializes fastest implementation
204  */
205 void aes_impl_init(void);
206 
207 /*
208  * Returns optimal allowed AES implementation
209  */
210 const struct aes_impl_ops *aes_impl_get_ops(void);
211 
212 #ifdef	__cplusplus
213 }
214 #endif
215 
216 #endif	/* _AES_IMPL_H */
217