/src/openssl/crypto/lhash/lhash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <string.h> |
12 | | #include <stdlib.h> |
13 | | #include <openssl/crypto.h> |
14 | | #include <openssl/lhash.h> |
15 | | #include <openssl/err.h> |
16 | | #include "crypto/ctype.h" |
17 | | #include "crypto/lhash.h" |
18 | | #include "lhash_local.h" |
19 | | |
20 | | /* |
21 | | * A hashing implementation that appears to be based on the linear hashing |
22 | | * algorithm: |
23 | | * https://en.wikipedia.org/wiki/Linear_hashing |
24 | | * |
25 | | * Litwin, Witold (1980), "Linear hashing: A new tool for file and table |
26 | | * addressing", Proc. 6th Conference on Very Large Databases: 212-223 |
27 | | * https://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf |
28 | | * |
29 | | * From the Wikipedia article "Linear hashing is used in the BDB Berkeley |
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 | | */ |
37 | | |
38 | | #undef MIN_NODES |
39 | 11.9k | #define MIN_NODES 16 |
40 | 2.68k | #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ |
41 | 2.68k | #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ |
42 | | |
43 | | static int expand(OPENSSL_LHASH *lh); |
44 | | static void contract(OPENSSL_LHASH *lh); |
45 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash); |
46 | | |
47 | | OPENSSL_LHASH *OPENSSL_LH_set_thunks(OPENSSL_LHASH *lh, |
48 | | OPENSSL_LH_HASHFUNCTHUNK hw, |
49 | | OPENSSL_LH_COMPFUNCTHUNK cw, |
50 | | OPENSSL_LH_DOALL_FUNC_THUNK daw, |
51 | | OPENSSL_LH_DOALL_FUNCARG_THUNK daaw) |
52 | 2.68k | { |
53 | | |
54 | 2.68k | if (lh == NULL) |
55 | 0 | return NULL; |
56 | 2.68k | lh->compw = cw; |
57 | 2.68k | lh->hashw = hw; |
58 | 2.68k | lh->daw = daw; |
59 | 2.68k | lh->daaw = daaw; |
60 | 2.68k | return lh; |
61 | 2.68k | } |
62 | | |
63 | | OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
64 | 2.68k | { |
65 | 2.68k | OPENSSL_LHASH *ret; |
66 | | |
67 | 2.68k | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) |
68 | 0 | return NULL; |
69 | 2.68k | if ((ret->b = OPENSSL_calloc(MIN_NODES, sizeof(*ret->b))) == NULL) |
70 | 0 | goto err; |
71 | 2.68k | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
72 | 2.68k | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); |
73 | 2.68k | ret->num_nodes = MIN_NODES / 2; |
74 | 2.68k | ret->num_alloc_nodes = MIN_NODES; |
75 | 2.68k | ret->pmax = MIN_NODES / 2; |
76 | 2.68k | ret->up_load = UP_LOAD; |
77 | 2.68k | ret->down_load = DOWN_LOAD; |
78 | 2.68k | return ret; |
79 | | |
80 | 0 | err: |
81 | 0 | OPENSSL_free(ret->b); |
82 | 0 | OPENSSL_free(ret); |
83 | 0 | return NULL; |
84 | 2.68k | } |
85 | | |
86 | | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
87 | 2.70k | { |
88 | 2.70k | if (lh == NULL) |
89 | 16 | return; |
90 | | |
91 | 2.68k | OPENSSL_LH_flush(lh); |
92 | 2.68k | OPENSSL_free(lh->b); |
93 | 2.68k | OPENSSL_free(lh); |
94 | 2.68k | } |
95 | | |
96 | | void OPENSSL_LH_flush(OPENSSL_LHASH *lh) |
97 | 2.68k | { |
98 | 2.68k | unsigned int i; |
99 | 2.68k | OPENSSL_LH_NODE *n, *nn; |
100 | | |
101 | 2.68k | if (lh == NULL) |
102 | 0 | return; |
103 | | |
104 | 40.9k | for (i = 0; i < lh->num_nodes; i++) { |
105 | 38.2k | n = lh->b[i]; |
106 | 68.6k | while (n != NULL) { |
107 | 30.3k | nn = n->next; |
108 | 30.3k | OPENSSL_free(n); |
109 | 30.3k | n = nn; |
110 | 30.3k | } |
111 | 38.2k | lh->b[i] = NULL; |
112 | 38.2k | } |
113 | | |
114 | 2.68k | lh->num_items = 0; |
115 | 2.68k | } |
116 | | |
117 | | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
118 | 83.0k | { |
119 | 83.0k | unsigned long hash; |
120 | 83.0k | OPENSSL_LH_NODE *nn, **rn; |
121 | 83.0k | void *ret; |
122 | | |
123 | 83.0k | lh->error = 0; |
124 | 83.0k | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
125 | 0 | return NULL; /* 'lh->error++' already done in 'expand' */ |
126 | | |
127 | 83.0k | rn = getrn(lh, data, &hash); |
128 | | |
129 | 83.0k | if (*rn == NULL) { |
130 | 34.2k | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
131 | 0 | lh->error++; |
132 | 0 | return NULL; |
133 | 0 | } |
134 | 34.2k | nn->data = data; |
135 | 34.2k | nn->next = NULL; |
136 | 34.2k | nn->hash = hash; |
137 | 34.2k | *rn = nn; |
138 | 34.2k | ret = NULL; |
139 | 34.2k | lh->num_items++; |
140 | 48.8k | } else { /* replace same key */ |
141 | 48.8k | ret = (*rn)->data; |
142 | 48.8k | (*rn)->data = data; |
143 | 48.8k | } |
144 | 83.0k | return ret; |
145 | 83.0k | } |
146 | | |
147 | | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
148 | 3.87k | { |
149 | 3.87k | unsigned long hash; |
150 | 3.87k | OPENSSL_LH_NODE *nn, **rn; |
151 | 3.87k | void *ret; |
152 | | |
153 | 3.87k | lh->error = 0; |
154 | 3.87k | rn = getrn(lh, data, &hash); |
155 | | |
156 | 3.87k | if (*rn == NULL) { |
157 | 0 | return NULL; |
158 | 3.87k | } else { |
159 | 3.87k | nn = *rn; |
160 | 3.87k | *rn = nn->next; |
161 | 3.87k | ret = nn->data; |
162 | 3.87k | OPENSSL_free(nn); |
163 | 3.87k | } |
164 | | |
165 | 3.87k | lh->num_items--; |
166 | 3.87k | if ((lh->num_nodes > MIN_NODES) && |
167 | 3.87k | (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) |
168 | 16 | contract(lh); |
169 | | |
170 | 3.87k | return ret; |
171 | 3.87k | } |
172 | | |
173 | | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
174 | 212k | { |
175 | 212k | unsigned long hash; |
176 | 212k | OPENSSL_LH_NODE **rn; |
177 | | |
178 | 212k | if (lh->error != 0) |
179 | 0 | lh->error = 0; |
180 | | |
181 | 212k | rn = getrn(lh, data, &hash); |
182 | | |
183 | 212k | return *rn == NULL ? NULL : (*rn)->data; |
184 | 212k | } |
185 | | |
186 | | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, |
187 | | OPENSSL_LH_DOALL_FUNC_THUNK wfunc, |
188 | | OPENSSL_LH_DOALL_FUNC func, |
189 | | OPENSSL_LH_DOALL_FUNCARG func_arg, |
190 | | OPENSSL_LH_DOALL_FUNCARG_THUNK wfunc_arg, |
191 | | void *arg) |
192 | 2.75k | { |
193 | 2.75k | int i; |
194 | 2.75k | OPENSSL_LH_NODE *a, *n; |
195 | | |
196 | 2.75k | if (lh == NULL) |
197 | 0 | return; |
198 | | |
199 | | /* |
200 | | * reverse the order so we search from 'top to bottom' We were having |
201 | | * memory leaks otherwise |
202 | | */ |
203 | 35.6k | for (i = lh->num_nodes - 1; i >= 0; i--) { |
204 | 32.9k | a = lh->b[i]; |
205 | 49.6k | while (a != NULL) { |
206 | 16.6k | n = a->next; |
207 | 16.6k | if (use_arg) |
208 | 7.74k | wfunc_arg(a->data, arg, func_arg); |
209 | 8.93k | else |
210 | 8.93k | wfunc(a->data, func); |
211 | 16.6k | a = n; |
212 | 16.6k | } |
213 | 32.9k | } |
214 | 2.75k | } |
215 | | |
216 | | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
217 | 2.72k | { |
218 | 2.72k | if (lh == NULL) |
219 | 0 | return; |
220 | | |
221 | 2.72k | doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL, |
222 | 2.72k | (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL); |
223 | 2.72k | } |
224 | | |
225 | | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, |
226 | | OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
227 | 0 | { |
228 | 0 | if (lh == NULL) |
229 | 0 | return; |
230 | | |
231 | 0 | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL, |
232 | 0 | (OPENSSL_LH_DOALL_FUNC)NULL, func, lh->daaw, arg); |
233 | 0 | } |
234 | | |
235 | | void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh, |
236 | | OPENSSL_LH_DOALL_FUNCARG_THUNK daaw, |
237 | | OPENSSL_LH_DOALL_FUNCARG fn, void *arg) |
238 | 32 | { |
239 | 32 | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL, |
240 | 32 | (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg); |
241 | 32 | } |
242 | | |
243 | | static int expand(OPENSSL_LHASH *lh) |
244 | 16.7k | { |
245 | 16.7k | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
246 | 16.7k | unsigned int p, pmax, nni, j; |
247 | 16.7k | unsigned long hash; |
248 | | |
249 | 16.7k | nni = lh->num_alloc_nodes; |
250 | 16.7k | p = lh->p; |
251 | 16.7k | pmax = lh->pmax; |
252 | 16.7k | if (p + 1 >= pmax) { |
253 | 144 | j = nni * 2; |
254 | 144 | n = OPENSSL_realloc_array(lh->b, j, sizeof(OPENSSL_LH_NODE *)); |
255 | 144 | if (n == NULL) { |
256 | 0 | lh->error++; |
257 | 0 | return 0; |
258 | 0 | } |
259 | 144 | lh->b = n; |
260 | 144 | memset(n + nni, 0, sizeof(*n) * (j - nni)); |
261 | 144 | lh->pmax = nni; |
262 | 144 | lh->num_alloc_nodes = j; |
263 | 144 | lh->p = 0; |
264 | 16.6k | } else { |
265 | 16.6k | lh->p++; |
266 | 16.6k | } |
267 | | |
268 | 16.7k | lh->num_nodes++; |
269 | 16.7k | n1 = &(lh->b[p]); |
270 | 16.7k | n2 = &(lh->b[p + pmax]); |
271 | 16.7k | *n2 = NULL; |
272 | | |
273 | 67.7k | for (np = *n1; np != NULL;) { |
274 | 50.9k | hash = np->hash; |
275 | 50.9k | if ((hash % nni) != p) { /* move it */ |
276 | 8.83k | *n1 = (*n1)->next; |
277 | 8.83k | np->next = *n2; |
278 | 8.83k | *n2 = np; |
279 | 8.83k | } else |
280 | 42.1k | n1 = &((*n1)->next); |
281 | 50.9k | np = *n1; |
282 | 50.9k | } |
283 | | |
284 | 16.7k | return 1; |
285 | 16.7k | } |
286 | | |
287 | | static void contract(OPENSSL_LHASH *lh) |
288 | 16 | { |
289 | 16 | OPENSSL_LH_NODE **n, *n1, *np; |
290 | | |
291 | 16 | np = lh->b[lh->p + lh->pmax - 1]; |
292 | 16 | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ |
293 | 16 | if (lh->p == 0) { |
294 | 0 | n = OPENSSL_realloc_array(lh->b, lh->pmax, sizeof(OPENSSL_LH_NODE *)); |
295 | 0 | if (n == NULL) { |
296 | | /* fputs("realloc error in lhash", stderr); */ |
297 | 0 | lh->error++; |
298 | 0 | } else { |
299 | 0 | lh->b = n; |
300 | 0 | } |
301 | 0 | lh->num_alloc_nodes /= 2; |
302 | 0 | lh->pmax /= 2; |
303 | 0 | lh->p = lh->pmax - 1; |
304 | 0 | } else |
305 | 16 | lh->p--; |
306 | | |
307 | 16 | lh->num_nodes--; |
308 | | |
309 | 16 | n1 = lh->b[(int)lh->p]; |
310 | 16 | if (n1 == NULL) |
311 | 16 | lh->b[(int)lh->p] = np; |
312 | 0 | else { |
313 | 0 | while (n1->next != NULL) |
314 | 0 | n1 = n1->next; |
315 | 0 | n1->next = np; |
316 | 0 | } |
317 | 16 | } |
318 | | |
319 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
320 | | const void *data, unsigned long *rhash) |
321 | 299k | { |
322 | 299k | OPENSSL_LH_NODE **ret, *n1; |
323 | 299k | unsigned long hash, nn; |
324 | | |
325 | 299k | if (lh->hashw != NULL) |
326 | 299k | hash = lh->hashw(data, lh->hash); |
327 | 0 | else |
328 | 0 | hash = lh->hash(data); |
329 | | |
330 | 299k | *rhash = hash; |
331 | | |
332 | 299k | nn = hash % lh->pmax; |
333 | 299k | if (nn < lh->p) |
334 | 140k | nn = hash % lh->num_alloc_nodes; |
335 | | |
336 | 299k | ret = &(lh->b[(int)nn]); |
337 | 525k | for (n1 = *ret; n1 != NULL; n1 = n1->next) { |
338 | 472k | if (n1->hash != hash) { |
339 | 219k | ret = &(n1->next); |
340 | 219k | continue; |
341 | 219k | } |
342 | | |
343 | 252k | if (lh->compw != NULL) { |
344 | 252k | if (lh->compw(n1->data, data, lh->comp) == 0) |
345 | 246k | break; |
346 | 252k | } else { |
347 | 0 | if (lh->comp(n1->data, data) == 0) |
348 | 0 | break; |
349 | 0 | } |
350 | 6.20k | ret = &(n1->next); |
351 | 6.20k | } |
352 | 299k | return ret; |
353 | 299k | } |
354 | | |
355 | | /* |
356 | | * The following hash seems to work very well on normal text strings no |
357 | | * collisions on /usr/dict/words and it distributes on %2^n quite well, not |
358 | | * as good as MD5, but still good. |
359 | | */ |
360 | | unsigned long OPENSSL_LH_strhash(const char *c) |
361 | 98.8k | { |
362 | 98.8k | unsigned long ret = 0; |
363 | 98.8k | long n; |
364 | 98.8k | unsigned long v; |
365 | 98.8k | int r; |
366 | | |
367 | 98.8k | if ((c == NULL) || (*c == '\0')) |
368 | 95.8k | return ret; |
369 | | |
370 | 3.07k | n = 0x100; |
371 | 47.6k | while (*c) { |
372 | 44.5k | v = n | (*c); |
373 | 44.5k | n += 0x100; |
374 | 44.5k | r = (int)((v >> 2) ^ v) & 0x0f; |
375 | | /* cast to uint64_t to avoid 32 bit shift of 32 bit value */ |
376 | 44.5k | ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r)); |
377 | 44.5k | ret &= 0xFFFFFFFFL; |
378 | 44.5k | ret ^= v * v; |
379 | 44.5k | c++; |
380 | 44.5k | } |
381 | 3.07k | return (ret >> 16) ^ ret; |
382 | 98.8k | } |
383 | | |
384 | | /* |
385 | | * Case insensitive string hashing. |
386 | | * |
387 | | * The lower/upper case bit is masked out (forcing all letters to be capitals). |
388 | | * The major side effect on non-alpha characters is mapping the symbols and |
389 | | * digits into the control character range (which should be harmless). |
390 | | * The duplication (with respect to the hash value) of printable characters |
391 | | * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^' |
392 | | * respectively (which seems tolerable). |
393 | | * |
394 | | * For EBCDIC, the alpha mapping is to lower case, most symbols go to control |
395 | | * characters. The only duplication is '0' mapping to '^', which is better |
396 | | * than for ASCII. |
397 | | */ |
398 | | unsigned long ossl_lh_strcasehash(const char *c) |
399 | 21.0k | { |
400 | 21.0k | unsigned long ret = 0; |
401 | 21.0k | long n; |
402 | 21.0k | unsigned long v; |
403 | 21.0k | int r; |
404 | | #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST) |
405 | | const long int case_adjust = ~0x40; |
406 | | #else |
407 | 21.0k | const long int case_adjust = ~0x20; |
408 | 21.0k | #endif |
409 | | |
410 | 21.0k | if (c == NULL || *c == '\0') |
411 | 0 | return ret; |
412 | | |
413 | 271k | for (n = 0x100; *c != '\0'; n += 0x100) { |
414 | 250k | v = n | (case_adjust & *c); |
415 | 250k | r = (int)((v >> 2) ^ v) & 0x0f; |
416 | | /* cast to uint64_t to avoid 32 bit shift of 32 bit value */ |
417 | 250k | ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r)); |
418 | 250k | ret &= 0xFFFFFFFFL; |
419 | 250k | ret ^= v * v; |
420 | 250k | c++; |
421 | 250k | } |
422 | 21.0k | return (ret >> 16) ^ ret; |
423 | 21.0k | } |
424 | | |
425 | | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
426 | 0 | { |
427 | 0 | return lh ? lh->num_items : 0; |
428 | 0 | } |
429 | | |
430 | | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
431 | 64 | { |
432 | 64 | return lh->down_load; |
433 | 64 | } |
434 | | |
435 | | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
436 | 112 | { |
437 | 112 | lh->down_load = down_load; |
438 | 112 | } |
439 | | |
440 | | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
441 | 4.11k | { |
442 | 4.11k | return lh->error; |
443 | 4.11k | } |