1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23eda14cbcSMatt Macy  */
24eda14cbcSMatt Macy 
25eda14cbcSMatt Macy #include <sys/zfs_context.h>
262a58b312SMartin Matuska #include <sys/cmn_err.h>
27eda14cbcSMatt Macy #include <modes/modes.h>
28eda14cbcSMatt Macy #include <sys/crypto/common.h>
29eda14cbcSMatt Macy #include <sys/crypto/icp.h>
30eda14cbcSMatt Macy #include <sys/crypto/impl.h>
31eda14cbcSMatt Macy #include <sys/byteorder.h>
32eda14cbcSMatt Macy #include <sys/simd.h>
33eda14cbcSMatt Macy #include <modes/gcm_impl.h>
34eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
35eda14cbcSMatt Macy #include <aes/aes_impl.h>
36eda14cbcSMatt Macy #endif
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy #define	GHASH(c, d, t, o) \
39eda14cbcSMatt Macy 	xor_block((uint8_t *)(d), (uint8_t *)(c)->gcm_ghash); \
40eda14cbcSMatt Macy 	(o)->mul((uint64_t *)(void *)(c)->gcm_ghash, (c)->gcm_H, \
41eda14cbcSMatt Macy 	(uint64_t *)(void *)(t));
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy /* Select GCM implementation */
44eda14cbcSMatt Macy #define	IMPL_FASTEST	(UINT32_MAX)
45eda14cbcSMatt Macy #define	IMPL_CYCLE	(UINT32_MAX-1)
46eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
47eda14cbcSMatt Macy #define	IMPL_AVX	(UINT32_MAX-2)
48eda14cbcSMatt Macy #endif
49eda14cbcSMatt Macy #define	GCM_IMPL_READ(i) (*(volatile uint32_t *) &(i))
50eda14cbcSMatt Macy static uint32_t icp_gcm_impl = IMPL_FASTEST;
51eda14cbcSMatt Macy static uint32_t user_sel_impl = IMPL_FASTEST;
52eda14cbcSMatt Macy 
532a58b312SMartin Matuska static inline int gcm_init_ctx_impl(boolean_t, gcm_ctx_t *, char *, size_t,
542a58b312SMartin Matuska     int (*)(const void *, const uint8_t *, uint8_t *),
552a58b312SMartin Matuska     void (*)(uint8_t *, uint8_t *),
562a58b312SMartin Matuska     void (*)(uint8_t *, uint8_t *));
572a58b312SMartin Matuska 
58eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
59eda14cbcSMatt Macy /* Does the architecture we run on support the MOVBE instruction? */
60eda14cbcSMatt Macy boolean_t gcm_avx_can_use_movbe = B_FALSE;
61eda14cbcSMatt Macy /*
62eda14cbcSMatt Macy  * Whether to use the optimized openssl gcm and ghash implementations.
63eda14cbcSMatt Macy  * Set to true if module parameter icp_gcm_impl == "avx".
64eda14cbcSMatt Macy  */
65eda14cbcSMatt Macy static boolean_t gcm_use_avx = B_FALSE;
66eda14cbcSMatt Macy #define	GCM_IMPL_USE_AVX	(*(volatile boolean_t *)&gcm_use_avx)
67eda14cbcSMatt Macy 
6815f0b8c3SMartin Matuska extern boolean_t ASMABI atomic_toggle_boolean_nv(volatile boolean_t *);
697877fdebSMatt Macy 
70eda14cbcSMatt Macy static inline boolean_t gcm_avx_will_work(void);
71eda14cbcSMatt Macy static inline void gcm_set_avx(boolean_t);
72eda14cbcSMatt Macy static inline boolean_t gcm_toggle_avx(void);
737877fdebSMatt Macy static inline size_t gcm_simd_get_htab_size(boolean_t);
74eda14cbcSMatt Macy 
75eda14cbcSMatt Macy static int gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *, char *, size_t,
76eda14cbcSMatt Macy     crypto_data_t *, size_t);
77eda14cbcSMatt Macy 
78eda14cbcSMatt Macy static int gcm_encrypt_final_avx(gcm_ctx_t *, crypto_data_t *, size_t);
79eda14cbcSMatt Macy static int gcm_decrypt_final_avx(gcm_ctx_t *, crypto_data_t *, size_t);
802a58b312SMartin Matuska static int gcm_init_avx(gcm_ctx_t *, const uint8_t *, size_t, const uint8_t *,
81eda14cbcSMatt Macy     size_t, size_t);
82eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
83eda14cbcSMatt Macy 
84eda14cbcSMatt Macy /*
85eda14cbcSMatt Macy  * Encrypt multiple blocks of data in GCM mode.  Decrypt for GCM mode
86eda14cbcSMatt Macy  * is done in another function.
87eda14cbcSMatt Macy  */
88eda14cbcSMatt Macy int
gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t * ctx,char * data,size_t length,crypto_data_t * out,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))89eda14cbcSMatt Macy gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length,
90eda14cbcSMatt Macy     crypto_data_t *out, size_t block_size,
91eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
92eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
93eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
94eda14cbcSMatt Macy {
95eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
96eda14cbcSMatt Macy 	if (ctx->gcm_use_avx == B_TRUE)
97eda14cbcSMatt Macy 		return (gcm_mode_encrypt_contiguous_blocks_avx(
98eda14cbcSMatt Macy 		    ctx, data, length, out, block_size));
99eda14cbcSMatt Macy #endif
100eda14cbcSMatt Macy 
101eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
102eda14cbcSMatt Macy 	size_t remainder = length;
103eda14cbcSMatt Macy 	size_t need = 0;
104eda14cbcSMatt Macy 	uint8_t *datap = (uint8_t *)data;
105eda14cbcSMatt Macy 	uint8_t *blockp;
106eda14cbcSMatt Macy 	uint8_t *lastp;
107eda14cbcSMatt Macy 	void *iov_or_mp;
108eda14cbcSMatt Macy 	offset_t offset;
109eda14cbcSMatt Macy 	uint8_t *out_data_1;
110eda14cbcSMatt Macy 	uint8_t *out_data_2;
111eda14cbcSMatt Macy 	size_t out_data_1_len;
112eda14cbcSMatt Macy 	uint64_t counter;
113eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	if (length + ctx->gcm_remainder_len < block_size) {
116eda14cbcSMatt Macy 		/* accumulate bytes here and return */
117da5137abSMartin Matuska 		memcpy((uint8_t *)ctx->gcm_remainder + ctx->gcm_remainder_len,
118da5137abSMartin Matuska 		    datap,
119eda14cbcSMatt Macy 		    length);
120eda14cbcSMatt Macy 		ctx->gcm_remainder_len += length;
121eda14cbcSMatt Macy 		if (ctx->gcm_copy_to == NULL) {
122eda14cbcSMatt Macy 			ctx->gcm_copy_to = datap;
123eda14cbcSMatt Macy 		}
124eda14cbcSMatt Macy 		return (CRYPTO_SUCCESS);
125eda14cbcSMatt Macy 	}
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy 	crypto_init_ptrs(out, &iov_or_mp, &offset);
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
130eda14cbcSMatt Macy 	do {
131eda14cbcSMatt Macy 		/* Unprocessed data from last call. */
132eda14cbcSMatt Macy 		if (ctx->gcm_remainder_len > 0) {
133eda14cbcSMatt Macy 			need = block_size - ctx->gcm_remainder_len;
134eda14cbcSMatt Macy 
135eda14cbcSMatt Macy 			if (need > remainder)
136eda14cbcSMatt Macy 				return (CRYPTO_DATA_LEN_RANGE);
137eda14cbcSMatt Macy 
138da5137abSMartin Matuska 			memcpy(&((uint8_t *)ctx->gcm_remainder)
139da5137abSMartin Matuska 			    [ctx->gcm_remainder_len], datap, need);
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy 			blockp = (uint8_t *)ctx->gcm_remainder;
142eda14cbcSMatt Macy 		} else {
143eda14cbcSMatt Macy 			blockp = datap;
144eda14cbcSMatt Macy 		}
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy 		/*
147eda14cbcSMatt Macy 		 * Increment counter. Counter bits are confined
148eda14cbcSMatt Macy 		 * to the bottom 32 bits of the counter block.
149eda14cbcSMatt Macy 		 */
150eda14cbcSMatt Macy 		counter = ntohll(ctx->gcm_cb[1] & counter_mask);
151eda14cbcSMatt Macy 		counter = htonll(counter + 1);
152eda14cbcSMatt Macy 		counter &= counter_mask;
153eda14cbcSMatt Macy 		ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
154eda14cbcSMatt Macy 
155eda14cbcSMatt Macy 		encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb,
156eda14cbcSMatt Macy 		    (uint8_t *)ctx->gcm_tmp);
157eda14cbcSMatt Macy 		xor_block(blockp, (uint8_t *)ctx->gcm_tmp);
158eda14cbcSMatt Macy 
159eda14cbcSMatt Macy 		lastp = (uint8_t *)ctx->gcm_tmp;
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += block_size;
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 		crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
164eda14cbcSMatt Macy 		    &out_data_1_len, &out_data_2, block_size);
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy 		/* copy block to where it belongs */
167eda14cbcSMatt Macy 		if (out_data_1_len == block_size) {
168eda14cbcSMatt Macy 			copy_block(lastp, out_data_1);
169eda14cbcSMatt Macy 		} else {
170da5137abSMartin Matuska 			memcpy(out_data_1, lastp, out_data_1_len);
171eda14cbcSMatt Macy 			if (out_data_2 != NULL) {
172da5137abSMartin Matuska 				memcpy(out_data_2,
173da5137abSMartin Matuska 				    lastp + out_data_1_len,
174eda14cbcSMatt Macy 				    block_size - out_data_1_len);
175eda14cbcSMatt Macy 			}
176eda14cbcSMatt Macy 		}
177eda14cbcSMatt Macy 		/* update offset */
178eda14cbcSMatt Macy 		out->cd_offset += block_size;
179eda14cbcSMatt Macy 
180eda14cbcSMatt Macy 		/* add ciphertext to the hash */
181eda14cbcSMatt Macy 		GHASH(ctx, ctx->gcm_tmp, ctx->gcm_ghash, gops);
182eda14cbcSMatt Macy 
183eda14cbcSMatt Macy 		/* Update pointer to next block of data to be processed. */
184eda14cbcSMatt Macy 		if (ctx->gcm_remainder_len != 0) {
185eda14cbcSMatt Macy 			datap += need;
186eda14cbcSMatt Macy 			ctx->gcm_remainder_len = 0;
187eda14cbcSMatt Macy 		} else {
188eda14cbcSMatt Macy 			datap += block_size;
189eda14cbcSMatt Macy 		}
190eda14cbcSMatt Macy 
191eda14cbcSMatt Macy 		remainder = (size_t)&data[length] - (size_t)datap;
192eda14cbcSMatt Macy 
193eda14cbcSMatt Macy 		/* Incomplete last block. */
194eda14cbcSMatt Macy 		if (remainder > 0 && remainder < block_size) {
195da5137abSMartin Matuska 			memcpy(ctx->gcm_remainder, datap, remainder);
196eda14cbcSMatt Macy 			ctx->gcm_remainder_len = remainder;
197eda14cbcSMatt Macy 			ctx->gcm_copy_to = datap;
198eda14cbcSMatt Macy 			goto out;
199eda14cbcSMatt Macy 		}
200eda14cbcSMatt Macy 		ctx->gcm_copy_to = NULL;
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy 	} while (remainder > 0);
203eda14cbcSMatt Macy out:
204eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
205eda14cbcSMatt Macy }
206eda14cbcSMatt Macy 
207eda14cbcSMatt Macy int
gcm_encrypt_final(gcm_ctx_t * ctx,crypto_data_t * out,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))208eda14cbcSMatt Macy gcm_encrypt_final(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
209eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
210eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
211eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
212eda14cbcSMatt Macy {
213e92ffd9bSMartin Matuska 	(void) copy_block;
214eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
215eda14cbcSMatt Macy 	if (ctx->gcm_use_avx == B_TRUE)
216eda14cbcSMatt Macy 		return (gcm_encrypt_final_avx(ctx, out, block_size));
217eda14cbcSMatt Macy #endif
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
220eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
221eda14cbcSMatt Macy 	uint8_t *ghash, *macp = NULL;
222eda14cbcSMatt Macy 	int i, rv;
223eda14cbcSMatt Macy 
224eda14cbcSMatt Macy 	if (out->cd_length <
225eda14cbcSMatt Macy 	    (ctx->gcm_remainder_len + ctx->gcm_tag_len)) {
226eda14cbcSMatt Macy 		return (CRYPTO_DATA_LEN_RANGE);
227eda14cbcSMatt Macy 	}
228eda14cbcSMatt Macy 
229eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
230eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
231eda14cbcSMatt Macy 
232eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
233eda14cbcSMatt Macy 		uint64_t counter;
234eda14cbcSMatt Macy 		uint8_t *tmpp = (uint8_t *)ctx->gcm_tmp;
235eda14cbcSMatt Macy 
236eda14cbcSMatt Macy 		/*
237eda14cbcSMatt Macy 		 * Here is where we deal with data that is not a
238eda14cbcSMatt Macy 		 * multiple of the block size.
239eda14cbcSMatt Macy 		 */
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 		/*
242eda14cbcSMatt Macy 		 * Increment counter.
243eda14cbcSMatt Macy 		 */
244eda14cbcSMatt Macy 		counter = ntohll(ctx->gcm_cb[1] & counter_mask);
245eda14cbcSMatt Macy 		counter = htonll(counter + 1);
246eda14cbcSMatt Macy 		counter &= counter_mask;
247eda14cbcSMatt Macy 		ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
248eda14cbcSMatt Macy 
249eda14cbcSMatt Macy 		encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb,
250eda14cbcSMatt Macy 		    (uint8_t *)ctx->gcm_tmp);
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy 		macp = (uint8_t *)ctx->gcm_remainder;
253da5137abSMartin Matuska 		memset(macp + ctx->gcm_remainder_len, 0,
254eda14cbcSMatt Macy 		    block_size - ctx->gcm_remainder_len);
255eda14cbcSMatt Macy 
256eda14cbcSMatt Macy 		/* XOR with counter block */
257eda14cbcSMatt Macy 		for (i = 0; i < ctx->gcm_remainder_len; i++) {
258eda14cbcSMatt Macy 			macp[i] ^= tmpp[i];
259eda14cbcSMatt Macy 		}
260eda14cbcSMatt Macy 
261eda14cbcSMatt Macy 		/* add ciphertext to the hash */
262eda14cbcSMatt Macy 		GHASH(ctx, macp, ghash, gops);
263eda14cbcSMatt Macy 
264eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += ctx->gcm_remainder_len;
265eda14cbcSMatt Macy 	}
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] =
268eda14cbcSMatt Macy 	    htonll(CRYPTO_BYTES2BITS(ctx->gcm_processed_data_len));
269eda14cbcSMatt Macy 	GHASH(ctx, ctx->gcm_len_a_len_c, ghash, gops);
270eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_J0,
271eda14cbcSMatt Macy 	    (uint8_t *)ctx->gcm_J0);
272eda14cbcSMatt Macy 	xor_block((uint8_t *)ctx->gcm_J0, ghash);
273eda14cbcSMatt Macy 
274eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
275eda14cbcSMatt Macy 		rv = crypto_put_output_data(macp, out, ctx->gcm_remainder_len);
276eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS)
277eda14cbcSMatt Macy 			return (rv);
278eda14cbcSMatt Macy 	}
279eda14cbcSMatt Macy 	out->cd_offset += ctx->gcm_remainder_len;
280eda14cbcSMatt Macy 	ctx->gcm_remainder_len = 0;
281eda14cbcSMatt Macy 	rv = crypto_put_output_data(ghash, out, ctx->gcm_tag_len);
282eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS)
283eda14cbcSMatt Macy 		return (rv);
284eda14cbcSMatt Macy 	out->cd_offset += ctx->gcm_tag_len;
285eda14cbcSMatt Macy 
286eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
287eda14cbcSMatt Macy }
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy /*
290eda14cbcSMatt Macy  * This will only deal with decrypting the last block of the input that
291eda14cbcSMatt Macy  * might not be a multiple of block length.
292eda14cbcSMatt Macy  */
293eda14cbcSMatt Macy static void
gcm_decrypt_incomplete_block(gcm_ctx_t * ctx,size_t block_size,size_t index,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))294eda14cbcSMatt Macy gcm_decrypt_incomplete_block(gcm_ctx_t *ctx, size_t block_size, size_t index,
295eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
296eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
297eda14cbcSMatt Macy {
298eda14cbcSMatt Macy 	uint8_t *datap, *outp, *counterp;
299eda14cbcSMatt Macy 	uint64_t counter;
300eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
301eda14cbcSMatt Macy 	int i;
302eda14cbcSMatt Macy 
303eda14cbcSMatt Macy 	/*
304eda14cbcSMatt Macy 	 * Increment counter.
305eda14cbcSMatt Macy 	 * Counter bits are confined to the bottom 32 bits
306eda14cbcSMatt Macy 	 */
307eda14cbcSMatt Macy 	counter = ntohll(ctx->gcm_cb[1] & counter_mask);
308eda14cbcSMatt Macy 	counter = htonll(counter + 1);
309eda14cbcSMatt Macy 	counter &= counter_mask;
310eda14cbcSMatt Macy 	ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
311eda14cbcSMatt Macy 
312eda14cbcSMatt Macy 	datap = (uint8_t *)ctx->gcm_remainder;
313eda14cbcSMatt Macy 	outp = &((ctx->gcm_pt_buf)[index]);
314eda14cbcSMatt Macy 	counterp = (uint8_t *)ctx->gcm_tmp;
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy 	/* authentication tag */
317da5137abSMartin Matuska 	memset((uint8_t *)ctx->gcm_tmp, 0, block_size);
318da5137abSMartin Matuska 	memcpy((uint8_t *)ctx->gcm_tmp, datap, ctx->gcm_remainder_len);
319eda14cbcSMatt Macy 
320eda14cbcSMatt Macy 	/* add ciphertext to the hash */
321eda14cbcSMatt Macy 	GHASH(ctx, ctx->gcm_tmp, ctx->gcm_ghash, gcm_impl_get_ops());
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy 	/* decrypt remaining ciphertext */
324eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, counterp);
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	/* XOR with counter block */
327eda14cbcSMatt Macy 	for (i = 0; i < ctx->gcm_remainder_len; i++) {
328eda14cbcSMatt Macy 		outp[i] = datap[i] ^ counterp[i];
329eda14cbcSMatt Macy 	}
330eda14cbcSMatt Macy }
331eda14cbcSMatt Macy 
332eda14cbcSMatt Macy int
gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t * ctx,char * data,size_t length,crypto_data_t * out,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))333eda14cbcSMatt Macy gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length,
334eda14cbcSMatt Macy     crypto_data_t *out, size_t block_size,
335eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
336eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
337eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
338eda14cbcSMatt Macy {
339e92ffd9bSMartin Matuska 	(void) out, (void) block_size, (void) encrypt_block, (void) copy_block,
340e92ffd9bSMartin Matuska 	    (void) xor_block;
341eda14cbcSMatt Macy 	size_t new_len;
342eda14cbcSMatt Macy 	uint8_t *new;
343eda14cbcSMatt Macy 
344eda14cbcSMatt Macy 	/*
345eda14cbcSMatt Macy 	 * Copy contiguous ciphertext input blocks to plaintext buffer.
346eda14cbcSMatt Macy 	 * Ciphertext will be decrypted in the final.
347eda14cbcSMatt Macy 	 */
348eda14cbcSMatt Macy 	if (length > 0) {
349eda14cbcSMatt Macy 		new_len = ctx->gcm_pt_buf_len + length;
350c03c5b1cSMartin Matuska 		new = vmem_alloc(new_len, KM_SLEEP);
351eda14cbcSMatt Macy 		if (new == NULL) {
352eda14cbcSMatt Macy 			vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
353eda14cbcSMatt Macy 			ctx->gcm_pt_buf = NULL;
354eda14cbcSMatt Macy 			return (CRYPTO_HOST_MEMORY);
355eda14cbcSMatt Macy 		}
356c03c5b1cSMartin Matuska 
357c03c5b1cSMartin Matuska 		if (ctx->gcm_pt_buf != NULL) {
358da5137abSMartin Matuska 			memcpy(new, ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
359eda14cbcSMatt Macy 			vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
360c03c5b1cSMartin Matuska 		} else {
361c03c5b1cSMartin Matuska 			ASSERT0(ctx->gcm_pt_buf_len);
362c03c5b1cSMartin Matuska 		}
363c03c5b1cSMartin Matuska 
364eda14cbcSMatt Macy 		ctx->gcm_pt_buf = new;
365eda14cbcSMatt Macy 		ctx->gcm_pt_buf_len = new_len;
366da5137abSMartin Matuska 		memcpy(&ctx->gcm_pt_buf[ctx->gcm_processed_data_len], data,
367eda14cbcSMatt Macy 		    length);
368eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += length;
369eda14cbcSMatt Macy 	}
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy 	ctx->gcm_remainder_len = 0;
372eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
373eda14cbcSMatt Macy }
374eda14cbcSMatt Macy 
375eda14cbcSMatt Macy int
gcm_decrypt_final(gcm_ctx_t * ctx,crypto_data_t * out,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))376eda14cbcSMatt Macy gcm_decrypt_final(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
377eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
378eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
379eda14cbcSMatt Macy {
380eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
381eda14cbcSMatt Macy 	if (ctx->gcm_use_avx == B_TRUE)
382eda14cbcSMatt Macy 		return (gcm_decrypt_final_avx(ctx, out, block_size));
383eda14cbcSMatt Macy #endif
384eda14cbcSMatt Macy 
385eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
386eda14cbcSMatt Macy 	size_t pt_len;
387eda14cbcSMatt Macy 	size_t remainder;
388eda14cbcSMatt Macy 	uint8_t *ghash;
389eda14cbcSMatt Macy 	uint8_t *blockp;
390eda14cbcSMatt Macy 	uint8_t *cbp;
391eda14cbcSMatt Macy 	uint64_t counter;
392eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
393eda14cbcSMatt Macy 	int processed = 0, rv;
394eda14cbcSMatt Macy 
395eda14cbcSMatt Macy 	ASSERT(ctx->gcm_processed_data_len == ctx->gcm_pt_buf_len);
396eda14cbcSMatt Macy 
397eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
398eda14cbcSMatt Macy 	pt_len = ctx->gcm_processed_data_len - ctx->gcm_tag_len;
399eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
400eda14cbcSMatt Macy 	blockp = ctx->gcm_pt_buf;
401eda14cbcSMatt Macy 	remainder = pt_len;
402eda14cbcSMatt Macy 	while (remainder > 0) {
403eda14cbcSMatt Macy 		/* Incomplete last block */
404eda14cbcSMatt Macy 		if (remainder < block_size) {
405da5137abSMartin Matuska 			memcpy(ctx->gcm_remainder, blockp, remainder);
406eda14cbcSMatt Macy 			ctx->gcm_remainder_len = remainder;
407eda14cbcSMatt Macy 			/*
408eda14cbcSMatt Macy 			 * not expecting anymore ciphertext, just
409eda14cbcSMatt Macy 			 * compute plaintext for the remaining input
410eda14cbcSMatt Macy 			 */
411eda14cbcSMatt Macy 			gcm_decrypt_incomplete_block(ctx, block_size,
412eda14cbcSMatt Macy 			    processed, encrypt_block, xor_block);
413eda14cbcSMatt Macy 			ctx->gcm_remainder_len = 0;
414eda14cbcSMatt Macy 			goto out;
415eda14cbcSMatt Macy 		}
416eda14cbcSMatt Macy 		/* add ciphertext to the hash */
417eda14cbcSMatt Macy 		GHASH(ctx, blockp, ghash, gops);
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy 		/*
420eda14cbcSMatt Macy 		 * Increment counter.
421eda14cbcSMatt Macy 		 * Counter bits are confined to the bottom 32 bits
422eda14cbcSMatt Macy 		 */
423eda14cbcSMatt Macy 		counter = ntohll(ctx->gcm_cb[1] & counter_mask);
424eda14cbcSMatt Macy 		counter = htonll(counter + 1);
425eda14cbcSMatt Macy 		counter &= counter_mask;
426eda14cbcSMatt Macy 		ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
427eda14cbcSMatt Macy 
428eda14cbcSMatt Macy 		cbp = (uint8_t *)ctx->gcm_tmp;
429eda14cbcSMatt Macy 		encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, cbp);
430eda14cbcSMatt Macy 
431eda14cbcSMatt Macy 		/* XOR with ciphertext */
432eda14cbcSMatt Macy 		xor_block(cbp, blockp);
433eda14cbcSMatt Macy 
434eda14cbcSMatt Macy 		processed += block_size;
435eda14cbcSMatt Macy 		blockp += block_size;
436eda14cbcSMatt Macy 		remainder -= block_size;
437eda14cbcSMatt Macy 	}
438eda14cbcSMatt Macy out:
439eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(pt_len));
440eda14cbcSMatt Macy 	GHASH(ctx, ctx->gcm_len_a_len_c, ghash, gops);
441eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_J0,
442eda14cbcSMatt Macy 	    (uint8_t *)ctx->gcm_J0);
443eda14cbcSMatt Macy 	xor_block((uint8_t *)ctx->gcm_J0, ghash);
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy 	/* compare the input authentication tag with what we calculated */
446da5137abSMartin Matuska 	if (memcmp(&ctx->gcm_pt_buf[pt_len], ghash, ctx->gcm_tag_len)) {
447eda14cbcSMatt Macy 		/* They don't match */
448eda14cbcSMatt Macy 		return (CRYPTO_INVALID_MAC);
449eda14cbcSMatt Macy 	} else {
450eda14cbcSMatt Macy 		rv = crypto_put_output_data(ctx->gcm_pt_buf, out, pt_len);
451eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS)
452eda14cbcSMatt Macy 			return (rv);
453eda14cbcSMatt Macy 		out->cd_offset += pt_len;
454eda14cbcSMatt Macy 	}
455eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
456eda14cbcSMatt Macy }
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy static int
gcm_validate_args(CK_AES_GCM_PARAMS * gcm_param)459eda14cbcSMatt Macy gcm_validate_args(CK_AES_GCM_PARAMS *gcm_param)
460eda14cbcSMatt Macy {
461eda14cbcSMatt Macy 	size_t tag_len;
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	/*
464eda14cbcSMatt Macy 	 * Check the length of the authentication tag (in bits).
465eda14cbcSMatt Macy 	 */
466eda14cbcSMatt Macy 	tag_len = gcm_param->ulTagBits;
467eda14cbcSMatt Macy 	switch (tag_len) {
468eda14cbcSMatt Macy 	case 32:
469eda14cbcSMatt Macy 	case 64:
470eda14cbcSMatt Macy 	case 96:
471eda14cbcSMatt Macy 	case 104:
472eda14cbcSMatt Macy 	case 112:
473eda14cbcSMatt Macy 	case 120:
474eda14cbcSMatt Macy 	case 128:
475eda14cbcSMatt Macy 		break;
476eda14cbcSMatt Macy 	default:
477eda14cbcSMatt Macy 		return (CRYPTO_MECHANISM_PARAM_INVALID);
478eda14cbcSMatt Macy 	}
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 	if (gcm_param->ulIvLen == 0)
481eda14cbcSMatt Macy 		return (CRYPTO_MECHANISM_PARAM_INVALID);
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
484eda14cbcSMatt Macy }
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy static void
gcm_format_initial_blocks(const uint8_t * iv,ulong_t iv_len,gcm_ctx_t * ctx,size_t block_size,void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))4872a58b312SMartin Matuska gcm_format_initial_blocks(const uint8_t *iv, ulong_t iv_len,
488eda14cbcSMatt Macy     gcm_ctx_t *ctx, size_t block_size,
489eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
490eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
491eda14cbcSMatt Macy {
492eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
493eda14cbcSMatt Macy 	uint8_t *cb;
494eda14cbcSMatt Macy 	ulong_t remainder = iv_len;
495eda14cbcSMatt Macy 	ulong_t processed = 0;
496eda14cbcSMatt Macy 	uint8_t *datap, *ghash;
497eda14cbcSMatt Macy 	uint64_t len_a_len_c[2];
498eda14cbcSMatt Macy 
499eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
500eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
501eda14cbcSMatt Macy 	cb = (uint8_t *)ctx->gcm_cb;
502eda14cbcSMatt Macy 	if (iv_len == 12) {
503da5137abSMartin Matuska 		memcpy(cb, iv, 12);
504eda14cbcSMatt Macy 		cb[12] = 0;
505eda14cbcSMatt Macy 		cb[13] = 0;
506eda14cbcSMatt Macy 		cb[14] = 0;
507eda14cbcSMatt Macy 		cb[15] = 1;
508eda14cbcSMatt Macy 		/* J0 will be used again in the final */
509eda14cbcSMatt Macy 		copy_block(cb, (uint8_t *)ctx->gcm_J0);
510eda14cbcSMatt Macy 	} else {
511eda14cbcSMatt Macy 		/* GHASH the IV */
512eda14cbcSMatt Macy 		do {
513eda14cbcSMatt Macy 			if (remainder < block_size) {
514da5137abSMartin Matuska 				memset(cb, 0, block_size);
515da5137abSMartin Matuska 				memcpy(cb, &(iv[processed]), remainder);
516eda14cbcSMatt Macy 				datap = (uint8_t *)cb;
517eda14cbcSMatt Macy 				remainder = 0;
518eda14cbcSMatt Macy 			} else {
519eda14cbcSMatt Macy 				datap = (uint8_t *)(&(iv[processed]));
520eda14cbcSMatt Macy 				processed += block_size;
521eda14cbcSMatt Macy 				remainder -= block_size;
522eda14cbcSMatt Macy 			}
523eda14cbcSMatt Macy 			GHASH(ctx, datap, ghash, gops);
524eda14cbcSMatt Macy 		} while (remainder > 0);
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 		len_a_len_c[0] = 0;
527eda14cbcSMatt Macy 		len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(iv_len));
528eda14cbcSMatt Macy 		GHASH(ctx, len_a_len_c, ctx->gcm_J0, gops);
529eda14cbcSMatt Macy 
530eda14cbcSMatt Macy 		/* J0 will be used again in the final */
531eda14cbcSMatt Macy 		copy_block((uint8_t *)ctx->gcm_J0, (uint8_t *)cb);
532eda14cbcSMatt Macy 	}
533eda14cbcSMatt Macy }
534eda14cbcSMatt Macy 
535eda14cbcSMatt Macy static int
gcm_init(gcm_ctx_t * ctx,const uint8_t * iv,size_t iv_len,const uint8_t * auth_data,size_t auth_data_len,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))5362a58b312SMartin Matuska gcm_init(gcm_ctx_t *ctx, const uint8_t *iv, size_t iv_len,
5372a58b312SMartin Matuska     const uint8_t *auth_data, size_t auth_data_len, size_t block_size,
538eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
539eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
540eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
541eda14cbcSMatt Macy {
542eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
543eda14cbcSMatt Macy 	uint8_t *ghash, *datap, *authp;
544eda14cbcSMatt Macy 	size_t remainder, processed;
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy 	/* encrypt zero block to get subkey H */
547da5137abSMartin Matuska 	memset(ctx->gcm_H, 0, sizeof (ctx->gcm_H));
548eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_H,
549eda14cbcSMatt Macy 	    (uint8_t *)ctx->gcm_H);
550eda14cbcSMatt Macy 
551eda14cbcSMatt Macy 	gcm_format_initial_blocks(iv, iv_len, ctx, block_size,
552eda14cbcSMatt Macy 	    copy_block, xor_block);
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
555eda14cbcSMatt Macy 	authp = (uint8_t *)ctx->gcm_tmp;
556eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
557da5137abSMartin Matuska 	memset(authp, 0, block_size);
558da5137abSMartin Matuska 	memset(ghash, 0, block_size);
559eda14cbcSMatt Macy 
560eda14cbcSMatt Macy 	processed = 0;
561eda14cbcSMatt Macy 	remainder = auth_data_len;
562eda14cbcSMatt Macy 	do {
563eda14cbcSMatt Macy 		if (remainder < block_size) {
564eda14cbcSMatt Macy 			/*
565eda14cbcSMatt Macy 			 * There's not a block full of data, pad rest of
566eda14cbcSMatt Macy 			 * buffer with zero
567eda14cbcSMatt Macy 			 */
568c03c5b1cSMartin Matuska 
569c03c5b1cSMartin Matuska 			if (auth_data != NULL) {
570da5137abSMartin Matuska 				memset(authp, 0, block_size);
571da5137abSMartin Matuska 				memcpy(authp, &(auth_data[processed]),
572da5137abSMartin Matuska 				    remainder);
573c03c5b1cSMartin Matuska 			} else {
574c03c5b1cSMartin Matuska 				ASSERT0(remainder);
575c03c5b1cSMartin Matuska 			}
576c03c5b1cSMartin Matuska 
577eda14cbcSMatt Macy 			datap = (uint8_t *)authp;
578eda14cbcSMatt Macy 			remainder = 0;
579eda14cbcSMatt Macy 		} else {
580eda14cbcSMatt Macy 			datap = (uint8_t *)(&(auth_data[processed]));
581eda14cbcSMatt Macy 			processed += block_size;
582eda14cbcSMatt Macy 			remainder -= block_size;
583eda14cbcSMatt Macy 		}
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 		/* add auth data to the hash */
586eda14cbcSMatt Macy 		GHASH(ctx, datap, ghash, gops);
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 	} while (remainder > 0);
589eda14cbcSMatt Macy 
590eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
591eda14cbcSMatt Macy }
592eda14cbcSMatt Macy 
593eda14cbcSMatt Macy /*
594eda14cbcSMatt Macy  * The following function is called at encrypt or decrypt init time
595eda14cbcSMatt Macy  * for AES GCM mode.
596eda14cbcSMatt Macy  */
597eda14cbcSMatt Macy int
gcm_init_ctx(gcm_ctx_t * gcm_ctx,char * param,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))598eda14cbcSMatt Macy gcm_init_ctx(gcm_ctx_t *gcm_ctx, char *param, size_t block_size,
599eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
600eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
601eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
602eda14cbcSMatt Macy {
6032a58b312SMartin Matuska 	return (gcm_init_ctx_impl(B_FALSE, gcm_ctx, param, block_size,
6042a58b312SMartin Matuska 	    encrypt_block, copy_block, xor_block));
6052a58b312SMartin Matuska }
6062a58b312SMartin Matuska 
6072a58b312SMartin Matuska /*
6082a58b312SMartin Matuska  * The following function is called at encrypt or decrypt init time
6092a58b312SMartin Matuska  * for AES GMAC mode.
6102a58b312SMartin Matuska  */
6112a58b312SMartin Matuska int
gmac_init_ctx(gcm_ctx_t * gcm_ctx,char * param,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))6122a58b312SMartin Matuska gmac_init_ctx(gcm_ctx_t *gcm_ctx, char *param, size_t block_size,
6132a58b312SMartin Matuska     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
6142a58b312SMartin Matuska     void (*copy_block)(uint8_t *, uint8_t *),
6152a58b312SMartin Matuska     void (*xor_block)(uint8_t *, uint8_t *))
6162a58b312SMartin Matuska {
6172a58b312SMartin Matuska 	return (gcm_init_ctx_impl(B_TRUE, gcm_ctx, param, block_size,
6182a58b312SMartin Matuska 	    encrypt_block, copy_block, xor_block));
6192a58b312SMartin Matuska }
6202a58b312SMartin Matuska 
6212a58b312SMartin Matuska /*
6222a58b312SMartin Matuska  * Init the GCM context struct. Handle the cycle and avx implementations here.
6232a58b312SMartin Matuska  * Initialization of a GMAC context differs slightly from a GCM context.
6242a58b312SMartin Matuska  */
6252a58b312SMartin Matuska static inline int
gcm_init_ctx_impl(boolean_t gmac_mode,gcm_ctx_t * gcm_ctx,char * param,size_t block_size,int (* encrypt_block)(const void *,const uint8_t *,uint8_t *),void (* copy_block)(uint8_t *,uint8_t *),void (* xor_block)(uint8_t *,uint8_t *))6262a58b312SMartin Matuska gcm_init_ctx_impl(boolean_t gmac_mode, gcm_ctx_t *gcm_ctx, char *param,
6272a58b312SMartin Matuska     size_t block_size, int (*encrypt_block)(const void *, const uint8_t *,
6282a58b312SMartin Matuska     uint8_t *), void (*copy_block)(uint8_t *, uint8_t *),
6292a58b312SMartin Matuska     void (*xor_block)(uint8_t *, uint8_t *))
6302a58b312SMartin Matuska {
631eda14cbcSMatt Macy 	CK_AES_GCM_PARAMS *gcm_param;
6322a58b312SMartin Matuska 	int rv = CRYPTO_SUCCESS;
6332a58b312SMartin Matuska 	size_t tag_len, iv_len;
634eda14cbcSMatt Macy 
635eda14cbcSMatt Macy 	if (param != NULL) {
636eda14cbcSMatt Macy 		gcm_param = (CK_AES_GCM_PARAMS *)(void *)param;
637eda14cbcSMatt Macy 
6382a58b312SMartin Matuska 		if (gmac_mode == B_FALSE) {
6392a58b312SMartin Matuska 			/* GCM mode. */
640eda14cbcSMatt Macy 			if ((rv = gcm_validate_args(gcm_param)) != 0) {
641eda14cbcSMatt Macy 				return (rv);
642eda14cbcSMatt Macy 			}
6432a58b312SMartin Matuska 			gcm_ctx->gcm_flags |= GCM_MODE;
644eda14cbcSMatt Macy 
6452a58b312SMartin Matuska 			size_t tbits = gcm_param->ulTagBits;
6462a58b312SMartin Matuska 			tag_len = CRYPTO_BITS2BYTES(tbits);
6472a58b312SMartin Matuska 			iv_len = gcm_param->ulIvLen;
6482a58b312SMartin Matuska 		} else {
6492a58b312SMartin Matuska 			/* GMAC mode. */
6502a58b312SMartin Matuska 			gcm_ctx->gcm_flags |= GMAC_MODE;
6512a58b312SMartin Matuska 			tag_len = CRYPTO_BITS2BYTES(AES_GMAC_TAG_BITS);
6522a58b312SMartin Matuska 			iv_len = AES_GMAC_IV_LEN;
6532a58b312SMartin Matuska 		}
6542a58b312SMartin Matuska 		gcm_ctx->gcm_tag_len = tag_len;
655eda14cbcSMatt Macy 		gcm_ctx->gcm_processed_data_len = 0;
656eda14cbcSMatt Macy 
657eda14cbcSMatt Macy 		/* these values are in bits */
658eda14cbcSMatt Macy 		gcm_ctx->gcm_len_a_len_c[0]
659eda14cbcSMatt Macy 		    = htonll(CRYPTO_BYTES2BITS(gcm_param->ulAADLen));
660eda14cbcSMatt Macy 	} else {
661eda14cbcSMatt Macy 		return (CRYPTO_MECHANISM_PARAM_INVALID);
662eda14cbcSMatt Macy 	}
663eda14cbcSMatt Macy 
6642a58b312SMartin Matuska 	const uint8_t *iv = (const uint8_t *)gcm_param->pIv;
6652a58b312SMartin Matuska 	const uint8_t *aad = (const uint8_t *)gcm_param->pAAD;
6662a58b312SMartin Matuska 	size_t aad_len = gcm_param->ulAADLen;
6672a58b312SMartin Matuska 
668eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
6692a58b312SMartin Matuska 	boolean_t needs_bswap =
6702a58b312SMartin Matuska 	    ((aes_key_t *)gcm_ctx->gcm_keysched)->ops->needs_byteswap;
6712a58b312SMartin Matuska 
672eda14cbcSMatt Macy 	if (GCM_IMPL_READ(icp_gcm_impl) != IMPL_CYCLE) {
673eda14cbcSMatt Macy 		gcm_ctx->gcm_use_avx = GCM_IMPL_USE_AVX;
674eda14cbcSMatt Macy 	} else {
675eda14cbcSMatt Macy 		/*
676eda14cbcSMatt Macy 		 * Handle the "cycle" implementation by creating avx and
677eda14cbcSMatt Macy 		 * non-avx contexts alternately.
678eda14cbcSMatt Macy 		 */
679eda14cbcSMatt Macy 		gcm_ctx->gcm_use_avx = gcm_toggle_avx();
6802a58b312SMartin Matuska 
6812a58b312SMartin Matuska 		/* The avx impl. doesn't handle byte swapped key schedules. */
6822a58b312SMartin Matuska 		if (gcm_ctx->gcm_use_avx == B_TRUE && needs_bswap == B_TRUE) {
683eda14cbcSMatt Macy 			gcm_ctx->gcm_use_avx = B_FALSE;
684eda14cbcSMatt Macy 		}
6852a58b312SMartin Matuska 		/*
6862a58b312SMartin Matuska 		 * If this is a GCM context, use the MOVBE and the BSWAP
6872a58b312SMartin Matuska 		 * variants alternately. GMAC contexts code paths do not
6882a58b312SMartin Matuska 		 * use the MOVBE instruction.
6892a58b312SMartin Matuska 		 */
6902a58b312SMartin Matuska 		if (gcm_ctx->gcm_use_avx == B_TRUE && gmac_mode == B_FALSE &&
691eda14cbcSMatt Macy 		    zfs_movbe_available() == B_TRUE) {
692eda14cbcSMatt Macy 			(void) atomic_toggle_boolean_nv(
693eda14cbcSMatt Macy 			    (volatile boolean_t *)&gcm_avx_can_use_movbe);
694eda14cbcSMatt Macy 		}
695eda14cbcSMatt Macy 	}
696eda14cbcSMatt Macy 	/*
6972a58b312SMartin Matuska 	 * We don't handle byte swapped key schedules in the avx code path,
6982a58b312SMartin Matuska 	 * still they could be created by the aes generic implementation.
6992a58b312SMartin Matuska 	 * Make sure not to use them since we'll corrupt data if we do.
700eda14cbcSMatt Macy 	 */
7012a58b312SMartin Matuska 	if (gcm_ctx->gcm_use_avx == B_TRUE && needs_bswap == B_TRUE) {
702eda14cbcSMatt Macy 		gcm_ctx->gcm_use_avx = B_FALSE;
7032a58b312SMartin Matuska 
7042a58b312SMartin Matuska 		cmn_err_once(CE_WARN,
7052a58b312SMartin Matuska 		    "ICP: Can't use the aes generic or cycle implementations "
7062a58b312SMartin Matuska 		    "in combination with the gcm avx implementation!");
7072a58b312SMartin Matuska 		cmn_err_once(CE_WARN,
7082a58b312SMartin Matuska 		    "ICP: Falling back to a compatible implementation, "
7092a58b312SMartin Matuska 		    "aes-gcm performance will likely be degraded.");
7102a58b312SMartin Matuska 		cmn_err_once(CE_WARN,
7112a58b312SMartin Matuska 		    "ICP: Choose at least the x86_64 aes implementation to "
7122a58b312SMartin Matuska 		    "restore performance.");
713eda14cbcSMatt Macy 	}
7142a58b312SMartin Matuska 
7157877fdebSMatt Macy 	/* Allocate Htab memory as needed. */
7167877fdebSMatt Macy 	if (gcm_ctx->gcm_use_avx == B_TRUE) {
7177877fdebSMatt Macy 		size_t htab_len = gcm_simd_get_htab_size(gcm_ctx->gcm_use_avx);
7187877fdebSMatt Macy 
7197877fdebSMatt Macy 		if (htab_len == 0) {
7207877fdebSMatt Macy 			return (CRYPTO_MECHANISM_PARAM_INVALID);
7217877fdebSMatt Macy 		}
7227877fdebSMatt Macy 		gcm_ctx->gcm_htab_len = htab_len;
7237877fdebSMatt Macy 		gcm_ctx->gcm_Htable =
72415f0b8c3SMartin Matuska 		    kmem_alloc(htab_len, KM_SLEEP);
7257877fdebSMatt Macy 
7267877fdebSMatt Macy 		if (gcm_ctx->gcm_Htable == NULL) {
7277877fdebSMatt Macy 			return (CRYPTO_HOST_MEMORY);
7287877fdebSMatt Macy 		}
7297877fdebSMatt Macy 	}
730eda14cbcSMatt Macy 	/* Avx and non avx context initialization differs from here on. */
731eda14cbcSMatt Macy 	if (gcm_ctx->gcm_use_avx == B_FALSE) {
732eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
7332a58b312SMartin Matuska 		if (gcm_init(gcm_ctx, iv, iv_len, aad, aad_len, block_size,
7342a58b312SMartin Matuska 		    encrypt_block, copy_block, xor_block) != CRYPTO_SUCCESS) {
735eda14cbcSMatt Macy 			rv = CRYPTO_MECHANISM_PARAM_INVALID;
736eda14cbcSMatt Macy 		}
737eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
738eda14cbcSMatt Macy 	} else {
7392a58b312SMartin Matuska 		if (gcm_init_avx(gcm_ctx, iv, iv_len, aad, aad_len,
7402a58b312SMartin Matuska 		    block_size) != CRYPTO_SUCCESS) {
741eda14cbcSMatt Macy 			rv = CRYPTO_MECHANISM_PARAM_INVALID;
742eda14cbcSMatt Macy 		}
743eda14cbcSMatt Macy 	}
744eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
745eda14cbcSMatt Macy 
746eda14cbcSMatt Macy 	return (rv);
747eda14cbcSMatt Macy }
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy void *
gcm_alloc_ctx(int kmflag)750eda14cbcSMatt Macy gcm_alloc_ctx(int kmflag)
751eda14cbcSMatt Macy {
752eda14cbcSMatt Macy 	gcm_ctx_t *gcm_ctx;
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy 	if ((gcm_ctx = kmem_zalloc(sizeof (gcm_ctx_t), kmflag)) == NULL)
755eda14cbcSMatt Macy 		return (NULL);
756eda14cbcSMatt Macy 
757eda14cbcSMatt Macy 	gcm_ctx->gcm_flags = GCM_MODE;
758eda14cbcSMatt Macy 	return (gcm_ctx);
759eda14cbcSMatt Macy }
760eda14cbcSMatt Macy 
761eda14cbcSMatt Macy void *
gmac_alloc_ctx(int kmflag)762eda14cbcSMatt Macy gmac_alloc_ctx(int kmflag)
763eda14cbcSMatt Macy {
764eda14cbcSMatt Macy 	gcm_ctx_t *gcm_ctx;
765eda14cbcSMatt Macy 
766eda14cbcSMatt Macy 	if ((gcm_ctx = kmem_zalloc(sizeof (gcm_ctx_t), kmflag)) == NULL)
767eda14cbcSMatt Macy 		return (NULL);
768eda14cbcSMatt Macy 
769eda14cbcSMatt Macy 	gcm_ctx->gcm_flags = GMAC_MODE;
770eda14cbcSMatt Macy 	return (gcm_ctx);
771eda14cbcSMatt Macy }
772eda14cbcSMatt Macy 
773eda14cbcSMatt Macy /* GCM implementation that contains the fastest methods */
774eda14cbcSMatt Macy static gcm_impl_ops_t gcm_fastest_impl = {
775eda14cbcSMatt Macy 	.name = "fastest"
776eda14cbcSMatt Macy };
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy /* All compiled in implementations */
779e92ffd9bSMartin Matuska static const gcm_impl_ops_t *gcm_all_impl[] = {
780eda14cbcSMatt Macy 	&gcm_generic_impl,
781eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_PCLMULQDQ)
782eda14cbcSMatt Macy 	&gcm_pclmulqdq_impl,
783eda14cbcSMatt Macy #endif
784eda14cbcSMatt Macy };
785eda14cbcSMatt Macy 
786eda14cbcSMatt Macy /* Indicate that benchmark has been completed */
787eda14cbcSMatt Macy static boolean_t gcm_impl_initialized = B_FALSE;
788eda14cbcSMatt Macy 
789eda14cbcSMatt Macy /* Hold all supported implementations */
790eda14cbcSMatt Macy static size_t gcm_supp_impl_cnt = 0;
791eda14cbcSMatt Macy static gcm_impl_ops_t *gcm_supp_impl[ARRAY_SIZE(gcm_all_impl)];
792eda14cbcSMatt Macy 
793eda14cbcSMatt Macy /*
794eda14cbcSMatt Macy  * Returns the GCM operations for encrypt/decrypt/key setup.  When a
795eda14cbcSMatt Macy  * SIMD implementation is not allowed in the current context, then
796eda14cbcSMatt Macy  * fallback to the fastest generic implementation.
797eda14cbcSMatt Macy  */
798eda14cbcSMatt Macy const gcm_impl_ops_t *
gcm_impl_get_ops(void)799716fd348SMartin Matuska gcm_impl_get_ops(void)
800eda14cbcSMatt Macy {
801eda14cbcSMatt Macy 	if (!kfpu_allowed())
802eda14cbcSMatt Macy 		return (&gcm_generic_impl);
803eda14cbcSMatt Macy 
804eda14cbcSMatt Macy 	const gcm_impl_ops_t *ops = NULL;
805eda14cbcSMatt Macy 	const uint32_t impl = GCM_IMPL_READ(icp_gcm_impl);
806eda14cbcSMatt Macy 
807eda14cbcSMatt Macy 	switch (impl) {
808eda14cbcSMatt Macy 	case IMPL_FASTEST:
809eda14cbcSMatt Macy 		ASSERT(gcm_impl_initialized);
810eda14cbcSMatt Macy 		ops = &gcm_fastest_impl;
811eda14cbcSMatt Macy 		break;
812eda14cbcSMatt Macy 	case IMPL_CYCLE:
813eda14cbcSMatt Macy 		/* Cycle through supported implementations */
814eda14cbcSMatt Macy 		ASSERT(gcm_impl_initialized);
815eda14cbcSMatt Macy 		ASSERT3U(gcm_supp_impl_cnt, >, 0);
816eda14cbcSMatt Macy 		static size_t cycle_impl_idx = 0;
817eda14cbcSMatt Macy 		size_t idx = (++cycle_impl_idx) % gcm_supp_impl_cnt;
818eda14cbcSMatt Macy 		ops = gcm_supp_impl[idx];
819eda14cbcSMatt Macy 		break;
820eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
821eda14cbcSMatt Macy 	case IMPL_AVX:
822eda14cbcSMatt Macy 		/*
823eda14cbcSMatt Macy 		 * Make sure that we return a valid implementation while
824eda14cbcSMatt Macy 		 * switching to the avx implementation since there still
825eda14cbcSMatt Macy 		 * may be unfinished non-avx contexts around.
826eda14cbcSMatt Macy 		 */
827eda14cbcSMatt Macy 		ops = &gcm_generic_impl;
828eda14cbcSMatt Macy 		break;
829eda14cbcSMatt Macy #endif
830eda14cbcSMatt Macy 	default:
831eda14cbcSMatt Macy 		ASSERT3U(impl, <, gcm_supp_impl_cnt);
832eda14cbcSMatt Macy 		ASSERT3U(gcm_supp_impl_cnt, >, 0);
833eda14cbcSMatt Macy 		if (impl < ARRAY_SIZE(gcm_all_impl))
834eda14cbcSMatt Macy 			ops = gcm_supp_impl[impl];
835eda14cbcSMatt Macy 		break;
836eda14cbcSMatt Macy 	}
837eda14cbcSMatt Macy 
838eda14cbcSMatt Macy 	ASSERT3P(ops, !=, NULL);
839eda14cbcSMatt Macy 
840eda14cbcSMatt Macy 	return (ops);
841eda14cbcSMatt Macy }
842eda14cbcSMatt Macy 
843eda14cbcSMatt Macy /*
844eda14cbcSMatt Macy  * Initialize all supported implementations.
845eda14cbcSMatt Macy  */
846eda14cbcSMatt Macy void
gcm_impl_init(void)847eda14cbcSMatt Macy gcm_impl_init(void)
848eda14cbcSMatt Macy {
849eda14cbcSMatt Macy 	gcm_impl_ops_t *curr_impl;
850eda14cbcSMatt Macy 	int i, c;
851eda14cbcSMatt Macy 
852eda14cbcSMatt Macy 	/* Move supported implementations into gcm_supp_impls */
853eda14cbcSMatt Macy 	for (i = 0, c = 0; i < ARRAY_SIZE(gcm_all_impl); i++) {
854eda14cbcSMatt Macy 		curr_impl = (gcm_impl_ops_t *)gcm_all_impl[i];
855eda14cbcSMatt Macy 
856eda14cbcSMatt Macy 		if (curr_impl->is_supported())
857eda14cbcSMatt Macy 			gcm_supp_impl[c++] = (gcm_impl_ops_t *)curr_impl;
858eda14cbcSMatt Macy 	}
859eda14cbcSMatt Macy 	gcm_supp_impl_cnt = c;
860eda14cbcSMatt Macy 
861eda14cbcSMatt Macy 	/*
862eda14cbcSMatt Macy 	 * Set the fastest implementation given the assumption that the
863eda14cbcSMatt Macy 	 * hardware accelerated version is the fastest.
864eda14cbcSMatt Macy 	 */
865eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_PCLMULQDQ)
866eda14cbcSMatt Macy 	if (gcm_pclmulqdq_impl.is_supported()) {
867eda14cbcSMatt Macy 		memcpy(&gcm_fastest_impl, &gcm_pclmulqdq_impl,
868eda14cbcSMatt Macy 		    sizeof (gcm_fastest_impl));
869eda14cbcSMatt Macy 	} else
870eda14cbcSMatt Macy #endif
871eda14cbcSMatt Macy 	{
872eda14cbcSMatt Macy 		memcpy(&gcm_fastest_impl, &gcm_generic_impl,
873eda14cbcSMatt Macy 		    sizeof (gcm_fastest_impl));
874eda14cbcSMatt Macy 	}
875eda14cbcSMatt Macy 
876eda14cbcSMatt Macy 	strlcpy(gcm_fastest_impl.name, "fastest", GCM_IMPL_NAME_MAX);
877eda14cbcSMatt Macy 
878eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
879eda14cbcSMatt Macy 	/*
880eda14cbcSMatt Macy 	 * Use the avx implementation if it's available and the implementation
881eda14cbcSMatt Macy 	 * hasn't changed from its default value of fastest on module load.
882eda14cbcSMatt Macy 	 */
883eda14cbcSMatt Macy 	if (gcm_avx_will_work()) {
884eda14cbcSMatt Macy #ifdef HAVE_MOVBE
885eda14cbcSMatt Macy 		if (zfs_movbe_available() == B_TRUE) {
886eda14cbcSMatt Macy 			atomic_swap_32(&gcm_avx_can_use_movbe, B_TRUE);
887eda14cbcSMatt Macy 		}
888eda14cbcSMatt Macy #endif
889eda14cbcSMatt Macy 		if (GCM_IMPL_READ(user_sel_impl) == IMPL_FASTEST) {
890eda14cbcSMatt Macy 			gcm_set_avx(B_TRUE);
891eda14cbcSMatt Macy 		}
892eda14cbcSMatt Macy 	}
893eda14cbcSMatt Macy #endif
894eda14cbcSMatt Macy 	/* Finish initialization */
895eda14cbcSMatt Macy 	atomic_swap_32(&icp_gcm_impl, user_sel_impl);
896eda14cbcSMatt Macy 	gcm_impl_initialized = B_TRUE;
897eda14cbcSMatt Macy }
898eda14cbcSMatt Macy 
899eda14cbcSMatt Macy static const struct {
900a0b956f5SMartin Matuska 	const char *name;
901eda14cbcSMatt Macy 	uint32_t sel;
902eda14cbcSMatt Macy } gcm_impl_opts[] = {
903eda14cbcSMatt Macy 		{ "cycle",	IMPL_CYCLE },
904eda14cbcSMatt Macy 		{ "fastest",	IMPL_FASTEST },
905eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
906eda14cbcSMatt Macy 		{ "avx",	IMPL_AVX },
907eda14cbcSMatt Macy #endif
908eda14cbcSMatt Macy };
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy /*
911eda14cbcSMatt Macy  * Function sets desired gcm implementation.
912eda14cbcSMatt Macy  *
913eda14cbcSMatt Macy  * If we are called before init(), user preference will be saved in
914eda14cbcSMatt Macy  * user_sel_impl, and applied in later init() call. This occurs when module
915eda14cbcSMatt Macy  * parameter is specified on module load. Otherwise, directly update
916eda14cbcSMatt Macy  * icp_gcm_impl.
917eda14cbcSMatt Macy  *
918eda14cbcSMatt Macy  * @val		Name of gcm implementation to use
919eda14cbcSMatt Macy  * @param	Unused.
920eda14cbcSMatt Macy  */
921eda14cbcSMatt Macy int
gcm_impl_set(const char * val)922eda14cbcSMatt Macy gcm_impl_set(const char *val)
923eda14cbcSMatt Macy {
924eda14cbcSMatt Macy 	int err = -EINVAL;
925eda14cbcSMatt Macy 	char req_name[GCM_IMPL_NAME_MAX];
926eda14cbcSMatt Macy 	uint32_t impl = GCM_IMPL_READ(user_sel_impl);
927eda14cbcSMatt Macy 	size_t i;
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 	/* sanitize input */
930eda14cbcSMatt Macy 	i = strnlen(val, GCM_IMPL_NAME_MAX);
931eda14cbcSMatt Macy 	if (i == 0 || i >= GCM_IMPL_NAME_MAX)
932eda14cbcSMatt Macy 		return (err);
933eda14cbcSMatt Macy 
934eda14cbcSMatt Macy 	strlcpy(req_name, val, GCM_IMPL_NAME_MAX);
935eda14cbcSMatt Macy 	while (i > 0 && isspace(req_name[i-1]))
936eda14cbcSMatt Macy 		i--;
937eda14cbcSMatt Macy 	req_name[i] = '\0';
938eda14cbcSMatt Macy 
939eda14cbcSMatt Macy 	/* Check mandatory options */
940eda14cbcSMatt Macy 	for (i = 0; i < ARRAY_SIZE(gcm_impl_opts); i++) {
941eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
942eda14cbcSMatt Macy 		/* Ignore avx implementation if it won't work. */
943eda14cbcSMatt Macy 		if (gcm_impl_opts[i].sel == IMPL_AVX && !gcm_avx_will_work()) {
944eda14cbcSMatt Macy 			continue;
945eda14cbcSMatt Macy 		}
946eda14cbcSMatt Macy #endif
947eda14cbcSMatt Macy 		if (strcmp(req_name, gcm_impl_opts[i].name) == 0) {
948eda14cbcSMatt Macy 			impl = gcm_impl_opts[i].sel;
949eda14cbcSMatt Macy 			err = 0;
950eda14cbcSMatt Macy 			break;
951eda14cbcSMatt Macy 		}
952eda14cbcSMatt Macy 	}
953eda14cbcSMatt Macy 
954eda14cbcSMatt Macy 	/* check all supported impl if init() was already called */
955eda14cbcSMatt Macy 	if (err != 0 && gcm_impl_initialized) {
956eda14cbcSMatt Macy 		/* check all supported implementations */
957eda14cbcSMatt Macy 		for (i = 0; i < gcm_supp_impl_cnt; i++) {
958eda14cbcSMatt Macy 			if (strcmp(req_name, gcm_supp_impl[i]->name) == 0) {
959eda14cbcSMatt Macy 				impl = i;
960eda14cbcSMatt Macy 				err = 0;
961eda14cbcSMatt Macy 				break;
962eda14cbcSMatt Macy 			}
963eda14cbcSMatt Macy 		}
964eda14cbcSMatt Macy 	}
965eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
966eda14cbcSMatt Macy 	/*
967eda14cbcSMatt Macy 	 * Use the avx implementation if available and the requested one is
968eda14cbcSMatt Macy 	 * avx or fastest.
969eda14cbcSMatt Macy 	 */
970eda14cbcSMatt Macy 	if (gcm_avx_will_work() == B_TRUE &&
971eda14cbcSMatt Macy 	    (impl == IMPL_AVX || impl == IMPL_FASTEST)) {
972eda14cbcSMatt Macy 		gcm_set_avx(B_TRUE);
973eda14cbcSMatt Macy 	} else {
974eda14cbcSMatt Macy 		gcm_set_avx(B_FALSE);
975eda14cbcSMatt Macy 	}
976eda14cbcSMatt Macy #endif
977eda14cbcSMatt Macy 
978eda14cbcSMatt Macy 	if (err == 0) {
979eda14cbcSMatt Macy 		if (gcm_impl_initialized)
980eda14cbcSMatt Macy 			atomic_swap_32(&icp_gcm_impl, impl);
981eda14cbcSMatt Macy 		else
982eda14cbcSMatt Macy 			atomic_swap_32(&user_sel_impl, impl);
983eda14cbcSMatt Macy 	}
984eda14cbcSMatt Macy 
985eda14cbcSMatt Macy 	return (err);
986eda14cbcSMatt Macy }
987eda14cbcSMatt Macy 
988eda14cbcSMatt Macy #if defined(_KERNEL) && defined(__linux__)
989eda14cbcSMatt Macy 
990eda14cbcSMatt Macy static int
icp_gcm_impl_set(const char * val,zfs_kernel_param_t * kp)991eda14cbcSMatt Macy icp_gcm_impl_set(const char *val, zfs_kernel_param_t *kp)
992eda14cbcSMatt Macy {
993eda14cbcSMatt Macy 	return (gcm_impl_set(val));
994eda14cbcSMatt Macy }
995eda14cbcSMatt Macy 
996eda14cbcSMatt Macy static int
icp_gcm_impl_get(char * buffer,zfs_kernel_param_t * kp)997eda14cbcSMatt Macy icp_gcm_impl_get(char *buffer, zfs_kernel_param_t *kp)
998eda14cbcSMatt Macy {
999eda14cbcSMatt Macy 	int i, cnt = 0;
1000eda14cbcSMatt Macy 	char *fmt;
1001eda14cbcSMatt Macy 	const uint32_t impl = GCM_IMPL_READ(icp_gcm_impl);
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy 	ASSERT(gcm_impl_initialized);
1004eda14cbcSMatt Macy 
1005eda14cbcSMatt Macy 	/* list mandatory options */
1006eda14cbcSMatt Macy 	for (i = 0; i < ARRAY_SIZE(gcm_impl_opts); i++) {
1007eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
1008eda14cbcSMatt Macy 		/* Ignore avx implementation if it won't work. */
1009eda14cbcSMatt Macy 		if (gcm_impl_opts[i].sel == IMPL_AVX && !gcm_avx_will_work()) {
1010eda14cbcSMatt Macy 			continue;
1011eda14cbcSMatt Macy 		}
1012eda14cbcSMatt Macy #endif
1013eda14cbcSMatt Macy 		fmt = (impl == gcm_impl_opts[i].sel) ? "[%s] " : "%s ";
1014bb2d13b6SMartin Matuska 		cnt += kmem_scnprintf(buffer + cnt, PAGE_SIZE - cnt, fmt,
1015bb2d13b6SMartin Matuska 		    gcm_impl_opts[i].name);
1016eda14cbcSMatt Macy 	}
1017eda14cbcSMatt Macy 
1018eda14cbcSMatt Macy 	/* list all supported implementations */
1019eda14cbcSMatt Macy 	for (i = 0; i < gcm_supp_impl_cnt; i++) {
1020eda14cbcSMatt Macy 		fmt = (i == impl) ? "[%s] " : "%s ";
1021bb2d13b6SMartin Matuska 		cnt += kmem_scnprintf(buffer + cnt, PAGE_SIZE - cnt, fmt,
1022bb2d13b6SMartin Matuska 		    gcm_supp_impl[i]->name);
1023eda14cbcSMatt Macy 	}
1024eda14cbcSMatt Macy 
1025eda14cbcSMatt Macy 	return (cnt);
1026eda14cbcSMatt Macy }
1027eda14cbcSMatt Macy 
1028eda14cbcSMatt Macy module_param_call(icp_gcm_impl, icp_gcm_impl_set, icp_gcm_impl_get,
1029eda14cbcSMatt Macy     NULL, 0644);
1030eda14cbcSMatt Macy MODULE_PARM_DESC(icp_gcm_impl, "Select gcm implementation.");
1031eda14cbcSMatt Macy #endif /* defined(__KERNEL) */
1032eda14cbcSMatt Macy 
1033eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
1034eda14cbcSMatt Macy #define	GCM_BLOCK_LEN 16
1035eda14cbcSMatt Macy /*
1036eda14cbcSMatt Macy  * The openssl asm routines are 6x aggregated and need that many bytes
1037eda14cbcSMatt Macy  * at minimum.
1038eda14cbcSMatt Macy  */
1039eda14cbcSMatt Macy #define	GCM_AVX_MIN_DECRYPT_BYTES (GCM_BLOCK_LEN * 6)
1040eda14cbcSMatt Macy #define	GCM_AVX_MIN_ENCRYPT_BYTES (GCM_BLOCK_LEN * 6 * 3)
1041eda14cbcSMatt Macy /*
1042eda14cbcSMatt Macy  * Ensure the chunk size is reasonable since we are allocating a
1043eda14cbcSMatt Macy  * GCM_AVX_MAX_CHUNK_SIZEd buffer and disabling preemption and interrupts.
1044eda14cbcSMatt Macy  */
1045eda14cbcSMatt Macy #define	GCM_AVX_MAX_CHUNK_SIZE \
1046eda14cbcSMatt Macy 	(((128*1024)/GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES)
1047eda14cbcSMatt Macy 
1048eda14cbcSMatt Macy /* Clear the FPU registers since they hold sensitive internal state. */
1049eda14cbcSMatt Macy #define	clear_fpu_regs() clear_fpu_regs_avx()
1050eda14cbcSMatt Macy #define	GHASH_AVX(ctx, in, len) \
10517877fdebSMatt Macy     gcm_ghash_avx((ctx)->gcm_ghash, (const uint64_t *)(ctx)->gcm_Htable, \
1052eda14cbcSMatt Macy     in, len)
1053eda14cbcSMatt Macy 
1054eda14cbcSMatt Macy #define	gcm_incr_counter_block(ctx) gcm_incr_counter_block_by(ctx, 1)
1055eda14cbcSMatt Macy 
1056e92ffd9bSMartin Matuska /* Get the chunk size module parameter. */
1057e92ffd9bSMartin Matuska #define	GCM_CHUNK_SIZE_READ *(volatile uint32_t *) &gcm_avx_chunk_size
1058e92ffd9bSMartin Matuska 
1059eda14cbcSMatt Macy /*
1060eda14cbcSMatt Macy  * Module parameter: number of bytes to process at once while owning the FPU.
1061eda14cbcSMatt Macy  * Rounded down to the next GCM_AVX_MIN_DECRYPT_BYTES byte boundary and is
1062eda14cbcSMatt Macy  * ensured to be greater or equal than GCM_AVX_MIN_DECRYPT_BYTES.
1063eda14cbcSMatt Macy  */
1064eda14cbcSMatt Macy static uint32_t gcm_avx_chunk_size =
1065eda14cbcSMatt Macy 	((32 * 1024) / GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES;
1066eda14cbcSMatt Macy 
106715f0b8c3SMartin Matuska extern void ASMABI clear_fpu_regs_avx(void);
106815f0b8c3SMartin Matuska extern void ASMABI gcm_xor_avx(const uint8_t *src, uint8_t *dst);
106915f0b8c3SMartin Matuska extern void ASMABI aes_encrypt_intel(const uint32_t rk[], int nr,
1070eda14cbcSMatt Macy     const uint32_t pt[4], uint32_t ct[4]);
1071eda14cbcSMatt Macy 
107215f0b8c3SMartin Matuska extern void ASMABI gcm_init_htab_avx(uint64_t *Htable, const uint64_t H[2]);
107315f0b8c3SMartin Matuska extern void ASMABI gcm_ghash_avx(uint64_t ghash[2], const uint64_t *Htable,
1074eda14cbcSMatt Macy     const uint8_t *in, size_t len);
1075eda14cbcSMatt Macy 
107615f0b8c3SMartin Matuska extern size_t ASMABI aesni_gcm_encrypt(const uint8_t *, uint8_t *, size_t,
1077eda14cbcSMatt Macy     const void *, uint64_t *, uint64_t *);
1078eda14cbcSMatt Macy 
107915f0b8c3SMartin Matuska extern size_t ASMABI aesni_gcm_decrypt(const uint8_t *, uint8_t *, size_t,
1080eda14cbcSMatt Macy     const void *, uint64_t *, uint64_t *);
1081eda14cbcSMatt Macy 
1082eda14cbcSMatt Macy static inline boolean_t
gcm_avx_will_work(void)1083eda14cbcSMatt Macy gcm_avx_will_work(void)
1084eda14cbcSMatt Macy {
1085eda14cbcSMatt Macy 	/* Avx should imply aes-ni and pclmulqdq, but make sure anyhow. */
1086eda14cbcSMatt Macy 	return (kfpu_allowed() &&
1087eda14cbcSMatt Macy 	    zfs_avx_available() && zfs_aes_available() &&
1088eda14cbcSMatt Macy 	    zfs_pclmulqdq_available());
1089eda14cbcSMatt Macy }
1090eda14cbcSMatt Macy 
1091eda14cbcSMatt Macy static inline void
gcm_set_avx(boolean_t val)1092eda14cbcSMatt Macy gcm_set_avx(boolean_t val)
1093eda14cbcSMatt Macy {
1094eda14cbcSMatt Macy 	if (gcm_avx_will_work() == B_TRUE) {
1095eda14cbcSMatt Macy 		atomic_swap_32(&gcm_use_avx, val);
1096eda14cbcSMatt Macy 	}
1097eda14cbcSMatt Macy }
1098eda14cbcSMatt Macy 
1099eda14cbcSMatt Macy static inline boolean_t
gcm_toggle_avx(void)1100eda14cbcSMatt Macy gcm_toggle_avx(void)
1101eda14cbcSMatt Macy {
1102eda14cbcSMatt Macy 	if (gcm_avx_will_work() == B_TRUE) {
1103eda14cbcSMatt Macy 		return (atomic_toggle_boolean_nv(&GCM_IMPL_USE_AVX));
1104eda14cbcSMatt Macy 	} else {
1105eda14cbcSMatt Macy 		return (B_FALSE);
1106eda14cbcSMatt Macy 	}
1107eda14cbcSMatt Macy }
1108eda14cbcSMatt Macy 
11097877fdebSMatt Macy static inline size_t
gcm_simd_get_htab_size(boolean_t simd_mode)11107877fdebSMatt Macy gcm_simd_get_htab_size(boolean_t simd_mode)
11117877fdebSMatt Macy {
11127877fdebSMatt Macy 	switch (simd_mode) {
11137877fdebSMatt Macy 	case B_TRUE:
11147877fdebSMatt Macy 		return (2 * 6 * 2 * sizeof (uint64_t));
11157877fdebSMatt Macy 
11167877fdebSMatt Macy 	default:
11177877fdebSMatt Macy 		return (0);
11187877fdebSMatt Macy 	}
11197877fdebSMatt Macy }
11207877fdebSMatt Macy 
1121eda14cbcSMatt Macy 
1122eda14cbcSMatt Macy /* Increment the GCM counter block by n. */
1123eda14cbcSMatt Macy static inline void
gcm_incr_counter_block_by(gcm_ctx_t * ctx,int n)1124eda14cbcSMatt Macy gcm_incr_counter_block_by(gcm_ctx_t *ctx, int n)
1125eda14cbcSMatt Macy {
1126eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
1127eda14cbcSMatt Macy 	uint64_t counter = ntohll(ctx->gcm_cb[1] & counter_mask);
1128eda14cbcSMatt Macy 
1129eda14cbcSMatt Macy 	counter = htonll(counter + n);
1130eda14cbcSMatt Macy 	counter &= counter_mask;
1131eda14cbcSMatt Macy 	ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
1132eda14cbcSMatt Macy }
1133eda14cbcSMatt Macy 
1134eda14cbcSMatt Macy /*
1135eda14cbcSMatt Macy  * Encrypt multiple blocks of data in GCM mode.
1136eda14cbcSMatt Macy  * This is done in gcm_avx_chunk_size chunks, utilizing AVX assembler routines
1137eda14cbcSMatt Macy  * if possible. While processing a chunk the FPU is "locked".
1138eda14cbcSMatt Macy  */
1139eda14cbcSMatt Macy static int
gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t * ctx,char * data,size_t length,crypto_data_t * out,size_t block_size)1140eda14cbcSMatt Macy gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *ctx, char *data,
1141eda14cbcSMatt Macy     size_t length, crypto_data_t *out, size_t block_size)
1142eda14cbcSMatt Macy {
1143eda14cbcSMatt Macy 	size_t bleft = length;
1144eda14cbcSMatt Macy 	size_t need = 0;
1145eda14cbcSMatt Macy 	size_t done = 0;
1146eda14cbcSMatt Macy 	uint8_t *datap = (uint8_t *)data;
1147eda14cbcSMatt Macy 	size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ;
1148eda14cbcSMatt Macy 	const aes_key_t *key = ((aes_key_t *)ctx->gcm_keysched);
1149eda14cbcSMatt Macy 	uint64_t *ghash = ctx->gcm_ghash;
1150eda14cbcSMatt Macy 	uint64_t *cb = ctx->gcm_cb;
1151eda14cbcSMatt Macy 	uint8_t *ct_buf = NULL;
1152eda14cbcSMatt Macy 	uint8_t *tmp = (uint8_t *)ctx->gcm_tmp;
1153eda14cbcSMatt Macy 	int rv = CRYPTO_SUCCESS;
1154eda14cbcSMatt Macy 
1155eda14cbcSMatt Macy 	ASSERT(block_size == GCM_BLOCK_LEN);
11562a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
11572a58b312SMartin Matuska 	    B_FALSE);
1158eda14cbcSMatt Macy 	/*
1159eda14cbcSMatt Macy 	 * If the last call left an incomplete block, try to fill
1160eda14cbcSMatt Macy 	 * it first.
1161eda14cbcSMatt Macy 	 */
1162eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
1163eda14cbcSMatt Macy 		need = block_size - ctx->gcm_remainder_len;
1164eda14cbcSMatt Macy 		if (length < need) {
1165eda14cbcSMatt Macy 			/* Accumulate bytes here and return. */
1166da5137abSMartin Matuska 			memcpy((uint8_t *)ctx->gcm_remainder +
1167da5137abSMartin Matuska 			    ctx->gcm_remainder_len, datap, length);
1168eda14cbcSMatt Macy 
1169eda14cbcSMatt Macy 			ctx->gcm_remainder_len += length;
1170eda14cbcSMatt Macy 			if (ctx->gcm_copy_to == NULL) {
1171eda14cbcSMatt Macy 				ctx->gcm_copy_to = datap;
1172eda14cbcSMatt Macy 			}
1173eda14cbcSMatt Macy 			return (CRYPTO_SUCCESS);
1174eda14cbcSMatt Macy 		} else {
1175eda14cbcSMatt Macy 			/* Complete incomplete block. */
1176da5137abSMartin Matuska 			memcpy((uint8_t *)ctx->gcm_remainder +
1177da5137abSMartin Matuska 			    ctx->gcm_remainder_len, datap, need);
1178eda14cbcSMatt Macy 
1179eda14cbcSMatt Macy 			ctx->gcm_copy_to = NULL;
1180eda14cbcSMatt Macy 		}
1181eda14cbcSMatt Macy 	}
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy 	/* Allocate a buffer to encrypt to if there is enough input. */
1184eda14cbcSMatt Macy 	if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) {
1185c03c5b1cSMartin Matuska 		ct_buf = vmem_alloc(chunk_size, KM_SLEEP);
1186eda14cbcSMatt Macy 		if (ct_buf == NULL) {
1187eda14cbcSMatt Macy 			return (CRYPTO_HOST_MEMORY);
1188eda14cbcSMatt Macy 		}
1189eda14cbcSMatt Macy 	}
1190eda14cbcSMatt Macy 
1191eda14cbcSMatt Macy 	/* If we completed an incomplete block, encrypt and write it out. */
1192eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
1193eda14cbcSMatt Macy 		kfpu_begin();
1194eda14cbcSMatt Macy 		aes_encrypt_intel(key->encr_ks.ks32, key->nr,
1195eda14cbcSMatt Macy 		    (const uint32_t *)cb, (uint32_t *)tmp);
1196eda14cbcSMatt Macy 
1197eda14cbcSMatt Macy 		gcm_xor_avx((const uint8_t *) ctx->gcm_remainder, tmp);
1198eda14cbcSMatt Macy 		GHASH_AVX(ctx, tmp, block_size);
1199eda14cbcSMatt Macy 		clear_fpu_regs();
1200eda14cbcSMatt Macy 		kfpu_end();
1201eda14cbcSMatt Macy 		rv = crypto_put_output_data(tmp, out, block_size);
1202eda14cbcSMatt Macy 		out->cd_offset += block_size;
1203eda14cbcSMatt Macy 		gcm_incr_counter_block(ctx);
1204eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += block_size;
1205eda14cbcSMatt Macy 		bleft -= need;
1206eda14cbcSMatt Macy 		datap += need;
1207eda14cbcSMatt Macy 		ctx->gcm_remainder_len = 0;
1208eda14cbcSMatt Macy 	}
1209eda14cbcSMatt Macy 
1210eda14cbcSMatt Macy 	/* Do the bulk encryption in chunk_size blocks. */
1211eda14cbcSMatt Macy 	for (; bleft >= chunk_size; bleft -= chunk_size) {
1212eda14cbcSMatt Macy 		kfpu_begin();
1213eda14cbcSMatt Macy 		done = aesni_gcm_encrypt(
1214eda14cbcSMatt Macy 		    datap, ct_buf, chunk_size, key, cb, ghash);
1215eda14cbcSMatt Macy 
1216eda14cbcSMatt Macy 		clear_fpu_regs();
1217eda14cbcSMatt Macy 		kfpu_end();
1218eda14cbcSMatt Macy 		if (done != chunk_size) {
1219eda14cbcSMatt Macy 			rv = CRYPTO_FAILED;
1220eda14cbcSMatt Macy 			goto out_nofpu;
1221eda14cbcSMatt Macy 		}
1222eda14cbcSMatt Macy 		rv = crypto_put_output_data(ct_buf, out, chunk_size);
1223eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS) {
1224eda14cbcSMatt Macy 			goto out_nofpu;
1225eda14cbcSMatt Macy 		}
1226eda14cbcSMatt Macy 		out->cd_offset += chunk_size;
1227eda14cbcSMatt Macy 		datap += chunk_size;
1228eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += chunk_size;
1229eda14cbcSMatt Macy 	}
1230eda14cbcSMatt Macy 	/* Check if we are already done. */
1231eda14cbcSMatt Macy 	if (bleft == 0) {
1232eda14cbcSMatt Macy 		goto out_nofpu;
1233eda14cbcSMatt Macy 	}
1234eda14cbcSMatt Macy 	/* Bulk encrypt the remaining data. */
1235eda14cbcSMatt Macy 	kfpu_begin();
1236eda14cbcSMatt Macy 	if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) {
1237eda14cbcSMatt Macy 		done = aesni_gcm_encrypt(datap, ct_buf, bleft, key, cb, ghash);
1238eda14cbcSMatt Macy 		if (done == 0) {
1239eda14cbcSMatt Macy 			rv = CRYPTO_FAILED;
1240eda14cbcSMatt Macy 			goto out;
1241eda14cbcSMatt Macy 		}
1242eda14cbcSMatt Macy 		rv = crypto_put_output_data(ct_buf, out, done);
1243eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS) {
1244eda14cbcSMatt Macy 			goto out;
1245eda14cbcSMatt Macy 		}
1246eda14cbcSMatt Macy 		out->cd_offset += done;
1247eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += done;
1248eda14cbcSMatt Macy 		datap += done;
1249eda14cbcSMatt Macy 		bleft -= done;
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	}
1252eda14cbcSMatt Macy 	/* Less than GCM_AVX_MIN_ENCRYPT_BYTES remain, operate on blocks. */
1253eda14cbcSMatt Macy 	while (bleft > 0) {
1254eda14cbcSMatt Macy 		if (bleft < block_size) {
1255da5137abSMartin Matuska 			memcpy(ctx->gcm_remainder, datap, bleft);
1256eda14cbcSMatt Macy 			ctx->gcm_remainder_len = bleft;
1257eda14cbcSMatt Macy 			ctx->gcm_copy_to = datap;
1258eda14cbcSMatt Macy 			goto out;
1259eda14cbcSMatt Macy 		}
1260eda14cbcSMatt Macy 		/* Encrypt, hash and write out. */
1261eda14cbcSMatt Macy 		aes_encrypt_intel(key->encr_ks.ks32, key->nr,
1262eda14cbcSMatt Macy 		    (const uint32_t *)cb, (uint32_t *)tmp);
1263eda14cbcSMatt Macy 
1264eda14cbcSMatt Macy 		gcm_xor_avx(datap, tmp);
1265eda14cbcSMatt Macy 		GHASH_AVX(ctx, tmp, block_size);
1266eda14cbcSMatt Macy 		rv = crypto_put_output_data(tmp, out, block_size);
1267eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS) {
1268eda14cbcSMatt Macy 			goto out;
1269eda14cbcSMatt Macy 		}
1270eda14cbcSMatt Macy 		out->cd_offset += block_size;
1271eda14cbcSMatt Macy 		gcm_incr_counter_block(ctx);
1272eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += block_size;
1273eda14cbcSMatt Macy 		datap += block_size;
1274eda14cbcSMatt Macy 		bleft -= block_size;
1275eda14cbcSMatt Macy 	}
1276eda14cbcSMatt Macy out:
1277eda14cbcSMatt Macy 	clear_fpu_regs();
1278eda14cbcSMatt Macy 	kfpu_end();
1279eda14cbcSMatt Macy out_nofpu:
1280eda14cbcSMatt Macy 	if (ct_buf != NULL) {
1281eda14cbcSMatt Macy 		vmem_free(ct_buf, chunk_size);
1282eda14cbcSMatt Macy 	}
1283eda14cbcSMatt Macy 	return (rv);
1284eda14cbcSMatt Macy }
1285eda14cbcSMatt Macy 
1286eda14cbcSMatt Macy /*
1287eda14cbcSMatt Macy  * Finalize the encryption: Zero fill, encrypt, hash and write out an eventual
1288eda14cbcSMatt Macy  * incomplete last block. Encrypt the ICB. Calculate the tag and write it out.
1289eda14cbcSMatt Macy  */
1290eda14cbcSMatt Macy static int
gcm_encrypt_final_avx(gcm_ctx_t * ctx,crypto_data_t * out,size_t block_size)1291eda14cbcSMatt Macy gcm_encrypt_final_avx(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size)
1292eda14cbcSMatt Macy {
1293eda14cbcSMatt Macy 	uint8_t *ghash = (uint8_t *)ctx->gcm_ghash;
1294eda14cbcSMatt Macy 	uint32_t *J0 = (uint32_t *)ctx->gcm_J0;
1295eda14cbcSMatt Macy 	uint8_t *remainder = (uint8_t *)ctx->gcm_remainder;
1296eda14cbcSMatt Macy 	size_t rem_len = ctx->gcm_remainder_len;
1297eda14cbcSMatt Macy 	const void *keysched = ((aes_key_t *)ctx->gcm_keysched)->encr_ks.ks32;
1298eda14cbcSMatt Macy 	int aes_rounds = ((aes_key_t *)keysched)->nr;
1299eda14cbcSMatt Macy 	int rv;
1300eda14cbcSMatt Macy 
1301eda14cbcSMatt Macy 	ASSERT(block_size == GCM_BLOCK_LEN);
13022a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
13032a58b312SMartin Matuska 	    B_FALSE);
1304eda14cbcSMatt Macy 
1305eda14cbcSMatt Macy 	if (out->cd_length < (rem_len + ctx->gcm_tag_len)) {
1306eda14cbcSMatt Macy 		return (CRYPTO_DATA_LEN_RANGE);
1307eda14cbcSMatt Macy 	}
1308eda14cbcSMatt Macy 
1309eda14cbcSMatt Macy 	kfpu_begin();
1310eda14cbcSMatt Macy 	/* Pad last incomplete block with zeros, encrypt and hash. */
1311eda14cbcSMatt Macy 	if (rem_len > 0) {
1312eda14cbcSMatt Macy 		uint8_t *tmp = (uint8_t *)ctx->gcm_tmp;
1313eda14cbcSMatt Macy 		const uint32_t *cb = (uint32_t *)ctx->gcm_cb;
1314eda14cbcSMatt Macy 
1315eda14cbcSMatt Macy 		aes_encrypt_intel(keysched, aes_rounds, cb, (uint32_t *)tmp);
1316da5137abSMartin Matuska 		memset(remainder + rem_len, 0, block_size - rem_len);
1317eda14cbcSMatt Macy 		for (int i = 0; i < rem_len; i++) {
1318eda14cbcSMatt Macy 			remainder[i] ^= tmp[i];
1319eda14cbcSMatt Macy 		}
1320eda14cbcSMatt Macy 		GHASH_AVX(ctx, remainder, block_size);
1321eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += rem_len;
1322eda14cbcSMatt Macy 		/* No need to increment counter_block, it's the last block. */
1323eda14cbcSMatt Macy 	}
1324eda14cbcSMatt Macy 	/* Finish tag. */
1325eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] =
1326eda14cbcSMatt Macy 	    htonll(CRYPTO_BYTES2BITS(ctx->gcm_processed_data_len));
1327eda14cbcSMatt Macy 	GHASH_AVX(ctx, (const uint8_t *)ctx->gcm_len_a_len_c, block_size);
1328eda14cbcSMatt Macy 	aes_encrypt_intel(keysched, aes_rounds, J0, J0);
1329eda14cbcSMatt Macy 
1330eda14cbcSMatt Macy 	gcm_xor_avx((uint8_t *)J0, ghash);
1331eda14cbcSMatt Macy 	clear_fpu_regs();
1332eda14cbcSMatt Macy 	kfpu_end();
1333eda14cbcSMatt Macy 
1334eda14cbcSMatt Macy 	/* Output remainder. */
1335eda14cbcSMatt Macy 	if (rem_len > 0) {
1336eda14cbcSMatt Macy 		rv = crypto_put_output_data(remainder, out, rem_len);
1337eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS)
1338eda14cbcSMatt Macy 			return (rv);
1339eda14cbcSMatt Macy 	}
1340eda14cbcSMatt Macy 	out->cd_offset += rem_len;
1341eda14cbcSMatt Macy 	ctx->gcm_remainder_len = 0;
1342eda14cbcSMatt Macy 	rv = crypto_put_output_data(ghash, out, ctx->gcm_tag_len);
1343eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS)
1344eda14cbcSMatt Macy 		return (rv);
1345eda14cbcSMatt Macy 
1346eda14cbcSMatt Macy 	out->cd_offset += ctx->gcm_tag_len;
1347eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
1348eda14cbcSMatt Macy }
1349eda14cbcSMatt Macy 
1350eda14cbcSMatt Macy /*
1351eda14cbcSMatt Macy  * Finalize decryption: We just have accumulated crypto text, so now we
1352eda14cbcSMatt Macy  * decrypt it here inplace.
1353eda14cbcSMatt Macy  */
1354eda14cbcSMatt Macy static int
gcm_decrypt_final_avx(gcm_ctx_t * ctx,crypto_data_t * out,size_t block_size)1355eda14cbcSMatt Macy gcm_decrypt_final_avx(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size)
1356eda14cbcSMatt Macy {
1357eda14cbcSMatt Macy 	ASSERT3U(ctx->gcm_processed_data_len, ==, ctx->gcm_pt_buf_len);
1358eda14cbcSMatt Macy 	ASSERT3U(block_size, ==, 16);
13592a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
13602a58b312SMartin Matuska 	    B_FALSE);
1361eda14cbcSMatt Macy 
1362eda14cbcSMatt Macy 	size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ;
1363eda14cbcSMatt Macy 	size_t pt_len = ctx->gcm_processed_data_len - ctx->gcm_tag_len;
1364eda14cbcSMatt Macy 	uint8_t *datap = ctx->gcm_pt_buf;
1365eda14cbcSMatt Macy 	const aes_key_t *key = ((aes_key_t *)ctx->gcm_keysched);
1366eda14cbcSMatt Macy 	uint32_t *cb = (uint32_t *)ctx->gcm_cb;
1367eda14cbcSMatt Macy 	uint64_t *ghash = ctx->gcm_ghash;
1368eda14cbcSMatt Macy 	uint32_t *tmp = (uint32_t *)ctx->gcm_tmp;
1369eda14cbcSMatt Macy 	int rv = CRYPTO_SUCCESS;
1370eda14cbcSMatt Macy 	size_t bleft, done;
1371eda14cbcSMatt Macy 
1372eda14cbcSMatt Macy 	/*
1373eda14cbcSMatt Macy 	 * Decrypt in chunks of gcm_avx_chunk_size, which is asserted to be
1374eda14cbcSMatt Macy 	 * greater or equal than GCM_AVX_MIN_ENCRYPT_BYTES, and a multiple of
1375eda14cbcSMatt Macy 	 * GCM_AVX_MIN_DECRYPT_BYTES.
1376eda14cbcSMatt Macy 	 */
1377eda14cbcSMatt Macy 	for (bleft = pt_len; bleft >= chunk_size; bleft -= chunk_size) {
1378eda14cbcSMatt Macy 		kfpu_begin();
1379eda14cbcSMatt Macy 		done = aesni_gcm_decrypt(datap, datap, chunk_size,
1380eda14cbcSMatt Macy 		    (const void *)key, ctx->gcm_cb, ghash);
1381eda14cbcSMatt Macy 		clear_fpu_regs();
1382eda14cbcSMatt Macy 		kfpu_end();
1383eda14cbcSMatt Macy 		if (done != chunk_size) {
1384eda14cbcSMatt Macy 			return (CRYPTO_FAILED);
1385eda14cbcSMatt Macy 		}
1386eda14cbcSMatt Macy 		datap += done;
1387eda14cbcSMatt Macy 	}
138816038816SMartin Matuska 	/* Decrypt remainder, which is less than chunk size, in one go. */
1389eda14cbcSMatt Macy 	kfpu_begin();
1390eda14cbcSMatt Macy 	if (bleft >= GCM_AVX_MIN_DECRYPT_BYTES) {
1391eda14cbcSMatt Macy 		done = aesni_gcm_decrypt(datap, datap, bleft,
1392eda14cbcSMatt Macy 		    (const void *)key, ctx->gcm_cb, ghash);
1393eda14cbcSMatt Macy 		if (done == 0) {
1394eda14cbcSMatt Macy 			clear_fpu_regs();
1395eda14cbcSMatt Macy 			kfpu_end();
1396eda14cbcSMatt Macy 			return (CRYPTO_FAILED);
1397eda14cbcSMatt Macy 		}
1398eda14cbcSMatt Macy 		datap += done;
1399eda14cbcSMatt Macy 		bleft -= done;
1400eda14cbcSMatt Macy 	}
1401eda14cbcSMatt Macy 	ASSERT(bleft < GCM_AVX_MIN_DECRYPT_BYTES);
1402eda14cbcSMatt Macy 
1403eda14cbcSMatt Macy 	/*
140416038816SMartin Matuska 	 * Now less than GCM_AVX_MIN_DECRYPT_BYTES bytes remain,
1405eda14cbcSMatt Macy 	 * decrypt them block by block.
1406eda14cbcSMatt Macy 	 */
1407eda14cbcSMatt Macy 	while (bleft > 0) {
1408eda14cbcSMatt Macy 		/* Incomplete last block. */
1409eda14cbcSMatt Macy 		if (bleft < block_size) {
1410eda14cbcSMatt Macy 			uint8_t *lastb = (uint8_t *)ctx->gcm_remainder;
1411eda14cbcSMatt Macy 
1412da5137abSMartin Matuska 			memset(lastb, 0, block_size);
1413da5137abSMartin Matuska 			memcpy(lastb, datap, bleft);
1414eda14cbcSMatt Macy 			/* The GCM processing. */
1415eda14cbcSMatt Macy 			GHASH_AVX(ctx, lastb, block_size);
1416eda14cbcSMatt Macy 			aes_encrypt_intel(key->encr_ks.ks32, key->nr, cb, tmp);
1417eda14cbcSMatt Macy 			for (size_t i = 0; i < bleft; i++) {
1418eda14cbcSMatt Macy 				datap[i] = lastb[i] ^ ((uint8_t *)tmp)[i];
1419eda14cbcSMatt Macy 			}
1420eda14cbcSMatt Macy 			break;
1421eda14cbcSMatt Macy 		}
1422eda14cbcSMatt Macy 		/* The GCM processing. */
1423eda14cbcSMatt Macy 		GHASH_AVX(ctx, datap, block_size);
1424eda14cbcSMatt Macy 		aes_encrypt_intel(key->encr_ks.ks32, key->nr, cb, tmp);
1425eda14cbcSMatt Macy 		gcm_xor_avx((uint8_t *)tmp, datap);
1426eda14cbcSMatt Macy 		gcm_incr_counter_block(ctx);
1427eda14cbcSMatt Macy 
1428eda14cbcSMatt Macy 		datap += block_size;
1429eda14cbcSMatt Macy 		bleft -= block_size;
1430eda14cbcSMatt Macy 	}
1431eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS) {
1432eda14cbcSMatt Macy 		clear_fpu_regs();
1433eda14cbcSMatt Macy 		kfpu_end();
1434eda14cbcSMatt Macy 		return (rv);
1435eda14cbcSMatt Macy 	}
1436eda14cbcSMatt Macy 	/* Decryption done, finish the tag. */
1437eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(pt_len));
1438eda14cbcSMatt Macy 	GHASH_AVX(ctx, (uint8_t *)ctx->gcm_len_a_len_c, block_size);
1439eda14cbcSMatt Macy 	aes_encrypt_intel(key->encr_ks.ks32, key->nr, (uint32_t *)ctx->gcm_J0,
1440eda14cbcSMatt Macy 	    (uint32_t *)ctx->gcm_J0);
1441eda14cbcSMatt Macy 
1442eda14cbcSMatt Macy 	gcm_xor_avx((uint8_t *)ctx->gcm_J0, (uint8_t *)ghash);
1443eda14cbcSMatt Macy 
1444eda14cbcSMatt Macy 	/* We are done with the FPU, restore its state. */
1445eda14cbcSMatt Macy 	clear_fpu_regs();
1446eda14cbcSMatt Macy 	kfpu_end();
1447eda14cbcSMatt Macy 
1448eda14cbcSMatt Macy 	/* Compare the input authentication tag with what we calculated. */
1449da5137abSMartin Matuska 	if (memcmp(&ctx->gcm_pt_buf[pt_len], ghash, ctx->gcm_tag_len)) {
1450eda14cbcSMatt Macy 		/* They don't match. */
1451eda14cbcSMatt Macy 		return (CRYPTO_INVALID_MAC);
1452eda14cbcSMatt Macy 	}
1453eda14cbcSMatt Macy 	rv = crypto_put_output_data(ctx->gcm_pt_buf, out, pt_len);
1454eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS) {
1455eda14cbcSMatt Macy 		return (rv);
1456eda14cbcSMatt Macy 	}
1457eda14cbcSMatt Macy 	out->cd_offset += pt_len;
1458eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
1459eda14cbcSMatt Macy }
1460eda14cbcSMatt Macy 
1461eda14cbcSMatt Macy /*
1462eda14cbcSMatt Macy  * Initialize the GCM params H, Htabtle and the counter block. Save the
1463eda14cbcSMatt Macy  * initial counter block.
1464eda14cbcSMatt Macy  */
1465eda14cbcSMatt Macy static int
gcm_init_avx(gcm_ctx_t * ctx,const uint8_t * iv,size_t iv_len,const uint8_t * auth_data,size_t auth_data_len,size_t block_size)14662a58b312SMartin Matuska gcm_init_avx(gcm_ctx_t *ctx, const uint8_t *iv, size_t iv_len,
14672a58b312SMartin Matuska     const uint8_t *auth_data, size_t auth_data_len, size_t block_size)
1468eda14cbcSMatt Macy {
1469eda14cbcSMatt Macy 	uint8_t *cb = (uint8_t *)ctx->gcm_cb;
1470eda14cbcSMatt Macy 	uint64_t *H = ctx->gcm_H;
1471eda14cbcSMatt Macy 	const void *keysched = ((aes_key_t *)ctx->gcm_keysched)->encr_ks.ks32;
1472eda14cbcSMatt Macy 	int aes_rounds = ((aes_key_t *)ctx->gcm_keysched)->nr;
14732a58b312SMartin Matuska 	const uint8_t *datap = auth_data;
1474eda14cbcSMatt Macy 	size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ;
1475eda14cbcSMatt Macy 	size_t bleft;
1476eda14cbcSMatt Macy 
1477eda14cbcSMatt Macy 	ASSERT(block_size == GCM_BLOCK_LEN);
14782a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
14792a58b312SMartin Matuska 	    B_FALSE);
1480eda14cbcSMatt Macy 
1481eda14cbcSMatt Macy 	/* Init H (encrypt zero block) and create the initial counter block. */
1482da5137abSMartin Matuska 	memset(ctx->gcm_ghash, 0, sizeof (ctx->gcm_ghash));
1483da5137abSMartin Matuska 	memset(H, 0, sizeof (ctx->gcm_H));
1484eda14cbcSMatt Macy 	kfpu_begin();
1485eda14cbcSMatt Macy 	aes_encrypt_intel(keysched, aes_rounds,
1486eda14cbcSMatt Macy 	    (const uint32_t *)H, (uint32_t *)H);
1487eda14cbcSMatt Macy 
1488eda14cbcSMatt Macy 	gcm_init_htab_avx(ctx->gcm_Htable, H);
1489eda14cbcSMatt Macy 
1490eda14cbcSMatt Macy 	if (iv_len == 12) {
1491da5137abSMartin Matuska 		memcpy(cb, iv, 12);
1492eda14cbcSMatt Macy 		cb[12] = 0;
1493eda14cbcSMatt Macy 		cb[13] = 0;
1494eda14cbcSMatt Macy 		cb[14] = 0;
1495eda14cbcSMatt Macy 		cb[15] = 1;
1496eda14cbcSMatt Macy 		/* We need the ICB later. */
1497da5137abSMartin Matuska 		memcpy(ctx->gcm_J0, cb, sizeof (ctx->gcm_J0));
1498eda14cbcSMatt Macy 	} else {
1499eda14cbcSMatt Macy 		/*
1500eda14cbcSMatt Macy 		 * Most consumers use 12 byte IVs, so it's OK to use the
1501eda14cbcSMatt Macy 		 * original routines for other IV sizes, just avoid nesting
1502eda14cbcSMatt Macy 		 * kfpu_begin calls.
1503eda14cbcSMatt Macy 		 */
1504eda14cbcSMatt Macy 		clear_fpu_regs();
1505eda14cbcSMatt Macy 		kfpu_end();
1506eda14cbcSMatt Macy 		gcm_format_initial_blocks(iv, iv_len, ctx, block_size,
1507eda14cbcSMatt Macy 		    aes_copy_block, aes_xor_block);
1508eda14cbcSMatt Macy 		kfpu_begin();
1509eda14cbcSMatt Macy 	}
1510eda14cbcSMatt Macy 
1511eda14cbcSMatt Macy 	/* Openssl post increments the counter, adjust for that. */
1512eda14cbcSMatt Macy 	gcm_incr_counter_block(ctx);
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy 	/* Ghash AAD in chunk_size blocks. */
1515eda14cbcSMatt Macy 	for (bleft = auth_data_len; bleft >= chunk_size; bleft -= chunk_size) {
1516eda14cbcSMatt Macy 		GHASH_AVX(ctx, datap, chunk_size);
1517eda14cbcSMatt Macy 		datap += chunk_size;
1518eda14cbcSMatt Macy 		clear_fpu_regs();
1519eda14cbcSMatt Macy 		kfpu_end();
1520eda14cbcSMatt Macy 		kfpu_begin();
1521eda14cbcSMatt Macy 	}
1522eda14cbcSMatt Macy 	/* Ghash the remainder and handle possible incomplete GCM block. */
1523eda14cbcSMatt Macy 	if (bleft > 0) {
1524eda14cbcSMatt Macy 		size_t incomp = bleft % block_size;
1525eda14cbcSMatt Macy 
1526eda14cbcSMatt Macy 		bleft -= incomp;
1527eda14cbcSMatt Macy 		if (bleft > 0) {
1528eda14cbcSMatt Macy 			GHASH_AVX(ctx, datap, bleft);
1529eda14cbcSMatt Macy 			datap += bleft;
1530eda14cbcSMatt Macy 		}
1531eda14cbcSMatt Macy 		if (incomp > 0) {
1532eda14cbcSMatt Macy 			/* Zero pad and hash incomplete last block. */
1533eda14cbcSMatt Macy 			uint8_t *authp = (uint8_t *)ctx->gcm_tmp;
1534eda14cbcSMatt Macy 
1535da5137abSMartin Matuska 			memset(authp, 0, block_size);
1536da5137abSMartin Matuska 			memcpy(authp, datap, incomp);
1537eda14cbcSMatt Macy 			GHASH_AVX(ctx, authp, block_size);
1538eda14cbcSMatt Macy 		}
1539eda14cbcSMatt Macy 	}
1540eda14cbcSMatt Macy 	clear_fpu_regs();
1541eda14cbcSMatt Macy 	kfpu_end();
1542eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
1543eda14cbcSMatt Macy }
1544eda14cbcSMatt Macy 
1545eda14cbcSMatt Macy #if defined(_KERNEL)
1546eda14cbcSMatt Macy static int
icp_gcm_avx_set_chunk_size(const char * buf,zfs_kernel_param_t * kp)1547eda14cbcSMatt Macy icp_gcm_avx_set_chunk_size(const char *buf, zfs_kernel_param_t *kp)
1548eda14cbcSMatt Macy {
1549eda14cbcSMatt Macy 	unsigned long val;
1550eda14cbcSMatt Macy 	char val_rounded[16];
1551eda14cbcSMatt Macy 	int error = 0;
1552eda14cbcSMatt Macy 
1553eda14cbcSMatt Macy 	error = kstrtoul(buf, 0, &val);
1554eda14cbcSMatt Macy 	if (error)
1555eda14cbcSMatt Macy 		return (error);
1556eda14cbcSMatt Macy 
1557eda14cbcSMatt Macy 	val = (val / GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES;
1558eda14cbcSMatt Macy 
1559eda14cbcSMatt Macy 	if (val < GCM_AVX_MIN_ENCRYPT_BYTES || val > GCM_AVX_MAX_CHUNK_SIZE)
1560eda14cbcSMatt Macy 		return (-EINVAL);
1561eda14cbcSMatt Macy 
1562eda14cbcSMatt Macy 	snprintf(val_rounded, 16, "%u", (uint32_t)val);
1563eda14cbcSMatt Macy 	error = param_set_uint(val_rounded, kp);
1564eda14cbcSMatt Macy 	return (error);
1565eda14cbcSMatt Macy }
1566eda14cbcSMatt Macy 
1567eda14cbcSMatt Macy module_param_call(icp_gcm_avx_chunk_size, icp_gcm_avx_set_chunk_size,
1568eda14cbcSMatt Macy     param_get_uint, &gcm_avx_chunk_size, 0644);
1569eda14cbcSMatt Macy 
1570eda14cbcSMatt Macy MODULE_PARM_DESC(icp_gcm_avx_chunk_size,
1571eda14cbcSMatt Macy 	"How many bytes to process while owning the FPU");
1572eda14cbcSMatt Macy 
1573eda14cbcSMatt Macy #endif /* defined(__KERNEL) */
1574eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
1575