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