Make crypto_digest_t allocated using minimal memory
We will soon have:
struct crypto_digest_t {
union {
SHA_CTX sha1;
SHA256_CTX sha2;
SHA512_CTX sha512; // added in 0.2.8
keccak_state sha3; // added by #17783
} d;
digest_algorithm_bitfield_t algorithm : 8;
};
On my 64-bit host:
Size of SHA_CTX == 96
Size of SHA256_CTX == 112
Size of SHA512_CTX == 216
Size of keccak_state == 432
This means that when we allocate a sha1 digest object in order to compute the running SHA1 of an input stream (for the current tor relay protocol), we have been wasting 16 bytes, we are now wasting 120 bytes, and we will soon be wasting 336 bytes.
We should probably adjust the way we lay out and allocate the crypto_digest_t structure so that it looks more like this:
struct crypto_digest_t {
digest_algorithm_t algorithm; // no point in a bitfield
union {
SHA_CTX sha1;
SHA256_CTX sha2;
SHA512_CTX sha512; // added in 0.2.8
keccak_state sha3; // added by #17783
} d;
and we should have crypto_digest*_new allocate no more bytes than the algorithm will actually use.