xref: /freebsd-src/sys/contrib/openzfs/module/icp/algs/modes/gcm.c (revision 75e1fea68aaa613a20dfdcd0c59dd403aca02c49)
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 
53eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
54eda14cbcSMatt Macy /* Does the architecture we run on support the MOVBE instruction? */
55eda14cbcSMatt Macy boolean_t gcm_avx_can_use_movbe = B_FALSE;
56eda14cbcSMatt Macy /*
57eda14cbcSMatt Macy  * Whether to use the optimized openssl gcm and ghash implementations.
58eda14cbcSMatt Macy  * Set to true if module parameter icp_gcm_impl == "avx".
59eda14cbcSMatt Macy  */
60eda14cbcSMatt Macy static boolean_t gcm_use_avx = B_FALSE;
61eda14cbcSMatt Macy #define	GCM_IMPL_USE_AVX	(*(volatile boolean_t *)&gcm_use_avx)
62eda14cbcSMatt Macy 
6315f0b8c3SMartin Matuska extern boolean_t ASMABI atomic_toggle_boolean_nv(volatile boolean_t *);
647877fdebSMatt Macy 
65eda14cbcSMatt Macy static inline boolean_t gcm_avx_will_work(void);
66eda14cbcSMatt Macy static inline void gcm_set_avx(boolean_t);
67eda14cbcSMatt Macy static inline boolean_t gcm_toggle_avx(void);
687877fdebSMatt Macy static inline size_t gcm_simd_get_htab_size(boolean_t);
69eda14cbcSMatt Macy 
70eda14cbcSMatt Macy static int gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *, char *, size_t,
71eda14cbcSMatt Macy     crypto_data_t *, size_t);
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy static int gcm_encrypt_final_avx(gcm_ctx_t *, crypto_data_t *, size_t);
74eda14cbcSMatt Macy static int gcm_decrypt_final_avx(gcm_ctx_t *, crypto_data_t *, size_t);
752a58b312SMartin Matuska static int gcm_init_avx(gcm_ctx_t *, const uint8_t *, size_t, const uint8_t *,
76eda14cbcSMatt Macy     size_t, size_t);
77eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy /*
80eda14cbcSMatt Macy  * Encrypt multiple blocks of data in GCM mode.  Decrypt for GCM mode
81eda14cbcSMatt Macy  * is done in another function.
82eda14cbcSMatt Macy  */
83eda14cbcSMatt Macy int
84eda14cbcSMatt Macy gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length,
85eda14cbcSMatt Macy     crypto_data_t *out, size_t block_size,
86eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
87eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
88eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
89eda14cbcSMatt Macy {
90eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
91eda14cbcSMatt Macy 	if (ctx->gcm_use_avx == B_TRUE)
92eda14cbcSMatt Macy 		return (gcm_mode_encrypt_contiguous_blocks_avx(
93eda14cbcSMatt Macy 		    ctx, data, length, out, block_size));
94eda14cbcSMatt Macy #endif
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
97eda14cbcSMatt Macy 	size_t remainder = length;
98eda14cbcSMatt Macy 	size_t need = 0;
99eda14cbcSMatt Macy 	uint8_t *datap = (uint8_t *)data;
100eda14cbcSMatt Macy 	uint8_t *blockp;
101eda14cbcSMatt Macy 	uint8_t *lastp;
102eda14cbcSMatt Macy 	void *iov_or_mp;
103eda14cbcSMatt Macy 	offset_t offset;
104eda14cbcSMatt Macy 	uint8_t *out_data_1;
105eda14cbcSMatt Macy 	uint8_t *out_data_2;
106eda14cbcSMatt Macy 	size_t out_data_1_len;
107eda14cbcSMatt Macy 	uint64_t counter;
108eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy 	if (length + ctx->gcm_remainder_len < block_size) {
111eda14cbcSMatt Macy 		/* accumulate bytes here and return */
112da5137abSMartin Matuska 		memcpy((uint8_t *)ctx->gcm_remainder + ctx->gcm_remainder_len,
113da5137abSMartin Matuska 		    datap,
114eda14cbcSMatt Macy 		    length);
115eda14cbcSMatt Macy 		ctx->gcm_remainder_len += length;
116eda14cbcSMatt Macy 		if (ctx->gcm_copy_to == NULL) {
117eda14cbcSMatt Macy 			ctx->gcm_copy_to = datap;
118eda14cbcSMatt Macy 		}
119eda14cbcSMatt Macy 		return (CRYPTO_SUCCESS);
120eda14cbcSMatt Macy 	}
121eda14cbcSMatt Macy 
122eda14cbcSMatt Macy 	crypto_init_ptrs(out, &iov_or_mp, &offset);
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
125eda14cbcSMatt Macy 	do {
126eda14cbcSMatt Macy 		/* Unprocessed data from last call. */
127eda14cbcSMatt Macy 		if (ctx->gcm_remainder_len > 0) {
128eda14cbcSMatt Macy 			need = block_size - ctx->gcm_remainder_len;
129eda14cbcSMatt Macy 
130eda14cbcSMatt Macy 			if (need > remainder)
131eda14cbcSMatt Macy 				return (CRYPTO_DATA_LEN_RANGE);
132eda14cbcSMatt Macy 
133da5137abSMartin Matuska 			memcpy(&((uint8_t *)ctx->gcm_remainder)
134da5137abSMartin Matuska 			    [ctx->gcm_remainder_len], datap, need);
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 			blockp = (uint8_t *)ctx->gcm_remainder;
137eda14cbcSMatt Macy 		} else {
138eda14cbcSMatt Macy 			blockp = datap;
139eda14cbcSMatt Macy 		}
140eda14cbcSMatt Macy 
141eda14cbcSMatt Macy 		/*
142eda14cbcSMatt Macy 		 * Increment counter. Counter bits are confined
143eda14cbcSMatt Macy 		 * to the bottom 32 bits of the counter block.
144eda14cbcSMatt Macy 		 */
145eda14cbcSMatt Macy 		counter = ntohll(ctx->gcm_cb[1] & counter_mask);
146eda14cbcSMatt Macy 		counter = htonll(counter + 1);
147eda14cbcSMatt Macy 		counter &= counter_mask;
148eda14cbcSMatt Macy 		ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy 		encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb,
151eda14cbcSMatt Macy 		    (uint8_t *)ctx->gcm_tmp);
152eda14cbcSMatt Macy 		xor_block(blockp, (uint8_t *)ctx->gcm_tmp);
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 		lastp = (uint8_t *)ctx->gcm_tmp;
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += block_size;
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy 		crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
159eda14cbcSMatt Macy 		    &out_data_1_len, &out_data_2, block_size);
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 		/* copy block to where it belongs */
162eda14cbcSMatt Macy 		if (out_data_1_len == block_size) {
163eda14cbcSMatt Macy 			copy_block(lastp, out_data_1);
164eda14cbcSMatt Macy 		} else {
165da5137abSMartin Matuska 			memcpy(out_data_1, lastp, out_data_1_len);
166eda14cbcSMatt Macy 			if (out_data_2 != NULL) {
167da5137abSMartin Matuska 				memcpy(out_data_2,
168da5137abSMartin Matuska 				    lastp + out_data_1_len,
169eda14cbcSMatt Macy 				    block_size - out_data_1_len);
170eda14cbcSMatt Macy 			}
171eda14cbcSMatt Macy 		}
172eda14cbcSMatt Macy 		/* update offset */
173eda14cbcSMatt Macy 		out->cd_offset += block_size;
174eda14cbcSMatt Macy 
175eda14cbcSMatt Macy 		/* add ciphertext to the hash */
176eda14cbcSMatt Macy 		GHASH(ctx, ctx->gcm_tmp, ctx->gcm_ghash, gops);
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy 		/* Update pointer to next block of data to be processed. */
179eda14cbcSMatt Macy 		if (ctx->gcm_remainder_len != 0) {
180eda14cbcSMatt Macy 			datap += need;
181eda14cbcSMatt Macy 			ctx->gcm_remainder_len = 0;
182eda14cbcSMatt Macy 		} else {
183eda14cbcSMatt Macy 			datap += block_size;
184eda14cbcSMatt Macy 		}
185eda14cbcSMatt Macy 
186eda14cbcSMatt Macy 		remainder = (size_t)&data[length] - (size_t)datap;
187eda14cbcSMatt Macy 
188eda14cbcSMatt Macy 		/* Incomplete last block. */
189eda14cbcSMatt Macy 		if (remainder > 0 && remainder < block_size) {
190da5137abSMartin Matuska 			memcpy(ctx->gcm_remainder, datap, remainder);
191eda14cbcSMatt Macy 			ctx->gcm_remainder_len = remainder;
192eda14cbcSMatt Macy 			ctx->gcm_copy_to = datap;
193eda14cbcSMatt Macy 			goto out;
194eda14cbcSMatt Macy 		}
195eda14cbcSMatt Macy 		ctx->gcm_copy_to = NULL;
196eda14cbcSMatt Macy 
197eda14cbcSMatt Macy 	} while (remainder > 0);
198eda14cbcSMatt Macy out:
199eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
200eda14cbcSMatt Macy }
201eda14cbcSMatt Macy 
202eda14cbcSMatt Macy int
203eda14cbcSMatt Macy gcm_encrypt_final(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
204eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
205eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
206eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
207eda14cbcSMatt Macy {
208e92ffd9bSMartin Matuska 	(void) copy_block;
209eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
210eda14cbcSMatt Macy 	if (ctx->gcm_use_avx == B_TRUE)
211eda14cbcSMatt Macy 		return (gcm_encrypt_final_avx(ctx, out, block_size));
212eda14cbcSMatt Macy #endif
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
215eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
216eda14cbcSMatt Macy 	uint8_t *ghash, *macp = NULL;
217eda14cbcSMatt Macy 	int i, rv;
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 	if (out->cd_length <
220eda14cbcSMatt Macy 	    (ctx->gcm_remainder_len + ctx->gcm_tag_len)) {
221eda14cbcSMatt Macy 		return (CRYPTO_DATA_LEN_RANGE);
222eda14cbcSMatt Macy 	}
223eda14cbcSMatt Macy 
224eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
225eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
228eda14cbcSMatt Macy 		uint64_t counter;
229eda14cbcSMatt Macy 		uint8_t *tmpp = (uint8_t *)ctx->gcm_tmp;
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy 		/*
232eda14cbcSMatt Macy 		 * Here is where we deal with data that is not a
233eda14cbcSMatt Macy 		 * multiple of the block size.
234eda14cbcSMatt Macy 		 */
235eda14cbcSMatt Macy 
236eda14cbcSMatt Macy 		/*
237eda14cbcSMatt Macy 		 * Increment counter.
238eda14cbcSMatt Macy 		 */
239eda14cbcSMatt Macy 		counter = ntohll(ctx->gcm_cb[1] & counter_mask);
240eda14cbcSMatt Macy 		counter = htonll(counter + 1);
241eda14cbcSMatt Macy 		counter &= counter_mask;
242eda14cbcSMatt Macy 		ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 		encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb,
245eda14cbcSMatt Macy 		    (uint8_t *)ctx->gcm_tmp);
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 		macp = (uint8_t *)ctx->gcm_remainder;
248da5137abSMartin Matuska 		memset(macp + ctx->gcm_remainder_len, 0,
249eda14cbcSMatt Macy 		    block_size - ctx->gcm_remainder_len);
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 		/* XOR with counter block */
252eda14cbcSMatt Macy 		for (i = 0; i < ctx->gcm_remainder_len; i++) {
253eda14cbcSMatt Macy 			macp[i] ^= tmpp[i];
254eda14cbcSMatt Macy 		}
255eda14cbcSMatt Macy 
256eda14cbcSMatt Macy 		/* add ciphertext to the hash */
257eda14cbcSMatt Macy 		GHASH(ctx, macp, ghash, gops);
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += ctx->gcm_remainder_len;
260eda14cbcSMatt Macy 	}
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] =
263eda14cbcSMatt Macy 	    htonll(CRYPTO_BYTES2BITS(ctx->gcm_processed_data_len));
264eda14cbcSMatt Macy 	GHASH(ctx, ctx->gcm_len_a_len_c, ghash, gops);
265eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_J0,
266eda14cbcSMatt Macy 	    (uint8_t *)ctx->gcm_J0);
267eda14cbcSMatt Macy 	xor_block((uint8_t *)ctx->gcm_J0, ghash);
268eda14cbcSMatt Macy 
269eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
270eda14cbcSMatt Macy 		rv = crypto_put_output_data(macp, out, ctx->gcm_remainder_len);
271eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS)
272eda14cbcSMatt Macy 			return (rv);
273eda14cbcSMatt Macy 	}
274eda14cbcSMatt Macy 	out->cd_offset += ctx->gcm_remainder_len;
275eda14cbcSMatt Macy 	ctx->gcm_remainder_len = 0;
276eda14cbcSMatt Macy 	rv = crypto_put_output_data(ghash, out, ctx->gcm_tag_len);
277eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS)
278eda14cbcSMatt Macy 		return (rv);
279eda14cbcSMatt Macy 	out->cd_offset += ctx->gcm_tag_len;
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
282eda14cbcSMatt Macy }
283eda14cbcSMatt Macy 
284eda14cbcSMatt Macy /*
285eda14cbcSMatt Macy  * This will only deal with decrypting the last block of the input that
286eda14cbcSMatt Macy  * might not be a multiple of block length.
287eda14cbcSMatt Macy  */
288eda14cbcSMatt Macy static void
289eda14cbcSMatt Macy gcm_decrypt_incomplete_block(gcm_ctx_t *ctx, size_t block_size, size_t index,
290eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
291eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
292eda14cbcSMatt Macy {
293eda14cbcSMatt Macy 	uint8_t *datap, *outp, *counterp;
294eda14cbcSMatt Macy 	uint64_t counter;
295eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
296eda14cbcSMatt Macy 	int i;
297eda14cbcSMatt Macy 
298eda14cbcSMatt Macy 	/*
299eda14cbcSMatt Macy 	 * Increment counter.
300eda14cbcSMatt Macy 	 * Counter bits are confined to the bottom 32 bits
301eda14cbcSMatt Macy 	 */
302eda14cbcSMatt Macy 	counter = ntohll(ctx->gcm_cb[1] & counter_mask);
303eda14cbcSMatt Macy 	counter = htonll(counter + 1);
304eda14cbcSMatt Macy 	counter &= counter_mask;
305eda14cbcSMatt Macy 	ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
306eda14cbcSMatt Macy 
307eda14cbcSMatt Macy 	datap = (uint8_t *)ctx->gcm_remainder;
308eda14cbcSMatt Macy 	outp = &((ctx->gcm_pt_buf)[index]);
309eda14cbcSMatt Macy 	counterp = (uint8_t *)ctx->gcm_tmp;
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy 	/* authentication tag */
312da5137abSMartin Matuska 	memset((uint8_t *)ctx->gcm_tmp, 0, block_size);
313da5137abSMartin Matuska 	memcpy((uint8_t *)ctx->gcm_tmp, datap, ctx->gcm_remainder_len);
314eda14cbcSMatt Macy 
315eda14cbcSMatt Macy 	/* add ciphertext to the hash */
316eda14cbcSMatt Macy 	GHASH(ctx, ctx->gcm_tmp, ctx->gcm_ghash, gcm_impl_get_ops());
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 	/* decrypt remaining ciphertext */
319eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, counterp);
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 	/* XOR with counter block */
322eda14cbcSMatt Macy 	for (i = 0; i < ctx->gcm_remainder_len; i++) {
323eda14cbcSMatt Macy 		outp[i] = datap[i] ^ counterp[i];
324eda14cbcSMatt Macy 	}
325eda14cbcSMatt Macy }
326eda14cbcSMatt Macy 
327eda14cbcSMatt Macy int
328eda14cbcSMatt Macy gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *ctx, char *data, size_t length,
329eda14cbcSMatt Macy     crypto_data_t *out, size_t block_size,
330eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
331eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
332eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
333eda14cbcSMatt Macy {
334e92ffd9bSMartin Matuska 	(void) out, (void) block_size, (void) encrypt_block, (void) copy_block,
335e92ffd9bSMartin Matuska 	    (void) xor_block;
336eda14cbcSMatt Macy 	size_t new_len;
337eda14cbcSMatt Macy 	uint8_t *new;
338eda14cbcSMatt Macy 
339eda14cbcSMatt Macy 	/*
340eda14cbcSMatt Macy 	 * Copy contiguous ciphertext input blocks to plaintext buffer.
341eda14cbcSMatt Macy 	 * Ciphertext will be decrypted in the final.
342eda14cbcSMatt Macy 	 */
343eda14cbcSMatt Macy 	if (length > 0) {
344eda14cbcSMatt Macy 		new_len = ctx->gcm_pt_buf_len + length;
345c03c5b1cSMartin Matuska 		new = vmem_alloc(new_len, KM_SLEEP);
346eda14cbcSMatt Macy 		if (new == NULL) {
347eda14cbcSMatt Macy 			vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
348eda14cbcSMatt Macy 			ctx->gcm_pt_buf = NULL;
349eda14cbcSMatt Macy 			return (CRYPTO_HOST_MEMORY);
350eda14cbcSMatt Macy 		}
351c03c5b1cSMartin Matuska 
352c03c5b1cSMartin Matuska 		if (ctx->gcm_pt_buf != NULL) {
353da5137abSMartin Matuska 			memcpy(new, ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
354eda14cbcSMatt Macy 			vmem_free(ctx->gcm_pt_buf, ctx->gcm_pt_buf_len);
355c03c5b1cSMartin Matuska 		} else {
356c03c5b1cSMartin Matuska 			ASSERT0(ctx->gcm_pt_buf_len);
357c03c5b1cSMartin Matuska 		}
358c03c5b1cSMartin Matuska 
359eda14cbcSMatt Macy 		ctx->gcm_pt_buf = new;
360eda14cbcSMatt Macy 		ctx->gcm_pt_buf_len = new_len;
361da5137abSMartin Matuska 		memcpy(&ctx->gcm_pt_buf[ctx->gcm_processed_data_len], data,
362eda14cbcSMatt Macy 		    length);
363eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += length;
364eda14cbcSMatt Macy 	}
365eda14cbcSMatt Macy 
366eda14cbcSMatt Macy 	ctx->gcm_remainder_len = 0;
367eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
368eda14cbcSMatt Macy }
369eda14cbcSMatt Macy 
370eda14cbcSMatt Macy int
371eda14cbcSMatt Macy gcm_decrypt_final(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
372eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
373eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
374eda14cbcSMatt Macy {
375eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
376eda14cbcSMatt Macy 	if (ctx->gcm_use_avx == B_TRUE)
377eda14cbcSMatt Macy 		return (gcm_decrypt_final_avx(ctx, out, block_size));
378eda14cbcSMatt Macy #endif
379eda14cbcSMatt Macy 
380eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
381eda14cbcSMatt Macy 	size_t pt_len;
382eda14cbcSMatt Macy 	size_t remainder;
383eda14cbcSMatt Macy 	uint8_t *ghash;
384eda14cbcSMatt Macy 	uint8_t *blockp;
385eda14cbcSMatt Macy 	uint8_t *cbp;
386eda14cbcSMatt Macy 	uint64_t counter;
387eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
388eda14cbcSMatt Macy 	int processed = 0, rv;
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy 	ASSERT(ctx->gcm_processed_data_len == ctx->gcm_pt_buf_len);
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
393eda14cbcSMatt Macy 	pt_len = ctx->gcm_processed_data_len - ctx->gcm_tag_len;
394eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
395eda14cbcSMatt Macy 	blockp = ctx->gcm_pt_buf;
396eda14cbcSMatt Macy 	remainder = pt_len;
397eda14cbcSMatt Macy 	while (remainder > 0) {
398eda14cbcSMatt Macy 		/* Incomplete last block */
399eda14cbcSMatt Macy 		if (remainder < block_size) {
400da5137abSMartin Matuska 			memcpy(ctx->gcm_remainder, blockp, remainder);
401eda14cbcSMatt Macy 			ctx->gcm_remainder_len = remainder;
402eda14cbcSMatt Macy 			/*
403eda14cbcSMatt Macy 			 * not expecting anymore ciphertext, just
404eda14cbcSMatt Macy 			 * compute plaintext for the remaining input
405eda14cbcSMatt Macy 			 */
406eda14cbcSMatt Macy 			gcm_decrypt_incomplete_block(ctx, block_size,
407eda14cbcSMatt Macy 			    processed, encrypt_block, xor_block);
408eda14cbcSMatt Macy 			ctx->gcm_remainder_len = 0;
409eda14cbcSMatt Macy 			goto out;
410eda14cbcSMatt Macy 		}
411eda14cbcSMatt Macy 		/* add ciphertext to the hash */
412eda14cbcSMatt Macy 		GHASH(ctx, blockp, ghash, gops);
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy 		/*
415eda14cbcSMatt Macy 		 * Increment counter.
416eda14cbcSMatt Macy 		 * Counter bits are confined to the bottom 32 bits
417eda14cbcSMatt Macy 		 */
418eda14cbcSMatt Macy 		counter = ntohll(ctx->gcm_cb[1] & counter_mask);
419eda14cbcSMatt Macy 		counter = htonll(counter + 1);
420eda14cbcSMatt Macy 		counter &= counter_mask;
421eda14cbcSMatt Macy 		ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
422eda14cbcSMatt Macy 
423eda14cbcSMatt Macy 		cbp = (uint8_t *)ctx->gcm_tmp;
424eda14cbcSMatt Macy 		encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_cb, cbp);
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 		/* XOR with ciphertext */
427eda14cbcSMatt Macy 		xor_block(cbp, blockp);
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy 		processed += block_size;
430eda14cbcSMatt Macy 		blockp += block_size;
431eda14cbcSMatt Macy 		remainder -= block_size;
432eda14cbcSMatt Macy 	}
433eda14cbcSMatt Macy out:
434eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(pt_len));
435eda14cbcSMatt Macy 	GHASH(ctx, ctx->gcm_len_a_len_c, ghash, gops);
436eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_J0,
437eda14cbcSMatt Macy 	    (uint8_t *)ctx->gcm_J0);
438eda14cbcSMatt Macy 	xor_block((uint8_t *)ctx->gcm_J0, ghash);
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	/* compare the input authentication tag with what we calculated */
441da5137abSMartin Matuska 	if (memcmp(&ctx->gcm_pt_buf[pt_len], ghash, ctx->gcm_tag_len)) {
442eda14cbcSMatt Macy 		/* They don't match */
443eda14cbcSMatt Macy 		return (CRYPTO_INVALID_MAC);
444eda14cbcSMatt Macy 	} else {
445eda14cbcSMatt Macy 		rv = crypto_put_output_data(ctx->gcm_pt_buf, out, pt_len);
446eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS)
447eda14cbcSMatt Macy 			return (rv);
448eda14cbcSMatt Macy 		out->cd_offset += pt_len;
449eda14cbcSMatt Macy 	}
450eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
451eda14cbcSMatt Macy }
452eda14cbcSMatt Macy 
453eda14cbcSMatt Macy static int
454eda14cbcSMatt Macy gcm_validate_args(CK_AES_GCM_PARAMS *gcm_param)
455eda14cbcSMatt Macy {
456eda14cbcSMatt Macy 	size_t tag_len;
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy 	/*
459eda14cbcSMatt Macy 	 * Check the length of the authentication tag (in bits).
460eda14cbcSMatt Macy 	 */
461eda14cbcSMatt Macy 	tag_len = gcm_param->ulTagBits;
462eda14cbcSMatt Macy 	switch (tag_len) {
463eda14cbcSMatt Macy 	case 32:
464eda14cbcSMatt Macy 	case 64:
465eda14cbcSMatt Macy 	case 96:
466eda14cbcSMatt Macy 	case 104:
467eda14cbcSMatt Macy 	case 112:
468eda14cbcSMatt Macy 	case 120:
469eda14cbcSMatt Macy 	case 128:
470eda14cbcSMatt Macy 		break;
471eda14cbcSMatt Macy 	default:
472eda14cbcSMatt Macy 		return (CRYPTO_MECHANISM_PARAM_INVALID);
473eda14cbcSMatt Macy 	}
474eda14cbcSMatt Macy 
475eda14cbcSMatt Macy 	if (gcm_param->ulIvLen == 0)
476eda14cbcSMatt Macy 		return (CRYPTO_MECHANISM_PARAM_INVALID);
477eda14cbcSMatt Macy 
478eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
479eda14cbcSMatt Macy }
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy static void
4822a58b312SMartin Matuska gcm_format_initial_blocks(const uint8_t *iv, ulong_t iv_len,
483eda14cbcSMatt Macy     gcm_ctx_t *ctx, size_t block_size,
484eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
485eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
486eda14cbcSMatt Macy {
487eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
488eda14cbcSMatt Macy 	uint8_t *cb;
489eda14cbcSMatt Macy 	ulong_t remainder = iv_len;
490eda14cbcSMatt Macy 	ulong_t processed = 0;
491eda14cbcSMatt Macy 	uint8_t *datap, *ghash;
492eda14cbcSMatt Macy 	uint64_t len_a_len_c[2];
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
495eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
496eda14cbcSMatt Macy 	cb = (uint8_t *)ctx->gcm_cb;
497eda14cbcSMatt Macy 	if (iv_len == 12) {
498da5137abSMartin Matuska 		memcpy(cb, iv, 12);
499eda14cbcSMatt Macy 		cb[12] = 0;
500eda14cbcSMatt Macy 		cb[13] = 0;
501eda14cbcSMatt Macy 		cb[14] = 0;
502eda14cbcSMatt Macy 		cb[15] = 1;
503eda14cbcSMatt Macy 		/* J0 will be used again in the final */
504eda14cbcSMatt Macy 		copy_block(cb, (uint8_t *)ctx->gcm_J0);
505eda14cbcSMatt Macy 	} else {
506eda14cbcSMatt Macy 		/* GHASH the IV */
507eda14cbcSMatt Macy 		do {
508eda14cbcSMatt Macy 			if (remainder < block_size) {
509da5137abSMartin Matuska 				memset(cb, 0, block_size);
510da5137abSMartin Matuska 				memcpy(cb, &(iv[processed]), remainder);
511eda14cbcSMatt Macy 				datap = (uint8_t *)cb;
512eda14cbcSMatt Macy 				remainder = 0;
513eda14cbcSMatt Macy 			} else {
514eda14cbcSMatt Macy 				datap = (uint8_t *)(&(iv[processed]));
515eda14cbcSMatt Macy 				processed += block_size;
516eda14cbcSMatt Macy 				remainder -= block_size;
517eda14cbcSMatt Macy 			}
518eda14cbcSMatt Macy 			GHASH(ctx, datap, ghash, gops);
519eda14cbcSMatt Macy 		} while (remainder > 0);
520eda14cbcSMatt Macy 
521eda14cbcSMatt Macy 		len_a_len_c[0] = 0;
522eda14cbcSMatt Macy 		len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(iv_len));
523eda14cbcSMatt Macy 		GHASH(ctx, len_a_len_c, ctx->gcm_J0, gops);
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 		/* J0 will be used again in the final */
526eda14cbcSMatt Macy 		copy_block((uint8_t *)ctx->gcm_J0, (uint8_t *)cb);
527eda14cbcSMatt Macy 	}
528eda14cbcSMatt Macy }
529eda14cbcSMatt Macy 
530eda14cbcSMatt Macy static int
5312a58b312SMartin Matuska gcm_init(gcm_ctx_t *ctx, const uint8_t *iv, size_t iv_len,
5322a58b312SMartin Matuska     const uint8_t *auth_data, size_t auth_data_len, size_t block_size,
533eda14cbcSMatt Macy     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
534eda14cbcSMatt Macy     void (*copy_block)(uint8_t *, uint8_t *),
535eda14cbcSMatt Macy     void (*xor_block)(uint8_t *, uint8_t *))
536eda14cbcSMatt Macy {
537eda14cbcSMatt Macy 	const gcm_impl_ops_t *gops;
538eda14cbcSMatt Macy 	uint8_t *ghash, *datap, *authp;
539eda14cbcSMatt Macy 	size_t remainder, processed;
540eda14cbcSMatt Macy 
541eda14cbcSMatt Macy 	/* encrypt zero block to get subkey H */
542da5137abSMartin Matuska 	memset(ctx->gcm_H, 0, sizeof (ctx->gcm_H));
543eda14cbcSMatt Macy 	encrypt_block(ctx->gcm_keysched, (uint8_t *)ctx->gcm_H,
544eda14cbcSMatt Macy 	    (uint8_t *)ctx->gcm_H);
545eda14cbcSMatt Macy 
546eda14cbcSMatt Macy 	gcm_format_initial_blocks(iv, iv_len, ctx, block_size,
547eda14cbcSMatt Macy 	    copy_block, xor_block);
548eda14cbcSMatt Macy 
549eda14cbcSMatt Macy 	gops = gcm_impl_get_ops();
550eda14cbcSMatt Macy 	authp = (uint8_t *)ctx->gcm_tmp;
551eda14cbcSMatt Macy 	ghash = (uint8_t *)ctx->gcm_ghash;
552da5137abSMartin Matuska 	memset(authp, 0, block_size);
553da5137abSMartin Matuska 	memset(ghash, 0, block_size);
554eda14cbcSMatt Macy 
555eda14cbcSMatt Macy 	processed = 0;
556eda14cbcSMatt Macy 	remainder = auth_data_len;
557eda14cbcSMatt Macy 	do {
558eda14cbcSMatt Macy 		if (remainder < block_size) {
559eda14cbcSMatt Macy 			/*
560eda14cbcSMatt Macy 			 * There's not a block full of data, pad rest of
561eda14cbcSMatt Macy 			 * buffer with zero
562eda14cbcSMatt Macy 			 */
563c03c5b1cSMartin Matuska 
564c03c5b1cSMartin Matuska 			if (auth_data != NULL) {
565da5137abSMartin Matuska 				memset(authp, 0, block_size);
566da5137abSMartin Matuska 				memcpy(authp, &(auth_data[processed]),
567da5137abSMartin Matuska 				    remainder);
568c03c5b1cSMartin Matuska 			} else {
569c03c5b1cSMartin Matuska 				ASSERT0(remainder);
570c03c5b1cSMartin Matuska 			}
571c03c5b1cSMartin Matuska 
572eda14cbcSMatt Macy 			datap = (uint8_t *)authp;
573eda14cbcSMatt Macy 			remainder = 0;
574eda14cbcSMatt Macy 		} else {
575eda14cbcSMatt Macy 			datap = (uint8_t *)(&(auth_data[processed]));
576eda14cbcSMatt Macy 			processed += block_size;
577eda14cbcSMatt Macy 			remainder -= block_size;
578eda14cbcSMatt Macy 		}
579eda14cbcSMatt Macy 
580eda14cbcSMatt Macy 		/* add auth data to the hash */
581eda14cbcSMatt Macy 		GHASH(ctx, datap, ghash, gops);
582eda14cbcSMatt Macy 
583eda14cbcSMatt Macy 	} while (remainder > 0);
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
586eda14cbcSMatt Macy }
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy /*
5892a58b312SMartin Matuska  * Init the GCM context struct. Handle the cycle and avx implementations here.
5902a58b312SMartin Matuska  */
591*75e1fea6SMartin Matuska int
592*75e1fea6SMartin Matuska gcm_init_ctx(gcm_ctx_t *gcm_ctx, char *param,
5932a58b312SMartin Matuska     size_t block_size, int (*encrypt_block)(const void *, const uint8_t *,
5942a58b312SMartin Matuska     uint8_t *), void (*copy_block)(uint8_t *, uint8_t *),
5952a58b312SMartin Matuska     void (*xor_block)(uint8_t *, uint8_t *))
5962a58b312SMartin Matuska {
597eda14cbcSMatt Macy 	CK_AES_GCM_PARAMS *gcm_param;
5982a58b312SMartin Matuska 	int rv = CRYPTO_SUCCESS;
5992a58b312SMartin Matuska 	size_t tag_len, iv_len;
600eda14cbcSMatt Macy 
601eda14cbcSMatt Macy 	if (param != NULL) {
602eda14cbcSMatt Macy 		gcm_param = (CK_AES_GCM_PARAMS *)(void *)param;
603eda14cbcSMatt Macy 
6042a58b312SMartin Matuska 		/* GCM mode. */
605eda14cbcSMatt Macy 		if ((rv = gcm_validate_args(gcm_param)) != 0) {
606eda14cbcSMatt Macy 			return (rv);
607eda14cbcSMatt Macy 		}
6082a58b312SMartin Matuska 		gcm_ctx->gcm_flags |= GCM_MODE;
609eda14cbcSMatt Macy 
6102a58b312SMartin Matuska 		size_t tbits = gcm_param->ulTagBits;
6112a58b312SMartin Matuska 		tag_len = CRYPTO_BITS2BYTES(tbits);
6122a58b312SMartin Matuska 		iv_len = gcm_param->ulIvLen;
613*75e1fea6SMartin Matuska 
6142a58b312SMartin Matuska 		gcm_ctx->gcm_tag_len = tag_len;
615eda14cbcSMatt Macy 		gcm_ctx->gcm_processed_data_len = 0;
616eda14cbcSMatt Macy 
617eda14cbcSMatt Macy 		/* these values are in bits */
618eda14cbcSMatt Macy 		gcm_ctx->gcm_len_a_len_c[0]
619eda14cbcSMatt Macy 		    = htonll(CRYPTO_BYTES2BITS(gcm_param->ulAADLen));
620eda14cbcSMatt Macy 	} else {
621eda14cbcSMatt Macy 		return (CRYPTO_MECHANISM_PARAM_INVALID);
622eda14cbcSMatt Macy 	}
623eda14cbcSMatt Macy 
6242a58b312SMartin Matuska 	const uint8_t *iv = (const uint8_t *)gcm_param->pIv;
6252a58b312SMartin Matuska 	const uint8_t *aad = (const uint8_t *)gcm_param->pAAD;
6262a58b312SMartin Matuska 	size_t aad_len = gcm_param->ulAADLen;
6272a58b312SMartin Matuska 
628eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
6292a58b312SMartin Matuska 	boolean_t needs_bswap =
6302a58b312SMartin Matuska 	    ((aes_key_t *)gcm_ctx->gcm_keysched)->ops->needs_byteswap;
6312a58b312SMartin Matuska 
632eda14cbcSMatt Macy 	if (GCM_IMPL_READ(icp_gcm_impl) != IMPL_CYCLE) {
633eda14cbcSMatt Macy 		gcm_ctx->gcm_use_avx = GCM_IMPL_USE_AVX;
634eda14cbcSMatt Macy 	} else {
635eda14cbcSMatt Macy 		/*
636eda14cbcSMatt Macy 		 * Handle the "cycle" implementation by creating avx and
637eda14cbcSMatt Macy 		 * non-avx contexts alternately.
638eda14cbcSMatt Macy 		 */
639eda14cbcSMatt Macy 		gcm_ctx->gcm_use_avx = gcm_toggle_avx();
6402a58b312SMartin Matuska 
6412a58b312SMartin Matuska 		/* The avx impl. doesn't handle byte swapped key schedules. */
6422a58b312SMartin Matuska 		if (gcm_ctx->gcm_use_avx == B_TRUE && needs_bswap == B_TRUE) {
643eda14cbcSMatt Macy 			gcm_ctx->gcm_use_avx = B_FALSE;
644eda14cbcSMatt Macy 		}
6452a58b312SMartin Matuska 		/*
6462a58b312SMartin Matuska 		 * If this is a GCM context, use the MOVBE and the BSWAP
647*75e1fea6SMartin Matuska 		 * variants alternately.
6482a58b312SMartin Matuska 		 */
649*75e1fea6SMartin Matuska 		if (gcm_ctx->gcm_use_avx == B_TRUE &&
650eda14cbcSMatt Macy 		    zfs_movbe_available() == B_TRUE) {
651eda14cbcSMatt Macy 			(void) atomic_toggle_boolean_nv(
652eda14cbcSMatt Macy 			    (volatile boolean_t *)&gcm_avx_can_use_movbe);
653eda14cbcSMatt Macy 		}
654eda14cbcSMatt Macy 	}
655eda14cbcSMatt Macy 	/*
6562a58b312SMartin Matuska 	 * We don't handle byte swapped key schedules in the avx code path,
6572a58b312SMartin Matuska 	 * still they could be created by the aes generic implementation.
6582a58b312SMartin Matuska 	 * Make sure not to use them since we'll corrupt data if we do.
659eda14cbcSMatt Macy 	 */
6602a58b312SMartin Matuska 	if (gcm_ctx->gcm_use_avx == B_TRUE && needs_bswap == B_TRUE) {
661eda14cbcSMatt Macy 		gcm_ctx->gcm_use_avx = B_FALSE;
6622a58b312SMartin Matuska 
6632a58b312SMartin Matuska 		cmn_err_once(CE_WARN,
6642a58b312SMartin Matuska 		    "ICP: Can't use the aes generic or cycle implementations "
6652a58b312SMartin Matuska 		    "in combination with the gcm avx implementation!");
6662a58b312SMartin Matuska 		cmn_err_once(CE_WARN,
6672a58b312SMartin Matuska 		    "ICP: Falling back to a compatible implementation, "
6682a58b312SMartin Matuska 		    "aes-gcm performance will likely be degraded.");
6692a58b312SMartin Matuska 		cmn_err_once(CE_WARN,
6702a58b312SMartin Matuska 		    "ICP: Choose at least the x86_64 aes implementation to "
6712a58b312SMartin Matuska 		    "restore performance.");
672eda14cbcSMatt Macy 	}
6732a58b312SMartin Matuska 
6747877fdebSMatt Macy 	/* Allocate Htab memory as needed. */
6757877fdebSMatt Macy 	if (gcm_ctx->gcm_use_avx == B_TRUE) {
6767877fdebSMatt Macy 		size_t htab_len = gcm_simd_get_htab_size(gcm_ctx->gcm_use_avx);
6777877fdebSMatt Macy 
6787877fdebSMatt Macy 		if (htab_len == 0) {
6797877fdebSMatt Macy 			return (CRYPTO_MECHANISM_PARAM_INVALID);
6807877fdebSMatt Macy 		}
6817877fdebSMatt Macy 		gcm_ctx->gcm_htab_len = htab_len;
6827877fdebSMatt Macy 		gcm_ctx->gcm_Htable =
68315f0b8c3SMartin Matuska 		    kmem_alloc(htab_len, KM_SLEEP);
6847877fdebSMatt Macy 
6857877fdebSMatt Macy 		if (gcm_ctx->gcm_Htable == NULL) {
6867877fdebSMatt Macy 			return (CRYPTO_HOST_MEMORY);
6877877fdebSMatt Macy 		}
6887877fdebSMatt Macy 	}
689eda14cbcSMatt Macy 	/* Avx and non avx context initialization differs from here on. */
690eda14cbcSMatt Macy 	if (gcm_ctx->gcm_use_avx == B_FALSE) {
691eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
6922a58b312SMartin Matuska 		if (gcm_init(gcm_ctx, iv, iv_len, aad, aad_len, block_size,
6932a58b312SMartin Matuska 		    encrypt_block, copy_block, xor_block) != CRYPTO_SUCCESS) {
694eda14cbcSMatt Macy 			rv = CRYPTO_MECHANISM_PARAM_INVALID;
695eda14cbcSMatt Macy 		}
696eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
697eda14cbcSMatt Macy 	} else {
6982a58b312SMartin Matuska 		if (gcm_init_avx(gcm_ctx, iv, iv_len, aad, aad_len,
6992a58b312SMartin Matuska 		    block_size) != CRYPTO_SUCCESS) {
700eda14cbcSMatt Macy 			rv = CRYPTO_MECHANISM_PARAM_INVALID;
701eda14cbcSMatt Macy 		}
702eda14cbcSMatt Macy 	}
703eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
704eda14cbcSMatt Macy 
705eda14cbcSMatt Macy 	return (rv);
706eda14cbcSMatt Macy }
707eda14cbcSMatt Macy 
708eda14cbcSMatt Macy void *
709eda14cbcSMatt Macy gcm_alloc_ctx(int kmflag)
710eda14cbcSMatt Macy {
711eda14cbcSMatt Macy 	gcm_ctx_t *gcm_ctx;
712eda14cbcSMatt Macy 
713eda14cbcSMatt Macy 	if ((gcm_ctx = kmem_zalloc(sizeof (gcm_ctx_t), kmflag)) == NULL)
714eda14cbcSMatt Macy 		return (NULL);
715eda14cbcSMatt Macy 
716eda14cbcSMatt Macy 	gcm_ctx->gcm_flags = GCM_MODE;
717eda14cbcSMatt Macy 	return (gcm_ctx);
718eda14cbcSMatt Macy }
719eda14cbcSMatt Macy 
720eda14cbcSMatt Macy /* GCM implementation that contains the fastest methods */
721eda14cbcSMatt Macy static gcm_impl_ops_t gcm_fastest_impl = {
722eda14cbcSMatt Macy 	.name = "fastest"
723eda14cbcSMatt Macy };
724eda14cbcSMatt Macy 
725eda14cbcSMatt Macy /* All compiled in implementations */
726e92ffd9bSMartin Matuska static const gcm_impl_ops_t *gcm_all_impl[] = {
727eda14cbcSMatt Macy 	&gcm_generic_impl,
728eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_PCLMULQDQ)
729eda14cbcSMatt Macy 	&gcm_pclmulqdq_impl,
730eda14cbcSMatt Macy #endif
731eda14cbcSMatt Macy };
732eda14cbcSMatt Macy 
733eda14cbcSMatt Macy /* Indicate that benchmark has been completed */
734eda14cbcSMatt Macy static boolean_t gcm_impl_initialized = B_FALSE;
735eda14cbcSMatt Macy 
736eda14cbcSMatt Macy /* Hold all supported implementations */
737eda14cbcSMatt Macy static size_t gcm_supp_impl_cnt = 0;
738eda14cbcSMatt Macy static gcm_impl_ops_t *gcm_supp_impl[ARRAY_SIZE(gcm_all_impl)];
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy /*
741eda14cbcSMatt Macy  * Returns the GCM operations for encrypt/decrypt/key setup.  When a
742eda14cbcSMatt Macy  * SIMD implementation is not allowed in the current context, then
743eda14cbcSMatt Macy  * fallback to the fastest generic implementation.
744eda14cbcSMatt Macy  */
745eda14cbcSMatt Macy const gcm_impl_ops_t *
746716fd348SMartin Matuska gcm_impl_get_ops(void)
747eda14cbcSMatt Macy {
748eda14cbcSMatt Macy 	if (!kfpu_allowed())
749eda14cbcSMatt Macy 		return (&gcm_generic_impl);
750eda14cbcSMatt Macy 
751eda14cbcSMatt Macy 	const gcm_impl_ops_t *ops = NULL;
752eda14cbcSMatt Macy 	const uint32_t impl = GCM_IMPL_READ(icp_gcm_impl);
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy 	switch (impl) {
755eda14cbcSMatt Macy 	case IMPL_FASTEST:
756eda14cbcSMatt Macy 		ASSERT(gcm_impl_initialized);
757eda14cbcSMatt Macy 		ops = &gcm_fastest_impl;
758eda14cbcSMatt Macy 		break;
759eda14cbcSMatt Macy 	case IMPL_CYCLE:
760eda14cbcSMatt Macy 		/* Cycle through supported implementations */
761eda14cbcSMatt Macy 		ASSERT(gcm_impl_initialized);
762eda14cbcSMatt Macy 		ASSERT3U(gcm_supp_impl_cnt, >, 0);
763eda14cbcSMatt Macy 		static size_t cycle_impl_idx = 0;
764eda14cbcSMatt Macy 		size_t idx = (++cycle_impl_idx) % gcm_supp_impl_cnt;
765eda14cbcSMatt Macy 		ops = gcm_supp_impl[idx];
766eda14cbcSMatt Macy 		break;
767eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
768eda14cbcSMatt Macy 	case IMPL_AVX:
769eda14cbcSMatt Macy 		/*
770eda14cbcSMatt Macy 		 * Make sure that we return a valid implementation while
771eda14cbcSMatt Macy 		 * switching to the avx implementation since there still
772eda14cbcSMatt Macy 		 * may be unfinished non-avx contexts around.
773eda14cbcSMatt Macy 		 */
774eda14cbcSMatt Macy 		ops = &gcm_generic_impl;
775eda14cbcSMatt Macy 		break;
776eda14cbcSMatt Macy #endif
777eda14cbcSMatt Macy 	default:
778eda14cbcSMatt Macy 		ASSERT3U(impl, <, gcm_supp_impl_cnt);
779eda14cbcSMatt Macy 		ASSERT3U(gcm_supp_impl_cnt, >, 0);
780eda14cbcSMatt Macy 		if (impl < ARRAY_SIZE(gcm_all_impl))
781eda14cbcSMatt Macy 			ops = gcm_supp_impl[impl];
782eda14cbcSMatt Macy 		break;
783eda14cbcSMatt Macy 	}
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 	ASSERT3P(ops, !=, NULL);
786eda14cbcSMatt Macy 
787eda14cbcSMatt Macy 	return (ops);
788eda14cbcSMatt Macy }
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy /*
791eda14cbcSMatt Macy  * Initialize all supported implementations.
792eda14cbcSMatt Macy  */
793eda14cbcSMatt Macy void
794eda14cbcSMatt Macy gcm_impl_init(void)
795eda14cbcSMatt Macy {
796eda14cbcSMatt Macy 	gcm_impl_ops_t *curr_impl;
797eda14cbcSMatt Macy 	int i, c;
798eda14cbcSMatt Macy 
799eda14cbcSMatt Macy 	/* Move supported implementations into gcm_supp_impls */
800eda14cbcSMatt Macy 	for (i = 0, c = 0; i < ARRAY_SIZE(gcm_all_impl); i++) {
801eda14cbcSMatt Macy 		curr_impl = (gcm_impl_ops_t *)gcm_all_impl[i];
802eda14cbcSMatt Macy 
803eda14cbcSMatt Macy 		if (curr_impl->is_supported())
804eda14cbcSMatt Macy 			gcm_supp_impl[c++] = (gcm_impl_ops_t *)curr_impl;
805eda14cbcSMatt Macy 	}
806eda14cbcSMatt Macy 	gcm_supp_impl_cnt = c;
807eda14cbcSMatt Macy 
808eda14cbcSMatt Macy 	/*
809eda14cbcSMatt Macy 	 * Set the fastest implementation given the assumption that the
810eda14cbcSMatt Macy 	 * hardware accelerated version is the fastest.
811eda14cbcSMatt Macy 	 */
812eda14cbcSMatt Macy #if defined(__x86_64) && defined(HAVE_PCLMULQDQ)
813eda14cbcSMatt Macy 	if (gcm_pclmulqdq_impl.is_supported()) {
814eda14cbcSMatt Macy 		memcpy(&gcm_fastest_impl, &gcm_pclmulqdq_impl,
815eda14cbcSMatt Macy 		    sizeof (gcm_fastest_impl));
816eda14cbcSMatt Macy 	} else
817eda14cbcSMatt Macy #endif
818eda14cbcSMatt Macy 	{
819eda14cbcSMatt Macy 		memcpy(&gcm_fastest_impl, &gcm_generic_impl,
820eda14cbcSMatt Macy 		    sizeof (gcm_fastest_impl));
821eda14cbcSMatt Macy 	}
822eda14cbcSMatt Macy 
823eda14cbcSMatt Macy 	strlcpy(gcm_fastest_impl.name, "fastest", GCM_IMPL_NAME_MAX);
824eda14cbcSMatt Macy 
825eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
826eda14cbcSMatt Macy 	/*
827eda14cbcSMatt Macy 	 * Use the avx implementation if it's available and the implementation
828eda14cbcSMatt Macy 	 * hasn't changed from its default value of fastest on module load.
829eda14cbcSMatt Macy 	 */
830eda14cbcSMatt Macy 	if (gcm_avx_will_work()) {
831eda14cbcSMatt Macy #ifdef HAVE_MOVBE
832eda14cbcSMatt Macy 		if (zfs_movbe_available() == B_TRUE) {
833eda14cbcSMatt Macy 			atomic_swap_32(&gcm_avx_can_use_movbe, B_TRUE);
834eda14cbcSMatt Macy 		}
835eda14cbcSMatt Macy #endif
836eda14cbcSMatt Macy 		if (GCM_IMPL_READ(user_sel_impl) == IMPL_FASTEST) {
837eda14cbcSMatt Macy 			gcm_set_avx(B_TRUE);
838eda14cbcSMatt Macy 		}
839eda14cbcSMatt Macy 	}
840eda14cbcSMatt Macy #endif
841eda14cbcSMatt Macy 	/* Finish initialization */
842eda14cbcSMatt Macy 	atomic_swap_32(&icp_gcm_impl, user_sel_impl);
843eda14cbcSMatt Macy 	gcm_impl_initialized = B_TRUE;
844eda14cbcSMatt Macy }
845eda14cbcSMatt Macy 
846eda14cbcSMatt Macy static const struct {
847a0b956f5SMartin Matuska 	const char *name;
848eda14cbcSMatt Macy 	uint32_t sel;
849eda14cbcSMatt Macy } gcm_impl_opts[] = {
850eda14cbcSMatt Macy 		{ "cycle",	IMPL_CYCLE },
851eda14cbcSMatt Macy 		{ "fastest",	IMPL_FASTEST },
852eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
853eda14cbcSMatt Macy 		{ "avx",	IMPL_AVX },
854eda14cbcSMatt Macy #endif
855eda14cbcSMatt Macy };
856eda14cbcSMatt Macy 
857eda14cbcSMatt Macy /*
858eda14cbcSMatt Macy  * Function sets desired gcm implementation.
859eda14cbcSMatt Macy  *
860eda14cbcSMatt Macy  * If we are called before init(), user preference will be saved in
861eda14cbcSMatt Macy  * user_sel_impl, and applied in later init() call. This occurs when module
862eda14cbcSMatt Macy  * parameter is specified on module load. Otherwise, directly update
863eda14cbcSMatt Macy  * icp_gcm_impl.
864eda14cbcSMatt Macy  *
865eda14cbcSMatt Macy  * @val		Name of gcm implementation to use
866eda14cbcSMatt Macy  * @param	Unused.
867eda14cbcSMatt Macy  */
868eda14cbcSMatt Macy int
869eda14cbcSMatt Macy gcm_impl_set(const char *val)
870eda14cbcSMatt Macy {
871eda14cbcSMatt Macy 	int err = -EINVAL;
872eda14cbcSMatt Macy 	char req_name[GCM_IMPL_NAME_MAX];
873eda14cbcSMatt Macy 	uint32_t impl = GCM_IMPL_READ(user_sel_impl);
874eda14cbcSMatt Macy 	size_t i;
875eda14cbcSMatt Macy 
876eda14cbcSMatt Macy 	/* sanitize input */
877eda14cbcSMatt Macy 	i = strnlen(val, GCM_IMPL_NAME_MAX);
878eda14cbcSMatt Macy 	if (i == 0 || i >= GCM_IMPL_NAME_MAX)
879eda14cbcSMatt Macy 		return (err);
880eda14cbcSMatt Macy 
881eda14cbcSMatt Macy 	strlcpy(req_name, val, GCM_IMPL_NAME_MAX);
882eda14cbcSMatt Macy 	while (i > 0 && isspace(req_name[i-1]))
883eda14cbcSMatt Macy 		i--;
884eda14cbcSMatt Macy 	req_name[i] = '\0';
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 	/* Check mandatory options */
887eda14cbcSMatt Macy 	for (i = 0; i < ARRAY_SIZE(gcm_impl_opts); i++) {
888eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
889eda14cbcSMatt Macy 		/* Ignore avx implementation if it won't work. */
890eda14cbcSMatt Macy 		if (gcm_impl_opts[i].sel == IMPL_AVX && !gcm_avx_will_work()) {
891eda14cbcSMatt Macy 			continue;
892eda14cbcSMatt Macy 		}
893eda14cbcSMatt Macy #endif
894eda14cbcSMatt Macy 		if (strcmp(req_name, gcm_impl_opts[i].name) == 0) {
895eda14cbcSMatt Macy 			impl = gcm_impl_opts[i].sel;
896eda14cbcSMatt Macy 			err = 0;
897eda14cbcSMatt Macy 			break;
898eda14cbcSMatt Macy 		}
899eda14cbcSMatt Macy 	}
900eda14cbcSMatt Macy 
901eda14cbcSMatt Macy 	/* check all supported impl if init() was already called */
902eda14cbcSMatt Macy 	if (err != 0 && gcm_impl_initialized) {
903eda14cbcSMatt Macy 		/* check all supported implementations */
904eda14cbcSMatt Macy 		for (i = 0; i < gcm_supp_impl_cnt; i++) {
905eda14cbcSMatt Macy 			if (strcmp(req_name, gcm_supp_impl[i]->name) == 0) {
906eda14cbcSMatt Macy 				impl = i;
907eda14cbcSMatt Macy 				err = 0;
908eda14cbcSMatt Macy 				break;
909eda14cbcSMatt Macy 			}
910eda14cbcSMatt Macy 		}
911eda14cbcSMatt Macy 	}
912eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
913eda14cbcSMatt Macy 	/*
914eda14cbcSMatt Macy 	 * Use the avx implementation if available and the requested one is
915eda14cbcSMatt Macy 	 * avx or fastest.
916eda14cbcSMatt Macy 	 */
917eda14cbcSMatt Macy 	if (gcm_avx_will_work() == B_TRUE &&
918eda14cbcSMatt Macy 	    (impl == IMPL_AVX || impl == IMPL_FASTEST)) {
919eda14cbcSMatt Macy 		gcm_set_avx(B_TRUE);
920eda14cbcSMatt Macy 	} else {
921eda14cbcSMatt Macy 		gcm_set_avx(B_FALSE);
922eda14cbcSMatt Macy 	}
923eda14cbcSMatt Macy #endif
924eda14cbcSMatt Macy 
925eda14cbcSMatt Macy 	if (err == 0) {
926eda14cbcSMatt Macy 		if (gcm_impl_initialized)
927eda14cbcSMatt Macy 			atomic_swap_32(&icp_gcm_impl, impl);
928eda14cbcSMatt Macy 		else
929eda14cbcSMatt Macy 			atomic_swap_32(&user_sel_impl, impl);
930eda14cbcSMatt Macy 	}
931eda14cbcSMatt Macy 
932eda14cbcSMatt Macy 	return (err);
933eda14cbcSMatt Macy }
934eda14cbcSMatt Macy 
935eda14cbcSMatt Macy #if defined(_KERNEL) && defined(__linux__)
936eda14cbcSMatt Macy 
937eda14cbcSMatt Macy static int
938eda14cbcSMatt Macy icp_gcm_impl_set(const char *val, zfs_kernel_param_t *kp)
939eda14cbcSMatt Macy {
940eda14cbcSMatt Macy 	return (gcm_impl_set(val));
941eda14cbcSMatt Macy }
942eda14cbcSMatt Macy 
943eda14cbcSMatt Macy static int
944eda14cbcSMatt Macy icp_gcm_impl_get(char *buffer, zfs_kernel_param_t *kp)
945eda14cbcSMatt Macy {
946eda14cbcSMatt Macy 	int i, cnt = 0;
947eda14cbcSMatt Macy 	char *fmt;
948eda14cbcSMatt Macy 	const uint32_t impl = GCM_IMPL_READ(icp_gcm_impl);
949eda14cbcSMatt Macy 
950eda14cbcSMatt Macy 	ASSERT(gcm_impl_initialized);
951eda14cbcSMatt Macy 
952eda14cbcSMatt Macy 	/* list mandatory options */
953eda14cbcSMatt Macy 	for (i = 0; i < ARRAY_SIZE(gcm_impl_opts); i++) {
954eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
955eda14cbcSMatt Macy 		/* Ignore avx implementation if it won't work. */
956eda14cbcSMatt Macy 		if (gcm_impl_opts[i].sel == IMPL_AVX && !gcm_avx_will_work()) {
957eda14cbcSMatt Macy 			continue;
958eda14cbcSMatt Macy 		}
959eda14cbcSMatt Macy #endif
960eda14cbcSMatt Macy 		fmt = (impl == gcm_impl_opts[i].sel) ? "[%s] " : "%s ";
961bb2d13b6SMartin Matuska 		cnt += kmem_scnprintf(buffer + cnt, PAGE_SIZE - cnt, fmt,
962bb2d13b6SMartin Matuska 		    gcm_impl_opts[i].name);
963eda14cbcSMatt Macy 	}
964eda14cbcSMatt Macy 
965eda14cbcSMatt Macy 	/* list all supported implementations */
966eda14cbcSMatt Macy 	for (i = 0; i < gcm_supp_impl_cnt; i++) {
967eda14cbcSMatt Macy 		fmt = (i == impl) ? "[%s] " : "%s ";
968bb2d13b6SMartin Matuska 		cnt += kmem_scnprintf(buffer + cnt, PAGE_SIZE - cnt, fmt,
969bb2d13b6SMartin Matuska 		    gcm_supp_impl[i]->name);
970eda14cbcSMatt Macy 	}
971eda14cbcSMatt Macy 
972eda14cbcSMatt Macy 	return (cnt);
973eda14cbcSMatt Macy }
974eda14cbcSMatt Macy 
975eda14cbcSMatt Macy module_param_call(icp_gcm_impl, icp_gcm_impl_set, icp_gcm_impl_get,
976eda14cbcSMatt Macy     NULL, 0644);
977eda14cbcSMatt Macy MODULE_PARM_DESC(icp_gcm_impl, "Select gcm implementation.");
978eda14cbcSMatt Macy #endif /* defined(__KERNEL) */
979eda14cbcSMatt Macy 
980eda14cbcSMatt Macy #ifdef CAN_USE_GCM_ASM
981eda14cbcSMatt Macy #define	GCM_BLOCK_LEN 16
982eda14cbcSMatt Macy /*
983eda14cbcSMatt Macy  * The openssl asm routines are 6x aggregated and need that many bytes
984eda14cbcSMatt Macy  * at minimum.
985eda14cbcSMatt Macy  */
986eda14cbcSMatt Macy #define	GCM_AVX_MIN_DECRYPT_BYTES (GCM_BLOCK_LEN * 6)
987eda14cbcSMatt Macy #define	GCM_AVX_MIN_ENCRYPT_BYTES (GCM_BLOCK_LEN * 6 * 3)
988eda14cbcSMatt Macy /*
989eda14cbcSMatt Macy  * Ensure the chunk size is reasonable since we are allocating a
990eda14cbcSMatt Macy  * GCM_AVX_MAX_CHUNK_SIZEd buffer and disabling preemption and interrupts.
991eda14cbcSMatt Macy  */
992eda14cbcSMatt Macy #define	GCM_AVX_MAX_CHUNK_SIZE \
993eda14cbcSMatt Macy 	(((128*1024)/GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES)
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy /* Clear the FPU registers since they hold sensitive internal state. */
996eda14cbcSMatt Macy #define	clear_fpu_regs() clear_fpu_regs_avx()
997eda14cbcSMatt Macy #define	GHASH_AVX(ctx, in, len) \
9987877fdebSMatt Macy     gcm_ghash_avx((ctx)->gcm_ghash, (const uint64_t *)(ctx)->gcm_Htable, \
999eda14cbcSMatt Macy     in, len)
1000eda14cbcSMatt Macy 
1001eda14cbcSMatt Macy #define	gcm_incr_counter_block(ctx) gcm_incr_counter_block_by(ctx, 1)
1002eda14cbcSMatt Macy 
1003e92ffd9bSMartin Matuska /* Get the chunk size module parameter. */
1004e92ffd9bSMartin Matuska #define	GCM_CHUNK_SIZE_READ *(volatile uint32_t *) &gcm_avx_chunk_size
1005e92ffd9bSMartin Matuska 
1006eda14cbcSMatt Macy /*
1007eda14cbcSMatt Macy  * Module parameter: number of bytes to process at once while owning the FPU.
1008eda14cbcSMatt Macy  * Rounded down to the next GCM_AVX_MIN_DECRYPT_BYTES byte boundary and is
1009eda14cbcSMatt Macy  * ensured to be greater or equal than GCM_AVX_MIN_DECRYPT_BYTES.
1010eda14cbcSMatt Macy  */
1011eda14cbcSMatt Macy static uint32_t gcm_avx_chunk_size =
1012eda14cbcSMatt Macy 	((32 * 1024) / GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES;
1013eda14cbcSMatt Macy 
101415f0b8c3SMartin Matuska extern void ASMABI clear_fpu_regs_avx(void);
101515f0b8c3SMartin Matuska extern void ASMABI gcm_xor_avx(const uint8_t *src, uint8_t *dst);
101615f0b8c3SMartin Matuska extern void ASMABI aes_encrypt_intel(const uint32_t rk[], int nr,
1017eda14cbcSMatt Macy     const uint32_t pt[4], uint32_t ct[4]);
1018eda14cbcSMatt Macy 
101915f0b8c3SMartin Matuska extern void ASMABI gcm_init_htab_avx(uint64_t *Htable, const uint64_t H[2]);
102015f0b8c3SMartin Matuska extern void ASMABI gcm_ghash_avx(uint64_t ghash[2], const uint64_t *Htable,
1021eda14cbcSMatt Macy     const uint8_t *in, size_t len);
1022eda14cbcSMatt Macy 
102315f0b8c3SMartin Matuska extern size_t ASMABI aesni_gcm_encrypt(const uint8_t *, uint8_t *, size_t,
1024eda14cbcSMatt Macy     const void *, uint64_t *, uint64_t *);
1025eda14cbcSMatt Macy 
102615f0b8c3SMartin Matuska extern size_t ASMABI aesni_gcm_decrypt(const uint8_t *, uint8_t *, size_t,
1027eda14cbcSMatt Macy     const void *, uint64_t *, uint64_t *);
1028eda14cbcSMatt Macy 
1029eda14cbcSMatt Macy static inline boolean_t
1030eda14cbcSMatt Macy gcm_avx_will_work(void)
1031eda14cbcSMatt Macy {
1032eda14cbcSMatt Macy 	/* Avx should imply aes-ni and pclmulqdq, but make sure anyhow. */
1033eda14cbcSMatt Macy 	return (kfpu_allowed() &&
1034eda14cbcSMatt Macy 	    zfs_avx_available() && zfs_aes_available() &&
1035eda14cbcSMatt Macy 	    zfs_pclmulqdq_available());
1036eda14cbcSMatt Macy }
1037eda14cbcSMatt Macy 
1038eda14cbcSMatt Macy static inline void
1039eda14cbcSMatt Macy gcm_set_avx(boolean_t val)
1040eda14cbcSMatt Macy {
1041eda14cbcSMatt Macy 	if (gcm_avx_will_work() == B_TRUE) {
1042eda14cbcSMatt Macy 		atomic_swap_32(&gcm_use_avx, val);
1043eda14cbcSMatt Macy 	}
1044eda14cbcSMatt Macy }
1045eda14cbcSMatt Macy 
1046eda14cbcSMatt Macy static inline boolean_t
1047eda14cbcSMatt Macy gcm_toggle_avx(void)
1048eda14cbcSMatt Macy {
1049eda14cbcSMatt Macy 	if (gcm_avx_will_work() == B_TRUE) {
1050eda14cbcSMatt Macy 		return (atomic_toggle_boolean_nv(&GCM_IMPL_USE_AVX));
1051eda14cbcSMatt Macy 	} else {
1052eda14cbcSMatt Macy 		return (B_FALSE);
1053eda14cbcSMatt Macy 	}
1054eda14cbcSMatt Macy }
1055eda14cbcSMatt Macy 
10567877fdebSMatt Macy static inline size_t
10577877fdebSMatt Macy gcm_simd_get_htab_size(boolean_t simd_mode)
10587877fdebSMatt Macy {
10597877fdebSMatt Macy 	switch (simd_mode) {
10607877fdebSMatt Macy 	case B_TRUE:
10617877fdebSMatt Macy 		return (2 * 6 * 2 * sizeof (uint64_t));
10627877fdebSMatt Macy 
10637877fdebSMatt Macy 	default:
10647877fdebSMatt Macy 		return (0);
10657877fdebSMatt Macy 	}
10667877fdebSMatt Macy }
10677877fdebSMatt Macy 
1068eda14cbcSMatt Macy 
1069eda14cbcSMatt Macy /* Increment the GCM counter block by n. */
1070eda14cbcSMatt Macy static inline void
1071eda14cbcSMatt Macy gcm_incr_counter_block_by(gcm_ctx_t *ctx, int n)
1072eda14cbcSMatt Macy {
1073eda14cbcSMatt Macy 	uint64_t counter_mask = ntohll(0x00000000ffffffffULL);
1074eda14cbcSMatt Macy 	uint64_t counter = ntohll(ctx->gcm_cb[1] & counter_mask);
1075eda14cbcSMatt Macy 
1076eda14cbcSMatt Macy 	counter = htonll(counter + n);
1077eda14cbcSMatt Macy 	counter &= counter_mask;
1078eda14cbcSMatt Macy 	ctx->gcm_cb[1] = (ctx->gcm_cb[1] & ~counter_mask) | counter;
1079eda14cbcSMatt Macy }
1080eda14cbcSMatt Macy 
1081eda14cbcSMatt Macy /*
1082eda14cbcSMatt Macy  * Encrypt multiple blocks of data in GCM mode.
1083eda14cbcSMatt Macy  * This is done in gcm_avx_chunk_size chunks, utilizing AVX assembler routines
1084eda14cbcSMatt Macy  * if possible. While processing a chunk the FPU is "locked".
1085eda14cbcSMatt Macy  */
1086eda14cbcSMatt Macy static int
1087eda14cbcSMatt Macy gcm_mode_encrypt_contiguous_blocks_avx(gcm_ctx_t *ctx, char *data,
1088eda14cbcSMatt Macy     size_t length, crypto_data_t *out, size_t block_size)
1089eda14cbcSMatt Macy {
1090eda14cbcSMatt Macy 	size_t bleft = length;
1091eda14cbcSMatt Macy 	size_t need = 0;
1092eda14cbcSMatt Macy 	size_t done = 0;
1093eda14cbcSMatt Macy 	uint8_t *datap = (uint8_t *)data;
1094eda14cbcSMatt Macy 	size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ;
1095eda14cbcSMatt Macy 	const aes_key_t *key = ((aes_key_t *)ctx->gcm_keysched);
1096eda14cbcSMatt Macy 	uint64_t *ghash = ctx->gcm_ghash;
1097eda14cbcSMatt Macy 	uint64_t *cb = ctx->gcm_cb;
1098eda14cbcSMatt Macy 	uint8_t *ct_buf = NULL;
1099eda14cbcSMatt Macy 	uint8_t *tmp = (uint8_t *)ctx->gcm_tmp;
1100eda14cbcSMatt Macy 	int rv = CRYPTO_SUCCESS;
1101eda14cbcSMatt Macy 
1102eda14cbcSMatt Macy 	ASSERT(block_size == GCM_BLOCK_LEN);
11032a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
11042a58b312SMartin Matuska 	    B_FALSE);
1105eda14cbcSMatt Macy 	/*
1106eda14cbcSMatt Macy 	 * If the last call left an incomplete block, try to fill
1107eda14cbcSMatt Macy 	 * it first.
1108eda14cbcSMatt Macy 	 */
1109eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
1110eda14cbcSMatt Macy 		need = block_size - ctx->gcm_remainder_len;
1111eda14cbcSMatt Macy 		if (length < need) {
1112eda14cbcSMatt Macy 			/* Accumulate bytes here and return. */
1113da5137abSMartin Matuska 			memcpy((uint8_t *)ctx->gcm_remainder +
1114da5137abSMartin Matuska 			    ctx->gcm_remainder_len, datap, length);
1115eda14cbcSMatt Macy 
1116eda14cbcSMatt Macy 			ctx->gcm_remainder_len += length;
1117eda14cbcSMatt Macy 			if (ctx->gcm_copy_to == NULL) {
1118eda14cbcSMatt Macy 				ctx->gcm_copy_to = datap;
1119eda14cbcSMatt Macy 			}
1120eda14cbcSMatt Macy 			return (CRYPTO_SUCCESS);
1121eda14cbcSMatt Macy 		} else {
1122eda14cbcSMatt Macy 			/* Complete incomplete block. */
1123da5137abSMartin Matuska 			memcpy((uint8_t *)ctx->gcm_remainder +
1124da5137abSMartin Matuska 			    ctx->gcm_remainder_len, datap, need);
1125eda14cbcSMatt Macy 
1126eda14cbcSMatt Macy 			ctx->gcm_copy_to = NULL;
1127eda14cbcSMatt Macy 		}
1128eda14cbcSMatt Macy 	}
1129eda14cbcSMatt Macy 
1130eda14cbcSMatt Macy 	/* Allocate a buffer to encrypt to if there is enough input. */
1131eda14cbcSMatt Macy 	if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) {
1132c03c5b1cSMartin Matuska 		ct_buf = vmem_alloc(chunk_size, KM_SLEEP);
1133eda14cbcSMatt Macy 		if (ct_buf == NULL) {
1134eda14cbcSMatt Macy 			return (CRYPTO_HOST_MEMORY);
1135eda14cbcSMatt Macy 		}
1136eda14cbcSMatt Macy 	}
1137eda14cbcSMatt Macy 
1138eda14cbcSMatt Macy 	/* If we completed an incomplete block, encrypt and write it out. */
1139eda14cbcSMatt Macy 	if (ctx->gcm_remainder_len > 0) {
1140eda14cbcSMatt Macy 		kfpu_begin();
1141eda14cbcSMatt Macy 		aes_encrypt_intel(key->encr_ks.ks32, key->nr,
1142eda14cbcSMatt Macy 		    (const uint32_t *)cb, (uint32_t *)tmp);
1143eda14cbcSMatt Macy 
1144eda14cbcSMatt Macy 		gcm_xor_avx((const uint8_t *) ctx->gcm_remainder, tmp);
1145eda14cbcSMatt Macy 		GHASH_AVX(ctx, tmp, block_size);
1146eda14cbcSMatt Macy 		clear_fpu_regs();
1147eda14cbcSMatt Macy 		kfpu_end();
1148eda14cbcSMatt Macy 		rv = crypto_put_output_data(tmp, out, block_size);
1149eda14cbcSMatt Macy 		out->cd_offset += block_size;
1150eda14cbcSMatt Macy 		gcm_incr_counter_block(ctx);
1151eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += block_size;
1152eda14cbcSMatt Macy 		bleft -= need;
1153eda14cbcSMatt Macy 		datap += need;
1154eda14cbcSMatt Macy 		ctx->gcm_remainder_len = 0;
1155eda14cbcSMatt Macy 	}
1156eda14cbcSMatt Macy 
1157eda14cbcSMatt Macy 	/* Do the bulk encryption in chunk_size blocks. */
1158eda14cbcSMatt Macy 	for (; bleft >= chunk_size; bleft -= chunk_size) {
1159eda14cbcSMatt Macy 		kfpu_begin();
1160eda14cbcSMatt Macy 		done = aesni_gcm_encrypt(
1161eda14cbcSMatt Macy 		    datap, ct_buf, chunk_size, key, cb, ghash);
1162eda14cbcSMatt Macy 
1163eda14cbcSMatt Macy 		clear_fpu_regs();
1164eda14cbcSMatt Macy 		kfpu_end();
1165eda14cbcSMatt Macy 		if (done != chunk_size) {
1166eda14cbcSMatt Macy 			rv = CRYPTO_FAILED;
1167eda14cbcSMatt Macy 			goto out_nofpu;
1168eda14cbcSMatt Macy 		}
1169eda14cbcSMatt Macy 		rv = crypto_put_output_data(ct_buf, out, chunk_size);
1170eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS) {
1171eda14cbcSMatt Macy 			goto out_nofpu;
1172eda14cbcSMatt Macy 		}
1173eda14cbcSMatt Macy 		out->cd_offset += chunk_size;
1174eda14cbcSMatt Macy 		datap += chunk_size;
1175eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += chunk_size;
1176eda14cbcSMatt Macy 	}
1177eda14cbcSMatt Macy 	/* Check if we are already done. */
1178eda14cbcSMatt Macy 	if (bleft == 0) {
1179eda14cbcSMatt Macy 		goto out_nofpu;
1180eda14cbcSMatt Macy 	}
1181eda14cbcSMatt Macy 	/* Bulk encrypt the remaining data. */
1182eda14cbcSMatt Macy 	kfpu_begin();
1183eda14cbcSMatt Macy 	if (bleft >= GCM_AVX_MIN_ENCRYPT_BYTES) {
1184eda14cbcSMatt Macy 		done = aesni_gcm_encrypt(datap, ct_buf, bleft, key, cb, ghash);
1185eda14cbcSMatt Macy 		if (done == 0) {
1186eda14cbcSMatt Macy 			rv = CRYPTO_FAILED;
1187eda14cbcSMatt Macy 			goto out;
1188eda14cbcSMatt Macy 		}
1189eda14cbcSMatt Macy 		rv = crypto_put_output_data(ct_buf, out, done);
1190eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS) {
1191eda14cbcSMatt Macy 			goto out;
1192eda14cbcSMatt Macy 		}
1193eda14cbcSMatt Macy 		out->cd_offset += done;
1194eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += done;
1195eda14cbcSMatt Macy 		datap += done;
1196eda14cbcSMatt Macy 		bleft -= done;
1197eda14cbcSMatt Macy 
1198eda14cbcSMatt Macy 	}
1199eda14cbcSMatt Macy 	/* Less than GCM_AVX_MIN_ENCRYPT_BYTES remain, operate on blocks. */
1200eda14cbcSMatt Macy 	while (bleft > 0) {
1201eda14cbcSMatt Macy 		if (bleft < block_size) {
1202da5137abSMartin Matuska 			memcpy(ctx->gcm_remainder, datap, bleft);
1203eda14cbcSMatt Macy 			ctx->gcm_remainder_len = bleft;
1204eda14cbcSMatt Macy 			ctx->gcm_copy_to = datap;
1205eda14cbcSMatt Macy 			goto out;
1206eda14cbcSMatt Macy 		}
1207eda14cbcSMatt Macy 		/* Encrypt, hash and write out. */
1208eda14cbcSMatt Macy 		aes_encrypt_intel(key->encr_ks.ks32, key->nr,
1209eda14cbcSMatt Macy 		    (const uint32_t *)cb, (uint32_t *)tmp);
1210eda14cbcSMatt Macy 
1211eda14cbcSMatt Macy 		gcm_xor_avx(datap, tmp);
1212eda14cbcSMatt Macy 		GHASH_AVX(ctx, tmp, block_size);
1213eda14cbcSMatt Macy 		rv = crypto_put_output_data(tmp, out, block_size);
1214eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS) {
1215eda14cbcSMatt Macy 			goto out;
1216eda14cbcSMatt Macy 		}
1217eda14cbcSMatt Macy 		out->cd_offset += block_size;
1218eda14cbcSMatt Macy 		gcm_incr_counter_block(ctx);
1219eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += block_size;
1220eda14cbcSMatt Macy 		datap += block_size;
1221eda14cbcSMatt Macy 		bleft -= block_size;
1222eda14cbcSMatt Macy 	}
1223eda14cbcSMatt Macy out:
1224eda14cbcSMatt Macy 	clear_fpu_regs();
1225eda14cbcSMatt Macy 	kfpu_end();
1226eda14cbcSMatt Macy out_nofpu:
1227eda14cbcSMatt Macy 	if (ct_buf != NULL) {
1228eda14cbcSMatt Macy 		vmem_free(ct_buf, chunk_size);
1229eda14cbcSMatt Macy 	}
1230eda14cbcSMatt Macy 	return (rv);
1231eda14cbcSMatt Macy }
1232eda14cbcSMatt Macy 
1233eda14cbcSMatt Macy /*
1234eda14cbcSMatt Macy  * Finalize the encryption: Zero fill, encrypt, hash and write out an eventual
1235eda14cbcSMatt Macy  * incomplete last block. Encrypt the ICB. Calculate the tag and write it out.
1236eda14cbcSMatt Macy  */
1237eda14cbcSMatt Macy static int
1238eda14cbcSMatt Macy gcm_encrypt_final_avx(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size)
1239eda14cbcSMatt Macy {
1240eda14cbcSMatt Macy 	uint8_t *ghash = (uint8_t *)ctx->gcm_ghash;
1241eda14cbcSMatt Macy 	uint32_t *J0 = (uint32_t *)ctx->gcm_J0;
1242eda14cbcSMatt Macy 	uint8_t *remainder = (uint8_t *)ctx->gcm_remainder;
1243eda14cbcSMatt Macy 	size_t rem_len = ctx->gcm_remainder_len;
1244eda14cbcSMatt Macy 	const void *keysched = ((aes_key_t *)ctx->gcm_keysched)->encr_ks.ks32;
1245eda14cbcSMatt Macy 	int aes_rounds = ((aes_key_t *)keysched)->nr;
1246eda14cbcSMatt Macy 	int rv;
1247eda14cbcSMatt Macy 
1248eda14cbcSMatt Macy 	ASSERT(block_size == GCM_BLOCK_LEN);
12492a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
12502a58b312SMartin Matuska 	    B_FALSE);
1251eda14cbcSMatt Macy 
1252eda14cbcSMatt Macy 	if (out->cd_length < (rem_len + ctx->gcm_tag_len)) {
1253eda14cbcSMatt Macy 		return (CRYPTO_DATA_LEN_RANGE);
1254eda14cbcSMatt Macy 	}
1255eda14cbcSMatt Macy 
1256eda14cbcSMatt Macy 	kfpu_begin();
1257eda14cbcSMatt Macy 	/* Pad last incomplete block with zeros, encrypt and hash. */
1258eda14cbcSMatt Macy 	if (rem_len > 0) {
1259eda14cbcSMatt Macy 		uint8_t *tmp = (uint8_t *)ctx->gcm_tmp;
1260eda14cbcSMatt Macy 		const uint32_t *cb = (uint32_t *)ctx->gcm_cb;
1261eda14cbcSMatt Macy 
1262eda14cbcSMatt Macy 		aes_encrypt_intel(keysched, aes_rounds, cb, (uint32_t *)tmp);
1263da5137abSMartin Matuska 		memset(remainder + rem_len, 0, block_size - rem_len);
1264eda14cbcSMatt Macy 		for (int i = 0; i < rem_len; i++) {
1265eda14cbcSMatt Macy 			remainder[i] ^= tmp[i];
1266eda14cbcSMatt Macy 		}
1267eda14cbcSMatt Macy 		GHASH_AVX(ctx, remainder, block_size);
1268eda14cbcSMatt Macy 		ctx->gcm_processed_data_len += rem_len;
1269eda14cbcSMatt Macy 		/* No need to increment counter_block, it's the last block. */
1270eda14cbcSMatt Macy 	}
1271eda14cbcSMatt Macy 	/* Finish tag. */
1272eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] =
1273eda14cbcSMatt Macy 	    htonll(CRYPTO_BYTES2BITS(ctx->gcm_processed_data_len));
1274eda14cbcSMatt Macy 	GHASH_AVX(ctx, (const uint8_t *)ctx->gcm_len_a_len_c, block_size);
1275eda14cbcSMatt Macy 	aes_encrypt_intel(keysched, aes_rounds, J0, J0);
1276eda14cbcSMatt Macy 
1277eda14cbcSMatt Macy 	gcm_xor_avx((uint8_t *)J0, ghash);
1278eda14cbcSMatt Macy 	clear_fpu_regs();
1279eda14cbcSMatt Macy 	kfpu_end();
1280eda14cbcSMatt Macy 
1281eda14cbcSMatt Macy 	/* Output remainder. */
1282eda14cbcSMatt Macy 	if (rem_len > 0) {
1283eda14cbcSMatt Macy 		rv = crypto_put_output_data(remainder, out, rem_len);
1284eda14cbcSMatt Macy 		if (rv != CRYPTO_SUCCESS)
1285eda14cbcSMatt Macy 			return (rv);
1286eda14cbcSMatt Macy 	}
1287eda14cbcSMatt Macy 	out->cd_offset += rem_len;
1288eda14cbcSMatt Macy 	ctx->gcm_remainder_len = 0;
1289eda14cbcSMatt Macy 	rv = crypto_put_output_data(ghash, out, ctx->gcm_tag_len);
1290eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS)
1291eda14cbcSMatt Macy 		return (rv);
1292eda14cbcSMatt Macy 
1293eda14cbcSMatt Macy 	out->cd_offset += ctx->gcm_tag_len;
1294eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
1295eda14cbcSMatt Macy }
1296eda14cbcSMatt Macy 
1297eda14cbcSMatt Macy /*
1298eda14cbcSMatt Macy  * Finalize decryption: We just have accumulated crypto text, so now we
1299eda14cbcSMatt Macy  * decrypt it here inplace.
1300eda14cbcSMatt Macy  */
1301eda14cbcSMatt Macy static int
1302eda14cbcSMatt Macy gcm_decrypt_final_avx(gcm_ctx_t *ctx, crypto_data_t *out, size_t block_size)
1303eda14cbcSMatt Macy {
1304eda14cbcSMatt Macy 	ASSERT3U(ctx->gcm_processed_data_len, ==, ctx->gcm_pt_buf_len);
1305eda14cbcSMatt Macy 	ASSERT3U(block_size, ==, 16);
13062a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
13072a58b312SMartin Matuska 	    B_FALSE);
1308eda14cbcSMatt Macy 
1309eda14cbcSMatt Macy 	size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ;
1310eda14cbcSMatt Macy 	size_t pt_len = ctx->gcm_processed_data_len - ctx->gcm_tag_len;
1311eda14cbcSMatt Macy 	uint8_t *datap = ctx->gcm_pt_buf;
1312eda14cbcSMatt Macy 	const aes_key_t *key = ((aes_key_t *)ctx->gcm_keysched);
1313eda14cbcSMatt Macy 	uint32_t *cb = (uint32_t *)ctx->gcm_cb;
1314eda14cbcSMatt Macy 	uint64_t *ghash = ctx->gcm_ghash;
1315eda14cbcSMatt Macy 	uint32_t *tmp = (uint32_t *)ctx->gcm_tmp;
1316eda14cbcSMatt Macy 	int rv = CRYPTO_SUCCESS;
1317eda14cbcSMatt Macy 	size_t bleft, done;
1318eda14cbcSMatt Macy 
1319eda14cbcSMatt Macy 	/*
1320eda14cbcSMatt Macy 	 * Decrypt in chunks of gcm_avx_chunk_size, which is asserted to be
1321eda14cbcSMatt Macy 	 * greater or equal than GCM_AVX_MIN_ENCRYPT_BYTES, and a multiple of
1322eda14cbcSMatt Macy 	 * GCM_AVX_MIN_DECRYPT_BYTES.
1323eda14cbcSMatt Macy 	 */
1324eda14cbcSMatt Macy 	for (bleft = pt_len; bleft >= chunk_size; bleft -= chunk_size) {
1325eda14cbcSMatt Macy 		kfpu_begin();
1326eda14cbcSMatt Macy 		done = aesni_gcm_decrypt(datap, datap, chunk_size,
1327eda14cbcSMatt Macy 		    (const void *)key, ctx->gcm_cb, ghash);
1328eda14cbcSMatt Macy 		clear_fpu_regs();
1329eda14cbcSMatt Macy 		kfpu_end();
1330eda14cbcSMatt Macy 		if (done != chunk_size) {
1331eda14cbcSMatt Macy 			return (CRYPTO_FAILED);
1332eda14cbcSMatt Macy 		}
1333eda14cbcSMatt Macy 		datap += done;
1334eda14cbcSMatt Macy 	}
133516038816SMartin Matuska 	/* Decrypt remainder, which is less than chunk size, in one go. */
1336eda14cbcSMatt Macy 	kfpu_begin();
1337eda14cbcSMatt Macy 	if (bleft >= GCM_AVX_MIN_DECRYPT_BYTES) {
1338eda14cbcSMatt Macy 		done = aesni_gcm_decrypt(datap, datap, bleft,
1339eda14cbcSMatt Macy 		    (const void *)key, ctx->gcm_cb, ghash);
1340eda14cbcSMatt Macy 		if (done == 0) {
1341eda14cbcSMatt Macy 			clear_fpu_regs();
1342eda14cbcSMatt Macy 			kfpu_end();
1343eda14cbcSMatt Macy 			return (CRYPTO_FAILED);
1344eda14cbcSMatt Macy 		}
1345eda14cbcSMatt Macy 		datap += done;
1346eda14cbcSMatt Macy 		bleft -= done;
1347eda14cbcSMatt Macy 	}
1348eda14cbcSMatt Macy 	ASSERT(bleft < GCM_AVX_MIN_DECRYPT_BYTES);
1349eda14cbcSMatt Macy 
1350eda14cbcSMatt Macy 	/*
135116038816SMartin Matuska 	 * Now less than GCM_AVX_MIN_DECRYPT_BYTES bytes remain,
1352eda14cbcSMatt Macy 	 * decrypt them block by block.
1353eda14cbcSMatt Macy 	 */
1354eda14cbcSMatt Macy 	while (bleft > 0) {
1355eda14cbcSMatt Macy 		/* Incomplete last block. */
1356eda14cbcSMatt Macy 		if (bleft < block_size) {
1357eda14cbcSMatt Macy 			uint8_t *lastb = (uint8_t *)ctx->gcm_remainder;
1358eda14cbcSMatt Macy 
1359da5137abSMartin Matuska 			memset(lastb, 0, block_size);
1360da5137abSMartin Matuska 			memcpy(lastb, datap, bleft);
1361eda14cbcSMatt Macy 			/* The GCM processing. */
1362eda14cbcSMatt Macy 			GHASH_AVX(ctx, lastb, block_size);
1363eda14cbcSMatt Macy 			aes_encrypt_intel(key->encr_ks.ks32, key->nr, cb, tmp);
1364eda14cbcSMatt Macy 			for (size_t i = 0; i < bleft; i++) {
1365eda14cbcSMatt Macy 				datap[i] = lastb[i] ^ ((uint8_t *)tmp)[i];
1366eda14cbcSMatt Macy 			}
1367eda14cbcSMatt Macy 			break;
1368eda14cbcSMatt Macy 		}
1369eda14cbcSMatt Macy 		/* The GCM processing. */
1370eda14cbcSMatt Macy 		GHASH_AVX(ctx, datap, block_size);
1371eda14cbcSMatt Macy 		aes_encrypt_intel(key->encr_ks.ks32, key->nr, cb, tmp);
1372eda14cbcSMatt Macy 		gcm_xor_avx((uint8_t *)tmp, datap);
1373eda14cbcSMatt Macy 		gcm_incr_counter_block(ctx);
1374eda14cbcSMatt Macy 
1375eda14cbcSMatt Macy 		datap += block_size;
1376eda14cbcSMatt Macy 		bleft -= block_size;
1377eda14cbcSMatt Macy 	}
1378eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS) {
1379eda14cbcSMatt Macy 		clear_fpu_regs();
1380eda14cbcSMatt Macy 		kfpu_end();
1381eda14cbcSMatt Macy 		return (rv);
1382eda14cbcSMatt Macy 	}
1383eda14cbcSMatt Macy 	/* Decryption done, finish the tag. */
1384eda14cbcSMatt Macy 	ctx->gcm_len_a_len_c[1] = htonll(CRYPTO_BYTES2BITS(pt_len));
1385eda14cbcSMatt Macy 	GHASH_AVX(ctx, (uint8_t *)ctx->gcm_len_a_len_c, block_size);
1386eda14cbcSMatt Macy 	aes_encrypt_intel(key->encr_ks.ks32, key->nr, (uint32_t *)ctx->gcm_J0,
1387eda14cbcSMatt Macy 	    (uint32_t *)ctx->gcm_J0);
1388eda14cbcSMatt Macy 
1389eda14cbcSMatt Macy 	gcm_xor_avx((uint8_t *)ctx->gcm_J0, (uint8_t *)ghash);
1390eda14cbcSMatt Macy 
1391eda14cbcSMatt Macy 	/* We are done with the FPU, restore its state. */
1392eda14cbcSMatt Macy 	clear_fpu_regs();
1393eda14cbcSMatt Macy 	kfpu_end();
1394eda14cbcSMatt Macy 
1395eda14cbcSMatt Macy 	/* Compare the input authentication tag with what we calculated. */
1396da5137abSMartin Matuska 	if (memcmp(&ctx->gcm_pt_buf[pt_len], ghash, ctx->gcm_tag_len)) {
1397eda14cbcSMatt Macy 		/* They don't match. */
1398eda14cbcSMatt Macy 		return (CRYPTO_INVALID_MAC);
1399eda14cbcSMatt Macy 	}
1400eda14cbcSMatt Macy 	rv = crypto_put_output_data(ctx->gcm_pt_buf, out, pt_len);
1401eda14cbcSMatt Macy 	if (rv != CRYPTO_SUCCESS) {
1402eda14cbcSMatt Macy 		return (rv);
1403eda14cbcSMatt Macy 	}
1404eda14cbcSMatt Macy 	out->cd_offset += pt_len;
1405eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
1406eda14cbcSMatt Macy }
1407eda14cbcSMatt Macy 
1408eda14cbcSMatt Macy /*
1409eda14cbcSMatt Macy  * Initialize the GCM params H, Htabtle and the counter block. Save the
1410eda14cbcSMatt Macy  * initial counter block.
1411eda14cbcSMatt Macy  */
1412eda14cbcSMatt Macy static int
14132a58b312SMartin Matuska gcm_init_avx(gcm_ctx_t *ctx, const uint8_t *iv, size_t iv_len,
14142a58b312SMartin Matuska     const uint8_t *auth_data, size_t auth_data_len, size_t block_size)
1415eda14cbcSMatt Macy {
1416eda14cbcSMatt Macy 	uint8_t *cb = (uint8_t *)ctx->gcm_cb;
1417eda14cbcSMatt Macy 	uint64_t *H = ctx->gcm_H;
1418eda14cbcSMatt Macy 	const void *keysched = ((aes_key_t *)ctx->gcm_keysched)->encr_ks.ks32;
1419eda14cbcSMatt Macy 	int aes_rounds = ((aes_key_t *)ctx->gcm_keysched)->nr;
14202a58b312SMartin Matuska 	const uint8_t *datap = auth_data;
1421eda14cbcSMatt Macy 	size_t chunk_size = (size_t)GCM_CHUNK_SIZE_READ;
1422eda14cbcSMatt Macy 	size_t bleft;
1423eda14cbcSMatt Macy 
1424eda14cbcSMatt Macy 	ASSERT(block_size == GCM_BLOCK_LEN);
14252a58b312SMartin Matuska 	ASSERT3S(((aes_key_t *)ctx->gcm_keysched)->ops->needs_byteswap, ==,
14262a58b312SMartin Matuska 	    B_FALSE);
1427eda14cbcSMatt Macy 
1428eda14cbcSMatt Macy 	/* Init H (encrypt zero block) and create the initial counter block. */
1429da5137abSMartin Matuska 	memset(ctx->gcm_ghash, 0, sizeof (ctx->gcm_ghash));
1430da5137abSMartin Matuska 	memset(H, 0, sizeof (ctx->gcm_H));
1431eda14cbcSMatt Macy 	kfpu_begin();
1432eda14cbcSMatt Macy 	aes_encrypt_intel(keysched, aes_rounds,
1433eda14cbcSMatt Macy 	    (const uint32_t *)H, (uint32_t *)H);
1434eda14cbcSMatt Macy 
1435eda14cbcSMatt Macy 	gcm_init_htab_avx(ctx->gcm_Htable, H);
1436eda14cbcSMatt Macy 
1437eda14cbcSMatt Macy 	if (iv_len == 12) {
1438da5137abSMartin Matuska 		memcpy(cb, iv, 12);
1439eda14cbcSMatt Macy 		cb[12] = 0;
1440eda14cbcSMatt Macy 		cb[13] = 0;
1441eda14cbcSMatt Macy 		cb[14] = 0;
1442eda14cbcSMatt Macy 		cb[15] = 1;
1443eda14cbcSMatt Macy 		/* We need the ICB later. */
1444da5137abSMartin Matuska 		memcpy(ctx->gcm_J0, cb, sizeof (ctx->gcm_J0));
1445eda14cbcSMatt Macy 	} else {
1446eda14cbcSMatt Macy 		/*
1447eda14cbcSMatt Macy 		 * Most consumers use 12 byte IVs, so it's OK to use the
1448eda14cbcSMatt Macy 		 * original routines for other IV sizes, just avoid nesting
1449eda14cbcSMatt Macy 		 * kfpu_begin calls.
1450eda14cbcSMatt Macy 		 */
1451eda14cbcSMatt Macy 		clear_fpu_regs();
1452eda14cbcSMatt Macy 		kfpu_end();
1453eda14cbcSMatt Macy 		gcm_format_initial_blocks(iv, iv_len, ctx, block_size,
1454eda14cbcSMatt Macy 		    aes_copy_block, aes_xor_block);
1455eda14cbcSMatt Macy 		kfpu_begin();
1456eda14cbcSMatt Macy 	}
1457eda14cbcSMatt Macy 
1458eda14cbcSMatt Macy 	/* Openssl post increments the counter, adjust for that. */
1459eda14cbcSMatt Macy 	gcm_incr_counter_block(ctx);
1460eda14cbcSMatt Macy 
1461eda14cbcSMatt Macy 	/* Ghash AAD in chunk_size blocks. */
1462eda14cbcSMatt Macy 	for (bleft = auth_data_len; bleft >= chunk_size; bleft -= chunk_size) {
1463eda14cbcSMatt Macy 		GHASH_AVX(ctx, datap, chunk_size);
1464eda14cbcSMatt Macy 		datap += chunk_size;
1465eda14cbcSMatt Macy 		clear_fpu_regs();
1466eda14cbcSMatt Macy 		kfpu_end();
1467eda14cbcSMatt Macy 		kfpu_begin();
1468eda14cbcSMatt Macy 	}
1469eda14cbcSMatt Macy 	/* Ghash the remainder and handle possible incomplete GCM block. */
1470eda14cbcSMatt Macy 	if (bleft > 0) {
1471eda14cbcSMatt Macy 		size_t incomp = bleft % block_size;
1472eda14cbcSMatt Macy 
1473eda14cbcSMatt Macy 		bleft -= incomp;
1474eda14cbcSMatt Macy 		if (bleft > 0) {
1475eda14cbcSMatt Macy 			GHASH_AVX(ctx, datap, bleft);
1476eda14cbcSMatt Macy 			datap += bleft;
1477eda14cbcSMatt Macy 		}
1478eda14cbcSMatt Macy 		if (incomp > 0) {
1479eda14cbcSMatt Macy 			/* Zero pad and hash incomplete last block. */
1480eda14cbcSMatt Macy 			uint8_t *authp = (uint8_t *)ctx->gcm_tmp;
1481eda14cbcSMatt Macy 
1482da5137abSMartin Matuska 			memset(authp, 0, block_size);
1483da5137abSMartin Matuska 			memcpy(authp, datap, incomp);
1484eda14cbcSMatt Macy 			GHASH_AVX(ctx, authp, block_size);
1485eda14cbcSMatt Macy 		}
1486eda14cbcSMatt Macy 	}
1487eda14cbcSMatt Macy 	clear_fpu_regs();
1488eda14cbcSMatt Macy 	kfpu_end();
1489eda14cbcSMatt Macy 	return (CRYPTO_SUCCESS);
1490eda14cbcSMatt Macy }
1491eda14cbcSMatt Macy 
1492eda14cbcSMatt Macy #if defined(_KERNEL)
1493eda14cbcSMatt Macy static int
1494eda14cbcSMatt Macy icp_gcm_avx_set_chunk_size(const char *buf, zfs_kernel_param_t *kp)
1495eda14cbcSMatt Macy {
1496eda14cbcSMatt Macy 	unsigned long val;
1497eda14cbcSMatt Macy 	char val_rounded[16];
1498eda14cbcSMatt Macy 	int error = 0;
1499eda14cbcSMatt Macy 
1500eda14cbcSMatt Macy 	error = kstrtoul(buf, 0, &val);
1501eda14cbcSMatt Macy 	if (error)
1502eda14cbcSMatt Macy 		return (error);
1503eda14cbcSMatt Macy 
1504eda14cbcSMatt Macy 	val = (val / GCM_AVX_MIN_DECRYPT_BYTES) * GCM_AVX_MIN_DECRYPT_BYTES;
1505eda14cbcSMatt Macy 
1506eda14cbcSMatt Macy 	if (val < GCM_AVX_MIN_ENCRYPT_BYTES || val > GCM_AVX_MAX_CHUNK_SIZE)
1507eda14cbcSMatt Macy 		return (-EINVAL);
1508eda14cbcSMatt Macy 
1509eda14cbcSMatt Macy 	snprintf(val_rounded, 16, "%u", (uint32_t)val);
1510eda14cbcSMatt Macy 	error = param_set_uint(val_rounded, kp);
1511eda14cbcSMatt Macy 	return (error);
1512eda14cbcSMatt Macy }
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy module_param_call(icp_gcm_avx_chunk_size, icp_gcm_avx_set_chunk_size,
1515eda14cbcSMatt Macy     param_get_uint, &gcm_avx_chunk_size, 0644);
1516eda14cbcSMatt Macy 
1517eda14cbcSMatt Macy MODULE_PARM_DESC(icp_gcm_avx_chunk_size,
1518eda14cbcSMatt Macy 	"How many bytes to process while owning the FPU");
1519eda14cbcSMatt Macy 
1520eda14cbcSMatt Macy #endif /* defined(__KERNEL) */
1521eda14cbcSMatt Macy #endif /* ifdef CAN_USE_GCM_ASM */
1522