diff --git a/include/poet/HashFunctions.hpp b/include/poet/HashFunctions.hpp new file mode 100644 index 000000000..84ae38ed6 --- /dev/null +++ b/include/poet/HashFunctions.hpp @@ -0,0 +1,38 @@ +/* +** Copyright (C) 2018-2021 Alexander Lindemann, Max Luebke (University of +** Potsdam) +** +** Copyright (C) 2018-2022 Marco De Lucia, Max Luebke (GFZ Potsdam) +** +** POET is free software; you can redistribute it and/or modify it under the +** terms of the GNU General Public License as published by the Free Software +** Foundation; either version 2 of the License, or (at your option) any later +** version. +** +** POET is distributed in the hope that it will be useful, but WITHOUT ANY +** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +** A PARTICULAR PURPOSE. See the GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License along with +** this program; if not, write to the Free Software Foundation, Inc., 51 +** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef HASHFUNCTIONS_H_ +#define HASHFUNCTIONS_H_ + +#include +#include + +namespace poet { + +void initHashCtx(const EVP_MD *md); +void freeHashCtx(); + +uint64_t hashDHT(int key_size, void *key); + + + +} + +#endif // HASHFUNCTIONS_H_ diff --git a/src/DHT_Wrapper.cpp b/src/DHT_Wrapper.cpp index 3f398e552..988d409af 100644 --- a/src/DHT_Wrapper.cpp +++ b/src/DHT_Wrapper.cpp @@ -18,39 +18,26 @@ ** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "poet/HashFunctions.hpp" +#include +#include +#include #include #include -#include #include +#include using namespace poet; using namespace std; -uint64_t poet::get_md5(int key_size, void *key) { - MD5_CTX ctx; - unsigned char sum[MD5_DIGEST_LENGTH]; - uint64_t retval, *v1, *v2; - - // calculate md5 using MD5 functions - MD5_Init(&ctx); - MD5_Update(&ctx, key, key_size); - MD5_Final(sum, &ctx); - - // divide hash in 2 64 bit parts and XOR them - v1 = (uint64_t *)&sum[0]; - v2 = (uint64_t *)&sum[8]; - retval = *v1 ^ *v2; - - return retval; -} - DHT_Wrapper::DHT_Wrapper(SimParams ¶ms, MPI_Comm dht_comm, int buckets_per_process, int data_size, int key_size) { + poet::initHashCtx(EVP_md5()); // initialize DHT object - dht_object = - DHT_create(dht_comm, buckets_per_process, data_size, key_size, &get_md5); + dht_object = DHT_create(dht_comm, buckets_per_process, data_size, key_size, + &poet::hashDHT); // allocate memory for fuzzing buffer fuzzing_buffer = (double *)malloc(key_size); @@ -69,6 +56,7 @@ DHT_Wrapper::~DHT_Wrapper() { DHT_free(dht_object, NULL, NULL); // free fuzzing buffer free(fuzzing_buffer); + poet::freeHashCtx(); } void DHT_Wrapper::checkDHT(int length, std::vector &out_result_index, @@ -149,7 +137,8 @@ int DHT_Wrapper::tableToFile(const char *filename) { int DHT_Wrapper::fileToTable(const char *filename) { int res = DHT_from_file(dht_object, filename); - if (res != DHT_SUCCESS) return res; + if (res != DHT_SUCCESS) + return res; #ifdef DHT_STATISTICS DHT_print_statistics(dht_object); @@ -228,5 +217,6 @@ void DHT_Wrapper::fuzzForDHT(int var_count, void *key, double dt) { } // if timestep differs over iterations set current current time step at the // end of fuzzing buffer - if (dt_differ) fuzzing_buffer[var_count] = dt; + if (dt_differ) + fuzzing_buffer[var_count] = dt; } diff --git a/src/HashFunctions.cpp b/src/HashFunctions.cpp new file mode 100644 index 000000000..ee6393e14 --- /dev/null +++ b/src/HashFunctions.cpp @@ -0,0 +1,62 @@ +/* +** Copyright (C) 2018-2021 Alexander Lindemann, Max Luebke (University of +** Potsdam) +** +** Copyright (C) 2018-2022 Marco De Lucia, Max Luebke (GFZ Potsdam) +** +** POET is free software; you can redistribute it and/or modify it under the +** terms of the GNU General Public License as published by the Free Software +** Foundation; either version 2 of the License, or (at your option) any later +** version. +** +** POET is distributed in the hope that it will be useful, but WITHOUT ANY +** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +** A PARTICULAR PURPOSE. See the GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License along with +** this program; if not, write to the Free Software Foundation, Inc., 51 +** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "poet/HashFunctions.hpp" + +#include +#include +#include +#include + +// HACK: I know this is not a good practice, but this will do it for now! +EVP_MD_CTX *ctx = NULL; + +void poet::initHashCtx(const EVP_MD *md) { + if (ctx == NULL) { + ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, md, NULL); + } +} + +void poet::freeHashCtx() { + EVP_MD_CTX_free(ctx); + ctx = NULL; +} + +uint64_t poet::hashDHT(int key_size, void *key) { + unsigned char sum[MD5_DIGEST_LENGTH]; + uint32_t md_len; + uint64_t retval, *v1, *v2; + + // calculate md5 using MD5 functions + EVP_DigestUpdate(ctx, key, key_size); + EVP_DigestFinal_ex(ctx, sum, &md_len); + + if (md_len != MD5_DIGEST_LENGTH) { + throw std::runtime_error("Something went wrong during MD5 hashing!"); + } + + // divide hash in 2 64 bit parts and XOR them + v1 = (uint64_t *)&sum[0]; + v2 = (uint64_t *)&sum[8]; + retval = *v1 ^ *v2; + + return retval; +}