Coverage Report

Created: 2023-06-08 06:43

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