aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtle_l4p/halfsiphash.h
blob: e8e21e4b4cc9c01b79411ecc13c992cc3d10b077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * SipHash reference C implementation

 * Copyright (c) 2016 Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>

 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.

 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

#ifndef _SIPHASH_
#define _SIPHASH_

#ifdef __cplusplus
extern "C" {
#endif

/* The below siphash logic is taken from the source
 * https://github.com/veorq/SipHash
 */

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <rte_debug.h>

#define STATE_V2 0x6c796765
#define STATE_V3 0x74656462

#define ROTL(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))

/*
 * Siphash hash functionality logically divided into different
 * phases and the functions are named based on the same.
 * SipHash-2-4 is used i.e: 2 compression rounds and 4 finalization rounds.
 */
static inline void
sipround(rte_xmm_t *v)
{
	v->u32[0] += v->u32[1];
	v->u32[1] = ROTL(v->u32[1], 5);
	v->u32[1] ^= v->u32[0];
	v->u32[0] = ROTL(v->u32[0], 16);
	v->u32[2] += v->u32[3];
	v->u32[3] = ROTL(v->u32[3], 8);
	v->u32[3] ^= v->u32[2];
	v->u32[0] += v->u32[3];
	v->u32[3] = ROTL(v->u32[3], 7);
	v->u32[3] ^= v->u32[0];
	v->u32[2] += v->u32[1];
	v->u32[1] = ROTL(v->u32[1], 13);
	v->u32[1] ^= v->u32[2];
	v->u32[2] = ROTL(v->u32[2], 16);
}

static inline void
siphash_initialization(rte_xmm_t *v, const rte_xmm_t *k)
{
	uint32_t k0 = k->u32[0];
	uint32_t k1 = k->u32[1];

	v->u32[0] = k0;
	v->u32[1] = k1;
	v->u32[2] = STATE_V2 ^ k0;
	v->u32[3] = STATE_V3 ^ k1;
}

static inline void
siphash_compression(const uint32_t *in, size_t len, rte_xmm_t *v)
{
	uint32_t i;

	for (i = 0; i < len; i++) {
		v->u32[3] ^= in[i];
		sipround(v);
		sipround(v);
		v->u32[0] ^= in[i];
	}
}

static inline void
siphash_finalization(rte_xmm_t *v)
{
	v->u32[2] ^= 0xff;
	sipround(v);
	sipround(v);
	sipround(v);
	sipround(v);
}

#ifdef __cplusplus
}
#endif

#endif /* __SIPHASH__ */