| 1 | |
| 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 |
|
| 14 | static 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 |
|
| 20 | static 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 |
|
| 41 | static void test_blake2s_all_key_and_hash_lens(struct kunit *test)
|
| 42 | {
|
| 43 | const size_t data_len = 100;
|
| 44 | u8 *data = alloc_buf(test, data_len);
|
| 45 | u8 *key = alloc_buf(test, BLAKE2S_KEY_SIZE);
|
| 46 | u8 *hash = alloc_buf(test, BLAKE2S_HASH_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 |
|
| 68 | static void test_blake2s_with_guarded_key_buf(struct kunit *test)
|
| 69 | {
|
| 70 | const size_t data_len = 100;
|
| 71 | u8 *data = alloc_buf(test, data_len);
|
| 72 | u8 *guarded_key_buf = alloc_guarded_buf(test, BLAKE2S_KEY_SIZE);
|
| 73 |
|
| 74 | rand_bytes(data, data_len);
|
| 75 | for (int key_len = 0; key_len <= BLAKE2S_KEY_SIZE; key_len++) {
|
| 76 | u8 key[BLAKE2S_KEY_SIZE];
|
| 77 | u8 *guarded_key = &guarded_key_buf[BLAKE2S_KEY_SIZE - key_len];
|
| 78 | u8 hash1[BLAKE2S_HASH_SIZE];
|
| 79 | u8 hash2[BLAKE2S_HASH_SIZE];
|
| 80 | struct blake2s_ctx ctx;
|
| 81 |
|
| 82 | rand_bytes(key, key_len);
|
| 83 | memcpy(guarded_key, key, key_len);
|
| 84 |
|
| 85 | blake2s(key, key_len, data, data_len, hash1, BLAKE2S_HASH_SIZE);
|
| 86 | blake2s(guarded_key, key_len, data, data_len, hash2,
|
| 87 | BLAKE2S_HASH_SIZE);
|
| 88 | KUNIT_ASSERT_MEMEQ(test, hash1, hash2, BLAKE2S_HASH_SIZE);
|
| 89 |
|
| 90 | blake2s_init_key(&ctx, BLAKE2S_HASH_SIZE, guarded_key, key_len);
|
| 91 | blake2s_update(&ctx, data, data_len);
|
| 92 | blake2s_final(&ctx, 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 |
|
| 101 | static void test_blake2s_with_guarded_out_buf(struct kunit *test)
|
| 102 | {
|
| 103 | const size_t data_len = 100;
|
| 104 | u8 *data = alloc_buf(test, data_len);
|
| 105 | u8 *out_buf = alloc_guarded_buf(test, BLAKE2S_HASH_SIZE);
|
| 106 |
|
| 107 | rand_bytes(data, data_len);
|
| 108 | for (int out_len = 1; out_len <= BLAKE2S_HASH_SIZE; out_len++) {
|
| 109 | u8 hash[BLAKE2S_HASH_SIZE];
|
| 110 | u8 *guarded_hash = &out_buf[BLAKE2S_HASH_SIZE - out_len];
|
| 111 |
|
| 112 | blake2s(NULL, 0, data, data_len, hash, out_len);
|
| 113 | blake2s(NULL, 0, data, data_len, guarded_hash, out_len);
|
| 114 | KUNIT_ASSERT_MEMEQ(test, hash, guarded_hash, out_len);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | static struct kunit_case blake2s_test_cases[] = {
|
| 119 | HASH_KUNIT_CASES,
|
| 120 | KUNIT_CASE(test_blake2s_all_key_and_hash_lens),
|
| 121 | KUNIT_CASE(test_blake2s_with_guarded_key_buf),
|
| 122 | KUNIT_CASE(test_blake2s_with_guarded_out_buf),
|
| 123 | KUNIT_CASE(benchmark_hash),
|
| 124 | {},
|
| 125 | };
|
| 126 |
|
| 127 | static struct kunit_suite blake2s_test_suite = {
|
| 128 | .name = "blake2s",
|
| 129 | .test_cases = blake2s_test_cases,
|
| 130 | };
|
| 131 | kunit_test_suite(blake2s_test_suite);
|
| 132 |
|
| 133 | MODULE_DESCRIPTION("KUnit tests and benchmark for BLAKE2s");
|
| 134 | MODULE_LICENSE("GPL");
|