Make BN_GF2m_mod_arr more constant time
[openssl.git] / crypto / lhash / lhash.c
1 /* crypto/lhash/lhash.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 /*-
60  * Code for dynamic hash table routines
61  * Author - Eric Young v 2.0
62  *
63  * 2.2 eay - added #include "crypto.h" so the memory leak checking code is
64  *           present. eay 18-Jun-98
65  *
66  * 2.1 eay - Added an 'error in last operation' flag. eay 6-May-98
67  *
68  * 2.0 eay - Fixed a bug that occurred when using lh_delete
69  *           from inside lh_doall().  As entries were deleted,
70  *           the 'table' was 'contract()ed', making some entries
71  *           jump from the end of the table to the start, there by
72  *           skipping the lh_doall() processing. eay - 4/12/95
73  *
74  * 1.9 eay - Fixed a memory leak in lh_free, the LHASH_NODEs
75  *           were not being free()ed. 21/11/95
76  *
77  * 1.8 eay - Put the stats routines into a separate file, lh_stats.c
78  *           19/09/95
79  *
80  * 1.7 eay - Removed the fputs() for realloc failures - the code
81  *           should silently tolerate them.  I have also fixed things
82  *           lint complained about 04/05/95
83  *
84  * 1.6 eay - Fixed an invalid pointers in contract/expand 27/07/92
85  *
86  * 1.5 eay - Fixed a misuse of realloc in expand 02/03/1992
87  *
88  * 1.4 eay - Fixed lh_doall so the function can call lh_delete 28/05/91
89  *
90  * 1.3 eay - Fixed a few lint problems 19/3/1991
91  *
92  * 1.2 eay - Fixed lh_doall problem 13/3/1991
93  *
94  * 1.1 eay - Added lh_doall
95  *
96  * 1.0 eay - First version
97  */
98 #include <stdio.h>
99 #include <string.h>
100 #include <stdlib.h>
101 #include <openssl/crypto.h>
102 #include <openssl/lhash.h>
103
104 /*
105  * A hashing implementation that appears to be based on the linear hashing
106  * alogrithm:
107  * https://en.wikipedia.org/wiki/Linear_hashing
108  *
109  * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
110  * addressing", Proc. 6th Conference on Very Large Databases: 212-223
111  * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
112  *
113  * From the wikipedia article "Linear hashing is used in the BDB Berkeley
114  * database system, which in turn is used by many software systems such as
115  * OpenLDAP, using a C implementation derived from the CACM article and first
116  * published on the Usenet in 1988 by Esmond Pitt."
117  *
118  * The CACM paper is available here:
119  * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
120  */
121
122 const char lh_version[] = "lhash" OPENSSL_VERSION_PTEXT;
123
124 #undef MIN_NODES
125 #define MIN_NODES       16
126 #define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
127 #define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
128
129 static int expand(_LHASH *lh);
130 static void contract(_LHASH *lh);
131 static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash);
132
133 _LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)
134 {
135     _LHASH *ret;
136     int i;
137
138     if ((ret = OPENSSL_malloc(sizeof(_LHASH))) == NULL)
139         goto err0;
140     if ((ret->b = OPENSSL_malloc(sizeof(LHASH_NODE *) * MIN_NODES)) == NULL)
141         goto err1;
142     for (i = 0; i < MIN_NODES; i++)
143         ret->b[i] = NULL;
144     ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);
145     ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);
146     ret->num_nodes = MIN_NODES / 2;
147     ret->num_alloc_nodes = MIN_NODES;
148     ret->p = 0;
149     ret->pmax = MIN_NODES / 2;
150     ret->up_load = UP_LOAD;
151     ret->down_load = DOWN_LOAD;
152     ret->num_items = 0;
153
154     ret->num_expands = 0;
155     ret->num_expand_reallocs = 0;
156     ret->num_contracts = 0;
157     ret->num_contract_reallocs = 0;
158     ret->num_hash_calls = 0;
159     ret->num_comp_calls = 0;
160     ret->num_insert = 0;
161     ret->num_replace = 0;
162     ret->num_delete = 0;
163     ret->num_no_delete = 0;
164     ret->num_retrieve = 0;
165     ret->num_retrieve_miss = 0;
166     ret->num_hash_comps = 0;
167
168     ret->error = 0;
169     return (ret);
170  err1:
171     OPENSSL_free(ret);
172  err0:
173     return (NULL);
174 }
175
176 void lh_free(_LHASH *lh)
177 {
178     unsigned int i;
179     LHASH_NODE *n, *nn;
180
181     if (lh == NULL)
182         return;
183
184     for (i = 0; i < lh->num_nodes; i++) {
185         n = lh->b[i];
186         while (n != NULL) {
187             nn = n->next;
188             OPENSSL_free(n);
189             n = nn;
190         }
191     }
192     OPENSSL_free(lh->b);
193     OPENSSL_free(lh);
194 }
195
196 void *lh_insert(_LHASH *lh, void *data)
197 {
198     unsigned long hash;
199     LHASH_NODE *nn, **rn;
200     void *ret;
201
202     lh->error = 0;
203     if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)
204             && !expand(lh))
205         return NULL;
206
207     rn = getrn(lh, data, &hash);
208
209     if (*rn == NULL) {
210         if ((nn = (LHASH_NODE *)OPENSSL_malloc(sizeof(LHASH_NODE))) == NULL) {
211             lh->error++;
212             return (NULL);
213         }
214         nn->data = data;
215         nn->next = NULL;
216 #ifndef OPENSSL_NO_HASH_COMP
217         nn->hash = hash;
218 #endif
219         *rn = nn;
220         ret = NULL;
221         lh->num_insert++;
222         lh->num_items++;
223     } else {                    /* replace same key */
224
225         ret = (*rn)->data;
226         (*rn)->data = data;
227         lh->num_replace++;
228     }
229     return (ret);
230 }
231
232 void *lh_delete(_LHASH *lh, const void *data)
233 {
234     unsigned long hash;
235     LHASH_NODE *nn, **rn;
236     void *ret;
237
238     lh->error = 0;
239     rn = getrn(lh, data, &hash);
240
241     if (*rn == NULL) {
242         lh->num_no_delete++;
243         return (NULL);
244     } else {
245         nn = *rn;
246         *rn = nn->next;
247         ret = nn->data;
248         OPENSSL_free(nn);
249         lh->num_delete++;
250     }
251
252     lh->num_items--;
253     if ((lh->num_nodes > MIN_NODES) &&
254         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
255         contract(lh);
256
257     return (ret);
258 }
259
260 void *lh_retrieve(_LHASH *lh, const void *data)
261 {
262     unsigned long hash;
263     LHASH_NODE **rn;
264     void *ret;
265
266     lh->error = 0;
267     rn = getrn(lh, data, &hash);
268
269     if (*rn == NULL) {
270         lh->num_retrieve_miss++;
271         return (NULL);
272     } else {
273         ret = (*rn)->data;
274         lh->num_retrieve++;
275     }
276     return (ret);
277 }
278
279 static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
280                           LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
281 {
282     int i;
283     LHASH_NODE *a, *n;
284
285     if (lh == NULL)
286         return;
287
288     /*
289      * reverse the order so we search from 'top to bottom' We were having
290      * memory leaks otherwise
291      */
292     for (i = lh->num_nodes - 1; i >= 0; i--) {
293         a = lh->b[i];
294         while (a != NULL) {
295             /*
296              * 28/05/91 - eay - n added so items can be deleted via lh_doall
297              */
298             /*
299              * 22/05/08 - ben - eh? since a is not passed, this should not be
300              * needed
301              */
302             n = a->next;
303             if (use_arg)
304                 func_arg(a->data, arg);
305             else
306                 func(a->data);
307             a = n;
308         }
309     }
310 }
311
312 void lh_doall(_LHASH *lh, LHASH_DOALL_FN_TYPE func)
313 {
314     doall_util_fn(lh, 0, func, (LHASH_DOALL_ARG_FN_TYPE)0, NULL);
315 }
316
317 void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)
318 {
319     doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);
320 }
321
322 static int expand(_LHASH *lh)
323 {
324     LHASH_NODE **n, **n1, **n2, *np;
325     unsigned int p, pmax, nni, j;
326     unsigned long hash;
327
328     nni = lh->num_alloc_nodes;
329     p = lh->p;
330     pmax = lh->pmax;
331     if (p + 1 >= pmax) {
332         j = nni * 2;
333         n = OPENSSL_realloc(lh->b, (int)(sizeof(LHASH_NODE *) * j));
334         if (n == NULL) {
335             lh->error++;
336             return 0;
337         }
338         lh->b = n;
339         memset(n + nni, 0, sizeof(*n) * (j - nni));
340         lh->pmax = nni;
341         lh->num_alloc_nodes = j;
342         lh->num_expand_reallocs++;
343         lh->p = 0;
344     } else {
345         lh->p++;
346     }
347
348     lh->num_nodes++;
349     lh->num_expands++;
350     n1 = &(lh->b[p]);
351     n2 = &(lh->b[p + pmax]);
352     *n2 = NULL;
353
354     for (np = *n1; np != NULL;) {
355 #ifndef OPENSSL_NO_HASH_COMP
356         hash = np->hash;
357 #else
358         hash = lh->hash(np->data);
359         lh->num_hash_calls++;
360 #endif
361         if ((hash % nni) != p) { /* move it */
362             *n1 = (*n1)->next;
363             np->next = *n2;
364             *n2 = np;
365         } else
366             n1 = &((*n1)->next);
367         np = *n1;
368     }
369
370     return 1;
371 }
372
373 static void contract(_LHASH *lh)
374 {
375     LHASH_NODE **n, *n1, *np;
376
377     np = lh->b[lh->p + lh->pmax - 1];
378     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
379     if (lh->p == 0) {
380         n = (LHASH_NODE **)OPENSSL_realloc(lh->b,
381                                            (unsigned int)(sizeof(LHASH_NODE *)
382                                                           * lh->pmax));
383         if (n == NULL) {
384 /*                      fputs("realloc error in lhash",stderr); */
385             lh->error++;
386             return;
387         }
388         lh->num_contract_reallocs++;
389         lh->num_alloc_nodes /= 2;
390         lh->pmax /= 2;
391         lh->p = lh->pmax - 1;
392         lh->b = n;
393     } else
394         lh->p--;
395
396     lh->num_nodes--;
397     lh->num_contracts++;
398
399     n1 = lh->b[(int)lh->p];
400     if (n1 == NULL)
401         lh->b[(int)lh->p] = np;
402     else {
403         while (n1->next != NULL)
404             n1 = n1->next;
405         n1->next = np;
406     }
407 }
408
409 static LHASH_NODE **getrn(_LHASH *lh, const void *data, unsigned long *rhash)
410 {
411     LHASH_NODE **ret, *n1;
412     unsigned long hash, nn;
413     LHASH_COMP_FN_TYPE cf;
414
415     hash = (*(lh->hash)) (data);
416     lh->num_hash_calls++;
417     *rhash = hash;
418
419     nn = hash % lh->pmax;
420     if (nn < lh->p)
421         nn = hash % lh->num_alloc_nodes;
422
423     cf = lh->comp;
424     ret = &(lh->b[(int)nn]);
425     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
426 #ifndef OPENSSL_NO_HASH_COMP
427         lh->num_hash_comps++;
428         if (n1->hash != hash) {
429             ret = &(n1->next);
430             continue;
431         }
432 #endif
433         lh->num_comp_calls++;
434         if (cf(n1->data, data) == 0)
435             break;
436         ret = &(n1->next);
437     }
438     return (ret);
439 }
440
441 /*
442  * The following hash seems to work very well on normal text strings no
443  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
444  * as good as MD5, but still good.
445  */
446 unsigned long lh_strhash(const char *c)
447 {
448     unsigned long ret = 0;
449     long n;
450     unsigned long v;
451     int r;
452
453     if ((c == NULL) || (*c == '\0'))
454         return (ret);
455 /*-
456     unsigned char b[16];
457     MD5(c,strlen(c),b);
458     return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
459 */
460
461     n = 0x100;
462     while (*c) {
463         v = n | (*c);
464         n += 0x100;
465         r = (int)((v >> 2) ^ v) & 0x0f;
466         ret = (ret << r) | (ret >> (32 - r));
467         ret &= 0xFFFFFFFFL;
468         ret ^= v * v;
469         c++;
470     }
471     return ((ret >> 16) ^ ret);
472 }
473
474 unsigned long lh_num_items(const _LHASH *lh)
475 {
476     return lh ? lh->num_items : 0;
477 }