/* * Test vectors for blake2s * * Copyright © 2026 Samuel Lidén Borell * * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later */ #include #include #include #include "compiler.h" #include "blake2.h" static void compare_hashes(const unsigned char *actual, const unsigned char *expected) { if (memcmp(actual, expected, BLAKE2S_OUTBYTES)) { int i; fprintf(stderr, "Error: blake2s test vector mismatch! Was:\n"); for (i = 0; i < BLAKE2S_OUTBYTES; i++) { fprintf(stderr, "0x%02x, ", actual[i]); } exit(EXIT_FAILURE); } } static void do_test(const char *input, size_t inlen, const unsigned char *expected) { unsigned char output[BLAKE2S_OUTBYTES]; blake2s_state state; size_t i; blake2s_init(&state, BLAKE2S_OUTBYTES); if (inlen != 0) { blake2s_update(&state, input, inlen); } blake2s_final(&state, output, BLAKE2S_OUTBYTES); compare_hashes(output, expected); if (inlen >= 2) { blake2s_init(&state, BLAKE2S_OUTBYTES); for (i = 0; i < inlen; i++) { char b = input[i]; blake2s_update(&state, &b, 1); } blake2s_final(&state, output, BLAKE2S_OUTBYTES); compare_hashes(output, expected); } } int main(void) { { /* From https://datatracker.ietf.org/doc/html/rfc7693#appendix-B */ static const char input[] = "abc"; static const unsigned char expected[] = { 0x50, 0x8C, 0x5E, 0x8C, 0x32, 0x7C, 0x14, 0xE2, 0xE1, 0xA7, 0x2B, 0xA3, 0x4E, 0xEB, 0x45, 0x2F, 0x37, 0x45, 0x8B, 0x20, 0x9E, 0xD6, 0x3A, 0x29, 0x4D, 0x99, 0x9B, 0x4C, 0x86, 0x67, 0x59, 0x82 }; do_test(input, sizeof(input)-1, expected); } { /* From https://en.wikipedia.org/wiki/BLAKE_(hash_function) page revision 1332829354, section "Example digests" */ static const unsigned char expected[] = { 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94, 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c, 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e, 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9 }; do_test(NULL, 0, expected); } { /* Test of multi-block hash computation. It can be verified with the following Python code: from hashlib import blake2s s = b'This string is more than 32 bytes ' + \ b'long, and therefore it will ' + \ b'trigger multiple compression operations ' + \ b'inside the blake2s code.' print(blake2s(s).hexdigest()) */ static const char input[] = "This string is more than 32 bytes long, and therefore it will " "trigger multiple compression operations inside the blake2s code."; static const unsigned char expected[] = { 0x28, 0xe6, 0xaf, 0x0e, 0xa2, 0xd0, 0xad, 0x86, 0x47, 0x81, 0x45, 0xfe, 0x64, 0xe9, 0xb8, 0x18, 0x75, 0x7d, 0x7c, 0x84, 0x02, 0x7d, 0x42, 0x7e, 0x19, 0xbe, 0x55, 0x9c, 0x75, 0xbb, 0x5d, 0xf5 }; do_test(input, sizeof(input)-1, expected); } return EXIT_SUCCESS; }