ingest.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include <pthread.h>
  2. #include "Enclave_t.h"
  3. #include "utils.hpp"
  4. #include "config.hpp"
  5. #include "route.hpp"
  6. #include "ingest.hpp"
  7. Ingestion g_ing;
  8. void displayMessage(unsigned char *msg, uint16_t msg_size) {
  9. clientid_t sid, rid;
  10. unsigned char *ptr = msg;
  11. sid = *((clientid_t*) ptr);
  12. ptr+=sizeof(sid);
  13. rid = *((clientid_t*) ptr);
  14. ptr+=sizeof(rid);
  15. printf("Sender ID: %d, Receiver ID: %d, Token: N/A\n", sid, rid );
  16. printf("Message: ");
  17. for(int j = 0; j<msg_size - sizeof(sid)*2; j++) {
  18. printf("%x", (*ptr));
  19. ptr++;
  20. }
  21. printf("\n");
  22. }
  23. void displayEncMessageBundle(unsigned char *bundle, uint16_t priv_out, uint16_t msg_size) {
  24. unsigned char *ptr = bundle;
  25. printf("IV: ");
  26. for(int i=0; i<SGX_AESGCM_IV_SIZE; i++) {
  27. printf("%x", ptr[i]);
  28. }
  29. printf("\n");
  30. ptr+= SGX_AESGCM_IV_SIZE;
  31. for(int i=0; i<priv_out; i++) {
  32. displayMessage(ptr, msg_size);
  33. ptr+=msg_size;
  34. }
  35. printf("MAC: ");
  36. for(int i=0; i<SGX_AESGCM_MAC_SIZE; i++) {
  37. printf("%x", ptr[i]);
  38. }
  39. printf("\n");
  40. }
  41. bool ecall_ingest_msgbundle(clientid_t cid, unsigned char *msgbundle,
  42. uint32_t num_msgs)
  43. {
  44. bool ret;
  45. ret = g_ing.processMsgBundle(cid, msgbundle, num_msgs);
  46. return ret;
  47. }
  48. bool ecall_authenticate(clientid_t cid, unsigned char *auth_message)
  49. {
  50. bool ret;
  51. ret = g_ing.authenticate(cid, auth_message);
  52. return ret;
  53. }
  54. void Ingestion::initialize(uint32_t num, uint32_t start, sgx_aes_gcm_128bit_key_t &ESK) {
  55. cnum = num;
  56. cstart = start;
  57. clients = new IngClient[cnum];
  58. generateClientKeys(ESK);
  59. max_buffer_size = g_teems_config.m_priv_out * cnum;
  60. buffer = &(route_state.ingbuf);
  61. }
  62. bool Ingestion::authenticate(clientid_t cid, unsigned char *auth_message)
  63. {
  64. uint32_t num_ing_nodes = g_teems_config.num_ingestion_nodes;
  65. uint32_t lcid = cid / num_ing_nodes;
  66. const sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  67. return(authenticateClient(auth_message, ckey));
  68. }
  69. bool Ingestion::processMsgBundle(clientid_t cid, unsigned char *msgbundle,
  70. uint32_t num_msgs) {
  71. // Fetch corresponding client key
  72. uint32_t num_ing_nodes = g_teems_config.num_ingestion_nodes;
  73. clientid_t lcid = cid / num_ing_nodes;
  74. sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  75. unsigned char *iv = msgbundle;
  76. msgbundle += SGX_AESGCM_IV_SIZE;
  77. uint16_t msg_size = g_teems_config.msg_size;
  78. uint32_t msgbundle_size = num_msgs * (msg_size + TOKEN_SIZE);
  79. unsigned char *dec_msgbundle = (unsigned char *) malloc (msgbundle_size);
  80. sgx_aes_gcm_128bit_tag_t *tag = (sgx_aes_gcm_128bit_tag_t*) (msgbundle + msgbundle_size);
  81. sgx_status_t ret = sgx_rijndael128GCM_decrypt(ckey, msgbundle, msgbundle_size,
  82. dec_msgbundle, iv, SGX_AESGCM_IV_SIZE, NULL, 0, tag);
  83. if(ret!=SGX_SUCCESS) {
  84. printf("Ingestion::processMsgBundle FAIL\n");
  85. printf("Error code: %d", (uint32_t) ret);
  86. free(dec_msgbundle);
  87. return false;
  88. }
  89. // TODO: Verify the tokens from end of the msgbundle
  90. // before appending them into the MsgBuffer
  91. bool verified = true;
  92. unsigned char token_body[TOKEN_SIZE];
  93. const sgx_aes_gcm_128bit_key_t *pTSK = &(g_teems_config.TSK);
  94. unsigned char *dm_ptr = dec_msgbundle;
  95. unsigned char *tkn_ptr = dm_ptr + (msg_size * num_msgs);
  96. sgx_cmac_state_handle_t cmac_handle;
  97. sgx_cmac_128bit_tag_t token;
  98. for(int k =0; k < num_msgs; k++)
  99. {
  100. ret = SGX_SUCCESS;
  101. memset(token_body, 0, TOKEN_SIZE);
  102. memcpy(token_body, dm_ptr, sizeof(clientid_t) * 2);
  103. memcpy(token_body + sizeof(clientid_t) * 2, (uint8_t*) &ingestion_epoch, sizeof(ingestion_epoch));
  104. ret = sgx_rijndael128_cmac_msg(pTSK, token_body, TOKEN_SIZE,
  105. (sgx_cmac_128bit_tag_t*) &token);
  106. if(ret!=SGX_SUCCESS) {
  107. printf("Ingestion::processMsgBundle: Token recreation FAIL\n");
  108. printf("ret = %x", ret);
  109. return false;
  110. }
  111. /*
  112. printf("Received token:\n");
  113. for(int l=0; l<SGX_CMAC_MAC_SIZE; l++) {
  114. printf("%x", tkn_ptr[l]);
  115. }
  116. printf("Verify created token:\n");
  117. for(int l=0; l<SGX_CMAC_MAC_SIZE; l++) {
  118. printf("%x", token[l]);
  119. }
  120. */
  121. int diff = memcmp(token, tkn_ptr, SGX_CMAC_MAC_SIZE);
  122. if(diff!=0) {
  123. printf("Ingestion::processMsgBundle: Tokens do not match FAIL\n");
  124. return false;
  125. }
  126. dm_ptr+= msg_size;
  127. tkn_ptr+=SGX_CMAC_MAC_SIZE;
  128. }
  129. if(verified) {
  130. // Append msgbundle to g_ing.buffer;
  131. MsgBuffer &msg_queue = *(g_ing.buffer);
  132. pthread_mutex_lock(&msg_queue.mutex);
  133. uint32_t head = msg_queue.reserved;
  134. if (head + num_msgs > g_ing.max_buffer_size) {
  135. pthread_mutex_unlock(&msg_queue.mutex);
  136. printf("Ingestions: Max %u messages exceeded\n",
  137. g_ing.max_buffer_size);
  138. return false;
  139. }
  140. msg_queue.reserved += num_msgs;
  141. pthread_mutex_unlock(&msg_queue.mutex);
  142. memmove(msg_queue.buf + head * msg_size,
  143. dec_msgbundle, num_msgs * msg_size);
  144. pthread_mutex_lock(&msg_queue.mutex);
  145. msg_queue.inserted += num_msgs;
  146. pthread_mutex_unlock(&msg_queue.mutex);
  147. free(dec_msgbundle);
  148. return true;
  149. }
  150. return false;
  151. }
  152. void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
  153. {
  154. //printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
  155. // cnum, cstart, cnum + cstart);
  156. uint32_t num_ing_nodes = g_teems_config.num_ingestion_nodes;
  157. for(uint32_t i=0; i<cnum; i++)
  158. {
  159. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  160. unsigned char iv[SGX_AESGCM_IV_SIZE];
  161. sgx_aes_gcm_128bit_tag_t tag;
  162. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  163. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  164. uint32_t client_num = cstart + (i * num_ing_nodes);
  165. memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
  166. sgx_status_t ret = SGX_SUCCESS;
  167. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
  168. zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients[i].key), iv,
  169. SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  170. if(ret!=SGX_SUCCESS) {
  171. printf("Ingestion::GCK FAIL\n");
  172. }
  173. /*
  174. printf("Client %d Ingestion: (after Gen) ", client_num);
  175. for(int j=0;j<SGX_AESGCM_KEY_SIZE;j++) {
  176. printf("%x", (clients[i].key)[j]);
  177. }
  178. printf("\n");
  179. */
  180. }
  181. }
  182. sgx_aes_gcm_128bit_key_t* Ingestion::getClientKey(uint32_t lcid)
  183. {
  184. return(&(clients[lcid].key));
  185. }