siphash.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. SipHash reference C implementation
  3. Copyright (c) 2012-2016 Jean-Philippe Aumasson
  4. <jeanphilippe.aumasson@gmail.com>
  5. Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>
  6. To the extent possible under law, the author(s) have dedicated all copyright
  7. and related and neighboring rights to this software to the public domain
  8. worldwide. This software is distributed without any warranty.
  9. You should have received a copy of the CC0 Public Domain Dedication along
  10. with
  11. this software. If not, see
  12. <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "common.h"
  18. #include "endianess.h"
  19. #include "siphash.h"
  20. /* default: SipHash-2-4 */
  21. #define cROUNDS 2
  22. #define dROUNDS 4
  23. #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
  24. #define SIPROUND \
  25. do { \
  26. v0 += v1; \
  27. v1 = ROTL(v1, 13); \
  28. v1 ^= v0; \
  29. v0 = ROTL(v0, 32); \
  30. v2 += v3; \
  31. v3 = ROTL(v3, 16); \
  32. v3 ^= v2; \
  33. v0 += v3; \
  34. v3 = ROTL(v3, 21); \
  35. v3 ^= v0; \
  36. v2 += v1; \
  37. v1 = ROTL(v1, 17); \
  38. v1 ^= v2; \
  39. v2 = ROTL(v2, 32); \
  40. } while (0)
  41. int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k,
  42. uint8_t *out, const size_t outlen) {
  43. uint64_t v0 = 0x736f6d6570736575ULL;
  44. uint64_t v1 = 0x646f72616e646f6dULL;
  45. uint64_t v2 = 0x6c7967656e657261ULL;
  46. uint64_t v3 = 0x7465646279746573ULL;
  47. uint64_t k0 = LOAD_U64_LITTLE(k);
  48. uint64_t k1 = LOAD_U64_LITTLE(k + 8);
  49. uint64_t m;
  50. const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
  51. const int left = inlen & 7;
  52. uint64_t b = ((uint64_t)inlen) << 56;
  53. assert((outlen == 8) || (outlen == 16));
  54. v3 ^= k1;
  55. v2 ^= k0;
  56. v1 ^= k1;
  57. v0 ^= k0;
  58. if (outlen == 16)
  59. v1 ^= 0xee;
  60. for (; in != end; in += 8) {
  61. m = LOAD_U64_LITTLE(in);
  62. v3 ^= m;
  63. SIPROUND;
  64. SIPROUND;
  65. v0 ^= m;
  66. }
  67. switch (left) {
  68. case 7:
  69. b |= ((uint64_t)in[6]) << 48;
  70. case 6:
  71. b |= ((uint64_t)in[5]) << 40;
  72. case 5:
  73. b |= ((uint64_t)in[4]) << 32;
  74. case 4:
  75. b |= ((uint64_t)in[3]) << 24;
  76. case 3:
  77. b |= ((uint64_t)in[2]) << 16;
  78. case 2:
  79. b |= ((uint64_t)in[1]) << 8;
  80. case 1:
  81. b |= ((uint64_t)in[0]);
  82. break;
  83. case 0:
  84. break;
  85. }
  86. v3 ^= b;
  87. SIPROUND;
  88. SIPROUND;
  89. v0 ^= b;
  90. v2 ^= outlen == 16 ? 0xEE : 0xFF;
  91. SIPROUND;
  92. SIPROUND;
  93. SIPROUND;
  94. SIPROUND;
  95. b = v0 ^ v1 ^ v2 ^ v3;
  96. STORE_U64_LITTLE(out, b);
  97. if (outlen == 16) {
  98. v1 ^= 0xdd;
  99. SIPROUND;
  100. SIPROUND;
  101. SIPROUND;
  102. SIPROUND;
  103. b = v0 ^ v1 ^ v2 ^ v3;
  104. STORE_U64_LITTLE(out + 8, b);
  105. }
  106. return 0;
  107. }