Commit | Line | Data |
---|---|---|
4d3a042d YO |
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 | |
4d3a042d YO |
7 | * (see also perl.c:perl_parse() and S_init_tls_and_interp() and util.c:get_hash_seed()) |
8 | */ | |
b41deab9 KW |
9 | #ifndef PERL_SEEN_HV_FUNC_H_ /* compile once */ |
10 | #define PERL_SEEN_HV_FUNC_H_ | |
1c4b2386 | 11 | #include "hv_macro.h" |
9d5e3f1a YO |
12 | |
13 | #if !( 0 \ | |
14 | || defined(PERL_HASH_FUNC_SIPHASH) \ | |
15 | || defined(PERL_HASH_FUNC_SIPHASH13) \ | |
9d5e3f1a YO |
16 | || defined(PERL_HASH_FUNC_ZAPHOD32) \ |
17 | ) | |
18 | # ifdef CAN64BITHASH | |
8bc8b6db | 19 | # define PERL_HASH_FUNC_SIPHASH13 |
9d5e3f1a YO |
20 | # else |
21 | # define PERL_HASH_FUNC_ZAPHOD32 | |
22 | # endif | |
23 | #endif | |
24 | ||
25 | #ifndef PERL_HASH_USE_SBOX32_ALSO | |
86d84dd0 YO |
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 | |
9d5e3f1a YO |
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) | |
86d84dd0 | 49 | # define PERL_HASH_FUNC_DEFINE "PERL_HASH_FUNC_SIPHASH" |
58411bc7 YO |
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)) | |
9d5e3f1a | 57 | #elif defined(PERL_HASH_FUNC_SIPHASH13) |
86d84dd0 | 58 | # define PERL_HASH_FUNC_DEFINE "PERL_HASH_FUNC_SIPHASH13" |
58411bc7 YO |
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),(U8*)(str),(len)) | |
9d5e3f1a | 66 | #elif defined(PERL_HASH_FUNC_ZAPHOD32) |
86d84dd0 | 67 | # define PERL_HASH_FUNC_DEFINE "PERL_HASH_FUNC_ZAPHOD32" |
58411bc7 YO |
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)) | |
9d5e3f1a YO |
75 | # include "zaphod32_hash.h" |
76 | #endif | |
77 | ||
58411bc7 | 78 | #ifndef PVT__PERL_HASH_WITH_STATE |
9d5e3f1a YO |
79 | #error "No hash function defined!" |
80 | #endif | |
58411bc7 YO |
81 | #ifndef PVT__PERL_HASH_SEED_BYTES |
82 | #error "PVT__PERL_HASH_SEED_BYTES not defined" | |
9d5e3f1a | 83 | #endif |
58411bc7 YO |
84 | #ifndef PVT__PERL_HASH_FUNC |
85 | #error "PVT__PERL_HASH_FUNC not defined" | |
9d5e3f1a YO |
86 | #endif |
87 | ||
1af90cb1 TC |
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" | |
9d5e3f1a | 93 | |
58411bc7 YO |
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) | |
f43079cb YO |
96 | |
97 | #define PL_hash_seed ((U8 *)PL_hash_seed_w) | |
98 | #define PL_hash_state ((U8 *)PL_hash_state_w) | |
99 | ||
86d84dd0 | 100 | #if PERL_HASH_USE_SBOX32_ALSO == 0 |
58411bc7 YO |
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) | |
9d5e3f1a YO |
106 | #else |
107 | ||
58411bc7 | 108 | #define PVT_PERL_HASH_FUNC "SBOX32_WITH_" PVT__PERL_HASH_FUNC |
8a35994a | 109 | /* note the 4 in the below code comes from the fact the seed to initialize the SBOX is 128 bits */ |
58411bc7 | 110 | #define PVT_PERL_HASH_SEED_BYTES ( PVT__PERL_HASH_SEED_BYTES + (int)( 4 * sizeof(U32)) ) |
9d5e3f1a | 111 | |
58411bc7 YO |
112 | #define PVT_PERL_HASH_STATE_BYTES \ |
113 | ( PVT__PERL_HASH_STATE_BYTES + ( ( 1 + ( 256 * SBOX32_MAX_LEN ) ) * sizeof(U32) ) ) | |
9d5e3f1a | 114 | |
58411bc7 YO |
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); \ | |
9d5e3f1a YO |
118 | } STMT_END |
119 | ||
58411bc7 | 120 | #define PVT_PERL_HASH_WITH_STATE(state,str,len) \ |
b02f3645 | 121 | (LIKELY(len <= SBOX32_MAX_LEN) \ |
58411bc7 YO |
122 | ? sbox32_hash_with_state((state + PVT__PERL_HASH_STATE_BYTES),(U8*)(str),(len)) \ |
123 | : PVT__PERL_HASH_WITH_STATE((state),(str),(len))) | |
9d5e3f1a YO |
124 | |
125 | #endif | |
126 | ||
9d5e3f1a | 127 | #define PERL_HASH_WITH_SEED(seed,hash,str,len) \ |
935d8e72 | 128 | (hash) = S_perl_hash_with_seed((const U8 *) seed, (const U8 *) str,len) |
9d5e3f1a | 129 | #define PERL_HASH_WITH_STATE(state,hash,str,len) \ |
58411bc7 | 130 | (hash) = PVT_PERL_HASH_WITH_STATE((state),(U8*)(str),(len)) |
f43079cb | 131 | |
58411bc7 YO |
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 | |
9d5e3f1a | 136 | |
58411bc7 YO |
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) | |
f43079cb | 139 | |
9d5e3f1a YO |
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]] ) \ | |
58411bc7 | 143 | : PVT_PERL_HASH_WITH_STATE(PL_hash_state,(U8*)(str),(len))) |
9d5e3f1a YO |
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 | |
9af31f94 | 153 | * initialize whatever state it might need later in hashing. */ |
9d5e3f1a YO |
154 | |
155 | #ifndef PERL_HASH_SEED | |
1a237f4f | 156 | # if defined(USE_HASH_SEED) |
9d5e3f1a YO |
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) | |
b716320d YO |
176 | #endif |
177 | ||
d18575f1 YO |
178 | PERL_STATIC_INLINE U32 |
179 | S_perl_hash_with_seed(const U8 * seed, const U8 *str, STRLEN len) { | |
58411bc7 YO |
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); | |
d18575f1 YO |
183 | } |
184 | ||
4d3a042d YO |
185 | #endif /*compile once*/ |
186 | ||
187 | /* | |
4d3a042d YO |
188 | * ex: set ts=8 sts=4 sw=4 et: |
189 | */ |