Fix memory overrun in rsa padding check functions
[oweals/openssl.git] / crypto / lhash / lhash.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <ctype.h>
16 #include "internal/lhash.h"
17 #include "lhash_lcl.h"
18
19 /*
20  * A hashing implementation that appears to be based on the linear hashing
21  * algorithm:
22  * https://en.wikipedia.org/wiki/Linear_hashing
23  *
24  * Litwin, Witold (1980), "Linear hashing: A new tool for file and table
25  * addressing", Proc. 6th Conference on Very Large Databases: 212-223
26  * http://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf
27  *
28  * From the wikipedia article "Linear hashing is used in the BDB Berkeley
29  * database system, which in turn is used by many software systems such as
30  * OpenLDAP, using a C implementation derived from the CACM article and first
31  * published on the Usenet in 1988 by Esmond Pitt."
32  *
33  * The CACM paper is available here:
34  * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf
35  */
36
37 #undef MIN_NODES
38 #define MIN_NODES       16
39 #define UP_LOAD         (2*LH_LOAD_MULT) /* load times 256 (default 2) */
40 #define DOWN_LOAD       (LH_LOAD_MULT) /* load times 256 (default 1) */
41
42 static int expand(OPENSSL_LHASH *lh);
43 static void contract(OPENSSL_LHASH *lh);
44 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash);
45
46 OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)
47 {
48     OPENSSL_LHASH *ret;
49
50     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
51         return NULL;
52     if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)
53         goto err;
54     if ((ret->retrieve_stats_lock = CRYPTO_THREAD_lock_new()) == NULL) 
55         goto err;
56     ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);
57     ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);
58     ret->num_nodes = MIN_NODES / 2;
59     ret->num_alloc_nodes = MIN_NODES;
60     ret->pmax = MIN_NODES / 2;
61     ret->up_load = UP_LOAD;
62     ret->down_load = DOWN_LOAD;
63     return (ret);
64
65 err:
66     OPENSSL_free(ret->b);
67     OPENSSL_free(ret);
68     return NULL;
69 }
70
71 void OPENSSL_LH_free(OPENSSL_LHASH *lh)
72 {
73     unsigned int i;
74     OPENSSL_LH_NODE *n, *nn;
75
76     if (lh == NULL)
77         return;
78
79     for (i = 0; i < lh->num_nodes; i++) {
80         n = lh->b[i];
81         while (n != NULL) {
82             nn = n->next;
83             OPENSSL_free(n);
84             n = nn;
85         }
86     }
87     CRYPTO_THREAD_lock_free(lh->retrieve_stats_lock);
88     OPENSSL_free(lh->b);
89     OPENSSL_free(lh);
90 }
91
92 void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)
93 {
94     unsigned long hash;
95     OPENSSL_LH_NODE *nn, **rn;
96     void *ret;
97
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
102     rn = getrn(lh, data, &hash);
103
104     if (*rn == NULL) {
105         if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {
106             lh->error++;
107             return (NULL);
108         }
109         nn->data = data;
110         nn->next = NULL;
111         nn->hash = hash;
112         *rn = nn;
113         ret = NULL;
114         lh->num_insert++;
115         lh->num_items++;
116     } else {                    /* replace same key */
117
118         ret = (*rn)->data;
119         (*rn)->data = data;
120         lh->num_replace++;
121     }
122     return (ret);
123 }
124
125 void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
126 {
127     unsigned long hash;
128     OPENSSL_LH_NODE *nn, **rn;
129     void *ret;
130
131     lh->error = 0;
132     rn = getrn(lh, data, &hash);
133
134     if (*rn == NULL) {
135         lh->num_no_delete++;
136         return (NULL);
137     } else {
138         nn = *rn;
139         *rn = nn->next;
140         ret = nn->data;
141         OPENSSL_free(nn);
142         lh->num_delete++;
143     }
144
145     lh->num_items--;
146     if ((lh->num_nodes > MIN_NODES) &&
147         (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
148         contract(lh);
149
150     return (ret);
151 }
152
153 void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)
154 {
155     unsigned long hash;
156     OPENSSL_LH_NODE **rn;
157     void *ret;
158     int scratch;
159
160     lh->error = 0;
161     rn = getrn(lh, data, &hash);
162
163     if (*rn == NULL) {
164         CRYPTO_atomic_add(&lh->num_retrieve_miss, 1, &scratch, lh->retrieve_stats_lock);
165         return NULL;
166     } else {
167         ret = (*rn)->data;
168         CRYPTO_atomic_add(&lh->num_retrieve, 1, &scratch, lh->retrieve_stats_lock);
169     }
170     return ret;
171 }
172
173 static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
174                           OPENSSL_LH_DOALL_FUNC func,
175                           OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
176 {
177     int i;
178     OPENSSL_LH_NODE *a, *n;
179
180     if (lh == NULL)
181         return;
182
183     /*
184      * reverse the order so we search from 'top to bottom' We were having
185      * memory leaks otherwise
186      */
187     for (i = lh->num_nodes - 1; i >= 0; i--) {
188         a = lh->b[i];
189         while (a != NULL) {
190             n = a->next;
191             if (use_arg)
192                 func_arg(a->data, arg);
193             else
194                 func(a->data);
195             a = n;
196         }
197     }
198 }
199
200 void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func)
201 {
202     doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL);
203 }
204
205 void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)
206 {
207     doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);
208 }
209
210 static int expand(OPENSSL_LHASH *lh)
211 {
212     OPENSSL_LH_NODE **n, **n1, **n2, *np;
213     unsigned int p, pmax, nni, j;
214     unsigned long hash;
215
216     nni = lh->num_alloc_nodes;
217     p = lh->p;
218     pmax = lh->pmax;
219     if (p + 1 >= pmax) {
220         j = nni * 2;
221         n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
222         if (n == NULL) {
223             lh->error++;
224             return 0;
225         }
226         lh->b = n;
227         memset(n + nni, 0, sizeof(*n) * (j - nni));
228         lh->pmax = nni;
229         lh->num_alloc_nodes = j;
230         lh->num_expand_reallocs++;
231         lh->p = 0;
232     } else {
233         lh->p++;
234     }
235
236     lh->num_nodes++;
237     lh->num_expands++;
238     n1 = &(lh->b[p]);
239     n2 = &(lh->b[p + pmax]);
240     *n2 = NULL;
241
242     for (np = *n1; np != NULL;) {
243         hash = np->hash;
244         if ((hash % nni) != p) { /* move it */
245             *n1 = (*n1)->next;
246             np->next = *n2;
247             *n2 = np;
248         } else
249             n1 = &((*n1)->next);
250         np = *n1;
251     }
252
253     return 1;
254 }
255
256 static void contract(OPENSSL_LHASH *lh)
257 {
258     OPENSSL_LH_NODE **n, *n1, *np;
259
260     np = lh->b[lh->p + lh->pmax - 1];
261     lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */
262     if (lh->p == 0) {
263         n = OPENSSL_realloc(lh->b,
264                             (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax));
265         if (n == NULL) {
266             /* fputs("realloc error in lhash",stderr); */
267             lh->error++;
268             return;
269         }
270         lh->num_contract_reallocs++;
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--;
279     lh->num_contracts++;
280
281     n1 = lh->b[(int)lh->p];
282     if (n1 == NULL)
283         lh->b[(int)lh->p] = np;
284     else {
285         while (n1->next != NULL)
286             n1 = n1->next;
287         n1->next = np;
288     }
289 }
290
291 static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh,
292                                const void *data, unsigned long *rhash)
293 {
294     OPENSSL_LH_NODE **ret, *n1;
295     unsigned long hash, nn;
296     OPENSSL_LH_COMPFUNC cf;
297     int scratch;
298
299     hash = (*(lh->hash)) (data);
300     CRYPTO_atomic_add(&lh->num_hash_calls, 1, &scratch, lh->retrieve_stats_lock);
301     *rhash = hash;
302
303     nn = hash % lh->pmax;
304     if (nn < lh->p)
305         nn = hash % lh->num_alloc_nodes;
306
307     cf = lh->comp;
308     ret = &(lh->b[(int)nn]);
309     for (n1 = *ret; n1 != NULL; n1 = n1->next) {
310         CRYPTO_atomic_add(&lh->num_hash_comps, 1, &scratch, lh->retrieve_stats_lock);
311         if (n1->hash != hash) {
312             ret = &(n1->next);
313             continue;
314         }
315         CRYPTO_atomic_add(&lh->num_comp_calls, 1, &scratch, lh->retrieve_stats_lock);
316         if (cf(n1->data, data) == 0)
317             break;
318         ret = &(n1->next);
319     }
320     return (ret);
321 }
322
323 /*
324  * The following hash seems to work very well on normal text strings no
325  * collisions on /usr/dict/words and it distributes on %2^n quite well, not
326  * as good as MD5, but still good.
327  */
328 unsigned long OPENSSL_LH_strhash(const char *c)
329 {
330     unsigned long ret = 0;
331     long n;
332     unsigned long v;
333     int r;
334
335     if ((c == NULL) || (*c == '\0'))
336         return (ret);
337 /*-
338     unsigned char b[16];
339     MD5(c,strlen(c),b);
340     return(b[0]|(b[1]<<8)|(b[2]<<16)|(b[3]<<24));
341 */
342
343     n = 0x100;
344     while (*c) {
345         v = n | (*c);
346         n += 0x100;
347         r = (int)((v >> 2) ^ v) & 0x0f;
348         ret = (ret << r) | (ret >> (32 - r));
349         ret &= 0xFFFFFFFFL;
350         ret ^= v * v;
351         c++;
352     }
353     return ((ret >> 16) ^ ret);
354 }
355
356 unsigned long openssl_lh_strcasehash(const char *c)
357 {
358     unsigned long ret = 0;
359     long n;
360     unsigned long v;
361     int r;
362
363     if (c == NULL || *c == '\0')
364         return ret;
365
366     for (n = 0x100; *c != '\0'; n += 0x100) {
367         v = n | tolower(*c);
368         r = (int)((v >> 2) ^ v) & 0x0f;
369         ret = (ret << r) | (ret >> (32 - r));
370         ret &= 0xFFFFFFFFL;
371         ret ^= v * v;
372         c++;
373     }
374     return (ret >> 16) ^ ret;
375 }
376
377 unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh)
378 {
379     return lh ? lh->num_items : 0;
380 }
381
382 unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh)
383 {
384     return lh->down_load;
385 }
386
387 void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load)
388 {
389     lh->down_load = down_load;
390 }
391
392 int OPENSSL_LH_error(OPENSSL_LHASH *lh)
393 {
394     return lh->error;
395 }