fix: replace deprecated calls to MD5 functions

perf: keep EVP context in memory during lifetime of DHT_Wrapper
This commit is contained in:
Max Luebke 2022-12-01 16:46:58 +01:00
parent f7b2e61589
commit d0307bb48b
3 changed files with 113 additions and 23 deletions

View File

@ -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 <cstdint>
#include <openssl/evp.h>
namespace poet {
void initHashCtx(const EVP_MD *md);
void freeHashCtx();
uint64_t hashDHT(int key_size, void *key);
}
#endif // HASHFUNCTIONS_H_

View File

@ -18,39 +18,26 @@
** Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "poet/HashFunctions.hpp"
#include <cstddef>
#include <cstdint>
#include <openssl/evp.h>
#include <poet/DHT_Wrapper.hpp>
#include <math.h>
#include <openssl/md5.h>
#include <iostream>
#include <stdexcept>
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 &params, 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<bool> &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;
}

62
src/HashFunctions.cpp Normal file
View File

@ -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 <cstddef>
#include <openssl/evp.h>
#include <openssl/md5.h>
#include <stdexcept>
// 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;
}