ingest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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;
  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. }
  85. // Append msgbundle to g_ing.buffer;
  86. MsgBuffer &msg_queue = *(g_ing.buffer);
  87. pthread_mutex_lock(&msg_queue.mutex);
  88. uint32_t head = msg_queue.reserved;
  89. if (head + num_msgs > g_ing.max_buffer_size) {
  90. pthread_mutex_unlock(&msg_queue.mutex);
  91. printf("Ingestions: Max %u messages exceeded\n",
  92. g_ing.max_buffer_size);
  93. return false;
  94. }
  95. msg_queue.reserved += num_msgs;
  96. pthread_mutex_unlock(&msg_queue.mutex);
  97. memmove(msg_queue.buf + head * msg_size,
  98. dec_msgbundle, num_msgs * msg_size);
  99. pthread_mutex_lock(&msg_queue.mutex);
  100. msg_queue.inserted += num_msgs;
  101. pthread_mutex_unlock(&msg_queue.mutex);
  102. free(dec_msgbundle);
  103. return true;
  104. }
  105. void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
  106. {
  107. printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
  108. cnum, cstart, cnum + cstart);
  109. for(uint32_t i=0; i<cnum; i++)
  110. {
  111. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  112. unsigned char iv[SGX_AESGCM_IV_SIZE];
  113. sgx_aes_gcm_128bit_tag_t tag;
  114. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  115. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  116. uint32_t client_num = cstart + i;
  117. memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
  118. sgx_status_t ret = SGX_SUCCESS;
  119. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
  120. zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients[i].key), iv,
  121. SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  122. if(ret!=SGX_SUCCESS) {
  123. printf("Ingestion::GCK FAIL\n");
  124. }
  125. }
  126. }
  127. sgx_aes_gcm_128bit_key_t* Ingestion::getClientKey(uint32_t lcid)
  128. {
  129. return(&(clients[lcid].key));
  130. }