siphash.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include "siphash-private.h"
  26. /*
  27. * Based on public domain SipHash reference C implementation
  28. *
  29. * Written in 2012 by
  30. * Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
  31. * Daniel J. Bernstein <djb@cr.yp.to>
  32. *
  33. */
  34. #define ROTL(x,b) (uint64_t) (((x) << (b)) | ((x) >> (64 - (b))))
  35. #define U32TO8_LE(p, v) \
  36. (p)[0] = (uint8_t) ((v)); \
  37. (p)[1] = (uint8_t) ((v) >> 8); \
  38. (p)[2] = (uint8_t) ((v) >> 16); \
  39. (p)[3] = (uint8_t) ((v) >> 24);
  40. #define U64TO8_LE(p, v) \
  41. U32TO8_LE((p), (uint32_t) ((v))); \
  42. U32TO8_LE((p) + 4, (uint32_t) ((v) >> 32));
  43. #define U8TO64_LE(p) \
  44. (((uint64_t) ((p)[0])) | \
  45. ((uint64_t) ((p)[1]) << 8) | \
  46. ((uint64_t) ((p)[2]) << 16) | \
  47. ((uint64_t) ((p)[3]) << 24) | \
  48. ((uint64_t) ((p)[4]) << 32) | \
  49. ((uint64_t) ((p)[5]) << 40) | \
  50. ((uint64_t) ((p)[6]) << 48) | \
  51. ((uint64_t) ((p)[7]) << 56))
  52. #define SIPROUND \
  53. do { \
  54. v0 += v1; v1=ROTL(v1, 13); \
  55. v1 ^= v0; v0=ROTL(v0, 32); \
  56. v2 += v3; v3=ROTL(v3, 16); \
  57. v3 ^= v2; \
  58. v0 += v3; v3=ROTL(v3, 21); \
  59. v3 ^= v0; \
  60. v2 += v1; v1=ROTL(v1, 17); \
  61. v1 ^= v2; v2=ROTL(v2, 32); \
  62. } while(0)
  63. void _siphash24(uint8_t out[8], const uint8_t *in, size_t inlen,
  64. const uint8_t k[16])
  65. {
  66. /* "somepseudorandomlygeneratedbytes" */
  67. uint64_t v0 = 0x736f6d6570736575ULL;
  68. uint64_t v1 = 0x646f72616e646f6dULL;
  69. uint64_t v2 = 0x6c7967656e657261ULL;
  70. uint64_t v3 = 0x7465646279746573ULL;
  71. uint64_t b;
  72. uint64_t k0 = U8TO64_LE(k);
  73. uint64_t k1 = U8TO64_LE(k + 8);
  74. uint64_t m;
  75. const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
  76. const int left = inlen & 7;
  77. b = ((uint64_t) inlen) << 56;
  78. v3 ^= k1;
  79. v2 ^= k0;
  80. v1 ^= k1;
  81. v0 ^= k0;
  82. for (; in != end; in += 8) {
  83. m = U8TO64_LE(in);
  84. v3 ^= m;
  85. SIPROUND;
  86. SIPROUND;
  87. v0 ^= m;
  88. }
  89. switch (left) {
  90. case 7:
  91. b |= ((uint64_t) in[6]) << 48;
  92. /* fall through */
  93. case 6:
  94. b |= ((uint64_t) in[5]) << 40;
  95. /* fall through */
  96. case 5:
  97. b |= ((uint64_t) in[4]) << 32;
  98. /* fall through */
  99. case 4:
  100. b |= ((uint64_t) in[3]) << 24;
  101. /* fall through */
  102. case 3:
  103. b |= ((uint64_t) in[2]) << 16;
  104. /* fall through */
  105. case 2:
  106. b |= ((uint64_t) in[1]) << 8;
  107. /* fall through */
  108. case 1:
  109. b |= ((uint64_t) in[0]);
  110. break;
  111. case 0:
  112. break;
  113. }
  114. v3 ^= b;
  115. SIPROUND;
  116. SIPROUND;
  117. v0 ^= b;
  118. v2 ^= 0xff;
  119. SIPROUND;
  120. SIPROUND;
  121. SIPROUND;
  122. SIPROUND;
  123. b = v0 ^ v1 ^ v2 ^ v3;
  124. U64TO8_LE(out, b)
  125. }