The constructor BLAKE2_Base(bool treeMode, unsigned int digestSize) uses an incorrect initialization block and produces wrong results if either treeMode or digestSize differ from default.
The patch is here:
blake2-init-patch.zip
In essense it replaces
UncheckedSetKey(NULL, 0, g_nullNameValuePairs);
with
UncheckedSetKey(NULL, 0, MakeParameters(Name::DigestSize(), (int)digestSize)(Name::TreeMode(), treeMode, false));
Thanks @c0ff.
Can you test this against master? I'm tied up at the moment with other testing.
git clone https://github.com/weidai11/cryptopp-fix
cd cryptopp-fix
./cryptest.sh
cryptest.sh is our test script from hell, and it torture tests the library under different configurations. On a modern desktop, like a 5th gen or 6th gen, it will take a couple of hours to run.
Stepping back to 10,000 feet, this is my fault. I did not generate test vectors for the configurations, so the bug passed without notice. I'll checkout Sam Neves' BLAKE2 GitHub (@sneves) and generate them.
bool ValidateBLAKE2b() does not cover this case. That's why it has gone undetected. I'll try to cook a patch for the test as well.
Adjusted and tested the patches against master: blake2-init-and-test-patches.zip
Though, I only did a short test with cryptest.exe v 74 and cryptest.exe v 75.
After applying blake2-test.patch:
Without blake2-init.patch:
failed_blake2s.txt
failed_blake2b.txt
With blake2-init.patch:
passed_blake2s.txt
passed_blake2b.txt
The new test vectors were generated with pyblake2 which uses 'BLAKE2 reference source code package' by Samuel Neves.
Thanks @c0ff.
Adjusted and tested the patches against master: blake2-init-and-test-patches.zip
Do you want to make a Pull Request (see Creating a pull request)? If you do, then you become contributor. I like to see folks get credit for their work, so I'd like to see you make one. It also relieves me from maintaining a separate file with a list of contributors.
If you don't want to, then that's fine, too. I'll drop your patch by hand and push it.
The new test vectors were generated with pyblake2 which uses 'BLAKE2 reference source code package' by Samuel Neves.
Cool, thanks. That's good provenance.
Ok, will try to make a Pull Request then, I need to get used to it anyway :)
Ok, will try to make a Pull Request then, I need to get used to it anyway :)
Just an FYI... The GitHub instructions for updating your clone from Wei's are kind of borked. They create a lot of unnecessary merges. Use these instructions instead: Keeping A GitHub Fork Updated.
Cleared with Pull Request 416.