siphash.rst 6.9 KB

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