1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * CRC vpmsum tester
4  * Copyright 2017 Daniel Axtens, IBM Corporation.
5  */
6 
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>
17 
18 static unsigned long iterations = 10000;
19 
20 #define MAX_CRC_LENGTH 65535
21 
22 
23 static int __init crc_test_init(void)
24 {
25 	u16 crc16 = 0, verify16 = 0;
26 	__le32 verify32le = 0;
27 	unsigned char *data;
28 	u32 verify32 = 0;
29 	unsigned long i;
30 	__le32 crc32;
31 	int ret;
32 
33 	struct crypto_shash *crct10dif_tfm;
34 	struct crypto_shash *crc32c_tfm;
35 
36 	if (!cpu_has_feature(CPU_FTR_ARCH_207S))
37 		return -ENODEV;
38 
39 	data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL);
40 	if (!data)
41 		return -ENOMEM;
42 
43 	crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
44 
45 	if (IS_ERR(crct10dif_tfm)) {
46 		pr_err("Error allocating crc-t10dif\n");
47 		goto free_buf;
48 	}
49 
50 	crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
51 
52 	if (IS_ERR(crc32c_tfm)) {
53 		pr_err("Error allocating crc32c\n");
54 		goto free_16;
55 	}
56 
57 	do {
58 		SHASH_DESC_ON_STACK(crct10dif_shash, crct10dif_tfm);
59 		SHASH_DESC_ON_STACK(crc32c_shash, crc32c_tfm);
60 
61 		crct10dif_shash->tfm = crct10dif_tfm;
62 		ret = crypto_shash_init(crct10dif_shash);
63 
64 		if (ret) {
65 			pr_err("Error initing crc-t10dif\n");
66 			goto free_32;
67 		}
68 
69 
70 		crc32c_shash->tfm = crc32c_tfm;
71 		ret = crypto_shash_init(crc32c_shash);
72 
73 		if (ret) {
74 			pr_err("Error initing crc32c\n");
75 			goto free_32;
76 		}
77 
78 		pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations);
79 		for (i=0; i<iterations; i++) {
80 			size_t offset = prandom_u32_max(16);
81 			size_t len = prandom_u32_max(MAX_CRC_LENGTH);
82 
83 			if (len <= offset)
84 				continue;
85 			prandom_bytes(data, len);
86 			len -= offset;
87 
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);
91 
92 
93 			if (crc16 != verify16) {
94 				pr_err("FAILURE in CRC16: got 0x%04x expected 0x%04x (len %lu)\n",
95 				       crc16, verify16, len);
96 				break;
97 			}
98 
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);
106 				break;
107 			}
108 		cond_resched();
109 		}
110 		pr_info("crc-vpmsum_test done, completed %lu iterations\n", i);
111 	} while (0);
112 
113 free_32:
114 	crypto_free_shash(crc32c_tfm);
115 
116 free_16:
117 	crypto_free_shash(crct10dif_tfm);
118 
119 free_buf:
120 	kfree(data);
121 
122 	return 0;
123 }
124 
125 static void __exit crc_test_exit(void) {}
126 
127 module_init(crc_test_init);
128 module_exit(crc_test_exit);
129 module_param(iterations, long, 0400);
130 
131 MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
132 MODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester");
133 MODULE_LICENSE("GPL");
134