siphash.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. SipHash - a short input PRF
  2. -----------------------------------------------
  3. Written by Jason A. Donenfeld <[email protected]>
  4. SipHash is a cryptographically secure PRF -- a keyed hash function -- that
  5. performs very well for short inputs, hence the name. It was designed by
  6. cryptographers Daniel J. Bernstein and Jean-Philippe Aumasson. It is intended
  7. as a replacement for some uses of: `jhash`, `md5_transform`, `sha_transform`,
  8. and so forth.
  9. SipHash takes a secret key filled with randomly generated numbers and either
  10. an input buffer or several input integers. It spits out an integer that is
  11. indistinguishable from random. You may then use that integer as part of secure
  12. sequence numbers, secure cookies, or mask it off for use in a hash table.
  13. 1. Generating a key
  14. Keys should always be generated from a cryptographically secure source of
  15. random numbers, either using get_random_bytes or get_random_once:
  16. siphash_key_t key;
  17. get_random_bytes(&key, sizeof(key));
  18. If you're not deriving your key from here, you're doing it wrong.
  19. 2. Using the functions
  20. There are two variants of the function, one that takes a list of integers, and
  21. one that takes a buffer:
  22. u64 siphash(const void *data, size_t len, const siphash_key_t *key);
  23. And:
  24. u64 siphash_1u64(u64, const siphash_key_t *key);
  25. u64 siphash_2u64(u64, u64, const siphash_key_t *key);
  26. u64 siphash_3u64(u64, u64, u64, const siphash_key_t *key);
  27. u64 siphash_4u64(u64, u64, u64, u64, const siphash_key_t *key);
  28. u64 siphash_1u32(u32, const siphash_key_t *key);
  29. u64 siphash_2u32(u32, u32, const siphash_key_t *key);
  30. u64 siphash_3u32(u32, u32, u32, const siphash_key_t *key);
  31. u64 siphash_4u32(u32, u32, u32, u32, const siphash_key_t *key);
  32. If you pass the generic siphash function something of a constant length, it
  33. will constant fold at compile-time and automatically choose one of the
  34. optimized functions.
  35. 3. Hashtable key function usage:
  36. struct some_hashtable {
  37. DECLARE_HASHTABLE(hashtable, 8);
  38. siphash_key_t key;
  39. };
  40. void init_hashtable(struct some_hashtable *table)
  41. {
  42. get_random_bytes(&table->key, sizeof(table->key));
  43. }
  44. static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
  45. {
  46. return &table->hashtable[siphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
  47. }
  48. You may then iterate like usual over the returned hash bucket.
  49. 4. Security
  50. SipHash has a very high security margin, with its 128-bit key. So long as the
  51. key is kept secret, it is impossible for an attacker to guess the outputs of
  52. the function, even if being able to observe many outputs, since 2^128 outputs
  53. is significant.
  54. Linux implements the "2-4" variant of SipHash.
  55. 5. Struct-passing Pitfalls
  56. Often times the XuY functions will not be large enough, and instead you'll
  57. want to pass a pre-filled struct to siphash. When doing this, it's important
  58. to always ensure the struct has no padding holes. The easiest way to do this
  59. is to simply arrange the members of the struct in descending order of size,
  60. and to use offsetendof() instead of sizeof() for getting the size. For
  61. performance reasons, if possible, it's probably a good thing to align the
  62. struct to the right boundary. Here's an example:
  63. const struct {
  64. struct in6_addr saddr;
  65. u32 counter;
  66. u16 dport;
  67. } __aligned(SIPHASH_ALIGNMENT) combined = {
  68. .saddr = *(struct in6_addr *)saddr,
  69. .counter = counter,
  70. .dport = dport
  71. };
  72. u64 h = siphash(&combined, offsetofend(typeof(combined), dport), &secret);
  73. 6. Resources
  74. Read the SipHash paper if you're interested in learning more:
  75. https://131002.net/siphash/siphash.pdf
  76. ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
  77. HalfSipHash - SipHash's insecure younger cousin
  78. -----------------------------------------------
  79. Written by Jason A. Donenfeld <[email protected]>
  80. On the off-chance that SipHash is not fast enough for your needs, you might be
  81. able to justify using HalfSipHash, a terrifying but potentially useful
  82. possibility. HalfSipHash cuts SipHash's rounds down from "2-4" to "1-3" and,
  83. even scarier, uses an easily brute-forcable 64-bit key (with a 32-bit output)
  84. instead of SipHash's 128-bit key. However, this may appeal to some
  85. high-performance `jhash` users.
  86. Danger!
  87. Do not ever use HalfSipHash except for as a hashtable key function, and only
  88. then when you can be absolutely certain that the outputs will never be
  89. transmitted out of the kernel. This is only remotely useful over `jhash` as a
  90. means of mitigating hashtable flooding denial of service attacks.
  91. 1. Generating a key
  92. Keys should always be generated from a cryptographically secure source of
  93. random numbers, either using get_random_bytes or get_random_once:
  94. hsiphash_key_t key;
  95. get_random_bytes(&key, sizeof(key));
  96. If you're not deriving your key from here, you're doing it wrong.
  97. 2. Using the functions
  98. There are two variants of the function, one that takes a list of integers, and
  99. one that takes a buffer:
  100. u32 hsiphash(const void *data, size_t len, const hsiphash_key_t *key);
  101. And:
  102. u32 hsiphash_1u32(u32, const hsiphash_key_t *key);
  103. u32 hsiphash_2u32(u32, u32, const hsiphash_key_t *key);
  104. u32 hsiphash_3u32(u32, u32, u32, const hsiphash_key_t *key);
  105. u32 hsiphash_4u32(u32, u32, u32, u32, const hsiphash_key_t *key);
  106. If you pass the generic hsiphash function something of a constant length, it
  107. will constant fold at compile-time and automatically choose one of the
  108. optimized functions.
  109. 3. Hashtable key function usage:
  110. struct some_hashtable {
  111. DECLARE_HASHTABLE(hashtable, 8);
  112. hsiphash_key_t key;
  113. };
  114. void init_hashtable(struct some_hashtable *table)
  115. {
  116. get_random_bytes(&table->key, sizeof(table->key));
  117. }
  118. static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
  119. {
  120. return &table->hashtable[hsiphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
  121. }
  122. You may then iterate like usual over the returned hash bucket.
  123. 4. Performance
  124. HalfSipHash is roughly 3 times slower than JenkinsHash. For many replacements,
  125. this will not be a problem, as the hashtable lookup isn't the bottleneck. And
  126. in general, this is probably a good sacrifice to make for the security and DoS
  127. resistance of HalfSipHash.