aes_ssse3_impl.c revision 1.2 1 /* $NetBSD: aes_ssse3_impl.c,v 1.2 2020/06/30 20:32:11 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.2 2020/06/30 20:32:11 riastradh Exp $");
31
32 #include <crypto/aes/aes.h>
33 #include <crypto/aes/arch/x86/aes_ssse3.h>
34
35 #ifdef _KERNEL
36 #include <x86/cpu.h>
37 #include <x86/cpuvar.h>
38 #include <x86/fpu.h>
39 #include <x86/specialreg.h>
40 #else
41 #include <cpuid.h>
42 #define fpu_kern_enter() ((void)0)
43 #define fpu_kern_leave() ((void)0)
44 #endif
45
46 static void
47 aes_ssse3_setenckey_impl(struct aesenc *enc, const uint8_t *key,
48 uint32_t nrounds)
49 {
50
51 fpu_kern_enter();
52 aes_ssse3_setenckey(enc, key, nrounds);
53 fpu_kern_leave();
54 }
55
56 static void
57 aes_ssse3_setdeckey_impl(struct aesdec *dec, const uint8_t *key,
58 uint32_t nrounds)
59 {
60
61 fpu_kern_enter();
62 aes_ssse3_setdeckey(dec, key, nrounds);
63 fpu_kern_leave();
64 }
65
66 static void
67 aes_ssse3_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
68 uint8_t out[static 16], uint32_t nrounds)
69 {
70
71 fpu_kern_enter();
72 aes_ssse3_enc(enc, in, out, nrounds);
73 fpu_kern_leave();
74 }
75
76 static void
77 aes_ssse3_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
78 uint8_t out[static 16], uint32_t nrounds)
79 {
80
81 fpu_kern_enter();
82 aes_ssse3_dec(dec, in, out, nrounds);
83 fpu_kern_leave();
84 }
85
86 static void
87 aes_ssse3_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
88 uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
89 uint32_t nrounds)
90 {
91
92 if (nbytes == 0)
93 return;
94 fpu_kern_enter();
95 aes_ssse3_cbc_enc(enc, in, out, nbytes, iv, nrounds);
96 fpu_kern_leave();
97 }
98
99 static void
100 aes_ssse3_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
101 uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
102 uint32_t nrounds)
103 {
104
105 if (nbytes == 0)
106 return;
107 fpu_kern_enter();
108 aes_ssse3_cbc_dec(dec, in, out, nbytes, iv, nrounds);
109 fpu_kern_leave();
110 }
111
112 static void
113 aes_ssse3_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
114 uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
115 uint32_t nrounds)
116 {
117
118 if (nbytes == 0)
119 return;
120 fpu_kern_enter();
121 aes_ssse3_xts_enc(enc, in, out, nbytes, iv, nrounds);
122 fpu_kern_leave();
123 }
124
125 static void
126 aes_ssse3_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
127 uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
128 uint32_t nrounds)
129 {
130
131 if (nbytes == 0)
132 return;
133 fpu_kern_enter();
134 aes_ssse3_xts_dec(dec, in, out, nbytes, iv, nrounds);
135 fpu_kern_leave();
136 }
137
138 static int
139 aes_ssse3_probe(void)
140 {
141 int result = 0;
142
143 /* Verify that the CPU supports SSE, SSE2, SSE3, and SSSE3. */
144 #ifdef _KERNEL
145 if (!i386_has_sse)
146 return -1;
147 if (!i386_has_sse2)
148 return -1;
149 if (((cpu_feature[1]) & CPUID2_SSE3) == 0)
150 return -1;
151 if (((cpu_feature[1]) & CPUID2_SSSE3) == 0)
152 return -1;
153 #else
154 unsigned eax, ebx, ecx, edx;
155 if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
156 return -1;
157 if ((edx & bit_SSE) == 0)
158 return -1;
159 if ((edx & bit_SSE2) == 0)
160 return -1;
161 if ((ecx & bit_SSE3) == 0)
162 return -1;
163 if ((ecx & bit_SSSE3) == 0)
164 return -1;
165 #endif
166
167 fpu_kern_enter();
168 result = aes_ssse3_selftest();
169 fpu_kern_leave();
170
171 return result;
172 }
173
174 struct aes_impl aes_ssse3_impl = {
175 .ai_name = "Intel SSSE3 vpaes",
176 .ai_probe = aes_ssse3_probe,
177 .ai_setenckey = aes_ssse3_setenckey_impl,
178 .ai_setdeckey = aes_ssse3_setdeckey_impl,
179 .ai_enc = aes_ssse3_enc_impl,
180 .ai_dec = aes_ssse3_dec_impl,
181 .ai_cbc_enc = aes_ssse3_cbc_enc_impl,
182 .ai_cbc_dec = aes_ssse3_cbc_dec_impl,
183 .ai_xts_enc = aes_ssse3_xts_enc_impl,
184 .ai_xts_dec = aes_ssse3_xts_dec_impl,
185 };
186