Home | History | Annotate | Line # | Download | only in arm
aes_armv8.c revision 1.5
      1  1.5  riastrad /*	$NetBSD: aes_armv8.c,v 1.5 2020/07/25 22:33: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.5  riastrad __KERNEL_RCSID(1, "$NetBSD: aes_armv8.c,v 1.5 2020/07/25 22:33:04 riastradh Exp $");
     31  1.1  riastrad 
     32  1.3  riastrad #ifdef _KERNEL
     33  1.1  riastrad #include <sys/types.h>
     34  1.1  riastrad #include <sys/proc.h>
     35  1.1  riastrad #include <sys/systm.h>
     36  1.3  riastrad #else
     37  1.3  riastrad #include <assert.h>
     38  1.3  riastrad #include <err.h>
     39  1.3  riastrad #include <stdint.h>
     40  1.3  riastrad #include <string.h>
     41  1.3  riastrad #define	KASSERT			assert
     42  1.3  riastrad #define	panic(fmt, args...)	err(1, fmt, args)
     43  1.3  riastrad #endif
     44  1.1  riastrad 
     45  1.1  riastrad #include <crypto/aes/aes.h>
     46  1.4  riastrad #include <crypto/aes/aes_impl.h>
     47  1.1  riastrad #include <crypto/aes/arch/arm/aes_armv8.h>
     48  1.1  riastrad 
     49  1.3  riastrad #include <aarch64/armreg.h>
     50  1.3  riastrad 
     51  1.3  riastrad #ifdef _KERNEL
     52  1.2  riastrad #include <arm/fpu.h>
     53  1.3  riastrad #else
     54  1.3  riastrad #include <sys/sysctl.h>
     55  1.3  riastrad #include <stddef.h>
     56  1.3  riastrad #define	fpu_kern_enter()	((void)0)
     57  1.3  riastrad #define	fpu_kern_leave()	((void)0)
     58  1.3  riastrad #endif
     59  1.1  riastrad 
     60  1.1  riastrad static void
     61  1.1  riastrad aesarmv8_setenckey(struct aesenc *enc, const uint8_t key[static 16],
     62  1.1  riastrad     uint32_t nrounds)
     63  1.1  riastrad {
     64  1.1  riastrad 
     65  1.1  riastrad 	switch (nrounds) {
     66  1.1  riastrad 	case 10:
     67  1.1  riastrad 		aesarmv8_setenckey128(enc, key);
     68  1.1  riastrad 		break;
     69  1.1  riastrad 	case 12:
     70  1.1  riastrad 		aesarmv8_setenckey192(enc, key);
     71  1.1  riastrad 		break;
     72  1.1  riastrad 	case 14:
     73  1.1  riastrad 		aesarmv8_setenckey256(enc, key);
     74  1.1  riastrad 		break;
     75  1.1  riastrad 	default:
     76  1.1  riastrad 		panic("invalid AES rounds: %u", nrounds);
     77  1.1  riastrad 	}
     78  1.1  riastrad }
     79  1.1  riastrad 
     80  1.1  riastrad static void
     81  1.1  riastrad aesarmv8_setenckey_impl(struct aesenc *enc, const uint8_t key[static 16],
     82  1.1  riastrad     uint32_t nrounds)
     83  1.1  riastrad {
     84  1.1  riastrad 
     85  1.1  riastrad 	fpu_kern_enter();
     86  1.1  riastrad 	aesarmv8_setenckey(enc, key, nrounds);
     87  1.1  riastrad 	fpu_kern_leave();
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad static void
     91  1.1  riastrad aesarmv8_setdeckey_impl(struct aesdec *dec, const uint8_t key[static 16],
     92  1.1  riastrad     uint32_t nrounds)
     93  1.1  riastrad {
     94  1.1  riastrad 	struct aesenc enc;
     95  1.1  riastrad 
     96  1.1  riastrad 	fpu_kern_enter();
     97  1.1  riastrad 	aesarmv8_setenckey(&enc, key, nrounds);
     98  1.1  riastrad 	aesarmv8_enctodec(&enc, dec, nrounds);
     99  1.1  riastrad 	fpu_kern_leave();
    100  1.1  riastrad 
    101  1.1  riastrad 	explicit_memset(&enc, 0, sizeof enc);
    102  1.1  riastrad }
    103  1.1  riastrad 
    104  1.1  riastrad static void
    105  1.1  riastrad aesarmv8_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    106  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
    107  1.1  riastrad {
    108  1.1  riastrad 
    109  1.1  riastrad 	fpu_kern_enter();
    110  1.1  riastrad 	aesarmv8_enc(enc, in, out, nrounds);
    111  1.1  riastrad 	fpu_kern_leave();
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad static void
    115  1.1  riastrad aesarmv8_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    116  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
    117  1.1  riastrad {
    118  1.1  riastrad 
    119  1.1  riastrad 	fpu_kern_enter();
    120  1.1  riastrad 	aesarmv8_dec(dec, in, out, nrounds);
    121  1.1  riastrad 	fpu_kern_leave();
    122  1.1  riastrad }
    123  1.1  riastrad 
    124  1.1  riastrad static void
    125  1.1  riastrad aesarmv8_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    126  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    127  1.1  riastrad     uint32_t nrounds)
    128  1.1  riastrad {
    129  1.1  riastrad 
    130  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    131  1.1  riastrad 
    132  1.1  riastrad 	fpu_kern_enter();
    133  1.1  riastrad 	aesarmv8_cbc_enc(enc, in, out, nbytes, iv, nrounds);
    134  1.1  riastrad 	fpu_kern_leave();
    135  1.1  riastrad }
    136  1.1  riastrad 
    137  1.1  riastrad static void
    138  1.1  riastrad aesarmv8_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    139  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    140  1.1  riastrad     uint32_t nrounds)
    141  1.1  riastrad {
    142  1.1  riastrad 
    143  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    144  1.1  riastrad 
    145  1.1  riastrad 	fpu_kern_enter();
    146  1.1  riastrad 
    147  1.1  riastrad 	if (nbytes % 128) {
    148  1.1  riastrad 		aesarmv8_cbc_dec1(dec, in, out, nbytes % 128, iv, nrounds);
    149  1.1  riastrad 		in += nbytes % 128;
    150  1.1  riastrad 		out += nbytes % 128;
    151  1.1  riastrad 		nbytes -= nbytes % 128;
    152  1.1  riastrad 	}
    153  1.1  riastrad 
    154  1.1  riastrad 	KASSERT(nbytes % 128 == 0);
    155  1.1  riastrad 	if (nbytes)
    156  1.1  riastrad 		aesarmv8_cbc_dec8(dec, in, out, nbytes, iv, nrounds);
    157  1.1  riastrad 
    158  1.1  riastrad 	fpu_kern_leave();
    159  1.1  riastrad }
    160  1.1  riastrad 
    161  1.1  riastrad static void
    162  1.1  riastrad aesarmv8_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    163  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    164  1.1  riastrad     uint32_t nrounds)
    165  1.1  riastrad {
    166  1.1  riastrad 
    167  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    168  1.1  riastrad 
    169  1.1  riastrad 	fpu_kern_enter();
    170  1.1  riastrad 
    171  1.1  riastrad 	if (nbytes % 128) {
    172  1.1  riastrad 		aesarmv8_xts_enc1(enc, in, out, nbytes % 128, tweak, nrounds);
    173  1.1  riastrad 		in += nbytes % 128;
    174  1.1  riastrad 		out += nbytes % 128;
    175  1.1  riastrad 		nbytes -= nbytes % 128;
    176  1.1  riastrad 	}
    177  1.1  riastrad 
    178  1.1  riastrad 	KASSERT(nbytes % 128 == 0);
    179  1.1  riastrad 	if (nbytes)
    180  1.1  riastrad 		aesarmv8_xts_enc8(enc, in, out, nbytes, tweak, nrounds);
    181  1.1  riastrad 
    182  1.1  riastrad 	fpu_kern_leave();
    183  1.1  riastrad }
    184  1.1  riastrad 
    185  1.1  riastrad static void
    186  1.1  riastrad aesarmv8_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    187  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    188  1.1  riastrad     uint32_t nrounds)
    189  1.1  riastrad {
    190  1.1  riastrad 
    191  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    192  1.1  riastrad 
    193  1.1  riastrad 	fpu_kern_enter();
    194  1.1  riastrad 
    195  1.1  riastrad 	if (nbytes % 128) {
    196  1.1  riastrad 		aesarmv8_xts_dec1(dec, in, out, nbytes % 128, tweak, nrounds);
    197  1.1  riastrad 		in += nbytes % 128;
    198  1.1  riastrad 		out += nbytes % 128;
    199  1.1  riastrad 		nbytes -= nbytes % 128;
    200  1.1  riastrad 	}
    201  1.1  riastrad 
    202  1.1  riastrad 	KASSERT(nbytes % 128 == 0);
    203  1.1  riastrad 	if (nbytes)
    204  1.1  riastrad 		aesarmv8_xts_dec8(dec, in, out, nbytes, tweak, nrounds);
    205  1.1  riastrad 
    206  1.1  riastrad 	fpu_kern_leave();
    207  1.1  riastrad }
    208  1.1  riastrad 
    209  1.5  riastrad static void
    210  1.5  riastrad aesarmv8_cbcmac_update1_impl(const struct aesenc *enc,
    211  1.5  riastrad     const uint8_t in[static 16], size_t nbytes, uint8_t auth[static 16],
    212  1.5  riastrad     uint32_t nrounds)
    213  1.5  riastrad {
    214  1.5  riastrad 
    215  1.5  riastrad 	KASSERT(nbytes);
    216  1.5  riastrad 	KASSERT(nbytes % 16 == 0);
    217  1.5  riastrad 
    218  1.5  riastrad 	fpu_kern_enter();
    219  1.5  riastrad 	aesarmv8_cbcmac_update1(enc, in, nbytes, auth, nrounds);
    220  1.5  riastrad 	fpu_kern_leave();
    221  1.5  riastrad }
    222  1.5  riastrad 
    223  1.5  riastrad static void
    224  1.5  riastrad aesarmv8_ccm_enc1_impl(const struct aesenc *enc, const uint8_t in[static 16],
    225  1.5  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32],
    226  1.5  riastrad     uint32_t nrounds)
    227  1.5  riastrad {
    228  1.5  riastrad 
    229  1.5  riastrad 	KASSERT(nbytes);
    230  1.5  riastrad 	KASSERT(nbytes % 16 == 0);
    231  1.5  riastrad 
    232  1.5  riastrad 	fpu_kern_enter();
    233  1.5  riastrad 	aesarmv8_ccm_enc1(enc, in, out, nbytes, authctr, nrounds);
    234  1.5  riastrad 	fpu_kern_leave();
    235  1.5  riastrad }
    236  1.5  riastrad 
    237  1.5  riastrad static void
    238  1.5  riastrad aesarmv8_ccm_dec1_impl(const struct aesenc *enc, const uint8_t in[static 16],
    239  1.5  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t authctr[static 32],
    240  1.5  riastrad     uint32_t nrounds)
    241  1.5  riastrad {
    242  1.5  riastrad 
    243  1.5  riastrad 	KASSERT(nbytes);
    244  1.5  riastrad 	KASSERT(nbytes % 16 == 0);
    245  1.5  riastrad 
    246  1.5  riastrad 	fpu_kern_enter();
    247  1.5  riastrad 	aesarmv8_ccm_dec1(enc, in, out, nbytes, authctr, nrounds);
    248  1.5  riastrad 	fpu_kern_leave();
    249  1.5  riastrad }
    250  1.5  riastrad 
    251  1.1  riastrad static int
    252  1.1  riastrad aesarmv8_xts_update_selftest(void)
    253  1.1  riastrad {
    254  1.1  riastrad 	static const struct {
    255  1.1  riastrad 		uint8_t	in[16], out[16];
    256  1.1  riastrad 	} cases[] = {
    257  1.1  riastrad 		{{1}, {2}},
    258  1.1  riastrad 		{{0,0,0,0x80}, {0,0,0,0,1}},
    259  1.1  riastrad 		{{0,0,0,0,0,0,0,0x80}, {0,0,0,0,0,0,0,0,1}},
    260  1.1  riastrad 		{{0,0,0,0x80,0,0,0,0x80}, {0,0,0,0,1,0,0,0,1}},
    261  1.1  riastrad 		{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x80}, {0x87}},
    262  1.1  riastrad 		{{0,0,0,0,0,0,0,0x80,0,0,0,0,0,0,0,0x80},
    263  1.1  riastrad 		 {0x87,0,0,0,0,0,0,0,1}},
    264  1.1  riastrad 		{{0,0,0,0x80,0,0,0,0,0,0,0,0,0,0,0,0x80}, {0x87,0,0,0,1}},
    265  1.1  riastrad 		{{0,0,0,0x80,0,0,0,0x80,0,0,0,0,0,0,0,0x80},
    266  1.1  riastrad 		 {0x87,0,0,0,1,0,0,0,1}},
    267  1.1  riastrad 	};
    268  1.1  riastrad 	unsigned i;
    269  1.1  riastrad 	uint8_t tweak[16];
    270  1.1  riastrad 
    271  1.1  riastrad 	for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
    272  1.1  riastrad 		aesarmv8_xts_update(cases[i].in, tweak);
    273  1.1  riastrad 		if (memcmp(tweak, cases[i].out, 16))
    274  1.1  riastrad 			return -1;
    275  1.1  riastrad 	}
    276  1.1  riastrad 
    277  1.1  riastrad 	/* Success!  */
    278  1.1  riastrad 	return 0;
    279  1.1  riastrad }
    280  1.1  riastrad 
    281  1.1  riastrad static int
    282  1.1  riastrad aesarmv8_probe(void)
    283  1.1  riastrad {
    284  1.1  riastrad 	struct aarch64_sysctl_cpu_id *id;
    285  1.1  riastrad 	int result = 0;
    286  1.1  riastrad 
    287  1.1  riastrad 	/* Verify that the CPU supports AES.  */
    288  1.3  riastrad #ifdef _KERNEL
    289  1.1  riastrad 	id = &curcpu()->ci_id;
    290  1.3  riastrad #else
    291  1.3  riastrad 	struct aarch64_sysctl_cpu_id ids;
    292  1.3  riastrad 	size_t idlen;
    293  1.3  riastrad 	id = &ids;
    294  1.3  riastrad 	idlen = sizeof ids;
    295  1.3  riastrad 	if (sysctlbyname("machdep.cpu0.cpu_id", id, &idlen, NULL, 0))
    296  1.3  riastrad 		return -1;
    297  1.3  riastrad 	if (idlen != sizeof ids)
    298  1.3  riastrad 		return -1;
    299  1.3  riastrad #endif
    300  1.1  riastrad 	switch (__SHIFTOUT(id->ac_aa64isar0, ID_AA64ISAR0_EL1_AES)) {
    301  1.1  riastrad 	case ID_AA64ISAR0_EL1_AES_AES:
    302  1.1  riastrad 	case ID_AA64ISAR0_EL1_AES_PMUL:
    303  1.1  riastrad 		break;
    304  1.1  riastrad 	default:
    305  1.1  riastrad 		return -1;
    306  1.1  riastrad 	}
    307  1.1  riastrad 
    308  1.1  riastrad 	fpu_kern_enter();
    309  1.1  riastrad 
    310  1.1  riastrad 	/* Verify that our XTS tweak update logic works.  */
    311  1.1  riastrad 	if (aesarmv8_xts_update_selftest())
    312  1.1  riastrad 		result = -1;
    313  1.1  riastrad 
    314  1.1  riastrad 	fpu_kern_leave();
    315  1.1  riastrad 
    316  1.1  riastrad 	return result;
    317  1.1  riastrad }
    318  1.1  riastrad 
    319  1.1  riastrad struct aes_impl aes_armv8_impl = {
    320  1.1  riastrad 	.ai_name = "ARMv8.0-AES",
    321  1.1  riastrad 	.ai_probe = aesarmv8_probe,
    322  1.1  riastrad 	.ai_setenckey = aesarmv8_setenckey_impl,
    323  1.1  riastrad 	.ai_setdeckey = aesarmv8_setdeckey_impl,
    324  1.1  riastrad 	.ai_enc = aesarmv8_enc_impl,
    325  1.1  riastrad 	.ai_dec = aesarmv8_dec_impl,
    326  1.1  riastrad 	.ai_cbc_enc = aesarmv8_cbc_enc_impl,
    327  1.1  riastrad 	.ai_cbc_dec = aesarmv8_cbc_dec_impl,
    328  1.1  riastrad 	.ai_xts_enc = aesarmv8_xts_enc_impl,
    329  1.1  riastrad 	.ai_xts_dec = aesarmv8_xts_dec_impl,
    330  1.5  riastrad 	.ai_cbcmac_update1 = aesarmv8_cbcmac_update1_impl,
    331  1.5  riastrad 	.ai_ccm_enc1 = aesarmv8_ccm_enc1_impl,
    332  1.5  riastrad 	.ai_ccm_dec1 = aesarmv8_ccm_dec1_impl,
    333  1.1  riastrad };
    334