crc-vpmsum_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CRC vpmsum tester
  4. * Copyright 2017 Daniel Axtens, IBM Corporation.
  5. */
  6. #include <linux/crc-t10dif.h>
  7. #include <linux/crc32.h>
  8. #include <crypto/internal/hash.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/random.h>
  12. #include <linux/string.h>
  13. #include <linux/kernel.h>
  14. #include <linux/cpufeature.h>
  15. #include <asm/switch_to.h>
  16. static unsigned long iterations = 10000;
  17. #define MAX_CRC_LENGTH 65535
  18. static int __init crc_test_init(void)
  19. {
  20. u16 crc16 = 0, verify16 = 0;
  21. __le32 verify32le = 0;
  22. unsigned char *data;
  23. u32 verify32 = 0;
  24. unsigned long i;
  25. __le32 crc32;
  26. int ret;
  27. struct crypto_shash *crct10dif_tfm;
  28. struct crypto_shash *crc32c_tfm;
  29. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  30. return -ENODEV;
  31. data = kmalloc(MAX_CRC_LENGTH, GFP_KERNEL);
  32. if (!data)
  33. return -ENOMEM;
  34. crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
  35. if (IS_ERR(crct10dif_tfm)) {
  36. pr_err("Error allocating crc-t10dif\n");
  37. goto free_buf;
  38. }
  39. crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
  40. if (IS_ERR(crc32c_tfm)) {
  41. pr_err("Error allocating crc32c\n");
  42. goto free_16;
  43. }
  44. do {
  45. SHASH_DESC_ON_STACK(crct10dif_shash, crct10dif_tfm);
  46. SHASH_DESC_ON_STACK(crc32c_shash, crc32c_tfm);
  47. crct10dif_shash->tfm = crct10dif_tfm;
  48. ret = crypto_shash_init(crct10dif_shash);
  49. if (ret) {
  50. pr_err("Error initing crc-t10dif\n");
  51. goto free_32;
  52. }
  53. crc32c_shash->tfm = crc32c_tfm;
  54. ret = crypto_shash_init(crc32c_shash);
  55. if (ret) {
  56. pr_err("Error initing crc32c\n");
  57. goto free_32;
  58. }
  59. pr_info("crc-vpmsum_test begins, %lu iterations\n", iterations);
  60. for (i=0; i<iterations; i++) {
  61. size_t offset = prandom_u32_max(16);
  62. size_t len = prandom_u32_max(MAX_CRC_LENGTH);
  63. if (len <= offset)
  64. continue;
  65. get_random_bytes(data, len);
  66. len -= offset;
  67. crypto_shash_update(crct10dif_shash, data+offset, len);
  68. crypto_shash_final(crct10dif_shash, (u8 *)(&crc16));
  69. verify16 = crc_t10dif_generic(verify16, data+offset, len);
  70. if (crc16 != verify16) {
  71. pr_err("FAILURE in CRC16: got 0x%04x expected 0x%04x (len %lu)\n",
  72. crc16, verify16, len);
  73. break;
  74. }
  75. crypto_shash_update(crc32c_shash, data+offset, len);
  76. crypto_shash_final(crc32c_shash, (u8 *)(&crc32));
  77. verify32 = le32_to_cpu(verify32le);
  78. verify32le = ~cpu_to_le32(__crc32c_le(~verify32, data+offset, len));
  79. if (crc32 != verify32le) {
  80. pr_err("FAILURE in CRC32: got 0x%08x expected 0x%08x (len %lu)\n",
  81. crc32, verify32, len);
  82. break;
  83. }
  84. cond_resched();
  85. }
  86. pr_info("crc-vpmsum_test done, completed %lu iterations\n", i);
  87. } while (0);
  88. free_32:
  89. crypto_free_shash(crc32c_tfm);
  90. free_16:
  91. crypto_free_shash(crct10dif_tfm);
  92. free_buf:
  93. kfree(data);
  94. return 0;
  95. }
  96. static void __exit crc_test_exit(void) {}
  97. module_init(crc_test_init);
  98. module_exit(crc_test_exit);
  99. module_param(iterations, long, 0400);
  100. MODULE_AUTHOR("Daniel Axtens <[email protected]>");
  101. MODULE_DESCRIPTION("Vector polynomial multiply-sum CRC tester");
  102. MODULE_LICENSE("GPL");