blob: bfd633947c903d1b508b7e59633c98047bb46728 [file] [log] [blame]
Michael Spangefa630b2020-07-08 22:23:08 -04001# Copyright (c) 2020 Project CHIP Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Łukasz Duda4e1faf22021-01-27 17:47:25 +010015import("//build_overrides/build.gni")
Michael Spangefa630b2020-07-08 22:23:08 -040016import("//build_overrides/chip.gni")
17import("//build_overrides/nlassert.gni")
18
Michael Spang09611bf2021-03-02 16:31:57 -050019import("${chip_root}/build/chip/buildconfig_header.gni")
Michael Spang1fcb6dd2020-08-24 11:19:04 -040020
Michael Spangefa630b2020-07-08 22:23:08 -040021import("crypto.gni")
22
Steven Cooreman3dc97f02022-08-04 20:34:20 +020023if (chip_crypto == "") {
24 if (current_os == "android" || current_os == "freertos" ||
Artur Tynecki0efd3182022-12-05 20:18:32 +010025 current_os == "zephyr" || current_os == "mbed" || current_os == "webos" ||
26 current_os == "cmsis-rtos") {
Steven Cooreman3dc97f02022-08-04 20:34:20 +020027 chip_crypto = "mbedtls"
Boris Zbarsky9c631df2023-02-09 12:29:36 -050028 } else if (current_os == "mac" || current_os == "ios") {
29 chip_crypto = "boringssl"
Steven Cooreman3dc97f02022-08-04 20:34:20 +020030 } else {
31 chip_crypto = "openssl"
32 }
33}
34
35assert(
Damian Królikd37c8012022-10-20 04:41:14 +020036 chip_crypto == "mbedtls" || chip_crypto == "psa" ||
andrei-menzopol7d17e472023-02-09 16:23:22 +020037 chip_crypto == "openssl" || chip_crypto == "boringssl" ||
38 chip_crypto == "platform",
39 "Please select a valid crypto implementation: mbedtls, psa, openssl, boringssl, platform")
Steven Cooreman3dc97f02022-08-04 20:34:20 +020040
Michael Spang1fcb6dd2020-08-24 11:19:04 -040041buildconfig_header("crypto_buildconfig") {
42 header = "CryptoBuildConfig.h"
43 header_dir = "crypto"
44
Tennessee Carmel-Veilleux768b5752022-07-18 22:13:02 -040045 chip_crypto_mbedtls = chip_crypto == "mbedtls"
Damian Królikd37c8012022-10-20 04:41:14 +020046 chip_crypto_psa = chip_crypto == "psa"
Tennessee Carmel-Veilleux768b5752022-07-18 22:13:02 -040047 chip_crypto_openssl = chip_crypto == "openssl"
48 chip_crypto_boringssl = chip_crypto == "boringssl"
Steven Cooreman3dc97f02022-08-04 20:34:20 +020049 chip_crypto_platform = chip_crypto == "platform"
Tennessee Carmel-Veilleux768b5752022-07-18 22:13:02 -040050
51 defines = [
52 "CHIP_CRYPTO_MBEDTLS=${chip_crypto_mbedtls}",
Damian Królikd37c8012022-10-20 04:41:14 +020053 "CHIP_CRYPTO_PSA=${chip_crypto_psa}",
Tennessee Carmel-Veilleux768b5752022-07-18 22:13:02 -040054 "CHIP_CRYPTO_OPENSSL=${chip_crypto_openssl}",
55 "CHIP_CRYPTO_BORINGSSL=${chip_crypto_boringssl}",
Steven Cooreman3dc97f02022-08-04 20:34:20 +020056 "CHIP_CRYPTO_PLATFORM=${chip_crypto_platform}",
Tennessee Carmel-Veilleux768b5752022-07-18 22:13:02 -040057 ]
Michael Spangefa630b2020-07-08 22:23:08 -040058}
59
Steven Cooreman3dc97f02022-08-04 20:34:20 +020060source_set("public_headers") {
61 sources = [
62 "CHIPCryptoPAL.h",
63 "OperationalKeystore.h",
Damian Królikb07872c2023-02-17 13:56:47 +010064 "SessionKeystore.h",
Steven Cooreman3dc97f02022-08-04 20:34:20 +020065 ]
66
67 public_deps = [
68 ":crypto_buildconfig",
69 "${chip_root}/src/lib/asn1",
70 "${chip_root}/src/lib/core",
Andrei Litvinb389c602024-02-01 00:25:59 -050071 "${chip_root}/src/lib/core:types",
Steven Cooreman3dc97f02022-08-04 20:34:20 +020072 "${chip_root}/src/lib/support",
73 "${nlassert_root}:nlassert",
74 ]
75}
76
Michael Spangefa630b2020-07-08 22:23:08 -040077if (chip_crypto == "openssl") {
Łukasz Duda4e1faf22021-01-27 17:47:25 +010078 import("${build_root}/config/linux/pkg_config.gni")
Michael Spangefa630b2020-07-08 22:23:08 -040079
80 pkg_config("openssl_config") {
81 packages = [ "openssl" ]
82 }
Steven Cooreman3dc97f02022-08-04 20:34:20 +020083
84 source_set("cryptopal_openssl") {
85 sources = [ "CHIPCryptoPALOpenSSL.cpp" ]
86 public_configs = [ ":openssl_config" ]
87 public_deps = [ ":public_headers" ]
88 }
Chris Letnickfa549eb2022-07-18 09:14:02 -040089} else if (chip_crypto == "boringssl") {
Vivien Nicolas7301ecf2022-10-06 16:17:52 +020090 import("${chip_root}/build_overrides/boringssl.gni")
Steven Cooreman3dc97f02022-08-04 20:34:20 +020091
92 source_set("cryptopal_boringssl") {
93 # BoringSSL is close enough to OpenSSL that it uses same PAL, with minor #ifdef differences
94 sources = [ "CHIPCryptoPALOpenSSL.cpp" ]
95 public_deps = [
96 ":public_headers",
97 "${boringssl_root}:boringssl",
98 ]
99 }
Chris Letnickfa549eb2022-07-18 09:14:02 -0400100} else if (chip_crypto == "mbedtls") {
Michael Spangefa630b2020-07-08 22:23:08 -0400101 import("//build_overrides/mbedtls.gni")
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200102
103 source_set("cryptopal_mbedtls") {
Damian Królike3b0cc02023-10-03 00:35:38 +0200104 sources = [
105 "CHIPCryptoPALmbedTLS.cpp",
106 "CHIPCryptoPALmbedTLS.h",
107 "CHIPCryptoPALmbedTLSCert.cpp",
108 ]
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200109 public_deps = [ ":public_headers" ]
110
alexhqwangd54101b2022-11-29 05:11:40 -0800111 if (!chip_external_mbedtls) {
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200112 public_deps += [ "${mbedtls_root}:mbedtls" ]
113 }
114 }
Damian Królikd37c8012022-10-20 04:41:14 +0200115} else if (chip_crypto == "psa") {
116 import("//build_overrides/mbedtls.gni")
117
118 source_set("cryptopal_psa") {
Damian Królik12b17442022-12-13 23:18:14 +0100119 sources = [
120 "CHIPCryptoPALPSA.cpp",
121 "CHIPCryptoPALPSA.h",
Damian Królike3b0cc02023-10-03 00:35:38 +0200122 "CHIPCryptoPALmbedTLS.h",
123 "CHIPCryptoPALmbedTLSCert.cpp",
Damian Królik12b17442022-12-13 23:18:14 +0100124 ]
Damian Królikd37c8012022-10-20 04:41:14 +0200125 public_deps = [ ":public_headers" ]
126
Damian Królikb07872c2023-02-17 13:56:47 +0100127 if (!chip_external_mbedtls) {
Damian Królikd37c8012022-10-20 04:41:14 +0200128 public_deps += [ "${mbedtls_root}:mbedtls" ]
129 }
130 }
Michael Spangefa630b2020-07-08 22:23:08 -0400131}
132
133static_library("crypto") {
134 output_name = "libChipCrypto"
135
Michael Spang84c6df42020-08-13 10:01:29 -0400136 sources = [
137 "CHIPCryptoPAL.cpp",
Damian Królikb07872c2023-02-17 13:56:47 +0100138 "DefaultSessionKeystore.h",
Tennessee Carmel-Veilleux4e3930a2022-06-10 09:08:31 -0400139 "PersistentStorageOperationalKeystore.cpp",
140 "PersistentStorageOperationalKeystore.h",
Tennessee Carmel-Veilleux267951c2021-10-13 22:05:45 -0400141 "RandUtils.cpp",
142 "RandUtils.h",
Michael Spang84c6df42020-08-13 10:01:29 -0400143 ]
Michael Spangefa630b2020-07-08 22:23:08 -0400144
Damian Królikb07872c2023-02-17 13:56:47 +0100145 if (chip_crypto == "psa") {
146 sources += [
Damian Królike3b0cc02023-10-03 00:35:38 +0200147 "PSAOperationalKeystore.cpp",
148 "PSAOperationalKeystore.h",
Damian Królikb07872c2023-02-17 13:56:47 +0100149 "PSASessionKeystore.cpp",
150 "PSASessionKeystore.h",
151 ]
152 } else {
153 sources += [
154 "RawKeySessionKeystore.cpp",
155 "RawKeySessionKeystore.h",
156 ]
157 }
158
Tennessee Carmel-Veilleux768b5752022-07-18 22:13:02 -0400159 public_configs = []
160
Boris Zbarsky902a8642020-09-25 17:41:22 -0400161 cflags = [ "-Wconversion" ]
162
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200163 public_deps = [ ":public_headers" ]
Michael Spangefa630b2020-07-08 22:23:08 -0400164
Michael Spangefa630b2020-07-08 22:23:08 -0400165 if (chip_crypto == "mbedtls") {
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200166 public_deps += [ ":cryptopal_mbedtls" ]
Damian Królikd37c8012022-10-20 04:41:14 +0200167 } else if (chip_crypto == "psa") {
168 public_deps += [ ":cryptopal_psa" ]
Michael Spangefa630b2020-07-08 22:23:08 -0400169 } else if (chip_crypto == "openssl") {
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200170 public_deps += [ ":cryptopal_openssl" ]
Chris Letnickfa549eb2022-07-18 09:14:02 -0400171 } else if (chip_crypto == "boringssl") {
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200172 public_deps += [ ":cryptopal_boringssl" ]
Steven Cooreman3dc97f02022-08-04 20:34:20 +0200173 } else if (chip_crypto == "platform") {
174 # Platform implementation is responsible for bringing their
175 # own implementation and dependencies
Michael Spangefa630b2020-07-08 22:23:08 -0400176 } else {
177 assert(false, "Invalid CHIP crypto")
178 }
Michael Spangefa630b2020-07-08 22:23:08 -0400179}