1 /*
2  * Copyright 1995-2022 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 #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) */
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 
tsan_lock(const OPENSSL_LHASH * lh)47 static ossl_inline int tsan_lock(const OPENSSL_LHASH *lh)
48 {
49 #ifdef TSAN_REQUIRES_LOCKING
50     if (!CRYPTO_THREAD_write_lock(lh->tsan_lock))
51         return 0;
52 #endif
53     return 1;
54 }
55 
tsan_unlock(const OPENSSL_LHASH * lh)56 static ossl_inline void tsan_unlock(const OPENSSL_LHASH *lh)
57 {
58 #ifdef TSAN_REQUIRES_LOCKING
59     CRYPTO_THREAD_unlock(lh->tsan_lock);
60 #endif
61 }
62 
OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h,OPENSSL_LH_COMPFUNC c)63 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
64 {
65     OPENSSL_LHASH *ret;
66 
67     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
68         /*
69          * Do not set the error code, because the ERR code uses LHASH
70          * and we want to avoid possible endless error loop.
71          * ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
72          */
73         return NULL;
74     }
75     if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
76         goto err;
77 #ifdef TSAN_REQUIRES_LOCKING
78     if ((ret->tsan_lock = CRYPTO_THREAD_lock_new()) == NULL)
79         goto err;
80 #endif
81     ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
82     ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
83     ret->num_nodes = MIN_NODES / 2;
84     ret->num_alloc_nodes = MIN_NODES;
85     ret->pmax = MIN_NODES / 2;
86     ret->up_load = UP_LOAD;
87     ret->down_load = DOWN_LOAD;
88     return ret;
89 
90 err:
91     OPENSSL_free(ret->b);
92     OPENSSL_free(ret);
93     return NULL;
94 }
95 
OPENSSL_LH_free(OPENSSL_LHASH * lh)96 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
97 {
98     if (lh == NULL)
99         return;
100 
101     OPENSSL_LH_flush(lh);
102 #ifdef TSAN_REQUIRES_LOCKING
103     CRYPTO_THREAD_lock_free(lh->tsan_lock);
104 #endif
105     OPENSSL_free(lh->b);
106     OPENSSL_free(lh);
107 }
108 
OPENSSL_LH_flush(OPENSSL_LHASH * lh)109 void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
110 {
111     unsigned int i;
112     OPENSSL_LH_NODE *n, *nn;
113 
114     if (lh == NULL)
115         return;
116 
117     for (i = 0; i < lh->num_nodes; i++) {
118         n = lh->b[i];
119         while (n != NULL) {
120             nn = n->next;
121             OPENSSL_free(n);
122             n = nn;
123         }
124         lh->b[i] = NULL;
125     }
126 }
127 
OPENSSL_LH_insert(OPENSSL_LHASH * lh,void * data)128 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
129 {
130     unsigned long hash;
131     OPENSSL_LH_NODE *nn, **rn;
132     void *ret;
133 
134     lh->error = 0;
135     if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
136         return NULL;        /* 'lh->error++' already done in 'expand' */
137 
138     rn = getrn(lh, data, &hash);
139 
140     if (*rn == NULL) {
141         if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
142             lh->error++;
143             return NULL;
144         }
145         nn->data = data;
146         nn->next = NULL;
147         nn->hash = hash;
148         *rn = nn;
149         ret = NULL;
150         lh->num_insert++;
151         lh->num_items++;
152     } else {                    /* replace same key */
153         ret = (*rn)->data;
154         (*rn)->data = data;
155         lh->num_replace++;
156     }
157     return ret;
158 }
159 
OPENSSL_LH_delete(OPENSSL_LHASH * lh,const void * data)160 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
161 {
162     unsigned long hash;
163     OPENSSL_LH_NODE *nn, **rn;
164     void *ret;
165 
166     lh->error = 0;
167     rn = getrn(lh, data, &hash);
168 
169     if (*rn == NULL) {
170         lh->num_no_delete++;
171         return NULL;
172     } else {
173         nn = *rn;
174         *rn = nn->next;
175         ret = nn->data;
176         OPENSSL_free(nn);
177         lh->num_delete++;
178     }
179 
180     lh->num_items--;
181     if ((lh->num_nodes > MIN_NODES) &&
182         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
183         contract(lh);
184 
185     return ret;
186 }
187 
OPENSSL_LH_retrieve(OPENSSL_LHASH * lh,const void * data)188 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
189 {
190     unsigned long hash;
191     OPENSSL_LH_NODE **rn;
192 
193     /*-
194      * This should be atomic without tsan.
195      * It's not clear why it was done this way and not elsewhere.
196      */
197     tsan_store((TSAN_QUALIFIER int *)&lh->error, 0);
198 
199     rn = getrn(lh, data, &hash);
200 
201     if (tsan_lock(lh)) {
202         tsan_counter(*rn == NULL ? &lh->num_retrieve_miss : &lh->num_retrieve);
203         tsan_unlock(lh);
204     }
205     return *rn == NULL ? NULL : (*rn)->data;
206 }
207 
doall_util_fn(OPENSSL_LHASH * lh,int use_arg,OPENSSL_LH_DOALL_FUNC func,OPENSSL_LH_DOALL_FUNCARG func_arg,void * arg)208 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
209                           OPENSSL_LH_DOALL_FUNC func,
210                           OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
211 {
212     int i;
213     OPENSSL_LH_NODE *a, *n;
214 
215     if (lh == NULL)
216         return;
217 
218     /*
219      * reverse the order so we search from 'top to bottom' We were having
220      * memory leaks otherwise
221      */
222     for (i = lh->num_nodes - 1; i >= 0; i--) {
223         a = lh->b[i];
224         while (a != NULL) {
225             n = a->next;
226             if (use_arg)
227                 func_arg(a->data, arg);
228             else
229                 func(a->data);
230             a = n;
231         }
232     }
233 }
234 
OPENSSL_LH_doall(OPENSSL_LHASH * lh,OPENSSL_LH_DOALL_FUNC func)235 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
236 {
237     doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
238 }
239 
OPENSSL_LH_doall_arg(OPENSSL_LHASH * lh,OPENSSL_LH_DOALL_FUNCARG func,void * arg)240 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
241 {
242     doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
243 }
244 
expand(OPENSSL_LHASH * lh)245 static int expand(OPENSSL_LHASH *lh)
246 {
247     OPENSSL_LH_NODE **n, **n1, **n2, *np;
248     unsigned int p, pmax, nni, j;
249     unsigned long hash;
250 
251     nni = lh->num_alloc_nodes;
252     p = lh->p;
253     pmax = lh->pmax;
254     if (p + 1 >= pmax) {
255         j = nni * 2;
256         n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
257         if (n == NULL) {
258             lh->error++;
259             return 0;
260         }
261         lh->b = n;
262         memset(n + nni, 0, sizeof(*n) * (j - nni));
263         lh->pmax = nni;
264         lh->num_alloc_nodes = j;
265         lh->num_expand_reallocs++;
266         lh->p = 0;
267     } else {
268         lh->p++;
269     }
270 
271     lh->num_nodes++;
272     lh->num_expands++;
273     n1 = &(lh->b[p]);
274     n2 = &(lh->b[p + pmax]);
275     *n2 = NULL;
276 
277     for (np = *n1; np != NULL;) {
278         hash = np->hash;
279         if ((hash % nni) != p) { /* move it */
280             *n1 = (*n1)->next;
281             np->next = *n2;
282             *n2 = np;
283         } else
284             n1 = &((*n1)->next);
285         np = *n1;
286     }
287 
288     return 1;
289 }
290 
contract(OPENSSL_LHASH * lh)291 static void contract(OPENSSL_LHASH *lh)
292 {
293     OPENSSL_LH_NODE **n, *n1, *np;
294 
295     np = lh->b[lh->p + lh->pmax - 1];
296     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
297     if (lh->p == 0) {
298         n = OPENSSL_realloc(lh->b,
299                             (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
300         if (n == NULL) {
301             /* fputs("realloc error in lhash",stderr); */
302             lh->error++;
303             return;
304         }
305         lh->num_contract_reallocs++;
306         lh->num_alloc_nodes /= 2;
307         lh->pmax /= 2;
308         lh->p = lh->pmax - 1;
309         lh->b = n;
310     } else
311         lh->p--;
312 
313     lh->num_nodes--;
314     lh->num_contracts++;
315 
316     n1 = lh->b[(int)lh->p];
317     if (n1 == NULL)
318         lh->b[(int)lh->p] = np;
319     else {
320         while (n1->next != NULL)
321             n1 = n1->next;
322         n1->next = np;
323     }
324 }
325 
getrn(OPENSSL_LHASH * lh,const void * data,unsigned long * rhash)326 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
327                                const void *data, unsigned long *rhash)
328 {
329     OPENSSL_LH_NODE **ret, *n1;
330     unsigned long hash, nn;
331     OPENSSL_LH_COMPFUNC cf;
332     int do_tsan = 1;
333 
334 #ifdef TSAN_REQUIRES_LOCKING
335     do_tsan = tsan_lock(lh);
336 #endif
337     hash = (*(lh->hash)) (data);
338     if (do_tsan)
339         tsan_counter(&lh->num_hash_calls);
340     *rhash = hash;
341 
342     nn = hash % lh->pmax;
343     if (nn < lh->p)
344         nn = hash % lh->num_alloc_nodes;
345 
346     cf = lh->comp;
347     ret = &(lh->b[(int)nn]);
348     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
349         if (do_tsan)
350             tsan_counter(&lh->num_hash_comps);
351         if (n1->hash != hash) {
352             ret = &(n1->next);
353             continue;
354         }
355         if (do_tsan)
356             tsan_counter(&lh->num_comp_calls);
357         if (cf(n1->data, data) == 0)
358             break;
359         ret = &(n1->next);
360     }
361     if (do_tsan)
362         tsan_unlock(lh);
363     return ret;
364 }
365 
366 /*
367  * The following hash seems to work very well on normal text strings no
368  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
369  * as good as MD5, but still good.
370  */
OPENSSL_LH_strhash(const char * c)371 unsigned long OPENSSL_LH_strhash(const char *c)
372 {
373     unsigned long ret = 0;
374     long n;
375     unsigned long v;
376     int r;
377 
378     if ((c == NULL) || (*c == '\0'))
379         return ret;
380 
381     n = 0x100;
382     while (*c) {
383         v = n | (*c);
384         n += 0x100;
385         r = (int)((v >> 2) ^ v) & 0x0f;
386         /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
387         ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
388         ret &= 0xFFFFFFFFL;
389         ret ^= v * v;
390         c++;
391     }
392     return (ret >> 16) ^ ret;
393 }
394 
ossl_lh_strcasehash(const char * c)395 unsigned long ossl_lh_strcasehash(const char *c)
396 {
397     unsigned long ret = 0;
398     long n;
399     unsigned long v;
400     int r;
401 
402     if (c == NULL || *c == '\0')
403         return ret;
404 
405     for (n = 0x100; *c != '\0'; n += 0x100) {
406         v = n | ossl_tolower(*c);
407         r = (int)((v >> 2) ^ v) & 0x0f;
408         /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
409         ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
410         ret &= 0xFFFFFFFFL;
411         ret ^= v * v;
412         c++;
413     }
414     return (ret >> 16) ^ ret;
415 }
416 
OPENSSL_LH_num_items(const OPENSSL_LHASH * lh)417 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
418 {
419     return lh ? lh->num_items : 0;
420 }
421 
OPENSSL_LH_get_down_load(const OPENSSL_LHASH * lh)422 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
423 {
424     return lh->down_load;
425 }
426 
OPENSSL_LH_set_down_load(OPENSSL_LHASH * lh,unsigned long down_load)427 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
428 {
429     lh->down_load = down_load;
430 }
431 
OPENSSL_LH_error(OPENSSL_LHASH * lh)432 int OPENSSL_LH_error(OPENSSL_LHASH *lh)
433 {
434     return lh->error;
435 }
436