123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include <pthread.h>
- #include "Enclave_t.h"
- #include "utils.hpp"
- #include "config.hpp"
- #include "route.hpp"
- #include "ingest.hpp"
- Ingestion g_ing;
- void displayMessage(unsigned char *msg, uint16_t msg_size) {
- clientid_t sid, rid;
- unsigned char *ptr = msg;
- sid = *((clientid_t*) ptr);
- ptr+=sizeof(sid);
- rid = *((clientid_t*) ptr);
- printf("Sender ID: %d, Receiver ID: %d, Token: N/A\n", sid, rid );
- printf("Message: ");
- for(int j = 0; j<msg_size - sizeof(sid)*2; j++) {
- printf("%x", (*ptr));
- ptr++;
- }
- printf("\n");
- }
- void displayEncMessageBundle(unsigned char *bundle, uint16_t priv_out, uint16_t msg_size) {
- unsigned char *ptr = bundle;
- printf("IV: ");
- for(int i=0; i<SGX_AESGCM_IV_SIZE; i++) {
- printf("%x", ptr[i]);
- }
- printf("\n");
- ptr+= SGX_AESGCM_IV_SIZE;
- for(int i=0; i<priv_out; i++) {
- displayMessage(ptr, msg_size);
- ptr+=msg_size;
- }
- printf("MAC: ");
- for(int i=0; i<SGX_AESGCM_MAC_SIZE; i++) {
- printf("%x", ptr[i]);
- }
- printf("\n");
- }
- bool ecall_ingest_msgbundle(clientid_t cid, unsigned char *msgbundle,
- uint32_t num_msgs)
- {
- bool ret;
- ret = g_ing.processMsgBundle(cid, msgbundle, num_msgs);
- return ret;
- }
- bool ecall_authenticate(clientid_t cid, unsigned char *auth_message)
- {
- bool ret;
- ret = g_ing.authenticate(cid, auth_message);
- return ret;
- }
- void Ingestion::initialize(uint32_t cnum, uint32_t cstart, sgx_aes_gcm_128bit_key_t &ESK) {
- clients.num = cnum;
- clients.start = cstart;
- clients.end = cnum + cstart;
- clients.keys = new sgx_aes_gcm_128bit_key_t[cnum];
- generateClientKeys(ESK);
- max_buffer_size = g_teems_config.m_priv_out * cnum;
- buffer = &(route_state.ingbuf);
- }
- bool Ingestion::authenticate(clientid_t cid, unsigned char *auth_message)
- {
- int auth_success = 0;
- unsigned long epoch_no = *((unsigned long*) auth_message);
- auth_message+=(sizeof(unsigned long));
- // Fetch corresponding client key
- clientid_t lcid = cid - g_ing.clients.start;
- sgx_aes_gcm_128bit_key_t &ckey = (g_ing.clients).keys[lcid];
- unsigned char computed_auth[SGX_AESGCM_KEY_SIZE];
- unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
- unsigned char iv[SGX_AESGCM_IV_SIZE];
- sgx_aes_gcm_128bit_tag_t mac;
- memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
- memset(iv, 0, SGX_AESGCM_IV_SIZE);
- sgx_status_t ret = SGX_SUCCESS;
- ret = sgx_rijndael128GCM_encrypt(&ckey, zeroes, SGX_AESGCM_KEY_SIZE,
- computed_auth, (unsigned char*) (&epoch_no), sizeof(epoch_no), NULL, 0, &mac);
- if(ret!=SGX_SUCCESS) {
- return -1;
- }
- auth_success = memcmp(auth_message, computed_auth, SGX_AESGCM_KEY_SIZE);
- if(auth_success == 0) {
- return true;
- } else {
- printf("authentication FAIL\n");
- return false;
- }
- }
- bool Ingestion::processMsgBundle(clientid_t cid, unsigned char *msgbundle,
- uint32_t num_msgs) {
- // Fetch corresponding client key
- clientid_t lcid = cid - g_ing.clients.start;
- sgx_aes_gcm_128bit_key_t &ckey = (g_ing.clients).keys[lcid];
- unsigned char *iv = msgbundle;
- msgbundle += SGX_AESGCM_IV_SIZE;
- uint16_t msg_size = g_teems_config.msg_size;
- uint32_t msgbundle_size = num_msgs * msg_size;
- unsigned char *dec_msgbundle = (unsigned char *) malloc (msgbundle_size);
- //sgx_aes_gcm_128bit_tag_t tag;
- //memcpy(tag, msgbundle + msgbundle_size, SGX_AESGCM_MAC_SIZE);
- sgx_aes_gcm_128bit_tag_t *tag = (sgx_aes_gcm_128bit_tag_t*) (msgbundle + msgbundle_size);
- sgx_status_t ret = sgx_rijndael128GCM_decrypt(&ckey, msgbundle, msgbundle_size,
- dec_msgbundle, iv, SGX_AESGCM_IV_SIZE, NULL, 0, tag);
- if(ret!=SGX_SUCCESS) {
- printf("Ingestion::processMsgBundle FAIL\n");
- printf("Error code: %d", (uint32_t) ret);
- }
- // Append msgbundle to g_ing.buffer;
- MsgBuffer &msg_queue = *(g_ing.buffer);
- pthread_mutex_lock(&msg_queue.mutex);
- uint32_t head = msg_queue.reserved;
- if (head + num_msgs > g_ing.max_buffer_size) {
- pthread_mutex_unlock(&msg_queue.mutex);
- printf("Ingestions: Max %u messages exceeded\n",
- g_ing.max_buffer_size);
- return false;
- }
- msg_queue.reserved += num_msgs;
- pthread_mutex_unlock(&msg_queue.mutex);
- memmove(msg_queue.buf + head * msg_size,
- dec_msgbundle, num_msgs * msg_size);
- pthread_mutex_lock(&msg_queue.mutex);
- msg_queue.inserted += num_msgs;
- pthread_mutex_unlock(&msg_queue.mutex);
- free(dec_msgbundle);
- return true;
- }
- void Ingestion::generateClientKeys(sgx_aes_gcm_128bit_key_t &ESK)
- {
- printf("In Ingestion::genCK, num_clients = %d, client_start = %d, client_end = %d\n",
- clients.num, clients.start, clients.end);
- for(uint32_t i=0; i<clients.num; i++)
- {
- unsigned char zeroes[SGX_AESGCM_KEY_SIZE];
- unsigned char iv[SGX_AESGCM_IV_SIZE];
- sgx_aes_gcm_128bit_tag_t tag;
- memset(zeroes, 0, SGX_AESGCM_KEY_SIZE);
- memset(iv, 0, SGX_AESGCM_IV_SIZE);
- uint32_t client_num = clients.start + i;
- memcpy(iv, (uint8_t*) (&client_num), sizeof(client_num));
- sgx_status_t ret = SGX_SUCCESS;
- ret = sgx_rijndael128GCM_encrypt((const sgx_aes_gcm_128bit_key_t *) (ESK),
- zeroes, SGX_AESGCM_KEY_SIZE, (uint8_t*) (clients.keys[i]), iv,
- SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
- if(ret!=SGX_SUCCESS) {
- printf("Ingestion::GCK FAIL\n");
- }
- }
- }
|