#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using std::map; #include "pirserver.h" #include "ZeroTrace/Globals.hpp" #include "utils.h" #include "ZT.hpp" EC_KEY *ENCLAVE_PUBLIC_KEY = NULL; // Not in use since we use a vector that can expand for LS ORAM #define START_NUM_BLOCKS 100 //MEM_MODE 0 = INSIDE_PRM // 1 = OUTSIDE_PRM #define MEM_MODE 0 //OBLIVIOUS_TYPE 0 = ACCESS_ONLY // 1 = FULL_OBLIVIOUS #define OBLIVIOUS_TYPE 0 //POPULATE_FLAG is for populating the LSORAM with dummy records #define POPULATE_FLAG 0 // TODO: Put everything above this point into a ZT_LSORAMServer.hpp class ZT_LSORAMServer : public PIRServer { private: string pubkey; uint32_t ZT_instance_id; map table; public: ZT_LSORAMServer(); void initializeZeroTrace(string ¶ms); 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(uint32_t instance_id, 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_LSORAMServer::ZT_LSORAMServer() { } void ZT_LSORAMServer::initializeZeroTrace(string ¶ms) { // Variables for Enclave Public Key retrieval 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]; ZT_Initialize(bin_x, bin_y, signature_r, signature_s, max_buff_size); 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(); int ret; if(NULL == (curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))) printf("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){ printf("GetEnclavePublishedKey : Verification Successful! \n"); } else{ printf("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) printf("EC_POINT_set_affine_coordinates FAILED \n"); if(EC_KEY_set_public_key(ENCLAVE_PUBLIC_KEY, pub_point)==0) printf("EC_KEY_set_public_key FAILED \n"); BN_CTX_free(bn_ctx); free(serialized_public_key); pubkey.assign((const char*) serialized_public_key, 2*PRIME256V1_KEY_SIZE); params.assign(pubkey); } void ZT_LSORAMServer::get_params(string ¶ms) { //We get a string to populate with params, (which is just the public key) initializeZeroTrace(params); //NOTE: num_blocks doesn't make a difference for Access-only Oblivious LSORAM //ZT_instance_id = ZT_New_LSORAM(num_blocks, key_size, value_size, mode, oblivious_type, populate_flag); ZT_instance_id = ZT_New_LSORAM(START_NUM_BLOCKS, BLINDED_KEY_SIZE, DESCRIPTOR_MAX_SIZE, MEM_MODE, OBLIVIOUS_TYPE, POPULATE_FLAG); } /* Inputs: a target pub key, a seriailzed request and request size. Outputs: instantiates and populates: client_pubkey, aes_key (from target_pubkey and generated client_pubkey ECDH) iv, encrypted request and tag for the request */ int ZT_LSORAMServer::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){ //Generate a new key EC_KEY *ephemeral_key = NULL; BIGNUM *x, *y; x = BN_new(); y = BN_new(); BN_CTX *bn_ctx = BN_CTX_new(); const EC_GROUP *curve = NULL; if(NULL == (curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))) printf("Setting EC_GROUP failed \n"); ephemeral_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); if(ephemeral_key==NULL) printf("Client: EC_KEY_new_by_curve_name Fail\n"); int ret = EC_KEY_generate_key(ephemeral_key); if(ret!=1) printf("Client: EC_KEY_generate_key Fail\n"); const EC_POINT *pub_point; pub_point = EC_KEY_get0_public_key((const EC_KEY *) ephemeral_key); if(pub_point == NULL) printf("Client: EC_KEY_get0_public_key Fail\n"); ret = EC_POINT_get_affine_coordinates_GFp(curve, pub_point, x, y, bn_ctx); if(ret==0) printf("Client: EC_POINT_get_affine_coordinates_GFp Failed \n"); unsigned char *bin_x, *bin_y; uint32_t size_bin_x = BN_num_bytes(x); uint32_t size_bin_y = BN_num_bytes(y); printf("(%d, %d)\n", size_bin_x, size_bin_y); bin_x = (unsigned char*) malloc(EC_KEY_SIZE); bin_y = (unsigned char*) malloc(EC_KEY_SIZE); BN_bn2bin(x, bin_x); BN_bn2bin(y, bin_y); *pubkey_size_x = size_bin_x; *pubkey_size_y = size_bin_y; *client_pubkey = (unsigned char*) malloc(size_bin_x + size_bin_y); memcpy(*client_pubkey, bin_x, size_bin_x); memcpy(*client_pubkey + size_bin_x, bin_y, size_bin_y); /* unsigned char *ptr = *client_pubkey; printf("Serialized Client's Public Key in encryptLSORAM :\n"); for(int t = 0; t < size_bin_x; t++) printf("%02X", ptr[t]); printf("\n"); printf("Serialized Client's Public Key in encryptLSORAM :\n"); for(int t = 0; t < size_bin_y; t++) printf("%02X", ptr[size_bin_x + t]); printf("\n"); */ uint32_t field_size = EC_GROUP_get_degree(EC_KEY_get0_group(target_public_key)); uint32_t secret_len = (field_size+7)/8; unsigned char *secret = (unsigned char*) malloc(secret_len); //Returns a 32 byte secret secret_len = ECDH_compute_key(secret, secret_len, EC_KEY_get0_public_key(target_public_key), ephemeral_key, NULL); //Sample IV; *ecdh_aes_key = (unsigned char*) malloc (KEY_LENGTH); *iv = (unsigned char*) malloc (IV_LENGTH); memcpy(*ecdh_aes_key, secret, KEY_LENGTH); memcpy(*iv, secret + KEY_LENGTH, IV_LENGTH); /* unsigned char *ecdh_ptr = (unsigned char *) *ecdh_aes_key; unsigned char *iv_ptr = (unsigned char *) *iv; printf("KEY_LENGTH = %d\n", KEY_LENGTH); printf("ecdh_key computed by Client :\n"); for(int t = 0; t < KEY_LENGTH; t++) printf("%02X", ecdh_ptr[t]); printf("\n"); printf("iv computed by Client :\n"); for(int t = 0; t < IV_LENGTH; t++) printf("%02X", iv_ptr[t]); printf("\n"); */ BN_CTX_free(bn_ctx); *encrypted_request = (unsigned char*) malloc (request_size); *tag = (unsigned char*) malloc (TAG_SIZE); uint32_t encrypted_request_size; /* printf("Request bytes before encrypting: \n"); for(int t = 0; t < request_size; t++) printf("%02X", serialized_request[t]); printf("\n"); */ encrypted_request_size = AES_GCM_128_encrypt(serialized_request, request_size, NULL, 0, (unsigned char*) *ecdh_aes_key, (unsigned char*) *iv, IV_LENGTH, *encrypted_request, *tag); /* unsigned char*tag_ptr = *tag; printf("Tag bytes after encryption: \n"); for(uint32_t t = 0; t < TAG_SIZE; t++) printf("%02X", tag_ptr[t]); printf("\n"); printf("Request_size = %d, Encrypted_request_size = %d,\n", request_size, encrypted_request_size); printf("Request bytes after encrypting: \n"); unsigned char *encrypted_ptr = (unsigned char*) *encrypted_request; for(uint32_t t = 0; t < encrypted_request_size; t++) printf("%02X", encrypted_ptr[t]); printf("\n"); */ return encrypted_request_size; } int ZT_LSORAMServer::LSORAM_Insert(uint32_t instance_id, unsigned char *key, uint32_t key_size, unsigned char* value, uint32_t value_size){ unsigned char *serialized_request, *encrypted_request, *tag_in; unsigned char *client_pubkey, *ecdh_aes_key, *iv; uint32_t pubkey_size_x, pubkey_size_y; uint32_t request_size = serializeLSORAMRequest(key, key_size, value, value_size, &serialized_request); encryptLSORAMRequest(ENCLAVE_PUBLIC_KEY, serialized_request, request_size, &encrypted_request, &client_pubkey, &pubkey_size_x, &pubkey_size_y, &ecdh_aes_key, &iv, &tag_in); /* printf("Clientpubkey going into ZT_LSORAM_insert:\n"); printf("X: :\n"); for(int t = 0; t < 32; t++) printf("%02X", client_pubkey[t]); printf("\n"); printf("Y :\n"); for(int t = 0; t < 32; t++) printf("%02X", client_pubkey[32+t]); printf("\n"); */ ZT_LSORAM_insert(instance_id, encrypted_request, request_size, tag_in, TAG_SIZE, client_pubkey, pubkey_size_x, pubkey_size_y); free(serialized_request); return 1; } void ZT_LSORAMServer::store(const string &key, const string &value){ if (value.length() > 0) { //Create encrypted request with strings key, value LSORAM_Insert(ZT_instance_id, (unsigned char*) key.c_str(), BLINDED_KEY_SIZE, (unsigned char*) value.c_str(), DESCRIPTOR_MAX_SIZE); } else { //int8_t ZT_LSORAM_evict(uint32_t id, unsigned char *key, uint32_t key_size); } } /* int ZT_LSORAMServer::LSORAM_Fetch(uint32_t instance_id, unsigned char *key, uint32_t key_size, unsigned char* encrypted_value, uint32_t value_size){ //value needs to be populated by ZT_LSORAM_fetch unsigned char *serialized_request, *encrypted_request, *tag_in; unsigned char *client_pubkey, *ecdh_aes_key, *iv, *response; uint32_t pubkey_size_x, pubkey_size_y; // Response buffer and tag, populated by the enclave unsigned char tag_out[TAG_SIZE]; uint32_t request_size = serializeLSORAMRequest(key, key_size, encrypted_value, 0, &serialized_request); encryptLSORAMRequest(ENCLAVE_PUBLIC_KEY, serialized_request, request_size, &encrypted_request, &client_pubkey, &pubkey_size_x, &pubkey_size_y, &ecdh_aes_key, &iv, &tag_in); ZT_LSORAM_fetch(instance_id, encrypted_request, request_size, encrypted_value, value_size, tag_in, tag_out, TAG_SIZE, client_pubkey, pubkey_size_x, pubkey_size_y); free(serialized_request); } */ /* 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_LSORAMServer::lookup(const string &lookup_query, string &lookup_response) { //TODO: Parse lookup_query and populate these: const char *lookup_query_cstr= lookup_query.c_str(); 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; 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); client_pubkey = (unsigned char*) malloc(pk_x_size+pk_y_size); memcpy(client_pubkey, ptr, pk_x_size+pk_y_size); uint32_t expected_size = BLINDED_KEY_SIZE+TAG_SIZE+ 2*sizeof(uint32_t) + pk_x_size + pk_y_size; if(lookup_query.length()!=expected_size) printf("Query size doesn't match KEY_SIZE + TAG_SIZE\n"); return 0; unsigned char *encrypted_response = (unsigned char*) malloc(DESCRIPTOR_MAX_SIZE); unsigned char *tag_out = (unsigned char*) malloc(TAG_SIZE); ZT_LSORAM_fetch(ZT_instance_id, encrypted_query, BLINDED_KEY_SIZE, encrypted_response, DESCRIPTOR_MAX_SIZE, tag_in, tag_out, TAG_SIZE, client_pubkey, pk_x_size, pk_y_size); 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); return 1; } int main(int argc, char **argv) { ZT_LSORAMServer server; server.mainloop(); return 0; }