config.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "Enclave_t.h"
  2. #include "comms.hpp"
  3. #include "config.hpp"
  4. #include "utils.hpp"
  5. #include "route.hpp"
  6. #include "ingest.hpp"
  7. #define CEILDIV(x,y) (((x)+(y)-1)/(y))
  8. Config g_teems_config;
  9. int generateMasterKeys(sgx_aes_gcm_128bit_key_t master_secret,
  10. sgx_aes_gcm_128bit_key_t &ESK, sgx_aes_gcm_128bit_key_t &TSK)
  11. {
  12. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  13. unsigned char iv[SGX_AESGCM_IV_SIZE];
  14. sgx_aes_gcm_128bit_tag_t mac;
  15. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  16. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  17. memcpy(iv, "Encryption", sizeof("Encryption"));
  18. sgx_status_t ret = SGX_SUCCESS;
  19. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *)
  20. (master_secret), zeroes, SGX_AESGCM_KEY_SIZE,
  21. (uint8_t*) ESK, iv, SGX_AESGCM_IV_SIZE, NULL, 0, &mac);
  22. if(ret!=SGX_SUCCESS) {
  23. return -1;
  24. }
  25. printf("Encryption Master Key: ");
  26. for(int i=0;i<SGX_AESGCM_KEY_SIZE;i++) {
  27. printf("%x", ESK[i]);
  28. }
  29. printf("\n\n");
  30. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  31. memcpy(iv, "Token", sizeof("Token"));
  32. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *)
  33. (master_secret), zeroes, SGX_AESGCM_KEY_SIZE,
  34. (uint8_t*) TSK, iv, SGX_AESGCM_IV_SIZE, NULL, 0, &mac);
  35. if(ret!=SGX_SUCCESS) {
  36. return -1;
  37. }
  38. printf("Token Master Key: ");
  39. for(int i=0;i<SGX_AESGCM_KEY_SIZE;i++) {
  40. printf("%x", TSK[i]);
  41. }
  42. printf("\n");
  43. return 1;
  44. }
  45. bool ecall_config_load(threadid_t nthreads, bool private_routing,
  46. EnclaveAPIParams *apiparams,
  47. EnclaveAPINodeConfig *apinodeconfigs,
  48. nodenum_t num_nodes, nodenum_t my_node_num)
  49. {
  50. g_teems_config.nthreads = nthreads;
  51. g_teems_config.num_nodes = num_nodes;
  52. g_teems_config.num_ingestion_nodes = 0;
  53. g_teems_config.num_routing_nodes = 0;
  54. g_teems_config.num_storage_nodes = 0;
  55. g_teems_config.my_node_num = my_node_num;
  56. g_teems_config.user_count = apiparams->user_count;
  57. g_teems_config.msg_size = apiparams->msg_size;
  58. g_teems_config.m_priv_out = apiparams->m_priv_out;
  59. g_teems_config.m_priv_in = apiparams->m_priv_in;
  60. g_teems_config.m_pub_out = apiparams->m_pub_out;
  61. g_teems_config.m_pub_in = apiparams->m_pub_in;
  62. g_teems_config.private_routing = private_routing;
  63. memcpy(g_teems_config.master_secret, apiparams->master_secret, SGX_AESGCM_KEY_SIZE);
  64. // Temporary vectors to store node numbers for nodes of different
  65. // types, where the node numbers are smaller than our own node
  66. // number
  67. std::vector<nodenum_t> ing_smaller, rte_smaller, str_smaller;
  68. uint16_t cumul_weight = 0;
  69. g_teems_config.weights.clear();
  70. g_teems_config.ingestion_nodes.clear();
  71. g_teems_config.routing_nodes.clear();
  72. g_teems_config.storage_nodes.clear();
  73. for (nodenum_t i=0; i<num_nodes; ++i) {
  74. NodeWeight nw;
  75. nw.startweight = cumul_weight;
  76. nw.weight = apinodeconfigs[i].weight;
  77. if (apinodeconfigs[i].roles & ROLE_INGESTION) {
  78. g_teems_config.num_ingestion_nodes += 1;
  79. if (i < my_node_num) {
  80. ing_smaller.push_back(i);
  81. } else {
  82. g_teems_config.ingestion_nodes.push_back(i);
  83. }
  84. }
  85. if (apinodeconfigs[i].roles & ROLE_ROUTING) {
  86. g_teems_config.num_routing_nodes += 1;
  87. if (i < my_node_num) {
  88. rte_smaller.push_back(i);
  89. } else {
  90. g_teems_config.routing_nodes.push_back(i);
  91. }
  92. }
  93. if (apinodeconfigs[i].roles & ROLE_STORAGE) {
  94. g_teems_config.num_storage_nodes += 1;
  95. if (i < my_node_num) {
  96. str_smaller.push_back(i);
  97. } else {
  98. g_teems_config.storage_nodes.push_back(i);
  99. }
  100. }
  101. cumul_weight += nw.weight;
  102. g_teems_config.weights.push_back(nw);
  103. if (i == my_node_num) {
  104. g_teems_config.my_weight = nw.weight;
  105. }
  106. }
  107. g_teems_config.tot_weight = cumul_weight;
  108. // Concatenate the *_smaller vectors to the ends of the
  109. // g_teems_config.*_nodes vectors. This way, each node has a list
  110. // of nodes of each role starting with itself and "looping around".
  111. // This should make the communication pattern have less of a
  112. // bottleneck.
  113. g_teems_config.ingestion_nodes.insert(
  114. g_teems_config.ingestion_nodes.end(),
  115. ing_smaller.begin(),
  116. ing_smaller.end());
  117. g_teems_config.routing_nodes.insert(
  118. g_teems_config.routing_nodes.end(),
  119. rte_smaller.begin(),
  120. rte_smaller.end());
  121. g_teems_config.storage_nodes.insert(
  122. g_teems_config.storage_nodes.end(),
  123. str_smaller.begin(),
  124. str_smaller.end());
  125. // Initialize the threadpool and the pseudorandom bytes pools
  126. threadpool_init(nthreads);
  127. PRB_pool_init(nthreads);
  128. if(apinodeconfigs[my_node_num].roles & ROLE_INGESTION) {
  129. sgx_aes_gcm_128bit_key_t ESK, TSK;
  130. generateMasterKeys(g_teems_config.master_secret, ESK, TSK);
  131. uint32_t num_clients_total = g_teems_config.user_count;
  132. uint32_t num_ing_nodes = g_teems_config.num_ingestion_nodes;
  133. uint32_t clients_per_server = CEILDIV(num_clients_total, num_ing_nodes);
  134. uint32_t ing_with_additional = num_clients_total % num_ing_nodes;
  135. uint32_t num_smaller_ing = (uint32_t) ing_smaller.size();
  136. uint32_t num_clients_this_ing = clients_per_server;
  137. num_clients_this_ing += (num_smaller_ing < ing_with_additional)? 1: 0;
  138. uint32_t client_start = num_smaller_ing * clients_per_server;
  139. if (ing_with_additional > 0) {
  140. if(ing_with_additional > num_smaller_ing) {
  141. client_start+= num_smaller_ing;
  142. } else {
  143. client_start+= ing_with_additional;
  144. }
  145. }
  146. g_ing.initialize(num_clients_this_ing, client_start, ESK);
  147. /*
  148. // Check that the keys generated in Enclave match the ones generated
  149. // in the client application
  150. for(uint32_t i=0; i<g_ing.getClientNum(); i++) {
  151. printf("Client Key %d: ", i + g_ing.getClientStart());
  152. sgx_aes_gcm_128bit_key_t key;
  153. unsigned char* ckey = (g_ing.getClientKeys())[i];
  154. memcpy(key, ckey, SGX_AESGCM_KEY_SIZE);
  155. for(int j = 0; j<SGX_AESGCM_KEY_SIZE; j++) {
  156. printf("%x", key[j]);
  157. }
  158. printf("\n");
  159. }
  160. */
  161. }
  162. if (!route_init()) {
  163. return false;
  164. }
  165. return comms_init_nodestate(apinodeconfigs, num_nodes, my_node_num);
  166. }
  167. void ecall_close()
  168. {
  169. PRB_pool_shutdown();
  170. threadpool_shutdown();
  171. }