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

lib/crypto: tests: Add KUnit tests for BLAKE2b

Add a KUnit test suite for the BLAKE2b library API, mirroring the
BLAKE2s test suite very closely.

As with the BLAKE2s test suite, a benchmark is included.

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

+501 -13
+9
lib/crypto/tests/Kconfig
··· 1 1 # SPDX-License-Identifier: GPL-2.0-or-later 2 2 3 + config CRYPTO_LIB_BLAKE2B_KUNIT_TEST 4 + tristate "KUnit tests for BLAKE2b" if !KUNIT_ALL_TESTS 5 + depends on KUNIT 6 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 7 + select CRYPTO_LIB_BENCHMARK_VISIBLE 8 + select CRYPTO_LIB_BLAKE2B 9 + help 10 + KUnit tests for the BLAKE2b cryptographic hash function. 11 + 3 12 config CRYPTO_LIB_BLAKE2S_KUNIT_TEST 4 13 tristate "KUnit tests for BLAKE2s" if !KUNIT_ALL_TESTS 5 14 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_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o 3 4 obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o 4 5 obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o 5 6 obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o
+342
lib/crypto/tests/blake2b-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py blake2b */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[BLAKE2B_HASH_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0x78, 0x6a, 0x02, 0xf7, 0x42, 0x01, 0x59, 0x03, 12 + 0xc6, 0xc6, 0xfd, 0x85, 0x25, 0x52, 0xd2, 0x72, 13 + 0x91, 0x2f, 0x47, 0x40, 0xe1, 0x58, 0x47, 0x61, 14 + 0x8a, 0x86, 0xe2, 0x17, 0xf7, 0x1f, 0x54, 0x19, 15 + 0xd2, 0x5e, 0x10, 0x31, 0xaf, 0xee, 0x58, 0x53, 16 + 0x13, 0x89, 0x64, 0x44, 0x93, 0x4e, 0xb0, 0x4b, 17 + 0x90, 0x3a, 0x68, 0x5b, 0x14, 0x48, 0xb7, 0x55, 18 + 0xd5, 0x6f, 0x70, 0x1a, 0xfe, 0x9b, 0xe2, 0xce, 19 + }, 20 + }, 21 + { 22 + .data_len = 1, 23 + .digest = { 24 + 0x6f, 0x2e, 0xcc, 0x83, 0x53, 0xa3, 0x20, 0x16, 25 + 0x5b, 0xda, 0xd0, 0x04, 0xd3, 0xcb, 0xe4, 0x37, 26 + 0x5b, 0xf0, 0x84, 0x36, 0xe1, 0xad, 0x45, 0xcc, 27 + 0x4d, 0x7f, 0x09, 0x68, 0xb2, 0x62, 0x93, 0x7f, 28 + 0x72, 0x32, 0xe8, 0xa7, 0x2f, 0x1f, 0x6f, 0xc6, 29 + 0x14, 0xd6, 0x70, 0xae, 0x0c, 0xf0, 0xf3, 0xce, 30 + 0x64, 0x4d, 0x22, 0xdf, 0xc7, 0xa7, 0xf8, 0xa8, 31 + 0x18, 0x23, 0xd8, 0x6c, 0xaf, 0x65, 0xa2, 0x54, 32 + }, 33 + }, 34 + { 35 + .data_len = 2, 36 + .digest = { 37 + 0x04, 0x13, 0xe2, 0x10, 0xbe, 0x65, 0xde, 0xce, 38 + 0x61, 0xa8, 0xe0, 0xd6, 0x35, 0xb1, 0xb8, 0x88, 39 + 0xd2, 0xea, 0x45, 0x3a, 0xe1, 0x8d, 0x94, 0xb5, 40 + 0x66, 0x06, 0x98, 0x96, 0x39, 0xf8, 0x0e, 0xcb, 41 + 0x34, 0xa6, 0xa8, 0x17, 0xfe, 0x56, 0xbc, 0xa9, 42 + 0x5e, 0x1b, 0xb1, 0xde, 0x3c, 0xc7, 0x78, 0x4f, 43 + 0x39, 0xc6, 0xfc, 0xa8, 0xb3, 0x27, 0x66, 0x3e, 44 + 0x4e, 0xb5, 0x5d, 0x08, 0x89, 0xee, 0xd1, 0xe0, 45 + }, 46 + }, 47 + { 48 + .data_len = 3, 49 + .digest = { 50 + 0x2b, 0x4a, 0xa3, 0x4e, 0x2b, 0x7a, 0x47, 0x20, 51 + 0x30, 0x5b, 0x09, 0x17, 0x3a, 0xf4, 0xcc, 0xf0, 52 + 0xf7, 0x7b, 0x97, 0x68, 0x98, 0x9f, 0x4f, 0x09, 53 + 0x46, 0x25, 0xe7, 0xd6, 0x53, 0x6b, 0xf9, 0x68, 54 + 0x48, 0x12, 0x44, 0x8c, 0x9a, 0xc8, 0xd4, 0x42, 55 + 0xeb, 0x2c, 0x5f, 0x41, 0xba, 0x17, 0xd0, 0xc3, 56 + 0xad, 0xfd, 0xfb, 0x42, 0x33, 0xcb, 0x08, 0x5d, 57 + 0xd2, 0x5c, 0x3d, 0xde, 0x87, 0x4d, 0xd6, 0xe4, 58 + }, 59 + }, 60 + { 61 + .data_len = 16, 62 + .digest = { 63 + 0xbf, 0x40, 0xf2, 0x38, 0x44, 0x8e, 0x24, 0x5e, 64 + 0xbc, 0x67, 0xbb, 0xf0, 0x10, 0x9a, 0x79, 0xbb, 65 + 0x36, 0x55, 0xce, 0xd2, 0xba, 0x04, 0x0d, 0xe8, 66 + 0x30, 0x29, 0x5c, 0x2a, 0xa6, 0x3a, 0x4f, 0x37, 67 + 0xac, 0x5f, 0xd4, 0x13, 0xa2, 0xf4, 0xfe, 0x80, 68 + 0x61, 0xd7, 0x58, 0x66, 0x0c, 0x7f, 0xa2, 0x56, 69 + 0x6b, 0x52, 0x7c, 0x22, 0x73, 0x7f, 0x17, 0xaa, 70 + 0x91, 0x5a, 0x22, 0x06, 0xd9, 0x00, 0x48, 0x12, 71 + }, 72 + }, 73 + { 74 + .data_len = 32, 75 + .digest = { 76 + 0x41, 0x04, 0x65, 0x93, 0x81, 0x9a, 0x20, 0x0a, 77 + 0x00, 0x60, 0x00, 0x64, 0x4c, 0x04, 0x3d, 0xe0, 78 + 0x6b, 0x17, 0x0c, 0xe1, 0x0e, 0x28, 0x8b, 0xa0, 79 + 0x76, 0xd2, 0x79, 0xb0, 0x33, 0x60, 0x61, 0x27, 80 + 0xf2, 0x64, 0xf1, 0x8a, 0xe5, 0x3e, 0xaa, 0x37, 81 + 0x60, 0xad, 0x2d, 0x75, 0x13, 0xae, 0xd8, 0x9e, 82 + 0xec, 0xe0, 0xe4, 0x40, 0x2f, 0x59, 0x44, 0xb0, 83 + 0x66, 0x7a, 0x68, 0x38, 0xce, 0x21, 0x99, 0x2a, 84 + }, 85 + }, 86 + { 87 + .data_len = 48, 88 + .digest = { 89 + 0x19, 0x6f, 0x9d, 0xc7, 0x87, 0x12, 0x5c, 0xa3, 90 + 0xe2, 0xd3, 0xf1, 0x82, 0xec, 0xf3, 0x55, 0x9c, 91 + 0x86, 0xd1, 0x6d, 0xde, 0xcf, 0x5b, 0xec, 0x4c, 92 + 0x43, 0x25, 0x85, 0x90, 0xef, 0xe8, 0xe3, 0x5f, 93 + 0x2c, 0x3a, 0x84, 0x07, 0xb8, 0x55, 0xfd, 0x5e, 94 + 0xa4, 0x45, 0xf2, 0xac, 0xe4, 0xbd, 0xc7, 0x96, 95 + 0x80, 0x59, 0x3e, 0xc9, 0xb1, 0x60, 0xb1, 0x2b, 96 + 0x17, 0x49, 0x7d, 0x3e, 0x7d, 0x4d, 0x70, 0x24, 97 + }, 98 + }, 99 + { 100 + .data_len = 49, 101 + .digest = { 102 + 0x73, 0x72, 0xd5, 0x0a, 0x97, 0xb4, 0x7d, 0xdb, 103 + 0x05, 0x14, 0x8e, 0x40, 0xc2, 0x9a, 0x8a, 0x74, 104 + 0x4b, 0xda, 0x7e, 0xfc, 0x97, 0x57, 0x23, 0x39, 105 + 0xdc, 0x57, 0x09, 0x13, 0x24, 0xfc, 0xf3, 0x23, 106 + 0x55, 0x48, 0xdd, 0xe5, 0x07, 0x9a, 0x6f, 0x7b, 107 + 0x62, 0xea, 0x4d, 0x79, 0xb4, 0xb9, 0xc5, 0x86, 108 + 0xc0, 0x34, 0xd6, 0xd2, 0x6c, 0xc3, 0x94, 0xfb, 109 + 0x34, 0xd6, 0x62, 0xae, 0xb8, 0x99, 0xf1, 0x38, 110 + }, 111 + }, 112 + { 113 + .data_len = 63, 114 + .digest = { 115 + 0x42, 0x3a, 0xe3, 0xa2, 0xae, 0x5a, 0x28, 0xce, 116 + 0xf1, 0x3c, 0x97, 0xc2, 0x34, 0xf6, 0xb5, 0x1e, 117 + 0xfc, 0x31, 0xb4, 0x04, 0x61, 0xb7, 0x54, 0x0b, 118 + 0x0d, 0x1a, 0x22, 0x9c, 0x04, 0x67, 0x5c, 0x4c, 119 + 0x75, 0x1b, 0x10, 0x0b, 0x99, 0xe2, 0xb1, 0x5e, 120 + 0x5d, 0x4b, 0x7a, 0xe6, 0xf6, 0xb5, 0x62, 0xee, 121 + 0x2d, 0x44, 0x57, 0xb2, 0x96, 0x73, 0x5e, 0xb9, 122 + 0x6a, 0xb2, 0xb3, 0x16, 0xa3, 0xd9, 0x6a, 0x60, 123 + }, 124 + }, 125 + { 126 + .data_len = 64, 127 + .digest = { 128 + 0x50, 0xb9, 0xbe, 0xb2, 0x69, 0x07, 0x45, 0x5b, 129 + 0x59, 0xde, 0x8d, 0xbf, 0x08, 0xdc, 0x2e, 0x7f, 130 + 0x93, 0x29, 0xc1, 0x91, 0xe8, 0x74, 0x03, 0x89, 131 + 0x20, 0xfb, 0xb2, 0x4b, 0xe8, 0x68, 0x6f, 0xe1, 132 + 0xb4, 0x30, 0xbe, 0x11, 0x3c, 0x43, 0x19, 0x66, 133 + 0x72, 0x78, 0xb7, 0xf4, 0xe9, 0x09, 0x18, 0x4e, 134 + 0xae, 0x4a, 0x24, 0xe0, 0x6f, 0x44, 0x02, 0xe3, 135 + 0xfd, 0xda, 0xb3, 0x3e, 0x3c, 0x6d, 0x54, 0x2e, 136 + }, 137 + }, 138 + { 139 + .data_len = 65, 140 + .digest = { 141 + 0xd6, 0xf2, 0xa9, 0x61, 0x3f, 0xce, 0x2a, 0x68, 142 + 0x19, 0x86, 0xff, 0xd1, 0xee, 0x89, 0x3b, 0xa4, 143 + 0x10, 0x9a, 0x91, 0x50, 0x35, 0x48, 0x9e, 0xf5, 144 + 0x9c, 0x95, 0xe0, 0xfb, 0x92, 0x0f, 0xa8, 0xf7, 145 + 0x6c, 0x43, 0x85, 0xf1, 0x6e, 0x11, 0x4e, 0x67, 146 + 0x78, 0xd7, 0x53, 0x25, 0x0c, 0xf8, 0xce, 0x38, 147 + 0x74, 0x08, 0xb0, 0x3c, 0x53, 0x20, 0x4d, 0xc4, 148 + 0x9a, 0xf5, 0x78, 0xe8, 0x41, 0x8f, 0xed, 0x1f, 149 + }, 150 + }, 151 + { 152 + .data_len = 127, 153 + .digest = { 154 + 0xe8, 0xb2, 0xc5, 0xa7, 0xf5, 0xfa, 0xee, 0xa0, 155 + 0x57, 0xba, 0x58, 0xf9, 0x0a, 0xf2, 0x64, 0x16, 156 + 0xa8, 0xa6, 0x03, 0x85, 0x3b, 0xb8, 0x6f, 0xca, 157 + 0x76, 0xc3, 0xa1, 0x2b, 0xec, 0xef, 0xc4, 0x66, 158 + 0x11, 0xdf, 0x03, 0x85, 0x9d, 0x0c, 0x37, 0x7b, 159 + 0xa9, 0x7b, 0x44, 0xfb, 0x11, 0x8f, 0x3f, 0x71, 160 + 0xcd, 0x81, 0x43, 0x2e, 0x71, 0x5c, 0x54, 0x9f, 161 + 0xca, 0x0f, 0x01, 0x91, 0xca, 0xaa, 0x93, 0xe9, 162 + }, 163 + }, 164 + { 165 + .data_len = 128, 166 + .digest = { 167 + 0x05, 0x8e, 0x9d, 0xdc, 0xe9, 0x36, 0x3e, 0x73, 168 + 0x63, 0x59, 0x69, 0x81, 0x0b, 0x8c, 0xc7, 0x9e, 169 + 0xcc, 0xe7, 0x9c, 0x19, 0x54, 0xa7, 0x2f, 0x86, 170 + 0xb5, 0xea, 0xae, 0x6d, 0xfe, 0x4e, 0x6e, 0x83, 171 + 0x8d, 0x1a, 0x1c, 0x70, 0x3f, 0x34, 0xa1, 0x04, 172 + 0x59, 0xd1, 0xbb, 0xaa, 0x58, 0xf7, 0xce, 0xfb, 173 + 0x86, 0x66, 0x22, 0xfc, 0x78, 0x74, 0x6e, 0x85, 174 + 0xf1, 0x59, 0x7d, 0x9e, 0x1c, 0x3b, 0xc6, 0x65, 175 + }, 176 + }, 177 + { 178 + .data_len = 129, 179 + .digest = { 180 + 0x6b, 0x1f, 0x7c, 0x9a, 0x65, 0x7f, 0x09, 0x61, 181 + 0xe5, 0x04, 0x9a, 0xf1, 0x4b, 0x36, 0x8e, 0x41, 182 + 0x86, 0xcf, 0x86, 0x19, 0xd8, 0xc9, 0x34, 0x70, 183 + 0x67, 0xd1, 0x03, 0x72, 0x12, 0xf7, 0x27, 0x92, 184 + 0x2e, 0x3d, 0x2b, 0x54, 0x9a, 0x48, 0xa4, 0xc2, 185 + 0x61, 0xea, 0x6a, 0xe8, 0xdd, 0x07, 0x41, 0x85, 186 + 0x58, 0x6d, 0xcd, 0x12, 0x0d, 0xbc, 0xb1, 0x23, 187 + 0xb2, 0xdb, 0x24, 0x1f, 0xc4, 0xa7, 0xae, 0xda, 188 + }, 189 + }, 190 + { 191 + .data_len = 256, 192 + .digest = { 193 + 0x50, 0xd8, 0xdc, 0xb2, 0x50, 0x24, 0x7a, 0x49, 194 + 0xb1, 0x00, 0x73, 0x16, 0x1f, 0xce, 0xf9, 0xe8, 195 + 0x77, 0x0a, 0x27, 0x74, 0xc7, 0xeb, 0xf0, 0x62, 196 + 0xb9, 0xf3, 0x24, 0xa6, 0x03, 0x18, 0x40, 0xde, 197 + 0x9b, 0x1d, 0xa8, 0xd0, 0xbf, 0x66, 0xa3, 0xc1, 198 + 0x31, 0x04, 0x95, 0xc7, 0xc3, 0xb7, 0x11, 0xe2, 199 + 0x1e, 0x31, 0x49, 0x98, 0x06, 0xab, 0xf0, 0xe6, 200 + 0x5c, 0xac, 0x88, 0x28, 0x0b, 0x3d, 0xb2, 0xc2, 201 + }, 202 + }, 203 + { 204 + .data_len = 511, 205 + .digest = { 206 + 0xd4, 0x2b, 0x6b, 0x9e, 0xfc, 0x44, 0xc0, 0x90, 207 + 0x64, 0x77, 0x5d, 0xf3, 0x44, 0xb6, 0x92, 0x8f, 208 + 0x80, 0xe2, 0xe4, 0x9b, 0xaf, 0x49, 0x04, 0xea, 209 + 0x29, 0xf7, 0x4a, 0x33, 0x3f, 0xc7, 0x3b, 0xab, 210 + 0xa1, 0x71, 0x7f, 0xa2, 0x8e, 0x03, 0xa0, 0xd6, 211 + 0xa7, 0xcd, 0xe0, 0xf8, 0xd7, 0x3b, 0xa4, 0x0d, 212 + 0x84, 0x79, 0x12, 0x72, 0x3f, 0x8e, 0x48, 0x35, 213 + 0x76, 0x4f, 0x56, 0xe9, 0x21, 0x40, 0x19, 0xbe, 214 + }, 215 + }, 216 + { 217 + .data_len = 513, 218 + .digest = { 219 + 0x84, 0xd4, 0xd8, 0x6c, 0x60, 0x3d, 0x6e, 0xfd, 220 + 0x84, 0xb7, 0xdf, 0xba, 0x13, 0x5e, 0x07, 0x94, 221 + 0x5b, 0x6b, 0x62, 0x1d, 0x82, 0x02, 0xa7, 0xb3, 222 + 0x21, 0xdf, 0x42, 0x20, 0x85, 0xa8, 0x6f, 0x30, 223 + 0xf7, 0x03, 0xba, 0x66, 0x0e, 0xa6, 0x42, 0x21, 224 + 0x37, 0xe8, 0xed, 0x5b, 0x22, 0xf5, 0x4e, 0xa5, 225 + 0xe5, 0x80, 0x1b, 0x47, 0xf0, 0x49, 0xb3, 0xe5, 226 + 0x6e, 0xd9, 0xd9, 0x95, 0x3d, 0x2e, 0x42, 0x13, 227 + }, 228 + }, 229 + { 230 + .data_len = 1000, 231 + .digest = { 232 + 0x71, 0x17, 0xab, 0x93, 0xfe, 0x3b, 0xa4, 0xe6, 233 + 0xcb, 0xb0, 0xea, 0x95, 0xe7, 0x1a, 0x01, 0xc0, 234 + 0x12, 0x33, 0xfe, 0xcc, 0x79, 0x15, 0xae, 0x56, 235 + 0xd2, 0x70, 0x44, 0x60, 0x54, 0x42, 0xa8, 0x69, 236 + 0x7e, 0xc3, 0x90, 0xa0, 0x0c, 0x63, 0x39, 0xff, 237 + 0x55, 0x53, 0xb8, 0x46, 0xef, 0x06, 0xcb, 0xba, 238 + 0x73, 0xf4, 0x76, 0x22, 0xf1, 0x60, 0x98, 0xbc, 239 + 0xbf, 0x76, 0x95, 0x85, 0x13, 0x1d, 0x11, 0x3b, 240 + }, 241 + }, 242 + { 243 + .data_len = 3333, 244 + .digest = { 245 + 0x3a, 0xaa, 0x85, 0xa0, 0x8c, 0x8e, 0xe1, 0x9c, 246 + 0x9b, 0x43, 0x72, 0x7f, 0x40, 0x88, 0x3b, 0xd1, 247 + 0xc4, 0xd8, 0x2b, 0x69, 0xa6, 0x74, 0x47, 0x69, 248 + 0x5f, 0x7d, 0xab, 0x75, 0xa9, 0xf9, 0x88, 0x54, 249 + 0xce, 0x57, 0xcc, 0x9d, 0xac, 0x13, 0x91, 0xdb, 250 + 0x6d, 0x5c, 0xd8, 0xf4, 0x35, 0xc9, 0x30, 0xf0, 251 + 0x4b, 0x91, 0x25, 0xab, 0x92, 0xa8, 0xc8, 0x6f, 252 + 0xa0, 0xeb, 0x71, 0x56, 0x95, 0xab, 0xfd, 0xd7, 253 + }, 254 + }, 255 + { 256 + .data_len = 4096, 257 + .digest = { 258 + 0xe1, 0xe9, 0xbe, 0x6c, 0x96, 0xe2, 0xe8, 0xa6, 259 + 0x53, 0xcd, 0x79, 0x77, 0x57, 0x51, 0x2f, 0xb2, 260 + 0x9f, 0xfc, 0x09, 0xaa, 0x2c, 0xbc, 0x6c, 0x5f, 261 + 0xb0, 0xf2, 0x12, 0x39, 0x54, 0xd7, 0x27, 0xf8, 262 + 0x33, 0x5d, 0xd4, 0x8a, 0xca, 0xd8, 0x2e, 0xbb, 263 + 0x02, 0x82, 0xca, 0x1b, 0x54, 0xfa, 0xd6, 0xf4, 264 + 0x49, 0x63, 0xfc, 0xc8, 0x73, 0xd4, 0x26, 0x8d, 265 + 0x4f, 0x1c, 0x56, 0xa7, 0xf4, 0x58, 0x6f, 0x51, 266 + }, 267 + }, 268 + { 269 + .data_len = 4128, 270 + .digest = { 271 + 0xf2, 0xf6, 0xe1, 0x16, 0x98, 0x69, 0x74, 0x5f, 272 + 0x6c, 0xc4, 0x9d, 0x34, 0xa2, 0x84, 0x5d, 0x47, 273 + 0xac, 0x39, 0xe0, 0x14, 0x2d, 0x78, 0xfa, 0x27, 274 + 0xd5, 0x18, 0xaf, 0x26, 0x89, 0xa4, 0x69, 0xd3, 275 + 0x56, 0xde, 0xfe, 0x4b, 0x9f, 0x0c, 0x9d, 0x5a, 276 + 0x9a, 0x73, 0x3e, 0x3c, 0x76, 0x4b, 0x96, 0xca, 277 + 0x49, 0xda, 0x05, 0x8c, 0x53, 0xbb, 0x85, 0x89, 278 + 0x60, 0xc7, 0xe0, 0xb3, 0x51, 0x18, 0xd2, 0xd2, 279 + }, 280 + }, 281 + { 282 + .data_len = 4160, 283 + .digest = { 284 + 0xfc, 0x5c, 0xcf, 0xbf, 0x29, 0xe3, 0x01, 0xef, 285 + 0x4b, 0x40, 0x70, 0x01, 0xca, 0x4d, 0x46, 0xce, 286 + 0xa9, 0x95, 0x5d, 0xb4, 0xf1, 0x79, 0x29, 0xdb, 287 + 0xac, 0x32, 0x3d, 0xd9, 0x60, 0x9e, 0x6b, 0xb8, 288 + 0x28, 0x62, 0xb7, 0x4a, 0xbb, 0x33, 0xb9, 0xd0, 289 + 0x83, 0xe0, 0xd7, 0x5a, 0x2d, 0x01, 0x4c, 0x61, 290 + 0x9e, 0x7d, 0x2d, 0x2d, 0x60, 0x29, 0x5e, 0x60, 291 + 0x10, 0xb7, 0x41, 0x00, 0x3f, 0xe5, 0xf7, 0x52, 292 + }, 293 + }, 294 + { 295 + .data_len = 4224, 296 + .digest = { 297 + 0xf8, 0xe5, 0x4b, 0xe5, 0x89, 0xf9, 0x1b, 0x43, 298 + 0xbb, 0x65, 0x3d, 0xa0, 0xb4, 0xdc, 0x04, 0x26, 299 + 0x68, 0x15, 0xae, 0x4d, 0xd6, 0x03, 0xb7, 0x27, 300 + 0x06, 0x8c, 0x2a, 0x82, 0x51, 0x96, 0xbf, 0x83, 301 + 0x38, 0x96, 0x21, 0x8a, 0xd9, 0xf9, 0x4e, 0x38, 302 + 0xc6, 0xb3, 0xbd, 0xfe, 0xd3, 0x49, 0x90, 0xbc, 303 + 0xa1, 0x77, 0xd0, 0xa0, 0x3c, 0x2b, 0x4e, 0x10, 304 + 0x34, 0xc3, 0x17, 0x85, 0x3d, 0xec, 0xa8, 0x05, 305 + }, 306 + }, 307 + { 308 + .data_len = 16384, 309 + .digest = { 310 + 0x38, 0x56, 0xaf, 0x83, 0x68, 0x9c, 0xba, 0xe3, 311 + 0xec, 0x51, 0xf5, 0xf4, 0x93, 0x48, 0x1d, 0xe6, 312 + 0xad, 0xa8, 0x8c, 0x70, 0x2a, 0xd9, 0xaa, 0x43, 313 + 0x04, 0x40, 0x95, 0xc1, 0xe6, 0x8a, 0xf5, 0x01, 314 + 0x6b, 0x79, 0xd9, 0xb4, 0xd0, 0x1d, 0x93, 0x26, 315 + 0xfe, 0xf5, 0x07, 0x57, 0xda, 0x08, 0x0a, 0x82, 316 + 0xc9, 0x17, 0x13, 0x5b, 0x9e, 0x11, 0x96, 0xa5, 317 + 0xd0, 0x92, 0xcd, 0xf1, 0xa3, 0x5b, 0x43, 0x21, 318 + }, 319 + }, 320 + }; 321 + 322 + static const u8 hash_testvec_consolidated[BLAKE2B_HASH_SIZE] = { 323 + 0xa4, 0xf8, 0xf6, 0xa1, 0x36, 0x89, 0xc0, 0x2a, 324 + 0xc3, 0x42, 0x32, 0x71, 0xe5, 0xea, 0x14, 0x77, 325 + 0xf3, 0x99, 0x91, 0x87, 0x49, 0xc2, 0x8d, 0xa5, 326 + 0x2f, 0xed, 0x01, 0x35, 0x39, 0x64, 0x09, 0x25, 327 + 0xe3, 0xa8, 0x50, 0x97, 0x35, 0x8b, 0xf5, 0x19, 328 + 0x1e, 0xd5, 0x9f, 0x03, 0x0b, 0x65, 0x55, 0x0e, 329 + 0xa0, 0xb7, 0xda, 0x18, 0x7b, 0x7f, 0x88, 0x55, 330 + 0x1f, 0xdb, 0x82, 0x6b, 0x98, 0x90, 0x1c, 0xdd, 331 + }; 332 + 333 + static const u8 blake2b_keyed_testvec_consolidated[BLAKE2B_HASH_SIZE] = { 334 + 0x2b, 0x89, 0x36, 0x3a, 0x36, 0xe4, 0x18, 0x38, 335 + 0xc4, 0x5b, 0x5c, 0xa5, 0x9a, 0xed, 0xf2, 0xee, 336 + 0x5a, 0xb6, 0x82, 0x6c, 0x63, 0xf2, 0x29, 0x57, 337 + 0xc7, 0xd5, 0x32, 0x27, 0xba, 0x88, 0xb1, 0xab, 338 + 0xf2, 0x2a, 0xc1, 0xea, 0xf3, 0x91, 0x89, 0x66, 339 + 0x47, 0x1e, 0x5b, 0xc6, 0x98, 0x12, 0xe9, 0x25, 340 + 0xbf, 0x72, 0xd2, 0x3f, 0x88, 0x97, 0x17, 0x51, 341 + 0xed, 0x96, 0xfb, 0xe9, 0xca, 0x52, 0x42, 0xc9, 342 + };
+133
lib/crypto/tests/blake2b_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/blake2b.h> 6 + #include "blake2b-testvecs.h" 7 + 8 + /* 9 + * The following are compatibility functions that present BLAKE2b as an unkeyed 10 + * hash function that produces hashes of fixed length BLAKE2B_HASH_SIZE, so that 11 + * hash-test-template.h can be reused to test it. 12 + */ 13 + 14 + static void blake2b_default(const u8 *data, size_t len, 15 + u8 out[BLAKE2B_HASH_SIZE]) 16 + { 17 + blake2b(NULL, 0, data, len, out, BLAKE2B_HASH_SIZE); 18 + } 19 + 20 + static void blake2b_init_default(struct blake2b_ctx *ctx) 21 + { 22 + blake2b_init(ctx, BLAKE2B_HASH_SIZE); 23 + } 24 + 25 + /* 26 + * Generate the HASH_KUNIT_CASES using hash-test-template.h. These test BLAKE2b 27 + * with a key length of 0 and a hash length of BLAKE2B_HASH_SIZE. 28 + */ 29 + #define HASH blake2b_default 30 + #define HASH_CTX blake2b_ctx 31 + #define HASH_SIZE BLAKE2B_HASH_SIZE 32 + #define HASH_INIT blake2b_init_default 33 + #define HASH_UPDATE blake2b_update 34 + #define HASH_FINAL blake2b_final 35 + #include "hash-test-template.h" 36 + 37 + /* 38 + * BLAKE2b specific test case which tests all possible combinations of key 39 + * length and hash length. 40 + */ 41 + static void test_blake2b_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 + BLAKE2B_KEY_SIZE; 47 + struct blake2b_ctx main_ctx; 48 + u8 main_hash[BLAKE2B_HASH_SIZE]; 49 + 50 + rand_bytes_seeded_from_len(data, data_len); 51 + blake2b_init(&main_ctx, BLAKE2B_HASH_SIZE); 52 + for (int key_len = 0; key_len <= BLAKE2B_KEY_SIZE; key_len++) { 53 + rand_bytes_seeded_from_len(key, key_len); 54 + for (int out_len = 1; out_len <= BLAKE2B_HASH_SIZE; out_len++) { 55 + blake2b(key, key_len, data, data_len, hash, out_len); 56 + blake2b_update(&main_ctx, hash, out_len); 57 + } 58 + } 59 + blake2b_final(&main_ctx, main_hash); 60 + KUNIT_ASSERT_MEMEQ(test, main_hash, blake2b_keyed_testvec_consolidated, 61 + BLAKE2B_HASH_SIZE); 62 + } 63 + 64 + /* 65 + * BLAKE2b specific test case which tests using a guarded buffer for all allowed 66 + * key lengths. Also tests both blake2b() and blake2b_init_key(). 67 + */ 68 + static void test_blake2b_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 <= BLAKE2B_KEY_SIZE; key_len++) { 74 + u8 key[BLAKE2B_KEY_SIZE]; 75 + u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len]; 76 + u8 hash1[BLAKE2B_HASH_SIZE]; 77 + u8 hash2[BLAKE2B_HASH_SIZE]; 78 + struct blake2b_ctx ctx; 79 + 80 + rand_bytes(key, key_len); 81 + memcpy(guarded_key, key, key_len); 82 + 83 + blake2b(key, key_len, test_buf, data_len, 84 + hash1, BLAKE2B_HASH_SIZE); 85 + blake2b(guarded_key, key_len, test_buf, data_len, 86 + hash2, BLAKE2B_HASH_SIZE); 87 + KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2B_HASH_SIZE); 88 + 89 + blake2b_init_key(&ctx, BLAKE2B_HASH_SIZE, guarded_key, key_len); 90 + blake2b_update(&ctx, test_buf, data_len); 91 + blake2b_final(&ctx, hash2); 92 + KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2B_HASH_SIZE); 93 + } 94 + } 95 + 96 + /* 97 + * BLAKE2b specific test case which tests using a guarded output buffer for all 98 + * allowed output lengths. 99 + */ 100 + static void test_blake2b_with_guarded_out_buf(struct kunit *test) 101 + { 102 + const size_t data_len = 100; 103 + 104 + rand_bytes(test_buf, data_len); 105 + for (int out_len = 1; out_len <= BLAKE2B_HASH_SIZE; out_len++) { 106 + u8 hash[BLAKE2B_HASH_SIZE]; 107 + u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len]; 108 + 109 + blake2b(NULL, 0, test_buf, data_len, hash, out_len); 110 + blake2b(NULL, 0, test_buf, data_len, guarded_hash, out_len); 111 + KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len); 112 + } 113 + } 114 + 115 + static struct kunit_case blake2b_test_cases[] = { 116 + HASH_KUNIT_CASES, 117 + KUNIT_CASE(test_blake2b_all_key_and_hash_lens), 118 + KUNIT_CASE(test_blake2b_with_guarded_key_buf), 119 + KUNIT_CASE(test_blake2b_with_guarded_out_buf), 120 + KUNIT_CASE(benchmark_hash), 121 + {}, 122 + }; 123 + 124 + static struct kunit_suite blake2b_test_suite = { 125 + .name = "blake2b", 126 + .test_cases = blake2b_test_cases, 127 + .suite_init = hash_suite_init, 128 + .suite_exit = hash_suite_exit, 129 + }; 130 + kunit_test_suite(blake2b_test_suite); 131 + 132 + MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2b"); 133 + MODULE_LICENSE("GPL");
+16 -13
scripts/crypto/gen-hash-testvecs.py
··· 85 85 print('\t\t},') 86 86 87 87 def alg_digest_size_const(alg): 88 - if alg == 'blake2s': 89 - return 'BLAKE2S_HASH_SIZE' 88 + if alg.startswith('blake2'): 89 + return f'{alg.upper()}_HASH_SIZE' 90 90 return f'{alg.upper()}_DIGEST_SIZE' 91 91 92 92 def gen_unkeyed_testvecs(alg): ··· 124 124 f'hmac_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]', 125 125 ctx.digest()) 126 126 127 - BLAKE2S_KEY_SIZE = 32 128 - BLAKE2S_HASH_SIZE = 32 129 - 130 - def gen_additional_blake2s_testvecs(): 127 + def gen_additional_blake2_testvecs(alg): 128 + if alg == 'blake2s': 129 + (max_key_size, max_hash_size) = (32, 32) 130 + elif alg == 'blake2b': 131 + (max_key_size, max_hash_size) = (64, 64) 132 + else: 133 + raise ValueError(f'Unsupported alg: {alg}') 131 134 hashes = b'' 132 - for key_len in range(BLAKE2S_KEY_SIZE + 1): 133 - for out_len in range(1, BLAKE2S_HASH_SIZE + 1): 134 - h = hashlib.blake2s(digest_size=out_len, key=rand_bytes(key_len)) 135 + for key_len in range(max_key_size + 1): 136 + for out_len in range(1, max_hash_size + 1): 137 + h = hashlib.new(alg, digest_size=out_len, key=rand_bytes(key_len)) 135 138 h.update(rand_bytes(100)) 136 139 hashes += h.digest() 137 140 print_static_u8_array_definition( 138 - 'blake2s_keyed_testvec_consolidated[BLAKE2S_HASH_SIZE]', 139 - compute_hash('blake2s', hashes)) 141 + f'{alg}_keyed_testvec_consolidated[{alg_digest_size_const(alg)}]', 142 + compute_hash(alg, hashes)) 140 143 141 144 def gen_additional_poly1305_testvecs(): 142 145 key = b'\xff' * POLY1305_KEY_SIZE ··· 163 160 print('/* SPDX-License-Identifier: GPL-2.0-or-later */') 164 161 print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */') 165 162 gen_unkeyed_testvecs(alg) 166 - if alg == 'blake2s': 167 - gen_additional_blake2s_testvecs() 163 + if alg.startswith('blake2'): 164 + gen_additional_blake2_testvecs(alg) 168 165 elif alg == 'poly1305': 169 166 gen_additional_poly1305_testvecs() 170 167 else: