storage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_msgbundles;
  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_msgbundles
  141. - ecall_send_msgbundle();
  142. */
  143. bool processMsgs() {
  144. unsigned char *epoch_buf_ptr = epoch_msgbundles;
  145. unsigned char *stg_buf_ptr = storage_state.stg_buf.buf;
  146. uint32_t msg_bundle_size = g_teems_config.m_priv_in * g_teems_config.msg_size;
  147. uint32_t enc_msg_bundle_size = msg_bundle_size + SGX_AESGCM_IV_SIZE + SGX_AESGCM_MAC_SIZE;
  148. sgx_status_t ret = SGX_SUCCESS;
  149. unsigned char *epoch_buf_ct_ptr = epoch_buf_ptr + SGX_AESGCM_IV_SIZE;
  150. unsigned char *epoch_buf_tag_ptr = epoch_buf_ct_ptr + msg_bundle_size;
  151. for(uint32_t lcid = 0; lcid <storage_state.max_users; lcid++) {
  152. memcpy(epoch_buf_ptr, clients[lcid].iv, SGX_AESGCM_IV_SIZE);
  153. ret = sgx_rijndael128GCM_encrypt(&(clients[lcid].key), stg_buf_ptr, msg_bundle_size,
  154. (uint8_t*) epoch_buf_ct_ptr, epoch_buf_ptr, SGX_AESGCM_IV_SIZE, NULL, 0,
  155. (sgx_aes_gcm_128bit_tag_t*) epoch_buf_tag_ptr);
  156. if(ret!=SGX_SUCCESS) {
  157. printf("processMsgs: Encrypting msgs FAIL\n");
  158. return false;
  159. }
  160. // Update IV
  161. uint64_t *iv_ctr = (uint64_t*) clients[lcid].iv;
  162. (*iv_ctr)+=1;
  163. /*
  164. if(lcid==0) {
  165. printf("\n\nMessage for lcid 0, S, R = %d, %d\n\n\n", *((uint32_t*) stg_buf_ptr),
  166. *((uint32_t*) (stg_buf_ptr + 4)));
  167. }
  168. */
  169. stg_buf_ptr+=msg_bundle_size;
  170. epoch_buf_ptr+=enc_msg_bundle_size;
  171. epoch_buf_ct_ptr+=enc_msg_bundle_size;
  172. epoch_buf_tag_ptr+=enc_msg_bundle_size;
  173. }
  174. return true;
  175. }
  176. // route_init will call this function; no one else should call it
  177. // explicitly. The parameter is the number of messages that can fit in
  178. // the storage-side MsgBuffer. Returns true on success, false on
  179. // failure.
  180. bool storage_init(uint32_t max_users, uint32_t msg_buf_size)
  181. {
  182. storage_state.max_users = max_users;
  183. storage_state.stg_buf.alloc(msg_buf_size);
  184. storage_state.dest.resize(msg_buf_size);
  185. uint32_t my_storage_node_id = 0;
  186. uint32_t my_stg_pos = 0;
  187. for (nodenum_t i=0; i<g_teems_config.num_nodes; ++i) {
  188. if (g_teems_config.roles[i] & ROLE_STORAGE) {
  189. if (i == g_teems_config.my_node_num) {
  190. storage_state.my_storage_node_id = my_storage_node_id << DEST_UID_BITS;
  191. my_stg_pos = my_storage_node_id;
  192. } else {
  193. ++my_storage_node_id;
  194. }
  195. }
  196. }
  197. printf("my_stg_pos = %d\n", my_stg_pos);
  198. storage_generateClientKeys(max_users, my_stg_pos);
  199. // sendClientTokens();
  200. return true;
  201. }
  202. // Handle the messages received by a storage node. Pass a _locked_
  203. // MsgBuffer. This function will itself reset and unlock it when it's
  204. // done with it.
  205. void storage_received(MsgBuffer &storage_buf)
  206. {
  207. uint16_t msg_size = g_teems_config.msg_size;
  208. nodenum_t my_node_num = g_teems_config.my_node_num;
  209. const uint8_t *msgs = storage_buf.buf;
  210. uint32_t num_msgs = storage_buf.inserted;
  211. uint32_t real = 0, padding = 0;
  212. uint32_t uid_mask = (1 << DEST_UID_BITS) - 1;
  213. uint32_t nid_mask = ~uid_mask;
  214. #ifdef PROFILE_STORAGE
  215. unsigned long start_received = printf_with_rtclock("begin storage_received (%u)\n", storage_buf.inserted);
  216. #endif
  217. // It's OK to test for errors in a way that's non-oblivous if
  218. // there's an error (but it should be oblivious if there are no
  219. // errors)
  220. for (uint32_t i=0; i<num_msgs; ++i) {
  221. uint32_t uid = *(const uint32_t*)(storage_buf.buf+(i*msg_size));
  222. bool ok = ((((uid & nid_mask) == storage_state.my_storage_node_id)
  223. & ((uid & uid_mask) < storage_state.max_users))
  224. | ((uid & uid_mask) == uid_mask));
  225. if (!ok) {
  226. printf("Received bad uid: %08x\n", uid);
  227. assert(ok);
  228. }
  229. }
  230. // Testing: report how many real and dummy messages arrived
  231. printf("Storage server received %u messages:\n", num_msgs);
  232. for (uint32_t i=0; i<num_msgs; ++i) {
  233. uint32_t dest_addr = *(const uint32_t*)msgs;
  234. nodenum_t dest_node =
  235. g_teems_config.storage_map[dest_addr >> DEST_UID_BITS];
  236. if (dest_node != my_node_num) {
  237. char hexbuf[2*msg_size + 1];
  238. for (uint32_t j=0;j<msg_size;++j) {
  239. snprintf(hexbuf+2*j, 3, "%02x", msgs[j]);
  240. }
  241. printf("Misrouted message: %s\n", hexbuf);
  242. } else if ((dest_addr & uid_mask) == uid_mask) {
  243. ++padding;
  244. } else {
  245. ++real;
  246. }
  247. msgs += msg_size;
  248. }
  249. printf("%u real, %u padding\n", real, padding);
  250. /*
  251. for (uint32_t i=0;i<num_msgs; ++i) {
  252. printf("%3d: %08x %08x\n", i,
  253. *(uint32_t*)(storage_buf.buf+(i*msg_size)),
  254. *(uint32_t*)(storage_buf.buf+(i*msg_size+4)));
  255. }
  256. */
  257. // Sort the received messages by userid into the
  258. // storage_state.stg_buf MsgBuffer.
  259. #ifdef PROFILE_STORAGE
  260. unsigned long start_sort = printf_with_rtclock("begin oblivious sort (%u)\n", storage_buf.inserted);
  261. #endif
  262. sort_mtobliv<UidKey>(g_teems_config.nthreads, storage_buf.buf,
  263. msg_size, storage_buf.inserted, storage_buf.bufsize,
  264. storage_state.stg_buf.buf);
  265. #ifdef PROFILE_STORAGE
  266. printf_with_rtclock_diff(start_sort, "end oblivious sort (%u)\n", storage_buf.inserted);
  267. #endif
  268. /*
  269. for (uint32_t i=0;i<num_msgs; ++i) {
  270. printf("%3d: %08x %08x\n", i,
  271. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  272. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)));
  273. }
  274. */
  275. #ifdef PROFILE_STORAGE
  276. unsigned long start_dest = printf_with_rtclock("begin setting dests (%u)\n", storage_state.stg_buf.bufsize);
  277. #endif
  278. // Obliviously set the dest array
  279. uint32_t *dests = storage_state.dest.data();
  280. uint32_t stg_size = storage_state.stg_buf.bufsize;
  281. const uint8_t *buf = storage_state.stg_buf.buf;
  282. uint32_t m_priv_in = g_teems_config.m_priv_in;
  283. uint32_t uid = *(uint32_t*)(buf);
  284. uid &= uid_mask;
  285. // num_msgs is not a private value
  286. if (num_msgs > 0) {
  287. dests[0] = oselect_uint32_t(uid * m_priv_in, 0xffffffff,
  288. uid == uid_mask);
  289. }
  290. uint32_t prev_uid = uid;
  291. for (uint32_t i=1; i<num_msgs; ++i) {
  292. uid = *(uint32_t*)(buf + i*msg_size);
  293. uid &= uid_mask;
  294. uint32_t next = oselect_uint32_t(uid * m_priv_in, dests[i-1]+1,
  295. uid == prev_uid);
  296. dests[i] = oselect_uint32_t(next, 0xffffffff, uid == uid_mask);
  297. prev_uid = uid;
  298. }
  299. for (uint32_t i=num_msgs; i<stg_size; ++i) {
  300. dests[i] = 0xffffffff;
  301. *(uint32_t*)(buf + i*msg_size) = 0xffffffff;
  302. }
  303. #ifdef PROFILE_STORAGE
  304. printf_with_rtclock_diff(start_dest, "end setting dests (%u)\n", stg_size);
  305. #endif
  306. /*
  307. for (uint32_t i=0;i<stg_size; ++i) {
  308. printf("%3d: %08x %08x %u\n", i,
  309. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  310. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)),
  311. dests[i]);
  312. }
  313. */
  314. #ifdef PROFILE_STORAGE
  315. unsigned long start_expand = printf_with_rtclock("begin ORExpand (%u)\n", stg_size);
  316. #endif
  317. ORExpand_parallel<OSWAP_16X>(storage_state.stg_buf.buf, dests,
  318. msg_size, stg_size, g_teems_config.nthreads);
  319. #ifdef PROFILE_STORAGE
  320. printf_with_rtclock_diff(start_expand, "end ORExpand (%u)\n", stg_size);
  321. #endif
  322. /*
  323. for (uint32_t i=0;i<stg_size; ++i) {
  324. printf("%3d: %08x %08x %u\n", i,
  325. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size)),
  326. *(uint32_t*)(storage_state.stg_buf.buf+(i*msg_size+4)),
  327. dests[i]);
  328. }
  329. */
  330. // You can do more processing after these lines, as long as they
  331. // don't touch storage_buf. They _can_ touch the backing buffer
  332. // storage_state.stg_buf.
  333. storage_buf.reset();
  334. pthread_mutex_unlock(&storage_buf.mutex);
  335. generate_all_tokens();
  336. uint32_t num_expected_msgs = g_teems_config.m_priv_in * storage_state.max_users;
  337. processMsgs();
  338. storage_epoch++;
  339. storage_state.stg_buf.reset();
  340. #ifdef PROFILE_STORAGE
  341. printf_with_rtclock_diff(start_received, "end storage_received (%u)\n", storage_buf.inserted);
  342. #endif
  343. }
  344. bool ecall_storage_authenticate(clientid_t cid, unsigned char *auth_message)
  345. {
  346. bool ret = false;
  347. uint32_t lcid = cid / g_teems_config.num_storage_nodes;
  348. const sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  349. ret = authenticateClient(auth_message, ckey);
  350. if(!ret) {
  351. printf("Storage authentication FAIL\n");
  352. }
  353. return ret;
  354. }
  355. void ecall_supply_storage_buffers(unsigned char *msgbundles,
  356. uint32_t msgbundles_size, unsigned char *tokens, uint32_t tokens_size)
  357. {
  358. epoch_msgbundles = msgbundles;
  359. epoch_tokens = tokens;
  360. }