Skip to content

Commit

Permalink
Updated PRF
Browse files Browse the repository at this point in the history
  • Loading branch information
sharathvemula committed Feb 20, 2023
1 parent 4447ab0 commit e83b362
Show file tree
Hide file tree
Showing 72 changed files with 130,674 additions and 34 deletions.
Binary file modified waffleClient/bin/proxy_benchmark
Binary file not shown.
Binary file modified waffleClient/bin/proxy_server
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
19,889 changes: 19,889 additions & 0 deletions waffleClient/data/BST_Latency_MonFeb2013:01/1

Large diffs are not rendered by default.

19,307 changes: 19,307 additions & 0 deletions waffleClient/data/BST_Latency_MonFeb2013:07/1

Large diffs are not rendered by default.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
583 changes: 583 additions & 0 deletions waffleClient/data/BST_Latency_SunFeb1919:05/1

Large diffs are not rendered by default.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2,340 changes: 2,340 additions & 0 deletions waffleClient/data/BST_Latency_SunFeb1923:21/1

Large diffs are not rendered by default.

19,895 changes: 19,895 additions & 0 deletions waffleClient/data/BST_Latency_SunFeb1923:23/1

Large diffs are not rendered by default.

2,077 changes: 2,077 additions & 0 deletions waffleClient/data/MonFeb2013:02/1

Large diffs are not rendered by default.

2,070 changes: 2,070 additions & 0 deletions waffleClient/data/MonFeb2013:08/1

Large diffs are not rendered by default.

19,691 changes: 19,691 additions & 0 deletions waffleClient/data/Redis_Latency_MonFeb2013:01/1

Large diffs are not rendered by default.

19,691 changes: 19,691 additions & 0 deletions waffleClient/data/Redis_Latency_MonFeb2013:07/1

Large diffs are not rendered by default.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
546 changes: 546 additions & 0 deletions waffleClient/data/Redis_Latency_SunFeb1919:05/1

Large diffs are not rendered by default.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2,188 changes: 2,188 additions & 0 deletions waffleClient/data/Redis_Latency_SunFeb1923:21/1

Large diffs are not rendered by default.

20,238 changes: 20,238 additions & 0 deletions waffleClient/data/Redis_Latency_SunFeb1923:23/1

Large diffs are not rendered by default.

2,101 changes: 2,101 additions & 0 deletions waffleClient/data/SunFeb1923:24/1

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions waffleClient/proxy/src/encryption_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "encryption_engine.h"
#include <iostream>

std::string encryption_engine::extractKey(std::string encryptedKey) {
std::string encryption_engine::extractKey(const std::string& encryptedKey) {
for(int i=encryptedKey.size()-1; i>=0; --i) {
if(encryptedKey[i] == '#') {
return encryptedKey.substr(0, i);
Expand Down Expand Up @@ -546,7 +546,7 @@ std::string encryption_engine::hmac(const std::string &key) {
};

std::string encryption_engine::encryptNonDeterministic(const std::string &plain_text) {
return encrypt(plain_text + '#' + gen_random(rand()%10));
return encrypt(plain_text + "#" + gen_random(rand()%10));
};

std::string encryption_engine::decryptNonDeterministic(const std::string &cipher_text){
Expand All @@ -555,4 +555,19 @@ std::string encryption_engine::decryptNonDeterministic(const std::string &cipher

std::string encryption_engine::getencryption_string_(){
return encryption_string_;
}
};

std::string encryption_engine::prf_encrypt(const std::string& key, const std::string& plaintext) {
unsigned char* result = new unsigned char[32];
unsigned int resultlen;

HMAC(EVP_sha256(), key.c_str(), key.length(), reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length(), result, &resultlen);

std::string ciphertext(reinterpret_cast<char*>(result), resultlen);
delete[] result;
return ciphertext;
};

std::string encryption_engine::prf(const std::string& plain_text) {
return prf_encrypt(encryption_string_, plain_text);
};
6 changes: 3 additions & 3 deletions waffleClient/proxy/src/encryption_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ class encryption_engine {
std::string encryptNonDeterministic(const std::string &plain_text);
std::string decryptNonDeterministic(const std::string &cipher_text);
std::string getencryption_string_();
std::string extractKey(std::string encryptedKey);
std::string extractKey(const std::string &encryptedKey);
std::string gen_random(const int len);


std::string prf(const std::string &plain_text);

private:
void handle_errors();
Expand All @@ -56,5 +55,6 @@ class encryption_engine {
unsigned char * encryption_key_;
unsigned char * iv_;
EVP_PKEY * skey_, * vkey_;
std::string prf_encrypt(const std::string& key, const std::string& plaintext);
};
#endif //WAFFLE_BASIC_CRYPTO_H
65 changes: 37 additions & 28 deletions waffleClient/proxy/src/waffle_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ void waffle_proxy::init(const std::vector<std::string> &keys, const std::vector<
//Adding the data to Database
std::cout << "Keys size in init() is " << keys.size() << std::endl;
std::unordered_map<std::string, std::string> keyValueMap;
// std::cout << "0th key is " << keys[0] << std::endl;
for(int i = 0; i<keys.size(); ++i) {
realBst.insert(keys[i]);
keyValueMap[encryption_engine_.hmac(keys[i] + "#" + std::to_string(realBst.getFrequency(keys[i])))] = encryption_engine_.encryptNonDeterministic(values[i]);
keyValueMap[encryption_engine_.prf(keys[i] + "#" + std::to_string(realBst.getFrequency(keys[i])))] = encryption_engine_.encryptNonDeterministic(values[i]);
}

// if(keyValueMap.find(encryption_engine_.encrypt("user5159055689701779241#0")) == keyValueMap.end()) {
// std::cout << "First querying key is not available " << std::endl;
// }

std::cout << "Init encryption string is " << encryption_engine_.getencryption_string_() << std::endl;
// Initialising Cache
size_t cacheCapacity = R+s;
std::unordered_set<std::string> temp;
Expand All @@ -132,7 +137,10 @@ void waffle_proxy::init(const std::vector<std::string> &keys, const std::vector<
temp.insert(keys[index]);
keysCacheUnencrypted.push_back(keys[index]);
valuesCache.push_back(values[index]);
keyValueMap.erase(encryption_engine_.hmac(keys[index] + "#" + std::to_string(realBst.getFrequency(keys[index]))));
if(keyValueMap.find(encryption_engine_.prf(keys[index] + "#" + std::to_string(realBst.getFrequency(keys[index])))) == keyValueMap.end()) {
std::cout << "WARNING: Key is Missing & this should not happen" << std::endl;
}
keyValueMap.erase(encryption_engine_.prf(keys[index] + "#" + std::to_string(realBst.getFrequency(keys[index]))));
}
}

Expand All @@ -144,8 +152,8 @@ void waffle_proxy::init(const std::vector<std::string> &keys, const std::vector<
++i;
fakeBst.insert(fakeKey);
tempFakeKeys.insert(fakeKey);
auto tempFakeKey = encryption_engine_.hmac(fakeKey + "#" + std::to_string(fakeBst.getFrequency(fakeKey)));
auto fakeKeyValue = encryption_engine_.encryptNonDeterministic(gen_random(rand()%10));
auto tempFakeKey = encryption_engine_.prf(fakeKey + "#" + std::to_string(fakeBst.getFrequency(fakeKey)));
auto fakeKeyValue = encryption_engine_.encryptNonDeterministic(gen_random(1 + rand()%10));
keyValueMap[tempFakeKey] = fakeKeyValue;
}
}
Expand All @@ -171,34 +179,27 @@ void waffle_proxy::init(const std::vector<std::string> &keys, const std::vector<
threads_.push_back(std::thread(&waffle_proxy::clearThread, this));

//Tests for the encryption
auto test1 = encryption_engine_.hmac("mynameissharath");
auto test2 = encryption_engine_.hmac("mynameissharath");

if(test1 == test2) {
std::cout << "Encryption is same" << std::endl;
} else {
std::cout << "Encryption is not same" << std::endl;
}


auto test3 = encryption_engine_.encryptNonDeterministic("mynameissharath");
auto test4 = encryption_engine_.encryptNonDeterministic("mynameissharath");
if(test3 == test4) {
std::cout << "Non Deterministic Encryption is same" << std::endl;
} else {
std::cout << "Non Deterministic Encryption is not same and test3 is " << test3 << std::endl;
std::cout << "Non Deterministic Encryption is not same and test4 is " << test4 << std::endl;
std::cout << "Non Deterministic Encryption is not same" << std::endl;
}

std::cout << "Encryption string is " << encryption_engine_.getencryption_string_() << std::endl;

std::string test5 = encryption_engine_.decryptNonDeterministic(test3);
std::string test6 = encryption_engine_.decryptNonDeterministic(test4);
if(test5 == test6) {
std::cout << "Decryption is same and the value is " << test5 << std::endl;
std::cout << "non deterministic Decryption is same and the value is " << test5 << std::endl;
} else {
std::cout << "Decryption is not same and the value is " << test5 << std::endl;
std::cout << "Decryption is not same and the value is " << test6 << std::endl;
std::cout << "non deterministic Decryption is not same and the value is " << test5 << std::endl;
std::cout << "non deterministic Decryption is not same and the value is " << test6 << std::endl;
}

auto test7 = encryption_engine_.prf("mynameissharath1");
auto test8 = encryption_engine_.prf("mynameissharath1");
if(test7 == test8) {
std::cout << "PRF encryption is same" << std::endl;
}
//Tests for the encryption ended

Expand Down Expand Up @@ -263,9 +264,12 @@ void waffle_proxy::execute_batch(const std::vector<operation> &operations, std::
if(latency) {
rdtscllProxy(start);
}

std::cout << "execute_batch encryption string is " << enc_engine->getencryption_string_() << std::endl;

for(int i = 0; i < operations.size(); i++){
std::string key = operations[i].key;
auto stKey = enc_engine->hmac(key + "#" + std::to_string(realBst.getFrequency(key)));
auto stKey = enc_engine->prf(key + "#" + std::to_string(realBst.getFrequency(key)));
readBatchMap[stKey] = key;
storage_keys.push_back(stKey);
realBst.incrementFrequency(key);
Expand All @@ -284,15 +288,15 @@ void waffle_proxy::execute_batch(const std::vector<operation> &operations, std::
// std::cout << "realKeysNotInCache size is " << realKeysNotInCache.size() << std::endl;

for(auto& iter: realKeysNotInCache) {
auto stKey = enc_engine->hmac(iter + "#" + std::to_string(realBst.getFrequency(iter)));
auto stKey = enc_engine->encrypt(iter + "#" + std::to_string(realBst.getFrequency(iter)));
readBatchMap[stKey] = iter;
storage_keys.push_back(stKey);
realBst.incrementFrequency(iter);
}

for(int i=0;i<(D-operations.size());++i) {
auto fakeMinKey = fakeBst.getKeyWithMinFrequency();
auto stKey = enc_engine->hmac(fakeMinKey + "#" + std::to_string(fakeBst.getFrequency(fakeMinKey)));
auto stKey = enc_engine->prf(fakeMinKey + "#" + std::to_string(fakeBst.getFrequency(fakeMinKey)));
readBatchMap[stKey] = fakeMinKey;
storage_keys.push_back(stKey);
fakeBst.incrementFrequency(fakeMinKey);
Expand All @@ -310,13 +314,19 @@ void waffle_proxy::execute_batch(const std::vector<operation> &operations, std::
if(latency) {
rdtscllProxy(start);
}

// std::cout << "Querying key value pairs in execute_batch() new" << std::endl;
// for(int i=0;i<storage_keys.size();++i) {
// std::cout << "Getting response for " << i << "th key" << std::endl;
// std::cout << storage_interface->get(storage_keys[i]) << std::endl;
// }
auto responses = storage_interface->get_batch(storage_keys);
// std::cout << "Got key value pairs" << std::endl;
for(int i = 0 ; i < storage_keys.size(); i++){
if(i < (operations.size() + realKeysNotInCache.size())) {
// This means ith request is for real key
auto kv_pair = cache.evictLRElementFromCache();
writeBatchKeys.push_back(enc_engine->hmac(kv_pair[0] + "#" + std::to_string(realBst.getFrequency(kv_pair[0]))));
writeBatchKeys.push_back(enc_engine->prf(kv_pair[0] + "#" + std::to_string(realBst.getFrequency(kv_pair[0]))));
writeBatchValues.push_back(enc_engine->encryptNonDeterministic(kv_pair[1]));

auto keyAboutToGoToCache = readBatchMap[storage_keys[i]];
Expand All @@ -334,9 +344,8 @@ void waffle_proxy::execute_batch(const std::vector<operation> &operations, std::
} else {
// Writing fake key values to DB
auto fakeWriteKey = readBatchMap[storage_keys[i]];
writeBatchKeys.push_back(enc_engine->hmac(fakeWriteKey + "#" + std::to_string(fakeBst.getFrequency(fakeWriteKey))));
writeBatchValues.push_back(enc_engine->encryptNonDeterministic(gen_random(rand()%10)));

writeBatchKeys.push_back(enc_engine->prf(fakeWriteKey + "#" + std::to_string(fakeBst.getFrequency(fakeWriteKey))));
writeBatchValues.push_back(enc_engine->encryptNonDeterministic(gen_random(1 + rand()%10)));
}
}
storage_interface_->put_batch(writeBatchKeys, writeBatchValues);
Expand Down

0 comments on commit e83b362

Please sign in to comment.