Browse Source

Refactored code + added in RSA key generation + sha256 hash of RSA public key which is in little endian format

dettanym 4 years ago
parent
commit
8cf97b17ce

+ 8 - 31
Makefile

@@ -4,17 +4,15 @@ CXX_FLAGS       = -Wall  -O2 -std=c++11 -fpic -fdata-sections -ffunction-section
 LD              = ${CXX} -v
 LD_FLAGS        = -Wall -shared -O2
 
-#all_non_sgx_cpp := $(filter-out((wildcard *.cpp), $(sgx_cpp))) #main.cpp ipc.cpp SealerWrapper.cpp ProtobufLAMessages.cpp ProtobufSealerMessages.cpp
 all_non_sgx_obj := $(system_obj) $(protobuf_obj) #$(patsubst %.cpp,%.o,$(all_non_sgx_cpp)}
-#main_system_and_wrapper_cpp			= $(filter-out(wildcard *.cpp, $(sgx_cpp) $(protobuf_cpp) $(protobuf_sgx_cpp)))
-#sgx_cpp = $(wildcard Sgx*)
+
 all_non_sgx_cpp := $(patsubst %.o,%.cpp,$(all_non_sgx_obj))
 sgx_protobuf_obj := $(patsubst %.cpp,%.o,$(wildcard SgxProtobuf*))
 sgx_obj := $(patsubst %.cpp,%.o,$(wildcard Sgx*))
-#sgx_only_obj := $(filter-out($(sgx_obj), $(sgx_protobuf_obj)))
+
 protobuf_obj := $(patsubst %.cpp,%.o,$(wildcard Protobuf*))
 system_obj := $(patsubst %.cpp,%.o,$(wildcard system*))
-OBJECTS := systemMain.o systemIpc.o systemSealerWrapper.o SgxCrypto.o SgxSealer.o SgxLAInitiator.o ${sgx_protobuf_obj} ${protobuf_obj} #$(system_obj) $(sgx_only_obj) $(sgx_protobuf_obj) $(protobuf_obj)
+OBJECTS := systemMain.o systemSealerWrapper.o SgxSealer.o ProtobufLAInitiator.o SgxProtobufLAInitiator.o SgxProtobufLAInitiator_Transforms.o ProtobufLAMessages.pb.o crypto.o 
 
 SGX_SDK := /home/m2mazmud/sgx2.1_installation/sgxsdk
 SGX_COMMON_CFLAGS := -m64
@@ -23,48 +21,27 @@ Trts_Library_Name := sgx_trts
 Service_Library_Name := sgx_tservice
 Crypto_Library_Name := sgx_tcrypto
 SGX_INCLUDE_PATHS := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx
+Openssl_Path := /home/m2mazmud/plain-openssl
 
 Uae_Library_Name := sgx_uae_service
 
-
 all:	verifier ${OBJECTS}
 
-#${RESULT}: ${OBJECTS} ${LocalAttestationCode_objects} ${LocalAttestationCode_ipc_objects} ${LocalAttestationCode_serialization_objects}
-#	$(LD) $(LD_FLAGS) -o $@ $^ -Wl,--no-undefined -lphpcpp -lprotobuf -L./  -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive -L$(SGX_LIBRARY_PATH)  -l$(Service_Library_Name) -l$(Crypto_Library_Name) -lsgx_tstdc -Wl,-Bsymbolic -Wl,-pie,-eenclave_entry -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections
-
 clean:
 	${RM} *.obj *~* ${OBJECTS} verifier #${LocalAttestationCode_objects} ${LocalAttestationCode_ipc_objects} ${LocalAttestationCode_serialization_objects}
 
-#SgxLAInititator.o and SgxSealer.o
-#SgxLAInititator.o: SgxLAInititator.cpp
-#	${CXX} -I./include -I$(SGX_SDK)/include ${CXX_FLAGS} -c  $^ -o $@ #TODO: Else try sgx_include_paths
-#SgxSealer.o: SgxSealer.cpp
-#	${CXX} -I./include -I$(SGX_SDK)/include ${CXX_FLAGS} -c $^ -o $@
-
-
-#ProtobufSgxLATransforms_Inititator.o and ProtobufSgxSealerTransforms.o
-#ProtobufSgxLATransforms_Inititator.o: ProtobufSgxLATransforms_Inititator.cpp
-#	${CXX} -I./include -I$(SGX_SDK)/include  ${CXX_FLAGS} -c $^ -o $@
-#ProtobufSgxSealerTransforms.o: ProtobufSgxSealerTransforms.cpp
-#	${CXX} -I./include -I$(SGX_SDK)/include ${CXX_FLAGS} -c $^ -o $@
 Sgx%.o: Sgx%.cpp
 	${CXX} -I./include -I$(SGX_SDK)/include ${CXX_FLAGS} -c $^ -o $@
-#SgxSealer.o: SgxSealer.cpp
-#        ${CXX} -I./include -I$(SGX_SDK)/include ${CXX_FLAGS} -c $^ -o $@
-#SgxLAInititator.o: SgxLAInititator
-
 
-#$(all_non_sgx_obj): $(all_non_sgx_cpp)
-#	${CXX} -I./include ${CXX_FLAGS} -c $^ -o $@
 Protobuf%.o: Protobuf%.cpp
 	${CXX} -I./include ${CXX_FLAGS} -c $^ -o $@
 
 system%.o: system%.cpp
 	gcc -I./include ${CXX_FLAGS} -c $^ -o $@
 
-#system%.o: system%.s
-#	gcc ${CXX_FLAGS} -o $@ $^
+crypto.o: crypto.cpp 
+	gcc -I${Openssl_Path}/include ${CXX_FLAGS} -c $^ -o $@
 
-verifier:  systemMain.o systemIpc.o systemSealerWrapper.o  SgxSealer.o SgxLAInitiator.o ${sgx_protobuf_obj} ${protobuf_obj}
+verifier:  systemMain.o systemSealerWrapper.o SgxSealer.o ProtobufLAInitiator.o SgxProtobufLAInitiator.o SgxProtobufLAInitiator_Transforms.o ProtobufLAMessages.pb.o crypto.o
 #	echo $(system_obj)
-	${CXX} ${CXX_FLAGS} systemMain.o systemIpc.o systemSealerWrapper.o SgxSealer.o SgxLAInitiator.o ${sgx_protobuf_obj} -L./ -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive  -L$(SGX_LIBRARY_PATH)  -l$(Service_Library_Name) -l$(Crypto_Library_Name) -l$(Uae_Library_Name) -lsgx_tstdc -lsgx_urts ${protobuf_obj} -lprotobuf -lsgx_tstdcxx -Wl,--verbose -o $@
+	${CXX} ${CXX_FLAGS} systemMain.o crypto.o -L${Openssl_Path}/lib -lssl -lcrypto systemSealerWrapper.o SgxSealer.o ProtobufLAInitiator.o SgxProtobufLAInitiator.o SgxProtobufLAInitiator_Transforms.o ProtobufLAMessages.pb.o -L./ -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive  -L$(SGX_LIBRARY_PATH)  -l$(Service_Library_Name) -l$(Crypto_Library_Name) -l$(Uae_Library_Name) -lsgx_tstdc -lsgx_urts -lprotobuf -lsgx_tstdcxx -Wl,--verbose -o $@

+ 148 - 0
ProtobufLAInitiator.cpp

@@ -0,0 +1,148 @@
+// Knows only protobuf_sgx objects, protobuf header.
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include<unistd.h>
+#include <stdio.h>
+#include "ProtobufLAMessages.h"
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+using namespace google::protobuf::io;
+#include "SgxProtobufLAInitiator.h"
+
+// For socket programming
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+
+// TODO: Make these private functions
+int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
+{
+  ZeroCopyInputStream* raw_input;
+  CodedInputStream* coded_input;
+  uint32_t size;
+  CodedInputStream::Limit limit;
+  raw_input = new FileInputStream(accept_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;
+}
+
+// TODO: private functions
+int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
+{
+  ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_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;
+}
+
+// Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
+// Also prints the errno on error.
+int set_up_socket_connect(int port)
+{
+  int sock = 0;
+  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+  {
+    printf("\n Error in socket call - errno is %d \n", errno);
+    return -1;
+  }
+
+  struct sockaddr_in serv_addr;
+  memset(&serv_addr, '0', sizeof(serv_addr));
+
+  serv_addr.sin_family = AF_INET;
+  serv_addr.sin_port = htons(port);
+
+  // Convert IPv4 and IPv6 addresses from text to binary form
+  if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
+  {
+    printf("\nError in inet_pton - errno is %d\n", errno);
+    return -1;
+  }
+
+  if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
+  {
+    printf("\nError in connect - errno is %d \n", errno);
+    return -1;
+  }
+
+  return sock;
+}
+
+int local_attestation_initiator(int port)
+{
+  // 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;
+
+  // For socket to listen to the Apache enclave.
+//  int server_fd=0; int accept_fd = 0;
+//  struct sockaddr_in own_addr;
+//  struct sockaddr_storage apache_addr; socklen_t apache_addr_size = sizeof(apache_addr);
+  uint32_t session_id;
+  //  int counter;
+
+  int decryptor_fd;
+  setbuf(stdout,NULL);
+  decryptor_fd=set_up_socket_connect(port);
+  if(decryptor_fd == -1)
+  {
+    perror("\nCould not set up the socket: had the following error: ");
+    fflush(stderr);
+  }
+//  printf("");
+  if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg1)!=0)
+    return -1;
+
+  protobuf_sgx_ret = process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_msg1, protobuf_msg2, &session_id);
+  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;
+  }
+
+  if(write_protobuf_msg_to_fd(decryptor_fd, protobuf_msg2)!=0)
+    return -1;
+
+  if(read_protobuf_msg_from_fd(decryptor_fd, protobuf_msg3)!=0)
+    return -1;
+
+  protobuf_sgx_ret = process_protobuf_dh_msg3(protobuf_msg3, &session_id);
+  if(protobuf_sgx_ret != 0)
+  {
+      printf("Error in process_protobuf_dh_msg3: 0x%x", protobuf_sgx_ret); fflush(stdout); return protobuf_sgx_ret;
+  }
+
+	printf("Successfully done Local attestation\n");
+	fflush(stdout);
+	return 0;
+}

+ 0 - 729
ProtobufSealerMessages.cpp

@@ -1,729 +0,0 @@
-// Generated by the protocol buffer compiler.  DO NOT EDIT!
-// source: ProtobufSealerMessages.proto
-
-#define INTERNAL_SUPPRESS_PROTOBUF_FIELD_DEPRECATION
-#include "ProtobufSealerMessages.pb.h"
-
-#include <algorithm>
-
-#include <google/protobuf/stubs/common.h>
-#include <google/protobuf/stubs/once.h>
-#include <google/protobuf/io/coded_stream.h>
-#include <google/protobuf/wire_format_lite_inl.h>
-#include <google/protobuf/descriptor.h>
-#include <google/protobuf/generated_message_reflection.h>
-#include <google/protobuf/reflection_ops.h>
-#include <google/protobuf/wire_format.h>
-// @@protoc_insertion_point(includes)
-
-namespace {
-
-const ::google::protobuf::Descriptor* protobuf_sgx_mc_uuid_t_descriptor_ = NULL;
-const ::google::protobuf::internal::GeneratedMessageReflection*
-  protobuf_sgx_mc_uuid_t_reflection_ = NULL;
-const ::google::protobuf::Descriptor* protobuf_plaintext_seal_message_descriptor_ = NULL;
-const ::google::protobuf::internal::GeneratedMessageReflection*
-  protobuf_plaintext_seal_message_reflection_ = NULL;
-
-}  // namespace
-
-
-void protobuf_AssignDesc_ProtobufSealerMessages_2eproto() {
-  protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-  const ::google::protobuf::FileDescriptor* file =
-    ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName(
-      "ProtobufSealerMessages.proto");
-  GOOGLE_CHECK(file != NULL);
-  protobuf_sgx_mc_uuid_t_descriptor_ = file->message_type(0);
-  static const int protobuf_sgx_mc_uuid_t_offsets_[2] = {
-    GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_sgx_mc_uuid_t, nonce_),
-    GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_sgx_mc_uuid_t, counter_id_),
-  };
-  protobuf_sgx_mc_uuid_t_reflection_ =
-    new ::google::protobuf::internal::GeneratedMessageReflection(
-      protobuf_sgx_mc_uuid_t_descriptor_,
-      protobuf_sgx_mc_uuid_t::default_instance_,
-      protobuf_sgx_mc_uuid_t_offsets_,
-      GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_sgx_mc_uuid_t, _has_bits_[0]),
-      GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_sgx_mc_uuid_t, _unknown_fields_),
-      -1,
-      ::google::protobuf::DescriptorPool::generated_pool(),
-      ::google::protobuf::MessageFactory::generated_factory(),
-      sizeof(protobuf_sgx_mc_uuid_t));
-  protobuf_plaintext_seal_message_descriptor_ = file->message_type(1);
-  static const int protobuf_plaintext_seal_message_offsets_[3] = {
-    GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_plaintext_seal_message, monotonic_counter_id_),
-    GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_plaintext_seal_message, monotonic_counter_value_),
-    GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_plaintext_seal_message, message_),
-  };
-  protobuf_plaintext_seal_message_reflection_ =
-    new ::google::protobuf::internal::GeneratedMessageReflection(
-      protobuf_plaintext_seal_message_descriptor_,
-      protobuf_plaintext_seal_message::default_instance_,
-      protobuf_plaintext_seal_message_offsets_,
-      GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_plaintext_seal_message, _has_bits_[0]),
-      GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(protobuf_plaintext_seal_message, _unknown_fields_),
-      -1,
-      ::google::protobuf::DescriptorPool::generated_pool(),
-      ::google::protobuf::MessageFactory::generated_factory(),
-      sizeof(protobuf_plaintext_seal_message));
-}
-
-namespace {
-
-GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_);
-inline void protobuf_AssignDescriptorsOnce() {
-  ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_,
-                 &protobuf_AssignDesc_ProtobufSealerMessages_2eproto);
-}
-
-void protobuf_RegisterTypes(const ::std::string&) {
-  protobuf_AssignDescriptorsOnce();
-  ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
-    protobuf_sgx_mc_uuid_t_descriptor_, &protobuf_sgx_mc_uuid_t::default_instance());
-  ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage(
-    protobuf_plaintext_seal_message_descriptor_, &protobuf_plaintext_seal_message::default_instance());
-}
-
-}  // namespace
-
-void protobuf_ShutdownFile_ProtobufSealerMessages_2eproto() {
-  delete protobuf_sgx_mc_uuid_t::default_instance_;
-  delete protobuf_sgx_mc_uuid_t_reflection_;
-  delete protobuf_plaintext_seal_message::default_instance_;
-  delete protobuf_plaintext_seal_message_reflection_;
-}
-
-void protobuf_AddDesc_ProtobufSealerMessages_2eproto() {
-  static bool already_here = false;
-  if (already_here) return;
-  already_here = true;
-  GOOGLE_PROTOBUF_VERIFY_VERSION;
-
-  ::google::protobuf::DescriptorPool::InternalAddGeneratedFile(
-    "\n\034ProtobufSealerMessages.proto\";\n\026protob"
-    "uf_sgx_mc_uuid_t\022\r\n\005nonce\030\001 \002(\014\022\022\n\ncount"
-    "er_id\030\002 \002(\014\"\212\001\n\037protobuf_plaintext_seal_"
-    "message\0225\n\024monotonic_counter_id\030\001 \002(\0132\027."
-    "protobuf_sgx_mc_uuid_t\022\037\n\027monotonic_coun"
-    "ter_value\030\002 \002(\r\022\017\n\007message\030\003 \002(\014", 232);
-  ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile(
-    "ProtobufSealerMessages.proto", &protobuf_RegisterTypes);
-  protobuf_sgx_mc_uuid_t::default_instance_ = new protobuf_sgx_mc_uuid_t();
-  protobuf_plaintext_seal_message::default_instance_ = new protobuf_plaintext_seal_message();
-  protobuf_sgx_mc_uuid_t::default_instance_->InitAsDefaultInstance();
-  protobuf_plaintext_seal_message::default_instance_->InitAsDefaultInstance();
-  ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_ProtobufSealerMessages_2eproto);
-}
-
-// Force AddDescriptors() to be called at static initialization time.
-struct StaticDescriptorInitializer_ProtobufSealerMessages_2eproto {
-  StaticDescriptorInitializer_ProtobufSealerMessages_2eproto() {
-    protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-  }
-} static_descriptor_initializer_ProtobufSealerMessages_2eproto_;
-
-// ===================================================================
-
-#ifndef _MSC_VER
-const int protobuf_sgx_mc_uuid_t::kNonceFieldNumber;
-const int protobuf_sgx_mc_uuid_t::kCounterIdFieldNumber;
-#endif  // !_MSC_VER
-
-protobuf_sgx_mc_uuid_t::protobuf_sgx_mc_uuid_t()
-  : ::google::protobuf::Message() {
-  SharedCtor();
-  // @@protoc_insertion_point(constructor:protobuf_sgx_mc_uuid_t)
-}
-
-void protobuf_sgx_mc_uuid_t::InitAsDefaultInstance() {
-}
-
-protobuf_sgx_mc_uuid_t::protobuf_sgx_mc_uuid_t(const protobuf_sgx_mc_uuid_t& from)
-  : ::google::protobuf::Message() {
-  SharedCtor();
-  MergeFrom(from);
-  // @@protoc_insertion_point(copy_constructor:protobuf_sgx_mc_uuid_t)
-}
-
-void protobuf_sgx_mc_uuid_t::SharedCtor() {
-  ::google::protobuf::internal::GetEmptyString();
-  _cached_size_ = 0;
-  nonce_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-  counter_id_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-  ::memset(_has_bits_, 0, sizeof(_has_bits_));
-}
-
-protobuf_sgx_mc_uuid_t::~protobuf_sgx_mc_uuid_t() {
-  // @@protoc_insertion_point(destructor:protobuf_sgx_mc_uuid_t)
-  SharedDtor();
-}
-
-void protobuf_sgx_mc_uuid_t::SharedDtor() {
-  if (nonce_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    delete nonce_;
-  }
-  if (counter_id_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    delete counter_id_;
-  }
-  if (this != default_instance_) {
-  }
-}
-
-void protobuf_sgx_mc_uuid_t::SetCachedSize(int size) const {
-  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
-  _cached_size_ = size;
-  GOOGLE_SAFE_CONCURRENT_WRITES_END();
-}
-const ::google::protobuf::Descriptor* protobuf_sgx_mc_uuid_t::descriptor() {
-  protobuf_AssignDescriptorsOnce();
-  return protobuf_sgx_mc_uuid_t_descriptor_;
-}
-
-const protobuf_sgx_mc_uuid_t& protobuf_sgx_mc_uuid_t::default_instance() {
-  if (default_instance_ == NULL) protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-  return *default_instance_;
-}
-
-protobuf_sgx_mc_uuid_t* protobuf_sgx_mc_uuid_t::default_instance_ = NULL;
-
-protobuf_sgx_mc_uuid_t* protobuf_sgx_mc_uuid_t::New() const {
-  return new protobuf_sgx_mc_uuid_t;
-}
-
-void protobuf_sgx_mc_uuid_t::Clear() {
-  if (_has_bits_[0 / 32] & 3) {
-    if (has_nonce()) {
-      if (nonce_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-        nonce_->clear();
-      }
-    }
-    if (has_counter_id()) {
-      if (counter_id_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-        counter_id_->clear();
-      }
-    }
-  }
-  ::memset(_has_bits_, 0, sizeof(_has_bits_));
-  mutable_unknown_fields()->Clear();
-}
-
-bool protobuf_sgx_mc_uuid_t::MergePartialFromCodedStream(
-    ::google::protobuf::io::CodedInputStream* input) {
-#define DO_(EXPRESSION) if (!(EXPRESSION)) goto failure
-  ::google::protobuf::uint32 tag;
-  // @@protoc_insertion_point(parse_start:protobuf_sgx_mc_uuid_t)
-  for (;;) {
-    ::std::pair< ::google::protobuf::uint32, bool> p = input->ReadTagWithCutoff(127);
-    tag = p.first;
-    if (!p.second) goto handle_unusual;
-    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
-      // required bytes nonce = 1;
-      case 1: {
-        if (tag == 10) {
-          DO_(::google::protobuf::internal::WireFormatLite::ReadBytes(
-                input, this->mutable_nonce()));
-        } else {
-          goto handle_unusual;
-        }
-        if (input->ExpectTag(18)) goto parse_counter_id;
-        break;
-      }
-
-      // required bytes counter_id = 2;
-      case 2: {
-        if (tag == 18) {
-         parse_counter_id:
-          DO_(::google::protobuf::internal::WireFormatLite::ReadBytes(
-                input, this->mutable_counter_id()));
-        } else {
-          goto handle_unusual;
-        }
-        if (input->ExpectAtEnd()) goto success;
-        break;
-      }
-
-      default: {
-      handle_unusual:
-        if (tag == 0 ||
-            ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
-            ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
-          goto success;
-        }
-        DO_(::google::protobuf::internal::WireFormat::SkipField(
-              input, tag, mutable_unknown_fields()));
-        break;
-      }
-    }
-  }
-success:
-  // @@protoc_insertion_point(parse_success:protobuf_sgx_mc_uuid_t)
-  return true;
-failure:
-  // @@protoc_insertion_point(parse_failure:protobuf_sgx_mc_uuid_t)
-  return false;
-#undef DO_
-}
-
-void protobuf_sgx_mc_uuid_t::SerializeWithCachedSizes(
-    ::google::protobuf::io::CodedOutputStream* output) const {
-  // @@protoc_insertion_point(serialize_start:protobuf_sgx_mc_uuid_t)
-  // required bytes nonce = 1;
-  if (has_nonce()) {
-    ::google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased(
-      1, this->nonce(), output);
-  }
-
-  // required bytes counter_id = 2;
-  if (has_counter_id()) {
-    ::google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased(
-      2, this->counter_id(), output);
-  }
-
-  if (!unknown_fields().empty()) {
-    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
-        unknown_fields(), output);
-  }
-  // @@protoc_insertion_point(serialize_end:protobuf_sgx_mc_uuid_t)
-}
-
-::google::protobuf::uint8* protobuf_sgx_mc_uuid_t::SerializeWithCachedSizesToArray(
-    ::google::protobuf::uint8* target) const {
-  // @@protoc_insertion_point(serialize_to_array_start:protobuf_sgx_mc_uuid_t)
-  // required bytes nonce = 1;
-  if (has_nonce()) {
-    target =
-      ::google::protobuf::internal::WireFormatLite::WriteBytesToArray(
-        1, this->nonce(), target);
-  }
-
-  // required bytes counter_id = 2;
-  if (has_counter_id()) {
-    target =
-      ::google::protobuf::internal::WireFormatLite::WriteBytesToArray(
-        2, this->counter_id(), target);
-  }
-
-  if (!unknown_fields().empty()) {
-    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
-        unknown_fields(), target);
-  }
-  // @@protoc_insertion_point(serialize_to_array_end:protobuf_sgx_mc_uuid_t)
-  return target;
-}
-
-int protobuf_sgx_mc_uuid_t::ByteSize() const {
-  int total_size = 0;
-
-  if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
-    // required bytes nonce = 1;
-    if (has_nonce()) {
-      total_size += 1 +
-        ::google::protobuf::internal::WireFormatLite::BytesSize(
-          this->nonce());
-    }
-
-    // required bytes counter_id = 2;
-    if (has_counter_id()) {
-      total_size += 1 +
-        ::google::protobuf::internal::WireFormatLite::BytesSize(
-          this->counter_id());
-    }
-
-  }
-  if (!unknown_fields().empty()) {
-    total_size +=
-      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
-        unknown_fields());
-  }
-  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
-  _cached_size_ = total_size;
-  GOOGLE_SAFE_CONCURRENT_WRITES_END();
-  return total_size;
-}
-
-void protobuf_sgx_mc_uuid_t::MergeFrom(const ::google::protobuf::Message& from) {
-  GOOGLE_CHECK_NE(&from, this);
-  const protobuf_sgx_mc_uuid_t* source =
-    ::google::protobuf::internal::dynamic_cast_if_available<const protobuf_sgx_mc_uuid_t*>(
-      &from);
-  if (source == NULL) {
-    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
-  } else {
-    MergeFrom(*source);
-  }
-}
-
-void protobuf_sgx_mc_uuid_t::MergeFrom(const protobuf_sgx_mc_uuid_t& from) {
-  GOOGLE_CHECK_NE(&from, this);
-  if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
-    if (from.has_nonce()) {
-      set_nonce(from.nonce());
-    }
-    if (from.has_counter_id()) {
-      set_counter_id(from.counter_id());
-    }
-  }
-  mutable_unknown_fields()->MergeFrom(from.unknown_fields());
-}
-
-void protobuf_sgx_mc_uuid_t::CopyFrom(const ::google::protobuf::Message& from) {
-  if (&from == this) return;
-  Clear();
-  MergeFrom(from);
-}
-
-void protobuf_sgx_mc_uuid_t::CopyFrom(const protobuf_sgx_mc_uuid_t& from) {
-  if (&from == this) return;
-  Clear();
-  MergeFrom(from);
-}
-
-bool protobuf_sgx_mc_uuid_t::IsInitialized() const {
-  if ((_has_bits_[0] & 0x00000003) != 0x00000003) return false;
-
-  return true;
-}
-
-void protobuf_sgx_mc_uuid_t::Swap(protobuf_sgx_mc_uuid_t* other) {
-  if (other != this) {
-    std::swap(nonce_, other->nonce_);
-    std::swap(counter_id_, other->counter_id_);
-    std::swap(_has_bits_[0], other->_has_bits_[0]);
-    _unknown_fields_.Swap(&other->_unknown_fields_);
-    std::swap(_cached_size_, other->_cached_size_);
-  }
-}
-
-::google::protobuf::Metadata protobuf_sgx_mc_uuid_t::GetMetadata() const {
-  protobuf_AssignDescriptorsOnce();
-  ::google::protobuf::Metadata metadata;
-  metadata.descriptor = protobuf_sgx_mc_uuid_t_descriptor_;
-  metadata.reflection = protobuf_sgx_mc_uuid_t_reflection_;
-  return metadata;
-}
-
-
-// ===================================================================
-
-#ifndef _MSC_VER
-const int protobuf_plaintext_seal_message::kMonotonicCounterIdFieldNumber;
-const int protobuf_plaintext_seal_message::kMonotonicCounterValueFieldNumber;
-const int protobuf_plaintext_seal_message::kMessageFieldNumber;
-#endif  // !_MSC_VER
-
-protobuf_plaintext_seal_message::protobuf_plaintext_seal_message()
-  : ::google::protobuf::Message() {
-  SharedCtor();
-  // @@protoc_insertion_point(constructor:protobuf_plaintext_seal_message)
-}
-
-void protobuf_plaintext_seal_message::InitAsDefaultInstance() {
-  monotonic_counter_id_ = const_cast< ::protobuf_sgx_mc_uuid_t*>(&::protobuf_sgx_mc_uuid_t::default_instance());
-}
-
-protobuf_plaintext_seal_message::protobuf_plaintext_seal_message(const protobuf_plaintext_seal_message& from)
-  : ::google::protobuf::Message() {
-  SharedCtor();
-  MergeFrom(from);
-  // @@protoc_insertion_point(copy_constructor:protobuf_plaintext_seal_message)
-}
-
-void protobuf_plaintext_seal_message::SharedCtor() {
-  ::google::protobuf::internal::GetEmptyString();
-  _cached_size_ = 0;
-  monotonic_counter_id_ = NULL;
-  monotonic_counter_value_ = 0u;
-  message_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-  ::memset(_has_bits_, 0, sizeof(_has_bits_));
-}
-
-protobuf_plaintext_seal_message::~protobuf_plaintext_seal_message() {
-  // @@protoc_insertion_point(destructor:protobuf_plaintext_seal_message)
-  SharedDtor();
-}
-
-void protobuf_plaintext_seal_message::SharedDtor() {
-  if (message_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    delete message_;
-  }
-  if (this != default_instance_) {
-    delete monotonic_counter_id_;
-  }
-}
-
-void protobuf_plaintext_seal_message::SetCachedSize(int size) const {
-  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
-  _cached_size_ = size;
-  GOOGLE_SAFE_CONCURRENT_WRITES_END();
-}
-const ::google::protobuf::Descriptor* protobuf_plaintext_seal_message::descriptor() {
-  protobuf_AssignDescriptorsOnce();
-  return protobuf_plaintext_seal_message_descriptor_;
-}
-
-const protobuf_plaintext_seal_message& protobuf_plaintext_seal_message::default_instance() {
-  if (default_instance_ == NULL) protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-  return *default_instance_;
-}
-
-protobuf_plaintext_seal_message* protobuf_plaintext_seal_message::default_instance_ = NULL;
-
-protobuf_plaintext_seal_message* protobuf_plaintext_seal_message::New() const {
-  return new protobuf_plaintext_seal_message;
-}
-
-void protobuf_plaintext_seal_message::Clear() {
-  if (_has_bits_[0 / 32] & 7) {
-    if (has_monotonic_counter_id()) {
-      if (monotonic_counter_id_ != NULL) monotonic_counter_id_->::protobuf_sgx_mc_uuid_t::Clear();
-    }
-    monotonic_counter_value_ = 0u;
-    if (has_message()) {
-      if (message_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-        message_->clear();
-      }
-    }
-  }
-  ::memset(_has_bits_, 0, sizeof(_has_bits_));
-  mutable_unknown_fields()->Clear();
-}
-
-bool protobuf_plaintext_seal_message::MergePartialFromCodedStream(
-    ::google::protobuf::io::CodedInputStream* input) {
-#define DO_(EXPRESSION) if (!(EXPRESSION)) goto failure
-  ::google::protobuf::uint32 tag;
-  // @@protoc_insertion_point(parse_start:protobuf_plaintext_seal_message)
-  for (;;) {
-    ::std::pair< ::google::protobuf::uint32, bool> p = input->ReadTagWithCutoff(127);
-    tag = p.first;
-    if (!p.second) goto handle_unusual;
-    switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) {
-      // required .protobuf_sgx_mc_uuid_t monotonic_counter_id = 1;
-      case 1: {
-        if (tag == 10) {
-          DO_(::google::protobuf::internal::WireFormatLite::ReadMessageNoVirtual(
-               input, mutable_monotonic_counter_id()));
-        } else {
-          goto handle_unusual;
-        }
-        if (input->ExpectTag(16)) goto parse_monotonic_counter_value;
-        break;
-      }
-
-      // required uint32 monotonic_counter_value = 2;
-      case 2: {
-        if (tag == 16) {
-         parse_monotonic_counter_value:
-          DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<
-                   ::google::protobuf::uint32, ::google::protobuf::internal::WireFormatLite::TYPE_UINT32>(
-                 input, &monotonic_counter_value_)));
-          set_has_monotonic_counter_value();
-        } else {
-          goto handle_unusual;
-        }
-        if (input->ExpectTag(26)) goto parse_message;
-        break;
-      }
-
-      // required bytes message = 3;
-      case 3: {
-        if (tag == 26) {
-         parse_message:
-          DO_(::google::protobuf::internal::WireFormatLite::ReadBytes(
-                input, this->mutable_message()));
-        } else {
-          goto handle_unusual;
-        }
-        if (input->ExpectAtEnd()) goto success;
-        break;
-      }
-
-      default: {
-      handle_unusual:
-        if (tag == 0 ||
-            ::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) ==
-            ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) {
-          goto success;
-        }
-        DO_(::google::protobuf::internal::WireFormat::SkipField(
-              input, tag, mutable_unknown_fields()));
-        break;
-      }
-    }
-  }
-success:
-  // @@protoc_insertion_point(parse_success:protobuf_plaintext_seal_message)
-  return true;
-failure:
-  // @@protoc_insertion_point(parse_failure:protobuf_plaintext_seal_message)
-  return false;
-#undef DO_
-}
-
-void protobuf_plaintext_seal_message::SerializeWithCachedSizes(
-    ::google::protobuf::io::CodedOutputStream* output) const {
-  // @@protoc_insertion_point(serialize_start:protobuf_plaintext_seal_message)
-  // required .protobuf_sgx_mc_uuid_t monotonic_counter_id = 1;
-  if (has_monotonic_counter_id()) {
-    ::google::protobuf::internal::WireFormatLite::WriteMessageMaybeToArray(
-      1, this->monotonic_counter_id(), output);
-  }
-
-  // required uint32 monotonic_counter_value = 2;
-  if (has_monotonic_counter_value()) {
-    ::google::protobuf::internal::WireFormatLite::WriteUInt32(2, this->monotonic_counter_value(), output);
-  }
-
-  // required bytes message = 3;
-  if (has_message()) {
-    ::google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased(
-      3, this->message(), output);
-  }
-
-  if (!unknown_fields().empty()) {
-    ::google::protobuf::internal::WireFormat::SerializeUnknownFields(
-        unknown_fields(), output);
-  }
-  // @@protoc_insertion_point(serialize_end:protobuf_plaintext_seal_message)
-}
-
-::google::protobuf::uint8* protobuf_plaintext_seal_message::SerializeWithCachedSizesToArray(
-    ::google::protobuf::uint8* target) const {
-  // @@protoc_insertion_point(serialize_to_array_start:protobuf_plaintext_seal_message)
-  // required .protobuf_sgx_mc_uuid_t monotonic_counter_id = 1;
-  if (has_monotonic_counter_id()) {
-    target = ::google::protobuf::internal::WireFormatLite::
-      WriteMessageNoVirtualToArray(
-        1, this->monotonic_counter_id(), target);
-  }
-
-  // required uint32 monotonic_counter_value = 2;
-  if (has_monotonic_counter_value()) {
-    target = ::google::protobuf::internal::WireFormatLite::WriteUInt32ToArray(2, this->monotonic_counter_value(), target);
-  }
-
-  // required bytes message = 3;
-  if (has_message()) {
-    target =
-      ::google::protobuf::internal::WireFormatLite::WriteBytesToArray(
-        3, this->message(), target);
-  }
-
-  if (!unknown_fields().empty()) {
-    target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray(
-        unknown_fields(), target);
-  }
-  // @@protoc_insertion_point(serialize_to_array_end:protobuf_plaintext_seal_message)
-  return target;
-}
-
-int protobuf_plaintext_seal_message::ByteSize() const {
-  int total_size = 0;
-
-  if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) {
-    // required .protobuf_sgx_mc_uuid_t monotonic_counter_id = 1;
-    if (has_monotonic_counter_id()) {
-      total_size += 1 +
-        ::google::protobuf::internal::WireFormatLite::MessageSizeNoVirtual(
-          this->monotonic_counter_id());
-    }
-
-    // required uint32 monotonic_counter_value = 2;
-    if (has_monotonic_counter_value()) {
-      total_size += 1 +
-        ::google::protobuf::internal::WireFormatLite::UInt32Size(
-          this->monotonic_counter_value());
-    }
-
-    // required bytes message = 3;
-    if (has_message()) {
-      total_size += 1 +
-        ::google::protobuf::internal::WireFormatLite::BytesSize(
-          this->message());
-    }
-
-  }
-  if (!unknown_fields().empty()) {
-    total_size +=
-      ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(
-        unknown_fields());
-  }
-  GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
-  _cached_size_ = total_size;
-  GOOGLE_SAFE_CONCURRENT_WRITES_END();
-  return total_size;
-}
-
-void protobuf_plaintext_seal_message::MergeFrom(const ::google::protobuf::Message& from) {
-  GOOGLE_CHECK_NE(&from, this);
-  const protobuf_plaintext_seal_message* source =
-    ::google::protobuf::internal::dynamic_cast_if_available<const protobuf_plaintext_seal_message*>(
-      &from);
-  if (source == NULL) {
-    ::google::protobuf::internal::ReflectionOps::Merge(from, this);
-  } else {
-    MergeFrom(*source);
-  }
-}
-
-void protobuf_plaintext_seal_message::MergeFrom(const protobuf_plaintext_seal_message& from) {
-  GOOGLE_CHECK_NE(&from, this);
-  if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) {
-    if (from.has_monotonic_counter_id()) {
-      mutable_monotonic_counter_id()->::protobuf_sgx_mc_uuid_t::MergeFrom(from.monotonic_counter_id());
-    }
-    if (from.has_monotonic_counter_value()) {
-      set_monotonic_counter_value(from.monotonic_counter_value());
-    }
-    if (from.has_message()) {
-      set_message(from.message());
-    }
-  }
-  mutable_unknown_fields()->MergeFrom(from.unknown_fields());
-}
-
-void protobuf_plaintext_seal_message::CopyFrom(const ::google::protobuf::Message& from) {
-  if (&from == this) return;
-  Clear();
-  MergeFrom(from);
-}
-
-void protobuf_plaintext_seal_message::CopyFrom(const protobuf_plaintext_seal_message& from) {
-  if (&from == this) return;
-  Clear();
-  MergeFrom(from);
-}
-
-bool protobuf_plaintext_seal_message::IsInitialized() const {
-  if ((_has_bits_[0] & 0x00000007) != 0x00000007) return false;
-
-  if (has_monotonic_counter_id()) {
-    if (!this->monotonic_counter_id().IsInitialized()) return false;
-  }
-  return true;
-}
-
-void protobuf_plaintext_seal_message::Swap(protobuf_plaintext_seal_message* other) {
-  if (other != this) {
-    std::swap(monotonic_counter_id_, other->monotonic_counter_id_);
-    std::swap(monotonic_counter_value_, other->monotonic_counter_value_);
-    std::swap(message_, other->message_);
-    std::swap(_has_bits_[0], other->_has_bits_[0]);
-    _unknown_fields_.Swap(&other->_unknown_fields_);
-    std::swap(_cached_size_, other->_cached_size_);
-  }
-}
-
-::google::protobuf::Metadata protobuf_plaintext_seal_message::GetMetadata() const {
-  protobuf_AssignDescriptorsOnce();
-  ::google::protobuf::Metadata metadata;
-  metadata.descriptor = protobuf_plaintext_seal_message_descriptor_;
-  metadata.reflection = protobuf_plaintext_seal_message_reflection_;
-  return metadata;
-}
-
-
-// @@protoc_insertion_point(namespace_scope)
-
-// @@protoc_insertion_point(global_scope)

+ 0 - 19
SgxCrypto.cpp

@@ -1,19 +0,0 @@
-#include "stdlib.h"
-//#include "string"
-#include "sgx_tcrypto.h"
-uint32_t create_rsa_key_pair_for_signing_manifest(/*std::string& priv_key, std::string& pub_key*/) 
-{
-	uint32_t ret_sgx;
-	uint8_t* n=(uint8_t*)malloc(384); 
-        uint8_t* d=(uint8_t*)malloc(384); 
-        uint8_t* p=(uint8_t*)malloc(192); 
-        uint8_t* q=(uint8_t*)malloc(192); 
-        uint8_t* dmp1=(uint8_t*)malloc(192); 
-        uint8_t* dmq1=(uint8_t*)malloc(192); 
-        uint8_t* iqmp=(uint8_t*)malloc(192); 
-	uint8_t e[4];
-
-	ret_sgx=sgx_create_rsa_key_pair(384, 4, n, d, e, p, q, dmp1, dmq1, iqmp);
-	free(n); free(d); free(p); free(q); free(dmp1); free(dmq1); free(iqmp);
-	return ret_sgx;
-}

+ 0 - 136
SgxLAInitiator.cpp

@@ -1,136 +0,0 @@
-/*
- * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- *   * Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer.
- *   * Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in
- *     the documentation and/or other materials provided with the
- *     distribution.
- *   * Neither the name of Intel Corporation nor the names of its
- *     contributors may be used to endorse or promote products derived
- *     from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-
-#include "sgx_trts.h"
-#include "sgx_utils.h"
-#include "sgx_eid.h"
-#include "error_codes.h"
-#include "sgx_ecp_types.h"
-#include "sgx_thread.h"
-#include <map>
-#include "sgx_dh.h"
-#include "dh_session_protocol.h"
-#include "sgx_tcrypto.h"
-#include "datatypes.h"
-#include "ProtobufSgxLATransforms_Inititator.h"
-#define MAX_SESSION_COUNT  16
-#define SGX_CAST(type, item) ((type)(item))
-#include <string.h>
-
-// uint32_t session_ids[MAX_SESSION_COUNT] ;
-
-// Our enclave will not be doing LA with more than 1 decryptor enclave at a time.
-// We should not need this.
-//std::map<int, dh_session_t>g_dest_session_info_map;
-dh_session_t global_session_info;
-// TODO: May be we need to store all previously assigned session IDs instead of just the counter; to prevent replay attacks -
-uint32_t global_session_id=0;
-
-uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer)
-{
-  int count=0;
-  if(!peer_enclave_identity)
-    return INVALID_PARAMETER_ERROR;
-
-  sgx_measurement_t actual_mr_enclave = peer_enclave_identity->mr_enclave;
-  sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
-/*  for(count=0; count<SGX_HASH_SIZE; count++)
-  {
-    if( actual_mr_enclave.m[count] != expected_mr_enclave[count] )
-    {
-        return ENCLAVE_TRUST_ERROR;
-    }
-    if( actual_mr_signer.m[count] != expected_mr_signer[count] )
-    {
-        return ENCLAVE_TRUST_ERROR; // TODO: Different error here.
-    }
-  }*/
-  return SUCCESS;
-}
-
-
-//Create a session with the destination enclave
-int create_session(int fd, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer, uint8_t* send_mr_signer)
-{
-  sgx_dh_msg1_t dh_msg1;            //Diffie-Hellman Message 1
-  sgx_key_128bit_t dh_aek;        // Session Key
-  sgx_dh_msg2_t dh_msg2;            //Diffie-Hellman Message 2
-  sgx_dh_msg3_t dh_msg3;            //Diffie-Hellman Message 3
-
-  int retstatus;
-  sgx_status_t status = SGX_SUCCESS;
-  sgx_dh_session_t sgx_dh_session;
-  sgx_dh_session_enclave_identity_t responder_identity;
-
-  memset(&dh_aek,0, sizeof(sgx_key_128bit_t));
-  memset(&dh_msg1, 0, sizeof(sgx_dh_msg1_t));
-  memset(&dh_msg2, 0, sizeof(sgx_dh_msg2_t));
-  memset(&dh_msg3, 0, sizeof(sgx_dh_msg3_t));
-  memset(&global_session_info, 0, sizeof(dh_session_t));
-
-  //Intialize the session as a session initiator
-  status = sgx_dh_init_session(SGX_DH_SESSION_INITIATOR, &sgx_dh_session);
-  if(status != SGX_SUCCESS)
-    return status;
-
-  retstatus = session_request_call(fd, &dh_msg1);//, &session_id);
-  if(retstatus!=0)
-    return retstatus;
-
-  //Process the message 1 obtained from desination enclave and generate message 2
-  status = sgx_dh_initiator_proc_msg1(&dh_msg1, &dh_msg2, &sgx_dh_session);
-  if(SGX_SUCCESS != status)
-    return status;
-
-  //Send Message 2 to Destination Enclave and get Message 3 in return
-  retstatus = exchange_report_call( fd, &dh_msg2, &dh_msg3); //, &session_id);
-  if(retstatus!=0)
-	 return retstatus;
-
-  //Process Message 3 obtained from the destination enclave
-  status = sgx_dh_initiator_proc_msg3(&dh_msg3, &sgx_dh_session, &dh_aek, &responder_identity);
-  if(SGX_SUCCESS != status)
-    return status;
-
-  // Verify the identity of the destination enclave
-  retstatus = verify_peer_enclave_trust(&responder_identity, expected_mr_enclave, expected_mr_signer);
-  if(retstatus != 0)
-    return retstatus;
-
-  memcpy(global_session_info.active.AEK, &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;
-}

+ 113 - 0
SgxProtobufLAInitiator.cpp

@@ -0,0 +1,113 @@
+#include "sgx_eid.h"
+#define __STDC_FORMAT_MACROS
+#include <inttypes.h>
+#include "ProtobufLAMessages.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 <map>
+#include "sgx_dh.h"
+#include "dh_session_protocol.h"
+#include "sgx_tcrypto.h"
+#include "datatypes.h"
+#include "SgxProtobufLAInitiator_Transforms.h"
+#define MAX_SESSION_COUNT  16
+#define SGX_CAST(type, item) ((type)(item))
+#include <string.h>
+
+dh_session_t global_session_info;
+
+sgx_dh_session_t sgx_dh_session;
+//  sgx_key_128bit_t dh_aek;        // Session Key
+
+uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer);
+
+
+uint32_t process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_sgx_dh_msg1_t& protobuf_msg1, protobuf_sgx_dh_msg2_t& protobuf_msg2, uint32_t* session_id)
+{
+  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(decode_msg1_from_protobuf(protobuf_msg1, &dh_msg1)!=0)
+    return -1;
+
+  //Intialize the session as a session initiator
+  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(SGX_SUCCESS != ret_status)
+    return ret_status;
+
+  encode_msg2_to_protobuf(protobuf_msg2, &dh_msg2);
+  return 0;
+}
+
+uint32_t process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id) {
+
+  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(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;
+
+  // Verify the identity of the destination enclave
+  ret_status = verify_peer_enclave_trust(&responder_identity, NULL, NULL);
+  if(ret_status != 0)
+    return ret_status;
+
+  memcpy(global_session_info.active.AEK, &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;
+}
+
+// TODO: Private function
+uint32_t verify_peer_enclave_trust(sgx_dh_session_enclave_identity_t* peer_enclave_identity, uint8_t* expected_mr_enclave, uint8_t* expected_mr_signer)
+{
+  int count=0;
+  if(!peer_enclave_identity)
+    return INVALID_PARAMETER_ERROR;
+
+  sgx_measurement_t actual_mr_enclave = peer_enclave_identity->mr_enclave;
+  sgx_measurement_t actual_mr_signer = peer_enclave_identity->mr_signer;
+
+	if(expected_mr_enclave != NULL)
+	{
+		for(count=0; count<SGX_HASH_SIZE; count++)
+		{
+			if( actual_mr_enclave.m[count] != expected_mr_enclave[count] )
+			        return ENCLAVE_TRUST_ERROR;
+  		}
+	}
+	if(expected_mr_signer !=NULL)
+	{
+		for(count=0; count<SGX_HASH_SIZE; count++)
+		{
+			if( actual_mr_signer.m[count] != expected_mr_signer[count] )
+			        return ENCLAVE_TRUST_ERROR; // TODO: Different error here.
+		}
+	}
+
+  return SUCCESS;
+}

+ 17 - 56
SgxProtobufLATransforms_Inititator.cpp → SgxProtobufLAInitiator_Transforms.cpp

@@ -27,6 +27,7 @@
 using namespace google::protobuf::io;
 #include <inttypes.h>
 
+// TODO: PRIVATE
 int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
 {
  if(temp32 > UINT8_MAX)
@@ -39,6 +40,7 @@ int fit_32_into_uint8_t(google::protobuf::uint32 temp32, uint8_t* temp8)
  }
 }
 
+// TODO: PRIVATE
 int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
 {
  if(temp32 > UINT16_MAX)
@@ -51,54 +53,7 @@ int fit_32_into_uint16_t(google::protobuf::uint32 temp32, uint16_t* temp16)
  }
 }
 
-int read_protobuf_msg_from_fd(int accept_fd, google::protobuf::MessageLite& message)
-{
-  ZeroCopyInputStream* raw_input;
-  CodedInputStream* coded_input;
-  uint32_t size;
-  CodedInputStream::Limit limit;
-  raw_input = new FileInputStream(accept_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);
-  coded_input->PopLimit(limit);
-
-    return -1;
-  }
-  coded_input->PopLimit(limit);
-  return 0;
-}
-
-int write_protobuf_msg_to_fd(int accept_fd, google::protobuf::MessageLite& message)
-{
-  ZeroCopyOutputStream* raw_output = new FileOutputStream(accept_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;
-}
-
+// TODO: PRIVATE
 void 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");
@@ -115,12 +70,14 @@ void encode_ec256_public_key_to_protobuf(protobuf_sgx_ec256_public_t* protobuf_g
 	printf("\n");
 }
 
+// TODO: PRIVATE
 void 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
 }
 
+// TODO: PRIVATE
 void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
 {
 	printf("\n report \n key id \n");
@@ -175,23 +132,23 @@ void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_repor
 		protobuf_report->mutable_body()->add_reserved4(temp32);
  	}
 
-	printf("\nmr enclave\n"); 
+	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("%x ", temp32);
  	}
-	
-	printf("\n mr signer\n");  fflush(stdout); 
+
+	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); 
+		printf("%x ", temp32);
  	}
-	
+
 
 	for(counter=0;counter<SGX_REPORT_DATA_SIZE;counter++)
 	{
@@ -200,6 +157,7 @@ void encode_report_to_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_repor
  	}
 }
 
+// TODO: PRIVATE
 int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attributes, sgx_attributes_t* attributes)
 {
         attributes->flags = protobuf_attributes->flags();
@@ -209,6 +167,7 @@ int decode_attributes_from_protobuf(protobuf_sgx_attributes_t* protobuf_attribut
         return 0;
 }
 
+// TODO: PRIVATE
 int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_report_t* report)
 {
   int counter; google::protobuf::uint32 temp32;
@@ -317,7 +276,7 @@ int decode_report_from_protobuf(protobuf_sgx_report_t* protobuf_report, sgx_repo
       return -1;
     printf("%d ", report->body.report_data.d[counter]);
   }
-  printf("\n------------------------ end of msg3 --------------------------\n"); 
+  printf("\n------------------------ end of msg3 --------------------------\n");
 	return 0;
 }
 
@@ -383,6 +342,7 @@ int decode_msg3_from_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_ms
   return 0;
 }
 
+// TODO: PRIVATE - OR EVEN GET RID OF IT
 int print_initialized_msg1( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1 /*, sgx_dh_msg1_t* native_dhmsg1*/)
 {
 	int counter;
@@ -438,7 +398,7 @@ void encode_msg2_to_protobuf( protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_ms
 
 	encode_report_to_protobuf(protobuf_dhmsg2.mutable_report(), &(native_dhmsg2->report));
 }
-
+/*
 // Got rid of the session ID - figure out its role.
 //message1 from the destination enclave through a socket set up before.
 // TODO: What do we do about session id?
@@ -487,3 +447,4 @@ int end_session_ocall()
 {
   return SGX_SUCCESS;
 }
+*/

+ 0 - 85
SgxProtobufSealerTransforms.cpp

@@ -1,85 +0,0 @@
-
-//#include "ProtobufSgxTransforms.h"
-#include "sgx_tae_service.h"
-#include "ProtobufSealerMessages.pb.h"
-#include <string>
-
-
-namespace ProtobufSgxTransforms {
-  namespace {
-    void encode_counter_id_and_value_to_protobuf(sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
-        protobuf_plaintext.mutable_monotonic_counter_id()->set_nonce(sgx_monotonic_counter_id->nonce, SGX_MC_UUID_NONCE_SIZE);
-        protobuf_plaintext.mutable_monotonic_counter_id()->set_counter_id(sgx_monotonic_counter_id->counter_id, SGX_MC_UUID_COUNTER_ID_SIZE);
-        protobuf_plaintext.set_monotonic_counter_value(*sgx_monotonic_counter_value);
-    }
-    void encode_message_to_protobuf(std::string& plaintext, protobuf_plaintext_seal_message& protobuf_plaintext) {
-      protobuf_plaintext.set_message(plaintext);
-    }
-    void encode_message_and_counter_to_protobuf(std::string& plaintext, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
-
-      encode_counter_id_and_value_to_protobuf(sgx_counter_id, sgx_counter_value, protobuf_plaintext);
-      encode_message_to_protobuf(plaintext, protobuf_plaintext);
-    }
-  }
-
-
-
-  uint32_t encode_message_and_counter_to_protobuf_string(std::string& plaintext, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value, std::string& serialized_plaintext) {
-    uint32_t ret;
-    protobuf_plaintext_seal_message protobuf_plaintext;
-    uint32_t total_plaintext_length;
-
-    encode_message_and_counter_to_protobuf(plaintext, sgx_counter_id, sgx_counter_value, protobuf_plaintext);
-
-    total_plaintext_length = protobuf_plaintext.ByteSize(); // TODO: This assignment could be problematic on platforms where the max. positive value for signed int is greater than that for unsigned int?
-    if(total_plaintext_length<=0)
-      return 1;
-
-    if(!protobuf_plaintext.SerializeToString(&serialized_plaintext))
-      return 1;
-    return 0;
-  }
-
-  void decode_protobuf_message_to_counter_id_and_value(std::string& temp_plaintext_str, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value) {
-/*	protobuf_plaintext_seal_message protobuf_encoded_msg; protobuf_encoded_msg.ParseFromString(temp_plaintext_str);
-
-    std::string protobuf_nonce = protobuf_encoded_msg.mutable_monotonic_counter_id()->nonce();
-    std::vector<uint8_t> sgx_nonce_vector(protobuf_nonce.begin(), protobuf_nonce.end());
-    uint8_t* id_char = &sgx_nonce_vector[0]; int counter;
-    for(counter=0; counter<SGX_MC_UUID_NONCE_SIZE; counter++)
-    {
-       sgx_counter_id->nonce[counter]=*id_char; id_char++;
-    }
-
-    std::string protobuf_counter_id = protobuf_encoded_msg.mutable_monotonic_counter_id()->counter_id();
-    std::vector<uint8_t> sgx_counter_id_vector(protobuf_counter_id.begin(), protobuf_counter_id.end());
-    id_char = &sgx_counter_id_vector[0];
-    for(counter=0; counter<SGX_MC_UUID_COUNTER_ID_SIZE; counter++)
-    {
-       sgx_counter_id->counter_id[counter]=*id_char; id_char++; 
-    }
-
-    *sgx_counter_value=protobuf_encoded_msg.monotonic_counter_value();
-*/
-    }
-
-  void decode_protobuf_message_to_plaintext(std::string& temp_plaintext_str , std::string& plaintext) {
-    protobuf_plaintext_seal_message protobuf_encoded_msg; protobuf_encoded_msg.ParseFromString(temp_plaintext_str);
-    plaintext = protobuf_encoded_msg.message();
-  }
-
-/*  namespace {
-    void encode_counter_id_and_value_to_protobuf(sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
-        protobuf_plaintext->mutable_monotonic_counter_id()->set_nonce(sgx_monotonic_counter_id->nonce, SGX_MC_UUID_NONCE_SIZE);
-        protobuf_plaintext->mutable_monotonic_counter_id()->set_counter_id(sgx_monotonic_counter_id->counter_id, SGX_MC_UUID_COUNTER_ID_SIZE);
-        protobuf_plaintext->set_monotonic_counter_value(*sgx_monotonic_counter_value);
-    }
-    void encode_message_to_protobuf(string& plaintext, protobuf_plaintext_seal_message& protobuf_plaintext) {
-      protobuf_plaintext->set_message(plaintext);
-    }
-    void encode_message_and_counter_to_protobuf(std::string& plaintext, sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value, protobuf_plaintext_seal_message& protobuf_plaintext) {
-      encode_counter_id_and_value_to_protobuf(&sgx_counter_id, &sgx_counter_value, protobuf_plaintext);
-      encode_message_to_protobuf(plaintext, protobuf_plaintext);
-    }
-  }*/
-}

+ 14 - 154
SgxSealer.cpp

@@ -31,186 +31,53 @@
 
 #include "sgx.h"
 #include "sgx_tseal.h"  // For sgx_seal_data, sgx_calc_sealed_data_size, sgx_get_encrypt_txt_len, sgx_unseal_data
-#include "sgx_tae_service.h" // For create, read counters and pse_session. Also exports SGX_MC_UUID_NONCE_SIZE and SGX_MC_UUID_COUNTER_ID_SIZE, TODO: apparently also ne$
-#include "sgx_uae_service.h" // for sgx_ps_get_cap and SGX_IS_MONOTONIC_COUNTER_AVAILABLE
 #include <string>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <vector>
-//#include "ProtobufSgxTransforms.h"
-#include "ProtobufSgxSealerTransforms.h"
 
 
-//class Sealer {
-
-//  namespace {
-    uint32_t verify_sgx_monotonic_counter_value(sgx_mc_uuid_t* sgx_monotonic_counter_id, uint32_t* sgx_monotonic_counter_value)
-    {
-/*        uint32_t ret = 0;
-        uint32_t expected_counter_value;
-        int busy_retry_times = 2;
-
-        do{
-            ret = sgx_create_pse_session();
-        }while (ret == SGX_ERROR_BUSY && busy_retry_times--);
-        if (ret != SGX_SUCCESS)
-            return ret;
-
-        ret = sgx_read_monotonic_counter(sgx_monotonic_counter_id,&expected_counter_value);
-        if(ret != SGX_SUCCESS)
-        {
-            switch(ret)
-            {
-            case SGX_ERROR_SERVICE_UNAVAILABLE:
-                // Architecture Enclave Service Manager is not installed or not working properly.
-                    break;
-            case SGX_ERROR_SERVICE_TIMEOUT:
-                // retry the operation later
-                    break;
-            case SGX_ERROR_BUSY:
-                // retry the operation later
-                    break;
-            case SGX_ERROR_MC_NOT_FOUND:
-                // the the Monotonic Counter ID is invalid.
-                    break;
-            default:
-                // other errors
-                break;
-            }
-        }
-        else if(expected_counter_value!=*sgx_monotonic_counter_value)
-        {
-            ret = 3; //REPLAY_DETECTED; // This must be by the OS.
-        }
-        sgx_close_pse_session();
-*/
-        return SGX_SUCCESS; //ret;
-    }
-
-    uint32_t create_sgx_monotonic_counter_id_and_value(sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value)
-    {
-    int counter=0;
-    for(counter=0;counter<3;counter++)
-    {
-       sgx_counter_id->nonce[counter]=1;
-    }
-    for(counter=0;counter<13;counter++)
-    {
-       sgx_counter_id->counter_id[counter]=1;
-    }
-
-//sgx_counter_id->nonce={0,0,0,0}; 
-//sgx_counter_id->counter_id={0,0,0,0,0,0,0,0,0,0,0,0,0}; 
-*sgx_counter_value=1; 
-/*
-      uint32_t ret = 0;
-      int busy_retry_times = 2;
-
-      do{
-          ret = sgx_create_pse_session();
-      }while (ret == SGX_ERROR_BUSY && busy_retry_times--);
-      if (ret != SGX_SUCCESS)
-          return ret;
-
-      ret = sgx_create_monotonic_counter(sgx_counter_id,sgx_counter_value);
-          if(ret != SGX_SUCCESS)
-          {
-              switch(ret)
-              {
-              case SGX_ERROR_SERVICE_UNAVAILABLE:
-                  // Architecture Enclave Service Manager is not installed or not working properly
-                  break;
-              case SGX_ERROR_SERVICE_TIMEOUT:
-                  // retry the operation latersgx_create_monotonic_counter
-                  break;
-              case SGX_ERROR_BUSY:
-                  // retry the operation later
-                  break;
-              case SGX_ERROR_MC_OVER_QUOTA:
-                  // SGX Platform Service enforces a quota scheme on the Monotonic Counters a SGX app can maintain. the enclave has reached the quota.
-                  break;
-              case SGX_ERROR_MC_USED_UP:
-                  // the Monotonic Counter has been used up and cannot create Monotonic Counter anymore.
-                  break;
-              default:
-                  // other errors
-                  break;
-              }
-          }
-          
-
-      sgx_close_pse_session(); // TODO: Check that we should do it here.
-*/
-  return SGX_SUCCESS; // ret; 
-    }
 
-
-  uint32_t initialize_pse_and_counter()
-  {
-    /*
-    sgx_status_t sgx_ret;
-    sgx_ps_cap_t ps_cap;
-    memset(&ps_cap, 0, sizeof(sgx_ps_cap_t));
-    sgx_ret = sgx_get_ps_cap(&ps_cap);
-    if (sgx_ret)
-    {
-    //    printf("cannot get platform service capability, error code = 0x%d", sgx_ret); 
-        return sgx_ret;
-    }
-    if (!SGX_IS_MONOTONIC_COUNTER_AVAILABLE(ps_cap))
-    {
-    //    printf("\nmonotonic counter is not supported\n");
-        return SGX_ERROR_SERVICE_UNAVAILABLE;
-    }*/
-    return SGX_SUCCESS;
-  }
+//class Sealer {
  uint32_t seal_message(std::string& plaintext_str, std::string& sgx_sealed_msg_str)
  {
-//   std::string protobuf_plaintext_str;
    uint32_t ret;
-//   sgx_mc_uuid_t sgx_counter_id;
-//   uint32_t sgx_counter_value;
+
    uint8_t* sgx_sealed_msg;
    uint32_t expected_sealed_msg_length;
 
-/*   ret = create_sgx_monotonic_counter_id_and_value(&sgx_counter_id, &sgx_counter_value);
-   if(ret!=SGX_SUCCESS)
-     return ret;
-
-   ProtobufSgxTransforms::encode_message_and_counter_to_protobuf_string(plaintext_str, &sgx_counter_id, &sgx_counter_value, protobuf_plaintext_str);
-*/   expected_sealed_msg_length = sgx_calc_sealed_data_size(0, 9);
+   expected_sealed_msg_length = sgx_calc_sealed_data_size(0, 9);
    if(expected_sealed_msg_length == 0xFFFFFFFF)
        return 1;
    sgx_sealed_msg = (uint8_t*)malloc(expected_sealed_msg_length); // Doesn't change with protobufs - convert the data here to protobuf format after it is initialized
 
-   ret = sgx_seal_data(0, NULL, 9, (uint8_t*) plaintext_str.c_str(), expected_sealed_msg_length, (sgx_sealed_data_t*) sgx_sealed_msg); 
+   ret = sgx_seal_data(0, NULL, 9, (uint8_t*) plaintext_str.c_str(), expected_sealed_msg_length, (sgx_sealed_data_t*) sgx_sealed_msg);
    sgx_sealed_msg_str = std::string((char*)sgx_sealed_msg, expected_sealed_msg_length); // TODO: Fishy conversion.
    free(sgx_sealed_msg);
    return ret;
  }
 
  uint32_t unseal_and_verify_sealed_message(std::string& sgx_sealed_msg_str, std::string& plaintext)
- {    
+ {
     uint32_t ret = 0;
-    uint8_t* sgx_sealed_msg; 
+    uint8_t* sgx_sealed_msg;
     uint8_t* temp_plaintext;
     std::string protobuf_encoded_str; std::string decoded_plaintext;
-    sgx_mc_uuid_t sgx_counter_id;
     uint32_t sgx_counter_value;
     uint32_t expected_plaintext_msg_length;
 
-    std::vector<uint8_t> sgx_sealed_msg_vector(sgx_sealed_msg_str.begin(), sgx_sealed_msg_str.end());// TODO: Add null termination? 
-    sgx_sealed_msg = &sgx_sealed_msg_vector[0]; 
+    std::vector<uint8_t> sgx_sealed_msg_vector(sgx_sealed_msg_str.begin(), sgx_sealed_msg_str.end());// TODO: Add null termination?
+    sgx_sealed_msg = &sgx_sealed_msg_vector[0];
     expected_plaintext_msg_length = sgx_get_encrypt_txt_len((sgx_sealed_data_t*)sgx_sealed_msg);
     if(expected_plaintext_msg_length == 0xffffffff)
       return 1;
-//    return expected_plaintext_msg_length;
+
     temp_plaintext = (uint8_t*)malloc( expected_plaintext_msg_length );
     ret = sgx_unseal_data((sgx_sealed_data_t*)sgx_sealed_msg, NULL, 0, temp_plaintext, &expected_plaintext_msg_length);
     if(ret != SGX_SUCCESS)
     {
-	free(temp_plaintext); 
+	free(temp_plaintext);
         switch(ret)
         {
         case SGX_ERROR_MAC_MISMATCH:
@@ -235,18 +102,11 @@
         return ret;
     }
 
-    protobuf_encoded_str = std::string((char*)temp_plaintext, expected_plaintext_msg_length); // TODO: Fishy conversion. 
+    protobuf_encoded_str = std::string((char*)temp_plaintext, expected_plaintext_msg_length); // TODO: Fishy conversion.
 
-    free(temp_plaintext); 
-/*    ProtobufSgxTransforms::decode_protobuf_message_to_counter_id_and_value(protobuf_encoded_str, &sgx_counter_id, &sgx_counter_value);
-    ret = verify_sgx_monotonic_counter_value(&sgx_counter_id, &sgx_counter_value);
-    if (ret == SGX_SUCCESS)
-    {
-          ProtobufSgxTransforms::decode_protobuf_message_to_plaintext(protobuf_encoded_str, decoded_plaintext);
-          plaintext = decoded_plaintext;
-    }
-*/plaintext = protobuf_encoded_str; 
-    return ret; 
+    free(temp_plaintext);
+    plaintext = protobuf_encoded_str;
+    return ret;
   }
 
 //}

+ 66 - 0
crypto.cpp

@@ -0,0 +1,66 @@
+#include <stdio.h> 
+#include <string> 
+#include <memory>
+using std::unique_ptr;
+
+#include <openssl/bn.h>
+#include <openssl/rsa.h>
+#include <openssl/sha.h>
+using BN_ptr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
+using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
+
+uint32_t generate_rsa_key(std::string& priv_key_str, std::string& pub_key_str)
+{
+    int rc;
+
+    RSA_ptr rsa(RSA_new(), ::RSA_free);
+    BN_ptr bn(BN_new(), ::BN_free);
+
+    rc = BN_set_word(bn.get(), 3);
+    if(rc != 1)
+      return 0x1;
+
+    rc = RSA_generate_key_ex(rsa.get(), 3072, bn.get(), NULL);
+    if(rc != 1)
+      return 0x2;
+	printf("Generated key\n"); fflush(stdout); 
+/*    int pub_key_der_encoded_len, priv_key_der_encoded_len;
+    unsigned char *pub_key_der, priv_key_der;
+
+    pub_key_der = NULL;
+    pub_key_der_encoded_len = i2d_RSAPublicKey(rsa.get(), (unsigned char**) &pub_key_der);
+    if (pub_key_der_encoded_len < 0)
+      return 0x3;
+
+    priv_key_der = NULL;
+    priv_key_der_encoded_len = i2d_RSAPrivateKey(rsa.get(), (unsigned char**) &priv_key_der);
+    if (priv_key_der_encoded_len < 0)
+      return 0x4;
+    printf("Done\n"); fflush(stdout); 
+//   priv_key_str=std::string(priv_key_der, priv_key_der_encoded_len); //, priv_key_der); 
+//   pub_key_str=std::string(pub_key_der, pub_key_der_encoded_len);  
+*/
+
+    const BIGNUM* n_internal_bigendian_struct;
+    RSA_get0_key(rsa.get(), &n_internal_bigendian_struct, NULL, NULL);
+    BIGNUM* n_bigendian_struct = BN_dup(n_internal_bigendian_struct); 
+
+    uint32_t count; 
+    int n_bignum_length=BN_num_bytes(n_bigendian_struct);
+    unsigned char *n_bigendian = (unsigned char*) malloc(n_bignum_length);
+    int length_bignum_le = BN_bn2bin(n_bigendian_struct, n_bigendian); 
+    unsigned char* n_littleendian = (unsigned char*) malloc(length_bignum_le);
+    for(count=0; count<length_bignum_le; count++)
+		n_littleendian[count] = n_bigendian[length_bignum_le-count-1]; 
+    unsigned char hash[SHA256_DIGEST_LENGTH];
+    SHA256_CTX sha256;
+    SHA256_Init(&sha256);
+    SHA256_Update(&sha256, n_littleendian, length_bignum_le);
+    SHA256_Final(hash, &sha256);
+// TODO: Return hash of the public key for now
+// TODO: Print public key 
+// TODO: Use EVP funs - https://www.openssl.org/docs/man1.1.0/crypto/EVP_DigestInit.html
+   free(n_bigendian); free(n_littleendian); 
+   return 0; //length_bignum_le; 
+}
+

+ 1 - 0
include/ProtobufLAInitiator.h

@@ -0,0 +1 @@
+int local_attestation_initiator(int port);

+ 0 - 560
include/ProtobufSealerMessages.pb.h

@@ -1,560 +0,0 @@
-// Generated by the protocol buffer compiler.  DO NOT EDIT!
-// source: ProtobufSealerMessages.proto
-
-#ifndef PROTOBUF_ProtobufSealerMessages_2eproto__INCLUDED
-#define PROTOBUF_ProtobufSealerMessages_2eproto__INCLUDED
-
-#include <string>
-
-#include <google/protobuf/stubs/common.h>
-
-#if GOOGLE_PROTOBUF_VERSION < 2006000
-#error This file was generated by a newer version of protoc which is
-#error incompatible with your Protocol Buffer headers.  Please update
-#error your headers.
-#endif
-#if 2006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
-#error This file was generated by an older version of protoc which is
-#error incompatible with your Protocol Buffer headers.  Please
-#error regenerate this file with a newer version of protoc.
-#endif
-
-#include <google/protobuf/generated_message_util.h>
-#include <google/protobuf/message.h>
-#include <google/protobuf/repeated_field.h>
-#include <google/protobuf/extension_set.h>
-#include <google/protobuf/unknown_field_set.h>
-// @@protoc_insertion_point(includes)
-
-// Internal implementation detail -- do not call these.
-void  protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-void protobuf_AssignDesc_ProtobufSealerMessages_2eproto();
-void protobuf_ShutdownFile_ProtobufSealerMessages_2eproto();
-
-class protobuf_sgx_mc_uuid_t;
-class protobuf_plaintext_seal_message;
-
-// ===================================================================
-
-class protobuf_sgx_mc_uuid_t : public ::google::protobuf::Message {
- public:
-  protobuf_sgx_mc_uuid_t();
-  virtual ~protobuf_sgx_mc_uuid_t();
-
-  protobuf_sgx_mc_uuid_t(const protobuf_sgx_mc_uuid_t& from);
-
-  inline protobuf_sgx_mc_uuid_t& operator=(const protobuf_sgx_mc_uuid_t& from) {
-    CopyFrom(from);
-    return *this;
-  }
-
-  inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
-    return _unknown_fields_;
-  }
-
-  inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
-    return &_unknown_fields_;
-  }
-
-  static const ::google::protobuf::Descriptor* descriptor();
-  static const protobuf_sgx_mc_uuid_t& default_instance();
-
-  void Swap(protobuf_sgx_mc_uuid_t* other);
-
-  // implements Message ----------------------------------------------
-
-  protobuf_sgx_mc_uuid_t* New() const;
-  void CopyFrom(const ::google::protobuf::Message& from);
-  void MergeFrom(const ::google::protobuf::Message& from);
-  void CopyFrom(const protobuf_sgx_mc_uuid_t& from);
-  void MergeFrom(const protobuf_sgx_mc_uuid_t& from);
-  void Clear();
-  bool IsInitialized() const;
-
-  int ByteSize() const;
-  bool MergePartialFromCodedStream(
-      ::google::protobuf::io::CodedInputStream* input);
-  void SerializeWithCachedSizes(
-      ::google::protobuf::io::CodedOutputStream* output) const;
-  ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
-  int GetCachedSize() const { return _cached_size_; }
-  private:
-  void SharedCtor();
-  void SharedDtor();
-  void SetCachedSize(int size) const;
-  public:
-  ::google::protobuf::Metadata GetMetadata() const;
-
-  // nested types ----------------------------------------------------
-
-  // accessors -------------------------------------------------------
-
-  // required bytes nonce = 1;
-  inline bool has_nonce() const;
-  inline void clear_nonce();
-  static const int kNonceFieldNumber = 1;
-  inline const ::std::string& nonce() const;
-  inline void set_nonce(const ::std::string& value);
-  inline void set_nonce(const char* value);
-  inline void set_nonce(const void* value, size_t size);
-  inline ::std::string* mutable_nonce();
-  inline ::std::string* release_nonce();
-  inline void set_allocated_nonce(::std::string* nonce);
-
-  // required bytes counter_id = 2;
-  inline bool has_counter_id() const;
-  inline void clear_counter_id();
-  static const int kCounterIdFieldNumber = 2;
-  inline const ::std::string& counter_id() const;
-  inline void set_counter_id(const ::std::string& value);
-  inline void set_counter_id(const char* value);
-  inline void set_counter_id(const void* value, size_t size);
-  inline ::std::string* mutable_counter_id();
-  inline ::std::string* release_counter_id();
-  inline void set_allocated_counter_id(::std::string* counter_id);
-
-  // @@protoc_insertion_point(class_scope:protobuf_sgx_mc_uuid_t)
- private:
-  inline void set_has_nonce();
-  inline void clear_has_nonce();
-  inline void set_has_counter_id();
-  inline void clear_has_counter_id();
-
-  ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
-  ::google::protobuf::uint32 _has_bits_[1];
-  mutable int _cached_size_;
-  ::std::string* nonce_;
-  ::std::string* counter_id_;
-  friend void  protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-  friend void protobuf_AssignDesc_ProtobufSealerMessages_2eproto();
-  friend void protobuf_ShutdownFile_ProtobufSealerMessages_2eproto();
-
-  void InitAsDefaultInstance();
-  static protobuf_sgx_mc_uuid_t* default_instance_;
-};
-// -------------------------------------------------------------------
-
-class protobuf_plaintext_seal_message : public ::google::protobuf::Message {
- public:
-  protobuf_plaintext_seal_message();
-  virtual ~protobuf_plaintext_seal_message();
-
-  protobuf_plaintext_seal_message(const protobuf_plaintext_seal_message& from);
-
-  inline protobuf_plaintext_seal_message& operator=(const protobuf_plaintext_seal_message& from) {
-    CopyFrom(from);
-    return *this;
-  }
-
-  inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
-    return _unknown_fields_;
-  }
-
-  inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
-    return &_unknown_fields_;
-  }
-
-  static const ::google::protobuf::Descriptor* descriptor();
-  static const protobuf_plaintext_seal_message& default_instance();
-
-  void Swap(protobuf_plaintext_seal_message* other);
-
-  // implements Message ----------------------------------------------
-
-  protobuf_plaintext_seal_message* New() const;
-  void CopyFrom(const ::google::protobuf::Message& from);
-  void MergeFrom(const ::google::protobuf::Message& from);
-  void CopyFrom(const protobuf_plaintext_seal_message& from);
-  void MergeFrom(const protobuf_plaintext_seal_message& from);
-  void Clear();
-  bool IsInitialized() const;
-
-  int ByteSize() const;
-  bool MergePartialFromCodedStream(
-      ::google::protobuf::io::CodedInputStream* input);
-  void SerializeWithCachedSizes(
-      ::google::protobuf::io::CodedOutputStream* output) const;
-  ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
-  int GetCachedSize() const { return _cached_size_; }
-  private:
-  void SharedCtor();
-  void SharedDtor();
-  void SetCachedSize(int size) const;
-  public:
-  ::google::protobuf::Metadata GetMetadata() const;
-
-  // nested types ----------------------------------------------------
-
-  // accessors -------------------------------------------------------
-
-  // required .protobuf_sgx_mc_uuid_t monotonic_counter_id = 1;
-  inline bool has_monotonic_counter_id() const;
-  inline void clear_monotonic_counter_id();
-  static const int kMonotonicCounterIdFieldNumber = 1;
-  inline const ::protobuf_sgx_mc_uuid_t& monotonic_counter_id() const;
-  inline ::protobuf_sgx_mc_uuid_t* mutable_monotonic_counter_id();
-  inline ::protobuf_sgx_mc_uuid_t* release_monotonic_counter_id();
-  inline void set_allocated_monotonic_counter_id(::protobuf_sgx_mc_uuid_t* monotonic_counter_id);
-
-  // required uint32 monotonic_counter_value = 2;
-  inline bool has_monotonic_counter_value() const;
-  inline void clear_monotonic_counter_value();
-  static const int kMonotonicCounterValueFieldNumber = 2;
-  inline ::google::protobuf::uint32 monotonic_counter_value() const;
-  inline void set_monotonic_counter_value(::google::protobuf::uint32 value);
-
-  // required bytes message = 3;
-  inline bool has_message() const;
-  inline void clear_message();
-  static const int kMessageFieldNumber = 3;
-  inline const ::std::string& message() const;
-  inline void set_message(const ::std::string& value);
-  inline void set_message(const char* value);
-  inline void set_message(const void* value, size_t size);
-  inline ::std::string* mutable_message();
-  inline ::std::string* release_message();
-  inline void set_allocated_message(::std::string* message);
-
-  // @@protoc_insertion_point(class_scope:protobuf_plaintext_seal_message)
- private:
-  inline void set_has_monotonic_counter_id();
-  inline void clear_has_monotonic_counter_id();
-  inline void set_has_monotonic_counter_value();
-  inline void clear_has_monotonic_counter_value();
-  inline void set_has_message();
-  inline void clear_has_message();
-
-  ::google::protobuf::UnknownFieldSet _unknown_fields_;
-
-  ::google::protobuf::uint32 _has_bits_[1];
-  mutable int _cached_size_;
-  ::protobuf_sgx_mc_uuid_t* monotonic_counter_id_;
-  ::std::string* message_;
-  ::google::protobuf::uint32 monotonic_counter_value_;
-  friend void  protobuf_AddDesc_ProtobufSealerMessages_2eproto();
-  friend void protobuf_AssignDesc_ProtobufSealerMessages_2eproto();
-  friend void protobuf_ShutdownFile_ProtobufSealerMessages_2eproto();
-
-  void InitAsDefaultInstance();
-  static protobuf_plaintext_seal_message* default_instance_;
-};
-// ===================================================================
-
-
-// ===================================================================
-
-// protobuf_sgx_mc_uuid_t
-
-// required bytes nonce = 1;
-inline bool protobuf_sgx_mc_uuid_t::has_nonce() const {
-  return (_has_bits_[0] & 0x00000001u) != 0;
-}
-inline void protobuf_sgx_mc_uuid_t::set_has_nonce() {
-  _has_bits_[0] |= 0x00000001u;
-}
-inline void protobuf_sgx_mc_uuid_t::clear_has_nonce() {
-  _has_bits_[0] &= ~0x00000001u;
-}
-inline void protobuf_sgx_mc_uuid_t::clear_nonce() {
-  if (nonce_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    nonce_->clear();
-  }
-  clear_has_nonce();
-}
-inline const ::std::string& protobuf_sgx_mc_uuid_t::nonce() const {
-  // @@protoc_insertion_point(field_get:protobuf_sgx_mc_uuid_t.nonce)
-  return *nonce_;
-}
-inline void protobuf_sgx_mc_uuid_t::set_nonce(const ::std::string& value) {
-  set_has_nonce();
-  if (nonce_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    nonce_ = new ::std::string;
-  }
-  nonce_->assign(value);
-  // @@protoc_insertion_point(field_set:protobuf_sgx_mc_uuid_t.nonce)
-}
-inline void protobuf_sgx_mc_uuid_t::set_nonce(const char* value) {
-  set_has_nonce();
-  if (nonce_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    nonce_ = new ::std::string;
-  }
-  nonce_->assign(value);
-  // @@protoc_insertion_point(field_set_char:protobuf_sgx_mc_uuid_t.nonce)
-}
-inline void protobuf_sgx_mc_uuid_t::set_nonce(const void* value, size_t size) {
-  set_has_nonce();
-  if (nonce_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    nonce_ = new ::std::string;
-  }
-  nonce_->assign(reinterpret_cast<const char*>(value), size);
-  // @@protoc_insertion_point(field_set_pointer:protobuf_sgx_mc_uuid_t.nonce)
-}
-inline ::std::string* protobuf_sgx_mc_uuid_t::mutable_nonce() {
-  set_has_nonce();
-  if (nonce_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    nonce_ = new ::std::string;
-  }
-  // @@protoc_insertion_point(field_mutable:protobuf_sgx_mc_uuid_t.nonce)
-  return nonce_;
-}
-inline ::std::string* protobuf_sgx_mc_uuid_t::release_nonce() {
-  clear_has_nonce();
-  if (nonce_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    return NULL;
-  } else {
-    ::std::string* temp = nonce_;
-    nonce_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-    return temp;
-  }
-}
-inline void protobuf_sgx_mc_uuid_t::set_allocated_nonce(::std::string* nonce) {
-  if (nonce_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    delete nonce_;
-  }
-  if (nonce) {
-    set_has_nonce();
-    nonce_ = nonce;
-  } else {
-    clear_has_nonce();
-    nonce_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-  }
-  // @@protoc_insertion_point(field_set_allocated:protobuf_sgx_mc_uuid_t.nonce)
-}
-
-// required bytes counter_id = 2;
-inline bool protobuf_sgx_mc_uuid_t::has_counter_id() const {
-  return (_has_bits_[0] & 0x00000002u) != 0;
-}
-inline void protobuf_sgx_mc_uuid_t::set_has_counter_id() {
-  _has_bits_[0] |= 0x00000002u;
-}
-inline void protobuf_sgx_mc_uuid_t::clear_has_counter_id() {
-  _has_bits_[0] &= ~0x00000002u;
-}
-inline void protobuf_sgx_mc_uuid_t::clear_counter_id() {
-  if (counter_id_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    counter_id_->clear();
-  }
-  clear_has_counter_id();
-}
-inline const ::std::string& protobuf_sgx_mc_uuid_t::counter_id() const {
-  // @@protoc_insertion_point(field_get:protobuf_sgx_mc_uuid_t.counter_id)
-  return *counter_id_;
-}
-inline void protobuf_sgx_mc_uuid_t::set_counter_id(const ::std::string& value) {
-  set_has_counter_id();
-  if (counter_id_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    counter_id_ = new ::std::string;
-  }
-  counter_id_->assign(value);
-  // @@protoc_insertion_point(field_set:protobuf_sgx_mc_uuid_t.counter_id)
-}
-inline void protobuf_sgx_mc_uuid_t::set_counter_id(const char* value) {
-  set_has_counter_id();
-  if (counter_id_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    counter_id_ = new ::std::string;
-  }
-  counter_id_->assign(value);
-  // @@protoc_insertion_point(field_set_char:protobuf_sgx_mc_uuid_t.counter_id)
-}
-inline void protobuf_sgx_mc_uuid_t::set_counter_id(const void* value, size_t size) {
-  set_has_counter_id();
-  if (counter_id_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    counter_id_ = new ::std::string;
-  }
-  counter_id_->assign(reinterpret_cast<const char*>(value), size);
-  // @@protoc_insertion_point(field_set_pointer:protobuf_sgx_mc_uuid_t.counter_id)
-}
-inline ::std::string* protobuf_sgx_mc_uuid_t::mutable_counter_id() {
-  set_has_counter_id();
-  if (counter_id_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    counter_id_ = new ::std::string;
-  }
-  // @@protoc_insertion_point(field_mutable:protobuf_sgx_mc_uuid_t.counter_id)
-  return counter_id_;
-}
-inline ::std::string* protobuf_sgx_mc_uuid_t::release_counter_id() {
-  clear_has_counter_id();
-  if (counter_id_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    return NULL;
-  } else {
-    ::std::string* temp = counter_id_;
-    counter_id_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-    return temp;
-  }
-}
-inline void protobuf_sgx_mc_uuid_t::set_allocated_counter_id(::std::string* counter_id) {
-  if (counter_id_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    delete counter_id_;
-  }
-  if (counter_id) {
-    set_has_counter_id();
-    counter_id_ = counter_id;
-  } else {
-    clear_has_counter_id();
-    counter_id_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-  }
-  // @@protoc_insertion_point(field_set_allocated:protobuf_sgx_mc_uuid_t.counter_id)
-}
-
-// -------------------------------------------------------------------
-
-// protobuf_plaintext_seal_message
-
-// required .protobuf_sgx_mc_uuid_t monotonic_counter_id = 1;
-inline bool protobuf_plaintext_seal_message::has_monotonic_counter_id() const {
-  return (_has_bits_[0] & 0x00000001u) != 0;
-}
-inline void protobuf_plaintext_seal_message::set_has_monotonic_counter_id() {
-  _has_bits_[0] |= 0x00000001u;
-}
-inline void protobuf_plaintext_seal_message::clear_has_monotonic_counter_id() {
-  _has_bits_[0] &= ~0x00000001u;
-}
-inline void protobuf_plaintext_seal_message::clear_monotonic_counter_id() {
-  if (monotonic_counter_id_ != NULL) monotonic_counter_id_->::protobuf_sgx_mc_uuid_t::Clear();
-  clear_has_monotonic_counter_id();
-}
-inline const ::protobuf_sgx_mc_uuid_t& protobuf_plaintext_seal_message::monotonic_counter_id() const {
-  // @@protoc_insertion_point(field_get:protobuf_plaintext_seal_message.monotonic_counter_id)
-  return monotonic_counter_id_ != NULL ? *monotonic_counter_id_ : *default_instance_->monotonic_counter_id_;
-}
-inline ::protobuf_sgx_mc_uuid_t* protobuf_plaintext_seal_message::mutable_monotonic_counter_id() {
-  set_has_monotonic_counter_id();
-  if (monotonic_counter_id_ == NULL) monotonic_counter_id_ = new ::protobuf_sgx_mc_uuid_t;
-  // @@protoc_insertion_point(field_mutable:protobuf_plaintext_seal_message.monotonic_counter_id)
-  return monotonic_counter_id_;
-}
-inline ::protobuf_sgx_mc_uuid_t* protobuf_plaintext_seal_message::release_monotonic_counter_id() {
-  clear_has_monotonic_counter_id();
-  ::protobuf_sgx_mc_uuid_t* temp = monotonic_counter_id_;
-  monotonic_counter_id_ = NULL;
-  return temp;
-}
-inline void protobuf_plaintext_seal_message::set_allocated_monotonic_counter_id(::protobuf_sgx_mc_uuid_t* monotonic_counter_id) {
-  delete monotonic_counter_id_;
-  monotonic_counter_id_ = monotonic_counter_id;
-  if (monotonic_counter_id) {
-    set_has_monotonic_counter_id();
-  } else {
-    clear_has_monotonic_counter_id();
-  }
-  // @@protoc_insertion_point(field_set_allocated:protobuf_plaintext_seal_message.monotonic_counter_id)
-}
-
-// required uint32 monotonic_counter_value = 2;
-inline bool protobuf_plaintext_seal_message::has_monotonic_counter_value() const {
-  return (_has_bits_[0] & 0x00000002u) != 0;
-}
-inline void protobuf_plaintext_seal_message::set_has_monotonic_counter_value() {
-  _has_bits_[0] |= 0x00000002u;
-}
-inline void protobuf_plaintext_seal_message::clear_has_monotonic_counter_value() {
-  _has_bits_[0] &= ~0x00000002u;
-}
-inline void protobuf_plaintext_seal_message::clear_monotonic_counter_value() {
-  monotonic_counter_value_ = 0u;
-  clear_has_monotonic_counter_value();
-}
-inline ::google::protobuf::uint32 protobuf_plaintext_seal_message::monotonic_counter_value() const {
-  // @@protoc_insertion_point(field_get:protobuf_plaintext_seal_message.monotonic_counter_value)
-  return monotonic_counter_value_;
-}
-inline void protobuf_plaintext_seal_message::set_monotonic_counter_value(::google::protobuf::uint32 value) {
-  set_has_monotonic_counter_value();
-  monotonic_counter_value_ = value;
-  // @@protoc_insertion_point(field_set:protobuf_plaintext_seal_message.monotonic_counter_value)
-}
-
-// required bytes message = 3;
-inline bool protobuf_plaintext_seal_message::has_message() const {
-  return (_has_bits_[0] & 0x00000004u) != 0;
-}
-inline void protobuf_plaintext_seal_message::set_has_message() {
-  _has_bits_[0] |= 0x00000004u;
-}
-inline void protobuf_plaintext_seal_message::clear_has_message() {
-  _has_bits_[0] &= ~0x00000004u;
-}
-inline void protobuf_plaintext_seal_message::clear_message() {
-  if (message_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    message_->clear();
-  }
-  clear_has_message();
-}
-inline const ::std::string& protobuf_plaintext_seal_message::message() const {
-  // @@protoc_insertion_point(field_get:protobuf_plaintext_seal_message.message)
-  return *message_;
-}
-inline void protobuf_plaintext_seal_message::set_message(const ::std::string& value) {
-  set_has_message();
-  if (message_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    message_ = new ::std::string;
-  }
-  message_->assign(value);
-  // @@protoc_insertion_point(field_set:protobuf_plaintext_seal_message.message)
-}
-inline void protobuf_plaintext_seal_message::set_message(const char* value) {
-  set_has_message();
-  if (message_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    message_ = new ::std::string;
-  }
-  message_->assign(value);
-  // @@protoc_insertion_point(field_set_char:protobuf_plaintext_seal_message.message)
-}
-inline void protobuf_plaintext_seal_message::set_message(const void* value, size_t size) {
-  set_has_message();
-  if (message_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    message_ = new ::std::string;
-  }
-  message_->assign(reinterpret_cast<const char*>(value), size);
-  // @@protoc_insertion_point(field_set_pointer:protobuf_plaintext_seal_message.message)
-}
-inline ::std::string* protobuf_plaintext_seal_message::mutable_message() {
-  set_has_message();
-  if (message_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    message_ = new ::std::string;
-  }
-  // @@protoc_insertion_point(field_mutable:protobuf_plaintext_seal_message.message)
-  return message_;
-}
-inline ::std::string* protobuf_plaintext_seal_message::release_message() {
-  clear_has_message();
-  if (message_ == &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    return NULL;
-  } else {
-    ::std::string* temp = message_;
-    message_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-    return temp;
-  }
-}
-inline void protobuf_plaintext_seal_message::set_allocated_message(::std::string* message) {
-  if (message_ != &::google::protobuf::internal::GetEmptyStringAlreadyInited()) {
-    delete message_;
-  }
-  if (message) {
-    set_has_message();
-    message_ = message;
-  } else {
-    clear_has_message();
-    message_ = const_cast< ::std::string*>(&::google::protobuf::internal::GetEmptyStringAlreadyInited());
-  }
-  // @@protoc_insertion_point(field_set_allocated:protobuf_plaintext_seal_message.message)
-}
-
-
-// @@protoc_insertion_point(namespace_scope)
-
-#ifndef SWIG
-namespace google {
-namespace protobuf {
-
-
-}  // namespace google
-}  // namespace protobuf
-#endif  // SWIG
-
-// @@protoc_insertion_point(global_scope)
-
-#endif  // PROTOBUF_ProtobufSealerMessages_2eproto__INCLUDED

+ 0 - 2
include/ProtobufSgxLATransforms_Inititator.h

@@ -1,2 +0,0 @@
-int exchange_report_call(int fd, sgx_dh_msg2_t *dh_msg2, sgx_dh_msg3_t *dh_msg3);
-int session_request_call(int fd, sgx_dh_msg1_t *dh_msg1); 

+ 0 - 11
include/ProtobufSgxSealerTransforms.h

@@ -1,11 +0,0 @@
-#pragma once
-#include <string>
-#include <sgx_tae_service.h> // For sgx_mc_uuid_t structure. // TODO: Uncomment this. 
-
-namespace ProtobufSgxTransforms {
-  uint32_t encode_message_and_counter_to_protobuf_string(std::string& plaintext, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value, std::string& serialized_plaintext);
-//  void decode_protobuf_message_to_counter_id_and_value(protobuf_plaintext_seal_message& protobuf_unsealed_msg, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value);
-  void decode_protobuf_message_to_counter_id_and_value(std::string& temp_plaintext_str, sgx_mc_uuid_t* sgx_counter_id, uint32_t* sgx_counter_value);
-//  void decode_protobuf_message_to_plaintext(protobuf_plaintext_seal_message& protobuf_unsealed_msg, std::string& plaintext);
-  void decode_protobuf_message_to_plaintext(std::string& temp_plaintext_str , std::string& plaintext);
-}

+ 0 - 1
include/SgxCrypto.h

@@ -1 +0,0 @@
-uint32_t create_rsa_key_pair_for_signing_manifest();

+ 2 - 0
include/SgxProtobufLAInitiator.h

@@ -0,0 +1,2 @@
+uint32_t process_protobuf_dh_msg1_generate_protobuf_dh_msg2(protobuf_sgx_dh_msg1_t& protobuf_msg1, protobuf_sgx_dh_msg2_t& protobuf_msg2, uint32_t* session_id);
+uint32_t process_protobuf_dh_msg3(protobuf_sgx_dh_msg3_t& protobuf_msg3, uint32_t* session_id);

+ 3 - 0
include/SgxProtobufLAInitiator_Transforms.h

@@ -0,0 +1,3 @@
+int decode_msg1_from_protobuf( protobuf_sgx_dh_msg1_t& protobuf_dhmsg1, sgx_dh_msg1_t* native_dhmsg1);
+int decode_msg3_from_protobuf(protobuf_sgx_dh_msg3_t& protobuf_dhmsg3, sgx_dh_msg3_t* native_dhmsg3);
+void encode_msg2_to_protobuf( protobuf_sgx_dh_msg2_t& protobuf_dhmsg2, sgx_dh_msg2_t* native_dhmsg2);

+ 1 - 0
include/crypto.h

@@ -0,0 +1 @@
+uint32_t generate_rsa_key(std::string& priv_key_str, std::string& pub_key_str);

+ 0 - 1
include/ipc.h

@@ -1 +0,0 @@
-int set_up_socket_connect(int port); 

+ 0 - 42
systemIpc.cpp

@@ -1,42 +0,0 @@
-// For socket programming
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <stdlib.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdio.h>
-
-// Sets up a socket connected to the port passed as input - returns the socket FD on success and -1 on error.
-// Also prints the errno on error.
-int set_up_socket_connect(int port)
-{
-  int sock = 0;
-  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-  {
-    printf("\n Error in socket call - errno is %d \n", errno);
-    return -1;
-  }
-
-  struct sockaddr_in serv_addr;
-  memset(&serv_addr, '0', sizeof(serv_addr));
-
-  serv_addr.sin_family = AF_INET;
-  serv_addr.sin_port = htons(port);
-
-  // Convert IPv4 and IPv6 addresses from text to binary form
-  if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
-  {
-    printf("\nError in inet_pton - errno is %d\n", errno);
-    return -1;
-  }
-
-  if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
-  {
-    printf("\nError in connect - errno is %d \n", errno);
-    return -1;
-  }
-
-  return sock;
-}

+ 16 - 34
systemMain.cpp

@@ -4,14 +4,13 @@
 #include <errno.h>
 
 #include <fcntl.h>
-#include <sys/types.h> 
-#include <sys/stat.h> 
+#include <sys/types.h>
+#include <sys/stat.h>
 
 
-#include "ipc.h"
-#include "SgxLAInititator.h"
+#include "ProtobufLAInitiator.h"
 #include "SealerWrapper.h"
-#include "SgxCrypto.h" 
+#include "crypto.h"
 using namespace std;
 
 #define DECRYPTOR_PORT 3825
@@ -19,19 +18,6 @@ using namespace std;
 int __ImageBase=0;
 int verify_apache(std::string& path, std::string& keypair) {return 0; }
 
-int local_attestation(int port, uint8_t* mr_enclave, uint8_t* mr_signer, uint8_t* send_mr_signer)
-{
-  int decryptor_fd;
-  setbuf(stdout,NULL);
-  decryptor_fd=set_up_socket_connect(port);
-  if(decryptor_fd!=-1)
-  {
-    printf("Set up a socket with the other module\n");
-    return create_session(decryptor_fd, mr_enclave, mr_signer, send_mr_signer);
-  }
-  else
-    return 0xFFFFFFFF;
-}
 
 int main(int argc, char** argv)
 {
@@ -39,24 +25,20 @@ int main(int argc, char** argv)
   uint8_t expected_apache_mr_signer[SGX_HASH_SIZE] = {0x3};
   std::string apache_signature_keypair_private("1234567890");
   std::string apache_public_key;
-  uint8_t decryptor_mr_enclave[SGX_HASH_SIZE] = {0x1};
-  uint8_t decryptor_mr_signer[SGX_HASH_SIZE] = {0x2};
+  std::string apache_private_key2; 
+//  generate_rsa_key(apache_public_key, apache_private_key2); 
+
+//  uint8_t decryptor_mr_enclave[SGX_HASH_SIZE] = {0x1};
+//  uint8_t decryptor_mr_signer[SGX_HASH_SIZE] = {0x2};
   uint32_t return_sgx; uint32_t return_internal;
   std::string recovered_plaintext;
   uint32_t expected_sealed_msg_size=0;
-/*
-  printf("about to generate rsa key pair\n");
-  fflush(stdout); 
-  return_sgx=create_rsa_key_pair_for_signing_manifest();
-  if(return_sgx!=0)
+  return_internal = generate_rsa_key(apache_public_key, apache_private_key2); 
+  if(return_internal !=0)
   {
-	printf("Could not successfully create rsa key pair.\n"); 
-	fflush(stdout); 
-	return 0xFFFFFFFF;
+	printf("Could not generate RSA keypair - error 0x%x\n", return_internal); fflush(stdout); return return_internal; 
   }
-  printf("generated rsa key pair\n");
-  fflush(stdout); */
-
+  printf("Generated key pair - outside the fun\n"); fflush(stdout); 
   int sealed_file_fd = open("sealed_msg.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
   if(sealed_file_fd == -1)
   {
@@ -82,7 +64,7 @@ int main(int argc, char** argv)
   printf("\n Successfully sealed the plaintext %s to length 0x%x.\n", apache_signature_keypair_private.c_str(), expected_sealed_msg_size);
   fflush(stdout);
 
-  return_sgx = local_attestation(DECRYPTOR_PORT, decryptor_mr_enclave, decryptor_mr_signer, expected_apache_mr_signer);
+  return_sgx = local_attestation_initiator(DECRYPTOR_PORT);
   if(return_sgx != 0)
   {
     if(return_sgx== 0xFFFFFFFF)
@@ -92,7 +74,7 @@ int main(int argc, char** argv)
     }
     else
     {
-      printf("\nHad the following error in SGX local attestation: %d", return_sgx);
+      printf("\nHad the following error in SGX local attestation: 0x%x", return_sgx);
       fflush(stdout);
     }
     return return_sgx;
@@ -100,7 +82,7 @@ int main(int argc, char** argv)
   printf("\nSuccessful LA with port %d.\n", DECRYPTOR_PORT);
   fflush(stdout);
 
-  sleep(50);
+//  sleep(50);
   printf("\n z z z z z z  z z z z z z z  (sleeping for a bit) z z z z z  z z z (meant to emulate the '2nd' stage of validator, that will be rerun whenever Apache changes)\n");
   return_sgx = unseal_message_from_file(sealed_file_fd, recovered_plaintext, &expected_sealed_msg_size);
   if(return_sgx!=0 && return_sgx!=0xFFFFFFFF)

+ 1 - 4
systemSealerWrapper.cpp

@@ -6,13 +6,10 @@
 #include "SgxSealer.h"
 
 //class SealerWrapper {
-uint32_t init() {
-  return initialize_pse_and_counter();
-}
 
 uint32_t seal_message_to_file(int fd, std::string& plaintext, uint32_t* actual_sealed_msg_length)
 {
-  uint32_t ret;  ssize_t bytes_written; std::string sgx_sealed_msg; 
+  uint32_t ret;  ssize_t bytes_written; std::string sgx_sealed_msg;
   ret = seal_message(plaintext, sgx_sealed_msg);
   if(ret!=0)
 	return ret;