Blame SOURCES/libgcrypt-1.8.5-ppc-chacha20-poly1305.patch

9925f7
From 83e50634789cab5071d648f66622cc1b3cf72318 Mon Sep 17 00:00:00 2001
9925f7
From: Eric Richter <erichte@linux.vnet.ibm.com>
9925f7
Date: Thu, 24 Jun 2021 18:31:51 -0600
9925f7
Subject: [PATCH] improvements for chacha20 and poly1305 on power
9925f7
9925f7
---
9925f7
 cipher/Makefile.am            |   22 +
9925f7
 cipher/chacha20-new.c         | 1344 +++++++++++++++++++++++++++++++++
9925f7
 cipher/chacha20-ppc.c         |  646 ++++++++++++++++
9925f7
 cipher/chacha20.c             |    7 +
9925f7
 cipher/cipher-internal.h      |    9 +-
9925f7
 cipher/mpi-new/mpi-asm-defs.h |    8 +
9925f7
 cipher/mpi-new/mpi-inline.h   |  161 ++++
9925f7
 cipher/mpi-new/mpi-internal.h |  305 ++++++++
9925f7
 cipher/poly1305-new.c         |  749 ++++++++++++++++++
9925f7
 cipher/poly1305.c             |    7 +
9925f7
 configure.ac                  |   24 +
9925f7
 mpi/longlong.h                |    2 -
9925f7
 12 files changed, 3281 insertions(+), 3 deletions(-)
9925f7
 create mode 100644 cipher/chacha20-new.c
9925f7
 create mode 100644 cipher/chacha20-ppc.c
9925f7
 create mode 100644 cipher/mpi-new/mpi-asm-defs.h
9925f7
 create mode 100644 cipher/mpi-new/mpi-inline.h
9925f7
 create mode 100644 cipher/mpi-new/mpi-internal.h
9925f7
 create mode 100644 cipher/poly1305-new.c
9925f7
9925f7
diff --git a/cipher/Makefile.am b/cipher/Makefile.am
9925f7
index 7a777ef2..86ae09fa 100644
9925f7
--- a/cipher/Makefile.am
9925f7
+++ b/cipher/Makefile.am
9925f7
@@ -65,6 +65,7 @@ blowfish.c blowfish-amd64.S blowfish-arm.S \
9925f7
 cast5.c cast5-amd64.S cast5-arm.S \
9925f7
 chacha20.c chacha20-sse2-amd64.S chacha20-ssse3-amd64.S chacha20-avx2-amd64.S \
9925f7
   chacha20-armv7-neon.S \
9925f7
+  chacha20-new.c chacha20-ppc.c \
9925f7
 cipher-gcm-ppc.c cipher-gcm-intel-pclmul.c \
9925f7
   cipher-gcm-armv8-aarch32-ce.S cipher-gcm-armv8-aarch64-ce.S \
9925f7
 crc.c \
9925f7
@@ -80,6 +81,7 @@ gostr3411-94.c \
9925f7
 md4.c \
9925f7
 md5.c \
9925f7
 poly1305-sse2-amd64.S poly1305-avx2-amd64.S poly1305-armv7-neon.S \
9925f7
+  poly1305-new.c \
9925f7
 rijndael.c rijndael-internal.h rijndael-tables.h rijndael-aesni.c \
9925f7
   rijndael-padlock.c rijndael-amd64.S rijndael-arm.S \
9925f7
   rijndael-ssse3-amd64.c rijndael-ssse3-amd64-asm.S \
9925f7
@@ -172,3 +174,23 @@ cipher-gcm-ppc.o: $(srcdir)/cipher-gcm-ppc.c Makefile
9925f7
 
9925f7
 cipher-gcm-ppc.lo: $(srcdir)/cipher-gcm-ppc.c Makefile
9925f7
 	`echo $(LTCOMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
+
9925f7
+
9925f7
+# MAYBE WILL NEED THIS ONE?
9925f7
+poly1305-new.o: $(srcdir)/poly1305-new.c Makefile
9925f7
+	`echo $(COMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
+
9925f7
+poly1305-new.lo: $(srcdir)/poly1305-new.c Makefile
9925f7
+	`echo $(LTCOMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
+
9925f7
+chacha20-ppc.o: $(srcdir)/chacha20-ppc.c Makefile
9925f7
+	`echo $(COMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
+
9925f7
+chacha20-ppc.lo: $(srcdir)/chacha20-ppc.c Makefile
9925f7
+	`echo $(LTCOMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
+
9925f7
+chacha20-new.o: $(srcdir)/chacha20-new.c Makefile
9925f7
+	`echo $(COMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
+
9925f7
+chacha20-new.lo: $(srcdir)/chacha20-new.c Makefile
9925f7
+	`echo $(LTCOMPILE) $(ppc_vcrypto_cflags) -c $< `
9925f7
diff --git a/cipher/chacha20-new.c b/cipher/chacha20-new.c
9925f7
new file mode 100644
9925f7
index 00000000..347d9726
9925f7
--- /dev/null
9925f7
+++ b/cipher/chacha20-new.c
9925f7
@@ -0,0 +1,1344 @@
9925f7
+/* chacha20.c  -  Bernstein's ChaCha20 cipher
9925f7
+ * Copyright (C) 2014,2017-2019 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9925f7
+ *
9925f7
+ * This file is part of Libgcrypt.
9925f7
+ *
9925f7
+ * Libgcrypt is free software; you can redistribute it and/or modify
9925f7
+ * it under the terms of the GNU Lesser general Public License as
9925f7
+ * published by the Free Software Foundation; either version 2.1 of
9925f7
+ * the License, or (at your option) any later version.
9925f7
+ *
9925f7
+ * Libgcrypt is distributed in the hope that it will be useful,
9925f7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9925f7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925f7
+ * GNU Lesser General Public License for more details.
9925f7
+ *
9925f7
+ * You should have received a copy of the GNU Lesser General Public
9925f7
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
9925f7
+ *
9925f7
+ * For a description of the algorithm, see:
9925f7
+ *   http://cr.yp.to/chacha.html
9925f7
+ */
9925f7
+
9925f7
+/*
9925f7
+ * Based on D. J. Bernstein reference implementation at
9925f7
+ * http://cr.yp.to/chacha.html:
9925f7
+ *
9925f7
+ * chacha-regs.c version 20080118
9925f7
+ * D. J. Bernstein
9925f7
+ * Public domain.
9925f7
+ */
9925f7
+
9925f7
+#include <config.h>
9925f7
+
9925f7
+#if defined(ENABLE_PPC_CRYPTO_SUPPORT) && \
9925f7
+    defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
9925f7
+    defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \
9925f7
+    defined(USE_CHACHA20) && \
9925f7
+    __GNUC__ >= 4
9925f7
+
9925f7
+#include <stdio.h>
9925f7
+#include <stdlib.h>
9925f7
+#include <string.h>
9925f7
+#include "types.h"
9925f7
+#include "g10lib.h"
9925f7
+#include "cipher.h"
9925f7
+#include "cipher-internal.h"
9925f7
+#include "bufhelp.h"
9925f7
+
9925f7
+
9925f7
+/* A structure with function pointers for bulk operations.  The cipher
9925f7
+   algorithm setkey function initializes them when bulk operations are
9925f7
+   available and the actual encryption routines use them if they are
9925f7
+   not NULL.  */
9925f7
+// Stolen from cipher-internal.h
9925f7
+typedef struct cipher_bulk_ops
9925f7
+{
9925f7
+  void (*cfb_enc)(void *context, unsigned char *iv, void *outbuf_arg,
9925f7
+		  const void *inbuf_arg, size_t nblocks);
9925f7
+  void (*cfb_dec)(void *context, unsigned char *iv, void *outbuf_arg,
9925f7
+		  const void *inbuf_arg, size_t nblocks);
9925f7
+  void (*cbc_enc)(void *context, unsigned char *iv, void *outbuf_arg,
9925f7
+		  const void *inbuf_arg, size_t nblocks, int cbc_mac);
9925f7
+  void (*cbc_dec)(void *context, unsigned char *iv, void *outbuf_arg,
9925f7
+		  const void *inbuf_arg, size_t nblocks);
9925f7
+  void (*ofb_enc)(void *context, unsigned char *iv, void *outbuf_arg,
9925f7
+		  const void *inbuf_arg, size_t nblocks);
9925f7
+  void (*ctr_enc)(void *context, unsigned char *iv, void *outbuf_arg,
9925f7
+		  const void *inbuf_arg, size_t nblocks);
9925f7
+  size_t (*ocb_crypt)(gcry_cipher_hd_t c, void *outbuf_arg,
9925f7
+		      const void *inbuf_arg, size_t nblocks, int encrypt);
9925f7
+  size_t (*ocb_auth)(gcry_cipher_hd_t c, const void *abuf_arg, size_t nblocks);
9925f7
+  void (*xts_crypt)(void *context, unsigned char *tweak, void *outbuf_arg,
9925f7
+		    const void *inbuf_arg, size_t nblocks, int encrypt);
9925f7
+  size_t (*gcm_crypt)(gcry_cipher_hd_t c, void *outbuf_arg,
9925f7
+		      const void *inbuf_arg, size_t nblocks, int encrypt);
9925f7
+} cipher_bulk_ops_t;
9925f7
+
9925f7
+
9925f7
+#define CHACHA20_MIN_KEY_SIZE 16        /* Bytes.  */
9925f7
+#define CHACHA20_MAX_KEY_SIZE 32        /* Bytes.  */
9925f7
+#define CHACHA20_BLOCK_SIZE   64        /* Bytes.  */
9925f7
+#define CHACHA20_MIN_IV_SIZE   8        /* Bytes.  */
9925f7
+#define CHACHA20_MAX_IV_SIZE  12        /* Bytes.  */
9925f7
+#define CHACHA20_CTR_SIZE     16        /* Bytes.  */
9925f7
+
9925f7
+
9925f7
+/* USE_SSSE3 indicates whether to compile with Intel SSSE3 code. */
9925f7
+#undef USE_SSSE3
9925f7
+#if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_SSSE3) && \
9925f7
+   (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
9925f7
+    defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
9925f7
+# define USE_SSSE3 1
9925f7
+#endif
9925f7
+
9925f7
+/* USE_AVX2 indicates whether to compile with Intel AVX2 code. */
9925f7
+#undef USE_AVX2
9925f7
+#if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX2) && \
9925f7
+    (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
9925f7
+     defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
9925f7
+# define USE_AVX2 1
9925f7
+#endif
9925f7
+
9925f7
+/* USE_ARMV7_NEON indicates whether to enable ARMv7 NEON assembly code. */
9925f7
+#undef USE_ARMV7_NEON
9925f7
+#ifdef ENABLE_NEON_SUPPORT
9925f7
+# if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
9925f7
+     && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
9925f7
+     && defined(HAVE_GCC_INLINE_ASM_NEON)
9925f7
+#  define USE_ARMV7_NEON 1
9925f7
+# endif
9925f7
+#endif
9925f7
+
9925f7
+/* USE_AARCH64_SIMD indicates whether to enable ARMv8 SIMD assembly
9925f7
+ * code. */
9925f7
+#undef USE_AARCH64_SIMD
9925f7
+#ifdef ENABLE_NEON_SUPPORT
9925f7
+# if defined(__AARCH64EL__) \
9925f7
+       && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \
9925f7
+       && defined(HAVE_GCC_INLINE_ASM_AARCH64_NEON)
9925f7
+#  define USE_AARCH64_SIMD 1
9925f7
+# endif
9925f7
+#endif
9925f7
+
9925f7
+/* USE_PPC_VEC indicates whether to enable PowerPC vector
9925f7
+ * accelerated code. */
9925f7
+#undef USE_PPC_VEC
9925f7
+#ifdef ENABLE_PPC_CRYPTO_SUPPORT
9925f7
+# if defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
9925f7
+     defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC)
9925f7
+#  if __GNUC__ >= 4
9925f7
+#   define USE_PPC_VEC 1
9925f7
+#  endif
9925f7
+# endif
9925f7
+#endif
9925f7
+
9925f7
+/* USE_S390X_VX indicates whether to enable zSeries code. */
9925f7
+#undef USE_S390X_VX
9925f7
+#if defined (__s390x__) && __GNUC__ >= 4 && __ARCH__ >= 9
9925f7
+# if defined(HAVE_GCC_INLINE_ASM_S390X_VX)
9925f7
+#  define USE_S390X_VX 1
9925f7
+# endif /* USE_S390X_VX */
9925f7
+#endif
9925f7
+
9925f7
+/* Assembly implementations use SystemV ABI, ABI conversion and additional
9925f7
+ * stack to store XMM6-XMM15 needed on Win64. */
9925f7
+#undef ASM_FUNC_ABI
9925f7
+#undef ASM_EXTRA_STACK
9925f7
+#if defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)
9925f7
+# define ASM_FUNC_ABI __attribute__((sysv_abi))
9925f7
+#else
9925f7
+# define ASM_FUNC_ABI
9925f7
+#endif
9925f7
+
9925f7
+
9925f7
+typedef struct CHACHA20_context_s
9925f7
+{
9925f7
+  u32 input[16];
9925f7
+  unsigned char pad[CHACHA20_BLOCK_SIZE];
9925f7
+  unsigned int unused; /* bytes in the pad.  */
9925f7
+  unsigned int use_ssse3:1;
9925f7
+  unsigned int use_avx2:1;
9925f7
+  unsigned int use_neon:1;
9925f7
+  unsigned int use_ppc:1;
9925f7
+  unsigned int use_s390x:1;
9925f7
+} CHACHA20_context_t;
9925f7
+
9925f7
+
9925f7
+#ifdef USE_SSSE3
9925f7
+
9925f7
+unsigned int _gcry_chacha20_amd64_ssse3_blocks4(u32 *state, byte *dst,
9925f7
+						const byte *src,
9925f7
+						size_t nblks) ASM_FUNC_ABI;
9925f7
+
9925f7
+unsigned int _gcry_chacha20_amd64_ssse3_blocks1(u32 *state, byte *dst,
9925f7
+						const byte *src,
9925f7
+						size_t nblks) ASM_FUNC_ABI;
9925f7
+
9925f7
+unsigned int _gcry_chacha20_poly1305_amd64_ssse3_blocks4(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		void *poly1305_state, const byte *poly1305_src) ASM_FUNC_ABI;
9925f7
+
9925f7
+unsigned int _gcry_chacha20_poly1305_amd64_ssse3_blocks1(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		void *poly1305_state, const byte *poly1305_src) ASM_FUNC_ABI;
9925f7
+
9925f7
+#endif /* USE_SSSE3 */
9925f7
+
9925f7
+#ifdef USE_AVX2
9925f7
+
9925f7
+unsigned int _gcry_chacha20_amd64_avx2_blocks8(u32 *state, byte *dst,
9925f7
+					       const byte *src,
9925f7
+					       size_t nblks) ASM_FUNC_ABI;
9925f7
+
9925f7
+unsigned int _gcry_chacha20_poly1305_amd64_avx2_blocks8(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		void *poly1305_state, const byte *poly1305_src) ASM_FUNC_ABI;
9925f7
+
9925f7
+#endif /* USE_AVX2 */
9925f7
+
9925f7
+#ifdef USE_PPC_VEC
9925f7
+
9925f7
+unsigned int _gcry_chacha20_ppc8_blocks4(u32 *state, byte *dst,
9925f7
+					 const byte *src,
9925f7
+					 size_t nblks);
9925f7
+
9925f7
+unsigned int _gcry_chacha20_ppc8_blocks1(u32 *state, byte *dst,
9925f7
+					 const byte *src,
9925f7
+					 size_t nblks);
9925f7
+
9925f7
+#undef USE_PPC_VEC_POLY1305
9925f7
+#if SIZEOF_UNSIGNED_LONG == 8
9925f7
+#define USE_PPC_VEC_POLY1305 1
9925f7
+unsigned int _gcry_chacha20_poly1305_ppc8_blocks4(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		POLY1305_STATE *st, const byte *poly1305_src);
9925f7
+#endif /* SIZEOF_UNSIGNED_LONG == 8 */
9925f7
+
9925f7
+#endif /* USE_PPC_VEC */
9925f7
+
9925f7
+#ifdef USE_S390X_VX
9925f7
+
9925f7
+unsigned int _gcry_chacha20_s390x_vx_blocks8(u32 *state, byte *dst,
9925f7
+					     const byte *src, size_t nblks);
9925f7
+
9925f7
+unsigned int _gcry_chacha20_s390x_vx_blocks4_2_1(u32 *state, byte *dst,
9925f7
+						 const byte *src, size_t nblks);
9925f7
+
9925f7
+#undef USE_S390X_VX_POLY1305
9925f7
+#if SIZEOF_UNSIGNED_LONG == 8
9925f7
+#define USE_S390X_VX_POLY1305 1
9925f7
+unsigned int _gcry_chacha20_poly1305_s390x_vx_blocks8(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		POLY1305_STATE *st, const byte *poly1305_src);
9925f7
+
9925f7
+unsigned int _gcry_chacha20_poly1305_s390x_vx_blocks4_2_1(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		POLY1305_STATE *st, const byte *poly1305_src);
9925f7
+#endif /* SIZEOF_UNSIGNED_LONG == 8 */
9925f7
+
9925f7
+#endif /* USE_S390X_VX */
9925f7
+
9925f7
+#ifdef USE_ARMV7_NEON
9925f7
+
9925f7
+unsigned int _gcry_chacha20_armv7_neon_blocks4(u32 *state, byte *dst,
9925f7
+					       const byte *src,
9925f7
+					       size_t nblks);
9925f7
+
9925f7
+#endif /* USE_ARMV7_NEON */
9925f7
+
9925f7
+#ifdef USE_AARCH64_SIMD
9925f7
+
9925f7
+unsigned int _gcry_chacha20_aarch64_blocks4(u32 *state, byte *dst,
9925f7
+					    const byte *src, size_t nblks);
9925f7
+
9925f7
+unsigned int _gcry_chacha20_poly1305_aarch64_blocks4(
9925f7
+		u32 *state, byte *dst, const byte *src, size_t nblks,
9925f7
+		void *poly1305_state, const byte *poly1305_src);
9925f7
+
9925f7
+#endif /* USE_AARCH64_SIMD */
9925f7
+
9925f7
+
9925f7
+static const char *selftest (void);
9925f7
+
9925f7
+
9925f7
+#define ROTATE(v,c)	(rol(v,c))
9925f7
+#define XOR(v,w)	((v) ^ (w))
9925f7
+#define PLUS(v,w)	((u32)((v) + (w)))
9925f7
+#define PLUSONE(v)	(PLUS((v),1))
9925f7
+
9925f7
+#define QUARTERROUND(a,b,c,d) \
9925f7
+  a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
9925f7
+  c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
9925f7
+  a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
9925f7
+  c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
9925f7
+
9925f7
+#define BUF_XOR_LE32(dst, src, offset, x) \
9925f7
+  buf_put_le32((dst) + (offset), buf_get_le32((src) + (offset)) ^ (x))
9925f7
+
9925f7
+static unsigned int
9925f7
+do_chacha20_blocks (u32 *input, byte *dst, const byte *src, size_t nblks)
9925f7
+{
9925f7
+  u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
9925f7
+  unsigned int i;
9925f7
+
9925f7
+  while (nblks)
9925f7
+    {
9925f7
+      x0 = input[0];
9925f7
+      x1 = input[1];
9925f7
+      x2 = input[2];
9925f7
+      x3 = input[3];
9925f7
+      x4 = input[4];
9925f7
+      x5 = input[5];
9925f7
+      x6 = input[6];
9925f7
+      x7 = input[7];
9925f7
+      x8 = input[8];
9925f7
+      x9 = input[9];
9925f7
+      x10 = input[10];
9925f7
+      x11 = input[11];
9925f7
+      x12 = input[12];
9925f7
+      x13 = input[13];
9925f7
+      x14 = input[14];
9925f7
+      x15 = input[15];
9925f7
+
9925f7
+      for (i = 20; i > 0; i -= 2)
9925f7
+	{
9925f7
+	  QUARTERROUND(x0, x4,  x8, x12)
9925f7
+	  QUARTERROUND(x1, x5,  x9, x13)
9925f7
+	  QUARTERROUND(x2, x6, x10, x14)
9925f7
+	  QUARTERROUND(x3, x7, x11, x15)
9925f7
+	  QUARTERROUND(x0, x5, x10, x15)
9925f7
+	  QUARTERROUND(x1, x6, x11, x12)
9925f7
+	  QUARTERROUND(x2, x7,  x8, x13)
9925f7
+	  QUARTERROUND(x3, x4,  x9, x14)
9925f7
+	}
9925f7
+
9925f7
+      x0 = PLUS(x0, input[0]);
9925f7
+      x1 = PLUS(x1, input[1]);
9925f7
+      x2 = PLUS(x2, input[2]);
9925f7
+      x3 = PLUS(x3, input[3]);
9925f7
+      x4 = PLUS(x4, input[4]);
9925f7
+      x5 = PLUS(x5, input[5]);
9925f7
+      x6 = PLUS(x6, input[6]);
9925f7
+      x7 = PLUS(x7, input[7]);
9925f7
+      x8 = PLUS(x8, input[8]);
9925f7
+      x9 = PLUS(x9, input[9]);
9925f7
+      x10 = PLUS(x10, input[10]);
9925f7
+      x11 = PLUS(x11, input[11]);
9925f7
+      x12 = PLUS(x12, input[12]);
9925f7
+      x13 = PLUS(x13, input[13]);
9925f7
+      x14 = PLUS(x14, input[14]);
9925f7
+      x15 = PLUS(x15, input[15]);
9925f7
+
9925f7
+      input[12] = PLUSONE(input[12]);
9925f7
+      input[13] = PLUS(input[13], !input[12]);
9925f7
+
9925f7
+      BUF_XOR_LE32(dst, src, 0, x0);
9925f7
+      BUF_XOR_LE32(dst, src, 4, x1);
9925f7
+      BUF_XOR_LE32(dst, src, 8, x2);
9925f7
+      BUF_XOR_LE32(dst, src, 12, x3);
9925f7
+      BUF_XOR_LE32(dst, src, 16, x4);
9925f7
+      BUF_XOR_LE32(dst, src, 20, x5);
9925f7
+      BUF_XOR_LE32(dst, src, 24, x6);
9925f7
+      BUF_XOR_LE32(dst, src, 28, x7);
9925f7
+      BUF_XOR_LE32(dst, src, 32, x8);
9925f7
+      BUF_XOR_LE32(dst, src, 36, x9);
9925f7
+      BUF_XOR_LE32(dst, src, 40, x10);
9925f7
+      BUF_XOR_LE32(dst, src, 44, x11);
9925f7
+      BUF_XOR_LE32(dst, src, 48, x12);
9925f7
+      BUF_XOR_LE32(dst, src, 52, x13);
9925f7
+      BUF_XOR_LE32(dst, src, 56, x14);
9925f7
+      BUF_XOR_LE32(dst, src, 60, x15);
9925f7
+
9925f7
+      src += CHACHA20_BLOCK_SIZE;
9925f7
+      dst += CHACHA20_BLOCK_SIZE;
9925f7
+      nblks--;
9925f7
+    }
9925f7
+
9925f7
+  /* burn_stack */
9925f7
+  return (17 * sizeof(u32) + 6 * sizeof(void *));
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static unsigned int
9925f7
+chacha20_blocks (CHACHA20_context_t *ctx, byte *dst, const byte *src,
9925f7
+		 size_t nblks)
9925f7
+{
9925f7
+#ifdef USE_SSSE3
9925f7
+  if (ctx->use_ssse3)
9925f7
+    {
9925f7
+      return _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, dst, src, nblks);
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_PPC_VEC
9925f7
+  if (ctx->use_ppc)
9925f7
+    {
9925f7
+      return _gcry_chacha20_ppc8_blocks1(ctx->input, dst, src, nblks);
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_S390X_VX
9925f7
+  if (ctx->use_s390x)
9925f7
+    {
9925f7
+      return _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, dst, src, nblks);
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+  return do_chacha20_blocks (ctx->input, dst, src, nblks);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static void
9925f7
+chacha20_keysetup (CHACHA20_context_t *ctx, const byte *key,
9925f7
+                   unsigned int keylen)
9925f7
+{
9925f7
+  static const char sigma[16] = "expand 32-byte k";
9925f7
+  static const char tau[16] = "expand 16-byte k";
9925f7
+  const char *constants;
9925f7
+
9925f7
+  ctx->input[4] = buf_get_le32(key + 0);
9925f7
+  ctx->input[5] = buf_get_le32(key + 4);
9925f7
+  ctx->input[6] = buf_get_le32(key + 8);
9925f7
+  ctx->input[7] = buf_get_le32(key + 12);
9925f7
+  if (keylen == CHACHA20_MAX_KEY_SIZE) /* 256 bits */
9925f7
+    {
9925f7
+      key += 16;
9925f7
+      constants = sigma;
9925f7
+    }
9925f7
+  else /* 128 bits */
9925f7
+    {
9925f7
+      constants = tau;
9925f7
+    }
9925f7
+  ctx->input[8] = buf_get_le32(key + 0);
9925f7
+  ctx->input[9] = buf_get_le32(key + 4);
9925f7
+  ctx->input[10] = buf_get_le32(key + 8);
9925f7
+  ctx->input[11] = buf_get_le32(key + 12);
9925f7
+  ctx->input[0] = buf_get_le32(constants + 0);
9925f7
+  ctx->input[1] = buf_get_le32(constants + 4);
9925f7
+  ctx->input[2] = buf_get_le32(constants + 8);
9925f7
+  ctx->input[3] = buf_get_le32(constants + 12);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static void
9925f7
+chacha20_ivsetup (CHACHA20_context_t * ctx, const byte *iv, size_t ivlen)
9925f7
+{
9925f7
+  if (ivlen == CHACHA20_CTR_SIZE)
9925f7
+    {
9925f7
+      ctx->input[12] = buf_get_le32 (iv + 0);
9925f7
+      ctx->input[13] = buf_get_le32 (iv + 4);
9925f7
+      ctx->input[14] = buf_get_le32 (iv + 8);
9925f7
+      ctx->input[15] = buf_get_le32 (iv + 12);
9925f7
+    }
9925f7
+  else if (ivlen == CHACHA20_MAX_IV_SIZE)
9925f7
+    {
9925f7
+      ctx->input[12] = 0;
9925f7
+      ctx->input[13] = buf_get_le32 (iv + 0);
9925f7
+      ctx->input[14] = buf_get_le32 (iv + 4);
9925f7
+      ctx->input[15] = buf_get_le32 (iv + 8);
9925f7
+    }
9925f7
+  else if (ivlen == CHACHA20_MIN_IV_SIZE)
9925f7
+    {
9925f7
+      ctx->input[12] = 0;
9925f7
+      ctx->input[13] = 0;
9925f7
+      ctx->input[14] = buf_get_le32 (iv + 0);
9925f7
+      ctx->input[15] = buf_get_le32 (iv + 4);
9925f7
+    }
9925f7
+  else
9925f7
+    {
9925f7
+      ctx->input[12] = 0;
9925f7
+      ctx->input[13] = 0;
9925f7
+      ctx->input[14] = 0;
9925f7
+      ctx->input[15] = 0;
9925f7
+    }
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static void
9925f7
+chacha20_setiv (void *context, const byte *iv, size_t ivlen)
9925f7
+{
9925f7
+  CHACHA20_context_t *ctx = (CHACHA20_context_t *) context;
9925f7
+
9925f7
+  /* draft-nir-cfrg-chacha20-poly1305-02 defines 96-bit and 64-bit nonce. */
9925f7
+  if (iv && ivlen != CHACHA20_MAX_IV_SIZE && ivlen != CHACHA20_MIN_IV_SIZE
9925f7
+      && ivlen != CHACHA20_CTR_SIZE)
9925f7
+    log_info ("WARNING: chacha20_setiv: bad ivlen=%u\n", (u32) ivlen);
9925f7
+
9925f7
+  if (iv && (ivlen == CHACHA20_MAX_IV_SIZE || ivlen == CHACHA20_MIN_IV_SIZE
9925f7
+             || ivlen == CHACHA20_CTR_SIZE))
9925f7
+    chacha20_ivsetup (ctx, iv, ivlen);
9925f7
+  else
9925f7
+    chacha20_ivsetup (ctx, NULL, 0);
9925f7
+
9925f7
+  /* Reset the unused pad bytes counter.  */
9925f7
+  ctx->unused = 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static gcry_err_code_t
9925f7
+chacha20_do_setkey (CHACHA20_context_t *ctx,
9925f7
+                    const byte *key, unsigned int keylen)
9925f7
+{
9925f7
+  static int initialized;
9925f7
+  static const char *selftest_failed;
9925f7
+  unsigned int features = _gcry_get_hw_features ();
9925f7
+
9925f7
+  if (!initialized)
9925f7
+    {
9925f7
+      initialized = 1;
9925f7
+      selftest_failed = selftest ();
9925f7
+      if (selftest_failed)
9925f7
+        log_error ("CHACHA20 selftest failed (%s)\n", selftest_failed);
9925f7
+    }
9925f7
+  if (selftest_failed)
9925f7
+    return GPG_ERR_SELFTEST_FAILED;
9925f7
+
9925f7
+  if (keylen != CHACHA20_MAX_KEY_SIZE && keylen != CHACHA20_MIN_KEY_SIZE)
9925f7
+    return GPG_ERR_INV_KEYLEN;
9925f7
+
9925f7
+#ifdef USE_SSSE3
9925f7
+  ctx->use_ssse3 = (features & HWF_INTEL_SSSE3) != 0;
9925f7
+#endif
9925f7
+#ifdef USE_AVX2
9925f7
+  ctx->use_avx2 = (features & HWF_INTEL_AVX2) != 0;
9925f7
+#endif
9925f7
+#ifdef USE_ARMV7_NEON
9925f7
+  ctx->use_neon = (features & HWF_ARM_NEON) != 0;
9925f7
+#endif
9925f7
+#ifdef USE_AARCH64_SIMD
9925f7
+  ctx->use_neon = (features & HWF_ARM_NEON) != 0;
9925f7
+#endif
9925f7
+#ifdef USE_PPC_VEC
9925f7
+  ctx->use_ppc = (features & HWF_PPC_ARCH_2_07) != 0;
9925f7
+#endif
9925f7
+#ifdef USE_S390X_VX
9925f7
+  ctx->use_s390x = (features & HWF_S390X_VX) != 0;
9925f7
+#endif
9925f7
+
9925f7
+  (void)features;
9925f7
+
9925f7
+  chacha20_keysetup (ctx, key, keylen);
9925f7
+
9925f7
+  /* We default to a zero nonce.  */
9925f7
+  chacha20_setiv (ctx, NULL, 0);
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static gcry_err_code_t
9925f7
+chacha20_setkey (void *context, const byte *key, unsigned int keylen,
9925f7
+                 cipher_bulk_ops_t *bulk_ops)
9925f7
+{
9925f7
+  CHACHA20_context_t *ctx = (CHACHA20_context_t *) context;
9925f7
+  gcry_err_code_t rc = chacha20_do_setkey (ctx, key, keylen);
9925f7
+  (void)bulk_ops;
9925f7
+  _gcry_burn_stack (4 + sizeof (void *) + 4 * sizeof (void *));
9925f7
+  return rc;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static unsigned int
9925f7
+do_chacha20_encrypt_stream_tail (CHACHA20_context_t *ctx, byte *outbuf,
9925f7
+				 const byte *inbuf, size_t length)
9925f7
+{
9925f7
+  static const unsigned char zero_pad[CHACHA20_BLOCK_SIZE] = { 0, };
9925f7
+  unsigned int nburn, burn = 0;
9925f7
+
9925f7
+#ifdef USE_AVX2
9925f7
+  if (ctx->use_avx2 && length >= CHACHA20_BLOCK_SIZE * 8)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 8;
9925f7
+      nburn = _gcry_chacha20_amd64_avx2_blocks8(ctx->input, outbuf, inbuf,
9925f7
+						nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_SSSE3
9925f7
+  if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 4;
9925f7
+      nburn = _gcry_chacha20_amd64_ssse3_blocks4(ctx->input, outbuf, inbuf,
9925f7
+						 nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_ARMV7_NEON
9925f7
+  if (ctx->use_neon && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 4;
9925f7
+      nburn = _gcry_chacha20_armv7_neon_blocks4(ctx->input, outbuf, inbuf,
9925f7
+						nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_AARCH64_SIMD
9925f7
+  if (ctx->use_neon && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 4;
9925f7
+      nburn = _gcry_chacha20_aarch64_blocks4(ctx->input, outbuf, inbuf,
9925f7
+					     nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_PPC_VEC
9925f7
+  if (ctx->use_ppc && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 4;
9925f7
+      nburn = _gcry_chacha20_ppc8_blocks4(ctx->input, outbuf, inbuf, nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_S390X_VX
9925f7
+  if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE * 8)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 8;
9925f7
+      nburn = _gcry_chacha20_s390x_vx_blocks8(ctx->input, outbuf, inbuf,
9925f7
+					      nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+  if (length >= CHACHA20_BLOCK_SIZE)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nburn = chacha20_blocks(ctx, outbuf, inbuf, nblocks);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+
9925f7
+  if (length > 0)
9925f7
+    {
9925f7
+      nburn = chacha20_blocks(ctx, ctx->pad, zero_pad, 1);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      buf_xor (outbuf, inbuf, ctx->pad, length);
9925f7
+      ctx->unused = CHACHA20_BLOCK_SIZE - length;
9925f7
+    }
9925f7
+
9925f7
+  if (burn)
9925f7
+    burn += 5 * sizeof(void *);
9925f7
+
9925f7
+  return burn;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static void
9925f7
+chacha20_encrypt_stream (void *context, byte *outbuf, const byte *inbuf,
9925f7
+                         size_t length)
9925f7
+{
9925f7
+  CHACHA20_context_t *ctx = (CHACHA20_context_t *) context;
9925f7
+  unsigned int nburn, burn = 0;
9925f7
+
9925f7
+  if (!length)
9925f7
+    return;
9925f7
+
9925f7
+  if (ctx->unused)
9925f7
+    {
9925f7
+      unsigned char *p = ctx->pad;
9925f7
+      size_t n;
9925f7
+
9925f7
+      gcry_assert (ctx->unused < CHACHA20_BLOCK_SIZE);
9925f7
+
9925f7
+      n = ctx->unused;
9925f7
+      if (n > length)
9925f7
+        n = length;
9925f7
+
9925f7
+      buf_xor (outbuf, inbuf, p + CHACHA20_BLOCK_SIZE - ctx->unused, n);
9925f7
+      length -= n;
9925f7
+      outbuf += n;
9925f7
+      inbuf += n;
9925f7
+      ctx->unused -= n;
9925f7
+
9925f7
+      if (!length)
9925f7
+        return;
9925f7
+      gcry_assert (!ctx->unused);
9925f7
+    }
9925f7
+
9925f7
+  nburn = do_chacha20_encrypt_stream_tail (ctx, outbuf, inbuf, length);
9925f7
+  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+  if (burn)
9925f7
+    _gcry_burn_stack (burn);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+gcry_err_code_t
9925f7
+_gcry_chacha20_poly1305_encrypt(gcry_cipher_hd_t c, byte *outbuf,
9925f7
+				const byte *inbuf, size_t length)
9925f7
+{
9925f7
+  CHACHA20_context_t *ctx = (void *) &c->context.c;
9925f7
+  unsigned int nburn, burn = 0;
9925f7
+  byte *authptr = NULL;
9925f7
+
9925f7
+  if (!length)
9925f7
+    return 0;
9925f7
+
9925f7
+  if (ctx->unused)
9925f7
+    {
9925f7
+      unsigned char *p = ctx->pad;
9925f7
+      size_t n;
9925f7
+
9925f7
+      gcry_assert (ctx->unused < CHACHA20_BLOCK_SIZE);
9925f7
+
9925f7
+      n = ctx->unused;
9925f7
+      if (n > length)
9925f7
+        n = length;
9925f7
+
9925f7
+      buf_xor (outbuf, inbuf, p + CHACHA20_BLOCK_SIZE - ctx->unused, n);
9925f7
+      nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, outbuf, n);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      length -= n;
9925f7
+      outbuf += n;
9925f7
+      inbuf += n;
9925f7
+      ctx->unused -= n;
9925f7
+
9925f7
+      if (!length)
9925f7
+	{
9925f7
+	  if (burn)
9925f7
+	    _gcry_burn_stack (burn);
9925f7
+
9925f7
+	  return 0;
9925f7
+	}
9925f7
+      gcry_assert (!ctx->unused);
9925f7
+    }
9925f7
+
9925f7
+  gcry_assert (c->u_mode.poly1305.ctx.leftover == 0);
9925f7
+
9925f7
+  if (0)
9925f7
+    { }
9925f7
+#ifdef USE_AVX2
9925f7
+  else if (ctx->use_avx2 && length >= CHACHA20_BLOCK_SIZE * 8)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_amd64_avx2_blocks8(ctx->input, outbuf, inbuf, 8);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 8 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 8 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 8 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+#ifdef USE_SSSE3
9925f7
+  else if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_amd64_ssse3_blocks4(ctx->input, outbuf, inbuf, 4);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+  else if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE * 2)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, outbuf, inbuf, 2);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 2 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 2 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 2 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+  else if (ctx->use_ssse3 && length >= CHACHA20_BLOCK_SIZE)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_amd64_ssse3_blocks1(ctx->input, outbuf, inbuf, 1);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 1 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 1 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 1 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+#ifdef USE_AARCH64_SIMD
9925f7
+  else if (ctx->use_neon && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_aarch64_blocks4(ctx->input, outbuf, inbuf, 4);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+#ifdef USE_PPC_VEC_POLY1305
9925f7
+  else if (ctx->use_ppc && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_ppc8_blocks4(ctx->input, outbuf, inbuf, 4);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+#ifdef USE_S390X_VX_POLY1305
9925f7
+  else if (ctx->use_s390x && length >= 2 * CHACHA20_BLOCK_SIZE * 8)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_s390x_vx_blocks8(ctx->input, outbuf, inbuf, 8);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 8 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 8 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 8 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+  else if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE * 4)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, outbuf, inbuf, 4);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 4 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+  else if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE * 2)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, outbuf, inbuf, 2);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 2 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 2 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 2 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+  else if (ctx->use_s390x && length >= CHACHA20_BLOCK_SIZE)
9925f7
+    {
9925f7
+      nburn = _gcry_chacha20_s390x_vx_blocks4_2_1(ctx->input, outbuf, inbuf, 1);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      authptr = outbuf;
9925f7
+      length -= 1 * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += 1 * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += 1 * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+  if (authptr)
9925f7
+    {
9925f7
+      size_t authoffset = outbuf - authptr;
9925f7
+
9925f7
+#ifdef USE_AVX2
9925f7
+      if (ctx->use_avx2 &&
9925f7
+	  length >= 8 * CHACHA20_BLOCK_SIZE &&
9925f7
+	  authoffset >= 8 * CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	  nblocks -= nblocks % 8;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_amd64_avx2_blocks8(
9925f7
+		      ctx->input, outbuf, inbuf, nblocks,
9925f7
+		      &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_SSSE3
9925f7
+      if (ctx->use_ssse3)
9925f7
+	{
9925f7
+	  if (length >= 4 * CHACHA20_BLOCK_SIZE &&
9925f7
+	      authoffset >= 4 * CHACHA20_BLOCK_SIZE)
9925f7
+	    {
9925f7
+	      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	      nblocks -= nblocks % 4;
9925f7
+
9925f7
+	      nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks4(
9925f7
+			  ctx->input, outbuf, inbuf, nblocks,
9925f7
+			  &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	      length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	    }
9925f7
+
9925f7
+	  if (length >= CHACHA20_BLOCK_SIZE &&
9925f7
+	      authoffset >= CHACHA20_BLOCK_SIZE)
9925f7
+	    {
9925f7
+	      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+
9925f7
+	      nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks1(
9925f7
+			  ctx->input, outbuf, inbuf, nblocks,
9925f7
+			  &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	      length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	    }
9925f7
+	}
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_AARCH64_SIMD
9925f7
+      if (ctx->use_neon &&
9925f7
+	  length >= 4 * CHACHA20_BLOCK_SIZE &&
9925f7
+	  authoffset >= 4 * CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	  nblocks -= nblocks % 4;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_aarch64_blocks4(
9925f7
+		      ctx->input, outbuf, inbuf, nblocks,
9925f7
+		      &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_PPC_VEC_POLY1305
9925f7
+      if (ctx->use_ppc &&
9925f7
+	  length >= 4 * CHACHA20_BLOCK_SIZE &&
9925f7
+	  authoffset >= 4 * CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	  nblocks -= nblocks % 4;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_ppc8_blocks4(
9925f7
+		      ctx->input, outbuf, inbuf, nblocks,
9925f7
+		      &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_S390X_VX_POLY1305
9925f7
+      if (ctx->use_s390x)
9925f7
+	{
9925f7
+	  if (length >= 8 * CHACHA20_BLOCK_SIZE &&
9925f7
+	      authoffset >= 8 * CHACHA20_BLOCK_SIZE)
9925f7
+	    {
9925f7
+	      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	      nblocks -= nblocks % 8;
9925f7
+
9925f7
+	      burn = _gcry_chacha20_poly1305_s390x_vx_blocks8(
9925f7
+			  ctx->input, outbuf, inbuf, nblocks,
9925f7
+			  &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	      length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	    }
9925f7
+
9925f7
+	  if (length >= CHACHA20_BLOCK_SIZE &&
9925f7
+	      authoffset >= CHACHA20_BLOCK_SIZE)
9925f7
+	    {
9925f7
+	      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+
9925f7
+	      burn = _gcry_chacha20_poly1305_s390x_vx_blocks4_2_1(
9925f7
+			  ctx->input, outbuf, inbuf, nblocks,
9925f7
+			  &c->u_mode.poly1305.ctx.state, authptr);
9925f7
+	      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	      length  -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      outbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      inbuf   += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	      authptr += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	    }
9925f7
+	}
9925f7
+#endif
9925f7
+
9925f7
+      if (authoffset > 0)
9925f7
+	{
9925f7
+	  _gcry_poly1305_update (&c->u_mode.poly1305.ctx, authptr, authoffset);
9925f7
+	  authptr += authoffset;
9925f7
+	  authoffset = 0;
9925f7
+	}
9925f7
+
9925f7
+      gcry_assert(authptr == outbuf);
9925f7
+    }
9925f7
+
9925f7
+  while (length)
9925f7
+    {
9925f7
+      size_t currlen = length;
9925f7
+
9925f7
+      /* Since checksumming is done after encryption, process input in 24KiB
9925f7
+       * chunks to keep data loaded in L1 cache for checksumming. */
9925f7
+      if (currlen > 24 * 1024)
9925f7
+	currlen = 24 * 1024;
9925f7
+
9925f7
+      nburn = do_chacha20_encrypt_stream_tail (ctx, outbuf, inbuf, currlen);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, outbuf,
9925f7
+					  currlen);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      outbuf += currlen;
9925f7
+      inbuf += currlen;
9925f7
+      length -= currlen;
9925f7
+    }
9925f7
+
9925f7
+  if (burn)
9925f7
+    _gcry_burn_stack (burn);
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+gcry_err_code_t
9925f7
+_gcry_chacha20_poly1305_decrypt(gcry_cipher_hd_t c, byte *outbuf,
9925f7
+				const byte *inbuf, size_t length)
9925f7
+{
9925f7
+  CHACHA20_context_t *ctx = (void *) &c->context.c;
9925f7
+  unsigned int nburn, burn = 0;
9925f7
+
9925f7
+  if (!length)
9925f7
+    return 0;
9925f7
+
9925f7
+  if (ctx->unused)
9925f7
+    {
9925f7
+      unsigned char *p = ctx->pad;
9925f7
+      size_t n;
9925f7
+
9925f7
+      gcry_assert (ctx->unused < CHACHA20_BLOCK_SIZE);
9925f7
+
9925f7
+      n = ctx->unused;
9925f7
+      if (n > length)
9925f7
+        n = length;
9925f7
+
9925f7
+      nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, inbuf, n);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+      buf_xor (outbuf, inbuf, p + CHACHA20_BLOCK_SIZE - ctx->unused, n);
9925f7
+      length -= n;
9925f7
+      outbuf += n;
9925f7
+      inbuf += n;
9925f7
+      ctx->unused -= n;
9925f7
+
9925f7
+      if (!length)
9925f7
+	{
9925f7
+	  if (burn)
9925f7
+	    _gcry_burn_stack (burn);
9925f7
+
9925f7
+	  return 0;
9925f7
+	}
9925f7
+      gcry_assert (!ctx->unused);
9925f7
+    }
9925f7
+
9925f7
+  gcry_assert (c->u_mode.poly1305.ctx.leftover == 0);
9925f7
+
9925f7
+#ifdef USE_AVX2
9925f7
+  if (ctx->use_avx2 && length >= 8 * CHACHA20_BLOCK_SIZE)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 8;
9925f7
+
9925f7
+      nburn = _gcry_chacha20_poly1305_amd64_avx2_blocks8(
9925f7
+			ctx->input, outbuf, inbuf, nblocks,
9925f7
+			&c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_SSSE3
9925f7
+  if (ctx->use_ssse3)
9925f7
+    {
9925f7
+      if (length >= 4 * CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	  nblocks -= nblocks % 4;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks4(
9925f7
+			    ctx->input, outbuf, inbuf, nblocks,
9925f7
+			    &c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+
9925f7
+      if (length >= CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_amd64_ssse3_blocks1(
9925f7
+			    ctx->input, outbuf, inbuf, nblocks,
9925f7
+			    &c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_AARCH64_SIMD
9925f7
+  if (ctx->use_neon && length >= 4 * CHACHA20_BLOCK_SIZE)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 4;
9925f7
+
9925f7
+      nburn = _gcry_chacha20_poly1305_aarch64_blocks4(
9925f7
+			ctx->input, outbuf, inbuf, nblocks,
9925f7
+			&c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_PPC_VEC_POLY1305
9925f7
+  if (ctx->use_ppc && length >= 4 * CHACHA20_BLOCK_SIZE)
9925f7
+    {
9925f7
+      size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+      nblocks -= nblocks % 4;
9925f7
+
9925f7
+      nburn = _gcry_chacha20_poly1305_ppc8_blocks4(
9925f7
+			ctx->input, outbuf, inbuf, nblocks,
9925f7
+			&c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+      inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+#ifdef USE_S390X_VX_POLY1305
9925f7
+  if (ctx->use_s390x)
9925f7
+    {
9925f7
+      if (length >= 8 * CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+	  nblocks -= nblocks % 8;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_s390x_vx_blocks8(
9925f7
+			    ctx->input, outbuf, inbuf, nblocks,
9925f7
+			    &c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+
9925f7
+      if (length >= CHACHA20_BLOCK_SIZE)
9925f7
+	{
9925f7
+	  size_t nblocks = length / CHACHA20_BLOCK_SIZE;
9925f7
+
9925f7
+	  nburn = _gcry_chacha20_poly1305_s390x_vx_blocks4_2_1(
9925f7
+			    ctx->input, outbuf, inbuf, nblocks,
9925f7
+			    &c->u_mode.poly1305.ctx.state, inbuf);
9925f7
+	  burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+	  length -= nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  outbuf += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	  inbuf  += nblocks * CHACHA20_BLOCK_SIZE;
9925f7
+	}
9925f7
+    }
9925f7
+#endif
9925f7
+
9925f7
+  while (length)
9925f7
+    {
9925f7
+      size_t currlen = length;
9925f7
+
9925f7
+      /* Since checksumming is done before decryption, process input in 24KiB
9925f7
+       * chunks to keep data loaded in L1 cache for decryption. */
9925f7
+      if (currlen > 24 * 1024)
9925f7
+	currlen = 24 * 1024;
9925f7
+
9925f7
+      nburn = _gcry_poly1305_update_burn (&c->u_mode.poly1305.ctx, inbuf,
9925f7
+					  currlen);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      nburn = do_chacha20_encrypt_stream_tail (ctx, outbuf, inbuf, currlen);
9925f7
+      burn = nburn > burn ? nburn : burn;
9925f7
+
9925f7
+      outbuf += currlen;
9925f7
+      inbuf += currlen;
9925f7
+      length -= currlen;
9925f7
+    }
9925f7
+
9925f7
+  if (burn)
9925f7
+    _gcry_burn_stack (burn);
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static const char *
9925f7
+selftest (void)
9925f7
+{
9925f7
+  byte ctxbuf[sizeof(CHACHA20_context_t) + 15];
9925f7
+  CHACHA20_context_t *ctx;
9925f7
+  byte scratch[127 + 1];
9925f7
+  byte buf[512 + 64 + 4];
9925f7
+  int i;
9925f7
+
9925f7
+  /* From draft-strombergson-chacha-test-vectors */
9925f7
+  static byte key_1[] = {
9925f7
+    0xc4, 0x6e, 0xc1, 0xb1, 0x8c, 0xe8, 0xa8, 0x78,
9925f7
+    0x72, 0x5a, 0x37, 0xe7, 0x80, 0xdf, 0xb7, 0x35,
9925f7
+    0x1f, 0x68, 0xed, 0x2e, 0x19, 0x4c, 0x79, 0xfb,
9925f7
+    0xc6, 0xae, 0xbe, 0xe1, 0xa6, 0x67, 0x97, 0x5d
9925f7
+  };
9925f7
+  static const byte nonce_1[] =
9925f7
+    { 0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21 };
9925f7
+  static const byte plaintext_1[127] = {
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+  };
9925f7
+  static const byte ciphertext_1[127] = {
9925f7
+    0xf6, 0x3a, 0x89, 0xb7, 0x5c, 0x22, 0x71, 0xf9,
9925f7
+    0x36, 0x88, 0x16, 0x54, 0x2b, 0xa5, 0x2f, 0x06,
9925f7
+    0xed, 0x49, 0x24, 0x17, 0x92, 0x30, 0x2b, 0x00,
9925f7
+    0xb5, 0xe8, 0xf8, 0x0a, 0xe9, 0xa4, 0x73, 0xaf,
9925f7
+    0xc2, 0x5b, 0x21, 0x8f, 0x51, 0x9a, 0xf0, 0xfd,
9925f7
+    0xd4, 0x06, 0x36, 0x2e, 0x8d, 0x69, 0xde, 0x7f,
9925f7
+    0x54, 0xc6, 0x04, 0xa6, 0xe0, 0x0f, 0x35, 0x3f,
9925f7
+    0x11, 0x0f, 0x77, 0x1b, 0xdc, 0xa8, 0xab, 0x92,
9925f7
+    0xe5, 0xfb, 0xc3, 0x4e, 0x60, 0xa1, 0xd9, 0xa9,
9925f7
+    0xdb, 0x17, 0x34, 0x5b, 0x0a, 0x40, 0x27, 0x36,
9925f7
+    0x85, 0x3b, 0xf9, 0x10, 0xb0, 0x60, 0xbd, 0xf1,
9925f7
+    0xf8, 0x97, 0xb6, 0x29, 0x0f, 0x01, 0xd1, 0x38,
9925f7
+    0xae, 0x2c, 0x4c, 0x90, 0x22, 0x5b, 0xa9, 0xea,
9925f7
+    0x14, 0xd5, 0x18, 0xf5, 0x59, 0x29, 0xde, 0xa0,
9925f7
+    0x98, 0xca, 0x7a, 0x6c, 0xcf, 0xe6, 0x12, 0x27,
9925f7
+    0x05, 0x3c, 0x84, 0xe4, 0x9a, 0x4a, 0x33
9925f7
+  };
9925f7
+
9925f7
+  /* 16-byte alignment required for amd64 implementation. */
9925f7
+  ctx = (CHACHA20_context_t *)((uintptr_t)(ctxbuf + 15) & ~(uintptr_t)15);
9925f7
+
9925f7
+  chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
9925f7
+  chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
9925f7
+  scratch[sizeof (scratch) - 1] = 0;
9925f7
+  chacha20_encrypt_stream (ctx, scratch, plaintext_1, sizeof plaintext_1);
9925f7
+  if (memcmp (scratch, ciphertext_1, sizeof ciphertext_1))
9925f7
+    return "ChaCha20 encryption test 1 failed.";
9925f7
+  if (scratch[sizeof (scratch) - 1])
9925f7
+    return "ChaCha20 wrote too much.";
9925f7
+  chacha20_setkey (ctx, key_1, sizeof (key_1), NULL);
9925f7
+  chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
9925f7
+  chacha20_encrypt_stream (ctx, scratch, scratch, sizeof plaintext_1);
9925f7
+  if (memcmp (scratch, plaintext_1, sizeof plaintext_1))
9925f7
+    return "ChaCha20 decryption test 1 failed.";
9925f7
+
9925f7
+  for (i = 0; i < sizeof buf; i++)
9925f7
+    buf[i] = i;
9925f7
+  chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
9925f7
+  chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
9925f7
+  /*encrypt */
9925f7
+  chacha20_encrypt_stream (ctx, buf, buf, sizeof buf);
9925f7
+  /*decrypt */
9925f7
+  chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
9925f7
+  chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
9925f7
+  chacha20_encrypt_stream (ctx, buf, buf, 1);
9925f7
+  chacha20_encrypt_stream (ctx, buf + 1, buf + 1, (sizeof buf) - 1 - 1);
9925f7
+  chacha20_encrypt_stream (ctx, buf + (sizeof buf) - 1,
9925f7
+                           buf + (sizeof buf) - 1, 1);
9925f7
+  for (i = 0; i < sizeof buf; i++)
9925f7
+    if (buf[i] != (byte) i)
9925f7
+      return "ChaCha20 encryption test 2 failed.";
9925f7
+
9925f7
+  chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
9925f7
+  chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
9925f7
+  /* encrypt */
9925f7
+  for (i = 0; i < sizeof buf; i++)
9925f7
+    chacha20_encrypt_stream (ctx, &buf[i], &buf[i], 1);
9925f7
+  /* decrypt */
9925f7
+  chacha20_setkey (ctx, key_1, sizeof key_1, NULL);
9925f7
+  chacha20_setiv (ctx, nonce_1, sizeof nonce_1);
9925f7
+  chacha20_encrypt_stream (ctx, buf, buf, sizeof buf);
9925f7
+  for (i = 0; i < sizeof buf; i++)
9925f7
+    if (buf[i] != (byte) i)
9925f7
+      return "ChaCha20 encryption test 3 failed.";
9925f7
+
9925f7
+  return NULL;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+gcry_cipher_spec_t _gcry_cipher_spec_chacha20 = {
9925f7
+  GCRY_CIPHER_CHACHA20,
9925f7
+  {0, 0},                       /* flags */
9925f7
+  "CHACHA20",                   /* name */
9925f7
+  NULL,                         /* aliases */
9925f7
+  NULL,                         /* oids */
9925f7
+  1,                            /* blocksize in bytes. */
9925f7
+  CHACHA20_MAX_KEY_SIZE * 8,    /* standard key length in bits. */
9925f7
+  sizeof (CHACHA20_context_t),
9925f7
+  chacha20_setkey,
9925f7
+  NULL,
9925f7
+  NULL,
9925f7
+  chacha20_encrypt_stream,
9925f7
+  chacha20_encrypt_stream,
9925f7
+  NULL,
9925f7
+  NULL,
9925f7
+  chacha20_setiv
9925f7
+};
9925f7
+
9925f7
+#endif /* ENABLE_PPC_CRYPTO_SUPPORT */
9925f7
diff --git a/cipher/chacha20-ppc.c b/cipher/chacha20-ppc.c
9925f7
new file mode 100644
9925f7
index 00000000..565b7156
9925f7
--- /dev/null
9925f7
+++ b/cipher/chacha20-ppc.c
9925f7
@@ -0,0 +1,646 @@
9925f7
+/* chacha20-ppc.c - PowerPC vector implementation of ChaCha20
9925f7
+ * Copyright (C) 2019 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9925f7
+ *
9925f7
+ * This file is part of Libgcrypt.
9925f7
+ *
9925f7
+ * Libgcrypt is free software; you can redistribute it and/or modify
9925f7
+ * it under the terms of the GNU Lesser General Public License as
9925f7
+ * published by the Free Software Foundation; either version 2.1 of
9925f7
+ * the License, or (at your option) any later version.
9925f7
+ *
9925f7
+ * Libgcrypt is distributed in the hope that it will be useful,
9925f7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9925f7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925f7
+ * GNU Lesser General Public License for more details.
9925f7
+ *
9925f7
+ * You should have received a copy of the GNU Lesser General Public
9925f7
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
9925f7
+ */
9925f7
+
9925f7
+#include <config.h>
9925f7
+
9925f7
+#if defined(ENABLE_PPC_CRYPTO_SUPPORT) && \
9925f7
+    defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
9925f7
+    defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \
9925f7
+    defined(USE_CHACHA20) && \
9925f7
+    __GNUC__ >= 4
9925f7
+
9925f7
+#include <altivec.h>
9925f7
+#include "bufhelp.h"
9925f7
+#include "poly1305-internal-new.h"
9925f7
+
9925f7
+#include "mpi/mpi-internal.h"
9925f7
+#include "mpi/longlong.h"
9925f7
+
9925f7
+
9925f7
+typedef vector unsigned char vector16x_u8;
9925f7
+typedef vector unsigned int vector4x_u32;
9925f7
+typedef vector unsigned long long vector2x_u64;
9925f7
+
9925f7
+
9925f7
+#define ALWAYS_INLINE inline __attribute__((always_inline))
9925f7
+#define NO_INLINE __attribute__((noinline))
9925f7
+#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
9925f7
+
9925f7
+#define ASM_FUNC_ATTR          NO_INSTRUMENT_FUNCTION
9925f7
+#define ASM_FUNC_ATTR_INLINE   ASM_FUNC_ATTR ALWAYS_INLINE
9925f7
+#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE
9925f7
+
9925f7
+
9925f7
+#ifdef WORDS_BIGENDIAN
9925f7
+static const vector16x_u8 le_bswap_const =
9925f7
+  { 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12 };
9925f7
+#endif
9925f7
+
9925f7
+
9925f7
+static ASM_FUNC_ATTR_INLINE vector4x_u32
9925f7
+vec_rol_elems(vector4x_u32 v, unsigned int idx)
9925f7
+{
9925f7
+#ifndef WORDS_BIGENDIAN
9925f7
+  return vec_sld (v, v, (16 - (4 * idx)) & 15);
9925f7
+#else
9925f7
+  return vec_sld (v, v, (4 * idx) & 15);
9925f7
+#endif
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static ASM_FUNC_ATTR_INLINE vector4x_u32
9925f7
+vec_load_le(unsigned long offset, const unsigned char *ptr)
9925f7
+{
9925f7
+  vector4x_u32 vec;
9925f7
+  vec = vec_vsx_ld (offset, (const u32 *)ptr);
9925f7
+#ifdef WORDS_BIGENDIAN
9925f7
+  vec = (vector4x_u32)vec_perm((vector16x_u8)vec, (vector16x_u8)vec,
9925f7
+			       le_bswap_const);
9925f7
+#endif
9925f7
+  return vec;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static ASM_FUNC_ATTR_INLINE void
9925f7
+vec_store_le(vector4x_u32 vec, unsigned long offset, unsigned char *ptr)
9925f7
+{
9925f7
+#ifdef WORDS_BIGENDIAN
9925f7
+  vec = (vector4x_u32)vec_perm((vector16x_u8)vec, (vector16x_u8)vec,
9925f7
+			       le_bswap_const);
9925f7
+#endif
9925f7
+  vec_vsx_st (vec, offset, (u32 *)ptr);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static ASM_FUNC_ATTR_INLINE vector4x_u32
9925f7
+vec_add_ctr_u64(vector4x_u32 v, vector4x_u32 a)
9925f7
+{
9925f7
+#ifdef WORDS_BIGENDIAN
9925f7
+  static const vector16x_u8 swap32 =
9925f7
+    { 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11 };
9925f7
+  vector2x_u64 vec, add, sum;
9925f7
+
9925f7
+  vec = (vector2x_u64)vec_perm((vector16x_u8)v, (vector16x_u8)v, swap32);
9925f7
+  add = (vector2x_u64)vec_perm((vector16x_u8)a, (vector16x_u8)a, swap32);
9925f7
+  sum = vec + add;
9925f7
+  return (vector4x_u32)vec_perm((vector16x_u8)sum, (vector16x_u8)sum, swap32);
9925f7
+#else
9925f7
+  return (vector4x_u32)((vector2x_u64)(v) + (vector2x_u64)(a));
9925f7
+#endif
9925f7
+}
9925f7
+
9925f7
+
9925f7
+/**********************************************************************
9925f7
+  2-way && 1-way chacha20
9925f7
+ **********************************************************************/
9925f7
+
9925f7
+#define ROTATE(v1,rolv)			\
9925f7
+	__asm__ ("vrlw %0,%1,%2\n\t" : "=v" (v1) : "v" (v1), "v" (rolv))
9925f7
+
9925f7
+#define WORD_ROL(v1,c)			\
9925f7
+	((v1) = vec_rol_elems((v1), (c)))
9925f7
+
9925f7
+#define XOR(ds,s) \
9925f7
+	((ds) ^= (s))
9925f7
+
9925f7
+#define PLUS(ds,s) \
9925f7
+	((ds) += (s))
9925f7
+
9925f7
+#define QUARTERROUND4(x0,x1,x2,x3,rol_x1,rol_x2,rol_x3) \
9925f7
+	PLUS(x0, x1); XOR(x3, x0); ROTATE(x3, rotate_16); \
9925f7
+	PLUS(x2, x3); XOR(x1, x2); ROTATE(x1, rotate_12); \
9925f7
+	PLUS(x0, x1); XOR(x3, x0); ROTATE(x3, rotate_8); \
9925f7
+	PLUS(x2, x3); \
9925f7
+	  WORD_ROL(x3, rol_x3); \
9925f7
+		      XOR(x1, x2); \
9925f7
+	  WORD_ROL(x2, rol_x2); \
9925f7
+				   ROTATE(x1, rotate_7); \
9925f7
+	  WORD_ROL(x1, rol_x1);
9925f7
+
9925f7
+#define ADD_U64(v,a) \
9925f7
+	(v = vec_add_ctr_u64(v, a))
9925f7
+
9925f7
+unsigned int ASM_FUNC_ATTR
9925f7
+_gcry_chacha20_ppc8_blocks1(u32 *state, byte *dst, const byte *src,
9925f7
+			    size_t nblks)
9925f7
+{
9925f7
+  vector4x_u32 counter_1 = { 1, 0, 0, 0 };
9925f7
+  vector4x_u32 rotate_16 = { 16, 16, 16, 16 };
9925f7
+  vector4x_u32 rotate_12 = { 12, 12, 12, 12 };
9925f7
+  vector4x_u32 rotate_8 = { 8, 8, 8, 8 };
9925f7
+  vector4x_u32 rotate_7 = { 7, 7, 7, 7 };
9925f7
+  vector4x_u32 state0, state1, state2, state3;
9925f7
+  vector4x_u32 v0, v1, v2, v3;
9925f7
+  vector4x_u32 v4, v5, v6, v7;
9925f7
+  int i;
9925f7
+
9925f7
+  /* force preload of constants to vector registers */
9925f7
+  __asm__ ("": "+v" (counter_1) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_16) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_12) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_8) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_7) :: "memory");
9925f7
+
9925f7
+  state0 = vec_vsx_ld(0 * 16, state);
9925f7
+  state1 = vec_vsx_ld(1 * 16, state);
9925f7
+  state2 = vec_vsx_ld(2 * 16, state);
9925f7
+  state3 = vec_vsx_ld(3 * 16, state);
9925f7
+
9925f7
+  while (nblks >= 2)
9925f7
+    {
9925f7
+      v0 = state0;
9925f7
+      v1 = state1;
9925f7
+      v2 = state2;
9925f7
+      v3 = state3;
9925f7
+
9925f7
+      v4 = state0;
9925f7
+      v5 = state1;
9925f7
+      v6 = state2;
9925f7
+      v7 = state3;
9925f7
+      ADD_U64(v7, counter_1);
9925f7
+
9925f7
+      for (i = 20; i > 0; i -= 2)
9925f7
+	{
9925f7
+	  QUARTERROUND4(v0, v1, v2, v3, 1, 2, 3);
9925f7
+	  QUARTERROUND4(v4, v5, v6, v7, 1, 2, 3);
9925f7
+	  QUARTERROUND4(v0, v1, v2, v3, 3, 2, 1);
9925f7
+	  QUARTERROUND4(v4, v5, v6, v7, 3, 2, 1);
9925f7
+	}
9925f7
+
9925f7
+      v0 += state0;
9925f7
+      v1 += state1;
9925f7
+      v2 += state2;
9925f7
+      v3 += state3;
9925f7
+      ADD_U64(state3, counter_1); /* update counter */
9925f7
+      v4 += state0;
9925f7
+      v5 += state1;
9925f7
+      v6 += state2;
9925f7
+      v7 += state3;
9925f7
+      ADD_U64(state3, counter_1); /* update counter */
9925f7
+
9925f7
+      v0 ^= vec_load_le(0 * 16, src);
9925f7
+      v1 ^= vec_load_le(1 * 16, src);
9925f7
+      v2 ^= vec_load_le(2 * 16, src);
9925f7
+      v3 ^= vec_load_le(3 * 16, src);
9925f7
+      vec_store_le(v0, 0 * 16, dst);
9925f7
+      vec_store_le(v1, 1 * 16, dst);
9925f7
+      vec_store_le(v2, 2 * 16, dst);
9925f7
+      vec_store_le(v3, 3 * 16, dst);
9925f7
+      src += 64;
9925f7
+      dst += 64;
9925f7
+      v4 ^= vec_load_le(0 * 16, src);
9925f7
+      v5 ^= vec_load_le(1 * 16, src);
9925f7
+      v6 ^= vec_load_le(2 * 16, src);
9925f7
+      v7 ^= vec_load_le(3 * 16, src);
9925f7
+      vec_store_le(v4, 0 * 16, dst);
9925f7
+      vec_store_le(v5, 1 * 16, dst);
9925f7
+      vec_store_le(v6, 2 * 16, dst);
9925f7
+      vec_store_le(v7, 3 * 16, dst);
9925f7
+      src += 64;
9925f7
+      dst += 64;
9925f7
+
9925f7
+      nblks -= 2;
9925f7
+    }
9925f7
+
9925f7
+  while (nblks)
9925f7
+    {
9925f7
+      v0 = state0;
9925f7
+      v1 = state1;
9925f7
+      v2 = state2;
9925f7
+      v3 = state3;
9925f7
+
9925f7
+      for (i = 20; i > 0; i -= 2)
9925f7
+	{
9925f7
+	  QUARTERROUND4(v0, v1, v2, v3, 1, 2, 3);
9925f7
+	  QUARTERROUND4(v0, v1, v2, v3, 3, 2, 1);
9925f7
+	}
9925f7
+
9925f7
+      v0 += state0;
9925f7
+      v1 += state1;
9925f7
+      v2 += state2;
9925f7
+      v3 += state3;
9925f7
+      ADD_U64(state3, counter_1); /* update counter */
9925f7
+
9925f7
+      v0 ^= vec_load_le(0 * 16, src);
9925f7
+      v1 ^= vec_load_le(1 * 16, src);
9925f7
+      v2 ^= vec_load_le(2 * 16, src);
9925f7
+      v3 ^= vec_load_le(3 * 16, src);
9925f7
+      vec_store_le(v0, 0 * 16, dst);
9925f7
+      vec_store_le(v1, 1 * 16, dst);
9925f7
+      vec_store_le(v2, 2 * 16, dst);
9925f7
+      vec_store_le(v3, 3 * 16, dst);
9925f7
+      src += 64;
9925f7
+      dst += 64;
9925f7
+
9925f7
+      nblks--;
9925f7
+    }
9925f7
+
9925f7
+  vec_vsx_st(state3, 3 * 16, state); /* store counter */
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+/**********************************************************************
9925f7
+  4-way chacha20
9925f7
+ **********************************************************************/
9925f7
+
9925f7
+/* 4x4 32-bit integer matrix transpose */
9925f7
+#define transpose_4x4(x0, x1, x2, x3) ({ \
9925f7
+	vector4x_u32 t1 = vec_mergeh(x0, x2); \
9925f7
+	vector4x_u32 t2 = vec_mergel(x0, x2); \
9925f7
+	vector4x_u32 t3 = vec_mergeh(x1, x3); \
9925f7
+	x3 = vec_mergel(x1, x3); \
9925f7
+	x0 = vec_mergeh(t1, t3); \
9925f7
+	x1 = vec_mergel(t1, t3); \
9925f7
+	x2 = vec_mergeh(t2, x3); \
9925f7
+	x3 = vec_mergel(t2, x3); \
9925f7
+      })
9925f7
+
9925f7
+#define QUARTERROUND2(a1,b1,c1,d1,a2,b2,c2,d2)			\
9925f7
+	PLUS(a1,b1); PLUS(a2,b2); XOR(d1,a1); XOR(d2,a2);	\
9925f7
+	    ROTATE(d1, rotate_16); ROTATE(d2, rotate_16);	\
9925f7
+	PLUS(c1,d1); PLUS(c2,d2); XOR(b1,c1); XOR(b2,c2);	\
9925f7
+	    ROTATE(b1, rotate_12); ROTATE(b2, rotate_12);	\
9925f7
+	PLUS(a1,b1); PLUS(a2,b2); XOR(d1,a1); XOR(d2,a2);	\
9925f7
+	    ROTATE(d1, rotate_8); ROTATE(d2, rotate_8);		\
9925f7
+	PLUS(c1,d1); PLUS(c2,d2); XOR(b1,c1); XOR(b2,c2);	\
9925f7
+	    ROTATE(b1, rotate_7); ROTATE(b2, rotate_7);
9925f7
+
9925f7
+unsigned int ASM_FUNC_ATTR
9925f7
+_gcry_chacha20_ppc8_blocks4(u32 *state, byte *dst, const byte *src,
9925f7
+			    size_t nblks)
9925f7
+{
9925f7
+  vector4x_u32 counters_0123 = { 0, 1, 2, 3 };
9925f7
+  vector4x_u32 counter_4 = { 4, 0, 0, 0 };
9925f7
+  vector4x_u32 rotate_16 = { 16, 16, 16, 16 };
9925f7
+  vector4x_u32 rotate_12 = { 12, 12, 12, 12 };
9925f7
+  vector4x_u32 rotate_8 = { 8, 8, 8, 8 };
9925f7
+  vector4x_u32 rotate_7 = { 7, 7, 7, 7 };
9925f7
+  vector4x_u32 state0, state1, state2, state3;
9925f7
+  vector4x_u32 v0, v1, v2, v3, v4, v5, v6, v7;
9925f7
+  vector4x_u32 v8, v9, v10, v11, v12, v13, v14, v15;
9925f7
+  vector4x_u32 tmp;
9925f7
+  int i;
9925f7
+
9925f7
+  /* force preload of constants to vector registers */
9925f7
+  __asm__ ("": "+v" (counters_0123) :: "memory");
9925f7
+  __asm__ ("": "+v" (counter_4) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_16) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_12) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_8) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_7) :: "memory");
9925f7
+
9925f7
+  state0 = vec_vsx_ld(0 * 16, state);
9925f7
+  state1 = vec_vsx_ld(1 * 16, state);
9925f7
+  state2 = vec_vsx_ld(2 * 16, state);
9925f7
+  state3 = vec_vsx_ld(3 * 16, state);
9925f7
+
9925f7
+  do
9925f7
+    {
9925f7
+      v0 = vec_splat(state0, 0);
9925f7
+      v1 = vec_splat(state0, 1);
9925f7
+      v2 = vec_splat(state0, 2);
9925f7
+      v3 = vec_splat(state0, 3);
9925f7
+      v4 = vec_splat(state1, 0);
9925f7
+      v5 = vec_splat(state1, 1);
9925f7
+      v6 = vec_splat(state1, 2);
9925f7
+      v7 = vec_splat(state1, 3);
9925f7
+      v8 = vec_splat(state2, 0);
9925f7
+      v9 = vec_splat(state2, 1);
9925f7
+      v10 = vec_splat(state2, 2);
9925f7
+      v11 = vec_splat(state2, 3);
9925f7
+      v12 = vec_splat(state3, 0);
9925f7
+      v13 = vec_splat(state3, 1);
9925f7
+      v14 = vec_splat(state3, 2);
9925f7
+      v15 = vec_splat(state3, 3);
9925f7
+
9925f7
+      v12 += counters_0123;
9925f7
+      v13 -= vec_cmplt(v12, counters_0123);
9925f7
+
9925f7
+      for (i = 20; i > 0; i -= 2)
9925f7
+	{
9925f7
+	  QUARTERROUND2(v0, v4,  v8, v12,   v1, v5,  v9, v13)
9925f7
+	  QUARTERROUND2(v2, v6, v10, v14,   v3, v7, v11, v15)
9925f7
+	  QUARTERROUND2(v0, v5, v10, v15,   v1, v6, v11, v12)
9925f7
+	  QUARTERROUND2(v2, v7,  v8, v13,   v3, v4,  v9, v14)
9925f7
+	}
9925f7
+
9925f7
+      v0 += vec_splat(state0, 0);
9925f7
+      v1 += vec_splat(state0, 1);
9925f7
+      v2 += vec_splat(state0, 2);
9925f7
+      v3 += vec_splat(state0, 3);
9925f7
+      v4 += vec_splat(state1, 0);
9925f7
+      v5 += vec_splat(state1, 1);
9925f7
+      v6 += vec_splat(state1, 2);
9925f7
+      v7 += vec_splat(state1, 3);
9925f7
+      v8 += vec_splat(state2, 0);
9925f7
+      v9 += vec_splat(state2, 1);
9925f7
+      v10 += vec_splat(state2, 2);
9925f7
+      v11 += vec_splat(state2, 3);
9925f7
+      tmp = vec_splat(state3, 0);
9925f7
+      tmp += counters_0123;
9925f7
+      v12 += tmp;
9925f7
+      v13 += vec_splat(state3, 1) - vec_cmplt(tmp, counters_0123);
9925f7
+      v14 += vec_splat(state3, 2);
9925f7
+      v15 += vec_splat(state3, 3);
9925f7
+      ADD_U64(state3, counter_4); /* update counter */
9925f7
+
9925f7
+      transpose_4x4(v0, v1, v2, v3);
9925f7
+      transpose_4x4(v4, v5, v6, v7);
9925f7
+      transpose_4x4(v8, v9, v10, v11);
9925f7
+      transpose_4x4(v12, v13, v14, v15);
9925f7
+
9925f7
+      v0 ^= vec_load_le((64 * 0 + 16 * 0), src);
9925f7
+      v1 ^= vec_load_le((64 * 1 + 16 * 0), src);
9925f7
+      v2 ^= vec_load_le((64 * 2 + 16 * 0), src);
9925f7
+      v3 ^= vec_load_le((64 * 3 + 16 * 0), src);
9925f7
+
9925f7
+      v4 ^= vec_load_le((64 * 0 + 16 * 1), src);
9925f7
+      v5 ^= vec_load_le((64 * 1 + 16 * 1), src);
9925f7
+      v6 ^= vec_load_le((64 * 2 + 16 * 1), src);
9925f7
+      v7 ^= vec_load_le((64 * 3 + 16 * 1), src);
9925f7
+
9925f7
+      v8 ^= vec_load_le((64 * 0 + 16 * 2), src);
9925f7
+      v9 ^= vec_load_le((64 * 1 + 16 * 2), src);
9925f7
+      v10 ^= vec_load_le((64 * 2 + 16 * 2), src);
9925f7
+      v11 ^= vec_load_le((64 * 3 + 16 * 2), src);
9925f7
+
9925f7
+      v12 ^= vec_load_le((64 * 0 + 16 * 3), src);
9925f7
+      v13 ^= vec_load_le((64 * 1 + 16 * 3), src);
9925f7
+      v14 ^= vec_load_le((64 * 2 + 16 * 3), src);
9925f7
+      v15 ^= vec_load_le((64 * 3 + 16 * 3), src);
9925f7
+
9925f7
+      vec_store_le(v0, (64 * 0 + 16 * 0), dst);
9925f7
+      vec_store_le(v1, (64 * 1 + 16 * 0), dst);
9925f7
+      vec_store_le(v2, (64 * 2 + 16 * 0), dst);
9925f7
+      vec_store_le(v3, (64 * 3 + 16 * 0), dst);
9925f7
+
9925f7
+      vec_store_le(v4, (64 * 0 + 16 * 1), dst);
9925f7
+      vec_store_le(v5, (64 * 1 + 16 * 1), dst);
9925f7
+      vec_store_le(v6, (64 * 2 + 16 * 1), dst);
9925f7
+      vec_store_le(v7, (64 * 3 + 16 * 1), dst);
9925f7
+
9925f7
+      vec_store_le(v8, (64 * 0 + 16 * 2), dst);
9925f7
+      vec_store_le(v9, (64 * 1 + 16 * 2), dst);
9925f7
+      vec_store_le(v10, (64 * 2 + 16 * 2), dst);
9925f7
+      vec_store_le(v11, (64 * 3 + 16 * 2), dst);
9925f7
+
9925f7
+      vec_store_le(v12, (64 * 0 + 16 * 3), dst);
9925f7
+      vec_store_le(v13, (64 * 1 + 16 * 3), dst);
9925f7
+      vec_store_le(v14, (64 * 2 + 16 * 3), dst);
9925f7
+      vec_store_le(v15, (64 * 3 + 16 * 3), dst);
9925f7
+
9925f7
+      src += 4*64;
9925f7
+      dst += 4*64;
9925f7
+
9925f7
+      nblks -= 4;
9925f7
+    }
9925f7
+  while (nblks);
9925f7
+
9925f7
+  vec_vsx_st(state3, 3 * 16, state); /* store counter */
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+#if SIZEOF_UNSIGNED_LONG == 8
9925f7
+
9925f7
+/**********************************************************************
9925f7
+  4-way stitched chacha20-poly1305
9925f7
+ **********************************************************************/
9925f7
+
9925f7
+#define ADD_1305_64(A2, A1, A0, B2, B1, B0) \
9925f7
+      __asm__ ("addc %0, %3, %0\n" \
9925f7
+	       "adde %1, %4, %1\n" \
9925f7
+	       "adde %2, %5, %2\n" \
9925f7
+	       : "+r" (A0), "+r" (A1), "+r" (A2) \
9925f7
+	       : "r" (B0), "r" (B1), "r" (B2) \
9925f7
+	       : "cc" )
9925f7
+
9925f7
+#define MUL_MOD_1305_64_PART1(H2, H1, H0, R1, R0, R1_MULT5) do { \
9925f7
+    /* x = a * r (partial mod 2^130-5) */ \
9925f7
+    umul_ppmm(x0_hi, x0_lo, H0, R0);  /* h0 * r0 */ \
9925f7
+    umul_ppmm(x1_hi, x1_lo, H0, R1);  /* h0 * r1 */ \
9925f7
+    \
9925f7
+    umul_ppmm(t0_hi, t0_lo, H1, R1_MULT5); /* h1 * r1 mod 2^130-5 */ \
9925f7
+  } while (0)
9925f7
+
9925f7
+#define MUL_MOD_1305_64_PART2(H2, H1, H0, R1, R0, R1_MULT5) do { \
9925f7
+    add_ssaaaa(x0_hi, x0_lo, x0_hi, x0_lo, t0_hi, t0_lo); \
9925f7
+    umul_ppmm(t1_hi, t1_lo, H1, R0);       /* h1 * r0 */ \
9925f7
+    add_ssaaaa(x1_hi, x1_lo, x1_hi, x1_lo, t1_hi, t1_lo); \
9925f7
+    \
9925f7
+    t1_lo = H2 * R1_MULT5; /* h2 * r1 mod 2^130-5 */ \
9925f7
+    t1_hi = H2 * R0;       /* h2 * r0 */ \
9925f7
+    add_ssaaaa(H0, H1, x1_hi, x1_lo, t1_hi, t1_lo); \
9925f7
+    \
9925f7
+    /* carry propagation */ \
9925f7
+    H2 = H0 & 3; \
9925f7
+    H0 = (H0 >> 2) * 5; /* msb mod 2^130-5 */ \
9925f7
+    ADD_1305_64(H2, H1, H0, (u64)0, x0_hi, x0_lo); \
9925f7
+  } while (0)
9925f7
+
9925f7
+#define POLY1305_BLOCK_PART1(in_pos) do { \
9925f7
+    m0 = buf_get_le64(poly1305_src + (in_pos) + 0); \
9925f7
+    m1 = buf_get_le64(poly1305_src + (in_pos) + 8); \
9925f7
+    /* a = h + m */ \
9925f7
+    ADD_1305_64(h2, h1, h0, m2, m1, m0); \
9925f7
+    /* h = a * r (partial mod 2^130-5) */ \
9925f7
+    MUL_MOD_1305_64_PART1(h2, h1, h0, r1, r0, r1_mult5); \
9925f7
+  } while (0)
9925f7
+
9925f7
+#define POLY1305_BLOCK_PART2(in_pos) do { \
9925f7
+    MUL_MOD_1305_64_PART2(h2, h1, h0, r1, r0, r1_mult5); \
9925f7
+  } while (0)
9925f7
+
9925f7
+unsigned int ASM_FUNC_ATTR
9925f7
+_gcry_chacha20_poly1305_ppc8_blocks4(u32 *state, byte *dst, const byte *src,
9925f7
+				     size_t nblks, POLY1305_STATE *st,
9925f7
+				     const byte *poly1305_src)
9925f7
+{
9925f7
+  vector4x_u32 counters_0123 = { 0, 1, 2, 3 };
9925f7
+  vector4x_u32 counter_4 = { 4, 0, 0, 0 };
9925f7
+  vector4x_u32 rotate_16 = { 16, 16, 16, 16 };
9925f7
+  vector4x_u32 rotate_12 = { 12, 12, 12, 12 };
9925f7
+  vector4x_u32 rotate_8 = { 8, 8, 8, 8 };
9925f7
+  vector4x_u32 rotate_7 = { 7, 7, 7, 7 };
9925f7
+  vector4x_u32 state0, state1, state2, state3;
9925f7
+  vector4x_u32 v0, v1, v2, v3, v4, v5, v6, v7;
9925f7
+  vector4x_u32 v8, v9, v10, v11, v12, v13, v14, v15;
9925f7
+  vector4x_u32 tmp;
9925f7
+  u64 r0, r1, r1_mult5;
9925f7
+  u64 h0, h1, h2;
9925f7
+  u64 m0, m1, m2;
9925f7
+  u64 x0_lo, x0_hi, x1_lo, x1_hi;
9925f7
+  u64 t0_lo, t0_hi, t1_lo, t1_hi;
9925f7
+  unsigned int i, o;
9925f7
+
9925f7
+  /* load poly1305 state */
9925f7
+  m2 = 1;
9925f7
+  h0 = st->h[0] + ((u64)st->h[1] << 32);
9925f7
+  h1 = st->h[2] + ((u64)st->h[3] << 32);
9925f7
+  h2 = st->h[4];
9925f7
+  r0 = st->r[0] + ((u64)st->r[1] << 32);
9925f7
+  r1 = st->r[2] + ((u64)st->r[3] << 32);
9925f7
+  r1_mult5 = (r1 >> 2) + r1;
9925f7
+
9925f7
+  /* force preload of constants to vector registers */
9925f7
+  __asm__ ("": "+v" (counters_0123) :: "memory");
9925f7
+  __asm__ ("": "+v" (counter_4) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_16) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_12) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_8) :: "memory");
9925f7
+  __asm__ ("": "+v" (rotate_7) :: "memory");
9925f7
+
9925f7
+  state0 = vec_vsx_ld(0 * 16, state);
9925f7
+  state1 = vec_vsx_ld(1 * 16, state);
9925f7
+  state2 = vec_vsx_ld(2 * 16, state);
9925f7
+  state3 = vec_vsx_ld(3 * 16, state);
9925f7
+
9925f7
+  do
9925f7
+    {
9925f7
+      v0 = vec_splat(state0, 0);
9925f7
+      v1 = vec_splat(state0, 1);
9925f7
+      v2 = vec_splat(state0, 2);
9925f7
+      v3 = vec_splat(state0, 3);
9925f7
+      v4 = vec_splat(state1, 0);
9925f7
+      v5 = vec_splat(state1, 1);
9925f7
+      v6 = vec_splat(state1, 2);
9925f7
+      v7 = vec_splat(state1, 3);
9925f7
+      v8 = vec_splat(state2, 0);
9925f7
+      v9 = vec_splat(state2, 1);
9925f7
+      v10 = vec_splat(state2, 2);
9925f7
+      v11 = vec_splat(state2, 3);
9925f7
+      v12 = vec_splat(state3, 0);
9925f7
+      v13 = vec_splat(state3, 1);
9925f7
+      v14 = vec_splat(state3, 2);
9925f7
+      v15 = vec_splat(state3, 3);
9925f7
+
9925f7
+      v12 += counters_0123;
9925f7
+      v13 -= vec_cmplt(v12, counters_0123);
9925f7
+
9925f7
+      for (o = 20; o; o -= 10)
9925f7
+	{
9925f7
+	  for (i = 8; i; i -= 2)
9925f7
+	    {
9925f7
+	      POLY1305_BLOCK_PART1(0 * 16);
9925f7
+	      QUARTERROUND2(v0, v4,  v8, v12,   v1, v5,  v9, v13)
9925f7
+	      POLY1305_BLOCK_PART2();
9925f7
+	      QUARTERROUND2(v2, v6, v10, v14,   v3, v7, v11, v15)
9925f7
+	      POLY1305_BLOCK_PART1(1 * 16);
9925f7
+	      poly1305_src += 2 * 16;
9925f7
+	      QUARTERROUND2(v0, v5, v10, v15,   v1, v6, v11, v12)
9925f7
+	      POLY1305_BLOCK_PART2();
9925f7
+	      QUARTERROUND2(v2, v7,  v8, v13,   v3, v4,  v9, v14)
9925f7
+	    }
9925f7
+
9925f7
+	  QUARTERROUND2(v0, v4,  v8, v12,   v1, v5,  v9, v13)
9925f7
+	  QUARTERROUND2(v2, v6, v10, v14,   v3, v7, v11, v15)
9925f7
+	  QUARTERROUND2(v0, v5, v10, v15,   v1, v6, v11, v12)
9925f7
+	  QUARTERROUND2(v2, v7,  v8, v13,   v3, v4,  v9, v14)
9925f7
+	}
9925f7
+
9925f7
+      v0 += vec_splat(state0, 0);
9925f7
+      v1 += vec_splat(state0, 1);
9925f7
+      v2 += vec_splat(state0, 2);
9925f7
+      v3 += vec_splat(state0, 3);
9925f7
+      v4 += vec_splat(state1, 0);
9925f7
+      v5 += vec_splat(state1, 1);
9925f7
+      v6 += vec_splat(state1, 2);
9925f7
+      v7 += vec_splat(state1, 3);
9925f7
+      v8 += vec_splat(state2, 0);
9925f7
+      v9 += vec_splat(state2, 1);
9925f7
+      v10 += vec_splat(state2, 2);
9925f7
+      v11 += vec_splat(state2, 3);
9925f7
+      tmp = vec_splat(state3, 0);
9925f7
+      tmp += counters_0123;
9925f7
+      v12 += tmp;
9925f7
+      v13 += vec_splat(state3, 1) - vec_cmplt(tmp, counters_0123);
9925f7
+      v14 += vec_splat(state3, 2);
9925f7
+      v15 += vec_splat(state3, 3);
9925f7
+      ADD_U64(state3, counter_4); /* update counter */
9925f7
+
9925f7
+      transpose_4x4(v0, v1, v2, v3);
9925f7
+      transpose_4x4(v4, v5, v6, v7);
9925f7
+      transpose_4x4(v8, v9, v10, v11);
9925f7
+      transpose_4x4(v12, v13, v14, v15);
9925f7
+
9925f7
+      v0 ^= vec_load_le((64 * 0 + 16 * 0), src);
9925f7
+      v1 ^= vec_load_le((64 * 1 + 16 * 0), src);
9925f7
+      v2 ^= vec_load_le((64 * 2 + 16 * 0), src);
9925f7
+      v3 ^= vec_load_le((64 * 3 + 16 * 0), src);
9925f7
+
9925f7
+      v4 ^= vec_load_le((64 * 0 + 16 * 1), src);
9925f7
+      v5 ^= vec_load_le((64 * 1 + 16 * 1), src);
9925f7
+      v6 ^= vec_load_le((64 * 2 + 16 * 1), src);
9925f7
+      v7 ^= vec_load_le((64 * 3 + 16 * 1), src);
9925f7
+
9925f7
+      v8 ^= vec_load_le((64 * 0 + 16 * 2), src);
9925f7
+      v9 ^= vec_load_le((64 * 1 + 16 * 2), src);
9925f7
+      v10 ^= vec_load_le((64 * 2 + 16 * 2), src);
9925f7
+      v11 ^= vec_load_le((64 * 3 + 16 * 2), src);
9925f7
+
9925f7
+      v12 ^= vec_load_le((64 * 0 + 16 * 3), src);
9925f7
+      v13 ^= vec_load_le((64 * 1 + 16 * 3), src);
9925f7
+      v14 ^= vec_load_le((64 * 2 + 16 * 3), src);
9925f7
+      v15 ^= vec_load_le((64 * 3 + 16 * 3), src);
9925f7
+
9925f7
+      vec_store_le(v0, (64 * 0 + 16 * 0), dst);
9925f7
+      vec_store_le(v1, (64 * 1 + 16 * 0), dst);
9925f7
+      vec_store_le(v2, (64 * 2 + 16 * 0), dst);
9925f7
+      vec_store_le(v3, (64 * 3 + 16 * 0), dst);
9925f7
+
9925f7
+      vec_store_le(v4, (64 * 0 + 16 * 1), dst);
9925f7
+      vec_store_le(v5, (64 * 1 + 16 * 1), dst);
9925f7
+      vec_store_le(v6, (64 * 2 + 16 * 1), dst);
9925f7
+      vec_store_le(v7, (64 * 3 + 16 * 1), dst);
9925f7
+
9925f7
+      vec_store_le(v8, (64 * 0 + 16 * 2), dst);
9925f7
+      vec_store_le(v9, (64 * 1 + 16 * 2), dst);
9925f7
+      vec_store_le(v10, (64 * 2 + 16 * 2), dst);
9925f7
+      vec_store_le(v11, (64 * 3 + 16 * 2), dst);
9925f7
+
9925f7
+      vec_store_le(v12, (64 * 0 + 16 * 3), dst);
9925f7
+      vec_store_le(v13, (64 * 1 + 16 * 3), dst);
9925f7
+      vec_store_le(v14, (64 * 2 + 16 * 3), dst);
9925f7
+      vec_store_le(v15, (64 * 3 + 16 * 3), dst);
9925f7
+
9925f7
+      src += 4*64;
9925f7
+      dst += 4*64;
9925f7
+
9925f7
+      nblks -= 4;
9925f7
+    }
9925f7
+  while (nblks);
9925f7
+
9925f7
+  vec_vsx_st(state3, 3 * 16, state); /* store counter */
9925f7
+
9925f7
+  /* store poly1305 state */
9925f7
+  st->h[0] = h0;
9925f7
+  st->h[1] = h0 >> 32;
9925f7
+  st->h[2] = h1;
9925f7
+  st->h[3] = h1 >> 32;
9925f7
+  st->h[4] = h2;
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+#endif /* SIZEOF_UNSIGNED_LONG == 8 */
9925f7
+
9925f7
+#endif /* ENABLE_PPC_CRYPTO_SUPPORT */
9925f7
diff --git a/cipher/chacha20.c b/cipher/chacha20.c
9925f7
index ebbfeb24..8eefba7d 100644
9925f7
--- a/cipher/chacha20.c
9925f7
+++ b/cipher/chacha20.c
9925f7
@@ -31,6 +31,11 @@
9925f7
 
9925f7
 
9925f7
 #include <config.h>
9925f7
+
9925f7
+#if !defined(ENABLE_PPC_CRYPTO_SUPPORT) || \
9925f7
+    !defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) || \
9925f7
+    !defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC)
9925f7
+
9925f7
 #include <stdio.h>
9925f7
 #include <stdlib.h>
9925f7
 #include <string.h>
9925f7
@@ -637,3 +642,5 @@ gcry_cipher_spec_t _gcry_cipher_spec_chacha20 = {
9925f7
   NULL,
9925f7
   chacha20_setiv
9925f7
 };
9925f7
+
9925f7
+#endif /* ENABLE_PPC_CRYPTO_SUPPORT */
9925f7
diff --git a/cipher/cipher-internal.h b/cipher/cipher-internal.h
9925f7
index a5fd3097..cd45e0a5 100644
9925f7
--- a/cipher/cipher-internal.h
9925f7
+++ b/cipher/cipher-internal.h
9925f7
@@ -20,8 +20,15 @@
9925f7
 #ifndef G10_CIPHER_INTERNAL_H
9925f7
 #define G10_CIPHER_INTERNAL_H
9925f7
 
9925f7
+#if defined(ENABLE_PPC_CRYPTO_SUPPORT) && \
9925f7
+    defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
9925f7
+    defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \
9925f7
+    defined(USE_CHACHA20) && \
9925f7
+    __GNUC__ >= 4
9925f7
+#include "./poly1305-internal-new.h"
9925f7
+#else
9925f7
 #include "./poly1305-internal.h"
9925f7
-
9925f7
+#endif /* ENABLE_PPC_CRYPTO_SUPPORT */
9925f7
 
9925f7
 /* The maximum supported size of a block in bytes.  */
9925f7
 #define MAX_BLOCKSIZE 16
9925f7
diff --git a/cipher/mpi-new/mpi-asm-defs.h b/cipher/mpi-new/mpi-asm-defs.h
9925f7
new file mode 100644
9925f7
index 00000000..e607806e
9925f7
--- /dev/null
9925f7
+++ b/cipher/mpi-new/mpi-asm-defs.h
9925f7
@@ -0,0 +1,8 @@
9925f7
+/* This file defines some basic constants for the MPI machinery.
9925f7
+ * AMD64 compiled for the x32 ABI is special and thus we can't use the
9925f7
+ * standard values for this ABI.  */
9925f7
+#if __GNUC__ >= 3 && defined(__x86_64__) && defined(__ILP32__)
9925f7
+#define BYTES_PER_MPI_LIMB 8
9925f7
+#else
9925f7
+#define BYTES_PER_MPI_LIMB  (SIZEOF_UNSIGNED_LONG)
9925f7
+#endif
9925f7
diff --git a/cipher/mpi-new/mpi-inline.h b/cipher/mpi-new/mpi-inline.h
9925f7
new file mode 100644
9925f7
index 00000000..94e2aec8
9925f7
--- /dev/null
9925f7
+++ b/cipher/mpi-new/mpi-inline.h
9925f7
@@ -0,0 +1,161 @@
9925f7
+/* mpi-inline.h  -  Internal to the Multi Precision Integers
9925f7
+ * Copyright (C) 1994, 1996, 1998, 1999,
9925f7
+ *               2001, 2002 Free Software Foundation, Inc.
9925f7
+ *
9925f7
+ * This file is part of Libgcrypt.
9925f7
+ *
9925f7
+ * Libgcrypt is free software; you can redistribute it and/or modify
9925f7
+ * it under the terms of the GNU Lesser General Public License as
9925f7
+ * published by the Free Software Foundation; either version 2.1 of
9925f7
+ * the License, or (at your option) any later version.
9925f7
+ *
9925f7
+ * Libgcrypt is distributed in the hope that it will be useful,
9925f7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9925f7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925f7
+ * GNU Lesser General Public License for more details.
9925f7
+ *
9925f7
+ * You should have received a copy of the GNU Lesser General Public
9925f7
+ * License along with this program; if not, write to the Free Software
9925f7
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
9925f7
+ *
9925f7
+ * Note: This code is heavily based on the GNU MP Library.
9925f7
+ *	 Actually it's the same code with only minor changes in the
9925f7
+ *	 way the data is stored; this is to support the abstraction
9925f7
+ *	 of an optional secure memory allocation which may be used
9925f7
+ *	 to avoid revealing of sensitive data due to paging etc.
9925f7
+ */
9925f7
+
9925f7
+#ifndef G10_MPI_INLINE_H
9925f7
+#define G10_MPI_INLINE_H
9925f7
+
9925f7
+/* Starting with gcc 4.3 "extern inline" conforms in c99 mode to the
9925f7
+   c99 semantics.  To keep the useful old semantics we use an
9925f7
+   attribute.  */
9925f7
+#ifndef G10_MPI_INLINE_DECL
9925f7
+# ifdef __GNUC_STDC_INLINE__
9925f7
+#  define G10_MPI_INLINE_DECL  extern inline __attribute__ ((__gnu_inline__))
9925f7
+# else
9925f7
+#  define G10_MPI_INLINE_DECL  extern __inline__
9925f7
+# endif
9925f7
+#endif
9925f7
+
9925f7
+G10_MPI_INLINE_DECL  mpi_limb_t
9925f7
+_gcry_mpih_add_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
9925f7
+	       mpi_size_t s1_size, mpi_limb_t s2_limb)
9925f7
+{
9925f7
+    mpi_limb_t x;
9925f7
+
9925f7
+    x = *s1_ptr++;
9925f7
+    s2_limb += x;
9925f7
+    *res_ptr++ = s2_limb;
9925f7
+    if( s2_limb < x ) { /* sum is less than the left operand: handle carry */
9925f7
+	while( --s1_size ) {
9925f7
+	    x = *s1_ptr++ + 1;	/* add carry */
9925f7
+	    *res_ptr++ = x;	/* and store */
9925f7
+	    if( x )		/* not 0 (no overflow): we can stop */
9925f7
+		goto leave;
9925f7
+	}
9925f7
+	return 1; /* return carry (size of s1 to small) */
9925f7
+    }
9925f7
+
9925f7
+  leave:
9925f7
+    if( res_ptr != s1_ptr ) { /* not the same variable */
9925f7
+	mpi_size_t i;	       /* copy the rest */
9925f7
+	for( i=0; i < s1_size-1; i++ )
9925f7
+	    res_ptr[i] = s1_ptr[i];
9925f7
+    }
9925f7
+    return 0; /* no carry */
9925f7
+}
9925f7
+
9925f7
+
9925f7
+
9925f7
+G10_MPI_INLINE_DECL mpi_limb_t
9925f7
+_gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
9925f7
+			       mpi_ptr_t s2_ptr, mpi_size_t s2_size)
9925f7
+{
9925f7
+    mpi_limb_t cy = 0;
9925f7
+
9925f7
+    if( s2_size )
9925f7
+	cy = _gcry_mpih_add_n( res_ptr, s1_ptr, s2_ptr, s2_size );
9925f7
+
9925f7
+    if( s1_size - s2_size )
9925f7
+	cy = _gcry_mpih_add_1( res_ptr + s2_size, s1_ptr + s2_size,
9925f7
+			    s1_size - s2_size, cy);
9925f7
+    return cy;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+G10_MPI_INLINE_DECL mpi_limb_t
9925f7
+_gcry_mpih_sub_1(mpi_ptr_t res_ptr,  mpi_ptr_t s1_ptr,
9925f7
+	      mpi_size_t s1_size, mpi_limb_t s2_limb )
9925f7
+{
9925f7
+    mpi_limb_t x;
9925f7
+
9925f7
+    x = *s1_ptr++;
9925f7
+    s2_limb = x - s2_limb;
9925f7
+    *res_ptr++ = s2_limb;
9925f7
+    if( s2_limb > x ) {
9925f7
+	while( --s1_size ) {
9925f7
+	    x = *s1_ptr++;
9925f7
+	    *res_ptr++ = x - 1;
9925f7
+	    if( x )
9925f7
+		goto leave;
9925f7
+	}
9925f7
+	return 1;
9925f7
+    }
9925f7
+
9925f7
+  leave:
9925f7
+    if( res_ptr != s1_ptr ) {
9925f7
+	mpi_size_t i;
9925f7
+	for( i=0; i < s1_size-1; i++ )
9925f7
+	    res_ptr[i] = s1_ptr[i];
9925f7
+    }
9925f7
+    return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+
9925f7
+G10_MPI_INLINE_DECL   mpi_limb_t
9925f7
+_gcry_mpih_sub( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
9925f7
+				mpi_ptr_t s2_ptr, mpi_size_t s2_size)
9925f7
+{
9925f7
+    mpi_limb_t cy = 0;
9925f7
+
9925f7
+    if( s2_size )
9925f7
+	cy = _gcry_mpih_sub_n(res_ptr, s1_ptr, s2_ptr, s2_size);
9925f7
+
9925f7
+    if( s1_size - s2_size )
9925f7
+	cy = _gcry_mpih_sub_1(res_ptr + s2_size, s1_ptr + s2_size,
9925f7
+				      s1_size - s2_size, cy);
9925f7
+    return cy;
9925f7
+}
9925f7
+
9925f7
+/****************
9925f7
+ * Compare OP1_PTR/OP1_SIZE with OP2_PTR/OP2_SIZE.
9925f7
+ * There are no restrictions on the relative sizes of
9925f7
+ * the two arguments.
9925f7
+ * Return 1 if OP1 > OP2, 0 if they are equal, and -1 if OP1 < OP2.
9925f7
+ */
9925f7
+G10_MPI_INLINE_DECL int
9925f7
+_gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size )
9925f7
+{
9925f7
+    mpi_size_t i;
9925f7
+    mpi_limb_t op1_word, op2_word;
9925f7
+
9925f7
+    for( i = size - 1; i >= 0 ; i--) {
9925f7
+	op1_word = op1_ptr[i];
9925f7
+	op2_word = op2_ptr[i];
9925f7
+	if( op1_word != op2_word )
9925f7
+	    goto diff;
9925f7
+    }
9925f7
+    return 0;
9925f7
+
9925f7
+  diff:
9925f7
+    /* This can *not* be simplified to
9925f7
+     *	 op2_word - op2_word
9925f7
+     * since that expression might give signed overflow.  */
9925f7
+    return (op1_word > op2_word) ? 1 : -1;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+#endif /*G10_MPI_INLINE_H*/
9925f7
diff --git a/cipher/mpi-new/mpi-internal.h b/cipher/mpi-new/mpi-internal.h
9925f7
new file mode 100644
9925f7
index 00000000..11fcbde4
9925f7
--- /dev/null
9925f7
+++ b/cipher/mpi-new/mpi-internal.h
9925f7
@@ -0,0 +1,305 @@
9925f7
+/* mpi-internal.h  -  Internal to the Multi Precision Integers
9925f7
+ * Copyright (C) 1994, 1996, 1998, 2000, 2002,
9925f7
+ *               2003 Free Software Foundation, Inc.
9925f7
+ *
9925f7
+ * This file is part of Libgcrypt.
9925f7
+ *
9925f7
+ * Libgcrypt is free software; you can redistribute it and/or modify
9925f7
+ * it under the terms of the GNU Lesser General Public License as
9925f7
+ * published by the Free Software Foundation; either version 2.1 of
9925f7
+ * the License, or (at your option) any later version.
9925f7
+ *
9925f7
+ * Libgcrypt is distributed in the hope that it will be useful,
9925f7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9925f7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925f7
+ * GNU Lesser General Public License for more details.
9925f7
+ *
9925f7
+ * You should have received a copy of the GNU Lesser General Public
9925f7
+ * License along with this program; if not, write to the Free Software
9925f7
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
9925f7
+ *
9925f7
+ * Note: This code is heavily based on the GNU MP Library.
9925f7
+ *	 Actually it's the same code with only minor changes in the
9925f7
+ *	 way the data is stored; this is to support the abstraction
9925f7
+ *	 of an optional secure memory allocation which may be used
9925f7
+ *	 to avoid revealing of sensitive data due to paging etc.
9925f7
+ */
9925f7
+
9925f7
+#ifndef G10_MPI_INTERNAL_H
9925f7
+#define G10_MPI_INTERNAL_H
9925f7
+
9925f7
+#include "mpi-asm-defs.h"
9925f7
+
9925f7
+#ifndef BITS_PER_MPI_LIMB
9925f7
+#if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
9925f7
+  typedef unsigned int mpi_limb_t;
9925f7
+  typedef   signed int mpi_limb_signed_t;
9925f7
+#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
9925f7
+  typedef unsigned long int mpi_limb_t;
9925f7
+  typedef   signed long int mpi_limb_signed_t;
9925f7
+#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
9925f7
+  typedef unsigned long long int mpi_limb_t;
9925f7
+  typedef   signed long long int mpi_limb_signed_t;
9925f7
+#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
9925f7
+  typedef unsigned short int mpi_limb_t;
9925f7
+  typedef   signed short int mpi_limb_signed_t;
9925f7
+#else
9925f7
+#error BYTES_PER_MPI_LIMB does not match any C type
9925f7
+#endif
9925f7
+#define BITS_PER_MPI_LIMB    (8*BYTES_PER_MPI_LIMB)
9925f7
+#endif /*BITS_PER_MPI_LIMB*/
9925f7
+
9925f7
+#include "mpi.h"
9925f7
+
9925f7
+/* If KARATSUBA_THRESHOLD is not already defined, define it to a
9925f7
+ * value which is good on most machines.  */
9925f7
+
9925f7
+/* tested 4, 16, 32 and 64, where 16 gave the best performance when
9925f7
+ * checking a 768 and a 1024 bit ElGamal signature.
9925f7
+ * (wk 22.12.97) */
9925f7
+#ifndef KARATSUBA_THRESHOLD
9925f7
+#define KARATSUBA_THRESHOLD 16
9925f7
+#endif
9925f7
+
9925f7
+/* The code can't handle KARATSUBA_THRESHOLD smaller than 2.  */
9925f7
+#if KARATSUBA_THRESHOLD < 2
9925f7
+#undef KARATSUBA_THRESHOLD
9925f7
+#define KARATSUBA_THRESHOLD 2
9925f7
+#endif
9925f7
+
9925f7
+
9925f7
+typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
9925f7
+typedef int mpi_size_t;        /* (must be a signed type) */
9925f7
+
9925f7
+#define ABS(x) (x >= 0 ? x : -x)
9925f7
+#define MIN(l,o) ((l) < (o) ? (l) : (o))
9925f7
+#define MAX(h,i) ((h) > (i) ? (h) : (i))
9925f7
+#define RESIZE_IF_NEEDED(a,b) \
9925f7
+    do {			   \
9925f7
+	if( (a)->alloced < (b) )   \
9925f7
+	    mpi_resize((a), (b));  \
9925f7
+    } while(0)
9925f7
+#define RESIZE_AND_CLEAR_IF_NEEDED(a,b) \
9925f7
+    do {			   \
9925f7
+	if( (a)->nlimbs < (b) )   \
9925f7
+	    mpi_resize((a), (b));  \
9925f7
+    } while(0)
9925f7
+
9925f7
+/* Copy N limbs from S to D.  */
9925f7
+#define MPN_COPY( d, s, n) \
9925f7
+    do {				\
9925f7
+	mpi_size_t _i;			\
9925f7
+	for( _i = 0; _i < (n); _i++ )	\
9925f7
+	    (d)[_i] = (s)[_i];		\
9925f7
+    } while(0)
9925f7
+
9925f7
+#define MPN_COPY_INCR( d, s, n) 	\
9925f7
+    do {				\
9925f7
+	mpi_size_t _i;			\
9925f7
+	for( _i = 0; _i < (n); _i++ )	\
9925f7
+	    (d)[_i] = (s)[_i];		\
9925f7
+    } while (0)
9925f7
+
9925f7
+#define MPN_COPY_DECR( d, s, n ) \
9925f7
+    do {				\
9925f7
+	mpi_size_t _i;			\
9925f7
+	for( _i = (n)-1; _i >= 0; _i--) \
9925f7
+	   (d)[_i] = (s)[_i];		\
9925f7
+    } while(0)
9925f7
+
9925f7
+/* Zero N limbs at D */
9925f7
+#define MPN_ZERO(d, n) \
9925f7
+    do {				  \
9925f7
+	int  _i;			  \
9925f7
+	for( _i = 0; _i < (n); _i++ )  \
9925f7
+	    (d)[_i] = 0;		    \
9925f7
+    } while (0)
9925f7
+
9925f7
+#define MPN_NORMALIZE(d, n)  \
9925f7
+    do {		       \
9925f7
+	while( (n) > 0 ) {     \
9925f7
+	    if( (d)[(n)-1] ) \
9925f7
+		break;	       \
9925f7
+	    (n)--;	       \
9925f7
+	}		       \
9925f7
+    } while(0)
9925f7
+
9925f7
+#define MPN_NORMALIZE_NOT_ZERO(d, n) \
9925f7
+    do {				    \
9925f7
+	for(;;) {			    \
9925f7
+	    if( (d)[(n)-1] )		    \
9925f7
+		break;			    \
9925f7
+	    (n)--;			    \
9925f7
+	}				    \
9925f7
+    } while(0)
9925f7
+
9925f7
+#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
9925f7
+    do {						\
9925f7
+	if( (size) < KARATSUBA_THRESHOLD )		\
9925f7
+	    mul_n_basecase (prodp, up, vp, size);	\
9925f7
+	else						\
9925f7
+	    mul_n (prodp, up, vp, size, tspace);	\
9925f7
+    } while (0);
9925f7
+
9925f7
+
9925f7
+/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
9925f7
+ * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
9925f7
+ * If this would yield overflow, DI should be the largest possible number
9925f7
+ * (i.e., only ones).  For correct operation, the most significant bit of D
9925f7
+ * has to be set.  Put the quotient in Q and the remainder in R.
9925f7
+ */
9925f7
+#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
9925f7
+    do {							    \
9925f7
+        mpi_limb_t _ql GCC_ATTR_UNUSED;                               \
9925f7
+	mpi_limb_t _q, _r;                                          \
9925f7
+	mpi_limb_t _xh, _xl;					    \
9925f7
+	umul_ppmm (_q, _ql, (nh), (di));			    \
9925f7
+	_q += (nh);	/* DI is 2**BITS_PER_MPI_LIMB too small */  \
9925f7
+	umul_ppmm (_xh, _xl, _q, (d));				    \
9925f7
+	sub_ddmmss (_xh, _r, (nh), (nl), _xh, _xl);		    \
9925f7
+	if( _xh ) {						    \
9925f7
+	    sub_ddmmss (_xh, _r, _xh, _r, 0, (d));		    \
9925f7
+	    _q++;						    \
9925f7
+	    if( _xh) {						    \
9925f7
+		sub_ddmmss (_xh, _r, _xh, _r, 0, (d));		    \
9925f7
+		_q++;						    \
9925f7
+	    }							    \
9925f7
+	}							    \
9925f7
+	if( _r >= (d) ) {					    \
9925f7
+	    _r -= (d);						    \
9925f7
+	    _q++;						    \
9925f7
+	}							    \
9925f7
+	(r) = _r;						    \
9925f7
+	(q) = _q;						    \
9925f7
+    } while (0)
9925f7
+
9925f7
+
9925f7
+/*-- mpiutil.c --*/
9925f7
+#define mpi_alloc_limb_space(n,f)  _gcry_mpi_alloc_limb_space((n),(f))
9925f7
+mpi_ptr_t _gcry_mpi_alloc_limb_space( unsigned nlimbs, int sec );
9925f7
+void _gcry_mpi_free_limb_space( mpi_ptr_t a, unsigned int nlimbs );
9925f7
+void _gcry_mpi_assign_limb_space( gcry_mpi_t a, mpi_ptr_t ap, unsigned nlimbs );
9925f7
+
9925f7
+/*-- mpi-bit.c --*/
9925f7
+#define mpi_rshift_limbs(a,n)  _gcry_mpi_rshift_limbs ((a), (n))
9925f7
+#define mpi_lshift_limbs(a,n)  _gcry_mpi_lshift_limbs ((a), (n))
9925f7
+
9925f7
+void _gcry_mpi_rshift_limbs( gcry_mpi_t a, unsigned int count );
9925f7
+void _gcry_mpi_lshift_limbs( gcry_mpi_t a, unsigned int count );
9925f7
+
9925f7
+
9925f7
+/*-- mpih-add.c --*/
9925f7
+mpi_limb_t _gcry_mpih_add_1(mpi_ptr_t res_ptr,  mpi_ptr_t s1_ptr,
9925f7
+			 mpi_size_t s1_size, mpi_limb_t s2_limb );
9925f7
+mpi_limb_t _gcry_mpih_add_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
9925f7
+			  mpi_ptr_t s2_ptr,  mpi_size_t size);
9925f7
+mpi_limb_t _gcry_mpih_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
9925f7
+		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
9925f7
+
9925f7
+/*-- mpih-sub.c --*/
9925f7
+mpi_limb_t _gcry_mpih_sub_1( mpi_ptr_t res_ptr,  mpi_ptr_t s1_ptr,
9925f7
+			  mpi_size_t s1_size, mpi_limb_t s2_limb );
9925f7
+mpi_limb_t _gcry_mpih_sub_n( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
9925f7
+			  mpi_ptr_t s2_ptr, mpi_size_t size);
9925f7
+mpi_limb_t _gcry_mpih_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
9925f7
+		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
9925f7
+
9925f7
+/*-- mpih-cmp.c --*/
9925f7
+int _gcry_mpih_cmp( mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size );
9925f7
+
9925f7
+/*-- mpih-mul.c --*/
9925f7
+
9925f7
+struct karatsuba_ctx {
9925f7
+    struct karatsuba_ctx *next;
9925f7
+    mpi_ptr_t tspace;
9925f7
+    unsigned int tspace_nlimbs;
9925f7
+    mpi_size_t tspace_size;
9925f7
+    mpi_ptr_t tp;
9925f7
+    unsigned int tp_nlimbs;
9925f7
+    mpi_size_t tp_size;
9925f7
+};
9925f7
+
9925f7
+void _gcry_mpih_release_karatsuba_ctx( struct karatsuba_ctx *ctx );
9925f7
+
9925f7
+mpi_limb_t _gcry_mpih_addmul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
9925f7
+			     mpi_size_t s1_size, mpi_limb_t s2_limb);
9925f7
+mpi_limb_t _gcry_mpih_submul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
9925f7
+			     mpi_size_t s1_size, mpi_limb_t s2_limb);
9925f7
+void _gcry_mpih_mul_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp,
9925f7
+						   mpi_size_t size);
9925f7
+mpi_limb_t _gcry_mpih_mul( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
9925f7
+					 mpi_ptr_t vp, mpi_size_t vsize);
9925f7
+void _gcry_mpih_sqr_n_basecase( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size );
9925f7
+void _gcry_mpih_sqr_n( mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
9925f7
+						mpi_ptr_t tspace);
9925f7
+
9925f7
+void _gcry_mpih_mul_karatsuba_case( mpi_ptr_t prodp,
9925f7
+				 mpi_ptr_t up, mpi_size_t usize,
9925f7
+				 mpi_ptr_t vp, mpi_size_t vsize,
9925f7
+				 struct karatsuba_ctx *ctx );
9925f7
+
9925f7
+
9925f7
+/*-- mpih-mul_1.c (or xxx/cpu/ *.S) --*/
9925f7
+mpi_limb_t _gcry_mpih_mul_1( mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
9925f7
+			  mpi_size_t s1_size, mpi_limb_t s2_limb);
9925f7
+
9925f7
+/*-- mpih-div.c --*/
9925f7
+mpi_limb_t _gcry_mpih_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
9925f7
+						 mpi_limb_t divisor_limb);
9925f7
+mpi_limb_t _gcry_mpih_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
9925f7
+			   mpi_ptr_t np, mpi_size_t nsize,
9925f7
+			   mpi_ptr_t dp, mpi_size_t dsize);
9925f7
+mpi_limb_t _gcry_mpih_divmod_1( mpi_ptr_t quot_ptr,
9925f7
+			     mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
9925f7
+			     mpi_limb_t divisor_limb);
9925f7
+
9925f7
+/*-- mpih-shift.c --*/
9925f7
+mpi_limb_t _gcry_mpih_lshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
9925f7
+							   unsigned cnt);
9925f7
+mpi_limb_t _gcry_mpih_rshift( mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
9925f7
+							   unsigned cnt);
9925f7
+
9925f7
+/*-- mpih-const-time.c --*/
9925f7
+#define mpih_set_cond(w,u,s,o) _gcry_mpih_set_cond ((w),(u),(s),(o))
9925f7
+#define mpih_add_n_cond(w,u,v,s,o) _gcry_mpih_add_n_cond ((w),(u),(v),(s),(o))
9925f7
+#define mpih_sub_n_cond(w,u,v,s,o) _gcry_mpih_sub_n_cond ((w),(u),(v),(s),(o))
9925f7
+#define mpih_swap_cond(u,v,s,o) _gcry_mpih_swap_cond ((u),(v),(s),(o))
9925f7
+#define mpih_abs_cond(w,u,s,o) _gcry_mpih_abs_cond ((w),(u),(s),(o))
9925f7
+#define mpih_mod(v,vs,u,us) _gcry_mpih_mod ((v),(vs),(u),(us))
9925f7
+
9925f7
+void _gcry_mpih_set_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
9925f7
+                          unsigned long op_enable);
9925f7
+mpi_limb_t _gcry_mpih_add_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
9925f7
+                                  mpi_size_t usize, unsigned long op_enable);
9925f7
+mpi_limb_t _gcry_mpih_sub_n_cond (mpi_ptr_t wp, mpi_ptr_t up, mpi_ptr_t vp,
9925f7
+                                  mpi_size_t usize, unsigned long op_enable);
9925f7
+void _gcry_mpih_swap_cond (mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t usize,
9925f7
+                           unsigned long op_enable);
9925f7
+void _gcry_mpih_abs_cond (mpi_ptr_t wp, mpi_ptr_t up,
9925f7
+                          mpi_size_t usize, unsigned long op_enable);
9925f7
+mpi_ptr_t _gcry_mpih_mod (mpi_ptr_t vp, mpi_size_t vsize,
9925f7
+                          mpi_ptr_t up, mpi_size_t usize);
9925f7
+int _gcry_mpih_cmp_ui (mpi_ptr_t up, mpi_size_t usize, unsigned long v);
9925f7
+
9925f7
+
9925f7
+/* Define stuff for longlong.h.  */
9925f7
+#define W_TYPE_SIZE BITS_PER_MPI_LIMB
9925f7
+  typedef mpi_limb_t   UWtype;
9925f7
+  typedef unsigned int UHWtype;
9925f7
+#if defined (__GNUC__)
9925f7
+  typedef unsigned int UQItype	  __attribute__ ((mode (QI)));
9925f7
+  typedef	   int SItype	  __attribute__ ((mode (SI)));
9925f7
+  typedef unsigned int USItype	  __attribute__ ((mode (SI)));
9925f7
+  typedef	   int DItype	  __attribute__ ((mode (DI)));
9925f7
+  typedef unsigned int UDItype	  __attribute__ ((mode (DI)));
9925f7
+#else
9925f7
+  typedef unsigned char UQItype;
9925f7
+  typedef	   long SItype;
9925f7
+  typedef unsigned long USItype;
9925f7
+#endif
9925f7
+
9925f7
+#ifdef __GNUC__
9925f7
+#include "mpi-inline.h"
9925f7
+#endif
9925f7
+
9925f7
+#endif /*G10_MPI_INTERNAL_H*/
9925f7
diff --git a/cipher/poly1305-new.c b/cipher/poly1305-new.c
9925f7
new file mode 100644
9925f7
index 00000000..56a1a56e
9925f7
--- /dev/null
9925f7
+++ b/cipher/poly1305-new.c
9925f7
@@ -0,0 +1,749 @@
9925f7
+/* poly1305.c  -  Poly1305 internals and generic implementation
9925f7
+ * Copyright (C) 2014,2017,2018 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9925f7
+ *
9925f7
+ * This file is part of Libgcrypt.
9925f7
+ *
9925f7
+ * Libgcrypt is free software; you can redistribute it and/or modify
9925f7
+ * it under the terms of the GNU Lesser general Public License as
9925f7
+ * published by the Free Software Foundation; either version 2.1 of
9925f7
+ * the License, or (at your option) any later version.
9925f7
+ *
9925f7
+ * Libgcrypt is distributed in the hope that it will be useful,
9925f7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9925f7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925f7
+ * GNU Lesser General Public License for more details.
9925f7
+ *
9925f7
+ * You should have received a copy of the GNU Lesser General Public
9925f7
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
9925f7
+ */
9925f7
+
9925f7
+#include <config.h>
9925f7
+
9925f7
+#if defined(ENABLE_PPC_CRYPTO_SUPPORT) && \
9925f7
+    defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
9925f7
+    defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && \
9925f7
+    defined(USE_CHACHA20) && \
9925f7
+    __GNUC__ >= 4
9925f7
+
9925f7
+#include <stdio.h>
9925f7
+#include <stdlib.h>
9925f7
+#include <string.h>
9925f7
+
9925f7
+#include "types.h"
9925f7
+#include "g10lib.h"
9925f7
+#include "cipher.h"
9925f7
+#include "bufhelp.h"
9925f7
+#include "poly1305-internal-new.h"
9925f7
+
9925f7
+#include "mpi-new/mpi-internal.h"
9925f7
+#include "mpi/longlong.h"
9925f7
+
9925f7
+
9925f7
+static const char *selftest (void);
9925f7
+
9925f7
+
9925f7
+#undef HAVE_ASM_POLY1305_BLOCKS
9925f7
+
9925f7
+#undef USE_MPI_64BIT
9925f7
+#undef USE_MPI_32BIT
9925f7
+#if BYTES_PER_MPI_LIMB == 8 // && defined(HAVE_TYPE_U64)  removed: added in types.h, can be assumed
9925f7
+# define USE_MPI_64BIT 1
9925f7
+#elif BYTES_PER_MPI_LIMB == 4
9925f7
+# define USE_MPI_32BIT 1
9925f7
+#else
9925f7
+# error please implement for this limb size.
9925f7
+#endif
9925f7
+
9925f7
+
9925f7
+/* USE_S390X_ASM indicates whether to enable zSeries code. */
9925f7
+#undef USE_S390X_ASM
9925f7
+#if BYTES_PER_MPI_LIMB == 8
9925f7
+# if defined (__s390x__) && __GNUC__ >= 4 && __ARCH__ >= 9
9925f7
+#  if defined(HAVE_GCC_INLINE_ASM_S390X)
9925f7
+#   define USE_S390X_ASM 1
9925f7
+#  endif /* USE_S390X_ASM */
9925f7
+# endif
9925f7
+#endif
9925f7
+
9925f7
+
9925f7
+#ifdef USE_S390X_ASM
9925f7
+
9925f7
+#define HAVE_ASM_POLY1305_BLOCKS 1
9925f7
+
9925f7
+extern unsigned int _gcry_poly1305_s390x_blocks1(void *state,
9925f7
+						 const byte *buf, size_t len,
9925f7
+						 byte high_pad);
9925f7
+
9925f7
+static unsigned int
9925f7
+poly1305_blocks (poly1305_context_t *ctx, const byte *buf, size_t len,
9925f7
+		 byte high_pad)
9925f7
+{
9925f7
+  return _gcry_poly1305_s390x_blocks1(&ctx->state, buf, len, high_pad);
9925f7
+}
9925f7
+
9925f7
+#endif /* USE_S390X_ASM */
9925f7
+
9925f7
+
9925f7
+static void poly1305_init (poly1305_context_t *ctx,
9925f7
+			   const byte key[POLY1305_KEYLEN])
9925f7
+{
9925f7
+  POLY1305_STATE *st = &ctx->state;
9925f7
+
9925f7
+  ctx->leftover = 0;
9925f7
+
9925f7
+  st->h[0] = 0;
9925f7
+  st->h[1] = 0;
9925f7
+  st->h[2] = 0;
9925f7
+  st->h[3] = 0;
9925f7
+  st->h[4] = 0;
9925f7
+
9925f7
+  st->r[0] = buf_get_le32(key + 0)  & 0x0fffffff;
9925f7
+  st->r[1] = buf_get_le32(key + 4)  & 0x0ffffffc;
9925f7
+  st->r[2] = buf_get_le32(key + 8)  & 0x0ffffffc;
9925f7
+  st->r[3] = buf_get_le32(key + 12) & 0x0ffffffc;
9925f7
+
9925f7
+  st->k[0] = buf_get_le32(key + 16);
9925f7
+  st->k[1] = buf_get_le32(key + 20);
9925f7
+  st->k[2] = buf_get_le32(key + 24);
9925f7
+  st->k[3] = buf_get_le32(key + 28);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+#ifdef USE_MPI_64BIT
9925f7
+
9925f7
+#if defined (__aarch64__) && defined(HAVE_CPU_ARCH_ARM) && __GNUC__ >= 4
9925f7
+
9925f7
+/* A += B (armv8/aarch64) */
9925f7
+#define ADD_1305_64(A2, A1, A0, B2, B1, B0) \
9925f7
+      __asm__ ("adds %0, %3, %0\n" \
9925f7
+	       "adcs %1, %4, %1\n" \
9925f7
+	       "adc  %2, %5, %2\n" \
9925f7
+	       : "+r" (A0), "+r" (A1), "+r" (A2) \
9925f7
+	       : "r" (B0), "r" (B1), "r" (B2) \
9925f7
+	       : "cc" )
9925f7
+
9925f7
+#endif /* __aarch64__ */
9925f7
+
9925f7
+#if defined (__x86_64__) && defined(HAVE_CPU_ARCH_X86) && __GNUC__ >= 4
9925f7
+
9925f7
+/* A += B (x86-64) */
9925f7
+#define ADD_1305_64(A2, A1, A0, B2, B1, B0) \
9925f7
+      __asm__ ("addq %3, %0\n" \
9925f7
+	       "adcq %4, %1\n" \
9925f7
+	       "adcq %5, %2\n" \
9925f7
+	       : "+r" (A0), "+r" (A1), "+r" (A2) \
9925f7
+	       : "g" (B0), "g" (B1), "g" (B2) \
9925f7
+	       : "cc" )
9925f7
+
9925f7
+#endif /* __x86_64__ */
9925f7
+
9925f7
+#if defined (__powerpc__) && defined(HAVE_CPU_ARCH_PPC) && __GNUC__ >= 4
9925f7
+
9925f7
+/* A += B (ppc64) */
9925f7
+#define ADD_1305_64(A2, A1, A0, B2, B1, B0) \
9925f7
+      __asm__ ("addc %0, %3, %0\n" \
9925f7
+	       "adde %1, %4, %1\n" \
9925f7
+	       "adde %2, %5, %2\n" \
9925f7
+	       : "+r" (A0), "+r" (A1), "+r" (A2) \
9925f7
+	       : "r" (B0), "r" (B1), "r" (B2) \
9925f7
+	       : "cc" )
9925f7
+
9925f7
+#endif /* __powerpc__ */
9925f7
+
9925f7
+#ifndef ADD_1305_64
9925f7
+/* A += B (generic, mpi) */
9925f7
+#  define ADD_1305_64(A2, A1, A0, B2, B1, B0) do { \
9925f7
+    u64 carry; \
9925f7
+    add_ssaaaa(carry, A0, 0, A0, 0, B0); \
9925f7
+    add_ssaaaa(A2, A1, A2, A1, B2, B1); \
9925f7
+    add_ssaaaa(A2, A1, A2, A1, 0, carry); \
9925f7
+  } while (0)
9925f7
+#endif
9925f7
+
9925f7
+/* H = H * R mod 2¹³⁰-5 */
9925f7
+#define MUL_MOD_1305_64(H2, H1, H0, R1, R0, R1_MULT5) do { \
9925f7
+    u64 x0_lo, x0_hi, x1_lo, x1_hi; \
9925f7
+    u64 t0_lo, t0_hi, t1_lo, t1_hi; \
9925f7
+    \
9925f7
+    /* x = a * r (partial mod 2^130-5) */ \
9925f7
+    umul_ppmm(x0_hi, x0_lo, H0, R0);  /* h0 * r0 */ \
9925f7
+    umul_ppmm(x1_hi, x1_lo, H0, R1);  /* h0 * r1 */ \
9925f7
+    \
9925f7
+    umul_ppmm(t0_hi, t0_lo, H1, R1_MULT5); /* h1 * r1 mod 2^130-5 */ \
9925f7
+    add_ssaaaa(x0_hi, x0_lo, x0_hi, x0_lo, t0_hi, t0_lo); \
9925f7
+    umul_ppmm(t1_hi, t1_lo, H1, R0);       /* h1 * r0 */ \
9925f7
+    add_ssaaaa(x1_hi, x1_lo, x1_hi, x1_lo, t1_hi, t1_lo); \
9925f7
+    \
9925f7
+    t1_lo = H2 * R1_MULT5; /* h2 * r1 mod 2^130-5 */ \
9925f7
+    t1_hi = H2 * R0;       /* h2 * r0 */ \
9925f7
+    add_ssaaaa(H0, H1, x1_hi, x1_lo, t1_hi, t1_lo); \
9925f7
+    \
9925f7
+    /* carry propagation */ \
9925f7
+    H2 = H0 & 3; \
9925f7
+    H0 = (H0 >> 2) * 5; /* msb mod 2^130-5 */ \
9925f7
+    ADD_1305_64(H2, H1, H0, (u64)0, x0_hi, x0_lo); \
9925f7
+  } while (0)
9925f7
+
9925f7
+#ifndef HAVE_ASM_POLY1305_BLOCKS
9925f7
+
9925f7
+static unsigned int
9925f7
+poly1305_blocks (poly1305_context_t *ctx, const byte *buf, size_t len,
9925f7
+		 byte high_pad)
9925f7
+{
9925f7
+  POLY1305_STATE *st = &ctx->state;
9925f7
+  u64 r0, r1, r1_mult5;
9925f7
+  u64 h0, h1, h2;
9925f7
+  u64 m0, m1, m2;
9925f7
+
9925f7
+  m2 = high_pad;
9925f7
+
9925f7
+  h0 = st->h[0] + ((u64)st->h[1] << 32);
9925f7
+  h1 = st->h[2] + ((u64)st->h[3] << 32);
9925f7
+  h2 = st->h[4];
9925f7
+
9925f7
+  r0 = st->r[0] + ((u64)st->r[1] << 32);
9925f7
+  r1 = st->r[2] + ((u64)st->r[3] << 32);
9925f7
+
9925f7
+  r1_mult5 = (r1 >> 2) + r1;
9925f7
+
9925f7
+  m0 = buf_get_le64(buf + 0);
9925f7
+  m1 = buf_get_le64(buf + 8);
9925f7
+  buf += POLY1305_BLOCKSIZE;
9925f7
+  len -= POLY1305_BLOCKSIZE;
9925f7
+
9925f7
+  while (len >= POLY1305_BLOCKSIZE)
9925f7
+    {
9925f7
+      /* a = h + m */
9925f7
+      ADD_1305_64(h2, h1, h0, m2, m1, m0);
9925f7
+
9925f7
+      m0 = buf_get_le64(buf + 0);
9925f7
+      m1 = buf_get_le64(buf + 8);
9925f7
+
9925f7
+      /* h = a * r (partial mod 2^130-5) */
9925f7
+      MUL_MOD_1305_64(h2, h1, h0, r1, r0, r1_mult5);
9925f7
+
9925f7
+      buf += POLY1305_BLOCKSIZE;
9925f7
+      len -= POLY1305_BLOCKSIZE;
9925f7
+    }
9925f7
+
9925f7
+  /* a = h + m */
9925f7
+  ADD_1305_64(h2, h1, h0, m2, m1, m0);
9925f7
+
9925f7
+  /* h = a * r (partial mod 2^130-5) */
9925f7
+  MUL_MOD_1305_64(h2, h1, h0, r1, r0, r1_mult5);
9925f7
+
9925f7
+  st->h[0] = h0;
9925f7
+  st->h[1] = h0 >> 32;
9925f7
+  st->h[2] = h1;
9925f7
+  st->h[3] = h1 >> 32;
9925f7
+  st->h[4] = h2;
9925f7
+
9925f7
+  return 6 * sizeof (void *) + 18 * sizeof (u64);
9925f7
+}
9925f7
+
9925f7
+#endif /* !HAVE_ASM_POLY1305_BLOCKS */
9925f7
+
9925f7
+static unsigned int poly1305_final (poly1305_context_t *ctx,
9925f7
+				    byte mac[POLY1305_TAGLEN])
9925f7
+{
9925f7
+  POLY1305_STATE *st = &ctx->state;
9925f7
+  unsigned int burn = 0;
9925f7
+  u64 u, carry;
9925f7
+  u64 k0, k1;
9925f7
+  u64 h0, h1;
9925f7
+  u64 h2;
9925f7
+
9925f7
+  /* process the remaining block */
9925f7
+  if (ctx->leftover)
9925f7
+    {
9925f7
+      ctx->buffer[ctx->leftover++] = 1;
9925f7
+      if (ctx->leftover < POLY1305_BLOCKSIZE)
9925f7
+	{
9925f7
+	  memset (&ctx->buffer[ctx->leftover], 0,
9925f7
+		  POLY1305_BLOCKSIZE - ctx->leftover);
9925f7
+	  ctx->leftover = POLY1305_BLOCKSIZE;
9925f7
+	}
9925f7
+      burn = poly1305_blocks (ctx, ctx->buffer, POLY1305_BLOCKSIZE, 0);
9925f7
+    }
9925f7
+
9925f7
+  h0 = st->h[0] + ((u64)st->h[1] << 32);
9925f7
+  h1 = st->h[2] + ((u64)st->h[3] << 32);
9925f7
+  h2 = st->h[4];
9925f7
+
9925f7
+  k0 = st->k[0] + ((u64)st->k[1] << 32);
9925f7
+  k1 = st->k[2] + ((u64)st->k[3] << 32);
9925f7
+
9925f7
+  /* check if h is more than 2^130-5, by adding 5. */
9925f7
+  add_ssaaaa(carry, u, 0, h0, 0, 5);
9925f7
+  add_ssaaaa(carry, u, 0, carry, 0, h1);
9925f7
+  u = (carry + h2) >> 2; /* u == 0 or 1 */
9925f7
+
9925f7
+  /* minus 2^130-5 ... (+5) */
9925f7
+  u = (-u) & 5;
9925f7
+  add_ssaaaa(h1, h0, h1, h0, 0, u);
9925f7
+
9925f7
+  /* add high part of key + h */
9925f7
+  add_ssaaaa(h1, h0, h1, h0, k1, k0);
9925f7
+  buf_put_le64(mac + 0, h0);
9925f7
+  buf_put_le64(mac + 8, h1);
9925f7
+
9925f7
+  /* burn_stack */
9925f7
+  return 4 * sizeof (void *) + 7 * sizeof (u64) + burn;
9925f7
+}
9925f7
+
9925f7
+#endif /* USE_MPI_64BIT */
9925f7
+
9925f7
+#ifdef USE_MPI_32BIT
9925f7
+
9925f7
+#ifdef HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS
9925f7
+
9925f7
+/* HI:LO += A * B (arm) */
9925f7
+#define UMUL_ADD_32(HI, LO, A, B) \
9925f7
+      __asm__ ("umlal %1, %0, %4, %5" \
9925f7
+	       : "=r" (HI), "=r" (LO) \
9925f7
+	       : "0" (HI), "1" (LO), "r" (A), "r" (B) )
9925f7
+
9925f7
+/* A += B (arm) */
9925f7
+#define ADD_1305_32(A4, A3, A2, A1, A0, B4, B3, B2, B1, B0) \
9925f7
+      __asm__ ("adds %0, %0, %5\n" \
9925f7
+	       "adcs %1, %1, %6\n" \
9925f7
+	       "adcs %2, %2, %7\n" \
9925f7
+	       "adcs %3, %3, %8\n" \
9925f7
+	       "adc %4, %4, %9\n" \
9925f7
+	       : "+r" (A0), "+r" (A1), "+r" (A2), "+r" (A3), "+r" (A4) \
9925f7
+	       : "r" (B0), "r" (B1), "r" (B2), "r" (B3), "r" (B4) \
9925f7
+	       : "cc" )
9925f7
+
9925f7
+#endif /* HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS */
9925f7
+
9925f7
+#if defined (__i386__) && defined(HAVE_CPU_ARCH_X86) && __GNUC__ >= 5
9925f7
+/* Note: ADD_1305_32 below does not compile on GCC-4.7 */
9925f7
+
9925f7
+/* A += B (i386) */
9925f7
+#define ADD_1305_32(A4, A3, A2, A1, A0, B4, B3, B2, B1, B0) \
9925f7
+      __asm__ ("addl %5, %0\n" \
9925f7
+	       "adcl %6, %1\n" \
9925f7
+	       "adcl %7, %2\n" \
9925f7
+	       "adcl %8, %3\n" \
9925f7
+	       "adcl %9, %4\n" \
9925f7
+	       : "+r" (A0), "+r" (A1), "+r" (A2), "+r" (A3), "+r" (A4) \
9925f7
+	       : "g" (B0), "g" (B1), "g" (B2), "g" (B3), "g" (B4) \
9925f7
+	       : "cc" )
9925f7
+
9925f7
+#endif /* __i386__ */
9925f7
+
9925f7
+#ifndef UMUL_ADD_32
9925f7
+/* HI:LO += A * B (generic, mpi) */
9925f7
+#  define UMUL_ADD_32(HI, LO, A, B) do { \
9925f7
+    u32 t_lo, t_hi; \
9925f7
+    umul_ppmm(t_hi, t_lo, A, B); \
9925f7
+    add_ssaaaa(HI, LO, HI, LO, t_hi, t_lo); \
9925f7
+  } while (0)
9925f7
+#endif
9925f7
+
9925f7
+#ifndef ADD_1305_32
9925f7
+/* A += B (generic, mpi) */
9925f7
+#  define ADD_1305_32(A4, A3, A2, A1, A0, B4, B3, B2, B1, B0) do { \
9925f7
+    u32 carry0, carry1, carry2; \
9925f7
+    add_ssaaaa(carry0, A0, 0, A0, 0, B0); \
9925f7
+    add_ssaaaa(carry1, A1, 0, A1, 0, B1); \
9925f7
+    add_ssaaaa(carry1, A1, carry1, A1, 0, carry0); \
9925f7
+    add_ssaaaa(carry2, A2, 0, A2, 0, B2); \
9925f7
+    add_ssaaaa(carry2, A2, carry2, A2, 0, carry1); \
9925f7
+    add_ssaaaa(A4, A3, A4, A3, B4, B3); \
9925f7
+    add_ssaaaa(A4, A3, A4, A3, 0, carry2); \
9925f7
+  } while (0)
9925f7
+#endif
9925f7
+
9925f7
+/* H = H * R mod 2¹³⁰-5 */
9925f7
+#define MUL_MOD_1305_32(H4, H3, H2, H1, H0, R3, R2, R1, R0, \
9925f7
+                        R3_MULT5, R2_MULT5, R1_MULT5) do { \
9925f7
+    u32 x0_lo, x0_hi, x1_lo, x1_hi, x2_lo, x2_hi, x3_lo, x3_hi; \
9925f7
+    u32 t0_lo, t0_hi; \
9925f7
+    \
9925f7
+    /* x = a * r (partial mod 2^130-5) */ \
9925f7
+    umul_ppmm(x0_hi, x0_lo, H0, R0);  /* h0 * r0 */ \
9925f7
+    umul_ppmm(x1_hi, x1_lo, H0, R1);  /* h0 * r1 */ \
9925f7
+    umul_ppmm(x2_hi, x2_lo, H0, R2);  /* h0 * r2 */ \
9925f7
+    umul_ppmm(x3_hi, x3_lo, H0, R3);  /* h0 * r3 */ \
9925f7
+    \
9925f7
+    UMUL_ADD_32(x0_hi, x0_lo, H1, R3_MULT5); /* h1 * r3 mod 2^130-5 */ \
9925f7
+    UMUL_ADD_32(x1_hi, x1_lo, H1, R0);       /* h1 * r0 */ \
9925f7
+    UMUL_ADD_32(x2_hi, x2_lo, H1, R1);       /* h1 * r1 */ \
9925f7
+    UMUL_ADD_32(x3_hi, x3_lo, H1, R2);       /* h1 * r2 */ \
9925f7
+    \
9925f7
+    UMUL_ADD_32(x0_hi, x0_lo, H2, R2_MULT5); /* h2 * r2 mod 2^130-5 */ \
9925f7
+    UMUL_ADD_32(x1_hi, x1_lo, H2, R3_MULT5); /* h2 * r3 mod 2^130-5 */ \
9925f7
+    UMUL_ADD_32(x2_hi, x2_lo, H2, R0);       /* h2 * r0 */ \
9925f7
+    UMUL_ADD_32(x3_hi, x3_lo, H2, R1);       /* h2 * r1 */ \
9925f7
+    \
9925f7
+    UMUL_ADD_32(x0_hi, x0_lo, H3, R1_MULT5); /* h3 * r1 mod 2^130-5 */ \
9925f7
+    H1 = x0_hi; \
9925f7
+    UMUL_ADD_32(x1_hi, x1_lo, H3, R2_MULT5); /* h3 * r2 mod 2^130-5 */ \
9925f7
+    UMUL_ADD_32(x2_hi, x2_lo, H3, R3_MULT5); /* h3 * r3 mod 2^130-5 */ \
9925f7
+    UMUL_ADD_32(x3_hi, x3_lo, H3, R0);       /* h3 * r0 */ \
9925f7
+    \
9925f7
+    t0_lo = H4 * R1_MULT5; /* h4 * r1 mod 2^130-5 */ \
9925f7
+    t0_hi = H4 * R2_MULT5; /* h4 * r2 mod 2^130-5 */ \
9925f7
+    add_ssaaaa(H2, x1_lo, x1_hi, x1_lo, 0, t0_lo); \
9925f7
+    add_ssaaaa(H3, x2_lo, x2_hi, x2_lo, 0, t0_hi); \
9925f7
+    t0_lo = H4 * R3_MULT5; /* h4 * r3 mod 2^130-5 */ \
9925f7
+    t0_hi = H4 * R0;       /* h4 * r0 */ \
9925f7
+    add_ssaaaa(H4, x3_lo, x3_hi, x3_lo, t0_hi, t0_lo); \
9925f7
+    \
9925f7
+    /* carry propagation */ \
9925f7
+    H0 = (H4 >> 2) * 5; /* msb mod 2^130-5 */ \
9925f7
+    H4 = H4 & 3; \
9925f7
+    ADD_1305_32(H4, H3, H2, H1, H0, 0, x3_lo, x2_lo, x1_lo, x0_lo); \
9925f7
+  } while (0)
9925f7
+
9925f7
+#ifndef HAVE_ASM_POLY1305_BLOCKS
9925f7
+
9925f7
+static unsigned int
9925f7
+poly1305_blocks (poly1305_context_t *ctx, const byte *buf, size_t len,
9925f7
+		 byte high_pad)
9925f7
+{
9925f7
+  POLY1305_STATE *st = &ctx->state;
9925f7
+  u32 r1_mult5, r2_mult5, r3_mult5;
9925f7
+  u32 h0, h1, h2, h3, h4;
9925f7
+  u32 m0, m1, m2, m3, m4;
9925f7
+
9925f7
+  m4 = high_pad;
9925f7
+
9925f7
+  h0 = st->h[0];
9925f7
+  h1 = st->h[1];
9925f7
+  h2 = st->h[2];
9925f7
+  h3 = st->h[3];
9925f7
+  h4 = st->h[4];
9925f7
+
9925f7
+  r1_mult5 = (st->r[1] >> 2) + st->r[1];
9925f7
+  r2_mult5 = (st->r[2] >> 2) + st->r[2];
9925f7
+  r3_mult5 = (st->r[3] >> 2) + st->r[3];
9925f7
+
9925f7
+  while (len >= POLY1305_BLOCKSIZE)
9925f7
+    {
9925f7
+      m0 = buf_get_le32(buf + 0);
9925f7
+      m1 = buf_get_le32(buf + 4);
9925f7
+      m2 = buf_get_le32(buf + 8);
9925f7
+      m3 = buf_get_le32(buf + 12);
9925f7
+
9925f7
+      /* a = h + m */
9925f7
+      ADD_1305_32(h4, h3, h2, h1, h0, m4, m3, m2, m1, m0);
9925f7
+
9925f7
+      /* h = a * r (partial mod 2^130-5) */
9925f7
+      MUL_MOD_1305_32(h4, h3, h2, h1, h0,
9925f7
+		      st->r[3], st->r[2], st->r[1], st->r[0],
9925f7
+		      r3_mult5, r2_mult5, r1_mult5);
9925f7
+
9925f7
+      buf += POLY1305_BLOCKSIZE;
9925f7
+      len -= POLY1305_BLOCKSIZE;
9925f7
+    }
9925f7
+
9925f7
+  st->h[0] = h0;
9925f7
+  st->h[1] = h1;
9925f7
+  st->h[2] = h2;
9925f7
+  st->h[3] = h3;
9925f7
+  st->h[4] = h4;
9925f7
+
9925f7
+  return 6 * sizeof (void *) + 28 * sizeof (u32);
9925f7
+}
9925f7
+
9925f7
+#endif /* !HAVE_ASM_POLY1305_BLOCKS */
9925f7
+
9925f7
+static unsigned int poly1305_final (poly1305_context_t *ctx,
9925f7
+				    byte mac[POLY1305_TAGLEN])
9925f7
+{
9925f7
+  POLY1305_STATE *st = &ctx->state;
9925f7
+  unsigned int burn = 0;
9925f7
+  u32 carry, tmp0, tmp1, tmp2, u;
9925f7
+  u32 h4, h3, h2, h1, h0;
9925f7
+
9925f7
+  /* process the remaining block */
9925f7
+  if (ctx->leftover)
9925f7
+    {
9925f7
+      ctx->buffer[ctx->leftover++] = 1;
9925f7
+      if (ctx->leftover < POLY1305_BLOCKSIZE)
9925f7
+	{
9925f7
+	  memset (&ctx->buffer[ctx->leftover], 0,
9925f7
+		  POLY1305_BLOCKSIZE - ctx->leftover);
9925f7
+	  ctx->leftover = POLY1305_BLOCKSIZE;
9925f7
+	}
9925f7
+      burn = poly1305_blocks (ctx, ctx->buffer, POLY1305_BLOCKSIZE, 0);
9925f7
+    }
9925f7
+
9925f7
+  h0 = st->h[0];
9925f7
+  h1 = st->h[1];
9925f7
+  h2 = st->h[2];
9925f7
+  h3 = st->h[3];
9925f7
+  h4 = st->h[4];
9925f7
+
9925f7
+  /* check if h is more than 2^130-5, by adding 5. */
9925f7
+  add_ssaaaa(carry, tmp0, 0, h0, 0, 5);
9925f7
+  add_ssaaaa(carry, tmp0, 0, carry, 0, h1);
9925f7
+  add_ssaaaa(carry, tmp0, 0, carry, 0, h2);
9925f7
+  add_ssaaaa(carry, tmp0, 0, carry, 0, h3);
9925f7
+  u = (carry + h4) >> 2; /* u == 0 or 1 */
9925f7
+
9925f7
+  /* minus 2^130-5 ... (+5) */
9925f7
+  u = (-u) & 5;
9925f7
+  add_ssaaaa(carry, h0, 0, h0, 0, u);
9925f7
+  add_ssaaaa(carry, h1, 0, h1, 0, carry);
9925f7
+  add_ssaaaa(carry, h2, 0, h2, 0, carry);
9925f7
+  add_ssaaaa(carry, h3, 0, h3, 0, carry);
9925f7
+
9925f7
+  /* add high part of key + h */
9925f7
+  add_ssaaaa(tmp0, h0, 0, h0, 0, st->k[0]);
9925f7
+  add_ssaaaa(tmp1, h1, 0, h1, 0, st->k[1]);
9925f7
+  add_ssaaaa(tmp1, h1, tmp1, h1, 0, tmp0);
9925f7
+  add_ssaaaa(tmp2, h2, 0, h2, 0, st->k[2]);
9925f7
+  add_ssaaaa(tmp2, h2, tmp2, h2, 0, tmp1);
9925f7
+  add_ssaaaa(carry, h3, 0, h3, 0, st->k[3]);
9925f7
+  h3 += tmp2;
9925f7
+
9925f7
+  buf_put_le32(mac + 0, h0);
9925f7
+  buf_put_le32(mac + 4, h1);
9925f7
+  buf_put_le32(mac + 8, h2);
9925f7
+  buf_put_le32(mac + 12, h3);
9925f7
+
9925f7
+  /* burn_stack */
9925f7
+  return 4 * sizeof (void *) + 10 * sizeof (u32) + burn;
9925f7
+}
9925f7
+
9925f7
+#endif /* USE_MPI_32BIT */
9925f7
+
9925f7
+
9925f7
+unsigned int
9925f7
+_gcry_poly1305_update_burn (poly1305_context_t *ctx, const byte *m,
9925f7
+			    size_t bytes)
9925f7
+{
9925f7
+  unsigned int burn = 0;
9925f7
+
9925f7
+  /* handle leftover */
9925f7
+  if (ctx->leftover)
9925f7
+    {
9925f7
+      size_t want = (POLY1305_BLOCKSIZE - ctx->leftover);
9925f7
+      if (want > bytes)
9925f7
+	want = bytes;
9925f7
+      buf_cpy (ctx->buffer + ctx->leftover, m, want);
9925f7
+      bytes -= want;
9925f7
+      m += want;
9925f7
+      ctx->leftover += want;
9925f7
+      if (ctx->leftover < POLY1305_BLOCKSIZE)
9925f7
+	return 0;
9925f7
+      burn = poly1305_blocks (ctx, ctx->buffer, POLY1305_BLOCKSIZE, 1);
9925f7
+      ctx->leftover = 0;
9925f7
+    }
9925f7
+
9925f7
+  /* process full blocks */
9925f7
+  if (bytes >= POLY1305_BLOCKSIZE)
9925f7
+    {
9925f7
+      size_t nblks = bytes / POLY1305_BLOCKSIZE;
9925f7
+      burn = poly1305_blocks (ctx, m, nblks * POLY1305_BLOCKSIZE, 1);
9925f7
+      m += nblks * POLY1305_BLOCKSIZE;
9925f7
+      bytes -= nblks * POLY1305_BLOCKSIZE;
9925f7
+    }
9925f7
+
9925f7
+  /* store leftover */
9925f7
+  if (bytes)
9925f7
+    {
9925f7
+      buf_cpy (ctx->buffer + ctx->leftover, m, bytes);
9925f7
+      ctx->leftover += bytes;
9925f7
+    }
9925f7
+
9925f7
+  return burn;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+void
9925f7
+_gcry_poly1305_update (poly1305_context_t *ctx, const byte *m, size_t bytes)
9925f7
+{
9925f7
+  unsigned int burn;
9925f7
+
9925f7
+  burn = _gcry_poly1305_update_burn (ctx, m, bytes);
9925f7
+
9925f7
+  if (burn)
9925f7
+    _gcry_burn_stack (burn);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+void
9925f7
+_gcry_poly1305_finish (poly1305_context_t *ctx, byte mac[POLY1305_TAGLEN])
9925f7
+{
9925f7
+  unsigned int burn;
9925f7
+
9925f7
+  burn = poly1305_final (ctx, mac);
9925f7
+
9925f7
+  _gcry_burn_stack (burn);
9925f7
+}
9925f7
+
9925f7
+
9925f7
+gcry_err_code_t
9925f7
+_gcry_poly1305_init (poly1305_context_t * ctx, const byte * key,
9925f7
+		     size_t keylen)
9925f7
+{
9925f7
+  static int initialized;
9925f7
+  static const char *selftest_failed;
9925f7
+
9925f7
+  if (!initialized)
9925f7
+    {
9925f7
+      initialized = 1;
9925f7
+      selftest_failed = selftest ();
9925f7
+      if (selftest_failed)
9925f7
+	log_error ("Poly1305 selftest failed (%s)\n", selftest_failed);
9925f7
+    }
9925f7
+
9925f7
+  if (keylen != POLY1305_KEYLEN)
9925f7
+    return GPG_ERR_INV_KEYLEN;
9925f7
+
9925f7
+  if (selftest_failed)
9925f7
+    return GPG_ERR_SELFTEST_FAILED;
9925f7
+
9925f7
+  poly1305_init (ctx, key);
9925f7
+
9925f7
+  return 0;
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static void
9925f7
+poly1305_auth (byte mac[POLY1305_TAGLEN], const byte * m, size_t bytes,
9925f7
+	       const byte * key)
9925f7
+{
9925f7
+  poly1305_context_t ctx;
9925f7
+
9925f7
+  memset (&ctx, 0, sizeof (ctx));
9925f7
+
9925f7
+  _gcry_poly1305_init (&ctx, key, POLY1305_KEYLEN);
9925f7
+  _gcry_poly1305_update (&ctx, m, bytes);
9925f7
+  _gcry_poly1305_finish (&ctx, mac);
9925f7
+
9925f7
+  wipememory (&ctx, sizeof (ctx));
9925f7
+}
9925f7
+
9925f7
+
9925f7
+static const char *
9925f7
+selftest (void)
9925f7
+{
9925f7
+  /* example from nacl */
9925f7
+  static const byte nacl_key[POLY1305_KEYLEN] = {
9925f7
+    0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91,
9925f7
+    0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d, 0x3c, 0x25,
9925f7
+    0x25, 0x39, 0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65,
9925f7
+    0x2d, 0x65, 0x1f, 0xa4, 0xc8, 0xcf, 0xf8, 0x80,
9925f7
+  };
9925f7
+
9925f7
+  static const byte nacl_msg[131] = {
9925f7
+    0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73,
9925f7
+    0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc, 0x76, 0xce,
9925f7
+    0x48, 0x33, 0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4,
9925f7
+    0x47, 0x6f, 0xb8, 0xc5, 0x31, 0xa1, 0x18, 0x6a,
9925f7
+    0xc0, 0xdf, 0xc1, 0x7c, 0x98, 0xdc, 0xe8, 0x7b,
9925f7
+    0x4d, 0xa7, 0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72,
9925f7
+    0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92, 0x8f, 0xe2,
9925f7
+    0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38,
9925f7
+    0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7, 0xcc, 0x8a,
9925f7
+    0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae,
9925f7
+    0x90, 0x22, 0x43, 0x68, 0x51, 0x7a, 0xcf, 0xea,
9925f7
+    0xbd, 0x6b, 0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda,
9925f7
+    0x99, 0x83, 0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde,
9925f7
+    0x56, 0x24, 0x4a, 0x9e, 0x88, 0xd5, 0xf9, 0xb3,
9925f7
+    0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6,
9925f7
+    0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4, 0x5a, 0x74,
9925f7
+    0xe3, 0x55, 0xa5
9925f7
+  };
9925f7
+
9925f7
+  static const byte nacl_mac[16] = {
9925f7
+    0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5,
9925f7
+    0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9
9925f7
+  };
9925f7
+
9925f7
+  /* generates a final value of (2^130 - 2) == 3 */
9925f7
+  static const byte wrap_key[POLY1305_KEYLEN] = {
9925f7
+    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+  };
9925f7
+
9925f7
+  static const byte wrap_msg[16] = {
9925f7
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
9925f7
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
9925f7
+  };
9925f7
+
9925f7
+  static const byte wrap_mac[16] = {
9925f7
+    0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9925f7
+  };
9925f7
+
9925f7
+  /* mac of the macs of messages of length 0 to 256, where the key and messages
9925f7
+   * have all their values set to the length
9925f7
+   */
9925f7
+  static const byte total_key[POLY1305_KEYLEN] = {
9925f7
+    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
9925f7
+    0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9,
9925f7
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
9925f7
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
9925f7
+  };
9925f7
+
9925f7
+  static const byte total_mac[16] = {
9925f7
+    0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd,
9925f7
+    0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39
9925f7
+  };
9925f7
+
9925f7
+  poly1305_context_t ctx;
9925f7
+  poly1305_context_t total_ctx;
9925f7
+  byte all_key[POLY1305_KEYLEN];
9925f7
+  byte all_msg[256];
9925f7
+  byte mac[16];
9925f7
+  size_t i, j;
9925f7
+
9925f7
+  memset (&ctx, 0, sizeof (ctx));
9925f7
+  memset (&total_ctx, 0, sizeof (total_ctx));
9925f7
+
9925f7
+  memset (mac, 0, sizeof (mac));
9925f7
+  poly1305_auth (mac, nacl_msg, sizeof (nacl_msg), nacl_key);
9925f7
+  if (memcmp (nacl_mac, mac, sizeof (nacl_mac)) != 0)
9925f7
+    return "Poly1305 test 1 failed.";
9925f7
+
9925f7
+  /* SSE2/AVX have a 32 byte block size, but also support 64 byte blocks, so
9925f7
+   * make sure everything still works varying between them */
9925f7
+  memset (mac, 0, sizeof (mac));
9925f7
+  _gcry_poly1305_init (&ctx, nacl_key, POLY1305_KEYLEN);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 0, 32);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 32, 64);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 96, 16);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 112, 8);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 120, 4);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 124, 2);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 126, 1);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 127, 1);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 128, 1);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 129, 1);
9925f7
+  _gcry_poly1305_update (&ctx, nacl_msg + 130, 1);
9925f7
+  _gcry_poly1305_finish (&ctx, mac);
9925f7
+  if (memcmp (nacl_mac, mac, sizeof (nacl_mac)) != 0)
9925f7
+    return "Poly1305 test 2 failed.";
9925f7
+
9925f7
+  memset (mac, 0, sizeof (mac));
9925f7
+  poly1305_auth (mac, wrap_msg, sizeof (wrap_msg), wrap_key);
9925f7
+  if (memcmp (wrap_mac, mac, sizeof (nacl_mac)) != 0)
9925f7
+    return "Poly1305 test 3 failed.";
9925f7
+
9925f7
+  _gcry_poly1305_init (&total_ctx, total_key, POLY1305_KEYLEN);
9925f7
+  for (i = 0; i < 256; i++)
9925f7
+    {
9925f7
+      /* set key and message to 'i,i,i..' */
9925f7
+      for (j = 0; j < sizeof (all_key); j++)
9925f7
+	all_key[j] = i;
9925f7
+      for (j = 0; j < i; j++)
9925f7
+	all_msg[j] = i;
9925f7
+      poly1305_auth (mac, all_msg, i, all_key);
9925f7
+      _gcry_poly1305_update (&total_ctx, mac, 16);
9925f7
+    }
9925f7
+  _gcry_poly1305_finish (&total_ctx, mac);
9925f7
+  if (memcmp (total_mac, mac, sizeof (total_mac)) != 0)
9925f7
+    return "Poly1305 test 4 failed.";
9925f7
+
9925f7
+  return NULL;
9925f7
+}
9925f7
+
9925f7
+#endif /* ENABLE_PPC_CRYPTO_SUPPORT */
9925f7
diff --git a/cipher/poly1305.c b/cipher/poly1305.c
9925f7
index 22255fb1..b45a9dc8 100644
9925f7
--- a/cipher/poly1305.c
9925f7
+++ b/cipher/poly1305.c
9925f7
@@ -23,6 +23,11 @@
9925f7
  */
9925f7
 
9925f7
 #include <config.h>
9925f7
+
9925f7
+#if !defined(ENABLE_PPC_CRYPTO_SUPPORT) || \
9925f7
+    !defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) || \
9925f7
+    !defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC)
9925f7
+
9925f7
 #include <stdio.h>
9925f7
 #include <stdlib.h>
9925f7
 #include <string.h>
9925f7
@@ -641,3 +646,5 @@ selftest (void)
9925f7
 
9925f7
   return NULL;
9925f7
 }
9925f7
+
9925f7
+#endif /* ENABLE_PPC_CRYPTO_SUPPORT */
9925f7
diff --git a/configure.ac b/configure.ac
9925f7
index 9bcb1318..397c2f19 100644
9925f7
--- a/configure.ac
9925f7
+++ b/configure.ac
9925f7
@@ -2504,6 +2504,18 @@ if test "$found" = "1" ; then
9925f7
          GCRYPT_CIPHERS="$GCRYPT_CIPHERS chacha20-ssse3-amd64.lo"
9925f7
          GCRYPT_CIPHERS="$GCRYPT_CIPHERS chacha20-avx2-amd64.lo"
9925f7
       ;;
9925f7
+      powerpc64le-*-*)
9925f7
+         # Build with the ppc8 vector implementation
9925f7
+         GCRYPT_CIPHERS="$GCRYPT_CIPHERS chacha20-ppc.lo chacha20-new.lo"
9925f7
+      ;;
9925f7
+      powerpc64-*-*)
9925f7
+         # Build with the ppc8 vector implementation
9925f7
+         GCRYPT_CIPHERS="$GCRYPT_CIPHERS chacha20-ppc.lo chacha20-new.lo"
9925f7
+      ;;
9925f7
+      powerpc-*-*)
9925f7
+         # Build with the ppc8 vector implementation
9925f7
+	 GCRYPT_CIPHERS="$GCRYPT_CIPHERS chacha20-ppc.lo chacha20-new.lo"
9925f7
+      ;;
9925f7
    esac
9925f7
 
9925f7
    if test x"$neonsupport" = xyes ; then
9925f7
@@ -2518,6 +2530,18 @@ case "${host}" in
9925f7
       GCRYPT_CIPHERS="$GCRYPT_CIPHERS poly1305-sse2-amd64.lo"
9925f7
       GCRYPT_CIPHERS="$GCRYPT_CIPHERS poly1305-avx2-amd64.lo"
9925f7
    ;;
9925f7
+   powerpc64le-*-*)
9925f7
+      # Build with the ppc8 vector implementation
9925f7
+      GCRYPT_CIPHERS="$GCRYPT_CIPHERS poly1305-new.lo"
9925f7
+   ;;
9925f7
+   powerpc64-*-*)
9925f7
+      # Build with the ppc8 vector implementation
9925f7
+      GCRYPT_CIPHERS="$GCRYPT_CIPHERS poly1305-new.lo"
9925f7
+   ;;
9925f7
+   powerpc-*-*)
9925f7
+      # Build with the ppc8 vector implementation
9925f7
+      GCRYPT_CIPHERS="$GCRYPT_CIPHERS poly1305-new.lo"
9925f7
+   ;;
9925f7
 esac
9925f7
 
9925f7
 if test x"$neonsupport" = xyes ; then
9925f7
diff --git a/mpi/longlong.h b/mpi/longlong.h
9925f7
index d6958f3b..c0f24c85 100644
9925f7
--- a/mpi/longlong.h
9925f7
+++ b/mpi/longlong.h
9925f7
@@ -1088,7 +1088,6 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
9925f7
 /* Powerpc 64 bit support taken from gmp-4.1.2. */
9925f7
 /* We should test _IBMR2 here when we add assembly support for the system
9925f7
    vendor compilers.  */
9925f7
-#if 0 /* Not yet enabled because we don't have hardware for a test. */
9925f7
 #if (defined (_ARCH_PPC) || defined (__powerpc__)) && W_TYPE_SIZE == 64
9925f7
 #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
9925f7
   do {									\
9925f7
@@ -1141,7 +1140,6 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
9925f7
 #define SMUL_TIME 14  /* ??? */
9925f7
 #define UDIV_TIME 120 /* ??? */
9925f7
 #endif /* 64-bit PowerPC.  */
9925f7
-#endif /* if 0 */
9925f7
 
9925f7
 /***************************************
9925f7
  **************  PYR  ******************
9925f7
9925f7
diff --git a/cipher/poly1305-internal-new.h b/cipher/poly1305-internal-new.h
9925f7
new file mode 100644
9925f7
index 00000000..c0f24c85 100644
9925f7
--- /dev/null
9925f7
+++ b/cipher/poly1305-internal-new.h
9925f7
@@ -0,0 +1,64 @@
9925f7
+/* poly1305-internal.h  -  Poly1305 internals
9925f7
+ * Copyright (C) 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
9925f7
+ *
9925f7
+ * This file is part of Libgcrypt.
9925f7
+ *
9925f7
+ * Libgcrypt is free software; you can redistribute it and/or modify
9925f7
+ * it under the terms of the GNU Lesser general Public License as
9925f7
+ * published by the Free Software Foundation; either version 2.1 of
9925f7
+ * the License, or (at your option) any later version.
9925f7
+ *
9925f7
+ * Libgcrypt is distributed in the hope that it will be useful,
9925f7
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9925f7
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9925f7
+ * GNU Lesser General Public License for more details.
9925f7
+ *
9925f7
+ * You should have received a copy of the GNU Lesser General Public
9925f7
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
9925f7
+ */
9925f7
+
9925f7
+#ifndef G10_POLY1305_INTERNAL_H
9925f7
+#define G10_POLY1305_INTERNAL_H
9925f7
+
9925f7
+#include <config.h>
9925f7
+#include <stdio.h>
9925f7
+#include <stdlib.h>
9925f7
+#include <string.h>
9925f7
+#include "types.h"
9925f7
+#include "g10lib.h"
9925f7
+#include "cipher.h"
9925f7
+#include "bufhelp.h"
9925f7
+
9925f7
+#define POLY1305_TAGLEN 16
9925f7
+#define POLY1305_KEYLEN 32
9925f7
+#define POLY1305_BLOCKSIZE 16
9925f7
+
9925f7
+
9925f7
+typedef struct
9925f7
+{
9925f7
+  u32 k[4];
9925f7
+  u32 r[4];
9925f7
+  u32 h[5];
9925f7
+} POLY1305_STATE;
9925f7
+
9925f7
+typedef struct poly1305_context_s
9925f7
+{
9925f7
+  POLY1305_STATE state;
9925f7
+  byte buffer[POLY1305_BLOCKSIZE];
9925f7
+  unsigned int leftover;
9925f7
+} poly1305_context_t;
9925f7
+
9925f7
+
9925f7
+gcry_err_code_t _gcry_poly1305_init (poly1305_context_t *ctx, const byte *key,
9925f7
+				     size_t keylen);
9925f7
+
9925f7
+void _gcry_poly1305_finish (poly1305_context_t *ctx,
9925f7
+			     byte mac[POLY1305_TAGLEN]);
9925f7
+
9925f7
+void _gcry_poly1305_update (poly1305_context_t *ctx, const byte *buf,
9925f7
+			     size_t buflen);
9925f7
+
9925f7
+unsigned int _gcry_poly1305_update_burn (poly1305_context_t *ctx,
9925f7
+					 const byte *m, size_t bytes);
9925f7
+
9925f7
+#endif /* G10_POLY1305_INTERNAL_H */
9925f7
9925f7
-- 
9925f7
2.27.0
9925f7