sha256_kunit.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2025 Google LLC
  4. */
  5. #include <crypto/sha2.h>
  6. #include "sha256-testvecs.h"
  7. /* Generate the HASH_KUNIT_CASES using hash-test-template.h. */
  8. #define HASH sha256
  9. #define HASH_CTX sha256_ctx
  10. #define HASH_SIZE SHA256_DIGEST_SIZE
  11. #define HASH_INIT sha256_init
  12. #define HASH_UPDATE sha256_update
  13. #define HASH_FINAL sha256_final
  14. #define HMAC_KEY hmac_sha256_key
  15. #define HMAC_CTX hmac_sha256_ctx
  16. #define HMAC_PREPAREKEY hmac_sha256_preparekey
  17. #define HMAC_INIT hmac_sha256_init
  18. #define HMAC_UPDATE hmac_sha256_update
  19. #define HMAC_FINAL hmac_sha256_final
  20. #define HMAC hmac_sha256
  21. #define HMAC_USINGRAWKEY hmac_sha256_usingrawkey
  22. #include "hash-test-template.h"
  23. static void free_guarded_buf(void *buf)
  24. {
  25. vfree(buf);
  26. }
  27. /*
  28. * Allocate a KUnit-managed buffer that has length @len bytes immediately
  29. * followed by an unmapped page, and assert that the allocation succeeds.
  30. */
  31. static void *alloc_guarded_buf(struct kunit *test, size_t len)
  32. {
  33. size_t full_len = round_up(len, PAGE_SIZE);
  34. void *buf = vmalloc(full_len);
  35. KUNIT_ASSERT_NOT_NULL(test, buf);
  36. KUNIT_ASSERT_EQ(test, 0,
  37. kunit_add_action_or_reset(test, free_guarded_buf, buf));
  38. return buf + full_len - len;
  39. }
  40. /*
  41. * Test for sha256_finup_2x(). Specifically, choose various data lengths and
  42. * salt lengths, and for each one, verify that sha256_finup_2x() produces the
  43. * same results as sha256_update() and sha256_final().
  44. *
  45. * Use guarded buffers for all inputs and outputs to reliably detect any
  46. * out-of-bounds reads or writes, even if they occur in assembly code.
  47. */
  48. static void test_sha256_finup_2x(struct kunit *test)
  49. {
  50. const size_t max_data_len = 16384;
  51. u8 *data1_buf, *data2_buf, *hash1, *hash2;
  52. u8 expected_hash1[SHA256_DIGEST_SIZE];
  53. u8 expected_hash2[SHA256_DIGEST_SIZE];
  54. u8 salt[SHA256_BLOCK_SIZE];
  55. struct sha256_ctx *ctx;
  56. data1_buf = alloc_guarded_buf(test, max_data_len);
  57. data2_buf = alloc_guarded_buf(test, max_data_len);
  58. hash1 = alloc_guarded_buf(test, SHA256_DIGEST_SIZE);
  59. hash2 = alloc_guarded_buf(test, SHA256_DIGEST_SIZE);
  60. ctx = alloc_guarded_buf(test, sizeof(*ctx));
  61. rand_bytes(data1_buf, max_data_len);
  62. rand_bytes(data2_buf, max_data_len);
  63. rand_bytes(salt, sizeof(salt));
  64. memset(ctx, 0, sizeof(*ctx));
  65. for (size_t i = 0; i < 500; i++) {
  66. size_t salt_len = rand_length(sizeof(salt));
  67. size_t data_len = rand_length(max_data_len);
  68. const u8 *data1 = data1_buf + max_data_len - data_len;
  69. const u8 *data2 = data2_buf + max_data_len - data_len;
  70. struct sha256_ctx orig_ctx;
  71. sha256_init(ctx);
  72. sha256_update(ctx, salt, salt_len);
  73. orig_ctx = *ctx;
  74. sha256_finup_2x(ctx, data1, data2, data_len, hash1, hash2);
  75. KUNIT_ASSERT_MEMEQ_MSG(
  76. test, ctx, &orig_ctx, sizeof(*ctx),
  77. "sha256_finup_2x() modified its ctx argument");
  78. sha256_update(ctx, data1, data_len);
  79. sha256_final(ctx, expected_hash1);
  80. sha256_update(&orig_ctx, data2, data_len);
  81. sha256_final(&orig_ctx, expected_hash2);
  82. KUNIT_ASSERT_MEMEQ_MSG(
  83. test, hash1, expected_hash1, SHA256_DIGEST_SIZE,
  84. "Wrong hash1 with salt_len=%zu data_len=%zu", salt_len,
  85. data_len);
  86. KUNIT_ASSERT_MEMEQ_MSG(
  87. test, hash2, expected_hash2, SHA256_DIGEST_SIZE,
  88. "Wrong hash2 with salt_len=%zu data_len=%zu", salt_len,
  89. data_len);
  90. }
  91. }
  92. /* Test sha256_finup_2x() with ctx == NULL */
  93. static void test_sha256_finup_2x_defaultctx(struct kunit *test)
  94. {
  95. const size_t data_len = 128;
  96. struct sha256_ctx ctx;
  97. u8 hash1_a[SHA256_DIGEST_SIZE];
  98. u8 hash2_a[SHA256_DIGEST_SIZE];
  99. u8 hash1_b[SHA256_DIGEST_SIZE];
  100. u8 hash2_b[SHA256_DIGEST_SIZE];
  101. rand_bytes(test_buf, 2 * data_len);
  102. sha256_init(&ctx);
  103. sha256_finup_2x(&ctx, test_buf, &test_buf[data_len], data_len, hash1_a,
  104. hash2_a);
  105. sha256_finup_2x(NULL, test_buf, &test_buf[data_len], data_len, hash1_b,
  106. hash2_b);
  107. KUNIT_ASSERT_MEMEQ(test, hash1_a, hash1_b, SHA256_DIGEST_SIZE);
  108. KUNIT_ASSERT_MEMEQ(test, hash2_a, hash2_b, SHA256_DIGEST_SIZE);
  109. }
  110. /*
  111. * Test that sha256_finup_2x() and sha256_update/final() produce consistent
  112. * results with total message lengths that require more than 32 bits.
  113. */
  114. static void test_sha256_finup_2x_hugelen(struct kunit *test)
  115. {
  116. const size_t data_len = 4 * SHA256_BLOCK_SIZE;
  117. struct sha256_ctx ctx = {};
  118. u8 expected_hash[SHA256_DIGEST_SIZE];
  119. u8 hash[SHA256_DIGEST_SIZE];
  120. rand_bytes(test_buf, data_len);
  121. for (size_t align = 0; align < SHA256_BLOCK_SIZE; align++) {
  122. sha256_init(&ctx);
  123. ctx.ctx.bytecount = 0x123456789abcd00 + align;
  124. sha256_finup_2x(&ctx, test_buf, test_buf, data_len, hash, hash);
  125. sha256_update(&ctx, test_buf, data_len);
  126. sha256_final(&ctx, expected_hash);
  127. KUNIT_ASSERT_MEMEQ(test, hash, expected_hash,
  128. SHA256_DIGEST_SIZE);
  129. }
  130. }
  131. /* Benchmark for sha256_finup_2x() */
  132. static void benchmark_sha256_finup_2x(struct kunit *test)
  133. {
  134. /*
  135. * Try a few different salt lengths, since sha256_finup_2x() performance
  136. * may vary slightly for the same data_len depending on how many bytes
  137. * were already processed in the initial context.
  138. */
  139. static const size_t salt_lens_to_test[] = { 0, 32, 64 };
  140. const size_t data_len = 4096;
  141. const size_t num_iters = 4096;
  142. struct sha256_ctx ctx;
  143. u8 hash1[SHA256_DIGEST_SIZE];
  144. u8 hash2[SHA256_DIGEST_SIZE];
  145. if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK))
  146. kunit_skip(test, "not enabled");
  147. if (!sha256_finup_2x_is_optimized())
  148. kunit_skip(test, "not relevant");
  149. rand_bytes(test_buf, data_len * 2);
  150. /* Warm-up */
  151. for (size_t i = 0; i < num_iters; i++)
  152. sha256_finup_2x(NULL, &test_buf[0], &test_buf[data_len],
  153. data_len, hash1, hash2);
  154. for (size_t i = 0; i < ARRAY_SIZE(salt_lens_to_test); i++) {
  155. size_t salt_len = salt_lens_to_test[i];
  156. u64 t0, t1;
  157. /*
  158. * Prepare the initial context. The time to process the salt is
  159. * not measured; we're just interested in sha256_finup_2x().
  160. */
  161. sha256_init(&ctx);
  162. sha256_update(&ctx, test_buf, salt_len);
  163. preempt_disable();
  164. t0 = ktime_get_ns();
  165. for (size_t j = 0; j < num_iters; j++)
  166. sha256_finup_2x(&ctx, &test_buf[0], &test_buf[data_len],
  167. data_len, hash1, hash2);
  168. t1 = ktime_get_ns();
  169. preempt_enable();
  170. kunit_info(test, "data_len=%zu salt_len=%zu: %llu MB/s",
  171. data_len, salt_len,
  172. div64_u64((u64)data_len * 2 * num_iters * 1000,
  173. t1 - t0 ?: 1));
  174. }
  175. }
  176. static struct kunit_case hash_test_cases[] = {
  177. HASH_KUNIT_CASES,
  178. KUNIT_CASE(test_sha256_finup_2x),
  179. KUNIT_CASE(test_sha256_finup_2x_defaultctx),
  180. KUNIT_CASE(test_sha256_finup_2x_hugelen),
  181. KUNIT_CASE(benchmark_hash),
  182. KUNIT_CASE(benchmark_sha256_finup_2x),
  183. {},
  184. };
  185. static struct kunit_suite hash_test_suite = {
  186. .name = "sha256",
  187. .test_cases = hash_test_cases,
  188. .suite_init = hash_suite_init,
  189. .suite_exit = hash_suite_exit,
  190. };
  191. kunit_test_suite(hash_test_suite);
  192. MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-256 and HMAC-SHA256");
  193. MODULE_LICENSE("GPL");