test_siphash.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Test cases for siphash.c
  2. *
  3. * Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. *
  5. * This file is provided under a dual BSD/GPLv2 license.
  6. *
  7. * SipHash: a fast short-input PRF
  8. * https://131002.net/siphash/
  9. *
  10. * This implementation is specifically for SipHash2-4.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/siphash.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/module.h>
  18. /* Test vectors taken from official reference source available at:
  19. * https://131002.net/siphash/siphash24.c
  20. */
  21. static const siphash_key_t test_key_siphash =
  22. {{ 0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL }};
  23. static const u64 test_vectors_siphash[64] = {
  24. 0x726fdb47dd0e0e31ULL, 0x74f839c593dc67fdULL, 0x0d6c8009d9a94f5aULL,
  25. 0x85676696d7fb7e2dULL, 0xcf2794e0277187b7ULL, 0x18765564cd99a68dULL,
  26. 0xcbc9466e58fee3ceULL, 0xab0200f58b01d137ULL, 0x93f5f5799a932462ULL,
  27. 0x9e0082df0ba9e4b0ULL, 0x7a5dbbc594ddb9f3ULL, 0xf4b32f46226bada7ULL,
  28. 0x751e8fbc860ee5fbULL, 0x14ea5627c0843d90ULL, 0xf723ca908e7af2eeULL,
  29. 0xa129ca6149be45e5ULL, 0x3f2acc7f57c29bdbULL, 0x699ae9f52cbe4794ULL,
  30. 0x4bc1b3f0968dd39cULL, 0xbb6dc91da77961bdULL, 0xbed65cf21aa2ee98ULL,
  31. 0xd0f2cbb02e3b67c7ULL, 0x93536795e3a33e88ULL, 0xa80c038ccd5ccec8ULL,
  32. 0xb8ad50c6f649af94ULL, 0xbce192de8a85b8eaULL, 0x17d835b85bbb15f3ULL,
  33. 0x2f2e6163076bcfadULL, 0xde4daaaca71dc9a5ULL, 0xa6a2506687956571ULL,
  34. 0xad87a3535c49ef28ULL, 0x32d892fad841c342ULL, 0x7127512f72f27cceULL,
  35. 0xa7f32346f95978e3ULL, 0x12e0b01abb051238ULL, 0x15e034d40fa197aeULL,
  36. 0x314dffbe0815a3b4ULL, 0x027990f029623981ULL, 0xcadcd4e59ef40c4dULL,
  37. 0x9abfd8766a33735cULL, 0x0e3ea96b5304a7d0ULL, 0xad0c42d6fc585992ULL,
  38. 0x187306c89bc215a9ULL, 0xd4a60abcf3792b95ULL, 0xf935451de4f21df2ULL,
  39. 0xa9538f0419755787ULL, 0xdb9acddff56ca510ULL, 0xd06c98cd5c0975ebULL,
  40. 0xe612a3cb9ecba951ULL, 0xc766e62cfcadaf96ULL, 0xee64435a9752fe72ULL,
  41. 0xa192d576b245165aULL, 0x0a8787bf8ecb74b2ULL, 0x81b3e73d20b49b6fULL,
  42. 0x7fa8220ba3b2eceaULL, 0x245731c13ca42499ULL, 0xb78dbfaf3a8d83bdULL,
  43. 0xea1ad565322a1a0bULL, 0x60e61c23a3795013ULL, 0x6606d7e446282b93ULL,
  44. 0x6ca4ecb15c5f91e1ULL, 0x9f626da15c9625f3ULL, 0xe51b38608ef25f57ULL,
  45. 0x958a324ceb064572ULL
  46. };
  47. static int __init siphash_test_init(void)
  48. {
  49. u8 in[64] __aligned(SIPHASH_ALIGNMENT);
  50. u8 in_unaligned[65] __aligned(SIPHASH_ALIGNMENT);
  51. u8 i;
  52. int ret = 0;
  53. for (i = 0; i < 64; ++i) {
  54. in[i] = i;
  55. in_unaligned[i + 1] = i;
  56. if (siphash(in, i, &test_key_siphash) !=
  57. test_vectors_siphash[i]) {
  58. pr_info("siphash self-test aligned %u: FAIL\n", i + 1);
  59. ret = -EINVAL;
  60. }
  61. if (siphash(in_unaligned + 1, i, &test_key_siphash) !=
  62. test_vectors_siphash[i]) {
  63. pr_info("siphash self-test unaligned %u: FAIL\n", i + 1);
  64. ret = -EINVAL;
  65. }
  66. }
  67. if (siphash_1u64(0x0706050403020100ULL, &test_key_siphash) !=
  68. test_vectors_siphash[8]) {
  69. pr_info("siphash self-test 1u64: FAIL\n");
  70. ret = -EINVAL;
  71. }
  72. if (siphash_2u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
  73. &test_key_siphash) != test_vectors_siphash[16]) {
  74. pr_info("siphash self-test 2u64: FAIL\n");
  75. ret = -EINVAL;
  76. }
  77. if (siphash_3u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
  78. 0x1716151413121110ULL, &test_key_siphash) !=
  79. test_vectors_siphash[24]) {
  80. pr_info("siphash self-test 3u64: FAIL\n");
  81. ret = -EINVAL;
  82. }
  83. if (siphash_4u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL,
  84. 0x1716151413121110ULL, 0x1f1e1d1c1b1a1918ULL,
  85. &test_key_siphash) != test_vectors_siphash[32]) {
  86. pr_info("siphash self-test 4u64: FAIL\n");
  87. ret = -EINVAL;
  88. }
  89. if (siphash_1u32(0x03020100U, &test_key_siphash) !=
  90. test_vectors_siphash[4]) {
  91. pr_info("siphash self-test 1u32: FAIL\n");
  92. ret = -EINVAL;
  93. }
  94. if (siphash_2u32(0x03020100U, 0x07060504U, &test_key_siphash) !=
  95. test_vectors_siphash[8]) {
  96. pr_info("siphash self-test 2u32: FAIL\n");
  97. ret = -EINVAL;
  98. }
  99. if (siphash_3u32(0x03020100U, 0x07060504U,
  100. 0x0b0a0908U, &test_key_siphash) !=
  101. test_vectors_siphash[12]) {
  102. pr_info("siphash self-test 3u32: FAIL\n");
  103. ret = -EINVAL;
  104. }
  105. if (siphash_4u32(0x03020100U, 0x07060504U,
  106. 0x0b0a0908U, 0x0f0e0d0cU, &test_key_siphash) !=
  107. test_vectors_siphash[16]) {
  108. pr_info("siphash self-test 4u32: FAIL\n");
  109. ret = -EINVAL;
  110. }
  111. if (!ret)
  112. pr_info("self-tests: pass\n");
  113. return ret;
  114. }
  115. static void __exit siphash_test_exit(void)
  116. {
  117. }
  118. module_init(siphash_test_init);
  119. module_exit(siphash_test_exit);
  120. MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
  121. MODULE_LICENSE("Dual BSD/GPL");