2 *--------------------------------------------------------------------------------------
3 * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
4 * to avoid "algorithmic complexity attacks".
6 * If USE_HASH_SEED is defined, hash randomisation is done by default
7 * (see also perl.c:perl_parse() and S_init_tls_and_interp() and util.c:get_hash_seed())
9 #ifndef PERL_SEEN_HV_FUNC_H /* compile once */
10 #define PERL_SEEN_HV_FUNC_H
14 || defined(PERL_HASH_FUNC_SIPHASH) \
15 || defined(PERL_HASH_FUNC_SIPHASH13) \
16 || defined(PERL_HASH_FUNC_STADTX) \
17 || defined(PERL_HASH_FUNC_ZAPHOD32) \
20 # define PERL_HASH_FUNC_STADTX
22 # define PERL_HASH_FUNC_ZAPHOD32
26 #ifndef PERL_HASH_USE_SBOX32_ALSO
27 #define PERL_HASH_USE_SBOX32_ALSO 1
30 #ifndef SBOX32_MAX_LEN
31 #define SBOX32_MAX_LEN 24
34 /* this must be after the SBOX32_MAX_LEN define */
35 #include "sbox32_hash.h"
37 #if defined(PERL_HASH_FUNC_SIPHASH)
38 # define __PERL_HASH_FUNC "SIPHASH_2_4"
39 # define __PERL_HASH_SEED_BYTES 16
40 # define __PERL_HASH_STATE_BYTES 32
41 # define __PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state)
42 # define __PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_2_4_with_state((state),(U8*)(str),(len))
43 #elif defined(PERL_HASH_FUNC_SIPHASH13)
44 # define __PERL_HASH_FUNC "SIPHASH_1_3"
45 # define __PERL_HASH_SEED_BYTES 16
46 # define __PERL_HASH_STATE_BYTES 32
47 # define __PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state)
48 # define __PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_1_3_with_state((state),(U8*)(str),(len))
49 #elif defined(PERL_HASH_FUNC_STADTX)
50 # define __PERL_HASH_FUNC "STATDX"
51 # define __PERL_HASH_SEED_BYTES 16
52 # define __PERL_HASH_STATE_BYTES 32
53 # define __PERL_HASH_SEED_STATE(seed,state) stadtx_seed_state(seed,state)
54 # define __PERL_HASH_WITH_STATE(state,str,len) (U32)stadtx_hash_with_state((state),(U8*)(str),(len))
55 # include "stadtx_hash.h"
56 #elif defined(PERL_HASH_FUNC_ZAPHOD32)
57 # define __PERL_HASH_FUNC "ZAPHOD32"
58 # define __PERL_HASH_SEED_BYTES 12
59 # define __PERL_HASH_STATE_BYTES 12
60 # define __PERL_HASH_SEED_STATE(seed,state) zaphod32_seed_state(seed,state)
61 # define __PERL_HASH_WITH_STATE(state,str,len) (U32)zaphod32_hash_with_state((state),(U8*)(str),(len))
62 # include "zaphod32_hash.h"
65 #ifndef __PERL_HASH_WITH_STATE
66 #error "No hash function defined!"
68 #ifndef __PERL_HASH_SEED_BYTES
69 #error "__PERL_HASH_SEED_BYTES not defined"
71 #ifndef __PERL_HASH_FUNC
72 #error "__PERL_HASH_FUNC not defined"
76 #if PERL_HASH_USE_SBOX32_ALSO == 1
77 # define _PERL_HASH_FUNC __PERL_HASH_FUNC
78 # define _PERL_HASH_SEED_BYTES __PERL_HASH_SEED_BYTES
79 # define _PERL_HASH_STATE_BYTES __PERL_HASH_STATE_BYTES
80 # define _PERL_HASH_SEED_STATE(seed,state) __PERL_HASH_SEED_STATE(seed,state)
81 # define _PERL_HASH_WITH_STATE(state,str,len) __PERL_HASH_WITH_STATE(state,str,len)
84 #define _PERL_HASH_FUNC "SBOX32_WITH_" __PERL_HASH_FUNC
86 #define _PERL_HASH_SEED_BYTES ( __PERL_HASH_SEED_BYTES + ( 3 * sizeof(U32) ) )
88 #define _PERL_HASH_STATE_BYTES \
89 ( __PERL_HASH_SEED_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) )
91 #define _PERL_HASH_SEED_STATE(seed,state) STMT_START { \
92 __PERL_HASH_SEED_STATE(seed,state); \
93 sbox32_seed_state96(seed + __PERL_HASH_SEED_BYTES , state + __PERL_HASH_STATE_BYTES); \
96 #define _PERL_HASH_WITH_STATE(state,str,len) \
97 (LIKELY(len <= SBOX32_MAX_LEN) \
98 ? sbox32_hash_with_state((state + __PERL_HASH_STATE_BYTES),(U8*)(str),(len)) \
99 : UNLIKELY(len > (STRLEN) I32_MAX) \
100 ? Perl_croak_nocontext("Sorry, hash keys must be smaller than 2**31 bytes") \
101 : __PERL_HASH_WITH_STATE((state),(str),(len)))
106 U32 S_perl_hash_with_seed(const U8 * const seed, const U8 * const str, const STRLEN len)
108 U8 state[_PERL_HASH_STATE_BYTES];
109 _PERL_HASH_SEED_STATE(seed,state);
110 return _PERL_HASH_WITH_STATE(state,str,len);
113 #define PERL_HASH_WITH_SEED(seed,hash,str,len) \
114 (hash) = S_perl_hash_with_seed(seed,str,len)
115 #define PERL_HASH_WITH_STATE(state,hash,str,len) \
116 (hash) = _PERL_HASH_WITH_STATE((state),(U8*)(str),(len))
117 #define PERL_HASH_SEED_STATE(seed,state) _PERL_HASH_SEED_STATE(seed,state)
118 #define PERL_HASH_SEED_BYTES _PERL_HASH_SEED_BYTES
119 #define PERL_HASH_STATE_BYTES _PERL_HASH_STATE_BYTES
120 #define PERL_HASH_FUNC _PERL_HASH_FUNC
122 #ifdef PERL_USE_SINGLE_CHAR_HASH_CACHE
123 #define PERL_HASH(state,str,len) \
124 (hash) = ((len) < 2 ? ( (len) == 0 ? PL_hash_chars[256] : PL_hash_chars[(U8)(str)[0]] ) \
125 : _PERL_HASH_WITH_STATE(PL_hash_state,(U8*)(str),(len)))
127 #define PERL_HASH(hash,str,len) \
128 PERL_HASH_WITH_STATE(PL_hash_state,hash,(U8*)(str),(len))
131 /* Setup the hash seed, either we do things dynamically at start up,
132 * including reading from the environment, or we randomly setup the
133 * seed. The seed will be passed into the PERL_HASH_SEED_STATE() function
134 * defined for the configuration defined for this perl, which will then
135 * initialze whatever state it might need later in hashing. */
137 #ifndef PERL_HASH_SEED
138 # if defined(USE_HASH_SEED)
139 # define PERL_HASH_SEED PL_hash_seed
141 /* this is a 512 bit seed, which should be more than enough for the
142 * configuration of any of our hash functions (with or without sbox).
143 * If you actually use a hard coded seed, you are strongly encouraged
144 * to replace this with something else of the correct length
145 * for the hash function you are using (24-32 bytes depending on build
146 * options). Repeat, you are *STRONGLY* encouraged not to use the value
149 # define PERL_HASH_SEED \
150 ((const U8 *)"A long string of pseudorandomly " \
151 "chosen bytes for hashing in Perl")
155 /* legacy - only mod_perl should be doing this. */
156 #ifdef PERL_HASH_INTERNAL_ACCESS
157 #define PERL_HASH_INTERNAL(hash,str,len) PERL_HASH(hash,str,len)
160 /* This is SipHash by Jean-Philippe Aumasson and Daniel J. Bernstein.
161 * The authors claim it is relatively secure compared to the alternatives
162 * and that performance wise it is a suitable hash for languages like Perl.
165 * https://www.131002.net/siphash/
167 * This implementation seems to perform slightly slower than one-at-a-time for
168 * short keys, but degrades slower for longer keys. Murmur Hash outperforms it
169 * regardless of keys size.
178 v0 += v1; v1=ROTL64(v1,13); v1 ^= v0; v0=ROTL64(v0,32); \
179 v2 += v3; v3=ROTL64(v3,16); v3 ^= v2; \
180 v0 += v3; v3=ROTL64(v3,21); v3 ^= v0; \
181 v2 += v1; v1=ROTL64(v1,17); v1 ^= v2; v2=ROTL64(v2,32); \
184 #define SIPHASH_SEED_STATE(key,v0,v1,v2,v3) \
186 v0 = v2 = U8TO64_LE(key + 0); \
187 v1 = v3 = U8TO64_LE(key + 8); \
188 /* "somepseudorandomlygeneratedbytes" */ \
189 v0 ^= 0x736f6d6570736575ull; \
190 v1 ^= 0x646f72616e646f6dull; \
191 v2 ^= 0x6c7967656e657261ull; \
192 v3 ^= 0x7465646279746573ull; \
196 void S_perl_siphash_seed_state(const unsigned char * const seed_buf, unsigned char * state_buf) {
197 U64 *v= (U64*) state_buf;
198 SIPHASH_SEED_STATE(seed_buf, v[0],v[1],v[2],v[3]);
201 #define PERL_SIPHASH_FNC(FNC,SIP_ROUNDS,SIP_FINAL_ROUNDS) \
202 PERL_STATIC_INLINE U32 \
204 (const unsigned char * const state, const unsigned char *in, const STRLEN inlen) \
206 const int left = inlen & 7; \
207 const U8 *end = in + inlen - left; \
209 U64 b = ( ( U64 )(inlen) ) << 56; \
211 U64 v0 = U8TO64_LE(state); \
212 U64 v1 = U8TO64_LE(state+8); \
213 U64 v2 = U8TO64_LE(state+16); \
214 U64 v3 = U8TO64_LE(state+24); \
216 for ( ; in != end; in += 8 ) \
218 m = U8TO64_LE( in ); \
228 case 7: b |= ( ( U64 )in[ 6] ) << 48; \
229 case 6: b |= ( ( U64 )in[ 5] ) << 40; \
230 case 5: b |= ( ( U64 )in[ 4] ) << 32; \
231 case 4: b |= ( ( U64 )in[ 3] ) << 24; \
232 case 3: b |= ( ( U64 )in[ 2] ) << 16; \
233 case 2: b |= ( ( U64 )in[ 1] ) << 8; \
234 case 1: b |= ( ( U64 )in[ 0] ); break; \
248 b = v0 ^ v1 ^ v2 ^ v3; \
249 return (U32)(b & U32_MAX); \
252 PERL_STATIC_INLINE U32 \
253 FNC (const unsigned char * const seed, const unsigned char *in, const STRLEN inlen) \
256 SIPHASH_SEED_STATE(seed,state[0],state[1],state[2],state[3]); \
257 return FNC ## _with_state((U8*)state,in,inlen); \
262 S_perl_hash_siphash_1_3
264 ,SIPROUND;SIPROUND;SIPROUND;
268 S_perl_hash_siphash_2_4
270 ,SIPROUND;SIPROUND;SIPROUND;SIPROUND;
272 #endif /* defined(CAN64BITHASH) */
275 #endif /*compile once*/
278 * ex: set ts=8 sts=4 sw=4 et: