siphash.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "internal/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. /* hash_size = crounds = drounds = 0 means SipHash24 with 16-byte output */
  69. int SipHash_Init(SIPHASH *ctx, const unsigned char *k, int hash_size, int crounds, int drounds)
  70. {
  71. uint64_t k0 = U8TO64_LE(k);
  72. uint64_t k1 = U8TO64_LE(k + 8);
  73. if (hash_size == 0)
  74. hash_size = SIPHASH_MAX_DIGEST_SIZE;
  75. else if (hash_size != SIPHASH_MIN_DIGEST_SIZE &&
  76. hash_size != SIPHASH_MAX_DIGEST_SIZE)
  77. return 0;
  78. if (drounds == 0)
  79. drounds = SIPHASH_D_ROUNDS;
  80. if (crounds == 0)
  81. crounds = SIPHASH_C_ROUNDS;
  82. ctx->crounds = crounds;
  83. ctx->drounds = drounds;
  84. ctx->hash_size = hash_size;
  85. ctx->len = 0;
  86. ctx->total_inlen = 0;
  87. ctx->v0 = 0x736f6d6570736575ULL ^ k0;
  88. ctx->v1 = 0x646f72616e646f6dULL ^ k1;
  89. ctx->v2 = 0x6c7967656e657261ULL ^ k0;
  90. ctx->v3 = 0x7465646279746573ULL ^ k1;
  91. if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
  92. ctx->v1 ^= 0xee;
  93. return 1;
  94. }
  95. void SipHash_Update(SIPHASH *ctx, const unsigned char *in, size_t inlen)
  96. {
  97. uint64_t m;
  98. const uint8_t *end;
  99. int left;
  100. int i;
  101. uint64_t v0 = ctx->v0;
  102. uint64_t v1 = ctx->v1;
  103. uint64_t v2 = ctx->v2;
  104. uint64_t v3 = ctx->v3;
  105. ctx->total_inlen += inlen;
  106. if (ctx->len) {
  107. /* deal with leavings */
  108. size_t available = SIPHASH_BLOCK_SIZE - ctx->len;
  109. /* not enough to fill leavings */
  110. if (inlen < available) {
  111. memcpy(&ctx->leavings[ctx->len], in, inlen);
  112. ctx->len += inlen;
  113. return;
  114. }
  115. /* copy data into leavings and reduce input */
  116. memcpy(&ctx->leavings[ctx->len], in, available);
  117. inlen -= available;
  118. in += available;
  119. /* process leavings */
  120. m = U8TO64_LE(ctx->leavings);
  121. v3 ^= m;
  122. for (i = 0; i < ctx->crounds; ++i)
  123. SIPROUND;
  124. v0 ^= m;
  125. }
  126. left = inlen & (SIPHASH_BLOCK_SIZE-1); /* gets put into leavings */
  127. end = in + inlen - left;
  128. for (; in != end; in += 8) {
  129. m = U8TO64_LE(in);
  130. v3 ^= m;
  131. for (i = 0; i < ctx->crounds; ++i)
  132. SIPROUND;
  133. v0 ^= m;
  134. }
  135. /* save leavings and other ctx */
  136. if (left)
  137. memcpy(ctx->leavings, end, left);
  138. ctx->len = left;
  139. ctx->v0 = v0;
  140. ctx->v1 = v1;
  141. ctx->v2 = v2;
  142. ctx->v3 = v3;
  143. }
  144. int SipHash_Final(SIPHASH *ctx, unsigned char *out, size_t outlen)
  145. {
  146. /* finalize hash */
  147. int i;
  148. uint64_t b = ctx->total_inlen << 56;
  149. uint64_t v0 = ctx->v0;
  150. uint64_t v1 = ctx->v1;
  151. uint64_t v2 = ctx->v2;
  152. uint64_t v3 = ctx->v3;
  153. if (outlen != (size_t)ctx->hash_size)
  154. return 0;
  155. switch (ctx->len) {
  156. case 7:
  157. b |= ((uint64_t)ctx->leavings[6]) << 48;
  158. /* fall thru */
  159. case 6:
  160. b |= ((uint64_t)ctx->leavings[5]) << 40;
  161. /* fall thru */
  162. case 5:
  163. b |= ((uint64_t)ctx->leavings[4]) << 32;
  164. /* fall thru */
  165. case 4:
  166. b |= ((uint64_t)ctx->leavings[3]) << 24;
  167. /* fall thru */
  168. case 3:
  169. b |= ((uint64_t)ctx->leavings[2]) << 16;
  170. /* fall thru */
  171. case 2:
  172. b |= ((uint64_t)ctx->leavings[1]) << 8;
  173. /* fall thru */
  174. case 1:
  175. b |= ((uint64_t)ctx->leavings[0]);
  176. case 0:
  177. break;
  178. }
  179. v3 ^= b;
  180. for (i = 0; i < ctx->crounds; ++i)
  181. SIPROUND;
  182. v0 ^= b;
  183. if (ctx->hash_size == SIPHASH_MAX_DIGEST_SIZE)
  184. v2 ^= 0xee;
  185. else
  186. v2 ^= 0xff;
  187. for (i = 0; i < ctx->drounds; ++i)
  188. SIPROUND;
  189. b = v0 ^ v1 ^ v2 ^ v3;
  190. U64TO8_LE(out, b);
  191. if (ctx->hash_size == SIPHASH_MIN_DIGEST_SIZE)
  192. return 1;
  193. v1 ^= 0xdd;
  194. for (i = 0; i < ctx->drounds; ++i)
  195. SIPROUND;
  196. b = v0 ^ v1 ^ v2 ^ v3;
  197. U64TO8_LE(out + 8, b);
  198. return 1;
  199. }