lib/crypto/tests/sha256_kunit.c

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

File Facts

System
Linux kernel
Corpus path
lib/crypto/tests/sha256_kunit.c
Extension
.c
Size
6817 bytes
Lines
225
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

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2025 Google LLC
 */
#include <crypto/sha2.h>
#include "sha256-testvecs.h"

/* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
#define HASH sha256
#define HASH_CTX sha256_ctx
#define HASH_SIZE SHA256_DIGEST_SIZE
#define HASH_INIT sha256_init
#define HASH_UPDATE sha256_update
#define HASH_FINAL sha256_final
#define HMAC_KEY hmac_sha256_key
#define HMAC_CTX hmac_sha256_ctx
#define HMAC_PREPAREKEY hmac_sha256_preparekey
#define HMAC_INIT hmac_sha256_init
#define HMAC_UPDATE hmac_sha256_update
#define HMAC_FINAL hmac_sha256_final
#define HMAC hmac_sha256
#define HMAC_USINGRAWKEY hmac_sha256_usingrawkey
#include "hash-test-template.h"

static void free_guarded_buf(void *buf)
{
	vfree(buf);
}

/*
 * Allocate a KUnit-managed buffer that has length @len bytes immediately
 * followed by an unmapped page, and assert that the allocation succeeds.
 */
static void *alloc_guarded_buf(struct kunit *test, size_t len)
{
	size_t full_len = round_up(len, PAGE_SIZE);
	void *buf = vmalloc(full_len);

	KUNIT_ASSERT_NOT_NULL(test, buf);
	KUNIT_ASSERT_EQ(test, 0,
			kunit_add_action_or_reset(test, free_guarded_buf, buf));
	return buf + full_len - len;
}

/*
 * Test for sha256_finup_2x().  Specifically, choose various data lengths and
 * salt lengths, and for each one, verify that sha256_finup_2x() produces the
 * same results as sha256_update() and sha256_final().
 *
 * Use guarded buffers for all inputs and outputs to reliably detect any
 * out-of-bounds reads or writes, even if they occur in assembly code.
 */
static void test_sha256_finup_2x(struct kunit *test)
{
	const size_t max_data_len = 16384;
	u8 *data1_buf, *data2_buf, *hash1, *hash2;
	u8 expected_hash1[SHA256_DIGEST_SIZE];
	u8 expected_hash2[SHA256_DIGEST_SIZE];
	u8 salt[SHA256_BLOCK_SIZE];
	struct sha256_ctx *ctx;

	data1_buf = alloc_guarded_buf(test, max_data_len);
	data2_buf = alloc_guarded_buf(test, max_data_len);
	hash1 = alloc_guarded_buf(test, SHA256_DIGEST_SIZE);
	hash2 = alloc_guarded_buf(test, SHA256_DIGEST_SIZE);
	ctx = alloc_guarded_buf(test, sizeof(*ctx));

	rand_bytes(data1_buf, max_data_len);
	rand_bytes(data2_buf, max_data_len);
	rand_bytes(salt, sizeof(salt));
	memset(ctx, 0, sizeof(*ctx));

	for (size_t i = 0; i < 500; i++) {
		size_t salt_len = rand_length(sizeof(salt));
		size_t data_len = rand_length(max_data_len);
		const u8 *data1 = data1_buf + max_data_len - data_len;
		const u8 *data2 = data2_buf + max_data_len - data_len;
		struct sha256_ctx orig_ctx;

		sha256_init(ctx);
		sha256_update(ctx, salt, salt_len);
		orig_ctx = *ctx;

		sha256_finup_2x(ctx, data1, data2, data_len, hash1, hash2);
		KUNIT_ASSERT_MEMEQ_MSG(
			test, ctx, &orig_ctx, sizeof(*ctx),
			"sha256_finup_2x() modified its ctx argument");

		sha256_update(ctx, data1, data_len);
		sha256_final(ctx, expected_hash1);

Annotation

Implementation Notes