xref: /linux/security/selinux/include/hash.h (revision 51e3b98d737aa3e76e077db77b9aa749436c93ac)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef _SELINUX_HASH_H_
4 #define _SELINUX_HASH_H_
5 
6 /*
7  * Based on MurmurHash3, written by Austin Appleby and placed in the
8  * public domain.
9  */
av_hash(u32 key1,u32 key2,u32 key3,u32 mask)10 static inline u32 av_hash(u32 key1, u32 key2, u32 key3, u32 mask)
11 {
12 	static const u32 c1 = 0xcc9e2d51;
13 	static const u32 c2 = 0x1b873593;
14 	static const u32 r1 = 15;
15 	static const u32 r2 = 13;
16 	static const u32 m = 5;
17 	static const u32 n = 0xe6546b64;
18 
19 	u32 hash = 0;
20 
21 #define mix(input)                                         \
22 	do {                                               \
23 		u32 v = input;                             \
24 		v *= c1;                                   \
25 		v = (v << r1) | (v >> (32 - r1));          \
26 		v *= c2;                                   \
27 		hash ^= v;                                 \
28 		hash = (hash << r2) | (hash >> (32 - r2)); \
29 		hash = hash * m + n;                       \
30 	} while (0)
31 
32 	mix(key1);
33 	mix(key2);
34 	mix(key3);
35 
36 #undef mix
37 
38 	hash ^= hash >> 16;
39 	hash *= 0x85ebca6b;
40 	hash ^= hash >> 13;
41 	hash *= 0xc2b2ae35;
42 	hash ^= hash >> 16;
43 
44 	return hash & mask;
45 }
46 
47 #endif /* _SELINUX_HASH_H_ */
48