pex: avoid sending a query to a host more than once every 15 seconds
[project/unetd.git] / siphash.h
1 /* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
2 *
3 * This file is provided under a dual BSD/GPLv2 license.
4 *
5 * SipHash: a fast short-input PRF
6 * https://131002.net/siphash/
7 *
8 * This implementation is specifically for SipHash2-4 for a secure PRF
9 * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for
10 * hashtables.
11 */
12
13 #ifndef _LINUX_SIPHASH_H
14 #define _LINUX_SIPHASH_H
15
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <stdbool.h>
19 #include "utils.h"
20
21 #define SIPHASH_ALIGNMENT __alignof__(uint64_t)
22 typedef struct {
23 uint64_t key[2];
24 } siphash_key_t;
25
26 static inline bool siphash_key_is_zero(const siphash_key_t *key)
27 {
28 return !(key->key[0] | key->key[1]);
29 }
30
31 uint64_t siphash(const void *data, size_t len, const siphash_key_t *key);
32
33 static inline void siphash_to_le64(void *dest, const void *data, size_t len,
34 const siphash_key_t *key)
35 {
36 uint64_t hash = siphash(data, len, key);
37
38 *(uint64_t *)dest = cpu_to_le64(hash);
39 }
40
41 static inline void siphash_to_be64(void *dest, const void *data, size_t len,
42 const siphash_key_t *key)
43 {
44 uint64_t hash = siphash(data, len, key);
45
46 *(uint64_t *)dest = cpu_to_be64(hash);
47 }
48
49 #endif /* _LINUX_SIPHASH_H */