From 52975b38ce626f4e6dda4b1155986c0d8a570a45 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 6 Jan 2026 23:18:21 +0100 Subject: [PATCH] Add ARM acceleration for SHA3 --- .../msvc/vs2010/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2012/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2013/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2015/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2017/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2019/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2022/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- .../msvc/vs2026/libsodium/libsodium.vcxproj | 7 +- .../libsodium/libsodium.vcxproj.filters | 33 ++- ci/appveyor/libsodium.vcxproj | 7 +- ci/appveyor/libsodium.vcxproj.filters | 21 +- src/libsodium/Makefile.am | 2 + .../keccak1600/armsha3/keccak1600_armsha3.c | 276 ++++++++++++++++++ .../keccak1600/armsha3/keccak1600_armsha3.h | 20 ++ .../crypto_core/keccak1600/keccak1600.c | 25 +- src/libsodium/include/sodium/private/quirks.h | 5 + test/symbols/all-symbols.txt | 5 + 24 files changed, 640 insertions(+), 41 deletions(-) create mode 100644 src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.c create mode 100644 src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.h diff --git a/builds/msvc/vs2010/libsodium/libsodium.vcxproj b/builds/msvc/vs2010/libsodium/libsodium.vcxproj index 1a9fdea3..62da89b0 100644 --- a/builds/msvc/vs2010/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2010/libsodium/libsodium.vcxproj @@ -189,9 +189,12 @@ + + + @@ -219,7 +222,6 @@ - @@ -231,6 +233,7 @@ + @@ -355,7 +358,9 @@ + + diff --git a/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2012/libsodium/libsodium.vcxproj b/builds/msvc/vs2012/libsodium/libsodium.vcxproj index 96354e31..9d0f3559 100644 --- a/builds/msvc/vs2012/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2012/libsodium/libsodium.vcxproj @@ -189,9 +189,12 @@ + + + @@ -219,7 +222,6 @@ - @@ -231,6 +233,7 @@ + @@ -355,7 +358,9 @@ + + diff --git a/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2013/libsodium/libsodium.vcxproj b/builds/msvc/vs2013/libsodium/libsodium.vcxproj index be6f4d8e..4b5690df 100644 --- a/builds/msvc/vs2013/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2013/libsodium/libsodium.vcxproj @@ -189,9 +189,12 @@ + + + @@ -219,7 +222,6 @@ - @@ -231,6 +233,7 @@ + @@ -355,7 +358,9 @@ + + diff --git a/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2015/libsodium/libsodium.vcxproj b/builds/msvc/vs2015/libsodium/libsodium.vcxproj index b92308c3..308955c9 100644 --- a/builds/msvc/vs2015/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2015/libsodium/libsodium.vcxproj @@ -189,9 +189,12 @@ + + + @@ -219,7 +222,6 @@ - @@ -231,6 +233,7 @@ + @@ -355,7 +358,9 @@ + + diff --git a/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2017/libsodium/libsodium.vcxproj b/builds/msvc/vs2017/libsodium/libsodium.vcxproj index 2c3b7506..4ba30ebb 100644 --- a/builds/msvc/vs2017/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2017/libsodium/libsodium.vcxproj @@ -189,9 +189,12 @@ + + + @@ -219,7 +222,6 @@ - @@ -231,6 +233,7 @@ + @@ -355,7 +358,9 @@ + + diff --git a/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2019/libsodium/libsodium.vcxproj b/builds/msvc/vs2019/libsodium/libsodium.vcxproj index 7fff9a3e..6f5734a7 100644 --- a/builds/msvc/vs2019/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2019/libsodium/libsodium.vcxproj @@ -213,9 +213,12 @@ + + + @@ -243,7 +246,6 @@ - @@ -255,6 +257,7 @@ + @@ -379,7 +382,9 @@ + + diff --git a/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2019/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2022/libsodium/libsodium.vcxproj b/builds/msvc/vs2022/libsodium/libsodium.vcxproj index 84158421..580ecfef 100644 --- a/builds/msvc/vs2022/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2022/libsodium/libsodium.vcxproj @@ -213,9 +213,12 @@ + + + @@ -243,7 +246,6 @@ - @@ -255,6 +257,7 @@ + @@ -379,7 +382,9 @@ + + diff --git a/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2022/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/builds/msvc/vs2026/libsodium/libsodium.vcxproj b/builds/msvc/vs2026/libsodium/libsodium.vcxproj index ff60bfd5..a63bcff3 100644 --- a/builds/msvc/vs2026/libsodium/libsodium.vcxproj +++ b/builds/msvc/vs2026/libsodium/libsodium.vcxproj @@ -213,9 +213,12 @@ + + + @@ -243,7 +246,6 @@ - @@ -255,6 +257,7 @@ + @@ -379,7 +382,9 @@ + + diff --git a/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters b/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters index 80b49816..5582a2e7 100644 --- a/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters +++ b/builds/msvc/vs2026/libsodium/libsodium.vcxproj.filters @@ -387,6 +387,12 @@ crypto_aead\chacha20poly1305 + + crypto_kem\mlkem768 + + + crypto_kem\mlkem768\ref + crypto_secretstream\xchacha20poly1305 @@ -396,6 +402,9 @@ crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\salsa\ref @@ -473,9 +482,6 @@ include\sodium - - include\sodium - include\sodium @@ -509,6 +515,9 @@ include\sodium + + include\sodium + include\sodium @@ -881,9 +890,15 @@ crypto_aead\aegis256 + + crypto_kem\mlkem768\ref + crypto_core\keccak1600\ref + + crypto_core\keccak1600\armsha3 + crypto_core\ed25519 @@ -985,6 +1000,9 @@ {85c1722f-2d65-30a8-aefb-ebf1cbfe185e} + + {855cc78f-1fe0-3ad2-a2ac-5e2b0c01c235} + {9462f285-fee0-3779-983b-2811dc621f7d} @@ -1033,6 +1051,15 @@ {baf7d894-9298-391d-8dc2-e4c3010f5ddc} + + {b9009d4a-dd97-341d-aafb-eeeb77191a49} + + + {ab0c8a55-3e1f-307e-82be-8291a3b43735} + + + {70a10765-f8fb-3898-8514-966e48719bc4} + {898b6bd5-1360-3a34-adcd-0fade7561685} diff --git a/ci/appveyor/libsodium.vcxproj b/ci/appveyor/libsodium.vcxproj index 840e3248..73786c50 100644 --- a/ci/appveyor/libsodium.vcxproj +++ b/ci/appveyor/libsodium.vcxproj @@ -437,9 +437,12 @@ + + + @@ -467,7 +470,6 @@ - @@ -479,6 +481,7 @@ + @@ -603,7 +606,9 @@ + + diff --git a/ci/appveyor/libsodium.vcxproj.filters b/ci/appveyor/libsodium.vcxproj.filters index 8d747b1e..e35abe42 100644 --- a/ci/appveyor/libsodium.vcxproj.filters +++ b/ci/appveyor/libsodium.vcxproj.filters @@ -378,6 +378,12 @@ Source Files + + Source Files + + + Source Files + Source Files @@ -387,6 +393,9 @@ Source Files + + Source Files + Source Files @@ -464,9 +473,6 @@ Header Files - - Header Files - Header Files @@ -500,6 +506,9 @@ Header Files + + Header Files + Header Files @@ -872,9 +881,15 @@ Header Files + + Header Files + Header Files + + Header Files + Header Files diff --git a/src/libsodium/Makefile.am b/src/libsodium/Makefile.am index 9680cf6d..c75aeb3a 100644 --- a/src/libsodium/Makefile.am +++ b/src/libsodium/Makefile.am @@ -32,6 +32,8 @@ libsodium_la_SOURCES = \ crypto_core/keccak1600/keccak1600.c \ crypto_core/keccak1600/ref/keccak1600_ref.c \ crypto_core/keccak1600/ref/keccak1600_ref.h \ + crypto_core/keccak1600/armsha3/keccak1600_armsha3.c \ + crypto_core/keccak1600/armsha3/keccak1600_armsha3.h \ crypto_core/salsa/ref/core_salsa_ref.c \ crypto_core/softaes/softaes.c \ crypto_generichash/crypto_generichash.c \ diff --git a/src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.c b/src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.c new file mode 100644 index 00000000..12c105cd --- /dev/null +++ b/src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.c @@ -0,0 +1,276 @@ +#include +#include + +#include "keccak1600_armsha3.h" +#include "private/common.h" + +#if defined(__ARM_FEATURE_SHA3) + +#include + +#ifdef __clang__ +# pragma clang attribute push(__attribute__((target("neon,sha3"))), apply_to = function) +#elif defined(__GNUC__) +# pragma GCC target("+simd+sha3") +#endif + +#define KECCAK1600_STATEBYTES 200 + +static const uint64_t RC[24] = { + 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL, 0x8000000080008000ULL, + 0x000000000000808bULL, 0x0000000080000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL, + 0x000000000000008aULL, 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL, + 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL, + 0x8000000000008002ULL, 0x8000000000000080ULL, 0x000000000000800aULL, 0x800000008000000aULL, + 0x8000000080008081ULL, 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL +}; + +#define ROUND(A, E, rc_idx) \ + do { \ + uint64x2_t rc = vdupq_n_u64(RC[rc_idx]); \ + Ca = veor3q_u64(A##ba, veor3q_u64(A##ga, A##ka, A##ma), A##sa); \ + Ce = veor3q_u64(A##be, veor3q_u64(A##ge, A##ke, A##me), A##se); \ + Ci = veor3q_u64(A##bi, veor3q_u64(A##gi, A##ki, A##mi), A##si); \ + Co = veor3q_u64(A##bo, veor3q_u64(A##go, A##ko, A##mo), A##so); \ + Cu = veor3q_u64(A##bu, veor3q_u64(A##gu, A##ku, A##mu), A##su); \ + Da = vrax1q_u64(Cu, Ce); \ + De = vrax1q_u64(Ca, Ci); \ + Di = vrax1q_u64(Ce, Co); \ + Do = vrax1q_u64(Ci, Cu); \ + Du = vrax1q_u64(Co, Ca); \ + Ba = veorq_u64(A##ba, Da); \ + Be = vxarq_u64(A##ge, De, 64 - 44); \ + Bi = vxarq_u64(A##ki, Di, 64 - 43); \ + Bo = vxarq_u64(A##mo, Do, 64 - 21); \ + Bu = vxarq_u64(A##su, Du, 64 - 14); \ + E##ba = veorq_u64(vbcaxq_u64(Ba, Bi, Be), rc); \ + E##be = vbcaxq_u64(Be, Bo, Bi); \ + E##bi = vbcaxq_u64(Bi, Bu, Bo); \ + E##bo = vbcaxq_u64(Bo, Ba, Bu); \ + E##bu = vbcaxq_u64(Bu, Be, Ba); \ + Ba = vxarq_u64(A##bo, Do, 64 - 28); \ + Be = vxarq_u64(A##gu, Du, 64 - 20); \ + Bi = vxarq_u64(A##ka, Da, 64 - 3); \ + Bo = vxarq_u64(A##me, De, 64 - 45); \ + Bu = vxarq_u64(A##si, Di, 64 - 61); \ + E##ga = vbcaxq_u64(Ba, Bi, Be); \ + E##ge = vbcaxq_u64(Be, Bo, Bi); \ + E##gi = vbcaxq_u64(Bi, Bu, Bo); \ + E##go = vbcaxq_u64(Bo, Ba, Bu); \ + E##gu = vbcaxq_u64(Bu, Be, Ba); \ + Ba = vxarq_u64(A##be, De, 64 - 1); \ + Be = vxarq_u64(A##gi, Di, 64 - 6); \ + Bi = vxarq_u64(A##ko, Do, 64 - 25); \ + Bo = vxarq_u64(A##mu, Du, 64 - 8); \ + Bu = vxarq_u64(A##sa, Da, 64 - 18); \ + E##ka = vbcaxq_u64(Ba, Bi, Be); \ + E##ke = vbcaxq_u64(Be, Bo, Bi); \ + E##ki = vbcaxq_u64(Bi, Bu, Bo); \ + E##ko = vbcaxq_u64(Bo, Ba, Bu); \ + E##ku = vbcaxq_u64(Bu, Be, Ba); \ + Ba = vxarq_u64(A##bu, Du, 64 - 27); \ + Be = vxarq_u64(A##ga, Da, 64 - 36); \ + Bi = vxarq_u64(A##ke, De, 64 - 10); \ + Bo = vxarq_u64(A##mi, Di, 64 - 15); \ + Bu = vxarq_u64(A##so, Do, 64 - 56); \ + E##ma = vbcaxq_u64(Ba, Bi, Be); \ + E##me = vbcaxq_u64(Be, Bo, Bi); \ + E##mi = vbcaxq_u64(Bi, Bu, Bo); \ + E##mo = vbcaxq_u64(Bo, Ba, Bu); \ + E##mu = vbcaxq_u64(Bu, Be, Ba); \ + Ba = vxarq_u64(A##bi, Di, 64 - 62); \ + Be = vxarq_u64(A##go, Do, 64 - 55); \ + Bi = vxarq_u64(A##ku, Du, 64 - 39); \ + Bo = vxarq_u64(A##ma, Da, 64 - 41); \ + Bu = vxarq_u64(A##se, De, 64 - 2); \ + E##sa = vbcaxq_u64(Ba, Bi, Be); \ + E##se = vbcaxq_u64(Be, Bo, Bi); \ + E##si = vbcaxq_u64(Bi, Bu, Bo); \ + E##so = vbcaxq_u64(Bo, Ba, Bu); \ + E##su = vbcaxq_u64(Bu, Be, Ba); \ + } while (0) + +static void +keccakf_24_rounds(uint64_t st[25]) +{ + uint64x2_t Aba, Abe, Abi, Abo, Abu; + uint64x2_t Aga, Age, Agi, Ago, Agu; + uint64x2_t Aka, Ake, Aki, Ako, Aku; + uint64x2_t Ama, Ame, Ami, Amo, Amu; + uint64x2_t Asa, Ase, Asi, Aso, Asu; + uint64x2_t Eba, Ebe, Ebi, Ebo, Ebu; + uint64x2_t Ega, Ege, Egi, Ego, Egu; + uint64x2_t Eka, Eke, Eki, Eko, Eku; + uint64x2_t Ema, Eme, Emi, Emo, Emu; + uint64x2_t Esa, Ese, Esi, Eso, Esu; + uint64x2_t Ca, Ce, Ci, Co, Cu; + uint64x2_t Da, De, Di, Do, Du; + uint64x2_t Ba, Be, Bi, Bo, Bu; + + Aba = vdupq_n_u64(st[0]); Abe = vdupq_n_u64(st[1]); + Abi = vdupq_n_u64(st[2]); Abo = vdupq_n_u64(st[3]); + Abu = vdupq_n_u64(st[4]); + Aga = vdupq_n_u64(st[5]); Age = vdupq_n_u64(st[6]); + Agi = vdupq_n_u64(st[7]); Ago = vdupq_n_u64(st[8]); + Agu = vdupq_n_u64(st[9]); + Aka = vdupq_n_u64(st[10]); Ake = vdupq_n_u64(st[11]); + Aki = vdupq_n_u64(st[12]); Ako = vdupq_n_u64(st[13]); + Aku = vdupq_n_u64(st[14]); + Ama = vdupq_n_u64(st[15]); Ame = vdupq_n_u64(st[16]); + Ami = vdupq_n_u64(st[17]); Amo = vdupq_n_u64(st[18]); + Amu = vdupq_n_u64(st[19]); + Asa = vdupq_n_u64(st[20]); Ase = vdupq_n_u64(st[21]); + Asi = vdupq_n_u64(st[22]); Aso = vdupq_n_u64(st[23]); + Asu = vdupq_n_u64(st[24]); + + ROUND(A, E, 0); ROUND(E, A, 1); + ROUND(A, E, 2); ROUND(E, A, 3); + ROUND(A, E, 4); ROUND(E, A, 5); + ROUND(A, E, 6); ROUND(E, A, 7); + ROUND(A, E, 8); ROUND(E, A, 9); + ROUND(A, E, 10); ROUND(E, A, 11); + ROUND(A, E, 12); ROUND(E, A, 13); + ROUND(A, E, 14); ROUND(E, A, 15); + ROUND(A, E, 16); ROUND(E, A, 17); + ROUND(A, E, 18); ROUND(E, A, 19); + ROUND(A, E, 20); ROUND(E, A, 21); + ROUND(A, E, 22); ROUND(E, A, 23); + + st[0] = vgetq_lane_u64(Aba, 0); st[1] = vgetq_lane_u64(Abe, 0); + st[2] = vgetq_lane_u64(Abi, 0); st[3] = vgetq_lane_u64(Abo, 0); + st[4] = vgetq_lane_u64(Abu, 0); + st[5] = vgetq_lane_u64(Aga, 0); st[6] = vgetq_lane_u64(Age, 0); + st[7] = vgetq_lane_u64(Agi, 0); st[8] = vgetq_lane_u64(Ago, 0); + st[9] = vgetq_lane_u64(Agu, 0); + st[10] = vgetq_lane_u64(Aka, 0); st[11] = vgetq_lane_u64(Ake, 0); + st[12] = vgetq_lane_u64(Aki, 0); st[13] = vgetq_lane_u64(Ako, 0); + st[14] = vgetq_lane_u64(Aku, 0); + st[15] = vgetq_lane_u64(Ama, 0); st[16] = vgetq_lane_u64(Ame, 0); + st[17] = vgetq_lane_u64(Ami, 0); st[18] = vgetq_lane_u64(Amo, 0); + st[19] = vgetq_lane_u64(Amu, 0); + st[20] = vgetq_lane_u64(Asa, 0); st[21] = vgetq_lane_u64(Ase, 0); + st[22] = vgetq_lane_u64(Asi, 0); st[23] = vgetq_lane_u64(Aso, 0); + st[24] = vgetq_lane_u64(Asu, 0); +} + +static void +keccakf_12_rounds(uint64_t st[25]) +{ + uint64x2_t Aba, Abe, Abi, Abo, Abu; + uint64x2_t Aga, Age, Agi, Ago, Agu; + uint64x2_t Aka, Ake, Aki, Ako, Aku; + uint64x2_t Ama, Ame, Ami, Amo, Amu; + uint64x2_t Asa, Ase, Asi, Aso, Asu; + uint64x2_t Eba, Ebe, Ebi, Ebo, Ebu; + uint64x2_t Ega, Ege, Egi, Ego, Egu; + uint64x2_t Eka, Eke, Eki, Eko, Eku; + uint64x2_t Ema, Eme, Emi, Emo, Emu; + uint64x2_t Esa, Ese, Esi, Eso, Esu; + uint64x2_t Ca, Ce, Ci, Co, Cu; + uint64x2_t Da, De, Di, Do, Du; + uint64x2_t Ba, Be, Bi, Bo, Bu; + + Aba = vdupq_n_u64(st[0]); Abe = vdupq_n_u64(st[1]); + Abi = vdupq_n_u64(st[2]); Abo = vdupq_n_u64(st[3]); + Abu = vdupq_n_u64(st[4]); + Aga = vdupq_n_u64(st[5]); Age = vdupq_n_u64(st[6]); + Agi = vdupq_n_u64(st[7]); Ago = vdupq_n_u64(st[8]); + Agu = vdupq_n_u64(st[9]); + Aka = vdupq_n_u64(st[10]); Ake = vdupq_n_u64(st[11]); + Aki = vdupq_n_u64(st[12]); Ako = vdupq_n_u64(st[13]); + Aku = vdupq_n_u64(st[14]); + Ama = vdupq_n_u64(st[15]); Ame = vdupq_n_u64(st[16]); + Ami = vdupq_n_u64(st[17]); Amo = vdupq_n_u64(st[18]); + Amu = vdupq_n_u64(st[19]); + Asa = vdupq_n_u64(st[20]); Ase = vdupq_n_u64(st[21]); + Asi = vdupq_n_u64(st[22]); Aso = vdupq_n_u64(st[23]); + Asu = vdupq_n_u64(st[24]); + + ROUND(A, E, 12); ROUND(E, A, 13); + ROUND(A, E, 14); ROUND(E, A, 15); + ROUND(A, E, 16); ROUND(E, A, 17); + ROUND(A, E, 18); ROUND(E, A, 19); + ROUND(A, E, 20); ROUND(E, A, 21); + ROUND(A, E, 22); ROUND(E, A, 23); + + st[0] = vgetq_lane_u64(Aba, 0); st[1] = vgetq_lane_u64(Abe, 0); + st[2] = vgetq_lane_u64(Abi, 0); st[3] = vgetq_lane_u64(Abo, 0); + st[4] = vgetq_lane_u64(Abu, 0); + st[5] = vgetq_lane_u64(Aga, 0); st[6] = vgetq_lane_u64(Age, 0); + st[7] = vgetq_lane_u64(Agi, 0); st[8] = vgetq_lane_u64(Ago, 0); + st[9] = vgetq_lane_u64(Agu, 0); + st[10] = vgetq_lane_u64(Aka, 0); st[11] = vgetq_lane_u64(Ake, 0); + st[12] = vgetq_lane_u64(Aki, 0); st[13] = vgetq_lane_u64(Ako, 0); + st[14] = vgetq_lane_u64(Aku, 0); + st[15] = vgetq_lane_u64(Ama, 0); st[16] = vgetq_lane_u64(Ame, 0); + st[17] = vgetq_lane_u64(Ami, 0); st[18] = vgetq_lane_u64(Amo, 0); + st[19] = vgetq_lane_u64(Amu, 0); + st[20] = vgetq_lane_u64(Asa, 0); st[21] = vgetq_lane_u64(Ase, 0); + st[22] = vgetq_lane_u64(Asi, 0); st[23] = vgetq_lane_u64(Aso, 0); + st[24] = vgetq_lane_u64(Asu, 0); +} + +void +keccak1600_armsha3_permute_24(void *state) +{ + uint64_t st[25]; + unsigned int i; + + for (i = 0U; i < 25U; i++) { + st[i] = LOAD64_LE((const unsigned char *) state + i * 8U); + } + + keccakf_24_rounds(st); + + for (i = 0U; i < 25U; i++) { + STORE64_LE((unsigned char *) state + i * 8U, st[i]); + } +} + +void +keccak1600_armsha3_permute_12(void *state) +{ + uint64_t st[25]; + unsigned int i; + + for (i = 0U; i < 25U; i++) { + st[i] = LOAD64_LE((const unsigned char *) state + i * 8U); + } + + keccakf_12_rounds(st); + + for (i = 0U; i < 25U; i++) { + STORE64_LE((unsigned char *) state + i * 8U, st[i]); + } +} + +void +keccak1600_armsha3_init(void *state) +{ + memset(state, 0, KECCAK1600_STATEBYTES); +} + +void +keccak1600_armsha3_xor_bytes(void *state, const unsigned char *data, size_t offset, size_t length) +{ + unsigned char *st = (unsigned char *) state; + size_t i; + + for (i = 0U; i < length; i++) { + st[offset + i] ^= data[i]; + } +} + +void +keccak1600_armsha3_extract_bytes(const void *state, unsigned char *data, size_t offset, size_t length) +{ + const unsigned char *st = (const unsigned char *) state; + + memcpy(data, st + offset, length); +} + +#ifdef __clang__ +# pragma clang attribute pop +#endif + +#endif /* __ARM_FEATURE_SHA3 */ diff --git a/src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.h b/src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.h new file mode 100644 index 00000000..10d9584a --- /dev/null +++ b/src/libsodium/crypto_core/keccak1600/armsha3/keccak1600_armsha3.h @@ -0,0 +1,20 @@ +#ifndef keccak1600_armsha3_H +#define keccak1600_armsha3_H + +#include + +#include "private/quirks.h" + +void keccak1600_armsha3_init(void *state); + +void keccak1600_armsha3_xor_bytes(void *state, const unsigned char *bytes, + size_t offset, size_t length); + +void keccak1600_armsha3_extract_bytes(const void *state, unsigned char *bytes, + size_t offset, size_t length); + +void keccak1600_armsha3_permute_24(void *state); + +void keccak1600_armsha3_permute_12(void *state); + +#endif /* keccak1600_armsha3_H */ diff --git a/src/libsodium/crypto_core/keccak1600/keccak1600.c b/src/libsodium/crypto_core/keccak1600/keccak1600.c index d1eeadb4..70459d11 100644 --- a/src/libsodium/crypto_core/keccak1600/keccak1600.c +++ b/src/libsodium/crypto_core/keccak1600/keccak1600.c @@ -1,6 +1,21 @@ #include "crypto_core_keccak1600.h" #include "ref/keccak1600_ref.h" +#ifdef __ARM_FEATURE_SHA3 +# include "armsha3/keccak1600_armsha3.h" +# define keccak1600_init keccak1600_armsha3_init +# define keccak1600_xor_bytes keccak1600_armsha3_xor_bytes +# define keccak1600_extract_bytes keccak1600_armsha3_extract_bytes +# define keccak1600_permute_24 keccak1600_armsha3_permute_24 +# define keccak1600_permute_12 keccak1600_armsha3_permute_12 +#else +# define keccak1600_init keccak1600_ref_init +# define keccak1600_xor_bytes keccak1600_ref_xor_bytes +# define keccak1600_extract_bytes keccak1600_ref_extract_bytes +# define keccak1600_permute_24 keccak1600_ref_permute_24 +# define keccak1600_permute_12 keccak1600_ref_permute_12 +#endif + size_t crypto_core_keccak1600_statebytes(void) { @@ -10,7 +25,7 @@ crypto_core_keccak1600_statebytes(void) void crypto_core_keccak1600_init(crypto_core_keccak1600_state *state) { - keccak1600_ref_init(state->opaque); + keccak1600_init(state->opaque); } void @@ -18,7 +33,7 @@ crypto_core_keccak1600_xor_bytes(crypto_core_keccak1600_state *state, const unsigned char *bytes, size_t offset, size_t length) { - keccak1600_ref_xor_bytes(state->opaque, bytes, offset, length); + keccak1600_xor_bytes(state->opaque, bytes, offset, length); } void @@ -26,17 +41,17 @@ crypto_core_keccak1600_extract_bytes(const crypto_core_keccak1600_state *state, unsigned char *bytes, size_t offset, size_t length) { - keccak1600_ref_extract_bytes(state->opaque, bytes, offset, length); + keccak1600_extract_bytes(state->opaque, bytes, offset, length); } void crypto_core_keccak1600_permute_24(crypto_core_keccak1600_state *state) { - keccak1600_ref_permute_24(state->opaque); + keccak1600_permute_24(state->opaque); } void crypto_core_keccak1600_permute_12(crypto_core_keccak1600_state *state) { - keccak1600_ref_permute_12(state->opaque); + keccak1600_permute_12(state->opaque); } diff --git a/src/libsodium/include/sodium/private/quirks.h b/src/libsodium/include/sodium/private/quirks.h index b155bc39..82ec2c5a 100644 --- a/src/libsodium/include/sodium/private/quirks.h +++ b/src/libsodium/include/sodium/private/quirks.h @@ -70,6 +70,11 @@ #define ge25519_scalarmult _sodium_ge25519_scalarmult #define ge25519_scalarmult_base _sodium_ge25519_scalarmult_base #define ge25519_tobytes _sodium_ge25519_tobytes +#define keccak1600_armsha3_extract_bytes _sodium_keccak1600_armsha3_extract_bytes +#define keccak1600_armsha3_init _sodium_keccak1600_armsha3_init +#define keccak1600_armsha3_permute_12 _sodium_keccak1600_armsha3_permute_12 +#define keccak1600_armsha3_permute_24 _sodium_keccak1600_armsha3_permute_24 +#define keccak1600_armsha3_xor_bytes _sodium_keccak1600_armsha3_xor_bytes #define keccak1600_ref_extract_bytes _sodium_keccak1600_ref_extract_bytes #define keccak1600_ref_init _sodium_keccak1600_ref_init #define keccak1600_ref_permute_12 _sodium_keccak1600_ref_permute_12 diff --git a/test/symbols/all-symbols.txt b/test/symbols/all-symbols.txt index cea505aa..4f0e142f 100644 --- a/test/symbols/all-symbols.txt +++ b/test/symbols/all-symbols.txt @@ -746,6 +746,11 @@ ge25519_p3_tobytes ge25519_scalarmult ge25519_scalarmult_base ge25519_tobytes +keccak1600_armsha3_extract_bytes +keccak1600_armsha3_init +keccak1600_armsha3_permute_12 +keccak1600_armsha3_permute_24 +keccak1600_armsha3_xor_bytes keccak1600_ref_extract_bytes keccak1600_ref_init keccak1600_ref_permute_12