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

lib/crypto: tests: Add KUnit tests for SHA-224 and SHA-256

Add KUnit test suites for the SHA-224 and SHA-256 library functions,
including the corresponding HMAC support. The core test logic is in the
previously-added hash-test-template.h. This commit just adds the actual
KUnit suites, and it adds the generated test vectors to the tree so that
gen-hash-testvecs.py won't have to be run at build time.

Acked-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250709200112.258500-3-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+590
+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
+29
lib/crypto/tests/Kconfig
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + # Option is named *_SHA256_KUNIT_TEST, though both SHA-224 and SHA-256 tests are 4 + # included, for consistency with the naming used elsewhere (e.g. CRYPTO_SHA256). 5 + config CRYPTO_LIB_SHA256_KUNIT_TEST 6 + tristate "KUnit tests for SHA-224 and SHA-256" if !KUNIT_ALL_TESTS 7 + depends on KUNIT 8 + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS 9 + select CRYPTO_LIB_BENCHMARK_VISIBLE 10 + select CRYPTO_LIB_SHA256 11 + help 12 + KUnit tests for the SHA-224 and SHA-256 cryptographic hash functions 13 + and their corresponding HMACs. 14 + 15 + config CRYPTO_LIB_BENCHMARK_VISIBLE 16 + bool 17 + 18 + config CRYPTO_LIB_BENCHMARK 19 + bool "Include benchmarks in KUnit tests for cryptographic functions" 20 + depends on CRYPTO_LIB_BENCHMARK_VISIBLE 21 + help 22 + Include benchmarks in the KUnit tests for cryptographic functions. 23 + The benchmark results are printed to the kernel log when the 24 + corresponding KUnit test suite runs. 25 + 26 + This is useful for evaluating the performance of the cryptographic 27 + functions. However, it will increase the runtime of the KUnit tests. 28 + 29 + If you're only interested in correctness testing, leave this disabled.
+3
lib/crypto/tests/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + 3 + obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o
+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");