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