Coverage Report

Created: 2023-09-25 06:45

/src/openssl/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
228k
#define MIN_NODES       16
40
48.8k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
48.8k
#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
48.8k
{
49
48.8k
    OPENSSL_LHASH *ret;
50
51
48.8k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52
0
        return NULL;
53
48.8k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54
0
        goto err;
55
48.8k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
56
48.8k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
57
48.8k
    ret->num_nodes = MIN_NODES / 2;
58
48.8k
    ret->num_alloc_nodes = MIN_NODES;
59
48.8k
    ret->pmax = MIN_NODES / 2;
60
48.8k
    ret->up_load = UP_LOAD;
61
48.8k
    ret->down_load = DOWN_LOAD;
62
48.8k
    return ret;
63
64
0
err:
65
0
    OPENSSL_free(ret->b);
66
0
    OPENSSL_free(ret);
67
0
    return NULL;
68
48.8k
}
69
70
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
71
32.7k
{
72
32.7k
    if (lh == NULL)
73
54
        return;
74
75
32.7k
    OPENSSL_LH_flush(lh);
76
32.7k
    OPENSSL_free(lh->b);
77
32.7k
    OPENSSL_free(lh);
78
32.7k
}
79
80
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
81
33.6k
{
82
33.6k
    unsigned int i;
83
33.6k
    OPENSSL_LH_NODE *n, *nn;
84
85
33.6k
    if (lh == NULL)
86
0
        return;
87
88
382k
    for (i = 0; i < lh->num_nodes; i++) {
89
348k
        n = lh->b[i];
90
471k
        while (n != NULL) {
91
122k
            nn = n->next;
92
122k
            OPENSSL_free(n);
93
122k
            n = nn;
94
122k
        }
95
348k
        lh->b[i] = NULL;
96
348k
    }
97
98
33.6k
    lh->num_items = 0;
99
33.6k
}
100
101
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
102
571k
{
103
571k
    unsigned long hash;
104
571k
    OPENSSL_LH_NODE *nn, **rn;
105
571k
    void *ret;
106
107
571k
    lh->error = 0;
108
571k
    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
571k
    rn = getrn(lh, data, &hash);
112
113
571k
    if (*rn == NULL) {
114
276k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
115
0
            lh->error++;
116
0
            return NULL;
117
0
        }
118
276k
        nn->data = data;
119
276k
        nn->next = NULL;
120
276k
        nn->hash = hash;
121
276k
        *rn = nn;
122
276k
        ret = NULL;
123
276k
        lh->num_items++;
124
294k
    } else {                    /* replace same key */
125
294k
        ret = (*rn)->data;
126
294k
        (*rn)->data = data;
127
294k
    }
128
571k
    return ret;
129
571k
}
130
131
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
132
82.4k
{
133
82.4k
    unsigned long hash;
134
82.4k
    OPENSSL_LH_NODE *nn, **rn;
135
82.4k
    void *ret;
136
137
82.4k
    lh->error = 0;
138
82.4k
    rn = getrn(lh, data, &hash);
139
140
82.4k
    if (*rn == NULL) {
141
0
        return NULL;
142
82.4k
    } else {
143
82.4k
        nn = *rn;
144
82.4k
        *rn = nn->next;
145
82.4k
        ret = nn->data;
146
82.4k
        OPENSSL_free(nn);
147
82.4k
    }
148
149
82.4k
    lh->num_items--;
150
82.4k
    if ((lh->num_nodes > MIN_NODES) &&
151
82.4k
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
152
31
        contract(lh);
153
154
82.4k
    return ret;
155
82.4k
}
156
157
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
158
240M
{
159
240M
    unsigned long hash;
160
240M
    OPENSSL_LH_NODE **rn;
161
162
240M
    if (lh->error != 0)
163
0
        lh->error = 0;
164
165
240M
    rn = getrn(lh, data, &hash);
166
167
240M
    return *rn == NULL ? NULL : (*rn)->data;
168
240M
}
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
7.10M
{
174
7.10M
    int i;
175
7.10M
    OPENSSL_LH_NODE *a, *n;
176
177
7.10M
    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
429M
    for (i = lh->num_nodes - 1; i >= 0; i--) {
185
422M
        a = lh->b[i];
186
1.26G
        while (a != NULL) {
187
840M
            n = a->next;
188
840M
            if (use_arg)
189
840M
                func_arg(a->data, arg);
190
69.6k
            else
191
69.6k
                func(a->data);
192
840M
            a = n;
193
840M
        }
194
422M
    }
195
7.10M
}
196
197
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
198
11.8k
{
199
11.8k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
200
11.8k
}
201
202
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
203
7.09M
{
204
7.09M
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
205
7.09M
}
206
207
static int expand(OPENSSL_LHASH *lh)
208
123k
{
209
123k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
210
123k
    unsigned int p, pmax, nni, j;
211
123k
    unsigned long hash;
212
213
123k
    nni = lh->num_alloc_nodes;
214
123k
    p = lh->p;
215
123k
    pmax = lh->pmax;
216
123k
    if (p + 1 >= pmax) {
217
1.80k
        j = nni * 2;
218
1.80k
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
219
1.80k
        if (n == NULL) {
220
0
            lh->error++;
221
0
            return 0;
222
0
        }
223
1.80k
        lh->b = n;
224
1.80k
        memset(n + nni, 0, sizeof(*n) * (j - nni));
225
1.80k
        lh->pmax = nni;
226
1.80k
        lh->num_alloc_nodes = j;
227
1.80k
        lh->p = 0;
228
121k
    } else {
229
121k
        lh->p++;
230
121k
    }
231
232
123k
    lh->num_nodes++;
233
123k
    n1 = &(lh->b[p]);
234
123k
    n2 = &(lh->b[p + pmax]);
235
123k
    *n2 = NULL;
236
237
522k
    for (np = *n1; np != NULL;) {
238
399k
        hash = np->hash;
239
399k
        if ((hash % nni) != p) { /* move it */
240
100k
            *n1 = (*n1)->next;
241
100k
            np->next = *n2;
242
100k
            *n2 = np;
243
100k
        } else
244
298k
            n1 = &((*n1)->next);
245
399k
        np = *n1;
246
399k
    }
247
248
123k
    return 1;
249
123k
}
250
251
static void contract(OPENSSL_LHASH *lh)
252
25
{
253
25
    OPENSSL_LH_NODE **n, *n1, *np;
254
255
25
    np = lh->b[lh->p + lh->pmax - 1];
256
25
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
257
25
    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
25
        lh->p--;
271
272
25
    lh->num_nodes--;
273
274
25
    n1 = lh->b[(int)lh->p];
275
25
    if (n1 == NULL)
276
25
        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
25
}
283
284
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
285
                               const void *data, unsigned long *rhash)
286
241M
{
287
241M
    OPENSSL_LH_NODE **ret, *n1;
288
241M
    unsigned long hash, nn;
289
241M
    OPENSSL_LH_COMPFUNC cf;
290
291
241M
    hash = (*(lh->hash)) (data);
292
241M
    *rhash = hash;
293
294
241M
    nn = hash % lh->pmax;
295
241M
    if (nn < lh->p)
296
155M
        nn = hash % lh->num_alloc_nodes;
297
298
241M
    cf = lh->comp;
299
241M
    ret = &(lh->b[(int)nn]);
300
904M
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
301
863M
        if (n1->hash != hash) {
302
662M
            ret = &(n1->next);
303
662M
            continue;
304
662M
        }
305
201M
        if (cf(n1->data, data) == 0)
306
201M
            break;
307
134k
        ret = &(n1->next);
308
134k
    }
309
241M
    return ret;
310
241M
}
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
10.5M
{
319
10.5M
    unsigned long ret = 0;
320
10.5M
    long n;
321
10.5M
    unsigned long v;
322
10.5M
    int r;
323
324
10.5M
    if ((c == NULL) || (*c == '\0'))
325
3.22M
        return ret;
326
327
7.34M
    n = 0x100;
328
287M
    while (*c) {
329
280M
        v = n | (*c);
330
280M
        n += 0x100;
331
280M
        r = (int)((v >> 2) ^ v) & 0x0f;
332
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
333
280M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
334
280M
        ret &= 0xFFFFFFFFL;
335
280M
        ret ^= v * v;
336
280M
        c++;
337
280M
    }
338
7.34M
    return (ret >> 16) ^ ret;
339
10.5M
}
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
231M
{
357
231M
    unsigned long ret = 0;
358
231M
    long n;
359
231M
    unsigned long v;
360
231M
    int r;
361
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
362
    const long int case_adjust = ~0x40;
363
#else
364
231M
    const long int case_adjust = ~0x20;
365
231M
#endif
366
367
231M
    if (c == NULL || *c == '\0')
368
0
        return ret;
369
370
1.43G
    for (n = 0x100; *c != '\0'; n += 0x100) {
371
1.19G
        v = n | (case_adjust & *c);
372
1.19G
        r = (int)((v >> 2) ^ v) & 0x0f;
373
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
374
1.19G
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
375
1.19G
        ret &= 0xFFFFFFFFL;
376
1.19G
        ret ^= v * v;
377
1.19G
        c++;
378
1.19G
    }
379
231M
    return (ret >> 16) ^ ret;
380
231M
}
381
382
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
383
2.05M
{
384
2.05M
    return lh ? lh->num_items : 0;
385
2.05M
}
386
387
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
388
37.9k
{
389
37.9k
    return lh->down_load;
390
37.9k
}
391
392
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
393
84.5k
{
394
84.5k
    lh->down_load = down_load;
395
84.5k
}
396
397
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
398
62.0k
{
399
62.0k
    return lh->error;
400
62.0k
}