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>
This commit is contained in:
Eric Biggers
2025-10-17 21:31:04 -07:00
parent 2dbb6f4a25
commit 6401fd334d
5 changed files with 501 additions and 13 deletions

View File

@@ -85,8 +85,8 @@ def print_c_struct_u8_array_field(name, value):
print('\t\t},')
def alg_digest_size_const(alg):
if alg == 'blake2s':
return 'BLAKE2S_HASH_SIZE'
if alg.startswith('blake2'):
return f'{alg.upper()}_HASH_SIZE'
return f'{alg.upper()}_DIGEST_SIZE'
def gen_unkeyed_testvecs(alg):
@@ -124,19 +124,22 @@ def gen_hmac_testvecs(alg):
f'hmac_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]',
ctx.digest())
BLAKE2S_KEY_SIZE = 32
BLAKE2S_HASH_SIZE = 32
def gen_additional_blake2s_testvecs():
def gen_additional_blake2_testvecs(alg):
if alg == 'blake2s':
(max_key_size, max_hash_size) = (32, 32)
elif alg == 'blake2b':
(max_key_size, max_hash_size) = (64, 64)
else:
raise ValueError(f'Unsupported alg: {alg}')
hashes = b''
for key_len in range(BLAKE2S_KEY_SIZE + 1):
for out_len in range(1, BLAKE2S_HASH_SIZE + 1):
h = hashlib.blake2s(digest_size=out_len, key=rand_bytes(key_len))
for key_len in range(max_key_size + 1):
for out_len in range(1, max_hash_size + 1):
h = hashlib.new(alg, digest_size=out_len, key=rand_bytes(key_len))
h.update(rand_bytes(100))
hashes += h.digest()
print_static_u8_array_definition(
'blake2s_keyed_testvec_consolidated[BLAKE2S_HASH_SIZE]',
compute_hash('blake2s', hashes))
f'{alg}_keyed_testvec_consolidated[{alg_digest_size_const(alg)}]',
compute_hash(alg, hashes))
def gen_additional_poly1305_testvecs():
key = b'\xff' * POLY1305_KEY_SIZE
@@ -160,8 +163,8 @@ alg = sys.argv[1]
print('/* SPDX-License-Identifier: GPL-2.0-or-later */')
print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */')
gen_unkeyed_testvecs(alg)
if alg == 'blake2s':
gen_additional_blake2s_testvecs()
if alg.startswith('blake2'):
gen_additional_blake2_testvecs(alg)
elif alg == 'poly1305':
gen_additional_poly1305_testvecs()
else: