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