aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/blake2test.c
blob: 56729eebe1360de7e68a16899e6bbe0c44dc71c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

/*
 * Test vectors for blake2s
 *
 * Copyright © 2026 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#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;
}