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

Configure Feed

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

Merge tag 'libcrypto-tests-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux

Pull crypto library test updates from Eric Biggers:
"Add KUnit test suites for the Poly1305, SHA-1, SHA-224, SHA-256,
SHA-384, and SHA-512 library functions.

These are the first KUnit tests for lib/crypto/. So in addition to
being useful tests for these specific algorithms, they also establish
some conventions for lib/crypto/ testing going forwards.

The new tests are fairly comprehensive: more comprehensive than the
generic crypto infrastructure's tests. They use a variety of
techniques to check for the types of implementation bugs that tend to
occur in the real world, rather than just naively checking some test
vectors. (Interestingly, poly1305_kunit found a bug in QEMU)

The core test logic is shared by all six algorithms, rather than being
duplicated for each algorithm.

Each algorithm's test suite also optionally includes a benchmark"

* tag 'libcrypto-tests-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux:
lib/crypto: tests: Annotate worker to be on stack
lib/crypto: tests: Add KUnit tests for SHA-1 and HMAC-SHA1
lib/crypto: tests: Add KUnit tests for Poly1305
lib/crypto: tests: Add KUnit tests for SHA-384 and SHA-512
lib/crypto: tests: Add KUnit tests for SHA-224 and SHA-256
lib/crypto: tests: Add hash-test-template.h and gen-hash-testvecs.py

+2766
+2
lib/crypto/Kconfig
··· 194 194 config CRYPTO_LIB_SM3 195 195 tristate 196 196 197 + source "lib/crypto/tests/Kconfig" 198 + 197 199 if !KMSAN # avoid false positives from assembly 198 200 if ARM 199 201 source "lib/crypto/arm/Kconfig"
+2
lib/crypto/Makefile
··· 8 8 quiet_cmd_perlasm_with_args = PERLASM $@ 9 9 cmd_perlasm_with_args = $(PERL) $(<) void $(@) 10 10 11 + obj-$(CONFIG_KUNIT) += tests/ 12 + 11 13 obj-$(CONFIG_CRYPTO_HASH_INFO) += hash_info.o 12 14 13 15 obj-$(CONFIG_CRYPTO_LIB_UTILS) += libcryptoutils.o
+60
lib/crypto/tests/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + config CRYPTO_LIB_POLY1305_KUNIT_TEST 4 + tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS 5 + depends on KUNIT 6 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 7 + select CRYPTO_LIB_BENCHMARK_VISIBLE 8 + select CRYPTO_LIB_POLY1305 9 + help 10 + KUnit tests for the Poly1305 library functions. 11 + 12 + config CRYPTO_LIB_SHA1_KUNIT_TEST 13 + tristate "KUnit tests for SHA-1" if !KUNIT_ALL_TESTS 14 + depends on KUNIT 15 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 16 + select CRYPTO_LIB_BENCHMARK_VISIBLE 17 + select CRYPTO_LIB_SHA1 18 + help 19 + KUnit tests for the SHA-1 cryptographic hash function and its 20 + corresponding HMAC. 21 + 22 + # Option is named *_SHA256_KUNIT_TEST, though both SHA-224 and SHA-256 tests are 23 + # included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA256). 24 + config CRYPTO_LIB_SHA256_KUNIT_TEST 25 + tristate "KUnit tests for SHA-224 and SHA-256" if !KUNIT_ALL_TESTS 26 + depends on KUNIT 27 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 28 + select CRYPTO_LIB_BENCHMARK_VISIBLE 29 + select CRYPTO_LIB_SHA256 30 + help 31 + KUnit tests for the SHA-224 and SHA-256 cryptographic hash functions 32 + and their corresponding HMACs. 33 + 34 + # Option is named *_SHA512_KUNIT_TEST, though both SHA-384 and SHA-512 tests are 35 + # included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA512). 36 + config CRYPTO_LIB_SHA512_KUNIT_TEST 37 + tristate "KUnit tests for SHA-384 and SHA-512" if !KUNIT_ALL_TESTS 38 + depends on KUNIT 39 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 40 + select CRYPTO_LIB_BENCHMARK_VISIBLE 41 + select CRYPTO_LIB_SHA512 42 + help 43 + KUnit tests for the SHA-384 and SHA-512 cryptographic hash functions 44 + and their corresponding HMACs. 45 + 46 + config CRYPTO_LIB_BENCHMARK_VISIBLE 47 + bool 48 + 49 + config CRYPTO_LIB_BENCHMARK 50 + bool "Include benchmarks in KUnit tests for cryptographic functions" 51 + depends on CRYPTO_LIB_BENCHMARK_VISIBLE 52 + help 53 + Include benchmarks in the KUnit tests for cryptographic functions. 54 + The benchmark results are printed to the kernel log when the 55 + corresponding KUnit test suite runs. 56 + 57 + This is useful for evaluating the performance of the cryptographic 58 + functions. However, it will increase the runtime of the KUnit tests. 59 + 60 + If you're only interested in correctness testing, leave this disabled.
+6
lib/crypto/tests/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o 4 + obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o 5 + obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o 6 + obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o sha512_kunit.o
+683
lib/crypto/tests/hash-test-template.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Test cases for hash functions, including a benchmark. This is included by 4 + * KUnit test suites that want to use it. See sha512_kunit.c for an example. 5 + * 6 + * Copyright 2025 Google LLC 7 + */ 8 + #include <kunit/test.h> 9 + #include <linux/hrtimer.h> 10 + #include <linux/timekeeping.h> 11 + #include <linux/vmalloc.h> 12 + #include <linux/workqueue.h> 13 + 14 + /* test_buf is a guarded buffer, i.e. &test_buf[TEST_BUF_LEN] is not mapped. */ 15 + #define TEST_BUF_LEN 16384 16 + static u8 *test_buf; 17 + 18 + static u8 *orig_test_buf; 19 + 20 + static u64 random_seed; 21 + 22 + /* 23 + * This is a simple linear congruential generator. It is used only for testing, 24 + * which does not require cryptographically secure random numbers. A hard-coded 25 + * algorithm is used instead of <linux/prandom.h> so that it matches the 26 + * algorithm used by the test vector generation script. This allows the input 27 + * data in random test vectors to be concisely stored as just the seed. 28 + */ 29 + static u32 rand32(void) 30 + { 31 + random_seed = (random_seed * 25214903917 + 11) & ((1ULL << 48) - 1); 32 + return random_seed >> 16; 33 + } 34 + 35 + static void rand_bytes(u8 *out, size_t len) 36 + { 37 + for (size_t i = 0; i < len; i++) 38 + out[i] = rand32(); 39 + } 40 + 41 + static void rand_bytes_seeded_from_len(u8 *out, size_t len) 42 + { 43 + random_seed = len; 44 + rand_bytes(out, len); 45 + } 46 + 47 + static bool rand_bool(void) 48 + { 49 + return rand32() % 2; 50 + } 51 + 52 + /* Generate a random length, preferring small lengths. */ 53 + static size_t rand_length(size_t max_len) 54 + { 55 + size_t len; 56 + 57 + switch (rand32() % 3) { 58 + case 0: 59 + len = rand32() % 128; 60 + break; 61 + case 1: 62 + len = rand32() % 3072; 63 + break; 64 + default: 65 + len = rand32(); 66 + break; 67 + } 68 + return len % (max_len + 1); 69 + } 70 + 71 + static size_t rand_offset(size_t max_offset) 72 + { 73 + return min(rand32() % 128, max_offset); 74 + } 75 + 76 + static int hash_suite_init(struct kunit_suite *suite) 77 + { 78 + /* 79 + * Allocate the test buffer using vmalloc() with a page-aligned length 80 + * so that it is immediately followed by a guard page. This allows 81 + * buffer overreads to be detected, even in assembly code. 82 + */ 83 + size_t alloc_len = round_up(TEST_BUF_LEN, PAGE_SIZE); 84 + 85 + orig_test_buf = vmalloc(alloc_len); 86 + if (!orig_test_buf) 87 + return -ENOMEM; 88 + 89 + test_buf = orig_test_buf + alloc_len - TEST_BUF_LEN; 90 + return 0; 91 + } 92 + 93 + static void hash_suite_exit(struct kunit_suite *suite) 94 + { 95 + vfree(orig_test_buf); 96 + orig_test_buf = NULL; 97 + test_buf = NULL; 98 + } 99 + 100 + /* 101 + * Test the hash function against a list of test vectors. 102 + * 103 + * Note that it's only necessary to run each test vector in one way (e.g., 104 + * one-shot instead of incremental), since consistency between different ways of 105 + * using the APIs is verified by other test cases. 106 + */ 107 + static void test_hash_test_vectors(struct kunit *test) 108 + { 109 + for (size_t i = 0; i < ARRAY_SIZE(hash_testvecs); i++) { 110 + size_t data_len = hash_testvecs[i].data_len; 111 + u8 actual_hash[HASH_SIZE]; 112 + 113 + KUNIT_ASSERT_LE(test, data_len, TEST_BUF_LEN); 114 + rand_bytes_seeded_from_len(test_buf, data_len); 115 + 116 + HASH(test_buf, data_len, actual_hash); 117 + KUNIT_ASSERT_MEMEQ_MSG( 118 + test, actual_hash, hash_testvecs[i].digest, HASH_SIZE, 119 + "Wrong result with test vector %zu; data_len=%zu", i, 120 + data_len); 121 + } 122 + } 123 + 124 + /* 125 + * Test that the hash function produces correct results for *every* length up to 126 + * 4096 bytes. To do this, generate seeded random data, then calculate a hash 127 + * value for each length 0..4096, then hash the hash values. Verify just the 128 + * final hash value, which should match only when all hash values were correct. 129 + */ 130 + static void test_hash_all_lens_up_to_4096(struct kunit *test) 131 + { 132 + struct HASH_CTX ctx; 133 + u8 hash[HASH_SIZE]; 134 + 135 + static_assert(TEST_BUF_LEN >= 4096); 136 + rand_bytes_seeded_from_len(test_buf, 4096); 137 + HASH_INIT(&ctx); 138 + for (size_t len = 0; len <= 4096; len++) { 139 + HASH(test_buf, len, hash); 140 + HASH_UPDATE(&ctx, hash, HASH_SIZE); 141 + } 142 + HASH_FINAL(&ctx, hash); 143 + KUNIT_ASSERT_MEMEQ(test, hash, hash_testvec_consolidated, HASH_SIZE); 144 + } 145 + 146 + /* 147 + * Test that the hash function produces the same result with a one-shot 148 + * computation as it does with an incremental computation. 149 + */ 150 + static void test_hash_incremental_updates(struct kunit *test) 151 + { 152 + for (int i = 0; i < 1000; i++) { 153 + size_t total_len, offset; 154 + struct HASH_CTX ctx; 155 + u8 hash1[HASH_SIZE]; 156 + u8 hash2[HASH_SIZE]; 157 + size_t num_parts = 0; 158 + size_t remaining_len, cur_offset; 159 + 160 + total_len = rand_length(TEST_BUF_LEN); 161 + offset = rand_offset(TEST_BUF_LEN - total_len); 162 + rand_bytes(&test_buf[offset], total_len); 163 + 164 + /* Compute the hash value in one shot. */ 165 + HASH(&test_buf[offset], total_len, hash1); 166 + 167 + /* 168 + * Compute the hash value incrementally, using a randomly 169 + * selected sequence of update lengths that sum to total_len. 170 + */ 171 + HASH_INIT(&ctx); 172 + remaining_len = total_len; 173 + cur_offset = offset; 174 + while (rand_bool()) { 175 + size_t part_len = rand_length(remaining_len); 176 + 177 + HASH_UPDATE(&ctx, &test_buf[cur_offset], part_len); 178 + num_parts++; 179 + cur_offset += part_len; 180 + remaining_len -= part_len; 181 + } 182 + if (remaining_len != 0 || rand_bool()) { 183 + HASH_UPDATE(&ctx, &test_buf[cur_offset], remaining_len); 184 + num_parts++; 185 + } 186 + HASH_FINAL(&ctx, hash2); 187 + 188 + /* Verify that the two hash values are the same. */ 189 + KUNIT_ASSERT_MEMEQ_MSG( 190 + test, hash1, hash2, HASH_SIZE, 191 + "Incremental test failed with total_len=%zu num_parts=%zu offset=%zu", 192 + total_len, num_parts, offset); 193 + } 194 + } 195 + 196 + /* 197 + * Test that the hash function does not overrun any buffers. Uses a guard page 198 + * to catch buffer overruns even if they occur in assembly code. 199 + */ 200 + static void test_hash_buffer_overruns(struct kunit *test) 201 + { 202 + const size_t max_tested_len = TEST_BUF_LEN - sizeof(struct HASH_CTX); 203 + void *const buf_end = &test_buf[TEST_BUF_LEN]; 204 + struct HASH_CTX *guarded_ctx = buf_end - sizeof(*guarded_ctx); 205 + 206 + rand_bytes(test_buf, TEST_BUF_LEN); 207 + 208 + for (int i = 0; i < 100; i++) { 209 + size_t len = rand_length(max_tested_len); 210 + struct HASH_CTX ctx; 211 + u8 hash[HASH_SIZE]; 212 + 213 + /* Check for overruns of the data buffer. */ 214 + HASH(buf_end - len, len, hash); 215 + HASH_INIT(&ctx); 216 + HASH_UPDATE(&ctx, buf_end - len, len); 217 + HASH_FINAL(&ctx, hash); 218 + 219 + /* Check for overruns of the hash value buffer. */ 220 + HASH(test_buf, len, buf_end - HASH_SIZE); 221 + HASH_INIT(&ctx); 222 + HASH_UPDATE(&ctx, test_buf, len); 223 + HASH_FINAL(&ctx, buf_end - HASH_SIZE); 224 + 225 + /* Check for overuns of the hash context. */ 226 + HASH_INIT(guarded_ctx); 227 + HASH_UPDATE(guarded_ctx, test_buf, len); 228 + HASH_FINAL(guarded_ctx, hash); 229 + } 230 + } 231 + 232 + /* 233 + * Test that the caller is permitted to alias the output digest and source data 234 + * buffer, and also modify the source data buffer after it has been used. 235 + */ 236 + static void test_hash_overlaps(struct kunit *test) 237 + { 238 + const size_t max_tested_len = TEST_BUF_LEN - HASH_SIZE; 239 + struct HASH_CTX ctx; 240 + u8 hash[HASH_SIZE]; 241 + 242 + rand_bytes(test_buf, TEST_BUF_LEN); 243 + 244 + for (int i = 0; i < 100; i++) { 245 + size_t len = rand_length(max_tested_len); 246 + size_t offset = HASH_SIZE + rand_offset(max_tested_len - len); 247 + bool left_end = rand_bool(); 248 + u8 *ovl_hash = left_end ? &test_buf[offset] : 249 + &test_buf[offset + len - HASH_SIZE]; 250 + 251 + HASH(&test_buf[offset], len, hash); 252 + HASH(&test_buf[offset], len, ovl_hash); 253 + KUNIT_ASSERT_MEMEQ_MSG( 254 + test, hash, ovl_hash, HASH_SIZE, 255 + "Overlap test 1 failed with len=%zu offset=%zu left_end=%d", 256 + len, offset, left_end); 257 + 258 + /* Repeat the above test, but this time use init+update+final */ 259 + HASH(&test_buf[offset], len, hash); 260 + HASH_INIT(&ctx); 261 + HASH_UPDATE(&ctx, &test_buf[offset], len); 262 + HASH_FINAL(&ctx, ovl_hash); 263 + KUNIT_ASSERT_MEMEQ_MSG( 264 + test, hash, ovl_hash, HASH_SIZE, 265 + "Overlap test 2 failed with len=%zu offset=%zu left_end=%d", 266 + len, offset, left_end); 267 + 268 + /* Test modifying the source data after it was used. */ 269 + HASH(&test_buf[offset], len, hash); 270 + HASH_INIT(&ctx); 271 + HASH_UPDATE(&ctx, &test_buf[offset], len); 272 + rand_bytes(&test_buf[offset], len); 273 + HASH_FINAL(&ctx, ovl_hash); 274 + KUNIT_ASSERT_MEMEQ_MSG( 275 + test, hash, ovl_hash, HASH_SIZE, 276 + "Overlap test 3 failed with len=%zu offset=%zu left_end=%d", 277 + len, offset, left_end); 278 + } 279 + } 280 + 281 + /* 282 + * Test that if the same data is hashed at different alignments in memory, the 283 + * results are the same. 284 + */ 285 + static void test_hash_alignment_consistency(struct kunit *test) 286 + { 287 + u8 hash1[128 + HASH_SIZE]; 288 + u8 hash2[128 + HASH_SIZE]; 289 + 290 + for (int i = 0; i < 100; i++) { 291 + size_t len = rand_length(TEST_BUF_LEN); 292 + size_t data_offs1 = rand_offset(TEST_BUF_LEN - len); 293 + size_t data_offs2 = rand_offset(TEST_BUF_LEN - len); 294 + size_t hash_offs1 = rand_offset(128); 295 + size_t hash_offs2 = rand_offset(128); 296 + 297 + rand_bytes(&test_buf[data_offs1], len); 298 + HASH(&test_buf[data_offs1], len, &hash1[hash_offs1]); 299 + memmove(&test_buf[data_offs2], &test_buf[data_offs1], len); 300 + HASH(&test_buf[data_offs2], len, &hash2[hash_offs2]); 301 + KUNIT_ASSERT_MEMEQ_MSG( 302 + test, &hash1[hash_offs1], &hash2[hash_offs2], HASH_SIZE, 303 + "Alignment consistency test failed with len=%zu data_offs=(%zu,%zu) hash_offs=(%zu,%zu)", 304 + len, data_offs1, data_offs2, hash_offs1, hash_offs2); 305 + } 306 + } 307 + 308 + /* Test that HASH_FINAL zeroizes the context. */ 309 + static void test_hash_ctx_zeroization(struct kunit *test) 310 + { 311 + static const u8 zeroes[sizeof(struct HASH_CTX)]; 312 + struct HASH_CTX ctx; 313 + 314 + rand_bytes(test_buf, 128); 315 + HASH_INIT(&ctx); 316 + HASH_UPDATE(&ctx, test_buf, 128); 317 + HASH_FINAL(&ctx, test_buf); 318 + KUNIT_ASSERT_MEMEQ_MSG(test, &ctx, zeroes, sizeof(ctx), 319 + "Hash context was not zeroized by finalization"); 320 + } 321 + 322 + #define IRQ_TEST_HRTIMER_INTERVAL us_to_ktime(5) 323 + 324 + struct hash_irq_test_state { 325 + bool (*func)(void *test_specific_state); 326 + void *test_specific_state; 327 + bool task_func_reported_failure; 328 + bool hardirq_func_reported_failure; 329 + bool softirq_func_reported_failure; 330 + unsigned long hardirq_func_calls; 331 + unsigned long softirq_func_calls; 332 + struct hrtimer timer; 333 + struct work_struct bh_work; 334 + }; 335 + 336 + static enum hrtimer_restart hash_irq_test_timer_func(struct hrtimer *timer) 337 + { 338 + struct hash_irq_test_state *state = 339 + container_of(timer, typeof(*state), timer); 340 + 341 + WARN_ON_ONCE(!in_hardirq()); 342 + state->hardirq_func_calls++; 343 + 344 + if (!state->func(state->test_specific_state)) 345 + state->hardirq_func_reported_failure = true; 346 + 347 + hrtimer_forward_now(&state->timer, IRQ_TEST_HRTIMER_INTERVAL); 348 + queue_work(system_bh_wq, &state->bh_work); 349 + return HRTIMER_RESTART; 350 + } 351 + 352 + static void hash_irq_test_bh_work_func(struct work_struct *work) 353 + { 354 + struct hash_irq_test_state *state = 355 + container_of(work, typeof(*state), bh_work); 356 + 357 + WARN_ON_ONCE(!in_serving_softirq()); 358 + state->softirq_func_calls++; 359 + 360 + if (!state->func(state->test_specific_state)) 361 + state->softirq_func_reported_failure = true; 362 + } 363 + 364 + /* 365 + * Helper function which repeatedly runs the given @func in task, softirq, and 366 + * hardirq context concurrently, and reports a failure to KUnit if any 367 + * invocation of @func in any context returns false. @func is passed 368 + * @test_specific_state as its argument. At most 3 invocations of @func will 369 + * run concurrently: one in each of task, softirq, and hardirq context. 370 + * 371 + * The main purpose of this interrupt context testing is to validate fallback 372 + * code paths that run in contexts where the normal code path cannot be used, 373 + * typically due to the FPU or vector registers already being in-use in kernel 374 + * mode. These code paths aren't covered when the test code is executed only by 375 + * the KUnit test runner thread in task context. The reason for the concurrency 376 + * is because merely using hardirq context is not sufficient to reach a fallback 377 + * code path on some architectures; the hardirq actually has to occur while the 378 + * FPU or vector unit was already in-use in kernel mode. 379 + * 380 + * Another purpose of this testing is to detect issues with the architecture's 381 + * irq_fpu_usable() and kernel_fpu_begin/end() or equivalent functions, 382 + * especially in softirq context when the softirq may have interrupted a task 383 + * already using kernel-mode FPU or vector (if the arch didn't prevent that). 384 + * Crypto functions are often executed in softirqs, so this is important. 385 + */ 386 + static void run_irq_test(struct kunit *test, bool (*func)(void *), 387 + int max_iterations, void *test_specific_state) 388 + { 389 + struct hash_irq_test_state state = { 390 + .func = func, 391 + .test_specific_state = test_specific_state, 392 + }; 393 + unsigned long end_jiffies; 394 + 395 + /* 396 + * Set up a hrtimer (the way we access hardirq context) and a work 397 + * struct for the BH workqueue (the way we access softirq context). 398 + */ 399 + hrtimer_setup_on_stack(&state.timer, hash_irq_test_timer_func, 400 + CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 401 + INIT_WORK_ONSTACK(&state.bh_work, hash_irq_test_bh_work_func); 402 + 403 + /* Run for up to max_iterations or 1 second, whichever comes first. */ 404 + end_jiffies = jiffies + HZ; 405 + hrtimer_start(&state.timer, IRQ_TEST_HRTIMER_INTERVAL, 406 + HRTIMER_MODE_REL_HARD); 407 + for (int i = 0; i < max_iterations && !time_after(jiffies, end_jiffies); 408 + i++) { 409 + if (!func(test_specific_state)) 410 + state.task_func_reported_failure = true; 411 + } 412 + 413 + /* Cancel the timer and work. */ 414 + hrtimer_cancel(&state.timer); 415 + flush_work(&state.bh_work); 416 + 417 + /* Sanity check: the timer and BH functions should have been run. */ 418 + KUNIT_EXPECT_GT_MSG(test, state.hardirq_func_calls, 0, 419 + "Timer function was not called"); 420 + KUNIT_EXPECT_GT_MSG(test, state.softirq_func_calls, 0, 421 + "BH work function was not called"); 422 + 423 + /* Check for incorrect hash values reported from any context. */ 424 + KUNIT_EXPECT_FALSE_MSG( 425 + test, state.task_func_reported_failure, 426 + "Incorrect hash values reported from task context"); 427 + KUNIT_EXPECT_FALSE_MSG( 428 + test, state.hardirq_func_reported_failure, 429 + "Incorrect hash values reported from hardirq context"); 430 + KUNIT_EXPECT_FALSE_MSG( 431 + test, state.softirq_func_reported_failure, 432 + "Incorrect hash values reported from softirq context"); 433 + } 434 + 435 + #define IRQ_TEST_DATA_LEN 256 436 + #define IRQ_TEST_NUM_BUFFERS 3 /* matches max concurrency level */ 437 + 438 + struct hash_irq_test1_state { 439 + u8 expected_hashes[IRQ_TEST_NUM_BUFFERS][HASH_SIZE]; 440 + atomic_t seqno; 441 + }; 442 + 443 + /* 444 + * Compute the hash of one of the test messages and verify that it matches the 445 + * expected hash from @state->expected_hashes. To increase the chance of 446 + * detecting problems, cycle through multiple messages. 447 + */ 448 + static bool hash_irq_test1_func(void *state_) 449 + { 450 + struct hash_irq_test1_state *state = state_; 451 + u32 i = (u32)atomic_inc_return(&state->seqno) % IRQ_TEST_NUM_BUFFERS; 452 + u8 actual_hash[HASH_SIZE]; 453 + 454 + HASH(&test_buf[i * IRQ_TEST_DATA_LEN], IRQ_TEST_DATA_LEN, actual_hash); 455 + return memcmp(actual_hash, state->expected_hashes[i], HASH_SIZE) == 0; 456 + } 457 + 458 + /* 459 + * Test that if hashes are computed in task, softirq, and hardirq context 460 + * concurrently, then all results are as expected. 461 + */ 462 + static void test_hash_interrupt_context_1(struct kunit *test) 463 + { 464 + struct hash_irq_test1_state state = {}; 465 + 466 + /* Prepare some test messages and compute the expected hash of each. */ 467 + rand_bytes(test_buf, IRQ_TEST_NUM_BUFFERS * IRQ_TEST_DATA_LEN); 468 + for (int i = 0; i < IRQ_TEST_NUM_BUFFERS; i++) 469 + HASH(&test_buf[i * IRQ_TEST_DATA_LEN], IRQ_TEST_DATA_LEN, 470 + state.expected_hashes[i]); 471 + 472 + run_irq_test(test, hash_irq_test1_func, 100000, &state); 473 + } 474 + 475 + struct hash_irq_test2_hash_ctx { 476 + struct HASH_CTX hash_ctx; 477 + atomic_t in_use; 478 + int offset; 479 + int step; 480 + }; 481 + 482 + struct hash_irq_test2_state { 483 + struct hash_irq_test2_hash_ctx ctxs[IRQ_TEST_NUM_BUFFERS]; 484 + u8 expected_hash[HASH_SIZE]; 485 + u16 update_lens[32]; 486 + int num_steps; 487 + }; 488 + 489 + static bool hash_irq_test2_func(void *state_) 490 + { 491 + struct hash_irq_test2_state *state = state_; 492 + struct hash_irq_test2_hash_ctx *ctx; 493 + bool ret = true; 494 + 495 + for (ctx = &state->ctxs[0]; ctx < &state->ctxs[ARRAY_SIZE(state->ctxs)]; 496 + ctx++) { 497 + if (atomic_cmpxchg(&ctx->in_use, 0, 1) == 0) 498 + break; 499 + } 500 + if (WARN_ON_ONCE(ctx == &state->ctxs[ARRAY_SIZE(state->ctxs)])) { 501 + /* 502 + * This should never happen, as the number of contexts is equal 503 + * to the maximum concurrency level of run_irq_test(). 504 + */ 505 + return false; 506 + } 507 + 508 + if (ctx->step == 0) { 509 + /* Init step */ 510 + HASH_INIT(&ctx->hash_ctx); 511 + ctx->offset = 0; 512 + ctx->step++; 513 + } else if (ctx->step < state->num_steps - 1) { 514 + /* Update step */ 515 + HASH_UPDATE(&ctx->hash_ctx, &test_buf[ctx->offset], 516 + state->update_lens[ctx->step - 1]); 517 + ctx->offset += state->update_lens[ctx->step - 1]; 518 + ctx->step++; 519 + } else { 520 + /* Final step */ 521 + u8 actual_hash[HASH_SIZE]; 522 + 523 + if (WARN_ON_ONCE(ctx->offset != TEST_BUF_LEN)) 524 + ret = false; 525 + HASH_FINAL(&ctx->hash_ctx, actual_hash); 526 + if (memcmp(actual_hash, state->expected_hash, HASH_SIZE) != 0) 527 + ret = false; 528 + ctx->step = 0; 529 + } 530 + atomic_set_release(&ctx->in_use, 0); 531 + return ret; 532 + } 533 + 534 + /* 535 + * Test that if hashes are computed in task, softirq, and hardirq context 536 + * concurrently, *including doing different parts of the same incremental 537 + * computation in different contexts*, then all results are as expected. 538 + * Besides detecting bugs similar to those that test_hash_interrupt_context_1 539 + * can detect, this test case can also detect bugs where hash function 540 + * implementations don't correctly handle these mixed incremental computations. 541 + */ 542 + static void test_hash_interrupt_context_2(struct kunit *test) 543 + { 544 + struct hash_irq_test2_state *state; 545 + int remaining = TEST_BUF_LEN; 546 + 547 + state = kunit_kzalloc(test, sizeof(*state), GFP_KERNEL); 548 + KUNIT_ASSERT_NOT_NULL(test, state); 549 + 550 + rand_bytes(test_buf, TEST_BUF_LEN); 551 + HASH(test_buf, TEST_BUF_LEN, state->expected_hash); 552 + 553 + /* 554 + * Generate a list of update lengths to use. Ensure that it contains 555 + * multiple entries but is limited to a maximum length. 556 + */ 557 + static_assert(TEST_BUF_LEN / 4096 > 1); 558 + for (state->num_steps = 0; 559 + state->num_steps < ARRAY_SIZE(state->update_lens) - 1 && remaining; 560 + state->num_steps++) { 561 + state->update_lens[state->num_steps] = 562 + rand_length(min(remaining, 4096)); 563 + remaining -= state->update_lens[state->num_steps]; 564 + } 565 + if (remaining) 566 + state->update_lens[state->num_steps++] = remaining; 567 + state->num_steps += 2; /* for init and final */ 568 + 569 + run_irq_test(test, hash_irq_test2_func, 250000, state); 570 + } 571 + 572 + #define UNKEYED_HASH_KUNIT_CASES \ 573 + KUNIT_CASE(test_hash_test_vectors), \ 574 + KUNIT_CASE(test_hash_all_lens_up_to_4096), \ 575 + KUNIT_CASE(test_hash_incremental_updates), \ 576 + KUNIT_CASE(test_hash_buffer_overruns), \ 577 + KUNIT_CASE(test_hash_overlaps), \ 578 + KUNIT_CASE(test_hash_alignment_consistency), \ 579 + KUNIT_CASE(test_hash_ctx_zeroization), \ 580 + KUNIT_CASE(test_hash_interrupt_context_1), \ 581 + KUNIT_CASE(test_hash_interrupt_context_2) 582 + /* benchmark_hash is omitted so that the suites can put it last. */ 583 + 584 + #ifdef HMAC 585 + /* 586 + * Test the corresponding HMAC variant. 587 + * 588 + * This test case is fairly short, since HMAC is just a simple C wrapper around 589 + * the underlying unkeyed hash function, which is already well-tested by the 590 + * other test cases. It's not useful to test things like data alignment or 591 + * interrupt context again for HMAC, nor to have a long list of test vectors. 592 + * 593 + * Thus, just do a single consolidated test, which covers all data lengths up to 594 + * 4096 bytes and all key lengths up to 292 bytes. For each data length, select 595 + * a key length, generate the inputs from a seed, and compute the HMAC value. 596 + * Concatenate all these HMAC values together, and compute the HMAC of that. 597 + * Verify that value. If this fails, then the HMAC implementation is wrong. 598 + * This won't show which specific input failed, but that should be fine. Any 599 + * failure would likely be non-input-specific or also show in the unkeyed tests. 600 + */ 601 + static void test_hmac(struct kunit *test) 602 + { 603 + static const u8 zeroes[sizeof(struct HMAC_CTX)]; 604 + u8 *raw_key; 605 + struct HMAC_KEY key; 606 + struct HMAC_CTX ctx; 607 + u8 mac[HASH_SIZE]; 608 + u8 mac2[HASH_SIZE]; 609 + 610 + static_assert(TEST_BUF_LEN >= 4096 + 293); 611 + rand_bytes_seeded_from_len(test_buf, 4096); 612 + raw_key = &test_buf[4096]; 613 + 614 + rand_bytes_seeded_from_len(raw_key, 32); 615 + HMAC_PREPAREKEY(&key, raw_key, 32); 616 + HMAC_INIT(&ctx, &key); 617 + for (size_t data_len = 0; data_len <= 4096; data_len++) { 618 + /* 619 + * Cycle through key lengths as well. Somewhat arbitrarily go 620 + * up to 293, which is somewhat larger than the largest hash 621 + * block size (which is the size at which the key starts being 622 + * hashed down to one block); going higher would not be useful. 623 + * To reduce correlation with data_len, use a prime number here. 624 + */ 625 + size_t key_len = data_len % 293; 626 + 627 + HMAC_UPDATE(&ctx, test_buf, data_len); 628 + 629 + rand_bytes_seeded_from_len(raw_key, key_len); 630 + HMAC_USINGRAWKEY(raw_key, key_len, test_buf, data_len, mac); 631 + HMAC_UPDATE(&ctx, mac, HASH_SIZE); 632 + 633 + /* Verify that HMAC() is consistent with HMAC_USINGRAWKEY(). */ 634 + HMAC_PREPAREKEY(&key, raw_key, key_len); 635 + HMAC(&key, test_buf, data_len, mac2); 636 + KUNIT_ASSERT_MEMEQ_MSG( 637 + test, mac, mac2, HASH_SIZE, 638 + "HMAC gave different results with raw and prepared keys"); 639 + } 640 + HMAC_FINAL(&ctx, mac); 641 + KUNIT_EXPECT_MEMEQ_MSG(test, mac, hmac_testvec_consolidated, HASH_SIZE, 642 + "HMAC gave wrong result"); 643 + KUNIT_EXPECT_MEMEQ_MSG(test, &ctx, zeroes, sizeof(ctx), 644 + "HMAC context was not zeroized by finalization"); 645 + } 646 + #define HASH_KUNIT_CASES UNKEYED_HASH_KUNIT_CASES, KUNIT_CASE(test_hmac) 647 + #else 648 + #define HASH_KUNIT_CASES UNKEYED_HASH_KUNIT_CASES 649 + #endif 650 + 651 + /* Benchmark the hash function on various data lengths. */ 652 + static void benchmark_hash(struct kunit *test) 653 + { 654 + static const size_t lens_to_test[] = { 655 + 1, 16, 64, 127, 128, 200, 256, 656 + 511, 512, 1024, 3173, 4096, 16384, 657 + }; 658 + u8 hash[HASH_SIZE]; 659 + 660 + if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK)) 661 + kunit_skip(test, "not enabled"); 662 + 663 + /* Warm-up */ 664 + for (size_t i = 0; i < 10000000; i += TEST_BUF_LEN) 665 + HASH(test_buf, TEST_BUF_LEN, hash); 666 + 667 + for (size_t i = 0; i < ARRAY_SIZE(lens_to_test); i++) { 668 + size_t len = lens_to_test[i]; 669 + /* The '+ 128' tries to account for per-message overhead. */ 670 + size_t num_iters = 10000000 / (len + 128); 671 + u64 t; 672 + 673 + KUNIT_ASSERT_LE(test, len, TEST_BUF_LEN); 674 + preempt_disable(); 675 + t = ktime_get_ns(); 676 + for (size_t j = 0; j < num_iters; j++) 677 + HASH(test_buf, len, hash); 678 + t = ktime_get_ns() - t; 679 + preempt_enable(); 680 + kunit_info(test, "len=%zu: %llu MB/s", len, 681 + div64_u64((u64)len * num_iters * 1000, t ?: 1)); 682 + } 683 + }
+186
lib/crypto/tests/poly1305-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py poly1305 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[POLY1305_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0xe8, 0x2d, 0x67, 0x2c, 0x01, 0x48, 0xf9, 0xb7, 12 + 0x87, 0x85, 0x3f, 0xcf, 0x18, 0x66, 0x8c, 0xd3, 13 + }, 14 + }, 15 + { 16 + .data_len = 1, 17 + .digest = { 18 + 0xb8, 0xad, 0xca, 0x6b, 0x32, 0xba, 0x34, 0x42, 19 + 0x54, 0x10, 0x28, 0xf5, 0x0f, 0x7e, 0x8e, 0xe3, 20 + }, 21 + }, 22 + { 23 + .data_len = 2, 24 + .digest = { 25 + 0xb8, 0xf7, 0xf4, 0xc2, 0x85, 0x33, 0x80, 0x63, 26 + 0xd1, 0x45, 0xda, 0xf8, 0x7c, 0x79, 0x42, 0xd1, 27 + }, 28 + }, 29 + { 30 + .data_len = 3, 31 + .digest = { 32 + 0xf3, 0x73, 0x7b, 0x60, 0x24, 0xcc, 0x5d, 0x3e, 33 + 0xd1, 0x95, 0x86, 0xce, 0x89, 0x0a, 0x33, 0xba, 34 + }, 35 + }, 36 + { 37 + .data_len = 16, 38 + .digest = { 39 + 0x0a, 0x1a, 0x2d, 0x39, 0xea, 0x49, 0x8f, 0xb7, 40 + 0x90, 0xb6, 0x74, 0x3b, 0x41, 0x3b, 0x96, 0x11, 41 + }, 42 + }, 43 + { 44 + .data_len = 32, 45 + .digest = { 46 + 0x99, 0x05, 0xe3, 0xa7, 0x9e, 0x2a, 0xd2, 0x42, 47 + 0xb9, 0x45, 0x0c, 0x08, 0xe7, 0x10, 0xe4, 0xe1, 48 + }, 49 + }, 50 + { 51 + .data_len = 48, 52 + .digest = { 53 + 0xe1, 0xb2, 0x15, 0xee, 0xa2, 0xf3, 0x04, 0xac, 54 + 0xdd, 0x27, 0x57, 0x95, 0x2f, 0x45, 0xa8, 0xd3, 55 + }, 56 + }, 57 + { 58 + .data_len = 49, 59 + .digest = { 60 + 0x1c, 0xf3, 0xab, 0x39, 0xc0, 0x69, 0x49, 0x69, 61 + 0x89, 0x6f, 0x1f, 0x03, 0x16, 0xe7, 0xc0, 0xf0, 62 + }, 63 + }, 64 + { 65 + .data_len = 63, 66 + .digest = { 67 + 0x30, 0xb0, 0x32, 0x87, 0x51, 0x55, 0x9c, 0x39, 68 + 0x38, 0x42, 0x06, 0xe9, 0x2a, 0x3e, 0x2c, 0x92, 69 + }, 70 + }, 71 + { 72 + .data_len = 64, 73 + .digest = { 74 + 0x2c, 0x04, 0x16, 0x36, 0x55, 0x25, 0x2d, 0xc6, 75 + 0x3d, 0x70, 0x5b, 0x88, 0x46, 0xb6, 0x71, 0x77, 76 + }, 77 + }, 78 + { 79 + .data_len = 65, 80 + .digest = { 81 + 0x03, 0x87, 0xdd, 0xbe, 0xe8, 0x30, 0xf2, 0x15, 82 + 0x40, 0x44, 0x29, 0x7b, 0xb1, 0xe9, 0x9d, 0xe7, 83 + }, 84 + }, 85 + { 86 + .data_len = 127, 87 + .digest = { 88 + 0x29, 0x83, 0x4f, 0xcb, 0x5a, 0x93, 0x25, 0xad, 89 + 0x05, 0xa4, 0xb3, 0x24, 0x77, 0x62, 0x2d, 0x3d, 90 + }, 91 + }, 92 + { 93 + .data_len = 128, 94 + .digest = { 95 + 0x20, 0x0e, 0x2c, 0x05, 0xe2, 0x0b, 0x85, 0xa0, 96 + 0x24, 0x73, 0x7f, 0x65, 0x70, 0x6c, 0x3e, 0xb0, 97 + }, 98 + }, 99 + { 100 + .data_len = 129, 101 + .digest = { 102 + 0xef, 0x2f, 0x98, 0x42, 0xc2, 0x90, 0x55, 0xea, 103 + 0xba, 0x28, 0x76, 0xfd, 0x9e, 0x3e, 0x4d, 0x53, 104 + }, 105 + }, 106 + { 107 + .data_len = 256, 108 + .digest = { 109 + 0x9e, 0x75, 0x4b, 0xc7, 0x69, 0x68, 0x51, 0x90, 110 + 0xdc, 0x29, 0xc8, 0xfa, 0x86, 0xf1, 0xc9, 0xb3, 111 + }, 112 + }, 113 + { 114 + .data_len = 511, 115 + .digest = { 116 + 0x9d, 0x13, 0xf5, 0x54, 0xe6, 0xe3, 0x45, 0x38, 117 + 0x8b, 0x6d, 0x5c, 0xc4, 0x50, 0xeb, 0x90, 0xcb, 118 + }, 119 + }, 120 + { 121 + .data_len = 513, 122 + .digest = { 123 + 0xaa, 0xb2, 0x3e, 0x3c, 0x2a, 0xfc, 0x62, 0x0e, 124 + 0xd4, 0xe6, 0xe5, 0x5c, 0x6b, 0x9f, 0x3d, 0xc7, 125 + }, 126 + }, 127 + { 128 + .data_len = 1000, 129 + .digest = { 130 + 0xd6, 0x8c, 0xea, 0x8a, 0x0f, 0x68, 0xa9, 0xa8, 131 + 0x67, 0x86, 0xf9, 0xc1, 0x4c, 0x26, 0x10, 0x6d, 132 + }, 133 + }, 134 + { 135 + .data_len = 3333, 136 + .digest = { 137 + 0xdc, 0xc1, 0x54, 0xe7, 0x38, 0xc6, 0xdb, 0x24, 138 + 0xa7, 0x0b, 0xff, 0xd3, 0x1b, 0x93, 0x01, 0xa6, 139 + }, 140 + }, 141 + { 142 + .data_len = 4096, 143 + .digest = { 144 + 0x8f, 0x88, 0x3e, 0x9c, 0x7b, 0x2e, 0x82, 0x5a, 145 + 0x1d, 0x31, 0x82, 0xcc, 0x69, 0xb4, 0x16, 0x26, 146 + }, 147 + }, 148 + { 149 + .data_len = 4128, 150 + .digest = { 151 + 0x23, 0x45, 0x94, 0xa8, 0x11, 0x54, 0x9d, 0xf2, 152 + 0xa1, 0x9a, 0xca, 0xf9, 0x3e, 0x65, 0x52, 0xfd, 153 + }, 154 + }, 155 + { 156 + .data_len = 4160, 157 + .digest = { 158 + 0x7b, 0xfc, 0xa9, 0x1e, 0x03, 0xad, 0xef, 0x03, 159 + 0xe2, 0x20, 0x92, 0xc7, 0x54, 0x83, 0xfa, 0x37, 160 + }, 161 + }, 162 + { 163 + .data_len = 4224, 164 + .digest = { 165 + 0x46, 0xab, 0x8c, 0x75, 0xb3, 0x10, 0xa6, 0x3f, 166 + 0x74, 0x55, 0x42, 0x6d, 0x69, 0x35, 0xd2, 0xf5, 167 + }, 168 + }, 169 + { 170 + .data_len = 16384, 171 + .digest = { 172 + 0xd0, 0xfe, 0x26, 0xc2, 0xca, 0x94, 0x2d, 0x52, 173 + 0x2d, 0xe1, 0x11, 0xdd, 0x42, 0x28, 0x83, 0xa6, 174 + }, 175 + }, 176 + }; 177 + 178 + static const u8 hash_testvec_consolidated[POLY1305_DIGEST_SIZE] = { 179 + 0x9d, 0x07, 0x5d, 0xc9, 0x6c, 0xeb, 0x62, 0x5d, 180 + 0x02, 0x5f, 0xe1, 0xe3, 0xd1, 0x71, 0x69, 0x34, 181 + }; 182 + 183 + static const u8 poly1305_allones_macofmacs[POLY1305_DIGEST_SIZE] = { 184 + 0x0c, 0x26, 0x6b, 0x45, 0x87, 0x06, 0xcf, 0xc4, 185 + 0x3f, 0x70, 0x7d, 0xb3, 0x50, 0xdd, 0x81, 0x25, 186 + };
+165
lib/crypto/tests/poly1305_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/poly1305.h> 6 + #include "poly1305-testvecs.h" 7 + 8 + /* 9 + * A fixed key used when presenting Poly1305 as an unkeyed hash function in 10 + * order to reuse hash-test-template.h. At the beginning of the test suite, 11 + * this is initialized to bytes generated from a fixed seed. 12 + */ 13 + static u8 test_key[POLY1305_KEY_SIZE]; 14 + 15 + /* This probably should be in the actual API, but just define it here for now */ 16 + static void poly1305(const u8 key[POLY1305_KEY_SIZE], const u8 *data, 17 + size_t len, u8 out[POLY1305_DIGEST_SIZE]) 18 + { 19 + struct poly1305_desc_ctx ctx; 20 + 21 + poly1305_init(&ctx, key); 22 + poly1305_update(&ctx, data, len); 23 + poly1305_final(&ctx, out); 24 + } 25 + 26 + static void poly1305_init_withtestkey(struct poly1305_desc_ctx *ctx) 27 + { 28 + poly1305_init(ctx, test_key); 29 + } 30 + 31 + static void poly1305_withtestkey(const u8 *data, size_t len, 32 + u8 out[POLY1305_DIGEST_SIZE]) 33 + { 34 + poly1305(test_key, data, len, out); 35 + } 36 + 37 + /* Generate the HASH_KUNIT_CASES using hash-test-template.h. */ 38 + #define HASH poly1305_withtestkey 39 + #define HASH_CTX poly1305_desc_ctx 40 + #define HASH_SIZE POLY1305_DIGEST_SIZE 41 + #define HASH_INIT poly1305_init_withtestkey 42 + #define HASH_UPDATE poly1305_update 43 + #define HASH_FINAL poly1305_final 44 + #include "hash-test-template.h" 45 + 46 + static int poly1305_suite_init(struct kunit_suite *suite) 47 + { 48 + rand_bytes_seeded_from_len(test_key, POLY1305_KEY_SIZE); 49 + return hash_suite_init(suite); 50 + } 51 + 52 + static void poly1305_suite_exit(struct kunit_suite *suite) 53 + { 54 + hash_suite_exit(suite); 55 + } 56 + 57 + /* 58 + * Poly1305 test case which uses a key and message consisting only of one bits: 59 + * 60 + * - Using an all-one-bits r_key tests the key clamping. 61 + * - Using an all-one-bits s_key tests carries in implementations of the 62 + * addition mod 2**128 during finalization. 63 + * - Using all-one-bits message, and to a lesser extent r_key, tends to maximize 64 + * any intermediate accumulator values. This increases the chance of 65 + * detecting bugs that occur only in rare cases where the accumulator values 66 + * get very large, for example the bug fixed by commit 678cce4019d746da 67 + * ("crypto: x86/poly1305 - fix overflow during partial reduction"). 68 + * 69 + * Accumulator overflow bugs may be specific to particular update lengths (in 70 + * blocks) and/or particular values of the previous acculumator. Note that the 71 + * accumulator starts at 0 which gives the lowest chance of an overflow. Thus, 72 + * a single all-one-bits test vector may be insufficient. 73 + * 74 + * Considering that, do the following test: continuously update a single 75 + * Poly1305 context with all-one-bits data of varying lengths (0, 16, 32, ..., 76 + * 4096 bytes). After each update, generate the MAC from the current context, 77 + * and feed that MAC into a separate Poly1305 context. Repeat that entire 78 + * sequence of updates 32 times without re-initializing either context, 79 + * resulting in a total of 8224 MAC computations from a long-running, cumulative 80 + * context. Finally, generate and verify the MAC of all the MACs. 81 + */ 82 + static void test_poly1305_allones_keys_and_message(struct kunit *test) 83 + { 84 + struct poly1305_desc_ctx mac_ctx, macofmacs_ctx; 85 + u8 mac[POLY1305_DIGEST_SIZE]; 86 + 87 + static_assert(TEST_BUF_LEN >= 4096); 88 + memset(test_buf, 0xff, 4096); 89 + 90 + poly1305_init(&mac_ctx, test_buf); 91 + poly1305_init(&macofmacs_ctx, test_buf); 92 + for (int i = 0; i < 32; i++) { 93 + for (size_t len = 0; len <= 4096; len += 16) { 94 + struct poly1305_desc_ctx tmp_ctx; 95 + 96 + poly1305_update(&mac_ctx, test_buf, len); 97 + tmp_ctx = mac_ctx; 98 + poly1305_final(&tmp_ctx, mac); 99 + poly1305_update(&macofmacs_ctx, mac, 100 + POLY1305_DIGEST_SIZE); 101 + } 102 + } 103 + poly1305_final(&macofmacs_ctx, mac); 104 + KUNIT_ASSERT_MEMEQ(test, mac, poly1305_allones_macofmacs, 105 + POLY1305_DIGEST_SIZE); 106 + } 107 + 108 + /* 109 + * Poly1305 test case which uses r_key=1, s_key=0, and a 48-byte message 110 + * consisting of three blocks with integer values [2**128 - i, 0, 0]. In this 111 + * case, the result of the polynomial evaluation is 2**130 - i. For small 112 + * values of i, this is very close to the modulus 2**130 - 5, which helps catch 113 + * edge case bugs in the modular reduction logic. 114 + */ 115 + static void test_poly1305_reduction_edge_cases(struct kunit *test) 116 + { 117 + static const u8 key[POLY1305_KEY_SIZE] = { 1 }; /* r_key=1, s_key=0 */ 118 + u8 data[3 * POLY1305_BLOCK_SIZE] = {}; 119 + u8 expected_mac[POLY1305_DIGEST_SIZE]; 120 + u8 actual_mac[POLY1305_DIGEST_SIZE]; 121 + 122 + for (int i = 1; i <= 10; i++) { 123 + /* Set the first data block to 2**128 - i. */ 124 + data[0] = -i; 125 + memset(&data[1], 0xff, POLY1305_BLOCK_SIZE - 1); 126 + 127 + /* 128 + * Assuming s_key=0, the expected MAC as an integer is 129 + * (2**130 - i mod 2**130 - 5) + 0 mod 2**128. If 1 <= i <= 5, 130 + * that's 5 - i. If 6 <= i <= 10, that's 2**128 - i. 131 + */ 132 + if (i <= 5) { 133 + expected_mac[0] = 5 - i; 134 + memset(&expected_mac[1], 0, POLY1305_DIGEST_SIZE - 1); 135 + } else { 136 + expected_mac[0] = -i; 137 + memset(&expected_mac[1], 0xff, 138 + POLY1305_DIGEST_SIZE - 1); 139 + } 140 + 141 + /* Compute and verify the MAC. */ 142 + poly1305(key, data, sizeof(data), actual_mac); 143 + KUNIT_ASSERT_MEMEQ(test, actual_mac, expected_mac, 144 + POLY1305_DIGEST_SIZE); 145 + } 146 + } 147 + 148 + static struct kunit_case poly1305_test_cases[] = { 149 + HASH_KUNIT_CASES, 150 + KUNIT_CASE(test_poly1305_allones_keys_and_message), 151 + KUNIT_CASE(test_poly1305_reduction_edge_cases), 152 + KUNIT_CASE(benchmark_hash), 153 + {}, 154 + }; 155 + 156 + static struct kunit_suite poly1305_test_suite = { 157 + .name = "poly1305", 158 + .test_cases = poly1305_test_cases, 159 + .suite_init = poly1305_suite_init, 160 + .suite_exit = poly1305_suite_exit, 161 + }; 162 + kunit_test_suite(poly1305_test_suite); 163 + 164 + MODULE_DESCRIPTION("KUnit tests and benchmark for Poly1305"); 165 + MODULE_LICENSE("GPL");
+212
lib/crypto/tests/sha1-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha1 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[SHA1_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 12 + 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 13 + 0xaf, 0xd8, 0x07, 0x09, 14 + }, 15 + }, 16 + { 17 + .data_len = 1, 18 + .digest = { 19 + 0x0a, 0xd0, 0x52, 0xdd, 0x9f, 0x32, 0x40, 0x55, 20 + 0x21, 0xe4, 0x3c, 0x6e, 0xbd, 0xc5, 0x2f, 0x5a, 21 + 0x02, 0x54, 0x93, 0xb2, 22 + }, 23 + }, 24 + { 25 + .data_len = 2, 26 + .digest = { 27 + 0x13, 0x83, 0x82, 0x03, 0x23, 0xff, 0x46, 0xd6, 28 + 0x12, 0x7f, 0xad, 0x05, 0x2b, 0xc3, 0x4a, 0x42, 29 + 0x49, 0x6a, 0xf8, 0x84, 30 + }, 31 + }, 32 + { 33 + .data_len = 3, 34 + .digest = { 35 + 0xe4, 0xdf, 0x7b, 0xdc, 0xe8, 0x6e, 0x81, 0x97, 36 + 0x1e, 0x0f, 0xe8, 0x8b, 0x76, 0xa8, 0x59, 0x04, 37 + 0xae, 0x92, 0x1a, 0x7c, 38 + }, 39 + }, 40 + { 41 + .data_len = 16, 42 + .digest = { 43 + 0x8c, 0x1c, 0x30, 0xd8, 0xbc, 0xc4, 0xc3, 0xf5, 44 + 0xf8, 0x83, 0x0d, 0x1e, 0x04, 0x5d, 0x29, 0xb5, 45 + 0x68, 0x89, 0xc1, 0xe9, 46 + }, 47 + }, 48 + { 49 + .data_len = 32, 50 + .digest = { 51 + 0x6c, 0x1d, 0x72, 0x31, 0xa5, 0x03, 0x4f, 0xdc, 52 + 0xff, 0x2d, 0x06, 0x3e, 0x24, 0x26, 0x34, 0x8d, 53 + 0x60, 0xa4, 0x67, 0x16, 54 + }, 55 + }, 56 + { 57 + .data_len = 48, 58 + .digest = { 59 + 0x37, 0x53, 0x33, 0xfa, 0xd0, 0x21, 0xad, 0xe7, 60 + 0xa5, 0x43, 0xf1, 0x94, 0x64, 0x11, 0x47, 0x9c, 61 + 0x72, 0xb5, 0x78, 0xb4, 62 + }, 63 + }, 64 + { 65 + .data_len = 49, 66 + .digest = { 67 + 0x51, 0x5c, 0xd8, 0x5a, 0xa9, 0xde, 0x7b, 0x2a, 68 + 0xa2, 0xff, 0x70, 0x09, 0x56, 0x88, 0x40, 0x2b, 69 + 0x50, 0x93, 0x82, 0x47, 70 + }, 71 + }, 72 + { 73 + .data_len = 63, 74 + .digest = { 75 + 0xbc, 0x9c, 0xab, 0x93, 0x06, 0xd5, 0xdb, 0xac, 76 + 0x2c, 0x33, 0x15, 0x83, 0x56, 0xf6, 0x91, 0x20, 77 + 0x09, 0xc7, 0xb2, 0x6b, 78 + }, 79 + }, 80 + { 81 + .data_len = 64, 82 + .digest = { 83 + 0x26, 0x90, 0x3b, 0x47, 0xe1, 0x92, 0x42, 0xd0, 84 + 0x85, 0x63, 0x2e, 0x6b, 0x68, 0xa4, 0xc4, 0x4c, 85 + 0xe6, 0xf4, 0xb0, 0x52, 86 + }, 87 + }, 88 + { 89 + .data_len = 65, 90 + .digest = { 91 + 0x55, 0x6f, 0x87, 0xdc, 0x34, 0x3d, 0xe2, 0x4f, 92 + 0xc3, 0x81, 0xa4, 0x82, 0x79, 0x84, 0x64, 0x01, 93 + 0x55, 0xa0, 0x1e, 0x36, 94 + }, 95 + }, 96 + { 97 + .data_len = 127, 98 + .digest = { 99 + 0xb7, 0xd5, 0x5f, 0xa4, 0xef, 0xbf, 0x4f, 0x96, 100 + 0x01, 0xc1, 0x06, 0xe3, 0x75, 0xa8, 0x90, 0x92, 101 + 0x4c, 0x5f, 0xf1, 0x21, 102 + }, 103 + }, 104 + { 105 + .data_len = 128, 106 + .digest = { 107 + 0x70, 0x0c, 0xea, 0xa4, 0x93, 0xd0, 0x56, 0xf0, 108 + 0x6f, 0xbb, 0x53, 0x42, 0x5b, 0xe3, 0xf2, 0xb0, 109 + 0x30, 0x66, 0x8e, 0x75, 110 + }, 111 + }, 112 + { 113 + .data_len = 129, 114 + .digest = { 115 + 0x15, 0x01, 0xbc, 0xb0, 0xee, 0xd8, 0xeb, 0xa8, 116 + 0x7d, 0xd9, 0x4d, 0x50, 0x2e, 0x41, 0x30, 0xba, 117 + 0x41, 0xaa, 0x7b, 0x02, 118 + }, 119 + }, 120 + { 121 + .data_len = 256, 122 + .digest = { 123 + 0x98, 0x05, 0x52, 0xf5, 0x0f, 0xf0, 0xd3, 0x97, 124 + 0x15, 0x8c, 0xa3, 0x9a, 0x2b, 0x4d, 0x67, 0x57, 125 + 0x29, 0xa0, 0xac, 0x61, 126 + }, 127 + }, 128 + { 129 + .data_len = 511, 130 + .digest = { 131 + 0x1f, 0x47, 0xf0, 0xcc, 0xd7, 0xda, 0xa5, 0x3b, 132 + 0x39, 0xb4, 0x5b, 0xa8, 0x33, 0xd4, 0xca, 0x2f, 133 + 0xdd, 0xf2, 0x39, 0x89, 134 + }, 135 + }, 136 + { 137 + .data_len = 513, 138 + .digest = { 139 + 0xb9, 0x75, 0xe6, 0x57, 0x42, 0x7f, 0x8b, 0x0a, 140 + 0xcc, 0x53, 0x10, 0x69, 0x45, 0xac, 0xfd, 0x11, 141 + 0xf7, 0x1f, 0x4e, 0x6f, 142 + }, 143 + }, 144 + { 145 + .data_len = 1000, 146 + .digest = { 147 + 0x63, 0x66, 0xcb, 0x44, 0xc1, 0x2c, 0xa2, 0x06, 148 + 0x5d, 0xb9, 0x8e, 0x31, 0xcb, 0x4f, 0x4e, 0x49, 149 + 0xe0, 0xfb, 0x3c, 0x4e, 150 + }, 151 + }, 152 + { 153 + .data_len = 3333, 154 + .digest = { 155 + 0x35, 0xbc, 0x74, 0xfb, 0x31, 0x9c, 0xd4, 0xdd, 156 + 0xe8, 0x87, 0xa7, 0x56, 0x3b, 0x08, 0xe5, 0x49, 157 + 0xe1, 0xe9, 0xc9, 0xa8, 158 + }, 159 + }, 160 + { 161 + .data_len = 4096, 162 + .digest = { 163 + 0x43, 0x00, 0xea, 0xcd, 0x4e, 0x7c, 0xe9, 0xe4, 164 + 0x32, 0xce, 0x25, 0xa8, 0xcd, 0x20, 0xa8, 0xaa, 165 + 0x7b, 0x63, 0x2c, 0x3c, 166 + }, 167 + }, 168 + { 169 + .data_len = 4128, 170 + .digest = { 171 + 0xd0, 0x67, 0x26, 0x0e, 0x22, 0x72, 0xaa, 0x63, 172 + 0xfc, 0x34, 0x55, 0x07, 0xab, 0xc8, 0x64, 0xb6, 173 + 0xc4, 0xea, 0xd5, 0x7c, 174 + }, 175 + }, 176 + { 177 + .data_len = 4160, 178 + .digest = { 179 + 0x6b, 0xc9, 0x5e, 0xb9, 0x41, 0x19, 0x50, 0x35, 180 + 0xf1, 0x39, 0xfe, 0xd9, 0x72, 0x6d, 0xd0, 0x55, 181 + 0xb8, 0x1f, 0x1a, 0x95, 182 + }, 183 + }, 184 + { 185 + .data_len = 4224, 186 + .digest = { 187 + 0x70, 0x5d, 0x10, 0x2e, 0x4e, 0x44, 0xc9, 0x80, 188 + 0x8f, 0xba, 0x13, 0xbc, 0xd0, 0x77, 0x78, 0xc7, 189 + 0x84, 0xe3, 0x24, 0x43, 190 + }, 191 + }, 192 + { 193 + .data_len = 16384, 194 + .digest = { 195 + 0xa8, 0x82, 0xca, 0x08, 0xb4, 0x84, 0x09, 0x13, 196 + 0xc0, 0x9c, 0x26, 0x18, 0xcf, 0x0f, 0xf3, 0x08, 197 + 0xff, 0xa1, 0xe4, 0x5d, 198 + }, 199 + }, 200 + }; 201 + 202 + static const u8 hash_testvec_consolidated[SHA1_DIGEST_SIZE] = { 203 + 0xe1, 0x72, 0xa5, 0x3c, 0xda, 0xf2, 0xe5, 0x56, 204 + 0xb8, 0xb5, 0x35, 0x6e, 0xce, 0xc8, 0x37, 0x57, 205 + 0x31, 0xb4, 0x05, 0xdd, 206 + }; 207 + 208 + static const u8 hmac_testvec_consolidated[SHA1_DIGEST_SIZE] = { 209 + 0x9d, 0xe5, 0xb1, 0x43, 0x97, 0x95, 0x16, 0x52, 210 + 0xa0, 0x7a, 0xc0, 0xe2, 0xc1, 0x60, 0x64, 0x7c, 211 + 0x24, 0xf9, 0x34, 0xd7, 212 + };
+39
lib/crypto/tests/sha1_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/sha1.h> 6 + #include "sha1-testvecs.h" 7 + 8 + #define HASH sha1 9 + #define HASH_CTX sha1_ctx 10 + #define HASH_SIZE SHA1_DIGEST_SIZE 11 + #define HASH_INIT sha1_init 12 + #define HASH_UPDATE sha1_update 13 + #define HASH_FINAL sha1_final 14 + #define HMAC_KEY hmac_sha1_key 15 + #define HMAC_CTX hmac_sha1_ctx 16 + #define HMAC_PREPAREKEY hmac_sha1_preparekey 17 + #define HMAC_INIT hmac_sha1_init 18 + #define HMAC_UPDATE hmac_sha1_update 19 + #define HMAC_FINAL hmac_sha1_final 20 + #define HMAC hmac_sha1 21 + #define HMAC_USINGRAWKEY hmac_sha1_usingrawkey 22 + #include "hash-test-template.h" 23 + 24 + static struct kunit_case hash_test_cases[] = { 25 + HASH_KUNIT_CASES, 26 + KUNIT_CASE(benchmark_hash), 27 + {}, 28 + }; 29 + 30 + static struct kunit_suite hash_test_suite = { 31 + .name = "sha1", 32 + .test_cases = hash_test_cases, 33 + .suite_init = hash_suite_init, 34 + .suite_exit = hash_suite_exit, 35 + }; 36 + kunit_test_suite(hash_test_suite); 37 + 38 + MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-1 and HMAC-SHA1"); 39 + MODULE_LICENSE("GPL");
+238
lib/crypto/tests/sha224-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha224 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[SHA224_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 12 + 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 13 + 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 14 + 0xc5, 0xb3, 0xe4, 0x2f, 15 + }, 16 + }, 17 + { 18 + .data_len = 1, 19 + .digest = { 20 + 0xe3, 0x4d, 0x79, 0x17, 0x75, 0x35, 0xdc, 0xd2, 21 + 0x27, 0xc9, 0x9d, 0x0b, 0x90, 0x0f, 0x21, 0x5d, 22 + 0x95, 0xfb, 0x9c, 0x6d, 0xa8, 0xec, 0x19, 0x15, 23 + 0x12, 0xef, 0xf5, 0x0f, 24 + }, 25 + }, 26 + { 27 + .data_len = 2, 28 + .digest = { 29 + 0x81, 0xc7, 0x60, 0x0d, 0x6d, 0x13, 0x75, 0x70, 30 + 0x4b, 0xc0, 0xab, 0xea, 0x04, 0xe3, 0x78, 0x7e, 31 + 0x73, 0xb9, 0x0f, 0xb6, 0xae, 0x90, 0xf3, 0x94, 32 + 0xb2, 0x56, 0xda, 0xc8, 33 + }, 34 + }, 35 + { 36 + .data_len = 3, 37 + .digest = { 38 + 0x24, 0xf0, 0x8c, 0x6e, 0x9d, 0xd6, 0x06, 0x80, 39 + 0x0a, 0x03, 0xee, 0x9b, 0x33, 0xec, 0x83, 0x42, 40 + 0x2c, 0x8b, 0xe7, 0xc7, 0xc6, 0x04, 0xfb, 0xc6, 41 + 0xa3, 0x3a, 0x4d, 0xc9, 42 + }, 43 + }, 44 + { 45 + .data_len = 16, 46 + .digest = { 47 + 0x1c, 0x08, 0xa8, 0x55, 0x8f, 0xc6, 0x0a, 0xea, 48 + 0x2f, 0x1b, 0x54, 0xff, 0x8d, 0xd2, 0xa3, 0xc7, 49 + 0x42, 0xc2, 0x93, 0x3d, 0x73, 0x18, 0x84, 0xba, 50 + 0x75, 0x49, 0x34, 0xfd, 51 + }, 52 + }, 53 + { 54 + .data_len = 32, 55 + .digest = { 56 + 0x45, 0xdd, 0xb5, 0xf0, 0x3c, 0xda, 0xe6, 0xd4, 57 + 0x6c, 0x86, 0x91, 0x29, 0x11, 0x2f, 0x88, 0x7d, 58 + 0xd8, 0x3c, 0xa3, 0xd6, 0xdd, 0x1e, 0xac, 0x98, 59 + 0xff, 0xf0, 0x14, 0x69, 60 + }, 61 + }, 62 + { 63 + .data_len = 48, 64 + .digest = { 65 + 0x0b, 0xfb, 0x71, 0x4c, 0x06, 0x7a, 0xd5, 0x89, 66 + 0x76, 0x0a, 0x43, 0x8b, 0x2b, 0x47, 0x12, 0x56, 67 + 0xa7, 0x64, 0x33, 0x1d, 0xd3, 0x44, 0x17, 0x95, 68 + 0x23, 0xe7, 0x53, 0x01, 69 + }, 70 + }, 71 + { 72 + .data_len = 49, 73 + .digest = { 74 + 0xc4, 0xae, 0x9c, 0x33, 0xd5, 0x1d, 0xf4, 0xa7, 75 + 0xfd, 0xb7, 0xd4, 0x6b, 0xc3, 0xeb, 0xa8, 0xbf, 76 + 0xfb, 0x07, 0x89, 0x4b, 0x07, 0x15, 0x22, 0xec, 77 + 0xe1, 0x45, 0x84, 0xba, 78 + }, 79 + }, 80 + { 81 + .data_len = 63, 82 + .digest = { 83 + 0xad, 0x01, 0x34, 0x2a, 0xe2, 0x3b, 0x58, 0x06, 84 + 0x9f, 0x20, 0xc8, 0xfb, 0xf3, 0x20, 0x82, 0xa6, 85 + 0x9f, 0xee, 0x7a, 0xbe, 0xdf, 0xf3, 0x5d, 0x57, 86 + 0x9b, 0xce, 0x79, 0x96, 87 + }, 88 + }, 89 + { 90 + .data_len = 64, 91 + .digest = { 92 + 0xa7, 0xa6, 0x47, 0xf7, 0xed, 0x2a, 0xe5, 0xe3, 93 + 0xc0, 0x1e, 0x7b, 0x40, 0xe4, 0xf7, 0x40, 0x65, 94 + 0x42, 0xc1, 0x6f, 0x7d, 0x8d, 0x0d, 0x17, 0x4f, 95 + 0xd3, 0xbc, 0x0d, 0x85, 96 + }, 97 + }, 98 + { 99 + .data_len = 65, 100 + .digest = { 101 + 0xc4, 0x9c, 0xb5, 0x6a, 0x01, 0x2d, 0x10, 0xa9, 102 + 0x5f, 0xa4, 0x5a, 0xe1, 0xba, 0x40, 0x12, 0x09, 103 + 0x7b, 0xea, 0xdb, 0xa6, 0x7b, 0xcb, 0x56, 0xf0, 104 + 0xfd, 0x5b, 0xe2, 0xe7, 105 + }, 106 + }, 107 + { 108 + .data_len = 127, 109 + .digest = { 110 + 0x14, 0xda, 0x0e, 0x01, 0xca, 0x78, 0x7d, 0x2d, 111 + 0x85, 0xa3, 0xca, 0x0e, 0x80, 0xf9, 0x95, 0x10, 112 + 0xa1, 0x7b, 0xa5, 0xaa, 0xfc, 0x95, 0x05, 0x08, 113 + 0x53, 0xda, 0x52, 0xee, 114 + }, 115 + }, 116 + { 117 + .data_len = 128, 118 + .digest = { 119 + 0xa5, 0x24, 0xc4, 0x54, 0xe1, 0x50, 0xab, 0xee, 120 + 0x22, 0xc1, 0xa7, 0x27, 0x15, 0x2c, 0x6f, 0xf7, 121 + 0x4c, 0x31, 0xe5, 0x15, 0x25, 0x4e, 0x71, 0xc6, 122 + 0x7e, 0xa0, 0x11, 0x5d, 123 + }, 124 + }, 125 + { 126 + .data_len = 129, 127 + .digest = { 128 + 0x73, 0xd0, 0x8c, 0xce, 0xed, 0xed, 0x9f, 0xaa, 129 + 0x21, 0xaf, 0xa2, 0x08, 0x80, 0x16, 0x15, 0x59, 130 + 0x3f, 0x1d, 0x7f, 0x0a, 0x79, 0x3d, 0x7b, 0x58, 131 + 0xf8, 0xc8, 0x5c, 0x27, 132 + }, 133 + }, 134 + { 135 + .data_len = 256, 136 + .digest = { 137 + 0x31, 0xa7, 0xa1, 0xca, 0x49, 0x72, 0x75, 0xcc, 138 + 0x6e, 0x02, 0x9e, 0xad, 0xea, 0x86, 0x5c, 0x91, 139 + 0x02, 0xe4, 0xc9, 0xf9, 0xd3, 0x9e, 0x74, 0x50, 140 + 0xd8, 0x43, 0x6b, 0x85, 141 + }, 142 + }, 143 + { 144 + .data_len = 511, 145 + .digest = { 146 + 0x40, 0x60, 0x8b, 0xb0, 0x03, 0xa9, 0x75, 0xab, 147 + 0x2d, 0x5b, 0x20, 0x9a, 0x05, 0x72, 0xb7, 0xa8, 148 + 0xce, 0xf2, 0x4f, 0x66, 0x62, 0xe3, 0x7e, 0x24, 149 + 0xd6, 0xe2, 0xea, 0xfa, 150 + }, 151 + }, 152 + { 153 + .data_len = 513, 154 + .digest = { 155 + 0x4f, 0x5f, 0x9f, 0x1e, 0xb3, 0x66, 0x81, 0xdb, 156 + 0x41, 0x5d, 0x65, 0x97, 0x00, 0x8d, 0xdc, 0x62, 157 + 0x03, 0xb0, 0x4d, 0x6b, 0x5c, 0x7f, 0x1e, 0xa0, 158 + 0xfe, 0xfc, 0x0e, 0xb8, 159 + }, 160 + }, 161 + { 162 + .data_len = 1000, 163 + .digest = { 164 + 0x08, 0xa8, 0xa1, 0xc0, 0xd8, 0xf9, 0xb4, 0xaa, 165 + 0x53, 0x22, 0xa1, 0x73, 0x0b, 0x45, 0xa0, 0x20, 166 + 0x72, 0xf3, 0xa9, 0xbc, 0x51, 0xd0, 0x20, 0x79, 167 + 0x69, 0x97, 0xf7, 0xe3, 168 + }, 169 + }, 170 + { 171 + .data_len = 3333, 172 + .digest = { 173 + 0xe8, 0x60, 0x5f, 0xb9, 0x12, 0xe1, 0x6b, 0x24, 174 + 0xc5, 0xe8, 0x43, 0xa9, 0x5c, 0x3f, 0x65, 0xed, 175 + 0xbe, 0xfd, 0x77, 0xf5, 0x47, 0xf2, 0x75, 0x21, 176 + 0xc2, 0x8f, 0x54, 0x8f, 177 + }, 178 + }, 179 + { 180 + .data_len = 4096, 181 + .digest = { 182 + 0xc7, 0xdf, 0x50, 0x16, 0x10, 0x01, 0xb7, 0xdf, 183 + 0x34, 0x1d, 0x18, 0xa2, 0xd5, 0xad, 0x1f, 0x50, 184 + 0xf7, 0xa8, 0x9a, 0x72, 0xfb, 0xfd, 0xd9, 0x1c, 185 + 0x57, 0xac, 0x08, 0x97, 186 + }, 187 + }, 188 + { 189 + .data_len = 4128, 190 + .digest = { 191 + 0xdf, 0x16, 0x76, 0x7f, 0xc0, 0x16, 0x84, 0x63, 192 + 0xac, 0xcf, 0xd0, 0x78, 0x1e, 0x96, 0x67, 0xc5, 193 + 0x3c, 0x06, 0xe9, 0xdb, 0x6e, 0x7d, 0xd0, 0x07, 194 + 0xaa, 0xb1, 0x56, 0xc9, 195 + }, 196 + }, 197 + { 198 + .data_len = 4160, 199 + .digest = { 200 + 0x49, 0xec, 0x5c, 0x18, 0xd7, 0x5b, 0xda, 0xed, 201 + 0x5b, 0x59, 0xde, 0x09, 0x34, 0xb2, 0x49, 0x43, 202 + 0x62, 0x6a, 0x0a, 0x63, 0x6a, 0x51, 0x08, 0x37, 203 + 0x8c, 0xb6, 0x29, 0x84, 204 + }, 205 + }, 206 + { 207 + .data_len = 4224, 208 + .digest = { 209 + 0x3d, 0xc2, 0xc8, 0x43, 0xcf, 0xb7, 0x33, 0x14, 210 + 0x04, 0x93, 0xed, 0xe2, 0xcd, 0x8a, 0x69, 0x5c, 211 + 0x5a, 0xd5, 0x9b, 0x52, 0xdf, 0x48, 0xa7, 0xaa, 212 + 0x28, 0x2b, 0x5d, 0x27, 213 + }, 214 + }, 215 + { 216 + .data_len = 16384, 217 + .digest = { 218 + 0xa7, 0xaf, 0xda, 0x92, 0xe2, 0xe7, 0x61, 0xdc, 219 + 0xa1, 0x32, 0x53, 0x2a, 0x3f, 0x41, 0x5c, 0x7e, 220 + 0xc9, 0x89, 0xda, 0x1c, 0xf7, 0x8d, 0x00, 0xbd, 221 + 0x21, 0x73, 0xb1, 0x69, 222 + }, 223 + }, 224 + }; 225 + 226 + static const u8 hash_testvec_consolidated[SHA224_DIGEST_SIZE] = { 227 + 0x9e, 0xb8, 0x82, 0xab, 0x83, 0x37, 0xe4, 0x63, 228 + 0x84, 0xee, 0x21, 0x15, 0xc2, 0xbb, 0xa3, 0x17, 229 + 0x8f, 0xc4, 0x99, 0x33, 0xa0, 0x2c, 0x9f, 0xec, 230 + 0xca, 0xd0, 0xf3, 0x73, 231 + }; 232 + 233 + static const u8 hmac_testvec_consolidated[SHA224_DIGEST_SIZE] = { 234 + 0x66, 0x34, 0x79, 0x92, 0x47, 0x0e, 0xcd, 0x70, 235 + 0xb0, 0x8b, 0x91, 0xcb, 0x94, 0x2f, 0x67, 0x65, 236 + 0x2f, 0xc9, 0xd2, 0x91, 0x32, 0xaf, 0xf7, 0x5f, 237 + 0xb6, 0x01, 0x5b, 0xf2, 238 + };
+39
lib/crypto/tests/sha224_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/sha2.h> 6 + #include "sha224-testvecs.h" 7 + 8 + #define HASH sha224 9 + #define HASH_CTX sha224_ctx 10 + #define HASH_SIZE SHA224_DIGEST_SIZE 11 + #define HASH_INIT sha224_init 12 + #define HASH_UPDATE sha224_update 13 + #define HASH_FINAL sha224_final 14 + #define HMAC_KEY hmac_sha224_key 15 + #define HMAC_CTX hmac_sha224_ctx 16 + #define HMAC_PREPAREKEY hmac_sha224_preparekey 17 + #define HMAC_INIT hmac_sha224_init 18 + #define HMAC_UPDATE hmac_sha224_update 19 + #define HMAC_FINAL hmac_sha224_final 20 + #define HMAC hmac_sha224 21 + #define HMAC_USINGRAWKEY hmac_sha224_usingrawkey 22 + #include "hash-test-template.h" 23 + 24 + static struct kunit_case hash_test_cases[] = { 25 + HASH_KUNIT_CASES, 26 + KUNIT_CASE(benchmark_hash), 27 + {}, 28 + }; 29 + 30 + static struct kunit_suite hash_test_suite = { 31 + .name = "sha224", 32 + .test_cases = hash_test_cases, 33 + .suite_init = hash_suite_init, 34 + .suite_exit = hash_suite_exit, 35 + }; 36 + kunit_test_suite(hash_test_suite); 37 + 38 + MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-224 and HMAC-SHA224"); 39 + MODULE_LICENSE("GPL");
+238
lib/crypto/tests/sha256-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha256 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[SHA256_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 12 + 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 13 + 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 14 + 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 15 + }, 16 + }, 17 + { 18 + .data_len = 1, 19 + .digest = { 20 + 0x45, 0xf8, 0x3d, 0x17, 0xe1, 0x0b, 0x34, 0xfc, 21 + 0xa0, 0x1e, 0xb8, 0xf4, 0x45, 0x4d, 0xac, 0x34, 22 + 0xa7, 0x77, 0xd9, 0x40, 0x4a, 0x46, 0x4e, 0x73, 23 + 0x2c, 0xf4, 0xab, 0xf2, 0xc0, 0xda, 0x94, 0xc4, 24 + }, 25 + }, 26 + { 27 + .data_len = 2, 28 + .digest = { 29 + 0xf9, 0xd3, 0x52, 0x2f, 0xd5, 0xe0, 0x99, 0x15, 30 + 0x1c, 0xd6, 0xa9, 0x24, 0x4f, 0x40, 0xba, 0x25, 31 + 0x33, 0x43, 0x3e, 0xe1, 0x78, 0x6a, 0xfe, 0x7d, 32 + 0x07, 0xe2, 0x29, 0x7b, 0x6d, 0xc5, 0x73, 0xf5, 33 + }, 34 + }, 35 + { 36 + .data_len = 3, 37 + .digest = { 38 + 0x71, 0xf7, 0xa1, 0xef, 0x69, 0x86, 0x0e, 0xe4, 39 + 0x87, 0x25, 0x58, 0x4c, 0x07, 0x2c, 0xfc, 0x60, 40 + 0xc5, 0xf6, 0xe2, 0x44, 0xaa, 0xfb, 0x41, 0xc7, 41 + 0x2b, 0xc5, 0x01, 0x8c, 0x39, 0x98, 0x30, 0x37, 42 + }, 43 + }, 44 + { 45 + .data_len = 16, 46 + .digest = { 47 + 0x09, 0x95, 0x9a, 0xfa, 0x25, 0x18, 0x86, 0x06, 48 + 0xfe, 0x65, 0xc9, 0x2f, 0x91, 0x15, 0x74, 0x06, 49 + 0x6c, 0xbf, 0xef, 0x7b, 0x0b, 0xc7, 0x2c, 0x05, 50 + 0xdd, 0x17, 0x5d, 0x6f, 0x8a, 0xa5, 0xde, 0x3c, 51 + }, 52 + }, 53 + { 54 + .data_len = 32, 55 + .digest = { 56 + 0xe5, 0x52, 0x3c, 0x85, 0xea, 0x1b, 0xe1, 0x6c, 57 + 0xe0, 0xdb, 0xc3, 0xef, 0xf0, 0xca, 0xc2, 0xe1, 58 + 0xb9, 0x36, 0xa1, 0x28, 0xb6, 0x9e, 0xf5, 0x6e, 59 + 0x70, 0xf7, 0xf9, 0xa7, 0x1c, 0xd3, 0x22, 0xd0, 60 + }, 61 + }, 62 + { 63 + .data_len = 48, 64 + .digest = { 65 + 0x5f, 0x84, 0xd4, 0xd7, 0x2e, 0x80, 0x09, 0xef, 66 + 0x1c, 0x77, 0x7c, 0x25, 0x59, 0x63, 0x88, 0x64, 67 + 0xfd, 0x56, 0xea, 0x23, 0xf4, 0x4f, 0x2e, 0x49, 68 + 0xcd, 0xb4, 0xaa, 0xc7, 0x5c, 0x8b, 0x75, 0x84, 69 + }, 70 + }, 71 + { 72 + .data_len = 49, 73 + .digest = { 74 + 0x22, 0x6e, 0xca, 0xda, 0x00, 0x2d, 0x90, 0x96, 75 + 0x24, 0xf8, 0x55, 0x17, 0x11, 0xda, 0x42, 0x1c, 76 + 0x78, 0x4e, 0xbf, 0xd9, 0xc5, 0xcf, 0xf3, 0xe3, 77 + 0xaf, 0xd3, 0x60, 0xcd, 0xaa, 0xe2, 0xc7, 0x22, 78 + }, 79 + }, 80 + { 81 + .data_len = 63, 82 + .digest = { 83 + 0x97, 0xe2, 0x74, 0xdc, 0x6b, 0xa4, 0xaf, 0x32, 84 + 0x3b, 0x50, 0x6d, 0x80, 0xb5, 0xd3, 0x0c, 0x36, 85 + 0xea, 0x3f, 0x5d, 0x36, 0xa7, 0x49, 0x51, 0xf3, 86 + 0xbd, 0x69, 0x68, 0x60, 0x9b, 0xde, 0x73, 0xf5, 87 + }, 88 + }, 89 + { 90 + .data_len = 64, 91 + .digest = { 92 + 0x13, 0x74, 0xb1, 0x72, 0xd6, 0x53, 0x48, 0x28, 93 + 0x42, 0xd8, 0xba, 0x64, 0x20, 0x60, 0xb6, 0x4c, 94 + 0xc3, 0xac, 0x5d, 0x93, 0x8c, 0xb9, 0xd4, 0xcc, 95 + 0xb4, 0x9f, 0x31, 0x1f, 0xeb, 0x68, 0x35, 0x58, 96 + }, 97 + }, 98 + { 99 + .data_len = 65, 100 + .digest = { 101 + 0xda, 0xbe, 0xd7, 0xbc, 0x6e, 0xe6, 0x5a, 0x57, 102 + 0xeb, 0x9a, 0x93, 0xaa, 0x66, 0xd0, 0xe0, 0xc4, 103 + 0x29, 0x7f, 0xe9, 0x3b, 0x8e, 0xdf, 0x81, 0x82, 104 + 0x8d, 0x15, 0x11, 0x59, 0x4e, 0x13, 0xa5, 0x58, 105 + }, 106 + }, 107 + { 108 + .data_len = 127, 109 + .digest = { 110 + 0x8c, 0x1a, 0xba, 0x40, 0x66, 0x94, 0x19, 0xf4, 111 + 0x2e, 0xa2, 0xae, 0x94, 0x53, 0x18, 0xb6, 0xfd, 112 + 0xa0, 0x12, 0xc5, 0xef, 0xd5, 0xd6, 0x1b, 0xa1, 113 + 0x37, 0xea, 0x19, 0x44, 0x35, 0x54, 0x85, 0x74, 114 + }, 115 + }, 116 + { 117 + .data_len = 128, 118 + .digest = { 119 + 0xfd, 0x07, 0xd8, 0x77, 0x7d, 0x8b, 0x4f, 0xee, 120 + 0x60, 0x60, 0x26, 0xef, 0x2a, 0x86, 0xfb, 0x67, 121 + 0xeb, 0x31, 0x27, 0x03, 0x99, 0x3c, 0xde, 0xe5, 122 + 0x84, 0x72, 0x71, 0x4c, 0x33, 0x7b, 0x87, 0x13, 123 + }, 124 + }, 125 + { 126 + .data_len = 129, 127 + .digest = { 128 + 0x97, 0xc5, 0x58, 0x38, 0x20, 0xc7, 0xde, 0xfa, 129 + 0xdd, 0x9b, 0x10, 0xc6, 0xc2, 0x2f, 0x94, 0xb5, 130 + 0xc0, 0x33, 0xc0, 0x20, 0x1c, 0x2f, 0xb4, 0x28, 131 + 0x5e, 0x36, 0xfa, 0x8c, 0x24, 0x1c, 0x18, 0x27, 132 + }, 133 + }, 134 + { 135 + .data_len = 256, 136 + .digest = { 137 + 0x62, 0x17, 0x84, 0x26, 0x98, 0x30, 0x57, 0xca, 138 + 0x4f, 0x32, 0xd9, 0x09, 0x09, 0x34, 0xe2, 0xcb, 139 + 0x92, 0x45, 0xd5, 0xeb, 0x8b, 0x9b, 0x3c, 0xd8, 140 + 0xaa, 0xc7, 0xd2, 0x2b, 0x04, 0xab, 0xb3, 0x35, 141 + }, 142 + }, 143 + { 144 + .data_len = 511, 145 + .digest = { 146 + 0x7f, 0xe1, 0x09, 0x78, 0x5d, 0x61, 0xfa, 0x5e, 147 + 0x9b, 0x8c, 0xb1, 0xa9, 0x09, 0x69, 0xb4, 0x24, 148 + 0x54, 0xf2, 0x1c, 0xc9, 0x5f, 0xfb, 0x59, 0x9d, 149 + 0x36, 0x1b, 0x37, 0x44, 0xfc, 0x64, 0x79, 0xb6, 150 + }, 151 + }, 152 + { 153 + .data_len = 513, 154 + .digest = { 155 + 0xd2, 0x3b, 0x3a, 0xe7, 0x13, 0x4f, 0xbd, 0x29, 156 + 0x6b, 0xd2, 0x79, 0x26, 0x6c, 0xd2, 0x22, 0x43, 157 + 0x25, 0x34, 0x9b, 0x9b, 0x22, 0xb0, 0x9f, 0x61, 158 + 0x1d, 0xf4, 0xe2, 0x65, 0x68, 0x95, 0x02, 0x6c, 159 + }, 160 + }, 161 + { 162 + .data_len = 1000, 163 + .digest = { 164 + 0x0c, 0x34, 0x53, 0x3f, 0x0f, 0x8a, 0x39, 0x8d, 165 + 0x63, 0xe4, 0x83, 0x6e, 0x11, 0x7d, 0x14, 0x8e, 166 + 0x5b, 0xf0, 0x4d, 0xca, 0x23, 0x24, 0xb5, 0xd2, 167 + 0x13, 0x3f, 0xd9, 0xde, 0x84, 0x74, 0x26, 0x59, 168 + }, 169 + }, 170 + { 171 + .data_len = 3333, 172 + .digest = { 173 + 0xa8, 0xb8, 0x83, 0x01, 0x1b, 0x38, 0x7a, 0xca, 174 + 0x59, 0xe9, 0x5b, 0x37, 0x6a, 0xab, 0xb4, 0x85, 175 + 0x94, 0x73, 0x26, 0x04, 0xef, 0xed, 0xf4, 0x0d, 176 + 0xd6, 0x09, 0x21, 0x09, 0x96, 0x78, 0xe3, 0xcf, 177 + }, 178 + }, 179 + { 180 + .data_len = 4096, 181 + .digest = { 182 + 0x0b, 0x12, 0x66, 0x96, 0x78, 0x4f, 0x2c, 0x35, 183 + 0xa4, 0xed, 0xbc, 0xb8, 0x30, 0xa6, 0x37, 0x9b, 184 + 0x94, 0x13, 0xae, 0x86, 0xf0, 0x20, 0xfb, 0x49, 185 + 0x8f, 0x5d, 0x20, 0x70, 0x60, 0x2b, 0x02, 0x70, 186 + }, 187 + }, 188 + { 189 + .data_len = 4128, 190 + .digest = { 191 + 0xe4, 0xbd, 0xe4, 0x3b, 0x85, 0xf4, 0x6f, 0x11, 192 + 0xad, 0xc4, 0x79, 0xcc, 0x8e, 0x6d, 0x8b, 0x15, 193 + 0xbb, 0xf9, 0xd3, 0x65, 0xe1, 0xf8, 0x8d, 0x22, 194 + 0x65, 0x66, 0x66, 0xb3, 0xf5, 0xd0, 0x9c, 0xaf, 195 + }, 196 + }, 197 + { 198 + .data_len = 4160, 199 + .digest = { 200 + 0x90, 0x5f, 0xe0, 0xfc, 0xb1, 0xdc, 0x38, 0x1b, 201 + 0xe5, 0x37, 0x3f, 0xd2, 0xcc, 0x48, 0xc4, 0xbc, 202 + 0xb4, 0xfd, 0xf7, 0x71, 0x5f, 0x6b, 0xf4, 0xc4, 203 + 0xa6, 0x08, 0x7e, 0xfc, 0x4e, 0x96, 0xf7, 0xc2, 204 + }, 205 + }, 206 + { 207 + .data_len = 4224, 208 + .digest = { 209 + 0x1f, 0x34, 0x0a, 0x3b, 0xdb, 0xf7, 0x7a, 0xdb, 210 + 0x3d, 0x89, 0x85, 0x0c, 0xd2, 0xf0, 0x0c, 0xbd, 211 + 0x25, 0x39, 0x14, 0x06, 0x28, 0x0f, 0x6b, 0x5f, 212 + 0xe3, 0x1f, 0x2a, 0xb6, 0xca, 0x56, 0x41, 0xa1, 213 + }, 214 + }, 215 + { 216 + .data_len = 16384, 217 + .digest = { 218 + 0x7b, 0x01, 0x2d, 0x84, 0x70, 0xee, 0xe0, 0x77, 219 + 0x3c, 0x17, 0x63, 0xfe, 0x40, 0xd7, 0xfd, 0xa1, 220 + 0x75, 0x90, 0xb8, 0x3e, 0x50, 0xcd, 0x06, 0xb7, 221 + 0xb9, 0xb9, 0x2b, 0x91, 0x4f, 0xba, 0xe4, 0x4c, 222 + }, 223 + }, 224 + }; 225 + 226 + static const u8 hash_testvec_consolidated[SHA256_DIGEST_SIZE] = { 227 + 0x78, 0x1c, 0xb1, 0x9f, 0xe5, 0xe1, 0xcb, 0x41, 228 + 0x8b, 0x34, 0x00, 0x33, 0x57, 0xc3, 0x1c, 0x8f, 229 + 0x5c, 0x84, 0xc7, 0x8b, 0x87, 0x6a, 0x13, 0x29, 230 + 0x2f, 0xc4, 0x1a, 0x70, 0x5e, 0x40, 0xf2, 0xfe, 231 + }; 232 + 233 + static const u8 hmac_testvec_consolidated[SHA256_DIGEST_SIZE] = { 234 + 0x56, 0x96, 0x2e, 0x23, 0x3f, 0x94, 0x89, 0x0d, 235 + 0x0f, 0x24, 0x36, 0x2e, 0x19, 0x3d, 0xb5, 0xac, 236 + 0xb8, 0xcd, 0xf1, 0xc9, 0xca, 0xac, 0xee, 0x9d, 237 + 0x62, 0xe6, 0x81, 0xe5, 0x96, 0xf9, 0x38, 0xf5, 238 + };
+39
lib/crypto/tests/sha256_kunit.c
··· 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 + 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 + 24 + static struct kunit_case hash_test_cases[] = { 25 + HASH_KUNIT_CASES, 26 + KUNIT_CASE(benchmark_hash), 27 + {}, 28 + }; 29 + 30 + static struct kunit_suite hash_test_suite = { 31 + .name = "sha256", 32 + .test_cases = hash_test_cases, 33 + .suite_init = hash_suite_init, 34 + .suite_exit = hash_suite_exit, 35 + }; 36 + kunit_test_suite(hash_test_suite); 37 + 38 + MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-256 and HMAC-SHA256"); 39 + MODULE_LICENSE("GPL");
+290
lib/crypto/tests/sha384-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha384 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[SHA384_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 12 + 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, 13 + 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 14 + 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 15 + 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 16 + 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b, 17 + }, 18 + }, 19 + { 20 + .data_len = 1, 21 + .digest = { 22 + 0x07, 0x34, 0x9d, 0x74, 0x48, 0x76, 0xa5, 0x72, 23 + 0x78, 0x02, 0xb8, 0x6e, 0x21, 0x59, 0xb0, 0x75, 24 + 0x09, 0x68, 0x11, 0x39, 0x53, 0x61, 0xee, 0x8d, 25 + 0xf2, 0x01, 0xf3, 0x90, 0x53, 0x7c, 0xd3, 0xde, 26 + 0x13, 0x9f, 0xd2, 0x74, 0x28, 0xfe, 0xe1, 0xc8, 27 + 0x2e, 0x95, 0xc6, 0x7d, 0x69, 0x4d, 0x04, 0xc6, 28 + }, 29 + }, 30 + { 31 + .data_len = 2, 32 + .digest = { 33 + 0xc4, 0xef, 0x6e, 0x8c, 0x19, 0x1c, 0xaa, 0x0e, 34 + 0x86, 0xf2, 0x68, 0xa1, 0xa0, 0x2d, 0x2e, 0xb2, 35 + 0x84, 0xbc, 0x5d, 0x53, 0x31, 0xf8, 0x03, 0x75, 36 + 0x56, 0xf4, 0x8b, 0x23, 0x1a, 0x68, 0x15, 0x9a, 37 + 0x60, 0xb2, 0xec, 0x05, 0xe1, 0xd4, 0x5e, 0x9e, 38 + 0xe8, 0x7c, 0x9d, 0xe4, 0x0f, 0x9c, 0x3a, 0xdd, 39 + }, 40 + }, 41 + { 42 + .data_len = 3, 43 + .digest = { 44 + 0x29, 0xd2, 0x02, 0xa2, 0x77, 0x24, 0xc7, 0xa7, 45 + 0x23, 0x0c, 0x3e, 0x30, 0x56, 0x47, 0xdb, 0x75, 46 + 0xd4, 0x41, 0xf8, 0xb3, 0x8e, 0x26, 0xf6, 0x92, 47 + 0xbc, 0x20, 0x2e, 0x96, 0xcc, 0x81, 0x5f, 0x32, 48 + 0x82, 0x60, 0xe2, 0xcf, 0x23, 0xd7, 0x3c, 0x90, 49 + 0xb2, 0x56, 0x8f, 0xb6, 0x0f, 0xf0, 0x6b, 0x80, 50 + }, 51 + }, 52 + { 53 + .data_len = 16, 54 + .digest = { 55 + 0x21, 0x4c, 0xac, 0xfe, 0xbd, 0x40, 0x74, 0x1f, 56 + 0xa2, 0x2d, 0x2f, 0x35, 0x91, 0xfd, 0xc9, 0x97, 57 + 0x88, 0x12, 0x6c, 0x0c, 0x6e, 0xd8, 0x50, 0x0b, 58 + 0x4b, 0x2c, 0x89, 0xa6, 0xa6, 0x4a, 0xad, 0xd7, 59 + 0x72, 0x62, 0x2c, 0x62, 0x81, 0xcd, 0x24, 0x74, 60 + 0xf5, 0x44, 0x05, 0xa0, 0x97, 0xea, 0xf1, 0x78, 61 + }, 62 + }, 63 + { 64 + .data_len = 32, 65 + .digest = { 66 + 0x06, 0x8b, 0x92, 0x9f, 0x8b, 0x64, 0xb2, 0x80, 67 + 0xde, 0xcc, 0xde, 0xc3, 0x2f, 0x22, 0x27, 0xe8, 68 + 0x3b, 0x6e, 0x16, 0x21, 0x14, 0x81, 0xbe, 0x5b, 69 + 0xa7, 0xa7, 0x14, 0x8a, 0x00, 0x8f, 0x0d, 0x38, 70 + 0x11, 0x63, 0xe8, 0x3e, 0xb9, 0xf1, 0xcf, 0x87, 71 + 0xb1, 0x28, 0xe5, 0xa1, 0x89, 0xa8, 0x7a, 0xde, 72 + }, 73 + }, 74 + { 75 + .data_len = 48, 76 + .digest = { 77 + 0x9e, 0x37, 0x76, 0x62, 0x98, 0x39, 0xbe, 0xfd, 78 + 0x2b, 0x91, 0x20, 0x54, 0x8f, 0x21, 0xe7, 0x30, 79 + 0x0a, 0x01, 0x7a, 0x65, 0x0b, 0xc9, 0xb3, 0x89, 80 + 0x3c, 0xb6, 0xd3, 0xa8, 0xff, 0xc9, 0x1b, 0x5c, 81 + 0xd4, 0xac, 0xb4, 0x7e, 0xba, 0x94, 0xc3, 0x8a, 82 + 0x26, 0x41, 0xf6, 0xd5, 0xed, 0x6f, 0x27, 0xa7, 83 + }, 84 + }, 85 + { 86 + .data_len = 49, 87 + .digest = { 88 + 0x03, 0x1f, 0xef, 0x5a, 0x16, 0x28, 0x78, 0x10, 89 + 0x29, 0xe8, 0xe2, 0xe4, 0x84, 0x36, 0x19, 0x10, 90 + 0xaa, 0xea, 0xde, 0x06, 0x39, 0x5f, 0xb2, 0x36, 91 + 0xca, 0x24, 0x4f, 0x7b, 0x66, 0xf7, 0xe7, 0x31, 92 + 0xf3, 0x9b, 0x74, 0x1e, 0x17, 0x20, 0x88, 0x62, 93 + 0x50, 0xeb, 0x5f, 0x9a, 0xa7, 0x2c, 0xf4, 0xc9, 94 + }, 95 + }, 96 + { 97 + .data_len = 63, 98 + .digest = { 99 + 0x10, 0xce, 0xed, 0x26, 0xb8, 0xac, 0xc1, 0x1b, 100 + 0xe6, 0xb9, 0xeb, 0x7c, 0xae, 0xcd, 0x55, 0x5a, 101 + 0x20, 0x2a, 0x7b, 0x43, 0xe6, 0x3e, 0xf0, 0x3f, 102 + 0xd9, 0x2f, 0x8c, 0x52, 0xe2, 0xf0, 0xb6, 0x24, 103 + 0x2e, 0xa4, 0xac, 0x24, 0x3a, 0x54, 0x99, 0x71, 104 + 0x65, 0xab, 0x97, 0x2d, 0xb6, 0xe6, 0x94, 0x20, 105 + }, 106 + }, 107 + { 108 + .data_len = 64, 109 + .digest = { 110 + 0x24, 0x6d, 0x9f, 0x59, 0x42, 0x36, 0xca, 0x34, 111 + 0x36, 0x41, 0xa2, 0xcd, 0x69, 0xdf, 0x3d, 0xcb, 112 + 0x64, 0x94, 0x54, 0xb2, 0xed, 0xc1, 0x1c, 0x31, 113 + 0xe3, 0x26, 0xcb, 0x71, 0xe6, 0x98, 0xb2, 0x56, 114 + 0x74, 0x30, 0xa9, 0x15, 0x98, 0x9d, 0xb3, 0x07, 115 + 0xcc, 0xa8, 0xcc, 0x6f, 0x42, 0xb0, 0x9d, 0x2b, 116 + }, 117 + }, 118 + { 119 + .data_len = 65, 120 + .digest = { 121 + 0x85, 0x1f, 0xbc, 0x5e, 0x2a, 0x00, 0x7d, 0xc2, 122 + 0x21, 0x4c, 0x28, 0x14, 0xc5, 0xd8, 0x0c, 0xe8, 123 + 0x55, 0xa5, 0xa0, 0x77, 0xda, 0x8f, 0xce, 0xd4, 124 + 0xf0, 0xcb, 0x30, 0xb8, 0x9c, 0x47, 0xe1, 0x33, 125 + 0x92, 0x18, 0xc5, 0x1f, 0xf2, 0xef, 0xb5, 0xe5, 126 + 0xbc, 0x63, 0xa6, 0xe5, 0x9a, 0xc9, 0xcc, 0xf1, 127 + }, 128 + }, 129 + { 130 + .data_len = 127, 131 + .digest = { 132 + 0x26, 0xd2, 0x4c, 0xb6, 0xce, 0xd8, 0x22, 0x2b, 133 + 0x44, 0x10, 0x6f, 0x59, 0xf7, 0x0d, 0xb9, 0x3f, 134 + 0x7d, 0x29, 0x75, 0xf1, 0x71, 0xb2, 0x71, 0x23, 135 + 0xef, 0x68, 0xb7, 0x25, 0xae, 0xb8, 0x45, 0xf8, 136 + 0xa3, 0xb2, 0x2d, 0x7a, 0x83, 0x0a, 0x05, 0x61, 137 + 0xbc, 0x73, 0xf1, 0xf9, 0xba, 0xfb, 0x3d, 0xc2, 138 + }, 139 + }, 140 + { 141 + .data_len = 128, 142 + .digest = { 143 + 0x7c, 0xe5, 0x7f, 0x5e, 0xea, 0xd9, 0x7e, 0x54, 144 + 0x14, 0x30, 0x6f, 0x37, 0x02, 0x71, 0x0f, 0xf1, 145 + 0x14, 0x16, 0xfa, 0xeb, 0x6e, 0x1e, 0xf0, 0xbe, 146 + 0x10, 0xed, 0x01, 0xbf, 0xa0, 0x9d, 0xcb, 0x07, 147 + 0x5f, 0x8b, 0x7f, 0x44, 0xe1, 0xd9, 0x13, 0xf0, 148 + 0x29, 0xa2, 0x54, 0x32, 0xd9, 0xb0, 0x69, 0x69, 149 + }, 150 + }, 151 + { 152 + .data_len = 129, 153 + .digest = { 154 + 0xc5, 0x54, 0x1f, 0xcb, 0x9d, 0x8f, 0xdf, 0xbf, 155 + 0xab, 0x55, 0x92, 0x1d, 0x3b, 0x93, 0x79, 0x26, 156 + 0xdf, 0xba, 0x9a, 0x28, 0xff, 0xa0, 0x6c, 0xae, 157 + 0x7b, 0x53, 0x8d, 0xfa, 0xef, 0x35, 0x88, 0x19, 158 + 0x16, 0xb8, 0x72, 0x86, 0x76, 0x2a, 0xf5, 0xe6, 159 + 0xec, 0xb2, 0xd7, 0xd4, 0xbe, 0x1a, 0xe4, 0x9f, 160 + }, 161 + }, 162 + { 163 + .data_len = 256, 164 + .digest = { 165 + 0x74, 0x9d, 0x77, 0xfb, 0xe8, 0x0f, 0x0c, 0x2d, 166 + 0x86, 0x0d, 0x49, 0xea, 0x2b, 0xd0, 0x13, 0xd1, 167 + 0xe8, 0xb8, 0xe1, 0xa3, 0x7b, 0x48, 0xab, 0x6a, 168 + 0x21, 0x2b, 0x4c, 0x48, 0x32, 0xb5, 0xdc, 0x31, 169 + 0x7f, 0xd0, 0x32, 0x67, 0x9a, 0xc0, 0x85, 0x53, 170 + 0xef, 0xe9, 0xfb, 0xe1, 0x8b, 0xd8, 0xcc, 0xc2, 171 + }, 172 + }, 173 + { 174 + .data_len = 511, 175 + .digest = { 176 + 0x7b, 0xa9, 0xde, 0xa3, 0x07, 0x5c, 0x4c, 0xaa, 177 + 0x31, 0xc6, 0x9e, 0x55, 0xd4, 0x3f, 0x52, 0xdd, 178 + 0xde, 0x36, 0x70, 0x96, 0x59, 0x6e, 0x90, 0x78, 179 + 0x4c, 0x6a, 0x27, 0xde, 0x83, 0x84, 0xc3, 0x35, 180 + 0x53, 0x76, 0x1d, 0xbf, 0x83, 0x64, 0xcf, 0xf2, 181 + 0xb0, 0x3e, 0x07, 0x27, 0xe4, 0x25, 0x6c, 0x56, 182 + }, 183 + }, 184 + { 185 + .data_len = 513, 186 + .digest = { 187 + 0x53, 0x50, 0xf7, 0x3b, 0x86, 0x1d, 0x7a, 0xe2, 188 + 0x5d, 0x9b, 0x71, 0xfa, 0x25, 0x23, 0x5a, 0xfe, 189 + 0x8c, 0xb9, 0xac, 0x8a, 0x9d, 0x6c, 0x99, 0xbc, 190 + 0x01, 0x9e, 0xa0, 0xd6, 0x3c, 0x03, 0x46, 0x21, 191 + 0xb6, 0xd0, 0xb0, 0xb3, 0x23, 0x23, 0x58, 0xf1, 192 + 0xea, 0x4e, 0xf2, 0x1a, 0x2f, 0x14, 0x2b, 0x5a, 193 + }, 194 + }, 195 + { 196 + .data_len = 1000, 197 + .digest = { 198 + 0x06, 0x03, 0xb3, 0xba, 0x14, 0xe0, 0x28, 0x07, 199 + 0xd5, 0x15, 0x97, 0x1f, 0x87, 0xef, 0x80, 0xba, 200 + 0x48, 0x03, 0xb6, 0xc5, 0x47, 0xca, 0x8c, 0x95, 201 + 0xed, 0x95, 0xfd, 0x27, 0xb6, 0x83, 0xda, 0x6d, 202 + 0xa7, 0xb2, 0x1a, 0xd2, 0xb5, 0x89, 0xbb, 0xb4, 203 + 0x00, 0xbc, 0x86, 0x54, 0x7d, 0x5a, 0x91, 0x63, 204 + }, 205 + }, 206 + { 207 + .data_len = 3333, 208 + .digest = { 209 + 0xd3, 0xe0, 0x6e, 0x7d, 0x80, 0x08, 0x53, 0x07, 210 + 0x8c, 0x0f, 0xc2, 0xce, 0x9f, 0x09, 0x86, 0x31, 211 + 0x28, 0x24, 0x3c, 0x3e, 0x2d, 0x36, 0xb4, 0x28, 212 + 0xc7, 0x1b, 0x70, 0xf9, 0x35, 0x9b, 0x10, 0xfa, 213 + 0xc8, 0x5e, 0x2b, 0x32, 0x7f, 0x65, 0xd2, 0x68, 214 + 0xb2, 0x84, 0x90, 0xf6, 0xc8, 0x6e, 0xb8, 0xdb, 215 + }, 216 + }, 217 + { 218 + .data_len = 4096, 219 + .digest = { 220 + 0x39, 0xeb, 0xc4, 0xb3, 0x08, 0xe2, 0xdd, 0xf3, 221 + 0x9f, 0x5e, 0x44, 0x93, 0x63, 0x8b, 0x39, 0x57, 222 + 0xd7, 0xe8, 0x7e, 0x3d, 0x74, 0xf8, 0xf6, 0xab, 223 + 0xfe, 0x74, 0x51, 0xe4, 0x1b, 0x4a, 0x23, 0xbc, 224 + 0x69, 0xfc, 0xbb, 0xa7, 0x71, 0xa7, 0x86, 0x24, 225 + 0xcc, 0x85, 0x70, 0xf2, 0x31, 0x0d, 0x47, 0xc0, 226 + }, 227 + }, 228 + { 229 + .data_len = 4128, 230 + .digest = { 231 + 0x23, 0xc3, 0x97, 0x06, 0x79, 0xbe, 0x8a, 0xe9, 232 + 0x1f, 0x1a, 0x43, 0xad, 0xe6, 0x76, 0x23, 0x13, 233 + 0x64, 0xae, 0xda, 0xe7, 0x8b, 0x88, 0x96, 0xb6, 234 + 0xa9, 0x1a, 0xb7, 0x80, 0x8e, 0x1c, 0x94, 0x98, 235 + 0x09, 0x08, 0xdb, 0x8e, 0x4d, 0x0a, 0x09, 0x65, 236 + 0xe5, 0x21, 0x1c, 0xd9, 0xab, 0x64, 0xbb, 0xea, 237 + }, 238 + }, 239 + { 240 + .data_len = 4160, 241 + .digest = { 242 + 0x4f, 0x4a, 0x88, 0x9f, 0x40, 0x89, 0xfe, 0xb6, 243 + 0xda, 0x9d, 0xcd, 0xa5, 0x27, 0xd2, 0x29, 0x71, 244 + 0x58, 0x60, 0xd4, 0x55, 0xfe, 0x92, 0xcd, 0x51, 245 + 0x8b, 0xec, 0x3b, 0xd3, 0xd1, 0x3e, 0x8d, 0x36, 246 + 0x7b, 0xb1, 0x41, 0xef, 0xec, 0x9d, 0xdf, 0xcd, 247 + 0x4e, 0xde, 0x5a, 0xe5, 0xe5, 0x16, 0x14, 0x54, 248 + }, 249 + }, 250 + { 251 + .data_len = 4224, 252 + .digest = { 253 + 0xb5, 0xa5, 0x3e, 0x86, 0x39, 0x20, 0x49, 0x4c, 254 + 0xcd, 0xb6, 0xdd, 0x03, 0xfe, 0x36, 0x6e, 0xa6, 255 + 0xfc, 0xff, 0x19, 0x33, 0x0c, 0x52, 0xea, 0x37, 256 + 0x94, 0xda, 0x5b, 0x27, 0xd1, 0x99, 0x5a, 0x89, 257 + 0x40, 0x78, 0xfa, 0x96, 0xb9, 0x2f, 0xa0, 0x48, 258 + 0xc9, 0xf8, 0x5c, 0xf0, 0x95, 0xf4, 0xea, 0x61, 259 + }, 260 + }, 261 + { 262 + .data_len = 16384, 263 + .digest = { 264 + 0x6f, 0x48, 0x6f, 0x21, 0xb9, 0xc1, 0xcc, 0x92, 265 + 0x4e, 0xed, 0x6b, 0xef, 0x51, 0x88, 0xdf, 0xfd, 266 + 0xcb, 0x3d, 0x44, 0x9c, 0x37, 0x85, 0xb4, 0xc5, 267 + 0xeb, 0x60, 0x55, 0x58, 0x01, 0x47, 0xbf, 0x75, 268 + 0x9b, 0xa8, 0x82, 0x8c, 0xec, 0xe8, 0x0e, 0x58, 269 + 0xc1, 0x26, 0xa2, 0x45, 0x87, 0x3e, 0xfb, 0x8d, 270 + }, 271 + }, 272 + }; 273 + 274 + static const u8 hash_testvec_consolidated[SHA384_DIGEST_SIZE] = { 275 + 0xfc, 0xcb, 0xe6, 0x42, 0xf0, 0x9e, 0x2b, 0x77, 276 + 0x7b, 0x62, 0xe8, 0x70, 0x86, 0xbf, 0xaf, 0x3c, 277 + 0xbb, 0x02, 0xd9, 0x86, 0xdc, 0xba, 0xd3, 0xa4, 278 + 0x0d, 0x8d, 0xb3, 0x2d, 0x0b, 0xa3, 0x84, 0x04, 279 + 0x7c, 0x16, 0x37, 0xaf, 0xba, 0x1e, 0xd4, 0x2f, 280 + 0x4c, 0x57, 0x55, 0x86, 0x52, 0x47, 0x9a, 0xec, 281 + }; 282 + 283 + static const u8 hmac_testvec_consolidated[SHA384_DIGEST_SIZE] = { 284 + 0x82, 0xcf, 0x7d, 0x80, 0x71, 0xdb, 0x91, 0x09, 285 + 0x67, 0xe8, 0x44, 0x4a, 0x0d, 0x03, 0xb1, 0xf9, 286 + 0x62, 0xde, 0x4e, 0xbb, 0x1f, 0x41, 0xcd, 0x62, 287 + 0x39, 0x6b, 0x2d, 0x44, 0x0e, 0xde, 0x98, 0x73, 288 + 0xdd, 0xeb, 0x9d, 0x53, 0xfb, 0xee, 0xd1, 0xc3, 289 + 0x96, 0xdb, 0xfc, 0x2a, 0x38, 0x90, 0x02, 0x53, 290 + };
+39
lib/crypto/tests/sha384_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/sha2.h> 6 + #include "sha384-testvecs.h" 7 + 8 + #define HASH sha384 9 + #define HASH_CTX sha384_ctx 10 + #define HASH_SIZE SHA384_DIGEST_SIZE 11 + #define HASH_INIT sha384_init 12 + #define HASH_UPDATE sha384_update 13 + #define HASH_FINAL sha384_final 14 + #define HMAC_KEY hmac_sha384_key 15 + #define HMAC_CTX hmac_sha384_ctx 16 + #define HMAC_PREPAREKEY hmac_sha384_preparekey 17 + #define HMAC_INIT hmac_sha384_init 18 + #define HMAC_UPDATE hmac_sha384_update 19 + #define HMAC_FINAL hmac_sha384_final 20 + #define HMAC hmac_sha384 21 + #define HMAC_USINGRAWKEY hmac_sha384_usingrawkey 22 + #include "hash-test-template.h" 23 + 24 + static struct kunit_case hash_test_cases[] = { 25 + HASH_KUNIT_CASES, 26 + KUNIT_CASE(benchmark_hash), 27 + {}, 28 + }; 29 + 30 + static struct kunit_suite hash_test_suite = { 31 + .name = "sha384", 32 + .test_cases = hash_test_cases, 33 + .suite_init = hash_suite_init, 34 + .suite_exit = hash_suite_exit, 35 + }; 36 + kunit_test_suite(hash_test_suite); 37 + 38 + MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-384 and HMAC-SHA384"); 39 + MODULE_LICENSE("GPL");
+342
lib/crypto/tests/sha512-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sha512 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[SHA512_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 12 + 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, 13 + 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 14 + 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 15 + 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 16 + 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 17 + 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 18 + 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e, 19 + }, 20 + }, 21 + { 22 + .data_len = 1, 23 + .digest = { 24 + 0x12, 0xf2, 0xb6, 0xec, 0x84, 0xa0, 0x8e, 0xcf, 25 + 0x0d, 0xec, 0x17, 0xfd, 0x1f, 0x91, 0x15, 0x69, 26 + 0xd2, 0xb9, 0x89, 0xff, 0x89, 0x9d, 0xd9, 0x0b, 27 + 0x7a, 0x0f, 0x82, 0x94, 0x57, 0x5b, 0xf3, 0x08, 28 + 0x42, 0x45, 0x23, 0x08, 0x44, 0x54, 0x35, 0x36, 29 + 0xed, 0x4b, 0xb3, 0xa5, 0xbf, 0x17, 0xc1, 0x3c, 30 + 0xdd, 0x25, 0x4a, 0x30, 0x64, 0xed, 0x66, 0x06, 31 + 0x72, 0x05, 0xc2, 0x71, 0x5a, 0x6c, 0xd0, 0x75, 32 + }, 33 + }, 34 + { 35 + .data_len = 2, 36 + .digest = { 37 + 0x01, 0x37, 0x97, 0xcc, 0x0a, 0xcb, 0x61, 0xa4, 38 + 0x93, 0x26, 0x36, 0x4b, 0xd2, 0x27, 0xea, 0xaf, 39 + 0xda, 0xfa, 0x8f, 0x86, 0x12, 0x99, 0x7b, 0xc8, 40 + 0x94, 0xa9, 0x1c, 0x70, 0x3f, 0x43, 0xf3, 0x9a, 41 + 0x02, 0xc5, 0x0d, 0x8e, 0x01, 0xf8, 0x3a, 0xa6, 42 + 0xec, 0xaa, 0xc5, 0xc7, 0x9d, 0x3d, 0x7f, 0x9d, 43 + 0x47, 0x0e, 0x58, 0x2d, 0x9a, 0x2d, 0x51, 0x1d, 44 + 0xc3, 0x77, 0xb2, 0x7f, 0x69, 0x9a, 0xc3, 0x50, 45 + }, 46 + }, 47 + { 48 + .data_len = 3, 49 + .digest = { 50 + 0xd4, 0xa3, 0xc2, 0x50, 0xa0, 0x33, 0xc6, 0xe4, 51 + 0x50, 0x08, 0xea, 0xc9, 0xb8, 0x35, 0x55, 0x34, 52 + 0x61, 0xb8, 0x2e, 0xa2, 0xe5, 0xdc, 0x04, 0x70, 53 + 0x99, 0x86, 0x5f, 0xee, 0x2e, 0x1e, 0xd4, 0x40, 54 + 0xf8, 0x88, 0x01, 0x84, 0x97, 0x16, 0x6a, 0xd3, 55 + 0x0a, 0xb4, 0xe2, 0x34, 0xca, 0x1f, 0xfc, 0x6b, 56 + 0x61, 0xf3, 0x7f, 0x72, 0xfa, 0x8d, 0x22, 0xcf, 57 + 0x7f, 0x2d, 0x87, 0x9d, 0xbb, 0x59, 0xac, 0x53, 58 + }, 59 + }, 60 + { 61 + .data_len = 16, 62 + .digest = { 63 + 0x3b, 0xae, 0x66, 0x48, 0x0f, 0x35, 0x3c, 0xcd, 64 + 0x92, 0xa9, 0xb6, 0xb9, 0xfe, 0x19, 0x90, 0x92, 65 + 0x52, 0xa5, 0x02, 0xd1, 0x89, 0xcf, 0x98, 0x86, 66 + 0x29, 0x28, 0xab, 0xc4, 0x9e, 0xcc, 0x75, 0x38, 67 + 0x95, 0xa7, 0x59, 0x43, 0xef, 0x8c, 0x3a, 0xeb, 68 + 0x40, 0xa3, 0xbe, 0x2b, 0x75, 0x0d, 0xfd, 0xc3, 69 + 0xaf, 0x69, 0x08, 0xad, 0x9f, 0xc9, 0xf4, 0x96, 70 + 0xa9, 0xc2, 0x2b, 0x1b, 0x66, 0x6f, 0x1d, 0x28, 71 + }, 72 + }, 73 + { 74 + .data_len = 32, 75 + .digest = { 76 + 0x9c, 0xfb, 0x3c, 0x40, 0xd5, 0x3b, 0xc4, 0xff, 77 + 0x07, 0xa7, 0xf0, 0x24, 0xb7, 0xd6, 0x5e, 0x12, 78 + 0x5b, 0x85, 0xb5, 0xa5, 0xe0, 0x82, 0xa6, 0xda, 79 + 0x30, 0x13, 0x2f, 0x1a, 0xe3, 0xd0, 0x55, 0xcb, 80 + 0x14, 0x19, 0xe2, 0x09, 0x91, 0x96, 0x26, 0xf9, 81 + 0x38, 0xd7, 0xfa, 0x4a, 0xfb, 0x2f, 0x6f, 0xc0, 82 + 0xf4, 0x95, 0xc3, 0x40, 0xf6, 0xdb, 0xe7, 0xc2, 83 + 0x79, 0x23, 0xa4, 0x20, 0x96, 0x3a, 0x00, 0xbb, 84 + }, 85 + }, 86 + { 87 + .data_len = 48, 88 + .digest = { 89 + 0x92, 0x1a, 0x21, 0x06, 0x6e, 0x08, 0x84, 0x09, 90 + 0x23, 0x8d, 0x63, 0xec, 0xd6, 0x72, 0xd3, 0x21, 91 + 0x51, 0xe8, 0x65, 0x94, 0xf8, 0x1f, 0x5f, 0xa7, 92 + 0xab, 0x6b, 0xae, 0x1c, 0x2c, 0xaf, 0xf9, 0x0c, 93 + 0x7c, 0x5a, 0x74, 0x1d, 0x90, 0x26, 0x4a, 0xc3, 94 + 0xa1, 0x60, 0xf4, 0x1d, 0xd5, 0x3c, 0x86, 0xe8, 95 + 0x00, 0xb3, 0x99, 0x27, 0xb8, 0x9d, 0x3e, 0x17, 96 + 0x32, 0x5a, 0x34, 0x3e, 0xc2, 0xb2, 0x6e, 0xbd, 97 + }, 98 + }, 99 + { 100 + .data_len = 49, 101 + .digest = { 102 + 0x5a, 0x1f, 0x40, 0x5f, 0xee, 0xf2, 0xdd, 0x67, 103 + 0x01, 0xcb, 0x26, 0x58, 0xf5, 0x1b, 0xe8, 0x7e, 104 + 0xeb, 0x7d, 0x9d, 0xef, 0xd3, 0x55, 0xd6, 0x89, 105 + 0x2e, 0xfc, 0x14, 0xe2, 0x98, 0x4c, 0x31, 0xaa, 106 + 0x69, 0x00, 0xf9, 0x4e, 0xb0, 0x75, 0x1b, 0x71, 107 + 0x93, 0x60, 0xdf, 0xa1, 0xaf, 0xba, 0xc2, 0xd3, 108 + 0x6a, 0x22, 0xa0, 0xff, 0xb5, 0x66, 0x15, 0x66, 109 + 0xd2, 0x24, 0x9a, 0x7e, 0xe4, 0xe5, 0x84, 0xdb, 110 + }, 111 + }, 112 + { 113 + .data_len = 63, 114 + .digest = { 115 + 0x32, 0xbd, 0xcf, 0x72, 0xa9, 0x74, 0x87, 0xe6, 116 + 0x2a, 0x53, 0x7e, 0x6d, 0xac, 0xc2, 0xdd, 0x2c, 117 + 0x87, 0xb3, 0xf7, 0x90, 0x29, 0xc9, 0x16, 0x59, 118 + 0xd2, 0x7e, 0x6e, 0x84, 0x1d, 0xa6, 0xaf, 0x3c, 119 + 0xca, 0xd6, 0x1a, 0x24, 0xa4, 0xcd, 0x59, 0x44, 120 + 0x20, 0xd7, 0xd2, 0x5b, 0x97, 0xda, 0xd5, 0xa9, 121 + 0x23, 0xb1, 0xa4, 0x60, 0xb8, 0x05, 0x98, 0xdc, 122 + 0xef, 0x89, 0x81, 0xe3, 0x3a, 0xf9, 0x24, 0x37, 123 + }, 124 + }, 125 + { 126 + .data_len = 64, 127 + .digest = { 128 + 0x96, 0x3a, 0x1a, 0xdd, 0x1b, 0xeb, 0x1a, 0x55, 129 + 0x24, 0x52, 0x3d, 0xec, 0x9d, 0x52, 0x2e, 0xa6, 130 + 0xfe, 0x81, 0xd6, 0x98, 0xac, 0xcc, 0x60, 0x56, 131 + 0x04, 0x9d, 0xa3, 0xf3, 0x56, 0x05, 0xe4, 0x8a, 132 + 0x61, 0xaf, 0x6f, 0x6e, 0x8e, 0x75, 0x67, 0x3a, 133 + 0xd2, 0xb0, 0x85, 0x2d, 0x17, 0xd2, 0x86, 0x8c, 134 + 0x50, 0x4b, 0xdd, 0xef, 0x35, 0x00, 0xde, 0x29, 135 + 0x3d, 0x4b, 0x04, 0x12, 0x8a, 0x81, 0xe2, 0xcc, 136 + }, 137 + }, 138 + { 139 + .data_len = 65, 140 + .digest = { 141 + 0x9c, 0x6e, 0xf0, 0x6f, 0x71, 0x77, 0xd5, 0xd0, 142 + 0xbb, 0x70, 0x1f, 0xcb, 0xbd, 0xd3, 0xfe, 0x23, 143 + 0x71, 0x78, 0xad, 0x3a, 0xd2, 0x1e, 0x34, 0xf4, 144 + 0x6d, 0xb4, 0xa2, 0x0a, 0x24, 0xcb, 0xe1, 0x99, 145 + 0x07, 0xd0, 0x79, 0x8f, 0x7e, 0x69, 0x31, 0x68, 146 + 0x29, 0xb5, 0x85, 0x82, 0x67, 0xdc, 0x4a, 0x8d, 147 + 0x44, 0x04, 0x02, 0xc0, 0xfb, 0xd2, 0x19, 0x66, 148 + 0x1e, 0x25, 0x8b, 0xd2, 0x5a, 0x59, 0x68, 0xc0, 149 + }, 150 + }, 151 + { 152 + .data_len = 127, 153 + .digest = { 154 + 0xb8, 0x8f, 0xa8, 0x29, 0x4d, 0xcf, 0x5f, 0x73, 155 + 0x3c, 0x55, 0x43, 0xd9, 0x1c, 0xbc, 0x0c, 0x17, 156 + 0x75, 0x0b, 0xc7, 0xb1, 0x1d, 0x9f, 0x7b, 0x2f, 157 + 0x4c, 0x3d, 0x2a, 0x71, 0xfb, 0x1b, 0x0e, 0xca, 158 + 0x4e, 0x96, 0xa0, 0x95, 0xee, 0xf4, 0x93, 0x76, 159 + 0x36, 0xfb, 0x5d, 0xd3, 0x46, 0xc4, 0x1d, 0x41, 160 + 0x32, 0x92, 0x9d, 0xed, 0xdb, 0x7f, 0xfa, 0xb3, 161 + 0x91, 0x61, 0x3e, 0xd6, 0xb2, 0xca, 0x8d, 0x81, 162 + }, 163 + }, 164 + { 165 + .data_len = 128, 166 + .digest = { 167 + 0x54, 0xac, 0x1a, 0xa1, 0xa6, 0xa3, 0x47, 0x2a, 168 + 0x5a, 0xac, 0x1a, 0x3a, 0x4b, 0xa1, 0x11, 0x08, 169 + 0xa7, 0x90, 0xec, 0x52, 0xcb, 0xaf, 0x68, 0x41, 170 + 0x38, 0x44, 0x53, 0x94, 0x93, 0x30, 0xaf, 0x3a, 171 + 0xec, 0x99, 0x3a, 0x7d, 0x2a, 0xd5, 0xb6, 0x05, 172 + 0xf5, 0xa6, 0xbb, 0x9b, 0x82, 0xc2, 0xbd, 0x98, 173 + 0x28, 0x62, 0x98, 0x3e, 0xe4, 0x27, 0x9b, 0xaa, 174 + 0xce, 0x0a, 0x6f, 0xab, 0x1b, 0x16, 0xf3, 0xdd, 175 + }, 176 + }, 177 + { 178 + .data_len = 129, 179 + .digest = { 180 + 0x04, 0x37, 0x60, 0xbc, 0xb3, 0xb1, 0xc6, 0x2d, 181 + 0x42, 0xc5, 0xd7, 0x7e, 0xd9, 0x86, 0x82, 0xe0, 182 + 0xf4, 0x62, 0xad, 0x75, 0x68, 0x0b, 0xc7, 0xa8, 183 + 0xd6, 0x9a, 0x76, 0xe5, 0x29, 0xb8, 0x37, 0x30, 184 + 0x0f, 0xc0, 0xbc, 0x81, 0x94, 0x7c, 0x13, 0xf4, 185 + 0x9c, 0x27, 0xbc, 0x59, 0xa1, 0x70, 0x6a, 0x87, 186 + 0x20, 0x12, 0x0a, 0x2a, 0x62, 0x5e, 0x6f, 0xca, 187 + 0x91, 0x6b, 0x34, 0x7e, 0x4c, 0x0d, 0xf0, 0x6c, 188 + }, 189 + }, 190 + { 191 + .data_len = 256, 192 + .digest = { 193 + 0x4b, 0x7c, 0x1f, 0x53, 0x52, 0xcc, 0x30, 0xed, 194 + 0x91, 0x44, 0x6f, 0x0d, 0xb5, 0x41, 0x79, 0x99, 195 + 0xaf, 0x82, 0x65, 0x52, 0x03, 0xf8, 0x55, 0x74, 196 + 0x7c, 0xd9, 0x41, 0xd6, 0xe8, 0x91, 0xa4, 0x85, 197 + 0xcb, 0x0a, 0x60, 0x08, 0x76, 0x07, 0x60, 0x99, 198 + 0x89, 0x76, 0xba, 0x84, 0xbd, 0x0b, 0xf2, 0xb3, 199 + 0xdc, 0xf3, 0x33, 0xd1, 0x9b, 0x0b, 0x2e, 0x5d, 200 + 0xf6, 0x9d, 0x0f, 0x67, 0xf4, 0x86, 0xb3, 0xd5, 201 + }, 202 + }, 203 + { 204 + .data_len = 511, 205 + .digest = { 206 + 0x7d, 0x83, 0x78, 0x6a, 0x5d, 0x52, 0x42, 0x2a, 207 + 0xb1, 0x97, 0xc6, 0x62, 0xa2, 0x2a, 0x7c, 0x8b, 208 + 0xcd, 0x4f, 0xa4, 0x86, 0x19, 0xa4, 0x5b, 0x1d, 209 + 0xc7, 0x6f, 0x2f, 0x9c, 0x03, 0xc3, 0x45, 0x2e, 210 + 0xa7, 0x8e, 0x38, 0xf2, 0x57, 0x55, 0x89, 0x47, 211 + 0xed, 0xeb, 0x81, 0xe2, 0xe0, 0x55, 0x9f, 0xe6, 212 + 0xca, 0x03, 0x59, 0xd3, 0xd4, 0xba, 0xc9, 0x2d, 213 + 0xaf, 0xbb, 0x62, 0x2e, 0xe6, 0x89, 0xe4, 0x11, 214 + }, 215 + }, 216 + { 217 + .data_len = 513, 218 + .digest = { 219 + 0xe9, 0x14, 0xe7, 0x01, 0xd0, 0x81, 0x09, 0x51, 220 + 0x78, 0x1c, 0x8e, 0x6c, 0x00, 0xd3, 0x28, 0xa0, 221 + 0x2a, 0x7b, 0xd6, 0x25, 0xca, 0xd0, 0xf9, 0xb8, 222 + 0xd8, 0xcf, 0xd0, 0xb7, 0x48, 0x25, 0xb7, 0x6a, 223 + 0x53, 0x8e, 0xf8, 0x52, 0x9c, 0x1f, 0x7d, 0xae, 224 + 0x4c, 0x22, 0xd5, 0x9d, 0xf0, 0xaf, 0x98, 0x91, 225 + 0x19, 0x1f, 0x99, 0xbd, 0xa6, 0xc2, 0x0f, 0x05, 226 + 0xa5, 0x9f, 0x3e, 0x87, 0xed, 0xc3, 0xab, 0x92, 227 + }, 228 + }, 229 + { 230 + .data_len = 1000, 231 + .digest = { 232 + 0x2e, 0xf4, 0x72, 0xd2, 0xd9, 0x4a, 0xd5, 0xf9, 233 + 0x20, 0x03, 0x4a, 0xad, 0xed, 0xa9, 0x1b, 0x64, 234 + 0x73, 0x38, 0xc6, 0x30, 0xa8, 0x7f, 0xf9, 0x3b, 235 + 0x8c, 0xbc, 0xa1, 0x2d, 0x22, 0x7b, 0x84, 0x37, 236 + 0xf5, 0xba, 0xee, 0xf0, 0x80, 0x9d, 0xe3, 0x82, 237 + 0xbd, 0x07, 0x68, 0x15, 0x01, 0x22, 0xf6, 0x88, 238 + 0x07, 0x0b, 0xfd, 0xb7, 0xb1, 0xc0, 0x68, 0x4b, 239 + 0x8d, 0x05, 0xec, 0xfb, 0xcd, 0xde, 0xa4, 0x2a, 240 + }, 241 + }, 242 + { 243 + .data_len = 3333, 244 + .digest = { 245 + 0x73, 0xe3, 0xe5, 0x87, 0x01, 0x0a, 0x29, 0x4d, 246 + 0xad, 0x92, 0x67, 0x64, 0xc7, 0x71, 0x0b, 0x22, 247 + 0x80, 0x8a, 0x6e, 0x8b, 0x20, 0x73, 0xb2, 0xd7, 248 + 0x98, 0x20, 0x35, 0x42, 0x42, 0x5d, 0x85, 0x12, 249 + 0xb0, 0x06, 0x69, 0x63, 0x5f, 0x5b, 0xe7, 0x63, 250 + 0x6f, 0xe6, 0x18, 0xa6, 0xc1, 0xa6, 0xae, 0x27, 251 + 0xa7, 0x6a, 0x73, 0x6b, 0x27, 0xd5, 0x47, 0xe1, 252 + 0xa2, 0x7d, 0xe4, 0x0d, 0xbd, 0x23, 0x7b, 0x7a, 253 + }, 254 + }, 255 + { 256 + .data_len = 4096, 257 + .digest = { 258 + 0x11, 0x5b, 0x77, 0x36, 0x6b, 0x3b, 0xe4, 0x42, 259 + 0xe4, 0x92, 0x23, 0xcb, 0x0c, 0x06, 0xff, 0xb7, 260 + 0x0c, 0x71, 0x64, 0xd9, 0x8a, 0x57, 0x75, 0x7b, 261 + 0xa2, 0xd2, 0x17, 0x19, 0xbb, 0xb5, 0x3c, 0xb3, 262 + 0x5f, 0xae, 0x35, 0x75, 0x8e, 0xa8, 0x97, 0x43, 263 + 0xce, 0xfe, 0x41, 0x84, 0xfe, 0xcb, 0x18, 0x70, 264 + 0x68, 0x2e, 0x16, 0x19, 0xd5, 0x10, 0x0d, 0x2f, 265 + 0x61, 0x87, 0x79, 0xee, 0x5f, 0x24, 0xdd, 0x76, 266 + }, 267 + }, 268 + { 269 + .data_len = 4128, 270 + .digest = { 271 + 0x9e, 0x96, 0xe1, 0x0a, 0xb2, 0xd5, 0xba, 0xcf, 272 + 0x27, 0xba, 0x6f, 0x85, 0xe9, 0xbf, 0x96, 0xb9, 273 + 0x5a, 0x00, 0x00, 0x06, 0xdc, 0xb7, 0xaf, 0x0a, 274 + 0x8f, 0x1d, 0x31, 0xf6, 0xce, 0xc3, 0x50, 0x2e, 275 + 0x61, 0x3a, 0x8b, 0x28, 0xaf, 0xb2, 0x50, 0x0d, 276 + 0x00, 0x98, 0x02, 0x11, 0x6b, 0xfa, 0x51, 0xc1, 277 + 0xde, 0xe1, 0x34, 0x9f, 0xda, 0x11, 0x63, 0xfa, 278 + 0x0a, 0xa0, 0xa2, 0x67, 0x39, 0xeb, 0x9b, 0xf1, 279 + }, 280 + }, 281 + { 282 + .data_len = 4160, 283 + .digest = { 284 + 0x46, 0x4e, 0x81, 0xd1, 0x08, 0x2a, 0x46, 0x12, 285 + 0x4e, 0xae, 0x1f, 0x5d, 0x57, 0xe5, 0x19, 0xbc, 286 + 0x76, 0x38, 0xb6, 0xa7, 0xe3, 0x72, 0x6d, 0xaf, 287 + 0x80, 0x3b, 0xd0, 0xbc, 0x06, 0xe8, 0xab, 0xab, 288 + 0x86, 0x4b, 0x0b, 0x7a, 0x61, 0xa6, 0x13, 0xff, 289 + 0x64, 0x47, 0x89, 0xb7, 0x63, 0x8a, 0xa5, 0x4c, 290 + 0x9f, 0x52, 0x70, 0xeb, 0x21, 0xe5, 0x2d, 0xe9, 291 + 0xe7, 0xab, 0x1c, 0x0e, 0x74, 0xf5, 0x72, 0xec, 292 + }, 293 + }, 294 + { 295 + .data_len = 4224, 296 + .digest = { 297 + 0xfa, 0x6e, 0xff, 0x3c, 0xc1, 0x98, 0x49, 0x42, 298 + 0x34, 0x67, 0xd4, 0xd3, 0xfa, 0xae, 0x27, 0xe4, 299 + 0x77, 0x11, 0x84, 0xd2, 0x57, 0x99, 0xf8, 0xfd, 300 + 0x41, 0x50, 0x84, 0x80, 0x7f, 0xf7, 0xb2, 0xd3, 301 + 0x88, 0x21, 0x9c, 0xe8, 0xb9, 0x05, 0xd3, 0x48, 302 + 0x64, 0xc5, 0xb7, 0x29, 0xd9, 0x21, 0x17, 0xad, 303 + 0x89, 0x9c, 0x79, 0x55, 0x51, 0x0b, 0x96, 0x3e, 304 + 0x10, 0x40, 0xe1, 0xdd, 0x7b, 0x39, 0x40, 0x86, 305 + }, 306 + }, 307 + { 308 + .data_len = 16384, 309 + .digest = { 310 + 0x41, 0xb3, 0xd2, 0x93, 0xcd, 0x79, 0x84, 0xc2, 311 + 0xf5, 0xea, 0xf3, 0xb3, 0x94, 0x23, 0xaa, 0x76, 312 + 0x87, 0x5f, 0xe3, 0xd2, 0x03, 0xd8, 0x00, 0xbb, 313 + 0xa1, 0x55, 0xe4, 0xcb, 0x16, 0x04, 0x5b, 0xdf, 314 + 0xf8, 0xd2, 0x63, 0x51, 0x02, 0x22, 0xc6, 0x0f, 315 + 0x98, 0x2b, 0x12, 0x52, 0x25, 0x64, 0x93, 0xd9, 316 + 0xab, 0xe9, 0x4d, 0x16, 0x4b, 0xf6, 0x09, 0x83, 317 + 0x5c, 0x63, 0x1c, 0x41, 0x19, 0xf6, 0x76, 0xe3, 318 + }, 319 + }, 320 + }; 321 + 322 + static const u8 hash_testvec_consolidated[SHA512_DIGEST_SIZE] = { 323 + 0x5b, 0x9d, 0xf9, 0xab, 0x8c, 0x8e, 0x52, 0xdb, 324 + 0x02, 0xa0, 0x4c, 0x24, 0x2d, 0xc4, 0xa8, 0x4e, 325 + 0x9c, 0x93, 0x2f, 0x72, 0xa8, 0x75, 0xfb, 0xb5, 326 + 0xdb, 0xef, 0x52, 0xc6, 0xa3, 0xfe, 0xeb, 0x6b, 327 + 0x92, 0x79, 0x18, 0x05, 0xf6, 0xd7, 0xaf, 0x7b, 328 + 0x36, 0xfc, 0x83, 0x2c, 0x7e, 0x7b, 0x59, 0x8b, 329 + 0xf9, 0x81, 0xaa, 0x98, 0x38, 0x11, 0x97, 0x56, 330 + 0x34, 0xe5, 0x2a, 0x4b, 0xf2, 0x9e, 0xf3, 0xdb, 331 + }; 332 + 333 + static const u8 hmac_testvec_consolidated[SHA512_DIGEST_SIZE] = { 334 + 0x40, 0xe7, 0xbc, 0x03, 0xdf, 0x22, 0xd4, 0x76, 335 + 0x66, 0x45, 0xf8, 0x1d, 0x25, 0xdf, 0xbe, 0xa2, 336 + 0x93, 0x06, 0x8c, 0x1d, 0x14, 0x23, 0x9b, 0x5c, 337 + 0xfa, 0xac, 0xdf, 0xbd, 0xa2, 0x24, 0xe5, 0xf7, 338 + 0xdc, 0xf7, 0xae, 0x96, 0xc1, 0x34, 0xe5, 0x24, 339 + 0x16, 0x24, 0xdc, 0xee, 0x4f, 0x62, 0x1c, 0x67, 340 + 0x4e, 0x02, 0x31, 0x4b, 0x9b, 0x65, 0x25, 0xeb, 341 + 0x32, 0x2e, 0x24, 0xfb, 0xcd, 0x2b, 0x59, 0xd8, 342 + };
+39
lib/crypto/tests/sha512_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/sha2.h> 6 + #include "sha512-testvecs.h" 7 + 8 + #define HASH sha512 9 + #define HASH_CTX sha512_ctx 10 + #define HASH_SIZE SHA512_DIGEST_SIZE 11 + #define HASH_INIT sha512_init 12 + #define HASH_UPDATE sha512_update 13 + #define HASH_FINAL sha512_final 14 + #define HMAC_KEY hmac_sha512_key 15 + #define HMAC_CTX hmac_sha512_ctx 16 + #define HMAC_PREPAREKEY hmac_sha512_preparekey 17 + #define HMAC_INIT hmac_sha512_init 18 + #define HMAC_UPDATE hmac_sha512_update 19 + #define HMAC_FINAL hmac_sha512_final 20 + #define HMAC hmac_sha512 21 + #define HMAC_USINGRAWKEY hmac_sha512_usingrawkey 22 + #include "hash-test-template.h" 23 + 24 + static struct kunit_case hash_test_cases[] = { 25 + HASH_KUNIT_CASES, 26 + KUNIT_CASE(benchmark_hash), 27 + {}, 28 + }; 29 + 30 + static struct kunit_suite hash_test_suite = { 31 + .name = "sha512", 32 + .test_cases = hash_test_cases, 33 + .suite_init = hash_suite_init, 34 + .suite_exit = hash_suite_exit, 35 + }; 36 + kunit_test_suite(hash_test_suite); 37 + 38 + MODULE_DESCRIPTION("KUnit tests and benchmark for SHA-512 and HMAC-SHA512"); 39 + MODULE_LICENSE("GPL");
+147
scripts/crypto/gen-hash-testvecs.py
··· 1 + #!/usr/bin/env python3 2 + # SPDX-License-Identifier: GPL-2.0-or-later 3 + # 4 + # Script that generates test vectors for the given cryptographic hash function. 5 + # 6 + # Copyright 2025 Google LLC 7 + 8 + import hashlib 9 + import hmac 10 + import sys 11 + 12 + DATA_LENS = [0, 1, 2, 3, 16, 32, 48, 49, 63, 64, 65, 127, 128, 129, 256, 511, 13 + 513, 1000, 3333, 4096, 4128, 4160, 4224, 16384] 14 + 15 + # Generate the given number of random bytes, using the length itself as the seed 16 + # for a simple linear congruential generator (LCG). The C test code uses the 17 + # same LCG with the same seeding strategy to reconstruct the data, ensuring 18 + # reproducibility without explicitly storing the data in the test vectors. 19 + def rand_bytes(length): 20 + seed = length 21 + out = [] 22 + for _ in range(length): 23 + seed = (seed * 25214903917 + 11) % 2**48 24 + out.append((seed >> 16) % 256) 25 + return bytes(out) 26 + 27 + POLY1305_KEY_SIZE = 32 28 + 29 + # A straightforward, unoptimized implementation of Poly1305. 30 + # Reference: https://cr.yp.to/mac/poly1305-20050329.pdf 31 + class Poly1305: 32 + def __init__(self, key): 33 + assert len(key) == POLY1305_KEY_SIZE 34 + self.h = 0 35 + rclamp = 0x0ffffffc0ffffffc0ffffffc0fffffff 36 + self.r = int.from_bytes(key[:16], byteorder='little') & rclamp 37 + self.s = int.from_bytes(key[16:], byteorder='little') 38 + 39 + # Note: this supports partial blocks only at the end. 40 + def update(self, data): 41 + for i in range(0, len(data), 16): 42 + chunk = data[i:i+16] 43 + c = int.from_bytes(chunk, byteorder='little') + 2**(8 * len(chunk)) 44 + self.h = ((self.h + c) * self.r) % (2**130 - 5) 45 + return self 46 + 47 + # Note: gen_additional_poly1305_testvecs() relies on this being 48 + # nondestructive, i.e. not changing any field of self. 49 + def digest(self): 50 + m = (self.h + self.s) % 2**128 51 + return m.to_bytes(16, byteorder='little') 52 + 53 + def hash_init(alg): 54 + if alg == 'poly1305': 55 + # Use a fixed random key here, to present Poly1305 as an unkeyed hash. 56 + # This allows all the test cases for unkeyed hashes to work on Poly1305. 57 + return Poly1305(rand_bytes(POLY1305_KEY_SIZE)) 58 + return hashlib.new(alg) 59 + 60 + def hash_update(ctx, data): 61 + ctx.update(data) 62 + 63 + def hash_final(ctx): 64 + return ctx.digest() 65 + 66 + def compute_hash(alg, data): 67 + ctx = hash_init(alg) 68 + hash_update(ctx, data) 69 + return hash_final(ctx) 70 + 71 + def print_bytes(prefix, value, bytes_per_line): 72 + for i in range(0, len(value), bytes_per_line): 73 + line = prefix + ''.join(f'0x{b:02x}, ' for b in value[i:i+bytes_per_line]) 74 + print(f'{line.rstrip()}') 75 + 76 + def print_static_u8_array_definition(name, value): 77 + print('') 78 + print(f'static const u8 {name} = {{') 79 + print_bytes('\t', value, 8) 80 + print('};') 81 + 82 + def print_c_struct_u8_array_field(name, value): 83 + print(f'\t\t.{name} = {{') 84 + print_bytes('\t\t\t', value, 8) 85 + print('\t\t},') 86 + 87 + def gen_unkeyed_testvecs(alg): 88 + print('') 89 + print('static const struct {') 90 + print('\tsize_t data_len;') 91 + print(f'\tu8 digest[{alg.upper()}_DIGEST_SIZE];') 92 + print('} hash_testvecs[] = {') 93 + for data_len in DATA_LENS: 94 + data = rand_bytes(data_len) 95 + print('\t{') 96 + print(f'\t\t.data_len = {data_len},') 97 + print_c_struct_u8_array_field('digest', compute_hash(alg, data)) 98 + print('\t},') 99 + print('};') 100 + 101 + data = rand_bytes(4096) 102 + ctx = hash_init(alg) 103 + for data_len in range(len(data) + 1): 104 + hash_update(ctx, compute_hash(alg, data[:data_len])) 105 + print_static_u8_array_definition( 106 + f'hash_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]', 107 + hash_final(ctx)) 108 + 109 + def gen_hmac_testvecs(alg): 110 + ctx = hmac.new(rand_bytes(32), digestmod=alg) 111 + data = rand_bytes(4096) 112 + for data_len in range(len(data) + 1): 113 + ctx.update(data[:data_len]) 114 + key_len = data_len % 293 115 + key = rand_bytes(key_len) 116 + mac = hmac.digest(key, data[:data_len], alg) 117 + ctx.update(mac) 118 + print_static_u8_array_definition( 119 + f'hmac_testvec_consolidated[{alg.upper()}_DIGEST_SIZE]', 120 + ctx.digest()) 121 + 122 + def gen_additional_poly1305_testvecs(): 123 + key = b'\xff' * POLY1305_KEY_SIZE 124 + data = b'' 125 + ctx = Poly1305(key) 126 + for _ in range(32): 127 + for j in range(0, 4097, 16): 128 + ctx.update(b'\xff' * j) 129 + data += ctx.digest() 130 + print_static_u8_array_definition( 131 + 'poly1305_allones_macofmacs[POLY1305_DIGEST_SIZE]', 132 + Poly1305(key).update(data).digest()) 133 + 134 + if len(sys.argv) != 2: 135 + sys.stderr.write('Usage: gen-hash-testvecs.py ALGORITHM\n') 136 + sys.stderr.write('ALGORITHM may be any supported by Python hashlib, or poly1305.\n') 137 + sys.stderr.write('Example: gen-hash-testvecs.py sha512\n') 138 + sys.exit(1) 139 + 140 + alg = sys.argv[1] 141 + print('/* SPDX-License-Identifier: GPL-2.0-or-later */') 142 + print(f'/* This file was generated by: {sys.argv[0]} {" ".join(sys.argv[1:])} */') 143 + gen_unkeyed_testvecs(alg) 144 + if alg == 'poly1305': 145 + gen_additional_poly1305_testvecs() 146 + else: 147 + gen_hmac_testvecs(alg)