mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 06:44:00 -04:00
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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user