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 "siphash.h"
  28. /* for tor_assert */
  29. #include "common/util.h"
  30. /* for memcpy */
  31. #include <string.h>
  32. #include "byteorder.h"
  33. #define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
  34. #define HALF_ROUND(a,b,c,d,s,t) \
  35. a += b; c += d; \
  36. b = ROTATE(b, s) ^ a; \
  37. d = ROTATE(d, t) ^ c; \
  38. a = ROTATE(a, 32);
  39. #define DOUBLE_ROUND(v0,v1,v2,v3) \
  40. HALF_ROUND(v0,v1,v2,v3,13,16); \
  41. HALF_ROUND(v2,v1,v0,v3,17,21); \
  42. HALF_ROUND(v0,v1,v2,v3,13,16); \
  43. HALF_ROUND(v2,v1,v0,v3,17,21);
  44. #if 0
  45. /* This does not seem to save very much runtime in the fast case, and it's
  46. * potentially a big loss in the slow case where we're misaligned and we cross
  47. * a cache line. */
  48. #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  49. defined(__x86_64) || defined(__x86_64__) || \
  50. defined(_M_AMD64) || defined(_M_X64) || defined(__INTEL__))
  51. # define UNALIGNED_OK 1
  52. #endif
  53. #endif
  54. uint64_t siphash24(const void *src, unsigned long src_sz, const struct sipkey *key) {
  55. const uint8_t *m = src;
  56. uint64_t k0 = key->k0;
  57. uint64_t k1 = key->k1;
  58. uint64_t last7 = (uint64_t)(src_sz & 0xff) << 56;
  59. size_t i, blocks;
  60. uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
  61. uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
  62. uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
  63. uint64_t v3 = k1 ^ 0x7465646279746573ULL;
  64. for (i = 0, blocks = (src_sz & ~7); i < blocks; i+= 8) {
  65. #ifdef UNALIGNED_OK
  66. uint64_t mi = _le64toh(*(m + i));
  67. #else
  68. uint64_t mi;
  69. memcpy(&mi, m + i, 8);
  70. mi = _le64toh(mi);
  71. #endif
  72. v3 ^= mi;
  73. DOUBLE_ROUND(v0,v1,v2,v3);
  74. v0 ^= mi;
  75. }
  76. switch (src_sz - blocks) {
  77. case 7: last7 |= (uint64_t)m[i + 6] << 48; /* Falls through. */
  78. case 6: last7 |= (uint64_t)m[i + 5] << 40; /* Falls through. */
  79. case 5: last7 |= (uint64_t)m[i + 4] << 32; /* Falls through. */
  80. case 4: last7 |= (uint64_t)m[i + 3] << 24; /* Falls through. */
  81. case 3: last7 |= (uint64_t)m[i + 2] << 16; /* Falls through. */
  82. case 2: last7 |= (uint64_t)m[i + 1] << 8; /* Falls through. */
  83. case 1: last7 |= (uint64_t)m[i + 0] ; /* Falls through. */
  84. case 0:
  85. default:;
  86. }
  87. v3 ^= last7;
  88. DOUBLE_ROUND(v0,v1,v2,v3);
  89. v0 ^= last7;
  90. v2 ^= 0xff;
  91. DOUBLE_ROUND(v0,v1,v2,v3);
  92. DOUBLE_ROUND(v0,v1,v2,v3);
  93. return v0 ^ v1 ^ v2 ^ v3;
  94. }
  95. static int the_siphash_key_is_set = 0;
  96. static struct sipkey the_siphash_key;
  97. uint64_t siphash24g(const void *src, unsigned long src_sz) {
  98. tor_assert(the_siphash_key_is_set);
  99. return siphash24(src, src_sz, &the_siphash_key);
  100. }
  101. void siphash_set_global_key(const struct sipkey *key)
  102. {
  103. tor_assert(! the_siphash_key_is_set);
  104. the_siphash_key.k0 = key->k0;
  105. the_siphash_key.k1 = key->k1;
  106. the_siphash_key_is_set = 1;
  107. }
  108. void siphash_unset_global_key(void)
  109. {
  110. the_siphash_key_is_set = 0;
  111. memset(&the_siphash_key, 0, sizeof(the_siphash_key));
  112. }