[Assignment-7] add SGX sample code from VM
All checks were successful
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (push) Successful in 1m2s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (push) Successful in 1m3s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (push) Successful in 1m0s
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (pull_request) Successful in 30s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (pull_request) Successful in 10s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (pull_request) Successful in 8s
All checks were successful
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (push) Successful in 1m2s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (push) Successful in 1m3s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (push) Successful in 1m0s
Latex Build / build-latex (Assignment 4 - Protokollsicherheit (Praxis)) (pull_request) Successful in 30s
Latex Build / build-latex (Assignment 5 - Software Security - Teil 1) (pull_request) Successful in 10s
Latex Build / build-latex (Assignment 6 - Software Security - Teil 2) (pull_request) Successful in 8s
This commit is contained in:
parent
5616ddc4e5
commit
ba8e969470
163 changed files with 24030 additions and 0 deletions
|
|
@ -0,0 +1,211 @@
|
|||
######## SGX SDK Settings ########
|
||||
SGX_SDK ?= /opt/intel/sgxsdk
|
||||
SGX_MODE ?= SIM
|
||||
SGX_ARCH ?= x64
|
||||
|
||||
ifeq ($(shell getconf LONG_BIT), 32)
|
||||
SGX_ARCH := x86
|
||||
else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
|
||||
SGX_ARCH := x86
|
||||
endif
|
||||
|
||||
ifeq ($(SGX_ARCH), x86)
|
||||
SGX_COMMON_CFLAGS := -m32
|
||||
SGX_LIBRARY_PATH := $(SGX_SDK)/lib
|
||||
SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
|
||||
SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
|
||||
else
|
||||
SGX_COMMON_CFLAGS := -m64
|
||||
SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
|
||||
SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
|
||||
SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
|
||||
endif
|
||||
|
||||
ifeq ($(SGX_DEBUG), 1)
|
||||
ifeq ($(SGX_PRERELEASE), 1)
|
||||
$(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(SGX_DEBUG), 1)
|
||||
SGX_COMMON_CFLAGS += -O0 -g
|
||||
else
|
||||
SGX_COMMON_CFLAGS += -O2
|
||||
endif
|
||||
|
||||
ifeq ($(SUPPLIED_KEY_DERIVATION), 1)
|
||||
SGX_COMMON_CFLAGS += -DSUPPLIED_KEY_DERIVATION
|
||||
endif
|
||||
|
||||
######## App Settings ########
|
||||
|
||||
ifneq ($(SGX_MODE), HW)
|
||||
Urts_Library_Name := sgx_urts_sim
|
||||
else
|
||||
Urts_Library_Name := sgx_urts
|
||||
endif
|
||||
|
||||
|
||||
App_Cpp_Files := isv_app/isv_app.cpp ../Util/LogBase.cpp ../Networking/NetworkManager.cpp ../Networking/Session.cpp ../Networking/Server.cpp \
|
||||
../Networking/Client.cpp ../Networking/NetworkManagerServer.cpp ../GoogleMessages/Messages.pb.cpp ../Networking/AbstractNetworkOps.cpp \
|
||||
../Util/UtilityFunctions.cpp ../Enclave/Enclave.cpp ../MessageHandler/MessageHandler.cpp ../Util/Base64.cpp
|
||||
|
||||
App_Include_Paths := -I../Util -Iservice_provider -I$(SGX_SDK)/include -Iheaders -I../Networking -Iisv_app -I../GoogleMessages -I/usr/local/include -I../Enclave \
|
||||
-I../MessageHandler
|
||||
|
||||
App_C_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths)
|
||||
|
||||
# Three configuration modes - Debug, prerelease, release
|
||||
# Debug - Macro DEBUG enabled.
|
||||
# Prerelease - Macro NDEBUG and EDEBUG enabled.
|
||||
# Release - Macro NDEBUG enabled.
|
||||
ifeq ($(SGX_DEBUG), 1)
|
||||
App_C_Flags += -DDEBUG -UNDEBUG -UEDEBUG
|
||||
else ifeq ($(SGX_PRERELEASE), 1)
|
||||
App_C_Flags += -DNDEBUG -DEDEBUG -UDEBUG
|
||||
else
|
||||
App_C_Flags += -DNDEBUG -UEDEBUG -UDEBUG
|
||||
endif
|
||||
|
||||
App_Cpp_Flags := $(App_C_Flags) -std=c++11 -DEnableServer
|
||||
App_Link_Flags := $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -L. -lsgx_ukey_exchange -lpthread -Wl,-rpath=$(CURDIR)/../sample_libcrypto -Wl,-rpath=$(CURDIR) -llog4cpp -lboost_system -lssl -lcrypto -lboost_thread -lprotobuf -L /usr/local/lib -ljsoncpp
|
||||
|
||||
ifneq ($(SGX_MODE), HW)
|
||||
App_Link_Flags += -lsgx_uae_service_sim
|
||||
else
|
||||
App_Link_Flags += -lsgx_uae_service
|
||||
endif
|
||||
|
||||
App_Cpp_Objects := $(App_Cpp_Files:.cpp=.o)
|
||||
|
||||
App_Name := app
|
||||
|
||||
|
||||
######## Enclave Settings ########
|
||||
ifneq ($(SGX_MODE), HW)
|
||||
Trts_Library_Name := sgx_trts_sim
|
||||
Service_Library_Name := sgx_tservice_sim
|
||||
else
|
||||
Trts_Library_Name := sgx_trts
|
||||
Service_Library_Name := sgx_tservice
|
||||
endif
|
||||
Crypto_Library_Name := sgx_tcrypto
|
||||
|
||||
Enclave_Cpp_Files := isv_enclave/isv_enclave.cpp
|
||||
Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport -I$(SGX_SDK)/include/crypto_px/include -I../Enclave/
|
||||
|
||||
Enclave_C_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(Enclave_Include_Paths)
|
||||
Enclave_Cpp_Flags := $(Enclave_C_Flags) -std=c++11 -nostdinc++
|
||||
|
||||
# To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
|
||||
# 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
|
||||
# so that the whole content of trts is included in the enclave.
|
||||
# 2. For other libraries, you just need to pull the required symbols.
|
||||
# Use `--start-group' and `--end-group' to link these libraries.
|
||||
# Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
|
||||
# Otherwise, you may get some undesirable errors.
|
||||
Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles -L$(SGX_LIBRARY_PATH) \
|
||||
-Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
|
||||
-Wl,--start-group -lsgx_tstdc -lsgx_tstdcxx -lsgx_tkey_exchange -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
|
||||
-Wl,-Bstatic -Wl,-Bsymbolic -Wl,--no-undefined \
|
||||
-Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
|
||||
-Wl,--defsym,__ImageBase=0 \
|
||||
-Wl,--version-script=isv_enclave/isv_enclave.lds
|
||||
|
||||
Enclave_Cpp_Objects := $(Enclave_Cpp_Files:.cpp=.o)
|
||||
|
||||
Enclave_Name := isv_enclave.so
|
||||
Signed_Enclave_Name := isv_enclave.signed.so
|
||||
Enclave_Config_File := isv_enclave/isv_enclave.config.xml
|
||||
|
||||
ifeq ($(SGX_MODE), HW)
|
||||
ifneq ($(SGX_DEBUG), 1)
|
||||
ifneq ($(SGX_PRERELEASE), 1)
|
||||
Build_Mode = HW_RELEASE
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
|
||||
.PHONY: all run
|
||||
|
||||
ifeq ($(Build_Mode), HW_RELEASE)
|
||||
all: $(App_Name) $(Enclave_Name)
|
||||
@echo "The project has been built in release hardware mode."
|
||||
@echo "Please sign the $(Enclave_Name) first with your signing key before you run the $(App_Name) to launch and access the enclave."
|
||||
@echo "To sign the enclave use the command:"
|
||||
@echo " $(SGX_ENCLAVE_SIGNER) sign -key <your key> -enclave $(Enclave_Name) -out <$(Signed_Enclave_Name)> -config $(Enclave_Config_File)"
|
||||
@echo "You can also sign the enclave using an external signing tool."
|
||||
@echo "To build the project in simulation mode set SGX_MODE=SIM. To build the project in prerelease mode set SGX_PRERELEASE=1 and SGX_MODE=HW."
|
||||
else
|
||||
all: $(App_Name) $(Signed_Enclave_Name)
|
||||
endif
|
||||
|
||||
run: all
|
||||
ifneq ($(Build_Mode), HW_RELEASE)
|
||||
@$(CURDIR)/$(App_Name)
|
||||
@echo "RUN => $(App_Name) [$(SGX_MODE)|$(SGX_ARCH), OK]"
|
||||
endif
|
||||
|
||||
|
||||
######## App Objects ########
|
||||
|
||||
isv_app/isv_enclave_u.c: $(SGX_EDGER8R) isv_enclave/isv_enclave.edl
|
||||
@cd isv_app && $(SGX_EDGER8R) --untrusted ../isv_enclave/isv_enclave.edl --search-path ../isv_enclave --search-path $(SGX_SDK)/include
|
||||
@echo "GEN => $@"
|
||||
|
||||
isv_app/isv_enclave_u.o: isv_app/isv_enclave_u.c
|
||||
@$(CC) $(App_C_Flags) -c $< -o $@
|
||||
@echo "CC <= $<"
|
||||
|
||||
isv_app/%.o: isv_app/%.cpp
|
||||
@$(CXX) $(App_Cpp_Flags) -c $< -o $@
|
||||
@echo "CXX <= $<"
|
||||
|
||||
../MessageHandler/%.o: ../MessageHandler/%.cpp
|
||||
@$(CXX) $(App_Cpp_Flags) -c $< -o $@
|
||||
@echo "CXX <= $<"
|
||||
|
||||
../Util/%.o: ../Util/%.cpp
|
||||
@$(CXX) $(App_Cpp_Flags) -c $< -o $@
|
||||
@echo "CXX <= $<"
|
||||
|
||||
../Networking/%.o: ../Networking/%.cpp
|
||||
@$(CXX) $(App_Cpp_Flags) -c $< -o $@
|
||||
@echo "CXX <= $<"
|
||||
|
||||
../Enclave/%.o: ../Enclave/%.cpp
|
||||
@$(CXX) $(App_Cpp_Flags) -c $< -o $@
|
||||
@echo "CXX <= $<"
|
||||
|
||||
$(App_Name): isv_app/isv_enclave_u.o $(App_Cpp_Objects)
|
||||
@$(CXX) $^ -o $@ $(App_Link_Flags)
|
||||
@echo "LINK => $@"
|
||||
|
||||
|
||||
######## Enclave Objects ########
|
||||
|
||||
isv_enclave/isv_enclave_t.c: $(SGX_EDGER8R) isv_enclave/isv_enclave.edl
|
||||
@cd isv_enclave && $(SGX_EDGER8R) --trusted ../isv_enclave/isv_enclave.edl --search-path ../isv_enclave --search-path $(SGX_SDK)/include
|
||||
@echo "GEN => $@"
|
||||
|
||||
isv_enclave/isv_enclave_t.o: isv_enclave/isv_enclave_t.c
|
||||
@$(CC) $(Enclave_C_Flags) -c $< -o $@
|
||||
@echo "CC <= $<"
|
||||
|
||||
isv_enclave/%.o: isv_enclave/%.cpp
|
||||
@$(CXX) $(Enclave_Cpp_Flags) -c $< -o $@
|
||||
@echo "CXX <= $<"
|
||||
|
||||
$(Enclave_Name): isv_enclave/isv_enclave_t.o $(Enclave_Cpp_Objects)
|
||||
@$(CXX) $^ -o $@ $(Enclave_Link_Flags)
|
||||
@echo "LINK => $@"
|
||||
|
||||
$(Signed_Enclave_Name): $(Enclave_Name)
|
||||
@$(SGX_ENCLAVE_SIGNER) sign -key isv_enclave/isv_enclave_private.pem -enclave $(Enclave_Name) -out $@ -config $(Enclave_Config_File)
|
||||
@echo "SIGN => $@"
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
@rm -f $(App_Name) $(Enclave_Name) $(Signed_Enclave_Name) $(App_Cpp_Objects) isv_app/isv_enclave_u.* $(Enclave_Cpp_Objects) isv_enclave/isv_enclave_t.* libservice_provider.* $(ServiceProvider_Cpp_Objects)
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "LogBase.h"
|
||||
|
||||
using namespace util;
|
||||
|
||||
#include "MessageHandler.h"
|
||||
|
||||
int Main(int argc, char* argv[]) {
|
||||
LogBase::Inst();
|
||||
|
||||
int ret = 0;
|
||||
|
||||
MessageHandler msg;
|
||||
msg.init();
|
||||
msg.start();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char **argv ) {
|
||||
try {
|
||||
return Main(argc, argv);
|
||||
} catch (std::exception& e) {
|
||||
Log("exception: %s", e.what());
|
||||
} catch (...) {
|
||||
Log("unexpected exception") ;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<EnclaveConfiguration>
|
||||
<ProdID>0</ProdID>
|
||||
<ISVSVN>0</ISVSVN>
|
||||
<StackMaxSize>0x40000</StackMaxSize>
|
||||
<HeapMaxSize>0x100000</HeapMaxSize>
|
||||
<TCSNum>1</TCSNum>
|
||||
<TCSPolicy>1</TCSPolicy>
|
||||
<DisableDebug>0</DisableDebug>
|
||||
<MiscSelect>0</MiscSelect>
|
||||
<MiscMask>0xFFFFFFFF</MiscMask>
|
||||
</EnclaveConfiguration>
|
||||
|
|
@ -0,0 +1,311 @@
|
|||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "isv_enclave_t.h"
|
||||
#include "sgx_tkey_exchange.h"
|
||||
#include "sgx_tcrypto.h"
|
||||
#include "string.h"
|
||||
|
||||
// This is the public EC key of the SP. The corresponding private EC key is
|
||||
// used by the SP to sign data used in the remote attestation SIGMA protocol
|
||||
// to sign channel binding data in MSG2. A successful verification of the
|
||||
// signature confirms the identity of the SP to the ISV app in remote
|
||||
// attestation secure channel binding. The public EC key should be hardcoded in
|
||||
// the enclave or delivered in a trustworthy manner. The use of a spoofed public
|
||||
// EC key in the remote attestation with secure channel binding session may lead
|
||||
// to a security compromise. Every different SP the enlcave communicates to
|
||||
// must have a unique SP public key. Delivery of the SP public key is
|
||||
// determined by the ISV. The TKE SIGMA protocl expects an Elliptical Curve key
|
||||
// based on NIST P-256
|
||||
static const sgx_ec256_public_t g_sp_pub_key = {
|
||||
{
|
||||
0x72, 0x12, 0x8a, 0x7a, 0x17, 0x52, 0x6e, 0xbf,
|
||||
0x85, 0xd0, 0x3a, 0x62, 0x37, 0x30, 0xae, 0xad,
|
||||
0x3e, 0x3d, 0xaa, 0xee, 0x9c, 0x60, 0x73, 0x1d,
|
||||
0xb0, 0x5b, 0xe8, 0x62, 0x1c, 0x4b, 0xeb, 0x38
|
||||
},
|
||||
{
|
||||
0xd4, 0x81, 0x40, 0xd9, 0x50, 0xe2, 0x57, 0x7b,
|
||||
0x26, 0xee, 0xb7, 0x41, 0xe7, 0xc6, 0x14, 0xe2,
|
||||
0x24, 0xb7, 0xbd, 0xc9, 0x03, 0xf2, 0x9a, 0x28,
|
||||
0xa8, 0x3c, 0xc8, 0x10, 0x11, 0x14, 0x5e, 0x06
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#ifdef SUPPLIED_KEY_DERIVATION
|
||||
|
||||
#pragma message ("Supplied key derivation function is used.")
|
||||
|
||||
typedef struct _hash_buffer_t {
|
||||
uint8_t counter[4];
|
||||
sgx_ec256_dh_shared_t shared_secret;
|
||||
uint8_t algorithm_id[4];
|
||||
} hash_buffer_t;
|
||||
|
||||
const char ID_U[] = "SGXRAENCLAVE";
|
||||
const char ID_V[] = "SGXRASERVER";
|
||||
|
||||
// Derive two keys from shared key and key id.
|
||||
bool derive_key(
|
||||
const sgx_ec256_dh_shared_t *p_shared_key,
|
||||
uint8_t key_id,
|
||||
sgx_ec_key_128bit_t *first_derived_key,
|
||||
sgx_ec_key_128bit_t *second_derived_key) {
|
||||
sgx_status_t sgx_ret = SGX_SUCCESS;
|
||||
hash_buffer_t hash_buffer;
|
||||
sgx_sha_state_handle_t sha_context;
|
||||
sgx_sha256_hash_t key_material;
|
||||
|
||||
memset(&hash_buffer, 0, sizeof(hash_buffer_t));
|
||||
/* counter in big endian */
|
||||
hash_buffer.counter[3] = key_id;
|
||||
|
||||
/*convert from little endian to big endian */
|
||||
for (size_t i = 0; i < sizeof(sgx_ec256_dh_shared_t); i++) {
|
||||
hash_buffer.shared_secret.s[i] = p_shared_key->s[sizeof(p_shared_key->s)-1 - i];
|
||||
}
|
||||
|
||||
sgx_ret = sgx_sha256_init(&sha_context);
|
||||
if (sgx_ret != SGX_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
sgx_ret = sgx_sha256_update((uint8_t*)&hash_buffer, sizeof(hash_buffer_t), sha_context);
|
||||
if (sgx_ret != SGX_SUCCESS) {
|
||||
sgx_sha256_close(sha_context);
|
||||
return false;
|
||||
}
|
||||
sgx_ret = sgx_sha256_update((uint8_t*)&ID_U, sizeof(ID_U), sha_context);
|
||||
if (sgx_ret != SGX_SUCCESS) {
|
||||
sgx_sha256_close(sha_context);
|
||||
return false;
|
||||
}
|
||||
sgx_ret = sgx_sha256_update((uint8_t*)&ID_V, sizeof(ID_V), sha_context);
|
||||
if (sgx_ret != SGX_SUCCESS) {
|
||||
sgx_sha256_close(sha_context);
|
||||
return false;
|
||||
}
|
||||
sgx_ret = sgx_sha256_get_hash(sha_context, &key_material);
|
||||
if (sgx_ret != SGX_SUCCESS) {
|
||||
sgx_sha256_close(sha_context);
|
||||
return false;
|
||||
}
|
||||
sgx_ret = sgx_sha256_close(sha_context);
|
||||
|
||||
assert(sizeof(sgx_ec_key_128bit_t)* 2 == sizeof(sgx_sha256_hash_t));
|
||||
memcpy(first_derived_key, &key_material, sizeof(sgx_ec_key_128bit_t));
|
||||
memcpy(second_derived_key, (uint8_t*)&key_material + sizeof(sgx_ec_key_128bit_t), sizeof(sgx_ec_key_128bit_t));
|
||||
|
||||
// memset here can be optimized away by compiler, so please use memset_s on
|
||||
// windows for production code and similar functions on other OSes.
|
||||
memset(&key_material, 0, sizeof(sgx_sha256_hash_t));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//isv defined key derivation function id
|
||||
#define ISV_KDF_ID 2
|
||||
|
||||
typedef enum _derive_key_type_t {
|
||||
DERIVE_KEY_SMK_SK = 0,
|
||||
DERIVE_KEY_MK_VK,
|
||||
} derive_key_type_t;
|
||||
|
||||
sgx_status_t key_derivation(const sgx_ec256_dh_shared_t* shared_key,
|
||||
uint16_t kdf_id,
|
||||
sgx_ec_key_128bit_t* smk_key,
|
||||
sgx_ec_key_128bit_t* sk_key,
|
||||
sgx_ec_key_128bit_t* mk_key,
|
||||
sgx_ec_key_128bit_t* vk_key) {
|
||||
bool derive_ret = false;
|
||||
|
||||
if (NULL == shared_key) {
|
||||
return SGX_ERROR_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
if (ISV_KDF_ID != kdf_id) {
|
||||
//fprintf(stderr, "\nError, key derivation id mismatch in [%s].", __FUNCTION__);
|
||||
return SGX_ERROR_KDF_MISMATCH;
|
||||
}
|
||||
|
||||
derive_ret = derive_key(shared_key, DERIVE_KEY_SMK_SK,
|
||||
smk_key, sk_key);
|
||||
if (derive_ret != true) {
|
||||
//fprintf(stderr, "\nError, derive key fail in [%s].", __FUNCTION__);
|
||||
return SGX_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
derive_ret = derive_key(shared_key, DERIVE_KEY_MK_VK,
|
||||
mk_key, vk_key);
|
||||
if (derive_ret != true) {
|
||||
//fprintf(stderr, "\nError, derive key fail in [%s].", __FUNCTION__);
|
||||
return SGX_ERROR_UNEXPECTED;
|
||||
}
|
||||
return SGX_SUCCESS;
|
||||
}
|
||||
#else
|
||||
#pragma message ("Default key derivation function is used.")
|
||||
#endif
|
||||
|
||||
// This ecall is a wrapper of sgx_ra_init to create the trusted
|
||||
// KE exchange key context needed for the remote attestation
|
||||
// SIGMA API's. Input pointers aren't checked since the trusted stubs
|
||||
// copy them into EPC memory.
|
||||
//
|
||||
// @param b_pse Indicates whether the ISV app is using the
|
||||
// platform services.
|
||||
// @param p_context Pointer to the location where the returned
|
||||
// key context is to be copied.
|
||||
//
|
||||
// @return Any error return from the create PSE session if b_pse
|
||||
// is true.
|
||||
// @return Any error returned from the trusted key exchange API
|
||||
// for creating a key context.
|
||||
|
||||
sgx_status_t enclave_init_ra(
|
||||
int b_pse,
|
||||
sgx_ra_context_t *p_context) {
|
||||
// isv enclave call to trusted key exchange library.
|
||||
sgx_status_t ret;
|
||||
if(b_pse) {
|
||||
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;
|
||||
}
|
||||
#ifdef SUPPLIED_KEY_DERIVATION
|
||||
ret = sgx_ra_init_ex(&g_sp_pub_key, b_pse, key_derivation, p_context);
|
||||
#else
|
||||
ret = sgx_ra_init(&g_sp_pub_key, b_pse, p_context);
|
||||
#endif
|
||||
if(b_pse) {
|
||||
sgx_close_pse_session();
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// Closes the tKE key context used during the SIGMA key
|
||||
// exchange.
|
||||
//
|
||||
// @param context The trusted KE library key context.
|
||||
//
|
||||
// @return Return value from the key context close API
|
||||
|
||||
sgx_status_t SGXAPI enclave_ra_close(
|
||||
sgx_ra_context_t context) {
|
||||
sgx_status_t ret;
|
||||
ret = sgx_ra_close(context);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// Verify the mac sent in att_result_msg from the SP using the
|
||||
// MK key. Input pointers aren't checked since the trusted stubs
|
||||
// copy them into EPC memory.
|
||||
//
|
||||
//
|
||||
// @param context The trusted KE library key context.
|
||||
// @param p_message Pointer to the message used to produce MAC
|
||||
// @param message_size Size in bytes of the message.
|
||||
// @param p_mac Pointer to the MAC to compare to.
|
||||
// @param mac_size Size in bytes of the MAC
|
||||
//
|
||||
// @return SGX_ERROR_INVALID_PARAMETER - MAC size is incorrect.
|
||||
// @return Any error produced by tKE API to get SK key.
|
||||
// @return Any error produced by the AESCMAC function.
|
||||
// @return SGX_ERROR_MAC_MISMATCH - MAC compare fails.
|
||||
|
||||
sgx_status_t verify_att_result_mac(sgx_ra_context_t context,
|
||||
uint8_t* p_message,
|
||||
size_t message_size,
|
||||
uint8_t* p_mac,
|
||||
size_t mac_size) {
|
||||
sgx_status_t ret;
|
||||
sgx_ec_key_128bit_t mk_key;
|
||||
|
||||
if(mac_size != sizeof(sgx_mac_t)) {
|
||||
ret = SGX_ERROR_INVALID_PARAMETER;
|
||||
return ret;
|
||||
}
|
||||
if(message_size > UINT32_MAX) {
|
||||
ret = SGX_ERROR_INVALID_PARAMETER;
|
||||
return ret;
|
||||
}
|
||||
|
||||
do {
|
||||
uint8_t mac[SGX_CMAC_MAC_SIZE] = {0};
|
||||
|
||||
ret = sgx_ra_get_keys(context, SGX_RA_KEY_MK, &mk_key);
|
||||
if(SGX_SUCCESS != ret) {
|
||||
break;
|
||||
}
|
||||
ret = sgx_rijndael128_cmac_msg(&mk_key,
|
||||
p_message,
|
||||
(uint32_t)message_size,
|
||||
&mac);
|
||||
if(SGX_SUCCESS != ret) {
|
||||
break;
|
||||
}
|
||||
if(0 == consttime_memequal(p_mac, mac, sizeof(mac))) {
|
||||
ret = SGX_ERROR_MAC_MISMATCH;
|
||||
break;
|
||||
}
|
||||
|
||||
} while(0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
sgx_status_t verify_secret_data (
|
||||
sgx_ra_context_t context,
|
||||
uint8_t *p_secret,
|
||||
uint32_t secret_size,
|
||||
uint8_t *p_gcm_mac,
|
||||
uint32_t max_verification_length,
|
||||
uint8_t *p_ret) {
|
||||
sgx_status_t ret = SGX_SUCCESS;
|
||||
sgx_ec_key_128bit_t sk_key;
|
||||
|
||||
do {
|
||||
ret = sgx_ra_get_keys(context, SGX_RA_KEY_SK, &sk_key);
|
||||
if (SGX_SUCCESS != ret) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t *decrypted = (uint8_t*) malloc(sizeof(uint8_t) * secret_size);
|
||||
uint8_t aes_gcm_iv[12] = {0};
|
||||
|
||||
ret = sgx_rijndael128GCM_decrypt(&sk_key,
|
||||
p_secret,
|
||||
secret_size,
|
||||
decrypted,
|
||||
&aes_gcm_iv[0],
|
||||
12,
|
||||
NULL,
|
||||
0,
|
||||
(const sgx_aes_gcm_128bit_tag_t *) (p_gcm_mac));
|
||||
|
||||
if (SGX_SUCCESS == ret) {
|
||||
if (decrypted[0] == 0) {
|
||||
if (decrypted[1] != 1) {
|
||||
ret = SGX_ERROR_INVALID_SIGNATURE;
|
||||
}
|
||||
} else {
|
||||
ret = SGX_ERROR_UNEXPECTED;
|
||||
}
|
||||
}
|
||||
|
||||
} while(0);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
enclave {
|
||||
from "sgx_tkey_exchange.edl" import *;
|
||||
|
||||
include "sgx_key_exchange.h"
|
||||
include "sgx_trts.h"
|
||||
|
||||
trusted {
|
||||
public sgx_status_t enclave_init_ra(int b_pse, [out] sgx_ra_context_t *p_context);
|
||||
|
||||
public sgx_status_t enclave_ra_close(sgx_ra_context_t context);
|
||||
|
||||
public sgx_status_t verify_att_result_mac(sgx_ra_context_t context,
|
||||
[in,size=message_size] uint8_t* message,
|
||||
size_t message_size,
|
||||
[in,size=mac_size] uint8_t* mac,
|
||||
size_t mac_size);
|
||||
|
||||
public sgx_status_t verify_secret_data(sgx_ra_context_t context,
|
||||
[in,size=secret_size] uint8_t* p_secret,
|
||||
uint32_t secret_size,
|
||||
[in,count=16] uint8_t* gcm_mac,
|
||||
uint32_t max_verification_length,
|
||||
[out, count=16] uint8_t *p_ret);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
enclave.so {
|
||||
global:
|
||||
g_global_data_sim;
|
||||
g_global_data;
|
||||
enclave_entry;
|
||||
local:
|
||||
*;
|
||||
};
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
-----BEGIN RSA PRIVATE KEY-----
|
||||
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
|
||||
-----END RSA PRIVATE KEY-----
|
||||
Loading…
Add table
Add a link
Reference in a new issue