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.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
crypto/sha3.hsha3-testvecs.hhash-test-template.h
Detected Declarations
function test_sha3_224_basicfunction test_sha3_256_basicfunction test_sha3_384_basicfunction test_sha3_512_basicfunction test_shake128_basicfunction test_shake256_basicfunction test_shake128_nistfunction test_shake256_nistfunction shakefunction shake_initfunction test_shake_all_lens_up_to_4096function test_shake_multiple_squeezesfunction test_shake_with_guarded_bufs
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
- Immediate include surface: `crypto/sha3.h`, `sha3-testvecs.h`, `hash-test-template.h`.
- Detected declarations: `function test_sha3_224_basic`, `function test_sha3_256_basic`, `function test_sha3_384_basic`, `function test_sha3_512_basic`, `function test_shake128_basic`, `function test_shake256_basic`, `function test_shake128_nist`, `function test_shake256_nist`, `function shake`, `function shake_init`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.