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