ingest.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. bool ecall_ingest_msgbundle(clientid_t cid, unsigned char *msgbundle,
  9. uint32_t num_msgs) {
  10. bool ret;
  11. ret = g_ing.processMsgBundle(cid, msgbundle, num_msgs);
  12. return ret;
  13. }
  14. void Ingestion::initialize(uint32_t cnum, uint32_t cstart, sgx_aes_gcm_128bit_key_t &ESK) {
  15. clients.num = cnum;
  16. clients.start = cstart;
  17. clients.end = cnum + cstart;
  18. clients.keys = new sgx_aes_gcm_128bit_key_t[num];
  19. generateClientKeys(ESK);
  20. // Initialize the MsgBuffer to correct size
  21. max_buffer_size = g_teems_config.m_priv_out * cnum;
  22. buffer.alloc(max_buffer_size);
  23. }
  24. bool Ingestion::processMsgBundle(clientid_t cid, unsigned char *msgbundle,
  25. uint32_t num_msgs) {
  26. // Fetch corresponding client key
  27. sgx_aes_gcm_128bit_key_t &ckey = g_ing.clients.keys[cid];
  28. // Decrypt and verify tag for the message bundle
  29. // Append msgbundle to g_ing.buffer;
  30. uint16_t msg_size = g_teems_config.msg_size;
  31. MsgBuffer &msg_queue = g_ing.buffer;
  32. pthread_mutex_lock(&msg_queue.mutex);
  33. uint32_t head = msg_queue.reserved;
  34. if (head + num_msgs > g_ing.max_buffer_size) {
  35. pthread_mutex_unlock(&msg_queue.mutex);
  36. printf("Max %u messages exceeded\n",
  37. g_ing.max_buffer_size);
  38. return false;
  39. }
  40. msg_queue.reserved += num_msgs;
  41. pthread_mutex_unlock(&msg_queue.mutex);
  42. memmove(msg_queue.buf + head * msg_size,
  43. msgbundle, num_msgs * msg_size);
  44. pthread_mutex_lock(&msg_queue.mutex);
  45. msg_queue.inserted += num_msgs;
  46. pthread_mutex_unlock(&msg_queue.mutex);
  47. return true;
  48. }
  49. void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
  50. {
  51. printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
  52. clients.num, clients.start, clients.end);
  53. for(uint32_t i=0; i<clients.num; i++)
  54. {
  55. unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
  56. unsigned char iv[SGX_AESGCM_IV_SIZE];
  57. sgx_aes_gcm_128bit_tag_t mac;
  58. memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
  59. memset(iv, 0, SGX_AESGCM_IV_SIZE);
  60. uint32_t client_num = clients.start + i;
  61. memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
  62. sgx_status_t ret = SGX_SUCCESS;
  63. ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
  64. zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients.keys[i]), iv,
  65. SGX_AESGCM_IV_SIZE, NULL, 0, &mac);
  66. if(ret!=SGX_SUCCESS) {
  67. printf("Ingestion::GCK FAIL\n");
  68. }
  69. }
  70. }