csiphash.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "torint.h"
  27. #include "siphash.h"
  28. /* for tor_assert */
  29. #include "util.h"
  30. /* for memcpy */
  31. #include <string.h>
  32. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  33. __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  34. # define _le64toh(x) ((uint64_t)(x))
  35. #elif defined(_WIN32)
  36. /* Windows is always little endian, unless you're on xbox360
  37. http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */
  38. # define _le64toh(x) ((uint64_t)(x))
  39. #elif defined(__APPLE__)
  40. # include <libkern/OSByteOrder.h>
  41. # define _le64toh(x) OSSwapLittleToHostInt64(x)
  42. #elif defined(sun) || defined(__sun)
  43. # include <sys/byteorder.h>
  44. # define _le64toh(x) LE_64(x)
  45. #else
  46. /* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
  47. # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  48. # include <sys/endian.h>
  49. # else
  50. # include <endian.h>
  51. # endif
  52. # if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
  53. __BYTE_ORDER == __LITTLE_ENDIAN
  54. # define _le64toh(x) ((uint64_t)(x))
  55. # else
  56. # if defined(__OpenBSD__)
  57. # define _le64toh(x) letoh64(x)
  58. # else
  59. # define _le64toh(x) le64toh(x)
  60. # endif
  61. # endif
  62. #endif
  63. #define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
  64. #define HALF_ROUND(a,b,c,d,s,t) \
  65. a += b; c += d; \
  66. b = ROTATE(b, s) ^ a; \
  67. d = ROTATE(d, t) ^ c; \
  68. a = ROTATE(a, 32);
  69. #define DOUBLE_ROUND(v0,v1,v2,v3) \
  70. HALF_ROUND(v0,v1,v2,v3,13,16); \
  71. HALF_ROUND(v2,v1,v0,v3,17,21); \
  72. HALF_ROUND(v0,v1,v2,v3,13,16); \
  73. HALF_ROUND(v2,v1,v0,v3,17,21);
  74. #if 0
  75. /* This does not seem to save very much runtime in the fast case, and it's
  76. * potentially a big loss in the slow case where we're misaligned and we cross
  77. * a cache line. */
  78. #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  79. defined(__x86_64) || defined(__x86_64__) || \
  80. defined(_M_AMD64) || defined(_M_X64) || defined(__INTEL__))
  81. # define UNALIGNED_OK 1
  82. #endif
  83. #endif
  84. uint64_t siphash24(const void *src, unsigned long src_sz, const struct sipkey *key) {
  85. const uint8_t *m = src;
  86. uint64_t k0 = key->k0;
  87. uint64_t k1 = key->k1;
  88. uint64_t last7 = (uint64_t)(src_sz & 0xff) << 56;
  89. size_t i, blocks;
  90. uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
  91. uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
  92. uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
  93. uint64_t v3 = k1 ^ 0x7465646279746573ULL;
  94. for (i = 0, blocks = (src_sz & ~7); i < blocks; i+= 8) {
  95. #ifdef UNALIGNED_OK
  96. uint64_t mi = _le64toh(*(m + i));
  97. #else
  98. uint64_t mi;
  99. memcpy(&mi, m + i, 8);
  100. mi = _le64toh(mi);
  101. #endif
  102. v3 ^= mi;
  103. DOUBLE_ROUND(v0,v1,v2,v3);
  104. v0 ^= mi;
  105. }
  106. switch (src_sz - blocks) {
  107. case 7: last7 |= (uint64_t)m[i + 6] << 48;
  108. case 6: last7 |= (uint64_t)m[i + 5] << 40;
  109. case 5: last7 |= (uint64_t)m[i + 4] << 32;
  110. case 4: last7 |= (uint64_t)m[i + 3] << 24;
  111. case 3: last7 |= (uint64_t)m[i + 2] << 16;
  112. case 2: last7 |= (uint64_t)m[i + 1] << 8;
  113. case 1: last7 |= (uint64_t)m[i + 0] ;
  114. case 0:
  115. default:;
  116. }
  117. v3 ^= last7;
  118. DOUBLE_ROUND(v0,v1,v2,v3);
  119. v0 ^= last7;
  120. v2 ^= 0xff;
  121. DOUBLE_ROUND(v0,v1,v2,v3);
  122. DOUBLE_ROUND(v0,v1,v2,v3);
  123. return v0 ^ v1 ^ v2 ^ v3;
  124. }
  125. static int the_siphash_key_is_set = 0;
  126. static struct sipkey the_siphash_key;
  127. uint64_t siphash24g(const void *src, unsigned long src_sz) {
  128. tor_assert(the_siphash_key_is_set);
  129. return siphash24(src, src_sz, &the_siphash_key);
  130. }
  131. void siphash_set_global_key(const struct sipkey *key)
  132. {
  133. tor_assert(! the_siphash_key_is_set);
  134. the_siphash_key.k0 = key->k0;
  135. the_siphash_key.k1 = key->k1;
  136. the_siphash_key_is_set = 1;
  137. }