History

lib/crypto: tests: Add additional SHAKE tests

Add the following test cases to cover gaps in the SHAKE testing: - test_shake_all_lens_up_to_4096() - test_shake_multiple_squeezes() - test_shake_with_guarded_bufs() Remove test_shake256_tiling() and test_shake256_tiling2() since they are superseded by test_shake_multiple_squeezes(). It provides better test coverage by using randomized testing. E.g., it's able to generate a zero-length squeeze followed by a nonzero-length squeeze, which the first 7 versions of the SHA-3 patchset handled incorrectly.

Eric Biggers committed 1 year ago

Commit b2210f31 parent 15c64c43 files · +174 −59Browse files

Showing the changes to scripts (1 of 3 files). Show all

Unified · Split

scripts/crypto/gen-hash-testvecs.py+23−4
@@ -111,6 +111,18 @@ def gen_unkeyed_testvecs(alg): · 110 unmodified lines
111111 f'hash_testvec_consolidated[{alg_digest_size_const(alg)}]',
112112 hash_final(ctx))
113113
114+def gen_additional_sha3_testvecs():
115+ max_len = 4096
116+ in_data = rand_bytes(max_len)
117+ for alg in ['shake128', 'shake256']:
118+ ctx = hashlib.new('sha3-256')
119+ for in_len in range(max_len + 1):
120+ out_len = (in_len * 293) % (max_len + 1)
121+ out = hashlib.new(alg, data=in_data[:in_len]).digest(out_len)
122+ ctx.update(out)
123+ print_static_u8_array_definition(f'{alg}_testvec_consolidated[SHA3_256_DIGEST_SIZE]',
124+ ctx.digest())
125+
114126 def gen_hmac_testvecs(alg):
115127 ctx = hmac.new(rand_bytes(32), digestmod=alg)
116128 data = rand_bytes(4096)
@@ -155,19 +167,26 @@ def gen_additional_poly1305_testvecs(): · 38 unmodified lines
155167
156168 if len(sys.argv) != 2:
157169 sys.stderr.write('Usage: gen-hash-testvecs.py ALGORITHM\n')
158− sys.stderr.write('ALGORITHM may be any supported by Python hashlib, or poly1305.\n')
170+ sys.stderr.write('ALGORITHM may be any supported by Python hashlib, or poly1305 or sha3.\n')
159171 sys.stderr.write('Example: gen-hash-testvecs.py sha512\n')
160172 sys.exit(1)
161173
162174 alg = sys.argv[1]
163175 print('/* SPDX-License-Identifier: GPL-2.0-or-later */')
164176 print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */')
165−gen_unkeyed_testvecs(alg)
166177 if alg.startswith('blake2'):
178+ gen_unkeyed_testvecs(alg)
167179 gen_additional_blake2_testvecs(alg)
168180 elif alg == 'poly1305':
181+ gen_unkeyed_testvecs(alg)
169182 gen_additional_poly1305_testvecs()
170−elif alg.startswith('sha3-'):
171− pass # no HMAC
183+elif alg == 'sha3':
184+ print()
185+ print('/* SHA3-256 test vectors */')
186+ gen_unkeyed_testvecs('sha3-256')
187+ print()
188+ print('/* SHAKE test vectors */')
189+ gen_additional_sha3_testvecs()
172190 else:
191+ gen_unkeyed_testvecs(alg)
173192 gen_hmac_testvecs(alg)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t