crc-vpmsum_test.c 3.2 KB

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