ingest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. {
  43. bool ret;
  44. ret = g_ing.processMsgBundle(cid, msgbundle, num_msgs);
  45. return ret;
  46. }
  47. bool ecall_authenticate(clientid_t cid, unsigned char *auth_message)
  48. {
  49. bool ret;
  50. ret = g_ing.authenticate(cid, auth_message);
  51. return ret;
  52. }
  53. void Ingestion::initialize(uint32_t cnum, uint32_t cstart, sgx_aes_gcm_128bit_key_t &ESK) {
  54. clients.num = cnum;
  55. clients.start = cstart;
  56. clients.end = cnum + cstart;
  57. clients.keys = new sgx_aes_gcm_128bit_key_t[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. int auth_success = 0;
  65. unsigned long epoch_no = *((unsigned long*) auth_message);
  66. auth_message+=(sizeof(unsigned long));
  67. // Fetch corresponding client key
  68. clientid_t lcid = cid - g_ing.clients.start;
  69. sgx_aes_gcm_128bit_key_t &ckey = (g_ing.clients).keys[lcid];
  70. unsigned char computed_auth[SGX_AESGCM_KEY_SIZE];
  71. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  72. unsigned char iv[SGX_AESGCM_IV_SIZE];
  73. sgx_aes_gcm_128bit_tag_t mac;
  74. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  75. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  76. sgx_status_t ret = SGX_SUCCESS;
  77. ret = sgx_rijndael128GCM_encrypt(&ckey, zeroes, SGX_AESGCM_KEY_SIZE,
  78. computed_auth, (unsigned char*) (&epoch_no), sizeof(epoch_no), NULL, 0, &mac);
  79. if(ret!=SGX_SUCCESS) {
  80. return -1;
  81. }
  82. auth_success = memcmp(auth_message, computed_auth, SGX_AESGCM_KEY_SIZE);
  83. if(auth_success == 0) {
  84. return true;
  85. } else {
  86. printf("authentication FAIL\n");
  87. return false;
  88. }
  89. }
  90. bool Ingestion::processMsgBundle(clientid_t cid, unsigned char *msgbundle,
  91. uint32_t num_msgs) {
  92. // Fetch corresponding client key
  93. clientid_t lcid = cid - g_ing.clients.start;
  94. sgx_aes_gcm_128bit_key_t &ckey = (g_ing.clients).keys[lcid];
  95. unsigned char *iv = msgbundle;
  96. msgbundle += SGX_AESGCM_IV_SIZE;
  97. uint16_t msg_size = g_teems_config.msg_size;
  98. uint32_t msgbundle_size = num_msgs * msg_size;
  99. unsigned char *dec_msgbundle = (unsigned char *) malloc (msgbundle_size);
  100. //sgx_aes_gcm_128bit_tag_t tag;
  101. //memcpy(tag, msgbundle + msgbundle_size, SGX_AESGCM_MAC_SIZE);
  102. sgx_aes_gcm_128bit_tag_t *tag = (sgx_aes_gcm_128bit_tag_t*) (msgbundle + msgbundle_size);
  103. sgx_status_t ret = sgx_rijndael128GCM_decrypt(&ckey, msgbundle, msgbundle_size,
  104. dec_msgbundle, iv, SGX_AESGCM_IV_SIZE, NULL, 0, tag);
  105. if(ret!=SGX_SUCCESS) {
  106. printf("Ingestion::processMsgBundle FAIL\n");
  107. printf("Error code: %d", (uint32_t) ret);
  108. }
  109. // Append msgbundle to g_ing.buffer;
  110. MsgBuffer &msg_queue = *(g_ing.buffer);
  111. pthread_mutex_lock(&msg_queue.mutex);
  112. uint32_t head = msg_queue.reserved;
  113. if (head + num_msgs > g_ing.max_buffer_size) {
  114. pthread_mutex_unlock(&msg_queue.mutex);
  115. printf("Ingestions: Max %u messages exceeded\n",
  116. g_ing.max_buffer_size);
  117. return false;
  118. }
  119. msg_queue.reserved += num_msgs;
  120. pthread_mutex_unlock(&msg_queue.mutex);
  121. memmove(msg_queue.buf + head * msg_size,
  122. dec_msgbundle, num_msgs * msg_size);
  123. pthread_mutex_lock(&msg_queue.mutex);
  124. msg_queue.inserted += num_msgs;
  125. pthread_mutex_unlock(&msg_queue.mutex);
  126. free(dec_msgbundle);
  127. return true;
  128. }
  129. void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
  130. {
  131. printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
  132. clients.num, clients.start, clients.end);
  133. for(uint32_t i=0; i<clients.num; i++)
  134. {
  135. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  136. unsigned char iv[SGX_AESGCM_IV_SIZE];
  137. sgx_aes_gcm_128bit_tag_t tag;
  138. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  139. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  140. uint32_t client_num = clients.start + i;
  141. memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
  142. sgx_status_t ret = SGX_SUCCESS;
  143. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
  144. zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients.keys[i]), iv,
  145. SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
  146. if(ret!=SGX_SUCCESS) {
  147. printf("Ingestion::GCK FAIL\n");
  148. }
  149. }
  150. }