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
111 f'hash_testvec_consolidated[{alg_digest_size_const(alg)}]',111 f'hash_testvec_consolidated[{alg_digest_size_const(alg)}]',
112 hash_final(ctx))112 hash_final(ctx))
113 113
114def 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
114def gen_hmac_testvecs(alg):126def gen_hmac_testvecs(alg):
115 ctx = hmac.new(rand_bytes(32), digestmod=alg)127 ctx = hmac.new(rand_bytes(32), digestmod=alg)
116 data = rand_bytes(4096)128 data = rand_bytes(4096)
@@ -155,19 +167,26 @@ def gen_additional_poly1305_testvecs(): · 38 unmodified lines
155 167
156if len(sys.argv) != 2:168if len(sys.argv) != 2:
157 sys.stderr.write('Usage: gen-hash-testvecs.py ALGORITHM\n')169 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')
159 sys.stderr.write('Example: gen-hash-testvecs.py sha512\n')171 sys.stderr.write('Example: gen-hash-testvecs.py sha512\n')
160 sys.exit(1)172 sys.exit(1)
161 173
162alg = sys.argv[1]174alg = sys.argv[1]
163print('/* SPDX-License-Identifier: GPL-2.0-or-later */')175print('/* SPDX-License-Identifier: GPL-2.0-or-later */')
164print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */')176print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */')
165gen_unkeyed_testvecs(alg)
166if alg.startswith('blake2'):177if alg.startswith('blake2'):
178 gen_unkeyed_testvecs(alg)
167 gen_additional_blake2_testvecs(alg)179 gen_additional_blake2_testvecs(alg)
168elif alg == 'poly1305':180elif alg == 'poly1305':
181 gen_unkeyed_testvecs(alg)
169 gen_additional_poly1305_testvecs()182 gen_additional_poly1305_testvecs()
170elif alg.startswith('sha3-'):183elif alg == 'sha3':
171 pass # no HMAC184 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()
172else:190else:
191 gen_unkeyed_testvecs(alg)
173 gen_hmac_testvecs(alg)192 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