Coverage Report

Created: 2024-05-21 06:52

/src/openssl/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
16.2k
#define MIN_NODES       16
40
4.21k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
4.21k
#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
4.21k
{
49
4.21k
    OPENSSL_LHASH *ret;
50
51
4.21k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52
0
        return NULL;
53
4.21k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54
0
        goto err;
55
4.21k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
56
4.21k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
57
4.21k
    ret->num_nodes = MIN_NODES / 2;
58
4.21k
    ret->num_alloc_nodes = MIN_NODES;
59
4.21k
    ret->pmax = MIN_NODES / 2;
60
4.21k
    ret->up_load = UP_LOAD;
61
4.21k
    ret->down_load = DOWN_LOAD;
62
4.21k
    return ret;
63
64
0
err:
65
0
    OPENSSL_free(ret->b);
66
0
    OPENSSL_free(ret);
67
0
    return NULL;
68
4.21k
}
69
70
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
71
4.23k
{
72
4.23k
    if (lh == NULL)
73
15
        return;
74
75
4.21k
    OPENSSL_LH_flush(lh);
76
4.21k
    OPENSSL_free(lh->b);
77
4.21k
    OPENSSL_free(lh);
78
4.21k
}
79
80
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
81
4.46k
{
82
4.46k
    unsigned int i;
83
4.46k
    OPENSSL_LH_NODE *n, *nn;
84
85
4.46k
    if (lh == NULL)
86
0
        return;
87
88
57.1k
    for (i = 0; i < lh->num_nodes; i++) {
89
52.6k
        n = lh->b[i];
90
84.3k
        while (n != NULL) {
91
31.7k
            nn = n->next;
92
31.7k
            OPENSSL_free(n);
93
31.7k
            n = nn;
94
31.7k
        }
95
52.6k
        lh->b[i] = NULL;
96
52.6k
    }
97
98
4.46k
    lh->num_items = 0;
99
4.46k
}
100
101
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
102
80.6k
{
103
80.6k
    unsigned long hash;
104
80.6k
    OPENSSL_LH_NODE *nn, **rn;
105
80.6k
    void *ret;
106
107
80.6k
    lh->error = 0;
108
80.6k
    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
109
0
        return NULL;        /* 'lh->error++' already done in 'expand' */
110
111
80.6k
    rn = getrn(lh, data, &hash);
112
113
80.6k
    if (*rn == NULL) {
114
35.3k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
115
0
            lh->error++;
116
0
            return NULL;
117
0
        }
118
35.3k
        nn->data = data;
119
35.3k
        nn->next = NULL;
120
35.3k
        nn->hash = hash;
121
35.3k
        *rn = nn;
122
35.3k
        ret = NULL;
123
35.3k
        lh->num_items++;
124
45.3k
    } else {                    /* replace same key */
125
45.3k
        ret = (*rn)->data;
126
45.3k
        (*rn)->data = data;
127
45.3k
    }
128
80.6k
    return ret;
129
80.6k
}
130
131
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
132
3.63k
{
133
3.63k
    unsigned long hash;
134
3.63k
    OPENSSL_LH_NODE *nn, **rn;
135
3.63k
    void *ret;
136
137
3.63k
    lh->error = 0;
138
3.63k
    rn = getrn(lh, data, &hash);
139
140
3.63k
    if (*rn == NULL) {
141
0
        return NULL;
142
3.63k
    } else {
143
3.63k
        nn = *rn;
144
3.63k
        *rn = nn->next;
145
3.63k
        ret = nn->data;
146
3.63k
        OPENSSL_free(nn);
147
3.63k
    }
148
149
3.63k
    lh->num_items--;
150
3.63k
    if ((lh->num_nodes > MIN_NODES) &&
151
3.63k
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
152
15
        contract(lh);
153
154
3.63k
    return ret;
155
3.63k
}
156
157
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
158
5.51M
{
159
5.51M
    unsigned long hash;
160
5.51M
    OPENSSL_LH_NODE **rn;
161
162
5.51M
    if (lh->error != 0)
163
0
        lh->error = 0;
164
165
5.51M
    rn = getrn(lh, data, &hash);
166
167
5.51M
    return *rn == NULL ? NULL : (*rn)->data;
168
5.51M
}
169
170
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
171
                          OPENSSL_LH_DOALL_FUNC func,
172
                          OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
173
403k
{
174
403k
    int i;
175
403k
    OPENSSL_LH_NODE *a, *n;
176
177
403k
    if (lh == NULL)
178
0
        return;
179
180
    /*
181
     * reverse the order so we search from 'top to bottom' We were having
182
     * memory leaks otherwise
183
     */
184
82.1M
    for (i = lh->num_nodes - 1; i >= 0; i--) {
185
81.7M
        a = lh->b[i];
186
244M
        while (a != NULL) {
187
162M
            n = a->next;
188
162M
            if (use_arg)
189
162M
                func_arg(a->data, arg);
190
14.8k
            else
191
14.8k
                func(a->data);
192
162M
            a = n;
193
162M
        }
194
81.7M
    }
195
403k
}
196
197
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
198
3.13k
{
199
3.13k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
200
3.13k
}
201
202
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
203
400k
{
204
400k
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
205
400k
}
206
207
static int expand(OPENSSL_LHASH *lh)
208
16.9k
{
209
16.9k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
210
16.9k
    unsigned int p, pmax, nni, j;
211
16.9k
    unsigned long hash;
212
213
16.9k
    nni = lh->num_alloc_nodes;
214
16.9k
    p = lh->p;
215
16.9k
    pmax = lh->pmax;
216
16.9k
    if (p + 1 >= pmax) {
217
195
        j = nni * 2;
218
195
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
219
195
        if (n == NULL) {
220
0
            lh->error++;
221
0
            return 0;
222
0
        }
223
195
        lh->b = n;
224
195
        memset(n + nni, 0, sizeof(*n) * (j - nni));
225
195
        lh->pmax = nni;
226
195
        lh->num_alloc_nodes = j;
227
195
        lh->p = 0;
228
16.7k
    } else {
229
16.7k
        lh->p++;
230
16.7k
    }
231
232
16.9k
    lh->num_nodes++;
233
16.9k
    n1 = &(lh->b[p]);
234
16.9k
    n2 = &(lh->b[p + pmax]);
235
16.9k
    *n2 = NULL;
236
237
70.8k
    for (np = *n1; np != NULL;) {
238
53.8k
        hash = np->hash;
239
53.8k
        if ((hash % nni) != p) { /* move it */
240
12.3k
            *n1 = (*n1)->next;
241
12.3k
            np->next = *n2;
242
12.3k
            *n2 = np;
243
12.3k
        } else
244
41.4k
            n1 = &((*n1)->next);
245
53.8k
        np = *n1;
246
53.8k
    }
247
248
16.9k
    return 1;
249
16.9k
}
250
251
static void contract(OPENSSL_LHASH *lh)
252
15
{
253
15
    OPENSSL_LH_NODE **n, *n1, *np;
254
255
15
    np = lh->b[lh->p + lh->pmax - 1];
256
15
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
257
15
    if (lh->p == 0) {
258
0
        n = OPENSSL_realloc(lh->b,
259
0
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
260
0
        if (n == NULL) {
261
            /* fputs("realloc error in lhash", stderr); */
262
0
            lh->error++;
263
0
        } else {
264
0
            lh->b = n;
265
0
        }
266
0
        lh->num_alloc_nodes /= 2;
267
0
        lh->pmax /= 2;
268
0
        lh->p = lh->pmax - 1;
269
0
    } else
270
15
        lh->p--;
271
272
15
    lh->num_nodes--;
273
274
15
    n1 = lh->b[(int)lh->p];
275
15
    if (n1 == NULL)
276
15
        lh->b[(int)lh->p] = np;
277
0
    else {
278
0
        while (n1->next != NULL)
279
0
            n1 = n1->next;
280
0
        n1->next = np;
281
0
    }
282
15
}
283
284
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
285
                               const void *data, unsigned long *rhash)
286
5.59M
{
287
5.59M
    OPENSSL_LH_NODE **ret, *n1;
288
5.59M
    unsigned long hash, nn;
289
5.59M
    OPENSSL_LH_COMPFUNC cf;
290
291
5.59M
    hash = (*(lh->hash)) (data);
292
5.59M
    *rhash = hash;
293
294
5.59M
    nn = hash % lh->pmax;
295
5.59M
    if (nn < lh->p)
296
2.95M
        nn = hash % lh->num_alloc_nodes;
297
298
5.59M
    cf = lh->comp;
299
5.59M
    ret = &(lh->b[(int)nn]);
300
9.45M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
301
9.10M
        if (n1->hash != hash) {
302
3.85M
            ret = &(n1->next);
303
3.85M
            continue;
304
3.85M
        }
305
5.25M
        if (cf(n1->data, data) == 0)
306
5.24M
            break;
307
2.67k
        ret = &(n1->next);
308
2.67k
    }
309
5.59M
    return ret;
310
5.59M
}
311
312
/*
313
 * The following hash seems to work very well on normal text strings no
314
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
315
 * as good as MD5, but still good.
316
 */
317
unsigned long OPENSSL_LH_strhash(const char *c)
318
557k
{
319
557k
    unsigned long ret = 0;
320
557k
    long n;
321
557k
    unsigned long v;
322
557k
    int r;
323
324
557k
    if ((c == NULL) || (*c == '\0'))
325
548k
        return ret;
326
327
8.10k
    n = 0x100;
328
116k
    while (*c) {
329
108k
        v = n | (*c);
330
108k
        n += 0x100;
331
108k
        r = (int)((v >> 2) ^ v) & 0x0f;
332
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
333
108k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
334
108k
        ret &= 0xFFFFFFFFL;
335
108k
        ret ^= v * v;
336
108k
        c++;
337
108k
    }
338
8.10k
    return (ret >> 16) ^ ret;
339
557k
}
340
341
/*
342
 * Case insensitive string hashing.
343
 *
344
 * The lower/upper case bit is masked out (forcing all letters to be capitals).
345
 * The major side effect on non-alpha characters is mapping the symbols and
346
 * digits into the control character range (which should be harmless).
347
 * The duplication (with respect to the hash value) of printable characters
348
 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
349
 * respectively (which seems tolerable).
350
 *
351
 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
352
 * characters.  The only duplication is '0' mapping to '^', which is better
353
 * than for ASCII.
354
 */
355
unsigned long ossl_lh_strcasehash(const char *c)
356
5.69M
{
357
5.69M
    unsigned long ret = 0;
358
5.69M
    long n;
359
5.69M
    unsigned long v;
360
5.69M
    int r;
361
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
362
    const long int case_adjust = ~0x40;
363
#else
364
5.69M
    const long int case_adjust = ~0x20;
365
5.69M
#endif
366
367
5.69M
    if (c == NULL || *c == '\0')
368
0
        return ret;
369
370
50.3M
    for (n = 0x100; *c != '\0'; n += 0x100) {
371
44.6M
        v = n | (case_adjust & *c);
372
44.6M
        r = (int)((v >> 2) ^ v) & 0x0f;
373
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
374
44.6M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
375
44.6M
        ret &= 0xFFFFFFFFL;
376
44.6M
        ret ^= v * v;
377
44.6M
        c++;
378
44.6M
    }
379
5.69M
    return (ret >> 16) ^ ret;
380
5.69M
}
381
382
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
383
401k
{
384
401k
    return lh ? lh->num_items : 0;
385
401k
}
386
387
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
388
1.42k
{
389
1.42k
    return lh->down_load;
390
1.42k
}
391
392
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
393
2.83k
{
394
2.83k
    lh->down_load = down_load;
395
2.83k
}
396
397
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
398
10.3k
{
399
10.3k
    return lh->error;
400
10.3k
}