djbdnscurve6 45
djbdnscurve6
Loading...
Searching...
No Matches
siphash.c
Go to the documentation of this file.
1/*
2 * siphash.c: originally written
3 * as 'dnscache-siphash.patch' by Mr Frank Denis.
4 * -> http://download.pureftpd.org/misc/dnscache-siphash.patch
5 *
6 * Copyright (c) 2012-2014 Frank Denis <j at pureftpd dot org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
19 *
20 */
21#include "siphash.h"
22#include "uint_t.h"
23
24typedef uint64 u64;
25typedef uint32 u32;
26typedef unsigned char u8;
27
28#define ROTL(x,b) (u64)(((x) << (b)) | ((x) >> (64 - (b))))
29
30#define U32TO8_LE(p, v) \
31 (p)[0] = (u8)((v) ); (p)[1] = (u8)((v) >> 8); \
32 (p)[2] = (u8)((v) >> 16); (p)[3] = (u8)((v) >> 24);
33
34#define U64TO8_LE(p, v) \
35 U32TO8_LE((p), (u32)((v) )); \
36 U32TO8_LE((p) + 4, (u32)((v) >> 32));
37
38#define U8TO64_LE(p) \
39 (((u64)((p)[0]) ) | \
40 ((u64)((p)[1]) << 8) | \
41 ((u64)((p)[2]) << 16) | \
42 ((u64)((p)[3]) << 24) | \
43 ((u64)((p)[4]) << 32) | \
44 ((u64)((p)[5]) << 40) | \
45 ((u64)((p)[6]) << 48) | \
46 ((u64)((p)[7]) << 56))
47
48#define SIPROUND \
49 do { \
50 x0 += x1; x1=ROTL(x1,13); x1 ^= x0; x0=ROTL(x0,32); \
51 x2 += x3; x3=ROTL(x3,16); x3 ^= x2; \
52 x0 += x3; x3=ROTL(x3,21); x3 ^= x0; \
53 x2 += x1; x1=ROTL(x1,17); x1 ^= x2; x2=ROTL(x2,32); \
54 } while(0)
55
56/* SipHash-2-4 */
57int siphash24(unsigned char *out, const unsigned char *in,unsigned long long inlen,const unsigned char *k)
58{
59 /* "somepseudorandomlygeneratedbytes" */
60 u64 x0 = 0x736f6d6570736575ULL;
61 u64 x1 = 0x646f72616e646f6dULL;
62 u64 x2 = 0x6c7967656e657261ULL;
63 u64 x3 = 0x7465646279746573ULL;
64
65 u64 b, m;
66 u64 k0 = U8TO64_LE(k);
67 u64 k1 = U8TO64_LE(k + 8);
68
69 const int left = inlen & 7;
70 const u8 *end = in + inlen - (inlen % sizeof(u64));
71
72 b = ((u64)inlen) << 56;
73 x3 ^= k1;
74 x2 ^= k0;
75 x1 ^= k1;
76 x0 ^= k0;
77
78 for (; in != end; in += 8) {
79 m = U8TO64_LE (in);
80 x3 ^= m;
83 x0 ^= m;
84 }
85
86 switch (left) {
87 case 7: b |= ((u64)in[6]) << 48;
88 case 6: b |= ((u64)in[5]) << 40;
89 case 5: b |= ((u64)in[4]) << 32;
90 case 4: b |= ((u64)in[3]) << 24;
91 case 3: b |= ((u64)in[2]) << 16;
92 case 2: b |= ((u64)in[1]) << 8;
93 case 1: b |= ((u64)in[0]); break;
94 case 0: break;
95 }
96
97 x3 ^= b;
100 x0 ^= b;
101 x2 ^= 0xff;
102 SIPROUND;
103 SIPROUND;
104 SIPROUND;
105 SIPROUND;
106
107 b = x0 ^ x1 ^ x2 ^ x3;
108 U64TO8_LE(out, b);
109
110 return 0;
111}
buffer b
Definition: auto-str.c:5
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
uint64 u64
Definition: siphash.c:24
uint32 u32
Definition: siphash.c:25
#define U8TO64_LE(p)
Definition: siphash.c:38
#define U64TO8_LE(p, v)
Definition: siphash.c:34
int siphash24(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *k)
Definition: siphash.c:57
#define SIPROUND
Definition: siphash.c:48
unsigned char u8
Definition: siphash.c:26