diff --git a/wallfacer/.keep b/wallfacer/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/wallfacer/verify/.keep b/wallfacer/verify/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/wallfacer/verify/enclave/.DS_Store b/wallfacer/verify/enclave/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..df5e54687d8fa32b7fa9c55f53a9e9493cda0f96 Binary files /dev/null and b/wallfacer/verify/enclave/.DS_Store differ diff --git a/wallfacer/verify/enclave/.keep b/wallfacer/verify/enclave/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/wallfacer/verify/enclave/CMakeLists.txt b/wallfacer/verify/enclave/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..b7d578a6320656675ab750aa1f59042f834a5bc7 --- /dev/null +++ b/wallfacer/verify/enclave/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +cmake_minimum_required(VERSION 3.10 FATAL_ERROR) +project(enclave) + +set(CMAKE_C_STANDARD 99) + +set(CURRENT_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) + +#set edl name +set(EDL_FILE enclave.edl) +#set(LOCAL_ROOT_PATH "$ENV{CC_SDK}") +#set(SECGEAR_INSTALL_PATH /usr/lib64/) + +if(CC_GP) + set(CODETYPE trustzone) + set(CODEGEN codegen_arm64) + execute_process(COMMAND uuidgen -r OUTPUT_VARIABLE UUID) + string(REPLACE "\n" "" UUID ${UUID}) + add_definitions(-DPATH="/data/${UUID}.sec") +endif() + +if(CC_SGX) + set(CODETYPE sgx) + set(CODEGEN codegen_x86_64) + add_definitions(-DPATH="${CMAKE_CURRENT_BINARY_DIR}/enclave/enclave.signed.so") +endif() + +add_subdirectory(${CURRENT_ROOT_PATH}/enclave) +add_subdirectory(${CURRENT_ROOT_PATH}/host) diff --git a/wallfacer/verify/enclave/enclave.edl b/wallfacer/verify/enclave/enclave.edl new file mode 100644 index 0000000000000000000000000000000000000000..b9f617106eeb963bc2b7b704449fb1ba4be43a7c --- /dev/null +++ b/wallfacer/verify/enclave/enclave.edl @@ -0,0 +1,28 @@ +enclave { + include "secgear_urts.h" + from "secgear_tstdc.edl" import *; + + trusted { + public void setRSAsource([in, size=len] char* src, size_t len); + public void getRSAresult([out, size=len] char* buf, size_t len); + public void calcEmbeddingResult(); + public void initKeys([in] uint32_t *src); + public void registerSeed([in] uint32_t *src); + public void setKeys([in,count=2] uint32_t *K); + public void getKeys([out,count=2] uint32_t *K); + public void setD([in, size=len] char* src, size_t len); + public void setCD([in, size=len] char* src, size_t len); + public void setN([in, size=len] char* src, size_t len); + public void getCD([out, size=len] char* buf, size_t len); + public void getX([out, size=len] char* buf, size_t len); + public void getMu([out, size=len] char* buf, size_t len); + public void getK([out, size=len] char* buf, size_t len); + public void getNWID([out, size=len] char* buf, size_t len); + public void setNPW([in, size=len] char* src, size_t len); + + }; + + untrusted { + + }; +}; diff --git a/wallfacer/verify/enclave/enclave/enclave/CMakeLists.txt b/wallfacer/verify/enclave/enclave/enclave/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..17c5826fa362fff593350f8db06bb9cef84194c3 --- /dev/null +++ b/wallfacer/verify/enclave/enclave/enclave/CMakeLists.txt @@ -0,0 +1,162 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# secGear is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. + +#set auto code prefix +set(PREFIX enclave) + +set(CMAKE_CXX_STANDARD 11) +#set sign key +set(PEM Enclave_private.pem) + +#set sign tool +set(SIGN_TOOL ${LOCAL_ROOT_PATH}/tools/sign_tool/sign_tool.sh) + +#set enclave src code +set(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/enclave.cpp) + +#set log level +set(PRINT_LEVEL 3) +add_definitions(-DPRINT_LEVEL=${PRINT_LEVEL}) + +if(CC_GP) + #set signed output + set(OUTPUT ${UUID}.sec) + #set itrustee device key + set(DEVICEPEM ${CMAKE_CURRENT_SOURCE_DIR}/rsa_public_key_cloud.pem) + #set whilelist. default: /vendor/bin/teec_hello + set(WHITE_LIST_0 /vendor/bin/enclave) + set(WHITE_LIST_OWNER root) + set(WHITE_LIST_1 /vendor/bin/secgear_enclave) + set(WHITELIST WHITE_LIST_0 WHITE_LIST_1) + + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_args.h) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/gp) +endif() + +if(CC_SGX) + set(OUTPUT enclave.signed.so) + set(AUTO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.h ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}_t.c) + add_custom_command(OUTPUT ${AUTO_FILES} + DEPENDS ${CURRENT_ROOT_PATH}/${EDL_FILE} + COMMAND ${CODEGEN} --${CODETYPE} --trusted ${CURRENT_ROOT_PATH}/${EDL_FILE} --search-path ${LOCAL_ROOT_PATH}/inc/host_inc/sgx --search-path ${SGXSDK}/include) +endif() + +set(COMMON_C_FLAGS "-W -Wall -fno-short-enums -fno-omit-frame-pointer -fstack-protector \ + -Wstack-protector --param ssp-buffer-size=4 -frecord-gcc-switches -Wextra -nostdinc -nodefaultlibs \ + -fno-peephole -fno-peephole2 -Wno-main -Wno-error=unused-parameter \ + -Wno-error=unused-but-set-variable -Wno-error=format-truncation=") + +set(COMMON_C_LINK_FLAGS "-Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -Wl,-nostdlib -nodefaultlibs -nostartfiles") +set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS}") + +if(CC_GP) + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt.in" "${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt") + + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -march=armv8-a ") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s -fPIC") + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-s") + + set(ITRUSTEE_TEEDIR ${iTrusteeSDK}/) + set(ITRUSTEE_LIBC ${iTrusteeSDK}/thirdparty/open_source/musl/libc) + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${SECGEAR_INSTALL_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories( ${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/gp + ${LOCAL_ROOT_PATH}/inc/enclave_inc + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp + ${ITRUSTEE_TEEDIR}/include/TA + ${ITRUSTEE_TEEDIR}/include/TA/huawei_ext + ${ITRUSTEE_LIBC}/arch/aarch64 + ${ITRUSTEE_LIBC}/ + ${ITRUSTEE_LIBC}/arch/arm/bits + ${ITRUSTEE_LIBC}/arch/generic + ${ITRUSTEE_LIBC}/arch/arm + ${LOCAL_ROOT_PATH}/inc/enclave_inc/gp/itrustee) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE ${SECGEAR_INSTALL_PATH}) + endif() + + foreach(WHITE_LIST ${WHITELIST}) + add_definitions(-D${WHITE_LIST}="${${WHITE_LIST}}") + endforeach(WHITE_LIST) + add_definitions(-DWHITE_LIST_OWNER="${WHITE_LIST_OWNER}") + + target_link_libraries(${PREFIX} -lsecgear_tee) + + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND bash ${SIGN_TOOL} -d sign -x trustzone -i /home/sunny/secGear/debug/lib/lib${PREFIX}.so -c ${CMAKE_CURRENT_SOURCE_DIR}/manifest.txt -m ${CMAKE_CURRENT_SOURCE_DIR}/config_cloud.ini + -o ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT}) + + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${OUTPUT} + DESTINATION /data + PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +endif() + +if(CC_SGX) + set(SGX_DIR ${SGXSDK}) + set(CMAKE_C_FLAGS "${COMMON_C_FLAGS} -m64 -fvisibility=hidden") + set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=c++11 -O2 -nostdinc++") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -s") + set(LINK_LIBRARY_PATH ${SGX_DIR}/lib64) + + if(CC_SIM) + set(Trts_Library_Name sgx_trts_sim) + set(Service_Library_Name sgx_tservice_sim) + else() + set(Trts_Library_Name sgx_trts) + set(Service_Library_Name sgx_tservice) + endif() + + set(Crypto_Library_Name sgx_tcrypto) + + set(CMAKE_SHARED_LINKER_FLAGS "${COMMON_C_LINK_FLAGS} -Wl,-z,defs -Wl,-pie -Bstatic -Bsymbolic -eenclave_entry \ + -Wl,--export-dynamic -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/Enclave.lds") + + if(${CMAKE_VERSION} VERSION_LESS "3.13.0") + link_directories(${LINK_LIBRARY_PATH}) + endif() + + add_library(${PREFIX} SHARED ${SOURCE_FILES} ${AUTO_FILES}) + + target_include_directories(${PREFIX} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR} + ${SGX_DIR}/include/tlibc + ${SGX_DIR}/include/libcxx + ${SGX_DIR}/include + ${LOCAL_ROOT_PATH}/inc/host_inc + ${LOCAL_ROOT_PATH}/inc/host_inc/sgx) + + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13.0") + target_link_directories(${PREFIX} PRIVATE + ${LINK_LIBRARY_PATH}) + endif() + + target_link_libraries(${PREFIX} -Wl,--whole-archive ${Trts_Library_Name} -Wl,--no-whole-archive + -Wl,--start-group -lsgx_tstdc -lsgx_tcxx -l${Crypto_Library_Name} -l${Service_Library_Name} -Wl,--end-group) + add_custom_command(TARGET ${PREFIX} + POST_BUILD + COMMAND umask 0177 + COMMAND openssl genrsa -3 -out ${PEM} 3072 + COMMAND bash ${SIGN_TOOL} -d sign -x sgx -i ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${PREFIX}.so -k ${PEM} -o ${OUTPUT} -c ${CMAKE_CURRENT_SOURCE_DIR}/Enclave.config.xml) +endif() + +set_target_properties(${PREFIX} PROPERTIES SKIP_BUILD_RPATH TRUE) diff --git a/wallfacer/verify/enclave/enclave/enclave/Enclave.config.xml b/wallfacer/verify/enclave/enclave/enclave/Enclave.config.xml new file mode 100644 index 0000000000000000000000000000000000000000..a5e05e0175b43f97645fde0ea33a1fe0562398cf --- /dev/null +++ b/wallfacer/verify/enclave/enclave/enclave/Enclave.config.xml @@ -0,0 +1,12 @@ + + 0 + 0 + 0x100000 + 0x80000 + 10 + 1 + + 1 + 0 + 0xFFFFFFFF + diff --git a/wallfacer/verify/enclave/enclave/enclave/Enclave.lds b/wallfacer/verify/enclave/enclave/enclave/Enclave.lds new file mode 100644 index 0000000000000000000000000000000000000000..ab77e6478ef3c3448356e80b9884a8a533dd32c6 --- /dev/null +++ b/wallfacer/verify/enclave/enclave/enclave/Enclave.lds @@ -0,0 +1,11 @@ +enclave.so +{ + global: + g_global_data_sim; + g_global_data; + enclave_entry; + g_peak_heap_used; + local: + *; +}; + diff --git a/wallfacer/verify/enclave/enclave/enclave/enclave.cpp b/wallfacer/verify/enclave/enclave/enclave/enclave.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cbaf6f8f3fda402f805e3c34b4e5a27618d432e --- /dev/null +++ b/wallfacer/verify/enclave/enclave/enclave/enclave.cpp @@ -0,0 +1,705 @@ +#include "Enclave1_t.h" +#include +#include +#include +#include +#define MAX_LEN 1024 + +using namespace std; + +namespace calc { + const int L = 16; + const std::string hexChars = "0123456789abcdef"; + int dec(char ch) { + if ('0' <= ch && ch <= '9') { + return ch - '0'; + } + if ('a' <= ch && ch <= 'f') { + return ch - 'a' + 10; + } + if ('A' <= ch && ch <= 'F') { + return ch - 'A' + 10; + } + return -1; + } + std::string hex(unsigned int x) { + std::string result = ""; + for (int j = 28; j >= 0; j -= 4) { + result += hexChars[x >> j & 0x0F]; + } + return result; + } + std::string hex(unsigned long long x) { + std::string result = ""; + for (int j = 60; j >= 0; j -= 4) { + result += hexChars[x >> j & 0x0F]; + } + return result; + } + std::string bin(unsigned int x) { + std::string result = ""; + for (int j = 31; j >= 0; j--) { + result += hexChars[x >> j & 0x01]; + } + return result; + } + std::string bin(unsigned long long x) { + std::string result = ""; + for (int j = 63; j >= 0; j--) { + result += hexChars[x >> j & 0x01]; + } + return result; + } + unsigned int rightRotate(unsigned int x, int u) { + unsigned long long xx = (unsigned long long)x << 32 | x; + return (unsigned int)((xx >> u)&(unsigned int)-1); + } + unsigned int LeftRotate(unsigned int x, int u) { + u &= 31; + unsigned long long xx = (unsigned long long)x << 32 | x; + return (unsigned int)((xx >> (32 - u))&(unsigned int)-1); + } + struct bigNum { + int siz, hexLen; + std::vectornum; + bigNum(int _siz = L) { + siz = _siz; hexLen = siz << 4; + num.resize(_siz, 0); + } + bigNum(std::string src) { + int len = src.length(); hexLen = src.length(); + siz = (len + 15) >> 4; + num.resize(siz, 0); + for (int i = 0, cnt = 0; i < len; i++, cnt++) { + num[cnt >> 4] |= (unsigned long long)dec(src[i]) << (((cnt & 0x0F) ^ 15) << 2); + } + } + bigNum FromU32(uint32_t src) { + bigNum result = bigNum(1); + result.num[0] = src; + return result; + } + std::string hex() { + std::string result = ""; + for (int i = 0; i < siz; i++) { + for (int j = 60; j >= 0; j -= 4) { + result += hexChars[num[i] >> j & 0x0F]; + } + } + result.erase(hexLen); + return result; + } + std::string bin() { + std::string result = "b"; + for (int i = 0; i < siz; i++) { + for (int j = 63; j >= 0; j--) { + result += hexChars[num[i] >> j & 0x01]; + } + } + result.erase(hexLen << 2); + return result; + } + bigNum& operator^=(const bigNum &rhs) { + bigNum result = *this; + int lmt = siz; + if (rhs.siz < lmt) lmt = rhs.siz; + for (int i = 0; i < lmt; i++) { + result.num[i] ^= rhs.num[i]; + } + return *this = result; + } + friend bigNum operator^(const bigNum& lhs, const bigNum& rhs) { + if(lhs.siz>=rhs.siz) return bigNum(lhs) ^= rhs; + else return bigNum(rhs) ^= lhs; + } + friend bigNum operator||(const bigNum& lhs, const bigNum& rhs) { + bigNum result = bigNum(bigNum(lhs).hex() + bigNum(rhs).hex()); + return result; + } + }; + + unsigned int P0(unsigned int x) { + return x ^ LeftRotate(x, 9) ^ LeftRotate(x, 17); + } + unsigned int P1(unsigned int x) { + return x ^ LeftRotate(x, 15) ^ LeftRotate(x, 23); + } + unsigned int FF(unsigned int j, unsigned int x, unsigned int y, unsigned int z) { + if (j < 16) return x ^ y ^ z; + else return (x&y) | (x&z) | (y&z); + } + unsigned int GG(unsigned int j, unsigned int x, unsigned int y, unsigned int z) { + if (j < 16) return x ^ y ^ z; + else return (x&y) | ((~x)&z) ; + } + bigNum SM3(bigNum src) { + const unsigned int T[64] = { + 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, + 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, 0x79cc4519, + 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, + 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, + 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, + 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, + 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, + 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, 0x7a879d8a, }; + unsigned int h[8] = { + 0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e }; + std::string tmpstr = bigNum(src).hex(); + tmpstr += "8"; + while (tmpstr.length() % 128 != 112) { + tmpstr += "0"; + } + tmpstr += hex((unsigned long long)src.hexLen << 2); + bigNum tmpnum = bigNum(tmpstr); + + for (int i = 0; i < tmpnum.siz; i += 8) { + unsigned int w[68] = {}, ww[64] = {}; + for (int j = 0; j < 16; j += 2) { + w[j] = (unsigned int)(tmpnum.num[i + (j >> 1)] >> 32); + w[j + 1] = (unsigned int)(tmpnum.num[i + (j >> 1)]); + } + for (int j = 16; j < 68; j++) { + w[j] = P1(w[j - 16] ^ w[j - 9] ^ LeftRotate(w[j - 3], 15)) ^ LeftRotate(w[j - 13], 7) ^ w[j - 6]; + } + for (int j = 0; j < 64; j++) { + ww[j] = w[j] ^ w[j + 4]; + } + unsigned int t[8] = {}; + for (int j = 0; j < 8; j++) { + t[j] = h[j]; + } + for (int j = 0; j < 64; j++) { + unsigned int SS1, SS2, TT1, TT2; + SS1 = LeftRotate(LeftRotate(t[0], 12) + t[4] + LeftRotate(T[j], j), 7); + SS2 = SS1 ^ LeftRotate(t[0], 12); + TT1 = FF(j, t[0], t[1], t[2]) + t[3] + SS2 + ww[j]; + TT2 = GG(j, t[4], t[5], t[6]) + t[7] + SS1 + w[j]; + t[7] = t[6]; + t[6] = LeftRotate(t[5], 19); + t[5] = t[4]; + t[4] = P0(TT2); + t[3] = t[2]; + t[2] = LeftRotate(t[1],9); + t[1] = t[0]; + t[0] = TT1; + } + for (int j = 0; j < 8; j++) { + h[j] = h[j] ^ t[j]; + } + } + + bigNum result = bigNum(4); + result.num[0] = ((unsigned long long)h[0] << 32) | h[1]; + result.num[1] = ((unsigned long long)h[2] << 32) | h[3]; + result.num[2] = ((unsigned long long)h[4] << 32) | h[5]; + result.num[3] = ((unsigned long long)h[6] << 32) | h[7]; + return result; + } + + struct smallNum { + int siz; + std::vectornum; + smallNum(int _siz = 1) { + siz = _siz; + num.resize(_siz, 0); + } + smallNum(char ch) { + siz = 1; + num.push_back(ch - '0'); + } + smallNum(std::string src) { + int len = src.length(); + siz = (len + 15) >> 4; + num.resize(siz, 0); + for (int i = len - 1, cnt = 0; i >= 0; i--, cnt++) { + num[cnt >> 4] |= (unsigned long long)dec(src[i]) << ((cnt & 0x0F) << 2); + } + } + smallNum fromBin(std::string src) { + smallNum result = smallNum(); + int len = src.length(); + if (!len) { + return smallNum(); + } + result.siz = (len + 63) >> 6; + result.num.resize(result.siz, 0); + for (int i = len - 1, cnt = 0; i >= 0; i--, cnt++) { + result.num[cnt >> 6] |= (unsigned long long)dec(src[i]) << (cnt & 0x3F); + } + return result; + } + smallNum(bigNum src) { + std::string srcstr = src.hex(); + *this = smallNum(srcstr); + } + smallNum fromBigNum(bigNum src) { + std::string srcstr = src.hex(); + return smallNum(srcstr); + } + bigNum toBigNum(smallNum src, int minHexLength = 0) { + std::string srcstr = src.hex(); + return bigNum(srcstr); + } + smallNum e() { + smallNum result = smallNum(1); + result.num[0] = 1; + return result; + } + smallNum FromU32(uint32_t src) { + smallNum result = smallNum(1); + result.num[0] = src; + return result; + } + std::string hex() { + std::string result = ""; + for (int i = siz - 1; i >= 0; i--) { + for (int j = 60; j >= 0; j -= 4) { + result += hexChars[num[i] >> j & 0x0F]; + } + } + return result; + } + std::string bin() { + std::string result = ""; + for (int i = siz - 1; i >= 0; i--) { + for (int j = 63; j >= 0; j--) { + result += hexChars[num[i] >> j & 0x01]; + } + } + return result; + } + smallNum& operator^=(const smallNum &rhs) { + smallNum result = *this; + int lmt = rhs.siz; + for (int i = 0; i < lmt; i++) { + result.num[i] ^= rhs.num[i]; + } + return *this = result; + } + friend smallNum operator^(const smallNum& lhs, const smallNum& rhs) { + if(lhs.siz>rhs.siz) return smallNum(lhs) ^= rhs; + else return smallNum(rhs) ^= lhs; + } + friend bool operator<(const smallNum& lhs, const smallNum& rhs) { + if (lhs.siz^rhs.siz) { + return lhs.siz < rhs.siz; + } + for (int i = lhs.siz - 1; i >= 0; i--) { + if (lhs.num[i] ^ rhs.num[i]) { + return lhs.num[i] < rhs.num[i]; + } + } + return 0; + } + friend bool operator<=(const smallNum& lhs, const smallNum& rhs) { + if (lhs.siz^rhs.siz) { + return lhs.siz < rhs.siz; + } + for (int i = lhs.siz - 1; i >= 0; i--) { + if (lhs.num[i] ^ rhs.num[i]) { + return lhs.num[i] < rhs.num[i]; + } + } + return 1; + } + friend bool operator>(const smallNum& lhs, const smallNum& rhs) { + return smallNum(rhs) < smallNum(lhs); + } + friend bool operator>=(const smallNum& lhs, const smallNum& rhs) { + return smallNum(rhs) <= smallNum(lhs); + } + friend bool operator==(const smallNum& lhs, const smallNum& rhs) { + if (lhs.siz^rhs.siz) { + return 0; + } + for (int i = lhs.siz - 1; i >= 0; i--) { + if (lhs.num[i] ^ rhs.num[i]) { + return 0; + } + } + return 1; + } + friend bool operator!=(const smallNum& lhs, const smallNum& rhs) { + if (lhs.siz^rhs.siz) { + return 1; + } + for (int i = lhs.siz - 1; i >= 0; i--) { + if (lhs.num[i] ^ rhs.num[i]) { + return 1; + } + } + return 0; + } + smallNum& operator+=(const smallNum& rhs) { + while (siz < rhs.siz) { + ++siz; num.push_back(0ULL); + } + unsigned long long carry0 = 0ULL, carry1 = 0ULL; + for (int i = 0; i < rhs.siz; i++) { + if (((num[i] >> 1) + (rhs.num[i] >> 1) + (((num[i] & 1ULL) + (rhs.num[i] & 1ULL) + carry0) >> 1)) >> 63) { + if (i + 1 == siz) { + ++siz; num.push_back(0ULL); + } + carry1 = 1ULL; + } + num[i] += rhs.num[i] + carry0; + carry0 = carry1; + carry1 = 0ULL; + } + for (int i = rhs.siz; i < siz; i++) { + if (((num[i] >> 1) + (((num[i] & 1ULL) + carry0) >> 1)) >> 63) { + if (i + 1 == siz) { + ++siz; num.push_back(0ULL); + } + carry1 = 1ULL; + } + num[i] += carry0; + carry0 = carry1; + carry1 = 0ULL; + } + return *this; + } + friend smallNum operator+(const smallNum& lhs, const smallNum&rhs) { + return smallNum(lhs) += rhs; + } + smallNum& operator-=(const smallNum& rhs){ + unsigned long long tmp,carry0=0,carry1=0; + for(int i=0;itmp){ + carry1=1; + } + tmp=num[i]; + num[i]-=carry0; + if(num[i]>tmp){ + carry1=1; + } + carry0=carry1; + carry1=0; + } + for(int i=rhs.siz;itmp){ + carry1=1; + } + carry0=carry1; + carry1=0; + } + while(siz>1&&!num[siz-1]){ + --siz;num.pop_back(); + } + return *this; + } + friend smallNum operator-(const smallNum& lhs, const smallNum&rhs) { + return smallNum(lhs) -= rhs; + } + void _add(unsigned long long u,unsigned long long v,unsigned long long &x,unsigned long long &y,unsigned long long &z){ + x+=u; if(x=rhs.siz?k-rhs.siz+1:0),j=k-i;i>64)&(-1ULL)),result.num[k],result.num[k+1],result.num[k+2]); + } + } + result.num.pop_back(); + if(result.siz>1&&!result.num[SIZ-1]){ + --result.siz; + result.num.pop_back(); + } + return *this=result; + } + + friend smallNum operator*(const smallNum& lhs, const smallNum&rhs) { + return smallNum(lhs) *= rhs; + } + smallNum pow(unsigned long long rhs) { + smallNum result = smallNum().e(), u = smallNum(*this); + while (rhs) { + if (rhs & 1) { + result *= u; + } + u *= u; + rhs >>= 1; + } + return result; + } + smallNum& operator%=(const smallNum& rhs) { + smallNum result = smallNum(); + std::string lhsBin = smallNum(*this).bin(); + int lhsLen = lhsBin.length(); + for (int i = 0; i < lhsLen; i++) { + result = result + result; + if (lhsBin[i] & 1) { + result.num[0] |= 1; + } + if (rhs <= result) { + result = result - rhs; + } + } + return *this = result; + } + friend smallNum operator%(const smallNum& lhs, const smallNum&rhs) { + return smallNum(lhs) %= rhs; + } + smallNum pow_mod(unsigned long long rhs, smallNum _mod) { + smallNum result = smallNum().e(), u = smallNum(*this); + while (rhs) { + if (rhs & 1) { + result = (result*u) % _mod; + } + u = (u*u) % _mod; + rhs >>= 1; + } + return result; + } + smallNum pow_mod(smallNum rhs, smallNum _mod) { + smallNum result = smallNum().e(), u = smallNum(*this); + std::string rhsstr = rhs.bin(); + int rhsLen = rhsstr.length(); + for (int i = rhsLen - 1; i >= 0; i--) { + if (rhsstr[i] & 1) { + result = (result*u) % _mod; + } + u = (u*u) % _mod; + } + return result; + } + smallNum fromDec(std::string src) { + if (src == "") { + return smallNum(); + } + smallNum result = smallNum(); + int siz = src.length(); + for (int i = 0; i < siz; i++) { + result *= smallNum("a"); + result += smallNum(src[i]); + } + return result; + } + }; + smallNum big2Small(bigNum src) { + std::string srcstr = src.hex(); + return smallNum(srcstr); + } + bigNum small2Big(smallNum src, int minHexLength = 0) { + std::string srcstr = src.hex(); + return bigNum(srcstr); + } +} + +namespace interface { + using namespace calc; + smallNum rsa_d(smallNum rsa_e, smallNum d, smallNum n) { + return rsa_e.pow_mod(d, n); + } +} + +char tmpresult[512]; +char* transferFromString(string src) { + int siz = src.length(); + if (siz < 512) { + for (int i = 0; i <= siz; i++) { + tmpresult[i] = src[i]; + } + } + return tmpresult; +} + +string transferToString(char* src) { + string result; + for (int i = 0; src[i]; i++) { + result += src[i]; + } + return result; +} + +unsigned int randSeed = 0; + +unsigned int getNextRand(unsigned int preRand) { + if (!preRand) return -1; + unsigned x = preRand; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return x; +} + +unsigned int getNextRand(unsigned int preRand,int rounds) { + if (!preRand) return -1; + unsigned x = preRand; + for (int i = 0; i < rounds; i++) { + x = getNextRand(x); + } + return x; +} + +uint32_t Keys[2]; + +void sealKeys() { + Keys[0] ^= getNextRand(12345678, 32); + Keys[1] ^= getNextRand(87654321, 32); +} + +void decryptKeys(uint32_t *keybuf) { + keybuf[0] = Keys[0] ^ getNextRand(12345678, 32); + keybuf[1] = Keys[1] ^ getNextRand(87654321, 32); +} + +void initKeys(uint32_t *src) { + randSeed = src[0]; + randSeed ^= getNextRand(randSeed, 16); + Keys[0] = getNextRand(randSeed, 555); // Ku + Keys[1] = getNextRand(randSeed, 777); // Kw + sealKeys(); +} + +void setKeys(uint32_t *K) { + memcpy(Keys, K, sizeof Keys); +} + +void getKeys(uint32_t *K) { + memcpy(K, Keys, sizeof Keys); +} + +using namespace calc; +string DebugStr; +string Dstr, Nstr, CDstr, NPWstr; + +void encryptD() { + uint32_t Dkeys[2] = {}; + decryptKeys(Dkeys); + CDstr = (smallNum(Dstr) ^ big2Small(SM3(bigNum().FromU32(Dkeys[0]) || bigNum(Nstr)))).hex(); + Dstr = ""; +} + +void decryptD() { + uint32_t Dkeys[2] = {}; + decryptKeys(Dkeys); + Dstr = (smallNum(CDstr) ^ big2Small(SM3(bigNum().FromU32(Dkeys[0]) || bigNum(Nstr)))).hex(); +} + +void setCD(char* src, size_t len) { + char source[MAX_LEN]; + if (len < MAX_LEN) { + memcpy(source, src, len + 1); + } + CDstr = transferToString(source); +} + +void setD(char* src, size_t len){ + char source[MAX_LEN]; + if (len < MAX_LEN) { + memcpy(source, src, len + 1); + } + Dstr = transferToString(source); + encryptD(); +} + +void setN(char* src, size_t len){ + char source[MAX_LEN]; + if (len < MAX_LEN) { + memcpy(source, src, len + 1); + } + Nstr = transferToString(source); +} + +void getCD(char *buf, size_t len) { + char* result = transferFromString(CDstr); + if (len > strlen(result)) { + memcpy(buf, result, strlen(result) + 1); + } +} + +string RSAsource; + +void setRSAsource(char* src, size_t len) { + char source[MAX_LEN]; + if (len < MAX_LEN) { + memcpy(source, src, len + 1); + } + RSAsource = transferToString(source); +} + +void getRSAresult(char *buf, size_t len) { + decryptD(); + string hpw = interface::rsa_d(smallNum(RSAsource), smallNum(Dstr), smallNum(Nstr)).hex(); + uint32_t Dkeys[2] = {}; + decryptKeys(Dkeys); + string npw = (smallNum(hpw) ^ big2Small(SM3(bigNum().FromU32(Dkeys[0]) || bigNum(Dstr)))).hex(); + char* result = transferFromString(npw); + Dstr = ""; + if (len > strlen(result)) { + memcpy(buf, result, strlen(result) + 1); + } +} + +uint32_t _uints[3]; + +void registerSeed(uint32_t *src){ + randSeed = src[0]; + randSeed ^= getNextRand(randSeed, 16); + _uints[0] = getNextRand(randSeed, 111) % 2001 + 2000; // x + _uints[1] = getNextRand(randSeed, 222) % 1001 + 2000; // mu + _uints[2] = getNextRand(randSeed, 333) % 15 + 1; // k +} + +void setNPW(char* src, size_t len) { + char source[MAX_LEN]; + if (len < MAX_LEN) { + memcpy(source, src, len + 1); + } + NPWstr = transferToString(source); +} + +string _xStr, _muStr, _kStr,_nwidStr; + +void calcEmbeddingResult() { + decryptD(); + string hwid = interface::rsa_d(smallNum(RSAsource), smallNum(Dstr), smallNum(Nstr)).hex(); + uint32_t Dkeys[2] = {}; + decryptKeys(Dkeys); + _nwidStr = (smallNum(hwid) ^ big2Small(SM3(bigNum().FromU32(Dkeys[1]) || bigNum(Dstr)))).hex(); + string hpw = (smallNum(NPWstr) ^ big2Small(SM3(bigNum().FromU32(Dkeys[0]) || bigNum(Dstr)))).hex(); + smallNum hashPart = big2Small(SM3(bigNum(hpw) || bigNum(hwid)).hex()); + _xStr = (smallNum().FromU32(_uints[0])*smallNum("001001001001001001001001001001001001001001001001001001001001001") ^ hashPart).hex(); + _muStr = (smallNum().FromU32(_uints[1])*smallNum("001001001001001001001001001001001001001001001001001001001001001") ^ hashPart).hex(); + _kStr = (smallNum().FromU32(_uints[2])*smallNum("1111111111111111111111111111111111111111111111111111111111111111") ^ hashPart).hex(); + Dstr = ""; + NPWstr = ""; +} + +void getX(char *buf, size_t len) { + char* result = transferFromString(_xStr); + if (len > strlen(result)) { + memcpy(buf, result, strlen(result) + 1); + } +} + +void getMu(char *buf, size_t len) { + char* result = transferFromString(_muStr); + if (len > strlen(result)) { + memcpy(buf, result, strlen(result) + 1); + } +} + +void getK(char *buf, size_t len) { + char* result = transferFromString(_kStr); + if (len > strlen(result)) { + memcpy(buf, result, strlen(result) + 1); + } +} + +void getNWID(char *buf, size_t len) { + char* result = transferFromString(_nwidStr); + if (len > strlen(result)) { + memcpy(buf, result, strlen(result) + 1); + } +} diff --git a/wallfacer/verify/enclave/enclave/enclave/manifest.txt.in b/wallfacer/verify/enclave/enclave/enclave/manifest.txt.in new file mode 100644 index 0000000000000000000000000000000000000000..7b8ecf5937d99b0c7f87478984017cdc92c44ed1 --- /dev/null +++ b/wallfacer/verify/enclave/enclave/enclave/manifest.txt.in @@ -0,0 +1,8 @@ +gpd.ta.appID: @UUID@ +gpd.ta.service_name: test0108 +gpd.ta.singleInstance: true +gpd.ta.multiSession: false +gpd.ta.multiCommand: false +gpd.ta.instanceKeepAlive: false +gpd.ta.dataSize: 16384 +gpd.ta.stackSize: 20480 diff --git a/wallfacer/verify/enclave/enclave/enclave/rsa_public_key_cloud.pem b/wallfacer/verify/enclave/enclave/enclave/rsa_public_key_cloud.pem new file mode 100644 index 0000000000000000000000000000000000000000..a321f63ed2fb0459c03fcb63de8f2f21618d8e56 --- /dev/null +++ b/wallfacer/verify/enclave/enclave/enclave/rsa_public_key_cloud.pem @@ -0,0 +1,11 @@ +-----BEGIN PUBLIC KEY----- +MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAzAPwbnbgBg7JgXERA9Bx +p7GLI1S3e1zL83RMd2+GXb6kO4yMKUL3NUCE2HhA2BtQYmLyGovx59UUcKnU58is +Xux++kH+A2shmOPjYvEFuX0Kt8tc19b8M9b/iHsY8ZmKykqia2a5U+IrECRFJo5p +DWUnl7jrHVtq78BSR1c7iXG1frrEC0AYCuqKJo/fxfmOKL0Y9mENCB3nAwjn9unD +BsO/OhkqvvB3nkeuMfNKPh4wCqtQPve13eTojbuxjX/3ePijplTI5X2Gr+n6Ximn +fYRlytQmMgMl/db0ARSKNApq9bmwzVNrnGWWZWJksdRvf6iL7t17Gs4L9AApOuC9 +WkzxPvwp5ZUqjsGd4oJGWeC6ZE6BTw2vxE+xMFI9uAKHxq9pBKkcGMa0g4fANNNV ++W+8JZGanxEXKB3y/M7BCyQAPCWOHC/RNjmRA1gczLYCPzC4pWu935UZdF1RR6zY +CD3t+FoOGGET/g4CwWgyhb5qkp65Hs6ayYt/DUAqo+yBAgMBAAE= +-----END PUBLIC KEY-----