Home | History | Annotate | Line # | Download | only in arm
aes_neon_impl.c revision 1.1
      1 /*	$NetBSD: aes_neon_impl.c,v 1.1 2020/06/29 23:56:31 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.1 2020/06/29 23:56:31 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 #include <arm/fpu.h>
     39 
     40 #ifdef __aarch64__
     41 #include <aarch64/armreg.h>
     42 #else
     43 #include <arm/locore.h>
     44 #endif
     45 
     46 static void
     47 aes_neon_setenckey_impl(struct aesenc *enc, const uint8_t *key,
     48     uint32_t nrounds)
     49 {
     50 
     51 	fpu_kern_enter();
     52 	aes_neon_setenckey(enc, key, nrounds);
     53 	fpu_kern_leave();
     54 }
     55 
     56 static void
     57 aes_neon_setdeckey_impl(struct aesdec *dec, const uint8_t *key,
     58     uint32_t nrounds)
     59 {
     60 
     61 	fpu_kern_enter();
     62 	aes_neon_setdeckey(dec, key, nrounds);
     63 	fpu_kern_leave();
     64 }
     65 
     66 static void
     67 aes_neon_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     68     uint8_t out[static 16], uint32_t nrounds)
     69 {
     70 
     71 	fpu_kern_enter();
     72 	aes_neon_enc(enc, in, out, nrounds);
     73 	fpu_kern_leave();
     74 }
     75 
     76 static void
     77 aes_neon_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
     78     uint8_t out[static 16], uint32_t nrounds)
     79 {
     80 
     81 	fpu_kern_enter();
     82 	aes_neon_dec(dec, in, out, nrounds);
     83 	fpu_kern_leave();
     84 }
     85 
     86 static void
     87 aes_neon_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     88     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
     89     uint32_t nrounds)
     90 {
     91 
     92 	if (nbytes == 0)
     93 		return;
     94 	fpu_kern_enter();
     95 	aes_neon_cbc_enc(enc, in, out, nbytes, iv, nrounds);
     96 	fpu_kern_leave();
     97 }
     98 
     99 static void
    100 aes_neon_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    101     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    102     uint32_t nrounds)
    103 {
    104 
    105 	if (nbytes == 0)
    106 		return;
    107 	fpu_kern_enter();
    108 	aes_neon_cbc_dec(dec, in, out, nbytes, iv, nrounds);
    109 	fpu_kern_leave();
    110 }
    111 
    112 static void
    113 aes_neon_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    114     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    115     uint32_t nrounds)
    116 {
    117 
    118 	if (nbytes == 0)
    119 		return;
    120 	fpu_kern_enter();
    121 	aes_neon_xts_enc(enc, in, out, nbytes, iv, nrounds);
    122 	fpu_kern_leave();
    123 }
    124 
    125 static void
    126 aes_neon_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    127     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    128     uint32_t nrounds)
    129 {
    130 
    131 	if (nbytes == 0)
    132 		return;
    133 	fpu_kern_enter();
    134 	aes_neon_xts_dec(dec, in, out, nbytes, iv, nrounds);
    135 	fpu_kern_leave();
    136 }
    137 
    138 static int
    139 aes_neon_probe(void)
    140 {
    141 #ifdef __aarch64__
    142 	struct aarch64_sysctl_cpu_id *id;
    143 #endif
    144 	int result = 0;
    145 
    146 	/* Verify that the CPU supports NEON.  */
    147 #ifdef __aarch64__
    148 	id = &curcpu()->ci_id;
    149 	switch (__SHIFTOUT(id->ac_aa64pfr0, ID_AA64PFR0_EL1_ADVSIMD)) {
    150 	case ID_AA64PFR0_EL1_ADV_SIMD_IMPL:
    151 		break;
    152 	default:
    153 		return -1;
    154 	}
    155 #else
    156 	if (!cpu_neon_present)
    157 		return -1;
    158 #endif
    159 
    160 	fpu_kern_enter();
    161 	result = aes_neon_selftest();
    162 	fpu_kern_leave();
    163 
    164 	return result;
    165 }
    166 
    167 struct aes_impl aes_neon_impl = {
    168 	.ai_name = "ARM NEON vpaes",
    169 	.ai_probe = aes_neon_probe,
    170 	.ai_setenckey = aes_neon_setenckey_impl,
    171 	.ai_setdeckey = aes_neon_setdeckey_impl,
    172 	.ai_enc = aes_neon_enc_impl,
    173 	.ai_dec = aes_neon_dec_impl,
    174 	.ai_cbc_enc = aes_neon_cbc_enc_impl,
    175 	.ai_cbc_dec = aes_neon_cbc_dec_impl,
    176 	.ai_xts_enc = aes_neon_xts_enc_impl,
    177 	.ai_xts_dec = aes_neon_xts_dec_impl,
    178 };
    179