File Index
Files and directories
Sign in
FILE

blake2s_kunit.c

File preview with syntax highlighting.

linux-6.19.14/lib/crypto/tests/blake2s_kunit.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright 2025 Google LLC
  4 */
  5#include <crypto/blake2s.h>
  6#include "blake2s-testvecs.h"
  7
  8/*
  9 * The following are compatibility functions that present BLAKE2s as an unkeyed
 10 * hash function that produces hashes of fixed length BLAKE2S_HASH_SIZE, so that
 11 * hash-test-template.h can be reused to test it.
 12 */
 13
 14static void blake2s_default(const u8 *data, size_t len,
 15			    u8 out[BLAKE2S_HASH_SIZE])
 16{
 17	blake2s(NULL, 0, data, len, out, BLAKE2S_HASH_SIZE);
 18}
 19
 20static void blake2s_init_default(struct blake2s_ctx *ctx)
 21{
 22	blake2s_init(ctx, BLAKE2S_HASH_SIZE);
 23}
 24
 25/*
 26 * Generate the HASH_KUNIT_CASES using hash-test-template.h.  These test BLAKE2s
 27 * with a key length of 0 and a hash length of BLAKE2S_HASH_SIZE.
 28 */
 29#define HASH blake2s_default
 30#define HASH_CTX blake2s_ctx
 31#define HASH_SIZE BLAKE2S_HASH_SIZE
 32#define HASH_INIT blake2s_init_default
 33#define HASH_UPDATE blake2s_update
 34#define HASH_FINAL blake2s_final
 35#include "hash-test-template.h"
 36
 37/*
 38 * BLAKE2s specific test case which tests all possible combinations of key
 39 * length and hash length.
 40 */
 41static void test_blake2s_all_key_and_hash_lens(struct kunit *test)
 42{
 43	const size_t data_len = 100;
 44	u8 *data = &test_buf[0];
 45	u8 *key = data + data_len;
 46	u8 *hash = key + BLAKE2S_KEY_SIZE;
 47	struct blake2s_ctx main_ctx;
 48	u8 main_hash[BLAKE2S_HASH_SIZE];
 49
 50	rand_bytes_seeded_from_len(data, data_len);
 51	blake2s_init(&main_ctx, BLAKE2S_HASH_SIZE);
 52	for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) {
 53		rand_bytes_seeded_from_len(key, key_len);
 54		for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) {
 55			blake2s(key, key_len, data, data_len, hash, out_len);
 56			blake2s_update(&main_ctx, hash, out_len);
 57		}
 58	}
 59	blake2s_final(&main_ctx, main_hash);
 60	KUNIT_ASSERT_MEMEQ(test, main_hash, blake2s_keyed_testvec_consolidated,
 61			   BLAKE2S_HASH_SIZE);
 62}
 63
 64/*
 65 * BLAKE2s specific test case which tests using a guarded buffer for all allowed
 66 * key lengths.  Also tests both blake2s() and blake2s_init_key().
 67 */
 68static void test_blake2s_with_guarded_key_buf(struct kunit *test)
 69{
 70	const size_t data_len = 100;
 71
 72	rand_bytes(test_buf, data_len);
 73	for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) {
 74		u8 key[BLAKE2S_KEY_SIZE];
 75		u8 *guarded_key = &test_buf[TEST_BUF_LEN - key_len];
 76		u8 hash1[BLAKE2S_HASH_SIZE];
 77		u8 hash2[BLAKE2S_HASH_SIZE];
 78		struct blake2s_ctx ctx;
 79
 80		rand_bytes(key, key_len);
 81		memcpy(guarded_key, key, key_len);
 82
 83		blake2s(key, key_len, test_buf, data_len,
 84			hash1, BLAKE2S_HASH_SIZE);
 85		blake2s(guarded_key, key_len, test_buf, data_len,
 86			hash2, BLAKE2S_HASH_SIZE);
 87		KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE);
 88
 89		blake2s_init_key(&ctx, BLAKE2S_HASH_SIZE, guarded_key, key_len);
 90		blake2s_update(&ctx, test_buf, data_len);
 91		blake2s_final(&ctx, hash2);
 92		KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE);
 93	}
 94}
 95
 96/*
 97 * BLAKE2s specific test case which tests using a guarded output buffer for all
 98 * allowed output lengths.
 99 */
100static void test_blake2s_with_guarded_out_buf(struct kunit *test)
101{
102	const size_t data_len = 100;
103
104	rand_bytes(test_buf, data_len);
105	for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) {
106		u8 hash[BLAKE2S_HASH_SIZE];
107		u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len];
108
109		blake2s(NULL, 0, test_buf, data_len, hash, out_len);
110		blake2s(NULL, 0, test_buf, data_len, guarded_hash, out_len);
111		KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len);
112	}
113}
114
115static struct kunit_case blake2s_test_cases[] = {
116	HASH_KUNIT_CASES,
117	KUNIT_CASE(test_blake2s_all_key_and_hash_lens),
118	KUNIT_CASE(test_blake2s_with_guarded_key_buf),
119	KUNIT_CASE(test_blake2s_with_guarded_out_buf),
120	KUNIT_CASE(benchmark_hash),
121	{},
122};
123
124static struct kunit_suite blake2s_test_suite = {
125	.name = "blake2s",
126	.test_cases = blake2s_test_cases,
127	.suite_init = hash_suite_init,
128	.suite_exit = hash_suite_exit,
129};
130kunit_test_suite(blake2s_test_suite);
131
132MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2s");
133MODULE_LICENSE("GPL");
~/ Read-only file preview.