#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "pirserver.h" #include "ZeroTrace/Globals.hpp" #include "utils.h" #include "ZT.hpp" #define OBLIVIOUS_TYPE_LS 0 #define MEM_MODE 0 #define POPULATE_FLAG 0 // SET ORAM TYPE: // ORAM_TYPE 0 = Path ORAM // 1 = Circuit ORAM #define ORAM_TYPE 0 //if(ORAM_TYPE == 0) #define STASH_SIZE 150 #define Z 4 //else: // #define STASH_SIZE 10 //TODO: When tinkering with CircuitORAM Z, // lookout for UTILIZATION_PARAMETER in LS // #define Z 2 EC_KEY *ENCLAVE_PUBLIC_KEY = NULL; // ZT Spawn new ORAM command: // ZT_New(max_blocks, data_size, stash_size, oblivious, recursion_data_size, oram_type, Z); #define MAX_BLOCKS 100 #define OBLIVIOUS_TYPE_ORAM 1 #define RECURSION_DATA_SIZE 64 //ORAM Index size (uint32_t) so 4 bytes #define INDEX_SIZE 4 //TODO: Fix PathORAM STASH_SIZE parameter // Fix CircuitORAM STASH_SIZE parameter // Pick STASH_SIZE parameter based on class ZT_ORAMServer : public PIRServer { private: string pubkey; uint32_t ZT_lsoram_iid; uint32_t ZT_oram_iid; uint32_t oram_index; public: ZT_ORAMServer(); void initializeZeroTrace(); virtual void get_params(string ¶ms); virtual void store(const string &key, const string &value); //Helper functions for store: int encryptLSORAMRequest(EC_KEY* target_public_key, unsigned char *serialized_request, uint32_t request_size, unsigned char **encrypted_request, unsigned char **client_pubkey, uint32_t *pubkey_size_x, uint32_t *pubkey_size_y, unsigned char **ecdh_aes_key, unsigned char **iv, unsigned char **tag); int LSORAM_Insert(unsigned char *key, uint32_t key_size, unsigned char* value, uint32_t value_size); virtual bool lookup(const string &lookup_query, string &lookup_response); //Helper functions for lookup: }; ZT_ORAMServer::ZT_ORAMServer() { initializeZeroTrace(); ZT_lsoram_iid = ZT_New_LSORAM(MAX_BLOCKS, BLINDED_KEY_SIZE, INDEX_SIZE, MEM_MODE, OBLIVIOUS_TYPE_LS, POPULATE_FLAG); ZT_oram_iid = ZT_New(MAX_BLOCKS, DESCRIPTOR_MAX_SIZE, STASH_SIZE, OBLIVIOUS_TYPE_ORAM, RECURSION_DATA_SIZE, ORAM_TYPE, Z); oram_index = 0; } void ZT_ORAMServer::initializeZeroTrace() { // Variables for Enclave Public Key retrieval //fprintf(stderr, "ZT_LSORAMServer: init: Starting initializezerotrace \n"); uint32_t max_buff_size = PRIME256V1_KEY_SIZE; unsigned char bin_x[PRIME256V1_KEY_SIZE], bin_y[PRIME256V1_KEY_SIZE], signature_r[PRIME256V1_KEY_SIZE], signature_s[PRIME256V1_KEY_SIZE]; int8_t ret; ret = ZT_Initialize(bin_x, bin_y, signature_r, signature_s, max_buff_size); //fprintf(stderr, "ZT_LSORAMServer: init: After ZT_Initialize ret = %d \n",ret); EC_GROUP *curve; EC_KEY *enclave_verification_key = NULL; ECDSA_SIG *sig_enclave = ECDSA_SIG_new(); BIGNUM *x, *y, *xh, *yh; BN_CTX *bn_ctx = BN_CTX_new(); if(NULL == (curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))) fprintf(stderr, "ZT_LSORAM: init: Setting EC_GROUP failed \n"); EC_POINT *pub_point = EC_POINT_new(curve); //Verify the Enclave Public Key enclave_verification_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); xh = BN_bin2bn(hardcoded_verification_key_x, PRIME256V1_KEY_SIZE, NULL); yh = BN_bin2bn(hardcoded_verification_key_y, PRIME256V1_KEY_SIZE, NULL); EC_KEY_set_public_key_affine_coordinates(enclave_verification_key, xh, yh); unsigned char *serialized_public_key = (unsigned char*) malloc (PRIME256V1_KEY_SIZE*2); memcpy(serialized_public_key, bin_x, PRIME256V1_KEY_SIZE); memcpy(serialized_public_key + PRIME256V1_KEY_SIZE, bin_y, PRIME256V1_KEY_SIZE); sig_enclave->r = BN_bin2bn(signature_r, PRIME256V1_KEY_SIZE, NULL); sig_enclave->s = BN_bin2bn(signature_s, PRIME256V1_KEY_SIZE, NULL); ret = ECDSA_do_verify((const unsigned char*) serialized_public_key, PRIME256V1_KEY_SIZE*2, sig_enclave, enclave_verification_key); if(ret==1){ fprintf(stderr, "ZT_LSORAM: init: GetEnclavePublishedKey : Verification Successful! \n"); } else{ fprintf(stderr, "ZT_LSORAM: init: GetEnclavePublishedKey : Verification FAILED! \n"); } //Load the Enclave Public Key ENCLAVE_PUBLIC_KEY = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); x = BN_bin2bn(bin_x, PRIME256V1_KEY_SIZE, NULL); y = BN_bin2bn(bin_y, PRIME256V1_KEY_SIZE, NULL); if(EC_POINT_set_affine_coordinates_GFp(curve, pub_point, x, y, bn_ctx)==0) fprintf(stderr, "ZT_LSORAMServer: EC_POINT_set_affine_coordinates FAILED \n"); if(EC_KEY_set_public_key(ENCLAVE_PUBLIC_KEY, pub_point)==0) fprintf(stderr, "ZT_LSORAMServer: EC_KEY_set_public_key FAILED \n"); BN_CTX_free(bn_ctx); pubkey.assign((const char*) serialized_public_key, 2*PRIME256V1_KEY_SIZE); free(serialized_public_key); fprintf(stderr, "ZT_LSORAMServer: Finished initializezerotrace, pubkey set \n"); } void ZT_ORAMServer::get_params(string ¶ms) { //Populate params with the enclave public key fprintf(stderr, "ZT_LSORAMServer: Started get_params(), params length = %ld\n", params.length()); if(pubkey.empty()) fprintf(stderr, "ZT_LSORAMServer: pubkey is empty when get_params is called. WHY? \n"); params.assign(pubkey); fprintf(stderr, "ZT_LSORAMServer: Finished get_params(), params.length = %ld,\n", params.length()); /* unsigned char *param_ptr = (unsigned char*) params.c_str(); fprintf(stderr, "Param in ZT_LSORAMServer::get_params():"); for(uint32_t i=0;i 0) { //Create encrypted request with strings key, value unsigned char *key_ptr = (unsigned char*) key.c_str(); unsigned char *value_ptr = (unsigned char*) value.c_str(); //fprintf(stderr, "ZT_LSORAMServer: Starting store(), key.length= %ld, value.length = %ld\n", key.length(), value.length()); LSORAM_Insert((unsigned char*) key.c_str(), BLINDED_KEY_SIZE, (unsigned char*) value.c_str(), DESCRIPTOR_MAX_SIZE); fprintf(stderr, "ZT_ORAMServer: STORED HSDesc Key = "); for(uint32_t i = 0; i <32; i++){ fprintf(stderr, "%02x", key_ptr[i]); } fprintf(stderr,"\n"); fprintf(stderr, "ZT_ORAMServer: STORED (First 32 bytes of ) HSDesc Value= "); for(uint64_t i = 0; i < 32; i++){ fprintf(stderr, "%02x", value_ptr[i]); } fprintf(stderr,"\n"); } else { //int8_t ZT_LSORAM_evict(uint32_t id, unsigned char *key, uint32_t key_size); } } /* In ZT_LSORAMClient lookup_query should be: encrypted_query||tag_in||pk_x_size||pk_y_size||client_pubkey where client_pubkey is of size pk_x_size+pk_y_size returns lookup_response: encrypted_response||tag_out */ bool ZT_ORAMServer::lookup(const string &lookup_query, string &lookup_response) { fprintf(stderr, "ZT_ORAMServer: Starting lookup() call\n"); const char *lookup_query_cstr= lookup_query.c_str(); fprintf(stderr, "ZT_ORAMServer: lookup_query length = %ld\n", lookup_query.length()); unsigned char *encrypted_query = (unsigned char*) malloc (BLINDED_KEY_SIZE); unsigned char *tag_in = (unsigned char*) malloc (TAG_SIZE); uint32_t pk_x_size; uint32_t pk_y_size; unsigned char *client_pubkey; fprintf(stderr, "ZT_ORAMServer: Before parsing lookup_query_cstr\n"); unsigned char *ptr = (unsigned char*) lookup_query_cstr; memcpy(encrypted_query, ptr, BLINDED_KEY_SIZE); ptr+=BLINDED_KEY_SIZE; memcpy(tag_in, ptr, TAG_SIZE); ptr+=TAG_SIZE; memcpy(&pk_x_size, ptr, sizeof(uint32_t)); ptr+=sizeof(uint32_t); memcpy(&pk_y_size, ptr, sizeof(uint32_t)); ptr+=sizeof(uint32_t); size_t expected_size = BLINDED_KEY_SIZE+TAG_SIZE+ 2*sizeof(uint32_t) + pk_x_size + pk_y_size; fprintf(stderr, "ZT_ORAMServer: Before allocating client_pubkey call (%d,%d)\n", pk_x_size, pk_y_size); fprintf(stderr, "ZT_ORAMServer: expected_size = %ld, lookup_query.length = %ld\n", expected_size, lookup_query.length()); /* if(lookup_query.length()!=expected_size) fprintf(stderr, "ZT_LSORAMServer: Query size doesn't match KEY_SIZE + TAG_SIZE\n"); return 0; */ client_pubkey = (unsigned char*) malloc (pk_x_size+pk_y_size); memcpy(client_pubkey, ptr, (pk_x_size + pk_y_size)); unsigned char *encrypted_response = (unsigned char*) malloc(DESCRIPTOR_MAX_SIZE); unsigned char *tag_out = (unsigned char*) malloc(TAG_SIZE); fprintf(stderr, "ZT_ORAMServer: Before ZT_LSORAM_fetch() call\n"); ZT_HSORAM_fetch(ZT_lsoram_iid, ZT_oram_iid, ORAM_TYPE, encrypted_query, BLINDED_KEY_SIZE, encrypted_response, DESCRIPTOR_MAX_SIZE, tag_in, tag_out, TAG_SIZE, client_pubkey, pk_x_size, pk_y_size); fprintf(stderr, "ZT_ORAMServer: After ZT_LSORAM_fetch() call\n"); lookup_response.assign((const char*) encrypted_response, DESCRIPTOR_MAX_SIZE); lookup_response.append((const char*) tag_out, TAG_SIZE); free(encrypted_response); free(tag_out); free(client_pubkey); free(encrypted_query); free(tag_in); fprintf(stderr, "ZT_ORAMServer: Finished lookup() call\n"); return 1; } int main(int argc, char **argv) { ZT_ORAMServer server; server.mainloop(); return 0; }