siphash.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 2017-2021 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. #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
  26. #define U32TO8_LE(p, v) \
  27. (p)[0] = (uint8_t)((v)); \
  28. (p)[1] = (uint8_t)((v) >> 8); \
  29. (p)[2] = (uint8_t)((v) >> 16); \
  30. (p)[3] = (uint8_t)((v) >> 24);
  31. #define U64TO8_LE(p, v) \
  32. U32TO8_LE((p), (uint32_t)((v))); \
  33. U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
  34. #define U8TO64_LE(p) \
  35. (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
  36. ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
  37. ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
  38. ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
  39. #define SIPROUND \
  40. do { \
  41. v0 += v1; \
  42. v1 = ROTL(v1, 13); \
  43. v1 ^= v0; \
  44. v0 = ROTL(v0, 32); \
  45. v2 += v3; \
  46. v3 = ROTL(v3, 16); \
  47. v3 ^= v2; \
  48. v0 += v3; \
  49. v3 = ROTL(v3, 21); \
  50. v3 ^= v0; \
  51. v2 += v1; \
  52. v1 = ROTL(v1, 17); \
  53. v1 ^= v2; \
  54. v2 = ROTL(v2, 32); \
  55. } while (0)
  56. size_t SipHash_ctx_size(void)
  57. {
  58. return sizeof(SIPHASH);
  59. }
  60. size_t SipHash_hash_size(SIPHASH *ctx)
  61. {
  62. return ctx->hash_size;
  63. }
  64. static size_t siphash_adjust_hash_size(size_t hash_size)
  65. {
  66. if (hash_size == 0)
  67. hash_size = SIPHASH_MAX_DIGEST_SIZE;
  68. return hash_size;
  69. }
  70. int SipHash_set_hash_size(SIPHASH *ctx, size_t hash_size)
  71. {
  72. hash_size = siphash_adjust_hash_size(hash_size);
  73. if (hash_size != SIPHASH_MIN_DIGEST_SIZE
  74. && hash_size != SIPHASH_MAX_DIGEST_SIZE)
  75. return 0;
  76. /*
  77. * It's possible that the key was set first. If the hash size changes,
  78. * we need to adjust v1 (see SipHash_Init().
  79. */
  80. /* Start by adjusting the stored size, to make things easier */
  81. ctx->hash_size = siphash_adjust_hash_size(ctx->hash_size);
  82. /* Now, adjust ctx->v1 if the old and the new size differ */
  83. if ((size_t)ctx->hash_size != hash_size) {
  84. ctx->v1 ^= 0xee;
  85. ctx->hash_size = hash_size;
  86. }
  87. return 1;
  88. }
  89. /* hash_size = crounds = drounds = 0 means SipHash24 with 16-byte output */
  90. int SipHash_Init(SIPHASH *ctx, const unsigned char *k, int crounds, int drounds)
  91. {
  92. uint64_t k0 = U8TO64_LE(k);
  93. uint64_t k1 = U8TO64_LE(k + 8);
  94. /* If the hash size wasn't set, i.e. is zero */
  95. ctx->hash_size = siphash_adjust_hash_size(ctx->hash_size);
  96. if (drounds == 0)
  97. drounds = SIPHASH_D_ROUNDS;
  98. if (crounds == 0)
  99. crounds = SIPHASH_C_ROUNDS;
  100. ctx->crounds = crounds;
  101. ctx->drounds = drounds;
  102. ctx->len = 0;
  103. ctx->total_inlen = 0;
  104. ctx->v0 = 0x736f6d6570736575ULL ^ k0;
  105. ctx->v1 = 0x646f72616e646f6dULL ^ k1;
  106. ctx->v2 = 0x6c7967656e657261ULL ^ k0;
  107. ctx->v3 = 0x7465646279746573ULL ^ k1;
  108. if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
  109. ctx->v1 ^= 0xee;
  110. return 1;
  111. }
  112. void SipHash_Update(SIPHASH *ctx, const unsigned char *in, size_t inlen)
  113. {
  114. uint64_t m;
  115. const uint8_t *end;
  116. int left;
  117. unsigned int i;
  118. uint64_t v0 = ctx->v0;
  119. uint64_t v1 = ctx->v1;
  120. uint64_t v2 = ctx->v2;
  121. uint64_t v3 = ctx->v3;
  122. ctx->total_inlen += inlen;
  123. if (ctx->len) {
  124. /* deal with leavings */
  125. size_t available = SIPHASH_BLOCK_SIZE - ctx->len;
  126. /* not enough to fill leavings */
  127. if (inlen < available) {
  128. memcpy(&ctx->leavings[ctx->len], in, inlen);
  129. ctx->len += inlen;
  130. return;
  131. }
  132. /* copy data into leavings and reduce input */
  133. memcpy(&ctx->leavings[ctx->len], in, available);
  134. inlen -= available;
  135. in += available;
  136. /* process leavings */
  137. m = U8TO64_LE(ctx->leavings);
  138. v3 ^= m;
  139. for (i = 0; i < ctx->crounds; ++i)
  140. SIPROUND;
  141. v0 ^= m;
  142. }
  143. left = inlen & (SIPHASH_BLOCK_SIZE-1); /* gets put into leavings */
  144. end = in + inlen - left;
  145. for (; in != end; in += 8) {
  146. m = U8TO64_LE(in);
  147. v3 ^= m;
  148. for (i = 0; i < ctx->crounds; ++i)
  149. SIPROUND;
  150. v0 ^= m;
  151. }
  152. /* save leavings and other ctx */
  153. if (left)
  154. memcpy(ctx->leavings, end, left);
  155. ctx->len = left;
  156. ctx->v0 = v0;
  157. ctx->v1 = v1;
  158. ctx->v2 = v2;
  159. ctx->v3 = v3;
  160. }
  161. int SipHash_Final(SIPHASH *ctx, unsigned char *out, size_t outlen)
  162. {
  163. /* finalize hash */
  164. unsigned int i;
  165. uint64_t b = ctx->total_inlen << 56;
  166. uint64_t v0 = ctx->v0;
  167. uint64_t v1 = ctx->v1;
  168. uint64_t v2 = ctx->v2;
  169. uint64_t v3 = ctx->v3;
  170. if (outlen != (size_t)ctx->hash_size)
  171. return 0;
  172. switch (ctx->len) {
  173. case 7:
  174. b |= ((uint64_t)ctx->leavings[6]) << 48;
  175. /* fall thru */
  176. case 6:
  177. b |= ((uint64_t)ctx->leavings[5]) << 40;
  178. /* fall thru */
  179. case 5:
  180. b |= ((uint64_t)ctx->leavings[4]) << 32;
  181. /* fall thru */
  182. case 4:
  183. b |= ((uint64_t)ctx->leavings[3]) << 24;
  184. /* fall thru */
  185. case 3:
  186. b |= ((uint64_t)ctx->leavings[2]) << 16;
  187. /* fall thru */
  188. case 2:
  189. b |= ((uint64_t)ctx->leavings[1]) << 8;
  190. /* fall thru */
  191. case 1:
  192. b |= ((uint64_t)ctx->leavings[0]);
  193. case 0:
  194. break;
  195. }
  196. v3 ^= b;
  197. for (i = 0; i < ctx->crounds; ++i)
  198. SIPROUND;
  199. v0 ^= b;
  200. if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
  201. v2 ^= 0xee;
  202. else
  203. v2 ^= 0xff;
  204. for (i = 0; i < ctx->drounds; ++i)
  205. SIPROUND;
  206. b = v0 ^ v1 ^ v2 ^ v3;
  207. U64TO8_LE(out, b);
  208. if (ctx->hash_size == SIPHASH_MIN_DIGEST_SIZE)
  209. return 1;
  210. v1 ^= 0xdd;
  211. for (i = 0; i < ctx->drounds; ++i)
  212. SIPROUND;
  213. b = v0 ^ v1 ^ v2 ^ v3;
  214. U64TO8_LE(out + 8, b);
  215. return 1;
  216. }