Coverage Report

Created: 2023-06-08 06:40

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