siphash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/siphash.h>
  12. #include <asm/unaligned.h>
  13. #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
  14. #include <linux/dcache.h>
  15. #include <asm/word-at-a-time.h>
  16. #endif
  17. #define SIPROUND \
  18. do { \
  19. v0 += v1; v1 = rol64(v1, 13); v1 ^= v0; v0 = rol64(v0, 32); \
  20. v2 += v3; v3 = rol64(v3, 16); v3 ^= v2; \
  21. v0 += v3; v3 = rol64(v3, 21); v3 ^= v0; \
  22. v2 += v1; v1 = rol64(v1, 17); v1 ^= v2; v2 = rol64(v2, 32); \
  23. } while (0)
  24. #define PREAMBLE(len) \
  25. u64 v0 = 0x736f6d6570736575ULL; \
  26. u64 v1 = 0x646f72616e646f6dULL; \
  27. u64 v2 = 0x6c7967656e657261ULL; \
  28. u64 v3 = 0x7465646279746573ULL; \
  29. u64 b = ((u64)(len)) << 56; \
  30. v3 ^= key->key[1]; \
  31. v2 ^= key->key[0]; \
  32. v1 ^= key->key[1]; \
  33. v0 ^= key->key[0];
  34. #define POSTAMBLE \
  35. v3 ^= b; \
  36. SIPROUND; \
  37. SIPROUND; \
  38. v0 ^= b; \
  39. v2 ^= 0xff; \
  40. SIPROUND; \
  41. SIPROUND; \
  42. SIPROUND; \
  43. SIPROUND; \
  44. return (v0 ^ v1) ^ (v2 ^ v3);
  45. u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key)
  46. {
  47. const u8 *end = data + len - (len % sizeof(u64));
  48. const u8 left = len & (sizeof(u64) - 1);
  49. u64 m;
  50. PREAMBLE(len)
  51. for (; data != end; data += sizeof(u64)) {
  52. m = le64_to_cpup(data);
  53. v3 ^= m;
  54. SIPROUND;
  55. SIPROUND;
  56. v0 ^= m;
  57. }
  58. #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
  59. if (left)
  60. b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
  61. bytemask_from_count(left)));
  62. #else
  63. switch (left) {
  64. case 7: b |= ((u64)end[6]) << 48;
  65. case 6: b |= ((u64)end[5]) << 40;
  66. case 5: b |= ((u64)end[4]) << 32;
  67. case 4: b |= le32_to_cpup(data); break;
  68. case 3: b |= ((u64)end[2]) << 16;
  69. case 2: b |= le16_to_cpup(data); break;
  70. case 1: b |= end[0];
  71. }
  72. #endif
  73. POSTAMBLE
  74. }
  75. EXPORT_SYMBOL(__siphash_aligned);
  76. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  77. u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key)
  78. {
  79. const u8 *end = data + len - (len % sizeof(u64));
  80. const u8 left = len & (sizeof(u64) - 1);
  81. u64 m;
  82. PREAMBLE(len)
  83. for (; data != end; data += sizeof(u64)) {
  84. m = get_unaligned_le64(data);
  85. v3 ^= m;
  86. SIPROUND;
  87. SIPROUND;
  88. v0 ^= m;
  89. }
  90. #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
  91. if (left)
  92. b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
  93. bytemask_from_count(left)));
  94. #else
  95. switch (left) {
  96. case 7: b |= ((u64)end[6]) << 48;
  97. case 6: b |= ((u64)end[5]) << 40;
  98. case 5: b |= ((u64)end[4]) << 32;
  99. case 4: b |= get_unaligned_le32(end); break;
  100. case 3: b |= ((u64)end[2]) << 16;
  101. case 2: b |= get_unaligned_le16(end); break;
  102. case 1: b |= end[0];
  103. }
  104. #endif
  105. POSTAMBLE
  106. }
  107. EXPORT_SYMBOL(__siphash_unaligned);
  108. #endif
  109. /**
  110. * siphash_1u64 - compute 64-bit siphash PRF value of a u64
  111. * @first: first u64
  112. * @key: the siphash key
  113. */
  114. u64 siphash_1u64(const u64 first, const siphash_key_t *key)
  115. {
  116. PREAMBLE(8)
  117. v3 ^= first;
  118. SIPROUND;
  119. SIPROUND;
  120. v0 ^= first;
  121. POSTAMBLE
  122. }
  123. EXPORT_SYMBOL(siphash_1u64);
  124. /**
  125. * siphash_2u64 - compute 64-bit siphash PRF value of 2 u64
  126. * @first: first u64
  127. * @second: second u64
  128. * @key: the siphash key
  129. */
  130. u64 siphash_2u64(const u64 first, const u64 second, const siphash_key_t *key)
  131. {
  132. PREAMBLE(16)
  133. v3 ^= first;
  134. SIPROUND;
  135. SIPROUND;
  136. v0 ^= first;
  137. v3 ^= second;
  138. SIPROUND;
  139. SIPROUND;
  140. v0 ^= second;
  141. POSTAMBLE
  142. }
  143. EXPORT_SYMBOL(siphash_2u64);
  144. /**
  145. * siphash_3u64 - compute 64-bit siphash PRF value of 3 u64
  146. * @first: first u64
  147. * @second: second u64
  148. * @third: third u64
  149. * @key: the siphash key
  150. */
  151. u64 siphash_3u64(const u64 first, const u64 second, const u64 third,
  152. const siphash_key_t *key)
  153. {
  154. PREAMBLE(24)
  155. v3 ^= first;
  156. SIPROUND;
  157. SIPROUND;
  158. v0 ^= first;
  159. v3 ^= second;
  160. SIPROUND;
  161. SIPROUND;
  162. v0 ^= second;
  163. v3 ^= third;
  164. SIPROUND;
  165. SIPROUND;
  166. v0 ^= third;
  167. POSTAMBLE
  168. }
  169. EXPORT_SYMBOL(siphash_3u64);
  170. /**
  171. * siphash_4u64 - compute 64-bit siphash PRF value of 4 u64
  172. * @first: first u64
  173. * @second: second u64
  174. * @third: third u64
  175. * @forth: forth u64
  176. * @key: the siphash key
  177. */
  178. u64 siphash_4u64(const u64 first, const u64 second, const u64 third,
  179. const u64 forth, const siphash_key_t *key)
  180. {
  181. PREAMBLE(32)
  182. v3 ^= first;
  183. SIPROUND;
  184. SIPROUND;
  185. v0 ^= first;
  186. v3 ^= second;
  187. SIPROUND;
  188. SIPROUND;
  189. v0 ^= second;
  190. v3 ^= third;
  191. SIPROUND;
  192. SIPROUND;
  193. v0 ^= third;
  194. v3 ^= forth;
  195. SIPROUND;
  196. SIPROUND;
  197. v0 ^= forth;
  198. POSTAMBLE
  199. }
  200. EXPORT_SYMBOL(siphash_4u64);
  201. u64 siphash_1u32(const u32 first, const siphash_key_t *key)
  202. {
  203. PREAMBLE(4)
  204. b |= first;
  205. POSTAMBLE
  206. }
  207. EXPORT_SYMBOL(siphash_1u32);
  208. u64 siphash_3u32(const u32 first, const u32 second, const u32 third,
  209. const siphash_key_t *key)
  210. {
  211. u64 combined = (u64)second << 32 | first;
  212. PREAMBLE(12)
  213. v3 ^= combined;
  214. SIPROUND;
  215. SIPROUND;
  216. v0 ^= combined;
  217. b |= third;
  218. POSTAMBLE
  219. }
  220. EXPORT_SYMBOL(siphash_3u32);