1 /* hash a key
2 *--------------------------------------------------------------------------------------
3 * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
4 * to avoid "algorithmic complexity attacks".
5 *
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())
8 */
9 #ifndef PERL_SEEN_HV_FUNC_H_ /* compile once */
10 #define PERL_SEEN_HV_FUNC_H_
11 #include "hv_macro.h"
12
13 #if !( 0 \
14 || defined(PERL_HASH_FUNC_SIPHASH) \
15 || defined(PERL_HASH_FUNC_SIPHASH13) \
16 || defined(PERL_HASH_FUNC_ZAPHOD32) \
17 )
18 # ifdef CAN64BITHASH
19 # define PERL_HASH_FUNC_SIPHASH13
20 # else
21 # define PERL_HASH_FUNC_ZAPHOD32
22 # endif
23 #endif
24
25 #ifndef PERL_HASH_USE_SBOX32_ALSO
26 # if defined(PERL_HASH_USE_SBOX32) || !defined(PERL_HASH_NO_SBOX32)
27 # define PERL_HASH_USE_SBOX32_ALSO 1
28 # else
29 # define PERL_HASH_USE_SBOX32_ALSO 0
30 # endif
31 #endif
32
33 #undef PERL_HASH_USE_SBOX32
34 #undef PERL_HASH_NO_SBOX32
35 #if PERL_HASH_USE_SBOX32_ALSO != 0
36 # define PERL_HASH_USE_SBOX32
37 #else
38 # define PERL_HASH_NO_SBOX32
39 #endif
40
41 #ifndef SBOX32_MAX_LEN
42 #define SBOX32_MAX_LEN 24
43 #endif
44
45 /* this must be after the SBOX32_MAX_LEN define */
46 #include "sbox32_hash.h"
47
48 #if defined(PERL_HASH_FUNC_SIPHASH)
49 # define PERL_HASH_FUNC_DEFINE "PERL_HASH_FUNC_SIPHASH"
50 # define PVT__PERL_HASH_FUNC "SIPHASH_2_4"
51 # define PVT__PERL_HASH_WORD_TYPE U64
52 # define PVT__PERL_HASH_WORD_SIZE sizeof(PVT__PERL_HASH_WORD_TYPE)
53 # define PVT__PERL_HASH_SEED_BYTES (PVT__PERL_HASH_WORD_SIZE * 2)
54 # define PVT__PERL_HASH_STATE_BYTES (PVT__PERL_HASH_WORD_SIZE * 4)
55 # define PVT__PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state)
56 # define PVT__PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_2_4_with_state((state),(U8*)(str),(len))
57 #elif defined(PERL_HASH_FUNC_SIPHASH13)
58 # define PERL_HASH_FUNC_DEFINE "PERL_HASH_FUNC_SIPHASH13"
59 # define PVT__PERL_HASH_FUNC "SIPHASH_1_3"
60 # define PVT__PERL_HASH_WORD_TYPE U64
61 # define PVT__PERL_HASH_WORD_SIZE sizeof(PVT__PERL_HASH_WORD_TYPE)
62 # define PVT__PERL_HASH_SEED_BYTES (PVT__PERL_HASH_WORD_SIZE * 2)
63 # define PVT__PERL_HASH_STATE_BYTES (PVT__PERL_HASH_WORD_SIZE * 4)
64 # define PVT__PERL_HASH_SEED_STATE(seed,state) S_perl_siphash_seed_state(seed,state)
65 # define PVT__PERL_HASH_WITH_STATE(state,str,len) S_perl_hash_siphash_1_3_with_state((state),(const U8*)(str),(len))
66 #elif defined(PERL_HASH_FUNC_ZAPHOD32)
67 # define PERL_HASH_FUNC_DEFINE "PERL_HASH_FUNC_ZAPHOD32"
68 # define PVT__PERL_HASH_FUNC "ZAPHOD32"
69 # define PVT__PERL_HASH_WORD_TYPE U32
70 # define PVT__PERL_HASH_WORD_SIZE sizeof(PVT__PERL_HASH_WORD_TYPE)
71 # define PVT__PERL_HASH_SEED_BYTES (PVT__PERL_HASH_WORD_SIZE * 3)
72 # define PVT__PERL_HASH_STATE_BYTES (PVT__PERL_HASH_WORD_SIZE * 3)
73 # define PVT__PERL_HASH_SEED_STATE(seed,state) zaphod32_seed_state(seed,state)
74 # define PVT__PERL_HASH_WITH_STATE(state,str,len) (U32)zaphod32_hash_with_state((state),(U8*)(str),(len))
75 # include "zaphod32_hash.h"
76 #endif
77
78 #ifndef PVT__PERL_HASH_WITH_STATE
79 #error "No hash function defined!"
80 #endif
81 #ifndef PVT__PERL_HASH_SEED_BYTES
82 #error "PVT__PERL_HASH_SEED_BYTES not defined"
83 #endif
84 #ifndef PVT__PERL_HASH_FUNC
85 #error "PVT__PERL_HASH_FUNC not defined"
86 #endif
87
88 /* Some siphash static functions are needed by XS::APItest even when
89 siphash isn't the current hash. For SipHash builds this needs to
90 be before the S_perl_hash_with_seed() definition.
91 */
92 #include "perl_siphash.h"
93
94 #define PVT__PERL_HASH_SEED_roundup(x, y) ( ( ( (x) + ( (y) - 1 ) ) / (y) ) * (y) )
95 #define PVT_PERL_HASH_SEED_roundup(x) PVT__PERL_HASH_SEED_roundup(x,PVT__PERL_HASH_WORD_SIZE)
96
97 #define PL_hash_seed ((U8 *)PL_hash_seed_w)
98 #define PL_hash_state ((U8 *)PL_hash_state_w)
99
100 #if PERL_HASH_USE_SBOX32_ALSO == 0
101 # define PVT_PERL_HASH_FUNC PVT__PERL_HASH_FUNC
102 # define PVT_PERL_HASH_SEED_BYTES PVT__PERL_HASH_SEED_BYTES
103 # define PVT_PERL_HASH_STATE_BYTES PVT__PERL_HASH_STATE_BYTES
104 # define PVT_PERL_HASH_SEED_STATE(seed,state) PVT__PERL_HASH_SEED_STATE(seed,state)
105 # define PVT_PERL_HASH_WITH_STATE(state,str,len) PVT__PERL_HASH_WITH_STATE(state,str,len)
106 #else
107
108 #define PVT_PERL_HASH_FUNC "SBOX32_WITH_" PVT__PERL_HASH_FUNC
109 /* note the 4 in the below code comes from the fact the seed to initialize the SBOX is 128 bits */
110 #define PVT_PERL_HASH_SEED_BYTES ( PVT__PERL_HASH_SEED_BYTES + (int)( 4 * sizeof(U32)) )
111
112 #define PVT_PERL_HASH_STATE_BYTES \
113 ( PVT__PERL_HASH_STATE_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) )
114
115 #define PVT_PERL_HASH_SEED_STATE(seed,state) STMT_START { \
116 PVT__PERL_HASH_SEED_STATE(seed,state); \
117 sbox32_seed_state128(seed + PVT__PERL_HASH_SEED_BYTES, state + PVT__PERL_HASH_STATE_BYTES); \
118 } STMT_END
119
120 #define PVT_PERL_HASH_WITH_STATE(state,str,len) \
121 (LIKELY(len <= SBOX32_MAX_LEN) \
122 ? sbox32_hash_with_state((state + PVT__PERL_HASH_STATE_BYTES),(const U8*)(str),(len)) \
123 : PVT__PERL_HASH_WITH_STATE((state),(str),(len)))
124
125 #endif
126
127 #define PERL_HASH_WITH_SEED(seed,hash,str,len) \
128 (hash) = S_perl_hash_with_seed((const U8 *) seed, (const U8 *) str,len)
129 #define PERL_HASH_WITH_STATE(state,hash,str,len) \
130 (hash) = PVT_PERL_HASH_WITH_STATE((state),(const U8*)(str),(len))
131
132 #define PERL_HASH_SEED_STATE(seed,state) PVT_PERL_HASH_SEED_STATE(seed,state)
133 #define PERL_HASH_SEED_BYTES PVT_PERL_HASH_SEED_roundup(PVT_PERL_HASH_SEED_BYTES)
134 #define PERL_HASH_STATE_BYTES PVT_PERL_HASH_SEED_roundup(PVT_PERL_HASH_STATE_BYTES)
135 #define PERL_HASH_FUNC PVT_PERL_HASH_FUNC
136
137 #define PERL_HASH_SEED_WORDS (PERL_HASH_SEED_BYTES/PVT__PERL_HASH_WORD_SIZE)
138 #define PERL_HASH_STATE_WORDS (PERL_HASH_STATE_BYTES/PVT__PERL_HASH_WORD_SIZE)
139
140 #ifdef PERL_USE_SINGLE_CHAR_HASH_CACHE
141 #define PERL_HASH(state,str,len) \
142 (hash) = ((len) < 2 ? ( (len) == 0 ? PL_hash_chars[256] : PL_hash_chars[(U8)(str)[0]] ) \
143 : PVT_PERL_HASH_WITH_STATE(PL_hash_state,(U8*)(str),(len)))
144 #else
145 #define PERL_HASH(hash,str,len) \
146 PERL_HASH_WITH_STATE(PL_hash_state,hash,(U8*)(str),(len))
147 #endif
148
149 /* Setup the hash seed, either we do things dynamically at start up,
150 * including reading from the environment, or we randomly setup the
151 * seed. The seed will be passed into the PERL_HASH_SEED_STATE() function
152 * defined for the configuration defined for this perl, which will then
153 * initialize whatever state it might need later in hashing. */
154
155 #ifndef PERL_HASH_SEED
156 # if defined(USE_HASH_SEED)
157 # define PERL_HASH_SEED PL_hash_seed
158 # else
159 /* this is a 512 bit seed, which should be more than enough for the
160 * configuration of any of our hash functions (with or without sbox).
161 * If you actually use a hard coded seed, you are strongly encouraged
162 * to replace this with something else of the correct length
163 * for the hash function you are using (24-32 bytes depending on build
164 * options). Repeat, you are *STRONGLY* encouraged not to use the value
165 * provided here.
166 */
167 # define PERL_HASH_SEED \
168 ((const U8 *)"A long string of pseudorandomly " \
169 "chosen bytes for hashing in Perl")
170 # endif
171 #endif
172
173 /* legacy - only mod_perl should be doing this. */
174 #ifdef PERL_HASH_INTERNAL_ACCESS
175 #define PERL_HASH_INTERNAL(hash,str,len) PERL_HASH(hash,str,len)
176 #endif
177
178 PERL_STATIC_INLINE U32
S_perl_hash_with_seed(const U8 * seed,const U8 * str,STRLEN len)179 S_perl_hash_with_seed(const U8 * seed, const U8 *str, STRLEN len) {
180 PVT__PERL_HASH_WORD_TYPE state[PERL_HASH_STATE_WORDS];
181 PVT_PERL_HASH_SEED_STATE(seed,(U8*)state);
182 return PVT_PERL_HASH_WITH_STATE((U8*)state,str,len);
183 }
184
185 #endif /*compile once*/
186
187 /*
188 * ex: set ts=8 sts=4 sw=4 et:
189 */
190