/src/openssl30/crypto/lhash/lhash.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2023 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 | 9.75M | #define MIN_NODES 16 |
40 | 796k | #define UP_LOAD (2 * LH_LOAD_MULT) /* load times 256 (default 2) */ |
41 | 796k | #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_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) |
48 | 796k | { |
49 | 796k | OPENSSL_LHASH *ret; |
50 | | |
51 | 796k | 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. |
55 | | * ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE); |
56 | | */ |
57 | 0 | return NULL; |
58 | 0 | } |
59 | 796k | if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) |
60 | 0 | goto err; |
61 | 796k | ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); |
62 | 796k | ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); |
63 | 796k | ret->num_nodes = MIN_NODES / 2; |
64 | 796k | ret->num_alloc_nodes = MIN_NODES; |
65 | 796k | ret->pmax = MIN_NODES / 2; |
66 | 796k | ret->up_load = UP_LOAD; |
67 | 796k | ret->down_load = DOWN_LOAD; |
68 | 796k | return ret; |
69 | | |
70 | 0 | err: |
71 | 0 | OPENSSL_free(ret->b); |
72 | 0 | OPENSSL_free(ret); |
73 | 0 | return NULL; |
74 | 796k | } |
75 | | |
76 | | void OPENSSL_LH_free(OPENSSL_LHASH *lh) |
77 | 789k | { |
78 | 789k | if (lh == NULL) |
79 | 220 | return; |
80 | | |
81 | 788k | OPENSSL_LH_flush(lh); |
82 | 788k | OPENSSL_free(lh->b); |
83 | 788k | OPENSSL_free(lh); |
84 | 788k | } |
85 | | |
86 | | void OPENSSL_LH_flush(OPENSSL_LHASH *lh) |
87 | 794k | { |
88 | 794k | unsigned int i; |
89 | 794k | OPENSSL_LH_NODE *n, *nn; |
90 | | |
91 | 794k | if (lh == NULL) |
92 | 0 | return; |
93 | | |
94 | 9.48M | for (i = 0; i < lh->num_nodes; i++) { |
95 | 8.69M | n = lh->b[i]; |
96 | 11.0M | while (n != NULL) { |
97 | 2.38M | nn = n->next; |
98 | 2.38M | OPENSSL_free(n); |
99 | 2.38M | n = nn; |
100 | 2.38M | } |
101 | 8.69M | lh->b[i] = NULL; |
102 | 8.69M | } |
103 | | |
104 | 794k | lh->num_items = 0; |
105 | 794k | } |
106 | | |
107 | | void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) |
108 | 10.8M | { |
109 | 10.8M | unsigned long hash; |
110 | 10.8M | OPENSSL_LH_NODE *nn, **rn; |
111 | 10.8M | void *ret; |
112 | | |
113 | 10.8M | lh->error = 0; |
114 | 10.8M | if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) |
115 | 0 | return NULL; /* 'lh->error++' already done in 'expand' */ |
116 | | |
117 | 10.8M | rn = getrn(lh, data, &hash); |
118 | | |
119 | 10.8M | if (*rn == NULL) { |
120 | 9.85M | if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { |
121 | 0 | lh->error++; |
122 | 0 | return NULL; |
123 | 0 | } |
124 | 9.85M | nn->data = data; |
125 | 9.85M | nn->next = NULL; |
126 | 9.85M | nn->hash = hash; |
127 | 9.85M | *rn = nn; |
128 | 9.85M | ret = NULL; |
129 | 9.85M | lh->num_items++; |
130 | 9.85M | } else { /* replace same key */ |
131 | 1.03M | ret = (*rn)->data; |
132 | 1.03M | (*rn)->data = data; |
133 | 1.03M | } |
134 | 10.8M | return ret; |
135 | 10.8M | } |
136 | | |
137 | | void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) |
138 | 7.36M | { |
139 | 7.36M | unsigned long hash; |
140 | 7.36M | OPENSSL_LH_NODE *nn, **rn; |
141 | 7.36M | void *ret; |
142 | | |
143 | 7.36M | lh->error = 0; |
144 | 7.36M | rn = getrn(lh, data, &hash); |
145 | | |
146 | 7.36M | if (*rn == NULL) { |
147 | 0 | return NULL; |
148 | 7.36M | } else { |
149 | 7.36M | nn = *rn; |
150 | 7.36M | *rn = nn->next; |
151 | 7.36M | ret = nn->data; |
152 | 7.36M | OPENSSL_free(nn); |
153 | 7.36M | } |
154 | | |
155 | 7.36M | lh->num_items--; |
156 | 7.36M | if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) |
157 | 1.72M | contract(lh); |
158 | | |
159 | 7.36M | return ret; |
160 | 7.36M | } |
161 | | |
162 | | void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) |
163 | 338M | { |
164 | 338M | unsigned long hash; |
165 | 338M | OPENSSL_LH_NODE **rn; |
166 | | |
167 | 338M | if (lh->error != 0) |
168 | 0 | lh->error = 0; |
169 | | |
170 | 338M | rn = getrn(lh, data, &hash); |
171 | | |
172 | 338M | return *rn == NULL ? NULL : (*rn)->data; |
173 | 338M | } |
174 | | |
175 | | static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, |
176 | | OPENSSL_LH_DOALL_FUNC func, |
177 | | OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) |
178 | 21.7M | { |
179 | 21.7M | int i; |
180 | 21.7M | OPENSSL_LH_NODE *a, *n; |
181 | | |
182 | 21.7M | if (lh == NULL) |
183 | 0 | return; |
184 | | |
185 | | /* |
186 | | * reverse the order so we search from 'top to bottom' We were having |
187 | | * memory leaks otherwise |
188 | | */ |
189 | 3.34G | for (i = lh->num_nodes - 1; i >= 0; i--) { |
190 | 3.32G | a = lh->b[i]; |
191 | 9.90G | while (a != NULL) { |
192 | 6.57G | n = a->next; |
193 | 6.57G | if (use_arg) |
194 | 6.57G | func_arg(a->data, arg); |
195 | 704k | else |
196 | 704k | func(a->data); |
197 | 6.57G | a = n; |
198 | 6.57G | } |
199 | 3.32G | } |
200 | 21.7M | } |
201 | | |
202 | | void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) |
203 | 2.40k | { |
204 | 2.40k | doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL); |
205 | 2.40k | } |
206 | | |
207 | | void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg) |
208 | 6.77M | { |
209 | 6.77M | doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg); |
210 | 6.77M | } |
211 | | |
212 | | static int expand(OPENSSL_LHASH *lh) |
213 | 4.11M | { |
214 | 4.11M | OPENSSL_LH_NODE **n, **n1, **n2, *np; |
215 | 4.11M | unsigned int p, pmax, nni, j; |
216 | 4.11M | unsigned long hash; |
217 | | |
218 | 4.11M | nni = lh->num_alloc_nodes; |
219 | 4.11M | p = lh->p; |
220 | 4.11M | pmax = lh->pmax; |
221 | 4.11M | if (p + 1 >= pmax) { |
222 | 48.7k | j = nni * 2; |
223 | 48.7k | n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); |
224 | 48.7k | if (n == NULL) { |
225 | 0 | lh->error++; |
226 | 0 | return 0; |
227 | 0 | } |
228 | 48.7k | lh->b = n; |
229 | 48.7k | memset(n + nni, 0, sizeof(*n) * (j - nni)); |
230 | 48.7k | lh->pmax = nni; |
231 | 48.7k | lh->num_alloc_nodes = j; |
232 | 48.7k | lh->p = 0; |
233 | 4.06M | } else { |
234 | 4.06M | lh->p++; |
235 | 4.06M | } |
236 | | |
237 | 4.11M | lh->num_nodes++; |
238 | 4.11M | n1 = &(lh->b[p]); |
239 | 4.11M | n2 = &(lh->b[p + pmax]); |
240 | 4.11M | *n2 = NULL; |
241 | | |
242 | 16.0M | for (np = *n1; np != NULL;) { |
243 | 11.9M | hash = np->hash; |
244 | 11.9M | if ((hash % nni) != p) { /* move it */ |
245 | 4.90M | *n1 = (*n1)->next; |
246 | 4.90M | np->next = *n2; |
247 | 4.90M | *n2 = np; |
248 | 4.90M | } else |
249 | 7.03M | n1 = &((*n1)->next); |
250 | 11.9M | np = *n1; |
251 | 11.9M | } |
252 | | |
253 | 4.11M | return 1; |
254 | 4.11M | } |
255 | | |
256 | | static void contract(OPENSSL_LHASH *lh) |
257 | 1.72M | { |
258 | 1.72M | OPENSSL_LH_NODE **n, *n1, *np; |
259 | | |
260 | 1.72M | np = lh->b[lh->p + lh->pmax - 1]; |
261 | 1.72M | lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ |
262 | 1.72M | if (lh->p == 0) { |
263 | 19.3k | n = OPENSSL_realloc(lh->b, |
264 | 19.3k | (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); |
265 | 19.3k | if (n == NULL) { |
266 | | /* fputs("realloc error in lhash",stderr); */ |
267 | 0 | lh->error++; |
268 | 19.3k | } else { |
269 | 19.3k | lh->b = n; |
270 | 19.3k | } |
271 | 19.3k | lh->num_alloc_nodes /= 2; |
272 | 19.3k | lh->pmax /= 2; |
273 | 19.3k | lh->p = lh->pmax - 1; |
274 | 19.3k | } else |
275 | 1.70M | lh->p--; |
276 | | |
277 | 1.72M | lh->num_nodes--; |
278 | | |
279 | 1.72M | n1 = lh->b[(int)lh->p]; |
280 | 1.72M | if (n1 == NULL) |
281 | 773k | lh->b[(int)lh->p] = np; |
282 | 951k | else { |
283 | 2.46M | while (n1->next != NULL) |
284 | 1.51M | n1 = n1->next; |
285 | 951k | n1->next = np; |
286 | 951k | } |
287 | 1.72M | } |
288 | | |
289 | | static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, |
290 | | const void *data, unsigned long *rhash) |
291 | 247M | { |
292 | 247M | OPENSSL_LH_NODE **ret, *n1; |
293 | 247M | unsigned long hash, nn; |
294 | 247M | OPENSSL_LH_COMPFUNC cf; |
295 | | |
296 | 247M | hash = (*(lh->hash))(data); |
297 | 247M | *rhash = hash; |
298 | | |
299 | 247M | nn = hash % lh->pmax; |
300 | 247M | if (nn < lh->p) |
301 | 162M | nn = hash % lh->num_alloc_nodes; |
302 | | |
303 | 247M | cf = lh->comp; |
304 | 247M | ret = &(lh->b[(int)nn]); |
305 | 932M | for (n1 = *ret; n1 != NULL; n1 = n1->next) { |
306 | 891M | if (n1->hash != hash) { |
307 | 685M | ret = &(n1->next); |
308 | 685M | continue; |
309 | 685M | } |
310 | 206M | if (cf(n1->data, data) == 0) |
311 | 206M | break; |
312 | 34.2k | ret = &(n1->next); |
313 | 34.2k | } |
314 | 247M | return ret; |
315 | 247M | } |
316 | | |
317 | | /* |
318 | | * The following hash seems to work very well on normal text strings no |
319 | | * collisions on /usr/dict/words and it distributes on %2^n quite well, not |
320 | | * as good as MD5, but still good. |
321 | | */ |
322 | | unsigned long OPENSSL_LH_strhash(const char *c) |
323 | 39.4M | { |
324 | 39.4M | unsigned long ret = 0; |
325 | 39.4M | long n; |
326 | 39.4M | unsigned long v; |
327 | 39.4M | int r; |
328 | | |
329 | 39.4M | if ((c == NULL) || (*c == '\0')) |
330 | 25.1M | return ret; |
331 | | |
332 | 14.2M | n = 0x100; |
333 | 768M | while (*c) { |
334 | 754M | v = n | (*c); |
335 | 754M | n += 0x100; |
336 | 754M | r = (int)((v >> 2) ^ v) & 0x0f; |
337 | | /* cast to uint64_t to avoid 32 bit shift of 32 bit value */ |
338 | 754M | ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r)); |
339 | 754M | ret &= 0xFFFFFFFFL; |
340 | 754M | ret ^= v * v; |
341 | 754M | c++; |
342 | 754M | } |
343 | 14.2M | return (ret >> 16) ^ ret; |
344 | 39.4M | } |
345 | | |
346 | | unsigned long ossl_lh_strcasehash(const char *c) |
347 | 249M | { |
348 | 249M | unsigned long ret = 0; |
349 | 249M | long n; |
350 | 249M | unsigned long v; |
351 | 249M | int r; |
352 | | |
353 | 249M | if (c == NULL || *c == '\0') |
354 | 1.08k | return ret; |
355 | | |
356 | 1.58G | for (n = 0x100; *c != '\0'; n += 0x100) { |
357 | 1.34G | v = n | ossl_tolower(*c); |
358 | 1.34G | r = (int)((v >> 2) ^ v) & 0x0f; |
359 | | /* cast to uint64_t to avoid 32 bit shift of 32 bit value */ |
360 | 1.34G | ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r)); |
361 | 1.34G | ret &= 0xFFFFFFFFL; |
362 | 1.34G | ret ^= v * v; |
363 | 1.34G | c++; |
364 | 1.34G | } |
365 | 249M | return (ret >> 16) ^ ret; |
366 | 249M | } |
367 | | |
368 | | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
369 | 1.73M | { |
370 | 1.73M | return lh ? lh->num_items : 0; |
371 | 1.73M | } |
372 | | |
373 | | unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) |
374 | 168k | { |
375 | 168k | return lh->down_load; |
376 | 168k | } |
377 | | |
378 | | void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) |
379 | 546k | { |
380 | 546k | lh->down_load = down_load; |
381 | 546k | } |
382 | | |
383 | | int OPENSSL_LH_error(OPENSSL_LHASH *lh) |
384 | 7.08M | { |
385 | 7.08M | return lh->error; |
386 | 7.08M | } |