Makefile 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #
  2. # Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions
  6. # are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in
  12. # the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Intel Corporation nor the names of its
  15. # contributors may be used to endorse or promote products derived
  16. # from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #
  30. #
  31. ######## SGX SDK Settings ########
  32. SGX_SDK := /home/m2mazmud/sgx2.1/sgxsdk
  33. SGX_MODE := HW
  34. SGX_ARCH := x64
  35. SGX_DEBUG := 1
  36. ifeq ($(shell getconf LONG_BIT), 32)
  37. SGX_ARCH := x86
  38. else ifeq ($(findstring -m32, $(CXXFLAGS)), -m32)
  39. SGX_ARCH := x86
  40. endif
  41. ifeq ($(SGX_ARCH), x86)
  42. SGX_COMMON_CFLAGS := -m32
  43. SGX_LIBRARY_PATH := $(SGX_SDK)/lib
  44. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x86/sgx_sign
  45. SGX_EDGER8R := $(SGX_SDK)/bin/x86/sgx_edger8r
  46. else
  47. SGX_COMMON_CFLAGS := -m64
  48. SGX_LIBRARY_PATH := $(SGX_SDK)/lib64
  49. SGX_ENCLAVE_SIGNER := $(SGX_SDK)/bin/x64/sgx_sign
  50. SGX_EDGER8R := $(SGX_SDK)/bin/x64/sgx_edger8r
  51. endif
  52. ifeq ($(SGX_DEBUG), 1)
  53. ifeq ($(SGX_PRERELEASE), 1)
  54. $(error Cannot set SGX_DEBUG and SGX_PRERELEASE at the same time!!)
  55. endif
  56. endif
  57. ifeq ($(SGX_DEBUG), 1)
  58. SGX_COMMON_CFLAGS += -O0 -g
  59. else
  60. SGX_COMMON_CFLAGS += -O2
  61. endif
  62. ######## Library Settings ########
  63. #PHPTrust_Lib_Name := libPHPLocalAttestation_Trusted.a
  64. #PHPTrustLib_Cpp_Files := $(wildcard PHPLocalAttestationCode/*.cpp)
  65. #PHPTrustLib_Cpp_Objects := $(PHPTrustLib_Cpp_Files:.cpp=.o)
  66. #PHPTrustLib_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx -I$(SGX_SDK)/include/epid -I./Include
  67. #PHPTrustLib_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(TrustLib_Include_Paths)
  68. #PHPTrustLib_Compile_Cxx_Flags := -std=c++11 -nostdinc++
  69. OPENSSL_LIBRARY_PATH := /home/m2mazmud/old_stuff/intel-sgx-ssl/Linux/package/lib64/
  70. OPENSSL_BAREBONES_LIBRARY_PATH := /home/m2mazmud/old_stuff/intel-sgx-ssl/openssl_source/openssl-1.1.0h/
  71. OpenSSL_Crypto_Library_Name := sgx_tsgxssl_crypto
  72. SGXSSL_Library_Name := sgx_tsgxssl
  73. SgxSSL_Link_Libraries := -L$(OPENSSL_LIBRARY_PATH) -Wl,--whole-archive -l$(SGXSSL_Library_Name) -Wl,--no-whole-archive -l$(OpenSSL_Crypto_Library_Name)
  74. #Security_Link_Flags := -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -pie
  75. OPENSSL_INCLUDES := /home/m2mazmud/old_stuff/intel-sgx-ssl/Linux/package/include
  76. Trust_Lib_Name := libLocalAttestation_Trusted.a
  77. TrustLib_Cpp_Files := LocalAttestationCode/LocalAttestationTrustedWrapper.cpp LocalAttestationCode/LocalAttestationTrusted.cpp LocalAttestationCode/LocalAttestationCode_t.c
  78. TrustLib_Cpp_Objects := $(TrustLib_Cpp_Files:.cpp=.o)
  79. TrustLib_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx -I$(SGX_SDK)/include/epid -I./Include -I./Decryptor
  80. TrustLib_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fstack-protector $(TrustLib_Include_Paths) -Wall
  81. TrustLib_Compile_Cxx_Flags := -std=c++11 -nostdinc++
  82. #UnTrustLib_Name := libLocalAttestation_unTrusted.a
  83. #UnTrustLib_Cpp_Files := $(wildcard Untrusted_LocalAttestation/*.cpp)
  84. #UnTrustLib_Cpp_Objects := $(UnTrustLib_Cpp_Files:.cpp=.o)
  85. #UnTrustLib_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/ippcp -I./Include -I./LocalAttestationCode
  86. #UnTrustLib_Compile_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes -std=c++11 $(UnTrustLib_Include_Paths)
  87. ######## App Settings ########
  88. ifneq ($(SGX_MODE), HW)
  89. Urts_Library_Name := sgx_urts_sim
  90. else
  91. Urts_Library_Name := sgx_urts
  92. endif
  93. App_Cpp_Files := $(wildcard App/*.cpp)
  94. App_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/ippcp -I./Include -I./LocalAttestationCode
  95. App_Compile_Flags := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(App_Include_Paths) -Wall
  96. # Three configuration modes - Debug, prerelease, release
  97. # Debug - Macro DEBUG enabled.
  98. # Prerelease - Macro NDEBUG and EDEBUG enabled.
  99. # Release - Macro NDEBUG enabled.
  100. ifeq ($(SGX_DEBUG), 1)
  101. App_Compile_Flags += -DDEBUG -UNDEBUG -UEDEBUG
  102. else ifeq ($(SGX_PRERELEASE), 1)
  103. App_Compile_Flags += -DNDEBUG -DEDEBUG -UDEBUG
  104. else
  105. App_Compile_Flags += -DNDEBUG -UEDEBUG -UDEBUG
  106. endif
  107. #App_Link_Flags := $(SGX_COMMON_CFLAGS) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -L. -lpthread -lprotobuf
  108. App_Link_Flags := $(SGX_COMMON_CFLAGS) $(Security_Link_Flags) -L$(SGX_LIBRARY_PATH) -l$(Urts_Library_Name) -lsgx_uae_service -L$(OPENSSL_LIBRARY_PATH) -lsgx_usgxssl -lpthread -lprotobuf #-L$(OPENSSL_BAREBONES_LIBRARY_PATH) -lcrypto
  109. ifneq ($(SGX_MODE), HW)
  110. App_Link_Flags += -lsgx_uae_service_sim
  111. else
  112. App_Link_Flags += -lsgx_uae_service
  113. endif
  114. App_Cpp_Objects := App/App.o App/LocalAttestationUntrusted.o App/protobufLAInitiator.o App/protobufSgxLATransformsInitiator.o App/ProtobufLAMessages.pb.o #$(App_Cpp_Files:.cpp=.o)
  115. App_Name := app
  116. ######## Enclave Settings ########
  117. Decryptor_Version_Script := Decryptor/Decryptor.lds
  118. ifneq ($(SGX_MODE), HW)
  119. Trts_Library_Name := sgx_trts_sim
  120. Service_Library_Name := sgx_tservice_sim
  121. else
  122. Trts_Library_Name := sgx_trts
  123. Service_Library_Name := sgx_tservice
  124. endif
  125. Crypto_Library_Name := sgx_tcrypto
  126. Enclave_Cpp_Files_2 := $(wildcard Decryptor/*.cpp)
  127. Enclave_Include_Paths := -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/libcxx -I./LocalAttestationCode -I./Include
  128. CC_BELOW_4_9 := $(shell expr "`$(CC) -dumpversion`" \< "4.9")
  129. ifeq ($(CC_BELOW_4_9), 1)
  130. Enclave_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections -fstack-protector -Wno-unused-variable -Wall
  131. else
  132. Enclave_Compile_Flags := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -ffunction-sections -fdata-sections -fstack-protector-strong -Wno-unused-variable -Wall
  133. endif
  134. Enclave_Compile_Flags += $(Enclave_Include_Paths)
  135. # To generate a proper enclave, it is recommended to follow below guideline to link the trusted libraries:
  136. # 1. Link sgx_trts with the `--whole-archive' and `--no-whole-archive' options,
  137. # so that the whole content of trts is included in the enclave.
  138. # 2. For other libraries, you just need to pull the required symbols.
  139. # Use `--start-group' and `--end-group' to link these libraries.
  140. # Do NOT move the libraries linked with `--start-group' and `--end-group' within `--whole-archive' and `--no-whole-archive' options.
  141. # Otherwise, you may get some undesirable errors.
  142. Common_Enclave_Link_Flags := $(SGX_COMMON_CFLAGS) -Wl,--no-undefined -nostdlib -nodefaultlibs -nostartfiles \
  143. -Wl,--whole-archive -L$(OPENSSL_LIBRARY_PATH) -lsgx_tsgxssl -Wl,--no-whole-archive -lsgx_tsgxssl_crypto \
  144. -Wl,--verbose -Wl,--whole-archive -l$(Trts_Library_Name) -Wl,--no-whole-archive \
  145. -Wl,--start-group -L$(SGX_LIBRARY_PATH) -L. -L./Decryptor -L./LocalAttestationCode -lsgx_tstdc -lsgx_tcxx -l$(Crypto_Library_Name) -l$(Service_Library_Name) -Wl,--end-group \
  146. -Wl,-Bstatic -Wl,-Bsymbolic \
  147. -Wl,-pie,-eenclave_entry -Wl,--export-dynamic \
  148. -Wl,--defsym,__ImageBase=0 -Wl,--gc-sections
  149. Decryptor_Link_Flags := $(Common_Enclave_Link_Flags) -Wl,--version-script=$(Decryptor_Version_Script)
  150. Enclave_Cpp_Objects_2 := $(Enclave_Cpp_Files_2:.cpp=.o)
  151. Enclave_Name_2 := libDecryptor.so
  152. ifeq ($(SGX_MODE), HW)
  153. ifeq ($(SGX_DEBUG), 1)
  154. Build_Mode = HW_DEBUG
  155. else ifeq ($(SGX_PRERELEASE), 1)
  156. Build_Mode = HW_PRERELEASE
  157. else
  158. Build_Mode = HW_RELEASE
  159. endif
  160. else
  161. ifeq ($(SGX_DEBUG), 1)
  162. Build_Mode = SIM_DEBUG
  163. else ifeq ($(SGX_PRERELEASE), 1)
  164. Build_Mode = SIM_PRERELEASE
  165. else
  166. Build_Mode = SIM_RELEASE
  167. endif
  168. endif
  169. ifeq ($(Build_Mode), HW_RELEASE)
  170. all: .config_$(Build_Mode)_$(SGX_ARCH) $(Trust_Lib_Name) $(UnTrustLib_Name) Decryptor.so $(App_Name)
  171. @echo "The project has been built in release hardware mode."
  172. @echo "Please sign the enclaves (PHP.so, Decryptor.so, Enclave3.so) first with your signing keys before you run the $(App_Name) to launch and access the enclave."
  173. @echo "To sign the enclaves use the following commands:"
  174. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <key1> -enclave PHP.so -out <$(Enclave_Name_1)> -config PHP/PHP.config.xml"
  175. @echo " $(SGX_ENCLAVE_SIGNER) sign -key <key2> -enclave Decryptor.so -out <$(Enclave_Name_2)> -config Decryptor/Decryptor.config.xml"
  176. # @echo " $(SGX_ENCLAVE_SIGNER) sign -key <key3> -enclave Enclave3.so -out <$(Enclave_Name_3)> -config Enclave3/Enclave3.config.xml"
  177. @echo "You can also sign the enclaves using an external signing tool."
  178. @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."
  179. else
  180. all: .config_$(Build_Mode)_$(SGX_ARCH) $(Trust_Lib_Name) $(UnTrustLib_Name) $(Enclave_Name_2) $(App_Name)
  181. ifeq ($(Build_Mode), HW_DEBUG)
  182. @echo "The project has been built in debug hardware mode."
  183. else ifeq ($(Build_Mode), SIM_DEBUG)
  184. @echo "The project has been built in debug simulation mode."
  185. else ifeq ($(Build_Mode), HW_PRERELEASE)
  186. @echo "The project has been built in pre-release hardware mode."
  187. else ifeq ($(Build_Mode), SIM_PRERELEASE)
  188. @echo "The project has been built in pre-release simulation mode."
  189. else
  190. @echo "The project has been built in release simulation mode."
  191. endif
  192. endif
  193. .config_$(Build_Mode)_$(SGX_ARCH):
  194. @rm -rf .config_* $(App_Name) *.so *.a App/*.o Decryptor/*.o Decryptor/*_t.* Decryptor/*_u.* LocalAttestationCode/*.o LocalAttestationCode/*_t.* #Enclave3/*.o Enclave3/*_t.* Enclave3/*_u.*
  195. @touch .config_$(Build_Mode)_$(SGX_ARCH)
  196. ######## Library Objects ########
  197. LocalAttestationCode/LocalAttestationCode_t.c LocalAttestationCode/LocalAttestationCode_t.h : $(SGX_EDGER8R) LocalAttestationCode/LocalAttestationCode.edl
  198. @cd LocalAttestationCode && $(SGX_EDGER8R) --trusted ../LocalAttestationCode/LocalAttestationCode.edl --search-path $(SGX_SDK)/include
  199. @echo "GEN => $@"
  200. LocalAttestationCode/LocalAttestationCode_t.o: LocalAttestationCode/LocalAttestationCode_t.c
  201. @$(CC) $(TrustLib_Compile_Flags) -c $< -o $@
  202. @echo "CC <= $<"
  203. LocalAttestationCode/LocalAttestationTrustedWrapper.o: LocalAttestationCode/LocalAttestationTrustedWrapper.cpp LocalAttestationCode/LocalAttestationCode_t.h
  204. @$(CXX) $(TrustLib_Compile_Flags) $(TrustLib_Compile_Cxx_Flags) -c $< -o $@
  205. @echo "CC <= $<"
  206. LocalAttestationCode/LocalAttestationTrusted.o: LocalAttestationCode/LocalAttestationTrusted.cpp LocalAttestationCode/LocalAttestationCode_t.h
  207. @$(CXX) $(TrustLib_Compile_Flags) $(TrustLib_Compile_Cxx_Flags) -c $< -o $@
  208. @echo "CC <= $<"
  209. $(Trust_Lib_Name): LocalAttestationCode/LocalAttestationCode_t.o LocalAttestationCode/LocalAttestationTrustedWrapper.o LocalAttestationCode/LocalAttestationTrusted.o
  210. @$(AR) rcs $@ $^
  211. @echo "GEN => $@"
  212. CommonOpensslCode/Openssl_crypto.o: CommonOpensslCode/Openssl_crypto.cpp
  213. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -I$(OPENSSL_INCLUDES) -c $< -o $@
  214. @echo "CXX <= $<"
  215. #Untrusted_LocalAttestation/%.o: Untrusted_LocalAttestation/%.cpp
  216. # @$(CXX) $(UnTrustLib_Compile_Flags) -c $< -o $@
  217. # @echo "CC <= $<"
  218. #$(UnTrustLib_Name): $(UnTrustLib_Cpp_Objects)
  219. # @$(AR) rcs $@ $^
  220. # @echo "GEN => $@"
  221. ######## App Objects ########
  222. Decryptor/Decryptor_u.c Decryptor/Decryptor_u.h: $(SGX_EDGER8R) Decryptor/Decryptor.edl
  223. @cd Decryptor && $(SGX_EDGER8R) --use-prefix --untrusted ../Decryptor/Decryptor.edl --search-path $(SGX_SDK)/include
  224. @echo "GEN => $@"
  225. App/Decryptor_u.o: Decryptor/Decryptor_u.c
  226. @$(CC) $(App_Compile_Flags) -c $< -o $@
  227. @echo "CC <= $<"
  228. App/%.o: App/%.cpp Decryptor/Decryptor_u.h #Enclave3/Enclave3_u.h
  229. @$(CXX) $(App_Compile_Flags) -c $< -o $@
  230. @echo "CXX <= $<"
  231. $(App_Name): $(App_Cpp_Objects) App/Decryptor_u.o #$(App_Cpp_Objects)# $(UnTrustLib_Name)
  232. @$(CXX) -Wl,--no-undefined $^ -o $@ $(App_Link_Flags)
  233. @echo "LINK => $@"
  234. ######### Crypto classes for enclave ############
  235. ######## Enclave Objects ########
  236. Decryptor/Decryptor_t.c: $(SGX_EDGER8R) Decryptor/Decryptor.edl
  237. @cd Decryptor && $(SGX_EDGER8R) --use-prefix --trusted ../Decryptor/Decryptor.edl --search-path $(SGX_SDK)/include
  238. @echo "GEN => $@"
  239. Decryptor/Decryptor_t.o: Decryptor/Decryptor_t.c
  240. @$(CC) $(Enclave_Compile_Flags) -c $< -o $@
  241. @echo "CC <= $<"
  242. Decryptor/%Box.o: Decryptor/%Box.cpp
  243. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  244. @echo "CXX <= $<"
  245. Decryptor/ECDHKeypair.o: Decryptor/ECDHKeypair.cpp
  246. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  247. @echo "CXX <= $<"
  248. Decryptor/DecryptorWrapper.o: Decryptor/DecryptorWrapper.cpp
  249. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  250. @echo "CXX <= $<"
  251. Decryptor/Decryptor.o: Decryptor/Decryptor.cpp #Decryptor/HybridEncryptionBox.cpp Decryptor/ECDSASignatureBox.cpp Decryptor/ECDHKeypair.cpp Decryptor/SymmetricEncryptionBox.cpp
  252. @$(CXX) -std=c++11 -nostdinc++ $(Enclave_Compile_Flags) -c $< -o $@
  253. @echo "CXX <= $<"
  254. Decryptor.so: Decryptor/Decryptor_t.o Decryptor/DecryptorWrapper.o Decryptor/Decryptor.o $(Trust_Lib_Name) Decryptor/HybridEncryptionBox.o Decryptor/ECDSASignatureBox.o Decryptor/SymmetricEncryptionBox.o Decryptor/ECDHKeypair.o CommonOpensslCode/Openssl_crypto.o
  255. @$(CXX) $^ -o $@ $(Decryptor_Link_Flags)
  256. @echo "LINK => $@"
  257. $(Enclave_Name_2): Decryptor.so
  258. @$(SGX_ENCLAVE_SIGNER) sign -key Decryptor/Decryptor_private.pem -enclave Decryptor.so -out $@ -config Decryptor/Decryptor.config.xml
  259. @echo "SIGN => $@"
  260. ######## Clean ########
  261. .PHONY: clean
  262. clean:
  263. @rm -rf .config_* $(App_Name) *.so *.a App/*.o Decryptor/*.o Decryptor/*_t.* Decryptor/*_u.* LocalAttestationCode/*.o LocalAttestationCode/*_t.*