Coverage Report

Created: 2023-09-25 06:41

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