Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * CRC vpmsum tester
0004  * Copyright 2017 Daniel Axtens, IBM Corporation.
0005  */
0006 
0007 #include <linux/crc-t10dif.h>
0008 #include <linux/crc32.h>
0009 #include <crypto/internal/hash.h>
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/random.h>
0013 #include <linux/string.h>
0014 #include <linux/kernel.h>
0015 #include <linux/cpufeature.h>
0016 #include <asm/switch_to.h>
0017 
0018 static unsigned long iterations = 10000;
0019 
0020 #define MAX_CRC_LENGTH 65535
0021 
0022 
0023 static int __init crc_test_init(void)
0024 {
0025     u16 crc16 = 0, verify16 = 0;
0026     __le32 verify32le = 0;
0027     unsigned char *data;
0028     u32 verify32 = 0;
0029     unsigned long i;
0030     __le32 crc32;
0031     int ret;
0032 
0033     struct crypto_shash *crct10dif_tfm;
0034     struct crypto_shash *crc32c_tfm;
0035 
0036     if (!cpu_has_feature(CPU_FTR_ARCH_207S))
0037         return -ENODEV;
0038 
0039     data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL);
0040     if (!data)
0041         return -ENOMEM;
0042 
0043     crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
0044 
0045     if (IS_ERR(crct10dif_tfm)) {
0046         pr_err("Error allocating crc-t10dif\n");
0047         goto free_buf;
0048     }
0049 
0050     crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
0051 
0052     if (IS_ERR(crc32c_tfm)) {
0053         pr_err("Error allocating crc32c\n");
0054         goto free_16;
0055     }
0056 
0057     do {
0058         SHASH_DESC_ON_STACK(crct10dif_shash, crct10dif_tfm);
0059         SHASH_DESC_ON_STACK(crc32c_shash, crc32c_tfm);
0060 
0061         crct10dif_shash->tfm = crct10dif_tfm;
0062         ret = crypto_shash_init(crct10dif_shash);
0063 
0064         if (ret) {
0065             pr_err("Error initing crc-t10dif\n");
0066             goto free_32;
0067         }
0068 
0069 
0070         crc32c_shash->tfm = crc32c_tfm;
0071         ret = crypto_shash_init(crc32c_shash);
0072 
0073         if (ret) {
0074             pr_err("Error initing crc32c\n");
0075             goto free_32;
0076         }
0077 
0078         pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations);
0079         for (i=0; i<iterations; i++) {
0080             size_t offset = prandom_u32_max(16);
0081             size_t len = prandom_u32_max(MAX_CRC_LENGTH);
0082 
0083             if (len <= offset)
0084                 continue;
0085             prandom_bytes(data, len);
0086             len -= offset;
0087 
0088             crypto_shash_update(crct10dif_shash, data+offset, len);
0089             crypto_shash_final(crct10dif_shash, (u8 *)(&crc16));
0090             verify16 = crc_t10dif_generic(verify16, data+offset, len);
0091 
0092 
0093             if (crc16 != verify16) {
0094                 pr_err("FAILURE in CRC16: got 0x%04x expected 0x%04x (len %lu)\n",
0095                        crc16, verify16, len);
0096                 break;
0097             }
0098 
0099             crypto_shash_update(crc32c_shash, data+offset, len);
0100             crypto_shash_final(crc32c_shash, (u8 *)(&crc32));
0101             verify32 = le32_to_cpu(verify32le);
0102                 verify32le = ~cpu_to_le32(__crc32c_le(~verify32, data+offset, len));
0103             if (crc32 != verify32le) {
0104                 pr_err("FAILURE in CRC32: got 0x%08x expected 0x%08x (len %lu)\n",
0105                        crc32, verify32, len);
0106                 break;
0107             }
0108         cond_resched();
0109         }
0110         pr_info("crc-vpmsum_test done, completed %lu iterations\n", i);
0111     } while (0);
0112 
0113 free_32:
0114     crypto_free_shash(crc32c_tfm);
0115 
0116 free_16:
0117     crypto_free_shash(crct10dif_tfm);
0118 
0119 free_buf:
0120     kfree(data);
0121 
0122     return 0;
0123 }
0124 
0125 static void __exit crc_test_exit(void) {}
0126 
0127 module_init(crc_test_init);
0128 module_exit(crc_test_exit);
0129 module_param(iterations, long, 0400);
0130 
0131 MODULE_AUTHOR("Daniel Axtens <dja@axtens.net>");
0132 MODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester");
0133 MODULE_LICENSE("GPL");