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