1/*	$NetBSD: aes_ssse3_impl.c,v 1.4 2020/07/25 22:31:04 riastradh Exp $	*/
2
3/*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(1, "$NetBSD: aes_ssse3_impl.c,v 1.4 2020/07/25 22:31:04 riastradh Exp $");
31
32#include <crypto/aes/aes.h>
33#include <crypto/aes/aes_impl.h>
34#include <crypto/aes/arch/x86/aes_ssse3.h>
35
36#ifdef _KERNEL
37#include <x86/cpu.h>
38#include <x86/cpuvar.h>
39#include <x86/fpu.h>
40#include <x86/specialreg.h>
41#else
42#include <cpuid.h>
43#define	fpu_kern_enter()	((void)0)
44#define	fpu_kern_leave()	((void)0)
45#endif
46
47static void
48aes_ssse3_setenckey_impl(struct aesenc *enc, const uint8_t *key,
49    uint32_t nrounds)
50{
51
52	fpu_kern_enter();
53	aes_ssse3_setenckey(enc, key, nrounds);
54	fpu_kern_leave();
55}
56
57static void
58aes_ssse3_setdeckey_impl(struct aesdec *dec, const uint8_t *key,
59    uint32_t nrounds)
60{
61
62	fpu_kern_enter();
63	aes_ssse3_setdeckey(dec, key, nrounds);
64	fpu_kern_leave();
65}
66
67static void
68aes_ssse3_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
69    uint8_t out[static 16], uint32_t nrounds)
70{
71
72	fpu_kern_enter();
73	aes_ssse3_enc(enc, in, out, nrounds);
74	fpu_kern_leave();
75}
76
77static void
78aes_ssse3_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
79    uint8_t out[static 16], uint32_t nrounds)
80{
81
82	fpu_kern_enter();
83	aes_ssse3_dec(dec, in, out, nrounds);
84	fpu_kern_leave();
85}
86
87static void
88aes_ssse3_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
89    uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
90    uint32_t nrounds)
91{
92
93	if (nbytes == 0)
94		return;
95	fpu_kern_enter();
96	aes_ssse3_cbc_enc(enc, in, out, nbytes, iv, nrounds);
97	fpu_kern_leave();
98}
99
100static void
101aes_ssse3_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
102    uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
103    uint32_t nrounds)
104{
105
106	if (nbytes == 0)
107		return;
108	fpu_kern_enter();
109	aes_ssse3_cbc_dec(dec, in, out, nbytes, iv, nrounds);
110	fpu_kern_leave();
111}
112
113static void
114aes_ssse3_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
115    uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
116    uint32_t nrounds)
117{
118
119	if (nbytes == 0)
120		return;
121	fpu_kern_enter();
122	aes_ssse3_xts_enc(enc, in, out, nbytes, iv, nrounds);
123	fpu_kern_leave();
124}
125
126static void
127aes_ssse3_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
128    uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
129    uint32_t nrounds)
130{
131
132	if (nbytes == 0)
133		return;
134	fpu_kern_enter();
135	aes_ssse3_xts_dec(dec, in, out, nbytes, iv, nrounds);
136	fpu_kern_leave();
137}
138
139static void
140aes_ssse3_cbcmac_update1_impl(const struct aesenc *enc,
141    const uint8_t in[static 16], size_t nbytes, uint8_t auth[static 16],
142    uint32_t nrounds)
143{
144
145	fpu_kern_enter();
146	aes_ssse3_cbcmac_update1(enc, in, nbytes, auth, nrounds);
147	fpu_kern_leave();
148}
149
150static void
151aes_ssse3_ccm_enc1_impl(const struct aesenc *enc, const uint8_t in[static 16],
152    uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32],
153    uint32_t nrounds)
154{
155
156	fpu_kern_enter();
157	aes_ssse3_ccm_enc1(enc, in, out, nbytes, authctr, nrounds);
158	fpu_kern_leave();
159}
160
161static void
162aes_ssse3_ccm_dec1_impl(const struct aesenc *enc, const uint8_t in[static 16],
163    uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32],
164    uint32_t nrounds)
165{
166
167	fpu_kern_enter();
168	aes_ssse3_ccm_dec1(enc, in, out, nbytes, authctr, nrounds);
169	fpu_kern_leave();
170}
171
172static int
173aes_ssse3_probe(void)
174{
175	int result = 0;
176
177	/* Verify that the CPU supports SSE, SSE2, SSE3, and SSSE3.  */
178#ifdef _KERNEL
179	if (!i386_has_sse)
180		return -1;
181	if (!i386_has_sse2)
182		return -1;
183	if (((cpu_feature[1]) & CPUID2_SSE3) == 0)
184		return -1;
185	if (((cpu_feature[1]) & CPUID2_SSSE3) == 0)
186		return -1;
187#else
188	unsigned eax, ebx, ecx, edx;
189	if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
190		return -1;
191	if ((edx & bit_SSE) == 0)
192		return -1;
193	if ((edx & bit_SSE2) == 0)
194		return -1;
195	if ((ecx & bit_SSE3) == 0)
196		return -1;
197	if ((ecx & bit_SSSE3) == 0)
198		return -1;
199#endif
200
201	fpu_kern_enter();
202	result = aes_ssse3_selftest();
203	fpu_kern_leave();
204
205	return result;
206}
207
208struct aes_impl aes_ssse3_impl = {
209	.ai_name = "Intel SSSE3 vpaes",
210	.ai_probe = aes_ssse3_probe,
211	.ai_setenckey = aes_ssse3_setenckey_impl,
212	.ai_setdeckey = aes_ssse3_setdeckey_impl,
213	.ai_enc = aes_ssse3_enc_impl,
214	.ai_dec = aes_ssse3_dec_impl,
215	.ai_cbc_enc = aes_ssse3_cbc_enc_impl,
216	.ai_cbc_dec = aes_ssse3_cbc_dec_impl,
217	.ai_xts_enc = aes_ssse3_xts_enc_impl,
218	.ai_xts_dec = aes_ssse3_xts_dec_impl,
219	.ai_cbcmac_update1 = aes_ssse3_cbcmac_update1_impl,
220	.ai_ccm_enc1 = aes_ssse3_ccm_enc1_impl,
221	.ai_ccm_dec1 = aes_ssse3_ccm_dec1_impl,
222};
223