Coverage Report

Created: 2025-06-13 06:58

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