mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 09:27:59 +00:00
fca13a17b1
This applies to all of AES, SHA-1, SHA-256 and SHA-512. All those source files previously contained multiple implementations of the algorithm, enabled or disabled by ifdefs detecting whether they would work on a given compiler. And in order to get advanced machine instructions like AES-NI or NEON crypto into the output file when the compile flags hadn't enabled them, we had to do nasty stuff with compiler-specific pragmas or attributes. Now we can do the detection at cmake time, and enable advanced instructions in the more sensible way, by compile-time flags. So I've broken up each of these modules into lots of sub-pieces: a file called (e.g.) 'foo-common.c' containing common definitions across all implementations (such as round constants), one called 'foo-select.c' containing the top-level vtable(s), and a separate file for each implementation exporting just the vtable(s) for that implementation. One advantage of this is that it depends a lot less on compiler- specific bodgery. My particular least favourite part of the previous setup was the part where I had to _manually_ define some Arm ACLE feature macros before including <arm_neon.h>, so that it would define the intrinsics I wanted. Now I'm enabling interesting architecture features in the normal way, on the compiler command line, there's no need for that kind of trick: the right feature macros are already defined and <arm_neon.h> does the right thing. Another change in this reorganisation is that I've stopped assuming there's just one hardware implementation per platform. Previously, the accelerated vtables were called things like sha256_hw, and varied between FOO-NI and NEON depending on platform; and the selection code would simply ask 'is hw available? if so, use hw, else sw'. Now, each HW acceleration strategy names its vtable its own way, and the selection vtable has a whole list of possibilities to iterate over looking for a supported one. So if someone feels like writing a second accelerated implementation of something for a given platform - for example, I've heard you can use plain NEON to speed up AES somewhat even without the crypto extension - then it will now have somewhere to drop in alongside the existing ones.
81 lines
2.0 KiB
C
81 lines
2.0 KiB
C
/*
|
|
* Unix implementation of the OS query functions that detect Arm
|
|
* architecture extensions.
|
|
*/
|
|
|
|
#include "putty.h"
|
|
#include "ssh.h"
|
|
|
|
#include "utils/arm_arch_queries.h"
|
|
|
|
#if defined __arm__ || defined __aarch64__
|
|
|
|
bool platform_aes_neon_available(void)
|
|
{
|
|
#if defined HWCAP_AES
|
|
return getauxval(AT_HWCAP) & HWCAP_AES;
|
|
#elif defined HWCAP2_AES
|
|
return getauxval(AT_HWCAP2) & HWCAP2_AES;
|
|
#elif defined __APPLE__
|
|
/* M1 macOS defines no optional sysctl flag indicating presence of
|
|
* the AES extension, which I assume to be because it's always
|
|
* present */
|
|
return true;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
bool platform_sha256_neon_available(void)
|
|
{
|
|
#if defined HWCAP_SHA2
|
|
return getauxval(AT_HWCAP) & HWCAP_SHA2;
|
|
#elif defined HWCAP2_SHA2
|
|
return getauxval(AT_HWCAP2) & HWCAP2_SHA2;
|
|
#elif defined __APPLE__
|
|
/* Assume always present on M1 macOS, similarly to AES */
|
|
return true;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
bool platform_sha1_neon_available(void)
|
|
{
|
|
#if defined HWCAP_SHA1
|
|
return getauxval(AT_HWCAP) & HWCAP_SHA1;
|
|
#elif defined HWCAP2_SHA1
|
|
return getauxval(AT_HWCAP2) & HWCAP2_SHA1;
|
|
#elif defined __APPLE__
|
|
/* Assume always present on M1 macOS, similarly to AES */
|
|
return true;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
bool platform_sha512_neon_available(void)
|
|
{
|
|
#if defined HWCAP_SHA512
|
|
return getauxval(AT_HWCAP) & HWCAP_SHA512;
|
|
#elif defined HWCAP2_SHA512
|
|
return getauxval(AT_HWCAP2) & HWCAP2_SHA512;
|
|
#elif defined __APPLE__
|
|
return test_sysctl_flag("hw.optional.armv8_2_sha512");
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
#else /* defined __arm__ || defined __aarch64__ */
|
|
|
|
/*
|
|
* Include _something_ in this file to prevent an annoying compiler
|
|
* warning, and to avoid having to condition out this file in
|
|
* CMakeLists. It's in a library, so this variable shouldn't end up in
|
|
* any actual program, because nothing will refer to it.
|
|
*/
|
|
const int arm_arch_queries_dummy_variable = 0;
|
|
|
|
#endif /* defined __arm__ || defined __aarch64__ */
|