Skip to content
Snippets Groups Projects
Commit 0dd409b8 authored by Mahyar Vahabi's avatar Mahyar Vahabi
Browse files

oram

parent 794dfb85
No related branches found
No related tags found
No related merge requests found
/***************************
* *************************
* Installations: apt-get install libssl-dev
* Compile like: g++ oram.cpp -o oram -I/usr/include -L/usr/lib -lssl -lcrypto
*/
#include <iostream>
#include <vector>
#include <openssl/rand.h>
#include <openssl/evp.h>
#define BLOCK_SIZE 16 // Memory block size
#define TREE_HEIGHT 4 // Path ORAM tree depth
using namespace std;
// Encrypt data using AES
void encrypt_block(vector<unsigned char>& data, const unsigned char* key) {
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
unsigned char iv[BLOCK_SIZE] = {0}; // Initialization vector
EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv);
int len;
EVP_EncryptUpdate(ctx, data.data(), &len, data.data(), BLOCK_SIZE);
EVP_EncryptFinal_ex(ctx, data.data() + len, &len);
EVP_CIPHER_CTX_free(ctx);
}
// ORAM Storage Class
class PathORAM {
private:
vector<vector<unsigned char>> storage; // Encrypted memory blocks
unsigned char key[16]; // AES encryption key
public:
PathORAM(int num_blocks) {
RAND_bytes(key, sizeof(key)); // Generate encryption key
storage.resize(num_blocks, vector<unsigned char>(BLOCK_SIZE, 0));
}
void write(int index, vector<unsigned char> data) {
encrypt_block(data, key);
storage[index] = data;
}
vector<unsigned char> read(int index) {
return storage[index]; // Return encrypted data (decryption step needed)
}
};
int main() {
PathORAM oram(16); // Initialize ORAM with 16 memory blocks
vector<unsigned char> model_data(BLOCK_SIZE, 42); // Sample ML model data
oram.write(0, model_data); // Store encrypted model parameters
vector<unsigned char> retrieved_data = oram.read(0); // Retrieve encrypted data
cout << "Encrypted data stored in ORAM: ";
for (unsigned char c : retrieved_data) {
cout << hex << (int)c << " ";
}
cout << endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment