storage.cpp 16 KB

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