Crash when generating blake2b hash

0

I'm trying to write a hash function with Botan as my back end that is:

std::string hex_hash(std::string &in, const std::string &HASH)
{
std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(HASH));

return Botan::hex_encode(hash->process(in));
}

HASH is a std::string that is the name of the hash requested. i.e. "SHA-512" for SHA512 and "BLAKE2b" for BLAKE2b

Any other hash supported by botan gets processed and outputted, but BLAKE2b doesn't and throws an exception:

Exception thrown at 0x00007FF661382C5A in test.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

And then crashes.

I did notice in the manual for botan (https://botan.randombit.net/manual/hash.html) that

BLAKE2b

Available if BOTAN_HAS_BLAKE2B is defined.

A recently designed hash function. Very fast on 64-bit processors. Can output a hash of any length between 1 and 64 bytes, this is specified by passing a value to the constructor with the desired length.

Is it possible that there is no default set? How would I set it? Is it possible I have the wrong name?

c++
hash
botan
asked on Stack Overflow Jan 30, 2019 by Matt • edited Jan 30, 2019 by Matt

1 Answer

1

The issue likely is the capitalization of the name. I'm using Botan 2.9.0 and "Blake2b" works, whereas "BLAKE2b" crashes. The master branch of Botan also accepts "BLAKE2b", but this was only added recently. The default output length is set to 512 bits, you can adapt it by passing e.g. "Blake2b(256)" or by calling the Blake2b constructor directly (as in the linked commit).

Small working example (main.cpp):


#include <iostream>
#include <memory>
#include <string>

#include "botan/hash.h"
#include "botan/hex.h"

std::string hex_hash(const std::string& in, const std::string& HASH) {
    std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create(HASH));
    return Botan::hex_encode(hash->process(in));
}

int main(int argc, char** argv) {
    std::cout << "Blake2b " << hex_hash("abc", "Blake2b") << std::endl;
    std::cout << "Blake2b(256) " << hex_hash("abc", "Blake2b(256)") << std::endl;
    return 0;
}
$ g++ `pkgconf --cflags --libs botan-2` main.cpp && ./a.out 
Blake2b BA80A53F981C4D0D6A2797B69F12F6E94C212F14685AC4B74B12BB6FDBFFA2D17D87C5392AAB792DC252D5DE4533CC9518D38AA8DBF1925AB92386EDD4009923
Blake2b(256) BDDD813C634239723171EF3FEE98579B94964E3BB1CB3E427262C8C068D52319

If this does not work for you, also ensure your Botan is compiled with Blake2b support, e.g.

$ grep BLAKE /usr/include/botan-2/botan/build.h 
#define BOTAN_HAS_BLAKE2B 20130131

User contributions licensed under CC BY-SA 3.0