]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/lhash/lhash.c
Update copyright year
[thirdparty/openssl.git] / crypto / lhash / lhash.c
CommitLineData
b1322259 1/*
fecb3aae 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
8573be06 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
d02b48c6
RE
10#include <stdio.h>
11#include <string.h>
12#include <stdlib.h>
ec577822
BM
13#include <openssl/crypto.h>
14#include <openssl/lhash.h>
fe1128dc 15#include <openssl/err.h>
25f2138b
DMSP
16#include "crypto/ctype.h"
17#include "crypto/lhash.h"
706457b7 18#include "lhash_local.h"
739a1eb1 19
4ce8bebc
MC
20/*
21 * A hashing implementation that appears to be based on the linear hashing
c2969ff6 22 * algorithm:
4ce8bebc
MC
23 * https://en.wikipedia.org/wiki/Linear_hashing
24 *
25 * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
b4d0fa49 26 * addressing", Proc. 6th Conference on Very Large Databases: 212-223
c2969ff6 27 * https://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
4ce8bebc 28 *
c2969ff6 29 * From the Wikipedia article "Linear hashing is used in the BDB Berkeley
4ce8bebc
MC
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 */
d02b48c6 37
0f113f3e
MC
38#undef MIN_NODES
39#define MIN_NODES 16
40#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
41#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
d02b48c6 42
0a1d3a81 43static int expand(OPENSSL_LHASH *lh);
739a1eb1
RS
44static void contract(OPENSSL_LHASH *lh);
45static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
d02b48c6 46
739a1eb1 47OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
0f113f3e 48{
739a1eb1 49 OPENSSL_LHASH *ret;
0f113f3e 50
fe1128dc
RS
51 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.
9311d0c4 55 * ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
fe1128dc 56 */
be606c01 57 return NULL;
fe1128dc 58 }
64b25758 59 if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
be606c01 60 goto err;
739a1eb1
RS
61 ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
62 ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
0f113f3e
MC
63 ret->num_nodes = MIN_NODES / 2;
64 ret->num_alloc_nodes = MIN_NODES;
0f113f3e
MC
65 ret->pmax = MIN_NODES / 2;
66 ret->up_load = UP_LOAD;
67 ret->down_load = DOWN_LOAD;
2e8b5d75 68 return ret;
64b25758 69
be606c01
RS
70err:
71 OPENSSL_free(ret->b);
0f113f3e 72 OPENSSL_free(ret);
be606c01 73 return NULL;
0f113f3e 74}
d02b48c6 75
739a1eb1 76void OPENSSL_LH_free(OPENSSL_LHASH *lh)
1bdbdaff
P
77{
78 if (lh == NULL)
79 return;
80
81 OPENSSL_LH_flush(lh);
82 OPENSSL_free(lh->b);
83 OPENSSL_free(lh);
84}
85
86void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
0f113f3e
MC
87{
88 unsigned int i;
739a1eb1 89 OPENSSL_LH_NODE *n, *nn;
0f113f3e
MC
90
91 if (lh == NULL)
92 return;
93
94 for (i = 0; i < lh->num_nodes; i++) {
95 n = lh->b[i];
96 while (n != NULL) {
97 nn = n->next;
98 OPENSSL_free(n);
99 n = nn;
100 }
bcb5d421 101 lh->b[i] = NULL;
0f113f3e 102 }
e5da6818
HL
103
104 lh->num_items = 0;
0f113f3e 105}
d02b48c6 106
739a1eb1 107void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
0f113f3e
MC
108{
109 unsigned long hash;
739a1eb1 110 OPENSSL_LH_NODE *nn, **rn;
0f113f3e 111 void *ret;
152d2646 112
0f113f3e 113 lh->error = 0;
152d2646 114 if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
115 return NULL; /* 'lh->error++' already done in 'expand' */
116
0f113f3e
MC
117 rn = getrn(lh, data, &hash);
118
119 if (*rn == NULL) {
b4faea50 120 if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
0f113f3e 121 lh->error++;
2e8b5d75 122 return NULL;
0f113f3e
MC
123 }
124 nn->data = data;
125 nn->next = NULL;
0f113f3e 126 nn->hash = hash;
0f113f3e
MC
127 *rn = nn;
128 ret = NULL;
0f113f3e
MC
129 lh->num_items++;
130 } else { /* replace same key */
0f113f3e
MC
131 ret = (*rn)->data;
132 (*rn)->data = data;
0f113f3e 133 }
2e8b5d75 134 return ret;
0f113f3e 135}
d02b48c6 136
739a1eb1 137void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
0f113f3e
MC
138{
139 unsigned long hash;
739a1eb1 140 OPENSSL_LH_NODE *nn, **rn;
0f113f3e
MC
141 void *ret;
142
143 lh->error = 0;
144 rn = getrn(lh, data, &hash);
145
146 if (*rn == NULL) {
2e8b5d75 147 return NULL;
0f113f3e
MC
148 } else {
149 nn = *rn;
150 *rn = nn->next;
151 ret = nn->data;
152 OPENSSL_free(nn);
0f113f3e
MC
153 }
154
155 lh->num_items--;
156 if ((lh->num_nodes > MIN_NODES) &&
157 (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
158 contract(lh);
159
2e8b5d75 160 return ret;
0f113f3e 161}
d02b48c6 162
739a1eb1 163void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
0f113f3e
MC
164{
165 unsigned long hash;
739a1eb1 166 OPENSSL_LH_NODE **rn;
0f113f3e 167
77d7b6ee
HL
168 if (lh->error != 0)
169 lh->error = 0;
cab76c0f 170
0f113f3e
MC
171 rn = getrn(lh, data, &hash);
172
43f13277 173 return *rn == NULL ? NULL : (*rn)->data;
0f113f3e 174}
d02b48c6 175
739a1eb1
RS
176static 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)
0f113f3e
MC
179{
180 int i;
739a1eb1 181 OPENSSL_LH_NODE *a, *n;
0f113f3e
MC
182
183 if (lh == NULL)
184 return;
185
186 /*
187 * reverse the order so we search from 'top to bottom' We were having
188 * memory leaks otherwise
189 */
190 for (i = lh->num_nodes - 1; i >= 0; i--) {
191 a = lh->b[i];
192 while (a != NULL) {
0f113f3e
MC
193 n = a->next;
194 if (use_arg)
195 func_arg(a->data, arg);
196 else
197 func(a->data);
198 a = n;
199 }
200 }
201}
d02b48c6 202
739a1eb1 203void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
0f113f3e 204{
739a1eb1 205 doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
0f113f3e 206}
18602745 207
739a1eb1 208void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
0f113f3e 209{
739a1eb1 210 doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
0f113f3e 211}
18602745 212
0a1d3a81 213static int expand(OPENSSL_LHASH *lh)
0f113f3e 214{
739a1eb1 215 OPENSSL_LH_NODE **n, **n1, **n2, *np;
4ce8bebc
MC
216 unsigned int p, pmax, nni, j;
217 unsigned long hash;
218
219 nni = lh->num_alloc_nodes;
220 p = lh->p;
221 pmax = lh->pmax;
222 if (p + 1 >= pmax) {
223 j = nni * 2;
224 n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
225 if (n == NULL) {
226 lh->error++;
227 return 0;
228 }
229 lh->b = n;
230 memset(n + nni, 0, sizeof(*n) * (j - nni));
231 lh->pmax = nni;
232 lh->num_alloc_nodes = j;
4ce8bebc
MC
233 lh->p = 0;
234 } else {
235 lh->p++;
236 }
0f113f3e
MC
237
238 lh->num_nodes++;
0f113f3e 239 n1 = &(lh->b[p]);
4ce8bebc 240 n2 = &(lh->b[p + pmax]);
739a1eb1 241 *n2 = NULL;
0f113f3e
MC
242
243 for (np = *n1; np != NULL;) {
0f113f3e 244 hash = np->hash;
0f113f3e
MC
245 if ((hash % nni) != p) { /* move it */
246 *n1 = (*n1)->next;
247 np->next = *n2;
248 *n2 = np;
249 } else
250 n1 = &((*n1)->next);
251 np = *n1;
252 }
253
152d2646 254 return 1;
0f113f3e 255}
d02b48c6 256
739a1eb1 257static void contract(OPENSSL_LHASH *lh)
0f113f3e 258{
739a1eb1 259 OPENSSL_LH_NODE **n, *n1, *np;
0f113f3e
MC
260
261 np = lh->b[lh->p + lh->pmax - 1];
262 lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
263 if (lh->p == 0) {
b196e7d9 264 n = OPENSSL_realloc(lh->b,
739a1eb1 265 (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
0f113f3e 266 if (n == NULL) {
1287dabd 267 /* fputs("realloc error in lhash", stderr); */
0f113f3e
MC
268 lh->error++;
269 return;
270 }
0f113f3e
MC
271 lh->num_alloc_nodes /= 2;
272 lh->pmax /= 2;
273 lh->p = lh->pmax - 1;
274 lh->b = n;
275 } else
276 lh->p--;
277
278 lh->num_nodes--;
0f113f3e
MC
279
280 n1 = lh->b[(int)lh->p];
281 if (n1 == NULL)
282 lh->b[(int)lh->p] = np;
283 else {
284 while (n1->next != NULL)
285 n1 = n1->next;
286 n1->next = np;
287 }
288}
d02b48c6 289
739a1eb1
RS
290static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
291 const void *data, unsigned long *rhash)
0f113f3e 292{
739a1eb1 293 OPENSSL_LH_NODE **ret, *n1;
0f113f3e 294 unsigned long hash, nn;
739a1eb1 295 OPENSSL_LH_COMPFUNC cf;
0f113f3e
MC
296
297 hash = (*(lh->hash)) (data);
0f113f3e
MC
298 *rhash = hash;
299
300 nn = hash % lh->pmax;
301 if (nn < lh->p)
302 nn = hash % lh->num_alloc_nodes;
303
304 cf = lh->comp;
305 ret = &(lh->b[(int)nn]);
306 for (n1 = *ret; n1 != NULL; n1 = n1->next) {
0f113f3e
MC
307 if (n1->hash != hash) {
308 ret = &(n1->next);
309 continue;
310 }
0f113f3e
MC
311 if (cf(n1->data, data) == 0)
312 break;
313 ret = &(n1->next);
314 }
2e8b5d75 315 return ret;
0f113f3e
MC
316}
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.
d02b48c6 322 */
739a1eb1 323unsigned long OPENSSL_LH_strhash(const char *c)
0f113f3e
MC
324{
325 unsigned long ret = 0;
326 long n;
327 unsigned long v;
328 int r;
329
330 if ((c == NULL) || (*c == '\0'))
2e8b5d75 331 return ret;
d02b48c6 332
0f113f3e
MC
333 n = 0x100;
334 while (*c) {
335 v = n | (*c);
336 n += 0x100;
337 r = (int)((v >> 2) ^ v) & 0x0f;
2ce0a3d1
TM
338 /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
339 ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
0f113f3e
MC
340 ret &= 0xFFFFFFFFL;
341 ret ^= v * v;
342 c++;
343 }
2e8b5d75 344 return (ret >> 16) ^ ret;
0f113f3e 345}
d02b48c6 346
285aa80e 347unsigned long ossl_lh_strcasehash(const char *c)
fc196a5e
P
348{
349 unsigned long ret = 0;
350 long n;
351 unsigned long v;
352 int r;
353
354 if (c == NULL || *c == '\0')
355 return ret;
356
357 for (n = 0x100; *c != '\0'; n += 0x100) {
358 v = n | ossl_tolower(*c);
359 r = (int)((v >> 2) ^ v) & 0x0f;
2ce0a3d1
TM
360 /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
361 ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
fc196a5e
P
362 ret &= 0xFFFFFFFFL;
363 ret ^= v * v;
364 c++;
365 }
366 return (ret >> 16) ^ ret;
367}
368
739a1eb1 369unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
0f113f3e
MC
370{
371 return lh ? lh->num_items : 0;
372}
e6b5c341 373
739a1eb1 374unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
e6b5c341
DSH
375{
376 return lh->down_load;
377}
378
739a1eb1 379void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
e6b5c341
DSH
380{
381 lh->down_load = down_load;
382}
383
739a1eb1 384int OPENSSL_LH_error(OPENSSL_LHASH *lh)
e6b5c341
DSH
385{
386 return lh->error;
387}