Experimental IRC client, daemon and bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

87 lines
2.2 KiB

  1. // Code taken from https://github.com/floodyberry/siphash with some edits.
  2. //
  3. // To the extent possible under law, the author(s) have dedicated all copyright
  4. // and related and neighboring rights to this software to the public domain
  5. // worldwide. This software is distributed without any warranty.
  6. //
  7. // You should have received a copy of the CC0 Public Domain Dedication along
  8. // with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  9. #include "siphash.h"
  10. static uint64_t inline
  11. u8to64_le (const unsigned char *p)
  12. {
  13. return
  14. (uint64_t) p[0] |
  15. (uint64_t) p[1] << 8 |
  16. (uint64_t) p[2] << 16 |
  17. (uint64_t) p[3] << 24 |
  18. (uint64_t) p[4] << 32 |
  19. (uint64_t) p[5] << 40 |
  20. (uint64_t) p[6] << 48 |
  21. (uint64_t) p[7] << 56 ;
  22. }
  23. uint64_t
  24. siphash (const unsigned char key[16], const unsigned char *m, size_t len)
  25. {
  26. uint64_t v0, v1, v2, v3;
  27. uint64_t mi, k0, k1;
  28. uint64_t last7;
  29. size_t i, blocks;
  30. k0 = u8to64_le (key + 0);
  31. k1 = u8to64_le (key + 8);
  32. v0 = k0 ^ 0x736f6d6570736575ull;
  33. v1 = k1 ^ 0x646f72616e646f6dull;
  34. v2 = k0 ^ 0x6c7967656e657261ull;
  35. v3 = k1 ^ 0x7465646279746573ull;
  36. last7 = (uint64_t) (len & 0xff) << 56;
  37. #define ROTL64(a,b) (((a)<<(b))|((a)>>(64-b)))
  38. #define COMPRESS \
  39. v0 += v1; v2 += v3; \
  40. v1 = ROTL64 (v1,13); v3 = ROTL64 (v3,16); \
  41. v1 ^= v0; v3 ^= v2; \
  42. v0 = ROTL64 (v0,32); \
  43. v2 += v1; v0 += v3; \
  44. v1 = ROTL64 (v1,17); v3 = ROTL64 (v3,21); \
  45. v1 ^= v2; v3 ^= v0; \
  46. v2 = ROTL64(v2,32);
  47. for (i = 0, blocks = (len & ~7); i < blocks; i += 8) {
  48. mi = u8to64_le (m + i);
  49. v3 ^= mi;
  50. COMPRESS
  51. COMPRESS
  52. v0 ^= mi;
  53. }
  54. switch (len - blocks)
  55. {
  56. case 7: last7 |= (uint64_t) m[i + 6] << 48;
  57. case 6: last7 |= (uint64_t) m[i + 5] << 40;
  58. case 5: last7 |= (uint64_t) m[i + 4] << 32;
  59. case 4: last7 |= (uint64_t) m[i + 3] << 24;
  60. case 3: last7 |= (uint64_t) m[i + 2] << 16;
  61. case 2: last7 |= (uint64_t) m[i + 1] << 8;
  62. case 1: last7 |= (uint64_t) m[i + 0] ;
  63. default:;
  64. };
  65. v3 ^= last7;
  66. COMPRESS
  67. COMPRESS
  68. v0 ^= last7;
  69. v2 ^= 0xff;
  70. COMPRESS
  71. COMPRESS
  72. COMPRESS
  73. COMPRESS
  74. return v0 ^ v1 ^ v2 ^ v3;
  75. }