Revert "l4p/tcp: introduce tle_tcp_stream_establish() API"
[tldk.git] / lib / libtle_l4p / halfsiphash.h
1 /*
2  * SipHash reference C implementation
3
4  * Copyright (c) 2016 Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
5
6  * To the extent possible under law, the author(s) have dedicated all copyright
7  * and related and neighboring rights to this software to the public domain
8  * worldwide. This software is distributed without any warranty.
9
10  * You should have received a copy of the CC0 Public Domain Dedication along
11  * with this software. If not, see
12  * <http://creativecommons.org/publicdomain/zero/1.0/>.
13  */
14
15 #ifndef _SIPHASH_
16 #define _SIPHASH_
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /* The below siphash logic is taken from the source
23  * https://github.com/veorq/SipHash
24  */
25
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <rte_debug.h>
31
32 #define STATE_V2 0x6c796765
33 #define STATE_V3 0x74656462
34
35 #define ROTL(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
36
37 /*
38  * Siphash hash functionality logically divided into different
39  * phases and the functions are named based on the same.
40  * SipHash-2-4 is used i.e: 2 compression rounds and 4 finalization rounds.
41  */
42 static inline void
43 sipround(rte_xmm_t *v)
44 {
45         v->u32[0] += v->u32[1];
46         v->u32[1] = ROTL(v->u32[1], 5);
47         v->u32[1] ^= v->u32[0];
48         v->u32[0] = ROTL(v->u32[0], 16);
49         v->u32[2] += v->u32[3];
50         v->u32[3] = ROTL(v->u32[3], 8);
51         v->u32[3] ^= v->u32[2];
52         v->u32[0] += v->u32[3];
53         v->u32[3] = ROTL(v->u32[3], 7);
54         v->u32[3] ^= v->u32[0];
55         v->u32[2] += v->u32[1];
56         v->u32[1] = ROTL(v->u32[1], 13);
57         v->u32[1] ^= v->u32[2];
58         v->u32[2] = ROTL(v->u32[2], 16);
59 }
60
61 static inline void
62 siphash_initialization(rte_xmm_t *v, const rte_xmm_t *k)
63 {
64         uint32_t k0 = k->u32[0];
65         uint32_t k1 = k->u32[1];
66
67         v->u32[0] = k0;
68         v->u32[1] = k1;
69         v->u32[2] = STATE_V2 ^ k0;
70         v->u32[3] = STATE_V3 ^ k1;
71 }
72
73 static inline void
74 siphash_compression(const uint32_t *in, size_t len, rte_xmm_t *v)
75 {
76         uint32_t i;
77
78         for (i = 0; i < len; i++) {
79                 v->u32[3] ^= in[i];
80                 sipround(v);
81                 sipround(v);
82                 v->u32[0] ^= in[i];
83         }
84 }
85
86 static inline void
87 siphash_finalization(rte_xmm_t *v)
88 {
89         v->u32[2] ^= 0xff;
90         sipround(v);
91         sipround(v);
92         sipround(v);
93         sipround(v);
94 }
95
96 #ifdef __cplusplus
97 }
98 #endif
99
100 #endif /* __SIPHASH__ */