Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v6.18-rc7 134 lines 4.1 kB view raw
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(out, data, NULL, BLAKE2S_HASH_SIZE, len, 0); 18} 19 20static void blake2s_init_default(struct blake2s_state *state) 21{ 22 blake2s_init(state, 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_state 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_state main_state; 48 u8 main_hash[BLAKE2S_HASH_SIZE]; 49 50 rand_bytes_seeded_from_len(data, data_len); 51 blake2s_init(&main_state, 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(hash, data, key, out_len, data_len, key_len); 56 blake2s_update(&main_state, hash, out_len); 57 } 58 } 59 blake2s_final(&main_state, 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_state state; 79 80 rand_bytes(key, key_len); 81 memcpy(guarded_key, key, key_len); 82 83 blake2s(hash1, test_buf, key, 84 BLAKE2S_HASH_SIZE, data_len, key_len); 85 blake2s(hash2, test_buf, guarded_key, 86 BLAKE2S_HASH_SIZE, data_len, key_len); 87 KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE); 88 89 blake2s_init_key(&state, BLAKE2S_HASH_SIZE, 90 guarded_key, key_len); 91 blake2s_update(&state, test_buf, data_len); 92 blake2s_final(&state, hash2); 93 KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE); 94 } 95} 96 97/* 98 * BLAKE2s specific test case which tests using a guarded output buffer for all 99 * allowed output lengths. 100 */ 101static void test_blake2s_with_guarded_out_buf(struct kunit *test) 102{ 103 const size_t data_len = 100; 104 105 rand_bytes(test_buf, data_len); 106 for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) { 107 u8 hash[BLAKE2S_HASH_SIZE]; 108 u8 *guarded_hash = &test_buf[TEST_BUF_LEN - out_len]; 109 110 blake2s(hash, test_buf, NULL, out_len, data_len, 0); 111 blake2s(guarded_hash, test_buf, NULL, out_len, data_len, 0); 112 KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len); 113 } 114} 115 116static struct kunit_case blake2s_test_cases[] = { 117 HASH_KUNIT_CASES, 118 KUNIT_CASE(test_blake2s_all_key_and_hash_lens), 119 KUNIT_CASE(test_blake2s_with_guarded_key_buf), 120 KUNIT_CASE(test_blake2s_with_guarded_out_buf), 121 KUNIT_CASE(benchmark_hash), 122 {}, 123}; 124 125static struct kunit_suite blake2s_test_suite = { 126 .name = "blake2s", 127 .test_cases = blake2s_test_cases, 128 .suite_init = hash_suite_init, 129 .suite_exit = hash_suite_exit, 130}; 131kunit_test_suite(blake2s_test_suite); 132 133MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2s"); 134MODULE_LICENSE("GPL");