Debian Codegraph @

src/momo/halfsiphash.c

raw
1
2 /*
3 SipHash reference C implementation
4
5 Copyright (c) 2016 Jean-Philippe Aumasson <[email protected]>
6
7 To the extent possible under law, the author(s) have dedicated all copyright
8 and related and neighboring rights to this software to the public domain
9 worldwide. This software is distributed without any warranty.
10
11 You should have received a copy of the CC0 Public Domain Dedication along
12 with
13 this software. If not, see
14 <http://creativecommons.org/publicdomain/zero/1.0/>.
15 */
16 // modified for simplicity
17 #include <stddef.h>
18 #include <stdint.h>
19
20 /* default: SipHash-2-4 */
21 #ifndef cROUNDS
22 #define cROUNDS 2
23 #endif
24 #ifndef dROUNDS
25 #define dROUNDS 4
26 #endif
27
28 #define ROTL(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
29
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
36 #define U8TO32_LE(p) \
37 (((uint32_t)((p)[0])) | ((uint32_t)((p)[1]) << 8) | \
38 ((uint32_t)((p)[2]) << 16) | ((uint32_t)((p)[3]) << 24))
39
40 #define SIPROUND \
41 do { \
42 v0 += v1; \
43 v1 = ROTL(v1, 5); \
44 v1 ^= v0; \
45 v0 = ROTL(v0, 16); \
46 v2 += v3; \
47 v3 = ROTL(v3, 8); \
48 v3 ^= v2; \
49 v0 += v3; \
50 v3 = ROTL(v3, 7); \
51 v3 ^= v0; \
52 v2 += v1; \
53 v1 = ROTL(v1, 13); \
54 v1 ^= v2; \
55 v2 = ROTL(v2, 16); \
56 } while (0)
57
58
59 #ifdef DEBUG_SIPHASH
60 #include <stdio.h>
61
62 #define TRACE \
63 do { \
64 printf("(%3zu) v0 %08" PRIx32 "\n", inlen, v0); \
65 printf("(%3zu) v1 %08" PRIx32 "\n", inlen, v1); \
66 printf("(%3zu) v2 %08" PRIx32 "\n", inlen, v2); \
67 printf("(%3zu) v3 %08" PRIx32 "\n", inlen, v3); \
68 } while (0)
69 #else
70 #define TRACE
71 #endif
72
73 /*
74 Computes a SipHash value
75 *in: pointer to input data (read-only)
76 inlen: input data length in bytes (any size_t value)
77 *k: pointer to the key data (read-only), must be 8 bytes
78 *out: pointer to output data (write-only), outlen bytes must be allocated
79 outlen: length of the output in bytes, must be 4 or 8
80 */
81 static uint32_t halfsiphash(const void *in, const size_t inlen, uint64_t k) {
82
83 const unsigned char *ni = (const unsigned char *)in;
84
85 uint32_t v0 = 0;
86 uint32_t v1 = 0;
87 uint32_t v2 = UINT32_C(0x6c796765);
88 uint32_t v3 = UINT32_C(0x74656462);
89 uint32_t k0 = k;
90 uint32_t k1 = k >> 32;
91 uint32_t m;
92 int i;
93 const unsigned char *end = ni + inlen - (inlen % sizeof(uint32_t));
94 const int left = inlen & 3;
95 uint32_t b = ((uint32_t)inlen) << 24;
96 v3 ^= k1;
97 v2 ^= k0;
98 v1 ^= k1;
99 v0 ^= k0;
100
101 for (; ni != end; ni += 4) {
102 m = U8TO32_LE(ni);
103 v3 ^= m;
104
105 TRACE;
106 for (i = 0; i < cROUNDS; ++i)
107 SIPROUND;
108
109 v0 ^= m;
110 }
111
112 switch (left) {
113 case 3:
114 b |= ((uint32_t)ni[2]) << 16;
115 /* FALLTHRU */
116 case 2:
117 b |= ((uint32_t)ni[1]) << 8;
118 /* FALLTHRU */
119 case 1:
120 b |= ((uint32_t)ni[0]);
121 break;
122 case 0:
123 break;
124 }
125
126 v3 ^= b;
127
128 TRACE;
129 for (i = 0; i < cROUNDS; ++i)
130 SIPROUND;
131
132 v0 ^= b;
133
134 v2 ^= 0xff;
135
136 TRACE;
137 for (i = 0; i < dROUNDS; ++i)
138 SIPROUND;
139
140 b = v1 ^ v3;
141 return b;
142 }