From b686476f1c413ac01d107240ce4726ec764a521c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Wed, 21 Jan 2015 21:23:28 +0100
Subject: [PATCH] Fat functions for arm neon.

---
 ChangeLog                           |  8 ++++
 arm/fat/salsa20-core-internal-2.asm | 37 +++++++++++++++++
 arm/fat/sha3-permute-2.asm          | 37 +++++++++++++++++
 arm/fat/sha512-compress-2.asm       | 37 +++++++++++++++++
 arm/fat/umac-nh-2.asm               | 37 +++++++++++++++++
 arm/fat/umac-nh-n-2.asm             | 37 +++++++++++++++++
 fat-arm.c                           | 61 +++++++++++++++++++++++++++++
 7 files changed, 254 insertions(+)
 create mode 100644 arm/fat/salsa20-core-internal-2.asm
 create mode 100644 arm/fat/sha3-permute-2.asm
 create mode 100644 arm/fat/sha512-compress-2.asm
 create mode 100644 arm/fat/umac-nh-2.asm
 create mode 100644 arm/fat/umac-nh-n-2.asm

diff --git a/ChangeLog b/ChangeLog
index fa25c0ed..b49e1c42 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2015-01-21  Niels Möller  <nisse@lysator.liu.se>
 
+	* fat-arm.c (fat_init): Setup for use of neon assembly functions.
+
+	* arm/fat/salsa20-core-internal-2.asm: New file.
+	* arm/fat/sha3-permute-2.asm: New file.
+	* arm/fat/sha512-compress-2.asm: New file.
+	* arm/fat/umac-nh-2.asm: New file.
+	* arm/fat/umac-nh-n-2.asm: New file.
+
 	* salsa20-core-internal.c: Prepare for fat build with C and
 	assembly implementations.
 	* sha512-compress.c: Likewise.
diff --git a/arm/fat/salsa20-core-internal-2.asm b/arm/fat/salsa20-core-internal-2.asm
new file mode 100644
index 00000000..64d90302
--- /dev/null
+++ b/arm/fat/salsa20-core-internal-2.asm
@@ -0,0 +1,37 @@
+C arm/fat/salsa20-core-internal-2.asm
+
+
+ifelse(<
+   Copyright (C) 2015 Niels Möller
+
+   This file is part of GNU Nettle.
+
+   GNU Nettle is free software: you can redistribute it and/or
+   modify it under the terms of either:
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at your
+       option) any later version.
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at your
+       option) any later version.
+
+   or both in parallel, as here.
+
+   GNU Nettle is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see http://www.gnu.org/licenses/.
+>)
+
+dnl PROLOGUE(_nettle_salsa20_core) picked up by configure
+
+define(<fat_transform>, <$1_neon>)
+include_src(<arm/neon/salsa20-core-internal.asm>)
diff --git a/arm/fat/sha3-permute-2.asm b/arm/fat/sha3-permute-2.asm
new file mode 100644
index 00000000..b423a762
--- /dev/null
+++ b/arm/fat/sha3-permute-2.asm
@@ -0,0 +1,37 @@
+C arm/fat/sha3-permute-2.asm
+
+
+ifelse(<
+   Copyright (C) 2015 Niels Möller
+
+   This file is part of GNU Nettle.
+
+   GNU Nettle is free software: you can redistribute it and/or
+   modify it under the terms of either:
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at your
+       option) any later version.
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at your
+       option) any later version.
+
+   or both in parallel, as here.
+
+   GNU Nettle is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see http://www.gnu.org/licenses/.
+>)
+
+dnl PROLOGUE(_nettle_sha3_permute) picked up by configure
+
+define(<fat_transform>, <_$1_neon>)
+include_src(<arm/neon/sha3-permute.asm>)
diff --git a/arm/fat/sha512-compress-2.asm b/arm/fat/sha512-compress-2.asm
new file mode 100644
index 00000000..428604e0
--- /dev/null
+++ b/arm/fat/sha512-compress-2.asm
@@ -0,0 +1,37 @@
+C arm/fat/sha3-compress-2.asm
+
+
+ifelse(<
+   Copyright (C) 2015 Niels Möller
+
+   This file is part of GNU Nettle.
+
+   GNU Nettle is free software: you can redistribute it and/or
+   modify it under the terms of either:
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at your
+       option) any later version.
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at your
+       option) any later version.
+
+   or both in parallel, as here.
+
+   GNU Nettle is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see http://www.gnu.org/licenses/.
+>)
+
+dnl PROLOGUE(_nettle_sha512_compress) picked up by configure
+
+define(<fat_transform>, <$1_neon>)
+include_src(<arm/neon/sha512-compress.asm>)
diff --git a/arm/fat/umac-nh-2.asm b/arm/fat/umac-nh-2.asm
new file mode 100644
index 00000000..fc97cc6b
--- /dev/null
+++ b/arm/fat/umac-nh-2.asm
@@ -0,0 +1,37 @@
+C arm/fat/umac-nh-2.asm
+
+
+ifelse(<
+   Copyright (C) 2015 Niels Möller
+
+   This file is part of GNU Nettle.
+
+   GNU Nettle is free software: you can redistribute it and/or
+   modify it under the terms of either:
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at your
+       option) any later version.
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at your
+       option) any later version.
+
+   or both in parallel, as here.
+
+   GNU Nettle is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see http://www.gnu.org/licenses/.
+>)
+
+dnl PROLOGUE(_nettle_umac_nh) picked up by configure
+
+define(<fat_transform>, <$1_neon>)
+include_src(<arm/neon/umac-nh.asm>)
diff --git a/arm/fat/umac-nh-n-2.asm b/arm/fat/umac-nh-n-2.asm
new file mode 100644
index 00000000..32b7a830
--- /dev/null
+++ b/arm/fat/umac-nh-n-2.asm
@@ -0,0 +1,37 @@
+C arm/fat/umac-nh-n-2.asm
+
+
+ifelse(<
+   Copyright (C) 2015 Niels Möller
+
+   This file is part of GNU Nettle.
+
+   GNU Nettle is free software: you can redistribute it and/or
+   modify it under the terms of either:
+
+     * the GNU Lesser General Public License as published by the Free
+       Software Foundation; either version 3 of the License, or (at your
+       option) any later version.
+
+   or
+
+     * the GNU General Public License as published by the Free
+       Software Foundation; either version 2 of the License, or (at your
+       option) any later version.
+
+   or both in parallel, as here.
+
+   GNU Nettle is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see http://www.gnu.org/licenses/.
+>)
+
+dnl PROLOGUE(_nettle_umac_nh_n) picked up by configure
+
+define(<fat_transform>, <$1_neon>)
+include_src(<arm/neon/umac-nh-n.asm>)
diff --git a/fat-arm.c b/fat-arm.c
index e577494a..e351fdcb 100644
--- a/fat-arm.c
+++ b/fat-arm.c
@@ -141,6 +141,26 @@ DECLARE_FAT_FUNC(_nettle_aes_decrypt, aes_crypt_internal_func)
 DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, arm)
 DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, armv6)
 
+DECLARE_FAT_FUNC(_nettle_salsa20_core, salsa20_core_func)
+DECLARE_FAT_FUNC_VAR(salsa20_core, salsa20_core_func, c)
+DECLARE_FAT_FUNC_VAR(salsa20_core, salsa20_core_func, neon)
+
+DECLARE_FAT_FUNC(_nettle_sha512_compress, sha512_compress_func)
+DECLARE_FAT_FUNC_VAR(sha512_compress, sha512_compress_func, c)
+DECLARE_FAT_FUNC_VAR(sha512_compress, sha512_compress_func, neon)
+
+DECLARE_FAT_FUNC(nettle_sha3_permute, sha3_permute_func)
+DECLARE_FAT_FUNC_VAR(sha3_permute, sha3_permute_func, c)
+DECLARE_FAT_FUNC_VAR(sha3_permute, sha3_permute_func, neon)
+
+DECLARE_FAT_FUNC(_nettle_umac_nh, umac_nh_func)
+DECLARE_FAT_FUNC_VAR(umac_nh, umac_nh_func, c);
+DECLARE_FAT_FUNC_VAR(umac_nh, umac_nh_func, neon);
+
+DECLARE_FAT_FUNC(_nettle_umac_nh_n, umac_nh_n_func)
+DECLARE_FAT_FUNC_VAR(umac_nh_n, umac_nh_n_func, c);
+DECLARE_FAT_FUNC_VAR(umac_nh_n, umac_nh_n_func, neon);
+
 static void CONSTRUCTOR
 fat_init (void)
 {
@@ -173,6 +193,26 @@ fat_init (void)
       _nettle_aes_encrypt_vec = _nettle_aes_encrypt_arm;
       _nettle_aes_decrypt_vec = _nettle_aes_decrypt_arm;
     }
+  if (features.have_neon)
+    {
+      if (verbose)
+	fprintf (stderr, "libnettle: enabling neon code.\n");
+      _nettle_salsa20_core_vec = _nettle_salsa20_core_neon;
+      _nettle_sha512_compress_vec = _nettle_sha512_compress_neon;
+      nettle_sha3_permute_vec = _nettle_sha3_permute_neon;
+      _nettle_umac_nh_vec = _nettle_umac_nh_neon;
+      _nettle_umac_nh_n_vec = _nettle_umac_nh_n_neon;
+    }
+  else
+    {
+      if (verbose)
+	fprintf (stderr, "libnettle: not enabling neon code.\n");
+      _nettle_salsa20_core_vec = _nettle_salsa20_core_c;
+      _nettle_sha512_compress_vec = _nettle_sha512_compress_c;
+      nettle_sha3_permute_vec = _nettle_sha3_permute_c;
+      _nettle_umac_nh_vec = _nettle_umac_nh_c;
+      _nettle_umac_nh_n_vec = _nettle_umac_nh_n_c;
+    }
   /* FIXME: Needs memory barrier, to enforce store ordering. */
   initialized = 1;
 }
@@ -190,3 +230,24 @@ DEFINE_FAT_FUNC(_nettle_aes_decrypt, void,
 		 size_t length, uint8_t *dst,
 		 const uint8_t *src),
 		(rounds, keys, T, length, dst, src))
+
+DEFINE_FAT_FUNC(_nettle_salsa20_core, void,
+		(uint32_t *dst, const uint32_t *src, unsigned rounds),
+		(dst, src, rounds))
+
+DEFINE_FAT_FUNC(_nettle_sha512_compress, void,
+		(uint64_t *state, const uint8_t *input, const uint64_t *k),
+		(state, input, k))
+
+DEFINE_FAT_FUNC(nettle_sha3_permute, void,
+		(struct sha3_state *state), (state))
+
+DEFINE_FAT_FUNC(_nettle_umac_nh, uint64_t,
+		(const uint32_t *key, unsigned length, const uint8_t *msg),
+		(key, length, msg))
+
+DEFINE_FAT_FUNC(_nettle_umac_nh_n, void,
+		(uint64_t *out, unsigned n, const uint32_t *key,
+		 unsigned length, const uint8_t *msg),
+		(out, n, key, length, msg))
+
-- 
GitLab