storage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "utils.hpp"
  2. #include "config.hpp"
  3. #include "ORExpand.hpp"
  4. #include "sort.hpp"
  5. #include "storage.hpp"
  6. #include "client.hpp"
  7. #define PROFILE_STORAGE
  8. StgClient *clients;
  9. uint8_t *epoch_tokens;
  10. uint8_t *epoch_mailboxes;
  11. static struct {
  12. uint32_t max_users;
  13. uint32_t my_storage_node_id;
  14. // A local storage buffer, used when we need to do non-in-place
  15. // sorts of the messages that have arrived
  16. MsgBuffer stg_buf;
  17. // The destination vector for ORExpand
  18. std::vector<uint32_t> dest;
  19. } storage_state;
  20. bool storage_generateClientKeys(uint32_t num_clients, uint32_t my_stg_no) {
  21. uint16_t num_priv_channels = g_teems_config.m_priv_in;
  22. uint16_t msg_size = g_teems_config.msg_size;
  23. uint32_t pt_msgbundle_size = num_priv_channels * msg_size;
  24. clients = new StgClient[num_clients];
  25. for(uint32_t i =0; i < num_clients; i++) {
  26. uint32_t mid = storage_state.my_storage_node_id + i;
  27. clients[i].my_id = mid;
  28. clients[i].priv_friends = new clientid_t[g_teems_config.m_priv_out];
  29. // Initialize this client's private channel friends as themself
  30. for(int j =0; j <g_teems_config.m_priv_out; j++) {
  31. (clients[i].priv_friends)[j] = mid;
  32. }
  33. }
  34. uint32_t num_stg_nodes = g_teems_config.num_storage_nodes;
  35. uint32_t c_simid = my_stg_no;
  36. for (uint32_t i=0; i<num_clients; i++) {
  37. const sgx_aes_gcm_128bit_key_t *pESK = &(g_teems_config.ESK);
  38. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  39. unsigned char iv[SGX_AESGCM_IV_SIZE];
  40. sgx_aes_gcm_128bit_tag_t tag;
  41. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  42. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  43. memcpy(iv, (uint8_t*) (&c_simid), sizeof(c_simid));
  44. memcpy(iv + sizeof(c_simid), "STG", sizeof("STG"));
  45. sgx_status_t ret = SGX_SUCCESS;
  46. ret = sgx_rijndael128GCM_encrypt(pESK, zeroes, SGX_AESGCM_KEY_SIZE,
  47. (uint8_t*) (clients[i].key), iv, SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  48. if(ret!=SGX_SUCCESS) {
  49. printf("stg_generateClientKeys FAIL\n");
  50. return false;
  51. }
  52. /*
  53. if(c_simid % 10 == 0) {
  54. printf("Storage: c_simid = %d, Key:", c_simid);
  55. for (int k = 0; k<SGX_AESGCM_KEY_SIZE; k++) {
  56. printf("%x", (clients[i].key)[k]);
  57. }
  58. printf("\n");
  59. }
  60. */
  61. c_simid+=num_stg_nodes;
  62. }
  63. return true;
  64. }
  65. bool generate_all_tokens()
  66. {
  67. uint32_t pt_tokens_size = (g_teems_config.m_priv_out * SGX_CMAC_MAC_SIZE);
  68. uint32_t enc_tokens_size = pt_tokens_size +
  69. SGX_AESGCM_IV_SIZE + SGX_AESGCM_MAC_SIZE;
  70. unsigned char token_body[pt_tokens_size];
  71. const sgx_aes_gcm_128bit_key_t *pTSK = &(g_teems_config.TSK);
  72. for(uint32_t lcid=0; lcid<storage_state.max_users; lcid++) {
  73. unsigned char *tkn_iv_ptr = epoch_tokens + enc_tokens_size * lcid;
  74. unsigned char *tkn_ptr = tkn_iv_ptr + SGX_AESGCM_IV_SIZE;
  75. unsigned char *tkn_tag = tkn_ptr + pt_tokens_size;
  76. // We construct the plaintext [S|R|Epoch] underlying the token in
  77. // the correct location for this client in the epoch_tokens buffer
  78. // The tokens ( i.e., CMAC over S|R|Epoch) is stored in token_body
  79. // Then encrypt token_body with the client's storage key and overwrite
  80. // the correct location in epoch_tokens
  81. memset(token_body, 0, pt_tokens_size);
  82. memset(tkn_iv_ptr, 0, SGX_AESGCM_IV_SIZE);
  83. // IV = epoch_no, for encrypting the token bundle
  84. // epoch_no used is for the next epoch
  85. unsigned long epoch_val = storage_epoch + 1;
  86. memcpy(tkn_iv_ptr, &epoch_val, sizeof(epoch_val));
  87. sgx_status_t ret = SGX_SUCCESS;
  88. unsigned char *ptr = tkn_ptr;
  89. unsigned char *tkn_body_ptr = token_body;
  90. for(int i = 0; i<g_teems_config.m_priv_out; i++)
  91. {
  92. memcpy(ptr, (&(clients[lcid].my_id)), sizeof(clientid_t));
  93. memcpy(ptr + sizeof(clientid_t), (&(clients[lcid].priv_friends[i])), sizeof(clientid_t));
  94. memcpy(ptr + 2 * sizeof(clientid_t), &epoch_val, sizeof(epoch_val));
  95. ret = sgx_rijndael128_cmac_msg(pTSK, ptr, pt_tokens_size,
  96. (sgx_cmac_128bit_tag_t*) tkn_body_ptr);
  97. if(ret!=SGX_SUCCESS) {
  98. printf("generate_tokens: Creating token FAIL\n");
  99. return false;
  100. }
  101. ptr+=SGX_CMAC_MAC_SIZE;
  102. tkn_body_ptr+=SGX_CMAC_MAC_SIZE;
  103. }
  104. /*
  105. if(lcid == 0) {
  106. printf("Checking generated token_body:");
  107. for(uint32_t i = 0; i < pt_tokens_size; i++) {
  108. printf("%x", token_body[i]);
  109. }
  110. printf("\n");
  111. }
  112. */
  113. unsigned char *cl_iv = clients[lcid].iv;
  114. ret = (sgx_rijndael128GCM_encrypt(&(clients[lcid].key), token_body, pt_tokens_size,
  115. (uint8_t*) tkn_ptr, cl_iv, SGX_AESGCM_IV_SIZE, NULL, 0,
  116. (sgx_aes_gcm_128bit_tag_t*) tkn_tag));
  117. if(ret!=SGX_SUCCESS) {
  118. printf("generate_tokens: Encrypting token FAIL\n");
  119. return false;
  120. }
  121. memcpy(tkn_iv_ptr, cl_iv, SGX_AESGCM_IV_SIZE);
  122. // Update IV
  123. uint64_t *iv_ctr = (uint64_t*) cl_iv;
  124. (*iv_ctr)+=1;
  125. /*
  126. if(lcid == 0) {
  127. printf("Encrypted client token bundle:");
  128. for(uint32_t i = 0; i < enc_tokens_size; i++) {
  129. printf("%x", tkn_iv_ptr[i]);
  130. }
  131. printf("\n");
  132. }
  133. */
  134. }
  135. return true;
  136. }
  137. /* processMsgs
  138. - Take all the messages in storage_state.stg_buf
  139. - Encrypt them all with their corresponding client key and IV and store into
  140. epoch_mailboxes
  141. */
  142. bool processMsgs() {
  143. unsigned char *epoch_buf_ptr = epoch_mailboxes;
  144. unsigned char *stg_buf_ptr = storage_state.stg_buf.buf;
  145. uint32_t mailbox_size = g_teems_config.m_priv_in * g_teems_config.msg_size;
  146. uint32_t enc_mailbox_size = mailbox_size + SGX_AESGCM_IV_SIZE + SGX_AESGCM_MAC_SIZE;
  147. sgx_status_t ret = SGX_SUCCESS;
  148. unsigned char *epoch_buf_ct_ptr = epoch_buf_ptr + SGX_AESGCM_IV_SIZE;
  149. unsigned char *epoch_buf_tag_ptr = epoch_buf_ct_ptr + mailbox_size;
  150. for(uint32_t lcid = 0; lcid <storage_state.max_users; lcid++) {
  151. memcpy(epoch_buf_ptr, clients[lcid].iv, SGX_AESGCM_IV_SIZE);
  152. ret = sgx_rijndael128GCM_encrypt(&(clients[lcid].key), stg_buf_ptr, mailbox_size,
  153. (uint8_t*) epoch_buf_ct_ptr, epoch_buf_ptr, SGX_AESGCM_IV_SIZE, NULL, 0,
  154. (sgx_aes_gcm_128bit_tag_t*) epoch_buf_tag_ptr);
  155. if(ret!=SGX_SUCCESS) {
  156. printf("processMsgs: Encrypting msgs FAIL\n");
  157. return false;
  158. }
  159. // Update IV
  160. uint64_t *iv_ctr = (uint64_t*) clients[lcid].iv;
  161. (*iv_ctr)+=1;
  162. /*
  163. if(lcid==0) {
  164. printf("\n\nMessage for lcid 0, S, R = %d, %d\n\n\n", *((uint32_t*) stg_buf_ptr),
  165. *((uint32_t*) (stg_buf_ptr + 4)));
  166. }
  167. */
  168. stg_buf_ptr+=mailbox_size;
  169. epoch_buf_ptr+=enc_mailbox_size;
  170. epoch_buf_ct_ptr+=enc_mailbox_size;
  171. epoch_buf_tag_ptr+=enc_mailbox_size;
  172. }
  173. return true;
  174. }
  175. // route_init will call this function; no one else should call it
  176. // explicitly. The parameter is the number of messages that can fit in
  177. // the storage-side MsgBuffer. Returns true on success, false on
  178. // failure.
  179. bool storage_init(uint32_t max_users, uint32_t msg_buf_size)
  180. {
  181. storage_state.max_users = max_users;
  182. storage_state.stg_buf.alloc(msg_buf_size);
  183. storage_state.dest.resize(msg_buf_size);
  184. uint32_t my_storage_node_id = 0;
  185. uint32_t my_stg_pos = 0;
  186. for (nodenum_t i=0; i<g_teems_config.num_nodes; ++i) {
  187. if (g_teems_config.roles[i] & ROLE_STORAGE) {
  188. if (i == g_teems_config.my_node_num) {
  189. storage_state.my_storage_node_id = my_storage_node_id << DEST_UID_BITS;
  190. my_stg_pos = my_storage_node_id;
  191. } else {
  192. ++my_storage_node_id;
  193. }
  194. }
  195. }
  196. storage_generateClientKeys(max_users, my_stg_pos);
  197. return true;
  198. }
  199. // Handle the messages received by a storage node. Pass a _locked_
  200. // MsgBuffer. This function will itself reset and unlock it when it's
  201. // done with it.
  202. void storage_received(MsgBuffer &storage_buf)
  203. {
  204. uint16_t msg_size = g_teems_config.msg_size;
  205. nodenum_t my_node_num = g_teems_config.my_node_num;
  206. const uint8_t *msgs = storage_buf.buf;
  207. uint32_t num_msgs = storage_buf.inserted;
  208. uint32_t real = 0, padding = 0;
  209. uint32_t uid_mask = (1 << DEST_UID_BITS) - 1;
  210. uint32_t nid_mask = ~uid_mask;
  211. #ifdef PROFILE_STORAGE
  212. unsigned long start_received = printf_with_rtclock("begin storage_received (%u)\n", storage_buf.inserted);
  213. #endif
  214. // It's OK to test for errors in a way that's non-oblivous if
  215. // there's an error (but it should be oblivious if there are no
  216. // errors)
  217. for (uint32_t i=0; i<num_msgs; ++i) {
  218. uint32_t uid = *(const uint32_t*)(storage_buf.buf+(i*msg_size));
  219. bool ok = ((((uid & nid_mask) == storage_state.my_storage_node_id)
  220. & ((uid & uid_mask) < storage_state.max_users))
  221. | ((uid & uid_mask) == uid_mask));
  222. if (!ok) {
  223. printf("Received bad uid: %08x\n", uid);
  224. assert(ok);
  225. }
  226. }
  227. // Testing: report how many real and dummy messages arrived
  228. printf("Storage server received %u messages:\n", num_msgs);
  229. for (uint32_t i=0; i<num_msgs; ++i) {
  230. uint32_t dest_addr = *(const uint32_t*)msgs;
  231. nodenum_t dest_node =
  232. g_teems_config.storage_map[dest_addr >> DEST_UID_BITS];
  233. if (dest_node != my_node_num) {
  234. char hexbuf[2*msg_size + 1];
  235. for (uint32_t j=0;j<msg_size;++j) {
  236. snprintf(hexbuf+2*j, 3, "%02x", msgs[j]);
  237. }
  238. printf("Misrouted message: %s\n", hexbuf);
  239. } else if ((dest_addr & uid_mask) == uid_mask) {
  240. ++padding;
  241. } else {
  242. ++real;
  243. }
  244. msgs += msg_size;
  245. }
  246. printf("%u real, %u padding\n", real, padding);
  247. /*
  248. for (uint32_t i=0;i<num_msgs; ++i) {
  249. printf("%3d: %08x %08x\n", i,
  250. *(uint32_t*)(storage_buf.buf+(i*msg_size)),
  251. *(uint32_t*)(storage_buf.buf+(i*msg_size+4)));
  252. }
  253. */
  254. // Sort the received messages by userid into the
  255. // storage_state.stg_buf MsgBuffer.
  256. #ifdef PROFILE_STORAGE
  257. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u)\n", storage_buf.inserted);
  258. #endif
  259. sort_mtobliv<UidKey>(g_teems_config.nthreads, storage_buf.buf,
  260. msg_size, storage_buf.inserted, storage_buf.bufsize,
  261. storage_state.stg_buf.buf);
  262. #ifdef PROFILE_STORAGE
  263. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u)\n", storage_buf.inserted);
  264. #endif
  265. /*
  266. for (uint32_t i=0;i<num_msgs; ++i) {
  267. printf("%3d: %08x %08x\n", i,
  268. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  269. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)));
  270. }
  271. */
  272. #ifdef PROFILE_STORAGE
  273. unsigned long start_dest = printf_with_rtclock("begin setting dests (%u)\n", storage_state.stg_buf.bufsize);
  274. #endif
  275. // Obliviously set the dest array
  276. uint32_t *dests = storage_state.dest.data();
  277. uint32_t stg_size = storage_state.stg_buf.bufsize;
  278. const uint8_t *buf = storage_state.stg_buf.buf;
  279. uint32_t m_priv_in = g_teems_config.m_priv_in;
  280. uint32_t uid = *(uint32_t*)(buf);
  281. uid &= uid_mask;
  282. // num_msgs is not a private value
  283. if (num_msgs > 0) {
  284. dests[0] = oselect_uint32_t(uid * m_priv_in, 0xffffffff,
  285. uid == uid_mask);
  286. }
  287. uint32_t prev_uid = uid;
  288. for (uint32_t i=1; i<num_msgs; ++i) {
  289. uid = *(uint32_t*)(buf + i*msg_size);
  290. uid &= uid_mask;
  291. uint32_t next = oselect_uint32_t(uid * m_priv_in, dests[i-1]+1,
  292. uid == prev_uid);
  293. dests[i] = oselect_uint32_t(next, 0xffffffff, uid == uid_mask);
  294. prev_uid = uid;
  295. }
  296. for (uint32_t i=num_msgs; i<stg_size; ++i) {
  297. dests[i] = 0xffffffff;
  298. *(uint32_t*)(buf + i*msg_size) = 0xffffffff;
  299. }
  300. #ifdef PROFILE_STORAGE
  301. printf_with_rtclock_diff(start_dest, "end setting dests (%u)\n", stg_size);
  302. #endif
  303. /*
  304. for (uint32_t i=0;i<stg_size; ++i) {
  305. printf("%3d: %08x %08x %u\n", i,
  306. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  307. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)),
  308. dests[i]);
  309. }
  310. */
  311. #ifdef PROFILE_STORAGE
  312. unsigned long start_expand = printf_with_rtclock("begin ORExpand (%u)\n", stg_size);
  313. #endif
  314. ORExpand_parallel<OSWAP_16X>(storage_state.stg_buf.buf, dests,
  315. msg_size, stg_size, g_teems_config.nthreads);
  316. #ifdef PROFILE_STORAGE
  317. printf_with_rtclock_diff(start_expand, "end ORExpand (%u)\n", stg_size);
  318. #endif
  319. /*
  320. for (uint32_t i=0;i<stg_size; ++i) {
  321. printf("%3d: %08x %08x %u\n", i,
  322. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  323. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)),
  324. dests[i]);
  325. }
  326. */
  327. // You can do more processing after these lines, as long as they
  328. // don't touch storage_buf. They _can_ touch the backing buffer
  329. // storage_state.stg_buf.
  330. storage_buf.reset();
  331. pthread_mutex_unlock(&storage_buf.mutex);
  332. bool ret = generate_all_tokens();
  333. uint32_t num_expected_msgs = g_teems_config.m_priv_in * storage_state.max_users;
  334. processMsgs();
  335. storage_epoch++;
  336. storage_state.stg_buf.reset();
  337. #ifdef PROFILE_STORAGE
  338. printf_with_rtclock_diff(start_received, "end storage_received (%u)\n", storage_buf.inserted);
  339. #endif
  340. }
  341. bool ecall_storage_authenticate(clientid_t cid, unsigned char *auth_message)
  342. {
  343. printf("In ecall_storage_authenticate!\n");
  344. bool ret = false;
  345. uint32_t lcid = cid / g_teems_config.num_storage_nodes;
  346. const sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  347. printf("In Enc/Stg::auth: invoked on cid = %d, lcid = %d\n", cid, lcid);
  348. ret = authenticateClient(auth_message, ckey);
  349. printf("After authenticateClient, ret = %d\n", ret);
  350. if(!ret) {
  351. printf("Storage authentication FAIL\n");
  352. }
  353. return ret;
  354. }
  355. void ecall_supply_storage_buffers(unsigned char *mailboxes,
  356. uint32_t mailboxes_size, unsigned char *tokens, uint32_t tokens_size)
  357. {
  358. epoch_mailboxes = mailboxes;
  359. epoch_tokens = tokens;
  360. }