csiphash.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* <MIT License>
  2. Copyright (c) 2013-2014 Marek Majkowski <marek@popcount.org>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. </MIT License>
  19. Original location:
  20. https://github.com/majek/csiphash/
  21. Solution inspired by code from:
  22. Samuel Neves (supercop/crypto_auth/siphash24/little)
  23. djb (supercop/crypto_auth/siphash24/little2)
  24. Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
  25. */
  26. #include "lib/cc/torint.h"
  27. #include "lib/log/util_bug.h"
  28. #include "siphash.h"
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "byteorder.h"
  32. #define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
  33. #define HALF_ROUND(a,b,c,d,s,t) \
  34. a += b; c += d; \
  35. b = ROTATE(b, s) ^ a; \
  36. d = ROTATE(d, t) ^ c; \
  37. a = ROTATE(a, 32);
  38. #define DOUBLE_ROUND(v0,v1,v2,v3) \
  39. HALF_ROUND(v0,v1,v2,v3,13,16); \
  40. HALF_ROUND(v2,v1,v0,v3,17,21); \
  41. HALF_ROUND(v0,v1,v2,v3,13,16); \
  42. HALF_ROUND(v2,v1,v0,v3,17,21);
  43. #if 0
  44. /* This does not seem to save very much runtime in the fast case, and it's
  45. * potentially a big loss in the slow case where we're misaligned and we cross
  46. * a cache line. */
  47. #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  48. defined(__x86_64) || defined(__x86_64__) || \
  49. defined(_M_AMD64) || defined(_M_X64) || defined(__INTEL__))
  50. # define UNALIGNED_OK 1
  51. #endif
  52. #endif
  53. uint64_t siphash24(const void *src, unsigned long src_sz, const struct sipkey *key) {
  54. const uint8_t *m = src;
  55. uint64_t k0 = key->k0;
  56. uint64_t k1 = key->k1;
  57. uint64_t last7 = (uint64_t)(src_sz & 0xff) << 56;
  58. size_t i, blocks;
  59. uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
  60. uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
  61. uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
  62. uint64_t v3 = k1 ^ 0x7465646279746573ULL;
  63. for (i = 0, blocks = (src_sz & ~7); i < blocks; i+= 8) {
  64. #ifdef UNALIGNED_OK
  65. uint64_t mi = _le64toh(*(m + i));
  66. #else
  67. uint64_t mi;
  68. memcpy(&mi, m + i, 8);
  69. mi = _le64toh(mi);
  70. #endif
  71. v3 ^= mi;
  72. DOUBLE_ROUND(v0,v1,v2,v3);
  73. v0 ^= mi;
  74. }
  75. switch (src_sz - blocks) {
  76. case 7: last7 |= (uint64_t)m[i + 6] << 48; /* Falls through. */
  77. case 6: last7 |= (uint64_t)m[i + 5] << 40; /* Falls through. */
  78. case 5: last7 |= (uint64_t)m[i + 4] << 32; /* Falls through. */
  79. case 4: last7 |= (uint64_t)m[i + 3] << 24; /* Falls through. */
  80. case 3: last7 |= (uint64_t)m[i + 2] << 16; /* Falls through. */
  81. case 2: last7 |= (uint64_t)m[i + 1] << 8; /* Falls through. */
  82. case 1: last7 |= (uint64_t)m[i + 0] ; /* Falls through. */
  83. case 0:
  84. default:;
  85. }
  86. v3 ^= last7;
  87. DOUBLE_ROUND(v0,v1,v2,v3);
  88. v0 ^= last7;
  89. v2 ^= 0xff;
  90. DOUBLE_ROUND(v0,v1,v2,v3);
  91. DOUBLE_ROUND(v0,v1,v2,v3);
  92. return v0 ^ v1 ^ v2 ^ v3;
  93. }
  94. static int the_siphash_key_is_set = 0;
  95. static struct sipkey the_siphash_key;
  96. uint64_t siphash24g(const void *src, unsigned long src_sz) {
  97. tor_assert(the_siphash_key_is_set);
  98. return siphash24(src, src_sz, &the_siphash_key);
  99. }
  100. void siphash_set_global_key(const struct sipkey *key)
  101. {
  102. tor_assert(! the_siphash_key_is_set);
  103. the_siphash_key.k0 = key->k0;
  104. the_siphash_key.k1 = key->k1;
  105. the_siphash_key_is_set = 1;
  106. }
  107. void siphash_unset_global_key(void)
  108. {
  109. the_siphash_key_is_set = 0;
  110. memset(&the_siphash_key, 0, sizeof(the_siphash_key));
  111. }