Blame SOURCES/nettle-3.4.1-powerpc64-ghash-asm.patch

185ddb
diff -up ./configure.ac.ghash ./configure.ac
185ddb
--- ./configure.ac.ghash	2021-07-14 14:11:58.126891572 +0200
185ddb
+++ ./configure.ac	2021-07-14 14:11:58.130891552 +0200
185ddb
@@ -211,6 +211,22 @@ AC_C_BIGENDIAN([AC_DEFINE([WORDS_BIGENDI
185ddb
 		ASM_WORDS_BIGENDIAN=yes],
185ddb
 	[ASM_WORDS_BIGENDIAN=no])
185ddb
 
185ddb
+AC_CACHE_CHECK([for __builtin_bswap64],
185ddb
+		nettle_cv_c_builtin_bswap64,
185ddb
+[AC_TRY_LINK([
185ddb
+#include <stdint.h>
185ddb
+],[
185ddb
+uint64_t x = 17;
185ddb
+uint64_t y = __builtin_bswap64(x);
185ddb
+],
185ddb
+nettle_cv_c_builtin_bswap64=yes,
185ddb
+nettle_cv_c_builtin_bswap64=no)])
185ddb
+
185ddb
+AH_TEMPLATE([HAVE_BUILTIN_BSWAP64], [Define if __builtin_bswap64 is available])
185ddb
+if test "x$nettle_cv_c_builtin_bswap64" = "xyes" ; then
185ddb
+  AC_DEFINE(HAVE_BUILTIN_BSWAP64)
185ddb
+fi
185ddb
+
185ddb
 LSH_GCC_ATTRIBUTES
185ddb
 
185ddb
 # According to Simon Josefsson, looking for uint32_t and friends in
185ddb
@@ -472,7 +488,7 @@ asm_replace_list="aes-encrypt-internal.a
185ddb
 		sha3-permute.asm umac-nh.asm umac-nh-n.asm machine.m4"
185ddb
 
185ddb
 # Assembler files which generate additional object files if they are used.
185ddb
-asm_nettle_optional_list="gcm-hash8.asm cpuid.asm \
185ddb
+asm_nettle_optional_list="gcm-hash.asm gcm-hash8.asm cpuid.asm \
185ddb
   aes-encrypt-internal-2.asm aes-decrypt-internal-2.asm memxor-2.asm \
185ddb
   salsa20-core-internal-2.asm sha1-compress-2.asm sha256-compress-2.asm \
185ddb
   sha3-permute-2.asm sha512-compress-2.asm \
185ddb
@@ -588,6 +604,10 @@ AH_VERBATIM([HAVE_NATIVE],
185ddb
 #undef HAVE_NATIVE_ecc_384_redc
185ddb
 #undef HAVE_NATIVE_ecc_521_modp
185ddb
 #undef HAVE_NATIVE_ecc_521_redc
185ddb
+#undef HAVE_NATIVE_gcm_init_key
185ddb
+#undef HAVE_NATIVE_fat_gcm_init_key
185ddb
+#undef HAVE_NATIVE_gcm_hash
185ddb
+#undef HAVE_NATIVE_fat_gcm_hash
185ddb
 #undef HAVE_NATIVE_gcm_hash8
185ddb
 #undef HAVE_NATIVE_salsa20_core
185ddb
 #undef HAVE_NATIVE_sha1_compress
185ddb
diff -up ./ctr16.c.ghash ./ctr16.c
185ddb
--- ./ctr16.c.ghash	2021-07-14 14:11:58.130891552 +0200
185ddb
+++ ./ctr16.c	2021-07-14 14:11:58.130891552 +0200
185ddb
@@ -0,0 +1,106 @@
185ddb
+/* ctr16.c
185ddb
+
185ddb
+   Cipher counter mode, optimized for 16-byte blocks.
185ddb
+
185ddb
+   Copyright (C) 2005-2018 Niels Möller
185ddb
+   Copyright (C) 2018 Red Hat, Inc.
185ddb
+
185ddb
+   This file is part of GNU Nettle.
185ddb
+
185ddb
+   GNU Nettle is free software: you can redistribute it and/or
185ddb
+   modify it under the terms of either:
185ddb
+
185ddb
+     * the GNU Lesser General Public License as published by the Free
185ddb
+       Software Foundation; either version 3 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or
185ddb
+
185ddb
+     * the GNU General Public License as published by the Free
185ddb
+       Software Foundation; either version 2 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or both in parallel, as here.
185ddb
+
185ddb
+   GNU Nettle is distributed in the hope that it will be useful,
185ddb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
185ddb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
185ddb
+   General Public License for more details.
185ddb
+
185ddb
+   You should have received copies of the GNU General Public License and
185ddb
+   the GNU Lesser General Public License along with this program.  If
185ddb
+   not, see http://www.gnu.org/licenses/.
185ddb
+*/
185ddb
+
185ddb
+#if HAVE_CONFIG_H
185ddb
+# include "config.h"
185ddb
+#endif
185ddb
+
185ddb
+#include <assert.h>
185ddb
+
185ddb
+#include "ctr.h"
185ddb
+
185ddb
+#include "ctr-internal.h"
185ddb
+#include "memxor.h"
185ddb
+#include "nettle-internal.h"
185ddb
+
185ddb
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
185ddb
+
185ddb
+void
185ddb
+_ctr_crypt16(const void *ctx, nettle_cipher_func *f,
185ddb
+	     nettle_fill16_func *fill, uint8_t *ctr,
185ddb
+	     size_t length, uint8_t *dst,
185ddb
+	     const uint8_t *src)
185ddb
+{
185ddb
+  if (dst != src && !((uintptr_t) dst % sizeof(uint64_t)))
185ddb
+    {
185ddb
+      size_t blocks = length / 16u;
185ddb
+      size_t done;
185ddb
+      fill (ctr, blocks, (union nettle_block16 *) dst);
185ddb
+
185ddb
+      done = blocks * 16;
185ddb
+      f(ctx, done, dst, dst);
185ddb
+      memxor (dst, src, done);
185ddb
+
185ddb
+      length -= done;
185ddb
+      if (length > 0)
185ddb
+	{ /* Left-over partial block */
185ddb
+	  union nettle_block16 block;
185ddb
+	  dst += done;
185ddb
+	  src += done;
185ddb
+	  assert (length < 16);
185ddb
+	  /* Use fill, to update ctr value in the same way in all cases. */
185ddb
+	  fill (ctr, 1, &block);
185ddb
+	  f (ctx, 16, block.b, block.b);
185ddb
+	  memxor3 (dst, src, block.b, length);
185ddb
+	}
185ddb
+    }
185ddb
+  else
185ddb
+    {
185ddb
+      /* Construct an aligned buffer of consecutive counter values, of
185ddb
+	 size at most CTR_BUFFER_LIMIT. */
185ddb
+      TMP_DECL(buffer, union nettle_block16, CTR_BUFFER_LIMIT / 16);
185ddb
+      size_t blocks = (length + 15) / 16u;
185ddb
+      size_t i;
185ddb
+      TMP_ALLOC(buffer, MIN(blocks, CTR_BUFFER_LIMIT / 16));
185ddb
+
185ddb
+      for (i = 0; blocks >= CTR_BUFFER_LIMIT / 16;
185ddb
+	   i += CTR_BUFFER_LIMIT, blocks -= CTR_BUFFER_LIMIT / 16)
185ddb
+	{
185ddb
+	  fill (ctr, CTR_BUFFER_LIMIT / 16, buffer);
185ddb
+	  f(ctx, CTR_BUFFER_LIMIT, buffer->b, buffer->b);
185ddb
+	  if (length - i < CTR_BUFFER_LIMIT)
185ddb
+	    goto done;
185ddb
+	  memxor3 (dst + i, src + i, buffer->b, CTR_BUFFER_LIMIT);
185ddb
+	}
185ddb
+
185ddb
+      if (blocks > 0)
185ddb
+	{
185ddb
+	  assert (length - i < CTR_BUFFER_LIMIT);
185ddb
+	  fill (ctr, blocks, buffer);
185ddb
+	  f(ctx, blocks * 16, buffer->b, buffer->b);
185ddb
+	done:
185ddb
+	  memxor3 (dst + i, src + i, buffer->b, length - i);
185ddb
+	}
185ddb
+    }
185ddb
+}
185ddb
diff -up ./ctr.c.ghash ./ctr.c
185ddb
--- ./ctr.c.ghash	2018-12-04 21:56:05.000000000 +0100
185ddb
+++ ./ctr.c	2021-07-14 14:13:07.714539484 +0200
185ddb
@@ -41,11 +41,83 @@
185ddb
 
185ddb
 #include "ctr.h"
185ddb
 
185ddb
+#include "ctr-internal.h"
185ddb
 #include "macros.h"
185ddb
 #include "memxor.h"
185ddb
 #include "nettle-internal.h"
185ddb
 
185ddb
-#define NBLOCKS 4
185ddb
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
185ddb
+
185ddb
+/* The 'u64' member has been added in the public header
185ddb
+   (nettle-types.h).  Check that the alignment is not affected with
185ddb
+   it using _Static_assert. */
185ddb
+union nettle_block16_
185ddb
+{
185ddb
+  uint8_t b[16];
185ddb
+  unsigned long w[16 / sizeof(unsigned long)];
185ddb
+};
185ddb
+_Static_assert(__alignof(union nettle_block16_) == __alignof(union nettle_block16),
185ddb
+	       "nettle_block16 alignment should be preserved");
185ddb
+
185ddb
+static size_t
185ddb
+ctr_fill (size_t block_size, uint8_t *ctr, size_t length, uint8_t *buffer)
185ddb
+{
185ddb
+  size_t i;
185ddb
+  for (i = 0; i + block_size <= length; i += block_size)
185ddb
+    {
185ddb
+      memcpy (buffer + i, ctr, block_size);
185ddb
+      INCREMENT(block_size, ctr);
185ddb
+    }
185ddb
+  return i;
185ddb
+}
185ddb
+
185ddb
+#if WORDS_BIGENDIAN
185ddb
+# define USE_CTR_CRYPT16 1
185ddb
+static nettle_fill16_func ctr_fill16;
185ddb
+static void
185ddb
+ctr_fill16(uint8_t *ctr, size_t blocks, union nettle_block16 *buffer)
185ddb
+{
185ddb
+  uint64_t hi, lo;
185ddb
+  size_t i;
185ddb
+  hi = READ_UINT64(ctr);
185ddb
+  lo = READ_UINT64(ctr + 8);
185ddb
+
185ddb
+  for (i = 0; i < blocks; i++)
185ddb
+    {
185ddb
+      buffer[i].u64[0] = hi;
185ddb
+      buffer[i].u64[1] = lo;
185ddb
+      hi += !(++lo);
185ddb
+    }
185ddb
+  WRITE_UINT64(ctr, hi);
185ddb
+  WRITE_UINT64(ctr + 8, lo);
185ddb
+}
185ddb
+#else /* !WORDS_BIGENDIAN */
185ddb
+# if HAVE_BUILTIN_BSWAP64
185ddb
+#  define USE_CTR_CRYPT16 1
185ddb
+static nettle_fill16_func ctr_fill16;
185ddb
+static void
185ddb
+ctr_fill16(uint8_t *ctr, size_t blocks, union nettle_block16 *buffer)
185ddb
+{
185ddb
+  uint64_t hi, lo;
185ddb
+  size_t i;
185ddb
+  /* Read hi in native endianness */
185ddb
+  hi = LE_READ_UINT64(ctr);
185ddb
+  lo = READ_UINT64(ctr + 8);
185ddb
+
185ddb
+  for (i = 0; i < blocks; i++)
185ddb
+    {
185ddb
+      buffer[i].u64[0] = hi;
185ddb
+      buffer[i].u64[1] = __builtin_bswap64(lo);
185ddb
+      if (!++lo)
185ddb
+	hi = __builtin_bswap64(__builtin_bswap64(hi) + 1);
185ddb
+    }
185ddb
+  LE_WRITE_UINT64(ctr, hi);
185ddb
+  WRITE_UINT64(ctr + 8, lo);
185ddb
+}
185ddb
+# else /* ! HAVE_BUILTIN_BSWAP64 */
185ddb
+#  define USE_CTR_CRYPT16 0
185ddb
+# endif
185ddb
+#endif /* !WORDS_BIGENDIAN */
185ddb
 
185ddb
 void
185ddb
 ctr_crypt(const void *ctx, nettle_cipher_func *f,
185ddb
@@ -53,84 +125,64 @@ ctr_crypt(const void *ctx, nettle_cipher
185ddb
 	  size_t length, uint8_t *dst,
185ddb
 	  const uint8_t *src)
185ddb
 {
185ddb
-  if (src != dst)
185ddb
+#if USE_CTR_CRYPT16
185ddb
+  if (block_size == 16)
185ddb
     {
185ddb
-      if (length == block_size)
185ddb
-	{
185ddb
-	  f(ctx, block_size, dst, ctr);
185ddb
-	  INCREMENT(block_size, ctr);
185ddb
-	  memxor(dst, src, block_size);
185ddb
-	}
185ddb
-      else
185ddb
+      _ctr_crypt16(ctx, f, ctr_fill16, ctr, length, dst, src);
185ddb
+      return;
185ddb
+    }
185ddb
+#endif
185ddb
+
185ddb
+  if(src != dst)
185ddb
+    {
185ddb
+      size_t filled = ctr_fill (block_size, ctr, length, dst);
185ddb
+
185ddb
+      f(ctx, filled, dst, dst);
185ddb
+      memxor(dst, src, filled);
185ddb
+
185ddb
+      if (filled < length)
185ddb
 	{
185ddb
-	  size_t left;
185ddb
-	  uint8_t *p;	  
185ddb
+	  TMP_DECL(block, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
185ddb
+	  TMP_ALLOC(block, block_size);
185ddb
 
185ddb
-	  for (p = dst, left = length;
185ddb
-	       left >= block_size;
185ddb
-	       left -= block_size, p += block_size)
185ddb
-	    {
185ddb
-	      memcpy (p, ctr, block_size);
185ddb
-	      INCREMENT(block_size, ctr);
185ddb
-	    }
185ddb
-
185ddb
-	  f(ctx, length - left, dst, dst);
185ddb
-	  memxor(dst, src, length - left);
185ddb
-
185ddb
-	  if (left)
185ddb
-	    {
185ddb
-	      TMP_DECL(buffer, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
185ddb
-	      TMP_ALLOC(buffer, block_size);
185ddb
-
185ddb
-	      f(ctx, block_size, buffer, ctr);
185ddb
-	      INCREMENT(block_size, ctr);
185ddb
-	      memxor3(dst + length - left, src + length - left, buffer, left);
185ddb
-	    }
185ddb
+	  f(ctx, block_size, block, ctr);
185ddb
+	  INCREMENT(block_size, ctr);
185ddb
+	  memxor3(dst + filled, src + filled, block, length - filled);
185ddb
 	}
185ddb
     }
185ddb
   else
185ddb
     {
185ddb
-      if (length > block_size)
185ddb
-	{
185ddb
-	  TMP_DECL(buffer, uint8_t, NBLOCKS * NETTLE_MAX_CIPHER_BLOCK_SIZE);
185ddb
-	  size_t chunk = NBLOCKS * block_size;
185ddb
+      /* For in-place CTR, construct a buffer of consecutive counter
185ddb
+	 values, of size at most CTR_BUFFER_LIMIT. */
185ddb
+      TMP_DECL(buffer, uint8_t, CTR_BUFFER_LIMIT);
185ddb
+
185ddb
+      size_t buffer_size;
185ddb
+      if (length < block_size)
185ddb
+	buffer_size = block_size;
185ddb
+      else if (length <= CTR_BUFFER_LIMIT)
185ddb
+	buffer_size = length;
185ddb
+      else
185ddb
+	buffer_size = CTR_BUFFER_LIMIT;
185ddb
 
185ddb
-	  TMP_ALLOC(buffer, chunk);
185ddb
+      TMP_ALLOC(buffer, buffer_size);
185ddb
 
185ddb
-	  for (; length >= chunk;
185ddb
-	       length -= chunk, src += chunk, dst += chunk)
185ddb
-	    {
185ddb
-	      unsigned n;
185ddb
-	      uint8_t *p;	  
185ddb
-	      for (n = 0, p = buffer; n < NBLOCKS; n++, p += block_size)
185ddb
-		{
185ddb
-		  memcpy (p, ctr, block_size);
185ddb
-		  INCREMENT(block_size, ctr);
185ddb
-		}
185ddb
-	      f(ctx, chunk, buffer, buffer);
185ddb
-	      memxor(dst, buffer, chunk);
185ddb
-	    }
185ddb
-
185ddb
-	  if (length > 0)
185ddb
-	    {
185ddb
-	      /* Final, possibly partial, blocks */
185ddb
-	      for (chunk = 0; chunk < length; chunk += block_size)
185ddb
-		{
185ddb
-		  memcpy (buffer + chunk, ctr, block_size);
185ddb
-		  INCREMENT(block_size, ctr);
185ddb
-		}
185ddb
-	      f(ctx, chunk, buffer, buffer);
185ddb
-	      memxor3(dst, src, buffer, length);
185ddb
-	    }
185ddb
+      while (length >= block_size)
185ddb
+	{
185ddb
+	  size_t filled
185ddb
+	    = ctr_fill (block_size, ctr, MIN(buffer_size, length), buffer);
185ddb
+	  assert (filled > 0);
185ddb
+	  f(ctx, filled, buffer, buffer);
185ddb
+	  memxor(dst, buffer, filled);
185ddb
+	  length -= filled;
185ddb
+	  dst += filled;
185ddb
 	}
185ddb
-      else if (length > 0)
185ddb
-      	{
185ddb
-	  TMP_DECL(buffer, uint8_t, NETTLE_MAX_CIPHER_BLOCK_SIZE);
185ddb
-	  TMP_ALLOC(buffer, block_size);
185ddb
 
185ddb
+      /* Final, possibly partial, block. */
185ddb
+      if (length > 0)
185ddb
+	{
185ddb
 	  f(ctx, block_size, buffer, ctr);
185ddb
 	  INCREMENT(block_size, ctr);
185ddb
-	  memxor3(dst, src, buffer, length);
185ddb
+	  memxor(dst, buffer, length);
185ddb
 	}
185ddb
     }
185ddb
 }
185ddb
diff -up ./ctr-internal.h.ghash ./ctr-internal.h
185ddb
--- ./ctr-internal.h.ghash	2021-07-14 14:11:58.130891552 +0200
185ddb
+++ ./ctr-internal.h	2021-07-14 14:11:58.130891552 +0200
185ddb
@@ -0,0 +1,56 @@
185ddb
+/* ctr-internal.h
185ddb
+
185ddb
+   Copyright (C) 2018 Niels Möller
185ddb
+
185ddb
+   This file is part of GNU Nettle.
185ddb
+
185ddb
+   GNU Nettle is free software: you can redistribute it and/or
185ddb
+   modify it under the terms of either:
185ddb
+
185ddb
+     * the GNU Lesser General Public License as published by the Free
185ddb
+       Software Foundation; either version 3 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or
185ddb
+
185ddb
+     * the GNU General Public License as published by the Free
185ddb
+       Software Foundation; either version 2 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or both in parallel, as here.
185ddb
+
185ddb
+   GNU Nettle is distributed in the hope that it will be useful,
185ddb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
185ddb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
185ddb
+   General Public License for more details.
185ddb
+
185ddb
+   You should have received copies of the GNU General Public License and
185ddb
+   the GNU Lesser General Public License along with this program.  If
185ddb
+   not, see http://www.gnu.org/licenses/.
185ddb
+*/
185ddb
+
185ddb
+#ifndef NETTLE_CTR_INTERNAL_H_INCLUDED
185ddb
+#define NETTLE_CTR_INTERNAL_H_INCLUDED
185ddb
+
185ddb
+#include "nettle-types.h"
185ddb
+
185ddb
+/* Name mangling */
185ddb
+#define _ctr_crypt16 _nettle_ctr_crypt16
185ddb
+
185ddb
+/* Size limit for temporary stack buffers. */
185ddb
+#define CTR_BUFFER_LIMIT 512
185ddb
+
185ddb
+/* Fill BUFFER (n blocks) with incrementing CTR values. It would be
185ddb
+   nice if CTR was always 64-bit aligned, but it isn't when called
185ddb
+   from ctr_crypt. */
185ddb
+typedef void
185ddb
+nettle_fill16_func(uint8_t *ctr, size_t n, union nettle_block16 *buffer);
185ddb
+
185ddb
+void
185ddb
+_ctr_crypt16(const void *ctx, nettle_cipher_func *f,
185ddb
+	     nettle_fill16_func *fill, uint8_t *ctr,
185ddb
+	     size_t length, uint8_t *dst,
185ddb
+	     const uint8_t *src);
185ddb
+
185ddb
+
185ddb
+#endif /* NETTLE_CTR_INTERNAL_H_INCLUDED */
185ddb
diff -up ./fat-ppc.c.ghash ./fat-ppc.c
185ddb
--- ./fat-ppc.c.ghash	2021-07-14 14:11:58.126891572 +0200
185ddb
+++ ./fat-ppc.c	2021-07-14 14:11:58.130891552 +0200
185ddb
@@ -49,6 +49,7 @@
185ddb
 
185ddb
 #include "aes-internal.h"
185ddb
 #include "gcm.h"
185ddb
+#include "gcm-internal.h"
185ddb
 #include "fat-setup.h"
185ddb
 
185ddb
 /* Define from arch/powerpc/include/uapi/asm/cputable.h in Linux kernel */
185ddb
@@ -87,6 +88,16 @@ DECLARE_FAT_FUNC(_nettle_aes_decrypt, ae
185ddb
 DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, c)
185ddb
 DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, ppc64)
185ddb
 
185ddb
+#if GCM_TABLE_BITS == 8
185ddb
+DECLARE_FAT_FUNC(_nettle_gcm_init_key, gcm_init_key_func)
185ddb
+DECLARE_FAT_FUNC_VAR(gcm_init_key, gcm_init_key_func, c)
185ddb
+DECLARE_FAT_FUNC_VAR(gcm_init_key, gcm_init_key_func, ppc64)
185ddb
+
185ddb
+DECLARE_FAT_FUNC(_nettle_gcm_hash, gcm_hash_func)
185ddb
+DECLARE_FAT_FUNC_VAR(gcm_hash, gcm_hash_func, c)
185ddb
+DECLARE_FAT_FUNC_VAR(gcm_hash, gcm_hash_func, ppc64)
185ddb
+#endif /* GCM_TABLE_BITS == 8 */
185ddb
+
185ddb
 static void CONSTRUCTOR
185ddb
 fat_init (void)
185ddb
 {
185ddb
@@ -101,17 +112,29 @@ fat_init (void)
185ddb
      features.have_crypto_ext ? "crypto extensions" : "");
185ddb
 
185ddb
   if (features.have_crypto_ext)
185ddb
-  {
185ddb
-     if (verbose)
185ddb
-        fprintf (stderr, "libnettle: enabling arch 2.07 code.\n");
185ddb
-     _nettle_aes_encrypt_vec = _nettle_aes_encrypt_ppc64;
185ddb
-     _nettle_aes_decrypt_vec = _nettle_aes_decrypt_ppc64;
185ddb
-  }
185ddb
+    {
185ddb
+      if (verbose)
185ddb
+	fprintf (stderr, "libnettle: enabling arch 2.07 code.\n");
185ddb
+      _nettle_aes_encrypt_vec = _nettle_aes_encrypt_ppc64;
185ddb
+      _nettle_aes_decrypt_vec = _nettle_aes_decrypt_ppc64;
185ddb
+#if GCM_TABLE_BITS == 8
185ddb
+      /* Make sure _nettle_gcm_init_key_vec function is compatible
185ddb
+         with _nettle_gcm_hash_vec function e.g. _nettle_gcm_init_key_c()
185ddb
+         fills gcm_key table with values that are incompatible with
185ddb
+         _nettle_gcm_hash_ppc64() */
185ddb
+      _nettle_gcm_init_key_vec = _nettle_gcm_init_key_ppc64;
185ddb
+      _nettle_gcm_hash_vec = _nettle_gcm_hash_ppc64;
185ddb
+#endif /* GCM_TABLE_BITS == 8 */
185ddb
+    }
185ddb
   else
185ddb
-  {
185ddb
-     _nettle_aes_encrypt_vec = _nettle_aes_encrypt_c;
185ddb
-     _nettle_aes_decrypt_vec = _nettle_aes_decrypt_c;
185ddb
-  }
185ddb
+    {
185ddb
+      _nettle_aes_encrypt_vec = _nettle_aes_encrypt_c;
185ddb
+      _nettle_aes_decrypt_vec = _nettle_aes_decrypt_c;
185ddb
+#if GCM_TABLE_BITS == 8
185ddb
+      _nettle_gcm_init_key_vec = _nettle_gcm_init_key_c;
185ddb
+      _nettle_gcm_hash_vec = _nettle_gcm_hash_c;
185ddb
+#endif /* GCM_TABLE_BITS == 8 */
185ddb
+    }
185ddb
 }
185ddb
 
185ddb
 DEFINE_FAT_FUNC(_nettle_aes_encrypt, void,
185ddb
@@ -127,3 +150,14 @@ DEFINE_FAT_FUNC(_nettle_aes_decrypt, voi
185ddb
  size_t length, uint8_t *dst,
185ddb
  const uint8_t *src),
185ddb
  (rounds, keys, T, length, dst, src))
185ddb
+
185ddb
+#if GCM_TABLE_BITS == 8
185ddb
+DEFINE_FAT_FUNC(_nettle_gcm_init_key, void,
185ddb
+		(union nettle_block16 *table),
185ddb
+		(table))
185ddb
+
185ddb
+DEFINE_FAT_FUNC(_nettle_gcm_hash, void,
185ddb
+		(const struct gcm_key *key, union nettle_block16 *x,
185ddb
+		 size_t length, const uint8_t *data),
185ddb
+		(key, x, length, data))
185ddb
+#endif /* GCM_TABLE_BITS == 8 */
185ddb
diff -up ./fat-setup.h.ghash ./fat-setup.h
185ddb
--- ./fat-setup.h.ghash	2018-12-04 21:56:06.000000000 +0100
185ddb
+++ ./fat-setup.h	2021-07-14 14:11:58.130891552 +0200
185ddb
@@ -159,6 +159,11 @@ typedef void aes_crypt_internal_func (un
185ddb
 				      size_t length, uint8_t *dst,
185ddb
 				      const uint8_t *src);
185ddb
 
185ddb
+typedef void gcm_init_key_func (union nettle_block16 *table);
185ddb
+
185ddb
+typedef void gcm_hash_func (const struct gcm_key *key, union nettle_block16 *x,
185ddb
+			    size_t length, const uint8_t *data);
185ddb
+
185ddb
 typedef void *(memxor_func)(void *dst, const void *src, size_t n);
185ddb
 
185ddb
 typedef void salsa20_core_func (uint32_t *dst, const uint32_t *src, unsigned rounds);
185ddb
diff -up ./gcm.c.ghash ./gcm.c
185ddb
--- ./gcm.c.ghash	2018-12-04 21:56:05.000000000 +0100
185ddb
+++ ./gcm.c	2021-07-14 14:11:58.131891547 +0200
185ddb
@@ -6,8 +6,9 @@
185ddb
    See also the gcm paper at
185ddb
    http://www.cryptobarn.com/papers/gcm-spec.pdf.
185ddb
 
185ddb
-   Copyright (C) 2011, 2013 Niels Möller
185ddb
    Copyright (C) 2011 Katholieke Universiteit Leuven
185ddb
+   Copyright (C) 2011, 2013, 2018 Niels Möller
185ddb
+   Copyright (C) 2018 Red Hat, Inc.
185ddb
    
185ddb
    Contributed by Nikos Mavrogiannopoulos
185ddb
 
185ddb
@@ -48,9 +49,11 @@
185ddb
 
185ddb
 #include "gcm.h"
185ddb
 
185ddb
+#include "gcm-internal.h"
185ddb
 #include "memxor.h"
185ddb
 #include "nettle-internal.h"
185ddb
 #include "macros.h"
185ddb
+#include "ctr-internal.h"
185ddb
 
185ddb
 #define GHASH_POLYNOMIAL 0xE1UL
185ddb
 
185ddb
@@ -112,7 +115,17 @@ gcm_gf_shift (union nettle_block16 *r, c
185ddb
 #endif /* ! WORDS_BIGENDIAN */
185ddb
 }
185ddb
 
185ddb
-#if GCM_TABLE_BITS == 0
185ddb
+#if GCM_TABLE_BITS != 8
185ddb
+/* The native implementations (currently ppc64 only) depend on the
185ddb
+   GCM_TABLE_BITS == 8 layout */
185ddb
+#undef HAVE_NATIVE_gcm_hash
185ddb
+#undef HAVE_NATIVE_gcm_init_key
185ddb
+#undef HAVE_NATIVE_fat_gcm_hash
185ddb
+#undef HAVE_NATIVE_fat_gcm_init_key
185ddb
+#endif
185ddb
+
185ddb
+#if !HAVE_NATIVE_gcm_hash
185ddb
+# if GCM_TABLE_BITS == 0
185ddb
 /* Sets x <- x * y mod r, using the plain bitwise algorithm from the
185ddb
    specification. y may be shorter than a full block, missing bytes
185ddb
    are assumed zero. */
185ddb
@@ -140,15 +153,15 @@ gcm_gf_mul (union nettle_block16 *x, con
185ddb
     }
185ddb
   memcpy (x->b, Z.b, sizeof(Z));
185ddb
 }
185ddb
-#else /* GCM_TABLE_BITS != 0 */
185ddb
+# else /* GCM_TABLE_BITS != 0 */
185ddb
 
185ddb
-# if WORDS_BIGENDIAN
185ddb
-#  define W(left,right) (0x##left##right)
185ddb
-# else
185ddb
-#  define W(left,right) (0x##right##left)
185ddb
-# endif
185ddb
+#  if WORDS_BIGENDIAN
185ddb
+#   define W(left,right) (0x##left##right)
185ddb
+#  else
185ddb
+#   define W(left,right) (0x##right##left)
185ddb
+#  endif
185ddb
 
185ddb
-# if GCM_TABLE_BITS == 4
185ddb
+#  if GCM_TABLE_BITS == 4
185ddb
 static const uint16_t
185ddb
 shift_table[0x10] = {
185ddb
   W(00,00),W(1c,20),W(38,40),W(24,60),W(70,80),W(6c,a0),W(48,c0),W(54,e0),
185ddb
@@ -177,26 +190,13 @@ gcm_gf_shift_4(union nettle_block16 *x)
185ddb
 #  error Unsupported word size. */
185ddb
 #endif
185ddb
 #else /* ! WORDS_BIGENDIAN */
185ddb
-# if SIZEOF_LONG == 4
185ddb
-#define RSHIFT_WORD(x) \
185ddb
-  ((((x) & 0xf0f0f0f0UL) >> 4)			\
185ddb
-   | (((x) & 0x000f0f0f) << 12))
185ddb
-  reduce = shift_table[(w[3] >> 24) & 0xf];
185ddb
-  w[3] = RSHIFT_WORD(w[3]) | ((w[2] >> 20) & 0xf0);
185ddb
-  w[2] = RSHIFT_WORD(w[2]) | ((w[1] >> 20) & 0xf0);
185ddb
-  w[1] = RSHIFT_WORD(w[1]) | ((w[0] >> 20) & 0xf0);
185ddb
-  w[0] = RSHIFT_WORD(w[0]) ^ reduce;
185ddb
-# elif SIZEOF_LONG == 8
185ddb
-#define RSHIFT_WORD(x) \
185ddb
-  ((((x) & 0xf0f0f0f0f0f0f0f0UL) >> 4) \
185ddb
-   | (((x) & 0x000f0f0f0f0f0f0fUL) << 12))
185ddb
-  reduce = shift_table[(w[1] >> 56) & 0xf];
185ddb
-  w[1] = RSHIFT_WORD(w[1]) | ((w[0] >> 52) & 0xf0);
185ddb
-  w[0] = RSHIFT_WORD(w[0]) ^ reduce;
185ddb
-# else
185ddb
-#  error Unsupported word size. */
185ddb
-# endif
185ddb
-# undef RSHIFT_WORD
185ddb
+# define RSHIFT_WORD_4(x) \
185ddb
+  ((((x) & UINT64_C(0xf0f0f0f0f0f0f0f0)) >> 4) \
185ddb
+   | (((x) & UINT64_C(0x000f0f0f0f0f0f0f)) << 12))
185ddb
+  reduce = shift_table[(u64[1] >> 56) & 0xf];
185ddb
+  u64[1] = RSHIFT_WORD_4(u64[1]) | ((u64[0] >> 52) & 0xf0);
185ddb
+  u64[0] = RSHIFT_WORD_4(u64[0]) ^ reduce;
185ddb
+# undef RSHIFT_WORD_4
185ddb
 #endif /* ! WORDS_BIGENDIAN */
185ddb
 }
185ddb
 
185ddb
@@ -219,10 +219,10 @@ gcm_gf_mul (union nettle_block16 *x, con
185ddb
     }
185ddb
   memcpy (x->b, Z.b, sizeof(Z));
185ddb
 }
185ddb
-# elif GCM_TABLE_BITS == 8
185ddb
-#  if HAVE_NATIVE_gcm_hash8
185ddb
+#  elif GCM_TABLE_BITS == 8
185ddb
+#   if HAVE_NATIVE_gcm_hash8
185ddb
 
185ddb
-#define gcm_hash _nettle_gcm_hash8
185ddb
+#define _nettle_gcm_hash _nettle_gcm_hash8
185ddb
 void
185ddb
 _nettle_gcm_hash8 (const struct gcm_key *key, union nettle_block16 *x,
185ddb
 		   size_t length, const uint8_t *data);
185ddb
@@ -317,18 +317,46 @@ gcm_gf_mul (union nettle_block16 *x, con
185ddb
   gcm_gf_shift_8(&Z);
185ddb
   gcm_gf_add(x, &Z, &table[x->b[0]]);
185ddb
 }
185ddb
-#  endif /* ! HAVE_NATIVE_gcm_hash8 */
185ddb
-# else /* GCM_TABLE_BITS != 8 */
185ddb
-#  error Unsupported table size. 
185ddb
-# endif /* GCM_TABLE_BITS != 8 */
185ddb
+#   endif /* ! HAVE_NATIVE_gcm_hash8 */
185ddb
+#  else /* GCM_TABLE_BITS != 8 */
185ddb
+#   error Unsupported table size.
185ddb
+#  endif /* GCM_TABLE_BITS != 8 */
185ddb
+
185ddb
+#  undef W
185ddb
+# endif /* GCM_TABLE_BITS != 0 */
185ddb
+#endif /* !HAVE_NATIVE_gcm_hash */
185ddb
 
185ddb
-#undef W
185ddb
-
185ddb
-#endif /* GCM_TABLE_BITS */
185ddb
 
185ddb
 /* Increment the rightmost 32 bits. */
185ddb
 #define INC32(block) INCREMENT(4, (block.b) + GCM_BLOCK_SIZE - 4)
185ddb
 
185ddb
+#if !HAVE_NATIVE_gcm_init_key
185ddb
+# if !HAVE_NATIVE_fat_gcm_hash
185ddb
+#  define _nettle_gcm_init_key _nettle_gcm_init_key_c
185ddb
+static
185ddb
+# endif
185ddb
+void
185ddb
+_nettle_gcm_init_key_c(union nettle_block16 *table)
185ddb
+{
185ddb
+#if GCM_TABLE_BITS
185ddb
+  /* Middle element if GCM_TABLE_BITS > 0, otherwise the first
185ddb
+     element */
185ddb
+  unsigned i = (1<
185ddb
+
185ddb
+  /* Algorithm 3 from the gcm paper. First do powers of two, then do
185ddb
+     the rest by adding. */
185ddb
+  while (i /= 2)
185ddb
+    gcm_gf_shift(&table[i], &table[2*i]);
185ddb
+  for (i = 2; i < 1<
185ddb
+    {
185ddb
+      unsigned j;
185ddb
+      for (j = 1; j < i; j++)
185ddb
+        gcm_gf_add(&table[i+j], &table[i], &table[j]);
185ddb
+    }
185ddb
+#endif
185ddb
+}
185ddb
+#endif /* !HAVE_NATIVE_gcm_init_key */
185ddb
+
185ddb
 /* Initialization of GCM.
185ddb
  * @ctx: The context of GCM
185ddb
  * @cipher: The context of the underlying block cipher
185ddb
@@ -345,25 +373,18 @@ gcm_set_key(struct gcm_key *key,
185ddb
   /* H */  
185ddb
   memset(key->h[0].b, 0, GCM_BLOCK_SIZE);
185ddb
   f (cipher, GCM_BLOCK_SIZE, key->h[i].b, key->h[0].b);
185ddb
-  
185ddb
-#if GCM_TABLE_BITS
185ddb
-  /* Algorithm 3 from the gcm paper. First do powers of two, then do
185ddb
-     the rest by adding. */
185ddb
-  while (i /= 2)
185ddb
-    gcm_gf_shift(&key->h[i], &key->h[2*i]);
185ddb
-  for (i = 2; i < 1<
185ddb
-    {
185ddb
-      unsigned j;
185ddb
-      for (j = 1; j < i; j++)
185ddb
-	gcm_gf_add(&key->h[i+j], &key->h[i],&key->h[j]);
185ddb
-    }
185ddb
-#endif
185ddb
+
185ddb
+  _nettle_gcm_init_key(key->h);
185ddb
 }
185ddb
 
185ddb
-#ifndef gcm_hash
185ddb
-static void
185ddb
-gcm_hash(const struct gcm_key *key, union nettle_block16 *x,
185ddb
-	 size_t length, const uint8_t *data)
185ddb
+#if !(HAVE_NATIVE_gcm_hash || HAVE_NATIVE_gcm_hash8)
185ddb
+# if !HAVE_NATIVE_fat_gcm_hash
185ddb
+#  define _nettle_gcm_hash _nettle_gcm_hash_c
185ddb
+static
185ddb
+# endif
185ddb
+void
185ddb
+_nettle_gcm_hash_c(const struct gcm_key *key, union nettle_block16 *x,
185ddb
+		   size_t length, const uint8_t *data)
185ddb
 {
185ddb
   for (; length >= GCM_BLOCK_SIZE;
185ddb
        length -= GCM_BLOCK_SIZE, data += GCM_BLOCK_SIZE)
185ddb
@@ -377,7 +398,7 @@ gcm_hash(const struct gcm_key *key, unio
185ddb
       gcm_gf_mul (x, key->h);
185ddb
     }
185ddb
 }
185ddb
-#endif /* !gcm_hash */
185ddb
+#endif /* !(HAVE_NATIVE_gcm_hash || HAVE_NATIVE_gcm_hash8) */
185ddb
 
185ddb
 static void
185ddb
 gcm_hash_sizes(const struct gcm_key *key, union nettle_block16 *x,
185ddb
@@ -391,7 +412,7 @@ gcm_hash_sizes(const struct gcm_key *key
185ddb
   WRITE_UINT64 (buffer, auth_size);
185ddb
   WRITE_UINT64 (buffer + 8, data_size);
185ddb
 
185ddb
-  gcm_hash(key, x, GCM_BLOCK_SIZE, buffer);
185ddb
+  _nettle_gcm_hash(key, x, GCM_BLOCK_SIZE, buffer);
185ddb
 }
185ddb
 
185ddb
 /* NOTE: The key is needed only if length != GCM_IV_SIZE */
185ddb
@@ -410,7 +431,7 @@ gcm_set_iv(struct gcm_ctx *ctx, const st
185ddb
   else
185ddb
     {
185ddb
       memset(ctx->iv.b, 0, GCM_BLOCK_SIZE);
185ddb
-      gcm_hash(key, &ctx->iv, length, iv);
185ddb
+      _nettle_gcm_hash(key, &ctx->iv, length, iv);
185ddb
       gcm_hash_sizes(key, &ctx->iv, 0, length);
185ddb
     }
185ddb
 
185ddb
@@ -429,47 +450,68 @@ gcm_update(struct gcm_ctx *ctx, const st
185ddb
   assert(ctx->auth_size % GCM_BLOCK_SIZE == 0);
185ddb
   assert(ctx->data_size == 0);
185ddb
 
185ddb
-  gcm_hash(key, &ctx->x, length, data);
185ddb
+  _nettle_gcm_hash(key, &ctx->x, length, data);
185ddb
 
185ddb
   ctx->auth_size += length;
185ddb
 }
185ddb
 
185ddb
+static nettle_fill16_func gcm_fill;
185ddb
+#if WORDS_BIGENDIAN
185ddb
 static void
185ddb
-gcm_crypt(struct gcm_ctx *ctx, const void *cipher, nettle_cipher_func *f,
185ddb
-	  size_t length, uint8_t *dst, const uint8_t *src)
185ddb
+gcm_fill(uint8_t *ctr, size_t blocks, union nettle_block16 *buffer)
185ddb
 {
185ddb
-  uint8_t buffer[GCM_BLOCK_SIZE];
185ddb
+  uint64_t hi, mid;
185ddb
+  uint32_t lo;
185ddb
+  size_t i;
185ddb
+  hi = READ_UINT64(ctr);
185ddb
+  mid = (uint64_t) READ_UINT32(ctr + 8) << 32;
185ddb
+  lo = READ_UINT32(ctr + 12);
185ddb
 
185ddb
-  if (src != dst)
185ddb
+  for (i = 0; i < blocks; i++)
185ddb
     {
185ddb
-      for (; length >= GCM_BLOCK_SIZE;
185ddb
-           (length -= GCM_BLOCK_SIZE,
185ddb
-	    src += GCM_BLOCK_SIZE, dst += GCM_BLOCK_SIZE))
185ddb
-        {
185ddb
-          f (cipher, GCM_BLOCK_SIZE, dst, ctx->ctr.b);
185ddb
-          memxor (dst, src, GCM_BLOCK_SIZE);
185ddb
-          INC32 (ctx->ctr);
185ddb
-        }
185ddb
+      buffer[i].u64[0] = hi;
185ddb
+      buffer[i].u64[1] = mid + lo++;
185ddb
     }
185ddb
-  else
185ddb
+  WRITE_UINT32(ctr + 12, lo);
185ddb
+
185ddb
+}
185ddb
+#elif HAVE_BUILTIN_BSWAP64
185ddb
+/* Assume __builtin_bswap32 is also available */
185ddb
+static void
185ddb
+gcm_fill(uint8_t *ctr, size_t blocks, union nettle_block16 *buffer)
185ddb
+{
185ddb
+  uint64_t hi, mid;
185ddb
+  uint32_t lo;
185ddb
+  size_t i;
185ddb
+  hi = LE_READ_UINT64(ctr);
185ddb
+  mid = LE_READ_UINT32(ctr + 8);
185ddb
+  lo = READ_UINT32(ctr + 12);
185ddb
+
185ddb
+  for (i = 0; i < blocks; i++)
185ddb
     {
185ddb
-      for (; length >= GCM_BLOCK_SIZE;
185ddb
-           (length -= GCM_BLOCK_SIZE,
185ddb
-	    src += GCM_BLOCK_SIZE, dst += GCM_BLOCK_SIZE))
185ddb
-        {
185ddb
-          f (cipher, GCM_BLOCK_SIZE, buffer, ctx->ctr.b);
185ddb
-          memxor3 (dst, src, buffer, GCM_BLOCK_SIZE);
185ddb
-          INC32 (ctx->ctr);
185ddb
-        }
185ddb
+      buffer[i].u64[0] = hi;
185ddb
+      buffer[i].u64[1] = mid + ((uint64_t)__builtin_bswap32(lo) << 32);
185ddb
+      lo++;
185ddb
     }
185ddb
-  if (length > 0)
185ddb
+  WRITE_UINT32(ctr + 12, lo);
185ddb
+}
185ddb
+#else
185ddb
+static void
185ddb
+gcm_fill(uint8_t *ctr, size_t blocks, union nettle_block16 *buffer)
185ddb
+{
185ddb
+  uint32_t c;
185ddb
+
185ddb
+  c = READ_UINT32(ctr + GCM_BLOCK_SIZE - 4);
185ddb
+
185ddb
+  for (; blocks-- > 0; buffer++, c++)
185ddb
     {
185ddb
-      /* A final partial block */
185ddb
-      f (cipher, GCM_BLOCK_SIZE, buffer, ctx->ctr.b);
185ddb
-      memxor3 (dst, src, buffer, length);
185ddb
-      INC32 (ctx->ctr);
185ddb
+      memcpy(buffer->b, ctr, GCM_BLOCK_SIZE - 4);
185ddb
+      WRITE_UINT32(buffer->b + GCM_BLOCK_SIZE - 4, c);
185ddb
     }
185ddb
+
185ddb
+  WRITE_UINT32(ctr + GCM_BLOCK_SIZE - 4, c);
185ddb
 }
185ddb
+#endif
185ddb
 
185ddb
 void
185ddb
 gcm_encrypt (struct gcm_ctx *ctx, const struct gcm_key *key,
185ddb
@@ -478,8 +520,8 @@ gcm_encrypt (struct gcm_ctx *ctx, const
185ddb
 {
185ddb
   assert(ctx->data_size % GCM_BLOCK_SIZE == 0);
185ddb
 
185ddb
-  gcm_crypt(ctx, cipher, f, length, dst, src);
185ddb
-  gcm_hash(key, &ctx->x, length, dst);
185ddb
+  _ctr_crypt16(cipher, f, gcm_fill, ctx->ctr.b, length, dst, src);
185ddb
+  _nettle_gcm_hash(key, &ctx->x, length, dst);
185ddb
 
185ddb
   ctx->data_size += length;
185ddb
 }
185ddb
@@ -491,8 +533,8 @@ gcm_decrypt(struct gcm_ctx *ctx, const s
185ddb
 {
185ddb
   assert(ctx->data_size % GCM_BLOCK_SIZE == 0);
185ddb
 
185ddb
-  gcm_hash(key, &ctx->x, length, src);
185ddb
-  gcm_crypt(ctx, cipher, f, length, dst, src);
185ddb
+  _nettle_gcm_hash(key, &ctx->x, length, src);
185ddb
+  _ctr_crypt16(cipher, f, gcm_fill, ctx->ctr.b, length, dst, src);
185ddb
 
185ddb
   ctx->data_size += length;
185ddb
 }
185ddb
diff -up ./gcm-internal.h.ghash ./gcm-internal.h
185ddb
--- ./gcm-internal.h.ghash	2021-07-14 14:11:58.131891547 +0200
185ddb
+++ ./gcm-internal.h	2021-07-14 14:11:58.131891547 +0200
185ddb
@@ -0,0 +1,54 @@
185ddb
+/* gcm-internal.h
185ddb
+
185ddb
+   Copyright (C) 2020 Niels Möller
185ddb
+
185ddb
+   This file is part of GNU Nettle.
185ddb
+
185ddb
+   GNU Nettle is free software: you can redistribute it and/or
185ddb
+   modify it under the terms of either:
185ddb
+
185ddb
+     * the GNU Lesser General Public License as published by the Free
185ddb
+       Software Foundation; either version 3 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or
185ddb
+
185ddb
+     * the GNU General Public License as published by the Free
185ddb
+       Software Foundation; either version 2 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or both in parallel, as here.
185ddb
+
185ddb
+   GNU Nettle is distributed in the hope that it will be useful,
185ddb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
185ddb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
185ddb
+   General Public License for more details.
185ddb
+
185ddb
+   You should have received copies of the GNU General Public License and
185ddb
+   the GNU Lesser General Public License along with this program.  If
185ddb
+   not, see http://www.gnu.org/licenses/.
185ddb
+*/
185ddb
+
185ddb
+#ifndef NETTLE_GCM_INTERNAL_H_INCLUDED
185ddb
+#define NETTLE_GCM_INTERNAL_H_INCLUDED
185ddb
+
185ddb
+/* Functions available only in some configurations */
185ddb
+void
185ddb
+_nettle_gcm_init_key (union nettle_block16 *table);
185ddb
+
185ddb
+void
185ddb
+_nettle_gcm_hash(const struct gcm_key *key, union nettle_block16 *x,
185ddb
+		 size_t length, const uint8_t *data);
185ddb
+
185ddb
+#if HAVE_NATIVE_fat_gcm_init_key
185ddb
+void
185ddb
+_nettle_gcm_init_key_c (union nettle_block16 *table);
185ddb
+#endif
185ddb
+
185ddb
+#if HAVE_NATIVE_fat_gcm_hash
185ddb
+void
185ddb
+_nettle_gcm_hash_c (const struct gcm_key *key, union nettle_block16 *x,
185ddb
+		    size_t length, const uint8_t *data);
185ddb
+#endif
185ddb
+
185ddb
+#endif /* NETTLE_GCM_INTERNAL_H_INCLUDED */
185ddb
diff -up ./Makefile.in.ghash ./Makefile.in
185ddb
--- ./Makefile.in.ghash	2021-07-14 14:11:58.124891582 +0200
185ddb
+++ ./Makefile.in	2021-07-14 14:11:58.131891547 +0200
185ddb
@@ -96,7 +96,7 @@ nettle_SOURCES = aes-decrypt-internal.c
185ddb
 		 chacha-crypt.c chacha-core-internal.c \
185ddb
 		 chacha-poly1305.c chacha-poly1305-meta.c \
185ddb
 		 chacha-set-key.c chacha-set-nonce.c \
185ddb
-		 ctr.c des.c des3.c des-compat.c \
185ddb
+		 ctr.c ctr16.c des.c des3.c des-compat.c \
185ddb
 		 eax.c eax-aes128.c eax-aes128-meta.c \
185ddb
 		 gcm.c gcm-aes.c \
185ddb
 		 gcm-aes128.c gcm-aes128-meta.c \
185ddb
@@ -233,6 +233,8 @@ DISTFILES = $(SOURCES) $(HEADERS) getopt
185ddb
 	cast128_sboxes.h desinfo.h desCode.h \
185ddb
 	memxor-internal.h nettle-internal.h nettle-write.h \
185ddb
 	rsa-internal.h \
185ddb
+	ctr-internal.h \
185ddb
+	gcm-internal.h \
185ddb
 	gmp-glue.h ecc-internal.h fat-setup.h \
185ddb
 	mini-gmp.h asm.m4 \
185ddb
 	nettle.texinfo nettle.info nettle.html nettle.pdf sha-example.c
185ddb
diff -up ./nettle-types.h.ghash ./nettle-types.h
185ddb
--- ./nettle-types.h.ghash	2018-12-04 21:56:06.000000000 +0100
185ddb
+++ ./nettle-types.h	2021-07-14 14:11:58.131891547 +0200
185ddb
@@ -48,6 +48,7 @@ union nettle_block16
185ddb
 {
185ddb
   uint8_t b[16];
185ddb
   unsigned long w[16 / sizeof(unsigned long)];
185ddb
+  uint64_t u64[2];
185ddb
 };
185ddb
 
185ddb
 /* Randomness. Used by key generation and dsa signature creation. */
185ddb
diff -up ./powerpc64/fat/gcm-hash.asm.ghash ./powerpc64/fat/gcm-hash.asm
185ddb
--- ./powerpc64/fat/gcm-hash.asm.ghash	2021-07-14 14:11:58.131891547 +0200
185ddb
+++ ./powerpc64/fat/gcm-hash.asm	2021-07-14 14:11:58.131891547 +0200
185ddb
@@ -0,0 +1,39 @@
185ddb
+C powerpc64/fat/gcm-hash.asm
185ddb
+
185ddb
+
185ddb
+ifelse(<
185ddb
+   Copyright (C) 2020 Mamone Tarsha
185ddb
+
185ddb
+   This file is part of GNU Nettle.
185ddb
+
185ddb
+   GNU Nettle is free software: you can redistribute it and/or
185ddb
+   modify it under the terms of either:
185ddb
+
185ddb
+     * the GNU Lesser General Public License as published by the Free
185ddb
+       Software Foundation; either version 3 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or
185ddb
+
185ddb
+     * the GNU General Public License as published by the Free
185ddb
+       Software Foundation; either version 2 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or both in parallel, as here.
185ddb
+
185ddb
+   GNU Nettle is distributed in the hope that it will be useful,
185ddb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
185ddb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
185ddb
+   General Public License for more details.
185ddb
+
185ddb
+   You should have received copies of the GNU General Public License and
185ddb
+   the GNU Lesser General Public License along with this program.  If
185ddb
+   not, see http://www.gnu.org/licenses/.
185ddb
+>)
185ddb
+
185ddb
+dnl picked up by configure
185ddb
+dnl PROLOGUE(_nettle_fat_gcm_init_key)
185ddb
+dnl PROLOGUE(_nettle_fat_gcm_hash)
185ddb
+
185ddb
+define(<fat_transform>, <$1_ppc64>)
185ddb
+include_src(<powerpc64/p8/gcm-hash.asm>)
185ddb
diff -up ./powerpc64/p8/gcm-hash.asm.ghash ./powerpc64/p8/gcm-hash.asm
185ddb
--- ./powerpc64/p8/gcm-hash.asm.ghash	2021-07-14 14:11:58.131891547 +0200
185ddb
+++ ./powerpc64/p8/gcm-hash.asm	2021-07-14 14:11:58.131891547 +0200
185ddb
@@ -0,0 +1,499 @@
185ddb
+C powerpc64/p8/gcm-hash.asm
185ddb
+
185ddb
+ifelse(<
185ddb
+   Copyright (C) 2020 Niels Möller and Mamone Tarsha
185ddb
+   This file is part of GNU Nettle.
185ddb
+
185ddb
+   GNU Nettle is free software: you can redistribute it and/or
185ddb
+   modify it under the terms of either:
185ddb
+
185ddb
+     * the GNU Lesser General Public License as published by the Free
185ddb
+       Software Foundation; either version 3 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or
185ddb
+
185ddb
+     * the GNU General Public License as published by the Free
185ddb
+       Software Foundation; either version 2 of the License, or (at your
185ddb
+       option) any later version.
185ddb
+
185ddb
+   or both in parallel, as here.
185ddb
+
185ddb
+   GNU Nettle is distributed in the hope that it will be useful,
185ddb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
185ddb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
185ddb
+   General Public License for more details.
185ddb
+
185ddb
+   You should have received copies of the GNU General Public License and
185ddb
+   the GNU Lesser General Public License along with this program.  If
185ddb
+   not, see http://www.gnu.org/licenses/.
185ddb
+>)
185ddb
+
185ddb
+C gcm_set_key() assigns H value in the middle element of the table
185ddb
+define(<H_Idx>, <128>)
185ddb
+
185ddb
+C Register usage:
185ddb
+
185ddb
+define(<SP>, <1>)
185ddb
+define(<TOCP>, <2>)
185ddb
+
185ddb
+define(, <3>)
185ddb
+
185ddb
+define(<ZERO>, <0>)
185ddb
+define(<B1>, <1>)
185ddb
+define(<EMSB>, <16>)
185ddb
+define(<POLY>, <17>)
185ddb
+define(<POLY_L>, <1>)
185ddb
+
185ddb
+define(<H>, <2>)
185ddb
+define(

, <3>)

185ddb
+define(

, <4>)

185ddb
+define(

, <5>)

185ddb
+define(<H1M>, <6>)
185ddb
+define(<H1L>, <7>)
185ddb
+define(<H2M>, <8>)
185ddb
+define(<H2L>, <9>)
185ddb
+define(<Hl>, <10>)
185ddb
+define(<Hm>, <11>)
185ddb
+define(<Hp>, <12>)
185ddb
+define(<Hl2>, <13>)
185ddb
+define(<Hm2>, <14>)
185ddb
+define(<Hp2>, <15>)
185ddb
+define(<R>, <13>)
185ddb
+define(<F>, <14>)
185ddb
+define(<T>, <15>)
185ddb
+define(<R2>, <16>)
185ddb
+define(<F2>, <17>)
185ddb
+define(<T2>, <18>)
185ddb
+
185ddb
+define(<LE_TEMP>, <18>)
185ddb
+define(<LE_MASK>, <19>)
185ddb
+
185ddb
+.file "gcm-hash.asm"
185ddb
+
185ddb
+.text
185ddb
+
185ddb
+    C void gcm_init_key (union gcm_block *table)
185ddb
+
185ddb
+C This function populates the gcm table as the following layout
185ddb
+C *******************************************************************************
185ddb
+C | H1M = (H1 div x⁶⁴)||((H1 mod x⁶⁴) × (x⁶⁴+x⁶³+x⁶²+x⁵⁷)) div x⁶⁴              |
185ddb
+C | H1L = (H1 mod x⁶⁴)||(((H1 mod x⁶⁴) × (x⁶³+x⁶²+x⁵⁷)) mod x⁶⁴) + (H1 div x⁶⁴) |
185ddb
+C |                                                                             |
185ddb
+C | H2M = (H2 div x⁶⁴)||((H2 mod x⁶⁴) × (x⁶⁴+x⁶³+x⁶²+x⁵⁷)) div x⁶⁴              |
185ddb
+C | H2L = (H2 mod x⁶⁴)||(((H2 mod x⁶⁴) × (x⁶³+x⁶²+x⁵⁷)) mod x⁶⁴) + (H2 div x⁶⁴) |
185ddb
+C |                                                                             |
185ddb
+C | H3M = (H3 div x⁶⁴)||((H3 mod x⁶⁴) × (x⁶⁴+x⁶³+x⁶²+x⁵⁷)) div x⁶⁴              |
185ddb
+C | H3L = (H3 mod x⁶⁴)||(((H3 mod x⁶⁴) × (x⁶³+x⁶²+x⁵⁷)) mod x⁶⁴) + (H3 div x⁶⁴) |
185ddb
+C |                                                                             |
185ddb
+C | H4M = (H3 div x⁶⁴)||((H4 mod x⁶⁴) × (x⁶⁴+x⁶³+x⁶²+x⁵⁷)) div x⁶⁴              |
185ddb
+C | H4L = (H3 mod x⁶⁴)||(((H4 mod x⁶⁴) × (x⁶³+x⁶²+x⁵⁷)) mod x⁶⁴) + (H4 div x⁶⁴) |
185ddb
+C *******************************************************************************
185ddb
+
185ddb
+define(<FUNC_ALIGN>, <5>)
185ddb
+PROLOGUE(_nettle_gcm_init_key)
185ddb
+    DATA_LOAD_VEC(POLY,.polynomial,7)           C 0xC2000000000000000000000000000001
185ddb
+IF_LE(<
185ddb
+    li             8,0
185ddb
+    lvsl           LE_MASK,0,8                  C 0x000102030405060708090A0B0C0D0E0F
185ddb
+    vspltisb       LE_TEMP,0x07                  C 0x07070707070707070707070707070707
185ddb
+    vxor           LE_MASK,LE_MASK,LE_TEMP       C 0x07060504030201000F0E0D0C0B0A0908
185ddb
+>)
185ddb
+
185ddb
+    C 'H' is assigned by gcm_set_key() to the middle element of the table
185ddb
+    li             10,H_Idx*16
185ddb
+    lxvd2x         VSR(H),10,TABLE              C load 'H'
185ddb
+    C byte-reverse of each doubleword permuting on little-endian mode
185ddb
+IF_LE(<
185ddb
+    vperm          H,H,H,LE_MASK
185ddb
+>)
185ddb
+
185ddb
+    C --- calculate H = H << 1 mod P(X), P(X) = (x¹²⁸+x¹²⁷+x¹²⁶+x¹²¹+1) ---
185ddb
+
185ddb
+    vupkhsb        EMSB,H                        C extend most significant bit to first byte
185ddb
+    vspltisb       B1,1                          C 0x01010101010101010101010101010101
185ddb
+    vspltb         EMSB,EMSB,0                   C first byte quadword-extend
185ddb
+    vsl            H,H,B1                        C H = H << 1
185ddb
+    vand           EMSB,EMSB,POLY                C EMSB &= 0xC2000000000000000000000000000001
185ddb
+    vxor           ZERO,ZERO,ZERO                C 0x00000000000000000000000000000000
185ddb
+    vxor           H,H,EMSB                      C H ^= EMSB
185ddb
+
185ddb
+    C --- calculate H^2 = H*H ---
185ddb
+
185ddb
+    xxmrghd        VSR(POLY_L),VSR(ZERO),VSR(POLY) C 0x0000000000000000C200000000000000
185ddb
+
185ddb
+    C --- Hp = (H mod x⁶⁴) / x⁶⁴ mod P(X) ---
185ddb
+    C --- Hp = (H mod x⁶⁴) × (x⁶⁴+x⁶³+x⁶²+x⁵⁷) mod P(X), deg(Hp) ≤ 127 ---
185ddb
+    C --- Hp = (H mod x⁶⁴) × (x⁶⁴+x⁶³+x⁶²+x⁵⁷) ---
185ddb
+    vpmsumd        Hp,H,POLY_L                   C Hp = (H mod x⁶⁴) × (x⁶³+x⁶²+x⁵⁷)
185ddb
+    xxswapd        VSR(Hm),VSR(H)
185ddb
+    xxmrgld        VSR(Hl),VSR(H),VSR(ZERO)      C Hl = (H mod x⁶⁴) × x⁶⁴
185ddb
+    vxor           Hm,Hm,Hp                      C Hm = Hm + Hp
185ddb
+    vxor           Hl,Hl,Hp                      C Hl = Hl + Hp
185ddb
+    xxmrgld        VSR(H1L),VSR(H),VSR(Hm)       C H1L = (H mod x⁶⁴)||(Hl mod x⁶⁴)
185ddb
+    xxmrghd        VSR(H1M),VSR(H),VSR(Hl)       C H1M = (H div x⁶⁴)||(Hl div x⁶⁴)
185ddb
+
185ddb
+    vpmsumd        F,H1L,H                       C F = (H1Lh × Hh) + (H1Ll × Hl)
185ddb
+    vpmsumd        R,H1M,H                       C R = (H1Mh × Hh) + (H1Ml × Hl)
185ddb
+
185ddb
+    C --- rduction ---
185ddb
+    vpmsumd        T,F,POLY_L                    C T = (F mod x⁶⁴) × (x⁶³+x⁶²+x⁵⁷)
185ddb
+    xxswapd        VSR(H2),VSR(F)
185ddb
+    vxor           R,R,T                         C R = R + T
185ddb
+    vxor           H2,R,H2
185ddb
+
185ddb
+    xxmrgld        VSR(Hl),VSR(H2),VSR(ZERO)
185ddb
+    xxswapd        VSR(Hm),VSR(H2)
185ddb
+    vpmsumd        Hp,H2,POLY_L
185ddb
+    vxor           Hl,Hl,Hp
185ddb
+    vxor           Hm,Hm,Hp
185ddb
+    xxmrghd        VSR(H2M),VSR(H2),VSR(Hl)
185ddb
+    xxmrgld        VSR(H2L),VSR(H2),VSR(Hm)
185ddb
+
185ddb
+    C store H1M, H1L, H2M, H2L
185ddb
+    li             8,1*16
185ddb
+    li             9,2*16
185ddb
+    li             10,3*16
185ddb
+    stxvd2x        VSR(H1M),0,TABLE
185ddb
+    stxvd2x        VSR(H1L),8,TABLE
185ddb
+    stxvd2x        VSR(H2M),9,TABLE
185ddb
+    stxvd2x        VSR(H2L),10,TABLE
185ddb
+
185ddb
+    C --- calculate H^3 = H^1*H^2, H^4 = H^2*H^2 ---
185ddb
+
185ddb
+    vpmsumd        F,H1L,H2
185ddb
+    vpmsumd        F2,H2L,H2
185ddb
+    vpmsumd        R,H1M,H2
185ddb
+    vpmsumd        R2,H2M,H2
185ddb
+
185ddb
+    vpmsumd        T,F,POLY_L
185ddb
+    vpmsumd        T2,F2,POLY_L
185ddb
+    xxswapd        VSR(H3),VSR(F)
185ddb
+    xxswapd        VSR(H4),VSR(F2)
185ddb
+    vxor           R,R,T
185ddb
+    vxor           R2,R2,T2
185ddb
+    vxor           H3,R,H3
185ddb
+    vxor           H4,R2,H4
185ddb
+
185ddb
+    xxmrgld        VSR(Hl),VSR(H3),VSR(ZERO)
185ddb
+    xxmrgld        VSR(Hl2),VSR(H4),VSR(ZERO)
185ddb
+    xxswapd        VSR(Hm),VSR(H3)
185ddb
+    xxswapd        VSR(Hm2),VSR(H4)
185ddb
+    vpmsumd        Hp,H3,POLY_L
185ddb
+    vpmsumd        Hp2,H4,POLY_L
185ddb
+    vxor           Hl,Hl,Hp
185ddb
+    vxor           Hl2,Hl2,Hp2
185ddb
+    vxor           Hm,Hm,Hp
185ddb
+    vxor           Hm2,Hm2,Hp2
185ddb
+    xxmrghd        VSR(H1M),VSR(H3),VSR(Hl)
185ddb
+    xxmrghd        VSR(H2M),VSR(H4),VSR(Hl2)
185ddb
+    xxmrgld        VSR(H1L),VSR(H3),VSR(Hm)
185ddb
+    xxmrgld        VSR(H2L),VSR(H4),VSR(Hm2)
185ddb
+
185ddb
+    C store H3M, H3L, H4M, H4L
185ddb
+    li             7,4*16
185ddb
+    li             8,5*16
185ddb
+    li             9,6*16
185ddb
+    li             10,7*16
185ddb
+    stxvd2x        VSR(H1M),7,TABLE
185ddb
+    stxvd2x        VSR(H1L),8,TABLE
185ddb
+    stxvd2x        VSR(H2M),9,TABLE
185ddb
+    stxvd2x        VSR(H2L),10,TABLE
185ddb
+
185ddb
+    blr
185ddb
+EPILOGUE(_nettle_gcm_init_key)
185ddb
+
185ddb
+define(, <3>)
185ddb
+define(<X>, <4>)
185ddb
+define(<LENGTH>, <5>)
185ddb
+define(<DATA>, <6>)
185ddb
+
185ddb
+define(<ZERO>, <16>)
185ddb
+define(<POLY>, <17>)
185ddb
+define(<POLY_L>, <0>)
185ddb
+
185ddb
+define(<D>, <1>)
185ddb
+define(<C0>, <2>)
185ddb
+define(<C1>, <3>)
185ddb
+define(<C2>, <4>)
185ddb
+define(<C3>, <5>)
185ddb
+define(<H1M>, <6>)
185ddb
+define(<H1L>, <7>)
185ddb
+define(<H2M>, <8>)
185ddb
+define(<H2L>, <9>)
185ddb
+define(<H3M>, <10>)
185ddb
+define(<H3L>, <11>)
185ddb
+define(<H4M>, <12>)
185ddb
+define(<H4L>, <13>)
185ddb
+define(<R>, <14>)
185ddb
+define(<F>, <15>)
185ddb
+define(<R2>, <16>)
185ddb
+define(<F2>, <17>)
185ddb
+define(<T>, <18>)
185ddb
+define(<R3>, <20>)
185ddb
+define(<F3>, <21>)
185ddb
+define(<R4>, <22>)
185ddb
+define(<F4>, <23>)
185ddb
+
185ddb
+define(<LE_TEMP>, <18>)
185ddb
+define(<LE_MASK>, <19>)
185ddb
+
185ddb
+    C void gcm_hash (const struct gcm_key *key, union gcm_block *x,
185ddb
+    C                size_t length, const uint8_t *data)
185ddb
+
185ddb
+define(<FUNC_ALIGN>, <5>)
185ddb
+PROLOGUE(_nettle_gcm_hash)
185ddb
+    vxor           ZERO,ZERO,ZERO
185ddb
+    DATA_LOAD_VEC(POLY,.polynomial,7)
185ddb
+IF_LE(<
185ddb
+    li             8,0
185ddb
+    lvsl           LE_MASK,0,8
185ddb
+    vspltisb       LE_TEMP,0x07
185ddb
+    vxor           LE_MASK,LE_MASK,LE_TEMP
185ddb
+>)
185ddb
+    xxmrghd        VSR(POLY_L),VSR(ZERO),VSR(POLY)
185ddb
+
185ddb
+    lxvd2x         VSR(D),0,X                    C load 'X' pointer
185ddb
+    C byte-reverse of each doubleword permuting on little-endian mode
185ddb
+IF_LE(<
185ddb
+    vperm          D,D,D,LE_MASK
185ddb
+>)
185ddb
+
185ddb
+    C --- process 4 blocks '128-bit each' per one loop ---
185ddb
+
185ddb
+    srdi.          7,LENGTH,6                   C 4-blocks loop count 'LENGTH / (4 * 16)'
185ddb
+    beq            L2x
185ddb
+
185ddb
+    mtctr          7                            C assign counter register to loop count
185ddb
+
185ddb
+    C store non-volatile vector registers
185ddb
+    addi           8,SP,-64
185ddb
+    stvx           20,0,8
185ddb
+    addi           8,8,16
185ddb
+    stvx           21,0,8
185ddb
+    addi           8,8,16
185ddb
+    stvx           22,0,8
185ddb
+    addi           8,8,16
185ddb
+    stvx           23,0,8
185ddb
+
185ddb
+    C load table elements
185ddb
+    li             8,1*16
185ddb
+    li             9,2*16
185ddb
+    li             10,3*16
185ddb
+    lxvd2x         VSR(H1M),0,TABLE
185ddb
+    lxvd2x         VSR(H1L),8,TABLE
185ddb
+    lxvd2x         VSR(H2M),9,TABLE
185ddb
+    lxvd2x         VSR(H2L),10,TABLE
185ddb
+    li             7,4*16
185ddb
+    li             8,5*16
185ddb
+    li             9,6*16
185ddb
+    li             10,7*16
185ddb
+    lxvd2x         VSR(H3M),7,TABLE
185ddb
+    lxvd2x         VSR(H3L),8,TABLE
185ddb
+    lxvd2x         VSR(H4M),9,TABLE
185ddb
+    lxvd2x         VSR(H4L),10,TABLE
185ddb
+
185ddb
+    li             8,0x10
185ddb
+    li             9,0x20
185ddb
+    li             10,0x30
185ddb
+.align 5
185ddb
+L4x_loop:
185ddb
+    C input loading
185ddb
+    lxvd2x         VSR(C0),0,DATA                C load C0
185ddb
+    lxvd2x         VSR(C1),8,DATA               C load C1
185ddb
+    lxvd2x         VSR(C2),9,DATA               C load C2
185ddb
+    lxvd2x         VSR(C3),10,DATA              C load C3
185ddb
+
185ddb
+IF_LE(<
185ddb
+    vperm          C0,C0,C0,LE_MASK
185ddb
+    vperm          C1,C1,C1,LE_MASK
185ddb
+    vperm          C2,C2,C2,LE_MASK
185ddb
+    vperm          C3,C3,C3,LE_MASK
185ddb
+>)
185ddb
+
185ddb
+    C previous digest combining
185ddb
+    vxor           C0,C0,D
185ddb
+
185ddb
+    C polynomial multiplication
185ddb
+    vpmsumd        F2,H3L,C1
185ddb
+    vpmsumd        R2,H3M,C1
185ddb
+    vpmsumd        F3,H2L,C2
185ddb
+    vpmsumd        R3,H2M,C2
185ddb
+    vpmsumd        F4,H1L,C3
185ddb
+    vpmsumd        R4,H1M,C3
185ddb
+    vpmsumd        F,H4L,C0
185ddb
+    vpmsumd        R,H4M,C0
185ddb
+
185ddb
+    C deferred recombination of partial products
185ddb
+    vxor           F3,F3,F4
185ddb
+    vxor           R3,R3,R4
185ddb
+    vxor           F,F,F2
185ddb
+    vxor           R,R,R2
185ddb
+    vxor           F,F,F3
185ddb
+    vxor           R,R,R3
185ddb
+
185ddb
+    C reduction
185ddb
+    vpmsumd        T,F,POLY_L
185ddb
+    xxswapd        VSR(D),VSR(F)
185ddb
+    vxor           R,R,T
185ddb
+    vxor           D,R,D
185ddb
+
185ddb
+    addi           DATA,DATA,0x40
185ddb
+    bdnz           L4x_loop
185ddb
+
185ddb
+    C restore non-volatile vector registers
185ddb
+    addi           8,SP,-64
185ddb
+    lvx            20,0,8
185ddb
+    addi           8,8,16
185ddb
+    lvx            21,0,8
185ddb
+    addi           8,8,16
185ddb
+    lvx            22,0,8
185ddb
+    addi           8,8,16
185ddb
+    lvx            23,0,8
185ddb
+
185ddb
+    clrldi         LENGTH,LENGTH,58              C 'set the high-order 58 bits to zeros'
185ddb
+L2x:
185ddb
+    C --- process 2 blocks ---
185ddb
+
185ddb
+    srdi.          7,LENGTH,5                   C 'LENGTH / (2 * 16)'
185ddb
+    beq            L1x
185ddb
+
185ddb
+    C load table elements
185ddb
+    li             8,1*16
185ddb
+    li             9,2*16
185ddb
+    li             10,3*16
185ddb
+    lxvd2x         VSR(H1M),0,TABLE
185ddb
+    lxvd2x         VSR(H1L),8,TABLE
185ddb
+    lxvd2x         VSR(H2M),9,TABLE
185ddb
+    lxvd2x         VSR(H2L),10,TABLE
185ddb
+
185ddb
+    C input loading
185ddb
+    li             10,0x10
185ddb
+    lxvd2x         VSR(C0),0,DATA                C load C0
185ddb
+    lxvd2x         VSR(C1),10,DATA              C load C1
185ddb
+
185ddb
+IF_LE(<
185ddb
+    vperm          C0,C0,C0,LE_MASK
185ddb
+    vperm          C1,C1,C1,LE_MASK
185ddb
+>)
185ddb
+
185ddb
+    C previous digest combining
185ddb
+    vxor           C0,C0,D
185ddb
+
185ddb
+    C polynomial multiplication
185ddb
+    vpmsumd        F2,H1L,C1
185ddb
+    vpmsumd        R2,H1M,C1
185ddb
+    vpmsumd        F,H2L,C0
185ddb
+    vpmsumd        R,H2M,C0
185ddb
+
185ddb
+    C deferred recombination of partial products
185ddb
+    vxor           F,F,F2
185ddb
+    vxor           R,R,R2
185ddb
+
185ddb
+    C reduction
185ddb
+    vpmsumd        T,F,POLY_L
185ddb
+    xxswapd        VSR(D),VSR(F)
185ddb
+    vxor           R,R,T
185ddb
+    vxor           D,R,D
185ddb
+
185ddb
+    addi           DATA,DATA,0x20
185ddb
+    clrldi         LENGTH,LENGTH,59              C 'set the high-order 59 bits to zeros'
185ddb
+L1x:
185ddb
+    C --- process 1 block ---
185ddb
+
185ddb
+    srdi.          7,LENGTH,4                   C 'LENGTH / (1 * 16)'
185ddb
+    beq            Lmod
185ddb
+
185ddb
+    C load table elements
185ddb
+    li             8,1*16
185ddb
+    lxvd2x         VSR(H1M),0,TABLE
185ddb
+    lxvd2x         VSR(H1L),8,TABLE
185ddb
+
185ddb
+    C input loading
185ddb
+    lxvd2x         VSR(C0),0,DATA                C load C0
185ddb
+
185ddb
+IF_LE(<
185ddb
+    vperm          C0,C0,C0,LE_MASK
185ddb
+>)
185ddb
+
185ddb
+    C previous digest combining
185ddb
+    vxor           C0,C0,D
185ddb
+
185ddb
+    C polynomial multiplication
185ddb
+    vpmsumd        F,H1L,C0
185ddb
+    vpmsumd        R,H1M,C0
185ddb
+
185ddb
+    C reduction
185ddb
+    vpmsumd        T,F,POLY_L
185ddb
+    xxswapd        VSR(D),VSR(F)
185ddb
+    vxor           R,R,T
185ddb
+    vxor           D,R,D
185ddb
+
185ddb
+    addi           DATA,DATA,0x10
185ddb
+    clrldi         LENGTH,LENGTH,60              C 'set the high-order 60 bits to zeros'
185ddb
+Lmod:
185ddb
+    C --- process the modulo bytes, padding the low-order bytes with zeros ---
185ddb
+
185ddb
+    cmpldi         LENGTH,0
185ddb
+    beq            Ldone
185ddb
+
185ddb
+    C load table elements
185ddb
+    li             8,1*16
185ddb
+    lxvd2x         VSR(H1M),0,TABLE
185ddb
+    lxvd2x         VSR(H1L),8,TABLE
185ddb
+
185ddb
+    C push every modulo byte to the stack and load them with padding into vector register
185ddb
+    vxor           ZERO,ZERO,ZERO
185ddb
+    addi           8,SP,-16
185ddb
+    stvx           ZERO,0,8
185ddb
+Lstb_loop:
185ddb
+    subic.         LENGTH,LENGTH,1
185ddb
+    lbzx           7,LENGTH,DATA
185ddb
+    stbx           7,LENGTH,8
185ddb
+    bne            Lstb_loop
185ddb
+    lxvd2x         VSR(C0),0,8
185ddb
+
185ddb
+IF_LE(<
185ddb
+    vperm          C0,C0,C0,LE_MASK
185ddb
+>)
185ddb
+
185ddb
+    C previous digest combining
185ddb
+    vxor           C0,C0,D
185ddb
+
185ddb
+    C polynomial multiplication
185ddb
+    vpmsumd        F,H1L,C0
185ddb
+    vpmsumd        R,H1M,C0
185ddb
+
185ddb
+    C reduction
185ddb
+    vpmsumd        T,F,POLY_L
185ddb
+    xxswapd        VSR(D),VSR(F)
185ddb
+    vxor           R,R,T
185ddb
+    vxor           D,R,D
185ddb
+
185ddb
+Ldone:
185ddb
+    C byte-reverse of each doubleword permuting on little-endian mode
185ddb
+IF_LE(<
185ddb
+    vperm          D,D,D,LE_MASK
185ddb
+>)
185ddb
+    stxvd2x        VSR(D),0,X                    C store digest 'D'
185ddb
+
185ddb
+    blr
185ddb
+EPILOGUE(_nettle_gcm_hash)
185ddb
+
185ddb
+.data
185ddb
+    C 0xC2000000000000000000000000000001
185ddb
+.polynomial:
185ddb
+.align 4
185ddb
+IF_BE(<
185ddb
+.byte 0xC2
185ddb
+.rept 14
185ddb
+.byte 0x00
185ddb
+.endr
185ddb
+.byte 0x01
185ddb
+>,<
185ddb
+.byte 0x01
185ddb
+.rept 14
185ddb
+.byte 0x00
185ddb
+.endr
185ddb
+.byte 0xC2
185ddb
+>)