Browse Source

Initial commit for all files common to verifier, Apache

Miti Mazmudar 2 years ago
commit
f6d47ba3ba
6 changed files with 657 additions and 0 deletions
  1. 117 0
      LAInitiator/LA.cpp
  2. 376 0
      LAInitiator/Transforms.cpp
  3. 70 0
      ProtobufMessageRW.cpp
  4. 23 0
      header_files/LA.h
  5. 19 0
      header_files/ProtobufMessageRW.h
  6. 52 0
      header_files/Transforms.h

+ 117 - 0
LAInitiator/LA.cpp

@@ -0,0 +1,117 @@
+#include "sgx_eid.h"
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+
+#include <stdio.h>
+
+#include "sgx_trts.h"
+#include "sgx_utils.h"
+#include "error_codes.h"
+#include "sgx_ecp_types.h"
+#include "sgx_thread.h"
+#include "sgx_tcrypto.h"
+#include "datatypes.h"
+#include "Transforms.h"
+#define MAX_SESSION_COUNT  16
+#define SGX_CAST(type, item) ((type)(item))
+#include <string.h>
+#include "crypto.h"
+#include "LA.h"
+uint32_t LA::process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_sgx_dh_msg1_t& protobuf_msg1, protobuf_sgx_dh_msg2_t& protobuf_msg2)
+{
+  sgx_dh_msg1_t dh_msg1;            //Diffie-Hellman Message 1
+  sgx_dh_msg2_t dh_msg2;
+  memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
+  uint32_t ret_status;
+
+  if(Transforms::decode_msg1_from_protobuf(protobuf_msg1, &dh_msg1)!=0)
+    return 0x1;
+
+  //Intialize the session as a session initiator
+  sgx_dh_session = (sgx_dh_session_t*) malloc(sizeof(sgx_dh_session_t));
+  ret_status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, sgx_dh_session);
+  if(ret_status != SGX_SUCCESS)
+    return ret_status;
+
+  //Process the message 1 obtained from desination enclave and generate message 2
+  ret_status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, sgx_dh_session);
+  if(ret_status != SGX_SUCCESS)
+    return ret_status;
+
+  Transforms::encode_msg2_to_protobuf(protobuf_msg2, &dh_msg2);
+  return 0;
+}
+
+uint32_t LA::process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3) {
+
+  uint32_t ret_status;
+  sgx_dh_msg3_t dh_msg3;
+  sgx_key_128bit_t dh_aek;        // Session Key
+  sgx_dh_session_enclave_identity_t responder_identity;
+
+  memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
+
+  if(Transforms::decode_msg3_from_protobuf(protobuf_msg3, &dh_msg3)!=0)
+    return -1;
+
+  //Process Message 3 obtained from the destination enclave
+  ret_status = sgx_dh_initiator_proc_msg3(&dh_msg3, sgx_dh_session, &dh_aek, &responder_identity);
+  if(SGX_SUCCESS != ret_status)
+    return ret_status;
+
+  //memcpy(global_session_info.active.AEK, &dh_aek, sizeof(sgx_key_128bit_t));
+  memcpy(key, &dh_aek, sizeof(sgx_key_128bit_t));
+  /*global_session_info.session_id = 1; // TODO: session_id;
+  global_session_info.active.counter = 0;
+  global_session_info.status = ACTIVE;
+   */
+  memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
+
+  return 0;
+}
+
+uint32_t LA::conduct_la(int decryptor_fd) {
+    // declare msg1, msg2, msg3 protobuf objects
+    protobuf_sgx_dh_msg1_t protobuf_msg1;
+    protobuf_sgx_dh_msg2_t protobuf_msg2;
+    protobuf_sgx_dh_msg3_t protobuf_msg3;
+    uint32_t protobuf_sgx_ret;
+
+    setbuf(stdout,NULL);
+
+    protobufReaderWriter.set_fd(decryptor_fd);
+
+    printf("Reading message 1\n"); fflush(stdout);
+    if(protobufReaderWriter.read_msg(protobuf_msg1)!=0)
+        return -1;
+
+    printf("Generating message 2\n"); fflush(stdout);
+    protobuf_sgx_ret = process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_msg1, protobuf_msg2);
+    if(protobuf_sgx_ret != 0)
+    {
+        printf("Error in process_protobuf_dh_msg1_generate_protobuf_dh_msg2: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
+    }
+
+    printf("Writing message 2\n"); fflush(stdout);
+    if(protobufReaderWriter.write_msg(protobuf_msg2)!=0)
+        return -1;
+
+    printf("Reading message 3\n"); fflush(stdout);
+    if(protobufReaderWriter.read_msg(protobuf_msg3)!=0)
+        return -1;
+
+    printf("Processing message 3\n"); fflush(stdout);
+    protobuf_sgx_ret = process_protobuf_dh_msg3(protobuf_msg3);
+    if(protobuf_sgx_ret != 0)
+    {
+        printf("Error in process_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
+    }
+    return 0;
+}
+
+void LA::get_la_symmetric_key(uint8_t* op_key)
+{
+    uint32_t counter;
+    for(counter=0; counter<16; counter++)
+        op_key[counter] = key[counter];
+}

+ 376 - 0
LAInitiator/Transforms.cpp

@@ -0,0 +1,376 @@
+#include "Transforms.h"
+int Transforms::fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
+    {
+        if(temp32 > UINT8_MAX)
+            return -1;
+        else
+        {
+            //		 *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
+            *temp8 = (uint8_t)temp32;
+            return 0;
+        }
+    }
+
+int Transforms::fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
+    {
+        if(temp32 > UINT16_MAX)
+            return -1;
+        else
+        {
+            //              *temp8 = *(uint8_t*)&temp32; // Probably works irrespective of endianness but not sure.
+            *temp16 = (uint16_t)temp32;
+            return 0;
+        }
+    }
+
+void Transforms::encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
+    {
+        printf("\n ec256 public key gx and gy \n");
+        int counter; google::protobuf::uint32 temp32;
+        for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
+        {
+            temp32 = g_a->gx[counter];
+            protobuf_g_a->add_gx(temp32);
+            printf("%d ", temp32);
+            temp32 = g_a->gy[counter];
+            protobuf_g_a->add_gy(temp32);
+            printf("%d ", temp32);
+        }
+        printf("\n");
+    }
+
+void Transforms::encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
+    {
+        protobuf_attributes->set_flags(attributes->flags); // 64 bit
+        protobuf_attributes->set_xfrm(attributes->xfrm); // 64 bit
+    }
+
+void Transforms::encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
+    {
+        printf("\n report \n key id \n");
+        int counter; google::protobuf::uint32 temp32;
+        for(counter=0;counter<SGX_KEYID_SIZE;counter++)
+        {
+            temp32=report->key_id.id[counter];
+            protobuf_report->add_key_id(temp32);
+            printf("%d ",temp32);
+        }
+
+        printf("\n mac \n");
+        for(counter=0;counter<SGX_MAC_SIZE;counter++)
+        {
+            temp32=report->mac[counter];
+            protobuf_report->add_mac(temp32);
+            printf("%d ", temp32);
+        }
+
+        protobuf_report->mutable_body()->set_misc_select(report->body.misc_select); // 32 bit
+        protobuf_report->mutable_body()->set_isv_svn(report->body.isv_svn); // 16 bit
+        protobuf_report->mutable_body()->set_isv_prod_id(report->body.isv_prod_id); // 16 bit
+        encode_attributes_to_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
+
+        for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
+        {
+            temp32=report->body.cpu_svn.svn[counter];
+            protobuf_report->mutable_body()->add_cpu_svn(temp32);
+        }
+
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
+        {
+            temp32=report->body.reserved1[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
+            protobuf_report->mutable_body()->add_reserved1(temp32);
+        }
+
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
+        {
+            temp32=report->body.reserved2[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
+            protobuf_report->mutable_body()->add_reserved2(temp32);
+        }
+
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
+        {
+            temp32=report->body.reserved3[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
+            protobuf_report->mutable_body()->add_reserved3(temp32);
+        }
+
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
+        {
+            temp32=report->body.reserved4[counter]; // TODO: Could be optimized out - if these are determined to be 0s.
+            protobuf_report->mutable_body()->add_reserved4(temp32);
+        }
+
+        printf("\nmr enclave\n");
+        fflush(stdout);
+        for(counter=0;counter<SGX_HASH_SIZE;counter++)
+        {
+            temp32=report->body.mr_enclave.m[counter];
+            protobuf_report->mutable_body()->add_mr_enclave(temp32);
+            printf("%x ", temp32);
+        }
+
+        printf("\n mr signer\n");  fflush(stdout);
+        for(counter=0;counter<SGX_HASH_SIZE;counter++)
+        {
+            temp32=report->body.mr_signer.m[counter];
+            protobuf_report->mutable_body()->add_mr_signer(temp32);
+            printf("%x ", temp32);
+        }
+
+
+        for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
+        {
+            temp32=report->body.report_data.d[counter];
+            protobuf_report->mutable_body()->add_report_data(temp32);
+        }
+    }
+
+int Transforms::decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
+    {
+        attributes->flags = protobuf_attributes->flags();
+        printf("\n flags %" PRIu64 " \n", attributes->flags);
+        attributes->xfrm = protobuf_attributes->xfrm();
+        printf("\n xfrm %" PRIu64 " \n", attributes->xfrm);
+        return 0;
+    }
+
+int Transforms::decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
+    {
+        int counter; google::protobuf::uint32 temp32;
+        printf("\n----------------------Decoding received msg3 ------------------------\n");
+        printf("\nreport body keyid\n");
+        for(counter=0;counter<SGX_KEYID_SIZE;counter++)
+        {
+            temp32=protobuf_report->key_id(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->key_id.id[counter]))!=0)
+                return -1;
+            printf("%d ", report->key_id.id[counter]);
+        }
+
+        printf("\nreport mac\n");
+        for(counter=0;counter<SGX_MAC_SIZE;counter++)
+        {
+            temp32=protobuf_report->mac(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->mac[counter]))!=0)
+                return -1;
+            printf("%d ", report->mac[counter]);
+        }
+
+        report->body.misc_select=protobuf_report->mutable_body()->misc_select(); // 32 bit
+        temp32=protobuf_report->mutable_body()->isv_svn();
+        if(fit_32_into_uint16_t(temp32, &(report->body.isv_svn))!=0)
+            return -1;
+        printf("\nmisc select %d \n", report->body.misc_select);
+
+        temp32=protobuf_report->mutable_body()->isv_prod_id();
+        if(fit_32_into_uint16_t(temp32, &(report->body.isv_prod_id))!=0)
+            return -1;
+        printf("\nprod id %d \n", report->body.isv_prod_id);
+
+        decode_attributes_from_protobuf(protobuf_report->mutable_body()->mutable_attributes(), &(report->body.attributes));
+
+        printf("\n cpu svn\n");
+        for(counter=0;counter<SGX_CPUSVN_SIZE;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->cpu_svn(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.cpu_svn.svn[counter]))!=0)
+                return -1;
+            printf("%d ", report->body.cpu_svn.svn[counter]);
+
+        }
+
+        printf("\n reserved1 \n");
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED1;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->reserved1(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.reserved1[counter]))!=0)
+                return -1;
+            printf("%d ", report->body.reserved1[counter]);
+        }
+
+        printf("\n reserved2 \n");
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED2;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->reserved2(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.reserved2[counter]))!=0)
+                return -1;
+            printf("%d ", report->body.reserved2[counter]);
+        }
+
+        printf("\n reserved3 \n");
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED3;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->reserved3(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.reserved3[counter]))!=0)
+                return -1;
+            printf("%d ", report->body.reserved3[counter]);
+        }
+
+        printf("\n reserved4 \n");
+        for(counter=0;counter<SGX_REPORT_BODY_RESERVED4;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->reserved4(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.reserved4[counter]))!=0)
+                return -1;
+            printf("%d ", report->body.reserved4[counter]);
+
+        }
+
+        printf("\n mrenclave \n");
+        for(counter=0;counter<SGX_HASH_SIZE;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->mr_enclave(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.mr_enclave.m[counter]))!=0)
+                return -1;
+            printf("%x ", report->body.mr_enclave.m[counter]);
+        }
+
+        printf("\n mrsigner \n");
+        for(counter=0;counter<SGX_HASH_SIZE;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->mr_signer(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.mr_signer.m[counter]))!=0)
+                return -1;
+            printf("%x ", report->body.mr_signer.m[counter]);
+        }
+
+        printf("\n report data\n");
+        for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
+        {
+            temp32=protobuf_report->mutable_body()->report_data(counter);
+            if(fit_32_into_uint8_t(temp32, &(report->body.report_data.d[counter]))!=0)
+                return -1;
+            printf("%d ", report->body.report_data.d[counter]);
+        }
+        printf("\n------------------------ end of msg3 --------------------------\n");
+        return 0;
+    }
+
+int Transforms::print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1)
+    {
+        int counter;
+        printf("gx\n");
+        for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
+        {
+            printf("%d ", protobuf_dhmsg1.g_a().gx(counter));
+        }
+
+        printf("\ngy\n");
+        for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
+        {
+            printf("%d ", protobuf_dhmsg1.g_a().gy(counter));
+        }
+
+        printf("\nmrenclave in target\n");
+        for(counter=0;counter<SGX_HASH_SIZE;counter++)
+        {
+            printf("%" PRIu32 " ", protobuf_dhmsg1.target().mr_enclave(counter));
+        }
+
+        printf("\nreserved1 in target\n");
+        for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
+        {
+            printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved1(counter));
+        }
+
+        printf("\nreserved2 in target\n");
+        for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
+        {
+            printf("%" PRIu32 " ", protobuf_dhmsg1.target().reserved2(counter));
+        }
+
+        printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().flags());
+        printf("\n %" PRIu64 "\n", protobuf_dhmsg1.target().attributes().xfrm());
+        printf("\n %" PRIu32 "\n", protobuf_dhmsg1.target().misc_select());
+
+        return 0;
+    }
+
+int Transforms::decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a)
+{
+    printf("\n ec256 pub key\n");
+    int counter; google::protobuf::uint32 temp32;
+    for(counter=0;counter<SGX_ECP256_KEY_SIZE;counter++)
+    {
+        temp32 = protobuf_g_a->gx(counter);
+        if(fit_32_into_uint8_t(temp32, &(g_a->gx[counter]))!=0)
+            return -1;
+        printf("%02x ",g_a->gx[counter]);
+        temp32 = protobuf_g_a->gy(counter);
+        if(fit_32_into_uint8_t(temp32, &(g_a->gy[counter]))!=0)
+            return -1;
+        printf("%02x ",g_a->gy[counter]);
+    }
+    return 0;
+}
+
+int Transforms::decode_msg1_from_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1)
+    {
+        int counter; google::protobuf::uint32 temp32;// google::protobuf::uint64 temp64;
+
+        if(decode_ec256_public_key_from_protobuf(protobuf_dhmsg1.mutable_g_a(), &(native_dhmsg1->g_a)) != 0)
+            return 0x1;
+
+        for(counter=0;counter<SGX_HASH_SIZE;counter++)
+        {
+            temp32 = protobuf_dhmsg1.mutable_target()->mr_enclave(counter);
+            if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.mr_enclave.m[counter]))!=0)
+                return -1;
+        }
+
+        for(counter=0;counter<SGX_TARGET_INFO_RESERVED1_BYTES;counter++)
+        {
+            temp32 = protobuf_dhmsg1.mutable_target()->reserved1(counter);
+            if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.reserved1[counter]))!=0)
+                return -1;
+        }
+
+        for(counter=0;counter<SGX_TARGET_INFO_RESERVED2_BYTES;counter++)
+        {
+            temp32 = protobuf_dhmsg1.mutable_target()->reserved2(counter);
+            if(fit_32_into_uint8_t(temp32, &(native_dhmsg1->target.reserved2[counter]))!=0)
+                return -1;
+        }
+
+        native_dhmsg1->target.attributes.flags = protobuf_dhmsg1.mutable_target()->mutable_attributes()->flags();
+        native_dhmsg1->target.attributes.xfrm = protobuf_dhmsg1.mutable_target()->mutable_attributes()->xfrm();
+        native_dhmsg1->target.misc_select = protobuf_dhmsg1.mutable_target()->misc_select();
+        return 0;
+    }
+
+int Transforms::decode_msg3_from_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3)
+    {
+        int counter; google::protobuf::uint32 temp32;
+        for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
+        {
+            temp32=protobuf_dhmsg3.cmac(counter);
+            if(fit_32_into_uint8_t(temp32, &(native_dhmsg3->cmac[counter]))!=0)
+                return -1;
+        }
+
+        if(decode_report_from_protobuf(protobuf_dhmsg3.mutable_msg3_body()->mutable_report(), &(native_dhmsg3->msg3_body.report))==-1)
+            return -1;
+        int max_counter=protobuf_dhmsg3.mutable_msg3_body()->additional_prop_size();
+        native_dhmsg3->msg3_body.additional_prop_length=max_counter;
+        // TODO: Need to assign a variable on the heap and then pass it as an argument to this function - set it to null if protobuf_dhmsg3.mutable_msg3_body()->additional_prop_size() is 0
+        // TODO: And then free it in that function (create_session) when it is done. It is likely that it is 0 in the SGX SDK sample code. And SDK people probably didn't deserialize it - as it may contain a pointer in the general case - to the array of additional_properties.
+        if(max_counter!=0)
+            return -1;
+        return 0;
+    }
+
+void Transforms::encode_msg2_to_protobuf( protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2)
+    {
+        int counter; google::protobuf::uint32 temp32; //google::protobuf::uint64 temp64;
+        printf("\n msg2 cmac \n");
+        for(counter=0;counter<SGX_DH_MAC_SIZE;counter++)
+        {
+            temp32=native_dhmsg2->cmac[counter];
+            protobuf_dhmsg2.add_cmac(temp32);
+            printf("%d ", temp32);
+        }
+
+        encode_ec256_public_key_to_protobuf(protobuf_dhmsg2.mutable_g_b(), &(native_dhmsg2->g_b));
+
+        encode_report_to_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report));
+    }
+

+ 70 - 0
ProtobufMessageRW.cpp

@@ -0,0 +1,70 @@
+//
+// Created by miti on 2019-12-24.
+//
+
+#include "ProtobufMessageRW.h"
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+using namespace google::protobuf::io;
+
+int ProtobufMessageRW::read_msg(google::protobuf::MessageLite& message)
+{
+    if(fd < 0)
+    {
+        printf("Need to call set_fd on this object first, to set the fd to a non-negative number.\n");
+        fflush(stdout);
+        return 0x2;
+    }
+    ZeroCopyInputStream* raw_input;
+    CodedInputStream* coded_input;
+    uint32_t size;
+    CodedInputStream::Limit limit;
+    raw_input = new FileInputStream(fd);
+    coded_input = new CodedInputStream(raw_input);
+    if(!coded_input->ReadVarint32(&size))
+    {
+        printf("Error in reading size of msg");
+        fflush(stdout);
+        return -1;
+    }
+    //printf("size of msg was read to be %" PRIu32 " \n", size);
+    fflush(stdout);
+    limit = coded_input->PushLimit(size);
+    if(!message.ParseFromCodedStream(coded_input))
+    {
+        printf("Error in parsing msg");
+        fflush(stdout);
+        return -1;
+    }
+    coded_input->PopLimit(limit);
+    return 0;
+}
+
+int ProtobufMessageRW::write_msg(google::protobuf::MessageLite& message)
+{
+    if(fd < 0)
+    {
+        printf("Need to call set_fd on this object first, to set the fd to a non-negative number.\n");
+        fflush(stdout);
+        return 0x2;
+    }
+    ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
+    CodedOutputStream* coded_output  = new CodedOutputStream(raw_output);
+    coded_output->WriteVarint32(message.ByteSize());
+    if(!message.SerializeToCodedStream(coded_output))
+    {
+        printf("SerializeToCodedStream failed");
+        fflush(stdout);
+        return -1;
+    }
+    // As per this - https://stackoverflow.com/questions/22881876/protocol-buffers-how-to-serialize-and-deserialize-multiple-messages-into-a-file?noredirect=1&lq=1
+    // TODO: There may be a better way to do this - 1) this happens with every accept now and 2) make it happen on the stack vs heap - destructor will be called on return from this function (main) and the items will then be written out. (We probably don't want that, actually)
+    delete coded_output;
+    delete raw_output;
+    fflush(stdout);
+    return 0;
+}
+
+void ProtobufMessageRW::set_fd(int given_fd) {
+    fd = given_fd;
+}

+ 23 - 0
header_files/LA.h

@@ -0,0 +1,23 @@
+//
+// Created by miti on 2019-12-21.
+//
+#ifndef VERIFIER_LA_H
+#define VERIFIER_LA_H
+#include "ProtobufMessageRW.h"
+#include "ProtobufLAMessages.pb.h"
+
+struct _sgx_dh_session_t;
+typedef _sgx_dh_session_t sgx_dh_session_t;
+
+class LA {
+    sgx_dh_session_t* sgx_dh_session;
+    // dh_session_t global_session_info;
+    ProtobufMessageRW protobufReaderWriter;
+    uint8_t key[16];
+    uint32_t process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3);
+    uint32_t process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_sgx_dh_msg1_t& protobuf_msg1, protobuf_sgx_dh_msg2_t& protobuf_msg2);
+public:
+    uint32_t conduct_la(int fd);
+    void get_la_symmetric_key(uint8_t* key);
+};
+#endif //LAINITIATOR_PROTOBUFINTERFACE_H

+ 19 - 0
header_files/ProtobufMessageRW.h

@@ -0,0 +1,19 @@
+//
+// Created by miti on 2019-12-24.
+//
+
+#ifndef VERIFIER_PROTOBUFMESSAGERW_H
+#define VERIFIER_PROTOBUFMESSAGERW_H
+//using namespace google::protobuf;
+#include <google/protobuf/message_lite.h>
+
+class ProtobufMessageRW {
+    int fd;
+public:
+    int read_msg(google::protobuf::MessageLite& message);
+    int write_msg(google::protobuf::MessageLite& message);
+    void set_fd(int fd);
+};
+
+
+#endif //VERIFIER_PROTOBUFMESSAGERW_H

+ 52 - 0
header_files/Transforms.h

@@ -0,0 +1,52 @@
+//
+// Created by miti on 2019-12-21.
+//
+
+#ifndef VERIFIER_INITIATOR_TRANSFORMS_H
+#define VERIFIER_INITIATOR_TRANSFORMS_H
+#include "sgx_eid.h"
+#include "error_codes.h"
+#include "datatypes.h"
+#include "sgx_urts.h"
+#include "sgx_dh.h"
+
+// For msg1
+#define SGX_TARGET_INFO_RESERVED1_BYTES 4
+#define SGX_TARGET_INFO_RESERVED2_BYTES 456
+#define SGX_ECP256_KEY_SIZE 32
+#define SGX_HASH_SIZE 32 /* SHA256 */
+// For msg2
+#define SGX_REPORT_DATA_SIZE 64
+#define SGX_KEYID_SIZE 32
+#define SGX_DH_MAC_SIZE 16
+#define SGX_REPORT_BODY_RESERVED1 28
+#define SGX_REPORT_BODY_RESERVED2 32
+#define SGX_REPORT_BODY_RESERVED3 96
+#define SGX_REPORT_BODY_RESERVED4 60
+
+
+#include <stdio.h>
+// For google protobufs and deserialization/serialization
+#include "ProtobufLAMessages.pb.h"
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+using namespace google::protobuf::io;
+#include <inttypes.h>
+
+
+class Transforms {
+    static int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8);
+    static int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16);
+    static void  encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a);
+    static int decode_ec256_public_key_from_protobuf(protobuf_sgx_ec256_public_t* protobuf_g_a , sgx_ec256_public_t* g_a);
+    static void  encode_attributes_to_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes);
+    static void  encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report);
+    static int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes);
+    static int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report);
+public:
+    static int decode_msg1_from_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1);
+    static int decode_msg3_from_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3);
+    static void  encode_msg2_to_protobuf( protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2);
+    static int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1);
+};
+#endif //VERIFIER_INITIATOR_TRANSFORMS_H