siphash.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license.
  4. *
  5. * SipHash: a fast short-input PRF
  6. * https://131002.net/siphash/
  7. *
  8. * This implementation is specifically for SipHash2-4 for a secure PRF
  9. * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for
  10. * hashtables.
  11. */
  12. #ifndef _LINUX_SIPHASH_H
  13. #define _LINUX_SIPHASH_H
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #define SIPHASH_ALIGNMENT __alignof__(u64)
  17. typedef struct {
  18. u64 key[2];
  19. } siphash_key_t;
  20. static inline bool siphash_key_is_zero(const siphash_key_t *key)
  21. {
  22. return !(key->key[0] | key->key[1]);
  23. }
  24. u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key);
  25. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  26. u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key);
  27. #endif
  28. u64 siphash_1u64(const u64 a, const siphash_key_t *key);
  29. u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
  30. u64 siphash_3u64(const u64 a, const u64 b, const u64 c,
  31. const siphash_key_t *key);
  32. u64 siphash_4u64(const u64 a, const u64 b, const u64 c, const u64 d,
  33. const siphash_key_t *key);
  34. u64 siphash_1u32(const u32 a, const siphash_key_t *key);
  35. u64 siphash_3u32(const u32 a, const u32 b, const u32 c,
  36. const siphash_key_t *key);
  37. static inline u64 siphash_2u32(const u32 a, const u32 b,
  38. const siphash_key_t *key)
  39. {
  40. return siphash_1u64((u64)b << 32 | a, key);
  41. }
  42. static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c,
  43. const u32 d, const siphash_key_t *key)
  44. {
  45. return siphash_2u64((u64)b << 32 | a, (u64)d << 32 | c, key);
  46. }
  47. static inline u64 ___siphash_aligned(const __le64 *data, size_t len,
  48. const siphash_key_t *key)
  49. {
  50. if (__builtin_constant_p(len) && len == 4)
  51. return siphash_1u32(le32_to_cpup((const __le32 *)data), key);
  52. if (__builtin_constant_p(len) && len == 8)
  53. return siphash_1u64(le64_to_cpu(data[0]), key);
  54. if (__builtin_constant_p(len) && len == 16)
  55. return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]),
  56. key);
  57. if (__builtin_constant_p(len) && len == 24)
  58. return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]),
  59. le64_to_cpu(data[2]), key);
  60. if (__builtin_constant_p(len) && len == 32)
  61. return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]),
  62. le64_to_cpu(data[2]), le64_to_cpu(data[3]),
  63. key);
  64. return __siphash_aligned(data, len, key);
  65. }
  66. /**
  67. * siphash - compute 64-bit siphash PRF value
  68. * @data: buffer to hash
  69. * @size: size of @data
  70. * @key: the siphash key
  71. */
  72. static inline u64 siphash(const void *data, size_t len,
  73. const siphash_key_t *key)
  74. {
  75. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  76. if (!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT))
  77. return __siphash_unaligned(data, len, key);
  78. #endif
  79. return ___siphash_aligned(data, len, key);
  80. }
  81. #define HSIPHASH_ALIGNMENT __alignof__(unsigned long)
  82. typedef struct {
  83. unsigned long key[2];
  84. } hsiphash_key_t;
  85. u32 __hsiphash_aligned(const void *data, size_t len,
  86. const hsiphash_key_t *key);
  87. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  88. u32 __hsiphash_unaligned(const void *data, size_t len,
  89. const hsiphash_key_t *key);
  90. #endif
  91. u32 hsiphash_1u32(const u32 a, const hsiphash_key_t *key);
  92. u32 hsiphash_2u32(const u32 a, const u32 b, const hsiphash_key_t *key);
  93. u32 hsiphash_3u32(const u32 a, const u32 b, const u32 c,
  94. const hsiphash_key_t *key);
  95. u32 hsiphash_4u32(const u32 a, const u32 b, const u32 c, const u32 d,
  96. const hsiphash_key_t *key);
  97. static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len,
  98. const hsiphash_key_t *key)
  99. {
  100. if (__builtin_constant_p(len) && len == 4)
  101. return hsiphash_1u32(le32_to_cpu(data[0]), key);
  102. if (__builtin_constant_p(len) && len == 8)
  103. return hsiphash_2u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]),
  104. key);
  105. if (__builtin_constant_p(len) && len == 12)
  106. return hsiphash_3u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]),
  107. le32_to_cpu(data[2]), key);
  108. if (__builtin_constant_p(len) && len == 16)
  109. return hsiphash_4u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]),
  110. le32_to_cpu(data[2]), le32_to_cpu(data[3]),
  111. key);
  112. return __hsiphash_aligned(data, len, key);
  113. }
  114. /**
  115. * hsiphash - compute 32-bit hsiphash PRF value
  116. * @data: buffer to hash
  117. * @size: size of @data
  118. * @key: the hsiphash key
  119. */
  120. static inline u32 hsiphash(const void *data, size_t len,
  121. const hsiphash_key_t *key)
  122. {
  123. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  124. if (!IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT))
  125. return __hsiphash_unaligned(data, len, key);
  126. #endif
  127. return ___hsiphash_aligned(data, len, key);
  128. }
  129. #endif /* _LINUX_SIPHASH_H */