|
@@ -8,6 +8,8 @@
|
|
|
#define PROFILE_STORAGE
|
|
|
|
|
|
StgClient *clients;
|
|
|
+uint8_t *epoch_tokens;
|
|
|
+uint8_t *epoch_msgbundles;
|
|
|
|
|
|
static struct {
|
|
|
uint32_t max_users;
|
|
@@ -19,6 +21,62 @@ static struct {
|
|
|
std::vector<uint32_t> dest;
|
|
|
} storage_state;
|
|
|
|
|
|
+bool storage_generateClientKeys(uint32_t num_clients, uint32_t my_stg_no) {
|
|
|
+ uint16_t num_priv_channels = g_teems_config.m_priv_in;
|
|
|
+ uint16_t msg_size = g_teems_config.msg_size;
|
|
|
+ uint32_t pt_msgbundle_size = num_priv_channels * msg_size;
|
|
|
+
|
|
|
+ clients = new StgClient[num_clients];
|
|
|
+
|
|
|
+ for(uint32_t i =0; i < num_clients; i++) {
|
|
|
+ uint32_t mid = storage_state.my_storage_node_id + i;
|
|
|
+ clients[i].my_id = mid;
|
|
|
+ clients[i].priv_friends = new clientid_t[g_teems_config.m_priv_in];
|
|
|
+ // Initialize this client's private channel friends as themself
|
|
|
+ for(int j =0; j <g_teems_config.m_priv_in; j++) {
|
|
|
+ (clients[i].priv_friends)[j] = mid;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ uint32_t num_stg_nodes = g_teems_config.num_storage_nodes;
|
|
|
+ uint32_t c_simid = my_stg_no;
|
|
|
+
|
|
|
+ //printf("In Ingestion::genCK, num_clients = %d\n", num_clients);
|
|
|
+ for (uint32_t i=0; i<num_clients; i++) {
|
|
|
+ const sgx_aes_gcm_128bit_key_t *pESK = &(g_teems_config.ESK);
|
|
|
+ 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);
|
|
|
+
|
|
|
+ memcpy(iv, (uint8_t*) (&c_simid), sizeof(c_simid));
|
|
|
+ memcpy(iv + sizeof(c_simid), "STG", sizeof("STG"));
|
|
|
+
|
|
|
+ sgx_status_t ret = SGX_SUCCESS;
|
|
|
+ ret = sgx_rijndael128GCM_encrypt(pESK, zeroes, SGX_AESGCM_KEY_SIZE,
|
|
|
+ (uint8_t*) (clients[i].key), iv, SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
|
|
|
+ if(ret!=SGX_SUCCESS) {
|
|
|
+ printf("stg_generateClientKeys FAIL\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ if(c_simid % 10 == 0) {
|
|
|
+ printf("Storage: c_simid = %d, Key:", c_simid);
|
|
|
+ for (int k = 0; k<SGX_AESGCM_KEY_SIZE; k++) {
|
|
|
+ printf("%x", (clients[i].key)[k]);
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+ }
|
|
|
+ */
|
|
|
+ c_simid+=num_stg_nodes;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
// route_init will call this function; no one else should call it
|
|
|
// explicitly. The parameter is the number of messages that can fit in
|
|
|
// the storage-side MsgBuffer. Returns true on success, false on
|
|
@@ -41,7 +99,9 @@ bool storage_init(uint32_t max_users, uint32_t msg_buf_size)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ printf("my_stg_pos = %d\n", my_stg_pos);
|
|
|
storage_generateClientKeys(max_users, my_stg_pos);
|
|
|
+ // sendClientTokens();
|
|
|
|
|
|
return true;
|
|
|
}
|
|
@@ -189,6 +249,22 @@ void storage_received(MsgBuffer &storage_buf)
|
|
|
storage_buf.reset();
|
|
|
pthread_mutex_unlock(&storage_buf.mutex);
|
|
|
|
|
|
+ // TODO: Send the storage_state.stg_buf to clients
|
|
|
+ // Encrypt the storage_state.stg_buf into MSG_BUNDLES
|
|
|
+ // encryptMsgBundles(num_clients, num_priv_channels, msg_size);
|
|
|
+ // Uses: storage_state.stg_buf, clients, epoch_msgbundles
|
|
|
+
|
|
|
+ // generateTokens();
|
|
|
+ // Uses: clients, epoch_tokens,
|
|
|
+
|
|
|
+ // ocall_process_msgbundles(epoch_msgbundles, epoch_tokens);
|
|
|
+ // sendClientTokens();
|
|
|
+ //
|
|
|
+
|
|
|
+ // processStorage(msg_bundles) :
|
|
|
+ // a) Send out msg_bundles to clients with open sockets
|
|
|
+ // b) Store the other msg_bundles in "backend"
|
|
|
+
|
|
|
storage_state.stg_buf.reset();
|
|
|
|
|
|
#ifdef PROFILE_STORAGE
|
|
@@ -196,49 +272,131 @@ void storage_received(MsgBuffer &storage_buf)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-bool storage_generateClientKeys(uint32_t num_clients, uint32_t my_stg_no) {
|
|
|
|
|
|
- clients = new StgClient[num_clients];
|
|
|
- uint32_t num_stg_nodes = g_teems_config.num_storage_nodes;
|
|
|
- uint32_t c_simid = my_stg_no;
|
|
|
+/*
|
|
|
+ Given a local client identifier, generate the tokens for this client
|
|
|
+ for their priv_friends for the next round.
|
|
|
+ Populates the supplied tokens buffer with the correct tokens.
|
|
|
+*/
|
|
|
+bool generate_tokens(clientid_t lcid)
|
|
|
+{
|
|
|
+ uint32_t pt_tokens_size = (g_teems_config.m_priv_in * SGX_AESGCM_KEY_SIZE);
|
|
|
+ uint32_t enc_tokens_size = (g_teems_config.m_priv_in * SGX_AESGCM_KEY_SIZE) +
|
|
|
+ SGX_AESGCM_IV_SIZE + SGX_AESGCM_MAC_SIZE;
|
|
|
|
|
|
- //printf("In Ingestion::genCK, num_clients = %d\n", num_clients);
|
|
|
- for (uint32_t i=0; i<num_clients; i++) {
|
|
|
- const sgx_aes_gcm_128bit_key_t *pESK = &(g_teems_config.ESK);
|
|
|
- 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);
|
|
|
+ const sgx_aes_gcm_128bit_key_t *pTSK = &(g_teems_config.TSK);
|
|
|
+ unsigned char *tkn_iv_ptr = epoch_tokens + enc_tokens_size * lcid;
|
|
|
+ unsigned char *tkn_ptr = tkn_iv_ptr + SGX_AESGCM_IV_SIZE;
|
|
|
+ unsigned char *tkn_tag = tkn_ptr + pt_tokens_size;
|
|
|
|
|
|
- memcpy(iv, (uint8_t*) (&c_simid), sizeof(c_simid));
|
|
|
- memcpy(iv + sizeof(c_simid), "STG", sizeof("STG"));
|
|
|
+ // We construct the plaintext underlying the token in the correct location
|
|
|
+ // for this client in epoch_tokens
|
|
|
+ // The tokens get stored in token_body
|
|
|
+ // Later we encrypt token_body with the client's storage key and overwrite
|
|
|
+ // the correct location in epoch_tokens
|
|
|
+ unsigned char token_body[pt_tokens_size];
|
|
|
|
|
|
- sgx_status_t ret = SGX_SUCCESS;
|
|
|
- ret = sgx_rijndael128GCM_encrypt(pESK, zeroes, SGX_AESGCM_KEY_SIZE,
|
|
|
- (uint8_t*) (clients[i].key), iv, SGX_AESGCM_IV_SIZE, NULL, 0, &tag);
|
|
|
- if(ret!=SGX_SUCCESS) {
|
|
|
- printf("stg_generateClientKeys FAIL\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
+ memset(token_body, 0, pt_tokens_size);
|
|
|
+ memset(tkn_iv_ptr, 0, SGX_AESGCM_IV_SIZE);
|
|
|
+ // IV = client_id | epoch_no
|
|
|
+ memcpy(tkn_iv_ptr, (uint8_t*) (&(clients[lcid].my_id)), sizeof(clientid_t));
|
|
|
+ // TODO: Add epoch to IV
|
|
|
+ //memcpy(tkn_iv_ptr + sizeof(clientid_t), epoch_no, sizeof(epoch_no));
|
|
|
|
|
|
- /*
|
|
|
- if(c_simid % 10 == 0) {
|
|
|
- printf("Storage: c_simid = %d, Key:", c_simid);
|
|
|
- for (int k = 0; k<SGX_AESGCM_KEY_SIZE; k++) {
|
|
|
- printf("%x", (clients[i].key)[k]);
|
|
|
- }
|
|
|
- printf("\n");
|
|
|
+ unsigned char *ptr = tkn_ptr;
|
|
|
+ for(int i = 0; i<g_teems_config.m_priv_in; i++)
|
|
|
+ {
|
|
|
+ memcpy(ptr, (uint8_t*) (&(clients[lcid].my_id)), sizeof(clientid_t));
|
|
|
+ memcpy(ptr + sizeof(clientid_t), (uint8_t*) (&(clients[lcid].priv_friends[i])), sizeof(clientid_t));
|
|
|
+ ptr+=SGX_AESGCM_KEY_SIZE;
|
|
|
+ }
|
|
|
+
|
|
|
+ sgx_status_t ret = SGX_SUCCESS;
|
|
|
+ ret = sgx_rijndael128GCM_encrypt(pTSK, tkn_ptr, pt_tokens_size,
|
|
|
+ (uint8_t*) token_body, tkn_iv_ptr, SGX_AESGCM_IV_SIZE, NULL, 0,
|
|
|
+ (sgx_aes_gcm_128bit_tag_t*) tkn_tag);
|
|
|
+ if(ret!=SGX_SUCCESS) {
|
|
|
+ printf("generate_tokens: Creating token FAIL\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ if(lcid == 0) {
|
|
|
+ printf("Checking generated token_body:");
|
|
|
+ for(uint32_t i = 0; i < pt_tokens_size; i++) {
|
|
|
+ printf("%x", token_body[i]);
|
|
|
}
|
|
|
- */
|
|
|
- c_simid+=num_stg_nodes;
|
|
|
+ printf("\n");
|
|
|
+ }
|
|
|
+ */
|
|
|
+
|
|
|
+ unsigned char *cl_iv = clients[lcid].iv;
|
|
|
+ ret = (sgx_rijndael128GCM_encrypt(&(clients[lcid].key), token_body, pt_tokens_size,
|
|
|
+ (uint8_t*) tkn_ptr, cl_iv, SGX_AESGCM_IV_SIZE, NULL, 0,
|
|
|
+ (sgx_aes_gcm_128bit_tag_t*) tkn_tag));
|
|
|
+ if(ret!=SGX_SUCCESS) {
|
|
|
+ printf("generate_tokens: Encrypting token FAIL\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
+ memcpy(tkn_iv_ptr, cl_iv, SGX_AESGCM_IV_SIZE);
|
|
|
+ // Update IV
|
|
|
+ uint64_t *iv_ctr = (uint64_t*) cl_iv;
|
|
|
+ (*iv_ctr)+=1;
|
|
|
+
|
|
|
+ /*
|
|
|
+ if(lcid == 0) {
|
|
|
+ printf("Encrypted client token:");
|
|
|
+ for(uint32_t i = 0; i < pt_tokens_size; i++) {
|
|
|
+ printf("%x", tkn_ptr[i]);
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
}
|
|
|
+ */
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-bool authenticateClient()
|
|
|
+
|
|
|
+bool ecall_storage_authenticate(clientid_t cid, unsigned char *auth_message)
|
|
|
{
|
|
|
+ bool ret = false;
|
|
|
+ uint32_t lcid = cid / g_teems_config.num_storage_nodes;
|
|
|
+ const sgx_aes_gcm_128bit_key_t *ckey = &(clients[lcid].key);
|
|
|
+
|
|
|
+ ret = authenticateClient(auth_message, ckey);
|
|
|
|
|
|
+ if(!ret) {
|
|
|
+ printf("Storage authentication FAIL\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ // When clients authenticate:
|
|
|
+ // 1) Send back mailbox archive
|
|
|
+ // 2) Send tokens for current epoch
|
|
|
+
|
|
|
+ ret &= generate_tokens(lcid);
|
|
|
+ /*
|
|
|
+ uint32_t enc_token_bundle_size = SGX_AESGCM_KEY_SIZE + SGX_AESGCM_IV_SIZE
|
|
|
+ unsigned char *token_ptr = lcid *
|
|
|
+ // sendTokens()
|
|
|
+
|
|
|
+ */
|
|
|
+ /*
|
|
|
+ if(ret) {
|
|
|
+ printf("ecall_storage_auth : ret = SUCCESS\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ printf("ecall_storage_auth : ret = FAIL\n");
|
|
|
+ }
|
|
|
+ */
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void ecall_supply_storage_buffers(unsigned char *msgbundles,
|
|
|
+ uint32_t msgbundles_size, unsigned char *tokens, uint32_t tokens_size)
|
|
|
+{
|
|
|
+ epoch_msgbundles = msgbundles;
|
|
|
+ epoch_tokens = tokens;
|
|
|
+ printf("Storage buffers were set");
|
|
|
}
|