1#-------------------------------------------------------------------------------
2# Copyright (c) 2020-2023, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7# Allow linking to things 'upwards' in the directory tree (in this case bl2 / tfm_psa_rot_partition_crypto)
8cmake_policy(SET CMP0079 NEW)
9# Allow relative paths
10cmake_policy(SET CMP0076 NEW)
11
12if(NOT DEFINED CC312_PATH)
13    set(CC312_PATH ../../../../lib/ext/cryptocell-312-runtime CACHE PATH "Path to CC312 lib")
14endif()
15
16if(BL2)
17    target_compile_definitions(platform_bl2
18        PRIVATE
19            CRYPTO_HW_ACCELERATOR
20    )
21endif()
22
23################################ BL2 ###########################################
24
25if(BL2)
26
27    set(CC312_LIB_PREFIX bl2_)
28    # Platform depedency needed to access platform specific dx_reg_base_host.h
29    set(CC312_PLATFORM_DEPENDENCY platform_bl2)
30
31    target_sources(bl2_crypto_hw
32        PRIVATE
33            ${CMAKE_CURRENT_SOURCE_DIR}/cc312.c
34    )
35
36    target_include_directories(bl2_crypto_hw
37        PUBLIC
38            ${CMAKE_CURRENT_SOURCE_DIR}
39    )
40
41    if (${PLATFORM_PSA_ADAC_SECURE_DEBUG})
42        add_subdirectory(psa-adac)
43    endif()
44
45    target_link_libraries(bl2_crypto_hw
46        PUBLIC
47            ${CC312_LIB_PREFIX}cc312
48        PRIVATE
49            bl2_mbedcrypto
50            platform_bl2
51    )
52
53    set(SAVED_BUILD_TYPE ${CMAKE_BUILD_TYPE})
54    set(CMAKE_BUILD_TYPE ${MBEDCRYPTO_BUILD_TYPE})
55    add_subdirectory(${CC312_PATH} ${CMAKE_CURRENT_BINARY_DIR}/${CC312_LIB_PREFIX}cc312)
56    set(CMAKE_BUILD_TYPE ${SAVED_BUILD_TYPE} CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
57
58    target_sources(${CC312_LIB_PREFIX}cc312
59        PUBLIC
60            $<$<OR:$<CONFIG:Debug>,$<CONFIG:relwithdebinfo>>:${CMAKE_CURRENT_SOURCE_DIR}/cc312_log.c>
61    )
62
63    # Adding two targets as link-time dependencies of each other seems bad, but
64    # in reality it just means that they'll share headers and compile defs.
65    target_link_libraries(${CC312_LIB_PREFIX}cc312_mbedtls_api
66        PRIVATE
67            bl2_mbedcrypto
68            platform_bl2
69        PUBLIC
70            platform_common_interface
71    )
72    target_link_libraries(bl2_mbedcrypto
73        PRIVATE
74            ${CC312_LIB_PREFIX}cc312_mbedtls_api
75        PUBLIC
76            bl2_crypto_hw
77    )
78
79    # boot_hal depends on crypto_hw_accelerator abstractions
80    target_link_libraries(platform_bl2
81        PRIVATE
82            bl2_crypto_hw
83            tfm_config
84    )
85
86    target_link_libraries(${CC312_LIB_PREFIX}cc312_cdmpu
87        INTERFACE
88            bl2_mbedcrypto
89    )
90
91    target_compile_definitions(bl2_mbedcrypto
92        PUBLIC
93            CRYPTO_HW_ACCELERATOR
94            MBEDTLS_ECDH_LEGACY_CONTEXT
95    )
96
97    target_compile_options(bl2_mbedcrypto
98        PRIVATE
99            $<$<C_COMPILER_ID:GNU>:-Wno-unused-parameter>
100            $<$<C_COMPILER_ID:ARMClang>:-Wno-unused-parameter>
101    )
102
103    target_compile_options(${CC312_LIB_PREFIX}cc312
104        PRIVATE
105            ${BL2_COMPILER_CP_FLAG}
106    )
107
108    target_compile_options(bl2_crypto_hw
109        PRIVATE
110            ${BL2_COMPILER_CP_FLAG}
111    )
112
113    target_compile_options(${CC312_LIB_PREFIX}cc312_cdmpu
114        INTERFACE
115            ${BL2_COMPILER_CP_FLAG}
116    )
117
118    unset(CC312_LIB_PREFIX)
119    unset(CC312_PLATFORM_DEPENDENCY)
120
121endif()
122
123############################ Crypto Service ####################################
124
125if (TFM_PARTITION_CRYPTO)
126    set(CC312_LIB_PREFIX crypto_service_)
127    # Platform depedency needed to access platform specific dx_reg_base_host.h
128    set(CC312_PLATFORM_DEPENDENCY platform_s)
129
130    target_sources(crypto_service_crypto_hw
131        PRIVATE
132            cc312.c
133            $<$<OR:$<CONFIG:Debug>,$<CONFIG:relwithdebinfo>>:${CMAKE_CURRENT_SOURCE_DIR}/cc312_log.c>
134    )
135
136    target_include_directories(crypto_service_crypto_hw
137        PUBLIC
138            ${CMAKE_CURRENT_SOURCE_DIR}
139    )
140
141    target_link_libraries(crypto_service_crypto_hw
142        PUBLIC
143            ${CC312_LIB_PREFIX}cc312
144        PRIVATE
145            crypto_service_mbedcrypto
146            platform_s
147    )
148
149    set(SAVED_BUILD_TYPE ${CMAKE_BUILD_TYPE})
150    set(CMAKE_BUILD_TYPE ${MBEDCRYPTO_BUILD_TYPE})
151    add_subdirectory(${CC312_PATH} ${CMAKE_CURRENT_BINARY_DIR}/${CC312_LIB_PREFIX}cc312)
152    set(CMAKE_BUILD_TYPE ${SAVED_BUILD_TYPE} CACHE STRING "Build type: [Debug, Release, RelWithDebInfo, MinSizeRel]" FORCE)
153
154    target_sources(${CC312_LIB_PREFIX}cc312
155        PRIVATE
156            $<$<OR:$<CONFIG:Debug>,$<CONFIG:relwithdebinfo>>:${CMAKE_CURRENT_SOURCE_DIR}/cc312_log.c>
157    )
158
159    # Control the enablement of the legacy CC-312 driver interface or the PSA
160    # driver interface. Default value is for the legacy interface
161    option(CC312_LEGACY_DRIVER_API_ENABLED
162           "This variable controls whether the legacy driver interface is used for CC-312." ON)
163
164    # FixMe: Secure tests enabled and Debug builds with FP support set to hardware
165    #        need to fallback to the legacy driver as the new PSA driver overflows
166    #        the available flash memory on Musca-S1 and Musca-B1
167    if ((NOT ${CC312_LEGACY_DRIVER_API_ENABLED}) AND
168        TEST_S AND (${CMAKE_BUILD_TYPE} STREQUAL "Debug") AND (${CONFIG_TFM_FLOAT_ABI} STREQUAL "hard"))
169        set(CC312_LEGACY_DRIVER_API_ENABLED ON)
170        message(WARNING
171            "The CC-312 legacy driver interface fallback is forced to ${CC312_LEGACY_DRIVER_API_ENABLED}. \
172            The following parameters are not supported at the same time: \
173            (TEST_S: ${TEST_S}, \
174            CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}, \
175            CONFIG_TFM_FLOAT_ABI: ${CONFIG_TFM_FLOAT_ABI}) for the PSA Cryptoprocessor driver interface due to flash memory constraints.")
176    endif()
177
178    # FixMe: Secure tests enabled and Debug builds on Musca-B1 need to fallback to
179    #        the legacy driver as the new PSA driver overflows the available flash
180    #        memory of the SSE-200 subsystem
181    if ((NOT ${CC312_LEGACY_DRIVER_API_ENABLED}) AND
182        TEST_S AND (${CMAKE_BUILD_TYPE} STREQUAL "Debug") AND (${TFM_PLATFORM} STREQUAL "arm/musca_b1"))
183        set(CC312_LEGACY_DRIVER_API_ENABLED ON)
184        message(WARNING
185            "The CC-312 legacy driver interface fallback is forced to ${CC312_LEGACY_DRIVER_API_ENABLED}. \
186            The following parameters are not supported at the same time: \
187            (TEST_S: ${TEST_S}, \
188            CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}, \
189            TFM_PLATFORM: ${TFM_PLATFORM}) for the PSA Cryptoprocessor driver interface due to flash memory constraints.")
190    endif()
191
192    if (${CC312_LEGACY_DRIVER_API_ENABLED})
193        set(CC312_DRIVER_API_TARGET ${CC312_LIB_PREFIX}cc312_mbedtls_api)
194    else()
195        set(CC312_DRIVER_API_TARGET ${CC312_LIB_PREFIX}cc312_psa_driver_api)
196    endif()
197
198    # Adding two targets as link-time dependencies of each other seems bad, but
199    # in reality it just means that they'll share headers and compile defs.
200    target_link_libraries(${CC312_DRIVER_API_TARGET}
201        PRIVATE
202            crypto_service_mbedcrypto
203        PUBLIC
204            platform_s
205    )
206    target_link_libraries(crypto_service_mbedcrypto
207        PUBLIC
208            ${CC312_DRIVER_API_TARGET}
209            crypto_service_crypto_hw
210    )
211
212    target_compile_definitions(crypto_service_mbedcrypto
213        PUBLIC
214            CRYPTO_HW_ACCELERATOR
215            MBEDTLS_ECDH_LEGACY_CONTEXT
216            $<$<BOOL:${CC312_LEGACY_DRIVER_API_ENABLED}>:LEGACY_DRIVER_API_ENABLED>
217            $<$<BOOL:${CC312_LEGACY_DRIVER_API_ENABLED}>:CC312_LEGACY_DRIVER_API_ENABLED>
218    )
219
220    target_include_directories(psa_crypto_config
221        INTERFACE
222            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
223    )
224
225    target_compile_options(crypto_service_cc312
226        PUBLIC
227            $<$<C_COMPILER_ID:GNU>:-Wno-unused-parameter>
228            $<$<C_COMPILER_ID:ARMClang>:-Wno-unused-parameter>
229    )
230
231    target_compile_options(crypto_service_mbedcrypto
232        PRIVATE
233            $<$<C_COMPILER_ID:GNU>:-Wno-unused-parameter>
234            $<$<C_COMPILER_ID:ARMClang>:-Wno-unused-parameter>
235    )
236
237    unset(CC312_LIB_PREFIX)
238    unset(CC312_PLATFORM_DEPENDENCY)
239endif()
240