storage.cpp 14 KB

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