ingest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. printf("Sender ID: %d, Receiver ID: %d, Token: N/A\n", sid, rid );
  15. printf("Message: ");
  16. for(int j = 0; j<msg_size - sizeof(sid)*2; j++) {
  17. printf("%x", (*ptr));
  18. ptr++;
  19. }
  20. printf("\n");
  21. }
  22. void displayEncMessageBundle(unsigned char *bundle, uint16_t priv_out, uint16_t msg_size) {
  23. unsigned char *ptr = bundle;
  24. printf("IV: ");
  25. for(int i=0; i<SGX_AESGCM_IV_SIZE; i++) {
  26. printf("%x", ptr[i]);
  27. }
  28. printf("\n");
  29. ptr+= SGX_AESGCM_IV_SIZE;
  30. for(int i=0; i<priv_out; i++) {
  31. displayMessage(ptr, msg_size);
  32. ptr+=msg_size;
  33. }
  34. printf("MAC: ");
  35. for(int i=0; i<SGX_AESGCM_MAC_SIZE; i++) {
  36. printf("%x", ptr[i]);
  37. }
  38. printf("\n");
  39. }
  40. bool ecall_ingest_msgbundle(clientid_t cid, unsigned char *msgbundle,
  41. uint32_t num_msgs) {
  42. bool ret;
  43. ret = g_ing.processMsgBundle(cid, msgbundle, num_msgs);
  44. return ret;
  45. }
  46. void Ingestion::initialize(uint32_t cnum, uint32_t cstart, sgx_aes_gcm_128bit_key_t &ESK) {
  47. clients.num = cnum;
  48. clients.start = cstart;
  49. clients.end = cnum + cstart;
  50. clients.keys = new sgx_aes_gcm_128bit_key_t[cnum];
  51. generateClientKeys(ESK);
  52. // Initialize the MsgBuffer to correct size
  53. max_buffer_size = g_teems_config.m_priv_out * cnum;
  54. buffer.alloc(max_buffer_size);
  55. }
  56. bool Ingestion::processMsgBundle(clientid_t cid, unsigned char *msgbundle,
  57. uint32_t num_msgs) {
  58. // Fetch corresponding client key
  59. clientid_t lcid = cid - g_ing.clients.start;
  60. sgx_aes_gcm_128bit_key_t ckey;
  61. memcpy(ckey, (g_ing.clients).keys[lcid], SGX_AESGCM_KEY_SIZE);
  62. unsigned char *iv = msgbundle;
  63. msgbundle += SGX_AESGCM_IV_SIZE;
  64. uint16_t msg_size = g_teems_config.msg_size;
  65. uint32_t msgbundle_size = num_msgs * msg_size;
  66. unsigned char *dec_msgbundle = (unsigned char *) malloc (msgbundle_size);
  67. sgx_aes_gcm_128bit_tag_t tag;
  68. memcpy(tag, msgbundle + msgbundle_size, SGX_AESGCM_MAC_SIZE);
  69. //sgx_aes_gcm_128bit_tag_t *tag = (sgx_aes_gcm_128bit_tag_t*) (msgbundle + msgbundle_size);
  70. sgx_status_t ret = sgx_rijndael128GCM_decrypt(&ckey, msgbundle, msgbundle_size,
  71. dec_msgbundle, iv, SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  72. if(ret!=SGX_SUCCESS) {
  73. printf("Ingestion::processMsgBundle FAIL\n");
  74. printf("Error code: %d", (uint32_t) ret);
  75. }
  76. // Append msgbundle to g_ing.buffer;
  77. MsgBuffer &msg_queue = g_ing.buffer;
  78. pthread_mutex_lock(&msg_queue.mutex);
  79. uint32_t head = msg_queue.reserved;
  80. if (head + num_msgs > g_ing.max_buffer_size) {
  81. pthread_mutex_unlock(&msg_queue.mutex);
  82. printf("Max %u messages exceeded\n",
  83. g_ing.max_buffer_size);
  84. return false;
  85. }
  86. msg_queue.reserved += num_msgs;
  87. pthread_mutex_unlock(&msg_queue.mutex);
  88. memmove(msg_queue.buf + head * msg_size,
  89. dec_msgbundle, num_msgs * msg_size);
  90. pthread_mutex_lock(&msg_queue.mutex);
  91. msg_queue.inserted += num_msgs;
  92. pthread_mutex_unlock(&msg_queue.mutex);
  93. free(dec_msgbundle);
  94. return true;
  95. }
  96. void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
  97. {
  98. printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
  99. clients.num, clients.start, clients.end);
  100. for(uint32_t i=0; i<clients.num; i++)
  101. {
  102. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  103. unsigned char iv[SGX_AESGCM_IV_SIZE];
  104. sgx_aes_gcm_128bit_tag_t tag;
  105. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  106. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  107. uint32_t client_num = clients.start + i;
  108. memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
  109. sgx_status_t ret = SGX_SUCCESS;
  110. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
  111. zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients.keys[i]), iv,
  112. SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  113. if(ret!=SGX_SUCCESS) {
  114. printf("Ingestion::GCK FAIL\n");
  115. }
  116. }
  117. }