1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright 2017 Daniel Axtens, IBM Corporation.
7 #include <linux/crc-t10dif.h>
8 #include <linux/crc32.h>
9 #include <crypto/internal/hash.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/random.h>
13 #include <linux/string.h>
14 #include <linux/kernel.h>
15 #include <linux/cpufeature.h>
16 #include <asm/switch_to.h>
18 static unsigned long iterations = 10000;
20 #define MAX_CRC_LENGTH 65535
23 static int __init crc_test_init(void)
25 u16 crc16 = 0, verify16 = 0;
26 __le32 verify32le = 0;
33 struct crypto_shash *crct10dif_tfm;
34 struct crypto_shash *crc32c_tfm;
36 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
39 data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL);
43 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
45 if (IS_ERR(crct10dif_tfm)) {
46 pr_err("Error allocating crc-t10dif\n");
50 crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
52 if (IS_ERR(crc32c_tfm)) {
53 pr_err("Error allocating crc32c\n");
58 SHASH_DESC_ON_STACK(crct10dif_shash, crct10dif_tfm);
59 SHASH_DESC_ON_STACK(crc32c_shash, crc32c_tfm);
61 crct10dif_shash->tfm = crct10dif_tfm;
62 ret = crypto_shash_init(crct10dif_shash);
65 pr_err("Error initing crc-t10dif\n");
70 crc32c_shash->tfm = crc32c_tfm;
71 ret = crypto_shash_init(crc32c_shash);
74 pr_err("Error initing crc32c\n");
78 pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations);
79 for (i=0; i<iterations; i++) {
80 size_t offset = get_random_u32_below(16);
81 size_t len = get_random_u32_below(MAX_CRC_LENGTH);
85 get_random_bytes(data, len);
88 crypto_shash_update(crct10dif_shash, data+offset, len);
89 crypto_shash_final(crct10dif_shash, (u8 *)(&crc16));
90 verify16 = crc_t10dif_generic(verify16, data+offset, len);
93 if (crc16 != verify16) {
94 pr_err("FAILURE in CRC16: got 0x%04x expected 0x%04x (len %lu)\n",
95 crc16, verify16, len);
99 crypto_shash_update(crc32c_shash, data+offset, len);
100 crypto_shash_final(crc32c_shash, (u8 *)(&crc32));
101 verify32 = le32_to_cpu(verify32le);
102 verify32le = ~cpu_to_le32(__crc32c_le(~verify32, data+offset, len));
103 if (crc32 != verify32le) {
104 pr_err("FAILURE in CRC32: got 0x%08x expected 0x%08x (len %lu)\n",
105 crc32, verify32, len);
110 pr_info("crc-vpmsum_test done, completed %lu iterations\n", i);
114 crypto_free_shash(crc32c_tfm);
117 crypto_free_shash(crct10dif_tfm);
125 static void __exit crc_test_exit(void) {}
127 module_init(crc_test_init);
128 module_exit(crc_test_exit);
129 module_param(iterations, long, 0400);
132 MODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester");
133 MODULE_LICENSE("GPL");