Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / powerpc / crypto / crc-vpmsum_test.c
blobc61a874a3a5c6b0662296f3d4bf78b6cf0b0d139
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * CRC vpmsum tester
4 * Copyright 2017 Daniel Axtens, IBM Corporation.
5 */
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;
27 unsigned char *data;
28 u32 verify32 = 0;
29 unsigned long i;
30 __le32 crc32;
31 int ret;
33 struct crypto_shash *crct10dif_tfm;
34 struct crypto_shash *crc32c_tfm;
36 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
37 return -ENODEV;
39 data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL);
40 if (!data)
41 return -ENOMEM;
43 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
45 if (IS_ERR(crct10dif_tfm)) {
46 pr_err("Error allocating crc-t10dif\n");
47 goto free_buf;
50 crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
52 if (IS_ERR(crc32c_tfm)) {
53 pr_err("Error allocating crc32c\n");
54 goto free_16;
57 do {
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);
64 if (ret) {
65 pr_err("Error initing crc-t10dif\n");
66 goto free_32;
70 crc32c_shash->tfm = crc32c_tfm;
71 ret = crypto_shash_init(crc32c_shash);
73 if (ret) {
74 pr_err("Error initing crc32c\n");
75 goto free_32;
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);
83 if (len <= offset)
84 continue;
85 get_random_bytes(data, len);
86 len -= offset;
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);
96 break;
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;
108 cond_resched();
110 pr_info("crc-vpmsum_test done, completed %lu iterations\n", i);
111 } while (0);
113 free_32:
114 crypto_free_shash(crc32c_tfm);
116 free_16:
117 crypto_free_shash(crct10dif_tfm);
119 free_buf:
120 kfree(data);
122 return 0;
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);
131 MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
132 MODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester");
133 MODULE_LICENSE("GPL");