1 1.5 maxv /* $NetBSD: aes_ni.c,v 1.5 2020/09/05 07:45:44 maxv 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.5 maxv __KERNEL_RCSID(1, "$NetBSD: aes_ni.c,v 1.5 2020/09/05 07:45:44 maxv Exp $"); 31 1.1 riastrad 32 1.2 riastrad #ifdef _KERNEL 33 1.1 riastrad #include <sys/types.h> 34 1.1 riastrad #include <sys/systm.h> 35 1.2 riastrad #else 36 1.2 riastrad #include <assert.h> 37 1.2 riastrad #include <err.h> 38 1.2 riastrad #include <stdint.h> 39 1.2 riastrad #include <string.h> 40 1.2 riastrad #define KASSERT assert 41 1.2 riastrad #define panic(fmt, args...) err(1, fmt, args) 42 1.2 riastrad #endif 43 1.1 riastrad 44 1.1 riastrad #include <crypto/aes/aes.h> 45 1.3 riastrad #include <crypto/aes/aes_impl.h> 46 1.1 riastrad #include <crypto/aes/arch/x86/aes_ni.h> 47 1.1 riastrad 48 1.2 riastrad #ifdef _KERNEL 49 1.1 riastrad #include <x86/cpuvar.h> 50 1.1 riastrad #include <x86/fpu.h> 51 1.1 riastrad #include <x86/specialreg.h> 52 1.2 riastrad #else 53 1.2 riastrad #include <cpuid.h> 54 1.2 riastrad #define fpu_kern_enter() ((void)0) 55 1.2 riastrad #define fpu_kern_leave() ((void)0) 56 1.2 riastrad #endif 57 1.1 riastrad 58 1.1 riastrad static void 59 1.1 riastrad aesni_setenckey(struct aesenc *enc, const uint8_t key[static 16], 60 1.1 riastrad uint32_t nrounds) 61 1.1 riastrad { 62 1.1 riastrad 63 1.1 riastrad switch (nrounds) { 64 1.1 riastrad case 10: 65 1.1 riastrad aesni_setenckey128(enc, key); 66 1.1 riastrad break; 67 1.1 riastrad case 12: 68 1.1 riastrad aesni_setenckey192(enc, key); 69 1.1 riastrad break; 70 1.1 riastrad case 14: 71 1.1 riastrad aesni_setenckey256(enc, key); 72 1.1 riastrad break; 73 1.1 riastrad default: 74 1.1 riastrad panic("invalid AES rounds: %u", nrounds); 75 1.1 riastrad } 76 1.1 riastrad } 77 1.1 riastrad 78 1.1 riastrad static void 79 1.1 riastrad aesni_setenckey_impl(struct aesenc *enc, const uint8_t key[static 16], 80 1.1 riastrad uint32_t nrounds) 81 1.1 riastrad { 82 1.1 riastrad 83 1.1 riastrad fpu_kern_enter(); 84 1.1 riastrad aesni_setenckey(enc, key, nrounds); 85 1.1 riastrad fpu_kern_leave(); 86 1.1 riastrad } 87 1.1 riastrad 88 1.1 riastrad static void 89 1.1 riastrad aesni_setdeckey_impl(struct aesdec *dec, const uint8_t key[static 16], 90 1.1 riastrad uint32_t nrounds) 91 1.1 riastrad { 92 1.1 riastrad struct aesenc enc; 93 1.1 riastrad 94 1.1 riastrad fpu_kern_enter(); 95 1.1 riastrad aesni_setenckey(&enc, key, nrounds); 96 1.1 riastrad aesni_enctodec(&enc, dec, nrounds); 97 1.1 riastrad fpu_kern_leave(); 98 1.1 riastrad 99 1.1 riastrad explicit_memset(&enc, 0, sizeof enc); 100 1.1 riastrad } 101 1.1 riastrad 102 1.1 riastrad static void 103 1.1 riastrad aesni_enc_impl(const struct aesenc *enc, const uint8_t in[static 16], 104 1.1 riastrad uint8_t out[static 16], uint32_t nrounds) 105 1.1 riastrad { 106 1.1 riastrad 107 1.1 riastrad fpu_kern_enter(); 108 1.1 riastrad aesni_enc(enc, in, out, nrounds); 109 1.1 riastrad fpu_kern_leave(); 110 1.1 riastrad } 111 1.1 riastrad 112 1.1 riastrad static void 113 1.1 riastrad aesni_dec_impl(const struct aesdec *dec, const uint8_t in[static 16], 114 1.1 riastrad uint8_t out[static 16], uint32_t nrounds) 115 1.1 riastrad { 116 1.1 riastrad 117 1.1 riastrad fpu_kern_enter(); 118 1.1 riastrad aesni_dec(dec, in, out, nrounds); 119 1.1 riastrad fpu_kern_leave(); 120 1.1 riastrad } 121 1.1 riastrad 122 1.1 riastrad static void 123 1.1 riastrad aesni_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16], 124 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16], 125 1.1 riastrad uint32_t nrounds) 126 1.1 riastrad { 127 1.1 riastrad 128 1.1 riastrad KASSERT(nbytes % 16 == 0); 129 1.1 riastrad 130 1.1 riastrad fpu_kern_enter(); 131 1.1 riastrad aesni_cbc_enc(enc, in, out, nbytes, iv, nrounds); 132 1.1 riastrad fpu_kern_leave(); 133 1.1 riastrad } 134 1.1 riastrad 135 1.1 riastrad static void 136 1.1 riastrad aesni_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16], 137 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16], 138 1.1 riastrad uint32_t nrounds) 139 1.1 riastrad { 140 1.1 riastrad 141 1.1 riastrad KASSERT(nbytes % 16 == 0); 142 1.1 riastrad 143 1.1 riastrad fpu_kern_enter(); 144 1.1 riastrad 145 1.1 riastrad if (nbytes % 128) { 146 1.1 riastrad aesni_cbc_dec1(dec, in, out, nbytes % 128, iv, nrounds); 147 1.1 riastrad in += nbytes % 128; 148 1.1 riastrad out += nbytes % 128; 149 1.1 riastrad nbytes -= nbytes % 128; 150 1.1 riastrad } 151 1.1 riastrad 152 1.1 riastrad KASSERT(nbytes % 128 == 0); 153 1.1 riastrad if (nbytes) 154 1.1 riastrad aesni_cbc_dec8(dec, in, out, nbytes, iv, nrounds); 155 1.1 riastrad 156 1.1 riastrad fpu_kern_leave(); 157 1.1 riastrad } 158 1.1 riastrad 159 1.1 riastrad static void 160 1.1 riastrad aesni_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16], 161 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16], 162 1.1 riastrad uint32_t nrounds) 163 1.1 riastrad { 164 1.1 riastrad 165 1.1 riastrad KASSERT(nbytes % 16 == 0); 166 1.1 riastrad 167 1.1 riastrad fpu_kern_enter(); 168 1.1 riastrad 169 1.1 riastrad if (nbytes % 128) { 170 1.1 riastrad aesni_xts_enc1(enc, in, out, nbytes % 128, iv, nrounds); 171 1.1 riastrad in += nbytes % 128; 172 1.1 riastrad out += nbytes % 128; 173 1.1 riastrad nbytes -= nbytes % 128; 174 1.1 riastrad } 175 1.1 riastrad 176 1.1 riastrad KASSERT(nbytes % 128 == 0); 177 1.1 riastrad if (nbytes) 178 1.1 riastrad aesni_xts_enc8(enc, in, out, nbytes, iv, nrounds); 179 1.1 riastrad 180 1.1 riastrad fpu_kern_leave(); 181 1.1 riastrad } 182 1.1 riastrad 183 1.1 riastrad static void 184 1.1 riastrad aesni_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16], 185 1.1 riastrad uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16], 186 1.1 riastrad uint32_t nrounds) 187 1.1 riastrad { 188 1.1 riastrad 189 1.1 riastrad KASSERT(nbytes % 16 == 0); 190 1.1 riastrad 191 1.1 riastrad fpu_kern_enter(); 192 1.1 riastrad 193 1.1 riastrad if (nbytes % 128) { 194 1.1 riastrad aesni_xts_dec1(dec, in, out, nbytes % 128, iv, nrounds); 195 1.1 riastrad in += nbytes % 128; 196 1.1 riastrad out += nbytes % 128; 197 1.1 riastrad nbytes -= nbytes % 128; 198 1.1 riastrad } 199 1.1 riastrad 200 1.1 riastrad KASSERT(nbytes % 128 == 0); 201 1.1 riastrad if (nbytes) 202 1.1 riastrad aesni_xts_dec8(dec, in, out, nbytes, iv, nrounds); 203 1.1 riastrad 204 1.1 riastrad fpu_kern_leave(); 205 1.1 riastrad } 206 1.1 riastrad 207 1.4 riastrad static void 208 1.4 riastrad aesni_cbcmac_update1_impl(const struct aesenc *enc, 209 1.4 riastrad const uint8_t in[static 16], size_t nbytes, uint8_t auth[static 16], 210 1.4 riastrad uint32_t nrounds) 211 1.4 riastrad { 212 1.4 riastrad 213 1.4 riastrad KASSERT(nbytes); 214 1.4 riastrad KASSERT(nbytes % 16 == 0); 215 1.4 riastrad 216 1.4 riastrad fpu_kern_enter(); 217 1.4 riastrad aesni_cbcmac_update1(enc, in, nbytes, auth, nrounds); 218 1.4 riastrad fpu_kern_leave(); 219 1.4 riastrad } 220 1.4 riastrad 221 1.4 riastrad static void 222 1.4 riastrad aesni_ccm_enc1_impl(const struct aesenc *enc, const uint8_t in[static 16], 223 1.4 riastrad uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32], 224 1.4 riastrad uint32_t nrounds) 225 1.4 riastrad { 226 1.4 riastrad 227 1.4 riastrad KASSERT(nbytes); 228 1.4 riastrad KASSERT(nbytes % 16 == 0); 229 1.4 riastrad 230 1.4 riastrad fpu_kern_enter(); 231 1.4 riastrad aesni_ccm_enc1(enc, in, out, nbytes, authctr, nrounds); 232 1.4 riastrad fpu_kern_leave(); 233 1.4 riastrad } 234 1.4 riastrad 235 1.4 riastrad static void 236 1.4 riastrad aesni_ccm_dec1_impl(const struct aesenc *enc, const uint8_t in[static 16], 237 1.4 riastrad uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32], 238 1.4 riastrad uint32_t nrounds) 239 1.4 riastrad { 240 1.4 riastrad 241 1.4 riastrad KASSERT(nbytes); 242 1.4 riastrad KASSERT(nbytes % 16 == 0); 243 1.4 riastrad 244 1.4 riastrad fpu_kern_enter(); 245 1.4 riastrad aesni_ccm_dec1(enc, in, out, nbytes, authctr, nrounds); 246 1.4 riastrad fpu_kern_leave(); 247 1.4 riastrad } 248 1.4 riastrad 249 1.1 riastrad static int 250 1.1 riastrad aesni_xts_update_selftest(void) 251 1.1 riastrad { 252 1.1 riastrad static const struct { 253 1.1 riastrad uint8_t in[16], out[16]; 254 1.1 riastrad } cases[] = { 255 1.1 riastrad {{1}, {2}}, 256 1.1 riastrad {{0,0,0,0x80}, {0,0,0,0,1}}, 257 1.1 riastrad {{0,0,0,0,0,0,0,0x80}, {0,0,0,0,0,0,0,0,1}}, 258 1.1 riastrad {{0,0,0,0x80,0,0,0,0x80}, {0,0,0,0,1,0,0,0,1}}, 259 1.1 riastrad {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x80}, {0x87}}, 260 1.1 riastrad {{0,0,0,0,0,0,0,0x80,0,0,0,0,0,0,0,0x80}, 261 1.1 riastrad {0x87,0,0,0,0,0,0,0,1}}, 262 1.1 riastrad {{0,0,0,0x80,0,0,0,0,0,0,0,0,0,0,0,0x80}, {0x87,0,0,0,1}}, 263 1.1 riastrad {{0,0,0,0x80,0,0,0,0x80,0,0,0,0,0,0,0,0x80}, 264 1.1 riastrad {0x87,0,0,0,1,0,0,0,1}}, 265 1.1 riastrad }; 266 1.1 riastrad unsigned i; 267 1.1 riastrad uint8_t tweak[16]; 268 1.1 riastrad 269 1.1 riastrad for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) { 270 1.1 riastrad aesni_xts_update(cases[i].in, tweak); 271 1.1 riastrad if (memcmp(tweak, cases[i].out, 16)) 272 1.1 riastrad return -1; 273 1.1 riastrad } 274 1.1 riastrad 275 1.1 riastrad /* Success! */ 276 1.1 riastrad return 0; 277 1.1 riastrad } 278 1.1 riastrad 279 1.1 riastrad static int 280 1.1 riastrad aesni_probe(void) 281 1.1 riastrad { 282 1.1 riastrad int result = 0; 283 1.1 riastrad 284 1.1 riastrad /* Verify that the CPU supports AES-NI. */ 285 1.2 riastrad #ifdef _KERNEL 286 1.5 maxv if ((cpu_feature[1] & CPUID2_AESNI) == 0) 287 1.1 riastrad return -1; 288 1.2 riastrad #else 289 1.2 riastrad unsigned eax, ebx, ecx, edx; 290 1.2 riastrad if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) 291 1.2 riastrad return -1; 292 1.2 riastrad if ((ecx & bit_AES) == 0) 293 1.2 riastrad return -1; 294 1.2 riastrad #endif 295 1.1 riastrad 296 1.1 riastrad fpu_kern_enter(); 297 1.1 riastrad 298 1.1 riastrad /* Verify that our XTS tweak update logic works. */ 299 1.1 riastrad if (aesni_xts_update_selftest()) 300 1.1 riastrad result = -1; 301 1.1 riastrad 302 1.1 riastrad fpu_kern_leave(); 303 1.1 riastrad 304 1.1 riastrad return result; 305 1.1 riastrad } 306 1.1 riastrad 307 1.1 riastrad struct aes_impl aes_ni_impl = { 308 1.1 riastrad .ai_name = "Intel AES-NI", 309 1.1 riastrad .ai_probe = aesni_probe, 310 1.1 riastrad .ai_setenckey = aesni_setenckey_impl, 311 1.1 riastrad .ai_setdeckey = aesni_setdeckey_impl, 312 1.1 riastrad .ai_enc = aesni_enc_impl, 313 1.1 riastrad .ai_dec = aesni_dec_impl, 314 1.1 riastrad .ai_cbc_enc = aesni_cbc_enc_impl, 315 1.1 riastrad .ai_cbc_dec = aesni_cbc_dec_impl, 316 1.1 riastrad .ai_xts_enc = aesni_xts_enc_impl, 317 1.1 riastrad .ai_xts_dec = aesni_xts_dec_impl, 318 1.4 riastrad .ai_cbcmac_update1 = aesni_cbcmac_update1_impl, 319 1.4 riastrad .ai_ccm_enc1 = aesni_ccm_enc1_impl, 320 1.4 riastrad .ai_ccm_dec1 = aesni_ccm_dec1_impl, 321 1.1 riastrad }; 322