]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
3c2bdd7d | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
8573be06 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 RE |
8 | */ |
9 | ||
d02b48c6 RE |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include <stdlib.h> | |
ec577822 BM |
13 | #include <openssl/crypto.h> |
14 | #include <openssl/lhash.h> | |
fe1128dc | 15 | #include <openssl/err.h> |
25f2138b DMSP |
16 | #include "crypto/ctype.h" |
17 | #include "crypto/lhash.h" | |
706457b7 | 18 | #include "lhash_local.h" |
739a1eb1 | 19 | |
4ce8bebc MC |
20 | /* |
21 | * A hashing implementation that appears to be based on the linear hashing | |
c2969ff6 | 22 | * algorithm: |
4ce8bebc MC |
23 | * https://en.wikipedia.org/wiki/Linear_hashing |
24 | * | |
25 | * Litwin, Witold (1980), "Linear hashing: A new tool for file and table | |
b4d0fa49 | 26 | * addressing", Proc. 6th Conference on Very Large Databases: 212-223 |
c2969ff6 | 27 | * https://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf |
4ce8bebc | 28 | * |
c2969ff6 | 29 | * From the Wikipedia article "Linear hashing is used in the BDB Berkeley |
4ce8bebc MC |
30 | * database system, which in turn is used by many software systems such as |
31 | * OpenLDAP, using a C implementation derived from the CACM article and first | |
32 | * published on the Usenet in 1988 by Esmond Pitt." | |
33 | * | |
34 | * The CACM paper is available here: | |
35 | * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf | |
36 | */ | |
d02b48c6 | 37 | |
0f113f3e MC |
38 | #undef MIN_NODES |
39 | #define MIN_NODES 16 | |
40 | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ | |
41 | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ | |
d02b48c6 | 42 | |
0a1d3a81 | 43 | static int expand(OPENSSL_LHASH *lh); |
739a1eb1 RS |
44 | static void contract(OPENSSL_LHASH *lh); |
45 | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash); | |
d02b48c6 | 46 | |
739a1eb1 | 47 | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
0f113f3e | 48 | { |
739a1eb1 | 49 | OPENSSL_LHASH *ret; |
0f113f3e | 50 | |
fe1128dc RS |
51 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { |
52 | /* | |
53 | * Do not set the error code, because the ERR code uses LHASH | |
54 | * and we want to avoid possible endless error loop. | |
9311d0c4 | 55 | * ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
fe1128dc | 56 | */ |
be606c01 | 57 | return NULL; |
fe1128dc | 58 | } |
64b25758 | 59 | if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) |
be606c01 | 60 | goto err; |
739a1eb1 RS |
61 | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
62 | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); | |
0f113f3e MC |
63 | ret->num_nodes = MIN_NODES / 2; |
64 | ret->num_alloc_nodes = MIN_NODES; | |
0f113f3e MC |
65 | ret->pmax = MIN_NODES / 2; |
66 | ret->up_load = UP_LOAD; | |
67 | ret->down_load = DOWN_LOAD; | |
2e8b5d75 | 68 | return ret; |
64b25758 | 69 | |
be606c01 RS |
70 | err: |
71 | OPENSSL_free(ret->b); | |
0f113f3e | 72 | OPENSSL_free(ret); |
be606c01 | 73 | return NULL; |
0f113f3e | 74 | } |
d02b48c6 | 75 | |
739a1eb1 | 76 | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
1bdbdaff P |
77 | { |
78 | if (lh == NULL) | |
79 | return; | |
80 | ||
81 | OPENSSL_LH_flush(lh); | |
82 | OPENSSL_free(lh->b); | |
83 | OPENSSL_free(lh); | |
84 | } | |
85 | ||
86 | void OPENSSL_LH_flush(OPENSSL_LHASH *lh) | |
0f113f3e MC |
87 | { |
88 | unsigned int i; | |
739a1eb1 | 89 | OPENSSL_LH_NODE *n, *nn; |
0f113f3e MC |
90 | |
91 | if (lh == NULL) | |
92 | return; | |
93 | ||
94 | for (i = 0; i < lh->num_nodes; i++) { | |
95 | n = lh->b[i]; | |
96 | while (n != NULL) { | |
97 | nn = n->next; | |
98 | OPENSSL_free(n); | |
99 | n = nn; | |
100 | } | |
bcb5d421 | 101 | lh->b[i] = NULL; |
0f113f3e | 102 | } |
0f113f3e | 103 | } |
d02b48c6 | 104 | |
739a1eb1 | 105 | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
0f113f3e MC |
106 | { |
107 | unsigned long hash; | |
739a1eb1 | 108 | OPENSSL_LH_NODE *nn, **rn; |
0f113f3e | 109 | void *ret; |
152d2646 | 110 | |
0f113f3e | 111 | lh->error = 0; |
152d2646 | 112 | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
113 | return NULL; /* 'lh->error++' already done in 'expand' */ | |
114 | ||
0f113f3e MC |
115 | rn = getrn(lh, data, &hash); |
116 | ||
117 | if (*rn == NULL) { | |
b4faea50 | 118 | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
0f113f3e | 119 | lh->error++; |
2e8b5d75 | 120 | return NULL; |
0f113f3e MC |
121 | } |
122 | nn->data = data; | |
123 | nn->next = NULL; | |
0f113f3e | 124 | nn->hash = hash; |
0f113f3e MC |
125 | *rn = nn; |
126 | ret = NULL; | |
127 | lh->num_insert++; | |
128 | lh->num_items++; | |
129 | } else { /* replace same key */ | |
0f113f3e MC |
130 | ret = (*rn)->data; |
131 | (*rn)->data = data; | |
132 | lh->num_replace++; | |
133 | } | |
2e8b5d75 | 134 | return ret; |
0f113f3e | 135 | } |
d02b48c6 | 136 | |
739a1eb1 | 137 | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
0f113f3e MC |
138 | { |
139 | unsigned long hash; | |
739a1eb1 | 140 | OPENSSL_LH_NODE *nn, **rn; |
0f113f3e MC |
141 | void *ret; |
142 | ||
143 | lh->error = 0; | |
144 | rn = getrn(lh, data, &hash); | |
145 | ||
146 | if (*rn == NULL) { | |
147 | lh->num_no_delete++; | |
2e8b5d75 | 148 | return NULL; |
0f113f3e MC |
149 | } else { |
150 | nn = *rn; | |
151 | *rn = nn->next; | |
152 | ret = nn->data; | |
153 | OPENSSL_free(nn); | |
154 | lh->num_delete++; | |
155 | } | |
156 | ||
157 | lh->num_items--; | |
158 | if ((lh->num_nodes > MIN_NODES) && | |
159 | (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) | |
160 | contract(lh); | |
161 | ||
2e8b5d75 | 162 | return ret; |
0f113f3e | 163 | } |
d02b48c6 | 164 | |
739a1eb1 | 165 | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
0f113f3e MC |
166 | { |
167 | unsigned long hash; | |
739a1eb1 | 168 | OPENSSL_LH_NODE **rn; |
0f113f3e MC |
169 | void *ret; |
170 | ||
cab76c0f AP |
171 | tsan_store((TSAN_QUALIFIER int *)&lh->error, 0); |
172 | ||
0f113f3e MC |
173 | rn = getrn(lh, data, &hash); |
174 | ||
175 | if (*rn == NULL) { | |
cab76c0f | 176 | tsan_counter(&lh->num_retrieve_miss); |
be606c01 | 177 | return NULL; |
0f113f3e MC |
178 | } else { |
179 | ret = (*rn)->data; | |
cab76c0f | 180 | tsan_counter(&lh->num_retrieve); |
0f113f3e | 181 | } |
cab76c0f | 182 | |
be606c01 | 183 | return ret; |
0f113f3e | 184 | } |
d02b48c6 | 185 | |
739a1eb1 RS |
186 | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, |
187 | OPENSSL_LH_DOALL_FUNC func, | |
188 | OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) | |
0f113f3e MC |
189 | { |
190 | int i; | |
739a1eb1 | 191 | OPENSSL_LH_NODE *a, *n; |
0f113f3e MC |
192 | |
193 | if (lh == NULL) | |
194 | return; | |
195 | ||
196 | /* | |
197 | * reverse the order so we search from 'top to bottom' We were having | |
198 | * memory leaks otherwise | |
199 | */ | |
200 | for (i = lh->num_nodes - 1; i >= 0; i--) { | |
201 | a = lh->b[i]; | |
202 | while (a != NULL) { | |
0f113f3e MC |
203 | n = a->next; |
204 | if (use_arg) | |
205 | func_arg(a->data, arg); | |
206 | else | |
207 | func(a->data); | |
208 | a = n; | |
209 | } | |
210 | } | |
211 | } | |
d02b48c6 | 212 | |
739a1eb1 | 213 | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
0f113f3e | 214 | { |
739a1eb1 | 215 | doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL); |
0f113f3e | 216 | } |
18602745 | 217 | |
739a1eb1 | 218 | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
0f113f3e | 219 | { |
739a1eb1 | 220 | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg); |
0f113f3e | 221 | } |
18602745 | 222 | |
0a1d3a81 | 223 | static int expand(OPENSSL_LHASH *lh) |
0f113f3e | 224 | { |
739a1eb1 | 225 | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
4ce8bebc MC |
226 | unsigned int p, pmax, nni, j; |
227 | unsigned long hash; | |
228 | ||
229 | nni = lh->num_alloc_nodes; | |
230 | p = lh->p; | |
231 | pmax = lh->pmax; | |
232 | if (p + 1 >= pmax) { | |
233 | j = nni * 2; | |
234 | n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); | |
235 | if (n == NULL) { | |
236 | lh->error++; | |
237 | return 0; | |
238 | } | |
239 | lh->b = n; | |
240 | memset(n + nni, 0, sizeof(*n) * (j - nni)); | |
241 | lh->pmax = nni; | |
242 | lh->num_alloc_nodes = j; | |
243 | lh->num_expand_reallocs++; | |
244 | lh->p = 0; | |
245 | } else { | |
246 | lh->p++; | |
247 | } | |
0f113f3e MC |
248 | |
249 | lh->num_nodes++; | |
250 | lh->num_expands++; | |
0f113f3e | 251 | n1 = &(lh->b[p]); |
4ce8bebc | 252 | n2 = &(lh->b[p + pmax]); |
739a1eb1 | 253 | *n2 = NULL; |
0f113f3e MC |
254 | |
255 | for (np = *n1; np != NULL;) { | |
0f113f3e | 256 | hash = np->hash; |
0f113f3e MC |
257 | if ((hash % nni) != p) { /* move it */ |
258 | *n1 = (*n1)->next; | |
259 | np->next = *n2; | |
260 | *n2 = np; | |
261 | } else | |
262 | n1 = &((*n1)->next); | |
263 | np = *n1; | |
264 | } | |
265 | ||
152d2646 | 266 | return 1; |
0f113f3e | 267 | } |
d02b48c6 | 268 | |
739a1eb1 | 269 | static void contract(OPENSSL_LHASH *lh) |
0f113f3e | 270 | { |
739a1eb1 | 271 | OPENSSL_LH_NODE **n, *n1, *np; |
0f113f3e MC |
272 | |
273 | np = lh->b[lh->p + lh->pmax - 1]; | |
274 | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ | |
275 | if (lh->p == 0) { | |
b196e7d9 | 276 | n = OPENSSL_realloc(lh->b, |
739a1eb1 | 277 | (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); |
0f113f3e | 278 | if (n == NULL) { |
b196e7d9 | 279 | /* fputs("realloc error in lhash",stderr); */ |
0f113f3e MC |
280 | lh->error++; |
281 | return; | |
282 | } | |
283 | lh->num_contract_reallocs++; | |
284 | lh->num_alloc_nodes /= 2; | |
285 | lh->pmax /= 2; | |
286 | lh->p = lh->pmax - 1; | |
287 | lh->b = n; | |
288 | } else | |
289 | lh->p--; | |
290 | ||
291 | lh->num_nodes--; | |
292 | lh->num_contracts++; | |
293 | ||
294 | n1 = lh->b[(int)lh->p]; | |
295 | if (n1 == NULL) | |
296 | lh->b[(int)lh->p] = np; | |
297 | else { | |
298 | while (n1->next != NULL) | |
299 | n1 = n1->next; | |
300 | n1->next = np; | |
301 | } | |
302 | } | |
d02b48c6 | 303 | |
739a1eb1 RS |
304 | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
305 | const void *data, unsigned long *rhash) | |
0f113f3e | 306 | { |
739a1eb1 | 307 | OPENSSL_LH_NODE **ret, *n1; |
0f113f3e | 308 | unsigned long hash, nn; |
739a1eb1 | 309 | OPENSSL_LH_COMPFUNC cf; |
0f113f3e MC |
310 | |
311 | hash = (*(lh->hash)) (data); | |
cab76c0f | 312 | tsan_counter(&lh->num_hash_calls); |
0f113f3e MC |
313 | *rhash = hash; |
314 | ||
315 | nn = hash % lh->pmax; | |
316 | if (nn < lh->p) | |
317 | nn = hash % lh->num_alloc_nodes; | |
318 | ||
319 | cf = lh->comp; | |
320 | ret = &(lh->b[(int)nn]); | |
321 | for (n1 = *ret; n1 != NULL; n1 = n1->next) { | |
cab76c0f | 322 | tsan_counter(&lh->num_hash_comps); |
0f113f3e MC |
323 | if (n1->hash != hash) { |
324 | ret = &(n1->next); | |
325 | continue; | |
326 | } | |
cab76c0f | 327 | tsan_counter(&lh->num_comp_calls); |
0f113f3e MC |
328 | if (cf(n1->data, data) == 0) |
329 | break; | |
330 | ret = &(n1->next); | |
331 | } | |
2e8b5d75 | 332 | return ret; |
0f113f3e MC |
333 | } |
334 | ||
335 | /* | |
336 | * The following hash seems to work very well on normal text strings no | |
337 | * collisions on /usr/dict/words and it distributes on %2^n quite well, not | |
338 | * as good as MD5, but still good. | |
d02b48c6 | 339 | */ |
739a1eb1 | 340 | unsigned long OPENSSL_LH_strhash(const char *c) |
0f113f3e MC |
341 | { |
342 | unsigned long ret = 0; | |
343 | long n; | |
344 | unsigned long v; | |
345 | int r; | |
346 | ||
347 | if ((c == NULL) || (*c == '\0')) | |
2e8b5d75 | 348 | return ret; |
d02b48c6 | 349 | |
0f113f3e MC |
350 | n = 0x100; |
351 | while (*c) { | |
352 | v = n | (*c); | |
353 | n += 0x100; | |
354 | r = (int)((v >> 2) ^ v) & 0x0f; | |
355 | ret = (ret << r) | (ret >> (32 - r)); | |
356 | ret &= 0xFFFFFFFFL; | |
357 | ret ^= v * v; | |
358 | c++; | |
359 | } | |
2e8b5d75 | 360 | return (ret >> 16) ^ ret; |
0f113f3e | 361 | } |
d02b48c6 | 362 | |
285aa80e | 363 | unsigned long ossl_lh_strcasehash(const char *c) |
fc196a5e P |
364 | { |
365 | unsigned long ret = 0; | |
366 | long n; | |
367 | unsigned long v; | |
368 | int r; | |
369 | ||
370 | if (c == NULL || *c == '\0') | |
371 | return ret; | |
372 | ||
373 | for (n = 0x100; *c != '\0'; n += 0x100) { | |
374 | v = n | ossl_tolower(*c); | |
375 | r = (int)((v >> 2) ^ v) & 0x0f; | |
376 | ret = (ret << r) | (ret >> (32 - r)); | |
377 | ret &= 0xFFFFFFFFL; | |
378 | ret ^= v * v; | |
379 | c++; | |
380 | } | |
381 | return (ret >> 16) ^ ret; | |
382 | } | |
383 | ||
739a1eb1 | 384 | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
0f113f3e MC |
385 | { |
386 | return lh ? lh->num_items : 0; | |
387 | } | |
e6b5c341 | 388 | |
739a1eb1 | 389 | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
e6b5c341 DSH |
390 | { |
391 | return lh->down_load; | |
392 | } | |
393 | ||
739a1eb1 | 394 | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
e6b5c341 DSH |
395 | { |
396 | lh->down_load = down_load; | |
397 | } | |
398 | ||
739a1eb1 | 399 | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
e6b5c341 DSH |
400 | { |
401 | return lh->error; | |
402 | } |