Coverage Report

Created: 2023-09-25 06:41

/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
29.4k
#define MIN_NODES       16
40
3.13k
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
3.13k
#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
3.13k
{
49
3.13k
    OPENSSL_LHASH *ret;
50
51
3.13k
    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
3.13k
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
60
0
        goto err;
61
3.13k
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62
3.13k
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
63
3.13k
    ret->num_nodes = MIN_NODES / 2;
64
3.13k
    ret->num_alloc_nodes = MIN_NODES;
65
3.13k
    ret->pmax = MIN_NODES / 2;
66
3.13k
    ret->up_load = UP_LOAD;
67
3.13k
    ret->down_load = DOWN_LOAD;
68
3.13k
    return ret;
69
70
0
err:
71
0
    OPENSSL_free(ret->b);
72
0
    OPENSSL_free(ret);
73
0
    return NULL;
74
3.13k
}
75
76
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
77
3.13k
{
78
3.13k
    if (lh == NULL)
79
2
        return;
80
81
3.13k
    OPENSSL_LH_flush(lh);
82
3.13k
    OPENSSL_free(lh->b);
83
3.13k
    OPENSSL_free(lh);
84
3.13k
}
85
86
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
87
3.13k
{
88
3.13k
    unsigned int i;
89
3.13k
    OPENSSL_LH_NODE *n, *nn;
90
91
3.13k
    if (lh == NULL)
92
0
        return;
93
94
42.0k
    for (i = 0; i < lh->num_nodes; i++) {
95
38.8k
        n = lh->b[i];
96
55.8k
        while (n != NULL) {
97
16.9k
            nn = n->next;
98
16.9k
            OPENSSL_free(n);
99
16.9k
            n = nn;
100
16.9k
        }
101
38.8k
        lh->b[i] = NULL;
102
38.8k
    }
103
104
3.13k
    lh->num_items = 0;
105
3.13k
}
106
107
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
108
49.8k
{
109
49.8k
    unsigned long hash;
110
49.8k
    OPENSSL_LH_NODE *nn, **rn;
111
49.8k
    void *ret;
112
113
49.8k
    lh->error = 0;
114
49.8k
    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
49.8k
    rn = getrn(lh, data, &hash);
118
119
49.8k
    if (*rn == NULL) {
120
37.0k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
121
0
            lh->error++;
122
0
            return NULL;
123
0
        }
124
37.0k
        nn->data = data;
125
37.0k
        nn->next = NULL;
126
37.0k
        nn->hash = hash;
127
37.0k
        *rn = nn;
128
37.0k
        ret = NULL;
129
37.0k
        lh->num_items++;
130
37.0k
    } else {                    /* replace same key */
131
12.8k
        ret = (*rn)->data;
132
12.8k
        (*rn)->data = data;
133
12.8k
    }
134
49.8k
    return ret;
135
49.8k
}
136
137
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
138
20.0k
{
139
20.0k
    unsigned long hash;
140
20.0k
    OPENSSL_LH_NODE *nn, **rn;
141
20.0k
    void *ret;
142
143
20.0k
    lh->error = 0;
144
20.0k
    rn = getrn(lh, data, &hash);
145
146
20.0k
    if (*rn == NULL) {
147
0
        return NULL;
148
20.0k
    } else {
149
20.0k
        nn = *rn;
150
20.0k
        *rn = nn->next;
151
20.0k
        ret = nn->data;
152
20.0k
        OPENSSL_free(nn);
153
20.0k
    }
154
155
20.0k
    lh->num_items--;
156
20.0k
    if ((lh->num_nodes > MIN_NODES) &&
157
20.0k
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
158
0
        contract(lh);
159
160
20.0k
    return ret;
161
20.0k
}
162
163
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
164
26.8k
{
165
26.8k
    unsigned long hash;
166
26.8k
    OPENSSL_LH_NODE **rn;
167
168
26.8k
    if (lh->error != 0)
169
0
        lh->error = 0;
170
171
26.8k
    rn = getrn(lh, data, &hash);
172
173
26.8k
    return *rn == NULL ? NULL : (*rn)->data;
174
26.8k
}
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
6.26k
{
180
6.26k
    int i;
181
6.26k
    OPENSSL_LH_NODE *a, *n;
182
183
6.26k
    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
81.1k
    for (i = lh->num_nodes - 1; i >= 0; i--) {
191
74.8k
        a = lh->b[i];
192
123k
        while (a != NULL) {
193
48.2k
            n = a->next;
194
48.2k
            if (use_arg)
195
34.1k
                func_arg(a->data, arg);
196
14.0k
            else
197
14.0k
                func(a->data);
198
48.2k
            a = n;
199
48.2k
        }
200
74.8k
    }
201
6.26k
}
202
203
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
204
3.13k
{
205
3.13k
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
206
3.13k
}
207
208
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
209
3.13k
{
210
3.13k
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
211
3.13k
}
212
213
static int expand(OPENSSL_LHASH *lh)
214
13.7k
{
215
13.7k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
216
13.7k
    unsigned int p, pmax, nni, j;
217
13.7k
    unsigned long hash;
218
219
13.7k
    nni = lh->num_alloc_nodes;
220
13.7k
    p = lh->p;
221
13.7k
    pmax = lh->pmax;
222
13.7k
    if (p + 1 >= pmax) {
223
371
        j = nni * 2;
224
371
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
225
371
        if (n == NULL) {
226
0
            lh->error++;
227
0
            return 0;
228
0
        }
229
371
        lh->b = n;
230
371
        memset(n + nni, 0, sizeof(*n) * (j - nni));
231
371
        lh->pmax = nni;
232
371
        lh->num_alloc_nodes = j;
233
371
        lh->p = 0;
234
13.4k
    } else {
235
13.4k
        lh->p++;
236
13.4k
    }
237
238
13.7k
    lh->num_nodes++;
239
13.7k
    n1 = &(lh->b[p]);
240
13.7k
    n2 = &(lh->b[p + pmax]);
241
13.7k
    *n2 = NULL;
242
243
54.4k
    for (np = *n1; np != NULL;) {
244
40.6k
        hash = np->hash;
245
40.6k
        if ((hash % nni) != p) { /* move it */
246
16.4k
            *n1 = (*n1)->next;
247
16.4k
            np->next = *n2;
248
16.4k
            *n2 = np;
249
16.4k
        } else
250
24.1k
            n1 = &((*n1)->next);
251
40.6k
        np = *n1;
252
40.6k
    }
253
254
13.7k
    return 1;
255
13.7k
}
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
        } else {
270
0
            lh->b = n;
271
0
        }
272
0
        lh->num_alloc_nodes /= 2;
273
0
        lh->pmax /= 2;
274
0
        lh->p = lh->pmax - 1;
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
96.7k
{
293
96.7k
    OPENSSL_LH_NODE **ret, *n1;
294
96.7k
    unsigned long hash, nn;
295
96.7k
    OPENSSL_LH_COMPFUNC cf;
296
297
96.7k
    hash = (*(lh->hash)) (data);
298
96.7k
    *rhash = hash;
299
300
96.7k
    nn = hash % lh->pmax;
301
96.7k
    if (nn < lh->p)
302
31.6k
        nn = hash % lh->num_alloc_nodes;
303
304
96.7k
    cf = lh->comp;
305
96.7k
    ret = &(lh->b[(int)nn]);
306
463k
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
307
411k
        if (n1->hash != hash) {
308
331k
            ret = &(n1->next);
309
331k
            continue;
310
331k
        }
311
79.7k
        if (cf(n1->data, data) == 0)
312
44.8k
            break;
313
34.9k
        ret = &(n1->next);
314
34.9k
    }
315
96.7k
    return ret;
316
96.7k
}
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
176k
{
325
176k
    unsigned long ret = 0;
326
176k
    long n;
327
176k
    unsigned long v;
328
176k
    int r;
329
330
176k
    if ((c == NULL) || (*c == '\0'))
331
71.6k
        return ret;
332
333
104k
    n = 0x100;
334
68.3M
    while (*c) {
335
68.2M
        v = n | (*c);
336
68.2M
        n += 0x100;
337
68.2M
        r = (int)((v >> 2) ^ v) & 0x0f;
338
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
339
68.2M
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
340
68.2M
        ret &= 0xFFFFFFFFL;
341
68.2M
        ret ^= v * v;
342
68.2M
        c++;
343
68.2M
    }
344
104k
    return (ret >> 16) ^ ret;
345
176k
}
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
3.13k
{
381
3.13k
    lh->down_load = down_load;
382
3.13k
}
383
384
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
385
14.0k
{
386
14.0k
    return lh->error;
387
14.0k
}