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 MD5 and HMAC-MD5

Add a KUnit test suite for the MD5 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 suite, 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.

Link: https://lore.kernel.org/r/20250805222855.10362-8-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+236
+10
lib/crypto/tests/Kconfig
··· 1 1 # SPDX-License-Identifier: GPL-2.0-or-later 2 2 3 + config CRYPTO_LIB_MD5_KUNIT_TEST 4 + tristate "KUnit tests for MD5" 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_MD5 9 + help 10 + KUnit tests for the MD5 cryptographic hash function and its 11 + corresponding HMAC. 12 + 3 13 config CRYPTO_LIB_POLY1305_KUNIT_TEST 4 14 tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS 5 15 depends on KUNIT
+1
lib/crypto/tests/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-or-later 2 2 3 + obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o 3 4 obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o 4 5 obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o 5 6 obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o
+186
lib/crypto/tests/md5-testvecs.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* This file was generated by: ./scripts/crypto/gen-hash-testvecs.py md5 */ 3 + 4 + static const struct { 5 + size_t data_len; 6 + u8 digest[MD5_DIGEST_SIZE]; 7 + } hash_testvecs[] = { 8 + { 9 + .data_len = 0, 10 + .digest = { 11 + 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 12 + 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e, 13 + }, 14 + }, 15 + { 16 + .data_len = 1, 17 + .digest = { 18 + 0x16, 0x7b, 0x86, 0xf2, 0x1d, 0xf3, 0x76, 0xc9, 19 + 0x6f, 0x10, 0xa0, 0x61, 0x5b, 0x14, 0x20, 0x0b, 20 + }, 21 + }, 22 + { 23 + .data_len = 2, 24 + .digest = { 25 + 0x2d, 0x30, 0x96, 0xc7, 0x43, 0x40, 0xed, 0xb2, 26 + 0xfb, 0x84, 0x63, 0x9a, 0xec, 0xc7, 0x3c, 0x3c, 27 + }, 28 + }, 29 + { 30 + .data_len = 3, 31 + .digest = { 32 + 0xe5, 0x0f, 0xce, 0xe0, 0xc8, 0xff, 0x4e, 0x08, 33 + 0x5e, 0x19, 0xe5, 0xf2, 0x08, 0x11, 0x19, 0x16, 34 + }, 35 + }, 36 + { 37 + .data_len = 16, 38 + .digest = { 39 + 0xe8, 0xca, 0x29, 0x05, 0x2f, 0xd1, 0xf3, 0x99, 40 + 0x40, 0x71, 0xf5, 0xc2, 0xf7, 0xf8, 0x17, 0x3e, 41 + }, 42 + }, 43 + { 44 + .data_len = 32, 45 + .digest = { 46 + 0xe3, 0x20, 0xc1, 0xd8, 0x21, 0x14, 0x44, 0x59, 47 + 0x1a, 0xf5, 0x91, 0xaf, 0x69, 0xbe, 0x93, 0x9d, 48 + }, 49 + }, 50 + { 51 + .data_len = 48, 52 + .digest = { 53 + 0xfb, 0x06, 0xb0, 0xf0, 0x00, 0x10, 0x4b, 0x68, 54 + 0x3d, 0x75, 0xf9, 0x70, 0xde, 0xbb, 0x32, 0x16, 55 + }, 56 + }, 57 + { 58 + .data_len = 49, 59 + .digest = { 60 + 0x52, 0x86, 0x48, 0x8b, 0xae, 0x91, 0x7c, 0x4e, 61 + 0xc2, 0x2a, 0x69, 0x07, 0x35, 0xcc, 0xb2, 0x88, 62 + }, 63 + }, 64 + { 65 + .data_len = 63, 66 + .digest = { 67 + 0xfa, 0xd3, 0xf6, 0xe6, 0x7b, 0x1a, 0xc6, 0x05, 68 + 0x73, 0x35, 0x02, 0xab, 0xc7, 0xb3, 0x47, 0xcb, 69 + }, 70 + }, 71 + { 72 + .data_len = 64, 73 + .digest = { 74 + 0xc5, 0x59, 0x29, 0xe9, 0x0a, 0x4a, 0x86, 0x43, 75 + 0x7c, 0xaf, 0xdf, 0x83, 0xd3, 0xb8, 0x33, 0x5f, 76 + }, 77 + }, 78 + { 79 + .data_len = 65, 80 + .digest = { 81 + 0x80, 0x05, 0x75, 0x39, 0xec, 0x44, 0x8a, 0x81, 82 + 0xe7, 0x6e, 0x8d, 0xd1, 0xc6, 0xeb, 0xc2, 0xf0, 83 + }, 84 + }, 85 + { 86 + .data_len = 127, 87 + .digest = { 88 + 0x3f, 0x02, 0xe8, 0xc6, 0xb8, 0x6a, 0x39, 0xc3, 89 + 0xa4, 0x1c, 0xd9, 0x8f, 0x4a, 0x71, 0x40, 0x30, 90 + }, 91 + }, 92 + { 93 + .data_len = 128, 94 + .digest = { 95 + 0x89, 0x4f, 0x79, 0x3e, 0xff, 0x0c, 0x22, 0x60, 96 + 0xa2, 0xdc, 0x10, 0x5f, 0x23, 0x0a, 0xe7, 0xc6, 97 + }, 98 + }, 99 + { 100 + .data_len = 129, 101 + .digest = { 102 + 0x06, 0x56, 0x61, 0xb8, 0x8a, 0x82, 0x77, 0x1b, 103 + 0x2c, 0x35, 0xb8, 0x9f, 0xd6, 0xf7, 0xbd, 0x5a, 104 + }, 105 + }, 106 + { 107 + .data_len = 256, 108 + .digest = { 109 + 0x5d, 0xdf, 0x7d, 0xc8, 0x43, 0x96, 0x3b, 0xdb, 110 + 0xc7, 0x0e, 0x44, 0x42, 0x23, 0xf7, 0xed, 0xdf, 111 + }, 112 + }, 113 + { 114 + .data_len = 511, 115 + .digest = { 116 + 0xf6, 0x5f, 0x26, 0x51, 0x8a, 0x5a, 0x46, 0x8f, 117 + 0x48, 0x72, 0x90, 0x74, 0x9d, 0x87, 0xbd, 0xdf, 118 + }, 119 + }, 120 + { 121 + .data_len = 513, 122 + .digest = { 123 + 0xd8, 0x2c, 0xc9, 0x76, 0xfa, 0x67, 0x2e, 0xa6, 124 + 0xc8, 0x12, 0x4a, 0x64, 0xaa, 0x0b, 0x3d, 0xbd, 125 + }, 126 + }, 127 + { 128 + .data_len = 1000, 129 + .digest = { 130 + 0xe2, 0x7e, 0xb4, 0x5f, 0xe1, 0x74, 0x51, 0xfc, 131 + 0xe0, 0xc8, 0xd5, 0xe6, 0x8b, 0x40, 0xd2, 0x0e, 132 + }, 133 + }, 134 + { 135 + .data_len = 3333, 136 + .digest = { 137 + 0xcd, 0x7d, 0x56, 0xa9, 0x4c, 0x47, 0xea, 0xc2, 138 + 0x34, 0x0b, 0x84, 0x05, 0xf9, 0xad, 0xbb, 0x46, 139 + }, 140 + }, 141 + { 142 + .data_len = 4096, 143 + .digest = { 144 + 0x63, 0x6e, 0x58, 0xb3, 0x94, 0x6b, 0x83, 0x5f, 145 + 0x1f, 0x0e, 0xd3, 0x66, 0x78, 0x71, 0x98, 0x42, 146 + }, 147 + }, 148 + { 149 + .data_len = 4128, 150 + .digest = { 151 + 0x9d, 0x68, 0xfc, 0x26, 0x8b, 0x4c, 0xa8, 0xe7, 152 + 0x30, 0x0b, 0x19, 0x52, 0x6e, 0xa5, 0x65, 0x1c, 153 + }, 154 + }, 155 + { 156 + .data_len = 4160, 157 + .digest = { 158 + 0x1c, 0xaa, 0x7d, 0xee, 0x91, 0x01, 0xe2, 0x5a, 159 + 0xec, 0xe9, 0xde, 0x57, 0x0a, 0xb6, 0x4c, 0x2f, 160 + }, 161 + }, 162 + { 163 + .data_len = 4224, 164 + .digest = { 165 + 0x1b, 0x31, 0xe3, 0x14, 0x07, 0x16, 0x17, 0xc6, 166 + 0x98, 0x79, 0x88, 0x23, 0xb6, 0x3b, 0x25, 0xc4, 167 + }, 168 + }, 169 + { 170 + .data_len = 16384, 171 + .digest = { 172 + 0xc6, 0x3d, 0x56, 0x90, 0xf0, 0xf6, 0xe6, 0x50, 173 + 0xf4, 0x76, 0x78, 0x67, 0xa3, 0xdd, 0x62, 0x7b, 174 + }, 175 + }, 176 + }; 177 + 178 + static const u8 hash_testvec_consolidated[MD5_DIGEST_SIZE] = { 179 + 0x70, 0x86, 0x9e, 0x6c, 0xa4, 0xc6, 0x71, 0x43, 180 + 0x26, 0x02, 0x1b, 0x3f, 0xfd, 0x56, 0x9f, 0xa6, 181 + }; 182 + 183 + static const u8 hmac_testvec_consolidated[MD5_DIGEST_SIZE] = { 184 + 0x10, 0x02, 0x74, 0xf6, 0x4d, 0xb3, 0x3c, 0xc7, 185 + 0xa1, 0xf7, 0xe6, 0xd4, 0x32, 0x64, 0xfa, 0x6d, 186 + };
+39
lib/crypto/tests/md5_kunit.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Copyright 2025 Google LLC 4 + */ 5 + #include <crypto/md5.h> 6 + #include "md5-testvecs.h" 7 + 8 + #define HASH md5 9 + #define HASH_CTX md5_ctx 10 + #define HASH_SIZE MD5_DIGEST_SIZE 11 + #define HASH_INIT md5_init 12 + #define HASH_UPDATE md5_update 13 + #define HASH_FINAL md5_final 14 + #define HMAC_KEY hmac_md5_key 15 + #define HMAC_CTX hmac_md5_ctx 16 + #define HMAC_PREPAREKEY hmac_md5_preparekey 17 + #define HMAC_INIT hmac_md5_init 18 + #define HMAC_UPDATE hmac_md5_update 19 + #define HMAC_FINAL hmac_md5_final 20 + #define HMAC hmac_md5 21 + #define HMAC_USINGRAWKEY hmac_md5_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 = "md5", 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 MD5 and HMAC-MD5"); 39 + MODULE_LICENSE("GPL");