1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-12 00:33:53 -05:00

Refactor the ssh_hash vtable. (NFC)

The idea is to arrange that an ssh_hash object can be reused without
having to free it and allocate a new one. So the 'final' method has
been replaced with 'digest', which does everything except the trailing
free; and there's also a new pair of methods 'reset' and 'copyfrom'
which overwrite the state of a hash with either the starting state or
a copy of another state. Meanwhile, the 'new' allocator function has
stopped performing 'reset' as a side effect; now it _just_ does the
administrative stuff (allocation, setting up vtables), and returns an
object which isn't yet ready to receive any actual data, expecting
that the caller will either reset it or copy another hash state into
it.

In particular, that means that the SHA-384 / SHA-512 pair no longer
need separate 'new' methods, because only the 'reset' part has to
change between them.

This commit makes no change to the user-facing API of wrapper
functions in ssh.h, except to add new functions which nothing yet
calls. The user-facing ssh_hash_new() calls the new and reset methods
in succession, and the copy and final methods still exist to do
new+copy and digest+free.
This commit is contained in:
Simon Tatham
2019-12-15 09:30:10 +00:00
parent 859c81e838
commit 156762fc02
5 changed files with 155 additions and 131 deletions

View File

@ -235,24 +235,24 @@ struct md5_hash {
static ssh_hash *md5_new(const ssh_hashalg *alg)
{
struct md5_hash *h = snew(struct md5_hash);
MD5Init(&h->state);
h->hash.vt = alg;
BinarySink_DELEGATE_INIT(&h->hash, &h->state);
return &h->hash;
}
static ssh_hash *md5_copy(ssh_hash *hashold)
static void md5_reset(ssh_hash *hash)
{
struct md5_hash *hold, *hnew;
ssh_hash *hashnew = md5_new(hashold->vt);
struct md5_hash *h = container_of(hash, struct md5_hash, hash);
MD5Init(&h->state);
}
hold = container_of(hashold, struct md5_hash, hash);
hnew = container_of(hashnew, struct md5_hash, hash);
static void md5_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
{
struct md5_hash *copy = container_of(hcopy, struct md5_hash, hash);
struct md5_hash *orig = container_of(horig, struct md5_hash, hash);
hnew->state = hold->state;
BinarySink_COPIED(&hnew->state);
return hashnew;
copy->state = orig->state;
BinarySink_COPIED(&copy->state);
}
static void md5_free(ssh_hash *hash)
@ -263,13 +263,13 @@ static void md5_free(ssh_hash *hash)
sfree(h);
}
static void md5_final(ssh_hash *hash, unsigned char *output)
static void md5_digest(ssh_hash *hash, unsigned char *output)
{
struct md5_hash *h = container_of(hash, struct md5_hash, hash);
MD5Final(output, &h->state);
md5_free(hash);
}
const ssh_hashalg ssh_md5 = {
md5_new, md5_copy, md5_final, md5_free, 16, 64, HASHALG_NAMES_BARE("MD5"),
md5_new, md5_reset, md5_copyfrom, md5_digest, md5_free,
16, 64, HASHALG_NAMES_BARE("MD5"),
};