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.

lib/crypto: tests: Add KUnit tests for SM3

Add a KUnit test suite for the SM3 library. It closely mirrors the test
suites for the other cryptographic hash functions. The actual test and
benchmark logic is already in hash-test-template.h; this just wires it
up for SM3 in the usual way.

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

+276
+1
lib/crypto/.kunitconfig
··· 16 16 CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST=y 17 17 CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST=y 18 18 CONFIG_CRYPTO_LIB_SHA3_KUNIT_TEST=y 19 + CONFIG_CRYPTO_LIB_SM3_KUNIT_TEST=y
+9
lib/crypto/tests/Kconfig
··· 124 124 including SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128 and 125 125 SHAKE256. 126 126 127 + config CRYPTO_LIB_SM3_KUNIT_TEST 128 + tristate "KUnit tests for SM3" if !KUNIT_ALL_TESTS 129 + depends on KUNIT && CRYPTO_LIB_SM3 130 + default KUNIT_ALL_TESTS 131 + select CRYPTO_LIB_BENCHMARK_VISIBLE 132 + help 133 + KUnit tests for the SM3 cryptographic hash function. 134 + 127 135 config CRYPTO_LIB_ENABLE_ALL_FOR_KUNIT 128 136 tristate "Enable all crypto library code for KUnit tests" 129 137 depends on KUNIT ··· 147 139 select CRYPTO_LIB_SHA256 148 140 select CRYPTO_LIB_SHA512 149 141 select CRYPTO_LIB_SHA3 142 + select CRYPTO_LIB_SM3 150 143 help 151 144 Enable all the crypto library code that has KUnit tests. 152 145
+1
lib/crypto/tests/Makefile
··· 14 14 obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o 15 15 obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o sha512_kunit.o 16 16 obj-$(CONFIG_CRYPTO_LIB_SHA3_KUNIT_TEST) += sha3_kunit.o 17 + obj-$(CONFIG_CRYPTO_LIB_SM3_KUNIT_TEST) += sm3_kunit.o
+231
lib/crypto/tests/sm3-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py sm3 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[SM3_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f, 12 + 0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f, 13 + 0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74, 14 + 0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b, 15 + }, 16 + }, 17 + { 18 + .data_len = 1, 19 + .digest = { 20 + 0xb6, 0x22, 0x2c, 0x39, 0xdc, 0x14, 0x9a, 0xee, 21 + 0x01, 0x9a, 0xcb, 0x0d, 0xe6, 0xc6, 0x75, 0x6e, 22 + 0x8f, 0x18, 0x7b, 0x0e, 0xe8, 0x98, 0x61, 0x71, 23 + 0x2b, 0xd8, 0x38, 0xa9, 0xee, 0x2c, 0x1e, 0x93, 24 + }, 25 + }, 26 + { 27 + .data_len = 2, 28 + .digest = { 29 + 0x62, 0x0c, 0x66, 0x77, 0x67, 0x28, 0x74, 0x8a, 30 + 0xe3, 0x64, 0xea, 0x44, 0x6a, 0x3f, 0x34, 0x61, 31 + 0x55, 0xc5, 0xaa, 0xb2, 0x6c, 0x67, 0x97, 0x68, 32 + 0x68, 0xae, 0x4d, 0x64, 0xa8, 0xb6, 0x72, 0x3e, 33 + }, 34 + }, 35 + { 36 + .data_len = 3, 37 + .digest = { 38 + 0x71, 0xd4, 0x63, 0xb1, 0xfa, 0x27, 0xc7, 0xae, 39 + 0x65, 0xed, 0x5c, 0x93, 0x70, 0xe0, 0x09, 0x34, 40 + 0x2f, 0x42, 0xe6, 0x71, 0x16, 0x8e, 0x90, 0x90, 41 + 0x9a, 0xdd, 0xa6, 0x44, 0x66, 0x71, 0x18, 0xf9, 42 + }, 43 + }, 44 + { 45 + .data_len = 16, 46 + .digest = { 47 + 0x79, 0x0b, 0x68, 0xb5, 0x41, 0xc1, 0x97, 0xa0, 48 + 0x50, 0xe6, 0x93, 0x70, 0xf6, 0x98, 0x49, 0xea, 49 + 0x92, 0xc9, 0xd0, 0xb1, 0x46, 0xbd, 0x4a, 0x0c, 50 + 0x8e, 0xe8, 0xf3, 0xe4, 0x8f, 0x90, 0x33, 0x3c, 51 + }, 52 + }, 53 + { 54 + .data_len = 32, 55 + .digest = { 56 + 0x32, 0x9f, 0xa3, 0x18, 0x18, 0x45, 0xe0, 0x28, 57 + 0xd3, 0xa4, 0x41, 0x3a, 0x25, 0x62, 0x9c, 0x95, 58 + 0xab, 0xfe, 0x02, 0xe0, 0x37, 0x7d, 0x3c, 0xc4, 59 + 0xce, 0x69, 0x57, 0x5a, 0x07, 0x0e, 0xb9, 0xf5, 60 + }, 61 + }, 62 + { 63 + .data_len = 48, 64 + .digest = { 65 + 0x0c, 0xcf, 0x7c, 0x48, 0x44, 0xa0, 0xb0, 0x8d, 66 + 0xdf, 0xbe, 0x22, 0x14, 0x7e, 0xd4, 0xc3, 0x8d, 67 + 0x6a, 0x23, 0xfc, 0x44, 0x0e, 0x0f, 0xde, 0xa5, 68 + 0x7c, 0x8b, 0xc4, 0x8b, 0xab, 0x8c, 0x87, 0x41, 69 + }, 70 + }, 71 + { 72 + .data_len = 49, 73 + .digest = { 74 + 0xb3, 0x76, 0x8b, 0x19, 0xf9, 0x10, 0xa9, 0x56, 75 + 0x4f, 0xce, 0x27, 0xaa, 0x65, 0x96, 0xe5, 0xdb, 76 + 0x90, 0x9b, 0x92, 0xcd, 0x32, 0x0d, 0x16, 0xac, 77 + 0xf8, 0xd0, 0x66, 0x62, 0x10, 0xf0, 0x44, 0xdf, 78 + }, 79 + }, 80 + { 81 + .data_len = 63, 82 + .digest = { 83 + 0x07, 0xc9, 0x45, 0x65, 0x9f, 0x68, 0x75, 0xc3, 84 + 0x74, 0xb2, 0x3b, 0x0c, 0x97, 0x05, 0xd3, 0x13, 85 + 0xc0, 0xb6, 0x21, 0xed, 0xf6, 0x10, 0x7a, 0xed, 86 + 0xec, 0xd8, 0x10, 0x29, 0xbf, 0x7a, 0x78, 0x37, 87 + }, 88 + }, 89 + { 90 + .data_len = 64, 91 + .digest = { 92 + 0x3e, 0x69, 0x18, 0x45, 0xd8, 0x25, 0x6f, 0x44, 93 + 0xc0, 0x02, 0xe5, 0xcf, 0xcd, 0x94, 0x42, 0xa9, 94 + 0xd5, 0x12, 0x62, 0x10, 0x15, 0xa0, 0xf9, 0x16, 95 + 0x19, 0x2d, 0x8d, 0x63, 0x31, 0xf2, 0x2f, 0x36, 96 + }, 97 + }, 98 + { 99 + .data_len = 65, 100 + .digest = { 101 + 0x6b, 0x3e, 0xc0, 0x20, 0xb7, 0x74, 0x30, 0xa0, 102 + 0xc6, 0x5c, 0xee, 0xbe, 0xdc, 0xe6, 0xe5, 0x4f, 103 + 0x3c, 0x61, 0x8d, 0x91, 0xac, 0x31, 0x4b, 0x2a, 104 + 0xdf, 0x1c, 0xef, 0x24, 0xdc, 0x0a, 0x10, 0xe8, 105 + }, 106 + }, 107 + { 108 + .data_len = 127, 109 + .digest = { 110 + 0xab, 0xd6, 0xa1, 0xbf, 0x39, 0x43, 0x75, 0xda, 111 + 0xbf, 0xc7, 0x22, 0xcc, 0x4e, 0xfc, 0xe4, 0x42, 112 + 0x6d, 0x1b, 0x87, 0x25, 0x64, 0x7f, 0x88, 0xf7, 113 + 0xc3, 0x0a, 0x0a, 0x4c, 0xd6, 0xa7, 0x68, 0xae, 114 + }, 115 + }, 116 + { 117 + .data_len = 128, 118 + .digest = { 119 + 0x1b, 0x70, 0xd4, 0x5f, 0x6c, 0xe4, 0x2d, 0x58, 120 + 0x2d, 0x0f, 0x9a, 0x12, 0x34, 0xbb, 0x5e, 0x38, 121 + 0xd8, 0x1f, 0x6a, 0x46, 0x8a, 0xef, 0xdb, 0x68, 122 + 0x18, 0x62, 0xbb, 0x85, 0xfc, 0xc4, 0x6e, 0x2e, 123 + }, 124 + }, 125 + { 126 + .data_len = 129, 127 + .digest = { 128 + 0x33, 0x62, 0xba, 0xa7, 0x4a, 0xbc, 0xd7, 0x7b, 129 + 0xd4, 0x67, 0x6d, 0x3e, 0xea, 0xe8, 0xb0, 0x64, 130 + 0x0d, 0xf3, 0xae, 0x1d, 0x52, 0x24, 0x11, 0x9f, 131 + 0xda, 0xa9, 0x7f, 0xd5, 0x22, 0x1a, 0xde, 0x8a, 132 + }, 133 + }, 134 + { 135 + .data_len = 256, 136 + .digest = { 137 + 0x70, 0xa8, 0xa6, 0x2b, 0xfb, 0x1f, 0x3b, 0x5a, 138 + 0xcc, 0x71, 0x76, 0x9e, 0x25, 0x4c, 0xfa, 0x8f, 139 + 0x39, 0x4a, 0x21, 0x8a, 0x9d, 0x74, 0x8d, 0x2c, 140 + 0x31, 0xa5, 0xb5, 0xff, 0x30, 0xc1, 0x14, 0xc4, 141 + }, 142 + }, 143 + { 144 + .data_len = 511, 145 + .digest = { 146 + 0x39, 0xd0, 0x8c, 0x5f, 0xfc, 0x36, 0xc2, 0x7c, 147 + 0xdb, 0x8b, 0x2e, 0xdc, 0x9d, 0x1b, 0xd1, 0xba, 148 + 0x9b, 0x52, 0x6b, 0x35, 0x46, 0x46, 0x75, 0x73, 149 + 0xe5, 0x62, 0x96, 0x6e, 0xf3, 0xba, 0xd9, 0x19, 150 + }, 151 + }, 152 + { 153 + .data_len = 513, 154 + .digest = { 155 + 0x76, 0xa0, 0x3d, 0xa2, 0x5f, 0xd4, 0xa6, 0xbe, 156 + 0x6b, 0xdb, 0xed, 0x14, 0x9e, 0xa8, 0x15, 0x77, 157 + 0xa9, 0x38, 0x30, 0x6b, 0x68, 0xfa, 0xb6, 0xe2, 158 + 0x93, 0x19, 0x24, 0x72, 0x67, 0x20, 0x72, 0xc3, 159 + }, 160 + }, 161 + { 162 + .data_len = 1000, 163 + .digest = { 164 + 0x16, 0xbc, 0x33, 0x77, 0x0b, 0xcf, 0x93, 0x5e, 165 + 0xec, 0x7d, 0x8d, 0x3c, 0xae, 0xd9, 0x75, 0xdf, 166 + 0x46, 0x24, 0x17, 0x7e, 0x03, 0x88, 0xf2, 0x75, 167 + 0xa9, 0x18, 0xa6, 0x1c, 0x7a, 0x74, 0x0d, 0xf3, 168 + }, 169 + }, 170 + { 171 + .data_len = 3333, 172 + .digest = { 173 + 0xdb, 0x54, 0x89, 0xe7, 0x1c, 0x50, 0xf2, 0xbf, 174 + 0xde, 0x3a, 0xbf, 0x5b, 0xee, 0x5a, 0x46, 0x62, 175 + 0x20, 0xb1, 0x80, 0x48, 0xac, 0x56, 0x33, 0xb3, 176 + 0xbb, 0x3f, 0xfa, 0x02, 0xc6, 0x43, 0xb5, 0x8c, 177 + }, 178 + }, 179 + { 180 + .data_len = 4096, 181 + .digest = { 182 + 0xdf, 0x0d, 0xed, 0x3b, 0x8f, 0xea, 0x81, 0xfd, 183 + 0xd6, 0x34, 0xae, 0x74, 0x24, 0x3a, 0x15, 0x38, 184 + 0xe7, 0xcf, 0x45, 0x7e, 0x8f, 0xf5, 0x50, 0x6c, 185 + 0xaa, 0x27, 0x23, 0x92, 0x6d, 0xab, 0x3b, 0xde, 186 + }, 187 + }, 188 + { 189 + .data_len = 4128, 190 + .digest = { 191 + 0x6a, 0xbd, 0x56, 0x5a, 0xf1, 0xc6, 0x40, 0x4d, 192 + 0xf3, 0x50, 0x77, 0x87, 0x86, 0x63, 0x1b, 0x4d, 193 + 0x21, 0x99, 0x96, 0xad, 0x24, 0x62, 0xce, 0xc0, 194 + 0x3e, 0xb7, 0x35, 0x52, 0x56, 0x0e, 0x55, 0x85, 195 + }, 196 + }, 197 + { 198 + .data_len = 4160, 199 + .digest = { 200 + 0x5b, 0xc1, 0x1f, 0x25, 0x43, 0xa3, 0x1c, 0xa0, 201 + 0x8c, 0xfc, 0x41, 0xf1, 0xcc, 0xb3, 0x95, 0x95, 202 + 0xe0, 0xb9, 0xd3, 0x29, 0xf4, 0x08, 0x31, 0x47, 203 + 0x6d, 0x09, 0xa8, 0x2e, 0xa5, 0xf4, 0xf1, 0x8d, 204 + }, 205 + }, 206 + { 207 + .data_len = 4224, 208 + .digest = { 209 + 0xec, 0x56, 0x1e, 0xa6, 0x1f, 0xb2, 0x87, 0xb2, 210 + 0x7e, 0x15, 0xd6, 0x30, 0x08, 0x74, 0xb0, 0x48, 211 + 0x90, 0x2a, 0xbe, 0x2f, 0x80, 0x3a, 0x88, 0xcc, 212 + 0xd7, 0xc5, 0x87, 0x8c, 0x04, 0xef, 0x78, 0x71, 213 + }, 214 + }, 215 + { 216 + .data_len = 16384, 217 + .digest = { 218 + 0xe7, 0xb8, 0x84, 0x20, 0xff, 0xd5, 0x53, 0xe6, 219 + 0x14, 0x31, 0x12, 0x75, 0xb7, 0x9a, 0x4f, 0x63, 220 + 0x63, 0x00, 0xfe, 0x2c, 0x54, 0xee, 0x06, 0xfc, 221 + 0x12, 0x16, 0xe5, 0xdc, 0xa4, 0x40, 0x62, 0x12, 222 + }, 223 + }, 224 + }; 225 + 226 + static const u8 hash_testvec_consolidated[SM3_DIGEST_SIZE] = { 227 + 0xe6, 0x58, 0xd4, 0x8e, 0x74, 0x92, 0xdf, 0xfe, 228 + 0x58, 0x05, 0xe5, 0x29, 0x83, 0xfb, 0xb7, 0x51, 229 + 0x7e, 0x66, 0x0c, 0x49, 0x3e, 0x11, 0x7e, 0x9b, 230 + 0xb1, 0x83, 0x3a, 0xa6, 0xb0, 0x3c, 0xf5, 0xe0, 231 + };
+31
lib/crypto/tests/sm3_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2026 Google LLC 4 + */ 5 + #include <crypto/sm3.h> 6 + #include "sm3-testvecs.h" 7 + 8 + #define HASH sm3 9 + #define HASH_CTX sm3_ctx 10 + #define HASH_SIZE SM3_DIGEST_SIZE 11 + #define HASH_INIT sm3_init 12 + #define HASH_UPDATE sm3_update 13 + #define HASH_FINAL sm3_final 14 + #include "hash-test-template.h" 15 + 16 + static struct kunit_case sm3_test_cases[] = { 17 + HASH_KUNIT_CASES, 18 + KUNIT_CASE(benchmark_hash), 19 + {}, 20 + }; 21 + 22 + static struct kunit_suite sm3_test_suite = { 23 + .name = "sm3", 24 + .test_cases = sm3_test_cases, 25 + .suite_init = hash_suite_init, 26 + .suite_exit = hash_suite_exit, 27 + }; 28 + kunit_test_suite(sm3_test_suite); 29 + 30 + MODULE_DESCRIPTION("KUnit tests and benchmark for SM3"); 31 + MODULE_LICENSE("GPL");
+3
scripts/crypto/gen-hash-testvecs.py
··· 356 356 print() 357 357 print('/* SHAKE test vectors */') 358 358 gen_additional_sha3_testvecs() 359 + elif alg == 'sm3': 360 + gen_unkeyed_testvecs(alg) 361 + # Kernel doesn't implement HMAC-SM3 library functions yet. 359 362 else: 360 363 gen_unkeyed_testvecs(alg) 361 364 gen_hmac_testvecs(alg)