ingest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 lcid = cid-cstart;
  65. const sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  66. return(authenticateClient(auth_message, ckey));
  67. }
  68. bool Ingestion::processMsgBundle(clientid_t cid, unsigned char *msgbundle,
  69. uint32_t num_msgs) {
  70. // Fetch corresponding client key
  71. clientid_t lcid = cid - g_ing.cstart;
  72. sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
  73. unsigned char *iv = msgbundle;
  74. msgbundle += SGX_AESGCM_IV_SIZE;
  75. uint16_t msg_size = g_teems_config.msg_size;
  76. uint32_t msgbundle_size = num_msgs * (msg_size + TOKEN_SIZE);
  77. unsigned char *dec_msgbundle = (unsigned char *) malloc (msgbundle_size);
  78. sgx_aes_gcm_128bit_tag_t *tag = (sgx_aes_gcm_128bit_tag_t*) (msgbundle + msgbundle_size);
  79. sgx_status_t ret = sgx_rijndael128GCM_decrypt(ckey, msgbundle, msgbundle_size,
  80. dec_msgbundle, iv, SGX_AESGCM_IV_SIZE, NULL, 0, tag);
  81. if(ret!=SGX_SUCCESS) {
  82. printf("Ingestion::processMsgBundle FAIL\n");
  83. printf("Error code: %d", (uint32_t) ret);
  84. free(dec_msgbundle);
  85. return false;
  86. }
  87. // TODO: Verify the tokens from end of the msgbundle
  88. // before appending them into the MsgBuffer
  89. bool verified = true;
  90. if(verified) {
  91. // Append msgbundle to g_ing.buffer;
  92. MsgBuffer &msg_queue = *(g_ing.buffer);
  93. pthread_mutex_lock(&msg_queue.mutex);
  94. uint32_t head = msg_queue.reserved;
  95. if (head + num_msgs > g_ing.max_buffer_size) {
  96. pthread_mutex_unlock(&msg_queue.mutex);
  97. printf("Ingestions: Max %u messages exceeded\n",
  98. g_ing.max_buffer_size);
  99. return false;
  100. }
  101. msg_queue.reserved += num_msgs;
  102. pthread_mutex_unlock(&msg_queue.mutex);
  103. /*
  104. if(lcid==0) {
  105. printf("\n\nIngestion: Message for lcid 0, S, R = %ld, %ld\n\n\n", *((uint32_t*) dec_msgbundle),
  106. *((uint32_t*) (dec_msgbundle + 4)));
  107. }
  108. */
  109. memmove(msg_queue.buf + head * msg_size,
  110. dec_msgbundle, num_msgs * msg_size);
  111. pthread_mutex_lock(&msg_queue.mutex);
  112. msg_queue.inserted += num_msgs;
  113. pthread_mutex_unlock(&msg_queue.mutex);
  114. free(dec_msgbundle);
  115. return true;
  116. }
  117. return false;
  118. }
  119. void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
  120. {
  121. printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
  122. cnum, cstart, cnum + cstart);
  123. for(uint32_t i=0; i<cnum; i++)
  124. {
  125. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  126. unsigned char iv[SGX_AESGCM_IV_SIZE];
  127. sgx_aes_gcm_128bit_tag_t tag;
  128. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  129. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  130. uint32_t client_num = cstart + i;
  131. memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
  132. sgx_status_t ret = SGX_SUCCESS;
  133. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
  134. zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients[i].key), iv,
  135. SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  136. if(ret!=SGX_SUCCESS) {
  137. printf("Ingestion::GCK FAIL\n");
  138. }
  139. }
  140. }
  141. sgx_aes_gcm_128bit_key_t* Ingestion::getClientKey(uint32_t lcid)
  142. {
  143. return(&(clients[lcid].key));
  144. }