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