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 "STADTX"
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"
75 /* Some siphash static functions are needed by XS::APItest even when
76 siphash isn't the current hash. For SipHash builds this needs to
77 be before the S_perl_hash_with_seed() definition.
79 #include "perl_siphash.h"
81 #if PERL_HASH_USE_SBOX32_ALSO != 1
82 # define _PERL_HASH_FUNC __PERL_HASH_FUNC
83 # define _PERL_HASH_SEED_BYTES __PERL_HASH_SEED_BYTES
84 # define _PERL_HASH_STATE_BYTES __PERL_HASH_STATE_BYTES
85 # define _PERL_HASH_SEED_STATE(seed,state) __PERL_HASH_SEED_STATE(seed,state)
86 # define _PERL_HASH_WITH_STATE(state,str,len) __PERL_HASH_WITH_STATE(state,str,len)
89 #define _PERL_HASH_FUNC "SBOX32_WITH_" __PERL_HASH_FUNC
91 #define _PERL_HASH_SEED_BYTES ( __PERL_HASH_SEED_BYTES + (int)( 3 * sizeof(U32) ) )
93 #define _PERL_HASH_STATE_BYTES \
94 ( __PERL_HASH_STATE_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) )
96 #define _PERL_HASH_SEED_STATE(seed,state) STMT_START { \
97 __PERL_HASH_SEED_STATE(seed,state); \
98 sbox32_seed_state96(seed + __PERL_HASH_SEED_BYTES, state + __PERL_HASH_STATE_BYTES); \
101 #define _PERL_HASH_WITH_STATE(state,str,len) \
102 (LIKELY(len <= SBOX32_MAX_LEN) \
103 ? sbox32_hash_with_state((state + __PERL_HASH_STATE_BYTES),(U8*)(str),(len)) \
104 : __PERL_HASH_WITH_STATE((state),(str),(len)))
109 U32 S_perl_hash_with_seed(const U8 * const seed, const U8 * const str, const STRLEN len)
111 U8 state[_PERL_HASH_STATE_BYTES];
112 _PERL_HASH_SEED_STATE(seed,state);
113 return _PERL_HASH_WITH_STATE(state,str,len);
116 #define PERL_HASH_WITH_SEED(seed,hash,str,len) \
117 (hash) = S_perl_hash_with_seed((const U8 *) seed, (const U8 *) str,len)
118 #define PERL_HASH_WITH_STATE(state,hash,str,len) \
119 (hash) = _PERL_HASH_WITH_STATE((state),(U8*)(str),(len))
120 #define PERL_HASH_SEED_STATE(seed,state) _PERL_HASH_SEED_STATE(seed,state)
121 #define PERL_HASH_SEED_BYTES _PERL_HASH_SEED_BYTES
122 #define PERL_HASH_STATE_BYTES _PERL_HASH_STATE_BYTES
123 #define PERL_HASH_FUNC _PERL_HASH_FUNC
125 #ifdef PERL_USE_SINGLE_CHAR_HASH_CACHE
126 #define PERL_HASH(state,str,len) \
127 (hash) = ((len) < 2 ? ( (len) == 0 ? PL_hash_chars[256] : PL_hash_chars[(U8)(str)[0]] ) \
128 : _PERL_HASH_WITH_STATE(PL_hash_state,(U8*)(str),(len)))
130 #define PERL_HASH(hash,str,len) \
131 PERL_HASH_WITH_STATE(PL_hash_state,hash,(U8*)(str),(len))
134 /* Setup the hash seed, either we do things dynamically at start up,
135 * including reading from the environment, or we randomly setup the
136 * seed. The seed will be passed into the PERL_HASH_SEED_STATE() function
137 * defined for the configuration defined for this perl, which will then
138 * initialize whatever state it might need later in hashing. */
140 #ifndef PERL_HASH_SEED
141 # if defined(USE_HASH_SEED)
142 # define PERL_HASH_SEED PL_hash_seed
144 /* this is a 512 bit seed, which should be more than enough for the
145 * configuration of any of our hash functions (with or without sbox).
146 * If you actually use a hard coded seed, you are strongly encouraged
147 * to replace this with something else of the correct length
148 * for the hash function you are using (24-32 bytes depending on build
149 * options). Repeat, you are *STRONGLY* encouraged not to use the value
152 # define PERL_HASH_SEED \
153 ((const U8 *)"A long string of pseudorandomly " \
154 "chosen bytes for hashing in Perl")
158 /* legacy - only mod_perl should be doing this. */
159 #ifdef PERL_HASH_INTERNAL_ACCESS
160 #define PERL_HASH_INTERNAL(hash,str,len) PERL_HASH(hash,str,len)
163 #endif /*compile once*/
166 * ex: set ts=8 sts=4 sw=4 et: