Home | History | Annotate | Line # | Download | only in arm
aes_neon_impl.c revision 1.2
      1 /*	$NetBSD: aes_neon_impl.c,v 1.2 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_neon_impl.c,v 1.2 2020/06/30 20:32:11 riastradh Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/proc.h>
     34 
     35 #include <crypto/aes/aes.h>
     36 #include <crypto/aes/arch/arm/aes_neon.h>
     37 
     38 #ifdef __aarch64__
     39 #include <aarch64/armreg.h>
     40 #endif
     41 
     42 #ifdef _KERNEL
     43 #ifndef __aarch64__
     44 #include <arm/locore.h>
     45 #endif
     46 #include <arm/fpu.h>
     47 #else
     48 #include <sys/sysctl.h>
     49 #include <stddef.h>
     50 #define	fpu_kern_enter()	((void)0)
     51 #define	fpu_kern_leave()	((void)0)
     52 #endif
     53 
     54 static void
     55 aes_neon_setenckey_impl(struct aesenc *enc, const uint8_t *key,
     56     uint32_t nrounds)
     57 {
     58 
     59 	fpu_kern_enter();
     60 	aes_neon_setenckey(enc, key, nrounds);
     61 	fpu_kern_leave();
     62 }
     63 
     64 static void
     65 aes_neon_setdeckey_impl(struct aesdec *dec, const uint8_t *key,
     66     uint32_t nrounds)
     67 {
     68 
     69 	fpu_kern_enter();
     70 	aes_neon_setdeckey(dec, key, nrounds);
     71 	fpu_kern_leave();
     72 }
     73 
     74 static void
     75 aes_neon_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     76     uint8_t out[static 16], uint32_t nrounds)
     77 {
     78 
     79 	fpu_kern_enter();
     80 	aes_neon_enc(enc, in, out, nrounds);
     81 	fpu_kern_leave();
     82 }
     83 
     84 static void
     85 aes_neon_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
     86     uint8_t out[static 16], uint32_t nrounds)
     87 {
     88 
     89 	fpu_kern_enter();
     90 	aes_neon_dec(dec, in, out, nrounds);
     91 	fpu_kern_leave();
     92 }
     93 
     94 static void
     95 aes_neon_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     96     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
     97     uint32_t nrounds)
     98 {
     99 
    100 	if (nbytes == 0)
    101 		return;
    102 	fpu_kern_enter();
    103 	aes_neon_cbc_enc(enc, in, out, nbytes, iv, nrounds);
    104 	fpu_kern_leave();
    105 }
    106 
    107 static void
    108 aes_neon_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    109     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    110     uint32_t nrounds)
    111 {
    112 
    113 	if (nbytes == 0)
    114 		return;
    115 	fpu_kern_enter();
    116 	aes_neon_cbc_dec(dec, in, out, nbytes, iv, nrounds);
    117 	fpu_kern_leave();
    118 }
    119 
    120 static void
    121 aes_neon_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    122     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    123     uint32_t nrounds)
    124 {
    125 
    126 	if (nbytes == 0)
    127 		return;
    128 	fpu_kern_enter();
    129 	aes_neon_xts_enc(enc, in, out, nbytes, iv, nrounds);
    130 	fpu_kern_leave();
    131 }
    132 
    133 static void
    134 aes_neon_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    135     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    136     uint32_t nrounds)
    137 {
    138 
    139 	if (nbytes == 0)
    140 		return;
    141 	fpu_kern_enter();
    142 	aes_neon_xts_dec(dec, in, out, nbytes, iv, nrounds);
    143 	fpu_kern_leave();
    144 }
    145 
    146 static int
    147 aes_neon_probe(void)
    148 {
    149 #ifdef __aarch64__
    150 	struct aarch64_sysctl_cpu_id *id;
    151 #endif
    152 	int result = 0;
    153 
    154 	/* Verify that the CPU supports NEON.  */
    155 #ifdef __aarch64__
    156 #ifdef _KERNEL
    157 	id = &curcpu()->ci_id;
    158 #else
    159 	struct aarch64_sysctl_cpu_id ids;
    160 	size_t idlen;
    161 	id = &ids;
    162 	idlen = sizeof ids;
    163 	if (sysctlbyname("machdep.cpu0.cpu_id", id, &idlen, NULL, 0))
    164 		return -1;
    165 	if (idlen != sizeof ids)
    166 		return -1;
    167 #endif
    168 	switch (__SHIFTOUT(id->ac_aa64pfr0, ID_AA64PFR0_EL1_ADVSIMD)) {
    169 	case ID_AA64PFR0_EL1_ADV_SIMD_IMPL:
    170 		break;
    171 	default:
    172 		return -1;
    173 	}
    174 #else
    175 #ifdef _KERNEL
    176 	if (!cpu_neon_present)
    177 		return -1;
    178 #else
    179 	int neon;
    180 	size_t neonlen = sizeof neon;
    181 	if (0 && sysctlbyname("machdep.neon_present", &neon, &neonlen, NULL, 0))
    182 		return -1;
    183 	if (0 && !neon)
    184 		return -1;
    185 #endif
    186 #endif
    187 
    188 	fpu_kern_enter();
    189 	result = aes_neon_selftest();
    190 	fpu_kern_leave();
    191 
    192 	return result;
    193 }
    194 
    195 struct aes_impl aes_neon_impl = {
    196 	.ai_name = "ARM NEON vpaes",
    197 	.ai_probe = aes_neon_probe,
    198 	.ai_setenckey = aes_neon_setenckey_impl,
    199 	.ai_setdeckey = aes_neon_setdeckey_impl,
    200 	.ai_enc = aes_neon_enc_impl,
    201 	.ai_dec = aes_neon_dec_impl,
    202 	.ai_cbc_enc = aes_neon_cbc_enc_impl,
    203 	.ai_cbc_dec = aes_neon_cbc_dec_impl,
    204 	.ai_xts_enc = aes_neon_xts_enc_impl,
    205 	.ai_xts_dec = aes_neon_xts_dec_impl,
    206 };
    207