From d73f692eea9dfbb000d0e0a5f69e52e4e591c5b0 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 20 Jan 2019 11:37:05 +0000 Subject: [PATCH] Add an 'extra' pointer field to ssh2_macalg. Similar to the versions in ssh_cipheralg and ssh_keyalg, this allows a set of vtables to share function pointers while providing varying constant data that the shared function can use to vary its behaviour. As an initial demonstration, I've used this to recombine the four trivial text_name methods for the HMAC-SHA1 variants. I'm about to use it for something more sensible, though. --- ssh.h | 3 +++ sshsha.c | 45 ++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/ssh.h b/ssh.h index ec7566f6..2aaa679b 100644 --- a/ssh.h +++ b/ssh.h @@ -630,6 +630,9 @@ struct ssh2_macalg { const char *(*text_name)(ssh2_mac *); const char *name, *etm_name; int len, keylen; + + /* Pointer to any extra data used by a particular implementation. */ + const void *extra; }; #define ssh2_mac_new(alg, cipher) ((alg)->new(alg, cipher)) diff --git a/sshsha.c b/sshsha.c index 2b9c1c80..0976bdd0 100644 --- a/sshsha.c +++ b/sshsha.c @@ -375,52 +375,51 @@ void hmac_sha1_simple(const void *key, int keylen, SHA_Final(&states[1], output); } +struct hmacsha1_extra { + const char *textname; +}; + static const char *hmacsha1_text_name(ssh2_mac *mac) { - return "HMAC-SHA1"; -} - -static const char *hmacsha196_text_name(ssh2_mac *mac) -{ - return "HMAC-SHA1-96"; -} - -static const char *hmacsha1bug_text_name(ssh2_mac *mac) -{ - return "bug-compatible HMAC-SHA1"; -} - -static const char *hmacsha196bug_text_name(ssh2_mac *mac) -{ - return "bug-compatible HMAC-SHA1-96"; + const struct hmacsha1_extra *extra = + (const struct hmacsha1_extra *)mac->vt->extra; + return extra->textname; } +const struct hmacsha1_extra ssh_hmac_sha1_extra = { "HMAC-SHA1" }; const ssh2_macalg ssh_hmac_sha1 = { hmacsha1_new, hmacsha1_free, hmacsha1_key, hmacsha1_start, hmacsha1_genresult, hmacsha1_text_name, "hmac-sha1", "hmac-sha1-etm@openssh.com", - 20, 20, + 20, 20, &ssh_hmac_sha1_extra, }; +const struct hmacsha1_extra ssh_hmac_sha1_96_extra = { "HMAC-SHA1-96" }; const ssh2_macalg ssh_hmac_sha1_96 = { hmacsha1_new, hmacsha1_free, hmacsha1_key, - hmacsha1_start, hmacsha1_genresult, hmacsha196_text_name, + hmacsha1_start, hmacsha1_genresult, hmacsha1_text_name, "hmac-sha1-96", "hmac-sha1-96-etm@openssh.com", - 12, 20, + 12, 20, &ssh_hmac_sha1_96_extra, }; +const struct hmacsha1_extra ssh_hmac_sha1_buggy_extra = { + "bug-compatible HMAC-SHA1", +}; const ssh2_macalg ssh_hmac_sha1_buggy = { hmacsha1_new, hmacsha1_free, hmacsha1_key, - hmacsha1_start, hmacsha1_genresult, hmacsha1bug_text_name, + hmacsha1_start, hmacsha1_genresult, hmacsha1_text_name, "hmac-sha1", NULL, - 20, 16, + 20, 16, &ssh_hmac_sha1_buggy_extra, }; +const struct hmacsha1_extra ssh_hmac_sha1_96_buggy_extra = { + "bug-compatible HMAC-SHA1-96", +}; const ssh2_macalg ssh_hmac_sha1_96_buggy = { hmacsha1_new, hmacsha1_free, hmacsha1_key, - hmacsha1_start, hmacsha1_genresult, hmacsha196bug_text_name, + hmacsha1_start, hmacsha1_genresult, hmacsha1_text_name, "hmac-sha1-96", NULL, - 12, 16, + 12, 16, &ssh_hmac_sha1_96_buggy_extra, }; #ifdef COMPILER_SUPPORTS_SHA_NI