storage.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. static std::vector<StgClient> clients;
  9. static uint8_t *epoch_tokens;
  10. static 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. // The selected array for compaction during public routing
  20. // Need a bool array for compaction, and std:vector<bool> lacks .data()
  21. bool *pub_selected;
  22. } storage_state;
  23. static bool storage_generateClientKeys(uint32_t num_clients,
  24. uint32_t my_stg_no) {
  25. clients.resize(num_clients);
  26. for(uint32_t i =0; i < num_clients; i++) {
  27. uint32_t mid = storage_state.my_storage_node_id + i;
  28. clients[i].my_id = mid;
  29. clients[i].priv_friends.resize(g_teems_config.m_priv_out);
  30. // Initialize this client's private channel friends as themself
  31. for(int j = 0; j < g_teems_config.m_priv_out; j++) {
  32. (clients[i].priv_friends)[j] = mid;
  33. }
  34. }
  35. uint32_t num_stg_nodes = g_teems_config.num_storage_nodes;
  36. uint32_t c_simid = my_stg_no;
  37. for (uint32_t i=0; i<num_clients; i++) {
  38. const sgx_aes_gcm_128bit_key_t *pESK = &(g_teems_config.ESK);
  39. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  40. unsigned char iv[SGX_AESGCM_IV_SIZE];
  41. sgx_aes_gcm_128bit_tag_t tag;
  42. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  43. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  44. memcpy(iv, (uint8_t*) (&c_simid), sizeof(c_simid));
  45. memcpy(iv + sizeof(c_simid), "STG", sizeof("STG"));
  46. sgx_status_t ret = SGX_SUCCESS;
  47. ret = sgx_rijndael128GCM_encrypt(pESK, zeroes, SGX_AESGCM_KEY_SIZE,
  48. (uint8_t*) (clients[i].key), iv, SGX_AESGCM_IV_SIZE,
  49. NULL, 0, &tag);
  50. if(ret!=SGX_SUCCESS) {
  51. printf("stg_generateClientKeys FAIL\n");
  52. return false;
  53. }
  54. /*
  55. if(c_simid % 10 == 0) {
  56. printf("Storage: c_simid = %d, Key:", c_simid);
  57. for (int k = 0; k<SGX_AESGCM_KEY_SIZE; k++) {
  58. printf("%x", (clients[i].key)[k]);
  59. }
  60. printf("\n");
  61. }
  62. */
  63. c_simid+=num_stg_nodes;
  64. }
  65. return true;
  66. }
  67. struct UserRange {
  68. uint32_t start, num;
  69. bool ret;
  70. };
  71. static void* generate_all_tokens_launch(void *voidargs)
  72. {
  73. UserRange *args = (UserRange *)voidargs;
  74. uint32_t pt_tokens_size = (g_teems_config.m_priv_out * SGX_CMAC_MAC_SIZE);
  75. uint32_t enc_tokens_size = pt_tokens_size +
  76. SGX_AESGCM_IV_SIZE + SGX_AESGCM_MAC_SIZE;
  77. unsigned char token_body[pt_tokens_size];
  78. uint32_t user_start = args->start;
  79. uint32_t user_end = args->start + args->num;
  80. const sgx_aes_gcm_128bit_key_t *pTSK = &(g_teems_config.TSK);
  81. for(uint32_t lcid=user_start; lcid < user_end; lcid++) {
  82. unsigned char *tkn_iv_ptr = epoch_tokens + enc_tokens_size * lcid;
  83. unsigned char *tkn_ptr = tkn_iv_ptr + SGX_AESGCM_IV_SIZE;
  84. unsigned char *tkn_tag = tkn_ptr + pt_tokens_size;
  85. // We construct the plaintext [S|R|Epoch] underlying the token in
  86. // the correct location for this client in the epoch_tokens buffer
  87. // The tokens ( i.e., CMAC over S|R|Epoch) is stored in token_body
  88. // Then encrypt token_body with the client's storage key and overwrite
  89. // the correct location in epoch_tokens
  90. memset(token_body, 0, pt_tokens_size);
  91. memset(tkn_iv_ptr, 0, SGX_AESGCM_IV_SIZE);
  92. // IV = epoch_no, for encrypting the token bundle
  93. // epoch_no used is for the next epoch
  94. unsigned long epoch_val = storage_epoch + 1;
  95. memcpy(tkn_iv_ptr, &epoch_val, sizeof(epoch_val));
  96. sgx_status_t ret = SGX_SUCCESS;
  97. unsigned char *ptr = tkn_ptr;
  98. unsigned char *tkn_body_ptr = token_body;
  99. for(int i = 0; i<g_teems_config.m_priv_out; i++)
  100. {
  101. memcpy(ptr, (&(clients[lcid].my_id)), sizeof(clientid_t));
  102. memcpy(ptr + sizeof(clientid_t),
  103. (&(clients[lcid].priv_friends[i])), sizeof(clientid_t));
  104. memcpy(ptr + 2 * sizeof(clientid_t),
  105. &epoch_val, sizeof(epoch_val));
  106. ret = sgx_rijndael128_cmac_msg(pTSK, ptr, pt_tokens_size,
  107. (sgx_cmac_128bit_tag_t*) tkn_body_ptr);
  108. if(ret!=SGX_SUCCESS) {
  109. printf("generate_tokens: Creating token FAIL\n");
  110. args->ret = false;
  111. return NULL;
  112. }
  113. ptr+=SGX_CMAC_MAC_SIZE;
  114. tkn_body_ptr+=SGX_CMAC_MAC_SIZE;
  115. }
  116. /*
  117. if(lcid == 0) {
  118. printf("Checking generated token_body:");
  119. for(uint32_t i = 0; i < pt_tokens_size; i++) {
  120. printf("%x", token_body[i]);
  121. }
  122. printf("\n");
  123. }
  124. */
  125. unsigned char *cl_iv = clients[lcid].iv;
  126. ret = (sgx_rijndael128GCM_encrypt(&(clients[lcid].key),
  127. token_body, pt_tokens_size,
  128. (uint8_t*) tkn_ptr, cl_iv, SGX_AESGCM_IV_SIZE, NULL, 0,
  129. (sgx_aes_gcm_128bit_tag_t*) tkn_tag));
  130. if(ret!=SGX_SUCCESS) {
  131. printf("generate_tokens: Encrypting token FAIL\n");
  132. args->ret = false;
  133. return NULL;
  134. }
  135. memcpy(tkn_iv_ptr, cl_iv, SGX_AESGCM_IV_SIZE);
  136. // Update IV
  137. uint64_t *iv_ctr = (uint64_t*) cl_iv;
  138. (*iv_ctr)+=1;
  139. /*
  140. if(lcid == 0) {
  141. printf("Encrypted client token bundle:");
  142. for(uint32_t i = 0; i < enc_tokens_size; i++) {
  143. printf("%x", tkn_iv_ptr[i]);
  144. }
  145. printf("\n");
  146. }
  147. */
  148. }
  149. args->ret = true;
  150. return NULL;
  151. }
  152. static bool launch_all_users(void *(*launch)(void *)) {
  153. threadid_t nthreads = g_teems_config.nthreads;
  154. // Special-case nthread=1 for efficiency
  155. if (nthreads <= 1) {
  156. UserRange args = { 0, storage_state.max_users, false };
  157. return launch(&args);
  158. }
  159. UserRange args[nthreads];
  160. uint32_t inc = storage_state.max_users / nthreads;
  161. uint32_t extra = storage_state.max_users % nthreads;
  162. uint32_t last = 0;
  163. for (threadid_t i=0; i<nthreads; ++i) {
  164. uint32_t num = inc + (i < extra);
  165. args[i] = { last, num, false };
  166. last += num;
  167. }
  168. // Launch all but the first section into other threads
  169. for (threadid_t i=1; i<nthreads; ++i) {
  170. threadpool_dispatch(g_thread_id+i, launch, args+i);
  171. }
  172. // Do the first section ourselves
  173. launch(args);
  174. // Join the threads
  175. for (threadid_t i=1; i<nthreads; ++i) {
  176. threadpool_join(g_thread_id+i, NULL);
  177. }
  178. bool ret = true;
  179. for (threadid_t i=0; i<nthreads; ++i) {
  180. ret &= args[i].ret;
  181. }
  182. return ret;
  183. }
  184. bool generate_all_tokens() {
  185. return launch_all_users(generate_all_tokens_launch);
  186. }
  187. /* processMsgs
  188. - Take all the messages in storage_state.stg_buf
  189. - Encrypt them all with their corresponding client key and IV and store into
  190. epoch_mailboxes
  191. */
  192. static void *processMsgs_launch(void *voidargs) {
  193. UserRange *args = (UserRange *)voidargs;
  194. uint32_t user_start = args->start;
  195. uint32_t user_end = args->start + args->num;
  196. uint32_t mailbox_size;
  197. if (g_teems_config.private_routing) {
  198. mailbox_size = g_teems_config.m_priv_in * g_teems_config.msg_size;
  199. } else {
  200. mailbox_size = g_teems_config.m_pub_in * g_teems_config.msg_size;
  201. }
  202. uint32_t enc_mailbox_size = mailbox_size + SGX_AESGCM_IV_SIZE +
  203. SGX_AESGCM_MAC_SIZE;
  204. unsigned char *epoch_buf_ptr = epoch_mailboxes +
  205. enc_mailbox_size * user_start;
  206. unsigned char *stg_buf_ptr = storage_state.stg_buf.buf +
  207. mailbox_size * user_start;
  208. sgx_status_t ret = SGX_SUCCESS;
  209. unsigned char *epoch_buf_ct_ptr = epoch_buf_ptr + SGX_AESGCM_IV_SIZE;
  210. unsigned char *epoch_buf_tag_ptr = epoch_buf_ct_ptr + mailbox_size;
  211. for(uint32_t lcid = user_start; lcid < user_end; lcid++) {
  212. memcpy(epoch_buf_ptr, clients[lcid].iv, SGX_AESGCM_IV_SIZE);
  213. ret = sgx_rijndael128GCM_encrypt(&(clients[lcid].key), stg_buf_ptr,
  214. mailbox_size, (uint8_t*) epoch_buf_ct_ptr, epoch_buf_ptr,
  215. SGX_AESGCM_IV_SIZE, NULL, 0,
  216. (sgx_aes_gcm_128bit_tag_t*) epoch_buf_tag_ptr);
  217. if(ret!=SGX_SUCCESS) {
  218. printf("processMsgs: Encrypting msgs FAIL\n");
  219. args->ret = false;
  220. return NULL;
  221. }
  222. // Update IV
  223. uint64_t *iv_ctr = (uint64_t*) clients[lcid].iv;
  224. (*iv_ctr)+=1;
  225. /*
  226. if(lcid==0) {
  227. printf("\n\nMessage for lcid 0, S, R = %d, %d\n\n\n", *((uint32_t*) stg_buf_ptr),
  228. *((uint32_t*) (stg_buf_ptr + 4)));
  229. }
  230. */
  231. stg_buf_ptr+=mailbox_size;
  232. epoch_buf_ptr+=enc_mailbox_size;
  233. epoch_buf_ct_ptr+=enc_mailbox_size;
  234. epoch_buf_tag_ptr+=enc_mailbox_size;
  235. }
  236. args->ret = true;
  237. return NULL;
  238. }
  239. bool processMsgs() {
  240. return launch_all_users(processMsgs_launch);
  241. }
  242. // route_init will call this function; no one else should call it
  243. // explicitly. The parameter is the number of messages that can fit in
  244. // the storage-side MsgBuffer. Returns true on success, false on
  245. // failure.
  246. bool storage_init(uint32_t max_users, uint32_t msg_buf_size)
  247. {
  248. storage_state.max_users = max_users;
  249. storage_state.stg_buf.alloc(msg_buf_size);
  250. storage_state.dest.resize(msg_buf_size);
  251. storage_state.pub_selected = new bool[msg_buf_size];
  252. uint32_t my_storage_node_id = 0;
  253. uint32_t my_stg_pos = 0;
  254. for (nodenum_t i=0; i<g_teems_config.num_nodes; ++i) {
  255. if (g_teems_config.roles[i] & ROLE_STORAGE) {
  256. if (i == g_teems_config.my_node_num) {
  257. storage_state.my_storage_node_id =
  258. my_storage_node_id << DEST_UID_BITS;
  259. my_stg_pos = my_storage_node_id;
  260. } else {
  261. ++my_storage_node_id;
  262. }
  263. }
  264. }
  265. storage_generateClientKeys(max_users, my_stg_pos);
  266. return true;
  267. }
  268. void storage_close() {
  269. delete[] storage_state.pub_selected;
  270. }
  271. // Handle the messages received by a storage node. Pass a _locked_
  272. // MsgBuffer. This function will itself reset and unlock it when it's
  273. // done with it.
  274. void storage_received(MsgBuffer &storage_buf)
  275. {
  276. uint16_t msg_size = g_teems_config.msg_size;
  277. nodenum_t my_node_num = g_teems_config.my_node_num;
  278. const uint8_t *msgs = storage_buf.buf;
  279. uint32_t num_msgs = storage_buf.inserted;
  280. uint32_t real = 0, padding = 0;
  281. uint32_t uid_mask = (1 << DEST_UID_BITS) - 1;
  282. uint32_t nid_mask = ~uid_mask;
  283. #ifdef PROFILE_STORAGE
  284. unsigned long start_received = printf_with_rtclock("begin storage_received (%u)\n", storage_buf.inserted);
  285. #endif
  286. // It's OK to test for errors in a way that's non-oblivous if
  287. // there's an error (but it should be oblivious if there are no
  288. // errors)
  289. for (uint32_t i=0; i<num_msgs; ++i) {
  290. uint32_t uid = *(const uint32_t*)(storage_buf.buf+(i*msg_size));
  291. bool ok = ((((uid & nid_mask) == storage_state.my_storage_node_id)
  292. & ((uid & uid_mask) < storage_state.max_users))
  293. | ((uid & uid_mask) == uid_mask));
  294. if (!ok) {
  295. printf("Received bad uid: %08x\n", uid);
  296. assert(ok);
  297. }
  298. }
  299. // Testing: report how many real and dummy messages arrived
  300. printf("Storage server received %u messages:\n", num_msgs);
  301. for (uint32_t i=0; i<num_msgs; ++i) {
  302. uint32_t dest_addr = *(const uint32_t*)msgs;
  303. nodenum_t dest_node =
  304. g_teems_config.storage_map[dest_addr >> DEST_UID_BITS];
  305. if (dest_node != my_node_num) {
  306. char hexbuf[2*msg_size + 1];
  307. for (uint32_t j=0;j<msg_size;++j) {
  308. snprintf(hexbuf+2*j, 3, "%02x", msgs[j]);
  309. }
  310. printf("Misrouted message: %s\n", hexbuf);
  311. } else if ((dest_addr & uid_mask) == uid_mask) {
  312. ++padding;
  313. } else {
  314. ++real;
  315. }
  316. msgs += msg_size;
  317. }
  318. printf("%u real, %u padding\n", real, padding);
  319. /*
  320. for (uint32_t i=0;i<num_msgs; ++i) {
  321. printf("%3d: %08x %08x\n", i,
  322. *(uint32_t*)(storage_buf.buf+(i*msg_size)),
  323. *(uint32_t*)(storage_buf.buf+(i*msg_size+4)));
  324. }
  325. */
  326. // Sort the received messages by userid into the
  327. // storage_state.stg_buf MsgBuffer.
  328. #ifdef PROFILE_STORAGE
  329. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u)\n", storage_buf.inserted);
  330. #endif
  331. sort_mtobliv<UidKey>(g_teems_config.nthreads, storage_buf.buf,
  332. msg_size, storage_buf.inserted, storage_buf.bufsize,
  333. storage_state.stg_buf.buf);
  334. #ifdef PROFILE_STORAGE
  335. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u)\n", storage_buf.inserted);
  336. #endif
  337. // For public routing, remove excess per-user messages by making them
  338. // padding, and then compact non-padding messages.
  339. if (!g_teems_config.private_routing) {
  340. uint8_t *msg = storage_state.stg_buf.buf;
  341. uint32_t uid;
  342. uint32_t prev_uid = uid_mask; // initialization technically unnecessary
  343. uint32_t num_user_msgs = 0; // number of messages to the user
  344. uint8_t sel;
  345. for (uint32_t i=0; i<num_msgs; ++i) {
  346. uid = (*(uint32_t*) msg) & uid_mask;
  347. num_user_msgs = oselect_uint32_t(1, num_user_msgs+1,
  348. uid == prev_uid);
  349. // Select if messages per user not exceeded and msg is not padding
  350. sel = ((uint8_t) ((num_user_msgs <= g_teems_config.m_pub_in))) &
  351. ((uint8_t) uid != uid_mask);
  352. storage_state.pub_selected[i] = (bool) sel;
  353. // Make padding if not selected
  354. *(uint32_t *) msg = (*(uint32_t *) msg) & nid_mask;
  355. *(uint32_t *) msg += oselect_uint32_t(uid_mask, uid, sel);
  356. msg += msg_size;
  357. prev_uid = uid;
  358. }
  359. #ifdef PROFILE_STORAGE
  360. unsigned long start_compaction =
  361. printf_with_rtclock("begin public-channel compaction (%u)\n",
  362. num_msgs);
  363. #endif
  364. TightCompact_parallel<OSWAP_16X>(
  365. (unsigned char *) storage_state.stg_buf.buf,
  366. num_msgs, msg_size, storage_state.pub_selected,
  367. g_teems_config.nthreads);
  368. #ifdef PROFILE_STORAGE
  369. printf_with_rtclock_diff(start_compaction,
  370. "end public-channel compaction (%u)\n", num_msgs);
  371. #endif
  372. }
  373. /*
  374. for (uint32_t i=0;i<num_msgs; ++i) {
  375. printf("%3d: %08x %08x\n", i,
  376. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  377. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)));
  378. }
  379. */
  380. #ifdef PROFILE_STORAGE
  381. unsigned long start_dest = printf_with_rtclock("begin setting dests (%u)\n", storage_state.stg_buf.bufsize);
  382. #endif
  383. // Obliviously set the dest array
  384. uint32_t *dests = storage_state.dest.data();
  385. uint32_t stg_size = storage_state.stg_buf.bufsize;
  386. uint8_t *buf = storage_state.stg_buf.buf;
  387. uint32_t m_in = g_teems_config.private_routing ?
  388. g_teems_config.m_priv_in : g_teems_config.m_pub_in;
  389. uint32_t uid = *(uint32_t*)(buf);
  390. uid &= uid_mask;
  391. // num_msgs is not a private value
  392. if (num_msgs > 0) {
  393. dests[0] = oselect_uint32_t(uid * m_in, 0xffffffff,
  394. uid == uid_mask);
  395. }
  396. uint32_t prev_uid = uid;
  397. for (uint32_t i=1; i<num_msgs; ++i) {
  398. uid = *(uint32_t*)(buf + i*msg_size);
  399. uid &= uid_mask;
  400. uint32_t next = oselect_uint32_t(uid * m_in, dests[i-1]+1,
  401. uid == prev_uid);
  402. dests[i] = oselect_uint32_t(next, 0xffffffff, uid == uid_mask);
  403. prev_uid = uid;
  404. }
  405. for (uint32_t i=num_msgs; i<stg_size; ++i) {
  406. dests[i] = 0xffffffff;
  407. *(uint32_t*)(buf + i*msg_size) = 0xffffffff;
  408. }
  409. #ifdef PROFILE_STORAGE
  410. printf_with_rtclock_diff(start_dest, "end setting dests (%u)\n", stg_size);
  411. #endif
  412. /*
  413. for (uint32_t i=0;i<stg_size; ++i) {
  414. printf("%3d: %08x %08x %u\n", i,
  415. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  416. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)),
  417. dests[i]);
  418. }
  419. */
  420. #ifdef PROFILE_STORAGE
  421. unsigned long start_expand = printf_with_rtclock("begin ORExpand (%u)\n", stg_size);
  422. #endif
  423. ORExpand_parallel<OSWAP_16X>(storage_state.stg_buf.buf, dests,
  424. msg_size, stg_size, g_teems_config.nthreads);
  425. #ifdef PROFILE_STORAGE
  426. printf_with_rtclock_diff(start_expand, "end ORExpand (%u)\n", stg_size);
  427. #endif
  428. /*
  429. for (uint32_t i=0;i<stg_size; ++i) {
  430. printf("%3d: %08x %08x %u\n", i,
  431. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  432. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)),
  433. dests[i]);
  434. }
  435. */
  436. // You can do more processing after these lines, as long as they
  437. // don't touch storage_buf. They _can_ touch the backing buffer
  438. // storage_state.stg_buf.
  439. storage_buf.reset();
  440. pthread_mutex_unlock(&storage_buf.mutex);
  441. if (g_teems_config.private_routing) {
  442. generate_all_tokens();
  443. }
  444. processMsgs();
  445. storage_epoch++;
  446. storage_state.stg_buf.reset();
  447. #ifdef PROFILE_STORAGE
  448. printf_with_rtclock_diff(start_received, "end storage_received (%u)\n", storage_buf.inserted);
  449. #endif
  450. }
  451. bool ecall_storage_authenticate(clientid_t cid, unsigned char *auth_message)
  452. {
  453. bool ret = false;
  454. uint32_t lcid = cid / g_teems_config.num_storage_nodes;
  455. const sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  456. ret = authenticateClient(auth_message, ckey);
  457. if(!ret) {
  458. printf("Storage authentication FAIL\n");
  459. }
  460. return ret;
  461. }
  462. void ecall_supply_storage_buffers(unsigned char *mailboxes,
  463. uint32_t mailboxes_size, unsigned char *tokens, uint32_t tokens_size)
  464. {
  465. epoch_mailboxes = mailboxes;
  466. epoch_tokens = tokens;
  467. }