polyval_kunit.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2025 Google LLC
  4. */
  5. #include <crypto/polyval.h>
  6. #include "polyval-testvecs.h"
  7. /*
  8. * A fixed key used when presenting POLYVAL as an unkeyed hash function in order
  9. * to reuse hash-test-template.h. At the beginning of the test suite, this is
  10. * initialized to a key prepared from bytes generated from a fixed seed.
  11. */
  12. static struct polyval_key test_key;
  13. static void polyval_init_withtestkey(struct polyval_ctx *ctx)
  14. {
  15. polyval_init(ctx, &test_key);
  16. }
  17. static void polyval_withtestkey(const u8 *data, size_t len,
  18. u8 out[POLYVAL_BLOCK_SIZE])
  19. {
  20. polyval(&test_key, data, len, out);
  21. }
  22. /* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
  23. #define HASH polyval_withtestkey
  24. #define HASH_CTX polyval_ctx
  25. #define HASH_SIZE POLYVAL_BLOCK_SIZE
  26. #define HASH_INIT polyval_init_withtestkey
  27. #define HASH_UPDATE polyval_update
  28. #define HASH_FINAL polyval_final
  29. #include "hash-test-template.h"
  30. /*
  31. * Test an example from RFC8452 ("AES-GCM-SIV: Nonce Misuse-Resistant
  32. * Authenticated Encryption") to ensure compatibility with that.
  33. */
  34. static void test_polyval_rfc8452_testvec(struct kunit *test)
  35. {
  36. static const u8 raw_key[POLYVAL_BLOCK_SIZE] =
  37. "\x31\x07\x28\xd9\x91\x1f\x1f\x38"
  38. "\x37\xb2\x43\x16\xc3\xfa\xb9\xa0";
  39. static const u8 data[48] =
  40. "\x65\x78\x61\x6d\x70\x6c\x65\x00"
  41. "\x00\x00\x00\x00\x00\x00\x00\x00"
  42. "\x48\x65\x6c\x6c\x6f\x20\x77\x6f"
  43. "\x72\x6c\x64\x00\x00\x00\x00\x00"
  44. "\x38\x00\x00\x00\x00\x00\x00\x00"
  45. "\x58\x00\x00\x00\x00\x00\x00\x00";
  46. static const u8 expected_hash[POLYVAL_BLOCK_SIZE] =
  47. "\xad\x7f\xcf\x0b\x51\x69\x85\x16"
  48. "\x62\x67\x2f\x3c\x5f\x95\x13\x8f";
  49. u8 hash[POLYVAL_BLOCK_SIZE];
  50. struct polyval_key key;
  51. polyval_preparekey(&key, raw_key);
  52. polyval(&key, data, sizeof(data), hash);
  53. KUNIT_ASSERT_MEMEQ(test, hash, expected_hash, sizeof(hash));
  54. }
  55. /*
  56. * Test a key and messages containing all one bits. This is useful to detect
  57. * overflow bugs in implementations that emulate carryless multiplication using
  58. * a series of standard multiplications with the bits spread out.
  59. */
  60. static void test_polyval_allones_key_and_message(struct kunit *test)
  61. {
  62. struct polyval_key key;
  63. struct polyval_ctx hashofhashes_ctx;
  64. u8 hash[POLYVAL_BLOCK_SIZE];
  65. static_assert(TEST_BUF_LEN >= 4096);
  66. memset(test_buf, 0xff, 4096);
  67. polyval_preparekey(&key, test_buf);
  68. polyval_init(&hashofhashes_ctx, &key);
  69. for (size_t len = 0; len <= 4096; len += 16) {
  70. polyval(&key, test_buf, len, hash);
  71. polyval_update(&hashofhashes_ctx, hash, sizeof(hash));
  72. }
  73. polyval_final(&hashofhashes_ctx, hash);
  74. KUNIT_ASSERT_MEMEQ(test, hash, polyval_allones_hashofhashes,
  75. sizeof(hash));
  76. }
  77. #define MAX_LEN_FOR_KEY_CHECK 1024
  78. /*
  79. * Given two prepared keys which should be identical (but may differ in
  80. * alignment and/or whether they are followed by a guard page or not), verify
  81. * that they produce consistent results on various data lengths.
  82. */
  83. static void check_key_consistency(struct kunit *test,
  84. const struct polyval_key *key1,
  85. const struct polyval_key *key2)
  86. {
  87. u8 *data = test_buf;
  88. u8 hash1[POLYVAL_BLOCK_SIZE];
  89. u8 hash2[POLYVAL_BLOCK_SIZE];
  90. rand_bytes(data, MAX_LEN_FOR_KEY_CHECK);
  91. KUNIT_ASSERT_MEMEQ(test, key1, key2, sizeof(*key1));
  92. for (int i = 0; i < 100; i++) {
  93. size_t len = rand_length(MAX_LEN_FOR_KEY_CHECK);
  94. polyval(key1, data, len, hash1);
  95. polyval(key2, data, len, hash2);
  96. KUNIT_ASSERT_MEMEQ(test, hash1, hash2, sizeof(hash1));
  97. }
  98. }
  99. /* Test that no buffer overreads occur on either raw_key or polyval_key. */
  100. static void test_polyval_with_guarded_key(struct kunit *test)
  101. {
  102. u8 raw_key[POLYVAL_BLOCK_SIZE];
  103. u8 *guarded_raw_key = &test_buf[TEST_BUF_LEN - sizeof(raw_key)];
  104. struct polyval_key key1, key2;
  105. struct polyval_key *guarded_key =
  106. (struct polyval_key *)&test_buf[TEST_BUF_LEN - sizeof(key1)];
  107. /* Prepare with regular buffers. */
  108. rand_bytes(raw_key, sizeof(raw_key));
  109. polyval_preparekey(&key1, raw_key);
  110. /* Prepare with guarded raw_key, then check that it works. */
  111. memcpy(guarded_raw_key, raw_key, sizeof(raw_key));
  112. polyval_preparekey(&key2, guarded_raw_key);
  113. check_key_consistency(test, &key1, &key2);
  114. /* Prepare guarded polyval_key, then check that it works. */
  115. polyval_preparekey(guarded_key, raw_key);
  116. check_key_consistency(test, &key1, guarded_key);
  117. }
  118. /*
  119. * Test that polyval_key only needs to be aligned to
  120. * __alignof__(struct polyval_key), i.e. 8 bytes. The assembly code may prefer
  121. * 16-byte or higher alignment, but it musn't require it.
  122. */
  123. static void test_polyval_with_minimally_aligned_key(struct kunit *test)
  124. {
  125. u8 raw_key[POLYVAL_BLOCK_SIZE];
  126. struct polyval_key key;
  127. struct polyval_key *minaligned_key =
  128. (struct polyval_key *)&test_buf[MAX_LEN_FOR_KEY_CHECK +
  129. __alignof__(struct polyval_key)];
  130. KUNIT_ASSERT_TRUE(test, IS_ALIGNED((uintptr_t)minaligned_key,
  131. __alignof__(struct polyval_key)));
  132. KUNIT_ASSERT_TRUE(test,
  133. !IS_ALIGNED((uintptr_t)minaligned_key,
  134. 2 * __alignof__(struct polyval_key)));
  135. rand_bytes(raw_key, sizeof(raw_key));
  136. polyval_preparekey(&key, raw_key);
  137. polyval_preparekey(minaligned_key, raw_key);
  138. check_key_consistency(test, &key, minaligned_key);
  139. }
  140. struct polyval_irq_test_state {
  141. struct polyval_key expected_key;
  142. u8 raw_key[POLYVAL_BLOCK_SIZE];
  143. };
  144. static bool polyval_irq_test_func(void *state_)
  145. {
  146. struct polyval_irq_test_state *state = state_;
  147. struct polyval_key key;
  148. polyval_preparekey(&key, state->raw_key);
  149. return memcmp(&key, &state->expected_key, sizeof(key)) == 0;
  150. }
  151. /*
  152. * Test that polyval_preparekey() produces the same output regardless of whether
  153. * FPU or vector registers are usable when it is called.
  154. */
  155. static void test_polyval_preparekey_in_irqs(struct kunit *test)
  156. {
  157. struct polyval_irq_test_state state;
  158. rand_bytes(state.raw_key, sizeof(state.raw_key));
  159. polyval_preparekey(&state.expected_key, state.raw_key);
  160. kunit_run_irq_test(test, polyval_irq_test_func, 200000, &state);
  161. }
  162. static int polyval_suite_init(struct kunit_suite *suite)
  163. {
  164. u8 raw_key[POLYVAL_BLOCK_SIZE];
  165. rand_bytes_seeded_from_len(raw_key, sizeof(raw_key));
  166. polyval_preparekey(&test_key, raw_key);
  167. return hash_suite_init(suite);
  168. }
  169. static void polyval_suite_exit(struct kunit_suite *suite)
  170. {
  171. hash_suite_exit(suite);
  172. }
  173. static struct kunit_case polyval_test_cases[] = {
  174. HASH_KUNIT_CASES,
  175. KUNIT_CASE(test_polyval_rfc8452_testvec),
  176. KUNIT_CASE(test_polyval_allones_key_and_message),
  177. KUNIT_CASE(test_polyval_with_guarded_key),
  178. KUNIT_CASE(test_polyval_with_minimally_aligned_key),
  179. KUNIT_CASE(test_polyval_preparekey_in_irqs),
  180. KUNIT_CASE(benchmark_hash),
  181. {},
  182. };
  183. static struct kunit_suite polyval_test_suite = {
  184. .name = "polyval",
  185. .test_cases = polyval_test_cases,
  186. .suite_init = polyval_suite_init,
  187. .suite_exit = polyval_suite_exit,
  188. };
  189. kunit_test_suite(polyval_test_suite);
  190. MODULE_DESCRIPTION("KUnit tests and benchmark for POLYVAL");
  191. MODULE_LICENSE("GPL");