Skip to contentSkip to conversation

WireGuard: Secure Network Tunnel

PATCH Superseded Jason A. Donenfeld 16 participants 146 messages Sep 25, 2018 – Nov 13, 2018 Page 2 of 2

Subsystems (30)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
author maintainer
#75 lore raw

PATCH v6 8/23 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 Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Ralf Baechle <[email protected]>
Cc Paul Burton <[email protected]>
Cc James Hogan <[email protected]>
---
 lib/zinc/Makefile                      |   2 +
 lib/zinc/chacha20/chacha20-mips-glue.h |  27 ++
 lib/zinc/chacha20/chacha20-mips.S      | 424 +++++++++++++++++++++++++
 lib/zinc/chacha20/chacha20.c           |   2 +
 4 files changed, 455 insertions(+)
 create mode 100644 lib/zinc/chacha20/chacha20-mips-glue.h
 create mode 100644 lib/zinc/chacha20/chacha20-mips.S
lib/zinc/Makefile +2 -0
index e47f64e12bbd..60d568cf5206 100644
@@ -6,4 +6,6 @@ zinc_chacha20-y := chacha20/chacha20.o
 zinc_chacha20-$(CONFIG_ZINC_ARCH_X86_64) += chacha20/chacha20-x86_64.o
 zinc_chacha20-$(CONFIG_ZINC_ARCH_ARM) += chacha20/chacha20-arm.o
 zinc_chacha20-$(CONFIG_ZINC_ARCH_ARM64) += chacha20/chacha20-arm64.o
+zinc_chacha20-$(CONFIG_ZINC_ARCH_MIPS) += chacha20/chacha20-mips.o
+AFLAGS_chacha20-mips.o += -O2 # This is required to fill the branch delay slots
 obj-$(CONFIG_ZINC_CHACHA20) += zinc_chacha20.o
lib/zinc/chacha20/chacha20-mips-glue.h new file +27 -0
new file mode 100644
index 000000000000..6e70dd6817d2
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+asmlinkage void chacha20_mips(u32 state[16], u8 *out, const u8 *in,
+			      const size_t len);
+static void __init chacha20_fpu_init(void)
+{
+}
+
+static inline bool chacha20_arch(struct chacha20_ctx *state, u8 *dst,
+				 const u8 *src, const size_t len,
+				 simd_context_t *simd_context)
+{
+	chacha20_mips((u32 *)state, dst, src, len);
+	return true;
+}
+
+
+static inline bool hchacha20_arch(u32 derived_key[CHACHA20_KEY_WORDS],
+				  const u8 nonce[HCHACHA20_NONCE_SIZE],
+				  const u8 key[HCHACHA20_KEY_SIZE],
+				  simd_context_t *simd_context)
+{
+	return false;
+}
lib/zinc/chacha20/chacha20-mips.S new file +424 -0

424 lines not on this page · raw message

lib/zinc/chacha20/chacha20.c +2 -0
index fc4f74fca653..34cb35528c3d 100644
@@ -18,6 +18,8 @@
 #include "chacha20-x86_64-glue.h"
 #elif defined(CONFIG_ZINC_ARCH_ARM) || defined(CONFIG_ZINC_ARCH_ARM64)
 #include "chacha20-arm-glue.h"
+#elif defined(CONFIG_ZINC_ARCH_MIPS)
+#include "chacha20-mips-glue.h"
 #else
 void __init chacha20_fpu_init(void)
 {
Signature · 1 line
2.19.0
author maintainer
#76 lore raw

PATCH v6 9/23 zinc: Poly1305 generic C implementations and selftest

In reply to Jason A. Donenfeld

These two C implementations -- a 32x32 one and a 64x64 one, depending on the platform -- come from Andrew Moon's public domain poly1305-donna portable code, modified for usage in the kernel and for usage with accelerated primitives.

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

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 include/zinc/poly1305.h              |   31 +
 lib/zinc/Kconfig                     |    3 +
 lib/zinc/Makefile                    |    3 +
 lib/zinc/poly1305/poly1305-donna32.h |  205 +++++
 lib/zinc/poly1305/poly1305-donna64.h |  182 +++++
 lib/zinc/poly1305/poly1305.c         |  155 ++++
 lib/zinc/selftest/poly1305.h         | 1112 ++++++++++++++++++++++++++
 7 files changed, 1691 insertions(+)
 create mode 100644 include/zinc/poly1305.h
 create mode 100644 lib/zinc/poly1305/poly1305-donna32.h
 create mode 100644 lib/zinc/poly1305/poly1305-donna64.h
 create mode 100644 lib/zinc/poly1305/poly1305.c
 create mode 100644 lib/zinc/selftest/poly1305.h
include/zinc/poly1305.h new file +31 -0
new file mode 100644
index 000000000000..13fe0e50fc3c
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * 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_init(struct poly1305_ctx *ctx, const u8 key[POLY1305_KEY_SIZE]);
+void poly1305_update(struct poly1305_ctx *ctx, const u8 *input, size_t len,
+		     simd_context_t *simd_context);
+void poly1305_final(struct poly1305_ctx *ctx, u8 mac[POLY1305_MAC_SIZE],
+		    simd_context_t *simd_context);
+
+#endif /* _ZINC_POLY1305_H */
lib/zinc/Kconfig +3 -0
index 1ca1ae1e9ea9..f9ef8f7e3c25 100644
@@ -2,6 +2,9 @@ config ZINC_CHACHA20
 	tristate
 	select CRYPTO_ALGAPI
 
+config ZINC_POLY1305
+	tristate
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	help
lib/zinc/Makefile +3 -0
index 60d568cf5206..6fc9626c55fa 100644
@@ -9,3 +9,6 @@ zinc_chacha20-$(CONFIG_ZINC_ARCH_ARM64) += chacha20/chacha20-arm64.o
 zinc_chacha20-$(CONFIG_ZINC_ARCH_MIPS) += chacha20/chacha20-mips.o
 AFLAGS_chacha20-mips.o += -O2 # This is required to fill the branch delay slots
 obj-$(CONFIG_ZINC_CHACHA20) += zinc_chacha20.o
+
+zinc_poly1305-y := poly1305/poly1305.o
+obj-$(CONFIG_ZINC_POLY1305) += zinc_poly1305.o
lib/zinc/poly1305/poly1305-donna32.h new file +205 -0

205 lines not on this page · raw message

lib/zinc/poly1305/poly1305-donna64.h new file +182 -0

182 lines not on this page · raw message

lib/zinc/poly1305/poly1305.c new file +155 -0

155 lines not on this page · raw message

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

1,112 lines not on this page · raw message

Signature · 1 line
2.19.0
author maintainer
#77 lore raw

PATCH v6 10/23 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 the following modifications from Samuel Neves:

- Some cosmetic changes, like renaming labels to .Lname, constants, and other Linux conventions.

- CPU feature checking is done in C by the glue code, so that has been removed from the assembly.

- poly1305_blocks_avx512 jumped to the middle of the poly1305_blocks_avx2 for the final blocks. To appease objtool, the relevant tail avx2 code was duplicated for the avx512 function.

- The original uses %rbp as a scratch register. However, the kernel expects %rbp to be a valid frame pointer at any given time in order to do proper unwinding. Thus we need to alter the code in order to preserve it. The most straightforward manner in which this was accomplished was by replacing $d3, formerly %r10, by %rdi, and replacing %rbp by %r10. Because %rdi, a pointer to the context structure, does not change and is not used by poly1305_iteration, it is safe to use it here, and the overhead of saving and restoring it should be minimal.

- The original hardcodes returns as .byte 0xf3,0xc3, aka "rep ret". We replace this by "ret". "rep ret" was meant to help with AMD K8 chips, cf. http://repzret.org/p/repzret. It makes no sense to continue to use this kludge for code that won't even run on ancient AMD chips.

While this is CRYPTOGAMS code, the originating code for this happens to be the same as OpenSSL's commit 4dfe4310c31c4483705991d9a798ce9be1ed1c68

Cycle counts on a Core i7 6700HQ using the AVX-2 codepath:

size old new
---- ---- ----
0 70 68
16 92 90
32 134 104
48 172 120
64 218 136
80 254 158
96 298 174
112 342 192
128 388 212
144 428 228
160 466 246
176 510 264
192 550 282
208 594 302
224 628 316
240 676 334
256 716 354
272 764 374
288 802 352
304 420 366
320 428 360
336 484 378
352 426 384
368 478 400
384 488 394
400 542 408
416 486 416
432 534 430
448 544 422
464 600 438
480 540 448
496 594 464
512 602 456
528 656 476
544 600 480
560 650 494
576 664 490
592 714 508
608 656 514
624 708 532
640 716 524
656 770 536
672 716 548
688 770 562
704 774 552
720 826 568
736 768 574
752 822 592
768 830 584
784 884 602
800 828 610
816 884 628
832 888 618
848 942 632
864 884 644
880 936 660
896 948 652
912 1000 664
928 942 676
944 994 690
960 1002 680
976 1054 694
992 1002 706
1008 1052 720

Cycle counts on a Xeon Gold 5120 using the AVX-512 codepath:

size old new
---- ---- ----
0 74 70
16 96 92
32 136 106
48 184 124
64 218 138
80 260 160
96 300 176
112 342 194
128 384 212
144 420 226
160 464 248
176 504 264
192 544 282
208 582 300
224 624 318
240 662 338
256 708 358
272 748 372
288 788 358
304 422 370
320 432 364
336 486 380
352 434 390
368 480 408
384 490 398
400 542 412
416 492 426
432 538 436
448 546 432
464 600 448
480 548 456
496 594 476
512 606 470
528 656 480
544 606 498
560 652 512
576 662 508
592 716 522
608 664 538
624 710 552
640 720 516
656 772 526
672 722 544
688 768 556
704 778 556
720 832 568
736 780 584
752 826 600
768 836 560
784 888 572
800 838 588
816 884 604
832 894 598
848 946 612
864 896 628
880 942 644
896 952 608
912 1004 616
928 954 634
944 1000 646
960 1008 646
976 1062 658
992 1012 674
1008 1058 690

Based-on-code-from: Andy Polyakov <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Greg KH <[email protected]>
Cc: Jean-Philippe Aumasson <[email protected]>
Cc: Andy Polyakov <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: [email protected]

---
 lib/zinc/Makefile                        |    1 +
 lib/zinc/poly1305/poly1305-x86_64-glue.h |  157 ++
 lib/zinc/poly1305/poly1305-x86_64.S      | 2792 ++++++++++++++++++++++
 lib/zinc/poly1305/poly1305.c             |    4 +-
 4 files changed, 2953 insertions(+), 1 deletion(-)
 create mode 100644 lib/zinc/poly1305/poly1305-x86_64-glue.h
 create mode 100644 lib/zinc/poly1305/poly1305-x86_64.S
lib/zinc/Makefile +1 -0
index 6fc9626c55fa..a8943d960b6a 100644
@@ -11,4 +11,5 @@ AFLAGS_chacha20-mips.o += -O2 # This is required to fill the branch delay slots
 obj-$(CONFIG_ZINC_CHACHA20) += zinc_chacha20.o
 
 zinc_poly1305-y := poly1305/poly1305.o
+zinc_poly1305-$(CONFIG_ZINC_ARCH_X86_64) += poly1305/poly1305-x86_64.o
 obj-$(CONFIG_ZINC_POLY1305) += zinc_poly1305.o
lib/zinc/poly1305/poly1305-x86_64-glue.h new file +157 -0

157 lines not on this page · raw message

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

2,792 lines not on this page · raw message

lib/zinc/poly1305/poly1305.c +3 -1
index 538abc359d1d..2ae1b3cb66cd 100644
@@ -15,7 +15,9 @@
 #include <linux/module.h>
 #include <linux/init.h>
 
-#ifndef HAVE_POLY1305_ARCH_IMPLEMENTATION
+#if defined(CONFIG_ZINC_ARCH_X86_64)
+#include "poly1305-x86_64-glue.h"
+#else
 static inline bool poly1305_init_arch(void *ctx,
 				      const u8 key[POLY1305_KEY_SIZE])
 {
Signature · 1 line
2.19.0
author maintainer
#78 lore raw

PATCH v6 11/23 zinc: import Andy Polyakov's Poly1305 ARM and ARM64 implementations

In reply to Jason A. Donenfeld

These NEON and non-NEON implementations come from Andy Polyakov's implementation, and are included here in raw form without modification, so that subsequent commits that fix these up for the kernel can see how it has changed. This awkward commit splitting has been requested for the ARM[64] implementations in particular.

While this is CRYPTOGAMS code, the originating code for this happens to be the same as OpenSSL's commit 5bb1cd2292b388263a0cc05392bb99141212aa53

Based-on-code-from: Andy Polyakov <[email protected]>
Cc: Samuel Neves <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Greg KH <[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

---
 lib/zinc/poly1305/poly1305-arm-cryptogams.S   | 1172 +++++++++++++++++
 lib/zinc/poly1305/poly1305-arm64-cryptogams.S |  869 ++++++++++++
 2 files changed, 2041 insertions(+)
 create mode 100644 lib/zinc/poly1305/poly1305-arm-cryptogams.S
 create mode 100644 lib/zinc/poly1305/poly1305-arm64-cryptogams.S
lib/zinc/poly1305/poly1305-arm-cryptogams.S new file +1172 -0
new file mode 100644
index 000000000000..884b465030e4
@@ -0,0 +1,1172 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
+/*
+ * Copyright (C) 2006-2017 CRYPTOGAMS by <[email protected]>. All Rights Reserved.
+ */
+
+#include "arm_arch.h"
+
+.text
+#if defined(__thumb2__)
+.syntax	unified
+.thumb
+#else
+.code	32
+#endif
+
+.globl	poly1305_emit
+.globl	poly1305_blocks
+.globl	poly1305_init
+.type	poly1305_init,%function
+.align	5
+poly1305_init:
+.Lpoly1305_init:
+	stmdb	sp!,{r4-r11}
+
+	eor	r3,r3,r3
+	cmp	r1,#0
+	str	r3,[r0,#0]		@ zero hash value
+	str	r3,[r0,#4]
+	str	r3,[r0,#8]
+	str	r3,[r0,#12]
+	str	r3,[r0,#16]
+	str	r3,[r0,#36]		@ is_base2_26
+	add	r0,r0,#20
+
+#ifdef	__thumb2__
+	it	eq
+#endif
+	moveq	r0,#0
+	beq	.Lno_key
+
+#if	__ARM_MAX_ARCH__>=7
+	adr	r11,.Lpoly1305_init
+	ldr	r12,.LOPENSSL_armcap
+#endif
+	ldrb	r4,[r1,#0]
+	mov	r10,#0x0fffffff
+	ldrb	r5,[r1,#1]
+	and	r3,r10,#-4		@ 0x0ffffffc
+	ldrb	r6,[r1,#2]
+	ldrb	r7,[r1,#3]
+	orr	r4,r4,r5,lsl#8
+	ldrb	r5,[r1,#4]
+	orr	r4,r4,r6,lsl#16
+	ldrb	r6,[r1,#5]
+	orr	r4,r4,r7,lsl#24
+	ldrb	r7,[r1,#6]
+	and	r4,r4,r10
+
+#if	__ARM_MAX_ARCH__>=7
+	ldr	r12,[r11,r12]		@ OPENSSL_armcap_P
+# ifdef	__APPLE__
+	ldr	r12,[r12]
+# endif
+#endif
+	ldrb	r8,[r1,#7]
+	orr	r5,r5,r6,lsl#8
+	ldrb	r6,[r1,#8]
+	orr	r5,r5,r7,lsl#16
+	ldrb	r7,[r1,#9]
+	orr	r5,r5,r8,lsl#24
+	ldrb	r8,[r1,#10]
+	and	r5,r5,r3
+
+#if	__ARM_MAX_ARCH__>=7
+	tst	r12,#ARMV7_NEON		@ check for NEON
+# ifdef	__APPLE__
+	adr	r9,poly1305_blocks_neon
+	adr	r11,poly1305_blocks
+#  ifdef __thumb2__
+	it	ne
+#  endif
+	movne	r11,r9
+	adr	r12,poly1305_emit
+	adr	r10,poly1305_emit_neon
+#  ifdef __thumb2__
+	it	ne
+#  endif
+	movne	r12,r10
+# else
+#  ifdef __thumb2__
+	itete	eq
+#  endif
+	addeq	r12,r11,#(poly1305_emit-.Lpoly1305_init)
+	addne	r12,r11,#(poly1305_emit_neon-.Lpoly1305_init)
+	addeq	r11,r11,#(poly1305_blocks-.Lpoly1305_init)
+	addne	r11,r11,#(poly1305_blocks_neon-.Lpoly1305_init)
+# endif
+# ifdef	__thumb2__
+	orr	r12,r12,#1	@ thumb-ify address
+	orr	r11,r11,#1
+# endif
+#endif
+	ldrb	r9,[r1,#11]
+	orr	r6,r6,r7,lsl#8
+	ldrb	r7,[r1,#12]
+	orr	r6,r6,r8,lsl#16
+	ldrb	r8,[r1,#13]
+	orr	r6,r6,r9,lsl#24
+	ldrb	r9,[r1,#14]
+	and	r6,r6,r3
+
+	ldrb	r10,[r1,#15]
+	orr	r7,r7,r8,lsl#8
+	str	r4,[r0,#0]
+	orr	r7,r7,r9,lsl#16
+	str	r5,[r0,#4]
+	orr	r7,r7,r10,lsl#24
+	str	r6,[r0,#8]
+	and	r7,r7,r3
+	str	r7,[r0,#12]
+#if	__ARM_MAX_ARCH__>=7
+	stmia	r2,{r11,r12}		@ fill functions table
+	mov	r0,#1
+#else
+	mov	r0,#0
+#endif
+.Lno_key:
+	ldmia	sp!,{r4-r11}
+#if	__ARM_ARCH__>=5
+	bx	lr				@ bx	lr
+#else
+	tst	lr,#1
+	moveq	pc,lr			@ be binary compatible with V4, yet
+	.word	0xe12fff1e			@ interoperable with Thumb ISA:-)
+#endif
+.size	poly1305_init,.-poly1305_init
+.type	poly1305_blocks,%function
+.align	5
+poly1305_blocks:
+.Lpoly1305_blocks:
+	stmdb	sp!,{r3-r11,lr}
+
+	ands	r2,r2,#-16
+	beq	.Lno_data
+
+	cmp	r3,#0
+	add	r2,r2,r1		@ end pointer
+	sub	sp,sp,#32
+
+	ldmia	r0,{r4-r12}		@ load context
+
+	str	r0,[sp,#12]		@ offload stuff
+	mov	lr,r1
+	str	r2,[sp,#16]
+	str	r10,[sp,#20]
+	str	r11,[sp,#24]
+	str	r12,[sp,#28]
+	b	.Loop
+
+.Loop:
+#if __ARM_ARCH__<7
+	ldrb	r0,[lr],#16		@ load input
+# ifdef	__thumb2__
+	it	hi
+# endif
+	addhi	r8,r8,#1		@ 1<<128
+	ldrb	r1,[lr,#-15]
+	ldrb	r2,[lr,#-14]
+	ldrb	r3,[lr,#-13]
+	orr	r1,r0,r1,lsl#8
+	ldrb	r0,[lr,#-12]
+	orr	r2,r1,r2,lsl#16
+	ldrb	r1,[lr,#-11]
+	orr	r3,r2,r3,lsl#24
+	ldrb	r2,[lr,#-10]
+	adds	r4,r4,r3		@ accumulate input
+
+	ldrb	r3,[lr,#-9]
+	orr	r1,r0,r1,lsl#8
+	ldrb	r0,[lr,#-8]
+	orr	r2,r1,r2,lsl#16
+	ldrb	r1,[lr,#-7]
+	orr	r3,r2,r3,lsl#24
+	ldrb	r2,[lr,#-6]
+	adcs	r5,r5,r3
+
+	ldrb	r3,[lr,#-5]
+	orr	r1,r0,r1,lsl#8
+	ldrb	r0,[lr,#-4]
+	orr	r2,r1,r2,lsl#16
+	ldrb	r1,[lr,#-3]
+	orr	r3,r2,r3,lsl#24
+	ldrb	r2,[lr,#-2]
+	adcs	r6,r6,r3
+
+	ldrb	r3,[lr,#-1]
+	orr	r1,r0,r1,lsl#8
+	str	lr,[sp,#8]		@ offload input pointer
+	orr	r2,r1,r2,lsl#16
+	add	r10,r10,r10,lsr#2
+	orr	r3,r2,r3,lsl#24
+#else
+	ldr	r0,[lr],#16		@ load input
+# ifdef	__thumb2__
+	it	hi
+# endif
+	addhi	r8,r8,#1		@ padbit
+	ldr	r1,[lr,#-12]
+	ldr	r2,[lr,#-8]
+	ldr	r3,[lr,#-4]
+# ifdef	__ARMEB__
+	rev	r0,r0
+	rev	r1,r1
+	rev	r2,r2
+	rev	r3,r3
+# endif
+	adds	r4,r4,r0		@ accumulate input
+	str	lr,[sp,#8]		@ offload input pointer
+	adcs	r5,r5,r1
+	add	r10,r10,r10,lsr#2
+	adcs	r6,r6,r2
+#endif
+	add	r11,r11,r11,lsr#2
+	adcs	r7,r7,r3
+	add	r12,r12,r12,lsr#2
+
+	umull	r2,r3,r5,r9
+	 adc	r8,r8,#0
+	umull	r0,r1,r4,r9
+	umlal	r2,r3,r8,r10
+	umlal	r0,r1,r7,r10
+	ldr	r10,[sp,#20]		@ reload r10
+	umlal	r2,r3,r6,r12
+	umlal	r0,r1,r5,r12
+	umlal	r2,r3,r7,r11
+	umlal	r0,r1,r6,r11
+	umlal	r2,r3,r4,r10
+	str	r0,[sp,#0]		@ future r4
+	 mul	r0,r11,r8
+	ldr	r11,[sp,#24]		@ reload r11
+	adds	r2,r2,r1		@ d1+=d0>>32
+	 eor	r1,r1,r1
+	adc	lr,r3,#0		@ future r6
+	str	r2,[sp,#4]		@ future r5
+
+	mul	r2,r12,r8
+	eor	r3,r3,r3
+	umlal	r0,r1,r7,r12
+	ldr	r12,[sp,#28]		@ reload r12
+	umlal	r2,r3,r7,r9
+	umlal	r0,r1,r6,r9
+	umlal	r2,r3,r6,r10
+	umlal	r0,r1,r5,r10
+	umlal	r2,r3,r5,r11
+	umlal	r0,r1,r4,r11
+	umlal	r2,r3,r4,r12
+	ldr	r4,[sp,#0]
+	mul	r8,r9,r8
+	ldr	r5,[sp,#4]
+
+	adds	r6,lr,r0		@ d2+=d1>>32
+	ldr	lr,[sp,#8]		@ reload input pointer
+	adc	r1,r1,#0
+	adds	r7,r2,r1		@ d3+=d2>>32
+	ldr	r0,[sp,#16]		@ reload end pointer
+	adc	r3,r3,#0
+	add	r8,r8,r3		@ h4+=d3>>32
+
+	and	r1,r8,#-4
+	and	r8,r8,#3
+	add	r1,r1,r1,lsr#2		@ *=5
+	adds	r4,r4,r1
+	adcs	r5,r5,#0
+	adcs	r6,r6,#0
+	adcs	r7,r7,#0
+	adc	r8,r8,#0
+
+	cmp	r0,lr			@ done yet?
+	bhi	.Loop
+
+	ldr	r0,[sp,#12]
+	add	sp,sp,#32
+	stmia	r0,{r4-r8}		@ store the result
+
+.Lno_data:
+#if	__ARM_ARCH__>=5
+	ldmia	sp!,{r3-r11,pc}
+#else
+	ldmia	sp!,{r3-r11,lr}
+	tst	lr,#1
+	moveq	pc,lr			@ be binary compatible with V4, yet
+	.word	0xe12fff1e			@ interoperable with Thumb ISA:-)
+#endif
+.size	poly1305_blocks,.-poly1305_blocks
+.type	poly1305_emit,%function
+.align	5
+poly1305_emit:
+	stmdb	sp!,{r4-r11}
+.Lpoly1305_emit_enter:
+
+	ldmia	r0,{r3-r7}
+	adds	r8,r3,#5		@ compare to modulus
+	adcs	r9,r4,#0
+	adcs	r10,r5,#0
+	adcs	r11,r6,#0
+	adc	r7,r7,#0
+	tst	r7,#4			@ did it carry/borrow?
+
+#ifdef	__thumb2__
+	it	ne
+#endif
+	movne	r3,r8
+	ldr	r8,[r2,#0]
+#ifdef	__thumb2__
+	it	ne
+#endif
+	movne	r4,r9
+	ldr	r9,[r2,#4]
+#ifdef	__thumb2__
+	it	ne
+#endif
+	movne	r5,r10
+	ldr	r10,[r2,#8]
+#ifdef	__thumb2__
+	it	ne
+#endif
+	movne	r6,r11
+	ldr	r11,[r2,#12]
+
+	adds	r3,r3,r8
+	adcs	r4,r4,r9
+	adcs	r5,r5,r10
+	adc	r6,r6,r11
+
+#if __ARM_ARCH__>=7
+# ifdef __ARMEB__
+	rev	r3,r3
+	rev	r4,r4
+	rev	r5,r5
+	rev	r6,r6
+# endif
+	str	r3,[r1,#0]
+	str	r4,[r1,#4]
+	str	r5,[r1,#8]
+	str	r6,[r1,#12]
+#else
+	strb	r3,[r1,#0]
+	mov	r3,r3,lsr#8
+	strb	r4,[r1,#4]
+	mov	r4,r4,lsr#8
+	strb	r5,[r1,#8]
+	mov	r5,r5,lsr#8
+	strb	r6,[r1,#12]
+	mov	r6,r6,lsr#8
+
+	strb	r3,[r1,#1]
+	mov	r3,r3,lsr#8
+	strb	r4,[r1,#5]
+	mov	r4,r4,lsr#8
+	strb	r5,[r1,#9]
+	mov	r5,r5,lsr#8
+	strb	r6,[r1,#13]
+	mov	r6,r6,lsr#8
+
+	strb	r3,[r1,#2]
+	mov	r3,r3,lsr#8
+	strb	r4,[r1,#6]
+	mov	r4,r4,lsr#8
+	strb	r5,[r1,#10]
+	mov	r5,r5,lsr#8
+	strb	r6,[r1,#14]
+	mov	r6,r6,lsr#8
+
+	strb	r3,[r1,#3]
+	strb	r4,[r1,#7]
+	strb	r5,[r1,#11]
+	strb	r6,[r1,#15]
+#endif
+	ldmia	sp!,{r4-r11}
+#if	__ARM_ARCH__>=5
+	bx	lr				@ bx	lr
+#else
+	tst	lr,#1
+	moveq	pc,lr			@ be binary compatible with V4, yet
+	.word	0xe12fff1e			@ interoperable with Thumb ISA:-)
+#endif
+.size	poly1305_emit,.-poly1305_emit
+#if	__ARM_MAX_ARCH__>=7
+.fpu	neon
+
+.type	poly1305_init_neon,%function
+.align	5
+poly1305_init_neon:
+	ldr	r4,[r0,#20]		@ load key base 2^32
+	ldr	r5,[r0,#24]
+	ldr	r6,[r0,#28]
+	ldr	r7,[r0,#32]
+
+	and	r2,r4,#0x03ffffff	@ base 2^32 -> base 2^26
+	mov	r3,r4,lsr#26
+	mov	r4,r5,lsr#20
+	orr	r3,r3,r5,lsl#6
+	mov	r5,r6,lsr#14
+	orr	r4,r4,r6,lsl#12
+	mov	r6,r7,lsr#8
+	orr	r5,r5,r7,lsl#18
+	and	r3,r3,#0x03ffffff
+	and	r4,r4,#0x03ffffff
+	and	r5,r5,#0x03ffffff
+
+	vdup.32	d0,r2			@ r^1 in both lanes
+	add	r2,r3,r3,lsl#2		@ *5
+	vdup.32	d1,r3
+	add	r3,r4,r4,lsl#2
+	vdup.32	d2,r2
+	vdup.32	d3,r4
+	add	r4,r5,r5,lsl#2
+	vdup.32	d4,r3
+	vdup.32	d5,r5
+	add	r5,r6,r6,lsl#2
+	vdup.32	d6,r4
+	vdup.32	d7,r6
+	vdup.32	d8,r5
+
+	mov	r5,#2		@ counter
+
+.Lsquare_neon:
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
+	@ d1 = h1*r0 + h0*r1   + h4*5*r2 + h3*5*r3 + h2*5*r4
+	@ d2 = h2*r0 + h1*r1   + h0*r2   + h4*5*r3 + h3*5*r4
+	@ d3 = h3*r0 + h2*r1   + h1*r2   + h0*r3   + h4*5*r4
+	@ d4 = h4*r0 + h3*r1   + h2*r2   + h1*r3   + h0*r4
+
+	vmull.u32	q5,d0,d0[1]
+	vmull.u32	q6,d1,d0[1]
+	vmull.u32	q7,d3,d0[1]
+	vmull.u32	q8,d5,d0[1]
+	vmull.u32	q9,d7,d0[1]
+
+	vmlal.u32	q5,d7,d2[1]
+	vmlal.u32	q6,d0,d1[1]
+	vmlal.u32	q7,d1,d1[1]
+	vmlal.u32	q8,d3,d1[1]
+	vmlal.u32	q9,d5,d1[1]
+
+	vmlal.u32	q5,d5,d4[1]
+	vmlal.u32	q6,d7,d4[1]
+	vmlal.u32	q8,d1,d3[1]
+	vmlal.u32	q7,d0,d3[1]
+	vmlal.u32	q9,d3,d3[1]
+
+	vmlal.u32	q5,d3,d6[1]
+	vmlal.u32	q8,d0,d5[1]
+	vmlal.u32	q6,d5,d6[1]
+	vmlal.u32	q7,d7,d6[1]
+	vmlal.u32	q9,d1,d5[1]
+
+	vmlal.u32	q8,d7,d8[1]
+	vmlal.u32	q5,d1,d8[1]
+	vmlal.u32	q6,d3,d8[1]
+	vmlal.u32	q7,d5,d8[1]
+	vmlal.u32	q9,d0,d7[1]
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ lazy reduction as discussed in "NEON crypto" by D.J. Bernstein
+	@ and P. Schwabe
+	@
+	@ H0>>+H1>>+H2>>+H3>>+H4
+	@ H3>>+H4>>*5+H0>>+H1
+	@
+	@ Trivia.
+	@
+	@ Result of multiplication of n-bit number by m-bit number is
+	@ n+m bits wide. However! Even though 2^n is a n+1-bit number,
+	@ m-bit number multiplied by 2^n is still n+m bits wide.
+	@
+	@ Sum of two n-bit numbers is n+1 bits wide, sum of three - n+2,
+	@ and so is sum of four. Sum of 2^m n-m-bit numbers and n-bit
+	@ one is n+1 bits wide.
+	@
+	@ >>+ denotes Hnext += Hn>>26, Hn &= 0x3ffffff. This means that
+	@ H0, H2, H3 are guaranteed to be 26 bits wide, while H1 and H4
+	@ can be 27. However! In cases when their width exceeds 26 bits
+	@ they are limited by 2^26+2^6. This in turn means that *sum*
+	@ of the products with these values can still be viewed as sum
+	@ of 52-bit numbers as long as the amount of addends is not a
+	@ power of 2. For example,
+	@
+	@ H4 = H4*R0 + H3*R1 + H2*R2 + H1*R3 + H0 * R4,
+	@
+	@ which can't be larger than 5 * (2^26 + 2^6) * (2^26 + 2^6), or
+	@ 5 * (2^52 + 2*2^32 + 2^12), which in turn is smaller than
+	@ 8 * (2^52) or 2^55. However, the value is then multiplied by
+	@ by 5, so we should be looking at 5 * 5 * (2^52 + 2^33 + 2^12),
+	@ which is less than 32 * (2^52) or 2^57. And when processing
+	@ data we are looking at triple as many addends...
+	@
+	@ In key setup procedure pre-reduced H0 is limited by 5*4+1 and
+	@ 5*H4 - by 5*5 52-bit addends, or 57 bits. But when hashing the
+	@ input H0 is limited by (5*4+1)*3 addends, or 58 bits, while
+	@ 5*H4 by 5*5*3, or 59[!] bits. How is this relevant? vmlal.u32
+	@ instruction accepts 2x32-bit input and writes 2x64-bit result.
+	@ This means that result of reduction have to be compressed upon
+	@ loop wrap-around. This can be done in the process of reduction
+	@ to minimize amount of instructions [as well as amount of
+	@ 128-bit instructions, which benefits low-end processors], but
+	@ one has to watch for H2 (which is narrower than H0) and 5*H4
+	@ not being wider than 58 bits, so that result of right shift
+	@ by 26 bits fits in 32 bits. This is also useful on x86,
+	@ because it allows to use paddd in place for paddq, which
+	@ benefits Atom, where paddq is ridiculously slow.
+
+	vshr.u64	q15,q8,#26
+	vmovn.i64	d16,q8
+	 vshr.u64	q4,q5,#26
+	 vmovn.i64	d10,q5
+	vadd.i64	q9,q9,q15		@ h3 -> h4
+	vbic.i32	d16,#0xfc000000	@ &=0x03ffffff
+	 vadd.i64	q6,q6,q4		@ h0 -> h1
+	 vbic.i32	d10,#0xfc000000
+
+	vshrn.u64	d30,q9,#26
+	vmovn.i64	d18,q9
+	 vshr.u64	q4,q6,#26
+	 vmovn.i64	d12,q6
+	 vadd.i64	q7,q7,q4		@ h1 -> h2
+	vbic.i32	d18,#0xfc000000
+	 vbic.i32	d12,#0xfc000000
+
+	vadd.i32	d10,d10,d30
+	vshl.u32	d30,d30,#2
+	 vshrn.u64	d8,q7,#26
+	 vmovn.i64	d14,q7
+	vadd.i32	d10,d10,d30	@ h4 -> h0
+	 vadd.i32	d16,d16,d8	@ h2 -> h3
+	 vbic.i32	d14,#0xfc000000
+
+	vshr.u32	d30,d10,#26
+	vbic.i32	d10,#0xfc000000
+	 vshr.u32	d8,d16,#26
+	 vbic.i32	d16,#0xfc000000
+	vadd.i32	d12,d12,d30	@ h0 -> h1
+	 vadd.i32	d18,d18,d8	@ h3 -> h4
+
+	subs		r5,r5,#1
+	beq		.Lsquare_break_neon
+
+	add		r6,r0,#(48+0*9*4)
+	add		r7,r0,#(48+1*9*4)
+
+	vtrn.32		d0,d10		@ r^2:r^1
+	vtrn.32		d3,d14
+	vtrn.32		d5,d16
+	vtrn.32		d1,d12
+	vtrn.32		d7,d18
+
+	vshl.u32	d4,d3,#2		@ *5
+	vshl.u32	d6,d5,#2
+	vshl.u32	d2,d1,#2
+	vshl.u32	d8,d7,#2
+	vadd.i32	d4,d4,d3
+	vadd.i32	d2,d2,d1
+	vadd.i32	d6,d6,d5
+	vadd.i32	d8,d8,d7
+
+	vst4.32		{d0[0],d1[0],d2[0],d3[0]},[r6]!
+	vst4.32		{d0[1],d1[1],d2[1],d3[1]},[r7]!
+	vst4.32		{d4[0],d5[0],d6[0],d7[0]},[r6]!
+	vst4.32		{d4[1],d5[1],d6[1],d7[1]},[r7]!
+	vst1.32		{d8[0]},[r6,:32]
+	vst1.32		{d8[1]},[r7,:32]
+
+	b		.Lsquare_neon
+
+.align	4
+.Lsquare_break_neon:
+	add		r6,r0,#(48+2*4*9)
+	add		r7,r0,#(48+3*4*9)
+
+	vmov		d0,d10		@ r^4:r^3
+	vshl.u32	d2,d12,#2		@ *5
+	vmov		d1,d12
+	vshl.u32	d4,d14,#2
+	vmov		d3,d14
+	vshl.u32	d6,d16,#2
+	vmov		d5,d16
+	vshl.u32	d8,d18,#2
+	vmov		d7,d18
+	vadd.i32	d2,d2,d12
+	vadd.i32	d4,d4,d14
+	vadd.i32	d6,d6,d16
+	vadd.i32	d8,d8,d18
+
+	vst4.32		{d0[0],d1[0],d2[0],d3[0]},[r6]!
+	vst4.32		{d0[1],d1[1],d2[1],d3[1]},[r7]!
+	vst4.32		{d4[0],d5[0],d6[0],d7[0]},[r6]!
+	vst4.32		{d4[1],d5[1],d6[1],d7[1]},[r7]!
+	vst1.32		{d8[0]},[r6]
+	vst1.32		{d8[1]},[r7]
+
+	bx	lr				@ bx	lr
+.size	poly1305_init_neon,.-poly1305_init_neon
+
+.type	poly1305_blocks_neon,%function
+.align	5
+poly1305_blocks_neon:
+	ldr	ip,[r0,#36]		@ is_base2_26
+	ands	r2,r2,#-16
+	beq	.Lno_data_neon
+
+	cmp	r2,#64
+	bhs	.Lenter_neon
+	tst	ip,ip			@ is_base2_26?
+	beq	.Lpoly1305_blocks
+
+.Lenter_neon:
+	stmdb	sp!,{r4-r7}
+	vstmdb	sp!,{d8-d15}		@ ABI specification says so
+
+	tst	ip,ip			@ is_base2_26?
+	bne	.Lbase2_26_neon
+
+	stmdb	sp!,{r1-r3,lr}
+	bl	poly1305_init_neon
+
+	ldr	r4,[r0,#0]		@ load hash value base 2^32
+	ldr	r5,[r0,#4]
+	ldr	r6,[r0,#8]
+	ldr	r7,[r0,#12]
+	ldr	ip,[r0,#16]
+
+	and	r2,r4,#0x03ffffff	@ base 2^32 -> base 2^26
+	mov	r3,r4,lsr#26
+	 veor	d10,d10,d10
+	mov	r4,r5,lsr#20
+	orr	r3,r3,r5,lsl#6
+	 veor	d12,d12,d12
+	mov	r5,r6,lsr#14
+	orr	r4,r4,r6,lsl#12
+	 veor	d14,d14,d14
+	mov	r6,r7,lsr#8
+	orr	r5,r5,r7,lsl#18
+	 veor	d16,d16,d16
+	and	r3,r3,#0x03ffffff
+	orr	r6,r6,ip,lsl#24
+	 veor	d18,d18,d18
+	and	r4,r4,#0x03ffffff
+	mov	r1,#1
+	and	r5,r5,#0x03ffffff
+	str	r1,[r0,#36]		@ is_base2_26
+
+	vmov.32	d10[0],r2
+	vmov.32	d12[0],r3
+	vmov.32	d14[0],r4
+	vmov.32	d16[0],r5
+	vmov.32	d18[0],r6
+	adr	r5,.Lzeros
+
+	ldmia	sp!,{r1-r3,lr}
+	b	.Lbase2_32_neon
+
+.align	4
+.Lbase2_26_neon:
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ load hash value
+
+	veor		d10,d10,d10
+	veor		d12,d12,d12
+	veor		d14,d14,d14
+	veor		d16,d16,d16
+	veor		d18,d18,d18
+	vld4.32		{d10[0],d12[0],d14[0],d16[0]},[r0]!
+	adr		r5,.Lzeros
+	vld1.32		{d18[0]},[r0]
+	sub		r0,r0,#16		@ rewind
+
+.Lbase2_32_neon:
+	add		r4,r1,#32
+	mov		r3,r3,lsl#24
+	tst		r2,#31
+	beq		.Leven
+
+	vld4.32		{d20[0],d22[0],d24[0],d26[0]},[r1]!
+	vmov.32		d28[0],r3
+	sub		r2,r2,#16
+	add		r4,r1,#32
+
+# ifdef	__ARMEB__
+	vrev32.8	q10,q10
+	vrev32.8	q13,q13
+	vrev32.8	q11,q11
+	vrev32.8	q12,q12
+# endif
+	vsri.u32	d28,d26,#8	@ base 2^32 -> base 2^26
+	vshl.u32	d26,d26,#18
+
+	vsri.u32	d26,d24,#14
+	vshl.u32	d24,d24,#12
+	vadd.i32	d29,d28,d18	@ add hash value and move to #hi
+
+	vbic.i32	d26,#0xfc000000
+	vsri.u32	d24,d22,#20
+	vshl.u32	d22,d22,#6
+
+	vbic.i32	d24,#0xfc000000
+	vsri.u32	d22,d20,#26
+	vadd.i32	d27,d26,d16
+
+	vbic.i32	d20,#0xfc000000
+	vbic.i32	d22,#0xfc000000
+	vadd.i32	d25,d24,d14
+
+	vadd.i32	d21,d20,d10
+	vadd.i32	d23,d22,d12
+
+	mov		r7,r5
+	add		r6,r0,#48
+
+	cmp		r2,r2
+	b		.Long_tail
+
+.align	4
+.Leven:
+	subs		r2,r2,#64
+	it		lo
+	movlo		r4,r5
+
+	vmov.i32	q14,#1<<24		@ padbit, yes, always
+	vld4.32		{d20,d22,d24,d26},[r1]	@ inp[0:1]
+	add		r1,r1,#64
+	vld4.32		{d21,d23,d25,d27},[r4]	@ inp[2:3] (or 0)
+	add		r4,r4,#64
+	itt		hi
+	addhi		r7,r0,#(48+1*9*4)
+	addhi		r6,r0,#(48+3*9*4)
+
+# ifdef	__ARMEB__
+	vrev32.8	q10,q10
+	vrev32.8	q13,q13
+	vrev32.8	q11,q11
+	vrev32.8	q12,q12
+# endif
+	vsri.u32	q14,q13,#8		@ base 2^32 -> base 2^26
+	vshl.u32	q13,q13,#18
+
+	vsri.u32	q13,q12,#14
+	vshl.u32	q12,q12,#12
+
+	vbic.i32	q13,#0xfc000000
+	vsri.u32	q12,q11,#20
+	vshl.u32	q11,q11,#6
+
+	vbic.i32	q12,#0xfc000000
+	vsri.u32	q11,q10,#26
+
+	vbic.i32	q10,#0xfc000000
+	vbic.i32	q11,#0xfc000000
+
+	bls		.Lskip_loop
+
+	vld4.32		{d0[1],d1[1],d2[1],d3[1]},[r7]!	@ load r^2
+	vld4.32		{d0[0],d1[0],d2[0],d3[0]},[r6]!	@ load r^4
+	vld4.32		{d4[1],d5[1],d6[1],d7[1]},[r7]!
+	vld4.32		{d4[0],d5[0],d6[0],d7[0]},[r6]!
+	b		.Loop_neon
+
+.align	5
+.Loop_neon:
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2
+	@ ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^3+inp[7]*r
+	@   ___________________/
+	@ ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2+inp[8])*r^2
+	@ ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^4+inp[7]*r^2+inp[9])*r
+	@   ___________________/ ____________________/
+	@
+	@ Note that we start with inp[2:3]*r^2. This is because it
+	@ doesn't depend on reduction in previous iteration.
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ d4 = h4*r0 + h3*r1   + h2*r2   + h1*r3   + h0*r4
+	@ d3 = h3*r0 + h2*r1   + h1*r2   + h0*r3   + h4*5*r4
+	@ d2 = h2*r0 + h1*r1   + h0*r2   + h4*5*r3 + h3*5*r4
+	@ d1 = h1*r0 + h0*r1   + h4*5*r2 + h3*5*r3 + h2*5*r4
+	@ d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ inp[2:3]*r^2
+
+	vadd.i32	d24,d24,d14	@ accumulate inp[0:1]
+	vmull.u32	q7,d25,d0[1]
+	vadd.i32	d20,d20,d10
+	vmull.u32	q5,d21,d0[1]
+	vadd.i32	d26,d26,d16
+	vmull.u32	q8,d27,d0[1]
+	vmlal.u32	q7,d23,d1[1]
+	vadd.i32	d22,d22,d12
+	vmull.u32	q6,d23,d0[1]
+
+	vadd.i32	d28,d28,d18
+	vmull.u32	q9,d29,d0[1]
+	subs		r2,r2,#64
+	vmlal.u32	q5,d29,d2[1]
+	it		lo
+	movlo		r4,r5
+	vmlal.u32	q8,d25,d1[1]
+	vld1.32		d8[1],[r7,:32]
+	vmlal.u32	q6,d21,d1[1]
+	vmlal.u32	q9,d27,d1[1]
+
+	vmlal.u32	q5,d27,d4[1]
+	vmlal.u32	q8,d23,d3[1]
+	vmlal.u32	q9,d25,d3[1]
+	vmlal.u32	q6,d29,d4[1]
+	vmlal.u32	q7,d21,d3[1]
+
+	vmlal.u32	q8,d21,d5[1]
+	vmlal.u32	q5,d25,d6[1]
+	vmlal.u32	q9,d23,d5[1]
+	vmlal.u32	q6,d27,d6[1]
+	vmlal.u32	q7,d29,d6[1]
+
+	vmlal.u32	q8,d29,d8[1]
+	vmlal.u32	q5,d23,d8[1]
+	vmlal.u32	q9,d21,d7[1]
+	vmlal.u32	q6,d25,d8[1]
+	vmlal.u32	q7,d27,d8[1]
+
+	vld4.32		{d21,d23,d25,d27},[r4]	@ inp[2:3] (or 0)
+	add		r4,r4,#64
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ (hash+inp[0:1])*r^4 and accumulate
+
+	vmlal.u32	q8,d26,d0[0]
+	vmlal.u32	q5,d20,d0[0]
+	vmlal.u32	q9,d28,d0[0]
+	vmlal.u32	q6,d22,d0[0]
+	vmlal.u32	q7,d24,d0[0]
+	vld1.32		d8[0],[r6,:32]
+
+	vmlal.u32	q8,d24,d1[0]
+	vmlal.u32	q5,d28,d2[0]
+	vmlal.u32	q9,d26,d1[0]
+	vmlal.u32	q6,d20,d1[0]
+	vmlal.u32	q7,d22,d1[0]
+
+	vmlal.u32	q8,d22,d3[0]
+	vmlal.u32	q5,d26,d4[0]
+	vmlal.u32	q9,d24,d3[0]
+	vmlal.u32	q6,d28,d4[0]
+	vmlal.u32	q7,d20,d3[0]
+
+	vmlal.u32	q8,d20,d5[0]
+	vmlal.u32	q5,d24,d6[0]
+	vmlal.u32	q9,d22,d5[0]
+	vmlal.u32	q6,d26,d6[0]
+	vmlal.u32	q8,d28,d8[0]
+
+	vmlal.u32	q7,d28,d6[0]
+	vmlal.u32	q5,d22,d8[0]
+	vmlal.u32	q9,d20,d7[0]
+	vmov.i32	q14,#1<<24		@ padbit, yes, always
+	vmlal.u32	q6,d24,d8[0]
+	vmlal.u32	q7,d26,d8[0]
+
+	vld4.32		{d20,d22,d24,d26},[r1]	@ inp[0:1]
+	add		r1,r1,#64
+# ifdef	__ARMEB__
+	vrev32.8	q10,q10
+	vrev32.8	q11,q11
+	vrev32.8	q12,q12
+	vrev32.8	q13,q13
+# endif
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ lazy reduction interleaved with base 2^32 -> base 2^26 of
+	@ inp[0:3] previously loaded to q10-q13 and smashed to q10-q14.
+
+	vshr.u64	q15,q8,#26
+	vmovn.i64	d16,q8
+	 vshr.u64	q4,q5,#26
+	 vmovn.i64	d10,q5
+	vadd.i64	q9,q9,q15		@ h3 -> h4
+	vbic.i32	d16,#0xfc000000
+	  vsri.u32	q14,q13,#8		@ base 2^32 -> base 2^26
+	 vadd.i64	q6,q6,q4		@ h0 -> h1
+	  vshl.u32	q13,q13,#18
+	 vbic.i32	d10,#0xfc000000
+
+	vshrn.u64	d30,q9,#26
+	vmovn.i64	d18,q9
+	 vshr.u64	q4,q6,#26
+	 vmovn.i64	d12,q6
+	 vadd.i64	q7,q7,q4		@ h1 -> h2
+	  vsri.u32	q13,q12,#14
+	vbic.i32	d18,#0xfc000000
+	  vshl.u32	q12,q12,#12
+	 vbic.i32	d12,#0xfc000000
+
+	vadd.i32	d10,d10,d30
+	vshl.u32	d30,d30,#2
+	  vbic.i32	q13,#0xfc000000
+	 vshrn.u64	d8,q7,#26
+	 vmovn.i64	d14,q7
+	vaddl.u32	q5,d10,d30	@ h4 -> h0 [widen for a sec]
+	  vsri.u32	q12,q11,#20
+	 vadd.i32	d16,d16,d8	@ h2 -> h3
+	  vshl.u32	q11,q11,#6
+	 vbic.i32	d14,#0xfc000000
+	  vbic.i32	q12,#0xfc000000
+
+	vshrn.u64	d30,q5,#26		@ re-narrow
+	vmovn.i64	d10,q5
+	  vsri.u32	q11,q10,#26
+	  vbic.i32	q10,#0xfc000000
+	 vshr.u32	d8,d16,#26
+	 vbic.i32	d16,#0xfc000000
+	vbic.i32	d10,#0xfc000000
+	vadd.i32	d12,d12,d30	@ h0 -> h1
+	 vadd.i32	d18,d18,d8	@ h3 -> h4
+	  vbic.i32	q11,#0xfc000000
+
+	bhi		.Loop_neon
+
+.Lskip_loop:
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ multiply (inp[0:1]+hash) or inp[2:3] by r^2:r^1
+
+	add		r7,r0,#(48+0*9*4)
+	add		r6,r0,#(48+1*9*4)
+	adds		r2,r2,#32
+	it		ne
+	movne		r2,#0
+	bne		.Long_tail
+
+	vadd.i32	d25,d24,d14	@ add hash value and move to #hi
+	vadd.i32	d21,d20,d10
+	vadd.i32	d27,d26,d16
+	vadd.i32	d23,d22,d12
+	vadd.i32	d29,d28,d18
+
+.Long_tail:
+	vld4.32		{d0[1],d1[1],d2[1],d3[1]},[r7]!	@ load r^1
+	vld4.32		{d0[0],d1[0],d2[0],d3[0]},[r6]!	@ load r^2
+
+	vadd.i32	d24,d24,d14	@ can be redundant
+	vmull.u32	q7,d25,d0
+	vadd.i32	d20,d20,d10
+	vmull.u32	q5,d21,d0
+	vadd.i32	d26,d26,d16
+	vmull.u32	q8,d27,d0
+	vadd.i32	d22,d22,d12
+	vmull.u32	q6,d23,d0
+	vadd.i32	d28,d28,d18
+	vmull.u32	q9,d29,d0
+
+	vmlal.u32	q5,d29,d2
+	vld4.32		{d4[1],d5[1],d6[1],d7[1]},[r7]!
+	vmlal.u32	q8,d25,d1
+	vld4.32		{d4[0],d5[0],d6[0],d7[0]},[r6]!
+	vmlal.u32	q6,d21,d1
+	vmlal.u32	q9,d27,d1
+	vmlal.u32	q7,d23,d1
+
+	vmlal.u32	q8,d23,d3
+	vld1.32		d8[1],[r7,:32]
+	vmlal.u32	q5,d27,d4
+	vld1.32		d8[0],[r6,:32]
+	vmlal.u32	q9,d25,d3
+	vmlal.u32	q6,d29,d4
+	vmlal.u32	q7,d21,d3
+
+	vmlal.u32	q8,d21,d5
+	 it		ne
+	 addne		r7,r0,#(48+2*9*4)
+	vmlal.u32	q5,d25,d6
+	 it		ne
+	 addne		r6,r0,#(48+3*9*4)
+	vmlal.u32	q9,d23,d5
+	vmlal.u32	q6,d27,d6
+	vmlal.u32	q7,d29,d6
+
+	vmlal.u32	q8,d29,d8
+	 vorn		q0,q0,q0	@ all-ones, can be redundant
+	vmlal.u32	q5,d23,d8
+	 vshr.u64	q0,q0,#38
+	vmlal.u32	q9,d21,d7
+	vmlal.u32	q6,d25,d8
+	vmlal.u32	q7,d27,d8
+
+	beq		.Lshort_tail
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ (hash+inp[0:1])*r^4:r^3 and accumulate
+
+	vld4.32		{d0[1],d1[1],d2[1],d3[1]},[r7]!	@ load r^3
+	vld4.32		{d0[0],d1[0],d2[0],d3[0]},[r6]!	@ load r^4
+
+	vmlal.u32	q7,d24,d0
+	vmlal.u32	q5,d20,d0
+	vmlal.u32	q8,d26,d0
+	vmlal.u32	q6,d22,d0
+	vmlal.u32	q9,d28,d0
+
+	vmlal.u32	q5,d28,d2
+	vld4.32		{d4[1],d5[1],d6[1],d7[1]},[r7]!
+	vmlal.u32	q8,d24,d1
+	vld4.32		{d4[0],d5[0],d6[0],d7[0]},[r6]!
+	vmlal.u32	q6,d20,d1
+	vmlal.u32	q9,d26,d1
+	vmlal.u32	q7,d22,d1
+
+	vmlal.u32	q8,d22,d3
+	vld1.32		d8[1],[r7,:32]
+	vmlal.u32	q5,d26,d4
+	vld1.32		d8[0],[r6,:32]
+	vmlal.u32	q9,d24,d3
+	vmlal.u32	q6,d28,d4
+	vmlal.u32	q7,d20,d3
+
+	vmlal.u32	q8,d20,d5
+	vmlal.u32	q5,d24,d6
+	vmlal.u32	q9,d22,d5
+	vmlal.u32	q6,d26,d6
+	vmlal.u32	q7,d28,d6
+
+	vmlal.u32	q8,d28,d8
+	 vorn		q0,q0,q0	@ all-ones
+	vmlal.u32	q5,d22,d8
+	 vshr.u64	q0,q0,#38
+	vmlal.u32	q9,d20,d7
+	vmlal.u32	q6,d24,d8
+	vmlal.u32	q7,d26,d8
+
+.Lshort_tail:
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ horizontal addition
+
+	vadd.i64	d16,d16,d17
+	vadd.i64	d10,d10,d11
+	vadd.i64	d18,d18,d19
+	vadd.i64	d12,d12,d13
+	vadd.i64	d14,d14,d15
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ lazy reduction, but without narrowing
+
+	vshr.u64	q15,q8,#26
+	vand.i64	q8,q8,q0
+	 vshr.u64	q4,q5,#26
+	 vand.i64	q5,q5,q0
+	vadd.i64	q9,q9,q15		@ h3 -> h4
+	 vadd.i64	q6,q6,q4		@ h0 -> h1
+
+	vshr.u64	q15,q9,#26
+	vand.i64	q9,q9,q0
+	 vshr.u64	q4,q6,#26
+	 vand.i64	q6,q6,q0
+	 vadd.i64	q7,q7,q4		@ h1 -> h2
+
+	vadd.i64	q5,q5,q15
+	vshl.u64	q15,q15,#2
+	 vshr.u64	q4,q7,#26
+	 vand.i64	q7,q7,q0
+	vadd.i64	q5,q5,q15		@ h4 -> h0
+	 vadd.i64	q8,q8,q4		@ h2 -> h3
+
+	vshr.u64	q15,q5,#26
+	vand.i64	q5,q5,q0
+	 vshr.u64	q4,q8,#26
+	 vand.i64	q8,q8,q0
+	vadd.i64	q6,q6,q15		@ h0 -> h1
+	 vadd.i64	q9,q9,q4		@ h3 -> h4
+
+	cmp		r2,#0
+	bne		.Leven
+
+	@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+	@ store hash value
+
+	vst4.32		{d10[0],d12[0],d14[0],d16[0]},[r0]!
+	vst1.32		{d18[0]},[r0]
+
+	vldmia	sp!,{d8-d15}			@ epilogue
+	ldmia	sp!,{r4-r7}
+.Lno_data_neon:
+	bx	lr					@ bx	lr
+.size	poly1305_blocks_neon,.-poly1305_blocks_neon
+
+.type	poly1305_emit_neon,%function
+.align	5
+poly1305_emit_neon:
+	ldr	ip,[r0,#36]		@ is_base2_26
+
+	stmdb	sp!,{r4-r11}
+
+	tst	ip,ip
+	beq	.Lpoly1305_emit_enter
+
+	ldmia	r0,{r3-r7}
+	eor	r8,r8,r8
+
+	adds	r3,r3,r4,lsl#26	@ base 2^26 -> base 2^32
+	mov	r4,r4,lsr#6
+	adcs	r4,r4,r5,lsl#20
+	mov	r5,r5,lsr#12
+	adcs	r5,r5,r6,lsl#14
+	mov	r6,r6,lsr#18
+	adcs	r6,r6,r7,lsl#8
+	adc	r7,r8,r7,lsr#24	@ can be partially reduced ...
+
+	and	r8,r7,#-4		@ ... so reduce
+	and	r7,r6,#3
+	add	r8,r8,r8,lsr#2	@ *= 5
+	adds	r3,r3,r8
+	adcs	r4,r4,#0
+	adcs	r5,r5,#0
+	adcs	r6,r6,#0
+	adc	r7,r7,#0
+
+	adds	r8,r3,#5		@ compare to modulus
+	adcs	r9,r4,#0
+	adcs	r10,r5,#0
+	adcs	r11,r6,#0
+	adc	r7,r7,#0
+	tst	r7,#4			@ did it carry/borrow?
+
+	it	ne
+	movne	r3,r8
+	ldr	r8,[r2,#0]
+	it	ne
+	movne	r4,r9
+	ldr	r9,[r2,#4]
+	it	ne
+	movne	r5,r10
+	ldr	r10,[r2,#8]
+	it	ne
+	movne	r6,r11
+	ldr	r11,[r2,#12]
+
+	adds	r3,r3,r8		@ accumulate nonce
+	adcs	r4,r4,r9
+	adcs	r5,r5,r10
+	adc	r6,r6,r11
+
+# ifdef __ARMEB__
+	rev	r3,r3
+	rev	r4,r4
+	rev	r5,r5
+	rev	r6,r6
+# endif
+	str	r3,[r1,#0]		@ store the result
+	str	r4,[r1,#4]
+	str	r5,[r1,#8]
+	str	r6,[r1,#12]
+
+	ldmia	sp!,{r4-r11}
+	bx	lr				@ bx	lr
+.size	poly1305_emit_neon,.-poly1305_emit_neon
+
+.align	5
+.Lzeros:
+.long	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
+.LOPENSSL_armcap:
+.word	OPENSSL_armcap_P-.Lpoly1305_init
+#endif
+.asciz	"Poly1305 for ARMv4/NEON, CRYPTOGAMS by <[email protected]>"
+.align	2
+#if	__ARM_MAX_ARCH__>=7
+.comm   OPENSSL_armcap_P,4,4
+#endif
lib/zinc/poly1305/poly1305-arm64-cryptogams.S new file +869 -0

869 lines not on this page · raw message

Signature · 1 line
2.19.0
#79 lore raw

Re: [PATCH net-next v6 11/23] zinc: import Andy Polyakov's Poly1305 ARM and ARM64 implementations

Quoted 8 lines · Jason A. Donenfeld

These NEON and non-NEON implementations come from Andy Polyakov's implementation, and are included here in raw form without modification, so that subsequent commits that fix these up for the kernel can see how it has changed. This awkward commit splitting has been requested for the ARM[64] implementations in particular.

While this is CRYPTOGAMS code, the originating code for this happens to be the same as OpenSSL's commit 5bb1cd2292b388263a0cc05392bb99141212aa53

Sorry to ruin the fun, but actually there are no Poly1305 implementations in CRYPTOGAMS (https://github.com/dot-asm/cryptogams). Nor are there any ChaCha20 implementations.

Andy P., can you please add your Poly1305 and ChaCha20 implementations to the CRYPTOGAMS repository, so that they have a clear kernel-compatible license?

It would be great if you'd include a kernel-compatible license directly in the versions in the OpenSSL tree too...

Thanks!

- Eric

#80 lore raw

Re: [PATCH net-next v6 11/23] zinc: import Andy Polyakov's Poly1305 ARM and ARM64 implementations

Quoted 7 lines · Eric Biggers
Quoted 6 lines · Jason A. Donenfeld

These NEON and non-NEON implementations come from Andy Polyakov's implementation, and are included here in raw form without modification, so that subsequent commits that fix these up for the kernel can see how it has changed. This awkward commit splitting has been requested for the ARM[64] implementations in particular.

"This awkward commit splitting"

Seriously?!?

So you really think it is fine to import huge chunks of code like this from other projects without keeping track of the local changes?

Quoted 7 lines
Quoted 2 lines

While this is CRYPTOGAMS code, the originating code for this happens to be the same as OpenSSL's commit 5bb1cd2292b388263a0cc05392bb99141212aa53

Sorry to ruin the fun, but actually there are no Poly1305 implementations in CRYPTOGAMS (https://github.com/dot-asm/cryptogams). Nor are there any ChaCha20 implementations.

So was this code taken directly from the OpenSSL project then? If so, why do the commit messages claim otherwise?

Quoted 6 lines

Andy P., can you please add your Poly1305 and ChaCha20 implementations to the CRYPTOGAMS repository, so that they have a clear kernel-compatible license?

It would be great if you'd include a kernel-compatible license directly in the versions in the OpenSSL tree too...

Yes please.

author maintainer
#81 lore raw

Re: [PATCH net-next v6 11/23] zinc: import Andy Polyakov's Poly1305 ARM and ARM64 implementations

Hi Ard,

Quoted 5 lines · Ard Biesheuvel
Quoted 3 lines
Quoted 3 lines

it has changed. This awkward commit splitting has been requested for the ARM[64] implementations in particular.

"This awkward commit splitting"

Awkward in the sense that only those two commits are doing it, whereas the rest of the series is not. Not awkward in any other sense that you seemed to have divined based on your oversized punctuation below. Fortunately, at your suggestion for v7, I've now done the splitting for all of the other places, and so I the comment in the dev tree a few days ago, since it's now done consistently across the patchset.

Quoted 5 lines

Seriously?!?

So you really think it is fine to import huge chunks of code like this from other projects without keeping track of the local changes?

As explained above, that's not what I meant at all.

Jason

author maintainer
#82 lore raw

Re: [PATCH net-next v6 11/23] zinc: import Andy Polyakov's Poly1305 ARM and ARM64 implementations

Quoted 1 line · Jason A. Donenfeld

for all of the other places, and so I the comment in the dev tree a

The missing word between "I" and "the" is "extirpated".

author maintainer
#83 lore raw

PATCH v6 12/23 zinc: Poly1305 ARM and ARM64 implementations

In reply to Jason A. Donenfeld

These wire Andy Polyakov's implementations up to the kernel. We make a few small changes to the assembly:

- Entries and exits use the proper kernel convention macro.
- CPU feature checking is done in C by the glue code, so that has been removed from the assembly.
- The function names have been renamed to fit kernel conventions.
- Labels have been renamed to fit kernel conventions.
- The neon code can jump to the scalar code when it makes sense to do so.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 lib/zinc/Makefile                             |   2 +
 lib/zinc/poly1305/poly1305-arm-glue.h         | 119 ++++++++++++++
 ...ly1305-arm-cryptogams.S => poly1305-arm.S} | 147 ++++++------------
 ...05-arm64-cryptogams.S => poly1305-arm64.S} | 103 ++++--------
 lib/zinc/poly1305/poly1305.c                  |   2 +
 5 files changed, 198 insertions(+), 175 deletions(-)
 create mode 100644 lib/zinc/poly1305/poly1305-arm-glue.h
 rename lib/zinc/poly1305/{poly1305-arm-cryptogams.S => poly1305-arm.S} (91%)
 rename lib/zinc/poly1305/{poly1305-arm64-cryptogams.S => poly1305-arm64.S} (90%)
lib/zinc/Makefile +2 -0
index a8943d960b6a..c09fd3de60f9 100644
@@ -12,4 +12,6 @@ obj-$(CONFIG_ZINC_CHACHA20) += zinc_chacha20.o
 
 zinc_poly1305-y := poly1305/poly1305.o
 zinc_poly1305-$(CONFIG_ZINC_ARCH_X86_64) += poly1305/poly1305-x86_64.o
+zinc_poly1305-$(CONFIG_ZINC_ARCH_ARM) += poly1305/poly1305-arm.o
+zinc_poly1305-$(CONFIG_ZINC_ARCH_ARM64) += poly1305/poly1305-arm64.o
 obj-$(CONFIG_ZINC_POLY1305) += zinc_poly1305.o
lib/zinc/poly1305/poly1305-arm-glue.h new file +119 -0

119 lines not on this page · raw message

lib/zinc/poly1305/poly1305-arm.S renamed from lib/zinc/poly1305/poly1305-arm-cryptogams.S +46 -101

334 lines not on this page · raw message

lib/zinc/poly1305/poly1305-arm64.S renamed from lib/zinc/poly1305/poly1305-arm64-cryptogams.S +29 -74

207 lines not on this page · raw message

lib/zinc/poly1305/poly1305.c +2 -0
index 2ae1b3cb66cd..647aa3354d38 100644
@@ -17,6 +17,8 @@
 
 #if defined(CONFIG_ZINC_ARCH_X86_64)
 #include "poly1305-x86_64-glue.h"
+#elif defined(CONFIG_ZINC_ARCH_ARM) || defined(CONFIG_ZINC_ARCH_ARM64)
+#include "poly1305-arm-glue.h"
 #else
 static inline bool poly1305_init_arch(void *ctx,
 				      const u8 key[POLY1305_KEY_SIZE])
Signature · 1 line
2.19.0
author maintainer
#84 lore raw

PATCH v6 13/23 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 results in a nice speedup on commodity Octeon hardware, and has been modified slightly from the original:

- The function names have been renamed to fit kernel conventions.
- A comment has been added.

No changes have been made to the actual instructions.

While MIPS64 is CRYPTOGAMS code, the originating code for this happens to be the same as OpenSSL's commit 947716c1872d210828122212d076d503ae68b928

Based-on-code-from: Andy Polyakov <[email protected]>
Cc: Samuel Neves <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Greg KH <[email protected]>
Cc: Jean-Philippe Aumasson <[email protected]>
Cc: Andy Polyakov <[email protected]>
Cc: Ralf Baechle <[email protected]>
Cc: Paul Burton <[email protected]>
Cc: James Hogan <[email protected]>
Cc: [email protected]

---
 lib/zinc/Makefile                      |   3 +
 lib/zinc/poly1305/poly1305-mips-glue.h |  35 +++
 lib/zinc/poly1305/poly1305-mips.S      | 407 +++++++++++++++++++++++++
 lib/zinc/poly1305/poly1305-mips64.S    | 355 +++++++++++++++++++++
 lib/zinc/poly1305/poly1305.c           |   2 +
 5 files changed, 802 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 +3 -0
index c09fd3de60f9..5c4b1d51cb03 100644
@@ -14,4 +14,7 @@ zinc_poly1305-y := poly1305/poly1305.o
 zinc_poly1305-$(CONFIG_ZINC_ARCH_X86_64) += poly1305/poly1305-x86_64.o
 zinc_poly1305-$(CONFIG_ZINC_ARCH_ARM) += poly1305/poly1305-arm.o
 zinc_poly1305-$(CONFIG_ZINC_ARCH_ARM64) += poly1305/poly1305-arm64.o
+zinc_poly1305-$(CONFIG_ZINC_ARCH_MIPS) += poly1305/poly1305-mips.o
+AFLAGS_poly1305-mips.o += -O2 # This is required to fill the branch delay slots
+zinc_poly1305-$(CONFIG_ZINC_ARCH_MIPS64) += poly1305/poly1305-mips64.o
 obj-$(CONFIG_ZINC_POLY1305) += zinc_poly1305.o
lib/zinc/poly1305/poly1305-mips-glue.h new file +35 -0
new file mode 100644
index 000000000000..eb38cd5a75ae
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+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]);
+static void __init poly1305_fpu_init(void)
+{
+}
+
+static inline bool poly1305_init_arch(void *ctx,
+				      const u8 key[POLY1305_KEY_SIZE])
+{
+	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;
+}
lib/zinc/poly1305/poly1305-mips.S new file +407 -0

407 lines not on this page · raw message

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

355 lines not on this page · raw message

lib/zinc/poly1305/poly1305.c +2 -0
index 647aa3354d38..361647054aff 100644
@@ -19,6 +19,8 @@
 #include "poly1305-x86_64-glue.h"
 #elif defined(CONFIG_ZINC_ARCH_ARM) || defined(CONFIG_ZINC_ARCH_ARM64)
 #include "poly1305-arm-glue.h"
+#elif defined(CONFIG_ZINC_ARCH_MIPS) || defined(CONFIG_ZINC_ARCH_MIPS64)
+#include "poly1305-mips-glue.h"
 #else
 static inline bool poly1305_init_arch(void *ctx,
 				      const u8 key[POLY1305_KEY_SIZE])
Signature · 1 line
2.19.0
author maintainer
#85 lore raw

PATCH v6 14/23 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 Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
---
 include/zinc/chacha20poly1305.h      |   50 +
 lib/zinc/Kconfig                     |    6 +
 lib/zinc/Makefile                    |    3 +
 lib/zinc/chacha20poly1305.c          |  361 +
 lib/zinc/selftest/chacha20poly1305.h | 9038 ++++++++++++++++++++++++++
 5 files changed, 9458 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 +50 -0
new file mode 100644
index 000000000000..d2753c5bada5
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * 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_NONCE_SIZE = 24,
+	CHACHA20POLY1305_KEY_SIZE = 32,
+	CHACHA20POLY1305_AUTHTAG_SIZE = 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_KEY_SIZE]);
+
+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_KEY_SIZE], 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_KEY_SIZE]);
+
+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_KEY_SIZE], 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_NONCE_SIZE],
+			       const u8 key[CHACHA20POLY1305_KEY_SIZE]);
+
+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_NONCE_SIZE],
+	const u8 key[CHACHA20POLY1305_KEY_SIZE]);
+
+#endif /* _ZINC_CHACHA20POLY1305_H */
lib/zinc/Kconfig +6 -0
index f9ef8f7e3c25..dec4633cdfb4 100644
@@ -5,6 +5,12 @@ config ZINC_CHACHA20
 config ZINC_POLY1305
 	tristate
 
+config ZINC_CHACHA20POLY1305
+	tristate
+	select ZINC_CHACHA20
+	select ZINC_POLY1305
+	select CRYPTO_BLKCIPHER
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	help
lib/zinc/Makefile +3 -0
index 5c4b1d51cb03..c31186b491e8 100644
@@ -18,3 +18,6 @@ zinc_poly1305-$(CONFIG_ZINC_ARCH_MIPS) += poly1305/poly1305-mips.o
 AFLAGS_poly1305-mips.o += -O2 # This is required to fill the branch delay slots
 zinc_poly1305-$(CONFIG_ZINC_ARCH_MIPS64) += poly1305/poly1305-mips64.o
 obj-$(CONFIG_ZINC_POLY1305) += zinc_poly1305.o
+
+zinc_chacha20poly1305-y := chacha20poly1305.o
+obj-$(CONFIG_ZINC_CHACHA20POLY1305) += zinc_chacha20poly1305.o
lib/zinc/chacha20poly1305.c new file +361 -0

361 lines not on this page · raw message

lib/zinc/selftest/chacha20poly1305.h new file +9038 -0

2,571 lines not on this page · raw message

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

Signature · 1 line
2.19.0

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

author maintainer
#86 lore raw

PATCH v6 15/23 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 Jean-Philippe Aumasson <[email protected]>
---
 include/zinc/blake2s.h      |   58 +
 lib/zinc/Kconfig            |    3 +
 lib/zinc/Makefile           |    3 +
 lib/zinc/blake2s/blake2s.c  |  307 +++++
 lib/zinc/selftest/blake2s.h | 2095 +++++++++++++++++++++++++++++++++++
 5 files changed, 2466 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 +58 -0
new file mode 100644
index 000000000000..da313dede041
@@ -0,0 +1,58 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * 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_BLOCK_SIZE = 64,
+	BLAKE2S_HASH_SIZE = 32,
+	BLAKE2S_KEY_SIZE = 32
+};
+
+struct blake2s_state {
+	u32 h[8];
+	u32 t[2];
+	u32 f[2];
+	u8 buf[BLAKE2S_BLOCK_SIZE];
+	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, u8 *out, const size_t outlen);
+
+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 DEBUG
+	BUG_ON((!in && inlen > 0) || !out || !outlen ||
+	       outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
+	       (!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);
+
+#endif /* _ZINC_BLAKE2S_H */
lib/zinc/Kconfig +3 -0
index dec4633cdfb4..7bf4bc88f81f 100644
@@ -11,6 +11,9 @@ config ZINC_CHACHA20POLY1305
 	select ZINC_POLY1305
 	select CRYPTO_BLKCIPHER
 
+config ZINC_BLAKE2S
+	tristate
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	help
lib/zinc/Makefile +3 -0
index c31186b491e8..d2ec55c33ef0 100644
@@ -21,3 +21,6 @@ obj-$(CONFIG_ZINC_POLY1305) += zinc_poly1305.o
 
 zinc_chacha20poly1305-y := chacha20poly1305.o
 obj-$(CONFIG_ZINC_CHACHA20POLY1305) += zinc_chacha20poly1305.o
+
+zinc_blake2s-y := blake2s/blake2s.o
+obj-$(CONFIG_ZINC_BLAKE2S) += zinc_blake2s.o
lib/zinc/blake2s/blake2s.c new file +307 -0

307 lines not on this page · raw message

lib/zinc/selftest/blake2s.h new file +2095 -0

2,095 lines not on this page · raw message

Signature · 1 line
2.19.0
author maintainer
#87 lore raw

PATCH v6 16/23 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 Jean-Philippe Aumasson <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 lib/zinc/Makefile                      |   1 +
 lib/zinc/blake2s/blake2s-x86_64-glue.h |  59 +++
 lib/zinc/blake2s/blake2s-x86_64.S      | 685 +++++++++++++++++++++++++
 lib/zinc/blake2s/blake2s.c             |   4 +-
 4 files changed, 748 insertions(+), 1 deletion(-)
 create mode 100644 lib/zinc/blake2s/blake2s-x86_64-glue.h
 create mode 100644 lib/zinc/blake2s/blake2s-x86_64.S
lib/zinc/Makefile +1 -0
index d2ec55c33ef0..67ad837c822c 100644
@@ -23,4 +23,5 @@ zinc_chacha20poly1305-y := chacha20poly1305.o
 obj-$(CONFIG_ZINC_CHACHA20POLY1305) += zinc_chacha20poly1305.o
 
 zinc_blake2s-y := blake2s/blake2s.o
+zinc_blake2s-$(CONFIG_ZINC_ARCH_X86_64) += blake2s/blake2s-x86_64.o
 obj-$(CONFIG_ZINC_BLAKE2S) += zinc_blake2s.o
lib/zinc/blake2s/blake2s-x86_64-glue.h new file +59 -0

59 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

lib/zinc/blake2s/blake2s.c +3 -1
index 1b64bee20bc4..b40bde23e5e4 100644
@@ -113,7 +113,9 @@ void blake2s_init_key(struct blake2s_state *state, const size_t outlen,
 }
 EXPORT_SYMBOL(blake2s_init_key);
 
-#ifndef HAVE_BLAKE2S_ARCH_IMPLEMENTATION
+#if defined(CONFIG_ZINC_ARCH_X86_64)
+#include "blake2s-x86_64-glue.h"
+#else
 static void __init blake2s_fpu_init(void)
 {
 }
Signature · 1 line
2.19.0
author maintainer
#88 lore raw

PATCH v6 17/23 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 Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Karthikeyan Bhargavan <[email protected]>
---
 include/zinc/curve25519.h               |   22 +
 lib/zinc/Kconfig                        |    4 +
 lib/zinc/Makefile                       |    3 +
 lib/zinc/curve25519/curve25519-fiat32.h |  860 +++++++++++++++
 lib/zinc/curve25519/curve25519-hacl64.h |  784 ++++++++++++++
 lib/zinc/curve25519/curve25519.c        |  109 ++
 lib/zinc/selftest/curve25519.h          | 1321 +++++++++++++++++++++++
 7 files changed, 3103 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 +22 -0
new file mode 100644
index 000000000000..def173e736fc
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * 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_KEY_SIZE = 32
+};
+
+bool __must_check curve25519(u8 mypublic[CURVE25519_KEY_SIZE],
+			     const u8 secret[CURVE25519_KEY_SIZE],
+			     const u8 basepoint[CURVE25519_KEY_SIZE]);
+void curve25519_generate_secret(u8 secret[CURVE25519_KEY_SIZE]);
+bool __must_check curve25519_generate_public(
+	u8 pub[CURVE25519_KEY_SIZE], const u8 secret[CURVE25519_KEY_SIZE]);
+
+#endif /* _ZINC_CURVE25519_H */
lib/zinc/Kconfig +4 -0
index 7bf4bc88f81f..0d8a94dd73a8 100644
@@ -14,6 +14,10 @@ config ZINC_CHACHA20POLY1305
 config ZINC_BLAKE2S
 	tristate
 
+config ZINC_CURVE25519
+	tristate
+	select CONFIG_CRYPTO
+
 config ZINC_DEBUG
 	bool "Zinc cryptography library debugging and self-tests"
 	help
lib/zinc/Makefile +3 -0
index 67ad837c822c..65440438c6e5 100644
@@ -25,3 +25,6 @@ obj-$(CONFIG_ZINC_CHACHA20POLY1305) += zinc_chacha20poly1305.o
 zinc_blake2s-y := blake2s/blake2s.o
 zinc_blake2s-$(CONFIG_ZINC_ARCH_X86_64) += blake2s/blake2s-x86_64.o
 obj-$(CONFIG_ZINC_BLAKE2S) += zinc_blake2s.o
+
+zinc_curve25519-y := curve25519/curve25519.o
+obj-$(CONFIG_ZINC_CURVE25519) += zinc_curve25519.o
lib/zinc/curve25519/curve25519-fiat32.h new file +860 -0

860 lines not on this page · raw message

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

784 lines not on this page · raw message

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

109 lines not on this page · raw message

lib/zinc/selftest/curve25519.h new file +1321 -0

1,209 lines not on this page · raw message

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

Signature · 1 line
2.19.0

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

#89 lore raw

Re: [PATCH net-next v6 17/23] zinc: Curve25519 generic C implementations and selftest

Quoted 8 lines · 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.

[]

lib/zinc/curve25519/curve25519-fiat32.h

Quoted 1 line
lib/zinc/curve25519/curve25519-fiat32.h +0 -0

[]

Quoted 45 lines
lib/zinc/curve25519/curve25519-fiat32.h +45 -0
+static __always_inline void fe_freeze(u32 out[10], const u32 in1[10])
+{
+	{ const u32 x17 = in1[9];
+	{ const u32 x18 = in1[8];
+	{ const u32 x16 = in1[7];
+	{ const u32 x14 = in1[6];
+	{ const u32 x12 = in1[5];
+	{ const u32 x10 = in1[4];
+	{ const u32 x8 = in1[3];
+	{ const u32 x6 = in1[2];
+	{ const u32 x4 = in1[1];
+	{ const u32 x2 = in1[0];
+	{ u32 x20; u8/*bool*/ x21 = subborrow_u26(0x0, x2, 0x3ffffed, &x20);
+	{ u32 x23; u8/*bool*/ x24 = subborrow_u25(x21, x4, 0x1ffffff, &x23);
+	{ u32 x26; u8/*bool*/ x27 = subborrow_u26(x24, x6, 0x3ffffff, &x26);
+	{ u32 x29; u8/*bool*/ x30 = subborrow_u25(x27, x8, 0x1ffffff, &x29);
+	{ u32 x32; u8/*bool*/ x33 = subborrow_u26(x30, x10, 0x3ffffff, &x32);
+	{ u32 x35; u8/*bool*/ x36 = subborrow_u25(x33, x12, 0x1ffffff, &x35);
+	{ u32 x38; u8/*bool*/ x39 = subborrow_u26(x36, x14, 0x3ffffff, &x38);
+	{ u32 x41; u8/*bool*/ x42 = subborrow_u25(x39, x16, 0x1ffffff, &x41);
+	{ u32 x44; u8/*bool*/ x45 = subborrow_u26(x42, x18, 0x3ffffff, &x44);
+	{ u32 x47; u8/*bool*/ x48 = subborrow_u25(x45, x17, 0x1ffffff, &x47);
+	{ u32 x49 = cmovznz32(x48, 0x0, 0xffffffff);
+	{ u32 x50 = (x49 & 0x3ffffed);
+	{ u32 x52; u8/*bool*/ x53 = addcarryx_u26(0x0, x20, x50, &x52);
+	{ u32 x54 = (x49 & 0x1ffffff);
+	{ u32 x56; u8/*bool*/ x57 = addcarryx_u25(x53, x23, x54, &x56);
+	{ u32 x58 = (x49 & 0x3ffffff);
+	{ u32 x60; u8/*bool*/ x61 = addcarryx_u26(x57, x26, x58, &x60);
+	{ u32 x62 = (x49 & 0x1ffffff);
+	{ u32 x64; u8/*bool*/ x65 = addcarryx_u25(x61, x29, x62, &x64);
+	{ u32 x66 = (x49 & 0x3ffffff);
+	{ u32 x68; u8/*bool*/ x69 = addcarryx_u26(x65, x32, x66, &x68);
+	{ u32 x70 = (x49 & 0x1ffffff);
+	{ u32 x72; u8/*bool*/ x73 = addcarryx_u25(x69, x35, x70, &x72);
+	{ u32 x74 = (x49 & 0x3ffffff);
+	{ u32 x76; u8/*bool*/ x77 = addcarryx_u26(x73, x38, x74, &x76);
+	{ u32 x78 = (x49 & 0x1ffffff);
+	{ u32 x80; u8/*bool*/ x81 = addcarryx_u25(x77, x41, x78, &x80);
+	{ u32 x82 = (x49 & 0x3ffffff);
+	{ u32 x84; u8/*bool*/ x85 = addcarryx_u26(x81, x44, x82, &x84);
+	{ u32 x86 = (x49 & 0x1ffffff);
+	{ u32 x88; addcarryx_u25(x85, x47, x86, &x88);
+	out[0] = x52;
+	out[1] = x56;

lib/zinc/curve25519/curve25519-fiat32.h line 158

Quoted 10 lines
lib/zinc/curve25519/curve25519-fiat32.h +10 -0
+	out[2] = x60;
+	out[3] = x64;
+	out[4] = x68;
+	out[5] = x72;
+	out[6] = x76;
+	out[7] = x80;
+	out[8] = x84;
+	out[9] = x88;
+	}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+}

Unidiomatic might be a stretch here...

[]

and elsewhere...

Quoted 24 lines
lib/zinc/curve25519/curve25519-fiat32.h +24 -0
+static void fe_add_impl(u32 out[10], const u32 in1[10], const u32 in2[10])
+{
+	{ const u32 x20 = in1[9];
+	{ const u32 x21 = in1[8];
+	{ const u32 x19 = in1[7];
+	{ const u32 x17 = in1[6];
+	{ const u32 x15 = in1[5];
+	{ const u32 x13 = in1[4];
+	{ const u32 x11 = in1[3];
+	{ const u32 x9 = in1[2];
+	{ const u32 x7 = in1[1];
+	{ const u32 x5 = in1[0];
+	{ const u32 x38 = in2[9];
+	{ const u32 x39 = in2[8];
+	{ const u32 x37 = in2[7];
+	{ const u32 x35 = in2[6];
+	{ const u32 x33 = in2[5];
+	{ const u32 x31 = in2[4];
+	{ const u32 x29 = in2[3];
+	{ const u32 x27 = in2[2];
+	{ const u32 x25 = in2[1];
+	{ const u32 x23 = in2[0];
+	out[0] = (x5 + x23);
+	out[1] = (x7 + x25);

lib/zinc/curve25519/curve25519-fiat32.h line 255

Quoted 10 lines
lib/zinc/curve25519/curve25519-fiat32.h +10 -0
+	out[2] = (x9 + x27);
+	out[3] = (x11 + x29);
+	out[4] = (x13 + x31);
+	out[5] = (x15 + x33);
+	out[6] = (x17 + x35);
+	out[7] = (x19 + x37);
+	out[8] = (x21 + x39);
+	out[9] = (x20 + x38);
+	}}}}}}}}}}}}}}}}}}}}
+}

etc...

author maintainer
#90 lore raw

PATCH v6 18/23 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 Jean-Philippe Aumasson <[email protected]>
Cc Armando Faz-Hernández <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 lib/zinc/curve25519/curve25519-x86_64-glue.h |   46 +
 lib/zinc/curve25519/curve25519-x86_64.h      | 2333 ++++++++++++++++++
 lib/zinc/curve25519/curve25519.c             |    4 +-
 3 files changed, 2382 insertions(+), 1 deletion(-)
 create mode 100644 lib/zinc/curve25519/curve25519-x86_64-glue.h
 create mode 100644 lib/zinc/curve25519/curve25519-x86_64.h
lib/zinc/curve25519/curve25519-x86_64-glue.h new file +46 -0
new file mode 100644
index 000000000000..142e4675773d
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <asm/cpufeature.h>
+#include <asm/processor.h>
+
+#include "curve25519-x86_64.h"
+
+static bool curve25519_use_bmi2 __ro_after_init;
+static bool curve25519_use_adx __ro_after_init;
+
+static void __init curve25519_fpu_init(void)
+{
+	curve25519_use_bmi2 = boot_cpu_has(X86_FEATURE_BMI2);
+	curve25519_use_adx = boot_cpu_has(X86_FEATURE_BMI2) &&
+			     boot_cpu_has(X86_FEATURE_ADX);
+}
+
+static inline bool curve25519_arch(u8 mypublic[CURVE25519_KEY_SIZE],
+				   const u8 secret[CURVE25519_KEY_SIZE],
+				   const u8 basepoint[CURVE25519_KEY_SIZE])
+{
+	if (curve25519_use_adx) {
+		curve25519_adx(mypublic, secret, basepoint);
+		return true;
+	} else if (curve25519_use_bmi2) {
+		curve25519_bmi2(mypublic, secret, basepoint);
+		return true;
+	}
+	return false;
+}
+
+static inline bool curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
+					const u8 secret[CURVE25519_KEY_SIZE])
+{
+	if (curve25519_use_adx) {
+		curve25519_adx_base(pub, secret);
+		return true;
+	} else if (curve25519_use_bmi2) {
+		curve25519_bmi2_base(pub, secret);
+		return true;
+	}
+	return false;
+}
lib/zinc/curve25519/curve25519-x86_64.h new file +2333 -0

2,333 lines not on this page · raw message

lib/zinc/curve25519/curve25519.c +3 -1
index 203fe67c0c9d..32536340d39d 100644
@@ -19,7 +19,9 @@
 #include <linux/init.h>
 #include <crypto/algapi.h>
 
-#ifndef HAVE_CURVE25519_ARCH_IMPLEMENTATION
+#if defined(CONFIG_ZINC_ARCH_X86_64)
+#include "curve25519-x86_64-glue.h"
+#else
 void __init curve25519_fpu_init(void)
 {
 }
Signature · 1 line
2.19.0
author maintainer
#91 lore raw

PATCH v6 19/23 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, as well as removing some qhasm strangeness to be more idiomatic.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 lib/zinc/Makefile                         |    1 +
 lib/zinc/curve25519/curve25519-arm-glue.h |   42 +
 lib/zinc/curve25519/curve25519-arm.S      | 2095 +++++++++++++++++++++
 lib/zinc/curve25519/curve25519.c          |    2 +
 4 files changed, 2140 insertions(+)
 create mode 100644 lib/zinc/curve25519/curve25519-arm-glue.h
 create mode 100644 lib/zinc/curve25519/curve25519-arm.S
lib/zinc/Makefile +1 -0
index 65440438c6e5..be73c342f9ba 100644
@@ -27,4 +27,5 @@ zinc_blake2s-$(CONFIG_ZINC_ARCH_X86_64) += blake2s/blake2s-x86_64.o
 obj-$(CONFIG_ZINC_BLAKE2S) += zinc_blake2s.o
 
 zinc_curve25519-y := curve25519/curve25519.o
+zinc_curve25519-$(CONFIG_ZINC_ARCH_ARM) += curve25519/curve25519-arm.o
 obj-$(CONFIG_ZINC_CURVE25519) += zinc_curve25519.o
lib/zinc/curve25519/curve25519-arm-glue.h new file +42 -0

42 lines not on this page · raw message

lib/zinc/curve25519/curve25519-arm.S new file +2095 -0

2,095 lines not on this page · raw message

lib/zinc/curve25519/curve25519.c +2 -0
index 32536340d39d..0d5ea97762d4 100644
@@ -21,6 +21,8 @@
 
 #if defined(CONFIG_ZINC_ARCH_X86_64)
 #include "curve25519-x86_64-glue.h"
+#elif defined(CONFIG_ZINC_ARCH_ARM)
+#include "curve25519-arm-glue.h"
 #else
 void __init curve25519_fpu_init(void)
 {
Signature · 1 line
2.19.0
#92 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

Hi Jason,

Quoted 68 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, as well as removing some qhasm strangeness to be more idiomatic.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Russell King <[email protected]>
Cc linux-arm-kernel at lists.infradead.org
---
 lib/zinc/Makefile                         |    1 +
 lib/zinc/curve25519/curve25519-arm-glue.h |   42 +
 lib/zinc/curve25519/curve25519-arm.S      | 2095 +++++++++++++++++++++
 lib/zinc/curve25519/curve25519.c          |    2 +
 4 files changed, 2140 insertions(+)
 create mode 100644 lib/zinc/curve25519/curve25519-arm-glue.h
 create mode 100644 lib/zinc/curve25519/curve25519-arm.S
lib/zinc/Makefile +1 -0
index 65440438c6e5..be73c342f9ba 100644
@@ -27,4 +27,5 @@ zinc_blake2s-$(CONFIG_ZINC_ARCH_X86_64) += blake2s/blake2s-x86_64.o
 obj-$(CONFIG_ZINC_BLAKE2S) += zinc_blake2s.o
 
 zinc_curve25519-y := curve25519/curve25519.o
+zinc_curve25519-$(CONFIG_ZINC_ARCH_ARM) += curve25519/curve25519-arm.o
 obj-$(CONFIG_ZINC_CURVE25519) += zinc_curve25519.o
lib/zinc/curve25519/curve25519-arm-glue.h new file +32 -0
new file mode 100644
index 000000000000..9211bcab5615
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <asm/hwcap.h>
+#include <asm/neon.h>
+#include <asm/simd.h>
+
+#if defined(CONFIG_KERNEL_MODE_NEON)
+asmlinkage void curve25519_neon(u8 mypublic[CURVE25519_KEY_SIZE],
+                               const u8 secret[CURVE25519_KEY_SIZE],
+                               const u8 basepoint[CURVE25519_KEY_SIZE]);
+#endif
+
+static bool curve25519_use_neon __ro_after_init;
+
+static void __init curve25519_fpu_init(void)
+{
+       curve25519_use_neon = elf_hwcap & HWCAP_NEON;
+}
+
+static inline bool curve25519_arch(u8 mypublic[CURVE25519_KEY_SIZE],
+                                  const u8 secret[CURVE25519_KEY_SIZE],
+                                  const u8 basepoint[CURVE25519_KEY_SIZE])
+{
+#if defined(CONFIG_KERNEL_MODE_NEON)
+       if (curve25519_use_neon && may_use_simd()) {
+               kernel_neon_begin();
+               curve25519_neon(mypublic, secret, basepoint);
+               kernel_neon_end();
+               return true;

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

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm-glue.h new file +10 -0
+       }
+#endif
+       return false;
+}
+
+static inline bool curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
+                                       const u8 secret[CURVE25519_KEY_SIZE])
+{
+       return false;
+}

Shouldn't this use the new simd abstraction as well?

Quoted 32 lines
lib/zinc/curve25519/curve25519-arm.S new file +26 -0
new file mode 100644
index 000000000000..db6570c20fd1
@@ -0,0 +1,2095 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/*
+ * Copyright (C) 2015-2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ *
+ * Based on public domain code from Daniel J. Bernstein and Peter Schwabe. This
+ * has been built from SUPERCOP's curve25519/neon2/scalarmult.pq using qhasm,
+ * but has subsequently been manually reworked for use in kernel space.
+ */
+
+#ifdef CONFIG_KERNEL_MODE_NEON
+#include <linux/linkage.h>
+
+.text
+.fpu neon
+.arch armv7-a
+.align 4
+
+ENTRY(curve25519_neon)
+       push            {r4-r11, lr}
+       mov             ip, sp
+       sub             r3, sp, #704
+       and             r3, r3, #0xfffffff0
+       mov             sp, r3
+       movw            r4, #0
+       movw            r5, #254
+       vmov.i32        q0, #1

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

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

I guess qhasm means generated code, right?

Because many of these adds are completely redundant ...

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

Quoted 11 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       add             r6, r3, #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
 
+       movw            r7, #960
+       sub             r7, r7, #2
+       neg             r7, r7
+       sub             r7, r7, r7, LSL #7

This looks odd as well.

Could you elaborate on what qhasm is exactly? And, as with the other patches, I would prefer it if we could have your changes as a separate patch (although having the qhasm base would be preferred)

Quoted 304 lines
lib/zinc/curve25519/curve25519-arm.S new file +304 -0
+       str             r7, [r6]
+       add             r6, sp, #672
+       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]!
+       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:
+       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, #456]
+       eor             r4, r4, r2
+       str             r2, [sp, #460]
+       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, #512
+       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

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

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       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, #480
+       vld1.8          {d18-d19}, [r2, : 128]

If you append a ! here ...

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

Quoted 6 lines
lib/zinc/curve25519/curve25519-arm.S new file +6 -0
+       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, #496

... you can drop this add

Quoted 127 lines
lib/zinc/curve25519/curve25519-arm.S new file +127 -0
+       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]

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

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       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, #528
+       vadd.i32        q10, q10, q2
+       vtrn.32         d24, d25
+       vst1.8          {d12-d13}, [r2, : 128]

same here

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

Quoted 3 lines
lib/zinc/curve25519/curve25519-arm.S new file +3 -0
+       vshl.i32        q6, q13, #1
+       add             r2, sp, #544
+       vst1.8          {d20-d21}, [r2, : 128]

and here

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

Quoted 3 lines
lib/zinc/curve25519/curve25519-arm.S new file +3 -0
+       vshl.i32        q10, q14, #1
+       add             r2, sp, #560
+       vst1.8          {d12-d13}, [r2, : 128]

and here

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

Quoted 6 lines
lib/zinc/curve25519/curve25519-arm.S new file +6 -0
+       vshl.i32        q15, q12, #1
+       vadd.i32        q8, q8, q4
+       vext.32         d10, d31, d30, #0
+       vadd.i32        q7, q7, q1
+       add             r2, sp, #576
+       vst1.8          {d16-d17}, [r2, : 128]

and here

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

Quoted 12 lines
lib/zinc/curve25519/curve25519-arm.S new file +12 -0
+       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, #592
+       vst1.8          {d14-d15}, [r2, : 128]

and here

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

Quoted 9 lines
lib/zinc/curve25519/curve25519-arm.S new file +9 -0
+       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, #608
+       vst1.8          {d20-d21}, [r2, : 128]

and here

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

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       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, #624
+       vst1.8          {d10-d11}, [r2, : 128]

and here

Quoted 17 lines
lib/zinc/curve25519/curve25519-arm.S new file +17 -0
+       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, #640
+       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

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

Quoted 10 lines
lib/zinc/curve25519/curve25519-arm.S new file +10 -0
+       add             r2, sp, #544
+       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, #544

redundant add

I'll stop here - let me just note that this code does not strike me as particularly well optimized for in-order cores (such as A7).

For instance, the sequence

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

can be reordered as

vmlal.s32 q2, d18, d7
vmlal.s32 q5, d18, d6
vmlal.s32 q1, d18, d21
vmlal.s32 q0, d18, d28
vmlal.s32 q6, d18, d29

vmlal.s32 q2, d19, d6
vmlal.s32 q5, d19, d21
vmlal.s32 q1, d19, d29
vmlal.s32 q0, d19, d9
vmlal.s32 q6, d19, d28

and not have every other instruction depend on the output of the previous one.

Obviously, the ultimate truth is in the benchmark numbers, but I'd thought I'd mention it anyway.

Quoted 1,538 lines
lib/zinc/curve25519/curve25519-arm.S new file +1522 -0
+       vst1.8          {d14-d15}, [r2, : 128]
+       vmull.s32       q7, d18, d6
+       vmlal.s32       q7, d26, d0
+       add             r2, sp, #624
+       vld1.8          {d30-d31}, [r2, : 128]
+       vmlal.s32       q2, d30, d21
+       vmlal.s32       q7, d19, d21
+       vmlal.s32       q7, d27, d20
+       add             r2, sp, #592
+       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, #576
+       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, #576
+       vst1.8          {d8-d9}, [r2, : 128]
+       add             r2, sp, #528
+       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, #608
+       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, #560
+       vld1.8          {d18-d19}, [r2, : 128]
+       add             r2, sp, #480
+       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, #496
+       vld1.8          {d6-d7}, [r2, : 128]
+       vmlal.s32       q6, d19, d21
+       add             r2, sp, #544
+       vld1.8          {d18-d19}, [r2, : 128]
+       vmlal.s32       q0, d30, d8
+       add             r2, sp, #640
+       vld1.8          {d20-d21}, [r2, : 128]
+       vmlal.s32       q5, d30, d29
+       add             r2, sp, #576
+       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, #528
+       vadd.i32        q10, q10, q2
+       vtrn.32         d24, d25
+       vst1.8          {d12-d13}, [r2, : 128]
+       vshl.i32        q6, q13, #1
+       add             r2, sp, #544
+       vst1.8          {d20-d21}, [r2, : 128]
+       vshl.i32        q10, q14, #1
+       add             r2, sp, #560
+       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, #576
+       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, #592
+       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, #608
+       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, #624
+       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, #640
+       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, #544
+       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, #544
+       vst1.8          {d14-d15}, [r2, : 128]
+       vmull.s32       q7, d18, d6
+       vmlal.s32       q7, d26, d0
+       add             r2, sp, #624
+       vld1.8          {d30-d31}, [r2, : 128]
+       vmlal.s32       q2, d30, d21
+       vmlal.s32       q7, d19, d21
+       vmlal.s32       q7, d27, d20
+       add             r2, sp, #592
+       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, #576
+       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, #576
+       vst1.8          {d8-d9}, [r2, : 128]
+       add             r2, sp, #528
+       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, #608
+       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, #560
+       vld1.8          {d18-d19}, [r2, : 128]
+       add             r2, sp, #480
+       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, #496
+       vld1.8          {d6-d7}, [r2, : 128]
+       vmlal.s32       q6, d19, d21
+       add             r2, sp, #544
+       vld1.8          {d18-d19}, [r2, : 128]
+       vmlal.s32       q0, d30, d8
+       add             r2, sp, #640
+       vld1.8          {d20-d21}, [r2, : 128]
+       vmlal.s32       q5, d30, d29
+       add             r2, sp, #576
+       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, #512
+       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, #480
+       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, #496
+       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, #528
+       vadd.i32        q10, q10, q2
+       vtrn.32         d24, d25
+       vst1.8          {d12-d13}, [r2, : 128]
+       vshl.i32        q6, q13, #1
+       add             r2, sp, #544
+       vst1.8          {d20-d21}, [r2, : 128]
+       vshl.i32        q10, q14, #1
+       add             r2, sp, #560
+       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, #576
+       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, #592
+       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, #608
+       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, #624
+       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, #640
+       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, #544
+       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, #544
+       vst1.8          {d14-d15}, [r2, : 128]
+       vmull.s32       q7, d18, d6
+       vmlal.s32       q7, d26, d0
+       add             r2, sp, #624
+       vld1.8          {d30-d31}, [r2, : 128]
+       vmlal.s32       q2, d30, d21
+       vmlal.s32       q7, d19, d21
+       vmlal.s32       q7, d27, d20
+       add             r2, sp, #592
+       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, #576
+       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, #576
+       vst1.8          {d8-d9}, [r2, : 128]
+       add             r2, sp, #528
+       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, #608
+       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, #560
+       vld1.8          {d18-d19}, [r2, : 128]
+       add             r2, sp, #480
+       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, #496
+       vld1.8          {d6-d7}, [r2, : 128]
+       vmlal.s32       q6, d19, d21
+       add             r2, sp, #544
+       vld1.8          {d18-d19}, [r2, : 128]
+       vmlal.s32       q0, d30, d8
+       add             r2, sp, #640
+       vld1.8          {d20-d21}, [r2, : 128]
+       vmlal.s32       q5, d30, d29
+       add             r2, sp, #576
+       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, #456]
+       ldr             r4, [sp, #460]
+       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]
+       movw            r1, #0
+.Linvertloop:
+       add             r2, r3, #144
+       movw            r4, #0
+       movw            r5, #2
+       cmp             r1, #1
+       moveq           r5, #1
+       addeq           r2, r3, #336
+       addeq           r4, r3, #48
+       cmp             r1, #2
+       moveq           r5, #1
+       addeq           r2, r3, #48
+       cmp             r1, #3
+       moveq           r5, #5
+       addeq           r4, r3, #336
+       cmp             r1, #4
+       moveq           r5, #10
+       cmp             r1, #5
+       moveq           r5, #20
+       cmp             r1, #6
+       moveq           r5, #10
+       addeq           r2, r3, #336
+       addeq           r4, r3, #336
+       cmp             r1, #7
+       moveq           r5, #50
+       cmp             r1, #8
+       moveq           r5, #100
+       cmp             r1, #9
+       moveq           r5, #50
+       addeq           r2, r3, #336
+       cmp             r1, #10
+       moveq           r5, #5
+       addeq           r2, r3, #48
+       cmp             r1, #11
+       moveq           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, #384
+       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, #480
+       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, #496
+       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, #384
+       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, #480
+       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, #496
+       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
+       mov             r9, r10, LSR #20
+       add             r1, r9, r1, LSL #6
+       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]
+       movw            r0, #0
+       mov             sp, ip
+       pop             {r4-r11, pc}
+ENDPROC(curve25519_neon)
+#endif
lib/zinc/curve25519/curve25519.c +2 -1
index 32536340d39d..0d5ea97762d4 100644
@@ -21,6 +21,8 @@
 
 #if defined(CONFIG_ZINC_ARCH_X86_64)
 #include "curve25519-x86_64-glue.h"
+#elif defined(CONFIG_ZINC_ARCH_ARM)
+#include "curve25519-arm-glue.h"
 #else
 void __init curve25519_fpu_init(void)
 {
--

2.19.0

#93 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

Ard,

Quoted 1 line · Ard Biesheuvel

I guess qhasm means generated code, right?

Yes.
Please see: http://cr.yp.to/qhasm.html

It generates code from a q-file (the "template"). Actually it is a rather powerful tool, but as you noticed, sometimes the code is not perfect.

Signature · 2 lines
Thanks,
//richard
author maintainer
#94 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

In reply to Ard Biesheuvel

(+Dan,Peter in CC. Replying to:
<https://lore.kernel.org/lkml/CAKv+Gu9FLDRLxHReKcveZYHNYerR5Y2pZd9gn-hWrU0jb2KgfA@mail.gmail.com/>
for context.)

Hi Ard,

Quoted 1 line · Ard Biesheuvel

Shouldn't this use the new simd abstraction as well?

Yes, it probably should, thanks.

Quoted 6 lines

I guess qhasm means generated code, right?
Because many of these adds are completely redundant ... This looks odd as well.
Could you elaborate on what qhasm is exactly? And, as with the other patches, I would prefer it if we could have your changes as a separate patch (although having the qhasm base would be preferred)

Indeed qhasm converts this --
<https://github.com/floodyberry/supercop/blob/master/crypto_scalarmult/curve25519/neon2/scalarmult.pq>
-- into this. It's a thing from Dan (CC'd now) --
<http://cr.yp.to/qhasm.html>. As you've requested, I can layer the patches to show our changes on top.

Quoted 8 lines

... you can drop this add
same here
and here
and here
and here
and here
and here
and here

Quoted 8 lines

redundant add
I'll stop here - let me just note that this code does not strike me as particularly well optimized for in-order cores (such as A7).
For instance, the sequence
can be reordered as
and not have every other instruction depend on the output of the previous one. Obviously, the ultimate truth is in the benchmark numbers, but I'd thought I'd mention it anyway.

Yes indeed the output is suboptimal in a lot of places. We can gradually clean this up -- slowly and carefully over time -- if you want. I can also look into producing a new implementation within HACL* so that it's verified. Assurance-wise, though, I feel pretty good about this implementation considering its origins, its breadth of use (in BoringSSL), the fuzzing hours it's incurred, and the actual implementation itself.

Either way, performance-wise, it's really worth having.

For example, on a Cortex-A7, we get these results (according to get_cycles()):

neon: 23142 cycles per call
fiat32: 49136 cycles per call
donna32: 71988 cycles per call

And on a Cortex-A9, we get these results (according to get_cycles()):

neon: 5020 cycles per call
fiat32: 17326 cycles per call
donna32: 28076 cycles per call

Jason

#95 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

For the in-order ARM Cortex-A8 (the target for this code), adjacent multiply-add instructions forward summands quickly. A simple in-order dot-product computation has no latency problems, while interleaving computations, as suggested in this thread, creates problems. Also, on this microarchitecture, occasional ARM instructions run in parallel with NEON, so trying to manually eliminate ARM instructions through global pointer tracking wouldn't gain speed; it would simply create unnecessary code-maintenance problems.

See https://cr.yp.to/papers.html#neoncrypto for analysis of the performance of---and remaining bottlenecks in---this code. Further speedups should be possible on this microarchitecture, but, for anyone interested in this, I recommend focusing on building a cycle-accurate simulator (e.g., fixing inaccuracies in the Sobole simulator) first.

Of course, there are other ARM microarchitectures, and there are many cases where different microarchitectures prefer different optimizations. The kernel already has boot-time benchmarks for different optimizations for raid6, and should do the same for crypto code, so that implementors can focus on each microarchitecture separately rather than living in the barbaric world of having to choose which CPUs to favor.

---Dan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20181005/4e88b0fb/attachment.sig>

#96 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

Quoted 15 lines · D. J. Bernstein

For the in-order ARM Cortex-A8 (the target for this code), adjacent multiply-add instructions forward summands quickly. A simple in-order dot-product computation has no latency problems, while interleaving computations, as suggested in this thread, creates problems. Also, on this microarchitecture, occasional ARM instructions run in parallel with NEON, so trying to manually eliminate ARM instructions through global pointer tracking wouldn't gain speed; it would simply create unnecessary code-maintenance problems.

See https://cr.yp.to/papers.html#neoncrypto for analysis of the performance of---and remaining bottlenecks in---this code. Further speedups should be possible on this microarchitecture, but, for anyone interested in this, I recommend focusing on building a cycle-accurate simulator (e.g., fixing inaccuracies in the Sobole simulator) first.

Quoted 7 lines

Of course, there are other ARM microarchitectures, and there are many cases where different microarchitectures prefer different optimizations. The kernel already has boot-time benchmarks for different optimizations for raid6, and should do the same for crypto code, so that implementors can focus on each microarchitecture separately rather than living in the barbaric world of having to choose which CPUs to favor.

Thanks Dan for the insight.

We have already established in a separate discussion that Cortex-A7, which is main optimization target for future development, does not have the microarchitectural peculiarity that you are referring to that ARM instructions are essentially free when interleaved with NEON code.

But I take your point re benchmarking (as I already indicated in my reply to Jason): if we optimize towards speed, we should ideally reuse the existing benchmarking infrastructure we have to select the fastest implementation at runtime. For instance, it turns out that scalar ChaCha20 is almost as fast as NEON (or even faster?) on A7, and using NEON in the kernel has some issues of its own.

author maintainer
#97 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

In reply to D. J. Bernstein

Hey Dan,

Quoted 6 lines · D. J. Bernstein

Of course, there are other ARM microarchitectures, and there are many cases where different microarchitectures prefer different optimizations. The kernel already has boot-time benchmarks for different optimizations for raid6, and should do the same for crypto code, so that implementors can focus on each microarchitecture separately rather than living in the barbaric world of having to choose which CPUs to favor.

I've been playing a bit with some code to do this sort of thing, choosing a set of implementations to enable or disable by trying all the combinations, and then calculating a quick median. I don't know if I'll submit that for the initial merge of this patchset -- and in fact all the current implementations I'm proposing are pretty much okay on microarchitectures -- but down the line this could be useful as a mechanism.

Jason
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20181005/5046b070/attachment.sig>

author maintainer
#98 lore raw

Re: [PATCH net-next v6 19/23] zinc: Curve25519 ARM implementation

In reply to Ard Biesheuvel
Quoted 3 lines · Ard Biesheuvel

Could you elaborate on what qhasm is exactly? And, as with the other patches, I would prefer it if we could have your changes as a separate patch (although having the qhasm base would be preferred)

By the way, as of a few minutes ago, if you look in the development tree at the commit called "zinc: Curve25519 ARM implementation", that now shows the diffs to the original, as you requested. I'll probably obsess over that a little bit more before v7, but if you see anything gnarly there beforehand, I'd be happy to hear.

Jason

author maintainer
#99 lore raw

PATCH v6 20/23 crypto: port Poly1305 to Zinc

In reply to Jason A. Donenfeld

Now that Poly1305 is in Zinc, we can have the crypto API code simply call into it. We have to do a little bit of book keeping here, because the crypto API receives the key in the first few calls to update.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Eric Biggers <[email protected]>
---
 arch/x86/crypto/Makefile               |   3 -
 arch/x86/crypto/poly1305-avx2-x86_64.S | 388 ----------------
 arch/x86/crypto/poly1305-sse2-x86_64.S | 584 -------------------------
 arch/x86/crypto/poly1305_glue.c        | 205 ---------
 crypto/Kconfig                         |  15 +-
 crypto/Makefile                        |   2 +-
 crypto/chacha20poly1305.c              |  12 +-
 crypto/poly1305_generic.c              | 304 -------------
 crypto/poly1305_zinc.c                 |  98 +++++
 include/crypto/poly1305.h              |  40 --
 10 files changed, 107 insertions(+), 1544 deletions(-)
 delete mode 100644 arch/x86/crypto/poly1305-avx2-x86_64.S
 delete mode 100644 arch/x86/crypto/poly1305-sse2-x86_64.S
 delete mode 100644 arch/x86/crypto/poly1305_glue.c
 delete mode 100644 crypto/poly1305_generic.c
 create mode 100644 crypto/poly1305_zinc.c
 delete mode 100644 include/crypto/poly1305.h
arch/x86/crypto/Makefile +0 -3
index a450ad573dcb..cf830219846b 100644
@@ -34,7 +34,6 @@ obj-$(CONFIG_CRYPTO_CRC32_PCLMUL) += crc32-pclmul.o
 obj-$(CONFIG_CRYPTO_SHA256_SSSE3) += sha256-ssse3.o
 obj-$(CONFIG_CRYPTO_SHA512_SSSE3) += sha512-ssse3.o
 obj-$(CONFIG_CRYPTO_CRCT10DIF_PCLMUL) += crct10dif-pclmul.o
-obj-$(CONFIG_CRYPTO_POLY1305_X86_64) += poly1305-x86_64.o
 
 obj-$(CONFIG_CRYPTO_AEGIS128_AESNI_SSE2) += aegis128-aesni.o
 obj-$(CONFIG_CRYPTO_AEGIS128L_AESNI_SSE2) += aegis128l-aesni.o
@@ -110,10 +109,8 @@ aesni-intel-y := aesni-intel_asm.o aesni-intel_glue.o fpu.o
 aesni-intel-$(CONFIG_64BIT) += aesni-intel_avx-x86_64.o aes_ctrby8_avx-x86_64.o
 ghash-clmulni-intel-y := ghash-clmulni-intel_asm.o ghash-clmulni-intel_glue.o
 sha1-ssse3-y := sha1_ssse3_asm.o sha1_ssse3_glue.o
-poly1305-x86_64-y := poly1305-sse2-x86_64.o poly1305_glue.o
 ifeq ($(avx2_supported),yes)
 sha1-ssse3-y += sha1_avx2_x86_64_asm.o
-poly1305-x86_64-y += poly1305-avx2-x86_64.o
 endif
 ifeq ($(sha1_ni_supported),yes)
 sha1-ssse3-y += sha1_ni_asm.o
arch/x86/crypto/poly1305-avx2-x86_64.S deleted +0 -388

388 lines not on this page · raw message

arch/x86/crypto/poly1305-sse2-x86_64.S deleted +0 -584

584 lines not on this page · raw message

arch/x86/crypto/poly1305_glue.c deleted +0 -205

205 lines not on this page · raw message

crypto/Kconfig +2 -13
index f3e40ac56d93..47859a0f8052 100644
@@ -656,24 +656,13 @@ config CRYPTO_GHASH
 config CRYPTO_POLY1305
 	tristate "Poly1305 authenticator algorithm"
 	select CRYPTO_HASH
+	select ZINC_POLY1305
 	help
 	  Poly1305 authenticator algorithm, RFC7539.
 
 	  Poly1305 is an authenticator algorithm designed by Daniel J. Bernstein.
 	  It is used for the ChaCha20-Poly1305 AEAD, specified in RFC7539 for use
-	  in IETF protocols. This is the portable C implementation of Poly1305.
-
-config CRYPTO_POLY1305_X86_64
-	tristate "Poly1305 authenticator algorithm (x86_64/SSE2/AVX2)"
-	depends on X86 && 64BIT
-	select CRYPTO_POLY1305
-	help
-	  Poly1305 authenticator algorithm, RFC7539.
-
-	  Poly1305 is an authenticator algorithm designed by Daniel J. Bernstein.
-	  It is used for the ChaCha20-Poly1305 AEAD, specified in RFC7539 for use
-	  in IETF protocols. This is the x86_64 assembler implementation using SIMD
-	  instructions.
+	  in IETF protocols.
 
 config CRYPTO_MD4
 	tristate "MD4 digest algorithm"
crypto/Makefile +1 -1
index 6d1d40eeb964..5e60348d02e2 100644
@@ -118,7 +118,7 @@ obj-$(CONFIG_CRYPTO_SEED) += seed.o
 obj-$(CONFIG_CRYPTO_SPECK) += speck.o
 obj-$(CONFIG_CRYPTO_SALSA20) += salsa20_generic.o
 obj-$(CONFIG_CRYPTO_CHACHA20) += chacha20_generic.o
-obj-$(CONFIG_CRYPTO_POLY1305) += poly1305_generic.o
+obj-$(CONFIG_CRYPTO_POLY1305) += poly1305_zinc.o
 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c_generic.o
crypto/chacha20poly1305.c +6 -6
index 600afa99941f..bf523797bef3 100644
@@ -14,7 +14,7 @@
 #include <crypto/internal/skcipher.h>
 #include <crypto/scatterwalk.h>
 #include <crypto/chacha20.h>
-#include <crypto/poly1305.h>
+#include <zinc/poly1305.h>
 #include <linux/err.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
@@ -62,7 +62,7 @@ struct chachapoly_req_ctx {
 	/* the key we generate for Poly1305 using Chacha20 */
 	u8 key[POLY1305_KEY_SIZE];
 	/* calculated Poly1305 tag */
-	u8 tag[POLY1305_DIGEST_SIZE];
+	u8 tag[POLY1305_MAC_SIZE];
 	/* length of data to en/decrypt, without ICV */
 	unsigned int cryptlen;
 	/* Actual AD, excluding IV */
@@ -471,7 +471,7 @@ static int chachapoly_decrypt(struct aead_request *req)
 {
 	struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
 
-	rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE;
+	rctx->cryptlen = req->cryptlen - POLY1305_MAC_SIZE;
 
 	/* decrypt call chain:
 	 * - poly_genkey/done()
@@ -513,7 +513,7 @@ static int chachapoly_setkey(struct crypto_aead *aead, const u8 *key,
 static int chachapoly_setauthsize(struct crypto_aead *tfm,
 				  unsigned int authsize)
 {
-	if (authsize != POLY1305_DIGEST_SIZE)
+	if (authsize != POLY1305_MAC_SIZE)
 		return -EINVAL;
 
 	return 0;
@@ -613,7 +613,7 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 	poly_hash = __crypto_hash_alg_common(poly);
 
 	err = -EINVAL;
-	if (poly_hash->digestsize != POLY1305_DIGEST_SIZE)
+	if (poly_hash->digestsize != POLY1305_MAC_SIZE)
 		goto out_put_poly;
 
 	err = -ENOMEM;
@@ -666,7 +666,7 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 				     ctx->saltlen;
 	inst->alg.ivsize = ivsize;
 	inst->alg.chunksize = crypto_skcipher_alg_chunksize(chacha);
-	inst->alg.maxauthsize = POLY1305_DIGEST_SIZE;
+	inst->alg.maxauthsize = POLY1305_MAC_SIZE;
 	inst->alg.init = chachapoly_init;
 	inst->alg.exit = chachapoly_exit;
 	inst->alg.encrypt = chachapoly_encrypt;
crypto/poly1305_generic.c deleted +0 -304

304 lines not on this page · raw message

crypto/poly1305_zinc.c new file +98 -0

98 lines not on this page · raw message

include/crypto/poly1305.h deleted +0 -40
deleted file mode 100644
index f718a19da82f..000000000000
@@ -1,40 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Common values for the Poly1305 algorithm
- */
-
-#ifndef _CRYPTO_POLY1305_H
-#define _CRYPTO_POLY1305_H
-
-#include <linux/types.h>
-#include <linux/crypto.h>
-
-#define POLY1305_BLOCK_SIZE	16
-#define POLY1305_KEY_SIZE	32
-#define POLY1305_DIGEST_SIZE	16
-
-struct poly1305_desc_ctx {
-	/* key */
-	u32 r[5];
-	/* finalize key */
-	u32 s[4];
-	/* accumulator */
-	u32 h[5];
-	/* partial buffer */
-	u8 buf[POLY1305_BLOCK_SIZE];
-	/* bytes used in partial buffer */
-	unsigned int buflen;
-	/* r key has been set */
-	bool rset;
-	/* s key has been set */
-	bool sset;
-};
-
-int crypto_poly1305_init(struct shash_desc *desc);
-unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
-					const u8 *src, unsigned int srclen);
-int crypto_poly1305_update(struct shash_desc *desc,
-			   const u8 *src, unsigned int srclen);
-int crypto_poly1305_final(struct shash_desc *desc, u8 *dst);
-
-#endif
Signature · 1 line
2.19.0
author maintainer
#100 lore raw

PATCH v6 21/23 crypto: port ChaCha20 to Zinc

In reply to Jason A. Donenfeld

Now that ChaCha20 is in Zinc, we can have the crypto API code simply call into it. The crypto API expects to have a stored key per instance and independent nonces, so we follow suite and store the key and initialize the nonce independently.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Eric Biggers <[email protected]>
---
 arch/arm/configs/exynos_defconfig       |   1 -
 arch/arm/configs/multi_v7_defconfig     |   1 -
 arch/arm/configs/omap2plus_defconfig    |   1 -
 arch/arm/crypto/Kconfig                 |   6 -
 arch/arm/crypto/Makefile                |   2 -
 arch/arm/crypto/chacha20-neon-core.S    | 521 --------------------
 arch/arm/crypto/chacha20-neon-glue.c    | 127 -----
 arch/arm64/configs/defconfig            |   1 -
 arch/arm64/crypto/Kconfig               |   6 -
 arch/arm64/crypto/Makefile              |   3 -
 arch/arm64/crypto/chacha20-neon-core.S  | 450 -----------------
 arch/arm64/crypto/chacha20-neon-glue.c  | 133 -----
 arch/x86/crypto/Makefile                |   3 -
 arch/x86/crypto/chacha20-avx2-x86_64.S  | 448 -----------------
 arch/x86/crypto/chacha20-ssse3-x86_64.S | 630 ------------------------
 arch/x86/crypto/chacha20_glue.c         | 146 ------
 crypto/Kconfig                          |  17 +-
 crypto/Makefile                         |   2 +-
 crypto/chacha20_generic.c               | 136 -----
 crypto/chacha20_zinc.c                  |  90 ++++
 crypto/chacha20poly1305.c               |   8 +-
 include/crypto/chacha20.h               |  12 -
 22 files changed, 96 insertions(+), 2648 deletions(-)
 delete mode 100644 arch/arm/crypto/chacha20-neon-core.S
 delete mode 100644 arch/arm/crypto/chacha20-neon-glue.c
 delete mode 100644 arch/arm64/crypto/chacha20-neon-core.S
 delete mode 100644 arch/arm64/crypto/chacha20-neon-glue.c
 delete mode 100644 arch/x86/crypto/chacha20-avx2-x86_64.S
 delete mode 100644 arch/x86/crypto/chacha20-ssse3-x86_64.S
 delete mode 100644 arch/x86/crypto/chacha20_glue.c
 delete mode 100644 crypto/chacha20_generic.c
 create mode 100644 crypto/chacha20_zinc.c
arch/arm/configs/exynos_defconfig +0 -1
index 27ea6dfcf2f2..95929b5e7b10 100644
@@ -350,7 +350,6 @@ CONFIG_CRYPTO_SHA1_ARM_NEON=m
 CONFIG_CRYPTO_SHA256_ARM=m
 CONFIG_CRYPTO_SHA512_ARM=m
 CONFIG_CRYPTO_AES_ARM_BS=m
-CONFIG_CRYPTO_CHACHA20_NEON=m
 CONFIG_CRC_CCITT=y
 CONFIG_FONTS=y
 CONFIG_FONT_7x14=y
arch/arm/configs/multi_v7_defconfig +0 -1
index fc33444e94f0..63be07724db3 100644
@@ -1000,4 +1000,3 @@ CONFIG_CRYPTO_AES_ARM_BS=m
 CONFIG_CRYPTO_AES_ARM_CE=m
 CONFIG_CRYPTO_GHASH_ARM_CE=m
 CONFIG_CRYPTO_CRC32_ARM_CE=m
-CONFIG_CRYPTO_CHACHA20_NEON=m
arch/arm/configs/omap2plus_defconfig +0 -1
index 6491419b1dad..f585a8ecc336 100644
@@ -547,7 +547,6 @@ CONFIG_CRYPTO_SHA512_ARM=m
 CONFIG_CRYPTO_AES_ARM=m
 CONFIG_CRYPTO_AES_ARM_BS=m
 CONFIG_CRYPTO_GHASH_ARM_CE=m
-CONFIG_CRYPTO_CHACHA20_NEON=m
 CONFIG_CRC_CCITT=y
 CONFIG_CRC_T10DIF=y
 CONFIG_CRC_ITU_T=y
arch/arm/crypto/Kconfig +0 -6
index 925d1364727a..fb80fd89f0e7 100644
@@ -115,12 +115,6 @@ config CRYPTO_CRC32_ARM_CE
 	depends on KERNEL_MODE_NEON && CRC32
 	select CRYPTO_HASH
 
-config CRYPTO_CHACHA20_NEON
-	tristate "NEON accelerated ChaCha20 symmetric cipher"
-	depends on KERNEL_MODE_NEON
-	select CRYPTO_BLKCIPHER
-	select CRYPTO_CHACHA20
-
 config CRYPTO_SPECK_NEON
 	tristate "NEON accelerated Speck cipher algorithms"
 	depends on KERNEL_MODE_NEON
arch/arm/crypto/Makefile +0 -2
index 8de542c48ade..bbfa98447063 100644
@@ -9,7 +9,6 @@ obj-$(CONFIG_CRYPTO_SHA1_ARM) += sha1-arm.o
 obj-$(CONFIG_CRYPTO_SHA1_ARM_NEON) += sha1-arm-neon.o
 obj-$(CONFIG_CRYPTO_SHA256_ARM) += sha256-arm.o
 obj-$(CONFIG_CRYPTO_SHA512_ARM) += sha512-arm.o
-obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o
 obj-$(CONFIG_CRYPTO_SPECK_NEON) += speck-neon.o
 
 ce-obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o
@@ -53,7 +52,6 @@ aes-arm-ce-y	:= aes-ce-core.o aes-ce-glue.o
 ghash-arm-ce-y	:= ghash-ce-core.o ghash-ce-glue.o
 crct10dif-arm-ce-y	:= crct10dif-ce-core.o crct10dif-ce-glue.o
 crc32-arm-ce-y:= crc32-ce-core.o crc32-ce-glue.o
-chacha20-neon-y := chacha20-neon-core.o chacha20-neon-glue.o
 speck-neon-y := speck-neon-core.o speck-neon-glue.o
 
 ifdef REGENERATE_ARM_CRYPTO
arch/arm/crypto/chacha20-neon-core.S deleted +0 -521

521 lines not on this page · raw message

arch/arm/crypto/chacha20-neon-glue.c deleted +0 -127

127 lines not on this page · raw message

arch/arm64/configs/defconfig +0 -1
index db8d364f8476..6cc3c8a0ad88 100644
@@ -709,5 +709,4 @@ CONFIG_CRYPTO_CRCT10DIF_ARM64_CE=m
 CONFIG_CRYPTO_CRC32_ARM64_CE=m
 CONFIG_CRYPTO_AES_ARM64_CE_CCM=y
 CONFIG_CRYPTO_AES_ARM64_CE_BLK=y
-CONFIG_CRYPTO_CHACHA20_NEON=m
 CONFIG_CRYPTO_AES_ARM64_BS=m
arch/arm64/crypto/Kconfig +0 -6
index e3fdb0fd6f70..9db6d775a880 100644
@@ -105,12 +105,6 @@ config CRYPTO_AES_ARM64_NEON_BLK
 	select CRYPTO_AES
 	select CRYPTO_SIMD
 
-config CRYPTO_CHACHA20_NEON
-	tristate "NEON accelerated ChaCha20 symmetric cipher"
-	depends on KERNEL_MODE_NEON
-	select CRYPTO_BLKCIPHER
-	select CRYPTO_CHACHA20
-
 config CRYPTO_AES_ARM64_BS
 	tristate "AES in ECB/CBC/CTR/XTS modes using bit-sliced NEON algorithm"
 	depends on KERNEL_MODE_NEON
arch/arm64/crypto/Makefile +0 -3
index bcafd016618e..507c4bfb86e3 100644
@@ -53,9 +53,6 @@ sha256-arm64-y := sha256-glue.o sha256-core.o
 obj-$(CONFIG_CRYPTO_SHA512_ARM64) += sha512-arm64.o
 sha512-arm64-y := sha512-glue.o sha512-core.o
 
-obj-$(CONFIG_CRYPTO_CHACHA20_NEON) += chacha20-neon.o
-chacha20-neon-y := chacha20-neon-core.o chacha20-neon-glue.o
-
 obj-$(CONFIG_CRYPTO_SPECK_NEON) += speck-neon.o
 speck-neon-y := speck-neon-core.o speck-neon-glue.o
 
arch/arm64/crypto/chacha20-neon-core.S deleted +0 -450

450 lines not on this page · raw message

arch/arm64/crypto/chacha20-neon-glue.c deleted +0 -133

133 lines not on this page · raw message

arch/x86/crypto/Makefile +0 -3
index cf830219846b..419212c31246 100644
@@ -23,7 +23,6 @@ obj-$(CONFIG_CRYPTO_CAMELLIA_X86_64) += camellia-x86_64.o
 obj-$(CONFIG_CRYPTO_BLOWFISH_X86_64) += blowfish-x86_64.o
 obj-$(CONFIG_CRYPTO_TWOFISH_X86_64) += twofish-x86_64.o
 obj-$(CONFIG_CRYPTO_TWOFISH_X86_64_3WAY) += twofish-x86_64-3way.o
-obj-$(CONFIG_CRYPTO_CHACHA20_X86_64) += chacha20-x86_64.o
 obj-$(CONFIG_CRYPTO_SERPENT_SSE2_X86_64) += serpent-sse2-x86_64.o
 obj-$(CONFIG_CRYPTO_AES_NI_INTEL) += aesni-intel.o
 obj-$(CONFIG_CRYPTO_GHASH_CLMUL_NI_INTEL) += ghash-clmulni-intel.o
@@ -76,7 +75,6 @@ camellia-x86_64-y := camellia-x86_64-asm_64.o camellia_glue.o
 blowfish-x86_64-y := blowfish-x86_64-asm_64.o blowfish_glue.o
 twofish-x86_64-y := twofish-x86_64-asm_64.o twofish_glue.o
 twofish-x86_64-3way-y := twofish-x86_64-asm_64-3way.o twofish_glue_3way.o
-chacha20-x86_64-y := chacha20-ssse3-x86_64.o chacha20_glue.o
 serpent-sse2-x86_64-y := serpent-sse2-x86_64-asm_64.o serpent_sse2_glue.o
 
 aegis128-aesni-y := aegis128-aesni-asm.o aegis128-aesni-glue.o
@@ -99,7 +97,6 @@ endif
 
 ifeq ($(avx2_supported),yes)
 	camellia-aesni-avx2-y := camellia-aesni-avx2-asm_64.o camellia_aesni_avx2_glue.o
-	chacha20-x86_64-y += chacha20-avx2-x86_64.o
 	serpent-avx2-y := serpent-avx2-asm_64.o serpent_avx2_glue.o
 
 	morus1280-avx2-y := morus1280-avx2-asm.o morus1280-avx2-glue.o
arch/x86/crypto/chacha20-avx2-x86_64.S deleted +0 -448

448 lines not on this page · raw message

arch/x86/crypto/chacha20-ssse3-x86_64.S deleted +0 -630

630 lines not on this page · raw message

arch/x86/crypto/chacha20_glue.c deleted +0 -146

146 lines not on this page · raw message

crypto/Kconfig +1 -16
index 47859a0f8052..42dc48aa9b81 100644
@@ -1428,27 +1428,12 @@ config CRYPTO_SALSA20
 config CRYPTO_CHACHA20
 	tristate "ChaCha20 cipher algorithm"
 	select CRYPTO_BLKCIPHER
+	select ZINC_CHACHA20
 	help
 	  ChaCha20 cipher algorithm, RFC7539.
 
 	  ChaCha20 is a 256-bit high-speed stream cipher designed by Daniel J.
 	  Bernstein and further specified in RFC7539 for use in IETF protocols.
-	  This is the portable C implementation of ChaCha20.
-
-	  See also:
-	  <http://cr.yp.to/chacha/chacha-20080128.pdf>
-
-config CRYPTO_CHACHA20_X86_64
-	tristate "ChaCha20 cipher algorithm (x86_64/SSSE3/AVX2)"
-	depends on X86 && 64BIT
-	select CRYPTO_BLKCIPHER
-	select CRYPTO_CHACHA20
-	help
-	  ChaCha20 cipher algorithm, RFC7539.
-
-	  ChaCha20 is a 256-bit high-speed stream cipher designed by Daniel J.
-	  Bernstein and further specified in RFC7539 for use in IETF protocols.
-	  This is the x86_64 assembler implementation using SIMD instructions.
 
 	  See also:
 	  <http://cr.yp.to/chacha/chacha-20080128.pdf>
crypto/Makefile +1 -1
index 5e60348d02e2..587103b87890 100644
@@ -117,7 +117,7 @@ obj-$(CONFIG_CRYPTO_ANUBIS) += anubis.o
 obj-$(CONFIG_CRYPTO_SEED) += seed.o
 obj-$(CONFIG_CRYPTO_SPECK) += speck.o
 obj-$(CONFIG_CRYPTO_SALSA20) += salsa20_generic.o
-obj-$(CONFIG_CRYPTO_CHACHA20) += chacha20_generic.o
+obj-$(CONFIG_CRYPTO_CHACHA20) += chacha20_zinc.o
 obj-$(CONFIG_CRYPTO_POLY1305) += poly1305_zinc.o
 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
crypto/chacha20_generic.c deleted +0 -136

136 lines not on this page · raw message

crypto/chacha20_zinc.c new file +90 -0

90 lines not on this page · raw message

crypto/chacha20poly1305.c +4 -4
index bf523797bef3..585c7ef4f543 100644
@@ -13,7 +13,7 @@
 #include <crypto/internal/hash.h>
 #include <crypto/internal/skcipher.h>
 #include <crypto/scatterwalk.h>
-#include <crypto/chacha20.h>
+#include <zinc/chacha20.h>
 #include <zinc/poly1305.h>
 #include <linux/err.h>
 #include <linux/init.h>
@@ -51,7 +51,7 @@ struct poly_req {
 };
 
 struct chacha_req {
-	u8 iv[CHACHA20_IV_SIZE];
+	u8 iv[CHACHA20_NONCE_SIZE];
 	struct scatterlist src[1];
 	struct skcipher_request req; /* must be last member */
 };
@@ -91,7 +91,7 @@ static void chacha_iv(u8 *iv, struct aead_request *req, u32 icb)
 	memcpy(iv, &leicb, sizeof(leicb));
 	memcpy(iv + sizeof(leicb), ctx->salt, ctx->saltlen);
 	memcpy(iv + sizeof(leicb) + ctx->saltlen, req->iv,
-	       CHACHA20_IV_SIZE - sizeof(leicb) - ctx->saltlen);
+	       CHACHA20_NONCE_SIZE - sizeof(leicb) - ctx->saltlen);
 }
 
 static int poly_verify_tag(struct aead_request *req)
@@ -639,7 +639,7 @@ static int chachapoly_create(struct crypto_template *tmpl, struct rtattr **tb,
 
 	err = -EINVAL;
 	/* Need 16-byte IV size, including Initial Block Counter value */
-	if (crypto_skcipher_alg_ivsize(chacha) != CHACHA20_IV_SIZE)
+	if (crypto_skcipher_alg_ivsize(chacha) != CHACHA20_NONCE_SIZE)
 		goto out_drop_chacha;
 	/* Not a stream cipher? */
 	if (chacha->base.cra_blocksize != 1)
include/crypto/chacha20.h +0 -12
index b83d66073db0..3b92f58f3891 100644
@@ -6,23 +6,11 @@
 #ifndef _CRYPTO_CHACHA20_H
 #define _CRYPTO_CHACHA20_H
 
-#include <crypto/skcipher.h>
-#include <linux/types.h>
-#include <linux/crypto.h>
-
 #define CHACHA20_IV_SIZE	16
 #define CHACHA20_KEY_SIZE	32
 #define CHACHA20_BLOCK_SIZE	64
 #define CHACHA20_BLOCK_WORDS	(CHACHA20_BLOCK_SIZE / sizeof(u32))
 
-struct chacha20_ctx {
-	u32 key[8];
-};
-
 void chacha20_block(u32 *state, u32 *stream);
-void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv);
-int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
-			   unsigned int keysize);
-int crypto_chacha20_crypt(struct skcipher_request *req);
 
 #endif
Signature · 1 line
2.19.0
maintainer
#101 lore raw

Re: [PATCH net-next v6 21/23] crypto: port ChaCha20 to Zinc

Quoted 35 lines · Jason A. Donenfeld

Now that ChaCha20 is in Zinc, we can have the crypto API code simply call into it. The crypto API expects to have a stored key per instance and independent nonces, so we follow suite and store the key and initialize the nonce independently.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Eric Biggers <[email protected]>

---

arch/arm/configs/exynos_defconfig       |   1 -
arch/arm/configs/multi_v7_defconfig     |   1 -
arch/arm/configs/omap2plus_defconfig    |   1 -
arch/arm/crypto/Kconfig                 |   6 -
arch/arm/crypto/Makefile                |   2 -
arch/arm/crypto/chacha20-neon-core.S    | 521 --------------------
arch/arm/crypto/chacha20-neon-glue.c    | 127 -----
arch/arm64/configs/defconfig            |   1 -
arch/arm64/crypto/Kconfig               |   6 -
arch/arm64/crypto/Makefile              |   3 -
arch/arm64/crypto/chacha20-neon-core.S  | 450 -----------------
arch/arm64/crypto/chacha20-neon-glue.c  | 133 -----
arch/x86/crypto/Makefile                |   3 -
arch/x86/crypto/chacha20-avx2-x86_64.S  | 448 -----------------
arch/x86/crypto/chacha20-ssse3-x86_64.S | 630 ------------------------
arch/x86/crypto/chacha20_glue.c         | 146 ------
crypto/Kconfig                          |  17 +-
crypto/Makefile                         |   2 +-
crypto/chacha20_generic.c               | 136 -----
crypto/chacha20_zinc.c                  |  90 ++++
crypto/chacha20poly1305.c               |   8 +-
include/crypto/chacha20.h               |  12 -
22 files changed, 96 insertions(+), 2648 deletions(-)
Quoted 9 lines

delete mode 100644 arch/arm/crypto/chacha20-neon-core.S delete mode 100644 arch/arm/crypto/chacha20-neon-glue.c delete mode 100644 arch/arm64/crypto/chacha20-neon-core.S delete mode 100644 arch/arm64/crypto/chacha20-neon-glue.c delete mode 100644 arch/x86/crypto/chacha20-avx2-x86_64.S delete mode 100644 arch/x86/crypto/chacha20-ssse3-x86_64.S delete mode 100644 arch/x86/crypto/chacha20_glue.c delete mode 100644 crypto/chacha20_generic.c create mode 100644 crypto/chacha20_zinc.c

Oh nice, so you did the conversion of the existing crypto code.

I presume someone has done the numbers and verified that there is no performance regression?

If so it would be good to include those numbers somewhere in this submission (within a patch description so that it goes into git).

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#102 lore raw

Re: [PATCH net-next v6 21/23] crypto: port ChaCha20 to Zinc

Hi Herbert,

Quoted 7 lines · Herbert Xu

Oh nice, so you did the conversion of the existing crypto code.

I presume someone has done the numbers and verified that there is no performance regression?

If so it would be good to include those numbers somewhere in this submission (within a patch description so that it goes into git).

Indeed I've done a pretty ridiculous amount of benchmarking, and the x86_64 numbers are included in those commits because they're pretty straightforward to show. The performance is also better on arm1176,cortex-a7,8,9,15,53,73. On mips, as well, performance is better, since there wasn't any optimized code there at all. Early versions of this patchset included a regression on cortex-a7, which was particularly problematic because Eric Biggers recent fscrypt work requires fast chacha on that hardware, but this v6 (and the future v7) contain code that is faster on all platforms across the board.

Regards,
Jason

#103 lore raw

Re: [PATCH net-next v6 21/23] crypto: port ChaCha20 to Zinc

In reply to Jason A. Donenfeld
Quoted 33 lines · Jason A. Donenfeld
crypto/chacha20_zinc.c new file +27 -0
new file mode 100644
index 000000000000..f7d70b3efc31
@@ -0,0 +1,90 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
+ */
+
+#include <asm/unaligned.h>
+#include <crypto/algapi.h>
+#include <crypto/internal/skcipher.h>
+#include <zinc/chacha20.h>
+#include <linux/module.h>
+
+static int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
+				  unsigned int keysize)
+{
+	struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
+
+	if (keysize != CHACHA20_KEY_SIZE)
+		return -EINVAL;
+	chacha20_init(ctx, key, 0);
+	return 0;
+}
+
+static int crypto_chacha20_crypt(struct skcipher_request *req)
+{
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+	struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
+	struct skcipher_walk walk;

crypto/chacha20_zinc.c line 37

Quoted 10 lines
crypto/chacha20_zinc.c new file +10 -0
+	simd_context_t simd_context;
+	int err, i;
+
+	err = skcipher_walk_virt(&walk, req, true);
+	if (unlikely(err))
+		return err;
+
+	for (i = 0; i < ARRAY_SIZE(ctx->counter); ++i)
+		ctx->counter[i] = get_unaligned_le32(walk.iv + i * sizeof(u32));
+

Multiple threads may use the same tfm concurrently, so the tfm context must not be used to store per-request information such as the IV.

- Eric

author maintainer
#104 lore raw

Re: [PATCH net-next v6 21/23] crypto: port ChaCha20 to Zinc

Hi Eric,

Quoted 2 lines · Eric Biggers

Multiple threads may use the same tfm concurrently, so the tfm context must not be used to store per-request information such as the IV.

Thanks, fixed for v7.

Jason

author maintainer
#105 lore raw

PATCH v6 22/23 security/keys: rewrite big_key crypto to use Zinc

In reply to Jason A. Donenfeld

A while back, I noticed that the crypto and crypto API usage in big_keys were entirely broken in multiple ways, so I rewrote it. Now, I'm rewriting it again, but this time using Zinc's ChaCha20Poly1305 function. This makes the file considerably more simple; the diffstat alone should justify this commit. It also should be faster, since it no longer requires a mutex around the "aead api object" (nor allocations), allowing us to encrypt multiple items in parallel. We also benefit from being able to pass any type of pointer to Zinc, so we can get rid of the ridiculously complex custom page allocator that big_key really doesn't need.

Cc Samuel Neves <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Greg KH <[email protected]>
Cc Jean-Philippe Aumasson <[email protected]>
Cc Eric Biggers <[email protected]>
Cc David Howells <[email protected]>
---
 security/keys/Kconfig   |   4 +-
 security/keys/big_key.c | 230 +++++-----------------------------------
 2 files changed, 28 insertions(+), 206 deletions(-)
security/keys/Kconfig +1 -3
index 6462e6654ccf..66ff26298fb3 100644
@@ -45,9 +45,7 @@ config BIG_KEYS
 	bool "Large payload keys"
 	depends on KEYS
 	depends on TMPFS
-	select CRYPTO
-	select CRYPTO_AES
-	select CRYPTO_GCM
+	select ZINC_CHACHA20POLY1305
 	help
 	  This option provides support for holding large keys within the kernel
 	  (for example Kerberos ticket caches).  The data may be stored out to
security/keys/big_key.c +27 -203

347 lines not on this page · raw message

Signature · 1 line
2.19.0
author maintainer
#106 lore raw

PATCH v6 23/23 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               |  18 +
 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              | 606 ++++++++++++++
 drivers/net/wireguard/netlink.h              |  12 +
 drivers/net/wireguard/noise.c                | 784 +++++++++++++++++++
 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              | 595 ++++++++++++++
 drivers/net/wireguard/selftest/allowedips.h  | 663 ++++++++++++++++
 drivers/net/wireguard/selftest/counter.h     | 103 +++
 drivers/net/wireguard/selftest/ratelimiter.h | 178 +++++
 drivers/net/wireguard/send.c                 | 420 ++++++++++
 drivers/net/wireguard/socket.c               | 432 ++++++++++
 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               | 190 +++++
 tools/testing/selftests/wireguard/netns.sh   | 499 ++++++++++++
 36 files changed, 7481 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 5967c737f3ce..32db7ebad86e 100644
@@ -15823,6 +15823,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 +18 -0
new file mode 100644
index 000000000000..d8856255bc9d
@@ -0,0 +1,18 @@
+ccflags-y := -O3
+ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt'
+ccflags-$(CONFIG_WIREGUARD_DEBUG) += -DDEBUG
+wireguard-y := main.o
+wireguard-y += noise.o
+wireguard-y += device.o
+wireguard-y += peer.o
+wireguard-y += timers.o
+wireguard-y += queueing.o
+wireguard-y += send.o
+wireguard-y += receive.o
+wireguard-y += socket.o
+wireguard-y += hashtables.o
+wireguard-y += allowedips.o
+wireguard-y += ratelimiter.o
+wireguard-y += cookie.o
+wireguard-y += 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 +606 -0

606 lines not on this page · raw message

drivers/net/wireguard/netlink.h new file +12 -0
new file mode 100644
index 000000000000..657fe1ad3786
@@ -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 +784 -0

587 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..83e420310c78

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

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

595 lines not on this page · raw message

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

663 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 +178 -0

178 lines not on this page · raw message

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

420 lines not on this page · raw message

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

432 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..50014d284668

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..fe8af5adc073

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

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

190 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.19.0

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

#107 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 5 lines · Jason A. Donenfeld

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

[]

Quoted 1 line

+enum { HANDSHAKE_DSCP = 0x88 /* AF41, plus 00 ECN */ };

[]

drivers/net/wireguard/receive.c line 394

Quoted 12 lines
drivers/net/wireguard/receive.c new file +12 -0
+	if (skb->protocol == htons(ETH_P_IP)) {
+		len = ntohs(ip_hdr(skb)->tot_len);
+		if (unlikely(len < sizeof(struct iphdr)))
+			goto dishonest_packet_size;
+		if (INET_ECN_is_ce(PACKET_CB(skb)->ds))
+			IP_ECN_set_ce(ip_hdr(skb));
+	} else if (skb->protocol == htons(ETH_P_IPV6)) {
+		len = ntohs(ipv6_hdr(skb)->payload_len) +
+		      sizeof(struct ipv6hdr);
+		if (INET_ECN_is_ce(PACKET_CB(skb)->ds))
+			IP6_ECN_set_ce(skb, ipv6_hdr(skb));
+	} else

[]

drivers/net/wireguard/send.c line 389

Quoted 9 lines
drivers/net/wireguard/send.c new file +9 -0
+	skb_queue_walk (&packets, skb) {
+		/* 0 for no outer TOS: no leak. TODO: should we use flowi->tos
+		 * as outer? */
+		PACKET_CB(skb)->ds = ip_tunnel_ecn_encap(0, ip_hdr(skb), skb);
+		PACKET_CB(skb)->nonce =
+				atomic64_inc_return(&key->counter.counter) - 1;
+		if (unlikely(PACKET_CB(skb)->nonce >= REJECT_AFTER_MESSAGES))
+			goto out_invalid;
+	}

Hi,

is there documentation and/or rationale for ecn handling? Quick search for ecn and dscp didn't reveal any.

Regards,
Ivan

author maintainer
#108 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Hi Ivan,

Quoted 32 lines · Ivan Labáth
Quoted 5 lines · Jason A. Donenfeld

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

[]

Quoted 1 line

+enum { HANDSHAKE_DSCP = 0x88 /* AF41, plus 00 ECN */ };

[]

drivers/net/wireguard/receive.c line 394

Quoted 12 lines
drivers/net/wireguard/receive.c new file +12 -0
+     if (skb->protocol == htons(ETH_P_IP)) {
+             len = ntohs(ip_hdr(skb)->tot_len);
+             if (unlikely(len < sizeof(struct iphdr)))
+                     goto dishonest_packet_size;
+             if (INET_ECN_is_ce(PACKET_CB(skb)->ds))
+                     IP_ECN_set_ce(ip_hdr(skb));
+     } else if (skb->protocol == htons(ETH_P_IPV6)) {
+             len = ntohs(ipv6_hdr(skb)->payload_len) +
+                   sizeof(struct ipv6hdr);
+             if (INET_ECN_is_ce(PACKET_CB(skb)->ds))
+                     IP6_ECN_set_ce(skb, ipv6_hdr(skb));
+     } else

[]

drivers/net/wireguard/send.c line 389

Quoted 9 lines
drivers/net/wireguard/send.c new file +9 -0
+     skb_queue_walk (&packets, skb) {
+             /* 0 for no outer TOS: no leak. TODO: should we use flowi->tos
+              * as outer? */
+             PACKET_CB(skb)->ds = ip_tunnel_ecn_encap(0, ip_hdr(skb), skb);
+             PACKET_CB(skb)->nonce =
+                             atomic64_inc_return(&key->counter.counter) - 1;
+             if (unlikely(PACKET_CB(skb)->nonce >= REJECT_AFTER_MESSAGES))
+                     goto out_invalid;
+     }
Quoted 4 lines

Hi,

is there documentation and/or rationale for ecn handling? Quick search for ecn and dscp didn't reveal any.

ECN support was developed with Dave Taht so that it does the right thing with CAKE and such. He's CC'd, so that he can fill in details, and sure, we can write these up. As well, I can add the rationale for the handshake-packet-specific DSCP value to the paper in the next few days; thanks for pointing out these documentation oversights.

Jason

#109 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 40 lines · Jason A. Donenfeld

Hi Ivan,

Quoted 32 lines · Ivan Labáth
Quoted 5 lines · Jason A. Donenfeld

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

[]

Quoted 1 line

+enum { HANDSHAKE_DSCP = 0x88 /* AF41, plus 00 ECN */ };

[]

drivers/net/wireguard/receive.c line 394

Quoted 12 lines
drivers/net/wireguard/receive.c new file +12 -0
+     if (skb->protocol == htons(ETH_P_IP)) {
+             len = ntohs(ip_hdr(skb)->tot_len);
+             if (unlikely(len < sizeof(struct iphdr)))
+                     goto dishonest_packet_size;
+             if (INET_ECN_is_ce(PACKET_CB(skb)->ds))
+                     IP_ECN_set_ce(ip_hdr(skb));
+     } else if (skb->protocol == htons(ETH_P_IPV6)) {
+             len = ntohs(ipv6_hdr(skb)->payload_len) +
+                   sizeof(struct ipv6hdr);
+             if (INET_ECN_is_ce(PACKET_CB(skb)->ds))
+                     IP6_ECN_set_ce(skb, ipv6_hdr(skb));
+     } else

[]

drivers/net/wireguard/send.c line 389

Quoted 9 lines
drivers/net/wireguard/send.c new file +9 -0
+     skb_queue_walk (&packets, skb) {
+             /* 0 for no outer TOS: no leak. TODO: should we use flowi->tos
+              * as outer? */
+             PACKET_CB(skb)->ds = ip_tunnel_ecn_encap(0, ip_hdr(skb), skb);
+             PACKET_CB(skb)->nonce =
+                             atomic64_inc_return(&key->counter.counter) - 1;
+             if (unlikely(PACKET_CB(skb)->nonce >= REJECT_AFTER_MESSAGES))
+                     goto out_invalid;
+     }
Quoted 4 lines

Hi,

is there documentation and/or rationale for ecn handling? Quick search for ecn and dscp didn't reveal any.

Quoted 8 lines

ECN support was developed with Dave Taht so that it does the right thing with CAKE and such. He's CC'd, so that he can fill in details, and sure, we can write these up. As well, I can add the rationale for the handshake-packet-specific DSCP value to the paper in the next few days; thanks for pointing out these documentation oversights.

Jason

Any news on this?

To be clear, question is not about an insignificant documentation oversight. It is about copying bits from inner packets to outer packets of a secure* tunnel and documenting it AFAICT nowhere, while claiming extensive documentation.

* it really should be specified what secure tunnel means, as it has many plausible interpretations and wireguard surely does not fulfill all of them.

Ivan

author maintainer
#110 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Hey Ivan,

Sorry for not getting back to you sooner.

Quoted 4 lines · Ivan Labáth

Any news on this?

To be clear, question is not about an insignificant documentation oversight. It is about copying bits from inner packets to outer packets

The short answer is RFC6040 with DSCP fixed to 0 so as not to leak anything. I've added a description of this to <wireguard.com/protocol/>.

Regards,
Jason

#111 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 6 lines · Jason A. Donenfeld

Hey Ivan,

Sorry for not getting back to you sooner.

On Mon, Nov 5, 2018 at 8:06 AM Ivan Labáth <[email protected]> wrote:

Quoted 8 lines
Quoted 4 lines

Any news on this?

To be clear, question is not about an insignificant documentation oversight. It is about copying bits from inner packets to outer packets

The short answer is RFC6040 with DSCP fixed to 0 so as not to leak anything. I've added a description of this to <wireguard.com/protocol/>.

you have a speling error (ECM). :)

side note:

I have to say that wireguard works really well with ecn and non-ecn marked flows against codel and fq_codel on the bottleneck router.

I'd still rather like it if wireguard focused a bit more on interleaving multiple flows better
rather than on single stream benchmarks, one day.

In this case, codel is managing things not fq and we could possibly shave a few ms of induced latency off of it in this particular test series:

http://tun.taht.net/~d/wireguard/rrul_-_comcast_v6.png

vs wireguard (doing it ivp6 over that ipv6)

http://tun.taht.net/~d/wireguard/rrul_-_wireguard.png

That said, I've been deploying wireguard widely in replacement of my old tinc network particularly on machines that were formerly cpu bottlenecked
and am insanely pleased with it. what's a few extra ms of latency between friends?

Quoted 3 lines

Regards,
Jason

Signature · 4 lines
Dave Täht
CTO, TekLibre, LLC
http://www.teklibre.com
Tel: 1-831-205-9740
author maintainer
#112 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 1 line · Dave Taht

you have a speling error (ECM). :)

Thanks.

Quoted 5 lines

side note:

I have to say that wireguard works really well with ecn and non-ecn marked flows against codel and fq_codel on the bottleneck router.

Yup!

Quoted 3 lines

I'd still rather like it if wireguard focused a bit more on interleaving multiple flows better
rather than on single stream benchmarks, one day.

We're working on it, actually. Toke has been running some tests on some beefy 100gbps hardware he recently acquired, and after v1 lands we'll probably have a few interesting ideas for this.

Jason

#113 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

In reply to Jason A. Donenfeld

Hi Jason

I know you have been concentrating on the crypto code, so i'm not expecting too many changes at the moment in the network code.

It would be good to further reduce the number of checkpatch warnings. The biggest problem i have at the moment is that all the trivial to fix warnings are hiding a few more important warnings. There are a few like these which are not obvious to see:

WARNING: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON() #2984: FILE: drivers/net/wireguard/noise.c:293:
+ BUG_ON(first_len > BLAKE2S_HASH_SIZE || second_len > BLAKE2S_HASH_SIZE ||

WARNING: Macros with flow control statements should be avoided #5471: FILE: drivers/net/wireguard/selftest/allowedips.h:456:
+#define init_peer(name) do { \

+               name = kzalloc(sizeof(*name), GFP_KERNEL);                 \
+               if (unlikely(!name)) {                                     \
+                       pr_info("allowedips self-test: out of memory\n");  \
+                       goto free;                                         \
+               }                                                          \
+               kref_init(&name->refcount);                                \

+ } while (0)

The namespace pollution also needs to be addresses. You have some pretty generic named global symbols. I picked out a few examples from objdump

00002a94 g     F .text	    00000060 peer_put
00003484 g     F .text	    0000004c timers_stop
00003520 g     F .text	    00000114 packet_queue_init
00002640 g     F .text	    00000034 device_uninit
000026bc g     F .text	    00000288 peer_create
000090d4 g     F .text	    000001bc ratelimiter_init

Please make use of a prefix for global symbols, e.g. wg_.

Andrew

author maintainer
#114 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Hi Andrew,

Thanks for following up with this.

Quoted 2 lines · Andrew Lunn

I know you have been concentrating on the crypto code, so i'm not expecting too many changes at the moment in the network code.

I should be addressing things in parallel, actually, so I'm happy to work on this.

Quoted 3 lines

WARNING: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON() #2984: FILE: drivers/net/wireguard/noise.c:293:
+ BUG_ON(first_len > BLAKE2S_HASH_SIZE || second_len > BLAKE2S_HASH_SIZE ||

I was actually going to ask you about this, because it applies similarly in another context too that I'm trying to refine. The above function you quote has the following properties:

- Only ever accepts fixed length parameters, so the compiler can constant fold invocations of it fantastically. Those parameters are fixed length in the sense that they're enum/macro constants. They never come from the user or from a packet or something.
- Never produces an incorrect result. For said constants, all inputs are valid, and so it succeeds in producing an output every time.
- Is a "pure" function, just knocking bytes around, without needing to interact with fancy kernel-y things; could be implemented on some sort of WWII-era rotor machine provided you had the patience.

Because of the above, there's never any error to return to the user of the function. Also, because it only ever takes constant sized inputs, in theory I should be able to change that BUG_ON() to BUILD_BUG_ON(), but in practice the optimizer/inliner isn't actually that aggressive.

But what I would like is some way of signaling to the developer using this function that they've passed it an illegal value, and their code should not ship until that's fixed, under any circumstances at all -- that their usage of the function is completely unacceptable and wrong. Bla bla strong statements.

For this, I figured the notion would come across with the aberrant behavior of "crash the developer's [in this case, my] QEMU instance" when "#ifdef DEBUG is true". This is the same kind of place where I'd have an "assert()" statement in userland. It sounds like what you're saying is that a WARN_ON is equally as effective instead? Or given the above, do you think the BUG_ON is actually sensible? Or neither and I should do something else?

drivers/net/wireguard/selftest/allowedips.h line 463

Quoted 10 lines

WARNING: Macros with flow control statements should be avoided #5471: FILE: drivers/net/wireguard/selftest/allowedips.h:456:

drivers/net/wireguard/selftest/allowedips.h new file +8 -0
+#define init_peer(name) do {                                               \
+               name = kzalloc(sizeof(*name), GFP_KERNEL);                 \
+               if (unlikely(!name)) {                                     \
+                       pr_info("allowedips self-test: out of memory\n");  \
+                       goto free;                                         \
+               }                                                          \
+               kref_init(&name->refcount);                                \
+       } while (0)

This is part of a debugging selftest, where I'm initing a bunch of peers one after another, and this macro helps keep the test clear while offloading the actual irrelevant coding part to this macro. The test itself then just has code like:

    init_peer(a);
    init_peer(b);
    init_peer(c);
    init_peer(d);
    init_peer(e);
    init_peer(f);
    init_peer(g);
    init_peer(h);

    insert(4, a, 192, 168, 4, 0, 24);
    insert(4, b, 192, 168, 4, 4, 32);
    insert(4, c, 192, 168, 0, 0, 16);
    insert(4, d, 192, 95, 5, 64, 27);
    /* replaces previous entry, and maskself is required */
    insert(4, c, 192, 95, 5, 65, 27);
    insert(6, d, 0x26075300, 0x60006b00, 0, 0xc05f0543, 128);
    insert(6, c, 0x26075300, 0x60006b00, 0, 0, 64);
    ...

And so forth. I can probably figure out a different way to code this if you really want, but I thought this would be clear.

Quoted 4 lines

The namespace pollution also needs to be addresses. You have some pretty generic named global symbols. I picked out a few examples from objdump

Quoted 8 lines
00002a94 g     F .text      00000060 peer_put
00003484 g     F .text      0000004c timers_stop
00003520 g     F .text      00000114 packet_queue_init
00002640 g     F .text      00000034 device_uninit
000026bc g     F .text      00000288 peer_create
000090d4 g     F .text      000001bc ratelimiter_init

Please make use of a prefix for global symbols, e.g. wg_.

Will do. v7 will include the wg_ prefix.

On a slightly related note, out of curiosity, any idea what's up with the future of LTO in the kernel? It sounds like that'd be nice to have on a module-by-module basis. IOW, I'd love to LTO all of my .c files in wireguard together, and then only ever expose mod_init/exit and whatever I explicitly EXPORT_SYMBOL, and then have the compiler and linker treat the rest of everything as essentially in one .c file and optimize the heck out of it, and then strip all the symbols. It would, incidentally, remove the need for said symbol prefixes, since I wouldn't be making anything global. (Probably perf/ftrace/etc would become harder to use though.)

Jason

author maintainer
#115 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 1 line · Jason A. Donenfeld

Will do. v7 will include the wg_ prefix.

$ nm *.o | while read a b c; do [[ $b == T ]] && echo $c; done | grep -v ^wg_
cleanup_module
init_module

Success.

#116 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

In reply to Jason A. Donenfeld
Quoted 11 lines · Jason A. Donenfeld

Hi Andrew,

Thanks for following up with this.

Quoted 2 lines · Andrew Lunn

I know you have been concentrating on the crypto code, so i'm not expecting too many changes at the moment in the network code.

I should be addressing things in parallel, actually, so I'm happy to work on this.

Quoted 7 lines
Quoted 3 lines

WARNING: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON() #2984: FILE: drivers/net/wireguard/noise.c:293:
+ BUG_ON(first_len > BLAKE2S_HASH_SIZE || second_len > BLAKE2S_HASH_SIZE ||

I was actually going to ask you about this, because it applies similarly in another context too that I'm trying to refine. The above function you quote has the following properties:

We have the above case, and we have the general case. The general case is, only use BUG_ON() when you know things have already gone bad and there is no recovery for the machine. I doubt that ever applies to wireguard. So i expect all the BUG_ON() to be removed. This is something which Linus keeps ranting about....

In this case:

You have it inside a #ifdef. Meaning you don't really care, you can keep going anyway if debugging is turned of. So just turn it into a WARN_ON() so you get the splat, but the kernel keeps running.

You have some other options. first_len, second_len, third_len are all parameter coming from #defines. As you suggested, you could do BUILD_BUG_ON(), but you have to do it at the caller. Which is fine, this is debug code, not user input validation code...

BUILD_BUG_ON(NOISE_HASH_LEN > BLAKE2S_HAS_SIZE) BUILD_BUG_ON(NOISE_SYMMETRIC_KEY_LEN > BLAKE2S_HAS_SIZE) BUILD_BUG_ON(NOISE_PUBLIC_KEY_LEN > BLAKE2S_HAS_SIZE) kdf(chaining_key, key, NULL, dh_calculation, NOISE_HASH_LEN,
NOISE_SYMMETRIC_KEY_LEN, 0, NOISE_PUBLIC_KEY_LEN, chaining_key);

Quoted 11 lines

drivers/net/wireguard/selftest/allowedips.h line 463

Quoted 10 lines

WARNING: Macros with flow control statements should be avoided #5471: FILE: drivers/net/wireguard/selftest/allowedips.h:456:

drivers/net/wireguard/selftest/allowedips.h new file +8 -0
+#define init_peer(name) do {                                               \
+               name = kzalloc(sizeof(*name), GFP_KERNEL);                 \
+               if (unlikely(!name)) {                                     \
+                       pr_info("allowedips self-test: out of memory\n");  \
+                       goto free;                                         \
+               }                                                          \
+               kref_init(&name->refcount);                                \
+       } while (0)
Quoted 4 lines

This is part of a debugging selftest, where I'm initing a bunch of peers one after another, and this macro helps keep the test clear while offloading the actual irrelevant coding part to this macro. The test itself then just has code like:

Please don't focus on just the examples i give you. Look at the bigger issue. We cannot see the woods for the trees. checkpatch is giving out lots of warnings. There are so many, it is hard to see the interesting ones from the simple ones where you need to add an extra space, or add missing {}.

If checkpatch was just issues one or two warnings about a goto in a macro, and nothing else, i probably would let it pass. This is bad, but it is not terrible. I personally have fallen into a trap caused by a goto in a macro. I changed the locking, not knowing about the goto, causing an error path not to unlock the lock. It took a while, but eventually the error happened, and soon after the machine deadlocked. At the time static checkers did not expand macros, so they did not detect the issue.

Quoted 2 lines

On a slightly related note, out of curiosity, any idea what's up with the future of LTO in the kernel?

Sorry, i've no idea. Not my corner of the kernel.

Quoted 4 lines

It sounds like that'd be nice to have
on a module-by-module basis. IOW, I'd love to LTO all of my .c files in wireguard together, and then only ever expose mod_init/exit and whatever I explicitly EXPORT_SYMBOL

The namespace is more than just about the linker. I see an Opps stack trace with wg_ symbols, i know i need to talk to Jason. Without any prefix, i have to go digging into the code to find out who i need to talk to. This is one reason why often every symbol has the prefix, not just the global scope ones. Go look at other code in driver/net. You will find that most drivers use a prefix everywhere, both local and global.

Andrew

author maintainer
#117 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 5 lines · Andrew Lunn

The namespace is more than just about the linker. I see an Opps stack trace with wg_ symbols, i know i need to talk to Jason. Without any prefix, i have to go digging into the code to find out who i need to talk to. This is one reason why often every symbol has the prefix, not just the global scope ones.

Good point. I'll see what the wg_ prefixing looks like for all the static functions too.

#118 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

In reply to Jason A. Donenfeld
Quoted 2 lines · 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.

...

Quoted 99 lines
---
 MAINTAINERS                                  |   8 +
 drivers/net/Kconfig                          |  30 +
 drivers/net/Makefile                         |   1 +
 drivers/net/wireguard/Makefile               |  18 +
 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              | 606 ++++++++++++++
 drivers/net/wireguard/netlink.h              |  12 +
 drivers/net/wireguard/noise.c                | 784 +++++++++++++++++++
 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              | 595 ++++++++++++++
 drivers/net/wireguard/selftest/allowedips.h  | 663 ++++++++++++++++
 drivers/net/wireguard/selftest/counter.h     | 103 +++
 drivers/net/wireguard/selftest/ratelimiter.h | 178 +++++
 drivers/net/wireguard/send.c                 | 420 ++++++++++
 drivers/net/wireguard/socket.c               | 432 ++++++++++
 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               | 190 +++++
 tools/testing/selftests/wireguard/netns.sh   | 499 ++++++++++++
 36 files changed, 7481 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 5967c737f3ce..32db7ebad86e 100644
@@ -15823,6 +15823,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 +0 -0
index d03775100f7d..aa631fe3b395 100644

drivers/net/Kconfig config DUMMY · line 75

Quoted 6 lines
drivers/net/Kconfig +3 -0
@@ -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

I think you need to add IPV6 here

drivers/net/Kconfig config DUMMY · line 81

Quoted 6 lines
drivers/net/Kconfig +6 -0
+       select NET_UDP_TUNNEL
+       select DST_CACHE
+       select ZINC_CHACHA20POLY1305
+       select ZINC_BLAKE2S
+       select ZINC_CURVE25519
+       default m

Please drop this - we usually leave it up to the defconfigs or distro configs to enable stuff like this.

Quoted 24 lines
drivers/net/Kconfig +21 -0
+       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---

...

author maintainer
#119 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Hi Ard,

Quoted 5 lines · Ard Biesheuvel

drivers/net/Kconfig config DUMMY · line 75

Quoted 3 lines
drivers/net/Kconfig +3 -0
+config WIREGUARD
+       tristate "WireGuard secure network tunnel"
+       depends on NET && INET

I think you need to add IPV6 here

Nope. Like much of the net tree, WireGuard can function on ipv6-less kernels. If you do find something in WireGuard isn't working in a v6-less configuration, I consider that to be a bug that needs fixing.

Jason

#120 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 7 lines · Jason A. Donenfeld

Hi Ard,

Quoted 4 lines · Ard Biesheuvel
Quoted 3 lines
drivers/net/Kconfig +3 -0
+config WIREGUARD
+       tristate "WireGuard secure network tunnel"
+       depends on NET && INET
Quoted 6 lines
Quoted 1 line

I think you need to add IPV6 here

Nope. Like much of the net tree, WireGuard can function on ipv6-less kernels. If you do find something in WireGuard isn't working in a v6-less configuration, I consider that to be a bug that needs fixing.

OK. I hit a build error yesterday, and setting CONFIG_IPV6 fixed it. Let me see if I can reproduce.

#121 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 10 lines · Ard Biesheuvel
Quoted 9 lines · Jason A. Donenfeld

Hi Ard,

Quoted 5 lines · Ard Biesheuvel

drivers/net/Kconfig config DUMMY · line 75

Quoted 3 lines
drivers/net/Kconfig +3 -0
+config WIREGUARD
+       tristate "WireGuard secure network tunnel"
+       depends on NET && INET

I think you need to add IPV6 here

Quoted 7 lines
Quoted 4 lines

Nope. Like much of the net tree, WireGuard can function on ipv6-less kernels. If you do find something in WireGuard isn't working in a v6-less configuration, I consider that to be a bug that needs fixing.

OK. I hit a build error yesterday, and setting CONFIG_IPV6 fixed it. Let me see if I can reproduce.

I get

drivers/net/wireguard/socket.o: In function `send6':
socket.c:(.text+0x56c): undefined reference to ipv6_chk_addr' drivers/net/wireguard/socket.o: In function wg_socket_send_skb_to_peer':
socket.c:(.text+0x904): undefined reference to ipv6_chk_addr' drivers/net/wireguard/socket.o: In function wg_socket_init':
socket.c:(.text+0x161c): undefined reference to `ipv6_mod_enabled'

if I build my kernel with WireGuard built in but IPv6 support enabled as a module.

author maintainer
#122 lore raw

Re: [PATCH net-next v6 23/23] net: WireGuard secure network tunnel

Quoted 24 lines · Ard Biesheuvel
Quoted 7 lines
Quoted 7 lines · Ard Biesheuvel
Quoted 5 lines

drivers/net/Kconfig config DUMMY · line 75

Quoted 3 lines
drivers/net/Kconfig +3 -0
+config WIREGUARD
+       tristate "WireGuard secure network tunnel"
+       depends on NET && INET

I think you need to add IPV6 here

Quoted 7 lines
Quoted 4 lines

Nope. Like much of the net tree, WireGuard can function on ipv6-less kernels. If you do find something in WireGuard isn't working in a v6-less configuration, I consider that to be a bug that needs fixing.

OK. I hit a build error yesterday, and setting CONFIG_IPV6 fixed it. Let me see if I can reproduce.

I get

drivers/net/wireguard/socket.o: In function `send6':
socket.c:(.text+0x56c): undefined reference to ipv6_chk_addr' drivers/net/wireguard/socket.o: In function wg_socket_send_skb_to_peer':
socket.c:(.text+0x904): undefined reference to ipv6_chk_addr' drivers/net/wireguard/socket.o: In function wg_socket_init':
socket.c:(.text+0x161c): undefined reference to `ipv6_mod_enabled'

Quoted 2 lines

if I build my kernel with WireGuard built in but IPv6 support enabled as a module.

Nice catch. Looks like the norm is adding "depends on IPV6 || !IPV6" or the like. I'll play and make sure this works for v7. Thanks for pointing it out.

Jason

#123 lore raw
In reply to Jason A. Donenfeld
Quoted 5 lines · Jason A. Donenfeld

It is intended that this entire patch series enter the kernel through DaveM's net-next tree. Subsequently, WireGuard patches will go through DaveM's net-next tree, while Zinc patches will go through Greg KH's tree.

Why is Herbert Xu's existing crypto tree being circumvented, especially for future patches (the initial merge isn't quite as important as that's a one-time event)? I like being able to check out cryptodev to test upcoming crypto patches. And currently, changes to APIs, algorithms, tests, and implementations all go through cryptodev, which is convenient for crypto developers.

Apparently, you're proposing that someone adding a new algorithm will now have to submit the API portion to one maintainer (Herbert Xu) and the implementation portion to another maintainer (you), and they'll go through separate git trees. That's inconvenient for developers, and it seems that in practice you and Herbert will be stepping on each other's toes a lot.

Can you please reach some kind of sane agreement with Herbert so that the development process isn't fractured into two? Perhaps you could review patches, but Herbert could still apply them?

I'm also wondering about the criteria for making additions and changes to "Zinc". You mentioned before that one of the "advantages" of Zinc is that it doesn't include "cipher modes from 90s cryptographers" -- what does that mean exactly? You've also indicated before that you don't want people modifying the Poly1305 implementations as they are too error-prone. Yet apparently it's fine to change them yourself, e.g. when you added the part that converts the accumulator from base 26 to base 32. I worry there may be double standards here, and useful contributions could be blocked or discouraged in the future. Can you please elaborate on your criteria for contributions to Zinc?

Also, will you allow algorithms that aren't up to modern security standards but are needed for compatibility reasons, e.g. MD5, SHA-1, and DES? There are existing standards, APIs, and data formats that use these "legacy" algorithms; so implementations of them are often still needed, whether we like it or not.

And does it matter who designed the algorithms, e.g. do algorithms from Daniel Bernstein get effectively a free pass, while algorithms from certain countries, governments, or organizations are not allowed? E.g. wireless driver developers may need the SM4 block cipher (which is now supported by the crypto API) as it's specified in a Chinese wireless standard. Will you allow SM4 in Zinc? Or will people have to submit some algorithms to Herbert and some to you due to disagreements about what algorithms should be included?

- Eric

author maintainer
#124 lore raw

Hi Eric,

Quoted 12 lines · Eric Biggers

Why is Herbert Xu's existing crypto tree being circumvented, especially for future patches (the initial merge isn't quite as important as that's a one-time event)? I like being able to check out cryptodev to test upcoming crypto patches. And currently, changes to APIs, algorithms, tests, and implementations all go through cryptodev, which is convenient for crypto developers.

Apparently, you're proposing that someone adding a new algorithm will now have to submit the API portion to one maintainer (Herbert Xu) and the implementation portion to another maintainer (you), and they'll go through separate git trees. That's inconvenient for developers, and it seems that in practice you and Herbert will be stepping on each other's toes a lot.

Quoted 3 lines

Can you please reach some kind of sane agreement with Herbert so that the development process isn't fractured into two? Perhaps you could review patches, but Herbert could still apply them?

I think you're overthinking it a bit. Zinc will have a few software implementations of primitives that are useful in cases where it's nice to call the primitive directly. Think: various usages of sha2, siphash, the wireguard suite (what this patchset includes), other things in lib/, etc. In so much as this winds up duplicating things within the crypto API, I'll work with Herbert to build one on top of the other -- as I've done in the two commits in this series. But beyond that, think of the two initiatives as orthogonal. I'm working on curating a few primitives that are maximally useful throughout the kernel for various uses, and doing so in a way that I think brings about a certain quality. Meanwhile the crypto API is amassing a huge collection of primitives for some things, and that will continue to exist, and Herbert will continue to maintain that. I expect for the crossover to be fairly isolated and manageable, without too much foreseeable tree- conflicts and such. Therefore, Samuel Neves and I plan to maintain the codebase we've spent quite some time writing, and maintain our own tree for it, which we'll be submitting through Greg. In other words, this is not a matter of "circumvention" or "stepping on toes", but rather separate efforts. I'm quite certain to the extent they overlap we'll be able to work out fairly easily.

Either way, I'll take your suggestion and reach out to Herbert, since at least a discussion between the two of us sounds like it could be productive.

Quoted 13 lines

I'm also wondering about the criteria for making additions and changes to "Zinc". You mentioned before that one of the "advantages" of Zinc is that it doesn't include "cipher modes from 90s cryptographers" -- what does that mean exactly? You've also indicated before that you don't want people modifying the Poly1305 implementations as they are too error-prone. Useful contributions could be blocked or discouraged in the future. Can you please elaborate on your criteria for contributions to Zinc?

Also, will you allow algorithms that aren't up to modern security standards but are needed for compatibility reasons, e.g. MD5, SHA-1, and DES? There are existing standards, APIs, and data formats that use these "legacy" algorithms; so implementations of them are often still needed, whether we like it or not.

Quoted 7 lines

And does it matter who designed the algorithms, e.g. do algorithms from Daniel Bernstein get effectively a free pass, while algorithms from certain countries, governments, or organizations are not allowed? E.g. wireless driver developers may need the SM4 block cipher (which is now supported by the crypto API) as it's specified in a Chinese wireless standard. Will you allow SM4 in Zinc? Or will people have to submit some algorithms to Herbert and some to you due to disagreements about what algorithms should be included?

Similarly here, I think you're over-politicizing everything. Stable address generation for IPv6 uses SHA1 -- see net/ipv6/addrconf.c:3203 -- do you think that this should use, say, the SM3 chinese hash function instead? No, of course not, for a variety of interesting reasons. Rather, it should use some simple hash function that's fast in software that we have available in Zinc. On the other hand, it seems like parts of the kernel that have pretty high- levels of cipher agility -- such as dmcrypt, ipsec, wifi apparently, and so on -- will continue to use dynamic-dispatch system like the crypto API, since that's what it was made to do and is effective at doing. And so, your example of SM4 seems to fit perfectly into what the crypto API is well-suited for, and it would fit naturally in there.

In other words, the "political criteria" for what we add to lib/zinc/ will mostly be the same as for the rest of lib/: are there things using it that benefit from it being there in a direct and obvious way, and does the implementation meet certain quality standards.

Quoted 3 lines

to change them yourself, e.g. when you added the part that converts the accumulator from base 26 to base 32. I worry there may be double standards here

We do actually appreciate your concern here. However, there's a lot more that went into that short patch than meets the eye:

- It matches exactly what Andy Polyakov's code is doing for the exact same reason, so this isn't something that's actually "new". (There are paths inside his implementation that branch from the vector code to the scalar code.)
- It has been discussed at length with Andy, including what kinds of proofs we'll need if we want to chop it down further (to remove that final reduction), and why we both don't want to do that yet, and so we go with the full carrying for the avoidance of risk.
- We've proved its correctness with Z3, actually using an even looser constraint on digit size than what Andy mentioned to us, thus resulting in a stronger proof result. So we're certain this isn't rubbish.
- There's been some considerable computing power sunk into fuzzing it.

There's no doubt about it, we've done our due-diligence here. This is in fact the kind of efforts we require of submissions. You could fault us for not detailing this in "the commit message" -- except as this is still a patch series, we're putting improvements into the 00/XX change log, instead of adding fixes and additions on top of the series. Of course in the ordinary course of kernel development, this would exist instead as a standalone commit. If you have a better idea of how this kind of thing can be communicated, and where precisely, in the pre-merge process, I'd be interested in hearing suggestions.

Thanks,
Jason

#125 lore raw
Quoted 39 lines · Jason A. Donenfeld

Hi Eric,

Quoted 12 lines · Eric Biggers

Why is Herbert Xu's existing crypto tree being circumvented, especially for future patches (the initial merge isn't quite as important as that's a one-time event)? I like being able to check out cryptodev to test upcoming crypto patches. And currently, changes to APIs, algorithms, tests, and implementations all go through cryptodev, which is convenient for crypto developers.

Apparently, you're proposing that someone adding a new algorithm will now have to submit the API portion to one maintainer (Herbert Xu) and the implementation portion to another maintainer (you), and they'll go through separate git trees. That's inconvenient for developers, and it seems that in practice you and Herbert will be stepping on each other's toes a lot.

Quoted 3 lines

Can you please reach some kind of sane agreement with Herbert so that the development process isn't fractured into two? Perhaps you could review patches, but Herbert could still apply them?

I think you're overthinking it a bit. Zinc will have a few software implementations of primitives that are useful in cases where it's nice to call the primitive directly. Think: various usages of sha2, siphash, the wireguard suite (what this patchset includes), other things in lib/, etc. In so much as this winds up duplicating things within the crypto API, I'll work with Herbert to build one on top of the other -- as I've done in the two commits in this series. But beyond that, think of the two initiatives as orthogonal. I'm working on curating a few primitives that are maximally useful throughout the kernel for various uses, and doing so in a way that I think brings about a certain quality. Meanwhile the crypto API is amassing a huge collection of primitives for some things, and that will continue to exist, and Herbert will continue to maintain that. I expect for the crossover to be fairly isolated and manageable, without too much foreseeable tree- conflicts and such. Therefore, Samuel Neves and I plan to maintain the codebase we've spent quite some time writing, and maintain our own tree for it, which we'll be submitting through Greg. In other words, this is not a matter of "circumvention" or "stepping on toes", but rather separate efforts. I'm quite certain to the extent they overlap we'll be able to work out fairly easily.

Quoted 2 lines

Either way, I'll take your suggestion and reach out to Herbert, since at least a discussion between the two of us sounds like it could be productive.

So, Zinc will simultaneously replace the current crypto implementations, *and* be "orthogonal" and "separate" from all the crypto code currently maintained by Herbert? You can't have your cake and eat it too...

I'm still concerned you're splitting the community in two. It will be unclear where new algorithms and implementations should go. Some people will choose Herbert and the current crypto API and conventions, and some people will choose you and Zinc... I still don't see clear guidelines for what will go where. And yes, you and Herbert will step on each others' toes and duplicate stuff, as the efforts are *not* separate, as you've even argued yourself.

Please reach out to Herbert to find a sane solution, ideally one that involves having a single git tree for crypto development and allows people to continue crypto development without choosing "sides".

Quoted 34 lines
Quoted 14 lines

I'm also wondering about the criteria for making additions and changes to "Zinc". You mentioned before that one of the "advantages" of Zinc is that it doesn't include "cipher modes from 90s cryptographers" -- what does that mean exactly? You've also indicated before that you don't want people modifying the Poly1305 implementations as they are too error-prone. Useful contributions could be blocked or discouraged in the future. Can you please elaborate on your criteria for contributions to Zinc?

Also, will you allow algorithms that aren't up to modern security standards but are needed for compatibility reasons, e.g. MD5, SHA-1, and DES? There are existing standards, APIs, and data formats that use these "legacy" algorithms; so implementations of them are often still needed, whether we like it or not.

Quoted 7 lines

And does it matter who designed the algorithms, e.g. do algorithms from Daniel Bernstein get effectively a free pass, while algorithms from certain countries, governments, or organizations are not allowed? E.g. wireless driver developers may need the SM4 block cipher (which is now supported by the crypto API) as it's specified in a Chinese wireless standard. Will you allow SM4 in Zinc? Or will people have to submit some algorithms to Herbert and some to you due to disagreements about what algorithms should be included?

Similarly here, I think you're over-politicizing everything. Stable address generation for IPv6 uses SHA1 -- see net/ipv6/addrconf.c:3203 -- do you think that this should use, say, the SM3 chinese hash function instead? No, of course not, for a variety of interesting reasons. Rather, it should use some simple hash function that's fast in software that we have available in Zinc. On the other hand, it seems like parts of the kernel that have pretty high- levels of cipher agility -- such as dmcrypt, ipsec, wifi apparently, and so on -- will continue to use dynamic-dispatch system like the crypto API, since that's what it was made to do and is effective at doing. And so, your example of SM4 seems to fit perfectly into what the crypto API is well-suited for, and it would fit naturally in there.

Quoted 5 lines

In other words, the "political criteria" for what we add to lib/zinc/ will mostly be the same as for the rest of lib/: are there things using it that benefit from it being there in a direct and obvious way, and does the implementation meet certain quality standards.

So, crypto implementations and algorithms will go to different maintainers, source locations, and git trees based purely on whether the current users need "cipher agility"? Note that usage can change over time; a user that requires a single cipher could later need multiple, and vice versa.

What if the portion of a wireless driver that needs SM4 doesn't need any other cipher in the same place, so static dispatch would suffice? Would SM4 be allowed in Zinc then?

Quoted 11 lines
Quoted 3 lines

to change them yourself, e.g. when you added the part that converts the accumulator from base 26 to base 32. I worry there may be double standards here

We do actually appreciate your concern here. However, there's a lot more that went into that short patch than meets the eye:

- It matches exactly what Andy Polyakov's code is doing for the exact same reason, so this isn't something that's actually "new". (There are paths inside his implementation that branch from the vector code to the scalar code.)

Matches Andy's code, where? The reason you had to add the radix conversion is because his code does *not* handle it...

Quoted 4 lines

- It has been discussed at length with Andy, including what kinds of proofs we'll need if we want to chop it down further (to remove that final reduction), and why we both don't want to do that yet, and so we go with the full carrying for the avoidance of risk.

Sorry, other people don't know about your private discussions. For the rest of us, why not add a comment to the code explaining what's going on?

Quoted 3 lines

- We've proved its correctness with Z3, actually using an even looser constraint on digit size than what Andy mentioned to us, thus resulting in a stronger proof result. So we're certain this isn't rubbish.

AFAICS actually it *is* rubbish, because your C code stores the accumulator as 64-bit integers whereas the asm code (at least, the 32-bit version) reads it as 32-bit integers. That won't work correctly on big endian ARM.

Quoted 3 lines

- There's been some considerable computing power sunk into fuzzing it.

There's no doubt about it, we've done our due-diligence here.

Apparently not, given that it's broken on big endian ARM.

Quoted 4 lines

fact the kind of efforts we require of submissions. You could fault us for not detailing this in "the commit message" -- except as this is still a patch series, we're putting improvements into the 00/XX change log, instead of adding fixes and additions on top of the series.

The details of the correctness proofs and fuzzing you claim to have done aren't explained, even in the cover letter; so for now we just have to trust you on that point. Of course, having bugs in code which you insist was proven correct
+ fuzzed doesn't exactly inspire trust.

I understand that your standards are still as high or even higher than Herbert's, which is good; crypto code should be held to high standards! But based on the evidence, I do worry there's a double standard going on where you get away with things yourself which you won't allow from others in Zinc. It's just not honest, and it will make people not want to contribute to Zinc. Maintainers are supposed to be unbiased and hold all contributions to the same standard.

We need "Zinc" to be Linux's crypto library, not "Jason's crypto library".

- Eric

author maintainer
#126 lore raw

Hi Eric,

Quoted 3 lines · Eric Biggers

So, Zinc will simultaneously replace the current crypto implementations, *and* be "orthogonal" and "separate" from all the crypto code currently maintained by Herbert? You can't have your cake and eat it too...

The plan is for it to replace many uses of the crypto API where it makes sense, but not replace uses where it doesn't make sense. Perhaps in the long run, over time, its usage will grow to cover those cases too, or, perhaps instead, Zinc will form a simple basis of software crypto algorithms in whatever future API designs crop up. In other words, like most changes in kernel development, things happen gradually, starting with a few good cases, and gradually growing as the need and design arise.

Quoted 4 lines

I'm still concerned you're splitting the community in two. It will be unclear where new algorithms and implementations should go. Some people will choose Herbert and the current crypto API and conventions, and some people will choose you and Zinc... I still don't see clear guidelines for what will go where.

I can try to work out some explicit guidelines and write these up for Documentation/, if that'd make a difference for you. I don't think this is a matter of "community splitting". On the contrary, I think Zinc will bring communities together, inviting the larger cryptography community to take an interest in improving the state of crypto in Linux. Either way, the litmus test for where code should go remains pretty similar to how it's been working so far. Are you tempted to stick it in lib/ because that fits your programming paradigm and because you think it's generally useful? If so, submit it to lib/zinc/. Conversely, is it only something used in the large array of options provided by dmcrypt, ipsec, afalg, etc? Submit it to the crypto API.

If you think this criteria is lacking, I'm amenable to adjusting that and changing it, especially as situations and designs change and morph over time. But that seems like a fairly decent starting point.

Quoted 2 lines

Please reach out to Herbert to find a sane solution crypto development without choosing "sides".

Please, don't politicize this. This has nothing to do with "sides". This has to do with which paradigm makes sense for implementing a particular algorithm. And everything that goes in Zinc gets to be used seamlessly by the crypto API anyway, through use of the trivial stub glue code, like what I've shown in the two commits in this series. Again, if it's something that will work well as a direct function call, then it seems like Zinc makes sense as a home for it.

With that said, I've reached out to Herbert, and we'll of course discuss and reach a good conclusion together.

Quoted 2 lines

Note that usage can change over time; a user that requires a single cipher could later need multiple, and vice versa.

I think this depends on the design of the driver and the style it's implemented in. For example, I could imagine something like this:

   encrypt_stuff_with_morus(obj, key);

evolving over time to:

  if (obj->type == MORUS_TYPE)
    encrypt_stuff_with_morus(obj, key);
  else if (obj->type == AEGIS_TYPE)
    encrypt_stuff_with_aegis(obj, key);

On the other hand, if the developer has good reason to use the crypto API's dynamic dispatch and async API and so forth, then perhaps it just changes from:

  static const char *cipher_name = "morus";

to

  static const char *cipher_name_type_1 = "morus";
  static const char *cipher_name_type_2 = "aegis";

I can imagine both programming styles and evolutions being desirable for different reasons.

Quoted 7 lines
Quoted 4 lines

- It matches exactly what Andy Polyakov's code is doing for the exact same reason, so this isn't something that's actually "new". (There are paths inside his implementation that branch from the vector code to the scalar code.)

Matches Andy's code, where? The reason you had to add the radix conversion is because his code does *not* handle it...

For example, check out the avx blocks function. The radix conversion happens in a few different places throughout. The reason we need it separately here is because, unlike userspace, it's possible the kernel code will transition from 2^26 back to 2^64 as a result of the FPU context changing.

As well, AndyP seems to like the idea of including this logic in the assembly instead of in C, if I understood our discussions correctly, so there's a decent chance this will migrate out of the glue code and into the assembly properly, which is probably a better place for it.

Quoted 7 lines
Quoted 4 lines

- It has been discussed at length with Andy, including what kinds of proofs we'll need if we want to chop it down further (to remove that final reduction), and why we both don't want to do that yet, and so we go with the full carrying for the avoidance of risk.

Sorry, other people don't know about your private discussions. For the rest of us, why not add a comment to the code explaining what's going on?

That's a good idea. I can include some discussion about this as well in the commit message that introduces the glue code, too, I guess? I've been hesitant to fill these commit messages up even more, given there are already so many walls of text and whatnot, but if you think that'd be useful, I'll do that for v7, and also add comments.

Quoted 10 lines
Quoted 3 lines

- We've proved its correctness with Z3, actually using an even looser constraint on digit size than what Andy mentioned to us, thus resulting in a stronger proof result. So we're certain this isn't rubbish.

AFAICS actually it *is* rubbish, because your C code stores the accumulator as 64-bit integers whereas the asm code (at least, the 32-bit version) reads it as 32-bit integers. That won't work correctly on big endian ARM.

Quoted 1 line

There's no doubt about it, we've done our due-diligence here.

Apparently not, given that it's broken on big endian ARM. Of course, having bugs in code which you insist was proven correct
+ fuzzed doesn't exactly inspire trust.

What's with the snark? It's not rubbish. I'm not sure if you noticed it in the development trees (both the WireGuard module tree and my kernel.org integration tree for this patch), but the big endian ARM support was fixed pretty shortly after I jumped the gun posting v6. Like, super soon after. That, and other big endian fixes (on aarch64 as well) are already queued up for v7. And now build.wireguard.com has more big endian running in CI.

Quoted 3 lines

The details of the correctness proofs and fuzzing you claim to have done aren't explained, even in the cover letter; so for now we just have to trust you on that point.

"Claim to have done", "trust you on that point" -- I think there's no reason to doubt the integrity of my "claims", and I don't appreciate the phrasing that appears to call that into question.

Regardless, sure, we can expand the "wall-of-text" commit messages even further, if you want, and include the verbatim Z3 scripts for reproduction.

Quoted 7 lines

I understand that your standards are still as high or even higher than Herbert's, which is good; crypto code should be held to high standards! But based on the evidence, I do worry there's a double standard going on where you get away with things yourself which you won't allow from others in Zinc. It's just not honest, and it will make people not want to contribute to Zinc. Maintainers are supposed to be unbiased and hold all contributions to the same standard.

This is complete and utter garbage, and I find its insinuations insulting and ridiculous. There is absolutely no lack of honesty and no double standard being applied whatsoever. Your attempt to cast doubt about the quality of standards applied and the integrity of the process is wholly inappropriate. When I tell you that high standards were applied and that due-diligence was done in developing a particular patch, I mean what I say.

Quoted 1 line

We need "Zinc" to be Linux's crypto library, not "Jason's crypto library".

This very much is a project directed toward the benefit of the kernel in a general sense. It's been this way from the start, and there's nothing in its goals or plans to the contrary of that. Please leave this vague and unproductive rhetoric aside.

Jason

#127 lore raw

Hi Jason,

Quoted 7 lines · Jason A. Donenfeld

Hi Eric,

Quoted 3 lines · Eric Biggers

So, Zinc will simultaneously replace the current crypto implementations, *and* be "orthogonal" and "separate" from all the crypto code currently maintained by Herbert? You can't have your cake and eat it too...

Quoted 8 lines

The plan is for it to replace many uses of the crypto API where it makes sense, but not replace uses where it doesn't make sense. Perhaps in the long run, over time, its usage will grow to cover those cases too, or, perhaps instead, Zinc will form a simple basis of software crypto algorithms in whatever future API designs crop up. In other words, like most changes in kernel development, things happen gradually, starting with a few good cases, and gradually growing as the need and design arise.

Please re-read what I wrote. Here I'm talking about the crypto code itself, not its users.

And you still haven't answered my question about adding a new algorithm that is useful to have in both APIs. You're proposing that in most cases the crypto API part will need to go through Herbert while the implementation will need to go through you/Greg, right? Or will you/Greg be taking both? Or will Herbert take both?

Quoted 18 lines
Quoted 5 lines

I'm still concerned you're splitting the community in two. It will be unclear where new algorithms and implementations should go. Some people will choose Herbert and the current crypto API and conventions, and some people will choose you and Zinc... I still don't see clear guidelines for what will go where.

I can try to work out some explicit guidelines and write these up for Documentation/, if that'd make a difference for you. I don't think this is a matter of "community splitting". On the contrary, I think Zinc will bring communities together, inviting the larger cryptography community to take an interest in improving the state of crypto in Linux. Either way, the litmus test for where code should go remains pretty similar to how it's been working so far. Are you tempted to stick it in lib/ because that fits your programming paradigm and because you think it's generally useful? If so, submit it to lib/zinc/. Conversely, is it only something used in the large array of options provided by dmcrypt, ipsec, afalg, etc? Submit it to the crypto API.

Quoted 3 lines

If you think this criteria is lacking, I'm amenable to adjusting that and changing it, especially as situations and designs change and morph over time. But that seems like a fairly decent starting point.

A documentation file for Zinc is a good idea. Some of the information in your commit messages should be moved there too.

Quoted 7 lines
Quoted 3 lines

Please reach out to Herbert to find a sane solution crypto development without choosing "sides".

Please, don't politicize this. This has nothing to do with "sides". This has to do with which paradigm makes sense for implementing a particular algorithm.

I'm not trying to "politicize" this, but rather determine your criteria for including algorithms in Zinc, which you haven't made clear. Since for the second time you've avoided answering my question about whether you'd allow the SM4 cipher in Zinc, and you designed WireGuard to be "cryptographically opinionated", and you were one of the loudest voices calling for the Speck cipher to be removed, and your justification for Zinc complains about cipher modes from "90s cryptographers", I think it's reasonable for people to wonder whether as the Zinc (i.e. Linux crypto library) maintainer you will restrict the inclusion of crypto algorithms to the ones you prefer, rather than the ones that users need. Note that the kernel is used by people all over the world and needs to support various standards, protocols, and APIs that use different crypto algorithms, not always the ones we'd like; and different users have different preferences. People need to know whether the Linux kernel's crypto library will meet (or be allowed to meet) their crypto needs.

Quoted 43 lines

And everything that goes in Zinc gets to be used seamlessly by the crypto API anyway, through use of the trivial stub glue code, like what I've shown in the two commits in this series. Again, if it's something that will work well as a direct function call, then it seems like Zinc makes sense as a home for it.

With that said, I've reached out to Herbert, and we'll of course discuss and reach a good conclusion together.

Quoted 2 lines

Note that usage can change over time; a user that requires a single cipher could later need multiple, and vice versa.

I think this depends on the design of the driver and the style it's implemented in. For example, I could imagine something like this:

   encrypt_stuff_with_morus(obj, key);

evolving over time to:

  if (obj->type == MORUS_TYPE)
    encrypt_stuff_with_morus(obj, key);
  else if (obj->type == AEGIS_TYPE)
    encrypt_stuff_with_aegis(obj, key);

On the other hand, if the developer has good reason to use the crypto API's dynamic dispatch and async API and so forth, then perhaps it just changes from:

  static const char *cipher_name = "morus";

to

  static const char *cipher_name_type_1 = "morus";
  static const char *cipher_name_type_2 = "aegis";

I can imagine both programming styles and evolutions being desirable for different reasons.

Quoted 5 lines
Quoted 4 lines

- It matches exactly what Andy Polyakov's code is doing for the exact same reason, so this isn't something that's actually "new". (There are paths inside his implementation that branch from the vector code to the scalar code.)

Quoted 8 lines
Quoted 2 lines

Matches Andy's code, where? The reason you had to add the radix conversion is because his code does *not* handle it...

For example, check out the avx blocks function. The radix conversion happens in a few different places throughout. The reason we need it separately here is because, unlike userspace, it's possible the kernel code will transition from 2^26 back to 2^64 as a result of the FPU context changing.

IOW, you had to rewrite the x86 assembly algorithm in C and make it use a different Poly1305 context format. That's a major change, where bugs can be introduced -- and at least one was introduced, in fact. I'd appreciate it if you were more accurate in describing your modifications and the potential risks involved.

And yes I know why converting radixes is needed, as I had pointed it out...

Quoted 11 lines

As well, AndyP seems to like the idea of including this logic in the assembly instead of in C, if I understood our discussions correctly, so there's a decent chance this will migrate out of the glue code and into the assembly properly, which is probably a better place for it.

Quoted 5 lines
Quoted 4 lines

- It has been discussed at length with Andy, including what kinds of proofs we'll need if we want to chop it down further (to remove that final reduction), and why we both don't want to do that yet, and so we go with the full carrying for the avoidance of risk.

Quoted 8 lines
Quoted 2 lines

Sorry, other people don't know about your private discussions. For the rest of us, why not add a comment to the code explaining what's going on?

That's a good idea. I can include some discussion about this as well in the commit message that introduces the glue code, too, I guess? I've been hesitant to fill these commit messages up even more, given there are already so many walls of text and whatnot, but if you think that'd be useful, I'll do that for v7, and also add comments.

Please prefer to put information in the code or documentation rather than in commit messages, when appropriate.

Quoted 38 lines
Quoted 11 lines
Quoted 4 lines

- We've proved its correctness with Z3, actually using an even looser constraint on digit size than what Andy mentioned to us, thus resulting in a stronger proof result. So we're certain this isn't rubbish.

AFAICS actually it *is* rubbish, because your C code stores the accumulator as 64-bit integers whereas the asm code (at least, the 32-bit version) reads it as 32-bit integers. That won't work correctly on big endian ARM.

Quoted 1 line

There's no doubt about it, we've done our due-diligence here.

Apparently not, given that it's broken on big endian ARM. Of course, having bugs in code which you insist was proven correct
+ fuzzed doesn't exactly inspire trust.

What's with the snark? It's not rubbish. I'm not sure if you noticed it in the development trees (both the WireGuard module tree and my kernel.org integration tree for this patch), but the big endian ARM support was fixed pretty shortly after I jumped the gun posting v6. Like, super soon after. That, and other big endian fixes (on aarch64 as well) are already queued up for v7. And now build.wireguard.com has more big endian running in CI.

Quoted 3 lines

The details of the correctness proofs and fuzzing you claim to have done aren't explained, even in the cover letter; so for now we just have to trust you on that point.

"Claim to have done", "trust you on that point" -- I think there's no reason to doubt the integrity of my "claims", and I don't appreciate the phrasing that appears to call that into question.

Regardless, sure, we can expand the "wall-of-text" commit messages even further, if you want, and include the verbatim Z3 scripts for reproduction.

Quoted 7 lines

I understand that your standards are still as high or even higher than Herbert's, which is good; crypto code should be held to high standards! But based on the evidence, I do worry there's a double standard going on where you get away with things yourself which you won't allow from others in Zinc. It's just not honest, and it will make people not want to contribute to Zinc. Maintainers are supposed to be unbiased and hold all contributions to the same standard.

Quoted 7 lines

This is complete and utter garbage, and I find its insinuations insulting and ridiculous. There is absolutely no lack of honesty and no double standard being applied whatsoever. Your attempt to cast doubt about the quality of standards applied and the integrity of the process is wholly inappropriate. When I tell you that high standards were applied and that due-diligence was done in developing a particular patch, I mean what I say.

No, I was not aware of the fixed version. I found the bug myself reading the latest patchset you've mailed out, v6. It's great that you found and fixed this bug on your own, and of course that raises my level of confidence in your work. Still, the v6 patchset still includes your claim that "All implementations have been extensively tested and fuzzed"; so that claim was objectively wrong. So I disagree that my concerns are "complete and utter garbage". And I think that the fact that you prefer to respond by attacking me, rather than committing to be more accurate in your claims and to treat all contributions fairly, is problematic.

Also, if you have extra tests that you're running, it would be great if you could include them as part of the submission so that others can run them too.

Quoted 8 lines
Quoted 2 lines

We need "Zinc" to be Linux's crypto library, not "Jason's crypto library".

This very much is a project directed toward the benefit of the kernel in a general sense. It's been this way from the start, and there's nothing in its goals or plans to the contrary of that. Please leave this vague and unproductive rhetoric aside.

Well, it doesn't help that you yourself claim that Zinc stands for "Zx2c4's INsane Cryptolib"...

- Eric

author maintainer
#128 lore raw

Hi Eric,

Quoted 5 lines · Eric Biggers

And you still haven't answered my question about adding a new algorithm that is useful to have in both APIs. You're proposing that in most cases the crypto API part will need to go through Herbert while the implementation will need to go through you/Greg, right? Or will you/Greg be taking both? Or will Herbert take both?

If an implementation enters Zinc, it will go through my tree. If it enters the crypto API, it will go through Herbert's tree. If there wind up being messy tree dependency and merge timing issues, I can work this out in the usual way with Herbert. I'll be sure to discuss these edge cases with him when we discuss. I think there's a way to handle that with minimum friction.

Quoted 2 lines

A documentation file for Zinc is a good idea. Some of the information in your commit messages should be moved there too.

Will do.

Quoted 6 lines

I'm not trying to "politicize" this, but rather determine your criteria for including algorithms in Zinc, which you haven't made clear. Since for the second time you've avoided answering my question about whether you'd allow the SM4 cipher in Zinc, and you designed WireGuard to be "cryptographically opinionated", and you were one of the loudest voices calling for the Speck cipher to be removed, and your justification for Zinc complains about cipher

Quoted 8 lines

modes from "90s cryptographers", I think it's reasonable for people to wonder whether as the Zinc (i.e. Linux crypto library) maintainer you will restrict the inclusion of crypto algorithms to the ones you prefer, rather than the ones that users need. Note that the kernel is used by people all over the world and needs to support various standards, protocols, and APIs that use different crypto algorithms, not always the ones we'd like; and different users have different preferences. People need to know whether the Linux kernel's crypto library will meet (or be allowed to meet) their crypto needs.

WireGuard is indeed quite opinionated in its primitive choices, but I don't think it'd be wise to apply the same design to Zinc. There are APIs where the goal is to have a limited set of high-level functions, and have those implemented in only a preferred set of primitives. NaCl is a good example of this -- functions like "crypto_secretbox" that are actually xsalsapoly under the hood. Zinc doesn't intend to become an API like those, but rather to provide the actual primitives for use cases where that specific primitive is used. Sometimes the kernel is in the business of being able to pick its own crypto -- random.c, tcp sequence numbers, big_key.c, etc -- but most of the time the kernel is in the business of implementing other people's crypto, for specific devices/protocols/diskformats. And for those use cases we need not some high-level API like NaCl, but rather direct access to the primitives that are required to implement those drivers. WireGuard is one such example, but so would be Bluetooth, CIFS/SMB, WiFi, and so on. We're in the business of writing drivers, after all. So, no, I don't think I'd knock down the addition of a primitive because of a simple preference for a different primitive, if it was clearly the case that the driver requiring it really benefited from having accessible via the plain Zinc function calls. Sorry if I hadn't made this clear earlier -- I thought Ard had asked more or less the same thing about DES and I answered accordingly, but maybe that wasn't made clear enough there.

Quoted 11 lines
Quoted 5 lines

For example, check out the avx blocks function. The radix conversion happens in a few different places throughout. The reason we need it separately here is because, unlike userspace, it's possible the kernel code will transition from 2^26 back to 2^64 as a result of the FPU context changing.

IOW, you had to rewrite the x86 assembly algorithm in C and make it use a different Poly1305 context format. That's a major change, where bugs can be introduced -- and at least one was introduced, in fact. I'd appreciate it if you were more accurate in describing your modifications and the potential risks involved.

A different Poly1305 context format? Not at all - it's using the exact same context struct as the assembly. And it's making the same conversion that the assembly is. There's not something "different" happening; that's the whole point.

Also, this is not some process of frightfully transcribing assembly to C and hoping it all works out. This is a careful process of reasoning about the limbs, proving that the carries are correct, and that the arithmetic done in C actually corresponds to what we want. And then finally we check that what we've implemented is indeed the same as what the assembly implemented. Finally, as I mentioned, hopefully Andy will be folding this back into his implementations sometime soon anyway.

Quoted 8 lines
Quoted 5 lines

That's a good idea. I can include some discussion about this as well in the commit message that introduces the glue code, too, I guess? I've been hesitant to fill these commit messages up even more, given there are already so many walls of text and whatnot, but if you think that'd be useful, I'll do that for v7, and also add comments.

Please prefer to put information in the code or documentation rather than in commit messages, when appropriate.

Okay, no problem.

Quoted 8 lines
Quoted 7 lines

This is complete and utter garbage, and I find its insinuations insulting and ridiculous. There is absolutely no lack of honesty and no double standard being applied whatsoever. Your attempt to cast doubt about the quality of standards applied and the integrity of the process is wholly inappropriate. When I tell you that high standards were applied and that due-diligence was done in developing a particular patch, I mean what I say.

Quoted 5 lines

I
disagree that my concerns are "complete and utter garbage". And I think that the fact that you prefer to respond by attacking me, rather than committing to be more accurate in your claims and to treat all contributions fairly, is problematic.

I believe you have the sequence of events wrong. I've stated and made that there isn't a double standard, that the standards intend to be evenly rigorous, and I solicited your feedback on how I could best communicate changes in pre-merged patch series; I did the things you've said one should do. But your rhetoric then moved to questioning my integrity, and I believe that was uncalled for, inappropriate, and not a useful contribution to this mostly productive discussion -- hence garbage. In other words, I provided an acceptable defense, not an attack. But can we move past all this schoolyard nonsense? Cut the rhetoric, please; it's really quite overwhelming.

Quoted 2 lines

It's great that you found and fixed this
bug on your own, and of course that raises my level of confidence in your work.

Good.

Quoted 2 lines

Still, the v6 patchset still includes your claim that "All implementations have been extensively tested and fuzzed"; so that claim was objectively wrong.

I don't think that claim is wrong. The fuzzing and testing that's been done has been extensive, and the fuzzing and testing that continues to occur is extensive. As mentioned, the bug was fixed pretty much right after git-send-email. If you'd like I can try to space out the patch submissions by a little bit longer -- that would probably have various benefits -- but given that the netdev code is yet to receive a thorough review, I think we've got a bit of time before this is merged. The faster-paced patch cycles might inadvertently introduce things that get fixed immediately after sending then, unfortunately, but I don't think this will be the case with the final series that's merged. Though I'm incorporating tons and tons of feedback right now, I do look forward to the structure of the series settling down a little bit and the pace of suggestions decreasing, so that I can focus on auditing and verifying again. But given the dynamism and interesting insights of the reviews so far, I think the fast pace has actually been useful for elucidating the various expectations and suggestions of reviewers. It's most certainly improved this patchset in terrific ways.

Quoted 2 lines

Well, it doesn't help that you yourself claim that Zinc stands for "Zx2c4's INsane Cryptolib"...

This expansion of the acronym was intended as a totally ridiculous joke. I guess it wasn't received well. I'll remove it from the next series. Sorry.

Jason

#129 lore raw
Quoted 9 lines · Jason A. Donenfeld

Hi Eric,

Quoted 5 lines · Eric Biggers

And you still haven't answered my question about adding a new algorithm that is useful to have in both APIs. You're proposing that in most cases the crypto API part will need to go through Herbert while the implementation will need to go through you/Greg, right? Or will you/Greg be taking both? Or will Herbert take both?

Quoted 7 lines

If an implementation enters Zinc, it will go through my tree. If it enters the crypto API, it will go through Herbert's tree. If there wind up being messy tree dependency and merge timing issues, I can work this out in the usual way with Herbert. I'll be sure to discuss these edge cases with him when we discuss. I think there's a way to handle that with minimum friction.

I would also strongly prefer that all crypto work is taken through Herbert's tree, so we have a coherent view of it before it goes upstream.

Quoted 133 lines
Quoted 2 lines

A documentation file for Zinc is a good idea. Some of the information in your commit messages should be moved there too.

Will do.

Quoted 6 lines

I'm not trying to "politicize" this, but rather determine your criteria for including algorithms in Zinc, which you haven't made clear. Since for the second time you've avoided answering my question about whether you'd allow the SM4 cipher in Zinc, and you designed WireGuard to be "cryptographically opinionated", and you were one of the loudest voices calling for the Speck cipher to be removed, and your justification for Zinc complains about cipher

Quoted 8 lines

modes from "90s cryptographers", I think it's reasonable for people to wonder whether as the Zinc (i.e. Linux crypto library) maintainer you will restrict the inclusion of crypto algorithms to the ones you prefer, rather than the ones that users need. Note that the kernel is used by people all over the world and needs to support various standards, protocols, and APIs that use different crypto algorithms, not always the ones we'd like; and different users have different preferences. People need to know whether the Linux kernel's crypto library will meet (or be allowed to meet) their crypto needs.

WireGuard is indeed quite opinionated in its primitive choices, but I don't think it'd be wise to apply the same design to Zinc. There are APIs where the goal is to have a limited set of high-level functions, and have those implemented in only a preferred set of primitives. NaCl is a good example of this -- functions like "crypto_secretbox" that are actually xsalsapoly under the hood. Zinc doesn't intend to become an API like those, but rather to provide the actual primitives for use cases where that specific primitive is used. Sometimes the kernel is in the business of being able to pick its own crypto -- random.c, tcp sequence numbers, big_key.c, etc -- but most of the time the kernel is in the business of implementing other people's crypto, for specific devices/protocols/diskformats. And for those use cases we need not some high-level API like NaCl, but rather direct access to the primitives that are required to implement those drivers. WireGuard is one such example, but so would be Bluetooth, CIFS/SMB, WiFi, and so on. We're in the business of writing drivers, after all. So, no, I don't think I'd knock down the addition of a primitive because of a simple preference for a different primitive, if it was clearly the case that the driver requiring it really benefited from having accessible via the plain Zinc function calls. Sorry if I hadn't made this clear earlier -- I thought Ard had asked more or less the same thing about DES and I answered accordingly, but maybe that wasn't made clear enough there.

Quoted 11 lines
Quoted 5 lines

For example, check out the avx blocks function. The radix conversion happens in a few different places throughout. The reason we need it separately here is because, unlike userspace, it's possible the kernel code will transition from 2^26 back to 2^64 as a result of the FPU context changing.

IOW, you had to rewrite the x86 assembly algorithm in C and make it use a different Poly1305 context format. That's a major change, where bugs can be introduced -- and at least one was introduced, in fact. I'd appreciate it if you were more accurate in describing your modifications and the potential risks involved.

A different Poly1305 context format? Not at all - it's using the exact same context struct as the assembly. And it's making the same conversion that the assembly is. There's not something "different" happening; that's the whole point.

Also, this is not some process of frightfully transcribing assembly to C and hoping it all works out. This is a careful process of reasoning about the limbs, proving that the carries are correct, and that the arithmetic done in C actually corresponds to what we want. And then finally we check that what we've implemented is indeed the same as what the assembly implemented. Finally, as I mentioned, hopefully Andy will be folding this back into his implementations sometime soon anyway.

Quoted 8 lines
Quoted 5 lines

That's a good idea. I can include some discussion about this as well in the commit message that introduces the glue code, too, I guess? I've been hesitant to fill these commit messages up even more, given there are already so many walls of text and whatnot, but if you think that'd be useful, I'll do that for v7, and also add comments.

Please prefer to put information in the code or documentation rather than in commit messages, when appropriate.

Okay, no problem.

Quoted 8 lines
Quoted 7 lines

This is complete and utter garbage, and I find its insinuations insulting and ridiculous. There is absolutely no lack of honesty and no double standard being applied whatsoever. Your attempt to cast doubt about the quality of standards applied and the integrity of the process is wholly inappropriate. When I tell you that high standards were applied and that due-diligence was done in developing a particular patch, I mean what I say.

Quoted 5 lines

I
disagree that my concerns are "complete and utter garbage". And I think that the fact that you prefer to respond by attacking me, rather than committing to be more accurate in your claims and to treat all contributions fairly, is problematic.

I believe you have the sequence of events wrong. I've stated and made that there isn't a double standard, that the standards intend to be evenly rigorous, and I solicited your feedback on how I could best communicate changes in pre-merged patch series; I did the things you've said one should do. But your rhetoric then moved to questioning my integrity, and I believe that was uncalled for, inappropriate, and not a useful contribution to this mostly productive discussion -- hence garbage. In other words, I provided an acceptable defense, not an attack. But can we move past all this schoolyard nonsense? Cut the rhetoric, please; it's really quite overwhelming.

Quoted 2 lines

It's great that you found and fixed this
bug on your own, and of course that raises my level of confidence in your work.

Good.

Quoted 2 lines

Still, the v6 patchset still includes your claim that "All implementations have been extensively tested and fuzzed"; so that claim was objectively wrong.

I don't think that claim is wrong. The fuzzing and testing that's been done has been extensive, and the fuzzing and testing that continues to occur is extensive. As mentioned, the bug was fixed pretty much right after git-send-email. If you'd like I can try to space out the patch submissions by a little bit longer -- that would probably have various benefits -- but given that the netdev code is yet to receive a thorough review, I think we've got a bit of time before this is merged. The faster-paced patch cycles might inadvertently introduce things that get fixed immediately after sending then, unfortunately, but I don't think this will be the case with the final series that's merged. Though I'm incorporating tons and tons of feedback right now, I do look forward to the structure of the series settling down a little bit and the pace of suggestions decreasing, so that I can focus on auditing and verifying again. But given the dynamism and interesting insights of the reviews so far, I think the fast pace has actually been useful for elucidating the various expectations and suggestions of reviewers. It's most certainly improved this patchset in terrific ways.

Quoted 7 lines
Quoted 2 lines

Well, it doesn't help that you yourself claim that Zinc stands for "Zx2c4's INsane Cryptolib"...

This expansion of the acronym was intended as a totally ridiculous joke. I guess it wasn't received well. I'll remove it from the next series. Sorry.

As I understood from someone who was at your Kernel Recipes talk, you mentioned that it actually stands for 'zinc is not crypto/' (note the slash)

That is needlessly divisive and condescending, so if you want to move past the rhetoric and the schoolyard nonsense, perhaps you can find a better name for it? Or just put your stuff into crypto/base/, crypto/core/ or something like that.

author maintainer
#130 lore raw
Quoted 3 lines · Ard Biesheuvel

As I understood from someone who was at your Kernel Recipes talk, you mentioned that it actually stands for 'zinc is not crypto/' (note the slash)

I mentioned this was in v1 but it wasn't taken as lightly as planned and was removed, so if it needs a recursive acronym it can be "zinc is nice crypto", "zinc is neat crypto", or as the commit message mentions, "zinc as in crypto." Alternatively, it can just not stand for anything at all. Zinc -> Zinc. That's what the thing is called.

maintainer
#131 lore raw
In reply to Ard Biesheuvel
Quoted 9 lines · Ard Biesheuvel
Quoted 6 lines · Jason A. Donenfeld

If an implementation enters Zinc, it will go through my tree. If it enters the crypto API, it will go through Herbert's tree. If there wind up being messy tree dependency and merge timing issues, I can work this out in the usual way with Herbert. I'll be sure to discuss these edge cases with him when we discuss. I think there's a way to handle that with minimum friction.

Quoted 3 lines

I would also strongly prefer that all crypto work is taken through Herbert's tree, so we have a coherent view of it before it goes upstream.

I agree. I don't have any problems with the zinc code living in its own git tree. But any upstream merges should definitely go through the crypto tree because the inherent ties between the two code-base.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author maintainer
#132 lore raw

Hi Herbert,

Quoted 8 lines · Herbert Xu
Quoted 3 lines

I would also strongly prefer that all crypto work is taken through Herbert's tree, so we have a coherent view of it before it goes upstream.

I agree. I don't have any problems with the zinc code living in its own git tree. But any upstream merges should definitely go through the crypto tree because the inherent ties between the two code-base.

I can send you pull requests then if there are development cycles when there are in fact relations between the two trees. I'll update the commit message describing Zinc to include this.

Jason

maintainer
#133 lore raw

Hi Jason:

Quoted 10 lines · Jason A. Donenfeld

Hi Herbert,

Quoted 4 lines

I agree. I don't have any problems with the zinc code living in its own git tree. But any upstream merges should definitely go through the crypto tree because the inherent ties between the two code-base.

I can send you pull requests then if there are development cycles when there are in fact relations between the two trees. I'll update the commit message describing Zinc to include this.

I think all merges should go through the crypto tree so that the kernel crypto community has oversight over the crypto implementations.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#134 lore raw
In reply to Jason A. Donenfeld
Quoted 7 lines · Jason A. Donenfeld

Hi Herbert,

Quoted 4 lines · Herbert Xu
Quoted 3 lines

I would also strongly prefer that all crypto work is taken through Herbert's tree, so we have a coherent view of it before it goes upstream.

Quoted 9 lines
Quoted 4 lines

I agree. I don't have any problems with the zinc code living in its own git tree. But any upstream merges should definitely go through the crypto tree because the inherent ties between the two code-base.

I can send you pull requests then if there are development cycles when there are in fact relations between the two trees. I'll update the commit message describing Zinc to include this.

Can you explain why you it is so important to you that your changes remain outside the crypto tree?

Also, I still think the name Zinc (zinc is not crypto/) is needlessly divisive and condescending, and unsaying it (in v2 and up) doesn't really work on the Internet (especially since you are still repeating it in your conference talk.)

#135 lore raw
Quoted 19 lines · Ard Biesheuvel
Quoted 7 lines · Jason A. Donenfeld

Hi Herbert,

Quoted 4 lines · Herbert Xu
Quoted 3 lines

I would also strongly prefer that all crypto work is taken through Herbert's tree, so we have a coherent view of it before it goes upstream.

Quoted 9 lines
Quoted 4 lines

I agree. I don't have any problems with the zinc code living in its own git tree. But any upstream merges should definitely go through the crypto tree because the inherent ties between the two code-base.

I can send you pull requests then if there are development cycles when there are in fact relations between the two trees. I'll update the commit message describing Zinc to include this.

Quoted 7 lines

Can you explain why you it is so important to you that your changes remain outside the crypto tree?

Also, I still think the name Zinc (zinc is not crypto/) is needlessly divisive and condescending, and unsaying it (in v2 and up) doesn't really work on the Internet (especially since you are still repeating it in your conference talk.)

I've been following the drama^Wdiscussion on the zinc for a long time now and I also think that "zinc" is a misleading name.
Jason, you seem to hate the existing crypto framework with passion, and the name reflects that.
Since we all agree that the framework can do better and your patches actually make it better, please just rename it to something that reflects what it is, a base
framework. I think Ard already suggested "crypto/base/" or "crypto/core/".

Signature · 2 lines
Thanks,
//richard
author maintainer
#136 lore raw
In reply to Ard Biesheuvel
Quoted 6 lines · Ard Biesheuvel

Also, I still think the name Zinc (zinc is not crypto/) is needlessly divisive and condescending, and unsaying it (in v2 and up) doesn't really work on the Internet (especially since you are still repeating it in your conference talk.)
Jason, you seem to hate the existing crypto framework with passion, and the name reflects that.

It's not divisive or condescending. I don't hate the existing framework with a passion -- this is patently false. The name even with its original acronym doesn't imply anything essentially negative. I see that you've repeatedly misinterpreted it this way -- which is why I removed that from v2 for the avoidance of doubt -- but that doesn't change the fact that its proper interpretation is not a condescending or divisive one.

Look, people love to bikeshed about names. I'm sure you'll be able to CC-in a large crew of people who have opinions in one way or another, and this thread could begin to have many voices on that front or on multiple fronts. There are real benefits of sticking with the name I've given to the library I've spent enormous amounts of time writing. And so I'm going to stick with Zinc, since that's why our library is called. Sorry. We can talk about this at Plumbers in Vancouver if you want, but I think you're not going to get very far with a mailing list naming bikeshed.

Meanwhile, I'm working around the clock to integrate your very useful technical suggestions, so please keep them coming if you still have technical nits you'd like to be in v7 of this patchset.

Jason

#137 lore raw
Quoted 16 lines · Jason A. Donenfeld
Quoted 6 lines · Ard Biesheuvel

Also, I still think the name Zinc (zinc is not crypto/) is needlessly divisive and condescending, and unsaying it (in v2 and up) doesn't really work on the Internet (especially since you are still repeating it in your conference talk.)
Jason, you seem to hate the existing crypto framework with passion, and the name reflects that.

It's not divisive or condescending. I don't hate the existing framework with a passion -- this is patently false. The name even with its original acronym doesn't imply anything essentially negative. I see that you've repeatedly misinterpreted it this way -- which is why I removed that from v2 for the avoidance of doubt -- but that doesn't change the fact that its proper interpretation is not a condescending or divisive one.

Quoted 10 lines

Look, people love to bikeshed about names. I'm sure you'll be able to CC-in a large crew of people who have opinions in one way or another, and this thread could begin to have many voices on that front or on multiple fronts. There are real benefits of sticking with the name I've given to the library I've spent enormous amounts of time writing. And so I'm going to stick with Zinc, since that's why our library is called. Sorry. We can talk about this at Plumbers in Vancouver if you want, but I think you're not going to get very far with a mailing list naming bikeshed.

It's not really about the name, though. It's actually about the whole way of thinking about the submission. Is it a new special library with its own things going on, or is it just some crypto helper functions? It's really just the latter, but you've been presenting it as the former, which is causing a lot of unnecessary confusion and controversy. And I think that largely because you've started from that perspective, you've ended up with some oddities like your own directories in lib/ and include/ separate from the other existing crypto helper functions, and proposals to maintain it completely separately from the existing crypto code including having a separate git tree and separate upstreaming path.

For example, we already have include/crypto/. What's the difference between include/crypto/ and include/zinc/? How do users know whether to include e.g. <crypto/chacha20.h> rather than <zinc/chacha20.h>? And are users going to remember and understand that "zinc" actually means "crypto" but not really? It would be much more straightforward and intuitive if we just had <crypto/chacha20.h>, and your new helper functions were declared in there.

Dismissing these concerns as bikeshedding about the name is missing the point.

- Eric

author maintainer
#138 lore raw

Hi Eric,

Quoted 4 lines · Eric Biggers

It's not really about the name, though. It's actually about the whole way of thinking about the submission. Is it a new special library with its own things going on, or is it just some crypto helper functions? It's really just the latter, but you've been presenting it as the former

No, it really is its own thing with important differences from the present crypto api. Zinc's focus is on simplicity and clarity. To the extent that we're at all tangled with the current crypto api, the goal is to untangle as much as possible. It intends to be a small and lightweight set of routines, whose relationships are obvious, and with this direct and to the point organization, as well as work with the larger cryptography community and with academia to invite collaboration. With this comes a different way of maintaining it, with higher standards and a preference for different implementations than the current situation. With Zinc, you have an obvious series of C function calls composing the whole thing, without complicated indirection. It's something that could be trivially lifted out into a userspace library, and used broadly, for example -- something I'll probably do at some point. That's a bit of a design change to the current crypto api, and sprinkling some direct function calls within the current crypto api's complicated enterprise situation would only kick the can further down the road, as much complexity would still remain. The goal is to move away from behemoth enterprise APIs, and large and complex codebases to a simple and direct way of doing things. This desire to untangle, to start from a simpler base, and to generally do things differently means it will go into lib/zinc/ and include/zinc/ and have different maintainers.

Jason

#139 lore raw
Quoted 20 lines · Jason A. Donenfeld
Quoted 4 lines · Eric Biggers

It's not really about the name, though. It's actually about the whole way of thinking about the submission. Is it a new special library with its own things going on, or is it just some crypto helper functions? It's really just the latter, but you've been presenting it as the former

No, it really is its own thing with important differences from the present crypto api. Zinc's focus is on simplicity and clarity. To the extent that we're at all tangled with the current crypto api, the goal is to untangle as much as possible. It intends to be a small and lightweight set of routines, whose relationships are obvious, and with this direct and to the point organization, as well as work with the larger cryptography community and with academia to invite collaboration. With this comes a different way of maintaining it, with higher standards and a preference for different implementations than the current situation. With Zinc, you have an obvious series of C function calls composing the whole thing, without complicated indirection. It's something that could be trivially lifted out into a userspace library, and used broadly, for example -- something I'll probably do at some point. That's a bit of a design change to the current crypto api, and

Quoted 8 lines

sprinkling some direct function calls within the current crypto api's complicated enterprise situation would only kick the can further down the road, as much complexity would still remain. The goal is to move away from behemoth enterprise APIs, and large and complex codebases to a simple and direct way of doing things. This desire to untangle, to start from a simpler base, and to generally do things differently means it will go into lib/zinc/ and include/zinc/ and have different maintainers.

So we will have two competing crypo stacks in the kernel?

Having a lightweight crypto API is a good thing but I really don't like the idea of having zinc parallel to the existing crypto stack.
And I strongly vote that Herbert Xu shall remain the maintainer of the whole crypto system (including zinc!) in the kernel.

Signature · 2 lines
Thanks,
//richard
author maintainer
#140 lore raw
Quoted 3 lines · Richard Weinberger

So we will have two competing crypo stacks in the kernel?
Having a lightweight crypto API is a good thing but I really don't like the idea of having zinc parallel to the existing crypto stack.

No, as you've seen in this patchset, the dynamic dispatch crypto API can trivially be done on top of Zinc. So each time we introduce a new primitive to Zinc that's also in the dynamic dispatch API, we reimplement the current crypto API in terms of Zinc. Check out the two patches in this series that do this; it's quite clean and sleek.

Quoted 2 lines

And I strongly vote that Herbert Xu shall remain the maintainer of the whole crypto system (including zinc!) in the kernel.

No, sorry, we intend to maintain the code we've written. But I am amenable to taking a tree-route into upstream based on whatever makes most sense with merge conflicts and such.

#141 lore raw

Am Freitag, 5. Oktober 2018, 15:46:29 CEST schrieb Jason A. Donenfeld:

Quoted 11 lines
Quoted 3 lines · Richard Weinberger

So we will have two competing crypo stacks in the kernel?
Having a lightweight crypto API is a good thing but I really don't like the idea of having zinc parallel to the existing crypto stack.

No, as you've seen in this patchset, the dynamic dispatch crypto API can trivially be done on top of Zinc. So each time we introduce a new primitive to Zinc that's also in the dynamic dispatch API, we reimplement the current crypto API in terms of Zinc. Check out the two patches in this series that do this; it's quite clean and sleek.

This is why I was asking. Your statement and the code didn't match for me.

Quoted 6 lines
Quoted 2 lines

And I strongly vote that Herbert Xu shall remain the maintainer of the whole crypto system (including zinc!) in the kernel.

No, sorry, we intend to maintain the code we've written. But I am amenable to taking a tree-route into upstream based on whatever makes most sense with merge conflicts and such.

So, you will be a sub-maintainer below Herbert's crypto, that's fine. What you wrote sounded like a parallel world...

Thanks,
//richard

#143 lore raw
In reply to Jason A. Donenfeld
Quoted 51 lines · Jason A. Donenfeld

Hi Eric,

Quoted 5 lines · Eric Biggers

And you still haven't answered my question about adding a new algorithm that is useful to have in both APIs. You're proposing that in most cases the crypto API part will need to go through Herbert while the implementation will need to go through you/Greg, right? Or will you/Greg be taking both? Or will Herbert take both?

If an implementation enters Zinc, it will go through my tree. If it enters the crypto API, it will go through Herbert's tree. If there wind up being messy tree dependency and merge timing issues, I can work this out in the usual way with Herbert. I'll be sure to discuss these edge cases with him when we discuss. I think there's a way to handle that with minimum friction.

Quoted 2 lines

A documentation file for Zinc is a good idea. Some of the information in your commit messages should be moved there too.

Will do.

Quoted 6 lines

I'm not trying to "politicize" this, but rather determine your criteria for including algorithms in Zinc, which you haven't made clear. Since for the second time you've avoided answering my question about whether you'd allow the SM4 cipher in Zinc, and you designed WireGuard to be "cryptographically opinionated", and you were one of the loudest voices calling for the Speck cipher to be removed, and your justification for Zinc complains about cipher

Quoted 8 lines

modes from "90s cryptographers", I think it's reasonable for people to wonder whether as the Zinc (i.e. Linux crypto library) maintainer you will restrict the inclusion of crypto algorithms to the ones you prefer, rather than the ones that users need. Note that the kernel is used by people all over the world and needs to support various standards, protocols, and APIs that use different crypto algorithms, not always the ones we'd like; and different users have different preferences. People need to know whether the Linux kernel's crypto library will meet (or be allowed to meet) their crypto needs.

WireGuard is indeed quite opinionated in its primitive choices, but I don't think it'd be wise to apply the same design to Zinc. There are APIs where the goal is to have a limited set of high-level functions, and have those implemented in only a preferred set of primitives. NaCl is a good example of this -- functions like "crypto_secretbox" that are actually xsalsapoly under the hood. Zinc doesn't intend to become an API like those, but rather to provide the actual primitives for use cases where that specific primitive is used. Sometimes the kernel is in the business of being able to pick its own crypto -- random.c, tcp sequence numbers, big_key.c, etc -- but most of the time the kernel is in the business of implementing other people's crypto, for specific devices/protocols/diskformats. And for those use cases we need not some high-level API like NaCl, but rather direct access to the primitives that are required to implement those drivers. WireGuard is one such example, but so would be Bluetooth, CIFS/SMB, WiFi, and so

Quoted 9 lines

on. We're in the business of writing drivers, after all. So, no, I don't think I'd knock down the addition of a primitive because of a simple preference for a different primitive, if it was clearly the case that the driver requiring it really benefited from having accessible via the plain Zinc function calls. Sorry if I hadn't made this clear earlier -- I thought Ard had asked more or less the same thing about DES and I answered accordingly, but maybe that wasn't made clear enough there.

CRC32 is another case study that I would like to bring up:
- the current status is a bit of a mess, where we treat crc32() and crc32c() differently - the latter is exposed by libcrc32.ko as a wrapper around the crypto API, and there is some networking code (SCTP iirc) that puts another kludge on top of that to ensure that the code is not used before the module is loaded
- we have C versions, scalar hw instruction based versions and carryless multiplication based versions for various architectures
- on ST platforms, we have a synchronous hw accelerator that is 10x faster than C code on the same platform.

AFAICT none of its current users rely on the async interface or runtime dispatch of the 'hash'.

CRC32/c is an area that I would *really* like to clean up (and am already doing for arm64) - just having a function call interface would be a huge improvement but it seems to me that the choice for monolithic modules per algo/architecture implies that we will have to leave ST behind in this case.

On the one hand, disregarding this seems fair, at least for the moment. On the other hand, fixing this in the crypto API, e.g., by permitting direct function calls to synchronous hashes and without the need to allocate a transform/request etc, blurs the line even more where different pieces should live.

Any thoughts?

author maintainer
#144 lore raw

[+Willy]

Hi Ard,

Quoted 38 lines · Ard Biesheuvel
Quoted 15 lines · Jason A. Donenfeld

WireGuard is indeed quite opinionated in its primitive choices, but I don't think it'd be wise to apply the same design to Zinc. There are APIs where the goal is to have a limited set of high-level functions, and have those implemented in only a preferred set of primitives. NaCl is a good example of this -- functions like "crypto_secretbox" that are actually xsalsapoly under the hood. Zinc doesn't intend to become an API like those, but rather to provide the actual primitives for use cases where that specific primitive is used. Sometimes the kernel is in the business of being able to pick its own crypto -- random.c, tcp sequence numbers, big_key.c, etc -- but most of the time the kernel is in the business of implementing other people's crypto, for specific devices/protocols/diskformats. And for those use cases we need not some high-level API like NaCl, but rather direct access to the primitives that are required to implement those drivers. WireGuard is one such example, but so would be Bluetooth, CIFS/SMB, WiFi, and so

Quoted 9 lines

on. We're in the business of writing drivers, after all. So, no, I don't think I'd knock down the addition of a primitive because of a simple preference for a different primitive, if it was clearly the case that the driver requiring it really benefited from having accessible via the plain Zinc function calls. Sorry if I hadn't made this clear earlier -- I thought Ard had asked more or less the same thing about DES and I answered accordingly, but maybe that wasn't made clear enough there.

CRC32 is another case study that I would like to bring up:
- the current status is a bit of a mess, where we treat crc32() and crc32c() differently - the latter is exposed by libcrc32.ko as a wrapper around the crypto API, and there is some networking code (SCTP iirc) that puts another kludge on top of that to ensure that the code is not used before the module is loaded
- we have C versions, scalar hw instruction based versions and carryless multiplication based versions for various architectures
- on ST platforms, we have a synchronous hw accelerator that is 10x faster than C code on the same platform.

Quoted 2 lines

AFAICT none of its current users rely on the async interface or runtime dispatch of the 'hash'.

I've added Willy to the CC, as we were actually discussing the crc32 case together two days ago. If my understanding was correct, he seemed to think it'd be pretty useful too.

It seems like unifying the crc32 implementations at some point would make sense, and since there's no usage of these as part of the crypto api, providing crc32 via a vanilla function call seems reasonable. It's not clear that on some strict formal level, crc32 belongs anywhere near Zinc, since it's not _exactly_ in the same category... But one could broaden the scope a bit and make the argument that this general idea of accepting some bytes, doing some "pure" arithmetic operations from a particular discipline on them in slightly different ways depending on the architecture, and then returning some other bytes, is what in essence is happening with these all of these function calls, no matter their security or intended use; so if crc32 would benefit from being implemented using the exact same design patterns, then it might as well be grouped in with the rest of Zinc. On the other hand, this might be a bit of a slippery slope ("are compression functions next?"), and a libcrc32.ko could just as well exist as a standalone thing. I can see it both ways and some other arguments too, but also this might form a good setting for some much needed cleanup of the crc32. So that's definitely something we can consider.

Regarding the synchronous ST driver: I'll give it a thorough read through, but I do wonder off the bat if actually it'd be possible to incorporate that into the Zinc paradigm in a fairly non-intrusive way. I'll give that some thought and play around a bit with it.

Jason

#145 lore raw
Quoted 11 lines · Jason A. Donenfeld
Quoted 11 lines

CRC32 is another case study that I would like to bring up:
- the current status is a bit of a mess, where we treat crc32() and crc32c() differently - the latter is exposed by libcrc32.ko as a wrapper around the crypto API, and there is some networking code (SCTP iirc) that puts another kludge on top of that to ensure that the code is not used before the module is loaded
- we have C versions, scalar hw instruction based versions and carryless multiplication based versions for various architectures
- on ST platforms, we have a synchronous hw accelerator that is 10x faster than C code on the same platform.

Quoted 6 lines
Quoted 2 lines

AFAICT none of its current users rely on the async interface or runtime dispatch of the 'hash'.

I've added Willy to the CC, as we were actually discussing the crc32 case together two days ago. If my understanding was correct, he seemed to think it'd be pretty useful too.

Yep, I've had a hard time finding a suitable implementation when I wrote libslz (deflate+gzip compression). Some were totally inefficient in C, spending their time negating the value twice for each byte because they were using the pre-computed tables found in literature, some were using optimized SIMD and I didn't understand them at all. Finally I spent some time studying the principle and implemented mine. Later I discovered that ARMv8 chips have a crc32 instruction for this which is very fast. I tried it and it works much faster than my C implementation. For me it's a perfect example of something that could go into a lib like zinc.

Quoted 5 lines

It seems like unifying the crc32 implementations at some point would make sense, and since there's no usage of these as part of the crypto api, providing crc32 via a vanilla function call seems reasonable. It's not clear that on some strict formal level, crc32 belongs anywhere near Zinc, since it's not _exactly_ in the same category...

Well, it's a hash. Not a cryptographically secure one (32 bits only) but definitely a hash usable (and used) for many cases. Those who need a hash could start with SHA2 and figure that they don't need something that secure since they only use 32 output bits and be fine with CRC32 instead. So that would make sense.

Quoted 8 lines

But one could broaden the scope a bit and make the argument that this general idea of accepting some bytes, doing some "pure" arithmetic operations from a particular discipline on them in slightly different ways depending on the architecture, and then returning some other bytes, is what in essence is happening with these all of these function calls, no matter their security or intended use; so if crc32 would benefit from being implemented using the exact same design patterns, then it might as well be grouped in with the rest of Zinc.

You should avoid suggesting it like this because you may end up implementing addition :-) You should stick to the initial intent which is to implement painful and boring stuff like crypto. But when you see how to implement CRC32, you'll see that it's as painful to implement as a crypto function. You start with a reference, you're not satisfied and start to optimize various parts of it until you break it for some inputs without noticing. I'm pretty sure that it reminds you your own experience with some crypto functions because I think we've all been in that situation in this domain.

Quoted 2 lines

On the other hand, this might be a bit of a slippery slope ("are compression functions next?"),

I was thinking about it as well. There's a relation between crypto and compression, all transform intelligible information into hidden one in a reversable way, so the API is quite similar. One could argue that compression usually requires memory allocation and huge states, so that's probably not for the same use cases if the functions have to deal with memory allocations.

In my opinion currently we have :
- asymmetric crypto whose goal is to reversably encrypt/decrypt in order to protect confidentiality on small data like keys, or to sign in order to authenticate small data like hashes

- symmetric crypto whose goal is to protect confidentiality on data blocks.

- hashes whose goal is to provide integrity of data blocks (crc32 fall into this category by the way)

It's visible that despite using similar APIs, compression doesn't fit into any of these categories.

Quoted 2 lines

and a libcrc32.ko could just as well
exist as a standalone thing.

Having it included could simplify exposing it as a static inline function when it's a single native instruction on the CPU.

Just my two cents,
Willy