1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 09:58:01 +00:00
putty-source/unix/utils/arm_arch_queries.c
Simon Tatham c1a2114b28 Implement AES-GCM using the @openssh.com protocol IDs.
I only recently found out that OpenSSH defined their own protocol IDs
for AES-GCM, defined to work the same as the standard ones except that
they fixed the semantics for how you select the linked cipher+MAC pair
during key exchange.

(RFC 5647 defines protocol ids for AES-GCM in both the cipher and MAC
namespaces, and requires that you MUST select both or neither - but
this contradicts the selection policy set out in the base SSH RFCs,
and there's no discussion of how you resolve a conflict between them!
OpenSSH's answer is to do it the same way ChaCha20-Poly1305 works,
because that will ensure the two suites don't fight.)

People do occasionally ask us for this linked cipher/MAC pair, and now
I know it's actually feasible, I've implemented it, including a pair
of vector implementations for x86 and Arm using their respective
architecture extensions for multiplying polynomials over GF(2).

Unlike ChaCha20-Poly1305, I've kept the cipher and MAC implementations
in separate objects, with an arm's-length link between them that the
MAC uses when it needs to encrypt single cipher blocks to use as the
inputs to the MAC algorithm. That enables the cipher and the MAC to be
independently selected from their hardware-accelerated versions, just
in case someone runs on a system that has polynomial multiplication
instructions but not AES acceleration, or vice versa.

There's a fourth implementation of the GCM MAC, which is a pure
software implementation of the same algorithm used in the vectorised
versions. It's too slow to use live, but I've kept it in the code for
future testing needs, and because it's a convenient place to dump my
design comments.

The vectorised implementations are fairly crude as far as optimisation
goes. I'm sure serious x86 _or_ Arm optimisation engineers would look
at them and laugh. But GCM is a fast MAC compared to HMAC-SHA-256
(indeed compared to HMAC-anything-at-all), so it should at least be
good enough to use. And we've got a working version with some tests
now, so if someone else wants to improve them, they can.
2022-08-16 20:33:58 +01:00

106 lines
2.9 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__
SysctlResult res = test_sysctl_flag("hw.optional.arm.FEAT_AES");
/* Older M1 macOS didn't provide this flag, but as far as I know
* implemented the crypto extension anyway, so treat 'feature
* missing' as 'implemented' */
return res != SYSCTL_OFF;
#else
return false;
#endif
}
bool platform_pmull_neon_available(void)
{
#if defined HWCAP_PMULL
return getauxval(AT_HWCAP) & HWCAP_PMULL;
#elif defined HWCAP2_PMULL
return getauxval(AT_HWCAP2) & HWCAP2_PMULL;
#elif defined __APPLE__
SysctlResult res = test_sysctl_flag("hw.optional.arm.FEAT_PMULL");
/* As above, treat 'missing' as enabled */
return res != SYSCTL_OFF;
#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__
SysctlResult res = test_sysctl_flag("hw.optional.arm.FEAT_SHA256");
/* As above, treat 'missing' as enabled */
return res != SYSCTL_OFF;
#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__
SysctlResult res = test_sysctl_flag("hw.optional.arm.FEAT_SHA1");
/* As above, treat 'missing' as enabled */
return res != SYSCTL_OFF;
#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__
/* There are two sysctl flags for this, apparently invented at
* different times. Try both, falling back to the older one. */
SysctlResult res = test_sysctl_flag("hw.optional.arm.FEAT_SHA512");
if (res != SYSCTL_MISSING)
return res == SYSCTL_ON;
res = test_sysctl_flag("hw.optional.armv8_2_sha512");
return res == SYSCTL_ON;
#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__ */