Coverage Report

Created: 2024-07-27 06:35

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