Home | History | Annotate | Line # | Download | only in arm
      1  1.2  jmcneill /*	$NetBSD: chacha_neon_impl.c,v 1.2 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.2  jmcneill __KERNEL_RCSID(1, "$NetBSD: chacha_neon_impl.c,v 1.2 2020/10/10 08:24:10 jmcneill Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include "chacha_neon.h"
     33  1.1  riastrad 
     34  1.1  riastrad #ifdef __aarch64__
     35  1.1  riastrad #include <aarch64/armreg.h>
     36  1.1  riastrad #endif
     37  1.1  riastrad 
     38  1.1  riastrad #ifdef _KERNEL
     39  1.1  riastrad #include <sys/proc.h>
     40  1.1  riastrad #ifndef __aarch64__
     41  1.1  riastrad #include <arm/locore.h>
     42  1.1  riastrad #endif
     43  1.1  riastrad #include <arm/fpu.h>
     44  1.1  riastrad #else
     45  1.1  riastrad #include <sys/sysctl.h>
     46  1.1  riastrad #include <stddef.h>
     47  1.1  riastrad #define	fpu_kern_enter()	((void)0)
     48  1.1  riastrad #define	fpu_kern_leave()	((void)0)
     49  1.1  riastrad #endif
     50  1.1  riastrad 
     51  1.1  riastrad static void
     52  1.1  riastrad chacha_core_neon_impl(uint8_t out[restrict static 64],
     53  1.1  riastrad     const uint8_t in[static 16],
     54  1.1  riastrad     const uint8_t k[static 32],
     55  1.1  riastrad     const uint8_t c[static 16],
     56  1.1  riastrad     unsigned nr)
     57  1.1  riastrad {
     58  1.1  riastrad 
     59  1.1  riastrad 	fpu_kern_enter();
     60  1.1  riastrad 	chacha_core_neon(out, in, k, c, nr);
     61  1.1  riastrad 	fpu_kern_leave();
     62  1.1  riastrad }
     63  1.1  riastrad 
     64  1.1  riastrad static void
     65  1.1  riastrad hchacha_neon_impl(uint8_t out[restrict static 32],
     66  1.1  riastrad     const uint8_t in[static 16],
     67  1.1  riastrad     const uint8_t k[static 32],
     68  1.1  riastrad     const uint8_t c[static 16],
     69  1.1  riastrad     unsigned nr)
     70  1.1  riastrad {
     71  1.1  riastrad 
     72  1.1  riastrad 	fpu_kern_enter();
     73  1.1  riastrad 	hchacha_neon(out, in, k, c, nr);
     74  1.1  riastrad 	fpu_kern_leave();
     75  1.1  riastrad }
     76  1.1  riastrad 
     77  1.1  riastrad static void
     78  1.1  riastrad chacha_stream_neon_impl(uint8_t *restrict s, size_t nbytes, uint32_t blkno,
     79  1.1  riastrad     const uint8_t nonce[static 12],
     80  1.1  riastrad     const uint8_t key[static 32],
     81  1.1  riastrad     unsigned nr)
     82  1.1  riastrad {
     83  1.1  riastrad 
     84  1.1  riastrad 	fpu_kern_enter();
     85  1.1  riastrad 	chacha_stream_neon(s, nbytes, blkno, nonce, key, nr);
     86  1.1  riastrad 	fpu_kern_leave();
     87  1.1  riastrad }
     88  1.1  riastrad 
     89  1.1  riastrad static void
     90  1.1  riastrad chacha_stream_xor_neon_impl(uint8_t *c, const uint8_t *p, size_t nbytes,
     91  1.1  riastrad     uint32_t blkno,
     92  1.1  riastrad     const uint8_t nonce[static 12],
     93  1.1  riastrad     const uint8_t key[static 32],
     94  1.1  riastrad     unsigned nr)
     95  1.1  riastrad {
     96  1.1  riastrad 
     97  1.1  riastrad 	fpu_kern_enter();
     98  1.1  riastrad 	chacha_stream_xor_neon(c, p, nbytes, blkno, nonce, key, nr);
     99  1.1  riastrad 	fpu_kern_leave();
    100  1.1  riastrad }
    101  1.1  riastrad 
    102  1.1  riastrad static void
    103  1.1  riastrad xchacha_stream_neon_impl(uint8_t *restrict s, size_t nbytes, uint32_t blkno,
    104  1.1  riastrad     const uint8_t nonce[static 24],
    105  1.1  riastrad     const uint8_t key[static 32],
    106  1.1  riastrad     unsigned nr)
    107  1.1  riastrad {
    108  1.1  riastrad 
    109  1.1  riastrad 	fpu_kern_enter();
    110  1.1  riastrad 	xchacha_stream_neon(s, nbytes, blkno, nonce, key, nr);
    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 xchacha_stream_xor_neon_impl(uint8_t *c, const uint8_t *p, size_t nbytes,
    116  1.1  riastrad     uint32_t blkno,
    117  1.1  riastrad     const uint8_t nonce[static 24],
    118  1.1  riastrad     const uint8_t key[static 32],
    119  1.1  riastrad     unsigned nr)
    120  1.1  riastrad {
    121  1.1  riastrad 
    122  1.1  riastrad 	fpu_kern_enter();
    123  1.1  riastrad 	xchacha_stream_xor_neon(c, p, nbytes, blkno, nonce, key, nr);
    124  1.1  riastrad 	fpu_kern_leave();
    125  1.1  riastrad }
    126  1.1  riastrad 
    127  1.1  riastrad static int
    128  1.1  riastrad chacha_probe_neon(void)
    129  1.1  riastrad {
    130  1.1  riastrad #ifdef __aarch64__
    131  1.1  riastrad 	struct aarch64_sysctl_cpu_id *id;
    132  1.1  riastrad #endif
    133  1.1  riastrad 	int result = 0;
    134  1.1  riastrad 
    135  1.1  riastrad 	/* Verify that the CPU supports NEON.  */
    136  1.1  riastrad #ifdef __aarch64__
    137  1.1  riastrad #ifdef _KERNEL
    138  1.1  riastrad 	id = &curcpu()->ci_id;
    139  1.1  riastrad #else
    140  1.1  riastrad 	struct aarch64_sysctl_cpu_id ids;
    141  1.1  riastrad 	size_t idlen;
    142  1.1  riastrad 	id = &ids;
    143  1.1  riastrad 	idlen = sizeof ids;
    144  1.1  riastrad 	if (sysctlbyname("machdep.cpu0.cpu_id", id, &idlen, NULL, 0))
    145  1.1  riastrad 		return -1;
    146  1.1  riastrad 	if (idlen != sizeof ids)
    147  1.1  riastrad 		return -1;
    148  1.1  riastrad #endif
    149  1.1  riastrad 	switch (__SHIFTOUT(id->ac_aa64pfr0, ID_AA64PFR0_EL1_ADVSIMD)) {
    150  1.2  jmcneill 	case ID_AA64PFR0_EL1_ADV_SIMD_NONE:
    151  1.2  jmcneill 		return -1;
    152  1.2  jmcneill 	default:
    153  1.1  riastrad 		break;
    154  1.1  riastrad 	}
    155  1.1  riastrad #else
    156  1.1  riastrad #ifdef _KERNEL
    157  1.1  riastrad 	if (!cpu_neon_present)
    158  1.1  riastrad 		return -1;
    159  1.1  riastrad #else
    160  1.1  riastrad 	int neon;
    161  1.1  riastrad 	size_t neonlen = sizeof neon;
    162  1.1  riastrad 	if (sysctlbyname("machdep.neon_present", &neon, &neonlen, NULL, 0))
    163  1.1  riastrad 		return -1;
    164  1.1  riastrad 	if (!neon)
    165  1.1  riastrad 		return -1;
    166  1.1  riastrad #endif
    167  1.1  riastrad #endif
    168  1.1  riastrad 
    169  1.1  riastrad 	return result;
    170  1.1  riastrad }
    171  1.1  riastrad 
    172  1.1  riastrad const struct chacha_impl chacha_neon_impl = {
    173  1.1  riastrad 	.ci_name = "ARM NEON ChaCha",
    174  1.1  riastrad 	.ci_probe = chacha_probe_neon,
    175  1.1  riastrad 	.ci_chacha_core = chacha_core_neon_impl,
    176  1.1  riastrad 	.ci_hchacha = hchacha_neon_impl,
    177  1.1  riastrad 	.ci_chacha_stream = chacha_stream_neon_impl,
    178  1.1  riastrad 	.ci_chacha_stream_xor = chacha_stream_xor_neon_impl,
    179  1.1  riastrad 	.ci_xchacha_stream = xchacha_stream_neon_impl,
    180  1.1  riastrad 	.ci_xchacha_stream_xor = xchacha_stream_xor_neon_impl,
    181  1.1  riastrad };
    182