lhash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright 1995-2023 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. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/lhash.h>
  14. #include <openssl/err.h>
  15. #include "crypto/ctype.h"
  16. #include "crypto/lhash.h"
  17. #include "lhash_local.h"
  18. /*
  19. * A hashing implementation that appears to be based on the linear hashing
  20. * algorithm:
  21. * https://en.wikipedia.org/wiki/Linear_hashing
  22. *
  23. * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
  24. * addressing", Proc. 6th Conference on Very Large Databases: 212-223
  25. * https://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
  26. *
  27. * From the Wikipedia article "Linear hashing is used in the BDB Berkeley
  28. * database system, which in turn is used by many software systems such as
  29. * OpenLDAP, using a C implementation derived from the CACM article and first
  30. * published on the Usenet in 1988 by Esmond Pitt."
  31. *
  32. * The CACM paper is available here:
  33. * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
  34. */
  35. #undef MIN_NODES
  36. #define MIN_NODES 16
  37. #define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */
  38. #define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */
  39. static int expand(OPENSSL_LHASH *lh);
  40. static void contract(OPENSSL_LHASH *lh);
  41. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
  42. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
  43. {
  44. OPENSSL_LHASH *ret;
  45. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  46. /*
  47. * Do not set the error code, because the ERR code uses LHASH
  48. * and we want to avoid possible endless error loop.
  49. * ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
  50. */
  51. return NULL;
  52. }
  53. if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
  54. goto err;
  55. ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
  56. ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
  57. ret->num_nodes = MIN_NODES / 2;
  58. ret->num_alloc_nodes = MIN_NODES;
  59. ret->pmax = MIN_NODES / 2;
  60. ret->up_load = UP_LOAD;
  61. ret->down_load = DOWN_LOAD;
  62. return ret;
  63. err:
  64. OPENSSL_free(ret->b);
  65. OPENSSL_free(ret);
  66. return NULL;
  67. }
  68. void OPENSSL_LH_free(OPENSSL_LHASH *lh)
  69. {
  70. if (lh == NULL)
  71. return;
  72. OPENSSL_LH_flush(lh);
  73. OPENSSL_free(lh->b);
  74. OPENSSL_free(lh);
  75. }
  76. void OPENSSL_LH_flush(OPENSSL_LHASH *lh)
  77. {
  78. unsigned int i;
  79. OPENSSL_LH_NODE *n, *nn;
  80. if (lh == NULL)
  81. return;
  82. for (i = 0; i < lh->num_nodes; i++) {
  83. n = lh->b[i];
  84. while (n != NULL) {
  85. nn = n->next;
  86. OPENSSL_free(n);
  87. n = nn;
  88. }
  89. lh->b[i] = NULL;
  90. }
  91. lh->num_items = 0;
  92. }
  93. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
  94. {
  95. unsigned long hash;
  96. OPENSSL_LH_NODE *nn, **rn;
  97. void *ret;
  98. lh->error = 0;
  99. if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))
  100. return NULL; /* 'lh->error++' already done in 'expand' */
  101. rn = getrn(lh, data, &hash);
  102. if (*rn == NULL) {
  103. if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
  104. lh->error++;
  105. return NULL;
  106. }
  107. nn->data = data;
  108. nn->next = NULL;
  109. nn->hash = hash;
  110. *rn = nn;
  111. ret = NULL;
  112. lh->num_items++;
  113. } else { /* replace same key */
  114. ret = (*rn)->data;
  115. (*rn)->data = data;
  116. }
  117. return ret;
  118. }
  119. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
  120. {
  121. unsigned long hash;
  122. OPENSSL_LH_NODE *nn, **rn;
  123. void *ret;
  124. lh->error = 0;
  125. rn = getrn(lh, data, &hash);
  126. if (*rn == NULL) {
  127. return NULL;
  128. } else {
  129. nn = *rn;
  130. *rn = nn->next;
  131. ret = nn->data;
  132. OPENSSL_free(nn);
  133. }
  134. lh->num_items--;
  135. if ((lh->num_nodes > MIN_NODES) &&
  136. (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
  137. contract(lh);
  138. return ret;
  139. }
  140. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
  141. {
  142. unsigned long hash;
  143. OPENSSL_LH_NODE **rn;
  144. if (lh->error != 0)
  145. lh->error = 0;
  146. rn = getrn(lh, data, &hash);
  147. return *rn == NULL ? NULL : (*rn)->data;
  148. }
  149. static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
  150. OPENSSL_LH_DOALL_FUNC func,
  151. OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
  152. {
  153. int i;
  154. OPENSSL_LH_NODE *a, *n;
  155. if (lh == NULL)
  156. return;
  157. /*
  158. * reverse the order so we search from 'top to bottom' We were having
  159. * memory leaks otherwise
  160. */
  161. for (i = lh->num_nodes - 1; i >= 0; i--) {
  162. a = lh->b[i];
  163. while (a != NULL) {
  164. n = a->next;
  165. if (use_arg)
  166. func_arg(a->data, arg);
  167. else
  168. func(a->data);
  169. a = n;
  170. }
  171. }
  172. }
  173. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
  174. {
  175. doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
  176. }
  177. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
  178. {
  179. doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
  180. }
  181. static int expand(OPENSSL_LHASH *lh)
  182. {
  183. OPENSSL_LH_NODE **n, **n1, **n2, *np;
  184. unsigned int p, pmax, nni, j;
  185. unsigned long hash;
  186. nni = lh->num_alloc_nodes;
  187. p = lh->p;
  188. pmax = lh->pmax;
  189. if (p + 1 >= pmax) {
  190. j = nni * 2;
  191. n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
  192. if (n == NULL) {
  193. lh->error++;
  194. return 0;
  195. }
  196. lh->b = n;
  197. memset(n + nni, 0, sizeof(*n) * (j - nni));
  198. lh->pmax = nni;
  199. lh->num_alloc_nodes = j;
  200. lh->p = 0;
  201. } else {
  202. lh->p++;
  203. }
  204. lh->num_nodes++;
  205. n1 = &(lh->b[p]);
  206. n2 = &(lh->b[p + pmax]);
  207. *n2 = NULL;
  208. for (np = *n1; np != NULL;) {
  209. hash = np->hash;
  210. if ((hash % nni) != p) { /* move it */
  211. *n1 = (*n1)->next;
  212. np->next = *n2;
  213. *n2 = np;
  214. } else
  215. n1 = &((*n1)->next);
  216. np = *n1;
  217. }
  218. return 1;
  219. }
  220. static void contract(OPENSSL_LHASH *lh)
  221. {
  222. OPENSSL_LH_NODE **n, *n1, *np;
  223. np = lh->b[lh->p + lh->pmax - 1];
  224. lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
  225. if (lh->p == 0) {
  226. n = OPENSSL_realloc(lh->b,
  227. (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
  228. if (n == NULL) {
  229. /* fputs("realloc error in lhash",stderr); */
  230. lh->error++;
  231. } else {
  232. lh->b = n;
  233. }
  234. lh->num_alloc_nodes /= 2;
  235. lh->pmax /= 2;
  236. lh->p = lh->pmax - 1;
  237. } else
  238. lh->p--;
  239. lh->num_nodes--;
  240. n1 = lh->b[(int)lh->p];
  241. if (n1 == NULL)
  242. lh->b[(int)lh->p] = np;
  243. else {
  244. while (n1->next != NULL)
  245. n1 = n1->next;
  246. n1->next = np;
  247. }
  248. }
  249. static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
  250. const void *data, unsigned long *rhash)
  251. {
  252. OPENSSL_LH_NODE **ret, *n1;
  253. unsigned long hash, nn;
  254. OPENSSL_LH_COMPFUNC cf;
  255. hash = (*(lh->hash)) (data);
  256. *rhash = hash;
  257. nn = hash % lh->pmax;
  258. if (nn < lh->p)
  259. nn = hash % lh->num_alloc_nodes;
  260. cf = lh->comp;
  261. ret = &(lh->b[(int)nn]);
  262. for (n1 = *ret; n1 != NULL; n1 = n1->next) {
  263. if (n1->hash != hash) {
  264. ret = &(n1->next);
  265. continue;
  266. }
  267. if (cf(n1->data, data) == 0)
  268. break;
  269. ret = &(n1->next);
  270. }
  271. return ret;
  272. }
  273. /*
  274. * The following hash seems to work very well on normal text strings no
  275. * collisions on /usr/dict/words and it distributes on %2^n quite well, not
  276. * as good as MD5, but still good.
  277. */
  278. unsigned long OPENSSL_LH_strhash(const char *c)
  279. {
  280. unsigned long ret = 0;
  281. long n;
  282. unsigned long v;
  283. int r;
  284. if ((c == NULL) || (*c == '\0'))
  285. return ret;
  286. n = 0x100;
  287. while (*c) {
  288. v = n | (*c);
  289. n += 0x100;
  290. r = (int)((v >> 2) ^ v) & 0x0f;
  291. /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
  292. ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
  293. ret &= 0xFFFFFFFFL;
  294. ret ^= v * v;
  295. c++;
  296. }
  297. return (ret >> 16) ^ ret;
  298. }
  299. /*
  300. * Case insensitive string hashing.
  301. *
  302. * The lower/upper case bit is masked out (forcing all letters to be capitals).
  303. * The major side effect on non-alpha characters is mapping the symbols and
  304. * digits into the control character range (which should be harmless).
  305. * The duplication (with respect to the hash value) of printable characters
  306. * are that '`', '{', '|', '}' and '~' map to '@', '[', '\', ']' and '^'
  307. * respectively (which seems tolerable).
  308. *
  309. * For EBCDIC, the alpha mapping is to lower case, most symbols go to control
  310. * characters. The only duplication is '0' mapping to '^', which is better
  311. * than for ASCII.
  312. */
  313. unsigned long ossl_lh_strcasehash(const char *c)
  314. {
  315. unsigned long ret = 0;
  316. long n;
  317. unsigned long v;
  318. int r;
  319. #if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
  320. const long int case_adjust = ~0x40;
  321. #else
  322. const long int case_adjust = ~0x20;
  323. #endif
  324. if (c == NULL || *c == '\0')
  325. return ret;
  326. for (n = 0x100; *c != '\0'; n += 0x100) {
  327. v = n | (case_adjust & *c);
  328. r = (int)((v >> 2) ^ v) & 0x0f;
  329. /* cast to uint64_t to avoid 32 bit shift of 32 bit value */
  330. ret = (ret << r) | (unsigned long)((uint64_t)ret >> (32 - r));
  331. ret &= 0xFFFFFFFFL;
  332. ret ^= v * v;
  333. c++;
  334. }
  335. return (ret >> 16) ^ ret;
  336. }
  337. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
  338. {
  339. return lh ? lh->num_items : 0;
  340. }
  341. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
  342. {
  343. return lh->down_load;
  344. }
  345. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
  346. {
  347. lh->down_load = down_load;
  348. }
  349. int OPENSSL_LH_error(OPENSSL_LHASH *lh)
  350. {
  351. return lh->error;
  352. }