1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/crypto/aesgcm-select.c

39 lines
1.1 KiB
C
Raw Normal View History

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 17:36:58 +00:00
#include "ssh.h"
#include "aesgcm.h"
static ssh2_mac *aesgcm_mac_selector_new(const ssh2_macalg *alg,
ssh_cipher *cipher)
{
static const ssh2_macalg *const real_algs[] = {
#if HAVE_CLMUL
&ssh2_aesgcm_mac_clmul,
#endif
#if HAVE_NEON_PMULL
&ssh2_aesgcm_mac_neon,
#endif
&ssh2_aesgcm_mac_sw,
NULL,
};
for (size_t i = 0; real_algs[i]; i++) {
const ssh2_macalg *alg = real_algs[i];
const struct aesgcm_extra *alg_extra =
(const struct aesgcm_extra *)alg->extra;
if (check_aesgcm_availability(alg_extra))
return ssh2_mac_new(alg, cipher);
}
/* We should never reach the NULL at the end of the list, because
* the last non-NULL entry should be software-only GCM, which is
* always available. */
unreachable("aesgcm_select ran off the end of its list");
}
const ssh2_macalg ssh2_aesgcm_mac = {
.new = aesgcm_mac_selector_new,
.name = "",
.etm_name = "", /* Not selectable independently */
.len = 16,
.keylen = 0,
};