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