################################################################################
#
# <t-sdk Sample Sha256 Trusted Application
#
################################################################################

# Set paths
TRUSTLET_DIR := Locals/Code

# output binary name without path or extension
OUTPUT_NAME := tlSkpm

#-------------------------------------------------------------------------------
# MobiConvert parameters, see manual for details
#-------------------------------------------------------------------------------
TRUSTLET_UUID := FFFFFFFF00000000000000000000002F
TRUSTLET_MEMTYPE := 2
TRUSTLET_NO_OF_THREADS := 1
TRUSTLET_SERVICE_TYPE := 3 # 2: service provider trustlet; 3: system trustlet
TRUSTLET_FLAGS := 0
TRUSTLET_INSTANCES := 1

ifeq ($(USE_TBASE_API_LEVEL_11),True)
    HEAP_SIZE_INIT := 300000
    HEAP_SIZE_MAX := 300000
    HW_FLOATING_POINT := Y
    TBASE_API_LEVEL := 11
else
    ifeq ($(USE_SCRYPTO),True)
        HW_FLOATING_POINT := Y
        TBASE_API_LEVEL := 5
    else
        TBASE_API_LEVEL := 4
    endif
endif

ifeq ($(USE_QUICKBUILD_RBS),True)
    RUNTYPE := $(SIGN_RUN_TYPE)
    ifeq ($(HW_FLOATING_POINT),Y)
        TRUSTLET_SIGN_CONF := "\"-s 3 -n 1 -i 1 -m 2 -f 8 -sh 300000 -mh 300000\""    
    else
        TRUSTLET_SIGN_CONF := "\"-s 3 -n 1 -i 1 -m 2 -f 0\""
    endif

    $(warning RUNTYPE : $(RUNTYPE))
    $(warning TRUSTLET_SIGN_CONF : $(TRUSTLET_SIGN_CONF))
endif

ifeq ($(TOOLCHAIN),GNU)
    TRUSTLET_OPTS += -Werror
endif
ifeq ($(TOOLCHAIN),ARM)
   TRUSTLET_OPTS += --diag_error=warning
endif

$(warning *****************************)
$(warning TBASE_API_LEVEL : $(TBASE_API_LEVEL))
$(warning TZ_CHIPSET : $(TZ_CHIPSET))
$(warning PLATFORM_VER : $(PLATFORM_VER))
$(warning USE_SCRYPTO : $(USE_SCRYPTO))
$(warning SCRYPTO_VER : $(SCRYPTO_VER))
$(warning USE_QUICKBUILD_RBS : $(USE_QUICKBUILD_RBS))
$(warning SUPPORT_NEW_TA_BUILD : $(SUPPORT_NEW_TA_BUILD))
$(warning USE_TBASE_API_LEVEL_11 : $(USE_TBASE_API_LEVEL_11))
$(warning SKIP_STACK_CHK_FAIL : $(SKIP_STACK_CHK_FAIL))
$(warning *****************************)

#-------------------------------------------------------------------------------
# CRYPTO
#-------------------------------------------------------------------------------
INCLUDE_DIRS += \
    $(TRUSTLET_DIR)/include/crypto

ifeq ($(USE_SCRYPTO),True)
INCLUDE_DIRS += \
    $(TRUSTLET_DIR)/include/crypto/scrypto/$(SCRYPTO_VER)

SRC_CPP += \
    $(TRUSTLET_DIR)/src/common/crypto/platform/mobicore.c \
    $(TRUSTLET_DIR)/src/common/crypto/crypto_module.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_ccm.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_print.c

else
INCLUDE_DIRS += \
    $(TRUSTLET_DIR)/include/crypto/cryptocore \
    ${TRUSTLET_DIR}/src/common/crypto \
    ${TRUSTLET_DIR}/src/common/crypto/libc_functions/portable/allocators

SRC_CPP += \
    $(TRUSTLET_DIR)/src/common/crypto/platform/mobicore.c \
    $(TRUSTLET_DIR)/src/common/crypto/crypto_module.c \
    $(TRUSTLET_DIR)/src/common/crypto/crypto_core_init.c \
    $(TRUSTLET_DIR)/src/common/crypto/cryptolib_init.c \
    $(TRUSTLET_DIR)/src/common/crypto/cryptolib_ver.c \
    $(TRUSTLET_DIR)/src/common/crypto/pd_rand/pd_rand.c \
    $(TRUSTLET_DIR)/src/common/crypto/rand/cc_rand.c \
    $(TRUSTLET_DIR)/src/common/crypto/drbg/ctr_drbg.c \
    $(TRUSTLET_DIR)/src/common/crypto/drbg/ctr_drbg_raw.c \
    $(TRUSTLET_DIR)/src/common/crypto/drbg/ctr_drbg_ossl.c \
    $(TRUSTLET_DIR)/src/common/crypto/libc_functions/libc_functions.c \
    $(TRUSTLET_DIR)/src/common/crypto/libc_functions/portable/allocators/memmgrs.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/mem_clr.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdh/ech_ossl.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdh/ech_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdh/ech_key.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/sha/sha256.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/sha/sha512.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/sha/sha1dgst.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/sha/sha1_one.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/sha/sha_one.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/sha/sha_dgst.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/lhash/lhash.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/gcm128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/cfb128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/ofb128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/cts128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/ctr128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/cbc128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/ccm128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/modes/xts128.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/a_object.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/tasn_utl.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/tasn_dec.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/a_dup.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/a_type.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/tasn_fre.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/tasn_enc.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/tasn_typ.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/x_algor.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/a_int.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/x_attrib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/x_sig.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/a_bitstr.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/x_bignum.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/asn1_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/asn1/tasn_new.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_word.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_recp.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_mpi.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_mod.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_print.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_const.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_sqr.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_asm.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_div.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_prime.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_mul.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_add.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_mont.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_rand.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_gcd.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_exp.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_shift.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_nist.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_blind.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_kron.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_sqrt.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bn/bn_ctx.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdsa/ecs_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdsa/ecs_ossl.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdsa/ecs_sign.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdsa/ecs_vrf.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ecdsa/ecs_asn1.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_cbc.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_ccm.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_cfb.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_core.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_ctr.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_ecb.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_ige.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_misc.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_ofb.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/aes/aes_wrap.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ex_data.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/m_sha1.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/e_aes.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/digest.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/c_all.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/c_allc.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/names.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/p_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/evp_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/pmeth_gn.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/c_alld.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/pmeth_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/evp/evp_enc.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/hmac/hmac.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/stack/stack.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/buffer/buffer.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/objects/obj_dat.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/objects/o_names.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/objects/obj_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/bio/b_print.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/cryptlib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rand/rand_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rand/rand_poll_target.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rand/md_rand.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/mem.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_pmeth.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_err.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_pk1.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_oaep.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_gen.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_x931.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_none.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_sign.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_crpt.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_ssl.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_eay.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_saos.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_pss.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_chk.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/rsa/rsa_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_pmeth.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_lib.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ecp_oct.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_mult.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_key.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_oct.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ecp_smpl.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_cvt.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_curve.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ec_print.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ecp_nist.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ecp_nistputil.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ecp_nistp256.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/ec/ecp_mont.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/o_init.c \
    $(TRUSTLET_DIR)/src/common/crypto/openssl/err/err.c
endif

#-------------------------------------------------------------------------------
# SWBC
#-------------------------------------------------------------------------------
INCLUDE_DIRS += \
    $(TRUSTLET_DIR)/include/swbc

SRC_CPP += \
    $(TRUSTLET_DIR)/src/common/swbc/aes128-swbc-layer1-cbc.c \
    $(TRUSTLET_DIR)/src/common/swbc/swbc-aes.c \
    $(TRUSTLET_DIR)/src/common/swbc/swbc-utils.c

#-------------------------------------------------------------------------------
# SKPM
#-------------------------------------------------------------------------------
INCLUDE_DIRS += \
    $(TRUSTLET_DIR)/include \
    $(TRUSTLET_DIR)/include/skpm \
    $(TRUSTLET_DIR)/include/skpm/mobicore

SRC_CPP += \
    $(TRUSTLET_DIR)/src/tlSkpm.c \
    $(TRUSTLET_DIR)/src/stack_protection.c \
    $(TRUSTLET_DIR)/src/common/skpm/debug.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm_tls.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm_util.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm_test.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm_crypto_util.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm_cmd_handler.c \
    $(TRUSTLET_DIR)/src/common/skpm/skpm_service_key.c

#-------------------------------------------------------------------------------

### Add source code files for C compiler here
SRC_C += # nothing

### Add source code files for assembler here
SRC_S += # nothing

# Add custom library
CUSTOMER_DRIVER_LIBS +=

ifeq ($(USE_SCRYPTO),True)
ARMCC_COMPILATION_FLAGS += \
    -DMOBICORE \
    -DOPENSSL_FIPS \
    -D_AEABI_PORTABILITY_LEVEL=1 \
    -D__ARMCC__

ARMCC_COMPILATION_FLAGS += --diag_remark=191

TA_LINK_OPTS += --diag_suppress=L6367E
TA_LINK_OPTS += --diag_suppress=L6366E
TA_LINK_OPTS += --diag_suppress=L6242E

else
ARMCC_COMPILATION_FLAGS += \
	-DOPENSSL_NO_LOCKING \
	-DNO_SYS_TYPES_H \
	-DOPENSSL_NO_DSA \
	-DOPENSSL_NO_DH \
	-DOPENSSL_NO_ENGINE \
	-DOPENSSL_NO_STDIO \
	-DGETPID_IS_MEANINGLESS \
	-DOPENSSL_NO_CMS \
	-DOPENSSL_EXPORT_VAR_AS_FUNCTION \
	-DOWN_ALLOCATOR \
	-DOPENSSL_NO_ASM \
	-DMOBICORE
endif

ARMCC_COMPILATION_FLAGS += --protect_stack --protect_stack_all

#-------------------------------------------------------------------------------
include $(TLSDK_DIR_SRC)/trustlet.mk

# Add library swbc
LIBS +=

# Add library scrypto
ifeq ($(USE_SCRYPTO),True)
CC_OPTS += -DUSE_SCRYPTO
LIBS += $(SCRYPTO_LIB_FILE)

run_fips_tools: $(TA_AXF)
	$(FIPS_TOOLS_SCRIPT) $<

all: run_fips_tools
endif

ifeq ($(USE_TBASE_API_LEVEL_11),True)
CC_OPTS += -DUSE_TBASE_API_LEVEL_11
endif

ifeq ($(SKIP_STACK_CHK_FAIL),True)
CC_OPTS += \
    -DSKIP_STACK_CHK_FAIL
endif

#-------------------------------------------------------------------------------
# Make libraries
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#CC_OPTS += -DDEBUG_LOW

# CONFIG
CC_OPTS += \
    -DUSE_MOBICORE \
    -DDEBUG
UPPER_TZ_CHIPSET := $(shell echo $(TZ_CHIPSET) | tr [a-z] [A-Z])
$(warning ########### $(UPPER_TZ_CHIPSET) ########### )
CC_OPTS += \
    -D$(UPPER_TZ_CHIPSET)

CC_OPTS += \
    -DTZ_CHIPSET=\"$(TZ_CHIPSET)\"

ifeq ($(PLATFORM_VER),T)
CC_OPTS += -DTIZEN
endif

