Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Implement AES in kernel using ARMv8.0-AES on aarch64.



details:   https://anonhg.NetBSD.org/src/rev/efda02643285
branches:  trunk
changeset: 935302:efda02643285
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Jun 29 23:31:41 2020 +0000

description:
Implement AES in kernel using ARMv8.0-AES on aarch64.

diffstat:

 sys/arch/aarch64/aarch64/cpu.c         |    28 +-
 sys/arch/aarch64/conf/files.aarch64    |     5 +-
 sys/crypto/aes/arch/arm/aes_armv8.c    |   259 ++++++++
 sys/crypto/aes/arch/arm/aes_armv8.h    |    68 ++
 sys/crypto/aes/arch/arm/aes_armv8_64.S |  1014 ++++++++++++++++++++++++++++++++
 sys/crypto/aes/arch/arm/files.aesarmv8 |     4 +
 6 files changed, 1375 insertions(+), 3 deletions(-)

diffs (truncated from 1447 to 300 lines):

diff -r d04f2d9c6b19 -r efda02643285 sys/arch/aarch64/aarch64/cpu.c
--- a/sys/arch/aarch64/aarch64/cpu.c    Mon Jun 29 23:29:39 2020 +0000
+++ b/sys/arch/aarch64/aarch64/cpu.c    Mon Jun 29 23:31:41 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu.c,v 1.48 2020/06/29 23:22:27 riastradh Exp $ */
+/* $NetBSD: cpu.c,v 1.49 2020/06/29 23:31:41 riastradh Exp $ */
 
 /*
  * Copyright (c) 2017 Ryo Shimizu <ryo%nerv.org@localhost>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: cpu.c,v 1.48 2020/06/29 23:22:27 riastradh Exp $");
+__KERNEL_RCSID(1, "$NetBSD: cpu.c,v 1.49 2020/06/29 23:31:41 riastradh Exp $");
 
 #include "locators.h"
 #include "opt_arm_debug.h"
@@ -44,6 +44,8 @@
 #include <sys/sysctl.h>
 #include <sys/systm.h>
 
+#include <crypto/aes/arch/arm/aes_armv8.h>
+
 #include <aarch64/armreg.h>
 #include <aarch64/cpu.h>
 #include <aarch64/cpufunc.h>
@@ -70,6 +72,7 @@
 static void cpu_setup_id(struct cpu_info *);
 static void cpu_setup_sysctl(device_t, struct cpu_info *);
 static void cpu_setup_rng(device_t, struct cpu_info *);
+static void cpu_setup_aes(device_t, struct cpu_info *);
 
 #ifdef MULTIPROCESSOR
 #define NCPUINFO       MAXCPUS
@@ -158,6 +161,7 @@
 
        cpu_setup_sysctl(dv, ci);
        cpu_setup_rng(dv, ci);
+       cpu_setup_aes(dv, ci);
 }
 
 struct cpuidtab {
@@ -589,6 +593,26 @@
            RND_FLAG_DEFAULT|RND_FLAG_HASCB);
 }
 
+/*
+ * setup the AES implementation
+ */
+static void
+cpu_setup_aes(device_t dv, struct cpu_info *ci)
+{
+       struct aarch64_sysctl_cpu_id *id = &ci->ci_id;
+
+       /* Verify that it is supported.  */
+       switch (__SHIFTOUT(id->ac_aa64isar0, ID_AA64ISAR0_EL1_AES)) {
+       case ID_AA64ISAR0_EL1_AES_AES:
+       case ID_AA64ISAR0_EL1_AES_PMUL:
+               break;
+       default:
+               return;
+       }
+
+       aes_md_init(&aes_armv8_impl);
+}
+
 #ifdef MULTIPROCESSOR
 void
 cpu_hatch(struct cpu_info *ci)
diff -r d04f2d9c6b19 -r efda02643285 sys/arch/aarch64/conf/files.aarch64
--- a/sys/arch/aarch64/conf/files.aarch64       Mon Jun 29 23:29:39 2020 +0000
+++ b/sys/arch/aarch64/conf/files.aarch64       Mon Jun 29 23:31:41 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.aarch64,v 1.22 2020/04/18 11:00:37 skrll Exp $
+#      $NetBSD: files.aarch64,v 1.23 2020/06/29 23:31:41 riastradh Exp $
 
 defflag opt_cpuoptions.h       AARCH64_ALIGNMENT_CHECK
 defflag opt_cpuoptions.h       AARCH64_EL0_STACK_ALIGNMENT_CHECK
@@ -138,3 +138,6 @@
 
 # profiling support
 file   dev/tprof/tprof_armv8.c                 tprof   needs-flag
+
+# ARMv8.0-AES
+include "crypto/aes/arch/arm/files.aesarmv8"
diff -r d04f2d9c6b19 -r efda02643285 sys/crypto/aes/arch/arm/aes_armv8.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/crypto/aes/arch/arm/aes_armv8.c       Mon Jun 29 23:31:41 2020 +0000
@@ -0,0 +1,259 @@
+/*     $NetBSD: aes_armv8.c,v 1.1 2020/06/29 23:31:41 riastradh Exp $  */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(1, "$NetBSD: aes_armv8.c,v 1.1 2020/06/29 23:31:41 riastradh Exp $");
+
+#include <sys/types.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+
+#include <crypto/aes/aes.h>
+#include <crypto/aes/arch/arm/aes_armv8.h>
+
+#include <aarch64/armreg.h>
+#include <aarch64/fpu.h>
+
+static void
+aesarmv8_setenckey(struct aesenc *enc, const uint8_t key[static 16],
+    uint32_t nrounds)
+{
+
+       switch (nrounds) {
+       case 10:
+               aesarmv8_setenckey128(enc, key);
+               break;
+       case 12:
+               aesarmv8_setenckey192(enc, key);
+               break;
+       case 14:
+               aesarmv8_setenckey256(enc, key);
+               break;
+       default:
+               panic("invalid AES rounds: %u", nrounds);
+       }
+}
+
+static void
+aesarmv8_setenckey_impl(struct aesenc *enc, const uint8_t key[static 16],
+    uint32_t nrounds)
+{
+
+       fpu_kern_enter();
+       aesarmv8_setenckey(enc, key, nrounds);
+       fpu_kern_leave();
+}
+
+static void
+aesarmv8_setdeckey_impl(struct aesdec *dec, const uint8_t key[static 16],
+    uint32_t nrounds)
+{
+       struct aesenc enc;
+
+       fpu_kern_enter();
+       aesarmv8_setenckey(&enc, key, nrounds);
+       aesarmv8_enctodec(&enc, dec, nrounds);
+       fpu_kern_leave();
+
+       explicit_memset(&enc, 0, sizeof enc);
+}
+
+static void
+aesarmv8_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
+    uint8_t out[static 16], uint32_t nrounds)
+{
+
+       fpu_kern_enter();
+       aesarmv8_enc(enc, in, out, nrounds);
+       fpu_kern_leave();
+}
+
+static void
+aesarmv8_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
+    uint8_t out[static 16], uint32_t nrounds)
+{
+
+       fpu_kern_enter();
+       aesarmv8_dec(dec, in, out, nrounds);
+       fpu_kern_leave();
+}
+
+static void
+aesarmv8_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
+    uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
+    uint32_t nrounds)
+{
+
+       KASSERT(nbytes % 16 == 0);
+
+       fpu_kern_enter();
+       aesarmv8_cbc_enc(enc, in, out, nbytes, iv, nrounds);
+       fpu_kern_leave();
+}
+
+static void
+aesarmv8_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
+    uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
+    uint32_t nrounds)
+{
+
+       KASSERT(nbytes % 16 == 0);
+
+       fpu_kern_enter();
+
+       if (nbytes % 128) {
+               aesarmv8_cbc_dec1(dec, in, out, nbytes % 128, iv, nrounds);
+               in += nbytes % 128;
+               out += nbytes % 128;
+               nbytes -= nbytes % 128;
+       }
+
+       KASSERT(nbytes % 128 == 0);
+       if (nbytes)
+               aesarmv8_cbc_dec8(dec, in, out, nbytes, iv, nrounds);
+
+       fpu_kern_leave();
+}
+
+static void
+aesarmv8_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
+    uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
+    uint32_t nrounds)
+{
+
+       KASSERT(nbytes % 16 == 0);
+
+       fpu_kern_enter();
+
+       if (nbytes % 128) {
+               aesarmv8_xts_enc1(enc, in, out, nbytes % 128, tweak, nrounds);
+               in += nbytes % 128;
+               out += nbytes % 128;
+               nbytes -= nbytes % 128;
+       }
+
+       KASSERT(nbytes % 128 == 0);
+       if (nbytes)
+               aesarmv8_xts_enc8(enc, in, out, nbytes, tweak, nrounds);
+
+       fpu_kern_leave();
+}
+
+static void
+aesarmv8_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
+    uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
+    uint32_t nrounds)
+{
+
+       KASSERT(nbytes % 16 == 0);
+
+       fpu_kern_enter();
+
+       if (nbytes % 128) {
+               aesarmv8_xts_dec1(dec, in, out, nbytes % 128, tweak, nrounds);
+               in += nbytes % 128;
+               out += nbytes % 128;
+               nbytes -= nbytes % 128;
+       }
+
+       KASSERT(nbytes % 128 == 0);
+       if (nbytes)
+               aesarmv8_xts_dec8(dec, in, out, nbytes, tweak, nrounds);
+
+       fpu_kern_leave();
+}
+
+static int
+aesarmv8_xts_update_selftest(void)
+{
+       static const struct {
+               uint8_t in[16], out[16];
+       } cases[] = {
+               {{1}, {2}},
+               {{0,0,0,0x80}, {0,0,0,0,1}},
+               {{0,0,0,0,0,0,0,0x80}, {0,0,0,0,0,0,0,0,1}},
+               {{0,0,0,0x80,0,0,0,0x80}, {0,0,0,0,1,0,0,0,1}},
+               {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x80}, {0x87}},
+               {{0,0,0,0,0,0,0,0x80,0,0,0,0,0,0,0,0x80},
+                {0x87,0,0,0,0,0,0,0,1}},
+               {{0,0,0,0x80,0,0,0,0,0,0,0,0,0,0,0,0x80}, {0x87,0,0,0,1}},
+               {{0,0,0,0x80,0,0,0,0x80,0,0,0,0,0,0,0,0x80},
+                {0x87,0,0,0,1,0,0,0,1}},
+       };
+       unsigned i;
+       uint8_t tweak[16];
+



Home | Main Index | Thread Index | Old Index