diff options
Diffstat (limited to 'roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash')
4 files changed, 557 insertions, 0 deletions
diff --git a/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/build.info b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/build.info new file mode 100644 index 000000000..30797f2ca --- /dev/null +++ b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/build.info @@ -0,0 +1,3 @@ +LIBS=../../libcrypto +SOURCE[../../libcrypto]=\ + lhash.c lh_stats.c diff --git a/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lh_stats.c b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lh_stats.c new file mode 100644 index 000000000..45f1b1055 --- /dev/null +++ b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lh_stats.c @@ -0,0 +1,117 @@ +/* + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +/* + * If you wish to build this outside of OpenSSL, remove the following lines + * and things should work as expected + */ +#include "internal/cryptlib.h" + +#include <openssl/bio.h> +#include <openssl/lhash.h> +#include "lhash_local.h" + +# ifndef OPENSSL_NO_STDIO +void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp) +{ + BIO *bp; + + bp = BIO_new(BIO_s_file()); + if (bp == NULL) + return; + BIO_set_fp(bp, fp, BIO_NOCLOSE); + OPENSSL_LH_stats_bio(lh, bp); + BIO_free(bp); +} + +void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp) +{ + BIO *bp; + + bp = BIO_new(BIO_s_file()); + if (bp == NULL) + return; + BIO_set_fp(bp, fp, BIO_NOCLOSE); + OPENSSL_LH_node_stats_bio(lh, bp); + BIO_free(bp); +} + +void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp) +{ + BIO *bp; + + bp = BIO_new(BIO_s_file()); + if (bp == NULL) + return; + BIO_set_fp(bp, fp, BIO_NOCLOSE); + OPENSSL_LH_node_usage_stats_bio(lh, bp); + BIO_free(bp); +} + +# endif + +void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out) +{ + BIO_printf(out, "num_items = %lu\n", lh->num_items); + BIO_printf(out, "num_nodes = %u\n", lh->num_nodes); + BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes); + BIO_printf(out, "num_expands = %lu\n", lh->num_expands); + BIO_printf(out, "num_expand_reallocs = %lu\n", lh->num_expand_reallocs); + BIO_printf(out, "num_contracts = %lu\n", lh->num_contracts); + BIO_printf(out, "num_contract_reallocs = %lu\n", lh->num_contract_reallocs); + BIO_printf(out, "num_hash_calls = %lu\n", lh->num_hash_calls); + BIO_printf(out, "num_comp_calls = %lu\n", lh->num_comp_calls); + BIO_printf(out, "num_insert = %lu\n", lh->num_insert); + BIO_printf(out, "num_replace = %lu\n", lh->num_replace); + BIO_printf(out, "num_delete = %lu\n", lh->num_delete); + BIO_printf(out, "num_no_delete = %lu\n", lh->num_no_delete); + BIO_printf(out, "num_retrieve = %lu\n", lh->num_retrieve); + BIO_printf(out, "num_retrieve_miss = %lu\n", lh->num_retrieve_miss); + BIO_printf(out, "num_hash_comps = %lu\n", lh->num_hash_comps); +} + +void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out) +{ + OPENSSL_LH_NODE *n; + unsigned int i, num; + + for (i = 0; i < lh->num_nodes; i++) { + for (n = lh->b[i], num = 0; n != NULL; n = n->next) + num++; + BIO_printf(out, "node %6u -> %3u\n", i, num); + } +} + +void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out) +{ + OPENSSL_LH_NODE *n; + unsigned long num; + unsigned int i; + unsigned long total = 0, n_used = 0; + + for (i = 0; i < lh->num_nodes; i++) { + for (n = lh->b[i], num = 0; n != NULL; n = n->next) + num++; + if (num != 0) { + n_used++; + total += num; + } + } + BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes); + BIO_printf(out, "%lu items\n", total); + if (n_used == 0) + return; + BIO_printf(out, "load %d.%02d actual load %d.%02d\n", + (int)(total / lh->num_nodes), + (int)((total % lh->num_nodes) * 100 / lh->num_nodes), + (int)(total / n_used), (int)((total % n_used) * 100 / n_used)); +} diff --git a/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lhash.c b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lhash.c new file mode 100644 index 000000000..9dc887d91 --- /dev/null +++ b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lhash.c @@ -0,0 +1,393 @@ +/* + * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <openssl/crypto.h> +#include <openssl/lhash.h> +#include <openssl/err.h> +#include "crypto/ctype.h" +#include "crypto/lhash.h" +#include "lhash_local.h" + +/* + * A hashing implementation that appears to be based on the linear hashing + * algorithm: + * https://en.wikipedia.org/wiki/Linear_hashing + * + * Litwin, Witold (1980), "Linear hashing: A new tool for file and table + * addressing", Proc. 6th Conference on Very Large Databases: 212-223 + * https://hackthology.com/pdfs/Litwin-1980-Linear_Hashing.pdf + * + * From the Wikipedia article "Linear hashing is used in the BDB Berkeley + * database system, which in turn is used by many software systems such as + * OpenLDAP, using a C implementation derived from the CACM article and first + * published on the Usenet in 1988 by Esmond Pitt." + * + * The CACM paper is available here: + * https://pdfs.semanticscholar.org/ff4d/1c5deca6269cc316bfd952172284dbf610ee.pdf + */ + +#undef MIN_NODES +#define MIN_NODES 16 +#define UP_LOAD (2*LH_LOAD_MULT) /* load times 256 (default 2) */ +#define DOWN_LOAD (LH_LOAD_MULT) /* load times 256 (default 1) */ + +static int expand(OPENSSL_LHASH *lh); +static void contract(OPENSSL_LHASH *lh); +static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, const void *data, unsigned long *rhash); + +OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c) +{ + OPENSSL_LHASH *ret; + + if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) { + /* + * Do not set the error code, because the ERR code uses LHASH + * and we want to avoid possible endless error loop. + * CRYPTOerr(CRYPTO_F_OPENSSL_LH_NEW, ERR_R_MALLOC_FAILURE); + */ + return NULL; + } + if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL) + goto err; + ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c); + ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h); + ret->num_nodes = MIN_NODES / 2; + ret->num_alloc_nodes = MIN_NODES; + ret->pmax = MIN_NODES / 2; + ret->up_load = UP_LOAD; + ret->down_load = DOWN_LOAD; + return ret; + +err: + OPENSSL_free(ret->b); + OPENSSL_free(ret); + return NULL; +} + +void OPENSSL_LH_free(OPENSSL_LHASH *lh) +{ + unsigned int i; + OPENSSL_LH_NODE *n, *nn; + + if (lh == NULL) + return; + + for (i = 0; i < lh->num_nodes; i++) { + n = lh->b[i]; + while (n != NULL) { + nn = n->next; + OPENSSL_free(n); + n = nn; + } + } + OPENSSL_free(lh->b); + OPENSSL_free(lh); +} + +void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data) +{ + unsigned long hash; + OPENSSL_LH_NODE *nn, **rn; + void *ret; + + lh->error = 0; + if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh)) + return NULL; /* 'lh->error++' already done in 'expand' */ + + rn = getrn(lh, data, &hash); + + if (*rn == NULL) { + if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) { + lh->error++; + return NULL; + } + nn->data = data; + nn->next = NULL; + nn->hash = hash; + *rn = nn; + ret = NULL; + lh->num_insert++; + lh->num_items++; + } else { /* replace same key */ + ret = (*rn)->data; + (*rn)->data = data; + lh->num_replace++; + } + return ret; +} + +void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data) +{ + unsigned long hash; + OPENSSL_LH_NODE *nn, **rn; + void *ret; + + lh->error = 0; + rn = getrn(lh, data, &hash); + + if (*rn == NULL) { + lh->num_no_delete++; + return NULL; + } else { + nn = *rn; + *rn = nn->next; + ret = nn->data; + OPENSSL_free(nn); + lh->num_delete++; + } + + lh->num_items--; + if ((lh->num_nodes > MIN_NODES) && + (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))) + contract(lh); + + return ret; +} + +void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data) +{ + unsigned long hash; + OPENSSL_LH_NODE **rn; + void *ret; + + tsan_store((TSAN_QUALIFIER int *)&lh->error, 0); + + rn = getrn(lh, data, &hash); + + if (*rn == NULL) { + tsan_counter(&lh->num_retrieve_miss); + return NULL; + } else { + ret = (*rn)->data; + tsan_counter(&lh->num_retrieve); + } + + return ret; +} + +static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg, + OPENSSL_LH_DOALL_FUNC func, + OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg) +{ + int i; + OPENSSL_LH_NODE *a, *n; + + if (lh == NULL) + return; + + /* + * reverse the order so we search from 'top to bottom' We were having + * memory leaks otherwise + */ + for (i = lh->num_nodes - 1; i >= 0; i--) { + a = lh->b[i]; + while (a != NULL) { + n = a->next; + if (use_arg) + func_arg(a->data, arg); + else + func(a->data); + a = n; + } + } +} + +void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func) +{ + doall_util_fn(lh, 0, func, (OPENSSL_LH_DOALL_FUNCARG)0, NULL); +} + +void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg) +{ + doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg); +} + +static int expand(OPENSSL_LHASH *lh) +{ + OPENSSL_LH_NODE **n, **n1, **n2, *np; + unsigned int p, pmax, nni, j; + unsigned long hash; + + nni = lh->num_alloc_nodes; + p = lh->p; + pmax = lh->pmax; + if (p + 1 >= pmax) { + j = nni * 2; + n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j); + if (n == NULL) { + lh->error++; + return 0; + } + lh->b = n; + memset(n + nni, 0, sizeof(*n) * (j - nni)); + lh->pmax = nni; + lh->num_alloc_nodes = j; + lh->num_expand_reallocs++; + lh->p = 0; + } else { + lh->p++; + } + + lh->num_nodes++; + lh->num_expands++; + n1 = &(lh->b[p]); + n2 = &(lh->b[p + pmax]); + *n2 = NULL; + + for (np = *n1; np != NULL;) { + hash = np->hash; + if ((hash % nni) != p) { /* move it */ + *n1 = (*n1)->next; + np->next = *n2; + *n2 = np; + } else + n1 = &((*n1)->next); + np = *n1; + } + + return 1; +} + +static void contract(OPENSSL_LHASH *lh) +{ + OPENSSL_LH_NODE **n, *n1, *np; + + np = lh->b[lh->p + lh->pmax - 1]; + lh->b[lh->p + lh->pmax - 1] = NULL; /* 24/07-92 - eay - weird but :-( */ + if (lh->p == 0) { + n = OPENSSL_realloc(lh->b, + (unsigned int)(sizeof(OPENSSL_LH_NODE *) * lh->pmax)); + if (n == NULL) { + /* fputs("realloc error in lhash",stderr); */ + lh->error++; + return; + } + lh->num_contract_reallocs++; + lh->num_alloc_nodes /= 2; + lh->pmax /= 2; + lh->p = lh->pmax - 1; + lh->b = n; + } else + lh->p--; + + lh->num_nodes--; + lh->num_contracts++; + + n1 = lh->b[(int)lh->p]; + if (n1 == NULL) + lh->b[(int)lh->p] = np; + else { + while (n1->next != NULL) + n1 = n1->next; + n1->next = np; + } +} + +static OPENSSL_LH_NODE **getrn(OPENSSL_LHASH *lh, + const void *data, unsigned long *rhash) +{ + OPENSSL_LH_NODE **ret, *n1; + unsigned long hash, nn; + OPENSSL_LH_COMPFUNC cf; + + hash = (*(lh->hash)) (data); + tsan_counter(&lh->num_hash_calls); + *rhash = hash; + + nn = hash % lh->pmax; + if (nn < lh->p) + nn = hash % lh->num_alloc_nodes; + + cf = lh->comp; + ret = &(lh->b[(int)nn]); + for (n1 = *ret; n1 != NULL; n1 = n1->next) { + tsan_counter(&lh->num_hash_comps); + if (n1->hash != hash) { + ret = &(n1->next); + continue; + } + tsan_counter(&lh->num_comp_calls); + if (cf(n1->data, data) == 0) + break; + ret = &(n1->next); + } + return ret; +} + +/* + * The following hash seems to work very well on normal text strings no + * collisions on /usr/dict/words and it distributes on %2^n quite well, not + * as good as MD5, but still good. + */ +unsigned long OPENSSL_LH_strhash(const char *c) +{ + unsigned long ret = 0; + long n; + unsigned long v; + int r; + + if ((c == NULL) || (*c == '\0')) + return ret; + + n = 0x100; + while (*c) { + v = n | (*c); + n += 0x100; + r = (int)((v >> 2) ^ v) & 0x0f; + ret = (ret << r) | (ret >> (32 - r)); + ret &= 0xFFFFFFFFL; + ret ^= v * v; + c++; + } + return (ret >> 16) ^ ret; +} + +unsigned long openssl_lh_strcasehash(const char *c) +{ + unsigned long ret = 0; + long n; + unsigned long v; + int r; + + if (c == NULL || *c == '\0') + return ret; + + for (n = 0x100; *c != '\0'; n += 0x100) { + v = n | ossl_tolower(*c); + r = (int)((v >> 2) ^ v) & 0x0f; + ret = (ret << r) | (ret >> (32 - r)); + ret &= 0xFFFFFFFFL; + ret ^= v * v; + c++; + } + return (ret >> 16) ^ ret; +} + +unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) +{ + return lh ? lh->num_items : 0; +} + +unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh) +{ + return lh->down_load; +} + +void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load) +{ + lh->down_load = down_load; +} + +int OPENSSL_LH_error(OPENSSL_LHASH *lh) +{ + return lh->error; +} diff --git a/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lhash_local.h b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lhash_local.h new file mode 100644 index 000000000..678224acd --- /dev/null +++ b/roms/edk2/CryptoPkg/Library/OpensslLib/openssl/crypto/lhash/lhash_local.h @@ -0,0 +1,44 @@ +/* + * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ +#include <openssl/crypto.h> + +#include "internal/tsan_assist.h" + +struct lhash_node_st { + void *data; + struct lhash_node_st *next; + unsigned long hash; +}; + +struct lhash_st { + OPENSSL_LH_NODE **b; + OPENSSL_LH_COMPFUNC comp; + OPENSSL_LH_HASHFUNC hash; + unsigned int num_nodes; + unsigned int num_alloc_nodes; + unsigned int p; + unsigned int pmax; + unsigned long up_load; /* load times 256 */ + unsigned long down_load; /* load times 256 */ + unsigned long num_items; + unsigned long num_expands; + unsigned long num_expand_reallocs; + unsigned long num_contracts; + unsigned long num_contract_reallocs; + TSAN_QUALIFIER unsigned long num_hash_calls; + TSAN_QUALIFIER unsigned long num_comp_calls; + unsigned long num_insert; + unsigned long num_replace; + unsigned long num_delete; + unsigned long num_no_delete; + TSAN_QUALIFIER unsigned long num_retrieve; + TSAN_QUALIFIER unsigned long num_retrieve_miss; + TSAN_QUALIFIER unsigned long num_hash_comps; + int error; +}; |