Coverage Report

Created: 2025-08-08 06:10

/src/rauc/subprojects/openssl-3.0.8/crypto/lhash/lhash.c
Line
Count
Source (jump to first uncovered line)
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
438
#define MIN_NODES       16
40
37
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
37
#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
37
{
49
37
    OPENSSL_LHASH *ret;
50
51
37
    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
37
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
60
0
        goto err;
61
37
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62
37
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
63
37
    ret->num_nodes = MIN_NODES / 2;
64
37
    ret->num_alloc_nodes = MIN_NODES;
65
37
    ret->pmax = MIN_NODES / 2;
66
37
    ret->up_load = UP_LOAD;
67
37
    ret->down_load = DOWN_LOAD;
68
37
    return ret;
69
70
0
err:
71
0
    OPENSSL_free(ret->b);
72
0
    OPENSSL_free(ret);
73
0
    return NULL;
74
37
}
75
76
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
77
38
{
78
38
    if (lh == NULL)
79
1
        return;
80
81
37
    OPENSSL_LH_flush(lh);
82
37
    OPENSSL_free(lh->b);
83
37
    OPENSSL_free(lh);
84
37
}
85
86
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
87
64
{
88
64
    unsigned int i;
89
64
    OPENSSL_LH_NODE *n, *nn;
90
91
64
    if (lh == NULL)
92
0
        return;
93
94
1.55k
    for (i = 0; i < lh->num_nodes; i++) {
95
1.49k
        n = lh->b[i];
96
3.22k
        while (n != NULL) {
97
1.73k
            nn = n->next;
98
1.73k
            OPENSSL_free(n);
99
1.73k
            n = nn;
100
1.73k
        }
101
1.49k
        lh->b[i] = NULL;
102
1.49k
    }
103
104
64
    lh->num_items = 0;
105
64
}
106
107
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
108
4.94k
{
109
4.94k
    unsigned long hash;
110
4.94k
    OPENSSL_LH_NODE *nn, **rn;
111
4.94k
    void *ret;
112
113
4.94k
    lh->error = 0;
114
4.94k
    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
4.94k
    rn = getrn(lh, data, &hash);
118
119
4.94k
    if (*rn == NULL) {
120
2.06k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
121
0
            lh->error++;
122
0
            return NULL;
123
0
        }
124
2.06k
        nn->data = data;
125
2.06k
        nn->next = NULL;
126
2.06k
        nn->hash = hash;
127
2.06k
        *rn = nn;
128
2.06k
        ret = NULL;
129
2.06k
        lh->num_items++;
130
2.88k
    } else {                    /* replace same key */
131
2.88k
        ret = (*rn)->data;
132
2.88k
        (*rn)->data = data;
133
2.88k
    }
134
4.94k
    return ret;
135
4.94k
}
136
137
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
138
327
{
139
327
    unsigned long hash;
140
327
    OPENSSL_LH_NODE *nn, **rn;
141
327
    void *ret;
142
143
327
    lh->error = 0;
144
327
    rn = getrn(lh, data, &hash);
145
146
327
    if (*rn == NULL) {
147
0
        return NULL;
148
327
    } else {
149
327
        nn = *rn;
150
327
        *rn = nn->next;
151
327
        ret = nn->data;
152
327
        OPENSSL_free(nn);
153
327
    }
154
155
327
    lh->num_items--;
156
327
    if ((lh->num_nodes > MIN_NODES) &&
157
327
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
158
1
        contract(lh);
159
160
327
    return ret;
161
327
}
162
163
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
164
300k
{
165
300k
    unsigned long hash;
166
300k
    OPENSSL_LH_NODE **rn;
167
168
300k
    if (lh->error != 0)
169
0
        lh->error = 0;
170
171
300k
    rn = getrn(lh, data, &hash);
172
173
300k
    return *rn == NULL ? NULL : (*rn)->data;
174
300k
}
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
6.47k
{
180
6.47k
    int i;
181
6.47k
    OPENSSL_LH_NODE *a, *n;
182
183
6.47k
    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
168k
    for (i = lh->num_nodes - 1; i >= 0; i--) {
191
161k
        a = lh->b[i];
192
483k
        while (a != NULL) {
193
321k
            n = a->next;
194
321k
            if (use_arg)
195
320k
                func_arg(a->data, arg);
196
848
            else
197
848
                func(a->data);
198
321k
            a = n;
199
321k
        }
200
161k
    }
201
6.47k
}
202
203
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
204
66
{
205
66
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
206
66
}
207
208
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
209
6.40k
{
210
6.40k
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
211
6.40k
}
212
213
static int expand(OPENSSL_LHASH *lh)
214
981
{
215
981
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
216
981
    unsigned int p, pmax, nni, j;
217
981
    unsigned long hash;
218
219
981
    nni = lh->num_alloc_nodes;
220
981
    p = lh->p;
221
981
    pmax = lh->pmax;
222
981
    if (p + 1 >= pmax) {
223
15
        j = nni * 2;
224
15
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
225
15
        if (n == NULL) {
226
0
            lh->error++;
227
0
            return 0;
228
0
        }
229
15
        lh->b = n;
230
15
        memset(n + nni, 0, sizeof(*n) * (j - nni));
231
15
        lh->pmax = nni;
232
15
        lh->num_alloc_nodes = j;
233
15
        lh->p = 0;
234
966
    } else {
235
966
        lh->p++;
236
966
    }
237
238
981
    lh->num_nodes++;
239
981
    n1 = &(lh->b[p]);
240
981
    n2 = &(lh->b[p + pmax]);
241
981
    *n2 = NULL;
242
243
4.23k
    for (np = *n1; np != NULL;) {
244
3.25k
        hash = np->hash;
245
3.25k
        if ((hash % nni) != p) { /* move it */
246
792
            *n1 = (*n1)->next;
247
792
            np->next = *n2;
248
792
            *n2 = np;
249
792
        } else
250
2.46k
            n1 = &((*n1)->next);
251
3.25k
        np = *n1;
252
3.25k
    }
253
254
981
    return 1;
255
981
}
256
257
static void contract(OPENSSL_LHASH *lh)
258
1
{
259
1
    OPENSSL_LH_NODE **n, *n1, *np;
260
261
1
    np = lh->b[lh->p + lh->pmax - 1];
262
1
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
263
1
    if (lh->p == 0) {
264
0
        n = OPENSSL_realloc(lh->b,
265
0
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
266
0
        if (n == NULL) {
267
            /* fputs("realloc error in lhash",stderr); */
268
0
            lh->error++;
269
0
            return;
270
0
        }
271
0
        lh->num_alloc_nodes /= 2;
272
0
        lh->pmax /= 2;
273
0
        lh->p = lh->pmax - 1;
274
0
        lh->b = n;
275
0
    } else
276
1
        lh->p--;
277
278
1
    lh->num_nodes--;
279
280
1
    n1 = lh->b[(int)lh->p];
281
1
    if (n1 == NULL)
282
1
        lh->b[(int)lh->p] = np;
283
0
    else {
284
0
        while (n1->next != NULL)
285
0
            n1 = n1->next;
286
0
        n1->next = np;
287
0
    }
288
1
}
289
290
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
291
                               const void *data, unsigned long *rhash)
292
305k
{
293
305k
    OPENSSL_LH_NODE **ret, *n1;
294
305k
    unsigned long hash, nn;
295
305k
    OPENSSL_LH_COMPFUNC cf;
296
297
305k
    hash = (*(lh->hash)) (data);
298
305k
    *rhash = hash;
299
300
305k
    nn = hash % lh->pmax;
301
305k
    if (nn < lh->p)
302
181k
        nn = hash % lh->num_alloc_nodes;
303
304
305k
    cf = lh->comp;
305
305k
    ret = &(lh->b[(int)nn]);
306
1.08M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
307
1.01M
        if (n1->hash != hash) {
308
776k
            ret = &(n1->next);
309
776k
            continue;
310
776k
        }
311
233k
        if (cf(n1->data, data) == 0)
312
233k
            break;
313
143
        ret = &(n1->next);
314
143
    }
315
305k
    return ret;
316
305k
}
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
8.19k
{
325
8.19k
    unsigned long ret = 0;
326
8.19k
    long n;
327
8.19k
    unsigned long v;
328
8.19k
    int r;
329
330
8.19k
    if ((c == NULL) || (*c == '\0'))
331
31
        return ret;
332
333
8.16k
    n = 0x100;
334
70.5k
    while (*c) {
335
62.4k
        v = n | (*c);
336
62.4k
        n += 0x100;
337
62.4k
        r = (int)((v >> 2) ^ v) & 0x0f;
338
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
339
62.4k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
340
62.4k
        ret &= 0xFFFFFFFFL;
341
62.4k
        ret ^= v * v;
342
62.4k
        c++;
343
62.4k
    }
344
8.16k
    return (ret >> 16) ^ ret;
345
8.19k
}
346
347
unsigned long ossl_lh_strcasehash(const char *c)
348
293k
{
349
293k
    unsigned long ret = 0;
350
293k
    long n;
351
293k
    unsigned long v;
352
293k
    int r;
353
354
293k
    if (c == NULL || *c == '\0')
355
0
        return ret;
356
357
1.92M
    for (n = 0x100; *c != '\0'; n += 0x100) {
358
1.63M
        v = n | ossl_tolower(*c);
359
1.63M
        r = (int)((v >> 2) ^ v) & 0x0f;
360
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
361
1.63M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
362
1.63M
        ret &= 0xFFFFFFFFL;
363
1.63M
        ret ^= v * v;
364
1.63M
        c++;
365
1.63M
    }
366
293k
    return (ret >> 16) ^ ret;
367
293k
}
368
369
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
370
892
{
371
892
    return lh ? lh->num_items : 0;
372
892
}
373
374
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
375
4
{
376
4
    return lh->down_load;
377
4
}
378
379
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
380
8
{
381
8
    lh->down_load = down_load;
382
8
}
383
384
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
385
555
{
386
555
    return lh->error;
387
555
}