Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2024-12-09 18:49:58


Vinnie Falco wrote:
> Sigh.. copy/paste typo.
>
> /** Return a seeded HashAlgorithm
> */
> template< class HashAlgorithm >
> HashAlgorithm make_seeded(
> unsigned char const* seed, std::size_t n )
> {
> if constexpr(std::is_constructible<HashAlgorithm,
> unsigned char const*, std::size_t>)
> return HashAlgorithm(seed, n);
> else
> {
> HashAlgorithm h;
> hash_append(h, seed, n);

You can in principle do something like that (except fix
it to compile) but (much) better practices would be

- hash.update( seed, n )
- encode `n` as 64 bit little endian in 8 bytes
- hash.update these bytes
- hash.update( "\x80", 1 )
- hash.update( "\x00", 1 )
- hash.update with as many zeroes as needed to reach
  a multiple of HashAlgorithm::block_size

That's, incidentally, exactly what `update(p, n); result();`
does. (What an amazing coincidence.)

Note that reaching a multiple of block_size is important,
because it ensures that the secret key you passed as
the seed is not left in the internal buffer of the hash
algorithm, visible in memory dumps.

The test

https://github.com/pdimov/hash2/blob/develop/test/plaintext_leak.cpp

tries to catch these mistakes.

That's why this line

https://github.com/pdimov/hash2/blob/7a25f8518692b657e9272884519519fbaca2ec54/include/boost/hash2/md5.hpp#L257

is needed, for example.

> return h;
> }
> }
>
> Thanks
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk