ZT_LSORAMserver.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <cstring>
  9. #include <iostream>
  10. #include <map>
  11. #include <iterator>
  12. #include <openssl/ec.h>
  13. #include <openssl/ecdh.h>
  14. #include <openssl/ecdsa.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/err.h>
  18. #include <openssl/obj_mac.h>
  19. using std::map;
  20. #include "pirserver.h"
  21. #include "ZeroTrace/Globals.hpp"
  22. #include "utils.h"
  23. #include "ZT.hpp"
  24. EC_KEY *ENCLAVE_PUBLIC_KEY = NULL;
  25. // Not in use since we use a vector that can expand for LS ORAM
  26. #define START_NUM_BLOCKS 100
  27. //MEM_MODE 0 = INSIDE_PRM
  28. // 1 = OUTSIDE_PRM
  29. #define MEM_MODE 0
  30. //OBLIVIOUS_TYPE 0 = ACCESS_ONLY
  31. // 1 = FULL_OBLIVIOUS
  32. #define OBLIVIOUS_TYPE 0
  33. //POPULATE_FLAG is for populating the LSORAM with dummy records
  34. #define POPULATE_FLAG 0
  35. // TODO: Put everything above this point into a ZT_LSORAMServer.hpp
  36. class ZT_LSORAMServer : public PIRServer {
  37. private:
  38. string pubkey;
  39. uint32_t ZT_instance_id;
  40. public:
  41. ZT_LSORAMServer();
  42. void initializeZeroTrace();
  43. virtual void get_params(string &params);
  44. virtual void store(const string &key, const string &value);
  45. //Helper functions for store:
  46. int encryptLSORAMRequest(EC_KEY* target_public_key, unsigned char *serialized_request,
  47. uint32_t request_size, unsigned char **encrypted_request, unsigned char **client_pubkey,
  48. uint32_t *pubkey_size_x, uint32_t *pubkey_size_y, unsigned char **ecdh_aes_key,
  49. unsigned char **iv, unsigned char **tag);
  50. int LSORAM_Insert(uint32_t instance_id, unsigned char *key, uint32_t key_size,
  51. unsigned char* value, uint32_t value_size);
  52. virtual bool lookup(const string &lookup_query, string &lookup_response);
  53. //Helper functions for lookup:
  54. };
  55. ZT_LSORAMServer::ZT_LSORAMServer() {
  56. initializeZeroTrace();
  57. //NOTE NUM_BLOCKS does not matter for Access_only oblivious LSORAM
  58. ZT_instance_id = ZT_New_LSORAM(START_NUM_BLOCKS, BLINDED_KEY_SIZE, DESCRIPTOR_MAX_SIZE, MEM_MODE, OBLIVIOUS_TYPE, POPULATE_FLAG);
  59. }
  60. void ZT_LSORAMServer::initializeZeroTrace() {
  61. // Variables for Enclave Public Key retrieval
  62. //fprintf(stderr, "ZT_LSORAMServer: init: Starting initializezerotrace \n");
  63. uint32_t max_buff_size = PRIME256V1_KEY_SIZE;
  64. unsigned char bin_x[PRIME256V1_KEY_SIZE], bin_y[PRIME256V1_KEY_SIZE], signature_r[PRIME256V1_KEY_SIZE], signature_s[PRIME256V1_KEY_SIZE];
  65. int8_t ret;
  66. ret = ZT_Initialize(bin_x, bin_y, signature_r, signature_s, max_buff_size);
  67. //fprintf(stderr, "ZT_LSORAMServer: init: After ZT_Initialize ret = %d \n",ret);
  68. EC_GROUP *curve;
  69. EC_KEY *enclave_verification_key = NULL;
  70. ECDSA_SIG *sig_enclave = ECDSA_SIG_new();
  71. BIGNUM *x, *y, *xh, *yh;
  72. BN_CTX *bn_ctx = BN_CTX_new();
  73. if(NULL == (curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)))
  74. fprintf(stderr, "ZT_LSORAM: init: Setting EC_GROUP failed \n");
  75. EC_POINT *pub_point = EC_POINT_new(curve);
  76. //Verify the Enclave Public Key
  77. enclave_verification_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  78. xh = BN_bin2bn(hardcoded_verification_key_x, PRIME256V1_KEY_SIZE, NULL);
  79. yh = BN_bin2bn(hardcoded_verification_key_y, PRIME256V1_KEY_SIZE, NULL);
  80. EC_KEY_set_public_key_affine_coordinates(enclave_verification_key, xh, yh);
  81. unsigned char *serialized_public_key = (unsigned char*) malloc (PRIME256V1_KEY_SIZE*2);
  82. memcpy(serialized_public_key, bin_x, PRIME256V1_KEY_SIZE);
  83. memcpy(serialized_public_key + PRIME256V1_KEY_SIZE, bin_y, PRIME256V1_KEY_SIZE);
  84. sig_enclave->r = BN_bin2bn(signature_r, PRIME256V1_KEY_SIZE, NULL);
  85. sig_enclave->s = BN_bin2bn(signature_s, PRIME256V1_KEY_SIZE, NULL);
  86. ret = ECDSA_do_verify((const unsigned char*) serialized_public_key, PRIME256V1_KEY_SIZE*2, sig_enclave, enclave_verification_key);
  87. if(ret==1){
  88. fprintf(stderr, "ZT_LSORAM: init: GetEnclavePublishedKey : Verification Successful! \n");
  89. }
  90. else{
  91. fprintf(stderr, "ZT_LSORAM: init: GetEnclavePublishedKey : Verification FAILED! \n");
  92. }
  93. //Load the Enclave Public Key
  94. ENCLAVE_PUBLIC_KEY = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  95. x = BN_bin2bn(bin_x, PRIME256V1_KEY_SIZE, NULL);
  96. y = BN_bin2bn(bin_y, PRIME256V1_KEY_SIZE, NULL);
  97. if(EC_POINT_set_affine_coordinates_GFp(curve, pub_point, x, y, bn_ctx)==0)
  98. fprintf(stderr, "ZT_LSORAMServer: EC_POINT_set_affine_coordinates FAILED \n");
  99. if(EC_KEY_set_public_key(ENCLAVE_PUBLIC_KEY, pub_point)==0)
  100. fprintf(stderr, "ZT_LSORAMServer: EC_KEY_set_public_key FAILED \n");
  101. BN_CTX_free(bn_ctx);
  102. pubkey.assign((const char*) serialized_public_key, 2*PRIME256V1_KEY_SIZE);
  103. free(serialized_public_key);
  104. fprintf(stderr, "ZT_LSORAMServer: Finished initializezerotrace, pubkey set \n");
  105. }
  106. void ZT_LSORAMServer::get_params(string &params) {
  107. //Populate params with the enclave public key
  108. fprintf(stderr, "ZT_LSORAMServer: Started get_params(), params length = %ld\n", params.length());
  109. if(pubkey.empty())
  110. fprintf(stderr, "ZT_LSORAMServer: pubkey is empty when get_params is called. WHY? \n");
  111. params.assign(pubkey);
  112. fprintf(stderr, "ZT_LSORAMServer: Finished get_params(), params.length = %ld,\n", params.length());
  113. /*
  114. unsigned char *param_ptr = (unsigned char*) params.c_str();
  115. fprintf(stderr, "Param in ZT_LSORAMServer::get_params():");
  116. for(uint32_t i=0;i<params.length();i++){
  117. fprintf(stderr, "%02x", param_ptr[i]);
  118. }
  119. fprintf(stderr, "\n");
  120. fprintf(stderr, "ZT_LSORAMServer::ZT_instance_id = %d, params length = %ld\n", ZT_instance_id, params.length());
  121. */
  122. }
  123. /*
  124. Inputs: a target pub key, a seriailzed request and request size.
  125. Outputs: instantiates and populates:
  126. client_pubkey, aes_key (from target_pubkey and generated client_pubkey ECDH)
  127. iv, encrypted request and tag for the request
  128. */
  129. int ZT_LSORAMServer::encryptLSORAMRequest(EC_KEY* target_public_key, unsigned char *serialized_request,
  130. uint32_t request_size, unsigned char **encrypted_request, unsigned char **client_pubkey,
  131. uint32_t *pubkey_size_x, uint32_t *pubkey_size_y, unsigned char **ecdh_aes_key,
  132. unsigned char **iv, unsigned char **tag){
  133. //Generate a new key
  134. EC_KEY *ephemeral_key = NULL;
  135. BIGNUM *x, *y;
  136. x = BN_new();
  137. y = BN_new();
  138. BN_CTX *bn_ctx = BN_CTX_new();
  139. const EC_GROUP *curve = NULL;
  140. if(NULL == (curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)))
  141. fprintf(stderr, "ZT_LSORAMServer: Setting EC_GROUP failed \n");
  142. ephemeral_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  143. if(ephemeral_key==NULL)
  144. fprintf(stderr, "ZT_LSORAMServer: EC_KEY_new_by_curve_name Fail\n");
  145. int ret = EC_KEY_generate_key(ephemeral_key);
  146. if(ret!=1)
  147. fprintf(stderr, "ZT_LSORAMServer: EC_KEY_generate_key Fail\n");
  148. const EC_POINT *pub_point;
  149. pub_point = EC_KEY_get0_public_key((const EC_KEY *) ephemeral_key);
  150. if(pub_point == NULL)
  151. fprintf(stderr, "ZT_LSORAMServer: EC_KEY_get0_public_key Fail\n");
  152. ret = EC_POINT_get_affine_coordinates_GFp(curve, pub_point, x, y, bn_ctx);
  153. if(ret==0)
  154. fprintf(stderr, "ZT_LSORAMServer: EC_POINT_get_affine_coordinates_GFp Failed \n");
  155. unsigned char *bin_x, *bin_y;
  156. uint32_t size_bin_x = BN_num_bytes(x);
  157. uint32_t size_bin_y = BN_num_bytes(y);
  158. //fprintf(stderr, "ZT_LSORAMServer: (%d, %d)\n", size_bin_x, size_bin_y);
  159. bin_x = (unsigned char*) malloc(PRIME256V1_KEY_SIZE);
  160. bin_y = (unsigned char*) malloc(PRIME256V1_KEY_SIZE);
  161. BN_bn2bin(x, bin_x);
  162. BN_bn2bin(y, bin_y);
  163. *pubkey_size_x = size_bin_x;
  164. *pubkey_size_y = size_bin_y;
  165. *client_pubkey = (unsigned char*) malloc(size_bin_x + size_bin_y);
  166. memcpy(*client_pubkey, bin_x, size_bin_x);
  167. memcpy(*client_pubkey + size_bin_x, bin_y, size_bin_y);
  168. /*
  169. unsigned char *ptr = *client_pubkey;
  170. printf("Serialized Client's Public Key in encryptLSORAM :\n");
  171. for(int t = 0; t < size_bin_x; t++)
  172. printf("%02X", ptr[t]);
  173. printf("\n");
  174. printf("Serialized Client's Public Key in encryptLSORAM :\n");
  175. for(int t = 0; t < size_bin_y; t++)
  176. printf("%02X", ptr[size_bin_x + t]);
  177. printf("\n");
  178. */
  179. //fprintf(stderr, "ZT_LSORAMServer: Before ECDH_compute_key \n");
  180. uint32_t field_size = EC_GROUP_get_degree(EC_KEY_get0_group(target_public_key));
  181. uint32_t secret_len = (field_size+7)/8;
  182. unsigned char *secret = (unsigned char*) malloc(secret_len);
  183. //Returns a 32 byte secret
  184. secret_len = ECDH_compute_key(secret, secret_len, EC_KEY_get0_public_key(target_public_key),
  185. ephemeral_key, NULL);
  186. //fprintf(stderr, "ZT_LSORAMServer: Finished ECDH_compute_key \n");
  187. //Sample IV;
  188. *ecdh_aes_key = (unsigned char*) malloc (KEY_LENGTH);
  189. *iv = (unsigned char*) malloc (IV_LENGTH);
  190. memcpy(*ecdh_aes_key, secret, KEY_LENGTH);
  191. memcpy(*iv, secret + KEY_LENGTH, IV_LENGTH);
  192. /*
  193. unsigned char *ecdh_ptr = (unsigned char *) *ecdh_aes_key;
  194. unsigned char *iv_ptr = (unsigned char *) *iv;
  195. printf("KEY_LENGTH = %d\n", KEY_LENGTH);
  196. printf("ecdh_key computed by Client :\n");
  197. for(int t = 0; t < KEY_LENGTH; t++)
  198. printf("%02X", ecdh_ptr[t]);
  199. printf("\n");
  200. printf("iv computed by Client :\n");
  201. for(int t = 0; t < IV_LENGTH; t++)
  202. printf("%02X", iv_ptr[t]);
  203. printf("\n");
  204. */
  205. BN_CTX_free(bn_ctx);
  206. *encrypted_request = (unsigned char*) malloc (request_size);
  207. *tag = (unsigned char*) malloc (TAG_SIZE);
  208. uint32_t encrypted_request_size;
  209. /*
  210. printf("Request bytes before encrypting: \n");
  211. for(int t = 0; t < request_size; t++)
  212. printf("%02X", serialized_request[t]);
  213. printf("\n");
  214. */
  215. //fprintf(stderr, "ZT_LSORAMServer: Before AES_GCM_128_encrypt call \n");
  216. encrypted_request_size = AES_GCM_128_encrypt(serialized_request, request_size,
  217. NULL, 0, (unsigned char*) *ecdh_aes_key, (unsigned char*) *iv,
  218. IV_LENGTH, *encrypted_request, *tag);
  219. /*
  220. unsigned char*tag_ptr = *tag;
  221. printf("Tag bytes after encryption: \n");
  222. for(uint32_t t = 0; t < TAG_SIZE; t++)
  223. printf("%02X", tag_ptr[t]);
  224. printf("\n");
  225. printf("Request_size = %d, Encrypted_request_size = %d,\n", request_size, encrypted_request_size);
  226. printf("Request bytes after encrypting: \n");
  227. unsigned char *encrypted_ptr = (unsigned char*) *encrypted_request;
  228. for(uint32_t t = 0; t < encrypted_request_size; t++)
  229. printf("%02X", encrypted_ptr[t]);
  230. printf("\n");
  231. */
  232. return encrypted_request_size;
  233. }
  234. int ZT_LSORAMServer::LSORAM_Insert(uint32_t instance_id, unsigned char *key, uint32_t key_size, unsigned char* value, uint32_t value_size){
  235. unsigned char *serialized_request, *encrypted_request, *tag_in;
  236. unsigned char *client_pubkey, *ecdh_aes_key, *iv;
  237. uint32_t pubkey_size_x, pubkey_size_y;
  238. uint32_t request_size = serializeLSORAMRequest(key, key_size, value, value_size, &serialized_request);
  239. //fprintf(stderr, "ZT_LSORAMServer: Before encryptLSORAMRequest\n");
  240. encryptLSORAMRequest(ENCLAVE_PUBLIC_KEY, serialized_request, request_size,
  241. &encrypted_request, &client_pubkey, &pubkey_size_x, &pubkey_size_y,
  242. &ecdh_aes_key, &iv, &tag_in);
  243. //fprintf(stderr, "ZT_LSORAMServer: After encryptLSORAMRequest\n");
  244. /*
  245. printf("Clientpubkey going into ZT_LSORAM_insert:\n");
  246. printf("X: :\n");
  247. for(int t = 0; t < 32; t++)
  248. printf("%02X", client_pubkey[t]);
  249. printf("\n");
  250. printf("Y :\n");
  251. for(int t = 0; t < 32; t++)
  252. printf("%02X", client_pubkey[32+t]);
  253. printf("\n");
  254. */
  255. //fprintf(stderr, "ZT_LSORAMServer: Before ZT_LSORAM_insert\n");
  256. ZT_LSORAM_insert(instance_id, encrypted_request, request_size,
  257. tag_in, TAG_SIZE, client_pubkey, pubkey_size_x, pubkey_size_y);
  258. //fprintf(stderr, "ZT_LSORAMServer: After ZT_LSORAM_insert\n");
  259. free(serialized_request);
  260. return 1;
  261. }
  262. void ZT_LSORAMServer::store(const string &key, const string &value){
  263. if (value.length() > 0) {
  264. //Create encrypted request with strings key, value
  265. unsigned char *key_ptr = (unsigned char*) key.c_str();
  266. unsigned char *value_ptr = (unsigned char*) value.c_str();
  267. //fprintf(stderr, "ZT_LSORAMServer: Starting store(), key.length= %ld, value.length = %ld\n", key.length(), value.length());
  268. LSORAM_Insert(ZT_instance_id, (unsigned char*) key.c_str(), BLINDED_KEY_SIZE,
  269. (unsigned char*) value.c_str(), DESCRIPTOR_MAX_SIZE);
  270. fprintf(stderr, "ZT_LSORAMServer: STORED HSDesc Key = ");
  271. for(uint32_t i = 0; i <32; i++){
  272. fprintf(stderr, "%02x", key_ptr[i]);
  273. }
  274. fprintf(stderr,"\n");
  275. fprintf(stderr, "ZT_LSORAMServer: STORED (First 32 bytes of ) HSDesc Value= ");
  276. for(uint64_t i = 0; i < 32; i++){
  277. fprintf(stderr, "%02x", value_ptr[i]);
  278. }
  279. fprintf(stderr,"\n");
  280. } else {
  281. //int8_t ZT_LSORAM_evict(uint32_t id, unsigned char *key, uint32_t key_size);
  282. }
  283. }
  284. /*
  285. In ZT_LSORAMClient lookup_query should be:
  286. encrypted_query||tag_in||pk_x_size||pk_y_size||client_pubkey
  287. where client_pubkey is of size pk_x_size+pk_y_size
  288. returns lookup_response:
  289. encrypted_response||tag_out
  290. */
  291. bool ZT_LSORAMServer::lookup(const string &lookup_query, string &lookup_response) {
  292. fprintf(stderr, "ZT_LSORAMServer: Starting lookup() call\n");
  293. const char *lookup_query_cstr= lookup_query.c_str();
  294. fprintf(stderr, "ZT_LSORAMServer: lookup_query length = %ld\n", lookup_query.length());
  295. unsigned char *encrypted_query = (unsigned char*) malloc (BLINDED_KEY_SIZE);
  296. unsigned char *tag_in = (unsigned char*) malloc (TAG_SIZE);
  297. uint32_t pk_x_size;
  298. uint32_t pk_y_size;
  299. unsigned char *client_pubkey;
  300. fprintf(stderr, "ZT_LSORAMServer: Before parsing lookup_query_cstr\n");
  301. unsigned char *ptr = (unsigned char*) lookup_query_cstr;
  302. memcpy(encrypted_query, ptr, BLINDED_KEY_SIZE);
  303. ptr+=BLINDED_KEY_SIZE;
  304. memcpy(tag_in, ptr, TAG_SIZE);
  305. ptr+=TAG_SIZE;
  306. memcpy(&pk_x_size, ptr, sizeof(uint32_t));
  307. ptr+=sizeof(uint32_t);
  308. memcpy(&pk_y_size, ptr, sizeof(uint32_t));
  309. ptr+=sizeof(uint32_t);
  310. size_t expected_size = BLINDED_KEY_SIZE+TAG_SIZE+ 2*sizeof(uint32_t) +
  311. pk_x_size + pk_y_size;
  312. fprintf(stderr, "ZT_LSORAMServer: Before allocating client_pubkey call (%d,%d)\n", pk_x_size, pk_y_size);
  313. fprintf(stderr, "ZT_LSORAMServer: expected_size = %ld, lookup_query.length = %ld\n", expected_size, lookup_query.length());
  314. /*
  315. if(lookup_query.length()!=expected_size)
  316. fprintf(stderr, "ZT_LSORAMServer: Query size doesn't match KEY_SIZE + TAG_SIZE\n");
  317. return 0;
  318. */
  319. client_pubkey = (unsigned char*) malloc (pk_x_size+pk_y_size);
  320. memcpy(client_pubkey, ptr, (pk_x_size + pk_y_size));
  321. unsigned char *encrypted_response = (unsigned char*) malloc(DESCRIPTOR_MAX_SIZE);
  322. unsigned char *tag_out = (unsigned char*) malloc(TAG_SIZE);
  323. fprintf(stderr, "ZT_LSORAMServer: Before ZT_LSORAM_fetch() call\n");
  324. ZT_LSORAM_fetch(ZT_instance_id, encrypted_query, BLINDED_KEY_SIZE,
  325. encrypted_response, DESCRIPTOR_MAX_SIZE, tag_in, tag_out, TAG_SIZE,
  326. client_pubkey, pk_x_size, pk_y_size);
  327. fprintf(stderr, "ZT_LSORAMServer: After ZT_LSORAM_fetch() call\n");
  328. lookup_response.assign((const char*) encrypted_response, DESCRIPTOR_MAX_SIZE);
  329. lookup_response.append((const char*) tag_out, TAG_SIZE);
  330. free(encrypted_response);
  331. free(tag_out);
  332. free(client_pubkey);
  333. free(encrypted_query);
  334. free(tag_in);
  335. fprintf(stderr, "ZT_LSORAMServer: Finished lookup() call\n");
  336. return 1;
  337. }
  338. int main(int argc, char **argv) {
  339. ZT_LSORAMServer server;
  340. server.mainloop();
  341. return 0;
  342. }