Skip to contentSkip to conversation

WireGuard: Secure Network Tunnel

PATCH Superseded Jason A. Donenfeld 10 participants 44 messages Aug 24, 2018 – Sep 6, 2018

Subsystems (26)

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Jason A. Donenfeld

author maintainer
#0 lore raw

v2 cover WireGuard: Secure Network Tunnel

This patchset is available on git.kernel.org in this branch, where it may be pulled directly for inclusion into net-next:

* https://git.kernel.org/pub/scm/linux/kernel/git/zx2c4/linux.git/log/?h=jd/wireguard

WireGuard is a secure network tunnel written especially for Linux, which has faced around three years of serious development, deployment, and scrutiny. It delivers excellent performance and is extremely easy to use and configure. It has been designed with the primary goal of being both easy to audit by virtue of being small and highly secure from a cryptography and systems security perspective. WireGuard is used by some massive companies pushing enormous amounts of traffic, and likely already today you've consumed bytes that at some point transited through a WireGuard tunnel. Even as an out-of-tree module, WireGuard has been integrated into various userspace tools, Linux distributions, mobile phones, and data centers. There are ports in several languages to several operating systems, and even commercial hardware and services sold integrating WireGuard. It is time, therefore, for WireGuard to be properly integrated into Linux.

Ample information, including documentation, installation instructions, and project details, is available at:

* https://www.wireguard.com/
* https://www.wireguard.com/papers/wireguard.pdf

As it is currently an out-of-tree module, it lives in its own git repo and has its own mailing list, and every commit for the module is tested against every stable kernel since 3.10 on a variety of architectures using an extensive test suite:

* https://git.zx2c4.com/WireGuard
https://git.kernel.org/pub/scm/linux/kernel/git/zx2c4/WireGuard.git/
* https://lists.zx2c4.com/mailman/listinfo/wireguard
* https://www.wireguard.com/build-status/

The project has been broadly discussed at conferences, and was presented to the Netdev developers in Seoul last November, where a paper was released detailing some interesting aspects of the project. Dave asked me after the talk if I would consider sending in a v1 "sooner rather than later", hence this patchset. A decision is still waiting from the LPC network track committee, but an update on these topics may be presented in Vancouver in a few months. Presentations:

* https://www.wireguard.com/presentations/
* https://www.wireguard.com/papers/wireguard-netdev22.pdf

The cryptography in the protocol itself has been formally verified by several independent academic teams with positive results, and I know of two additional efforts on their way to further corroborate those findings. The version 1 protocol is "complete", and so the purpose of this review is to assess the implementation of the protocol. However, it still may be of interest to know that the thing you're reviewing uses a protocol with various nice security properties:

* https://www.wireguard.com/formal-verification/

This patchset is divided into three segments. The first introduces a very simple helper for working with the FPU state for the purposes of amortizing SIMD operations. The second segment is a small collection of cryptographic primitives, split up into several commits by primitive and by hardware. The third is WireGuard itself, presented as an unintrusive and self-contained virtual network driver.

It is intended that this entire patch series enter the kernel through DaveM's net-next tree.

Changes v1->v2:
- Zinc has been split into multiple patches.
- Zinc has been split into multiple config options.
- WireGuard has been wrapped at 80 characters.
- Numerous cleanups of inside WireGuard.
- Splitting out the simd helper into linux/.
- Simplification of bitwise arithmetic inside some Zinc primitives.
- Better handling of unaligned buffers inside Zinc.
- Expanded CC list.

Enjoy,
Jason

author maintainer
#1 lore raw

PATCH v2 1/17 asm: simd context helper API

Sometimes it's useful to amortize calls to XSAVE/XRSTOR and the related FPU/SIMD functions over a number of calls, because FPU restoration is quite expensive. This adds a simple header for carrying out this pattern:

    simd_context_t simd_context = simd_get();
    while ((item = get_item_from_queue()) != NULL) {
        encrypt_item(item, simd_context);
        simd_context = simd_relax(simd_context);
    }
    simd_put(simd_context);

The relaxation step ensures that we don't trample over preemption, and the get/put API should be a familiar paradigm in the kernel.

---
 arch/alpha/include/asm/Kbuild      |  5 ++--
 arch/arc/include/asm/Kbuild        |  1 +
 arch/arm/include/asm/simd.h        | 42 ++++++++++++++++++++++++++++++
 arch/arm64/include/asm/simd.h      | 37 +++++++++++++++++++++-----
 arch/c6x/include/asm/Kbuild        |  3 ++-
 arch/h8300/include/asm/Kbuild      |  3 ++-
 arch/hexagon/include/asm/Kbuild    |  1 +
 arch/ia64/include/asm/Kbuild       |  1 +
 arch/m68k/include/asm/Kbuild       |  1 +
 arch/microblaze/include/asm/Kbuild |  1 +
 arch/mips/include/asm/Kbuild       |  1 +
 arch/nds32/include/asm/Kbuild      |  7 ++---
 arch/nios2/include/asm/Kbuild      |  1 +
 arch/openrisc/include/asm/Kbuild   |  7 ++---
 arch/parisc/include/asm/Kbuild     |  1 +
 arch/powerpc/include/asm/Kbuild    |  3 ++-
 arch/riscv/include/asm/Kbuild      |  3 ++-
 arch/s390/include/asm/Kbuild       |  3 ++-
 arch/sh/include/asm/Kbuild         |  1 +
 arch/sparc/include/asm/Kbuild      |  1 +
 arch/um/include/asm/Kbuild         |  3 ++-
 arch/unicore32/include/asm/Kbuild  |  1 +
 arch/x86/include/asm/simd.h        | 30 ++++++++++++++++++++-
 arch/xtensa/include/asm/Kbuild     |  1 +
 include/asm-generic/simd.h         | 15 +++++++++++
 include/linux/simd.h               | 28 ++++++++++++++++++++
 26 files changed, 180 insertions(+), 21 deletions(-)
 create mode 100644 arch/arm/include/asm/simd.h
 create mode 100644 include/linux/simd.h
arch/alpha/include/asm/Kbuild +3 -2
index 0580cb8c84b2..07b2c1025d34 100644
@@ -2,14 +2,15 @@
 
 
 generic-y += compat.h
+generic-y += current.h
 generic-y += exec.h
 generic-y += export.h
 generic-y += fb.h
 generic-y += irq_work.h
+generic-y += kprobes.h
 generic-y += mcs_spinlock.h
 generic-y += mm-arch-hooks.h
 generic-y += preempt.h
 generic-y += sections.h
+generic-y += simd.h
 generic-y += trace_clock.h
-generic-y += current.h
-generic-y += kprobes.h
arch/arc/include/asm/Kbuild +1 -0
index feed50ce89fa..a7f4255f1649 100644
@@ -22,6 +22,7 @@ generic-y += parport.h
 generic-y += pci.h
 generic-y += percpu.h
 generic-y += preempt.h
+generic-y += simd.h
 generic-y += topology.h
 generic-y += trace_clock.h
 generic-y += user.h
arch/arm/include/asm/simd.h new file +42 -0

42 lines not on this page · raw message

arch/arm64/include/asm/simd.h +31 -6
index 6495cc51246f..058c336de38d 100644
@@ -1,11 +1,10 @@
-/*
- * Copyright (C) 2017 Linaro Ltd. <[email protected]>
+/* SPDX-License-Identifier: GPL-2.0
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published
- * by the Free Software Foundation.
+ * Copyright (C) 2017 Linaro Ltd. <[email protected]>
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  */
 
+#include <linux/simd.h>
 #ifndef __ASM_SIMD_H
 #define __ASM_SIMD_H
 
@@ -16,6 +15,8 @@
 #include <linux/types.h>
 
 #ifdef CONFIG_KERNEL_MODE_NEON
+#include <asm/neon.h>
+#include <asm/simd.h>
 
 DECLARE_PER_CPU(bool, kernel_neon_busy);
 
@@ -40,12 +41,36 @@ static __must_check inline bool may_use_simd(void)
 		!this_cpu_read(kernel_neon_busy);
 }
 
+static inline simd_context_t simd_get(void)
+{
+	bool have_simd = may_use_simd();
+	if (have_simd)
+		kernel_neon_begin();
+	return have_simd ? HAVE_FULL_SIMD : HAVE_NO_SIMD;
+}
+
+static inline void simd_put(simd_context_t prior_context)
+{
+	if (prior_context != HAVE_NO_SIMD)
+		kernel_neon_end();
+}
+
 #else /* ! CONFIG_KERNEL_MODE_NEON */
 
-static __must_check inline bool may_use_simd(void) {
+static __must_check inline bool may_use_simd(void)
+{
 	return false;
 }
 
+static inline simd_context_t simd_get(void)
+{
+	return HAVE_NO_SIMD;
+}
+
+static inline void simd_put(simd_context_t prior_context)
+{
+}
+
 #endif /* ! CONFIG_KERNEL_MODE_NEON */
 
 #endif
arch/c6x/include/asm/Kbuild +2 -1
index 33a2c94fed0d..22f3d8333c74 100644
@@ -5,8 +5,8 @@ generic-y += compat.h
 generic-y += current.h
 generic-y += device.h
 generic-y += div64.h
-generic-y += dma.h
 generic-y += dma-mapping.h
+generic-y += dma.h
 generic-y += emergency-restart.h
 generic-y += exec.h
 generic-y += extable.h
@@ -30,6 +30,7 @@ generic-y += pgalloc.h
 generic-y += preempt.h
 generic-y += segment.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += tlbflush.h
 generic-y += topology.h
 generic-y += trace_clock.h
arch/h8300/include/asm/Kbuild +2 -1
index a5d0b2991f47..f5c2f12d593e 100644
@@ -8,8 +8,8 @@ generic-y += current.h
 generic-y += delay.h
 generic-y += device.h
 generic-y += div64.h
-generic-y += dma.h
 generic-y += dma-mapping.h
+generic-y += dma.h
 generic-y += emergency-restart.h
 generic-y += exec.h
 generic-y += extable.h
@@ -39,6 +39,7 @@ generic-y += preempt.h
 generic-y += scatterlist.h
 generic-y += sections.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += sizes.h
 generic-y += spinlock.h
 generic-y += timex.h
arch/hexagon/include/asm/Kbuild +1 -0
index dd2fd9c0d292..217d4695fd8a 100644
@@ -29,6 +29,7 @@ generic-y += rwsem.h
 generic-y += sections.h
 generic-y += segment.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += sizes.h
 generic-y += topology.h
 generic-y += trace_clock.h
arch/ia64/include/asm/Kbuild +1 -0
index 557bbc8ba9f5..41c5ebdf79e5 100644
@@ -4,6 +4,7 @@ generic-y += irq_work.h
 generic-y += mcs_spinlock.h
 generic-y += mm-arch-hooks.h
 generic-y += preempt.h
+generic-y += simd.h
 generic-y += trace_clock.h
 generic-y += vtime.h
 generic-y += word-at-a-time.h
arch/m68k/include/asm/Kbuild +1 -0
index a4b8d3331a9e..73898dd1a4d0 100644
@@ -19,6 +19,7 @@ generic-y += mm-arch-hooks.h
 generic-y += percpu.h
 generic-y += preempt.h
 generic-y += sections.h
+generic-y += simd.h
 generic-y += spinlock.h
 generic-y += topology.h
 generic-y += trace_clock.h
arch/microblaze/include/asm/Kbuild +1 -0
index fe6a6c6e5003..9002fb24888c 100644
@@ -24,6 +24,7 @@ generic-y += parport.h
 generic-y += percpu.h
 generic-y += preempt.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += syscalls.h
 generic-y += topology.h
 generic-y += trace_clock.h
arch/mips/include/asm/Kbuild +1 -0
index 58351e48421e..e8868e0fb2c3 100644
@@ -16,6 +16,7 @@ generic-y += qrwlock.h
 generic-y += qspinlock.h
 generic-y += sections.h
 generic-y += segment.h
+generic-y += simd.h
 generic-y += trace_clock.h
 generic-y += unaligned.h
 generic-y += user.h
arch/nds32/include/asm/Kbuild +4 -3
index dbc4e5422550..603c1d020620 100644
@@ -7,14 +7,14 @@ generic-y += bug.h
 generic-y += bugs.h
 generic-y += checksum.h
 generic-y += clkdev.h
-generic-y += cmpxchg.h
 generic-y += cmpxchg-local.h
+generic-y += cmpxchg.h
 generic-y += compat.h
 generic-y += cputime.h
 generic-y += device.h
 generic-y += div64.h
-generic-y += dma.h
 generic-y += dma-mapping.h
+generic-y += dma.h
 generic-y += emergency-restart.h
 generic-y += errno.h
 generic-y += exec.h
@@ -46,14 +46,15 @@ generic-y += sections.h
 generic-y += segment.h
 generic-y += serial.h
 generic-y += shmbuf.h
+generic-y += simd.h
 generic-y += sizes.h
 generic-y += stat.h
 generic-y += switch_to.h
 generic-y += timex.h
 generic-y += topology.h
 generic-y += trace_clock.h
-generic-y += xor.h
 generic-y += unaligned.h
 generic-y += user.h
 generic-y += vga.h
 generic-y += word-at-a-time.h
+generic-y += xor.h
arch/nios2/include/asm/Kbuild +1 -0
index 8fde4fa2c34f..571a9d9ad107 100644
@@ -33,6 +33,7 @@ generic-y += preempt.h
 generic-y += sections.h
 generic-y += segment.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += spinlock.h
 generic-y += topology.h
 generic-y += trace_clock.h
arch/openrisc/include/asm/Kbuild +4 -3
index 65964d390b10..81a39e274f6f 100644
@@ -27,12 +27,13 @@ generic-y += module.h
 generic-y += pci.h
 generic-y += percpu.h
 generic-y += preempt.h
-generic-y += qspinlock_types.h
-generic-y += qspinlock.h
-generic-y += qrwlock_types.h
 generic-y += qrwlock.h
+generic-y += qrwlock_types.h
+generic-y += qspinlock.h
+generic-y += qspinlock_types.h
 generic-y += sections.h
 generic-y += segment.h
+generic-y += simd.h
 generic-y += string.h
 generic-y += switch_to.h
 generic-y += topology.h
arch/parisc/include/asm/Kbuild +1 -0
index 2013d639e735..97970b4d05ab 100644
@@ -17,6 +17,7 @@ generic-y += percpu.h
 generic-y += preempt.h
 generic-y += seccomp.h
 generic-y += segment.h
+generic-y += simd.h
 generic-y += topology.h
 generic-y += trace_clock.h
 generic-y += user.h
arch/powerpc/include/asm/Kbuild +2 -1
index 3196d227e351..64290f48e733 100644
@@ -4,7 +4,8 @@ generic-y += irq_regs.h
 generic-y += irq_work.h
 generic-y += local64.h
 generic-y += mcs_spinlock.h
+generic-y += msi.h
 generic-y += preempt.h
 generic-y += rwsem.h
+generic-y += simd.h
 generic-y += vtime.h
-generic-y += msi.h
arch/riscv/include/asm/Kbuild +2 -1
index 576ffdca06ba..8d3e7aef3234 100644
@@ -4,9 +4,9 @@ generic-y += checksum.h
 generic-y += cputime.h
 generic-y += device.h
 generic-y += div64.h
-generic-y += dma.h
 generic-y += dma-contiguous.h
 generic-y += dma-mapping.h
+generic-y += dma.h
 generic-y += emergency-restart.h
 generic-y += errno.h
 generic-y += exec.h
@@ -45,6 +45,7 @@ generic-y += setup.h
 generic-y += shmbuf.h
 generic-y += shmparam.h
 generic-y += signal.h
+generic-y += simd.h
 generic-y += socket.h
 generic-y += sockios.h
 generic-y += stat.h
arch/s390/include/asm/Kbuild +2 -1
index e3239772887a..7a26dc6ce815 100644
@@ -7,9 +7,9 @@ generated-y += unistd_nr.h
 generic-y += asm-offsets.h
 generic-y += cacheflush.h
 generic-y += device.h
+generic-y += div64.h
 generic-y += dma-contiguous.h
 generic-y += dma-mapping.h
-generic-y += div64.h
 generic-y += emergency-restart.h
 generic-y += export.h
 generic-y += fb.h
@@ -22,6 +22,7 @@ generic-y += mcs_spinlock.h
 generic-y += mm-arch-hooks.h
 generic-y += preempt.h
 generic-y += rwsem.h
+generic-y += simd.h
 generic-y += trace_clock.h
 generic-y += unaligned.h
 generic-y += word-at-a-time.h
arch/sh/include/asm/Kbuild +1 -0
index 6a5609a55965..8e64ff35a933 100644
@@ -16,6 +16,7 @@ generic-y += percpu.h
 generic-y += preempt.h
 generic-y += rwsem.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += sizes.h
 generic-y += trace_clock.h
 generic-y += xor.h
arch/sparc/include/asm/Kbuild +1 -0
index 410b263ef5c8..72b9e08fb350 100644
@@ -17,5 +17,6 @@ generic-y += msi.h
 generic-y += preempt.h
 generic-y += rwsem.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += trace_clock.h
 generic-y += word-at-a-time.h
arch/um/include/asm/Kbuild +2 -1
index b10dde6cb793..d37288b08dd2 100644
@@ -16,15 +16,16 @@ generic-y += io.h
 generic-y += irq_regs.h
 generic-y += irq_work.h
 generic-y += kdebug.h
+generic-y += kprobes.h
 generic-y += mcs_spinlock.h
 generic-y += mm-arch-hooks.h
 generic-y += param.h
 generic-y += pci.h
 generic-y += percpu.h
 generic-y += preempt.h
+generic-y += simd.h
 generic-y += switch_to.h
 generic-y += topology.h
 generic-y += trace_clock.h
 generic-y += word-at-a-time.h
 generic-y += xor.h
-generic-y += kprobes.h
arch/unicore32/include/asm/Kbuild +1 -0
index bfc7abe77905..98a908720bbd 100644
@@ -27,6 +27,7 @@ generic-y += preempt.h
 generic-y += sections.h
 generic-y += segment.h
 generic-y += serial.h
+generic-y += simd.h
 generic-y += sizes.h
 generic-y += syscalls.h
 generic-y += topology.h
arch/x86/include/asm/simd.h +29 -1
index a341c878e977..79411178988a 100644
@@ -1,4 +1,11 @@
-/* SPDX-License-Identifier: GPL-2.0 */
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <linux/simd.h>
+#ifndef _ASM_SIMD_H
+#define _ASM_SIMD_H
 
 #include <asm/fpu/api.h>
 
@@ -10,3 +17,24 @@ static __must_check inline bool may_use_simd(void)
 {
 	return irq_fpu_usable();
 }
+
+static inline simd_context_t simd_get(void)
+{
+	bool have_simd = false;
+#if !defined(CONFIG_UML)
+	have_simd = may_use_simd();
+	if (have_simd)
+		kernel_fpu_begin();
+#endif
+	return have_simd ? HAVE_FULL_SIMD : HAVE_NO_SIMD;
+}
+
+static inline void simd_put(simd_context_t prior_context)
+{
+#if !defined(CONFIG_UML)
+	if (prior_context != HAVE_NO_SIMD)
+		kernel_fpu_end();
+#endif
+}
+
+#endif /* _ASM_SIMD_H */
arch/xtensa/include/asm/Kbuild +1 -0
index e5e1e61c538c..e3b194a187f9 100644
@@ -23,6 +23,7 @@ generic-y += percpu.h
 generic-y += preempt.h
 generic-y += rwsem.h
 generic-y += sections.h
+generic-y += simd.h
 generic-y += topology.h
 generic-y += trace_clock.h
 generic-y += word-at-a-time.h
include/asm-generic/simd.h +15 -0
index d0343d58a74a..fad899a5a92d 100644
@@ -1,5 +1,9 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
+#include <linux/simd.h>
+#ifndef _ASM_SIMD_H
+#define _ASM_SIMD_H
+
 #include <linux/hardirq.h>
 
 /*
@@ -13,3 +17,14 @@ static __must_check inline bool may_use_simd(void)
 {
 	return !in_interrupt();
 }
+
+static inline simd_context_t simd_get(void)
+{
+	return HAVE_NO_SIMD;
+}
+
+static inline void simd_put(simd_context_t prior_context)
+{
+}
+
+#endif /* _ASM_SIMD_H */
include/linux/simd.h new file +28 -0
new file mode 100644
index 000000000000..f62d047188bf
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _SIMD_H
+#define _SIMD_H
+
+typedef enum {
+	HAVE_NO_SIMD,
+	HAVE_FULL_SIMD
+} simd_context_t;
+
+#include <linux/sched.h>
+#include <asm/simd.h>
+
+static inline simd_context_t simd_relax(simd_context_t prior_context)
+{
+#ifdef CONFIG_PREEMPT
+	if (prior_context != HAVE_NO_SIMD && need_resched()) {
+		simd_put(prior_context);
+		return simd_get();
+	}
+#endif
+	return prior_context;
+}
+
+#endif /* _SIMD_H */
Signature · 1 line
2.18.0
#2 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 10 lines · Jason A. Donenfeld

Sometimes it's useful to amortize calls to XSAVE/XRSTOR and the related FPU/SIMD functions over a number of calls, because FPU restoration is quite expensive. This adds a simple header for carrying out this pattern:

    simd_context_t simd_context = simd_get();
    while ((item = get_item_from_queue()) != NULL) {
        encrypt_item(item, simd_context);
        simd_context = simd_relax(simd_context);
    }
    simd_put(simd_context);

I'm not too fond of this simply because it requires that relax() step in all code pathes. I'd rather make that completely transparent by just marking the task as FPU using and let the context switch code deal with it in case that it gets preempted. I'll let one of my engineers look into that next week.

Thanks,

tglx

author maintainer
#3 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Hey Thomas,

Quoted 5 lines · Thomas Gleixner

I'm not too fond of this simply because it requires that relax() step in all code pathes. I'd rather make that completely transparent by just marking the task as FPU using and let the context switch code deal with it in case that it gets preempted. I'll let one of my engineers look into that next week.

Do you mean to say you intend to make kernel_fpu_end() and kernel_neon_end() only actually do something upon context switch, but not when it's actually called? So that multiple calls to kernel_fpu_begin() and kernel_neon_begin() can be made without penalty? If so, that'd be great, and I'd certainly prefer this to the simd_context_t passing. I consider the simd_get/put/relax API a stopgap measure until something like that is implemented.

Jason

#4 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Jason,

Quoted 7 lines · Jason A. Donenfeld
Quoted 5 lines · Thomas Gleixner

I'm not too fond of this simply because it requires that relax() step in all code pathes. I'd rather make that completely transparent by just marking the task as FPU using and let the context switch code deal with it in case that it gets preempted. I'll let one of my engineers look into that next week.

Quoted 5 lines

Do you mean to say you intend to make kernel_fpu_end() and kernel_neon_end() only actually do something upon context switch, but not when it's actually called? So that multiple calls to kernel_fpu_begin() and kernel_neon_begin() can be made without penalty?

On context switch and exit to user. That allows to keep those code pathes fully preemptible. Still twisting my brain around the details.

Quoted 3 lines

If so, that'd be great, and I'd certainly prefer this to the simd_context_t passing. I consider the simd_get/put/relax API a stopgap measure until something like that is implemented.

I really want to avoid this stopgap^Wducttape thing.

Thanks,

tglx

author maintainer
#5 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 8 lines · Thomas Gleixner
Quoted 5 lines

Do you mean to say you intend to make kernel_fpu_end() and kernel_neon_end() only actually do something upon context switch, but not when it's actually called? So that multiple calls to kernel_fpu_begin() and kernel_neon_begin() can be made without penalty?

On context switch and exit to user. That allows to keep those code pathes fully preemptible. Still twisting my brain around the details.

Just to make sure we're on the same page, the goal is so that this code:

kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
...

has the same performance as this code:

kernel_fpu_begin();
kernel_fpu_end();

(Unless of course the process is preempted or the like.)

Currently the present situation makes the performance of the above wildly different, since kernel_fpu_end() does something immediately.

What about something like this:
- Add a tristate flag connected to task_struct (or in the global fpu struct in the case that this happens in irq and there isn't a valid current).
- On kernel_fpu_begin(), if the flag is 0, do the usual expensive XSAVE stuff, and set the flag to 1.
- On kernel_fpu_begin(), if the flag is non-0, just set the flag to 1 and return.
- On kernel_fpu_end(), if the flag is non-0, set the flag to 2. (Otherwise WARN() or BUG() or something.)
- On context switch / preemption / etc away from the task, if the flag is non-0, XRSTOR and such.
- On context switch / preemption / etc back to the task, if the flag is 1, XSAVE and such. If the flag is 2, set it to 0.

Jason

#6 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 41 lines · Jason A. Donenfeld
Quoted 8 lines · Thomas Gleixner
Quoted 5 lines

Do you mean to say you intend to make kernel_fpu_end() and kernel_neon_end() only actually do something upon context switch, but not when it's actually called? So that multiple calls to kernel_fpu_begin() and kernel_neon_begin() can be made without penalty?

On context switch and exit to user. That allows to keep those code pathes fully preemptible. Still twisting my brain around the details.

Just to make sure we're on the same page, the goal is so that this code:

kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
kernel_fpu_begin();
kernel_fpu_end();
...

has the same performance as this code:

kernel_fpu_begin();
kernel_fpu_end();

(Unless of course the process is preempted or the like.)

Currently the present situation makes the performance of the above wildly different, since kernel_fpu_end() does something immediately.

Quoted 12 lines

What about something like this:
- Add a tristate flag connected to task_struct (or in the global fpu struct in the case that this happens in irq and there isn't a valid current).
- On kernel_fpu_begin(), if the flag is 0, do the usual expensive XSAVE stuff, and set the flag to 1.
- On kernel_fpu_begin(), if the flag is non-0, just set the flag to 1 and return.
- On kernel_fpu_end(), if the flag is non-0, set the flag to 2. (Otherwise WARN() or BUG() or something.)
- On context switch / preemption / etc away from the task, if the flag is non-0, XRSTOR and such.

It’s not that simple. First, these states need names, at least for thinking about. 0 is “user state in regs”. 1 is “kernel state active”. 2 is “nothing active”.

The actual encoding will be something like TIF_XSTATE_UNLOADED: user state is not in regs. TIF_KERNEL_XSTATE: kernel is using FPU. And this fundamentally doubles the size of struct fpu.

Tglx, that doubling-the-size-of-fpu makes me question the idea of letting the kernel use the fpu while preemptible.

Quoted 7 lines

- On context switch / preemption / etc back to the task, if the flag is 1, XSAVE and such. If the flag is 2, set it to 0.

Jason

#7 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

In reply to Thomas Gleixner
Quoted 14 lines · Thomas Gleixner

Jason,

Quoted 8 lines · Jason A. Donenfeld
Quoted 6 lines · Thomas Gleixner

I'm not too fond of this simply because it requires that relax() step in all code pathes. I'd rather make that completely transparent by just marking the task as FPU using and let the context switch code deal with it in case that it gets preempted. I'll let one of my engineers look into that next week.

Quoted 8 lines
Quoted 5 lines

Do you mean to say you intend to make kernel_fpu_end() and kernel_neon_end() only actually do something upon context switch, but not when it's actually called? So that multiple calls to kernel_fpu_begin() and kernel_neon_begin() can be made without penalty?

On context switch and exit to user. That allows to keep those code pathes fully preemptible. Still twisting my brain around the details.

I think you’ll have to treat exit to user and context switch as different things. For exit to user, we want to restore the *user* state, but, for context switch, we’ll need to restore *kernel* state.

Do user first as its own patch set. It’ll be less painful that way.

And someone needs to rework PKRU for this to make sense. See previous threads.

#8 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 25 lines · Andy Lutomirski
Quoted 25 lines

On Aug 26, 2018, at 7:06 AM, Thomas Gleixner <[email protected]> wrote:

Jason,

Quoted 19 lines · Jason A. Donenfeld
Quoted 11 lines

On Sun, Aug 26, 2018 at 6:10 AM Thomas Gleixner < [email protected]> wrote:
I'm not too fond of this simply because it requires that relax() step in
all code pathes. I'd rather make that completely transparent by just
marking the task as FPU using and let the context switch code deal with it
in case that it gets preempted. I'll let one of my engineers look into
that next week.

Do you mean to say you intend to make kernel_fpu_end() and kernel_neon_end() only actually do something upon context switch, but
not when it's actually called? So that multiple calls to kernel_fpu_begin() and kernel_neon_begin() can be made without penalty?

Quoted 7 lines
Quoted 3 lines

On context switch and exit to user. That allows to keep those code pathes
fully preemptible. Still twisting my brain around the details.

I think you’ll have to treat exit to user and context switch as different things. For exit to user, we want to restore the *user* state, but, for context switch, we’ll need to restore *kernel* state.

For non-preemptible kernel_fpu_begin/end (which seems like a good starting point since since it gets the code halfway to where Thomas would like it to go), the rules would be a little simpler:

- For exit to userspace, restore the user FPU state.
- At kernel_fpu_begin(), save the user FPU state (if still loaded).
- At context switch time, save the user FPU state (if still loaded).

Quoted 4 lines

Do user first as its own patch set. It’ll be less painful that way.

And someone needs to rework PKRU for this to make sense. See previous threads.

I sent Thomas the patches I worked on in the past.

That series is likely incomplete, but should be a
reasonable starting point.

Signature · 1 line
All Rights Reversed.
author maintainer
#9 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

In reply to Thomas Gleixner

Hey Thomas,

I'd like to move ahead with my patchset and make some forward progress in LKML submission. If you've got something brewing regarding the FPU context on x86 and ARM, I'm happy to wait a bit longer so as to build on that. But if that is instead a far-off theoretical eventual thing, perhaps it's better for me to move ahead as planned, and we can switch to the superior FPU semantics whenever you get around to it? Either way, please let me know what you have in mind so our plans can stay somewhat sync'd.

Talk soon,
Jason

#10 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 10 lines · Jason A. Donenfeld

Hey Thomas,

I'd like to move ahead with my patchset and make some forward progress in LKML submission. If you've got something brewing regarding the FPU context on x86 and ARM, I'm happy to wait a bit longer so as to build on that. But if that is instead a far-off theoretical eventual thing, perhaps it's better for me to move ahead as planned, and we can switch to the superior FPU semantics whenever you get around to it? Either way, please let me know what you have in mind so our plans can stay somewhat sync'd.

I tend to think the right approach is to merge Jason's code and then make it better later. Even with a totally perfect lazy FPU restore implementation on x86, we'll probably still need some way of dealing with SIMD contexts. I think we're highly unlikely to ever a allow SIMD usage in all NMI contexts, for example, and there will always be cases where we specifically don't want to use all available SIMD capabilities even if we can. For example, generating random numbers does crypto, but we probably don't want to do *SIMD* crypto, since that will force a save and restore and will probably fire up the AVX512 unit, and that's not worth it unless we're already using it for some other reason.

Also, as Rik has discovered, lazy FPU restore is conceptually straightforward but isn't entirely trivial :)

--Andy

author maintainer
#11 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 12 lines · Andy Lutomirski

I tend to think the right approach is to merge Jason's code and then make it better later. Even with a totally perfect lazy FPU restore implementation on x86, we'll probably still need some way of dealing with SIMD contexts. I think we're highly unlikely to ever a allow SIMD usage in all NMI contexts, for example, and there will always be cases where we specifically don't want to use all available SIMD capabilities even if we can. For example, generating random numbers does crypto, but we probably don't want to do *SIMD* crypto, since that will force a save and restore and will probably fire up the AVX512 unit, and that's not worth it unless we're already using it for some other reason.

Quoted 2 lines

Also, as Rik has discovered, lazy FPU restore is conceptually straightforward but isn't entirely trivial :)

Sounds good. I'll move ahead on this basis.

#12 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Quoted 13 lines · Jason A. Donenfeld
Quoted 12 lines · Andy Lutomirski

I tend to think the right approach is to merge Jason's code and then make it better later. Even with a totally perfect lazy FPU restore implementation on x86, we'll probably still need some way of dealing with SIMD contexts. I think we're highly unlikely to ever a allow SIMD usage in all NMI contexts, for example, and there will always be cases where we specifically don't want to use all available SIMD capabilities even if we can. For example, generating random numbers does crypto, but we probably don't want to do *SIMD* crypto, since that will force a save and restore and will probably fire up the AVX512 unit, and that's not worth it unless we're already using it for some other reason.

Quoted 4 lines
Quoted 2 lines

Also, as Rik has discovered, lazy FPU restore is conceptually straightforward but isn't entirely trivial :)

Sounds good. I'll move ahead on this basis.

Fine with me.

author maintainer
#13 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

Hi Thomas,

Quoted 15 lines · Thomas Gleixner
Quoted 13 lines · Jason A. Donenfeld
Quoted 12 lines · Andy Lutomirski

I tend to think the right approach is to merge Jason's code and then make it better later. Even with a totally perfect lazy FPU restore implementation on x86, we'll probably still need some way of dealing with SIMD contexts. I think we're highly unlikely to ever a allow SIMD usage in all NMI contexts, for example, and there will always be cases where we specifically don't want to use all available SIMD capabilities even if we can. For example, generating random numbers does crypto, but we probably don't want to do *SIMD* crypto, since that will force a save and restore and will probably fire up the AVX512 unit, and that's not worth it unless we're already using it for some other reason.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Also, as Rik has discovered, lazy FPU restore is conceptually straightforward but isn't entirely trivial :)

Sounds good. I'll move ahead on this basis.

Fine with me.

Do you want to pull this single patch [01/17] into your tree now, and then when I submit v3 of WireGuard and such, I can just drop this patch from it, and then the rest will enter like usual networking stuff through Dave's tree?

Jason

Reviewed-by on v2 1/17
#14 lore raw

Re: [PATCH v2 01/17] asm: simd context helper API

In reply to Jason A. Donenfeld
Quoted 49 lines · On Fri, 24 Aug 2018 14:38:33 PDT (-0700), [email protected] wrote:

Sometimes it's useful to amortize calls to XSAVE/XRSTOR and the related FPU/SIMD functions over a number of calls, because FPU restoration is quite expensive. This adds a simple header for carrying out this pattern:

    simd_context_t simd_context = simd_get();
    while ((item = get_item_from_queue()) != NULL) {
        encrypt_item(item, simd_context);
        simd_context = simd_relax(simd_context);
    }
    simd_put(simd_context);

The relaxation step ensures that we don't trample over preemption, and the get/put API should be a familiar paradigm in the kernel.

---
 arch/alpha/include/asm/Kbuild      |  5 ++--
 arch/arc/include/asm/Kbuild        |  1 +
 arch/arm/include/asm/simd.h        | 42 ++++++++++++++++++++++++++++++
 arch/arm64/include/asm/simd.h      | 37 +++++++++++++++++++++-----
 arch/c6x/include/asm/Kbuild        |  3 ++-
 arch/h8300/include/asm/Kbuild      |  3 ++-
 arch/hexagon/include/asm/Kbuild    |  1 +
 arch/ia64/include/asm/Kbuild       |  1 +
 arch/m68k/include/asm/Kbuild       |  1 +
 arch/microblaze/include/asm/Kbuild |  1 +
 arch/mips/include/asm/Kbuild       |  1 +
 arch/nds32/include/asm/Kbuild      |  7 ++---
 arch/nios2/include/asm/Kbuild      |  1 +
 arch/openrisc/include/asm/Kbuild   |  7 ++---
 arch/parisc/include/asm/Kbuild     |  1 +
 arch/powerpc/include/asm/Kbuild    |  3 ++-
 arch/riscv/include/asm/Kbuild      |  3 ++-
 arch/s390/include/asm/Kbuild       |  3 ++-
 arch/sh/include/asm/Kbuild         |  1 +
 arch/sparc/include/asm/Kbuild      |  1 +
 arch/um/include/asm/Kbuild         |  3 ++-
 arch/unicore32/include/asm/Kbuild  |  1 +
 arch/x86/include/asm/simd.h        | 30 ++++++++++++++++++++-
 arch/xtensa/include/asm/Kbuild     |  1 +
 include/asm-generic/simd.h         | 15 +++++++++++
 include/linux/simd.h               | 28 ++++++++++++++++++++
 26 files changed, 180 insertions(+), 21 deletions(-)
 create mode 100644 arch/arm/include/asm/simd.h
 create mode 100644 include/linux/simd.h

...

Quoted 5 lines
arch/riscv/include/asm/Kbuild +0 -0
index 576ffdca06ba..8d3e7aef3234 100644

arch/riscv/include/asm/Kbuild generic-y += checksum.h · line 12

Quoted 10 lines
arch/riscv/include/asm/Kbuild +1 -1
@@ -4,9 +4,9 @@ generic-y += checksum.h
 generic-y += cputime.h
 generic-y += device.h
 generic-y += div64.h
-generic-y += dma.h
 generic-y += dma-contiguous.h
 generic-y += dma-mapping.h
+generic-y += dma.h
 generic-y += emergency-restart.h
 generic-y += errno.h
 generic-y += exec.h

If this is the canonical ordering and doing so makes your life easier then I'm OK taking this as a separate patch into the RISC-V tree, but if not then feel free to roll something like this up into your next patch set.

arch/riscv/include/asm/Kbuild generic-y += setup.h · line 51

Quoted 8 lines
arch/riscv/include/asm/Kbuild +1 -0
@@ -45,6 +45,7 @@ generic-y += setup.h
 generic-y += shmbuf.h
 generic-y += shmparam.h
 generic-y += signal.h
+generic-y += simd.h
 generic-y += socket.h
 generic-y += sockios.h
 generic-y += stat.h

Either way, this looks fine for as far as the RISC-V stuff goes as it's pretty much a NOP. As long as it stays a NOP then feel free to add a

as far as the RISC-V parts are conceded. It looks like there's a lot of other issues, though, so it's not much of a review :)

author maintainer
#15 lore raw

PATCH v2 2/17 zinc: introduce minimal cryptography library

In reply to Jason A. Donenfeld

Zinc stands for "Zinc Is Neat Crypto" or "Zinc as IN Crypto" or maybe just "Zx2c4's INsane Cryptolib." It's also short, easy to type, and plays nicely with the recent trend of naming crypto libraries after elements. The guiding principle is "don't overdo it". It's less of a library and more of a directory tree for organizing well-curated direct implementations of cryptography primitives.

Zinc is a new cryptography API that is much more minimal and lower-level than the current one. It intends to complement it and provide a basis upon which the current crypto API might build, as the provider of software implementations of cryptographic primitives. It is motivated by three primary observations in crypto API design:

* Highly composable "cipher modes" and related abstractions from 90s cryptographers did not turn out to be as terrific an idea as hoped, leading to a host of API misuse problems.

* Most programmers are afraid of crypto code, and so prefer to integrate it into libraries in a highly abstracted manner, so as to shield themselves from implementation details. Cryptographers, on the other hand, prefer simple direct implementations, which they're able to verify for high assurance and optimize in accordance with their expertise.

* Overly abstracted and flexible cryptography APIs lead to a host of dangerous problems and performance issues. The kernel is in the business usually not of coming up with new uses of crypto, but rather implementing various constructions, which means it essentially needs a library of primitives, not a highly abstracted enterprise-ready pluggable system, with a few particular exceptions.

This last observation has seen itself play out several times over and over again within the kernel:

* The perennial move of actual primitives away from crypto/ and into lib/, so that users can actually call these functions directly with no overhead and without lots of allocations, function pointers, string specifier parsing, and general clunkiness. For example:
sha256, chacha20, siphash, sha1, and so forth live in lib/ rather than in crypto/. Zinc intends to stop the cluttering of lib/ and introduce these direct primitives into their proper place, lib/zinc/.

* An abundance of misuse bugs with the present crypto API that have been very unpleasant to clean up.

* A hesitance to even use cryptography, because of the overhead and headaches involved in accessing the routines.

Zinc goes in a rather different direction. Rather than providing a thoroughly designed and abstracted API, Zinc gives you simple functions, which implement some primitive, or some particular and specific construction of primitives. It is not dynamic in the least, though one could imagine implementing a complex dynamic dispatch mechanism (such as the current crypto API) on top of these basic functions. After all, dynamic dispatch is usually needed for applications with cipher agility, such as IPsec, dm-crypt, AF_ALG, and so forth, and the existing crypto API will continue to play that role. However, Zinc will provide a non- haphazard way of directly utilizing crypto routines in applications that do have neither the need nor desire for abstraction and dynamic dispatch.

It also organizes the implementations in a simple, straight-forward, and direct manner, making it enjoyable and intuitive to work on. Rather than moving optimized assembly implementations into arch/, it keeps them all together in lib/zinc/, making it simple and obvious to compare and contrast what's happening. This is, notably, exactly what the lib/raid6/ tree does, and that seems to work out rather well. It's also the pattern of most successful crypto libraries. The architecture- specific glue-code is made a part of each translation unit, rather than being in a separate one, so that generic and architecture-optimized code are combined at compile-time, and incompatibility branches compiled out by the optimizer.

All implementations have been extensively tested and fuzzed, and are selected for their quality, trustworthiness, and performance. Wherever possible and performant, formally verified implementations are used, such as those from HACL* [1] and Fiat-Crypto [2]. The routines also take special care to zero out secrets using memzero_explicit (and future work is planned to have gcc do this more reliably and performantly with compiler plugins). The performance of the selected implementations is state-of-the-art and unrivaled on a broad array of hardware, though of course we will continue to fine tune these to the hardware demands needed by kernel contributors. Each implementation also comes with extensive self-tests and crafted test vectors, pulled from various places such as Wycheproof [9].

Regularity of function signatures is important, so that users can easily "guess" the name of the function they want. Though, individual primitives are oftentimes not trivially interchangeable, having been designed for different things and requiring different parameters and semantics, and so the function signatures they provide will directly reflect the realities of the primitives' usages, rather than hiding it behind (inevitably leaky) abstractions. Also, in contrast to the current crypto API, Zinc functions can work on stack buffers, and can be called with different keys, without requiring allocations or locking.

SIMD is used automatically when available, though some routines may benefit from either having their SIMD disabled for particular invocations, or to have the SIMD initialization calls amortized over several invocations of the function, and so Zinc utilizes function signatures enabling that in conjunction with the recently introduced simd_context_t.

More generally, Zinc provides function signatures that allow just what is required by the various callers. This isn't to say that users of the functions will be permitted to pollute the function semantics with weird particular needs, but we are trying very hard not to overdo it, and that means looking carefully at what's actually necessary, and doing just that, and not much more than that. Remember: practicality and cleanliness rather than over-zealous infrastructure.

Zinc provides also an opening for the best implementers in academia to contribute their time and effort to the kernel, by being sufficiently simple and inviting. In discussing this commit with some of the best and brightest over the last few years, there are many who are eager to devote rare talent and energy to this effort.

Following the merging of this, I expect for the primitives that currently exist in lib/ to work their way into lib/zinc/, after intense scrutiny of each implementation, potentially replacing them with either formally-verified implementations, or better studied and faster state-of-the-art implementations.

Also following the merging of this, I expect for the old crypto API implementations to be ported over to use Zinc for their software-based implementations.

As Zinc is simply library code, its config options are un-menued, with the exception of CONFIG_ZINC_DEBUG, which enables various selftests and BUG_ONs.

[1] https://github.com/project-everest/hacl-star
[2] https://github.com/mit-plv/fiat-crypto
[3] https://cr.yp.to/ecdh.html
[4] https://cr.yp.to/chacha.html
[5] https://cr.yp.to/snuffle/xsalsa-20081128.pdf
[6] https://cr.yp.to/mac.html
[7] https://blake2.net/
[8] https://tools.ietf.org/html/rfc8439
[9] https://github.com/google/wycheproof

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 MAINTAINERS       |  8 ++++++++
 lib/Kconfig       |  2 ++
 lib/Makefile      |  2 ++
 lib/zinc/Kconfig  | 20 ++++++++++++++++++++
 lib/zinc/Makefile |  7 +++++++
 lib/zinc/main.c   | 31 +++++++++++++++++++++++++++++++
 6 files changed, 70 insertions(+)
 create mode 100644 lib/zinc/Kconfig
 create mode 100644 lib/zinc/Makefile
 create mode 100644 lib/zinc/main.c
MAINTAINERS +8 -0
index 955463f8d518..f194eda86011 100644
@@ -16103,6 +16103,14 @@ Q:	https://patchwork.linuxtv.org/project/linux-media/list/
 S:	Maintained
 F:	drivers/media/dvb-frontends/zd1301_demod*
 
+ZINC CRYPTOGRAPHY LIBRARY
+M:	Jason A. Donenfeld <[email protected]>
+M:	Samuel Neves <[email protected]>
+S:	Maintained
+F:	lib/zinc/
+F:	include/zinc/
+L:	[email protected]
+
 ZPOOL COMPRESSED PAGE STORAGE API
 M:	Dan Streetman <[email protected]>
 L:	[email protected]
lib/Kconfig +2 -0
index 706836ec314d..7ea5437e9f7d 100644
@@ -478,6 +478,8 @@ config GLOB_SELFTEST
 	  module load) by a small amount, so you're welcome to play with
 	  it, but you probably don't need it.
 
+source "lib/zinc/Kconfig"
+
 #
 # Netlink attribute parsing support is select'ed if needed
 #
lib/Makefile +2 -0
index d95bb2525101..ee41151cba7a 100644
@@ -212,6 +212,8 @@ obj-$(CONFIG_PERCPU_TEST) += percpu_test.o
 
 obj-$(CONFIG_ASN1) += asn1_decoder.o
 
+obj-$(CONFIG_ZINC) += zinc/
+
 obj-$(CONFIG_FONT_SUPPORT) += fonts/
 
 obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
lib/zinc/Kconfig new file +20 -0
new file mode 100644
index 000000000000..aa4f8d449d6b
@@ -0,0 +1,20 @@
+config ZINC
+	tristate
+	select CRYPTO_BLKCIPHER
+	select VFP
+	select VFPv3
+	select NEON
+	select KERNEL_MODE_NEON
+
+config ZINC_DEBUG
+	bool "Zinc cryptography library debugging and self-tests"
+	depends on ZINC
+	help
+	  This builds a series of self-tests for the Zinc crypto library, which
+	  help diagnose any cryptographic algorithm implementation issues that
+	  might be at the root cause of potential bugs. It also adds various
+	  debugging traps.
+
+	  Unless you're developing and testing cryptographic routines, or are
+	  especially paranoid about correctness on your hardware, you may say
+	  N here.
lib/zinc/Makefile new file +7 -0
new file mode 100644
index 000000000000..8e30115217db
@@ -0,0 +1,7 @@
+ccflags-y := -O3
+ccflags-y += -Wframe-larger-than=8192
+ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
+
+zinc-y += main.o
+
+obj-$(CONFIG_ZINC) := zinc.o
lib/zinc/main.c new file +31 -0
new file mode 100644
index 000000000000..590872563955
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+
+#ifdef CONFIG_ZINC_DEBUG
+#define selftest(which) do { \
+	if (!which ## _selftest()) \
+		return -ENOTRECOVERABLE; \
+} while (0)
+#else
+#define selftest(which)
+#endif
+
+static int __init mod_init(void)
+{
+	return 0;
+}
+
+static void __exit mod_exit(void)
+{
+}
+
+module_init(mod_init);
+module_exit(mod_exit);
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Zinc cryptography library");
+MODULE_AUTHOR("Jason A. Donenfeld <[email protected]>");
Signature · 1 line
2.18.0
#16 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

Hi Jason,

Quoted 141 lines · Jason A. Donenfeld

Zinc stands for "Zinc Is Neat Crypto" or "Zinc as IN Crypto" or maybe just "Zx2c4's INsane Cryptolib." It's also short, easy to type, and plays nicely with the recent trend of naming crypto libraries after elements. The guiding principle is "don't overdo it". It's less of a library and more of a directory tree for organizing well-curated direct implementations of cryptography primitives.

Zinc is a new cryptography API that is much more minimal and lower-level than the current one. It intends to complement it and provide a basis upon which the current crypto API might build, as the provider of software implementations of cryptographic primitives. It is motivated by three primary observations in crypto API design:

* Highly composable "cipher modes" and related abstractions from 90s cryptographers did not turn out to be as terrific an idea as hoped, leading to a host of API misuse problems.

* Most programmers are afraid of crypto code, and so prefer to integrate it into libraries in a highly abstracted manner, so as to shield themselves from implementation details. Cryptographers, on the other hand, prefer simple direct implementations, which they're able to verify for high assurance and optimize in accordance with their expertise.

* Overly abstracted and flexible cryptography APIs lead to a host of dangerous problems and performance issues. The kernel is in the business usually not of coming up with new uses of crypto, but rather implementing various constructions, which means it essentially needs a library of primitives, not a highly abstracted enterprise-ready pluggable system, with a few particular exceptions.

This last observation has seen itself play out several times over and over again within the kernel:

* The perennial move of actual primitives away from crypto/ and into lib/, so that users can actually call these functions directly with no overhead and without lots of allocations, function pointers, string specifier parsing, and general clunkiness. For example:
sha256, chacha20, siphash, sha1, and so forth live in lib/ rather than in crypto/. Zinc intends to stop the cluttering of lib/ and introduce these direct primitives into their proper place, lib/zinc/.

* An abundance of misuse bugs with the present crypto API that have been very unpleasant to clean up.

* A hesitance to even use cryptography, because of the overhead and headaches involved in accessing the routines.

Zinc goes in a rather different direction. Rather than providing a thoroughly designed and abstracted API, Zinc gives you simple functions, which implement some primitive, or some particular and specific construction of primitives. It is not dynamic in the least, though one could imagine implementing a complex dynamic dispatch mechanism (such as the current crypto API) on top of these basic functions. After all, dynamic dispatch is usually needed for applications with cipher agility, such as IPsec, dm-crypt, AF_ALG, and so forth, and the existing crypto API will continue to play that role. However, Zinc will provide a non- haphazard way of directly utilizing crypto routines in applications that do have neither the need nor desire for abstraction and dynamic dispatch.

It also organizes the implementations in a simple, straight-forward, and direct manner, making it enjoyable and intuitive to work on. Rather than moving optimized assembly implementations into arch/, it keeps them all together in lib/zinc/, making it simple and obvious to compare and contrast what's happening. This is, notably, exactly what the lib/raid6/ tree does, and that seems to work out rather well. It's also the pattern of most successful crypto libraries. The architecture- specific glue-code is made a part of each translation unit, rather than being in a separate one, so that generic and architecture-optimized code are combined at compile-time, and incompatibility branches compiled out by the optimizer.

All implementations have been extensively tested and fuzzed, and are selected for their quality, trustworthiness, and performance. Wherever possible and performant, formally verified implementations are used, such as those from HACL* [1] and Fiat-Crypto [2]. The routines also take special care to zero out secrets using memzero_explicit (and future work is planned to have gcc do this more reliably and performantly with compiler plugins). The performance of the selected implementations is state-of-the-art and unrivaled on a broad array of hardware, though of course we will continue to fine tune these to the hardware demands needed by kernel contributors. Each implementation also comes with extensive self-tests and crafted test vectors, pulled from various places such as Wycheproof [9].

Regularity of function signatures is important, so that users can easily "guess" the name of the function they want. Though, individual primitives are oftentimes not trivially interchangeable, having been designed for different things and requiring different parameters and semantics, and so the function signatures they provide will directly reflect the realities of the primitives' usages, rather than hiding it behind (inevitably leaky) abstractions. Also, in contrast to the current crypto API, Zinc functions can work on stack buffers, and can be called with different keys, without requiring allocations or locking.

SIMD is used automatically when available, though some routines may benefit from either having their SIMD disabled for particular invocations, or to have the SIMD initialization calls amortized over several invocations of the function, and so Zinc utilizes function signatures enabling that in conjunction with the recently introduced simd_context_t.

More generally, Zinc provides function signatures that allow just what is required by the various callers. This isn't to say that users of the functions will be permitted to pollute the function semantics with weird particular needs, but we are trying very hard not to overdo it, and that means looking carefully at what's actually necessary, and doing just that, and not much more than that. Remember: practicality and cleanliness rather than over-zealous infrastructure.

Zinc provides also an opening for the best implementers in academia to contribute their time and effort to the kernel, by being sufficiently simple and inviting. In discussing this commit with some of the best and brightest over the last few years, there are many who are eager to devote rare talent and energy to this effort.

Following the merging of this, I expect for the primitives that currently exist in lib/ to work their way into lib/zinc/, after intense scrutiny of each implementation, potentially replacing them with either formally-verified implementations, or better studied and faster state-of-the-art implementations.

Also following the merging of this, I expect for the old crypto API implementations to be ported over to use Zinc for their software-based implementations.

As Zinc is simply library code, its config options are un-menued, with the exception of CONFIG_ZINC_DEBUG, which enables various selftests and BUG_ONs.

[1] https://github.com/project-everest/hacl-star
[2] https://github.com/mit-plv/fiat-crypto
[3] https://cr.yp.to/ecdh.html
[4] https://cr.yp.to/chacha.html
[5] https://cr.yp.to/snuffle/xsalsa-20081128.pdf
[6] https://cr.yp.to/mac.html
[7] https://blake2.net/
[8] https://tools.ietf.org/html/rfc8439
[9] https://github.com/google/wycheproof

Quoted 6 lines
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>

I think the crypto portion of the patchset is looking *slightly* better from v1. (Thanks for getting rid of the #ifdef mazes!) But here are some more comments, with the caveat that I haven't really reviewed any actual implementations yet, and it's a *lot* of new code so this is really just scratching the surface...:

I thought you were going to wrap lines at 80 characters? It's hard to read the extremely long lines, and they encourage deep nesting.

As I said before, I still think you need to switch the crypto API ChaCha20 and Poly1305 over to use the new implementations. It's not okay to have two completely different sets of ChaCha20 and Poly1305 implementations just because you want a different API, so you might as well get started on it... The thing is that before you try it, it's not clear what problems will come up that require changes to the design. So, this really ought to be addressed up-front.

It's also not clearly explained whether/why/how the new ChaCha20 and Poly1305 implementations are better than the existing ones. The patch adding the ARM and ARM64 ChaCha, for example, just says who wrote them, with no mention of why the particular implementations were chosen.

You've also documented a lot of stuff in commit messages which will be lost as it isn't being added to the source itself. There are various examples of this, such as information about where the various implementations came from, what you changed, why a particular implementation isn't used on Skylake or whatever, etc. Can you please make sure that any important information is in comments, e.g. at the top of the files? There maybe should even be a Documentation/ file for "Zinc", rather than only a long commit message.

I still think the "Zinc" name is confusing and misleading, and people are going to forget that it means "crypto". lib/crypto/ would be more intuitive. But I don't care *that* much myself, and you should see what others think...

It seems you still don't explicitly clarify anywhere in the source itself that the copyright holders of the code from OpenSSL have relicensed it under GPLv2. I only see a GPLv2 license slapped on the files, yet no such license is presence in the OpenSSL originals, at least in the one I checked. If you did receive explicit permission, then you should include an explicit clarification in each file like the one in arch/arm/crypto/sha1-armv4-large.S. Otherwise people will be confused and come asking about the license status.

As Ard and I discussed recently on my patch
"crypto: arm/poly1305 - add NEON accelerated Poly1305 implementation" which proposed adding the exact same poly1305-arm.S file, for all the OpenSSL assembly it would probably be better to include the .pl file and generate the .S file as part of the build process. For one, there is semantic information like register names in the .pl script that is lost in the .S file, thereby making the .S file less readable.

There are still some alignment bugs where integers are loaded from byte arrays without using the unaligned access macros, e.g. in chacha20_init(), hchacha20_generic(), and fe_frombytes_impl(). I found these grepping for le32_to_cpu. Interestingly, that last one is in "formally verified" code :-)

Thanks!

Eric

#17 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

Quoted 6 lines

It seems you still don't explicitly clarify anywhere in the source itself that the copyright holders of the code from OpenSSL have relicensed it under GPLv2. I only see a GPLv2 license slapped on the files, yet no such license is presence in the OpenSSL originals, at least in the one I checked. If you did receive explicit permission, then you should include an explicit clarification in each file like the one in arch/arm/crypto/sha1-armv4-large.S.

Better yet, get the copyright holders to publicly send a
signed-off-by: or acked-by: so it is clear they agree to this.

Andrew

author maintainer
#18 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

In reply to Eric Biggers

Hey Eric,

Quoted 2 lines · Eric Biggers

I thought you were going to wrap lines at 80 characters? It's hard to read the extremely long lines, and they encourage deep nesting.

I somehow noted this for the WireGuard side of things but assumed I didn't need to do so for Zinc. Hah, such wishful thinking. I'll have this wrapped at 80 for v3.

Quoted 6 lines

As I said before, I still think you need to switch the crypto API ChaCha20 and Poly1305 over to use the new implementations. It's not okay to have two completely different sets of ChaCha20 and Poly1305 implementations just because you want a different API, so you might as well get started on it... The thing is that before you try it, it's not clear what problems will come up that require changes to the design. So, this really ought to be addressed up-front.

It was my hope that this entire series could enter the tree through Dave's net-next, and that I wouldn't have to touch anything in crypto/ or touch any of Herbert's stuff at all in anyway. However, if you want, I can start to play with that in another branch for a separate patchset, and of course I'd really value your feedback on that and on doing it right.

Quoted 4 lines

It's also not clearly explained whether/why/how the new ChaCha20 and Poly1305 implementations are better than the existing ones. The patch adding the ARM and ARM64 ChaCha, for example, just says who wrote them, with no mention of why the particular implementations were chosen.

I can expand on that, sure. One primary advantage that I do touch on on the big introductory commit message, though, is that sharing code means that we benefit from the eyeballs and fuzzing hours spent on OpenSSL, and I think this general advantage is extremely compelling.

Quoted 6 lines

You've also documented a lot of stuff in commit messages which will be lost as it isn't being added to the source itself. There are various examples of this, such as information about where the various implementations came from, what you changed, why a particular implementation isn't used on Skylake or whatever, etc. Can you please make sure that any important information is in comments, e.g. at the top of the files?

Good idea. I'll do that for v3.

Quoted 3 lines

I still think the "Zinc" name is confusing and misleading, and people are going to forget that it means "crypto". lib/crypto/ would be more intuitive. But I don't care *that* much myself, and you should see what others think...

I'd like to keep it. It also enables a natural path for a corresponding userspace library that shares all of the same code, and one that I think will be compelling to attract academics and developers to contributing to these efforts. Plus, can't I name what I made?

Quoted 4 lines

It seems you still don't explicitly clarify anywhere in the source itself that the copyright holders of the code from OpenSSL have relicensed it under GPLv2. I only see a GPLv2 license slapped on the files, yet no such license is presence in the OpenSSL originals, at least in the one I checked.

The SPDX headers for those came out of a discussion on how to encode CRYPTOGAMS into SPDX from the SPDX mailing list several months ago.

Quoted 3 lines

If you did receive
explicit permission, then you should include an explicit clarification in each file like the one in arch/arm/crypto/sha1-armv4-large.S.

That's a good idea.

Quoted 7 lines

As Ard and I discussed recently on my patch
"crypto: arm/poly1305 - add NEON accelerated Poly1305 implementation" which proposed adding the exact same poly1305-arm.S file, for all the OpenSSL assembly it would probably be better to include the .pl file and generate the .S file as part of the build process. For one, there is semantic information like register names in the .pl script that is lost in the .S file, thereby making the .S file less readable.

But on the other hand, the .S files have been modified and changed to fit kernel constraints and conventions. They're very much no longer merely "generated". This is most apparent with the x86_64 files, but is present too with the ARM files. We're still, I believe, bug-for-bug similar to the originals -- keeping with the point of going with Andy's implementations in the first place -- and we've been able to pretty trivially track OpenSSL changes -- but nonetheless important tweaks have been done to make this suitable to kernel space.

Quoted 4 lines

There are still some alignment bugs where integers are loaded from byte arrays without using the unaligned access macros, e.g. in chacha20_init(), hchacha20_generic(), and fe_frombytes_impl(). I found these grepping for le32_to_cpu. Interestingly, that last one is in "formally verified" code :-)

Thanks. I'll do another pass at these for v3.

Jason

#19 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

Quoted 9 lines · Jason A. Donenfeld

Hey Eric,

Quoted 2 lines · Eric Biggers

I thought you were going to wrap lines at 80 characters? It's hard to read the extremely long lines, and they encourage deep nesting.

I somehow noted this for the WireGuard side of things but assumed I didn't need to do so for Zinc. Hah, such wishful thinking. I'll have this wrapped at 80 for v3.

Hi Jason

The coding style document applies to all code in the tree. Plus some out of tree stuff, like iproute2 if, you need user space patches for that.

I've been running checkpatch on just the network parts. But i expect at some point, somebody will run it on the crypto stuff as well, and ask you to fix up some of the errors and warning.

There is also a general trend that you won't get in detail human reviews until the automated review tools indicate the code is O.K.,

Andrew

author maintainer
#20 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

In reply to Eric Biggers
Quoted 6 lines · Eric Biggers

I thought you were going to wrap lines at 80 characters? It's hard to read the extremely long lines, and they encourage deep nesting.

There are still some alignment bugs where integers are loaded from byte arrays without using the unaligned access macros, e.g. in chacha20_init(), hchacha20_generic(), and fe_frombytes_impl().

These fixes are now completed in the development tree.

#21 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

In reply to Jason A. Donenfeld

Hi Jason,

Quoted 24 lines · Jason A. Donenfeld

Zinc stands for "Zinc Is Neat Crypto" or "Zinc as IN Crypto" or maybe just "Zx2c4's INsane Cryptolib." It's also short, easy to type, and plays nicely with the recent trend of naming crypto libraries after elements. The guiding principle is "don't overdo it". It's less of a library and more of a directory tree for organizing well-curated direct implementations of cryptography primitives.

Zinc is a new cryptography API that is much more minimal and lower-level than the current one. It intends to complement it and provide a basis upon which the current crypto API might build, as the provider of software implementations of cryptographic primitives. It is motivated by three primary observations in crypto API design:

* Highly composable "cipher modes" and related abstractions from 90s cryptographers did not turn out to be as terrific an idea as hoped, leading to a host of API misuse problems.

* Most programmers are afraid of crypto code, and so prefer to integrate it into libraries in a highly abstracted manner, so as to shield themselves from implementation details. Cryptographers, on the other hand, prefer simple direct implementations, which they're able to verify for high assurance and optimize in accordance with their expertise.

Quoted 7 lines

* Overly abstracted and flexible cryptography APIs lead to a host of dangerous problems and performance issues. The kernel is in the business usually not of coming up with new uses of crypto, but rather implementing various constructions, which means it essentially needs a library of primitives, not a highly abstracted enterprise-ready pluggable system, with a few particular exceptions.

Do we really need a new crypto API for WireGuard? Surely, you yourself are not affected by these concerns, and I don't anticipate an explosion of new crypto use cases in the kernel that would justify maintaining two parallel crypto stacks.

Also, I take it this means that WireGuard will only work with your routines? We don't usually impose that kind of policy in the kernel:
on my arm64 systems, AES/GCM runs at 2.3 cycles per byte, which is a good enough reason to prefer it over a ChaCha20/Poly1305 based AEAD, even if your code is faster than the current code (which is debatable, as you know)

It also means that users of the crypto API will not benefit from your better code. I'm sure you agree that it is a bad idea to force those same crypto-impaired programmers to port their code to a new API.

I also suggested that we work with Andy Polyakov (as I have in the past) to make the changes required for the kernel in the OpenSSL upstream. That way, we can take the .pl files unmodified, and benefit from the maintenance and review upstream. Dumping 10,000s of lines of generated assembler in the kernel tree like that is really unacceptable IMO.

I think you will have to engage with the kernel community to identify the problems with the current crypto API, and get them fixed. I think it makes a lot of sense to have crypto primitives in lib/ (and arch-specific accelerated versions in arch/<arch>/lib), and layer many of the current crypto API drivers on top of that. Also, having more test cases is useful as well. I'm not sure what the relevance of formally verified implementations is, though, since it only proves that the code is true to the algorithm, not that the algorithm is secure. Or am I missing something?

Upstreaming your code is a lot easier if you don't cater for your own needs only but also for the needs of others. Please work with us to fix the problems in the current crypto API before parachuting in a new one.

Quoted 252 lines

This last observation has seen itself play out several times over and over again within the kernel:

* The perennial move of actual primitives away from crypto/ and into lib/, so that users can actually call these functions directly with no overhead and without lots of allocations, function pointers, string specifier parsing, and general clunkiness. For example:
sha256, chacha20, siphash, sha1, and so forth live in lib/ rather than in crypto/. Zinc intends to stop the cluttering of lib/ and introduce these direct primitives into their proper place, lib/zinc/.

* An abundance of misuse bugs with the present crypto API that have been very unpleasant to clean up.

* A hesitance to even use cryptography, because of the overhead and headaches involved in accessing the routines.

Zinc goes in a rather different direction. Rather than providing a thoroughly designed and abstracted API, Zinc gives you simple functions, which implement some primitive, or some particular and specific construction of primitives. It is not dynamic in the least, though one could imagine implementing a complex dynamic dispatch mechanism (such as the current crypto API) on top of these basic functions. After all, dynamic dispatch is usually needed for applications with cipher agility, such as IPsec, dm-crypt, AF_ALG, and so forth, and the existing crypto API will continue to play that role. However, Zinc will provide a non- haphazard way of directly utilizing crypto routines in applications that do have neither the need nor desire for abstraction and dynamic dispatch.

It also organizes the implementations in a simple, straight-forward, and direct manner, making it enjoyable and intuitive to work on. Rather than moving optimized assembly implementations into arch/, it keeps them all together in lib/zinc/, making it simple and obvious to compare and contrast what's happening. This is, notably, exactly what the lib/raid6/ tree does, and that seems to work out rather well. It's also the pattern of most successful crypto libraries. The architecture- specific glue-code is made a part of each translation unit, rather than being in a separate one, so that generic and architecture-optimized code are combined at compile-time, and incompatibility branches compiled out by the optimizer.

All implementations have been extensively tested and fuzzed, and are selected for their quality, trustworthiness, and performance. Wherever possible and performant, formally verified implementations are used, such as those from HACL* [1] and Fiat-Crypto [2]. The routines also take special care to zero out secrets using memzero_explicit (and future work is planned to have gcc do this more reliably and performantly with compiler plugins). The performance of the selected implementations is state-of-the-art and unrivaled on a broad array of hardware, though of course we will continue to fine tune these to the hardware demands needed by kernel contributors. Each implementation also comes with extensive self-tests and crafted test vectors, pulled from various places such as Wycheproof [9].

Regularity of function signatures is important, so that users can easily "guess" the name of the function they want. Though, individual primitives are oftentimes not trivially interchangeable, having been designed for different things and requiring different parameters and semantics, and so the function signatures they provide will directly reflect the realities of the primitives' usages, rather than hiding it behind (inevitably leaky) abstractions. Also, in contrast to the current crypto API, Zinc functions can work on stack buffers, and can be called with different keys, without requiring allocations or locking.

SIMD is used automatically when available, though some routines may benefit from either having their SIMD disabled for particular invocations, or to have the SIMD initialization calls amortized over several invocations of the function, and so Zinc utilizes function signatures enabling that in conjunction with the recently introduced simd_context_t.

More generally, Zinc provides function signatures that allow just what is required by the various callers. This isn't to say that users of the functions will be permitted to pollute the function semantics with weird particular needs, but we are trying very hard not to overdo it, and that means looking carefully at what's actually necessary, and doing just that, and not much more than that. Remember: practicality and cleanliness rather than over-zealous infrastructure.

Zinc provides also an opening for the best implementers in academia to contribute their time and effort to the kernel, by being sufficiently simple and inviting. In discussing this commit with some of the best and brightest over the last few years, there are many who are eager to devote rare talent and energy to this effort.

Following the merging of this, I expect for the primitives that currently exist in lib/ to work their way into lib/zinc/, after intense scrutiny of each implementation, potentially replacing them with either formally-verified implementations, or better studied and faster state-of-the-art implementations.

Also following the merging of this, I expect for the old crypto API implementations to be ported over to use Zinc for their software-based implementations.

As Zinc is simply library code, its config options are un-menued, with the exception of CONFIG_ZINC_DEBUG, which enables various selftests and BUG_ONs.

[1] https://github.com/project-everest/hacl-star
[2] https://github.com/mit-plv/fiat-crypto
[3] https://cr.yp.to/ecdh.html
[4] https://cr.yp.to/chacha.html
[5] https://cr.yp.to/snuffle/xsalsa-20081128.pdf
[6] https://cr.yp.to/mac.html
[7] https://blake2.net/
[8] https://tools.ietf.org/html/rfc8439
[9] https://github.com/google/wycheproof

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 MAINTAINERS       |  8 ++++++++
 lib/Kconfig       |  2 ++
 lib/Makefile      |  2 ++
 lib/zinc/Kconfig  | 20 ++++++++++++++++++++
 lib/zinc/Makefile |  7 +++++++
 lib/zinc/main.c   | 31 +++++++++++++++++++++++++++++++
 6 files changed, 70 insertions(+)
 create mode 100644 lib/zinc/Kconfig
 create mode 100644 lib/zinc/Makefile
 create mode 100644 lib/zinc/main.c
MAINTAINERS +8 -0
index 955463f8d518..f194eda86011 100644
@@ -16103,6 +16103,14 @@ Q:     https://patchwork.linuxtv.org/project/linux-media/list/
 S:     Maintained
 F:     drivers/media/dvb-frontends/zd1301_demod*
 
+ZINC CRYPTOGRAPHY LIBRARY
+M:     Jason A. Donenfeld <[email protected]>
+M:     Samuel Neves <[email protected]>
+S:     Maintained
+F:     lib/zinc/
+F:     include/zinc/
+L:     [email protected]
+
 ZPOOL COMPRESSED PAGE STORAGE API
 M:     Dan Streetman <[email protected]>
 L:     [email protected]
lib/Kconfig +2 -0
index 706836ec314d..7ea5437e9f7d 100644
@@ -478,6 +478,8 @@ config GLOB_SELFTEST
          module load) by a small amount, so you're welcome to play with
          it, but you probably don't need it.
 
+source "lib/zinc/Kconfig"
+
 #
 # Netlink attribute parsing support is select'ed if needed
 #
lib/Makefile +2 -0
index d95bb2525101..ee41151cba7a 100644
@@ -212,6 +212,8 @@ obj-$(CONFIG_PERCPU_TEST) += percpu_test.o
 
 obj-$(CONFIG_ASN1) += asn1_decoder.o
 
+obj-$(CONFIG_ZINC) += zinc/
+
 obj-$(CONFIG_FONT_SUPPORT) += fonts/
 
 obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
lib/zinc/Kconfig new file +20 -0
new file mode 100644
index 000000000000..aa4f8d449d6b
@@ -0,0 +1,20 @@
+config ZINC
+       tristate
+       select CRYPTO_BLKCIPHER
+       select VFP
+       select VFPv3
+       select NEON
+       select KERNEL_MODE_NEON
+
+config ZINC_DEBUG
+       bool "Zinc cryptography library debugging and self-tests"
+       depends on ZINC
+       help
+         This builds a series of self-tests for the Zinc crypto library, which
+         help diagnose any cryptographic algorithm implementation issues that
+         might be at the root cause of potential bugs. It also adds various
+         debugging traps.
+
+         Unless you're developing and testing cryptographic routines, or are
+         especially paranoid about correctness on your hardware, you may say
+         N here.
lib/zinc/Makefile new file +7 -0
new file mode 100644
index 000000000000..8e30115217db
@@ -0,0 +1,7 @@
+ccflags-y := -O3
+ccflags-y += -Wframe-larger-than=8192
+ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
+
+zinc-y += main.o
+
+obj-$(CONFIG_ZINC) := zinc.o
lib/zinc/main.c new file +31 -1
new file mode 100644
index 000000000000..590872563955
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+
+#ifdef CONFIG_ZINC_DEBUG
+#define selftest(which) do { \
+       if (!which ## _selftest()) \
+               return -ENOTRECOVERABLE; \
+} while (0)
+#else
+#define selftest(which)
+#endif
+
+static int __init mod_init(void)
+{
+       return 0;
+}
+
+static void __exit mod_exit(void)
+{
+}
+
+module_init(mod_init);
+module_exit(mod_exit);
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("Zinc cryptography library");
+MODULE_AUTHOR("Jason A. Donenfeld <[email protected]>");
--

2.18.0

author maintainer
#22 lore raw

Re: [PATCH v2 02/17] zinc: introduce minimal cryptography library

Hello Ard,

Quoted 4 lines · Ard Biesheuvel

Do we really need a new crypto API for WireGuard? Surely, you yourself are not affected by these concerns, and I don't anticipate an explosion of new crypto use cases in the kernel that would justify maintaining two parallel crypto stacks.

Yes, we really do. And the new API won't be useful for WireGuard but also for the majority of cryptography users within the kernel, which will gradually be ported to use the new API.

Quoted 6 lines

Also, I take it this means that WireGuard will only work with your routines? We don't usually impose that kind of policy in the kernel:
on my arm64 systems, AES/GCM runs at 2.3 cycles per byte, which is a good enough reason to prefer it over a ChaCha20/Poly1305 based AEAD, even if your code is faster than the current code (which is debatable, as you know)

WireGuard is a protocol that specifies ChaPoly, and I am proposing an implementation of that protocol. The protocol explicitly does not specify other ciphers. Please read the whitepaper for extensive discussion and feel free to come on over to [email protected] if you really want to hardcore bikeshed on protocol particulars. The short answer, however, is: no WireGuard won't be adding cipher negotiation, and no, this isn't considered a good feature, and no, you won't have any luck changing that. I am certain, though, that pushing this point will be a terrific means of steering this implementation thread far off course, as various folks with their various opinions feel compelled to jump in about their thoughts on cipher negotiation. Yawn.

Besides, your claim about "impos[ing] that kind of policy in the kernel" is bogus: with the exception of IPsec, dm-crypt, and AF_ALG, basically all users of cryptography I can find are using a very particular set of ciphers. For example, Bluetooth uses a particular elliptic curve that has been specified by the Bluetooth people, and the kernel therefore implements a driver that does computations over that elliptic curve. Nobody on LKML is clamoring for that driver to also support Curve448 or something, since that's not what Bluetooth specifies or how Bluetooth works. So too, that's not what WireGuard specifies or how WireGuard works.

Quoted 3 lines

It also means that users of the crypto API will not benefit from your better code. I'm sure you agree that it is a bad idea to force those same crypto-impaired programmers to port their code to a new API.

As mentioned, I think we'll certainly be gradually porting existing crypto API users over to the new API where appropriate. As always, this is a gradual thing, and won't ever come all at once in the first patchset, but I'm pretty confident we can get there somewhat quickly, especially as new primitives become available in the new API.

Quoted 6 lines

I also suggested that we work with Andy Polyakov (as I have in the past) to make the changes required for the kernel in the OpenSSL upstream. That way, we can take the .pl files unmodified, and benefit from the maintenance and review upstream. Dumping 10,000s of lines of generated assembler in the kernel tree like that is really unacceptable IMO.

I'm happy to do that, and indeed I do enjoy working with Andy. However, I don't think your characterization of the current situation is at all accurate. Rather, those .S files have been extensively worked with and crafted. They're far from being merely generated, and they've been pretty heavily reviewed.

Quoted 4 lines

I'm not sure what the relevance of
formally verified implementations is, though, since it only proves that the code is true to the algorithm, not that the algorithm is secure. Or am I missing something?

You are indeed missing something.

When the code is not true to the algorithm, that is very bad. When the code is true to the algorithm, that is very good.

Formally verified implementations intend to increase our confidence in the latter.

Meanwhile on the algorithm side of thing, the WireGuard protocol has a number of proofs that the protocol is secure:
- https://www.wireguard.com/papers/wireguard-formal-verification.pdf
- https://www.wireguard.com/papers/dowling-paterson-computational-2018.pdf
- https://www.wireguard.com/formal-verification/
Before you get too excited though, keep in mind that this is a proof of the protocol, and not of each primitive, like, say, "ChaCha20" or "Curve25519." However, academic literature is full of all sorts of curious and fascinating security analyses of the various primitives if this is something you're interested in.

Jason

author maintainer
#24 lore raw

PATCH v2 3/17 zinc: ChaCha20 generic C implementation

In reply to Jason A. Donenfeld

This implements the ChaCha20 permutation as a single C statement, by way of the comma operator, which the compiler is able to simplify terrifically.

Information: https://cr.yp.to/chacha.html

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 include/zinc/chacha20.h      |  47 ++++++++++++
 lib/zinc/Kconfig             |   4 +
 lib/zinc/Makefile            |   4 +
 lib/zinc/chacha20/chacha20.c | 137 +++++++++++++++++++++++++++++++++++
 lib/zinc/main.c              |   5 ++
 5 files changed, 197 insertions(+)
 create mode 100644 include/zinc/chacha20.h
 create mode 100644 lib/zinc/chacha20/chacha20.c
include/zinc/chacha20.h new file +47 -0
new file mode 100644
index 000000000000..ba50a2424ad0
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _ZINC_CHACHA20_H
+#define _ZINC_CHACHA20_H
+
+#include <linux/simd.h>
+#include <linux/kernel.h>
+#include <linux/types.h>
+
+enum {
+	CHACHA20_IV_SIZE = 16,
+	CHACHA20_KEY_SIZE = 32,
+	CHACHA20_BLOCK_SIZE = 64,
+	HCHACHA20_KEY_SIZE = 32,
+	HCHACHA20_NONCE_SIZE = 16
+};
+
+struct chacha20_ctx {
+	u32 key[8];
+	u32 counter[4];
+} __aligned(32);
+
+void chacha20_fpu_init(void);
+
+static inline void chacha20_init(struct chacha20_ctx *state, const u8 key[CHACHA20_KEY_SIZE], const u64 nonce)
+{
+	__le32 *le_key = (__le32 *)key;
+	state->key[0] = le32_to_cpu(le_key[0]);
+	state->key[1] = le32_to_cpu(le_key[1]);
+	state->key[2] = le32_to_cpu(le_key[2]);
+	state->key[3] = le32_to_cpu(le_key[3]);
+	state->key[4] = le32_to_cpu(le_key[4]);
+	state->key[5] = le32_to_cpu(le_key[5]);
+	state->key[6] = le32_to_cpu(le_key[6]);
+	state->key[7] = le32_to_cpu(le_key[7]);
+	state->counter[0] = state->counter[1] = 0;
+	state->counter[2] = nonce & U32_MAX;
+	state->counter[3] = nonce >> 32;
+}
+void chacha20(struct chacha20_ctx *state, u8 *dst, const u8 *src, u32 len, simd_context_t simd_context);
+
+void hchacha20(u8 derived_key[CHACHA20_KEY_SIZE], const u8 nonce[HCHACHA20_NONCE_SIZE], const u8 key[HCHACHA20_KEY_SIZE], simd_context_t simd_context);
+
+#endif /* _ZINC_CHACHA20_H */
lib/zinc/Kconfig +4 -0
index aa4f8d449d6b..5311a0d6ba8b 100644
@@ -6,6 +6,10 @@ config ZINC
 	select NEON
 	select KERNEL_MODE_NEON
 
+config ZINC_CHACHA20
+	bool
+	select ZINC
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	depends on ZINC
lib/zinc/Makefile +4 -0
index 8e30115217db..6ec77feb4ab7 100644
@@ -2,6 +2,10 @@ ccflags-y := -O3
 ccflags-y += -Wframe-larger-than=8192
 ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
 
+ifeq ($(CONFIG_ZINC_CHACHA20),y)
+zinc-y += chacha20/chacha20.o
+endif
+
 zinc-y += main.o
 
 obj-$(CONFIG_ZINC) := zinc.o
lib/zinc/chacha20/chacha20.c new file +137 -0

137 lines not on this page · raw message

lib/zinc/main.c +5 -0
index 590872563955..79b85045f22f 100644
@@ -3,6 +3,8 @@
  * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  */
 
+#include <zinc/chacha20.h>
+
 #include <linux/init.h>
 #include <linux/module.h>
 
@@ -17,6 +19,9 @@
 
 static int __init mod_init(void)
 {
+#ifdef CONFIG_ZINC_CHACHA20
+	chacha20_fpu_init();
+#endif
 	return 0;
 }
 
Signature · 1 line
2.18.0
author maintainer
#25 lore raw

PATCH v2 4/17 zinc: ChaCha20 ARM and ARM64 implementations

In reply to Jason A. Donenfeld

These NEON and non-NEON implementations come from Andy Polyakov's implementation, with several modifications for being friendly to kernel space.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Andy Polyakov <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
Cc linux-crypto at vger.kernel.org
---
 lib/zinc/Makefile                     |    8 +
 lib/zinc/chacha20/chacha20-arm-glue.h |   42 +
 lib/zinc/chacha20/chacha20-arm.S      | 1471 +++++++++++++++++++
 lib/zinc/chacha20/chacha20-arm64.S    | 1940 +++++++++++++++++++++++++
 4 files changed, 3461 insertions(+)
 create mode 100644 lib/zinc/chacha20/chacha20-arm-glue.h
 create mode 100644 lib/zinc/chacha20/chacha20-arm.S
 create mode 100644 lib/zinc/chacha20/chacha20-arm64.S
lib/zinc/Makefile +8 -0
index 6ec77feb4ab7..87be627f3354 100644
@@ -4,6 +4,14 @@ ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
 
 ifeq ($(CONFIG_ZINC_CHACHA20),y)
 zinc-y += chacha20/chacha20.o
+ifeq ($(CONFIG_ARM),y)
+zinc-y += chacha20/chacha20-arm.o
+CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-arm-glue.h
+endif
+ifeq ($(CONFIG_ARM64),y)
+zinc-y += chacha20/chacha20-arm64.o
+CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-arm-glue.h
+endif
 endif
 
 zinc-y += main.o
lib/zinc/chacha20/chacha20-arm-glue.h new file +42 -0

42 lines not on this page · raw message

lib/zinc/chacha20/chacha20-arm.S new file +1471 -0

1,471 lines not on this page · raw message

lib/zinc/chacha20/chacha20-arm64.S new file +1940 -0

1,473 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.18.0

Part of the diff is not shown here. View the raw message.

author maintainer
#26 lore raw

PATCH v2 5/17 zinc: ChaCha20 x86_64 implementation

In reply to Jason A. Donenfeld

This provides SSSE3, AVX-2, AVX-512F, and AVX-512VL implementations for ChaCha20. The AVX-512F implementation is disabled on Skylake, due to throttling, and the VL ymm implementation is used instead. These come from Andy Polyakov's implementation, with some heavy modifications from Samuel Neves and me.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Andy Polyakov <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 lib/zinc/Makefile                        |    4 +
 lib/zinc/chacha20/chacha20-x86_64-glue.h |   84 +
 lib/zinc/chacha20/chacha20-x86_64.S      | 2630 ++++++++++++++++++++++
 3 files changed, 2718 insertions(+)
 create mode 100644 lib/zinc/chacha20/chacha20-x86_64-glue.h
 create mode 100644 lib/zinc/chacha20/chacha20-x86_64.S
lib/zinc/Makefile +4 -0
index 87be627f3354..fc6ca1c84cd1 100644
@@ -4,6 +4,10 @@ ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
 
 ifeq ($(CONFIG_ZINC_CHACHA20),y)
 zinc-y += chacha20/chacha20.o
+ifeq ($(CONFIG_X86_64),y)
+zinc-y += chacha20/chacha20-x86_64.o
+CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-x86_64-glue.h
+endif
 ifeq ($(CONFIG_ARM),y)
 zinc-y += chacha20/chacha20-arm.o
 CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-arm-glue.h
lib/zinc/chacha20/chacha20-x86_64-glue.h new file +84 -0

84 lines not on this page · raw message

lib/zinc/chacha20/chacha20-x86_64.S new file +2630 -0

2,630 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#27 lore raw

PATCH v2 6/17 zinc: ChaCha20 MIPS32r2 implementation

In reply to Jason A. Donenfeld

This MIPS32r2 implementation comes from René van Dorst and me and results in a nice speedup on the usual OpenWRT targets.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc René van Dorst <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Paul Burton <[email protected]>
Cc James Hogan <[email protected]>
---
 lib/zinc/Makefile                      |   4 +
 lib/zinc/chacha20/chacha20-mips-glue.h |  19 +
 lib/zinc/chacha20/chacha20-mips.S      | 474 +++++++++++++++++++++++++
 3 files changed, 497 insertions(+)
 create mode 100644 lib/zinc/chacha20/chacha20-mips-glue.h
 create mode 100644 lib/zinc/chacha20/chacha20-mips.S
lib/zinc/Makefile +4 -0
index fc6ca1c84cd1..3ba830b51695 100644
@@ -16,6 +16,10 @@ ifeq ($(CONFIG_ARM64),y)
 zinc-y += chacha20/chacha20-arm64.o
 CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-arm-glue.h
 endif
+ifeq ($(CONFIG_MIPS)$(CONFIG_CPU_MIPS32_R2),yy)
+zinc-y += chacha20/chacha20-mips.o
+CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-mips-glue.h
+endif
 endif
 
 zinc-y += main.o
lib/zinc/chacha20/chacha20-mips-glue.h new file +19 -0
new file mode 100644
index 000000000000..54c71a0b5bd0
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <zinc/chacha20.h>
+
+asmlinkage void chacha20_mips(u8 *out, const u8 *in, const size_t len, const u32 key[8], const u32 counter[4]);
+void __init chacha20_fpu_init(void) { }
+
+static inline bool chacha20_arch(u8 *dst, const u8 *src, const size_t len, const u32 key[8], const u32 counter[4], simd_context_t simd_context)
+{
+	chacha20_mips(dst, src, len, key, counter);
+	return true;
+}
+
+static inline bool hchacha20_arch(u8 *derived_key, const u8 *nonce, const u8 *key, simd_context_t simd_context) { return false; }
+
+#define HAVE_CHACHA20_ARCH_IMPLEMENTATION
lib/zinc/chacha20/chacha20-mips.S new file +474 -0

474 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#28 lore raw

PATCH v2 7/17 zinc: Poly1305 generic C implementation and selftest

In reply to Jason A. Donenfeld

The C implementation is based on Andy Polyakov's implementation, heavily modified by Samuel Neves.

Information: https://cr.yp.to/mac.html

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Andy Polyakov <[email protected]>
---
 include/zinc/poly1305.h      |   35 +
 lib/zinc/Kconfig             |    4 +
 lib/zinc/Makefile            |    4 +
 lib/zinc/main.c              |    5 +
 lib/zinc/poly1305/poly1305.c |  257 ++++++
 lib/zinc/selftest/poly1305.h | 1566 ++++++++++++++++++++++++++++++++++
 6 files changed, 1871 insertions(+)
 create mode 100644 include/zinc/poly1305.h
 create mode 100644 lib/zinc/poly1305/poly1305.c
 create mode 100644 lib/zinc/selftest/poly1305.h
include/zinc/poly1305.h new file +35 -0
new file mode 100644
index 000000000000..9fcae37a41ee
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _ZINC_POLY1305_H
+#define _ZINC_POLY1305_H
+
+#include <linux/simd.h>
+#include <linux/types.h>
+
+enum poly1305_lengths {
+	POLY1305_BLOCK_SIZE = 16,
+	POLY1305_KEY_SIZE = 32,
+	POLY1305_MAC_SIZE = 16
+};
+
+struct poly1305_ctx {
+	u8 opaque[24 * sizeof(u64)];
+	u32 nonce[4];
+	u8 data[POLY1305_BLOCK_SIZE];
+	size_t num;
+} __aligned(8);
+
+void poly1305_fpu_init(void);
+
+void poly1305_init(struct poly1305_ctx *ctx, const u8 key[POLY1305_KEY_SIZE], simd_context_t simd_context);
+void poly1305_update(struct poly1305_ctx *ctx, const u8 *inp, const size_t len, simd_context_t simd_context);
+void poly1305_finish(struct poly1305_ctx *ctx, u8 mac[POLY1305_MAC_SIZE], simd_context_t simd_context);
+
+#ifdef CONFIG_ZINC_DEBUG
+bool poly1305_selftest(void);
+#endif
+
+#endif /* _ZINC_POLY1305_H */
lib/zinc/Kconfig +4 -0
index 5311a0d6ba8b..86936739a05f 100644
@@ -10,6 +10,10 @@ config ZINC_CHACHA20
 	bool
 	select ZINC
 
+config ZINC_POLY1305
+	bool
+	select ZINC
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	depends on ZINC
lib/zinc/Makefile +4 -0
index 3ba830b51695..bf7b3a75f4c2 100644
@@ -22,6 +22,10 @@ CFLAGS_chacha20.o += -include $(srctree)/$(src)/chacha20/chacha20-mips-glue.h
 endif
 endif
 
+ifeq ($(CONFIG_ZINC_POLY1305),y)
+zinc-y += poly1305/poly1305.o
+endif
+
 zinc-y += main.o
 
 obj-$(CONFIG_ZINC) := zinc.o
lib/zinc/main.c +5 -0
index 79b85045f22f..80a2e9f963f7 100644
@@ -4,6 +4,7 @@
  */
 
 #include <zinc/chacha20.h>
+#include <zinc/poly1305.h>
 
 #include <linux/init.h>
 #include <linux/module.h>
@@ -21,6 +22,10 @@ static int __init mod_init(void)
 {
 #ifdef CONFIG_ZINC_CHACHA20
 	chacha20_fpu_init();
+#endif
+#ifdef CONFIG_ZINC_POLY1305
+	poly1305_fpu_init();
+	selftest(poly1305);
 #endif
 	return 0;
 }
lib/zinc/poly1305/poly1305.c new file +257 -0

257 lines not on this page · raw message

lib/zinc/selftest/poly1305.h new file +1566 -0

1,566 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#29 lore raw

PATCH v2 8/17 zinc: Poly1305 ARM and ARM64 implementations

In reply to Jason A. Donenfeld

These NEON and non-NEON implementations come from Andy Polyakov's implementation, with several modifications for being friendly to kernel space.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Andy Polyakov <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
Cc linux-crypto at vger.kernel.org
---
 lib/zinc/Makefile                     |    8 +
 lib/zinc/poly1305/poly1305-arm-glue.h |   58 ++
 lib/zinc/poly1305/poly1305-arm.S      | 1115 +++++++++++++++++++++++++
 lib/zinc/poly1305/poly1305-arm64.S    |  820 ++++++++++++++++++
 4 files changed, 2001 insertions(+)
 create mode 100644 lib/zinc/poly1305/poly1305-arm-glue.h
 create mode 100644 lib/zinc/poly1305/poly1305-arm.S
 create mode 100644 lib/zinc/poly1305/poly1305-arm64.S
lib/zinc/Makefile +8 -0
index bf7b3a75f4c2..db70ffedf7dd 100644
@@ -24,6 +24,14 @@ endif
 
 ifeq ($(CONFIG_ZINC_POLY1305),y)
 zinc-y += poly1305/poly1305.o
+ifeq ($(CONFIG_ARM),y)
+zinc-y += poly1305/poly1305-arm.o
+CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-arm-glue.h
+endif
+ifeq ($(CONFIG_ARM64),y)
+zinc-y += poly1305/poly1305-arm64.o
+CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-arm-glue.h
+endif
 endif
 
 zinc-y += main.o
lib/zinc/poly1305/poly1305-arm-glue.h new file +58 -0

58 lines not on this page · raw message

lib/zinc/poly1305/poly1305-arm.S new file +1115 -0

1,115 lines not on this page · raw message

lib/zinc/poly1305/poly1305-arm64.S new file +820 -0

820 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#30 lore raw

PATCH v2 9/17 zinc: Poly1305 x86_64 implementation

In reply to Jason A. Donenfeld

This provides AVX, AVX-2, and AVX-512F implementations for Poly1305. The AVX-512F implementation is disabled on Skylake, due to throttling. These come from Andy Polyakov's implementation, with some heavy modifications from Samuel Neves and me.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Andy Polyakov <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 lib/zinc/Makefile                        |    4 +
 lib/zinc/poly1305/poly1305-x86_64-glue.h |   91 +
 lib/zinc/poly1305/poly1305-x86_64.S      | 2790 ++++++++++++++++++++++
 3 files changed, 2885 insertions(+)
 create mode 100644 lib/zinc/poly1305/poly1305-x86_64-glue.h
 create mode 100644 lib/zinc/poly1305/poly1305-x86_64.S
lib/zinc/Makefile +4 -0
index db70ffedf7dd..57fe42b7f97c 100644
@@ -24,6 +24,10 @@ endif
 
 ifeq ($(CONFIG_ZINC_POLY1305),y)
 zinc-y += poly1305/poly1305.o
+ifeq ($(CONFIG_X86_64),y)
+zinc-y += poly1305/poly1305-x86_64.o
+CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-x86_64-glue.h
+endif
 ifeq ($(CONFIG_ARM),y)
 zinc-y += poly1305/poly1305-arm.o
 CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-arm-glue.h
lib/zinc/poly1305/poly1305-x86_64-glue.h new file +91 -0

91 lines not on this page · raw message

lib/zinc/poly1305/poly1305-x86_64.S new file +2790 -0

2,790 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#31 lore raw

PATCH v2 10/17 zinc: Poly1305 MIPS32r2 and MIPS64 implementations

In reply to Jason A. Donenfeld

This MIPS32r2 implementation comes from René van Dorst and me and results in a nice speedup on the usual OpenWRT targets. The MIPS64 implementation comes from Andy Polyakov with some kernel-friendly modifications from me and results in a nice speedup on commodity Octeon hardware.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Andy Polyakov <[email protected]>
Cc René van Dorst <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Paul Burton <[email protected]>
Cc James Hogan <[email protected]>
---
 lib/zinc/Makefile                      |   8 +
 lib/zinc/poly1305/poly1305-mips-glue.h |  31 ++
 lib/zinc/poly1305/poly1305-mips.S      | 417 +++++++++++++++++++++++++
 lib/zinc/poly1305/poly1305-mips64.S    | 357 +++++++++++++++++++++
 4 files changed, 813 insertions(+)
 create mode 100644 lib/zinc/poly1305/poly1305-mips-glue.h
 create mode 100644 lib/zinc/poly1305/poly1305-mips.S
 create mode 100644 lib/zinc/poly1305/poly1305-mips64.S
lib/zinc/Makefile +8 -0
index 57fe42b7f97c..6b21ed4464c6 100644
@@ -36,6 +36,14 @@ ifeq ($(CONFIG_ARM64),y)
 zinc-y += poly1305/poly1305-arm64.o
 CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-arm-glue.h
 endif
+ifeq ($(CONFIG_MIPS)$(CONFIG_CPU_MIPS32_R2),yy)
+zinc-y += poly1305/poly1305-mips.o
+CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-mips-glue.h
+endif
+ifeq ($(CONFIG_MIPS)$(CONFIG_64BIT),yy)
+zinc-y += poly1305/poly1305-mips64.o
+CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-mips-glue.h
+endif
 endif
 
 zinc-y += main.o
lib/zinc/poly1305/poly1305-mips-glue.h new file +31 -0
new file mode 100644
index 000000000000..3eb7d4c7be94
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <zinc/poly1305.h>
+
+asmlinkage void poly1305_init_mips(void *ctx, const u8 key[16]);
+asmlinkage void poly1305_blocks_mips(void *ctx, const u8 *inp, const size_t len, const u32 padbit);
+asmlinkage void poly1305_emit_mips(void *ctx, u8 mac[16], const u32 nonce[4]);
+void __init poly1305_fpu_init(void) { }
+
+static inline bool poly1305_init_arch(void *ctx, const u8 key[POLY1305_KEY_SIZE], simd_context_t simd_context)
+{
+	poly1305_init_mips(ctx, key);
+	return true;
+}
+
+static inline bool poly1305_blocks_arch(void *ctx, const u8 *inp, const size_t len, const u32 padbit, simd_context_t simd_context)
+{
+	poly1305_blocks_mips(ctx, inp, len, padbit);
+	return true;
+}
+
+static inline bool poly1305_emit_arch(void *ctx, u8 mac[POLY1305_MAC_SIZE], const u32 nonce[4], simd_context_t simd_context)
+{
+	poly1305_emit_mips(ctx, mac, nonce);
+	return true;
+}
+
+#define HAVE_POLY1305_ARCH_IMPLEMENTATION
lib/zinc/poly1305/poly1305-mips.S new file +417 -0

417 lines not on this page · raw message

lib/zinc/poly1305/poly1305-mips64.S new file +357 -0

357 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#32 lore raw

PATCH v2 11/17 zinc: ChaCha20Poly1305 construction and selftest

In reply to Jason A. Donenfeld

This is an implementation of the ChaCha20Poly1305 AEAD, with an easy API for encrypting either contiguous buffers or scatter gather lists (such as those created from skb_to_sgvec).

Information: https://tools.ietf.org/html/rfc8439

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 include/zinc/chacha20poly1305.h      |   52 +
 lib/zinc/Kconfig                     |    6 +
 lib/zinc/Makefile                    |    4 +
 lib/zinc/chacha20poly1305.c          |  284 ++
 lib/zinc/main.c                      |    4 +
 lib/zinc/selftest/chacha20poly1305.h | 4359 ++++++++++++++++++++++++++
 6 files changed, 4709 insertions(+)
 create mode 100644 include/zinc/chacha20poly1305.h
 create mode 100644 lib/zinc/chacha20poly1305.c
 create mode 100644 lib/zinc/selftest/chacha20poly1305.h
include/zinc/chacha20poly1305.h new file +52 -0
new file mode 100644
index 000000000000..8b6007a9e7c3
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _ZINC_CHACHA20POLY1305_H
+#define _ZINC_CHACHA20POLY1305_H
+
+#include <linux/simd.h>
+#include <linux/types.h>
+
+struct scatterlist;
+
+enum chacha20poly1305_lengths {
+	XCHACHA20POLY1305_NONCELEN = 24,
+	CHACHA20POLY1305_KEYLEN = 32,
+	CHACHA20POLY1305_AUTHTAGLEN = 16
+};
+
+void chacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
+			      const u8 *ad, const size_t ad_len,
+			      const u64 nonce, const u8 key[CHACHA20POLY1305_KEYLEN]);
+
+bool __must_check chacha20poly1305_encrypt_sg(struct scatterlist *dst, struct scatterlist *src, const size_t src_len,
+					      const u8 *ad, const size_t ad_len,
+					      const u64 nonce, const u8 key[CHACHA20POLY1305_KEYLEN],
+					      simd_context_t simd_context);
+
+bool __must_check chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
+					   const u8 *ad, const size_t ad_len,
+					   const u64 nonce, const u8 key[CHACHA20POLY1305_KEYLEN]);
+
+bool __must_check chacha20poly1305_decrypt_sg(struct scatterlist *dst, struct scatterlist *src, const size_t src_len,
+					      const u8 *ad, const size_t ad_len,
+					      const u64 nonce, const u8 key[CHACHA20POLY1305_KEYLEN],
+					      simd_context_t simd_context);
+
+void xchacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
+			       const u8 *ad, const size_t ad_len,
+			       const u8 nonce[XCHACHA20POLY1305_NONCELEN],
+			       const u8 key[CHACHA20POLY1305_KEYLEN]);
+
+bool __must_check xchacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
+					    const u8 *ad, const size_t ad_len,
+					    const u8 nonce[XCHACHA20POLY1305_NONCELEN],
+					    const u8 key[CHACHA20POLY1305_KEYLEN]);
+
+#ifdef CONFIG_ZINC_DEBUG
+bool chacha20poly1305_selftest(void);
+#endif
+
+#endif /* _ZINC_CHACHA20POLY1305_H */
lib/zinc/Kconfig +6 -0
index 86936739a05f..44967cf87b95 100644
@@ -14,6 +14,12 @@ config ZINC_POLY1305
 	bool
 	select ZINC
 
+config ZINC_CHACHA20POLY1305
+	bool
+	select ZINC
+	select ZINC_CHACHA20
+	select ZINC_POLY1305
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	depends on ZINC
lib/zinc/Makefile +4 -0
index 6b21ed4464c6..e6a20d768b79 100644
@@ -46,6 +46,10 @@ CFLAGS_poly1305.o += -include $(srctree)/$(src)/poly1305/poly1305-mips-glue.h
 endif
 endif
 
+ifeq ($(CONFIG_ZINC_CHACHA20POLY1305),y)
+zinc-y += chacha20poly1305.o
+endif
+
 zinc-y += main.o
 
 obj-$(CONFIG_ZINC) := zinc.o
lib/zinc/chacha20poly1305.c new file +284 -0

284 lines not on this page · raw message

lib/zinc/main.c +4 -0
index 80a2e9f963f7..ba5eb7563f81 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  */
 
+#include <zinc/chacha20poly1305.h>
 #include <zinc/chacha20.h>
 #include <zinc/poly1305.h>
 
@@ -26,6 +27,9 @@ static int __init mod_init(void)
 #ifdef CONFIG_ZINC_POLY1305
 	poly1305_fpu_init();
 	selftest(poly1305);
+#endif
+#ifdef CONFIG_ZINC_CHACHA20POLY1305
+	selftest(chacha20poly1305);
 #endif
 	return 0;
 }
lib/zinc/selftest/chacha20poly1305.h new file +4359 -0

2,626 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.18.0

Part of the diff is not shown here. View the raw message.

author maintainer
#33 lore raw

PATCH v2 12/17 zinc: BLAKE2s generic C implementation and selftest

In reply to Jason A. Donenfeld

The C implementation was originally based on Samuel Neves' public domain reference implementation but has since been heavily modified for the kernel. We're able to do compile-time optimizations by moving some scaffolding around the final function into the header file.

Information: https://blake2.net/

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 include/zinc/blake2s.h      |  94 ++++++
 lib/zinc/Kconfig            |   4 +
 lib/zinc/Makefile           |   4 +
 lib/zinc/blake2s/blake2s.c  | 257 +++++++++++++++++
 lib/zinc/main.c             |   5 +
 lib/zinc/selftest/blake2s.h | 559 ++++++++++++++++++++++++++++++++++++
 6 files changed, 923 insertions(+)
 create mode 100644 include/zinc/blake2s.h
 create mode 100644 lib/zinc/blake2s/blake2s.c
 create mode 100644 lib/zinc/selftest/blake2s.h
include/zinc/blake2s.h new file +94 -0
new file mode 100644
index 000000000000..812c4c68bbe9
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _ZINC_BLAKE2S_H
+#define _ZINC_BLAKE2S_H
+
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <crypto/algapi.h>
+
+enum blake2s_lengths {
+	BLAKE2S_BLOCKBYTES = 64,
+	BLAKE2S_OUTBYTES = 32,
+	BLAKE2S_KEYBYTES = 32
+};
+
+struct blake2s_state {
+	u32 h[8];
+	u32 t[2];
+	u32 f[2];
+	u8 buf[BLAKE2S_BLOCKBYTES];
+	size_t buflen;
+	u8 last_node;
+};
+
+void blake2s_init(struct blake2s_state *state, const size_t outlen);
+void blake2s_init_key(struct blake2s_state *state, const size_t outlen, const void *key, const size_t keylen);
+void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
+void __blake2s_final(struct blake2s_state *state);
+static inline void blake2s_final(struct blake2s_state *state, u8 *out, const size_t outlen)
+{
+	int i;
+
+#ifdef CONFIG_ZINC_DEBUG
+	BUG_ON(!out || !outlen || outlen > BLAKE2S_OUTBYTES);
+#endif
+	__blake2s_final(state);
+
+	if (__builtin_constant_p(outlen) && !(outlen % sizeof(u32))) {
+		if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || IS_ALIGNED((unsigned long)out, __alignof__(u32))) {
+			__le32 *outwords = (__le32 *)out;
+
+			for (i = 0; i < outlen / sizeof(u32); ++i)
+				outwords[i] = cpu_to_le32(state->h[i]);
+		} else {
+			__le32 buffer[BLAKE2S_OUTBYTES];
+
+			for (i = 0; i < outlen / sizeof(u32); ++i)
+				buffer[i] = cpu_to_le32(state->h[i]);
+			memcpy(out, buffer, outlen);
+			memzero_explicit(buffer, sizeof(buffer));
+		}
+	} else {
+		u8 buffer[BLAKE2S_OUTBYTES] __aligned(__alignof__(u32));
+		__le32 *outwords = (__le32 *)buffer;
+
+		for (i = 0; i < 8; ++i)
+			outwords[i] = cpu_to_le32(state->h[i]);
+		memcpy(out, buffer, outlen);
+		memzero_explicit(buffer, sizeof(buffer));
+	}
+
+	memzero_explicit(state, sizeof(struct blake2s_state));
+}
+
+
+static inline void blake2s(u8 *out, const u8 *in, const u8 *key, const size_t outlen, const size_t inlen, const size_t keylen)
+{
+	struct blake2s_state state;
+
+#ifdef CONFIG_ZINC_DEBUG
+	BUG_ON((!in && inlen > 0) || !out || !outlen || outlen > BLAKE2S_OUTBYTES || keylen > BLAKE2S_KEYBYTES || (!key && keylen));
+#endif
+
+	if (keylen)
+		blake2s_init_key(&state, outlen, key, keylen);
+	else
+		blake2s_init(&state, outlen);
+
+	blake2s_update(&state, in, inlen);
+	blake2s_final(&state, out, outlen);
+}
+
+void blake2s_hmac(u8 *out, const u8 *in, const u8 *key, const size_t outlen, const size_t inlen, const size_t keylen);
+
+void blake2s_fpu_init(void);
+
+#ifdef CONFIG_ZINC_DEBUG
+bool blake2s_selftest(void);
+#endif
+
+#endif /* _ZINC_BLAKE2S_H */
lib/zinc/Kconfig +4 -0
index 44967cf87b95..25b46a87509f 100644
@@ -20,6 +20,10 @@ config ZINC_CHACHA20POLY1305
 	select ZINC_CHACHA20
 	select ZINC_POLY1305
 
+config ZINC_BLAKE2S
+	bool
+	select ZINC
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	depends on ZINC
lib/zinc/Makefile +4 -0
index e6a20d768b79..663cf6bc382d 100644
@@ -50,6 +50,10 @@ ifeq ($(CONFIG_ZINC_CHACHA20POLY1305),y)
 zinc-y += chacha20poly1305.o
 endif
 
+ifeq ($(CONFIG_ZINC_BLAKE2S),y)
+zinc-y += blake2s/blake2s.o
+endif
+
 zinc-y += main.o
 
 obj-$(CONFIG_ZINC) := zinc.o
lib/zinc/blake2s/blake2s.c new file +257 -0

257 lines not on this page · raw message

lib/zinc/main.c +5 -0
index ba5eb7563f81..db4704257586 100644
@@ -6,6 +6,7 @@
 #include <zinc/chacha20poly1305.h>
 #include <zinc/chacha20.h>
 #include <zinc/poly1305.h>
+#include <zinc/blake2s.h>
 
 #include <linux/init.h>
 #include <linux/module.h>
@@ -30,6 +31,10 @@ static int __init mod_init(void)
 #endif
 #ifdef CONFIG_ZINC_CHACHA20POLY1305
 	selftest(chacha20poly1305);
+#endif
+#ifdef CONFIG_ZINC_BLAKE2S
+	blake2s_fpu_init();
+	selftest(blake2s);
 #endif
 	return 0;
 }
lib/zinc/selftest/blake2s.h new file +559 -0

559 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#34 lore raw

PATCH v2 13/17 zinc: BLAKE2s x86_64 implementation

In reply to Jason A. Donenfeld

These implementations from Samuel Neves support AVX and AVX-512VL. Originally this used AVX-512F, but Skylake thermal throttling made AVX-512VL more attractive and possible to do with negligable difference.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 lib/zinc/Makefile                      |   4 +
 lib/zinc/blake2s/blake2s-x86_64-glue.h |  53 ++
 lib/zinc/blake2s/blake2s-x86_64.S      | 685 +++++++++++++++++++++++++
 3 files changed, 742 insertions(+)
 create mode 100644 lib/zinc/blake2s/blake2s-x86_64-glue.h
 create mode 100644 lib/zinc/blake2s/blake2s-x86_64.S
lib/zinc/Makefile +4 -0
index 663cf6bc382d..e252eea24844 100644
@@ -52,6 +52,10 @@ endif
 
 ifeq ($(CONFIG_ZINC_BLAKE2S),y)
 zinc-y += blake2s/blake2s.o
+ifeq ($(CONFIG_X86_64),y)
+zinc-y += blake2s/blake2s-x86_64.o
+CFLAGS_blake2s.o += -include $(srctree)/$(src)/blake2s/blake2s-x86_64-glue.h
+endif
 endif
 
 zinc-y += main.o
lib/zinc/blake2s/blake2s-x86_64-glue.h new file +53 -0

53 lines not on this page · raw message

lib/zinc/blake2s/blake2s-x86_64.S new file +685 -0

685 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#35 lore raw

PATCH v2 14/17 zinc: Curve25519 generic C implementations and selftest

In reply to Jason A. Donenfeld

This contains two formally verified C implementations of the Curve25519 scalar multiplication function, one for 32-bit systems, and one for 64-bit systems whose compiler supports efficient 128-bit integer types. Not only are these implementations formally verified, but they are also the fastest available C implementations. They have been modified to be friendly to kernel space and to be generally less horrendous looking, but still an effort has been made to retain their formally verified characteristic, and so the C might look slightly unidiomatic.

The 64-bit version comes from HACL*: https://github.com/project-everest/hacl-star
The 32-bit version comes from Fiat: https://github.com/mit-plv/fiat-crypto

Information: https://cr.yp.to/ecdh.html

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Karthikeyan Bhargavan <[email protected]>
---
 include/zinc/curve25519.h               |  25 +
 lib/zinc/Kconfig                        |   4 +
 lib/zinc/Makefile                       |   4 +
 lib/zinc/curve25519/curve25519-fiat32.h | 838 ++++++++++++++++++++++++
 lib/zinc/curve25519/curve25519-hacl64.h | 744 +++++++++++++++++++++
 lib/zinc/curve25519/curve25519.c        |  62 ++
 lib/zinc/main.c                         |   5 +
 lib/zinc/selftest/curve25519.h          | 607 +++++++++++++++++
 8 files changed, 2289 insertions(+)
 create mode 100644 include/zinc/curve25519.h
 create mode 100644 lib/zinc/curve25519/curve25519-fiat32.h
 create mode 100644 lib/zinc/curve25519/curve25519-hacl64.h
 create mode 100644 lib/zinc/curve25519/curve25519.c
 create mode 100644 lib/zinc/selftest/curve25519.h
include/zinc/curve25519.h new file +25 -0
new file mode 100644
index 000000000000..b6bd387e3b8a
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _ZINC_CURVE25519_H
+#define _ZINC_CURVE25519_H
+
+#include <linux/types.h>
+
+enum curve25519_lengths {
+	CURVE25519_POINT_SIZE = 32
+};
+
+bool __must_check curve25519(u8 mypublic[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE], const u8 basepoint[CURVE25519_POINT_SIZE]);
+void curve25519_generate_secret(u8 secret[CURVE25519_POINT_SIZE]);
+bool __must_check curve25519_generate_public(u8 pub[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE]);
+
+void curve25519_fpu_init(void);
+
+#ifdef CONFIG_ZINC_DEBUG
+bool curve25519_selftest(void);
+#endif
+
+#endif /* _ZINC_CURVE25519_H */
lib/zinc/Kconfig +4 -0
index 25b46a87509f..285ba527efed 100644
@@ -24,6 +24,10 @@ config ZINC_BLAKE2S
 	bool
 	select ZINC
 
+config ZINC_CURVE25519
+	bool
+	select ZINC
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	depends on ZINC
lib/zinc/Makefile +4 -0
index e252eea24844..b6b0614f8743 100644
@@ -50,6 +50,10 @@ ifeq ($(CONFIG_ZINC_CHACHA20POLY1305),y)
 zinc-y += chacha20poly1305.o
 endif
 
+ifeq ($(CONFIG_ZINC_CURVE25519),y)
+zinc-y += curve25519/curve25519.o
+endif
+
 ifeq ($(CONFIG_ZINC_BLAKE2S),y)
 zinc-y += blake2s/blake2s.o
 ifeq ($(CONFIG_X86_64),y)
lib/zinc/curve25519/curve25519-fiat32.h new file +838 -0

838 lines not on this page · raw message

lib/zinc/curve25519/curve25519-hacl64.h new file +744 -0

744 lines not on this page · raw message

lib/zinc/curve25519/curve25519.c new file +62 -0

62 lines not on this page · raw message

lib/zinc/main.c +5 -0
index db4704257586..a4767ea656d6 100644
@@ -7,6 +7,7 @@
 #include <zinc/chacha20.h>
 #include <zinc/poly1305.h>
 #include <zinc/blake2s.h>
+#include <zinc/curve25519.h>
 
 #include <linux/init.h>
 #include <linux/module.h>
@@ -35,6 +36,10 @@ static int __init mod_init(void)
 #ifdef CONFIG_ZINC_BLAKE2S
 	blake2s_fpu_init();
 	selftest(blake2s);
+#endif
+#ifdef CONFIG_ZINC_CURVE25519
+	curve25519_fpu_init();
+	selftest(curve25519);
 #endif
 	return 0;
 }
lib/zinc/selftest/curve25519.h new file +607 -0

607 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#36 lore raw

PATCH v2 15/17 zinc: Curve25519 ARM implementation

In reply to Jason A. Donenfeld

This comes from Dan Bernstein and Peter Schwabe's public domain NEON code, and has been modified to be friendly for kernel space.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc D. J. Bernstein <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
Cc linux-crypto at vger.kernel.org
---
 lib/zinc/Makefile                         |    4 +
 lib/zinc/curve25519/curve25519-arm-glue.h |   33 +
 lib/zinc/curve25519/curve25519-arm.S      | 2110 +++++++++++++++++++++
 3 files changed, 2147 insertions(+)
 create mode 100644 lib/zinc/curve25519/curve25519-arm-glue.h
 create mode 100644 lib/zinc/curve25519/curve25519-arm.S
lib/zinc/Makefile +4 -0
index b6b0614f8743..13e403780ce0 100644
@@ -52,6 +52,10 @@ endif
 
 ifeq ($(CONFIG_ZINC_CURVE25519),y)
 zinc-y += curve25519/curve25519.o
+ifeq ($(CONFIG_ARM)$(CONFIG_KERNEL_MODE_NEON),yy)
+zinc-y += curve25519/curve25519-arm.o
+CFLAGS_curve25519.o += -include $(srctree)/$(src)/curve25519/curve25519-arm-glue.h
+endif
 endif
 
 ifeq ($(CONFIG_ZINC_BLAKE2S),y)
lib/zinc/curve25519/curve25519-arm-glue.h new file +33 -0
new file mode 100644
index 000000000000..1d5c029e9195
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <zinc/curve25519.h>
+#include <asm/hwcap.h>
+#include <asm/neon.h>
+#include <asm/simd.h>
+
+asmlinkage void curve25519_neon(u8 mypublic[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE], const u8 basepoint[CURVE25519_POINT_SIZE]);
+
+static bool curve25519_use_neon __ro_after_init;
+
+void __init curve25519_fpu_init(void)
+{
+	curve25519_use_neon = elf_hwcap & HWCAP_NEON;
+}
+
+static inline bool curve25519_arch(u8 mypublic[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE], const u8 basepoint[CURVE25519_POINT_SIZE])
+{
+	if (curve25519_use_neon && may_use_simd()) {
+		kernel_neon_begin();
+		curve25519_neon(mypublic, secret, basepoint);
+		kernel_neon_end();
+		return true;
+	}
+	return false;
+}
+
+static inline bool curve25519_base_arch(u8 pub[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE]) { return false; }
+
+#define HAVE_CURVE25519_ARCH_IMPLEMENTATION
lib/zinc/curve25519/curve25519-arm.S new file +2110 -0

2,110 lines not on this page · raw message

Signature · 1 line
2.18.0
#37 lore raw

Re: [PATCH v2 15/17] zinc: Curve25519 ARM implementation

Quoted 12 lines · Jason A. Donenfeld

This comes from Dan Bernstein and Peter Schwabe's public domain NEON code, and has been modified to be friendly for kernel space.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc D. J. Bernstein <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
Cc linux-crypto at vger.kernel.org
Quoted 4 lines
---
 lib/zinc/Makefile                         |    4 +
 lib/zinc/curve25519/curve25519-arm-glue.h |   33 +
 lib/zinc/curve25519/curve25519-arm.S      | 2110 +++++++++++++++++++++

As discussed in the context of patch #2, I have my doubts about the general approach you are taking. I think this code belongs in arch/arm somewhere, exposed in a way so it naturally supersedes a dummy implementation by library search order.

In the mean time, let me comment on the code in this patch.

Quoted 9 lines
 3 files changed, 2147 insertions(+)
 create mode 100644 lib/zinc/curve25519/curve25519-arm-glue.h
 create mode 100644 lib/zinc/curve25519/curve25519-arm.S
lib/zinc/Makefile +0 -0
index b6b0614f8743..13e403780ce0 100644

lib/zinc/Makefile endif · line 57

Quoted 6 lines
lib/zinc/Makefile +3 -0
@@ -52,6 +52,10 @@ endif
 
 ifeq ($(CONFIG_ZINC_CURVE25519),y)
 zinc-y += curve25519/curve25519.o
+ifeq ($(CONFIG_ARM)$(CONFIG_KERNEL_MODE_NEON),yy)
+zinc-y += curve25519/curve25519-arm.o
+CFLAGS_curve25519.o += -include $(srctree)/$(src)/curve25519/curve25519-arm-glue.h

If you put the code below in arch/arm/lib/curve25519.c, you don't need this clunkiness.

Quoted 12 lines
lib/zinc/Makefile +1 -0
+endif
 endif
 
 ifeq ($(CONFIG_ZINC_BLAKE2S),y)
lib/zinc/curve25519/curve25519-arm-glue.h new file +2 -0
new file mode 100644
index 000000000000..1d5c029e9195
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *

lib/zinc/curve25519/curve25519-arm-glue.h line 12

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm-glue.h new file +10 -0
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <zinc/curve25519.h>
+#include <asm/hwcap.h>
+#include <asm/neon.h>
+#include <asm/simd.h>
+
+asmlinkage void curve25519_neon(u8 mypublic[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE], const u8 basepoint[CURVE25519_POINT_SIZE]);
+

Please follow the coding style of the kernel.

Quoted 27 lines
lib/zinc/curve25519/curve25519-arm-glue.h new file +21 -0
+static bool curve25519_use_neon __ro_after_init;
+
+void __init curve25519_fpu_init(void)
+{
+       curve25519_use_neon = elf_hwcap & HWCAP_NEON;
+}
+
+static inline bool curve25519_arch(u8 mypublic[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE], const u8 basepoint[CURVE25519_POINT_SIZE])
+{
+       if (curve25519_use_neon && may_use_simd()) {
+               kernel_neon_begin();
+               curve25519_neon(mypublic, secret, basepoint);
+               kernel_neon_end();
+               return true;
+       }
+       return false;
+}
+
+static inline bool curve25519_base_arch(u8 pub[CURVE25519_POINT_SIZE], const u8 secret[CURVE25519_POINT_SIZE]) { return false; }
+
+#define HAVE_CURVE25519_ARCH_IMPLEMENTATION
lib/zinc/curve25519/curve25519-arm.S new file +0 -0
new file mode 100644
index 000000000000..2c02e66cc87c

lib/zinc/curve25519/curve25519-arm.S line 9

Quoted 9 lines
lib/zinc/curve25519/curve25519-arm.S new file +9 -0
@@ -0,0 +1,2110 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ *
+ * Based on public domain code from Daniel J. Bernstein and Peter Schwabe.
+ */
+
+#if IS_ENABLED(CONFIG_KERNEL_MODE_NEON)
+

This #if is redundant - you only build the file if it is true.

lib/zinc/curve25519/curve25519-arm.S line 16

Quoted 7 lines
lib/zinc/curve25519/curve25519-arm.S new file +7 -0
+#include <linux/linkage.h>
+
+       .text
+       .fpu            neon
+       .align          4
+
+ENTRY(curve25519_neon)

Could you please indent the asm with two tabs for legibility?

lib/zinc/curve25519/curve25519-arm.S line 17

Quoted 1 line
lib/zinc/curve25519/curve25519-arm.S new file +1 -0
+       vpush {q4,q5,q6,q7}

You can drop this

lib/zinc/curve25519/curve25519-arm.S line 20

Quoted 3 lines
lib/zinc/curve25519/curve25519-arm.S new file +3 -0
+       mov r12,sp
+       sub r3,sp,#736
+       and r3,r3,#0xffffffe0

I don't think you need 32 byte alignment, 16 bytes should be sufficient

lib/zinc/curve25519/curve25519-arm.S line 27

Quoted 7 lines
lib/zinc/curve25519/curve25519-arm.S new file +7 -0
+       mov sp,r3
+       strd r4,[sp,#0]
+       strd r6,[sp,#8]
+       strd r8,[sp,#16]
+       strd r10,[sp,#24]
+       str r12,[sp,#480]
+       str r14,[sp,#484]

This is a very odd way to create a stack frame.

lib/zinc/curve25519/curve25519-arm.S line 30

Quoted 3 lines
lib/zinc/curve25519/curve25519-arm.S new file +3 -0
+       mov r0,r0
+       mov r1,r1
+       mov r2,r2

Drop these

lib/zinc/curve25519/curve25519-arm.S line 33

Quoted 3 lines
lib/zinc/curve25519/curve25519-arm.S new file +3 -0
+       add r3,sp,#32
+       ldr r4,=0
+       ldr r5,=254

NEON implies v7, so you can use movw for any 16-bit immediate. No need for ldr.

Quoted 12 lines
lib/zinc/curve25519/curve25519-arm.S new file +12 -0
+       vmov.i32 q0,#1
+       vshr.u64 q1,q0,#7
+       vshr.u64 q0,q0,#8
+       vmov.i32 d4,#19
+       vmov.i32 d5,#38
+       add r6,sp,#512
+       vst1.8 {d2-d3},[r6,: 128]
+       add r6,sp,#528
+       vst1.8 {d0-d1},[r6,: 128]
+       add r6,sp,#544
+       vst1.8 {d4-d5},[r6,: 128]
+       add r6,r3,#0

lib/zinc/curve25519/curve25519-arm.S line 54

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +9 -0
+       vmov.i32 q2,#0
+       vst1.8 {d4-d5},[r6,: 128]!
+       vst1.8 {d4-d5},[r6,: 128]!
+       vst1.8 d4,[r6,: 64]
+       add r6,r3,#0
 
+       ldr r7,=960
+       sub r7,r7,#2
+       neg r7,r7
+       sub r7,r7,r7,LSL #7

What is this little dance for?

Quoted 154 lines
lib/zinc/curve25519/curve25519-arm.S new file +154 -0
+       str r7,[r6]
+       add r6,sp,#704
+       vld1.8 {d4-d5},[r1]!
+       vld1.8 {d6-d7},[r1]
+       vst1.8 {d4-d5},[r6,: 128]!
+       vst1.8 {d6-d7},[r6,: 128]
+       sub r1,r6,#16
+       ldrb r6,[r1]
+       and r6,r6,#248
+       strb r6,[r1]
+       ldrb r6,[r1,#31]
+       and r6,r6,#127
+       orr r6,r6,#64
+       strb r6,[r1,#31]
+       vmov.i64 q2,#0xffffffff
+       vshr.u64 q3,q2,#7
+       vshr.u64 q2,q2,#6
+       vld1.8 {d8},[r2]
+       vld1.8 {d10},[r2]
+       add r2,r2,#6
+       vld1.8 {d12},[r2]
+       vld1.8 {d14},[r2]
+       add r2,r2,#6
+       vld1.8 {d16},[r2]
+       add r2,r2,#4
+       vld1.8 {d18},[r2]
+       vld1.8 {d20},[r2]
+       add r2,r2,#6
+       vld1.8 {d22},[r2]
+       add r2,r2,#2
+       vld1.8 {d24},[r2]
+       vld1.8 {d26},[r2]
+       vshr.u64 q5,q5,#26
+       vshr.u64 q6,q6,#3
+       vshr.u64 q7,q7,#29
+       vshr.u64 q8,q8,#6
+       vshr.u64 q10,q10,#25
+       vshr.u64 q11,q11,#3
+       vshr.u64 q12,q12,#12
+       vshr.u64 q13,q13,#38
+       vand q4,q4,q2
+       vand q6,q6,q2
+       vand q8,q8,q2
+       vand q10,q10,q2
+       vand q2,q12,q2
+       vand q5,q5,q3
+       vand q7,q7,q3
+       vand q9,q9,q3
+       vand q11,q11,q3
+       vand q3,q13,q3
+       add r2,r3,#48
+       vadd.i64 q12,q4,q1
+       vadd.i64 q13,q10,q1
+       vshr.s64 q12,q12,#26
+       vshr.s64 q13,q13,#26
+       vadd.i64 q5,q5,q12
+       vshl.i64 q12,q12,#26
+       vadd.i64 q14,q5,q0
+       vadd.i64 q11,q11,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q15,q11,q0
+       vsub.i64 q4,q4,q12
+       vshr.s64 q12,q14,#25
+       vsub.i64 q10,q10,q13
+       vshr.s64 q13,q15,#25
+       vadd.i64 q6,q6,q12
+       vshl.i64 q12,q12,#25
+       vadd.i64 q14,q6,q1
+       vadd.i64 q2,q2,q13
+       vsub.i64 q5,q5,q12
+       vshr.s64 q12,q14,#26
+       vshl.i64 q13,q13,#25
+       vadd.i64 q14,q2,q1
+       vadd.i64 q7,q7,q12
+       vshl.i64 q12,q12,#26
+       vadd.i64 q15,q7,q0
+       vsub.i64 q11,q11,q13
+       vshr.s64 q13,q14,#26
+       vsub.i64 q6,q6,q12
+       vshr.s64 q12,q15,#25
+       vadd.i64 q3,q3,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q14,q3,q0
+       vadd.i64 q8,q8,q12
+       vshl.i64 q12,q12,#25
+       vadd.i64 q15,q8,q1
+       add r2,r2,#8
+       vsub.i64 q2,q2,q13
+       vshr.s64 q13,q14,#25
+       vsub.i64 q7,q7,q12
+       vshr.s64 q12,q15,#26
+       vadd.i64 q14,q13,q13
+       vadd.i64 q9,q9,q12
+       vtrn.32 d12,d14
+       vshl.i64 q12,q12,#26
+       vtrn.32 d13,d15
+       vadd.i64 q0,q9,q0
+       vadd.i64 q4,q4,q14
+       vst1.8 d12,[r2,: 64]!
+       vshl.i64 q6,q13,#4
+       vsub.i64 q7,q8,q12
+       vshr.s64 q0,q0,#25
+       vadd.i64 q4,q4,q6
+       vadd.i64 q6,q10,q0
+       vshl.i64 q0,q0,#25
+       vadd.i64 q8,q6,q1
+       vadd.i64 q4,q4,q13
+       vshl.i64 q10,q13,#25
+       vadd.i64 q1,q4,q1
+       vsub.i64 q0,q9,q0
+       vshr.s64 q8,q8,#26
+       vsub.i64 q3,q3,q10
+       vtrn.32 d14,d0
+       vshr.s64 q1,q1,#26
+       vtrn.32 d15,d1
+       vadd.i64 q0,q11,q8
+       vst1.8 d14,[r2,: 64]
+       vshl.i64 q7,q8,#26
+       vadd.i64 q5,q5,q1
+       vtrn.32 d4,d6
+       vshl.i64 q1,q1,#26
+       vtrn.32 d5,d7
+       vsub.i64 q3,q6,q7
+       add r2,r2,#16
+       vsub.i64 q1,q4,q1
+       vst1.8 d4,[r2,: 64]
+       vtrn.32 d6,d0
+       vtrn.32 d7,d1
+       sub r2,r2,#8
+       vtrn.32 d2,d10
+       vtrn.32 d3,d11
+       vst1.8 d6,[r2,: 64]
+       sub r2,r2,#24
+       vst1.8 d2,[r2,: 64]
+       add r2,r3,#96
+       vmov.i32 q0,#0
+       vmov.i64 d2,#0xff
+       vmov.i64 d3,#0
+       vshr.u32 q1,q1,#7
+       vst1.8 {d2-d3},[r2,: 128]!
+       vst1.8 {d0-d1},[r2,: 128]!
+       vst1.8 d0,[r2,: 64]
+       add r2,r3,#144
+       vmov.i32 q0,#0
+       vst1.8 {d0-d1},[r2,: 128]!
+       vst1.8 {d0-d1},[r2,: 128]!
+       vst1.8 d0,[r2,: 64]
+       add r2,r3,#240
+       vmov.i32 q0,#0
+       vmov.i64 d2,#0xff
+       vmov.i64 d3,#0
+       vshr.u32 q1,q1,#7
+       vst1.8 {d2-d3},[r2,: 128]!
+       vst1.8 {d0-d1},[r2,: 128]!

lib/zinc/curve25519/curve25519-arm.S line 218

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       vst1.8 d0,[r2,: 64]
+       add r2,r3,#48
+       add r6,r3,#192
+       vld1.8 {d0-d1},[r2,: 128]!
+       vld1.8 {d2-d3},[r2,: 128]!
+       vld1.8 {d4},[r2,: 64]
+       vst1.8 {d0-d1},[r6,: 128]!
+       vst1.8 {d2-d3},[r6,: 128]!
+       vst1.8 d4,[r6,: 64]
+       .Lmainloop:

Please put labels in the first column.

Quoted 1,870 lines
lib/zinc/curve25519/curve25519-arm.S new file +1870 -0
+       mov r2,r5,LSR #3
+       and r6,r5,#7
+       ldrb r2,[r1,r2]
+       mov r2,r2,LSR r6
+       and r2,r2,#1
+       str r5,[sp,#488]
+       eor r4,r4,r2
+       str r2,[sp,#492]
+       neg r2,r4
+       add r4,r3,#96
+       add r5,r3,#192
+       add r6,r3,#144
+       vld1.8 {d8-d9},[r4,: 128]!
+       add r7,r3,#240
+       vld1.8 {d10-d11},[r5,: 128]!
+       veor q6,q4,q5
+       vld1.8 {d14-d15},[r6,: 128]!
+       vdup.i32 q8,r2
+       vld1.8 {d18-d19},[r7,: 128]!
+       veor q10,q7,q9
+       vld1.8 {d22-d23},[r4,: 128]!
+       vand q6,q6,q8
+       vld1.8 {d24-d25},[r5,: 128]!
+       vand q10,q10,q8
+       vld1.8 {d26-d27},[r6,: 128]!
+       veor q4,q4,q6
+       vld1.8 {d28-d29},[r7,: 128]!
+       veor q5,q5,q6
+       vld1.8 {d0},[r4,: 64]
+       veor q6,q7,q10
+       vld1.8 {d2},[r5,: 64]
+       veor q7,q9,q10
+       vld1.8 {d4},[r6,: 64]
+       veor q9,q11,q12
+       vld1.8 {d6},[r7,: 64]
+       veor q10,q0,q1
+       sub r2,r4,#32
+       vand q9,q9,q8
+       sub r4,r5,#32
+       vand q10,q10,q8
+       sub r5,r6,#32
+       veor q11,q11,q9
+       sub r6,r7,#32
+       veor q0,q0,q10
+       veor q9,q12,q9
+       veor q1,q1,q10
+       veor q10,q13,q14
+       veor q12,q2,q3
+       vand q10,q10,q8
+       vand q8,q12,q8
+       veor q12,q13,q10
+       veor q2,q2,q8
+       veor q10,q14,q10
+       veor q3,q3,q8
+       vadd.i32 q8,q4,q6
+       vsub.i32 q4,q4,q6
+       vst1.8 {d16-d17},[r2,: 128]!
+       vadd.i32 q6,q11,q12
+       vst1.8 {d8-d9},[r5,: 128]!
+       vsub.i32 q4,q11,q12
+       vst1.8 {d12-d13},[r2,: 128]!
+       vadd.i32 q6,q0,q2
+       vst1.8 {d8-d9},[r5,: 128]!
+       vsub.i32 q0,q0,q2
+       vst1.8 d12,[r2,: 64]
+       vadd.i32 q2,q5,q7
+       vst1.8 d0,[r5,: 64]
+       vsub.i32 q0,q5,q7
+       vst1.8 {d4-d5},[r4,: 128]!
+       vadd.i32 q2,q9,q10
+       vst1.8 {d0-d1},[r6,: 128]!
+       vsub.i32 q0,q9,q10
+       vst1.8 {d4-d5},[r4,: 128]!
+       vadd.i32 q2,q1,q3
+       vst1.8 {d0-d1},[r6,: 128]!
+       vsub.i32 q0,q1,q3
+       vst1.8 d4,[r4,: 64]
+       vst1.8 d0,[r6,: 64]
+       add r2,sp,#544
+       add r4,r3,#96
+       add r5,r3,#144
+       vld1.8 {d0-d1},[r2,: 128]
+       vld1.8 {d2-d3},[r4,: 128]!
+       vld1.8 {d4-d5},[r5,: 128]!
+       vzip.i32 q1,q2
+       vld1.8 {d6-d7},[r4,: 128]!
+       vld1.8 {d8-d9},[r5,: 128]!
+       vshl.i32 q5,q1,#1
+       vzip.i32 q3,q4
+       vshl.i32 q6,q2,#1
+       vld1.8 {d14},[r4,: 64]
+       vshl.i32 q8,q3,#1
+       vld1.8 {d15},[r5,: 64]
+       vshl.i32 q9,q4,#1
+       vmul.i32 d21,d7,d1
+       vtrn.32 d14,d15
+       vmul.i32 q11,q4,q0
+       vmul.i32 q0,q7,q0
+       vmull.s32 q12,d2,d2
+       vmlal.s32 q12,d11,d1
+       vmlal.s32 q12,d12,d0
+       vmlal.s32 q12,d13,d23
+       vmlal.s32 q12,d16,d22
+       vmlal.s32 q12,d7,d21
+       vmull.s32 q10,d2,d11
+       vmlal.s32 q10,d4,d1
+       vmlal.s32 q10,d13,d0
+       vmlal.s32 q10,d6,d23
+       vmlal.s32 q10,d17,d22
+       vmull.s32 q13,d10,d4
+       vmlal.s32 q13,d11,d3
+       vmlal.s32 q13,d13,d1
+       vmlal.s32 q13,d16,d0
+       vmlal.s32 q13,d17,d23
+       vmlal.s32 q13,d8,d22
+       vmull.s32 q1,d10,d5
+       vmlal.s32 q1,d11,d4
+       vmlal.s32 q1,d6,d1
+       vmlal.s32 q1,d17,d0
+       vmlal.s32 q1,d8,d23
+       vmull.s32 q14,d10,d6
+       vmlal.s32 q14,d11,d13
+       vmlal.s32 q14,d4,d4
+       vmlal.s32 q14,d17,d1
+       vmlal.s32 q14,d18,d0
+       vmlal.s32 q14,d9,d23
+       vmull.s32 q11,d10,d7
+       vmlal.s32 q11,d11,d6
+       vmlal.s32 q11,d12,d5
+       vmlal.s32 q11,d8,d1
+       vmlal.s32 q11,d19,d0
+       vmull.s32 q15,d10,d8
+       vmlal.s32 q15,d11,d17
+       vmlal.s32 q15,d12,d6
+       vmlal.s32 q15,d13,d5
+       vmlal.s32 q15,d19,d1
+       vmlal.s32 q15,d14,d0
+       vmull.s32 q2,d10,d9
+       vmlal.s32 q2,d11,d8
+       vmlal.s32 q2,d12,d7
+       vmlal.s32 q2,d13,d6
+       vmlal.s32 q2,d14,d1
+       vmull.s32 q0,d15,d1
+       vmlal.s32 q0,d10,d14
+       vmlal.s32 q0,d11,d19
+       vmlal.s32 q0,d12,d8
+       vmlal.s32 q0,d13,d17
+       vmlal.s32 q0,d6,d6
+       add r2,sp,#512
+       vld1.8 {d18-d19},[r2,: 128]
+       vmull.s32 q3,d16,d7
+       vmlal.s32 q3,d10,d15
+       vmlal.s32 q3,d11,d14
+       vmlal.s32 q3,d12,d9
+       vmlal.s32 q3,d13,d8
+       add r2,sp,#528
+       vld1.8 {d8-d9},[r2,: 128]
+       vadd.i64 q5,q12,q9
+       vadd.i64 q6,q15,q9
+       vshr.s64 q5,q5,#26
+       vshr.s64 q6,q6,#26
+       vadd.i64 q7,q10,q5
+       vshl.i64 q5,q5,#26
+       vadd.i64 q8,q7,q4
+       vadd.i64 q2,q2,q6
+       vshl.i64 q6,q6,#26
+       vadd.i64 q10,q2,q4
+       vsub.i64 q5,q12,q5
+       vshr.s64 q8,q8,#25
+       vsub.i64 q6,q15,q6
+       vshr.s64 q10,q10,#25
+       vadd.i64 q12,q13,q8
+       vshl.i64 q8,q8,#25
+       vadd.i64 q13,q12,q9
+       vadd.i64 q0,q0,q10
+       vsub.i64 q7,q7,q8
+       vshr.s64 q8,q13,#26
+       vshl.i64 q10,q10,#25
+       vadd.i64 q13,q0,q9
+       vadd.i64 q1,q1,q8
+       vshl.i64 q8,q8,#26
+       vadd.i64 q15,q1,q4
+       vsub.i64 q2,q2,q10
+       vshr.s64 q10,q13,#26
+       vsub.i64 q8,q12,q8
+       vshr.s64 q12,q15,#25
+       vadd.i64 q3,q3,q10
+       vshl.i64 q10,q10,#26
+       vadd.i64 q13,q3,q4
+       vadd.i64 q14,q14,q12
+       add r2,r3,#288
+       vshl.i64 q12,q12,#25
+       add r4,r3,#336
+       vadd.i64 q15,q14,q9
+       add r2,r2,#8
+       vsub.i64 q0,q0,q10
+       add r4,r4,#8
+       vshr.s64 q10,q13,#25
+       vsub.i64 q1,q1,q12
+       vshr.s64 q12,q15,#26
+       vadd.i64 q13,q10,q10
+       vadd.i64 q11,q11,q12
+       vtrn.32 d16,d2
+       vshl.i64 q12,q12,#26
+       vtrn.32 d17,d3
+       vadd.i64 q1,q11,q4
+       vadd.i64 q4,q5,q13
+       vst1.8 d16,[r2,: 64]!
+       vshl.i64 q5,q10,#4
+       vst1.8 d17,[r4,: 64]!
+       vsub.i64 q8,q14,q12
+       vshr.s64 q1,q1,#25
+       vadd.i64 q4,q4,q5
+       vadd.i64 q5,q6,q1
+       vshl.i64 q1,q1,#25
+       vadd.i64 q6,q5,q9
+       vadd.i64 q4,q4,q10
+       vshl.i64 q10,q10,#25
+       vadd.i64 q9,q4,q9
+       vsub.i64 q1,q11,q1
+       vshr.s64 q6,q6,#26
+       vsub.i64 q3,q3,q10
+       vtrn.32 d16,d2
+       vshr.s64 q9,q9,#26
+       vtrn.32 d17,d3
+       vadd.i64 q1,q2,q6
+       vst1.8 d16,[r2,: 64]
+       vshl.i64 q2,q6,#26
+       vst1.8 d17,[r4,: 64]
+       vadd.i64 q6,q7,q9
+       vtrn.32 d0,d6
+       vshl.i64 q7,q9,#26
+       vtrn.32 d1,d7
+       vsub.i64 q2,q5,q2
+       add r2,r2,#16
+       vsub.i64 q3,q4,q7
+       vst1.8 d0,[r2,: 64]
+       add r4,r4,#16
+       vst1.8 d1,[r4,: 64]
+       vtrn.32 d4,d2
+       vtrn.32 d5,d3
+       sub r2,r2,#8
+       sub r4,r4,#8
+       vtrn.32 d6,d12
+       vtrn.32 d7,d13
+       vst1.8 d4,[r2,: 64]
+       vst1.8 d5,[r4,: 64]
+       sub r2,r2,#24
+       sub r4,r4,#24
+       vst1.8 d6,[r2,: 64]
+       vst1.8 d7,[r4,: 64]
+       add r2,r3,#240
+       add r4,r3,#96
+       vld1.8 {d0-d1},[r4,: 128]!
+       vld1.8 {d2-d3},[r4,: 128]!
+       vld1.8 {d4},[r4,: 64]
+       add r4,r3,#144
+       vld1.8 {d6-d7},[r4,: 128]!
+       vtrn.32 q0,q3
+       vld1.8 {d8-d9},[r4,: 128]!
+       vshl.i32 q5,q0,#4
+       vtrn.32 q1,q4
+       vshl.i32 q6,q3,#4
+       vadd.i32 q5,q5,q0
+       vadd.i32 q6,q6,q3
+       vshl.i32 q7,q1,#4
+       vld1.8 {d5},[r4,: 64]
+       vshl.i32 q8,q4,#4
+       vtrn.32 d4,d5
+       vadd.i32 q7,q7,q1
+       vadd.i32 q8,q8,q4
+       vld1.8 {d18-d19},[r2,: 128]!
+       vshl.i32 q10,q2,#4
+       vld1.8 {d22-d23},[r2,: 128]!
+       vadd.i32 q10,q10,q2
+       vld1.8 {d24},[r2,: 64]
+       vadd.i32 q5,q5,q0
+       add r2,r3,#192
+       vld1.8 {d26-d27},[r2,: 128]!
+       vadd.i32 q6,q6,q3
+       vld1.8 {d28-d29},[r2,: 128]!
+       vadd.i32 q8,q8,q4
+       vld1.8 {d25},[r2,: 64]
+       vadd.i32 q10,q10,q2
+       vtrn.32 q9,q13
+       vadd.i32 q7,q7,q1
+       vadd.i32 q5,q5,q0
+       vtrn.32 q11,q14
+       vadd.i32 q6,q6,q3
+       add r2,sp,#560
+       vadd.i32 q10,q10,q2
+       vtrn.32 d24,d25
+       vst1.8 {d12-d13},[r2,: 128]
+       vshl.i32 q6,q13,#1
+       add r2,sp,#576
+       vst1.8 {d20-d21},[r2,: 128]
+       vshl.i32 q10,q14,#1
+       add r2,sp,#592
+       vst1.8 {d12-d13},[r2,: 128]
+       vshl.i32 q15,q12,#1
+       vadd.i32 q8,q8,q4
+       vext.32 d10,d31,d30,#0
+       vadd.i32 q7,q7,q1
+       add r2,sp,#608
+       vst1.8 {d16-d17},[r2,: 128]
+       vmull.s32 q8,d18,d5
+       vmlal.s32 q8,d26,d4
+       vmlal.s32 q8,d19,d9
+       vmlal.s32 q8,d27,d3
+       vmlal.s32 q8,d22,d8
+       vmlal.s32 q8,d28,d2
+       vmlal.s32 q8,d23,d7
+       vmlal.s32 q8,d29,d1
+       vmlal.s32 q8,d24,d6
+       vmlal.s32 q8,d25,d0
+       add r2,sp,#624
+       vst1.8 {d14-d15},[r2,: 128]
+       vmull.s32 q2,d18,d4
+       vmlal.s32 q2,d12,d9
+       vmlal.s32 q2,d13,d8
+       vmlal.s32 q2,d19,d3
+       vmlal.s32 q2,d22,d2
+       vmlal.s32 q2,d23,d1
+       vmlal.s32 q2,d24,d0
+       add r2,sp,#640
+       vst1.8 {d20-d21},[r2,: 128]
+       vmull.s32 q7,d18,d9
+       vmlal.s32 q7,d26,d3
+       vmlal.s32 q7,d19,d8
+       vmlal.s32 q7,d27,d2
+       vmlal.s32 q7,d22,d7
+       vmlal.s32 q7,d28,d1
+       vmlal.s32 q7,d23,d6
+       vmlal.s32 q7,d29,d0
+       add r2,sp,#656
+       vst1.8 {d10-d11},[r2,: 128]
+       vmull.s32 q5,d18,d3
+       vmlal.s32 q5,d19,d2
+       vmlal.s32 q5,d22,d1
+       vmlal.s32 q5,d23,d0
+       vmlal.s32 q5,d12,d8
+       add r2,sp,#672
+       vst1.8 {d16-d17},[r2,: 128]
+       vmull.s32 q4,d18,d8
+       vmlal.s32 q4,d26,d2
+       vmlal.s32 q4,d19,d7
+       vmlal.s32 q4,d27,d1
+       vmlal.s32 q4,d22,d6
+       vmlal.s32 q4,d28,d0
+       vmull.s32 q8,d18,d7
+       vmlal.s32 q8,d26,d1
+       vmlal.s32 q8,d19,d6
+       vmlal.s32 q8,d27,d0
+       add r2,sp,#576
+       vld1.8 {d20-d21},[r2,: 128]
+       vmlal.s32 q7,d24,d21
+       vmlal.s32 q7,d25,d20
+       vmlal.s32 q4,d23,d21
+       vmlal.s32 q4,d29,d20
+       vmlal.s32 q8,d22,d21
+       vmlal.s32 q8,d28,d20
+       vmlal.s32 q5,d24,d20
+       add r2,sp,#576
+       vst1.8 {d14-d15},[r2,: 128]
+       vmull.s32 q7,d18,d6
+       vmlal.s32 q7,d26,d0
+       add r2,sp,#656
+       vld1.8 {d30-d31},[r2,: 128]
+       vmlal.s32 q2,d30,d21
+       vmlal.s32 q7,d19,d21
+       vmlal.s32 q7,d27,d20
+       add r2,sp,#624
+       vld1.8 {d26-d27},[r2,: 128]
+       vmlal.s32 q4,d25,d27
+       vmlal.s32 q8,d29,d27
+       vmlal.s32 q8,d25,d26
+       vmlal.s32 q7,d28,d27
+       vmlal.s32 q7,d29,d26
+       add r2,sp,#608
+       vld1.8 {d28-d29},[r2,: 128]
+       vmlal.s32 q4,d24,d29
+       vmlal.s32 q8,d23,d29
+       vmlal.s32 q8,d24,d28
+       vmlal.s32 q7,d22,d29
+       vmlal.s32 q7,d23,d28
+       add r2,sp,#608
+       vst1.8 {d8-d9},[r2,: 128]
+       add r2,sp,#560
+       vld1.8 {d8-d9},[r2,: 128]
+       vmlal.s32 q7,d24,d9
+       vmlal.s32 q7,d25,d31
+       vmull.s32 q1,d18,d2
+       vmlal.s32 q1,d19,d1
+       vmlal.s32 q1,d22,d0
+       vmlal.s32 q1,d24,d27
+       vmlal.s32 q1,d23,d20
+       vmlal.s32 q1,d12,d7
+       vmlal.s32 q1,d13,d6
+       vmull.s32 q6,d18,d1
+       vmlal.s32 q6,d19,d0
+       vmlal.s32 q6,d23,d27
+       vmlal.s32 q6,d22,d20
+       vmlal.s32 q6,d24,d26
+       vmull.s32 q0,d18,d0
+       vmlal.s32 q0,d22,d27
+       vmlal.s32 q0,d23,d26
+       vmlal.s32 q0,d24,d31
+       vmlal.s32 q0,d19,d20
+       add r2,sp,#640
+       vld1.8 {d18-d19},[r2,: 128]
+       vmlal.s32 q2,d18,d7
+       vmlal.s32 q2,d19,d6
+       vmlal.s32 q5,d18,d6
+       vmlal.s32 q5,d19,d21
+       vmlal.s32 q1,d18,d21
+       vmlal.s32 q1,d19,d29
+       vmlal.s32 q0,d18,d28
+       vmlal.s32 q0,d19,d9
+       vmlal.s32 q6,d18,d29
+       vmlal.s32 q6,d19,d28
+       add r2,sp,#592
+       vld1.8 {d18-d19},[r2,: 128]
+       add r2,sp,#512
+       vld1.8 {d22-d23},[r2,: 128]
+       vmlal.s32 q5,d19,d7
+       vmlal.s32 q0,d18,d21
+       vmlal.s32 q0,d19,d29
+       vmlal.s32 q6,d18,d6
+       add r2,sp,#528
+       vld1.8 {d6-d7},[r2,: 128]
+       vmlal.s32 q6,d19,d21
+       add r2,sp,#576
+       vld1.8 {d18-d19},[r2,: 128]
+       vmlal.s32 q0,d30,d8
+       add r2,sp,#672
+       vld1.8 {d20-d21},[r2,: 128]
+       vmlal.s32 q5,d30,d29
+       add r2,sp,#608
+       vld1.8 {d24-d25},[r2,: 128]
+       vmlal.s32 q1,d30,d28
+       vadd.i64 q13,q0,q11
+       vadd.i64 q14,q5,q11
+       vmlal.s32 q6,d30,d9
+       vshr.s64 q4,q13,#26
+       vshr.s64 q13,q14,#26
+       vadd.i64 q7,q7,q4
+       vshl.i64 q4,q4,#26
+       vadd.i64 q14,q7,q3
+       vadd.i64 q9,q9,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q15,q9,q3
+       vsub.i64 q0,q0,q4
+       vshr.s64 q4,q14,#25
+       vsub.i64 q5,q5,q13
+       vshr.s64 q13,q15,#25
+       vadd.i64 q6,q6,q4
+       vshl.i64 q4,q4,#25
+       vadd.i64 q14,q6,q11
+       vadd.i64 q2,q2,q13
+       vsub.i64 q4,q7,q4
+       vshr.s64 q7,q14,#26
+       vshl.i64 q13,q13,#25
+       vadd.i64 q14,q2,q11
+       vadd.i64 q8,q8,q7
+       vshl.i64 q7,q7,#26
+       vadd.i64 q15,q8,q3
+       vsub.i64 q9,q9,q13
+       vshr.s64 q13,q14,#26
+       vsub.i64 q6,q6,q7
+       vshr.s64 q7,q15,#25
+       vadd.i64 q10,q10,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q14,q10,q3
+       vadd.i64 q1,q1,q7
+       add r2,r3,#144
+       vshl.i64 q7,q7,#25
+       add r4,r3,#96
+       vadd.i64 q15,q1,q11
+       add r2,r2,#8
+       vsub.i64 q2,q2,q13
+       add r4,r4,#8
+       vshr.s64 q13,q14,#25
+       vsub.i64 q7,q8,q7
+       vshr.s64 q8,q15,#26
+       vadd.i64 q14,q13,q13
+       vadd.i64 q12,q12,q8
+       vtrn.32 d12,d14
+       vshl.i64 q8,q8,#26
+       vtrn.32 d13,d15
+       vadd.i64 q3,q12,q3
+       vadd.i64 q0,q0,q14
+       vst1.8 d12,[r2,: 64]!
+       vshl.i64 q7,q13,#4
+       vst1.8 d13,[r4,: 64]!
+       vsub.i64 q1,q1,q8
+       vshr.s64 q3,q3,#25
+       vadd.i64 q0,q0,q7
+       vadd.i64 q5,q5,q3
+       vshl.i64 q3,q3,#25
+       vadd.i64 q6,q5,q11
+       vadd.i64 q0,q0,q13
+       vshl.i64 q7,q13,#25
+       vadd.i64 q8,q0,q11
+       vsub.i64 q3,q12,q3
+       vshr.s64 q6,q6,#26
+       vsub.i64 q7,q10,q7
+       vtrn.32 d2,d6
+       vshr.s64 q8,q8,#26
+       vtrn.32 d3,d7
+       vadd.i64 q3,q9,q6
+       vst1.8 d2,[r2,: 64]
+       vshl.i64 q6,q6,#26
+       vst1.8 d3,[r4,: 64]
+       vadd.i64 q1,q4,q8
+       vtrn.32 d4,d14
+       vshl.i64 q4,q8,#26
+       vtrn.32 d5,d15
+       vsub.i64 q5,q5,q6
+       add r2,r2,#16
+       vsub.i64 q0,q0,q4
+       vst1.8 d4,[r2,: 64]
+       add r4,r4,#16
+       vst1.8 d5,[r4,: 64]
+       vtrn.32 d10,d6
+       vtrn.32 d11,d7
+       sub r2,r2,#8
+       sub r4,r4,#8
+       vtrn.32 d0,d2
+       vtrn.32 d1,d3
+       vst1.8 d10,[r2,: 64]
+       vst1.8 d11,[r4,: 64]
+       sub r2,r2,#24
+       sub r4,r4,#24
+       vst1.8 d0,[r2,: 64]
+       vst1.8 d1,[r4,: 64]
+       add r2,r3,#288
+       add r4,r3,#336
+       vld1.8 {d0-d1},[r2,: 128]!
+       vld1.8 {d2-d3},[r4,: 128]!
+       vsub.i32 q0,q0,q1
+       vld1.8 {d2-d3},[r2,: 128]!
+       vld1.8 {d4-d5},[r4,: 128]!
+       vsub.i32 q1,q1,q2
+       add r5,r3,#240
+       vld1.8 {d4},[r2,: 64]
+       vld1.8 {d6},[r4,: 64]
+       vsub.i32 q2,q2,q3
+       vst1.8 {d0-d1},[r5,: 128]!
+       vst1.8 {d2-d3},[r5,: 128]!
+       vst1.8 d4,[r5,: 64]
+       add r2,r3,#144
+       add r4,r3,#96
+       add r5,r3,#144
+       add r6,r3,#192
+       vld1.8 {d0-d1},[r2,: 128]!
+       vld1.8 {d2-d3},[r4,: 128]!
+       vsub.i32 q2,q0,q1
+       vadd.i32 q0,q0,q1
+       vld1.8 {d2-d3},[r2,: 128]!
+       vld1.8 {d6-d7},[r4,: 128]!
+       vsub.i32 q4,q1,q3
+       vadd.i32 q1,q1,q3
+       vld1.8 {d6},[r2,: 64]
+       vld1.8 {d10},[r4,: 64]
+       vsub.i32 q6,q3,q5
+       vadd.i32 q3,q3,q5
+       vst1.8 {d4-d5},[r5,: 128]!
+       vst1.8 {d0-d1},[r6,: 128]!
+       vst1.8 {d8-d9},[r5,: 128]!
+       vst1.8 {d2-d3},[r6,: 128]!
+       vst1.8 d12,[r5,: 64]
+       vst1.8 d6,[r6,: 64]
+       add r2,r3,#0
+       add r4,r3,#240
+       vld1.8 {d0-d1},[r4,: 128]!
+       vld1.8 {d2-d3},[r4,: 128]!
+       vld1.8 {d4},[r4,: 64]
+       add r4,r3,#336
+       vld1.8 {d6-d7},[r4,: 128]!
+       vtrn.32 q0,q3
+       vld1.8 {d8-d9},[r4,: 128]!
+       vshl.i32 q5,q0,#4
+       vtrn.32 q1,q4
+       vshl.i32 q6,q3,#4
+       vadd.i32 q5,q5,q0
+       vadd.i32 q6,q6,q3
+       vshl.i32 q7,q1,#4
+       vld1.8 {d5},[r4,: 64]
+       vshl.i32 q8,q4,#4
+       vtrn.32 d4,d5
+       vadd.i32 q7,q7,q1
+       vadd.i32 q8,q8,q4
+       vld1.8 {d18-d19},[r2,: 128]!
+       vshl.i32 q10,q2,#4
+       vld1.8 {d22-d23},[r2,: 128]!
+       vadd.i32 q10,q10,q2
+       vld1.8 {d24},[r2,: 64]
+       vadd.i32 q5,q5,q0
+       add r2,r3,#288
+       vld1.8 {d26-d27},[r2,: 128]!
+       vadd.i32 q6,q6,q3
+       vld1.8 {d28-d29},[r2,: 128]!
+       vadd.i32 q8,q8,q4
+       vld1.8 {d25},[r2,: 64]
+       vadd.i32 q10,q10,q2
+       vtrn.32 q9,q13
+       vadd.i32 q7,q7,q1
+       vadd.i32 q5,q5,q0
+       vtrn.32 q11,q14
+       vadd.i32 q6,q6,q3
+       add r2,sp,#560
+       vadd.i32 q10,q10,q2
+       vtrn.32 d24,d25
+       vst1.8 {d12-d13},[r2,: 128]
+       vshl.i32 q6,q13,#1
+       add r2,sp,#576
+       vst1.8 {d20-d21},[r2,: 128]
+       vshl.i32 q10,q14,#1
+       add r2,sp,#592
+       vst1.8 {d12-d13},[r2,: 128]
+       vshl.i32 q15,q12,#1
+       vadd.i32 q8,q8,q4
+       vext.32 d10,d31,d30,#0
+       vadd.i32 q7,q7,q1
+       add r2,sp,#608
+       vst1.8 {d16-d17},[r2,: 128]
+       vmull.s32 q8,d18,d5
+       vmlal.s32 q8,d26,d4
+       vmlal.s32 q8,d19,d9
+       vmlal.s32 q8,d27,d3
+       vmlal.s32 q8,d22,d8
+       vmlal.s32 q8,d28,d2
+       vmlal.s32 q8,d23,d7
+       vmlal.s32 q8,d29,d1
+       vmlal.s32 q8,d24,d6
+       vmlal.s32 q8,d25,d0
+       add r2,sp,#624
+       vst1.8 {d14-d15},[r2,: 128]
+       vmull.s32 q2,d18,d4
+       vmlal.s32 q2,d12,d9
+       vmlal.s32 q2,d13,d8
+       vmlal.s32 q2,d19,d3
+       vmlal.s32 q2,d22,d2
+       vmlal.s32 q2,d23,d1
+       vmlal.s32 q2,d24,d0
+       add r2,sp,#640
+       vst1.8 {d20-d21},[r2,: 128]
+       vmull.s32 q7,d18,d9
+       vmlal.s32 q7,d26,d3
+       vmlal.s32 q7,d19,d8
+       vmlal.s32 q7,d27,d2
+       vmlal.s32 q7,d22,d7
+       vmlal.s32 q7,d28,d1
+       vmlal.s32 q7,d23,d6
+       vmlal.s32 q7,d29,d0
+       add r2,sp,#656
+       vst1.8 {d10-d11},[r2,: 128]
+       vmull.s32 q5,d18,d3
+       vmlal.s32 q5,d19,d2
+       vmlal.s32 q5,d22,d1
+       vmlal.s32 q5,d23,d0
+       vmlal.s32 q5,d12,d8
+       add r2,sp,#672
+       vst1.8 {d16-d17},[r2,: 128]
+       vmull.s32 q4,d18,d8
+       vmlal.s32 q4,d26,d2
+       vmlal.s32 q4,d19,d7
+       vmlal.s32 q4,d27,d1
+       vmlal.s32 q4,d22,d6
+       vmlal.s32 q4,d28,d0
+       vmull.s32 q8,d18,d7
+       vmlal.s32 q8,d26,d1
+       vmlal.s32 q8,d19,d6
+       vmlal.s32 q8,d27,d0
+       add r2,sp,#576
+       vld1.8 {d20-d21},[r2,: 128]
+       vmlal.s32 q7,d24,d21
+       vmlal.s32 q7,d25,d20
+       vmlal.s32 q4,d23,d21
+       vmlal.s32 q4,d29,d20
+       vmlal.s32 q8,d22,d21
+       vmlal.s32 q8,d28,d20
+       vmlal.s32 q5,d24,d20
+       add r2,sp,#576
+       vst1.8 {d14-d15},[r2,: 128]
+       vmull.s32 q7,d18,d6
+       vmlal.s32 q7,d26,d0
+       add r2,sp,#656
+       vld1.8 {d30-d31},[r2,: 128]
+       vmlal.s32 q2,d30,d21
+       vmlal.s32 q7,d19,d21
+       vmlal.s32 q7,d27,d20
+       add r2,sp,#624
+       vld1.8 {d26-d27},[r2,: 128]
+       vmlal.s32 q4,d25,d27
+       vmlal.s32 q8,d29,d27
+       vmlal.s32 q8,d25,d26
+       vmlal.s32 q7,d28,d27
+       vmlal.s32 q7,d29,d26
+       add r2,sp,#608
+       vld1.8 {d28-d29},[r2,: 128]
+       vmlal.s32 q4,d24,d29
+       vmlal.s32 q8,d23,d29
+       vmlal.s32 q8,d24,d28
+       vmlal.s32 q7,d22,d29
+       vmlal.s32 q7,d23,d28
+       add r2,sp,#608
+       vst1.8 {d8-d9},[r2,: 128]
+       add r2,sp,#560
+       vld1.8 {d8-d9},[r2,: 128]
+       vmlal.s32 q7,d24,d9
+       vmlal.s32 q7,d25,d31
+       vmull.s32 q1,d18,d2
+       vmlal.s32 q1,d19,d1
+       vmlal.s32 q1,d22,d0
+       vmlal.s32 q1,d24,d27
+       vmlal.s32 q1,d23,d20
+       vmlal.s32 q1,d12,d7
+       vmlal.s32 q1,d13,d6
+       vmull.s32 q6,d18,d1
+       vmlal.s32 q6,d19,d0
+       vmlal.s32 q6,d23,d27
+       vmlal.s32 q6,d22,d20
+       vmlal.s32 q6,d24,d26
+       vmull.s32 q0,d18,d0
+       vmlal.s32 q0,d22,d27
+       vmlal.s32 q0,d23,d26
+       vmlal.s32 q0,d24,d31
+       vmlal.s32 q0,d19,d20
+       add r2,sp,#640
+       vld1.8 {d18-d19},[r2,: 128]
+       vmlal.s32 q2,d18,d7
+       vmlal.s32 q2,d19,d6
+       vmlal.s32 q5,d18,d6
+       vmlal.s32 q5,d19,d21
+       vmlal.s32 q1,d18,d21
+       vmlal.s32 q1,d19,d29
+       vmlal.s32 q0,d18,d28
+       vmlal.s32 q0,d19,d9
+       vmlal.s32 q6,d18,d29
+       vmlal.s32 q6,d19,d28
+       add r2,sp,#592
+       vld1.8 {d18-d19},[r2,: 128]
+       add r2,sp,#512
+       vld1.8 {d22-d23},[r2,: 128]
+       vmlal.s32 q5,d19,d7
+       vmlal.s32 q0,d18,d21
+       vmlal.s32 q0,d19,d29
+       vmlal.s32 q6,d18,d6
+       add r2,sp,#528
+       vld1.8 {d6-d7},[r2,: 128]
+       vmlal.s32 q6,d19,d21
+       add r2,sp,#576
+       vld1.8 {d18-d19},[r2,: 128]
+       vmlal.s32 q0,d30,d8
+       add r2,sp,#672
+       vld1.8 {d20-d21},[r2,: 128]
+       vmlal.s32 q5,d30,d29
+       add r2,sp,#608
+       vld1.8 {d24-d25},[r2,: 128]
+       vmlal.s32 q1,d30,d28
+       vadd.i64 q13,q0,q11
+       vadd.i64 q14,q5,q11
+       vmlal.s32 q6,d30,d9
+       vshr.s64 q4,q13,#26
+       vshr.s64 q13,q14,#26
+       vadd.i64 q7,q7,q4
+       vshl.i64 q4,q4,#26
+       vadd.i64 q14,q7,q3
+       vadd.i64 q9,q9,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q15,q9,q3
+       vsub.i64 q0,q0,q4
+       vshr.s64 q4,q14,#25
+       vsub.i64 q5,q5,q13
+       vshr.s64 q13,q15,#25
+       vadd.i64 q6,q6,q4
+       vshl.i64 q4,q4,#25
+       vadd.i64 q14,q6,q11
+       vadd.i64 q2,q2,q13
+       vsub.i64 q4,q7,q4
+       vshr.s64 q7,q14,#26
+       vshl.i64 q13,q13,#25
+       vadd.i64 q14,q2,q11
+       vadd.i64 q8,q8,q7
+       vshl.i64 q7,q7,#26
+       vadd.i64 q15,q8,q3
+       vsub.i64 q9,q9,q13
+       vshr.s64 q13,q14,#26
+       vsub.i64 q6,q6,q7
+       vshr.s64 q7,q15,#25
+       vadd.i64 q10,q10,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q14,q10,q3
+       vadd.i64 q1,q1,q7
+       add r2,r3,#288
+       vshl.i64 q7,q7,#25
+       add r4,r3,#96
+       vadd.i64 q15,q1,q11
+       add r2,r2,#8
+       vsub.i64 q2,q2,q13
+       add r4,r4,#8
+       vshr.s64 q13,q14,#25
+       vsub.i64 q7,q8,q7
+       vshr.s64 q8,q15,#26
+       vadd.i64 q14,q13,q13
+       vadd.i64 q12,q12,q8
+       vtrn.32 d12,d14
+       vshl.i64 q8,q8,#26
+       vtrn.32 d13,d15
+       vadd.i64 q3,q12,q3
+       vadd.i64 q0,q0,q14
+       vst1.8 d12,[r2,: 64]!
+       vshl.i64 q7,q13,#4
+       vst1.8 d13,[r4,: 64]!
+       vsub.i64 q1,q1,q8
+       vshr.s64 q3,q3,#25
+       vadd.i64 q0,q0,q7
+       vadd.i64 q5,q5,q3
+       vshl.i64 q3,q3,#25
+       vadd.i64 q6,q5,q11
+       vadd.i64 q0,q0,q13
+       vshl.i64 q7,q13,#25
+       vadd.i64 q8,q0,q11
+       vsub.i64 q3,q12,q3
+       vshr.s64 q6,q6,#26
+       vsub.i64 q7,q10,q7
+       vtrn.32 d2,d6
+       vshr.s64 q8,q8,#26
+       vtrn.32 d3,d7
+       vadd.i64 q3,q9,q6
+       vst1.8 d2,[r2,: 64]
+       vshl.i64 q6,q6,#26
+       vst1.8 d3,[r4,: 64]
+       vadd.i64 q1,q4,q8
+       vtrn.32 d4,d14
+       vshl.i64 q4,q8,#26
+       vtrn.32 d5,d15
+       vsub.i64 q5,q5,q6
+       add r2,r2,#16
+       vsub.i64 q0,q0,q4
+       vst1.8 d4,[r2,: 64]
+       add r4,r4,#16
+       vst1.8 d5,[r4,: 64]
+       vtrn.32 d10,d6
+       vtrn.32 d11,d7
+       sub r2,r2,#8
+       sub r4,r4,#8
+       vtrn.32 d0,d2
+       vtrn.32 d1,d3
+       vst1.8 d10,[r2,: 64]
+       vst1.8 d11,[r4,: 64]
+       sub r2,r2,#24
+       sub r4,r4,#24
+       vst1.8 d0,[r2,: 64]
+       vst1.8 d1,[r4,: 64]
+       add r2,sp,#544
+       add r4,r3,#144
+       add r5,r3,#192
+       vld1.8 {d0-d1},[r2,: 128]
+       vld1.8 {d2-d3},[r4,: 128]!
+       vld1.8 {d4-d5},[r5,: 128]!
+       vzip.i32 q1,q2
+       vld1.8 {d6-d7},[r4,: 128]!
+       vld1.8 {d8-d9},[r5,: 128]!
+       vshl.i32 q5,q1,#1
+       vzip.i32 q3,q4
+       vshl.i32 q6,q2,#1
+       vld1.8 {d14},[r4,: 64]
+       vshl.i32 q8,q3,#1
+       vld1.8 {d15},[r5,: 64]
+       vshl.i32 q9,q4,#1
+       vmul.i32 d21,d7,d1
+       vtrn.32 d14,d15
+       vmul.i32 q11,q4,q0
+       vmul.i32 q0,q7,q0
+       vmull.s32 q12,d2,d2
+       vmlal.s32 q12,d11,d1
+       vmlal.s32 q12,d12,d0
+       vmlal.s32 q12,d13,d23
+       vmlal.s32 q12,d16,d22
+       vmlal.s32 q12,d7,d21
+       vmull.s32 q10,d2,d11
+       vmlal.s32 q10,d4,d1
+       vmlal.s32 q10,d13,d0
+       vmlal.s32 q10,d6,d23
+       vmlal.s32 q10,d17,d22
+       vmull.s32 q13,d10,d4
+       vmlal.s32 q13,d11,d3
+       vmlal.s32 q13,d13,d1
+       vmlal.s32 q13,d16,d0
+       vmlal.s32 q13,d17,d23
+       vmlal.s32 q13,d8,d22
+       vmull.s32 q1,d10,d5
+       vmlal.s32 q1,d11,d4
+       vmlal.s32 q1,d6,d1
+       vmlal.s32 q1,d17,d0
+       vmlal.s32 q1,d8,d23
+       vmull.s32 q14,d10,d6
+       vmlal.s32 q14,d11,d13
+       vmlal.s32 q14,d4,d4
+       vmlal.s32 q14,d17,d1
+       vmlal.s32 q14,d18,d0
+       vmlal.s32 q14,d9,d23
+       vmull.s32 q11,d10,d7
+       vmlal.s32 q11,d11,d6
+       vmlal.s32 q11,d12,d5
+       vmlal.s32 q11,d8,d1
+       vmlal.s32 q11,d19,d0
+       vmull.s32 q15,d10,d8
+       vmlal.s32 q15,d11,d17
+       vmlal.s32 q15,d12,d6
+       vmlal.s32 q15,d13,d5
+       vmlal.s32 q15,d19,d1
+       vmlal.s32 q15,d14,d0
+       vmull.s32 q2,d10,d9
+       vmlal.s32 q2,d11,d8
+       vmlal.s32 q2,d12,d7
+       vmlal.s32 q2,d13,d6
+       vmlal.s32 q2,d14,d1
+       vmull.s32 q0,d15,d1
+       vmlal.s32 q0,d10,d14
+       vmlal.s32 q0,d11,d19
+       vmlal.s32 q0,d12,d8
+       vmlal.s32 q0,d13,d17
+       vmlal.s32 q0,d6,d6
+       add r2,sp,#512
+       vld1.8 {d18-d19},[r2,: 128]
+       vmull.s32 q3,d16,d7
+       vmlal.s32 q3,d10,d15
+       vmlal.s32 q3,d11,d14
+       vmlal.s32 q3,d12,d9
+       vmlal.s32 q3,d13,d8
+       add r2,sp,#528
+       vld1.8 {d8-d9},[r2,: 128]
+       vadd.i64 q5,q12,q9
+       vadd.i64 q6,q15,q9
+       vshr.s64 q5,q5,#26
+       vshr.s64 q6,q6,#26
+       vadd.i64 q7,q10,q5
+       vshl.i64 q5,q5,#26
+       vadd.i64 q8,q7,q4
+       vadd.i64 q2,q2,q6
+       vshl.i64 q6,q6,#26
+       vadd.i64 q10,q2,q4
+       vsub.i64 q5,q12,q5
+       vshr.s64 q8,q8,#25
+       vsub.i64 q6,q15,q6
+       vshr.s64 q10,q10,#25
+       vadd.i64 q12,q13,q8
+       vshl.i64 q8,q8,#25
+       vadd.i64 q13,q12,q9
+       vadd.i64 q0,q0,q10
+       vsub.i64 q7,q7,q8
+       vshr.s64 q8,q13,#26
+       vshl.i64 q10,q10,#25
+       vadd.i64 q13,q0,q9
+       vadd.i64 q1,q1,q8
+       vshl.i64 q8,q8,#26
+       vadd.i64 q15,q1,q4
+       vsub.i64 q2,q2,q10
+       vshr.s64 q10,q13,#26
+       vsub.i64 q8,q12,q8
+       vshr.s64 q12,q15,#25
+       vadd.i64 q3,q3,q10
+       vshl.i64 q10,q10,#26
+       vadd.i64 q13,q3,q4
+       vadd.i64 q14,q14,q12
+       add r2,r3,#144
+       vshl.i64 q12,q12,#25
+       add r4,r3,#192
+       vadd.i64 q15,q14,q9
+       add r2,r2,#8
+       vsub.i64 q0,q0,q10
+       add r4,r4,#8
+       vshr.s64 q10,q13,#25
+       vsub.i64 q1,q1,q12
+       vshr.s64 q12,q15,#26
+       vadd.i64 q13,q10,q10
+       vadd.i64 q11,q11,q12
+       vtrn.32 d16,d2
+       vshl.i64 q12,q12,#26
+       vtrn.32 d17,d3
+       vadd.i64 q1,q11,q4
+       vadd.i64 q4,q5,q13
+       vst1.8 d16,[r2,: 64]!
+       vshl.i64 q5,q10,#4
+       vst1.8 d17,[r4,: 64]!
+       vsub.i64 q8,q14,q12
+       vshr.s64 q1,q1,#25
+       vadd.i64 q4,q4,q5
+       vadd.i64 q5,q6,q1
+       vshl.i64 q1,q1,#25
+       vadd.i64 q6,q5,q9
+       vadd.i64 q4,q4,q10
+       vshl.i64 q10,q10,#25
+       vadd.i64 q9,q4,q9
+       vsub.i64 q1,q11,q1
+       vshr.s64 q6,q6,#26
+       vsub.i64 q3,q3,q10
+       vtrn.32 d16,d2
+       vshr.s64 q9,q9,#26
+       vtrn.32 d17,d3
+       vadd.i64 q1,q2,q6
+       vst1.8 d16,[r2,: 64]
+       vshl.i64 q2,q6,#26
+       vst1.8 d17,[r4,: 64]
+       vadd.i64 q6,q7,q9
+       vtrn.32 d0,d6
+       vshl.i64 q7,q9,#26
+       vtrn.32 d1,d7
+       vsub.i64 q2,q5,q2
+       add r2,r2,#16
+       vsub.i64 q3,q4,q7
+       vst1.8 d0,[r2,: 64]
+       add r4,r4,#16
+       vst1.8 d1,[r4,: 64]
+       vtrn.32 d4,d2
+       vtrn.32 d5,d3
+       sub r2,r2,#8
+       sub r4,r4,#8
+       vtrn.32 d6,d12
+       vtrn.32 d7,d13
+       vst1.8 d4,[r2,: 64]
+       vst1.8 d5,[r4,: 64]
+       sub r2,r2,#24
+       sub r4,r4,#24
+       vst1.8 d6,[r2,: 64]
+       vst1.8 d7,[r4,: 64]
+       add r2,r3,#336
+       add r4,r3,#288
+       vld1.8 {d0-d1},[r2,: 128]!
+       vld1.8 {d2-d3},[r4,: 128]!
+       vadd.i32 q0,q0,q1
+       vld1.8 {d2-d3},[r2,: 128]!
+       vld1.8 {d4-d5},[r4,: 128]!
+       vadd.i32 q1,q1,q2
+       add r5,r3,#288
+       vld1.8 {d4},[r2,: 64]
+       vld1.8 {d6},[r4,: 64]
+       vadd.i32 q2,q2,q3
+       vst1.8 {d0-d1},[r5,: 128]!
+       vst1.8 {d2-d3},[r5,: 128]!
+       vst1.8 d4,[r5,: 64]
+       add r2,r3,#48
+       add r4,r3,#144
+       vld1.8 {d0-d1},[r4,: 128]!
+       vld1.8 {d2-d3},[r4,: 128]!
+       vld1.8 {d4},[r4,: 64]
+       add r4,r3,#288
+       vld1.8 {d6-d7},[r4,: 128]!
+       vtrn.32 q0,q3
+       vld1.8 {d8-d9},[r4,: 128]!
+       vshl.i32 q5,q0,#4
+       vtrn.32 q1,q4
+       vshl.i32 q6,q3,#4
+       vadd.i32 q5,q5,q0
+       vadd.i32 q6,q6,q3
+       vshl.i32 q7,q1,#4
+       vld1.8 {d5},[r4,: 64]
+       vshl.i32 q8,q4,#4
+       vtrn.32 d4,d5
+       vadd.i32 q7,q7,q1
+       vadd.i32 q8,q8,q4
+       vld1.8 {d18-d19},[r2,: 128]!
+       vshl.i32 q10,q2,#4
+       vld1.8 {d22-d23},[r2,: 128]!
+       vadd.i32 q10,q10,q2
+       vld1.8 {d24},[r2,: 64]
+       vadd.i32 q5,q5,q0
+       add r2,r3,#240
+       vld1.8 {d26-d27},[r2,: 128]!
+       vadd.i32 q6,q6,q3
+       vld1.8 {d28-d29},[r2,: 128]!
+       vadd.i32 q8,q8,q4
+       vld1.8 {d25},[r2,: 64]
+       vadd.i32 q10,q10,q2
+       vtrn.32 q9,q13
+       vadd.i32 q7,q7,q1
+       vadd.i32 q5,q5,q0
+       vtrn.32 q11,q14
+       vadd.i32 q6,q6,q3
+       add r2,sp,#560
+       vadd.i32 q10,q10,q2
+       vtrn.32 d24,d25
+       vst1.8 {d12-d13},[r2,: 128]
+       vshl.i32 q6,q13,#1
+       add r2,sp,#576
+       vst1.8 {d20-d21},[r2,: 128]
+       vshl.i32 q10,q14,#1
+       add r2,sp,#592
+       vst1.8 {d12-d13},[r2,: 128]
+       vshl.i32 q15,q12,#1
+       vadd.i32 q8,q8,q4
+       vext.32 d10,d31,d30,#0
+       vadd.i32 q7,q7,q1
+       add r2,sp,#608
+       vst1.8 {d16-d17},[r2,: 128]
+       vmull.s32 q8,d18,d5
+       vmlal.s32 q8,d26,d4
+       vmlal.s32 q8,d19,d9
+       vmlal.s32 q8,d27,d3
+       vmlal.s32 q8,d22,d8
+       vmlal.s32 q8,d28,d2
+       vmlal.s32 q8,d23,d7
+       vmlal.s32 q8,d29,d1
+       vmlal.s32 q8,d24,d6
+       vmlal.s32 q8,d25,d0
+       add r2,sp,#624
+       vst1.8 {d14-d15},[r2,: 128]
+       vmull.s32 q2,d18,d4
+       vmlal.s32 q2,d12,d9
+       vmlal.s32 q2,d13,d8
+       vmlal.s32 q2,d19,d3
+       vmlal.s32 q2,d22,d2
+       vmlal.s32 q2,d23,d1
+       vmlal.s32 q2,d24,d0
+       add r2,sp,#640
+       vst1.8 {d20-d21},[r2,: 128]
+       vmull.s32 q7,d18,d9
+       vmlal.s32 q7,d26,d3
+       vmlal.s32 q7,d19,d8
+       vmlal.s32 q7,d27,d2
+       vmlal.s32 q7,d22,d7
+       vmlal.s32 q7,d28,d1
+       vmlal.s32 q7,d23,d6
+       vmlal.s32 q7,d29,d0
+       add r2,sp,#656
+       vst1.8 {d10-d11},[r2,: 128]
+       vmull.s32 q5,d18,d3
+       vmlal.s32 q5,d19,d2
+       vmlal.s32 q5,d22,d1
+       vmlal.s32 q5,d23,d0
+       vmlal.s32 q5,d12,d8
+       add r2,sp,#672
+       vst1.8 {d16-d17},[r2,: 128]
+       vmull.s32 q4,d18,d8
+       vmlal.s32 q4,d26,d2
+       vmlal.s32 q4,d19,d7
+       vmlal.s32 q4,d27,d1
+       vmlal.s32 q4,d22,d6
+       vmlal.s32 q4,d28,d0
+       vmull.s32 q8,d18,d7
+       vmlal.s32 q8,d26,d1
+       vmlal.s32 q8,d19,d6
+       vmlal.s32 q8,d27,d0
+       add r2,sp,#576
+       vld1.8 {d20-d21},[r2,: 128]
+       vmlal.s32 q7,d24,d21
+       vmlal.s32 q7,d25,d20
+       vmlal.s32 q4,d23,d21
+       vmlal.s32 q4,d29,d20
+       vmlal.s32 q8,d22,d21
+       vmlal.s32 q8,d28,d20
+       vmlal.s32 q5,d24,d20
+       add r2,sp,#576
+       vst1.8 {d14-d15},[r2,: 128]
+       vmull.s32 q7,d18,d6
+       vmlal.s32 q7,d26,d0
+       add r2,sp,#656
+       vld1.8 {d30-d31},[r2,: 128]
+       vmlal.s32 q2,d30,d21
+       vmlal.s32 q7,d19,d21
+       vmlal.s32 q7,d27,d20
+       add r2,sp,#624
+       vld1.8 {d26-d27},[r2,: 128]
+       vmlal.s32 q4,d25,d27
+       vmlal.s32 q8,d29,d27
+       vmlal.s32 q8,d25,d26
+       vmlal.s32 q7,d28,d27
+       vmlal.s32 q7,d29,d26
+       add r2,sp,#608
+       vld1.8 {d28-d29},[r2,: 128]
+       vmlal.s32 q4,d24,d29
+       vmlal.s32 q8,d23,d29
+       vmlal.s32 q8,d24,d28
+       vmlal.s32 q7,d22,d29
+       vmlal.s32 q7,d23,d28
+       add r2,sp,#608
+       vst1.8 {d8-d9},[r2,: 128]
+       add r2,sp,#560
+       vld1.8 {d8-d9},[r2,: 128]
+       vmlal.s32 q7,d24,d9
+       vmlal.s32 q7,d25,d31
+       vmull.s32 q1,d18,d2
+       vmlal.s32 q1,d19,d1
+       vmlal.s32 q1,d22,d0
+       vmlal.s32 q1,d24,d27
+       vmlal.s32 q1,d23,d20
+       vmlal.s32 q1,d12,d7
+       vmlal.s32 q1,d13,d6
+       vmull.s32 q6,d18,d1
+       vmlal.s32 q6,d19,d0
+       vmlal.s32 q6,d23,d27
+       vmlal.s32 q6,d22,d20
+       vmlal.s32 q6,d24,d26
+       vmull.s32 q0,d18,d0
+       vmlal.s32 q0,d22,d27
+       vmlal.s32 q0,d23,d26
+       vmlal.s32 q0,d24,d31
+       vmlal.s32 q0,d19,d20
+       add r2,sp,#640
+       vld1.8 {d18-d19},[r2,: 128]
+       vmlal.s32 q2,d18,d7
+       vmlal.s32 q2,d19,d6
+       vmlal.s32 q5,d18,d6
+       vmlal.s32 q5,d19,d21
+       vmlal.s32 q1,d18,d21
+       vmlal.s32 q1,d19,d29
+       vmlal.s32 q0,d18,d28
+       vmlal.s32 q0,d19,d9
+       vmlal.s32 q6,d18,d29
+       vmlal.s32 q6,d19,d28
+       add r2,sp,#592
+       vld1.8 {d18-d19},[r2,: 128]
+       add r2,sp,#512
+       vld1.8 {d22-d23},[r2,: 128]
+       vmlal.s32 q5,d19,d7
+       vmlal.s32 q0,d18,d21
+       vmlal.s32 q0,d19,d29
+       vmlal.s32 q6,d18,d6
+       add r2,sp,#528
+       vld1.8 {d6-d7},[r2,: 128]
+       vmlal.s32 q6,d19,d21
+       add r2,sp,#576
+       vld1.8 {d18-d19},[r2,: 128]
+       vmlal.s32 q0,d30,d8
+       add r2,sp,#672
+       vld1.8 {d20-d21},[r2,: 128]
+       vmlal.s32 q5,d30,d29
+       add r2,sp,#608
+       vld1.8 {d24-d25},[r2,: 128]
+       vmlal.s32 q1,d30,d28
+       vadd.i64 q13,q0,q11
+       vadd.i64 q14,q5,q11
+       vmlal.s32 q6,d30,d9
+       vshr.s64 q4,q13,#26
+       vshr.s64 q13,q14,#26
+       vadd.i64 q7,q7,q4
+       vshl.i64 q4,q4,#26
+       vadd.i64 q14,q7,q3
+       vadd.i64 q9,q9,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q15,q9,q3
+       vsub.i64 q0,q0,q4
+       vshr.s64 q4,q14,#25
+       vsub.i64 q5,q5,q13
+       vshr.s64 q13,q15,#25
+       vadd.i64 q6,q6,q4
+       vshl.i64 q4,q4,#25
+       vadd.i64 q14,q6,q11
+       vadd.i64 q2,q2,q13
+       vsub.i64 q4,q7,q4
+       vshr.s64 q7,q14,#26
+       vshl.i64 q13,q13,#25
+       vadd.i64 q14,q2,q11
+       vadd.i64 q8,q8,q7
+       vshl.i64 q7,q7,#26
+       vadd.i64 q15,q8,q3
+       vsub.i64 q9,q9,q13
+       vshr.s64 q13,q14,#26
+       vsub.i64 q6,q6,q7
+       vshr.s64 q7,q15,#25
+       vadd.i64 q10,q10,q13
+       vshl.i64 q13,q13,#26
+       vadd.i64 q14,q10,q3
+       vadd.i64 q1,q1,q7
+       add r2,r3,#240
+       vshl.i64 q7,q7,#25
+       add r4,r3,#144
+       vadd.i64 q15,q1,q11
+       add r2,r2,#8
+       vsub.i64 q2,q2,q13
+       add r4,r4,#8
+       vshr.s64 q13,q14,#25
+       vsub.i64 q7,q8,q7
+       vshr.s64 q8,q15,#26
+       vadd.i64 q14,q13,q13
+       vadd.i64 q12,q12,q8
+       vtrn.32 d12,d14
+       vshl.i64 q8,q8,#26
+       vtrn.32 d13,d15
+       vadd.i64 q3,q12,q3
+       vadd.i64 q0,q0,q14
+       vst1.8 d12,[r2,: 64]!
+       vshl.i64 q7,q13,#4
+       vst1.8 d13,[r4,: 64]!
+       vsub.i64 q1,q1,q8
+       vshr.s64 q3,q3,#25
+       vadd.i64 q0,q0,q7
+       vadd.i64 q5,q5,q3
+       vshl.i64 q3,q3,#25
+       vadd.i64 q6,q5,q11
+       vadd.i64 q0,q0,q13
+       vshl.i64 q7,q13,#25
+       vadd.i64 q8,q0,q11
+       vsub.i64 q3,q12,q3
+       vshr.s64 q6,q6,#26
+       vsub.i64 q7,q10,q7
+       vtrn.32 d2,d6
+       vshr.s64 q8,q8,#26
+       vtrn.32 d3,d7
+       vadd.i64 q3,q9,q6
+       vst1.8 d2,[r2,: 64]
+       vshl.i64 q6,q6,#26
+       vst1.8 d3,[r4,: 64]
+       vadd.i64 q1,q4,q8
+       vtrn.32 d4,d14
+       vshl.i64 q4,q8,#26
+       vtrn.32 d5,d15
+       vsub.i64 q5,q5,q6
+       add r2,r2,#16
+       vsub.i64 q0,q0,q4
+       vst1.8 d4,[r2,: 64]
+       add r4,r4,#16
+       vst1.8 d5,[r4,: 64]
+       vtrn.32 d10,d6
+       vtrn.32 d11,d7
+       sub r2,r2,#8
+       sub r4,r4,#8
+       vtrn.32 d0,d2
+       vtrn.32 d1,d3
+       vst1.8 d10,[r2,: 64]
+       vst1.8 d11,[r4,: 64]
+       sub r2,r2,#24
+       sub r4,r4,#24
+       vst1.8 d0,[r2,: 64]
+       vst1.8 d1,[r4,: 64]
+       ldr r2,[sp,#488]
+       ldr r4,[sp,#492]
+       subs r5,r2,#1
+       bge .Lmainloop
+       add r1,r3,#144
+       add r2,r3,#336
+       vld1.8 {d0-d1},[r1,: 128]!
+       vld1.8 {d2-d3},[r1,: 128]!
+       vld1.8 {d4},[r1,: 64]
+       vst1.8 {d0-d1},[r2,: 128]!
+       vst1.8 {d2-d3},[r2,: 128]!
+       vst1.8 d4,[r2,: 64]
+       ldr r1,=0
+       .Linvertloop:
+       add r2,r3,#144
+       ldr r4,=0
+       ldr r5,=2
+       cmp r1,#1
+       ldreq r5,=1
+       addeq r2,r3,#336
+       addeq r4,r3,#48
+       cmp r1,#2
+       ldreq r5,=1
+       addeq r2,r3,#48
+       cmp r1,#3
+       ldreq r5,=5
+       addeq r4,r3,#336
+       cmp r1,#4
+       ldreq r5,=10
+       cmp r1,#5
+       ldreq r5,=20
+       cmp r1,#6
+       ldreq r5,=10
+       addeq r2,r3,#336
+       addeq r4,r3,#336
+       cmp r1,#7
+       ldreq r5,=50
+       cmp r1,#8
+       ldreq r5,=100
+       cmp r1,#9
+       ldreq r5,=50
+       addeq r2,r3,#336
+       cmp r1,#10
+       ldreq r5,=5
+       addeq r2,r3,#48
+       cmp r1,#11
+       ldreq r5,=0
+       addeq r2,r3,#96
+       add r6,r3,#144
+       add r7,r3,#288
+       vld1.8 {d0-d1},[r6,: 128]!
+       vld1.8 {d2-d3},[r6,: 128]!
+       vld1.8 {d4},[r6,: 64]
+       vst1.8 {d0-d1},[r7,: 128]!
+       vst1.8 {d2-d3},[r7,: 128]!
+       vst1.8 d4,[r7,: 64]
+       cmp r5,#0
+       beq .Lskipsquaringloop
+       .Lsquaringloop:
+       add r6,r3,#288
+       add r7,r3,#288
+       add r8,r3,#288
+       vmov.i32 q0,#19
+       vmov.i32 q1,#0
+       vmov.i32 q2,#1
+       vzip.i32 q1,q2
+       vld1.8 {d4-d5},[r7,: 128]!
+       vld1.8 {d6-d7},[r7,: 128]!
+       vld1.8 {d9},[r7,: 64]
+       vld1.8 {d10-d11},[r6,: 128]!
+       add r7,sp,#416
+       vld1.8 {d12-d13},[r6,: 128]!
+       vmul.i32 q7,q2,q0
+       vld1.8 {d8},[r6,: 64]
+       vext.32 d17,d11,d10,#1
+       vmul.i32 q9,q3,q0
+       vext.32 d16,d10,d8,#1
+       vshl.u32 q10,q5,q1
+       vext.32 d22,d14,d4,#1
+       vext.32 d24,d18,d6,#1
+       vshl.u32 q13,q6,q1
+       vshl.u32 d28,d8,d2
+       vrev64.i32 d22,d22
+       vmul.i32 d1,d9,d1
+       vrev64.i32 d24,d24
+       vext.32 d29,d8,d13,#1
+       vext.32 d0,d1,d9,#1
+       vrev64.i32 d0,d0
+       vext.32 d2,d9,d1,#1
+       vext.32 d23,d15,d5,#1
+       vmull.s32 q4,d20,d4
+       vrev64.i32 d23,d23
+       vmlal.s32 q4,d21,d1
+       vrev64.i32 d2,d2
+       vmlal.s32 q4,d26,d19
+       vext.32 d3,d5,d15,#1
+       vmlal.s32 q4,d27,d18
+       vrev64.i32 d3,d3
+       vmlal.s32 q4,d28,d15
+       vext.32 d14,d12,d11,#1
+       vmull.s32 q5,d16,d23
+       vext.32 d15,d13,d12,#1
+       vmlal.s32 q5,d17,d4
+       vst1.8 d8,[r7,: 64]!
+       vmlal.s32 q5,d14,d1
+       vext.32 d12,d9,d8,#0
+       vmlal.s32 q5,d15,d19
+       vmov.i64 d13,#0
+       vmlal.s32 q5,d29,d18
+       vext.32 d25,d19,d7,#1
+       vmlal.s32 q6,d20,d5
+       vrev64.i32 d25,d25
+       vmlal.s32 q6,d21,d4
+       vst1.8 d11,[r7,: 64]!
+       vmlal.s32 q6,d26,d1
+       vext.32 d9,d10,d10,#0
+       vmlal.s32 q6,d27,d19
+       vmov.i64 d8,#0
+       vmlal.s32 q6,d28,d18
+       vmlal.s32 q4,d16,d24
+       vmlal.s32 q4,d17,d5
+       vmlal.s32 q4,d14,d4
+       vst1.8 d12,[r7,: 64]!
+       vmlal.s32 q4,d15,d1
+       vext.32 d10,d13,d12,#0
+       vmlal.s32 q4,d29,d19
+       vmov.i64 d11,#0
+       vmlal.s32 q5,d20,d6
+       vmlal.s32 q5,d21,d5
+       vmlal.s32 q5,d26,d4
+       vext.32 d13,d8,d8,#0
+       vmlal.s32 q5,d27,d1
+       vmov.i64 d12,#0
+       vmlal.s32 q5,d28,d19
+       vst1.8 d9,[r7,: 64]!
+       vmlal.s32 q6,d16,d25
+       vmlal.s32 q6,d17,d6
+       vst1.8 d10,[r7,: 64]
+       vmlal.s32 q6,d14,d5
+       vext.32 d8,d11,d10,#0
+       vmlal.s32 q6,d15,d4
+       vmov.i64 d9,#0
+       vmlal.s32 q6,d29,d1
+       vmlal.s32 q4,d20,d7
+       vmlal.s32 q4,d21,d6
+       vmlal.s32 q4,d26,d5
+       vext.32 d11,d12,d12,#0
+       vmlal.s32 q4,d27,d4
+       vmov.i64 d10,#0
+       vmlal.s32 q4,d28,d1
+       vmlal.s32 q5,d16,d0
+       sub r6,r7,#32
+       vmlal.s32 q5,d17,d7
+       vmlal.s32 q5,d14,d6
+       vext.32 d30,d9,d8,#0
+       vmlal.s32 q5,d15,d5
+       vld1.8 {d31},[r6,: 64]!
+       vmlal.s32 q5,d29,d4
+       vmlal.s32 q15,d20,d0
+       vext.32 d0,d6,d18,#1
+       vmlal.s32 q15,d21,d25
+       vrev64.i32 d0,d0
+       vmlal.s32 q15,d26,d24
+       vext.32 d1,d7,d19,#1
+       vext.32 d7,d10,d10,#0
+       vmlal.s32 q15,d27,d23
+       vrev64.i32 d1,d1
+       vld1.8 {d6},[r6,: 64]
+       vmlal.s32 q15,d28,d22
+       vmlal.s32 q3,d16,d4
+       add r6,r6,#24
+       vmlal.s32 q3,d17,d2
+       vext.32 d4,d31,d30,#0
+       vmov d17,d11
+       vmlal.s32 q3,d14,d1
+       vext.32 d11,d13,d13,#0
+       vext.32 d13,d30,d30,#0
+       vmlal.s32 q3,d15,d0
+       vext.32 d1,d8,d8,#0
+       vmlal.s32 q3,d29,d3
+       vld1.8 {d5},[r6,: 64]
+       sub r6,r6,#16
+       vext.32 d10,d6,d6,#0
+       vmov.i32 q1,#0xffffffff
+       vshl.i64 q4,q1,#25
+       add r7,sp,#512
+       vld1.8 {d14-d15},[r7,: 128]
+       vadd.i64 q9,q2,q7
+       vshl.i64 q1,q1,#26
+       vshr.s64 q10,q9,#26
+       vld1.8 {d0},[r6,: 64]!
+       vadd.i64 q5,q5,q10
+       vand q9,q9,q1
+       vld1.8 {d16},[r6,: 64]!
+       add r6,sp,#528
+       vld1.8 {d20-d21},[r6,: 128]
+       vadd.i64 q11,q5,q10
+       vsub.i64 q2,q2,q9
+       vshr.s64 q9,q11,#25
+       vext.32 d12,d5,d4,#0
+       vand q11,q11,q4
+       vadd.i64 q0,q0,q9
+       vmov d19,d7
+       vadd.i64 q3,q0,q7
+       vsub.i64 q5,q5,q11
+       vshr.s64 q11,q3,#26
+       vext.32 d18,d11,d10,#0
+       vand q3,q3,q1
+       vadd.i64 q8,q8,q11
+       vadd.i64 q11,q8,q10
+       vsub.i64 q0,q0,q3
+       vshr.s64 q3,q11,#25
+       vand q11,q11,q4
+       vadd.i64 q3,q6,q3
+       vadd.i64 q6,q3,q7
+       vsub.i64 q8,q8,q11
+       vshr.s64 q11,q6,#26
+       vand q6,q6,q1
+       vadd.i64 q9,q9,q11
+       vadd.i64 d25,d19,d21
+       vsub.i64 q3,q3,q6
+       vshr.s64 d23,d25,#25
+       vand q4,q12,q4
+       vadd.i64 d21,d23,d23
+       vshl.i64 d25,d23,#4
+       vadd.i64 d21,d21,d23
+       vadd.i64 d25,d25,d21
+       vadd.i64 d4,d4,d25
+       vzip.i32 q0,q8
+       vadd.i64 d12,d4,d14
+       add r6,r8,#8
+       vst1.8 d0,[r6,: 64]
+       vsub.i64 d19,d19,d9
+       add r6,r6,#16
+       vst1.8 d16,[r6,: 64]
+       vshr.s64 d22,d12,#26
+       vand q0,q6,q1
+       vadd.i64 d10,d10,d22
+       vzip.i32 q3,q9
+       vsub.i64 d4,d4,d0
+       sub r6,r6,#8
+       vst1.8 d6,[r6,: 64]
+       add r6,r6,#16
+       vst1.8 d18,[r6,: 64]
+       vzip.i32 q2,q5
+       sub r6,r6,#32
+       vst1.8 d4,[r6,: 64]
+       subs r5,r5,#1
+       bhi .Lsquaringloop
+       .Lskipsquaringloop:
+       mov r2,r2
+       add r5,r3,#288
+       add r6,r3,#144
+       vmov.i32 q0,#19
+       vmov.i32 q1,#0
+       vmov.i32 q2,#1
+       vzip.i32 q1,q2
+       vld1.8 {d4-d5},[r5,: 128]!
+       vld1.8 {d6-d7},[r5,: 128]!
+       vld1.8 {d9},[r5,: 64]
+       vld1.8 {d10-d11},[r2,: 128]!
+       add r5,sp,#416
+       vld1.8 {d12-d13},[r2,: 128]!
+       vmul.i32 q7,q2,q0
+       vld1.8 {d8},[r2,: 64]
+       vext.32 d17,d11,d10,#1
+       vmul.i32 q9,q3,q0
+       vext.32 d16,d10,d8,#1
+       vshl.u32 q10,q5,q1
+       vext.32 d22,d14,d4,#1
+       vext.32 d24,d18,d6,#1
+       vshl.u32 q13,q6,q1
+       vshl.u32 d28,d8,d2
+       vrev64.i32 d22,d22
+       vmul.i32 d1,d9,d1
+       vrev64.i32 d24,d24
+       vext.32 d29,d8,d13,#1
+       vext.32 d0,d1,d9,#1
+       vrev64.i32 d0,d0
+       vext.32 d2,d9,d1,#1
+       vext.32 d23,d15,d5,#1
+       vmull.s32 q4,d20,d4
+       vrev64.i32 d23,d23
+       vmlal.s32 q4,d21,d1
+       vrev64.i32 d2,d2
+       vmlal.s32 q4,d26,d19
+       vext.32 d3,d5,d15,#1
+       vmlal.s32 q4,d27,d18
+       vrev64.i32 d3,d3
+       vmlal.s32 q4,d28,d15
+       vext.32 d14,d12,d11,#1
+       vmull.s32 q5,d16,d23
+       vext.32 d15,d13,d12,#1
+       vmlal.s32 q5,d17,d4
+       vst1.8 d8,[r5,: 64]!
+       vmlal.s32 q5,d14,d1
+       vext.32 d12,d9,d8,#0
+       vmlal.s32 q5,d15,d19
+       vmov.i64 d13,#0
+       vmlal.s32 q5,d29,d18
+       vext.32 d25,d19,d7,#1
+       vmlal.s32 q6,d20,d5
+       vrev64.i32 d25,d25
+       vmlal.s32 q6,d21,d4
+       vst1.8 d11,[r5,: 64]!
+       vmlal.s32 q6,d26,d1
+       vext.32 d9,d10,d10,#0
+       vmlal.s32 q6,d27,d19
+       vmov.i64 d8,#0
+       vmlal.s32 q6,d28,d18
+       vmlal.s32 q4,d16,d24
+       vmlal.s32 q4,d17,d5
+       vmlal.s32 q4,d14,d4
+       vst1.8 d12,[r5,: 64]!
+       vmlal.s32 q4,d15,d1
+       vext.32 d10,d13,d12,#0
+       vmlal.s32 q4,d29,d19
+       vmov.i64 d11,#0
+       vmlal.s32 q5,d20,d6
+       vmlal.s32 q5,d21,d5
+       vmlal.s32 q5,d26,d4
+       vext.32 d13,d8,d8,#0
+       vmlal.s32 q5,d27,d1
+       vmov.i64 d12,#0
+       vmlal.s32 q5,d28,d19
+       vst1.8 d9,[r5,: 64]!
+       vmlal.s32 q6,d16,d25
+       vmlal.s32 q6,d17,d6
+       vst1.8 d10,[r5,: 64]
+       vmlal.s32 q6,d14,d5
+       vext.32 d8,d11,d10,#0
+       vmlal.s32 q6,d15,d4
+       vmov.i64 d9,#0
+       vmlal.s32 q6,d29,d1
+       vmlal.s32 q4,d20,d7
+       vmlal.s32 q4,d21,d6
+       vmlal.s32 q4,d26,d5
+       vext.32 d11,d12,d12,#0
+       vmlal.s32 q4,d27,d4
+       vmov.i64 d10,#0
+       vmlal.s32 q4,d28,d1
+       vmlal.s32 q5,d16,d0
+       sub r2,r5,#32
+       vmlal.s32 q5,d17,d7
+       vmlal.s32 q5,d14,d6
+       vext.32 d30,d9,d8,#0
+       vmlal.s32 q5,d15,d5
+       vld1.8 {d31},[r2,: 64]!
+       vmlal.s32 q5,d29,d4
+       vmlal.s32 q15,d20,d0
+       vext.32 d0,d6,d18,#1
+       vmlal.s32 q15,d21,d25
+       vrev64.i32 d0,d0
+       vmlal.s32 q15,d26,d24
+       vext.32 d1,d7,d19,#1
+       vext.32 d7,d10,d10,#0
+       vmlal.s32 q15,d27,d23
+       vrev64.i32 d1,d1
+       vld1.8 {d6},[r2,: 64]
+       vmlal.s32 q15,d28,d22
+       vmlal.s32 q3,d16,d4
+       add r2,r2,#24
+       vmlal.s32 q3,d17,d2
+       vext.32 d4,d31,d30,#0
+       vmov d17,d11
+       vmlal.s32 q3,d14,d1
+       vext.32 d11,d13,d13,#0
+       vext.32 d13,d30,d30,#0
+       vmlal.s32 q3,d15,d0
+       vext.32 d1,d8,d8,#0
+       vmlal.s32 q3,d29,d3
+       vld1.8 {d5},[r2,: 64]
+       sub r2,r2,#16
+       vext.32 d10,d6,d6,#0
+       vmov.i32 q1,#0xffffffff
+       vshl.i64 q4,q1,#25
+       add r5,sp,#512
+       vld1.8 {d14-d15},[r5,: 128]
+       vadd.i64 q9,q2,q7
+       vshl.i64 q1,q1,#26
+       vshr.s64 q10,q9,#26
+       vld1.8 {d0},[r2,: 64]!
+       vadd.i64 q5,q5,q10
+       vand q9,q9,q1
+       vld1.8 {d16},[r2,: 64]!
+       add r2,sp,#528
+       vld1.8 {d20-d21},[r2,: 128]
+       vadd.i64 q11,q5,q10
+       vsub.i64 q2,q2,q9
+       vshr.s64 q9,q11,#25
+       vext.32 d12,d5,d4,#0
+       vand q11,q11,q4
+       vadd.i64 q0,q0,q9
+       vmov d19,d7
+       vadd.i64 q3,q0,q7
+       vsub.i64 q5,q5,q11
+       vshr.s64 q11,q3,#26
+       vext.32 d18,d11,d10,#0
+       vand q3,q3,q1
+       vadd.i64 q8,q8,q11
+       vadd.i64 q11,q8,q10
+       vsub.i64 q0,q0,q3
+       vshr.s64 q3,q11,#25
+       vand q11,q11,q4
+       vadd.i64 q3,q6,q3
+       vadd.i64 q6,q3,q7
+       vsub.i64 q8,q8,q11
+       vshr.s64 q11,q6,#26
+       vand q6,q6,q1
+       vadd.i64 q9,q9,q11
+       vadd.i64 d25,d19,d21
+       vsub.i64 q3,q3,q6
+       vshr.s64 d23,d25,#25
+       vand q4,q12,q4
+       vadd.i64 d21,d23,d23
+       vshl.i64 d25,d23,#4
+       vadd.i64 d21,d21,d23
+       vadd.i64 d25,d25,d21
+       vadd.i64 d4,d4,d25
+       vzip.i32 q0,q8
+       vadd.i64 d12,d4,d14
+       add r2,r6,#8
+       vst1.8 d0,[r2,: 64]
+       vsub.i64 d19,d19,d9
+       add r2,r2,#16
+       vst1.8 d16,[r2,: 64]
+       vshr.s64 d22,d12,#26
+       vand q0,q6,q1
+       vadd.i64 d10,d10,d22
+       vzip.i32 q3,q9
+       vsub.i64 d4,d4,d0
+       sub r2,r2,#8
+       vst1.8 d6,[r2,: 64]
+       add r2,r2,#16
+       vst1.8 d18,[r2,: 64]
+       vzip.i32 q2,q5
+       sub r2,r2,#32
+       vst1.8 d4,[r2,: 64]
+       cmp r4,#0
+       beq .Lskippostcopy
+       add r2,r3,#144
+       mov r4,r4
+       vld1.8 {d0-d1},[r2,: 128]!
+       vld1.8 {d2-d3},[r2,: 128]!
+       vld1.8 {d4},[r2,: 64]
+       vst1.8 {d0-d1},[r4,: 128]!
+       vst1.8 {d2-d3},[r4,: 128]!
+       vst1.8 d4,[r4,: 64]
+       .Lskippostcopy:
+       cmp r1,#1
+       bne .Lskipfinalcopy
+       add r2,r3,#288
+       add r4,r3,#144
+       vld1.8 {d0-d1},[r2,: 128]!
+       vld1.8 {d2-d3},[r2,: 128]!
+       vld1.8 {d4},[r2,: 64]
+       vst1.8 {d0-d1},[r4,: 128]!
+       vst1.8 {d2-d3},[r4,: 128]!
+       vst1.8 d4,[r4,: 64]
+       .Lskipfinalcopy:
+       add r1,r1,#1
+       cmp r1,#12
+       blo .Linvertloop
+       add r1,r3,#144
+       ldr r2,[r1],#4
+       ldr r3,[r1],#4
+       ldr r4,[r1],#4
+       ldr r5,[r1],#4
+       ldr r6,[r1],#4
+       ldr r7,[r1],#4
+       ldr r8,[r1],#4
+       ldr r9,[r1],#4
+       ldr r10,[r1],#4
+       ldr r1,[r1]
+       add r11,r1,r1,LSL #4
+       add r11,r11,r1,LSL #1
+       add r11,r11,#16777216
+       mov r11,r11,ASR #25
+       add r11,r11,r2
+       mov r11,r11,ASR #26
+       add r11,r11,r3
+       mov r11,r11,ASR #25
+       add r11,r11,r4
+       mov r11,r11,ASR #26
+       add r11,r11,r5
+       mov r11,r11,ASR #25
+       add r11,r11,r6
+       mov r11,r11,ASR #26
+       add r11,r11,r7
+       mov r11,r11,ASR #25
+       add r11,r11,r8
+       mov r11,r11,ASR #26
+       add r11,r11,r9
+       mov r11,r11,ASR #25
+       add r11,r11,r10
+       mov r11,r11,ASR #26
+       add r11,r11,r1
+       mov r11,r11,ASR #25
+       add r2,r2,r11
+       add r2,r2,r11,LSL #1
+       add r2,r2,r11,LSL #4
+       mov r11,r2,ASR #26
+       add r3,r3,r11
+       sub r2,r2,r11,LSL #26
+       mov r11,r3,ASR #25
+       add r4,r4,r11
+       sub r3,r3,r11,LSL #25
+       mov r11,r4,ASR #26
+       add r5,r5,r11
+       sub r4,r4,r11,LSL #26
+       mov r11,r5,ASR #25
+       add r6,r6,r11
+       sub r5,r5,r11,LSL #25
+       mov r11,r6,ASR #26
+       add r7,r7,r11
+       sub r6,r6,r11,LSL #26
+       mov r11,r7,ASR #25
+       add r8,r8,r11
+       sub r7,r7,r11,LSL #25
+       mov r11,r8,ASR #26
+       add r9,r9,r11
+       sub r8,r8,r11,LSL #26
+       mov r11,r9,ASR #25
+       add r10,r10,r11
+       sub r9,r9,r11,LSL #25
+       mov r11,r10,ASR #26
+       add r1,r1,r11
+       sub r10,r10,r11,LSL #26
+       mov r11,r1,ASR #25
+       sub r1,r1,r11,LSL #25
+       add r2,r2,r3,LSL #26
+       mov r3,r3,LSR #6
+       add r3,r3,r4,LSL #19
+       mov r4,r4,LSR #13
+       add r4,r4,r5,LSL #13
+       mov r5,r5,LSR #19
+       add r5,r5,r6,LSL #6
+       add r6,r7,r8,LSL #25
+       mov r7,r8,LSR #7
+       add r7,r7,r9,LSL #19
+       mov r8,r9,LSR #13
+       add r8,r8,r10,LSL #12

lib/zinc/curve25519/curve25519-arm.S line 2098

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       mov r9,r10,LSR #20
+       add r1,r9,r1,LSL #6
+       str r2,[r0],#4
+       str r3,[r0],#4
+       str r4,[r0],#4
+       str r5,[r0],#4
+       str r6,[r0],#4
+       str r7,[r0],#4
+       str r8,[r0],#4
+       str r1,[r0]

Why the post increment? Although it hardly matters here, this should be more efficient

str r2, [r0]
str r3, [r0, #4]
str r4, [r0, #8]
str r5, [r0, #12]
str r6, [r0, #16]
str r7, [r0, #20]
str r8, [r0, #24]
str r1, [r0, #28]

lib/zinc/curve25519/curve25519-arm.S line 2107

Quoted 9 lines
lib/zinc/curve25519/curve25519-arm.S new file +9 -0
+       ldrd r4,[sp,#0]
+       ldrd r6,[sp,#8]
+       ldrd r8,[sp,#16]
+       ldrd r10,[sp,#24]
+       ldr r12,[sp,#480]
+       ldr r14,[sp,#484]
+       ldr r0,=0
+       mov sp,r12
+       vpop {q4,q5,q6,q7}

Drop this

Quoted 6 lines
lib/zinc/curve25519/curve25519-arm.S new file +3 -1
+       bx lr
+ENDPROC(curve25519_neon)
+#endif
--

2.18.0

author maintainer
#39 lore raw

PATCH v2 16/17 zinc: Curve25519 x86_64 implementation

In reply to Jason A. Donenfeld

This implementation is the fastest available x86_64 implementation, and unlike Sandy2x, it doesn't requie use of the floating point registers at all. Instead it makes use of BMI2 and ADX, available on recent microarchitectures. The implementation was written by Armando Faz-Hernández with contributions (upstream) from Samuel Neves and me, in addition to further changes in the kernel implementation from us.

Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Samuel Neves <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Armando Faz-Hernández <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 lib/zinc/Makefile                            |    3 +
 lib/zinc/curve25519/curve25519-x86_64-glue.h |   45 +
 lib/zinc/curve25519/curve25519-x86_64.h      | 2053 ++++++++++++++++++
 3 files changed, 2101 insertions(+)
 create mode 100644 lib/zinc/curve25519/curve25519-x86_64-glue.h
 create mode 100644 lib/zinc/curve25519/curve25519-x86_64.h
lib/zinc/Makefile +3 -0
index 13e403780ce0..f35a7017d847 100644
@@ -56,6 +56,9 @@ ifeq ($(CONFIG_ARM)$(CONFIG_KERNEL_MODE_NEON),yy)
 zinc-y += curve25519/curve25519-arm.o
 CFLAGS_curve25519.o += -include $(srctree)/$(src)/curve25519/curve25519-arm-glue.h
 endif
+ifeq ($(CONFIG_X86_64),y)
+CFLAGS_curve25519.o += -include $(srctree)/$(src)/curve25519/curve25519-x86_64-glue.h
+endif
 endif
 
 ifeq ($(CONFIG_ZINC_BLAKE2S),y)
lib/zinc/curve25519/curve25519-x86_64-glue.h new file +45 -0

45 lines not on this page · raw message

lib/zinc/curve25519/curve25519-x86_64.h new file +2053 -0

2,053 lines not on this page · raw message

Signature · 1 line
2.18.0
author maintainer
#40 lore raw

PATCH v2 17/17 net: WireGuard secure network tunnel

In reply to Jason A. Donenfeld

WireGuard is a layer 3 secure networking tunnel made specifically for the kernel, that aims to be much simpler and easier to audit than IPsec. Extensive documentation and description of the protocol and considerations, along with formal proofs of the cryptography, are available at:

* https://www.wireguard.com/
* https://www.wireguard.com/papers/wireguard.pdf

This commit implements WireGuard as a simple network device driver, accessible in the usual RTNL way used by virtual network drivers. It makes use of the udp_tunnel APIs, GRO, GSO, NAPI, and the usual set of networking subsystem APIs. It has a somewhat novel multicore queueing system designed for maximum throughput and minimal latency of encryption operations, but it is implemented modestly using workqueues and NAPI. Configuration is done via generic Netlink, and following a review from the Netlink maintainer a year ago, several high profile userspace have already implemented the API.

This commit also comes with several different tests, both in-kernel tests and out-of-kernel tests based on network namespaces, taking profit of the fact that sockets used by WireGuard intentionally stay in the namespace the WireGuard interface was originally created, exactly like the semantics of userspace tun devices. See wireguard.com/netns/ for pictures and examples.

The source code is fairly short, but rather than combining everything into a single file, WireGuard is developed as cleanly separable files, making auditing and comprehension easier. Things are laid out as follows:

* noise.[ch], cookie.[ch], messages.h: These implement the bulk of the cryptographic aspects of the protocol, and are mostly data-only in nature, taking in buffers of bytes and spitting out buffers of bytes. They also handle reference counting for their various shared pieces of data, like keys and key lists.

* ratelimiter.[ch]: Used as an integral part of cookie.[ch] for ratelimiting certain types of cryptographic operations in accordance with particular WireGuard semantics.

* allowedips.[ch], hashtables.[ch]: The main lookup structures of WireGuard, the former being trie-like with particular semantics, an integral part of the design of the protocol, and the latter just being nice helper functions around the specific hashtables we use.

* device.[ch]: Implementation of functions for the netdevice and for rtnl, responsible for maintaining the life of a given interface and wiring it up to the rest of WireGuard.

* peer.[ch]: Each interface has a list of peers, with helper functions available here for creation, destruction, and reference counting.

* socket.[ch]: Implementation of functions related to udp_socket and the general set of kernel socket APIs, for sending and receiving ciphertext UDP packets, and taking care of WireGuard-specific sticky socket routing semantics for the automatic roaming.

* netlink.[ch]: Userspace API entry point for configuring WireGuard peers and devices. The API has been implemented by several userspace tools and network management utility, and the WireGuard project distributes the basic wg(8) tool.

* queueing.[ch]: Shared function on the rx and tx path for handling the various queues used in the multicore algorithms.

* send.c: Handles encrypting outgoing packets in parallel on multiple cores, before sending them in order on a single core, via workqueues and ring buffers. Also handles sending handshake and cookie messages as part of the protocol, in parallel.

* receive.c: Handles decrypting incoming packets in parallel on multiple cores, before passing them off in order to be ingested via the rest of the networking subsystem with GRO via the typical NAPI poll function. Also handles receiving handshake and cookie messages as part of the protocol, in parallel.

* timers.[ch]: Uses the timer wheel to implement protocol particular event timeouts, and gives a set of very simple event-driven entry point functions for callers.

* main.c, version.h: Initialization and deinitialization of the module.

* selftest/*.h: Runtime unit tests for some of the most security sensitive functions.

* tools/testing/selftests/wireguard/netns.sh: Aforementioned testing script using network namespaces.

This commit aims to be as self-contained as possible, implementing WireGuard as a standalone module not needing much special handling or coordination from the network subsystem. I expect for future optimizations to the network stack to positively improve WireGuard, and vice-versa, but for the time being, this exists as intentionally standalone.

We introduce a menu option for CONFIG_WIREGUARD, as well as providing a verbose debug log and self-tests via CONFIG_WIREGUARD_DEBUG.

---
 MAINTAINERS                                  |   8 +
 drivers/net/Kconfig                          |  30 +
 drivers/net/Makefile                         |   1 +
 drivers/net/wireguard/Makefile               |   4 +
 drivers/net/wireguard/allowedips.c           | 404 ++++++++++
 drivers/net/wireguard/allowedips.h           |  55 ++
 drivers/net/wireguard/cookie.c               | 234 ++++++
 drivers/net/wireguard/cookie.h               |  59 ++
 drivers/net/wireguard/device.c               | 438 +++++++++++
 drivers/net/wireguard/device.h               |  65 ++
 drivers/net/wireguard/hashtables.c           | 209 +++++
 drivers/net/wireguard/hashtables.h           |  63 ++
 drivers/net/wireguard/main.c                 |  65 ++
 drivers/net/wireguard/messages.h             | 128 +++
 drivers/net/wireguard/netlink.c              | 598 ++++++++++++++
 drivers/net/wireguard/netlink.h              |  12 +
 drivers/net/wireguard/noise.c                | 785 +++++++++++++++++++
 drivers/net/wireguard/noise.h                | 129 +++
 drivers/net/wireguard/peer.c                 | 191 +++++
 drivers/net/wireguard/peer.h                 |  87 ++
 drivers/net/wireguard/queueing.c             |  52 ++
 drivers/net/wireguard/queueing.h             | 193 +++++
 drivers/net/wireguard/ratelimiter.c          | 220 ++++++
 drivers/net/wireguard/ratelimiter.h          |  19 +
 drivers/net/wireguard/receive.c              | 597 ++++++++++++++
 drivers/net/wireguard/selftest/allowedips.h  | 658 ++++++++++++++++
 drivers/net/wireguard/selftest/counter.h     | 103 +++
 drivers/net/wireguard/selftest/ratelimiter.h | 174 ++++
 drivers/net/wireguard/send.c                 | 424 ++++++++++
 drivers/net/wireguard/socket.c               | 435 ++++++++++
 drivers/net/wireguard/socket.h               |  44 ++
 drivers/net/wireguard/timers.c               | 256 ++++++
 drivers/net/wireguard/timers.h               |  30 +
 drivers/net/wireguard/version.h              |   1 +
 include/uapi/linux/wireguard.h               | 182 +++++
 tools/testing/selftests/wireguard/netns.sh   | 499 ++++++++++++
 36 files changed, 7452 insertions(+)
 create mode 100644 drivers/net/wireguard/Makefile
 create mode 100644 drivers/net/wireguard/allowedips.c
 create mode 100644 drivers/net/wireguard/allowedips.h
 create mode 100644 drivers/net/wireguard/cookie.c
 create mode 100644 drivers/net/wireguard/cookie.h
 create mode 100644 drivers/net/wireguard/device.c
 create mode 100644 drivers/net/wireguard/device.h
 create mode 100644 drivers/net/wireguard/hashtables.c
 create mode 100644 drivers/net/wireguard/hashtables.h
 create mode 100644 drivers/net/wireguard/main.c
 create mode 100644 drivers/net/wireguard/messages.h
 create mode 100644 drivers/net/wireguard/netlink.c
 create mode 100644 drivers/net/wireguard/netlink.h
 create mode 100644 drivers/net/wireguard/noise.c
 create mode 100644 drivers/net/wireguard/noise.h
 create mode 100644 drivers/net/wireguard/peer.c
 create mode 100644 drivers/net/wireguard/peer.h
 create mode 100644 drivers/net/wireguard/queueing.c
 create mode 100644 drivers/net/wireguard/queueing.h
 create mode 100644 drivers/net/wireguard/ratelimiter.c
 create mode 100644 drivers/net/wireguard/ratelimiter.h
 create mode 100644 drivers/net/wireguard/receive.c
 create mode 100644 drivers/net/wireguard/selftest/allowedips.h
 create mode 100644 drivers/net/wireguard/selftest/counter.h
 create mode 100644 drivers/net/wireguard/selftest/ratelimiter.h
 create mode 100644 drivers/net/wireguard/send.c
 create mode 100644 drivers/net/wireguard/socket.c
 create mode 100644 drivers/net/wireguard/socket.h
 create mode 100644 drivers/net/wireguard/timers.c
 create mode 100644 drivers/net/wireguard/timers.h
 create mode 100644 drivers/net/wireguard/version.h
 create mode 100644 include/uapi/linux/wireguard.h
 create mode 100755 tools/testing/selftests/wireguard/netns.sh
MAINTAINERS +8 -0
index f194eda86011..0326ede9948b 100644
@@ -15756,6 +15756,14 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/gpio/gpio-ws16c48.c
 
+WIREGUARD SECURE NETWORK TUNNEL
+M:	Jason A. Donenfeld <[email protected]>
+S:	Maintained
+F:	drivers/net/wireguard/
+F:	tools/testing/selftests/wireguard/
+L:	[email protected]
+L:	[email protected]
+
 WISTRON LAPTOP BUTTON DRIVER
 M:	Miloslav Trmac <[email protected]>
 S:	Maintained
drivers/net/Kconfig +30 -0
index d03775100f7d..aa631fe3b395 100644
@@ -70,6 +70,36 @@ config DUMMY
 	  To compile this driver as a module, choose M here: the module
 	  will be called dummy.
 
+config WIREGUARD
+	tristate "WireGuard secure network tunnel"
+	depends on NET && INET
+	select NET_UDP_TUNNEL
+	select DST_CACHE
+	select ZINC_CHACHA20POLY1305
+	select ZINC_BLAKE2S
+	select ZINC_CURVE25519
+	default m
+	help
+	  WireGuard is a secure, fast, and easy to use replacement for IPSec
+	  that uses modern cryptography and clever networking tricks. It's
+	  designed to be fairly general purpose and abstract enough to fit most
+	  use cases, while at the same time remaining extremely simple to
+	  configure. See www.wireguard.com for more info.
+
+	  It's safe to say Y or M here, as the driver is very lightweight and
+	  is only in use when an administrator chooses to add an interface.
+
+config WIREGUARD_DEBUG
+	bool "Debugging checks and verbose messages"
+	depends on WIREGUARD
+	help
+	  This will write log messages for handshake and other events
+	  that occur for a WireGuard interface. It will also perform some
+	  extra validation checks and unit tests at various points. This is
+	  only useful for debugging.
+
+	  Say N here unless you know what you're doing.
+
 config EQUALIZER
 	tristate "EQL (serial line load balancing) support"
 	---help---
drivers/net/Makefile +1 -0
index 21cde7e78621..f0acd11a143d 100644
@@ -24,6 +24,7 @@ obj-$(CONFIG_RIONET) += rionet.o
 obj-$(CONFIG_NET_TEAM) += team/
 obj-$(CONFIG_TUN) += tun.o
 obj-$(CONFIG_TAP) += tap.o
+obj-$(CONFIG_WIREGUARD) += wireguard/
 obj-$(CONFIG_VETH) += veth.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
 obj-$(CONFIG_VXLAN) += vxlan.o
drivers/net/wireguard/Makefile new file +4 -0
new file mode 100644
index 000000000000..3ca7ef30808e
@@ -0,0 +1,4 @@
+ccflags-y := -O3
+ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
+wireguard-y := main.o noise.o device.o peer.o timers.o queueing.o send.o receive.o socket.o hashtables.o allowedips.o ratelimiter.o cookie.o netlink.o
+obj-$(CONFIG_WIREGUARD) := wireguard.o
drivers/net/wireguard/allowedips.c new file +404 -0

404 lines not on this page · raw message

drivers/net/wireguard/allowedips.h new file +55 -0

55 lines not on this page · raw message

drivers/net/wireguard/cookie.c new file +234 -0

234 lines not on this page · raw message

drivers/net/wireguard/cookie.h new file +59 -0

59 lines not on this page · raw message

drivers/net/wireguard/device.c new file +438 -0

438 lines not on this page · raw message

drivers/net/wireguard/device.h new file +65 -0

65 lines not on this page · raw message

drivers/net/wireguard/hashtables.c new file +209 -0

209 lines not on this page · raw message

drivers/net/wireguard/hashtables.h new file +63 -0

63 lines not on this page · raw message

drivers/net/wireguard/main.c new file +65 -0

65 lines not on this page · raw message

drivers/net/wireguard/messages.h new file +128 -0

128 lines not on this page · raw message

drivers/net/wireguard/netlink.c new file +598 -0

598 lines not on this page · raw message

drivers/net/wireguard/netlink.h new file +12 -0
new file mode 100644
index 000000000000..c1cd9b019bd1
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#ifndef _WG_NETLINK_H
+#define _WG_NETLINK_H
+
+int genetlink_init(void);
+void genetlink_uninit(void);
+
+#endif /* _WG_NETLINK_H */
drivers/net/wireguard/noise.c new file +785 -0

609 lines not on this page · raw message

drivers/net/wireguard/noise.h new file +129 -0

129 lines not on this page · raw message

drivers/net/wireguard/peer.c new file +191 -0

191 lines not on this page · raw message

drivers/net/wireguard/peer.h new file +87 -0

87 lines not on this page · raw message

drivers/net/wireguard/queueing.c new file +52 -0

52 lines not on this page · raw message

drivers/net/wireguard/queueing.h new file +193 -0

193 lines not on this page · raw message

drivers/net/wireguard/ratelimiter.c new file +220 -0

220 lines not on this page · raw message

drivers/net/wireguard/ratelimiter.h new file +19 -0
new file mode 100644
index 000000000000..a86c6d19b3a5

Lines of this file are not shown. View the raw message.

drivers/net/wireguard/receive.c new file +597 -0

597 lines not on this page · raw message

drivers/net/wireguard/selftest/allowedips.h new file +658 -0

658 lines not on this page · raw message

drivers/net/wireguard/selftest/counter.h new file +103 -0

103 lines not on this page · raw message

drivers/net/wireguard/selftest/ratelimiter.h new file +174 -0

174 lines not on this page · raw message

drivers/net/wireguard/send.c new file +424 -0

424 lines not on this page · raw message

drivers/net/wireguard/socket.c new file +435 -0

435 lines not on this page · raw message

drivers/net/wireguard/socket.h new file +44 -0

44 lines not on this page · raw message

drivers/net/wireguard/timers.c new file +256 -0

256 lines not on this page · raw message

drivers/net/wireguard/timers.h new file +30 -0
new file mode 100644
index 000000000000..483529c9d873

Lines of this file are not shown. View the raw message.

drivers/net/wireguard/version.h new file +1 -0
new file mode 100644
index 000000000000..a14de772487d

Lines of this file are not shown. View the raw message.

include/uapi/linux/wireguard.h new file +182 -0

182 lines not on this page · raw message

tools/testing/selftests/wireguard/netns.sh new file +499 -0

499 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.18.0

Part of the diff is not shown here. View the raw message.

#41 lore raw

Re: [PATCH v2 17/17] net: WireGuard secure network tunnel

Hi Jason

It is normal to include after the --- what you have changed since the previous version.

I ran checkpatch on this again.

Last time, we had:

total: 6 errors, 763 warnings, 6514 lines checked

This time, we have:

total: 8 errors, 196 warnings, 7470 lines checked

So much better with the warnings. Thanks.

The errors should be simple to fix:

ERROR: that open brace { should be on the previous line
ERROR: that open brace { should be on the previous line
ERROR: spaces required around that '=' (ctx:VxW)
ERROR: spaces required around that '=' (ctx:VxW)
ERROR: spaces required around that '=' (ctx:VxW)
ERROR: that open brace { should be on the previous line
ERROR: Use of __initconst requires a separate use of const
ERROR: trailing whitespace

One appears to be a false positive:

+static const struct {
+ bool result;
+ unsigned int msec_to_sleep_before;
+} expected_results[] __initconst = {

+       [0 ... PACKETS_BURSTABLE - 1] = { true, 0 },
+       [PACKETS_BURSTABLE] = { false, 0 },
+       [PACKETS_BURSTABLE + 1] = { true, MSEC_PER_SEC / PACKETS_PER_SECOND },
+       [PACKETS_BURSTABLE + 2] = { false, 0 },
+       [PACKETS_BURSTABLE + 3] = { true, (MSEC_PER_SEC / PACKETS_PER_SECOND) * 2 },
+       [PACKETS_BURSTABLE + 4] = { true, 0 },
+       [PACKETS_BURSTABLE + 5] = { false, 0 }

+};

Looking at some of the warnings:

WARNING: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON() #428: FILE: drivers/net/wireguard/allowedips.c:112:
+ BUG_ON(len >= 128);

WARNING: Macros with flow control statements should be avoided #5905: FILE: drivers/net/wireguard/selftest/ratelimiter.h:87:
+#define ensure_time do { \
+ if (time_is_before_jiffies(loop_start_time + \
+ maximum_jiffies_at_index(i))) { \

+                       if (++tries >= 5000)                               \
+                               goto err;                                  \
+                       gc_entries(NULL);                                  \
+                       rcu_barrier();                                     \
+                       msleep(500);                                       \
+                       goto restart;                                      \
+               }                                                          \

+ } while (0)

WARNING: Macros with flow control statements should be avoided #6948: FILE: drivers/net/wireguard/timers.c:29:
+#define peer_get_from_timer(timer_name) \

+       struct wireguard_peer *peer;                                           \
+       rcu_read_lock_bh();                                                    \
+       peer = peer_get_maybe_zero(from_timer(peer, timer, timer_name));       \
+       rcu_read_unlock_bh();                                                  \
+       if (unlikely(!peer))                                                   \

+ return;

are not very nice.

WARNING: Possible unnecessary 'out of memory' message #5306: FILE: drivers/net/wireguard/selftest/allowedips.h:261:
+ if (!peers) {
+ pr_info("allowedips random self-test: out of memory\n");

kcalloc and friends are pretty noisy when they fails. No need to add your own print.

I see we still have a lot of __always_inline in C files :-(

Andrew

#42 lore raw

Re: [PATCH v2 17/17] net: WireGuard secure network tunnel

In reply to Jason A. Donenfeld
I love your patch! Yet something to improve:

Hi Jason,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.19-rc1 next-20180827]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Jason-A-Donenfeld/WireGuard-Secure-Network-Tunnel/20180827-073051
config: arm-raumfeld_defconfig (attached as .config)
compiler: arm-linux-gnueabi-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=arm

All errors (new ones prefixed by >>):

In file included from <command-line>:0:0:
lib/zinc/chacha20/chacha20-arm-glue.h: In function 'chacha20_arch':

>> lib/zinc/chacha20/chacha20-arm-glue.h:36:3: error: implicit declaration of function 'chacha20_neon'; did you mean 'chacha20_arch'? [-Werror=implicit-function-declaration]
      chacha20_neon(dst, src, len, key, counter);
      ^~~~~~~~~~~~~
      chacha20_arch

cc1: some warnings being treated as errors

vim +36 lib/zinc/chacha20/chacha20-arm-glue.h

959d9378 Jason A. Donenfeld 2018-08-24  26  
959d9378 Jason A. Donenfeld 2018-08-24  27  static inline bool chacha20_arch(u8 *dst, const u8 *src, const size_t len, const u32 key[8], const u32 counter[4], simd_context_t simd_context)
959d9378 Jason A. Donenfeld 2018-08-24  28  {
959d9378 Jason A. Donenfeld 2018-08-24  29  	if (simd_context != HAVE_FULL_SIMD
959d9378 Jason A. Donenfeld 2018-08-24  30  #if defined(ARM_USE_NEON)
959d9378 Jason A. Donenfeld 2018-08-24  31  		|| !chacha20_use_neon
959d9378 Jason A. Donenfeld 2018-08-24  32  #endif
959d9378 Jason A. Donenfeld 2018-08-24  33  	)
959d9378 Jason A. Donenfeld 2018-08-24  34  		chacha20_arm(dst, src, len, key, counter);
959d9378 Jason A. Donenfeld 2018-08-24  35  	else
959d9378 Jason A. Donenfeld 2018-08-24 @36  		chacha20_neon(dst, src, len, key, counter);
959d9378 Jason A. Donenfeld 2018-08-24  37  	return true;
959d9378 Jason A. Donenfeld 2018-08-24  38  }

959d9378 Jason A. Donenfeld 2018-08-24 39

:::::: The code at line 36 was first introduced by commit
:::::: 959d93782f7ebf927cceeeb5ba86331211abfcd8 zinc: ChaCha20 ARM and ARM64 implementations

:::::: TO: Jason A. Donenfeld <[email protected]>
:::::: CC: 0day robot <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#43 lore raw

Re: [PATCH v2 17/17] net: WireGuard secure network tunnel

In reply to Jason A. Donenfeld
I love your patch! Yet something to improve:

Hi Jason,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.19-rc1 next-20180827]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Jason-A-Donenfeld/WireGuard-Secure-Network-Tunnel/20180827-073051
config: um-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=um 

All error/warnings (new ones prefixed by >>):

In file included from lib/zinc/chacha20/chacha20-x86_64-glue.h:8:0,

                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from <command-line>:0:0:
   At top level:
   lib/zinc/chacha20/chacha20-x86_64-glue.h:27:13: warning: 'chacha20_use_avx512vl' defined but not used [-Wunused-variable]
    static bool chacha20_use_avx512vl __ro_after_init;
                ^~~~~~~~~~~~~~~~~~~~~
   lib/zinc/chacha20/chacha20-x86_64-glue.h:26:13: warning: 'chacha20_use_avx512' defined but not used [-Wunused-variable]
    static bool chacha20_use_avx512 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~~
   lib/zinc/chacha20/chacha20-x86_64-glue.h:25:13: warning: 'chacha20_use_avx2' defined but not used [-Wunused-variable]
    static bool chacha20_use_avx2 __ro_after_init;
                ^~~~~~~~~~~~~~~~~
   lib/zinc/chacha20/chacha20-x86_64-glue.h:24:13: warning: 'chacha20_use_ssse3' defined but not used [-Wunused-variable]
    static bool chacha20_use_ssse3 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~
--
   In file included from lib/zinc/poly1305/poly1305-x86_64-glue.h:8:0,
                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from <command-line>:0:0:
   At top level:
   lib/zinc/poly1305/poly1305-x86_64-glue.h:28:13: warning: 'poly1305_use_avx512' defined but not used [-Wunused-variable]
    static bool poly1305_use_avx512 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~~
   lib/zinc/poly1305/poly1305-x86_64-glue.h:27:13: warning: 'poly1305_use_avx2' defined but not used [-Wunused-variable]
    static bool poly1305_use_avx2 __ro_after_init;
                ^~~~~~~~~~~~~~~~~
   lib/zinc/poly1305/poly1305-x86_64-glue.h:26:13: warning: 'poly1305_use_avx' defined but not used [-Wunused-variable]
    static bool poly1305_use_avx __ro_after_init;
                ^~~~~~~~~~~~~~~~
--
   In file included from lib/zinc/curve25519/curve25519-x86_64-glue.h:7:0,
                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:49:41: error: 'NBUGINTS' undeclared here (not in a function)
    extern const char * const x86_bug_flags[NBUGINTS*32];
                                            ^~~~~~~~
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:196:24: error: 'X86_FEATURE_ALWAYS' undeclared (first use in this function)
           [always]   "i" (X86_FEATURE_ALWAYS),
                           ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   arch/x86/include/asm/cpufeature.h:196:24: note: each undeclared identifier is reported only once for each function it appears in
           [always]   "i" (X86_FEATURE_ALWAYS),
                           ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from lib/zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib/zinc/curve25519/curve25519-x86_64.h: In function 'inv_eltfp25519_1w_adx':
>> lib/zinc/curve25519/curve25519-x86_64.h:1543:2: error: implicit declaration of function 'memzero_explicit' [-Werror=implicit-function-declaration]
     memzero_explicit(&m, sizeof(m));
     ^~~~~~~~~~~~~~~~
   In file included from lib/zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib/zinc/curve25519/curve25519-x86_64.h: In function 'curve25519_adx':
>> lib/zinc/curve25519/curve25519-x86_64.h:1706:2: error: implicit declaration of function 'memcpy'; did you mean 'pte_copy'? [-Werror=implicit-function-declaration]
     memcpy(m.private, private_key, sizeof(m.private));
     ^~~~~~
     pte_copy
   In file included from <command-line>:0:0:
   lib/zinc/curve25519/curve25519-x86_64-glue.h: At top level:
>> lib/zinc/curve25519/curve25519-x86_64-glue.h:12:33: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__ro_after_init'
    static bool curve25519_use_bmi2 __ro_after_init;
                                    ^~~~~~~~~~~~~~~
   lib/zinc/curve25519/curve25519-x86_64-glue.h:13:32: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__ro_after_init'
    static bool curve25519_use_adx __ro_after_init;
                                   ^~~~~~~~~~~~~~~
>> lib/zinc/curve25519/curve25519-x86_64-glue.h:15:13: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'curve25519_fpu_init'
    void __init curve25519_fpu_init(void)
                ^~~~~~~~~~~~~~~~~~~
   In file included from <command-line>:0:0:
   lib/zinc/curve25519/curve25519-x86_64-glue.h: In function 'curve25519_arch':
>> lib/zinc/curve25519/curve25519-x86_64-glue.h:23:6: error: 'curve25519_use_adx' undeclared (first use in this function); did you mean 'curve25519_adx'?
     if (curve25519_use_adx) {
         ^~~~~~~~~~~~~~~~~~
         curve25519_adx
>> lib/zinc/curve25519/curve25519-x86_64-glue.h:26:13: error: 'curve25519_use_bmi2' undeclared (first use in this function); did you mean 'curve25519_use_adx'?
     } else if (curve25519_use_bmi2) {
                ^~~~~~~~~~~~~~~~~~~
                curve25519_use_adx
   lib/zinc/curve25519/curve25519-x86_64-glue.h: In function 'curve25519_base_arch':
   lib/zinc/curve25519/curve25519-x86_64-glue.h:35:6: error: 'curve25519_use_adx' undeclared (first use in this function); did you mean 'curve25519_adx'?
     if (curve25519_use_adx) {
         ^~~~~~~~~~~~~~~~~~
         curve25519_adx
   lib/zinc/curve25519/curve25519-x86_64-glue.h:38:13: error: 'curve25519_use_bmi2' undeclared (first use in this function); did you mean 'curve25519_use_adx'?
     } else if (curve25519_use_bmi2) {
                ^~~~~~~~~~~~~~~~~~~
                curve25519_use_adx
   In file included from arch/x86/include/asm/string.h:5:0,
                    from include/linux/string.h:20,
                    from lib/zinc/curve25519/curve25519.c:9:
   arch/x86/include/asm/string_64.h: At top level:
>> arch/x86/include/asm/string_64.h:32:14: error: conflicting types for 'memcpy'
    extern void *memcpy(void *to, const void *from, size_t len);
                 ^~~~~~
   In file included from lib/zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib/zinc/curve25519/curve25519-x86_64.h:1706:2: note: previous implicit declaration of 'memcpy' was here
     memcpy(m.private, private_key, sizeof(m.private));
     ^~~~~~
   In file included from lib/zinc/curve25519/curve25519.c:9:0:
>> include/linux/string.h:216:6: warning: conflicting types for 'memzero_explicit'
    void memzero_explicit(void *s, size_t count);
         ^~~~~~~~~~~~~~~~
   In file included from lib/zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib/zinc/curve25519/curve25519-x86_64.h:1543:2: note: previous implicit declaration of 'memzero_explicit' was here
     memzero_explicit(&m, sizeof(m));
     ^~~~~~~~~~~~~~~~
   cc1: some warnings being treated as errors
--
   In file included from lib/zinc/blake2s/blake2s-x86_64-glue.h:7:0,
                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from <command-line>:0:0:
   At top level:
   lib/zinc/blake2s/blake2s-x86_64-glue.h:20:13: warning: 'blake2s_use_avx512' defined but not used [-Wunused-variable]
    static bool blake2s_use_avx512 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~
   lib/zinc/blake2s/blake2s-x86_64-glue.h:19:13: warning: 'blake2s_use_avx' defined but not used [-Wunused-variable]
    static bool blake2s_use_avx __ro_after_init;
                ^~~~~~~~~~~~~~~
--
   In file included from lib//zinc/chacha20/chacha20-x86_64-glue.h:8:0,
                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from <command-line>:0:0:
   At top level:
   lib//zinc/chacha20/chacha20-x86_64-glue.h:27:13: warning: 'chacha20_use_avx512vl' defined but not used [-Wunused-variable]
    static bool chacha20_use_avx512vl __ro_after_init;
                ^~~~~~~~~~~~~~~~~~~~~
   lib//zinc/chacha20/chacha20-x86_64-glue.h:26:13: warning: 'chacha20_use_avx512' defined but not used [-Wunused-variable]
    static bool chacha20_use_avx512 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~~
   lib//zinc/chacha20/chacha20-x86_64-glue.h:25:13: warning: 'chacha20_use_avx2' defined but not used [-Wunused-variable]
    static bool chacha20_use_avx2 __ro_after_init;
                ^~~~~~~~~~~~~~~~~
   lib//zinc/chacha20/chacha20-x86_64-glue.h:24:13: warning: 'chacha20_use_ssse3' defined but not used [-Wunused-variable]
    static bool chacha20_use_ssse3 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~
--
   In file included from lib//zinc/poly1305/poly1305-x86_64-glue.h:8:0,
                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from <command-line>:0:0:
   At top level:
   lib//zinc/poly1305/poly1305-x86_64-glue.h:28:13: warning: 'poly1305_use_avx512' defined but not used [-Wunused-variable]
    static bool poly1305_use_avx512 __ro_after_init;
                ^~~~~~~~~~~~~~~~~~~
   lib//zinc/poly1305/poly1305-x86_64-glue.h:27:13: warning: 'poly1305_use_avx2' defined but not used [-Wunused-variable]
    static bool poly1305_use_avx2 __ro_after_init;
                ^~~~~~~~~~~~~~~~~
   lib//zinc/poly1305/poly1305-x86_64-glue.h:26:13: warning: 'poly1305_use_avx' defined but not used [-Wunused-variable]
    static bool poly1305_use_avx __ro_after_init;
                ^~~~~~~~~~~~~~~~
--
   In file included from lib//zinc/curve25519/curve25519-x86_64-glue.h:7:0,
                    from <command-line>:0:
>> arch/x86/include/asm/cpufeature.h:49:41: error: 'NBUGINTS' undeclared here (not in a function)
    extern const char * const x86_bug_flags[NBUGINTS*32];
                                            ^~~~~~~~
>> arch/x86/include/asm/cpufeature.h:134:34: warning: 'struct cpuinfo_x86' declared inside parameter list will not be visible outside of this definition or declaration
    extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
                                     ^~~~~~~~~~~
   In file included from include/linux/compiler_types.h:64:0,
                    from <command-line>:0:
   arch/x86/include/asm/cpufeature.h: In function '_static_cpu_has':
>> arch/x86/include/asm/cpufeature.h:196:24: error: 'X86_FEATURE_ALWAYS' undeclared (first use in this function)
           [always]   "i" (X86_FEATURE_ALWAYS),
                           ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   arch/x86/include/asm/cpufeature.h:196:24: note: each undeclared identifier is reported only once for each function it appears in
           [always]   "i" (X86_FEATURE_ALWAYS),
                           ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
>> arch/x86/include/asm/cpufeature.h:198:52: error: 'struct cpuinfo_um' has no member named 'x86_capability'
           [cap_byte] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
                                                       ^
   include/linux/compiler-gcc.h:182:47: note: in definition of macro 'asm_volatile_goto'
    #define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
                                                  ^
   In file included from lib//zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib//zinc/curve25519/curve25519-x86_64.h: In function 'inv_eltfp25519_1w_adx':
   lib//zinc/curve25519/curve25519-x86_64.h:1543:2: error: implicit declaration of function 'memzero_explicit' [-Werror=implicit-function-declaration]
     memzero_explicit(&m, sizeof(m));
     ^~~~~~~~~~~~~~~~
   In file included from lib//zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib//zinc/curve25519/curve25519-x86_64.h: In function 'curve25519_adx':
   lib//zinc/curve25519/curve25519-x86_64.h:1706:2: error: implicit declaration of function 'memcpy'; did you mean 'pte_copy'? [-Werror=implicit-function-declaration]
     memcpy(m.private, private_key, sizeof(m.private));
     ^~~~~~
     pte_copy
   In file included from <command-line>:0:0:
   lib//zinc/curve25519/curve25519-x86_64-glue.h: At top level:
   lib//zinc/curve25519/curve25519-x86_64-glue.h:12:33: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__ro_after_init'
    static bool curve25519_use_bmi2 __ro_after_init;
                                    ^~~~~~~~~~~~~~~
   lib//zinc/curve25519/curve25519-x86_64-glue.h:13:32: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__ro_after_init'
    static bool curve25519_use_adx __ro_after_init;
                                   ^~~~~~~~~~~~~~~
   lib//zinc/curve25519/curve25519-x86_64-glue.h:15:13: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'curve25519_fpu_init'
    void __init curve25519_fpu_init(void)
                ^~~~~~~~~~~~~~~~~~~
   In file included from <command-line>:0:0:
   lib//zinc/curve25519/curve25519-x86_64-glue.h: In function 'curve25519_arch':
   lib//zinc/curve25519/curve25519-x86_64-glue.h:23:6: error: 'curve25519_use_adx' undeclared (first use in this function); did you mean 'curve25519_adx'?
     if (curve25519_use_adx) {
         ^~~~~~~~~~~~~~~~~~
         curve25519_adx
   lib//zinc/curve25519/curve25519-x86_64-glue.h:26:13: error: 'curve25519_use_bmi2' undeclared (first use in this function); did you mean 'curve25519_use_adx'?
     } else if (curve25519_use_bmi2) {
                ^~~~~~~~~~~~~~~~~~~
                curve25519_use_adx
   lib//zinc/curve25519/curve25519-x86_64-glue.h: In function 'curve25519_base_arch':
   lib//zinc/curve25519/curve25519-x86_64-glue.h:35:6: error: 'curve25519_use_adx' undeclared (first use in this function); did you mean 'curve25519_adx'?
     if (curve25519_use_adx) {
         ^~~~~~~~~~~~~~~~~~
         curve25519_adx
   lib//zinc/curve25519/curve25519-x86_64-glue.h:38:13: error: 'curve25519_use_bmi2' undeclared (first use in this function); did you mean 'curve25519_use_adx'?
     } else if (curve25519_use_bmi2) {
                ^~~~~~~~~~~~~~~~~~~
                curve25519_use_adx
   In file included from arch/x86/include/asm/string.h:5:0,
                    from include/linux/string.h:20,
                    from lib//zinc/curve25519/curve25519.c:9:
   arch/x86/include/asm/string_64.h: At top level:
>> arch/x86/include/asm/string_64.h:32:14: error: conflicting types for 'memcpy'
    extern void *memcpy(void *to, const void *from, size_t len);
                 ^~~~~~
   In file included from lib//zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib//zinc/curve25519/curve25519-x86_64.h:1706:2: note: previous implicit declaration of 'memcpy' was here
     memcpy(m.private, private_key, sizeof(m.private));
     ^~~~~~
   In file included from lib//zinc/curve25519/curve25519.c:9:0:
>> include/linux/string.h:216:6: warning: conflicting types for 'memzero_explicit'
    void memzero_explicit(void *s, size_t count);
         ^~~~~~~~~~~~~~~~
   In file included from lib//zinc/curve25519/curve25519-x86_64-glue.h:10:0,
                    from <command-line>:0:
   lib//zinc/curve25519/curve25519-x86_64.h:1543:2: note: previous implicit declaration of 'memzero_explicit' was here
     memzero_explicit(&m, sizeof(m));
     ^~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
..

vim +/memzero_explicit +1543 lib/zinc/curve25519/curve25519-x86_64.h

468c57c7 Jason A. Donenfeld 2018-08-24  1498  
468c57c7 Jason A. Donenfeld 2018-08-24  1499  static void inv_eltfp25519_1w_adx(u64 *const c, const u64 *const a)
468c57c7 Jason A. Donenfeld 2018-08-24  1500  {
468c57c7 Jason A. Donenfeld 2018-08-24  1501  	struct {
468c57c7 Jason A. Donenfeld 2018-08-24  1502  		eltfp25519_1w_buffer buffer;
468c57c7 Jason A. Donenfeld 2018-08-24  1503  		eltfp25519_1w x0, x1, x2;
468c57c7 Jason A. Donenfeld 2018-08-24  1504  	} __aligned(32) m;
468c57c7 Jason A. Donenfeld 2018-08-24  1505  	u64 *T[4];
468c57c7 Jason A. Donenfeld 2018-08-24  1506  
468c57c7 Jason A. Donenfeld 2018-08-24  1507  	T[0] = m.x0;
468c57c7 Jason A. Donenfeld 2018-08-24  1508  	T[1] = c; /* x^(-1) */
468c57c7 Jason A. Donenfeld 2018-08-24  1509  	T[2] = m.x1;
468c57c7 Jason A. Donenfeld 2018-08-24  1510  	T[3] = m.x2;
468c57c7 Jason A. Donenfeld 2018-08-24  1511  
468c57c7 Jason A. Donenfeld 2018-08-24  1512  	copy_eltfp25519_1w(T[1], a);
468c57c7 Jason A. Donenfeld 2018-08-24  1513  	sqrn_eltfp25519_1w_adx(T[1], 1);
468c57c7 Jason A. Donenfeld 2018-08-24  1514  	copy_eltfp25519_1w(T[2], T[1]);
468c57c7 Jason A. Donenfeld 2018-08-24  1515  	sqrn_eltfp25519_1w_adx(T[2], 2);
468c57c7 Jason A. Donenfeld 2018-08-24  1516  	mul_eltfp25519_1w_adx(T[0], a, T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1517  	mul_eltfp25519_1w_adx(T[1], T[1], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1518  	copy_eltfp25519_1w(T[2], T[1]);
468c57c7 Jason A. Donenfeld 2018-08-24  1519  	sqrn_eltfp25519_1w_adx(T[2], 1);
468c57c7 Jason A. Donenfeld 2018-08-24  1520  	mul_eltfp25519_1w_adx(T[0], T[0], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1521  	copy_eltfp25519_1w(T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1522  	sqrn_eltfp25519_1w_adx(T[2], 5);
468c57c7 Jason A. Donenfeld 2018-08-24  1523  	mul_eltfp25519_1w_adx(T[0], T[0], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1524  	copy_eltfp25519_1w(T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1525  	sqrn_eltfp25519_1w_adx(T[2], 10);
468c57c7 Jason A. Donenfeld 2018-08-24  1526  	mul_eltfp25519_1w_adx(T[2], T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1527  	copy_eltfp25519_1w(T[3], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1528  	sqrn_eltfp25519_1w_adx(T[3], 20);
468c57c7 Jason A. Donenfeld 2018-08-24  1529  	mul_eltfp25519_1w_adx(T[3], T[3], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1530  	sqrn_eltfp25519_1w_adx(T[3], 10);
468c57c7 Jason A. Donenfeld 2018-08-24  1531  	mul_eltfp25519_1w_adx(T[3], T[3], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1532  	copy_eltfp25519_1w(T[0], T[3]);
468c57c7 Jason A. Donenfeld 2018-08-24  1533  	sqrn_eltfp25519_1w_adx(T[0], 50);
468c57c7 Jason A. Donenfeld 2018-08-24  1534  	mul_eltfp25519_1w_adx(T[0], T[0], T[3]);
468c57c7 Jason A. Donenfeld 2018-08-24  1535  	copy_eltfp25519_1w(T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1536  	sqrn_eltfp25519_1w_adx(T[2], 100);
468c57c7 Jason A. Donenfeld 2018-08-24  1537  	mul_eltfp25519_1w_adx(T[2], T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1538  	sqrn_eltfp25519_1w_adx(T[2], 50);
468c57c7 Jason A. Donenfeld 2018-08-24  1539  	mul_eltfp25519_1w_adx(T[2], T[2], T[3]);
468c57c7 Jason A. Donenfeld 2018-08-24  1540  	sqrn_eltfp25519_1w_adx(T[2], 5);
468c57c7 Jason A. Donenfeld 2018-08-24  1541  	mul_eltfp25519_1w_adx(T[1], T[1], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1542  
468c57c7 Jason A. Donenfeld 2018-08-24 @1543  	memzero_explicit(&m, sizeof(m));
468c57c7 Jason A. Donenfeld 2018-08-24  1544  }
468c57c7 Jason A. Donenfeld 2018-08-24  1545  
468c57c7 Jason A. Donenfeld 2018-08-24  1546  static void inv_eltfp25519_1w_bmi2(u64 *const c, const u64 *const a)
468c57c7 Jason A. Donenfeld 2018-08-24  1547  {
468c57c7 Jason A. Donenfeld 2018-08-24  1548  	struct {
468c57c7 Jason A. Donenfeld 2018-08-24  1549  		eltfp25519_1w_buffer buffer;
468c57c7 Jason A. Donenfeld 2018-08-24  1550  		eltfp25519_1w x0, x1, x2;
468c57c7 Jason A. Donenfeld 2018-08-24  1551  	} __aligned(32) m;
468c57c7 Jason A. Donenfeld 2018-08-24  1552  	u64 *T[5];
468c57c7 Jason A. Donenfeld 2018-08-24  1553  
468c57c7 Jason A. Donenfeld 2018-08-24  1554  	T[0] = m.x0;
468c57c7 Jason A. Donenfeld 2018-08-24  1555  	T[1] = c; /* x^(-1) */
468c57c7 Jason A. Donenfeld 2018-08-24  1556  	T[2] = m.x1;
468c57c7 Jason A. Donenfeld 2018-08-24  1557  	T[3] = m.x2;
468c57c7 Jason A. Donenfeld 2018-08-24  1558  
468c57c7 Jason A. Donenfeld 2018-08-24  1559  	copy_eltfp25519_1w(T[1], a);
468c57c7 Jason A. Donenfeld 2018-08-24  1560  	sqrn_eltfp25519_1w_bmi2(T[1], 1);
468c57c7 Jason A. Donenfeld 2018-08-24  1561  	copy_eltfp25519_1w(T[2], T[1]);
468c57c7 Jason A. Donenfeld 2018-08-24  1562  	sqrn_eltfp25519_1w_bmi2(T[2], 2);
468c57c7 Jason A. Donenfeld 2018-08-24  1563  	mul_eltfp25519_1w_bmi2(T[0], a, T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1564  	mul_eltfp25519_1w_bmi2(T[1], T[1], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1565  	copy_eltfp25519_1w(T[2], T[1]);
468c57c7 Jason A. Donenfeld 2018-08-24  1566  	sqrn_eltfp25519_1w_bmi2(T[2], 1);
468c57c7 Jason A. Donenfeld 2018-08-24  1567  	mul_eltfp25519_1w_bmi2(T[0], T[0], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1568  	copy_eltfp25519_1w(T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1569  	sqrn_eltfp25519_1w_bmi2(T[2], 5);
468c57c7 Jason A. Donenfeld 2018-08-24  1570  	mul_eltfp25519_1w_bmi2(T[0], T[0], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1571  	copy_eltfp25519_1w(T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1572  	sqrn_eltfp25519_1w_bmi2(T[2], 10);
468c57c7 Jason A. Donenfeld 2018-08-24  1573  	mul_eltfp25519_1w_bmi2(T[2], T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1574  	copy_eltfp25519_1w(T[3], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1575  	sqrn_eltfp25519_1w_bmi2(T[3], 20);
468c57c7 Jason A. Donenfeld 2018-08-24  1576  	mul_eltfp25519_1w_bmi2(T[3], T[3], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1577  	sqrn_eltfp25519_1w_bmi2(T[3], 10);
468c57c7 Jason A. Donenfeld 2018-08-24  1578  	mul_eltfp25519_1w_bmi2(T[3], T[3], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1579  	copy_eltfp25519_1w(T[0], T[3]);
468c57c7 Jason A. Donenfeld 2018-08-24  1580  	sqrn_eltfp25519_1w_bmi2(T[0], 50);
468c57c7 Jason A. Donenfeld 2018-08-24  1581  	mul_eltfp25519_1w_bmi2(T[0], T[0], T[3]);
468c57c7 Jason A. Donenfeld 2018-08-24  1582  	copy_eltfp25519_1w(T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1583  	sqrn_eltfp25519_1w_bmi2(T[2], 100);
468c57c7 Jason A. Donenfeld 2018-08-24  1584  	mul_eltfp25519_1w_bmi2(T[2], T[2], T[0]);
468c57c7 Jason A. Donenfeld 2018-08-24  1585  	sqrn_eltfp25519_1w_bmi2(T[2], 50);
468c57c7 Jason A. Donenfeld 2018-08-24  1586  	mul_eltfp25519_1w_bmi2(T[2], T[2], T[3]);
468c57c7 Jason A. Donenfeld 2018-08-24  1587  	sqrn_eltfp25519_1w_bmi2(T[2], 5);
468c57c7 Jason A. Donenfeld 2018-08-24  1588  	mul_eltfp25519_1w_bmi2(T[1], T[1], T[2]);
468c57c7 Jason A. Donenfeld 2018-08-24  1589  
468c57c7 Jason A. Donenfeld 2018-08-24  1590  	memzero_explicit(&m, sizeof(m));
468c57c7 Jason A. Donenfeld 2018-08-24  1591  }
468c57c7 Jason A. Donenfeld 2018-08-24  1592  
468c57c7 Jason A. Donenfeld 2018-08-24  1593  /* Given c, a 256-bit number, fred_eltfp25519_1w updates c
468c57c7 Jason A. Donenfeld 2018-08-24  1594   * with a number such that 0 <= C < 2**255-19.
468c57c7 Jason A. Donenfeld 2018-08-24  1595   */
468c57c7 Jason A. Donenfeld 2018-08-24  1596  static __always_inline void fred_eltfp25519_1w(u64 *const c)
468c57c7 Jason A. Donenfeld 2018-08-24  1597  {
468c57c7 Jason A. Donenfeld 2018-08-24  1598  	u64 tmp0 = 38, tmp1 = 19;
468c57c7 Jason A. Donenfeld 2018-08-24  1599  	asm volatile(
468c57c7 Jason A. Donenfeld 2018-08-24  1600  		"btrq   $63,    %3 ;" /* Put bit 255 in carry flag and clear */
468c57c7 Jason A. Donenfeld 2018-08-24  1601  		"cmovncl %k5,   %k4 ;" /* c[255] ? 38 : 19 */
468c57c7 Jason A. Donenfeld 2018-08-24  1602  
468c57c7 Jason A. Donenfeld 2018-08-24  1603  		/* Add either 19 or 38 to c */
468c57c7 Jason A. Donenfeld 2018-08-24  1604  		"addq    %4,   %0 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1605  		"adcq    $0,   %1 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1606  		"adcq    $0,   %2 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1607  		"adcq    $0,   %3 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1608  
468c57c7 Jason A. Donenfeld 2018-08-24  1609  		/* Test for bit 255 again; only triggered on overflow modulo 2^255-19 */
468c57c7 Jason A. Donenfeld 2018-08-24  1610  		"movl    $0,  %k4 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1611  		"cmovnsl %k5,  %k4 ;" /* c[255] ? 0 : 19 */
468c57c7 Jason A. Donenfeld 2018-08-24  1612  		"btrq   $63,   %3 ;" /* Clear bit 255 */
468c57c7 Jason A. Donenfeld 2018-08-24  1613  
468c57c7 Jason A. Donenfeld 2018-08-24  1614  		/* Subtract 19 if necessary */
468c57c7 Jason A. Donenfeld 2018-08-24  1615  		"subq    %4,   %0 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1616  		"sbbq    $0,   %1 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1617  		"sbbq    $0,   %2 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1618  		"sbbq    $0,   %3 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1619  
468c57c7 Jason A. Donenfeld 2018-08-24  1620  		: "+r"(c[0]), "+r"(c[1]), "+r"(c[2]), "+r"(c[3]), "+r"(tmp0), "+r"(tmp1)
468c57c7 Jason A. Donenfeld 2018-08-24  1621  		:
468c57c7 Jason A. Donenfeld 2018-08-24  1622  		: "memory", "cc");
468c57c7 Jason A. Donenfeld 2018-08-24  1623  }
468c57c7 Jason A. Donenfeld 2018-08-24  1624  
468c57c7 Jason A. Donenfeld 2018-08-24  1625  static __always_inline void cswap(u8 bit, u64 *const px, u64 *const py)
468c57c7 Jason A. Donenfeld 2018-08-24  1626  {
468c57c7 Jason A. Donenfeld 2018-08-24  1627  	u64 temp;
468c57c7 Jason A. Donenfeld 2018-08-24  1628  	asm volatile(
468c57c7 Jason A. Donenfeld 2018-08-24  1629  		"test %9, %9 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1630  		"movq %0, %8 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1631  		"cmovnzq %4, %0 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1632  		"cmovnzq %8, %4 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1633  		"movq %1, %8 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1634  		"cmovnzq %5, %1 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1635  		"cmovnzq %8, %5 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1636  		"movq %2, %8 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1637  		"cmovnzq %6, %2 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1638  		"cmovnzq %8, %6 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1639  		"movq %3, %8 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1640  		"cmovnzq %7, %3 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1641  		"cmovnzq %8, %7 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1642  		: "+r"(px[0]), "+r"(px[1]), "+r"(px[2]), "+r"(px[3]),
468c57c7 Jason A. Donenfeld 2018-08-24  1643  		  "+r"(py[0]), "+r"(py[1]), "+r"(py[2]), "+r"(py[3]),
468c57c7 Jason A. Donenfeld 2018-08-24  1644  		  "=r"(temp)
468c57c7 Jason A. Donenfeld 2018-08-24  1645  		: "r"(bit)
468c57c7 Jason A. Donenfeld 2018-08-24  1646  		: "cc"
468c57c7 Jason A. Donenfeld 2018-08-24  1647  	);
468c57c7 Jason A. Donenfeld 2018-08-24  1648  }
468c57c7 Jason A. Donenfeld 2018-08-24  1649  
468c57c7 Jason A. Donenfeld 2018-08-24  1650  static __always_inline void cselect(u8 bit, u64 *const px, const u64 *const py)
468c57c7 Jason A. Donenfeld 2018-08-24  1651  {
468c57c7 Jason A. Donenfeld 2018-08-24  1652  	asm volatile(
468c57c7 Jason A. Donenfeld 2018-08-24  1653  		"test %4, %4 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1654  		"cmovnzq %5, %0 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1655  		"cmovnzq %6, %1 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1656  		"cmovnzq %7, %2 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1657  		"cmovnzq %8, %3 ;"
468c57c7 Jason A. Donenfeld 2018-08-24  1658  		: "+r"(px[0]), "+r"(px[1]), "+r"(px[2]), "+r"(px[3])
468c57c7 Jason A. Donenfeld 2018-08-24  1659  		: "r"(bit), "rm"(py[0]), "rm"(py[1]), "rm"(py[2]), "rm"(py[3])
468c57c7 Jason A. Donenfeld 2018-08-24  1660  		: "cc"
468c57c7 Jason A. Donenfeld 2018-08-24  1661  	);
468c57c7 Jason A. Donenfeld 2018-08-24  1662  }
468c57c7 Jason A. Donenfeld 2018-08-24  1663  
468c57c7 Jason A. Donenfeld 2018-08-24  1664  static __always_inline void clamp_secret(u8 secret[CURVE25519_POINT_SIZE])
468c57c7 Jason A. Donenfeld 2018-08-24  1665  {
468c57c7 Jason A. Donenfeld 2018-08-24  1666  	secret[0] &= 248;
468c57c7 Jason A. Donenfeld 2018-08-24  1667  	secret[31] &= 127;
468c57c7 Jason A. Donenfeld 2018-08-24  1668  	secret[31] |= 64;
468c57c7 Jason A. Donenfeld 2018-08-24  1669  }
468c57c7 Jason A. Donenfeld 2018-08-24  1670  
468c57c7 Jason A. Donenfeld 2018-08-24  1671  static void curve25519_adx(u8 shared[CURVE25519_POINT_SIZE], const u8 private_key[CURVE25519_POINT_SIZE], const u8 session_key[CURVE25519_POINT_SIZE])
468c57c7 Jason A. Donenfeld 2018-08-24  1672  {
468c57c7 Jason A. Donenfeld 2018-08-24  1673  	struct {
468c57c7 Jason A. Donenfeld 2018-08-24  1674  		u64 buffer[4 * NUM_WORDS_ELTFP25519];
468c57c7 Jason A. Donenfeld 2018-08-24  1675  		u64 coordinates[4 * NUM_WORDS_ELTFP25519];
468c57c7 Jason A. Donenfeld 2018-08-24  1676  		u64 workspace[6 * NUM_WORDS_ELTFP25519];
468c57c7 Jason A. Donenfeld 2018-08-24  1677  		u8 session[CURVE25519_POINT_SIZE];
468c57c7 Jason A. Donenfeld 2018-08-24  1678  		u8 private[CURVE25519_POINT_SIZE];
468c57c7 Jason A. Donenfeld 2018-08-24  1679  	} __aligned(32) m;
468c57c7 Jason A. Donenfeld 2018-08-24  1680  
468c57c7 Jason A. Donenfeld 2018-08-24  1681  	int i = 0, j = 0;
468c57c7 Jason A. Donenfeld 2018-08-24  1682  	u64 prev = 0;
468c57c7 Jason A. Donenfeld 2018-08-24  1683  	u64 *const X1 = (u64 *)m.session;
468c57c7 Jason A. Donenfeld 2018-08-24  1684  	u64 *const key = (u64 *)m.private;
468c57c7 Jason A. Donenfeld 2018-08-24  1685  	u64 *const Px = m.coordinates + 0;
468c57c7 Jason A. Donenfeld 2018-08-24  1686  	u64 *const Pz = m.coordinates + 4;
468c57c7 Jason A. Donenfeld 2018-08-24  1687  	u64 *const Qx = m.coordinates + 8;
468c57c7 Jason A. Donenfeld 2018-08-24  1688  	u64 *const Qz = m.coordinates + 12;
468c57c7 Jason A. Donenfeld 2018-08-24  1689  	u64 *const X2 = Qx;
468c57c7 Jason A. Donenfeld 2018-08-24  1690  	u64 *const Z2 = Qz;
468c57c7 Jason A. Donenfeld 2018-08-24  1691  	u64 *const X3 = Px;
468c57c7 Jason A. Donenfeld 2018-08-24  1692  	u64 *const Z3 = Pz;
468c57c7 Jason A. Donenfeld 2018-08-24  1693  	u64 *const X2Z2 = Qx;
468c57c7 Jason A. Donenfeld 2018-08-24  1694  	u64 *const X3Z3 = Px;
468c57c7 Jason A. Donenfeld 2018-08-24  1695  
468c57c7 Jason A. Donenfeld 2018-08-24  1696  	u64 *const A = m.workspace + 0;
468c57c7 Jason A. Donenfeld 2018-08-24  1697  	u64 *const B = m.workspace + 4;
468c57c7 Jason A. Donenfeld 2018-08-24  1698  	u64 *const D = m.workspace + 8;
468c57c7 Jason A. Donenfeld 2018-08-24  1699  	u64 *const C = m.workspace + 12;
468c57c7 Jason A. Donenfeld 2018-08-24  1700  	u64 *const DA = m.workspace + 16;
468c57c7 Jason A. Donenfeld 2018-08-24  1701  	u64 *const CB = m.workspace + 20;
468c57c7 Jason A. Donenfeld 2018-08-24  1702  	u64 *const AB = A;
468c57c7 Jason A. Donenfeld 2018-08-24  1703  	u64 *const DC = D;
468c57c7 Jason A. Donenfeld 2018-08-24  1704  	u64 *const DACB = DA;
468c57c7 Jason A. Donenfeld 2018-08-24  1705  
468c57c7 Jason A. Donenfeld 2018-08-24 @1706  	memcpy(m.private, private_key, sizeof(m.private));
468c57c7 Jason A. Donenfeld 2018-08-24  1707  	memcpy(m.session, session_key, sizeof(m.session));
468c57c7 Jason A. Donenfeld 2018-08-24  1708  
468c57c7 Jason A. Donenfeld 2018-08-24  1709  	clamp_secret(m.private);
468c57c7 Jason A. Donenfeld 2018-08-24  1710  
468c57c7 Jason A. Donenfeld 2018-08-24  1711  	/* As in the draft:
468c57c7 Jason A. Donenfeld 2018-08-24  1712  	 * When receiving such an array, implementations of curve25519
468c57c7 Jason A. Donenfeld 2018-08-24  1713  	 * MUST mask the most-significant bit in the final byte. This
468c57c7 Jason A. Donenfeld 2018-08-24  1714  	 * is done to preserve compatibility with point formats which
468c57c7 Jason A. Donenfeld 2018-08-24  1715  	 * reserve the sign bit for use in other protocols and to
468c57c7 Jason A. Donenfeld 2018-08-24  1716  	 * increase resistance to implementation fingerprinting
468c57c7 Jason A. Donenfeld 2018-08-24  1717  	 */
468c57c7 Jason A. Donenfeld 2018-08-24  1718  	m.session[CURVE25519_POINT_SIZE - 1] &= (1 << (255 % 8)) - 1;
468c57c7 Jason A. Donenfeld 2018-08-24  1719  
468c57c7 Jason A. Donenfeld 2018-08-24  1720  	copy_eltfp25519_1w(Px, X1);
468c57c7 Jason A. Donenfeld 2018-08-24  1721  	setzero_eltfp25519_1w(Pz);
468c57c7 Jason A. Donenfeld 2018-08-24  1722  	setzero_eltfp25519_1w(Qx);
468c57c7 Jason A. Donenfeld 2018-08-24  1723  	setzero_eltfp25519_1w(Qz);
468c57c7 Jason A. Donenfeld 2018-08-24  1724  
468c57c7 Jason A. Donenfeld 2018-08-24  1725  	Pz[0] = 1;
468c57c7 Jason A. Donenfeld 2018-08-24  1726  	Qx[0] = 1;
468c57c7 Jason A. Donenfeld 2018-08-24  1727  
468c57c7 Jason A. Donenfeld 2018-08-24  1728  	/* main-loop */
468c57c7 Jason A. Donenfeld 2018-08-24  1729  	prev = 0;
468c57c7 Jason A. Donenfeld 2018-08-24  1730  	j = 62;
468c57c7 Jason A. Donenfeld 2018-08-24  1731  	for (i = 3; i >= 0; --i) {
468c57c7 Jason A. Donenfeld 2018-08-24  1732  		while (j >= 0) {
468c57c7 Jason A. Donenfeld 2018-08-24  1733  			u64 bit = (key[i] >> j) & 0x1;
468c57c7 Jason A. Donenfeld 2018-08-24  1734  			u64 swap = bit ^ prev;
468c57c7 Jason A. Donenfeld 2018-08-24  1735  			prev = bit;
468c57c7 Jason A. Donenfeld 2018-08-24  1736  
468c57c7 Jason A. Donenfeld 2018-08-24  1737  			add_eltfp25519_1w_adx(A, X2, Z2);	/* A = (X2+Z2) */
468c57c7 Jason A. Donenfeld 2018-08-24  1738  			sub_eltfp25519_1w(B, X2, Z2);		/* B = (X2-Z2) */
468c57c7 Jason A. Donenfeld 2018-08-24  1739  			add_eltfp25519_1w_adx(C, X3, Z3);	/* C = (X3+Z3) */
468c57c7 Jason A. Donenfeld 2018-08-24  1740  			sub_eltfp25519_1w(D, X3, Z3);		/* D = (X3-Z3) */
468c57c7 Jason A. Donenfeld 2018-08-24  1741  			mul_eltfp25519_2w_adx(DACB, AB, DC);	/* [DA|CB] = [A|B]*[D|C] */
468c57c7 Jason A. Donenfeld 2018-08-24  1742  
468c57c7 Jason A. Donenfeld 2018-08-24  1743  			cselect(swap, A, C);
468c57c7 Jason A. Donenfeld 2018-08-24  1744  			cselect(swap, B, D);
468c57c7 Jason A. Donenfeld 2018-08-24  1745  
468c57c7 Jason A. Donenfeld 2018-08-24  1746  			sqr_eltfp25519_2w_adx(AB);		/* [AA|BB] = [A^2|B^2] */
468c57c7 Jason A. Donenfeld 2018-08-24  1747  			add_eltfp25519_1w_adx(X3, DA, CB);	/* X3 = (DA+CB) */
468c57c7 Jason A. Donenfeld 2018-08-24  1748  			sub_eltfp25519_1w(Z3, DA, CB);		/* Z3 = (DA-CB) */
468c57c7 Jason A. Donenfeld 2018-08-24  1749  			sqr_eltfp25519_2w_adx(X3Z3);		/* [X3|Z3] = [(DA+CB)|(DA+CB)]^2 */
468c57c7 Jason A. Donenfeld 2018-08-24  1750  
468c57c7 Jason A. Donenfeld 2018-08-24  1751  			copy_eltfp25519_1w(X2, B);		/* X2 = B^2 */
468c57c7 Jason A. Donenfeld 2018-08-24  1752  			sub_eltfp25519_1w(Z2, A, B);		/* Z2 = E = AA-BB */
468c57c7 Jason A. Donenfeld 2018-08-24  1753  
468c57c7 Jason A. Donenfeld 2018-08-24  1754  			mul_a24_eltfp25519_1w(B, Z2);		/* B = a24*E */
468c57c7 Jason A. Donenfeld 2018-08-24  1755  			add_eltfp25519_1w_adx(B, B, X2);	/* B = a24*E+B */
468c57c7 Jason A. Donenfeld 2018-08-24  1756  			mul_eltfp25519_2w_adx(X2Z2, X2Z2, AB);	/* [X2|Z2] = [B|E]*[A|a24*E+B] */
468c57c7 Jason A. Donenfeld 2018-08-24  1757  			mul_eltfp25519_1w_adx(Z3, Z3, X1);	/* Z3 = Z3*X1 */
468c57c7 Jason A. Donenfeld 2018-08-24  1758  			--j;
468c57c7 Jason A. Donenfeld 2018-08-24  1759  		}
468c57c7 Jason A. Donenfeld 2018-08-24  1760  		j = 63;
468c57c7 Jason A. Donenfeld 2018-08-24  1761  	}
468c57c7 Jason A. Donenfeld 2018-08-24  1762  
468c57c7 Jason A. Donenfeld 2018-08-24  1763  	inv_eltfp25519_1w_adx(A, Qz);
468c57c7 Jason A. Donenfeld 2018-08-24  1764  	mul_eltfp25519_1w_adx((u64 *)shared, Qx, A);
468c57c7 Jason A. Donenfeld 2018-08-24  1765  	fred_eltfp25519_1w((u64 *)shared);
468c57c7 Jason A. Donenfeld 2018-08-24  1766  
468c57c7 Jason A. Donenfeld 2018-08-24  1767  	memzero_explicit(&m, sizeof(m));
468c57c7 Jason A. Donenfeld 2018-08-24  1768  }
468c57c7 Jason A. Donenfeld 2018-08-24  1769  

:::::: The code at line 1543 was first introduced by commit
:::::: 468c57c74ac7091c9c04ab2acccf68fe300cd9bc zinc: Curve25519 x86_64 implementation

:::::: TO: Jason A. Donenfeld <[email protected]>
:::::: CC: 0day robot <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation