blake2s_kunit.c 3.9 KB

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