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