Bladeren bron

Obliviously sanitize padding messages before putting them in clients' mailboxes

For each message, if it is padding (the low DEST_UID_BITS of the
receiver id are all 1), the sender and destination ids are set to
0xffffffff and the priority (for public routing) and body are set to 0.
This is done obliviously as to whether the message is padding or not.
Ian Goldberg 11 maanden geleden
bovenliggende
commit
d4e3e7f793
1 gewijzigde bestanden met toevoegingen van 57 en 2 verwijderingen
  1. 57 2
      Enclave/storage.cpp

+ 57 - 2
Enclave/storage.cpp

@@ -219,6 +219,52 @@ bool generate_all_tokens() {
     return launch_all_users(generate_all_tokens_launch);
 }
 
+// Obliviously sanitize the given message by setting everything to 0
+// except the sender and receiver id, which are set to 0xffffffff,
+// if it is a padding message (the low DEST_UID_BITS of the receiver id
+// are all 1).  The function is oblivious as to whether the message is a
+// padding message.  msg_size must be a multiple of 16.
+// sender_id_offset should be set to 4 for private routine, or 8 for
+// public routing.
+static inline void padding_sanitize_msg(unsigned char *msg,
+    size_t msg_size, size_t sender_id_offset)
+{
+    static const uint32_t uid_mask = (1<<DEST_UID_BITS)-1;
+
+    // The first 4 bytes of the message are the receiver id
+    uint32_t receiver_id = *(uint32_t*)msg;
+
+    // The message is padding if receiver_id & uid_mask == uid_mask, or
+    // equivalently, (~receiver_id) & uid_mask == 0.
+
+    // So !((~receiver_id) & uid_mask) is 1 if this is a padding
+    // message, and 0 if not.  Then (!((~receiver_id) & uid_mask))-1 is
+    // 0 if this is a padding message, and -1 (0xffffffffffffffff) if
+    // not.  ANDing this mask with the body will do what we want: keep
+    // it unchanged if this is not padding, and set it to 0 if it is
+    // padding
+    uint64_t content_mask = (!((~receiver_id) & uid_mask))-1;
+
+    // Mask the first 16 bytes, which includes the 8 or 12 byte header
+    // (depending on private or public routing).  The first 4 bytes are
+    // the destination id; set those to 0xffffffff if this is a padding
+    // message.  Set the rest of the first 16 bytes to 0 if this is a
+    // padding message (all obliviously).
+    *(uint32_t*)(msg) |= uint32_t(~content_mask);
+    *(uint32_t*)(msg+4) &= uint32_t(content_mask);
+    *(uint64_t*)(msg+8) &= content_mask;
+
+    // Set the sender id to 0xffffffff if this is a padding message
+    *(uint32_t*)(msg+sender_id_offset) |= uint32_t(~content_mask);
+
+    // Set the rest of the message to 0 if this is a padding message
+    unsigned char *msgend = msg + msg_size;
+    for (msg = msg+16; msg<msgend; msg += 16) {
+        *(uint64_t*)(msg) &= content_mask;
+        *(uint64_t*)(msg+8) &= content_mask;
+    }
+}
+
 /* processMsgs
    - Take all the messages in storage_state.stg_buf
    - Encrypt them all with their corresponding client key and IV and store into
@@ -228,12 +274,16 @@ static void *processMsgs_launch(void *voidargs) {
     UserRange *args = (UserRange *)voidargs;
     uint32_t user_start = args->start;
     uint32_t user_end = args->start + args->num;
+    uint32_t msg_size = g_teems_config.msg_size;
 
     uint32_t mailbox_size;
+    size_t sender_id_offset;
     if (g_teems_config.private_routing) {
-        mailbox_size = g_teems_config.m_priv_in * g_teems_config.msg_size;
+        mailbox_size = g_teems_config.m_priv_in * msg_size;
+        sender_id_offset = 4;
     } else {
-        mailbox_size = g_teems_config.m_pub_in * g_teems_config.msg_size;
+        mailbox_size = g_teems_config.m_pub_in * msg_size;
+        sender_id_offset = 8;
     }
 
     uint32_t enc_mailbox_size = mailbox_size + SGX_AESGCM_IV_SIZE +
@@ -248,6 +298,11 @@ static void *processMsgs_launch(void *voidargs) {
 
     for(uint32_t lcid = user_start; lcid < user_end; lcid++) {
         memcpy(epoch_buf_ptr, clients[lcid].iv, SGX_AESGCM_IV_SIZE);
+        unsigned char *stg_buf_end = stg_buf_ptr + mailbox_size;
+        for (unsigned char *msg = stg_buf_ptr; msg < stg_buf_end;
+                msg += msg_size) {
+            padding_sanitize_msg(msg, msg_size, sender_id_offset);
+        }
         ret = sgx_rijndael128GCM_encrypt(&(clients[lcid].key), stg_buf_ptr,
             mailbox_size, (uint8_t*) epoch_buf_ct_ptr, epoch_buf_ptr,
             SGX_AESGCM_IV_SIZE, NULL, 0,