Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/crypto: tests: Add KUnit tests for BLAKE2s

Add a KUnit test suite for BLAKE2s. Most of the core test logic is in
the previously-added hash-test-template.h. This commit just adds the
actual KUnit suite, commits the generated test vectors to the tree so
that gen-hash-testvecs.py won't have to be run at build time, and adds a
few BLAKE2s-specific test cases.

This is the replacement for blake2s-selftest, which an earlier commit
removed. Improvements over blake2s-selftest include integration with
KUnit, more comprehensive test cases, and support for benchmarking.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250827151131.27733-13-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+407 -3
+10
lib/crypto/tests/Kconfig
··· 1 1 # SPDX-License-Identifier: GPL-2.0-or-later 2 2 3 + config CRYPTO_LIB_BLAKE2S_KUNIT_TEST 4 + tristate "KUnit tests for BLAKE2s" if !KUNIT_ALL_TESTS 5 + depends on KUNIT 6 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 7 + select CRYPTO_LIB_BENCHMARK_VISIBLE 8 + # No need to select CRYPTO_LIB_BLAKE2S here, as that option doesn't 9 + # exist; the BLAKE2s code is always built-in for the /dev/random driver. 10 + help 11 + KUnit tests for the BLAKE2s cryptographic hash function. 12 + 3 13 config CRYPTO_LIB_MD5_KUNIT_TEST 4 14 tristate "KUnit tests for MD5" if !KUNIT_ALL_TESTS 5 15 depends on KUNIT
+1
lib/crypto/tests/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-or-later 2 2 3 + obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o 3 4 obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o 4 5 obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o 5 6 obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o
+238
lib/crypto/tests/blake2s-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py blake2s */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[BLAKE2S_HASH_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94, 12 + 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c, 13 + 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e, 14 + 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9, 15 + }, 16 + }, 17 + { 18 + .data_len = 1, 19 + .digest = { 20 + 0x7c, 0xab, 0x53, 0xe2, 0x48, 0x87, 0xdf, 0x64, 21 + 0x98, 0x6a, 0xc1, 0x7e, 0xf0, 0x01, 0x4d, 0xc9, 22 + 0x07, 0x4f, 0xb8, 0x2f, 0x46, 0xd7, 0xee, 0xa9, 23 + 0xad, 0xe5, 0xf8, 0x21, 0xac, 0xfe, 0x17, 0x58, 24 + }, 25 + }, 26 + { 27 + .data_len = 2, 28 + .digest = { 29 + 0x5e, 0x63, 0x2c, 0xd0, 0xf8, 0x7b, 0xf5, 0xae, 30 + 0x61, 0x97, 0x94, 0x57, 0xc8, 0x76, 0x22, 0xd9, 31 + 0x8b, 0x04, 0x5e, 0xf1, 0x5d, 0xd0, 0xfc, 0xd9, 32 + 0x0c, 0x19, 0x2e, 0xe2, 0xc5, 0xd9, 0x73, 0x51, 33 + }, 34 + }, 35 + { 36 + .data_len = 3, 37 + .digest = { 38 + 0x33, 0x65, 0xa6, 0x37, 0xbf, 0xf8, 0x4f, 0x15, 39 + 0x4c, 0xac, 0x9e, 0xa4, 0x3b, 0x02, 0x07, 0x0c, 40 + 0x80, 0x86, 0x0d, 0x6c, 0xe4, 0xaf, 0x1c, 0xbc, 41 + 0x0b, 0x9c, 0x0a, 0x98, 0xc2, 0x99, 0x71, 0xcd, 42 + }, 43 + }, 44 + { 45 + .data_len = 16, 46 + .digest = { 47 + 0x59, 0xd2, 0x10, 0xd3, 0x75, 0xac, 0x48, 0x32, 48 + 0xb1, 0xea, 0xee, 0xcf, 0x0a, 0xd2, 0x8b, 0x15, 49 + 0x5d, 0x72, 0x71, 0x4c, 0xa7, 0x29, 0xb0, 0x7a, 50 + 0x44, 0x48, 0x8a, 0x54, 0x54, 0x54, 0x41, 0xf5, 51 + }, 52 + }, 53 + { 54 + .data_len = 32, 55 + .digest = { 56 + 0xdc, 0xfc, 0x46, 0x81, 0xc6, 0x1b, 0x2b, 0x47, 57 + 0x8b, 0xed, 0xe0, 0x73, 0x34, 0x38, 0x53, 0x92, 58 + 0x97, 0x2f, 0xfb, 0x51, 0xab, 0x4f, 0x2d, 0x9d, 59 + 0x69, 0x04, 0xa9, 0x5d, 0x33, 0xef, 0xcb, 0x1c, 60 + }, 61 + }, 62 + { 63 + .data_len = 48, 64 + .digest = { 65 + 0xd6, 0x2a, 0x7f, 0x96, 0x04, 0x4d, 0x16, 0xc8, 66 + 0x49, 0xe0, 0x37, 0x33, 0xe3, 0x7b, 0x34, 0x56, 67 + 0x99, 0xc5, 0x78, 0x57, 0x06, 0x02, 0xb4, 0xea, 68 + 0x80, 0xc4, 0xf8, 0x8f, 0x8d, 0x2b, 0xe4, 0x05, 69 + }, 70 + }, 71 + { 72 + .data_len = 49, 73 + .digest = { 74 + 0x8b, 0x58, 0x62, 0xb5, 0x85, 0xf6, 0x83, 0x36, 75 + 0xf5, 0x34, 0xb8, 0xd4, 0xbc, 0x5c, 0x8b, 0x38, 76 + 0xfd, 0x15, 0xcd, 0x44, 0x83, 0x25, 0x71, 0xe1, 77 + 0xd5, 0xe8, 0xa1, 0xa4, 0x36, 0x98, 0x7e, 0x68, 78 + }, 79 + }, 80 + { 81 + .data_len = 63, 82 + .digest = { 83 + 0x7e, 0xeb, 0x06, 0x87, 0xdf, 0x1a, 0xdc, 0xe5, 84 + 0xfb, 0x64, 0xd4, 0xd1, 0x5d, 0x9e, 0x75, 0xc0, 85 + 0xb9, 0xad, 0x55, 0x6c, 0xe6, 0xba, 0x4d, 0x98, 86 + 0x2f, 0xbf, 0x72, 0xad, 0x61, 0x37, 0xf6, 0x11, 87 + }, 88 + }, 89 + { 90 + .data_len = 64, 91 + .digest = { 92 + 0x72, 0xdb, 0x43, 0x16, 0x57, 0x8e, 0x3a, 0x96, 93 + 0xf3, 0x98, 0x19, 0x24, 0x17, 0x3b, 0xe8, 0xad, 94 + 0xa1, 0x9b, 0xa4, 0x1b, 0x74, 0x85, 0x2e, 0x24, 95 + 0x70, 0xea, 0x31, 0x5a, 0x1c, 0xbe, 0x43, 0xb5, 96 + }, 97 + }, 98 + { 99 + .data_len = 65, 100 + .digest = { 101 + 0x32, 0x48, 0xb0, 0xf0, 0x3f, 0xbb, 0xd2, 0xa3, 102 + 0xfd, 0xf6, 0x28, 0x4a, 0x2a, 0xc5, 0xbe, 0x4b, 103 + 0x73, 0x50, 0x63, 0xd6, 0x16, 0x00, 0xef, 0xed, 104 + 0xfe, 0x97, 0x41, 0x29, 0xb2, 0x84, 0xc4, 0xa3, 105 + }, 106 + }, 107 + { 108 + .data_len = 127, 109 + .digest = { 110 + 0x17, 0xda, 0x6b, 0x96, 0x6a, 0xa6, 0xa4, 0xa6, 111 + 0xa6, 0xf3, 0x9d, 0x18, 0x19, 0x8d, 0x98, 0x7c, 112 + 0x66, 0x38, 0xe8, 0x99, 0xe7, 0x0a, 0x50, 0x92, 113 + 0xaf, 0x11, 0x80, 0x05, 0x66, 0xed, 0xab, 0x74, 114 + }, 115 + }, 116 + { 117 + .data_len = 128, 118 + .digest = { 119 + 0x13, 0xd5, 0x8b, 0x22, 0xae, 0x90, 0x7b, 0x67, 120 + 0x87, 0x4e, 0x3c, 0x35, 0x4e, 0x01, 0xf0, 0xb1, 121 + 0xd3, 0xd1, 0x67, 0xbb, 0x43, 0xdb, 0x7c, 0x75, 122 + 0xa4, 0xc7, 0x64, 0x83, 0x1e, 0x9b, 0x98, 0xad, 123 + }, 124 + }, 125 + { 126 + .data_len = 129, 127 + .digest = { 128 + 0x6f, 0xe0, 0x5d, 0x9d, 0xd5, 0x78, 0x29, 0xfb, 129 + 0xd0, 0x77, 0xd1, 0x8a, 0xf0, 0x80, 0xcb, 0x81, 130 + 0x71, 0x9e, 0x4d, 0x49, 0xde, 0x74, 0x2a, 0x37, 131 + 0xc0, 0xd5, 0xf0, 0xfa, 0x50, 0xe6, 0x23, 0xfe, 132 + }, 133 + }, 134 + { 135 + .data_len = 256, 136 + .digest = { 137 + 0x89, 0xac, 0xf6, 0xe7, 0x5e, 0xba, 0x53, 0xf4, 138 + 0x92, 0x32, 0xd5, 0x64, 0xfb, 0xc4, 0x08, 0xac, 139 + 0x2c, 0x19, 0x6e, 0x63, 0x13, 0x75, 0xd0, 0x60, 140 + 0x54, 0x35, 0x82, 0xc4, 0x6d, 0x03, 0x1a, 0x05, 141 + }, 142 + }, 143 + { 144 + .data_len = 511, 145 + .digest = { 146 + 0x1c, 0xaf, 0x94, 0x7d, 0x9c, 0xce, 0x57, 0x64, 147 + 0xf8, 0xa8, 0x25, 0x45, 0x32, 0x86, 0x2b, 0x04, 148 + 0xb3, 0x2e, 0x67, 0xca, 0x73, 0x04, 0x2f, 0xab, 149 + 0xcc, 0xda, 0x9e, 0x42, 0xa1, 0xaf, 0x83, 0x5a, 150 + }, 151 + }, 152 + { 153 + .data_len = 513, 154 + .digest = { 155 + 0x21, 0xdf, 0xdc, 0x29, 0xd9, 0xfc, 0x7b, 0xe7, 156 + 0x3a, 0xc4, 0xe1, 0x61, 0xc5, 0xb5, 0xe1, 0xee, 157 + 0x7a, 0x9d, 0x0c, 0x66, 0x36, 0x63, 0xe4, 0x12, 158 + 0x62, 0xe2, 0xf5, 0x68, 0x72, 0xfc, 0x1e, 0x18, 159 + }, 160 + }, 161 + { 162 + .data_len = 1000, 163 + .digest = { 164 + 0x6e, 0xc7, 0x2e, 0xac, 0xd0, 0xbb, 0x22, 0xe0, 165 + 0xc2, 0x40, 0xb2, 0xfe, 0x8c, 0xaf, 0x9e, 0xcf, 166 + 0x32, 0x06, 0xc6, 0x45, 0x29, 0xbd, 0xe0, 0x7f, 167 + 0x53, 0x32, 0xc3, 0x2b, 0x2f, 0x68, 0x12, 0xcd, 168 + }, 169 + }, 170 + { 171 + .data_len = 3333, 172 + .digest = { 173 + 0x76, 0xba, 0x52, 0xb5, 0x09, 0xf5, 0x19, 0x09, 174 + 0x70, 0x1c, 0x09, 0x28, 0xb4, 0xaa, 0x98, 0x6a, 175 + 0x79, 0xe7, 0x5e, 0xcd, 0xe8, 0xa4, 0x73, 0x69, 176 + 0x1f, 0xf8, 0x05, 0x0a, 0xb4, 0xfe, 0xf9, 0x63, 177 + }, 178 + }, 179 + { 180 + .data_len = 4096, 181 + .digest = { 182 + 0xf7, 0xad, 0xf9, 0xc8, 0x0e, 0x04, 0x2f, 0xdf, 183 + 0xbe, 0x39, 0x79, 0x07, 0x0d, 0xd8, 0x1b, 0x06, 184 + 0x42, 0x3a, 0x43, 0x93, 0xf6, 0x7c, 0xc4, 0xe5, 185 + 0xc2, 0xd5, 0xd0, 0xa6, 0x35, 0x6c, 0xbd, 0x17, 186 + }, 187 + }, 188 + { 189 + .data_len = 4128, 190 + .digest = { 191 + 0x38, 0xd7, 0xab, 0x7e, 0x08, 0xdc, 0x1e, 0xab, 192 + 0x55, 0xbb, 0x3b, 0x7b, 0x6a, 0x17, 0xcc, 0x79, 193 + 0xa7, 0x02, 0x62, 0x66, 0x9b, 0xca, 0xee, 0xc0, 194 + 0x3d, 0x75, 0x34, 0x2e, 0x55, 0x82, 0x26, 0x3c, 195 + }, 196 + }, 197 + { 198 + .data_len = 4160, 199 + .digest = { 200 + 0xf7, 0xeb, 0x2f, 0x24, 0x98, 0x54, 0x04, 0x5a, 201 + 0x19, 0xe4, 0x12, 0x9d, 0x97, 0xbc, 0x87, 0xa5, 202 + 0x0b, 0x85, 0x29, 0xa1, 0x36, 0x89, 0xc9, 0xba, 203 + 0xa0, 0xe0, 0xac, 0x99, 0x7d, 0xa4, 0x51, 0x9f, 204 + }, 205 + }, 206 + { 207 + .data_len = 4224, 208 + .digest = { 209 + 0x8f, 0xe8, 0xa7, 0x79, 0x02, 0xbb, 0x4a, 0x56, 210 + 0x66, 0x91, 0xef, 0x22, 0xd1, 0x09, 0x26, 0x6c, 211 + 0xa9, 0x13, 0xd7, 0x44, 0xc7, 0x19, 0x9c, 0x0b, 212 + 0xfb, 0x4f, 0xca, 0x72, 0x8f, 0x34, 0xf7, 0x82, 213 + }, 214 + }, 215 + { 216 + .data_len = 16384, 217 + .digest = { 218 + 0xaa, 0x21, 0xbb, 0x25, 0x4b, 0x66, 0x6e, 0x29, 219 + 0x71, 0xc1, 0x44, 0x67, 0x19, 0xed, 0xe6, 0xe6, 220 + 0x61, 0x13, 0xf4, 0xb7, 0x02, 0x94, 0x81, 0x0f, 221 + 0xa7, 0x4d, 0xbb, 0x2c, 0xb8, 0xeb, 0x41, 0x0e, 222 + }, 223 + }, 224 + }; 225 + 226 + static const u8 hash_testvec_consolidated[BLAKE2S_HASH_SIZE] = { 227 + 0x84, 0x21, 0xbb, 0x73, 0x64, 0x47, 0x45, 0xe0, 228 + 0xc1, 0x83, 0x78, 0xf1, 0xea, 0xe5, 0xfd, 0xdb, 229 + 0x01, 0xda, 0xb7, 0x86, 0x70, 0x3b, 0x83, 0xb3, 230 + 0xbc, 0xd9, 0xfd, 0x96, 0xbd, 0x50, 0x06, 0x67, 231 + }; 232 + 233 + static const u8 blake2s_keyed_testvec_consolidated[BLAKE2S_HASH_SIZE] = { 234 + 0xa6, 0xad, 0xcd, 0xb8, 0xd9, 0xdd, 0xc7, 0x70, 235 + 0x07, 0x09, 0x7f, 0x9f, 0x41, 0xa9, 0x70, 0xa4, 236 + 0x1c, 0xca, 0x61, 0xbb, 0x58, 0xb5, 0xb2, 0x1d, 237 + 0xd1, 0x71, 0x16, 0xb0, 0x49, 0x4f, 0x9e, 0x1b, 238 + };
+134
lib/crypto/tests/blake2s_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/blake2s.h> 6 + #include "blake2s-testvecs.h" 7 + 8 + /* 9 + * The following are compatibility functions that present BLAKE2s as an unkeyed 10 + * hash function that produces hashes of fixed length BLAKE2S_HASH_SIZE, so that 11 + * hash-test-template.h can be reused to test it. 12 + */ 13 + 14 + static void blake2s_default(const u8 *data, size_t len, 15 + u8 out[BLAKE2S_HASH_SIZE]) 16 + { 17 + blake2s(out, data, NULL, BLAKE2S_HASH_SIZE, len, 0); 18 + } 19 + 20 + static void blake2s_init_default(struct blake2s_state *state) 21 + { 22 + blake2s_init(state, BLAKE2S_HASH_SIZE); 23 + } 24 + 25 + /* 26 + * Generate the HASH_KUNIT_CASES using hash-test-template.h. These test BLAKE2s 27 + * with a key length of 0 and a hash length of BLAKE2S_HASH_SIZE. 28 + */ 29 + #define HASH blake2s_default 30 + #define HASH_CTX blake2s_state 31 + #define HASH_SIZE BLAKE2S_HASH_SIZE 32 + #define HASH_INIT blake2s_init_default 33 + #define HASH_UPDATE blake2s_update 34 + #define HASH_FINAL blake2s_final 35 + #include "hash-test-template.h" 36 + 37 + /* 38 + * BLAKE2s specific test case which tests all possible combinations of key 39 + * length and hash length. 40 + */ 41 + static void test_blake2s_all_key_and_hash_lens(struct kunit *test) 42 + { 43 + const size_t data_len = 100; 44 + u8 *data = &test_buf[0]; 45 + u8 *key = data + data_len; 46 + u8 *hash = key + BLAKE2S_KEY_SIZE; 47 + struct blake2s_state main_state; 48 + u8 main_hash[BLAKE2S_HASH_SIZE]; 49 + 50 + rand_bytes_seeded_from_len(data, data_len); 51 + blake2s_init(&main_state, BLAKE2S_HASH_SIZE); 52 + for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) { 53 + rand_bytes_seeded_from_len(key, key_len); 54 + for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) { 55 + blake2s(hash, data, key, out_len, data_len, key_len); 56 + blake2s_update(&main_state, hash, out_len); 57 + } 58 + } 59 + blake2s_final(&main_state, main_hash); 60 + KUNIT_ASSERT_MEMEQ(test, main_hash, blake2s_keyed_testvec_consolidated, 61 + BLAKE2S_HASH_SIZE); 62 + } 63 + 64 + /* 65 + * BLAKE2s specific test case which tests using a guarded buffer for all allowed 66 + * key lengths. Also tests both blake2s() and blake2s_init_key(). 67 + */ 68 + static void test_blake2s_with_guarded_key_buf(struct kunit *test) 69 + { 70 + const size_t data_len = 100; 71 + 72 + rand_bytes(test_buf, data_len); 73 + for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) { 74 + u8 key[BLAKE2S_KEY_SIZE]; 75 + u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len]; 76 + u8 hash1[BLAKE2S_HASH_SIZE]; 77 + u8 hash2[BLAKE2S_HASH_SIZE]; 78 + struct blake2s_state state; 79 + 80 + rand_bytes(key, key_len); 81 + memcpy(guarded_key, key, key_len); 82 + 83 + blake2s(hash1, test_buf, key, 84 + BLAKE2S_HASH_SIZE, data_len, key_len); 85 + blake2s(hash2, test_buf, guarded_key, 86 + BLAKE2S_HASH_SIZE, data_len, key_len); 87 + KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE); 88 + 89 + blake2s_init_key(&state, BLAKE2S_HASH_SIZE, 90 + guarded_key, key_len); 91 + blake2s_update(&state, test_buf, data_len); 92 + blake2s_final(&state, hash2); 93 + KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE); 94 + } 95 + } 96 + 97 + /* 98 + * BLAKE2s specific test case which tests using a guarded output buffer for all 99 + * allowed output lengths. 100 + */ 101 + static void test_blake2s_with_guarded_out_buf(struct kunit *test) 102 + { 103 + const size_t data_len = 100; 104 + 105 + rand_bytes(test_buf, data_len); 106 + for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) { 107 + u8 hash[BLAKE2S_HASH_SIZE]; 108 + u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len]; 109 + 110 + blake2s(hash, test_buf, NULL, out_len, data_len, 0); 111 + blake2s(guarded_hash, test_buf, NULL, out_len, data_len, 0); 112 + KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len); 113 + } 114 + } 115 + 116 + static struct kunit_case blake2s_test_cases[] = { 117 + HASH_KUNIT_CASES, 118 + KUNIT_CASE(test_blake2s_all_key_and_hash_lens), 119 + KUNIT_CASE(test_blake2s_with_guarded_key_buf), 120 + KUNIT_CASE(test_blake2s_with_guarded_out_buf), 121 + KUNIT_CASE(benchmark_hash), 122 + {}, 123 + }; 124 + 125 + static struct kunit_suite blake2s_test_suite = { 126 + .name = "blake2s", 127 + .test_cases = blake2s_test_cases, 128 + .suite_init = hash_suite_init, 129 + .suite_exit = hash_suite_exit, 130 + }; 131 + kunit_test_suite(blake2s_test_suite); 132 + 133 + MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2s"); 134 + MODULE_LICENSE("GPL");
+24 -3
scripts/crypto/gen-hash-testvecs.py
··· 84 84 print_bytes('\t\t\t', value, 8) 85 85 print('\t\t},') 86 86 87 + def alg_digest_size_const(alg): 88 + if alg == 'blake2s': 89 + return 'BLAKE2S_HASH_SIZE' 90 + return f'{alg.upper()}_DIGEST_SIZE' 91 + 87 92 def gen_unkeyed_testvecs(alg): 88 93 print('') 89 94 print('static const struct {') 90 95 print('\tsize_t data_len;') 91 - print(f'\tu8 digest[{alg.upper()}_DIGEST_SIZE];') 96 + print(f'\tu8 digest[{alg_digest_size_const(alg)}];') 92 97 print('} hash_testvecs[] = {') 93 98 for data_len in DATA_LENS: 94 99 data = rand_bytes(data_len) ··· 108 103 for data_len in range(len(data) + 1): 109 104 hash_update(ctx, compute_hash(alg, data[:data_len])) 110 105 print_static_u8_array_definition( 111 - f'hash_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]', 106 + f'hash_testvec_consolidated[{alg_digest_size_const(alg)}]', 112 107 hash_final(ctx)) 113 108 114 109 def gen_hmac_testvecs(alg): ··· 123 118 print_static_u8_array_definition( 124 119 f'hmac_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]', 125 120 ctx.digest()) 121 + 122 + BLAKE2S_KEY_SIZE = 32 123 + BLAKE2S_HASH_SIZE = 32 124 + 125 + def gen_additional_blake2s_testvecs(): 126 + hashes = b'' 127 + for key_len in range(BLAKE2S_KEY_SIZE + 1): 128 + for out_len in range(1, BLAKE2S_HASH_SIZE + 1): 129 + h = hashlib.blake2s(digest_size=out_len, key=rand_bytes(key_len)) 130 + h.update(rand_bytes(100)) 131 + hashes += h.digest() 132 + print_static_u8_array_definition( 133 + 'blake2s_keyed_testvec_consolidated[BLAKE2S_HASH_SIZE]', 134 + compute_hash('blake2s', hashes)) 126 135 127 136 def gen_additional_poly1305_testvecs(): 128 137 key = b'\xff' * POLY1305_KEY_SIZE ··· 160 141 print('/* SPDX-License-Identifier: GPL-2.0-or-later */') 161 142 print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */') 162 143 gen_unkeyed_testvecs(alg) 163 - if alg == 'poly1305': 144 + if alg == 'blake2s': 145 + gen_additional_blake2s_testvecs() 146 + elif alg == 'poly1305': 164 147 gen_additional_poly1305_testvecs() 165 148 else: 166 149 gen_hmac_testvecs(alg)