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

Unified · Split

3 files
  1. lib/crypto/tests/sha3-testvecs.h +19 −1
  2. lib/crypto/tests/sha3_kunit.c +132 −54
  3. scripts/crypto/gen-hash-testvecs.py +23 −4
lib/crypto/tests/sha3-testvecs.h+19−1
@@ -1,5 +1,7 @@
1/* SPDX-License-Identifier: GPL-2.0-or-later */1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha3-256 */2/* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha3 */
3
4/* SHA3-256 test vectors */
3 5
4static const struct {6static const struct {
5 size_t data_len;7 size_t data_len;
@@ -229,3 +231,19 @@ static const u8 hash_testvec_consolidated[SHA3_256_DIGEST_SIZE] = { · 223 unmodified lines
229 0x5e, 0xab, 0x9f, 0xb1, 0xe4, 0x23, 0x7c, 0x2c,231 0x5e, 0xab, 0x9f, 0xb1, 0xe4, 0x23, 0x7c, 0x2c,
230 0x80, 0xcf, 0x09, 0x75, 0xf8, 0xe2, 0xfa, 0x30,232 0x80, 0xcf, 0x09, 0x75, 0xf8, 0xe2, 0xfa, 0x30,
231};233};
234
235/* SHAKE test vectors */
236
237static const u8 shake128_testvec_consolidated[SHA3_256_DIGEST_SIZE] = {
238 0x89, 0x88, 0x3a, 0x44, 0xec, 0xfe, 0x3c, 0xeb,
239 0x2f, 0x1c, 0x1d, 0xda, 0x9e, 0x36, 0x64, 0xf0,
240 0x85, 0x4c, 0x49, 0x12, 0x76, 0x5a, 0x4d, 0xe7,
241 0xa8, 0xfd, 0xcd, 0xbe, 0x45, 0xb4, 0x6f, 0xb0,
242};
243
244static const u8 shake256_testvec_consolidated[SHA3_256_DIGEST_SIZE] = {
245 0x5a, 0xfd, 0x66, 0x62, 0x5c, 0x37, 0x2b, 0x41,
246 0x77, 0x1c, 0x01, 0x5d, 0x64, 0x7c, 0x63, 0x7a,
247 0x7c, 0x76, 0x9e, 0xa8, 0xd1, 0xb0, 0x8e, 0x02,
248 0x16, 0x9b, 0xfe, 0x0e, 0xb5, 0xd8, 0x6a, 0xb5,
249};
lib/crypto/tests/sha3_kunit.c+132−54
@@ -247,72 +247,149 @@ static void test_shake256_nist(struct kunit *test) · 246 unmodified lines
247 "SHAKE256 gives wrong output for NIST.1600");247 "SHAKE256 gives wrong output for NIST.1600");
248}248}
249 249
250/*250static void shake(int alg, const u8 *in, size_t in_len, u8 *out, size_t out_len)
251 * Output tiling test of SHAKE256; equal output tiles barring the last. A
252 * series of squeezings of the same context should, if laid end-to-end, match a
253 * single squeezing of the combined size.
254 */
255static void test_shake256_tiling(struct kunit *test)
256{251{
257 struct shake_ctx ctx;252 if (alg == 0)
258 u8 out[8 + SHA3_512_DIGEST_SIZE + 8];253 shake128(in, in_len, out, out_len);
254 else
255 shake256(in, in_len, out, out_len);
256}
259 257
260 for (int tile_size = 1; tile_size < SHAKE256_DEFAULT_SIZE; tile_size++) {258static void shake_init(struct shake_ctx *ctx, int alg)
261 int left = SHAKE256_DEFAULT_SIZE;259{
262 u8 *p = out + 8;260 if (alg == 0)
261 shake128_init(ctx);
262 else
263 shake256_init(ctx);
264}
263 265
264 memset(out, 0, sizeof(out));266/*
265 shake256_init(&ctx);267 * Test each of SHAKE128 and SHAKE256 with all input lengths 0 through 4096, for
266 shake_update(&ctx, test_sha3_sample,268 * both input and output. The input and output lengths cycle through the values
267 sizeof(test_sha3_sample) - 1);269 * together, so we do 4096 tests total. To verify all the SHAKE outputs,
268 while (left > 0) {270 * compute and verify the SHA3-256 digest of all of them concatenated together.
269 int part = umin(tile_size, left);271 */
272static void test_shake_all_lens_up_to_4096(struct kunit *test)
273{
274 struct sha3_ctx main_ctx;
275 const size_t max_len = 4096;
276 u8 *const in = test_buf;
277 u8 *const out = &test_buf[TEST_BUF_LEN - max_len];
278 u8 main_hash[SHA3_256_DIGEST_SIZE];
279
280 KUNIT_ASSERT_LE(test, 2 * max_len, TEST_BUF_LEN);
281
282 rand_bytes_seeded_from_len(in, max_len);
283 for (int alg = 0; alg < 2; alg++) {
284 sha3_256_init(&main_ctx);
285 for (size_t in_len = 0; in_len <= max_len; in_len++) {
286 size_t out_len = (in_len * 293) % (max_len + 1);
287
288 shake(alg, in, in_len, out, out_len);
289 sha3_update(&main_ctx, out, out_len);
290 }
291 sha3_final(&main_ctx, main_hash);
292 if (alg == 0)
293 KUNIT_ASSERT_MEMEQ_MSG(test, main_hash,
294 shake128_testvec_consolidated,
295 sizeof(main_hash),
296 "shake128() gives wrong output");
297 else
298 KUNIT_ASSERT_MEMEQ_MSG(test, main_hash,
299 shake256_testvec_consolidated,
300 sizeof(main_hash),
301 "shake256() gives wrong output");
302 }
303}
270 304
271 shake_squeeze(&ctx, p, part);305/*
272 p += part;306 * Test that a sequence of SHAKE squeezes gives the same output as a single
273 left -= part;307 * squeeze of the same total length.
308 */
309static void test_shake_multiple_squeezes(struct kunit *test)
310{
311 const size_t max_len = 512;
312 u8 *ref_out;
313
314 KUNIT_ASSERT_GE(test, TEST_BUF_LEN, 2 * max_len);
315
316 ref_out = kunit_kzalloc(test, max_len, GFP_KERNEL);
317 KUNIT_ASSERT_NOT_NULL(test, ref_out);
318
319 for (int i = 0; i < 2000; i++) {
320 const int alg = rand32() % 2;
321 const size_t in_len = rand_length(max_len);
322 const size_t out_len = rand_length(max_len);
323 const size_t in_offs = rand_offset(max_len - in_len);
324 const size_t out_offs = rand_offset(max_len - out_len);
325 u8 *const in = &test_buf[in_offs];
326 u8 *const out = &test_buf[out_offs];
327 struct shake_ctx ctx;
328 size_t remaining_len, j, num_parts;
329
330 rand_bytes(in, in_len);
331 rand_bytes(out, out_len);
332
333 /* Compute the output using the one-shot function. */
334 shake(alg, in, in_len, ref_out, out_len);
335
336 /* Compute the output using a random sequence of squeezes. */
337 shake_init(&ctx, alg);
338 shake_update(&ctx, in, in_len);
339 remaining_len = out_len;
340 j = 0;
341 num_parts = 0;
342 while (rand_bool()) {
343 size_t part_len = rand_length(remaining_len);
344
345 shake_squeeze(&ctx, &out[j], part_len);
346 num_parts++;
347 j += part_len;
348 remaining_len -= part_len;
349 }
350 if (remaining_len != 0 || rand_bool()) {
351 shake_squeeze(&ctx, &out[j], remaining_len);
352 num_parts++;
274 }353 }
275 354
276 KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256, sizeof(test_shake256),355 /* Verify that the outputs are the same. */
277 "SHAKE tile %u gives wrong output", tile_size);356 KUNIT_ASSERT_MEMEQ_MSG(
357 test, out, ref_out, out_len,
358 "Multi-squeeze test failed with in_len=%zu in_offs=%zu out_len=%zu out_offs=%zu num_parts=%zu alg=%d",
359 in_len, in_offs, out_len, out_offs, num_parts, alg);
278 }360 }
279}361}
280 362
281/*363/*
282 * Output tiling test of SHAKE256; output tiles getting gradually smaller and364 * Test that SHAKE operations on buffers immediately followed by an unmapped
283 * then cycling round to medium sized ones. A series of squeezings of the same365 * page work as expected. This catches out-of-bounds memory accesses even if
284 * context should, if laid end-to-end, match a single squeezing of the combined366 * they occur in assembly code.
285 * size.
286 */367 */
287static void test_shake256_tiling2(struct kunit *test)368static void test_shake_with_guarded_bufs(struct kunit *test)
288{369{
289 struct shake_ctx ctx;370 const size_t max_len = 512;
290 u8 out[8 + SHA3_512_DIGEST_SIZE + 8];371 u8 *reg_buf;
291 372
292 for (int first_tile_size = 3;373 KUNIT_ASSERT_GE(test, TEST_BUF_LEN, max_len);
293 first_tile_size < SHAKE256_DEFAULT_SIZE;
294 first_tile_size++) {
295 int tile_size = first_tile_size;
296 int left = SHAKE256_DEFAULT_SIZE;
297 u8 *p = out + 8;
298
299 memset(out, 0, sizeof(out));
300 shake256_init(&ctx);
301 shake_update(&ctx, test_sha3_sample,
302 sizeof(test_sha3_sample) - 1);
303 while (left > 0) {
304 int part = umin(tile_size, left);
305
306 shake_squeeze(&ctx, p, part);
307 p += part;
308 left -= part;
309 tile_size--;
310 if (tile_size < 1)
311 tile_size = 5;
312 }
313 374
314 KUNIT_ASSERT_MEMEQ_MSG(test, out, test_shake256, sizeof(test_shake256),375 reg_buf = kunit_kzalloc(test, max_len, GFP_KERNEL);
315 "SHAKE tile %u gives wrong output", tile_size);376 KUNIT_ASSERT_NOT_NULL(test, reg_buf);
377
378 for (int alg = 0; alg < 2; alg++) {
379 for (size_t len = 0; len <= max_len; len++) {
380 u8 *guarded_buf = &test_buf[TEST_BUF_LEN - len];
381
382 rand_bytes(reg_buf, len);
383 memcpy(guarded_buf, reg_buf, len);
384
385 shake(alg, reg_buf, len, reg_buf, len);
386 shake(alg, guarded_buf, len, guarded_buf, len);
387
388 KUNIT_ASSERT_MEMEQ_MSG(
389 test, reg_buf, guarded_buf, len,
390 "Guard page test failed with len=%zu alg=%d",
391 len, alg);
392 }
316 }393 }
317}394}
318 395
@@ -326,8 +403,9 @@ static struct kunit_case sha3_test_cases[] = { · 7 unmodified lines
326 KUNIT_CASE(test_shake256_basic),403 KUNIT_CASE(test_shake256_basic),
327 KUNIT_CASE(test_shake128_nist),404 KUNIT_CASE(test_shake128_nist),
328 KUNIT_CASE(test_shake256_nist),405 KUNIT_CASE(test_shake256_nist),
329 KUNIT_CASE(test_shake256_tiling),406 KUNIT_CASE(test_shake_all_lens_up_to_4096),
330 KUNIT_CASE(test_shake256_tiling2),407 KUNIT_CASE(test_shake_multiple_squeezes),
408 KUNIT_CASE(test_shake_with_guarded_bufs),
331 KUNIT_CASE(benchmark_hash),409 KUNIT_CASE(benchmark_hash),
332 {},410 {},
333};411};
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