siphash.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef _LINUX_SIPHASH_H
  11. #define _LINUX_SIPHASH_H
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #define SIPHASH_ALIGNMENT __alignof__(u64)
  15. typedef struct {
  16. u64 key[2];
  17. } siphash_key_t;
  18. static inline bool siphash_key_is_zero(const siphash_key_t *key)
  19. {
  20. return !(key->key[0] | key->key[1]);
  21. }
  22. u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key);
  23. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  24. u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key);
  25. #endif
  26. u64 siphash_1u64(const u64 a, const siphash_key_t *key);
  27. u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
  28. u64 siphash_3u64(const u64 a, const u64 b, const u64 c,
  29. const siphash_key_t *key);
  30. u64 siphash_4u64(const u64 a, const u64 b, const u64 c, const u64 d,
  31. const siphash_key_t *key);
  32. u64 siphash_1u32(const u32 a, const siphash_key_t *key);
  33. u64 siphash_3u32(const u32 a, const u32 b, const u32 c,
  34. const siphash_key_t *key);
  35. static inline u64 siphash_2u32(const u32 a, const u32 b,
  36. const siphash_key_t *key)
  37. {
  38. return siphash_1u64((u64)b << 32 | a, key);
  39. }
  40. static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c,
  41. const u32 d, const siphash_key_t *key)
  42. {
  43. return siphash_2u64((u64)b << 32 | a, (u64)d << 32 | c, key);
  44. }
  45. static inline u64 ___siphash_aligned(const __le64 *data, size_t len,
  46. const siphash_key_t *key)
  47. {
  48. if (__builtin_constant_p(len) && len == 4)
  49. return siphash_1u32(le32_to_cpup((const __le32 *)data), key);
  50. if (__builtin_constant_p(len) && len == 8)
  51. return siphash_1u64(le64_to_cpu(data[0]), key);
  52. if (__builtin_constant_p(len) && len == 16)
  53. return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]),
  54. key);
  55. if (__builtin_constant_p(len) && len == 24)
  56. return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]),
  57. le64_to_cpu(data[2]), key);
  58. if (__builtin_constant_p(len) && len == 32)
  59. return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]),
  60. le64_to_cpu(data[2]), le64_to_cpu(data[3]),
  61. key);
  62. return __siphash_aligned(data, len, key);
  63. }
  64. /**
  65. * siphash - compute 64-bit siphash PRF value
  66. * @data: buffer to hash
  67. * @size: size of @data
  68. * @key: the siphash key
  69. */
  70. static inline u64 siphash(const void *data, size_t len,
  71. const siphash_key_t *key)
  72. {
  73. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  74. if (!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT))
  75. return __siphash_unaligned(data, len, key);
  76. #endif
  77. return ___siphash_aligned(data, len, key);
  78. }
  79. #endif /* _LINUX_SIPHASH_H */