siphash.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* $OpenBSD: siphash.c,v 1.3 2015/02/20 11:51:03 tedu Exp $ */
  2. /*-
  3. * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote
  15. * products derived from this software without specific prior written
  16. * permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /*
  31. * SipHash is a family of PRFs SipHash-c-d where the integer parameters c and d
  32. * are the number of compression rounds and the number of finalization rounds.
  33. * A compression round is identical to a finalization round and this round
  34. * function is called SipRound. Given a 128-bit key k and a (possibly empty)
  35. * byte string m, SipHash-c-d returns a 64-bit value SipHash-c-d(k; m).
  36. *
  37. * Implemented from the paper "SipHash: a fast short-input PRF", 2012.09.18,
  38. * by Jean-Philippe Aumasson and Daniel J. Bernstein,
  39. * Permanent Document ID b9a943a805fbfc6fde808af9fc0ecdfa
  40. * https://131002.net/siphash/siphash.pdf
  41. * https://131002.net/siphash/
  42. */
  43. #include <sys/param.h>
  44. #include <sys/systm.h>
  45. #include <crypto/siphash.h>
  46. static void SipHash_CRounds(SIPHASH_CTX *, int);
  47. static void SipHash_Rounds(SIPHASH_CTX *, int);
  48. void
  49. SipHash_Init(SIPHASH_CTX *ctx, const SIPHASH_KEY *key)
  50. {
  51. uint64_t k0, k1;
  52. k0 = lemtoh64(&key->k0);
  53. k1 = lemtoh64(&key->k1);
  54. ctx->v[0] = 0x736f6d6570736575ULL ^ k0;
  55. ctx->v[1] = 0x646f72616e646f6dULL ^ k1;
  56. ctx->v[2] = 0x6c7967656e657261ULL ^ k0;
  57. ctx->v[3] = 0x7465646279746573ULL ^ k1;
  58. memset(ctx->buf, 0, sizeof(ctx->buf));
  59. ctx->bytes = 0;
  60. }
  61. void
  62. SipHash_Update(SIPHASH_CTX *ctx, int rc, int rf, const void *src, size_t len)
  63. {
  64. const uint8_t *ptr = src;
  65. size_t left, used;
  66. if (len == 0)
  67. return;
  68. used = ctx->bytes % sizeof(ctx->buf);
  69. ctx->bytes += len;
  70. if (used > 0) {
  71. left = sizeof(ctx->buf) - used;
  72. if (len >= left) {
  73. memcpy(&ctx->buf[used], ptr, left);
  74. SipHash_CRounds(ctx, rc);
  75. len -= left;
  76. ptr += left;
  77. } else {
  78. memcpy(&ctx->buf[used], ptr, len);
  79. return;
  80. }
  81. }
  82. while (len >= sizeof(ctx->buf)) {
  83. memcpy(ctx->buf, ptr, sizeof(ctx->buf));
  84. SipHash_CRounds(ctx, rc);
  85. len -= sizeof(ctx->buf);
  86. ptr += sizeof(ctx->buf);
  87. }
  88. if (len > 0)
  89. memcpy(&ctx->buf[used], ptr, len);
  90. }
  91. void
  92. SipHash_Final(void *dst, SIPHASH_CTX *ctx, int rc, int rf)
  93. {
  94. uint64_t r;
  95. r = SipHash_End(ctx, rc, rf);
  96. htolem64((uint64_t *)dst, r);
  97. }
  98. uint64_t
  99. SipHash_End(SIPHASH_CTX *ctx, int rc, int rf)
  100. {
  101. uint64_t r;
  102. size_t left, used;
  103. used = ctx->bytes % sizeof(ctx->buf);
  104. left = sizeof(ctx->buf) - used;
  105. memset(&ctx->buf[used], 0, left - 1);
  106. ctx->buf[7] = ctx->bytes;
  107. SipHash_CRounds(ctx, rc);
  108. ctx->v[2] ^= 0xff;
  109. SipHash_Rounds(ctx, rf);
  110. r = (ctx->v[0] ^ ctx->v[1]) ^ (ctx->v[2] ^ ctx->v[3]);
  111. explicit_bzero(ctx, sizeof(*ctx));
  112. return (r);
  113. }
  114. uint64_t
  115. SipHash(const SIPHASH_KEY *key, int rc, int rf, const void *src, size_t len)
  116. {
  117. SIPHASH_CTX ctx;
  118. SipHash_Init(&ctx, key);
  119. SipHash_Update(&ctx, rc, rf, src, len);
  120. return (SipHash_End(&ctx, rc, rf));
  121. }
  122. #define SIP_ROTL(x, b) ((x) << (b)) | ( (x) >> (64 - (b)))
  123. static void
  124. SipHash_Rounds(SIPHASH_CTX *ctx, int rounds)
  125. {
  126. while (rounds--) {
  127. ctx->v[0] += ctx->v[1];
  128. ctx->v[2] += ctx->v[3];
  129. ctx->v[1] = SIP_ROTL(ctx->v[1], 13);
  130. ctx->v[3] = SIP_ROTL(ctx->v[3], 16);
  131. ctx->v[1] ^= ctx->v[0];
  132. ctx->v[3] ^= ctx->v[2];
  133. ctx->v[0] = SIP_ROTL(ctx->v[0], 32);
  134. ctx->v[2] += ctx->v[1];
  135. ctx->v[0] += ctx->v[3];
  136. ctx->v[1] = SIP_ROTL(ctx->v[1], 17);
  137. ctx->v[3] = SIP_ROTL(ctx->v[3], 21);
  138. ctx->v[1] ^= ctx->v[2];
  139. ctx->v[3] ^= ctx->v[0];
  140. ctx->v[2] = SIP_ROTL(ctx->v[2], 32);
  141. }
  142. }
  143. static void
  144. SipHash_CRounds(SIPHASH_CTX *ctx, int rounds)
  145. {
  146. uint64_t m = lemtoh64((uint64_t *)ctx->buf);
  147. ctx->v[3] ^= m;
  148. SipHash_Rounds(ctx, rounds);
  149. ctx->v[0] ^= m;
  150. }