Coverage Report

Created: 2023-06-07 07:24

/src/openssl/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
743
#define MIN_NODES       16
40
167
#define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41
167
#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
167
{
49
167
    OPENSSL_LHASH *ret;
50
51
167
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
52
0
        return NULL;
53
167
    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
54
0
        goto err;
55
167
    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
56
167
    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
57
167
    ret->num_nodes = MIN_NODES / 2;
58
167
    ret->num_alloc_nodes = MIN_NODES;
59
167
    ret->pmax = MIN_NODES / 2;
60
167
    ret->up_load = UP_LOAD;
61
167
    ret->down_load = DOWN_LOAD;
62
167
    return ret;
63
64
0
err:
65
0
    OPENSSL_free(ret->b);
66
0
    OPENSSL_free(ret);
67
0
    return NULL;
68
167
}
69
70
void OPENSSL_LH_free(OPENSSL_LHASH *lh)
71
168
{
72
168
    if (lh == NULL)
73
1
        return;
74
75
167
    OPENSSL_LH_flush(lh);
76
167
    OPENSSL_free(lh->b);
77
167
    OPENSSL_free(lh);
78
167
}
79
80
void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
81
167
{
82
167
    unsigned int i;
83
167
    OPENSSL_LH_NODE *n, *nn;
84
85
167
    if (lh == NULL)
86
0
        return;
87
88
2.69k
    for (i = 0; i < lh->num_nodes; i++) {
89
2.52k
        n = lh->b[i];
90
4.72k
        while (n != NULL) {
91
2.19k
            nn = n->next;
92
2.19k
            OPENSSL_free(n);
93
2.19k
            n = nn;
94
2.19k
        }
95
2.52k
        lh->b[i] = NULL;
96
2.52k
    }
97
98
167
    lh->num_items = 0;
99
167
}
100
101
void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
102
5.49k
{
103
5.49k
    unsigned long hash;
104
5.49k
    OPENSSL_LH_NODE *nn, **rn;
105
5.49k
    void *ret;
106
107
5.49k
    lh->error = 0;
108
5.49k
    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
109
0
        return NULL;        /* 'lh->error++' already done in 'expand' */
110
111
5.49k
    rn = getrn(lh, data, &hash);
112
113
5.49k
    if (*rn == NULL) {
114
2.44k
        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
115
0
            lh->error++;
116
0
            return NULL;
117
0
        }
118
2.44k
        nn->data = data;
119
2.44k
        nn->next = NULL;
120
2.44k
        nn->hash = hash;
121
2.44k
        *rn = nn;
122
2.44k
        ret = NULL;
123
2.44k
        lh->num_items++;
124
3.05k
    } else {                    /* replace same key */
125
3.05k
        ret = (*rn)->data;
126
3.05k
        (*rn)->data = data;
127
3.05k
    }
128
5.49k
    return ret;
129
5.49k
}
130
131
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
132
242
{
133
242
    unsigned long hash;
134
242
    OPENSSL_LH_NODE *nn, **rn;
135
242
    void *ret;
136
137
242
    lh->error = 0;
138
242
    rn = getrn(lh, data, &hash);
139
140
242
    if (*rn == NULL) {
141
0
        return NULL;
142
242
    } else {
143
242
        nn = *rn;
144
242
        *rn = nn->next;
145
242
        ret = nn->data;
146
242
        OPENSSL_free(nn);
147
242
    }
148
149
242
    lh->num_items--;
150
242
    if ((lh->num_nodes > MIN_NODES) &&
151
242
        (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
152
1
        contract(lh);
153
154
242
    return ret;
155
242
}
156
157
void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
158
2.36k
{
159
2.36k
    unsigned long hash;
160
2.36k
    OPENSSL_LH_NODE **rn;
161
162
2.36k
    if (lh->error != 0)
163
0
        lh->error = 0;
164
165
2.36k
    rn = getrn(lh, data, &hash);
166
167
2.36k
    return *rn == NULL ? NULL : (*rn)->data;
168
2.36k
}
169
170
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
171
                          OPENSSL_LH_DOALL_FUNC func,
172
                          OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
173
327
{
174
327
    int i;
175
327
    OPENSSL_LH_NODE *a, *n;
176
177
327
    if (lh == NULL)
178
0
        return;
179
180
    /*
181
     * reverse the order so we search from 'top to bottom' We were having
182
     * memory leaks otherwise
183
     */
184
27.8k
    for (i = lh->num_nodes - 1; i >= 0; i--) {
185
27.5k
        a = lh->b[i];
186
79.4k
        while (a != NULL) {
187
51.9k
            n = a->next;
188
51.9k
            if (use_arg)
189
50.9k
                func_arg(a->data, arg);
190
950
            else
191
950
                func(a->data);
192
51.9k
            a = n;
193
51.9k
        }
194
27.5k
    }
195
327
}
196
197
void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
198
169
{
199
169
    doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
200
169
}
201
202
void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
203
158
{
204
158
    doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
205
158
}
206
207
static int expand(OPENSSL_LHASH *lh)
208
1.19k
{
209
1.19k
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
210
1.19k
    unsigned int p, pmax, nni, j;
211
1.19k
    unsigned long hash;
212
213
1.19k
    nni = lh->num_alloc_nodes;
214
1.19k
    p = lh->p;
215
1.19k
    pmax = lh->pmax;
216
1.19k
    if (p + 1 >= pmax) {
217
13
        j = nni * 2;
218
13
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
219
13
        if (n == NULL) {
220
0
            lh->error++;
221
0
            return 0;
222
0
        }
223
13
        lh->b = n;
224
13
        memset(n + nni, 0, sizeof(*n) * (j - nni));
225
13
        lh->pmax = nni;
226
13
        lh->num_alloc_nodes = j;
227
13
        lh->p = 0;
228
1.17k
    } else {
229
1.17k
        lh->p++;
230
1.17k
    }
231
232
1.19k
    lh->num_nodes++;
233
1.19k
    n1 = &(lh->b[p]);
234
1.19k
    n2 = &(lh->b[p + pmax]);
235
1.19k
    *n2 = NULL;
236
237
4.89k
    for (np = *n1; np != NULL;) {
238
3.70k
        hash = np->hash;
239
3.70k
        if ((hash % nni) != p) { /* move it */
240
825
            *n1 = (*n1)->next;
241
825
            np->next = *n2;
242
825
            *n2 = np;
243
825
        } else
244
2.87k
            n1 = &((*n1)->next);
245
3.70k
        np = *n1;
246
3.70k
    }
247
248
1.19k
    return 1;
249
1.19k
}
250
251
static void contract(OPENSSL_LHASH *lh)
252
1
{
253
1
    OPENSSL_LH_NODE **n, *n1, *np;
254
255
1
    np = lh->b[lh->p + lh->pmax - 1];
256
1
    lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
257
1
    if (lh->p == 0) {
258
0
        n = OPENSSL_realloc(lh->b,
259
0
                            (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
260
0
        if (n == NULL) {
261
            /* fputs("realloc error in lhash", stderr); */
262
0
            lh->error++;
263
0
            return;
264
0
        }
265
0
        lh->num_alloc_nodes /= 2;
266
0
        lh->pmax /= 2;
267
0
        lh->p = lh->pmax - 1;
268
0
        lh->b = n;
269
0
    } else
270
1
        lh->p--;
271
272
1
    lh->num_nodes--;
273
274
1
    n1 = lh->b[(int)lh->p];
275
1
    if (n1 == NULL)
276
1
        lh->b[(int)lh->p] = np;
277
0
    else {
278
0
        while (n1->next != NULL)
279
0
            n1 = n1->next;
280
0
        n1->next = np;
281
0
    }
282
1
}
283
284
static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
285
                               const void *data, unsigned long *rhash)
286
8.09k
{
287
8.09k
    OPENSSL_LH_NODE **ret, *n1;
288
8.09k
    unsigned long hash, nn;
289
8.09k
    OPENSSL_LH_COMPFUNC cf;
290
291
8.09k
    hash = (*(lh->hash)) (data);
292
8.09k
    *rhash = hash;
293
294
8.09k
    nn = hash % lh->pmax;
295
8.09k
    if (nn < lh->p)
296
3.91k
        nn = hash % lh->num_alloc_nodes;
297
298
8.09k
    cf = lh->comp;
299
8.09k
    ret = &(lh->b[(int)nn]);
300
20.6k
    for (n1 = *ret; n1 != NULL; n1 = n1->next) {
301
17.4k
        if (n1->hash != hash) {
302
12.3k
            ret = &(n1->next);
303
12.3k
            continue;
304
12.3k
        }
305
5.07k
        if (cf(n1->data, data) == 0)
306
4.88k
            break;
307
193
        ret = &(n1->next);
308
193
    }
309
8.09k
    return ret;
310
8.09k
}
311
312
/*
313
 * The following hash seems to work very well on normal text strings no
314
 * collisions on /usr/dict/words and it distributes on %2^n quite well, not
315
 * as good as MD5, but still good.
316
 */
317
unsigned long OPENSSL_LH_strhash(const char *c)
318
201
{
319
201
    unsigned long ret = 0;
320
201
    long n;
321
201
    unsigned long v;
322
201
    int r;
323
324
201
    if ((c == NULL) || (*c == '\0'))
325
10
        return ret;
326
327
191
    n = 0x100;
328
2.96k
    while (*c) {
329
2.76k
        v = n | (*c);
330
2.76k
        n += 0x100;
331
2.76k
        r = (int)((v >> 2) ^ v) & 0x0f;
332
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
333
2.76k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
334
2.76k
        ret &= 0xFFFFFFFFL;
335
2.76k
        ret ^= v * v;
336
2.76k
        c++;
337
2.76k
    }
338
191
    return (ret >> 16) ^ ret;
339
201
}
340
341
/*
342
 * Case insensitive string hashing.
343
 *
344
 * The lower/upper case bit is masked out (forcing all letters to be capitals).
345
 * The major side effect on non-alpha characters is mapping the symbols and
346
 * digits into the control character range (which should be harmless).
347
 * The duplication (with respect to the hash value) of printable characters
348
 * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
349
 * respectively (which seems tolerable).
350
 *
351
 * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
352
 * characters.  The only duplication is '0' mapping to '^', which is better
353
 * than for ASCII.
354
 */
355
unsigned long ossl_lh_strcasehash(const char *c)
356
3.16k
{
357
3.16k
    unsigned long ret = 0;
358
3.16k
    long n;
359
3.16k
    unsigned long v;
360
3.16k
    int r;
361
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
362
    const long int case_adjust = ~0x40;
363
#else
364
3.16k
    const long int case_adjust = ~0x20;
365
3.16k
#endif
366
367
3.16k
    if (c == NULL || *c == '\0')
368
0
        return ret;
369
370
43.9k
    for (n = 0x100; *c != '\0'; n += 0x100) {
371
40.7k
        v = n | (case_adjust & *c);
372
40.7k
        r = (int)((v >> 2) ^ v) & 0x0f;
373
        /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
374
40.7k
        ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
375
40.7k
        ret &= 0xFFFFFFFFL;
376
40.7k
        ret ^= v * v;
377
40.7k
        c++;
378
40.7k
    }
379
3.16k
    return (ret >> 16) ^ ret;
380
3.16k
}
381
382
unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
383
156
{
384
156
    return lh ? lh->num_items : 0;
385
156
}
386
387
unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
388
4
{
389
4
    return lh->down_load;
390
4
}
391
392
void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
393
7
{
394
7
    lh->down_load = down_load;
395
7
}
396
397
int OPENSSL_LH_error(OPENSSL_LHASH *lh)
398
649
{
399
649
    return lh->error;
400
649
}