Coverage Report

Created: 2025-12-14 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/lhash/lhash.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 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.7k
    for (i = 0; i < lh->num_nodes; i++) {
105
38.0k
        n = lh->b[i];
106
68.0k
        while (n != NULL) {
107
29.9k
            nn = n->next;
108
29.9k
            OPENSSL_free(n);
109
29.9k
            n = nn;
110
29.9k
        }
111
38.0k
        lh->b[i] = NULL;
112
38.0k
    }
113
114
2.68k
    lh->num_items = 0;
115
2.68k
}
116
117
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
118
81.3k
{
119
81.3k
    unsigned long hash;
120
81.3k
    OPENSSL_LH_NODE *nn, **rn;
121
81.3k
    void *ret;
122
123
81.3k
    lh->error = 0;
124
81.3k
    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
81.3k
    rn = getrn(lh, data, &hash);
128
129
81.3k
    if (*rn == NULL) {
130
33.8k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
131
0
            lh->error++;
132
0
            return NULL;
133
0
        }
134
33.8k
        nn->data = data;
135
33.8k
        nn->next = NULL;
136
33.8k
        nn->hash = hash;
137
33.8k
        *rn = nn;
138
33.8k
        ret = NULL;
139
33.8k
        lh->num_items++;
140
47.5k
    } else { /* replace same key */
141
47.5k
        ret = (*rn)->data;
142
47.5k
        (*rn)->data = data;
143
47.5k
    }
144
81.3k
    return ret;
145
81.3k
}
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) && (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
167
16
        contract(lh);
168
169
3.87k
    return ret;
170
3.87k
}
171
172
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
173
259k
{
174
259k
    unsigned long hash;
175
259k
    OPENSSL_LH_NODE **rn;
176
177
259k
    if (lh->error != 0)
178
0
        lh->error = 0;
179
180
259k
    rn = getrn(lh, data, &hash);
181
182
259k
    return *rn == NULL ? NULL : (*rn)->data;
183
259k
}
184
185
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
186
    OPENSSL_LH_DOALL_FUNC_THUNK wfunc,
187
    OPENSSL_LH_DOALL_FUNC func,
188
    OPENSSL_LH_DOALL_FUNCARG func_arg,
189
    OPENSSL_LH_DOALL_FUNCARG_THUNK wfunc_arg,
190
    void *arg)
191
2.75k
{
192
2.75k
    int i;
193
2.75k
    OPENSSL_LH_NODE *a, *n;
194
195
2.75k
    if (lh == NULL)
196
0
        return;
197
198
    /*
199
     * reverse the order so we search from 'top to bottom' We were having
200
     * memory leaks otherwise
201
     */
202
35.6k
    for (i = lh->num_nodes - 1; i >= 0; i--) {
203
32.9k
        a = lh->b[i];
204
49.6k
        while (a != NULL) {
205
16.6k
            n = a->next;
206
16.6k
            if (use_arg)
207
7.74k
                wfunc_arg(a->data, arg, func_arg);
208
8.93k
            else
209
8.93k
                wfunc(a->data, func);
210
16.6k
            a = n;
211
16.6k
        }
212
32.9k
    }
213
2.75k
}
214
215
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
216
2.72k
{
217
2.72k
    if (lh == NULL)
218
0
        return;
219
220
2.72k
    doall_util_fn(lh, 0, lh->daw, func, (OPENSSL_LH_DOALL_FUNCARG)NULL,
221
2.72k
        (OPENSSL_LH_DOALL_FUNCARG_THUNK)NULL, NULL);
222
2.72k
}
223
224
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh,
225
    OPENSSL_LH_DOALL_FUNCARG func, void *arg)
226
0
{
227
0
    if (lh == NULL)
228
0
        return;
229
230
0
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
231
0
        (OPENSSL_LH_DOALL_FUNC)NULL, func, lh->daaw, arg);
232
0
}
233
234
void OPENSSL_LH_doall_arg_thunk(OPENSSL_LHASH *lh,
235
    OPENSSL_LH_DOALL_FUNCARG_THUNK daaw,
236
    OPENSSL_LH_DOALL_FUNCARG fn, void *arg)
237
32
{
238
32
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC_THUNK)NULL,
239
32
        (OPENSSL_LH_DOALL_FUNC)NULL, fn, daaw, arg);
240
32
}
241
242
static int expand(OPENSSL_LHASH *lh)
243
16.5k
{
244
16.5k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
245
16.5k
    unsigned int p, pmax, nni, j;
246
16.5k
    unsigned long hash;
247
248
16.5k
    nni = lh->num_alloc_nodes;
249
16.5k
    p = lh->p;
250
16.5k
    pmax = lh->pmax;
251
16.5k
    if (p + 1 >= pmax) {
252
144
        j = nni * 2;
253
144
        n = OPENSSL_realloc_array(lh->b, j, sizeof(OPENSSL_LH_NODE *));
254
144
        if (n == NULL) {
255
0
            lh->error++;
256
0
            return 0;
257
0
        }
258
144
        lh->b = n;
259
144
        memset(n + nni, 0, sizeof(*n) * (j - nni));
260
144
        lh->pmax = nni;
261
144
        lh->num_alloc_nodes = j;
262
144
        lh->p = 0;
263
16.4k
    } else {
264
16.4k
        lh->p++;
265
16.4k
    }
266
267
16.5k
    lh->num_nodes++;
268
16.5k
    n1 = &(lh->b[p]);
269
16.5k
    n2 = &(lh->b[p + pmax]);
270
16.5k
    *n2 = NULL;
271
272
67.7k
    for (np = *n1; np != NULL;) {
273
51.2k
        hash = np->hash;
274
51.2k
        if ((hash % nni) != p) { /* move it */
275
8.86k
            *n1 = (*n1)->next;
276
8.86k
            np->next = *n2;
277
8.86k
            *n2 = np;
278
8.86k
        } else
279
42.3k
            n1 = &((*n1)->next);
280
51.2k
        np = *n1;
281
51.2k
    }
282
283
16.5k
    return 1;
284
16.5k
}
285
286
static void contract(OPENSSL_LHASH *lh)
287
16
{
288
16
    OPENSSL_LH_NODE **n, *n1, *np;
289
290
16
    np = lh->b[lh->p + lh->pmax - 1];
291
16
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
292
16
    if (lh->p == 0) {
293
0
        n = OPENSSL_realloc_array(lh->b, lh->pmax, sizeof(OPENSSL_LH_NODE *));
294
0
        if (n == NULL) {
295
            /* fputs("realloc error in lhash", stderr); */
296
0
            lh->error++;
297
0
        } else {
298
0
            lh->b = n;
299
0
        }
300
0
        lh->num_alloc_nodes /= 2;
301
0
        lh->pmax /= 2;
302
0
        lh->p = lh->pmax - 1;
303
0
    } else
304
16
        lh->p--;
305
306
16
    lh->num_nodes--;
307
308
16
    n1 = lh->b[(int)lh->p];
309
16
    if (n1 == NULL)
310
16
        lh->b[(int)lh->p] = np;
311
0
    else {
312
0
        while (n1->next != NULL)
313
0
            n1 = n1->next;
314
0
        n1->next = np;
315
0
    }
316
16
}
317
318
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
319
    const void *data, unsigned long *rhash)
320
345k
{
321
345k
    OPENSSL_LH_NODE **ret, *n1;
322
345k
    unsigned long hash, nn;
323
324
345k
    if (lh->hashw != NULL)
325
345k
        hash = lh->hashw(data, lh->hash);
326
0
    else
327
0
        hash = lh->hash(data);
328
329
345k
    *rhash = hash;
330
331
345k
    nn = hash % lh->pmax;
332
345k
    if (nn < lh->p)
333
162k
        nn = hash % lh->num_alloc_nodes;
334
335
345k
    ret = &(lh->b[(int)nn]);
336
589k
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
337
532k
        if (n1->hash != hash) {
338
238k
            ret = &(n1->next);
339
238k
            continue;
340
238k
        }
341
342
294k
        if (lh->compw != NULL) {
343
294k
            if (lh->compw(n1->data, data, lh->comp) == 0)
344
288k
                break;
345
294k
        } else {
346
0
            if (lh->comp(n1->data, data) == 0)
347
0
                break;
348
0
        }
349
6.02k
        ret = &(n1->next);
350
6.02k
    }
351
345k
    return ret;
352
345k
}
353
354
/*
355
 * The following hash seems to work very well on normal text strings no
356
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
357
 * as good as MD5, but still good.
358
 */
359
unsigned long OPENSSL_LH_strhash(const char *c)
360
119k
{
361
119k
    unsigned long ret = 0;
362
119k
    long n;
363
119k
    unsigned long v;
364
119k
    int r;
365
366
119k
    if ((c == NULL) || (*c == '\0'))
367
116k
        return ret;
368
369
3.07k
    n = 0x100;
370
47.6k
    while (*c) {
371
44.5k
        v = n | (*c);
372
44.5k
        n += 0x100;
373
44.5k
        r = (int)((v >> 2) ^ v) & 0x0f;
374
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
375
44.5k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
376
44.5k
        ret &= 0xFFFFFFFFL;
377
44.5k
        ret ^= v * v;
378
44.5k
        c++;
379
44.5k
    }
380
3.07k
    return (ret >> 16) ^ ret;
381
119k
}
382
383
/*
384
 * Case insensitive string hashing.
385
 *
386
 * The lower/upper case bit is masked out (forcing all letters to be capitals).
387
 * The major side effect on non-alpha characters is mapping the symbols and
388
 * digits into the control character range (which should be harmless).
389
 * The duplication (with respect to the hash value) of printable characters
390
 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
391
 * respectively (which seems tolerable).
392
 *
393
 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
394
 * characters.  The only duplication is '0' mapping to '^', which is better
395
 * than for ASCII.
396
 */
397
unsigned long ossl_lh_strcasehash(const char *c)
398
21.0k
{
399
21.0k
    unsigned long ret = 0;
400
21.0k
    long n;
401
21.0k
    unsigned long v;
402
21.0k
    int r;
403
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
404
    const long int case_adjust = ~0x40;
405
#else
406
21.0k
    const long int case_adjust = ~0x20;
407
21.0k
#endif
408
409
21.0k
    if (c == NULL || *c == '\0')
410
0
        return ret;
411
412
271k
    for (n = 0x100; *c != '\0'; n += 0x100) {
413
250k
        v = n | (case_adjust & *c);
414
250k
        r = (int)((v >> 2) ^ v) & 0x0f;
415
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
416
250k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
417
250k
        ret &= 0xFFFFFFFFL;
418
250k
        ret ^= v * v;
419
250k
        c++;
420
250k
    }
421
21.0k
    return (ret >> 16) ^ ret;
422
21.0k
}
423
424
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
425
0
{
426
0
    return lh ? lh->num_items : 0;
427
0
}
428
429
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
430
64
{
431
64
    return lh->down_load;
432
64
}
433
434
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
435
112
{
436
112
    lh->down_load = down_load;
437
112
}
438
439
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
440
4.11k
{
441
4.11k
    return lh->error;
442
4.11k
}