lib/crypto/tests/sha3_kunit.c

Source file repositories/reference/linux-study-clean/lib/crypto/tests/sha3_kunit.c

File Facts

System
Linux kernel
Corpus path
lib/crypto/tests/sha3_kunit.c
Extension
.c
Size
13752 bytes
Lines
423
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

while (rand_bool()) {
			size_t part_len = rand_length(remaining_len);

			shake_squeeze(&ctx, &out[j], part_len);
			num_parts++;
			j += part_len;
			remaining_len -= part_len;
		}
		if (remaining_len != 0 || rand_bool()) {
			shake_squeeze(&ctx, &out[j], remaining_len);
			num_parts++;
		}

		/* Verify that the outputs are the same. */
		KUNIT_ASSERT_MEMEQ_MSG(
			test, out, ref_out, out_len,
			"Multi-squeeze test failed with in_len=%zu in_offs=%zu out_len=%zu out_offs=%zu num_parts=%zu alg=%d",
			in_len, in_offs, out_len, out_offs, num_parts, alg);
	}
}

/*
 * Test that SHAKE operations on buffers immediately followed by an unmapped
 * page work as expected.  This catches out-of-bounds memory accesses even if
 * they occur in assembly code.
 */
static void test_shake_with_guarded_bufs(struct kunit *test)
{
	const size_t max_len = 512;
	u8 *reg_buf;

	KUNIT_ASSERT_GE(test, TEST_BUF_LEN, max_len);

	reg_buf = kunit_kzalloc(test, max_len, GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, reg_buf);

	for (int alg = 0; alg < 2; alg++) {
		for (size_t len = 0; len <= max_len; len++) {
			u8 *guarded_buf = &test_buf[TEST_BUF_LEN - len];

			rand_bytes(reg_buf, len);
			memcpy(guarded_buf, reg_buf, len);

			shake(alg, reg_buf, len, reg_buf, len);
			shake(alg, guarded_buf, len, guarded_buf, len);

			KUNIT_ASSERT_MEMEQ_MSG(
				test, reg_buf, guarded_buf, len,
				"Guard page test failed with len=%zu alg=%d",
				len, alg);
		}
	}
}

static struct kunit_case sha3_test_cases[] = {
	HASH_KUNIT_CASES,
	KUNIT_CASE(test_sha3_224_basic),
	KUNIT_CASE(test_sha3_256_basic),
	KUNIT_CASE(test_sha3_384_basic),
	KUNIT_CASE(test_sha3_512_basic),
	KUNIT_CASE(test_shake128_basic),
	KUNIT_CASE(test_shake256_basic),
	KUNIT_CASE(test_shake128_nist),
	KUNIT_CASE(test_shake256_nist),
	KUNIT_CASE(test_shake_all_lens_up_to_4096),
	KUNIT_CASE(test_shake_multiple_squeezes),
	KUNIT_CASE(test_shake_with_guarded_bufs),
	KUNIT_CASE(benchmark_hash),
	{},
};

static struct kunit_suite sha3_test_suite = {
	.name = "sha3",
	.test_cases = sha3_test_cases,
	.suite_init = hash_suite_init,
	.suite_exit = hash_suite_exit,
};
kunit_test_suite(sha3_test_suite);

MODULE_DESCRIPTION("KUnit tests and benchmark for SHA3");
MODULE_LICENSE("GPL");

Annotation

Implementation Notes