Coverage Report

Created: 2024-11-21 07:03

/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
2.07k
#define MIN_NODES       16
40
530
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
530
#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
530
{
53
54
530
    if (lh == NULL)
55
0
        return NULL;
56
530
    lh->compw = cw;
57
530
    lh->hashw = hw;
58
530
    lh->daw = daw;
59
530
    lh->daaw = daaw;
60
530
    return lh;
61
530
}
62
63
OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
64
530
{
65
530
    OPENSSL_LHASH *ret;
66
67
530
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
68
0
        return NULL;
69
530
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
70
0
        goto err;
71
530
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
72
530
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
73
530
    ret->num_nodes = MIN_NODES / 2;
74
530
    ret->num_alloc_nodes = MIN_NODES;
75
530
    ret->pmax = MIN_NODES / 2;
76
530
    ret->up_load = UP_LOAD;
77
530
    ret->down_load = DOWN_LOAD;
78
530
    return ret;
79
80
0
err:
81
0
    OPENSSL_free(ret->b);
82
0
    OPENSSL_free(ret);
83
0
    return NULL;
84
530
}
85
86
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
87
534
{
88
534
    if (lh == NULL)
89
4
        return;
90
91
530
    OPENSSL_LH_flush(lh);
92
530
    OPENSSL_free(lh->b);
93
530
    OPENSSL_free(lh);
94
530
}
95
96
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
97
530
{
98
530
    unsigned int i;
99
530
    OPENSSL_LH_NODE *n, *nn;
100
101
530
    if (lh == NULL)
102
0
        return;
103
104
7.95k
    for (i = 0; i < lh->num_nodes; i++) {
105
7.42k
        n = lh->b[i];
106
13.7k
        while (n != NULL) {
107
6.28k
            nn = n->next;
108
6.28k
            OPENSSL_free(n);
109
6.28k
            n = nn;
110
6.28k
        }
111
7.42k
        lh->b[i] = NULL;
112
7.42k
    }
113
114
530
    lh->num_items = 0;
115
530
}
116
117
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
118
18.2k
{
119
18.2k
    unsigned long hash;
120
18.2k
    OPENSSL_LH_NODE *nn, **rn;
121
18.2k
    void *ret;
122
123
18.2k
    lh->error = 0;
124
18.2k
    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
18.2k
    rn = getrn(lh, data, &hash);
128
129
18.2k
    if (*rn == NULL) {
130
6.76k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
131
0
            lh->error++;
132
0
            return NULL;
133
0
        }
134
6.76k
        nn->data = data;
135
6.76k
        nn->next = NULL;
136
6.76k
        nn->hash = hash;
137
6.76k
        *rn = nn;
138
6.76k
        ret = NULL;
139
6.76k
        lh->num_items++;
140
11.5k
    } else {                    /* replace same key */
141
11.5k
        ret = (*rn)->data;
142
11.5k
        (*rn)->data = data;
143
11.5k
    }
144
18.2k
    return ret;
145
18.2k
}
146
147
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
148
480
{
149
480
    unsigned long hash;
150
480
    OPENSSL_LH_NODE *nn, **rn;
151
480
    void *ret;
152
153
480
    lh->error = 0;
154
480
    rn = getrn(lh, data, &hash);
155
156
480
    if (*rn == NULL) {
157
0
        return NULL;
158
480
    } else {
159
480
        nn = *rn;
160
480
        *rn = nn->next;
161
480
        ret = nn->data;
162
480
        OPENSSL_free(nn);
163
480
    }
164
165
480
    lh->num_items--;
166
480
    if ((lh->num_nodes > MIN_NODES) &&
167
480
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
168
2
        contract(lh);
169
170
480
    return ret;
171
480
}
172
173
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
174
32.6k
{
175
32.6k
    unsigned long hash;
176
32.6k
    OPENSSL_LH_NODE **rn;
177
178
32.6k
    if (lh->error != 0)
179
0
        lh->error = 0;
180
181
32.6k
    rn = getrn(lh, data, &hash);
182
183
32.6k
    return *rn == NULL ? NULL : (*rn)->data;
184
32.6k
}
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
536
{
193
536
    int i;
194
536
    OPENSSL_LH_NODE *a, *n;
195
196
536
    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
6.17k
    for (i = lh->num_nodes - 1; i >= 0; i--) {
204
5.64k
        a = lh->b[i];
205
7.99k
        while (a != NULL) {
206
2.34k
            n = a->next;
207
2.34k
            if (use_arg)
208
960
                wfunc_arg(a->data, arg, func_arg);
209
1.38k
            else
210
1.38k
                wfunc(a->data, func);
211
2.34k
            a = n;
212
2.34k
        }
213
5.64k
    }
214
536
}
215
216
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
217
532
{
218
532
    if (lh == NULL)
219
0
        return;
220
221
532
    doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL,
222
532
                  (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL);
223
532
}
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
4
{
239
4
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
240
4
                  (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg);
241
4
}
242
243
static int expand(OPENSSL_LHASH *lh)
244
3.18k
{
245
3.18k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
246
3.18k
    unsigned int p, pmax, nni, j;
247
3.18k
    unsigned long hash;
248
249
3.18k
    nni = lh->num_alloc_nodes;
250
3.18k
    p = lh->p;
251
3.18k
    pmax = lh->pmax;
252
3.18k
    if (p + 1 >= pmax) {
253
30
        j = nni * 2;
254
30
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
255
30
        if (n == NULL) {
256
0
            lh->error++;
257
0
            return 0;
258
0
        }
259
30
        lh->b = n;
260
30
        memset(n + nni, 0, sizeof(*n) * (j - nni));
261
30
        lh->pmax = nni;
262
30
        lh->num_alloc_nodes = j;
263
30
        lh->p = 0;
264
3.15k
    } else {
265
3.15k
        lh->p++;
266
3.15k
    }
267
268
3.18k
    lh->num_nodes++;
269
3.18k
    n1 = &(lh->b[p]);
270
3.18k
    n2 = &(lh->b[p + pmax]);
271
3.18k
    *n2 = NULL;
272
273
14.4k
    for (np = *n1; np != NULL;) {
274
11.3k
        hash = np->hash;
275
11.3k
        if ((hash % nni) != p) { /* move it */
276
1.90k
            *n1 = (*n1)->next;
277
1.90k
            np->next = *n2;
278
1.90k
            *n2 = np;
279
1.90k
        } else
280
9.41k
            n1 = &((*n1)->next);
281
11.3k
        np = *n1;
282
11.3k
    }
283
284
3.18k
    return 1;
285
3.18k
}
286
287
static void contract(OPENSSL_LHASH *lh)
288
2
{
289
2
    OPENSSL_LH_NODE **n, *n1, *np;
290
291
2
    np = lh->b[lh->p + lh->pmax - 1];
292
2
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
293
2
    if (lh->p == 0) {
294
0
        n = OPENSSL_realloc(lh->b,
295
0
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
296
0
        if (n == NULL) {
297
            /* fputs("realloc error in lhash", stderr); */
298
0
            lh->error++;
299
0
        } else {
300
0
            lh->b = n;
301
0
        }
302
0
        lh->num_alloc_nodes /= 2;
303
0
        lh->pmax /= 2;
304
0
        lh->p = lh->pmax - 1;
305
0
    } else
306
2
        lh->p--;
307
308
2
    lh->num_nodes--;
309
310
2
    n1 = lh->b[(int)lh->p];
311
2
    if (n1 == NULL)
312
2
        lh->b[(int)lh->p] = np;
313
0
    else {
314
0
        while (n1->next != NULL)
315
0
            n1 = n1->next;
316
0
        n1->next = np;
317
0
    }
318
2
}
319
320
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
321
                               const void *data, unsigned long *rhash)
322
51.4k
{
323
51.4k
    OPENSSL_LH_NODE **ret, *n1;
324
51.4k
    unsigned long hash, nn;
325
326
51.4k
    if (lh->hashw != NULL)
327
51.4k
        hash = lh->hashw(data, lh->hash);
328
0
    else
329
0
        hash = lh->hash(data);
330
331
51.4k
    *rhash = hash;
332
333
51.4k
    nn = hash % lh->pmax;
334
51.4k
    if (nn < lh->p)
335
10.4k
        nn = hash % lh->num_alloc_nodes;
336
337
51.4k
    ret = &(lh->b[(int)nn]);
338
82.4k
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
339
74.6k
        if (n1->hash != hash) {
340
30.4k
            ret = &(n1->next);
341
30.4k
            continue;
342
30.4k
        }
343
344
44.2k
        if (lh->compw != NULL) {
345
44.2k
            if (lh->compw(n1->data, data, lh->comp) == 0)
346
43.6k
                break;
347
44.2k
        } else {
348
0
            if (lh->comp(n1->data, data) == 0)
349
0
                break;
350
0
        }
351
620
        ret = &(n1->next);
352
620
    }
353
51.4k
    return ret;
354
51.4k
}
355
356
/*
357
 * The following hash seems to work very well on normal text strings no
358
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
359
 * as good as MD5, but still good.
360
 */
361
unsigned long OPENSSL_LH_strhash(const char *c)
362
30.6k
{
363
30.6k
    unsigned long ret = 0;
364
30.6k
    long n;
365
30.6k
    unsigned long v;
366
30.6k
    int r;
367
368
30.6k
    if ((c == NULL) || (*c == '\0'))
369
30.0k
        return ret;
370
371
620
    n = 0x100;
372
9.41k
    while (*c) {
373
8.79k
        v = n | (*c);
374
8.79k
        n += 0x100;
375
8.79k
        r = (int)((v >> 2) ^ v) & 0x0f;
376
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
377
8.79k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
378
8.79k
        ret &= 0xFFFFFFFFL;
379
8.79k
        ret ^= v * v;
380
8.79k
        c++;
381
8.79k
    }
382
620
    return (ret >> 16) ^ ret;
383
30.6k
}
384
385
/*
386
 * Case insensitive string hashing.
387
 *
388
 * The lower/upper case bit is masked out (forcing all letters to be capitals).
389
 * The major side effect on non-alpha characters is mapping the symbols and
390
 * digits into the control character range (which should be harmless).
391
 * The duplication (with respect to the hash value) of printable characters
392
 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
393
 * respectively (which seems tolerable).
394
 *
395
 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
396
 * characters.  The only duplication is '0' mapping to '^', which is better
397
 * than for ASCII.
398
 */
399
unsigned long ossl_lh_strcasehash(const char *c)
400
3.35k
{
401
3.35k
    unsigned long ret = 0;
402
3.35k
    long n;
403
3.35k
    unsigned long v;
404
3.35k
    int r;
405
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
406
    const long int case_adjust = ~0x40;
407
#else
408
3.35k
    const long int case_adjust = ~0x20;
409
3.35k
#endif
410
411
3.35k
    if (c == NULL || *c == '\0')
412
0
        return ret;
413
414
38.1k
    for (n = 0x100; *c != '\0'; n += 0x100) {
415
34.7k
        v = n | (case_adjust & *c);
416
34.7k
        r = (int)((v >> 2) ^ v) & 0x0f;
417
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
418
34.7k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
419
34.7k
        ret &= 0xFFFFFFFFL;
420
34.7k
        ret ^= v * v;
421
34.7k
        c++;
422
34.7k
    }
423
3.35k
    return (ret >> 16) ^ ret;
424
3.35k
}
425
426
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
427
0
{
428
0
    return lh ? lh->num_items : 0;
429
0
}
430
431
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
432
8
{
433
8
    return lh->down_load;
434
8
}
435
436
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
437
14
{
438
14
    lh->down_load = down_load;
439
14
}
440
441
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
442
790
{
443
790
    return lh->error;
444
790
}