]> git.ozlabs.org Git - ccan/blob - ccan/crypto/siphash24/siphash24.c
Remove travis workarounds for previous build system
[ccan] / ccan / crypto / siphash24 / siphash24.c
1 /* CC0 license (public domain) - see LICENSE file for details */
2 /* Based on CC0 reference implementation:
3  * https://github.com/veorq/SipHash c03e6bbf6613243bc30788912ad4afbc0b992d47
4  */
5 #include <ccan/crypto/siphash24/siphash24.h>
6 #include <ccan/endian/endian.h>
7 #include <stdbool.h>
8 #include <assert.h>
9 #include <string.h>
10
11 /* default: SipHash-2-4 */
12 #define cROUNDS 2
13 #define dROUNDS 4
14
15 #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
16
17 #define SIPROUND(v)                                                     \
18         do {                                                            \
19                 v[0] += v[1];                                           \
20                 v[1] = ROTL(v[1], 13);                                  \
21                 v[1] ^= v[0];                                           \
22                 v[0] = ROTL(v[0], 32);                                  \
23                 v[2] += v[3];                                           \
24                 v[3] = ROTL(v[3], 16);                                  \
25                 v[3] ^= v[2];                                           \
26                 v[0] += v[3];                                           \
27                 v[3] = ROTL(v[3], 21);                                  \
28                 v[3] ^= v[0];                                           \
29                 v[2] += v[1];                                           \
30                 v[1] = ROTL(v[1], 17);                                  \
31                 v[1] ^= v[2];                                           \
32                 v[2] = ROTL(v[2], 32);                                  \
33         } while (0)
34
35 static void invalidate_siphash24(struct siphash24_ctx *ctx)
36 {
37         ctx->bytes = -1ULL;
38 }
39
40 static void check_siphash24(struct siphash24_ctx *ctx)
41 {
42         assert(ctx->bytes != -1ULL);
43 }
44
45 static bool alignment_ok(const void *p, size_t n)
46 {
47 #if HAVE_UNALIGNED_ACCESS
48         return true;
49 #else
50         return ((size_t)p % n == 0);
51 #endif
52 }
53
54 static void add_64bits(uint64_t v[4], uint64_t in)
55 {
56         int i;
57         uint64_t m = cpu_to_le64(in);
58         v[3] ^= m;
59
60         for (i = 0; i < cROUNDS; ++i)
61                 SIPROUND(v);
62
63         v[0] ^= m;
64 }
65
66 static void add(struct siphash24_ctx *ctx, const void *p, size_t len)
67 {
68         const unsigned char *data = p;
69         size_t bufsize = ctx->bytes % sizeof(ctx->buf.u8);
70
71         if (bufsize + len >= sizeof(ctx->buf.u8)) {
72                 // Fill the buffer, and process it.
73                 memcpy(ctx->buf.u8 + bufsize, data,
74                        sizeof(ctx->buf.u8) - bufsize);
75                 ctx->bytes += sizeof(ctx->buf.u8) - bufsize;
76                 data += sizeof(ctx->buf.u8) - bufsize;
77                 len -= sizeof(ctx->buf.u8) - bufsize;
78                 add_64bits(ctx->v, ctx->buf.u64);
79                 bufsize = 0;
80         }
81
82         while (len >= sizeof(ctx->buf.u8)) {
83                 // Process full chunks directly from the source.
84                 if (alignment_ok(data, sizeof(uint64_t)))
85                         add_64bits(ctx->v, *(const uint64_t *)data);
86                 else {
87                         memcpy(ctx->buf.u8, data, sizeof(ctx->buf));
88                         add_64bits(ctx->v, ctx->buf.u64);
89                 }
90                 ctx->bytes += sizeof(ctx->buf.u8);
91                 data += sizeof(ctx->buf.u8);
92                 len -= sizeof(ctx->buf.u8);
93         }
94             
95         if (len) {
96                 // Fill the buffer with what remains.
97                 memcpy(ctx->buf.u8 + bufsize, data, len);
98                 ctx->bytes += len;
99         }
100 }
101
102 void siphash24_init(struct siphash24_ctx *ctx, const struct siphash_seed *seed)
103 {
104         struct siphash24_ctx init = SIPHASH24_INIT(0, 0);
105         *ctx = init;
106         ctx->v[0] ^= seed->u.u64[0];
107         ctx->v[1] ^= seed->u.u64[1];
108         ctx->v[2] ^= seed->u.u64[0];
109         ctx->v[3] ^= seed->u.u64[1];
110 }
111
112 void siphash24_update(struct siphash24_ctx *ctx, const void *p, size_t size)
113 {
114         check_siphash24(ctx);
115         add(ctx, p, size);
116 }
117
118 uint64_t siphash24_done(struct siphash24_ctx *ctx)
119 {
120         uint64_t b;
121         int i;
122
123         b = ctx->bytes << 56;
124
125         switch (ctx->bytes % 8) {
126         case 7:
127                 b |= ((uint64_t)ctx->buf.u8[6]) << 48;
128         case 6:
129                 b |= ((uint64_t)ctx->buf.u8[5]) << 40;
130         case 5:
131                 b |= ((uint64_t)ctx->buf.u8[4]) << 32;
132         case 4:
133                 b |= ((uint64_t)ctx->buf.u8[3]) << 24;
134         case 3:
135                 b |= ((uint64_t)ctx->buf.u8[2]) << 16;
136         case 2:
137                 b |= ((uint64_t)ctx->buf.u8[1]) << 8;
138         case 1:
139                 b |= ((uint64_t)ctx->buf.u8[0]);
140                 break;
141         case 0:
142                 break;
143         }
144
145         ctx->v[3] ^= b;
146
147         for (i = 0; i < cROUNDS; ++i)
148                 SIPROUND(ctx->v);
149
150         ctx->v[0] ^= b;
151
152         ctx->v[2] ^= 0xff;
153
154         for (i = 0; i < dROUNDS; ++i)
155                 SIPROUND(ctx->v);
156
157         b = ctx->v[0] ^ ctx->v[1] ^ ctx->v[2] ^ ctx->v[3];
158
159         invalidate_siphash24(ctx);
160         return cpu_to_le64(b);
161 }
162
163 uint64_t siphash24(const struct siphash_seed *seed, const void *p, size_t size)
164 {
165         struct siphash24_ctx ctx;
166
167         siphash24_init(&ctx, seed);
168         siphash24_update(&ctx, p, size);
169         return siphash24_done(&ctx);
170 }
171         
172 void siphash24_u8(struct siphash24_ctx *ctx, uint8_t v)
173 {
174         siphash24_update(ctx, &v, sizeof(v));
175 }
176
177 void siphash24_u16(struct siphash24_ctx *ctx, uint16_t v)
178 {
179         siphash24_update(ctx, &v, sizeof(v));
180 }
181
182 void siphash24_u32(struct siphash24_ctx *ctx, uint32_t v)
183 {
184         siphash24_update(ctx, &v, sizeof(v));
185 }
186
187 void siphash24_u64(struct siphash24_ctx *ctx, uint64_t v)
188 {
189         siphash24_update(ctx, &v, sizeof(v));
190 }
191
192 /* Add as little-endian */
193 void siphash24_le16(struct siphash24_ctx *ctx, uint16_t v)
194 {
195         leint16_t lev = cpu_to_le16(v);
196         siphash24_update(ctx, &lev, sizeof(lev));
197 }
198         
199 void siphash24_le32(struct siphash24_ctx *ctx, uint32_t v)
200 {
201         leint32_t lev = cpu_to_le32(v);
202         siphash24_update(ctx, &lev, sizeof(lev));
203 }
204         
205 void siphash24_le64(struct siphash24_ctx *ctx, uint64_t v)
206 {
207         leint64_t lev = cpu_to_le64(v);
208         siphash24_update(ctx, &lev, sizeof(lev));
209 }
210
211 /* Add as big-endian */
212 void siphash24_be16(struct siphash24_ctx *ctx, uint16_t v)
213 {
214         beint16_t bev = cpu_to_be16(v);
215         siphash24_update(ctx, &bev, sizeof(bev));
216 }
217         
218 void siphash24_be32(struct siphash24_ctx *ctx, uint32_t v)
219 {
220         beint32_t bev = cpu_to_be32(v);
221         siphash24_update(ctx, &bev, sizeof(bev));
222 }
223         
224 void siphash24_be64(struct siphash24_ctx *ctx, uint64_t v)
225 {
226         beint64_t bev = cpu_to_be64(v);
227         siphash24_update(ctx, &bev, sizeof(bev));
228 }