siphash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Based on https://131002.net/siphash C reference implementation */
  10. /*
  11. SipHash reference C implementation
  12. Copyright (c) 2012-2016 Jean-Philippe Aumasson
  13. Copyright (c) 2012-2014 Daniel J. Bernstein
  14. To the extent possible under law, the author(s) have dedicated all copyright
  15. and related and neighboring rights to this software to the public domain
  16. worldwide. This software is distributed without any warranty.
  17. You should have received a copy of the CC0 Public Domain Dedication along
  18. with this software. If not, see
  19. <http://creativecommons.org/publicdomain/zero/1.0/>.
  20. */
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <openssl/crypto.h>
  24. #include "crypto/siphash.h"
  25. #include "siphash_local.h"
  26. /* default: SipHash-2-4 */
  27. #define SIPHASH_C_ROUNDS 2
  28. #define SIPHASH_D_ROUNDS 4
  29. #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
  30. #define U32TO8_LE(p, v) \
  31. (p)[0] = (uint8_t)((v)); \
  32. (p)[1] = (uint8_t)((v) >> 8); \
  33. (p)[2] = (uint8_t)((v) >> 16); \
  34. (p)[3] = (uint8_t)((v) >> 24);
  35. #define U64TO8_LE(p, v) \
  36. U32TO8_LE((p), (uint32_t)((v))); \
  37. U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
  38. #define U8TO64_LE(p) \
  39. (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
  40. ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
  41. ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
  42. ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
  43. #define SIPROUND \
  44. do { \
  45. v0 += v1; \
  46. v1 = ROTL(v1, 13); \
  47. v1 ^= v0; \
  48. v0 = ROTL(v0, 32); \
  49. v2 += v3; \
  50. v3 = ROTL(v3, 16); \
  51. v3 ^= v2; \
  52. v0 += v3; \
  53. v3 = ROTL(v3, 21); \
  54. v3 ^= v0; \
  55. v2 += v1; \
  56. v1 = ROTL(v1, 17); \
  57. v1 ^= v2; \
  58. v2 = ROTL(v2, 32); \
  59. } while (0)
  60. size_t SipHash_ctx_size(void)
  61. {
  62. return sizeof(SIPHASH);
  63. }
  64. size_t SipHash_hash_size(SIPHASH *ctx)
  65. {
  66. return ctx->hash_size;
  67. }
  68. static size_t siphash_adjust_hash_size(size_t hash_size)
  69. {
  70. if (hash_size == 0)
  71. hash_size = SIPHASH_MAX_DIGEST_SIZE;
  72. return hash_size;
  73. }
  74. int SipHash_set_hash_size(SIPHASH *ctx, size_t hash_size)
  75. {
  76. hash_size = siphash_adjust_hash_size(hash_size);
  77. if (hash_size != SIPHASH_MIN_DIGEST_SIZE
  78. && hash_size != SIPHASH_MAX_DIGEST_SIZE)
  79. return 0;
  80. /*
  81. * It's possible that the key was set first. If the hash size changes,
  82. * we need to adjust v1 (see SipHash_Init().
  83. */
  84. /* Start by adjusting the stored size, to make things easier */
  85. ctx->hash_size = siphash_adjust_hash_size(ctx->hash_size);
  86. /* Now, adjust ctx->v1 if the old and the new size differ */
  87. if ((size_t)ctx->hash_size != hash_size) {
  88. ctx->v1 ^= 0xee;
  89. ctx->hash_size = hash_size;
  90. }
  91. return 1;
  92. }
  93. /* hash_size = crounds = drounds = 0 means SipHash24 with 16-byte output */
  94. int SipHash_Init(SIPHASH *ctx, const unsigned char *k, int crounds, int drounds)
  95. {
  96. uint64_t k0 = U8TO64_LE(k);
  97. uint64_t k1 = U8TO64_LE(k + 8);
  98. /* If the hash size wasn't set, i.e. is zero */
  99. ctx->hash_size = siphash_adjust_hash_size(ctx->hash_size);
  100. if (drounds == 0)
  101. drounds = SIPHASH_D_ROUNDS;
  102. if (crounds == 0)
  103. crounds = SIPHASH_C_ROUNDS;
  104. ctx->crounds = crounds;
  105. ctx->drounds = drounds;
  106. ctx->len = 0;
  107. ctx->total_inlen = 0;
  108. ctx->v0 = 0x736f6d6570736575ULL ^ k0;
  109. ctx->v1 = 0x646f72616e646f6dULL ^ k1;
  110. ctx->v2 = 0x6c7967656e657261ULL ^ k0;
  111. ctx->v3 = 0x7465646279746573ULL ^ k1;
  112. if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
  113. ctx->v1 ^= 0xee;
  114. return 1;
  115. }
  116. void SipHash_Update(SIPHASH *ctx, const unsigned char *in, size_t inlen)
  117. {
  118. uint64_t m;
  119. const uint8_t *end;
  120. int left;
  121. int i;
  122. uint64_t v0 = ctx->v0;
  123. uint64_t v1 = ctx->v1;
  124. uint64_t v2 = ctx->v2;
  125. uint64_t v3 = ctx->v3;
  126. ctx->total_inlen += inlen;
  127. if (ctx->len) {
  128. /* deal with leavings */
  129. size_t available = SIPHASH_BLOCK_SIZE - ctx->len;
  130. /* not enough to fill leavings */
  131. if (inlen < available) {
  132. memcpy(&ctx->leavings[ctx->len], in, inlen);
  133. ctx->len += inlen;
  134. return;
  135. }
  136. /* copy data into leavings and reduce input */
  137. memcpy(&ctx->leavings[ctx->len], in, available);
  138. inlen -= available;
  139. in += available;
  140. /* process leavings */
  141. m = U8TO64_LE(ctx->leavings);
  142. v3 ^= m;
  143. for (i = 0; i < ctx->crounds; ++i)
  144. SIPROUND;
  145. v0 ^= m;
  146. }
  147. left = inlen & (SIPHASH_BLOCK_SIZE-1); /* gets put into leavings */
  148. end = in + inlen - left;
  149. for (; in != end; in += 8) {
  150. m = U8TO64_LE(in);
  151. v3 ^= m;
  152. for (i = 0; i < ctx->crounds; ++i)
  153. SIPROUND;
  154. v0 ^= m;
  155. }
  156. /* save leavings and other ctx */
  157. if (left)
  158. memcpy(ctx->leavings, end, left);
  159. ctx->len = left;
  160. ctx->v0 = v0;
  161. ctx->v1 = v1;
  162. ctx->v2 = v2;
  163. ctx->v3 = v3;
  164. }
  165. int SipHash_Final(SIPHASH *ctx, unsigned char *out, size_t outlen)
  166. {
  167. /* finalize hash */
  168. int i;
  169. uint64_t b = ctx->total_inlen << 56;
  170. uint64_t v0 = ctx->v0;
  171. uint64_t v1 = ctx->v1;
  172. uint64_t v2 = ctx->v2;
  173. uint64_t v3 = ctx->v3;
  174. if (outlen != (size_t)ctx->hash_size)
  175. return 0;
  176. switch (ctx->len) {
  177. case 7:
  178. b |= ((uint64_t)ctx->leavings[6]) << 48;
  179. /* fall thru */
  180. case 6:
  181. b |= ((uint64_t)ctx->leavings[5]) << 40;
  182. /* fall thru */
  183. case 5:
  184. b |= ((uint64_t)ctx->leavings[4]) << 32;
  185. /* fall thru */
  186. case 4:
  187. b |= ((uint64_t)ctx->leavings[3]) << 24;
  188. /* fall thru */
  189. case 3:
  190. b |= ((uint64_t)ctx->leavings[2]) << 16;
  191. /* fall thru */
  192. case 2:
  193. b |= ((uint64_t)ctx->leavings[1]) << 8;
  194. /* fall thru */
  195. case 1:
  196. b |= ((uint64_t)ctx->leavings[0]);
  197. case 0:
  198. break;
  199. }
  200. v3 ^= b;
  201. for (i = 0; i < ctx->crounds; ++i)
  202. SIPROUND;
  203. v0 ^= b;
  204. if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
  205. v2 ^= 0xee;
  206. else
  207. v2 ^= 0xff;
  208. for (i = 0; i < ctx->drounds; ++i)
  209. SIPROUND;
  210. b = v0 ^ v1 ^ v2 ^ v3;
  211. U64TO8_LE(out, b);
  212. if (ctx->hash_size == SIPHASH_MIN_DIGEST_SIZE)
  213. return 1;
  214. v1 ^= 0xdd;
  215. for (i = 0; i < ctx->drounds; ++i)
  216. SIPROUND;
  217. b = v0 ^ v1 ^ v2 ^ v3;
  218. U64TO8_LE(out + 8, b);
  219. return 1;
  220. }