blob: 88316effe657e4a439a8fd738f9fca86857cb494 [file] [log] [blame]
David Brown63902772017-07-12 09:47:49 -06001// Build mcuboot as a library, based on the requested features.
2
Fabio Utzig455cad52018-10-15 14:36:33 -07003extern crate cc;
David Brown63902772017-07-12 09:47:49 -06004
David Brown5f4e1482021-09-16 16:44:09 -06005use std::collections::BTreeSet;
David Brown63902772017-07-12 09:47:49 -06006use std::env;
7use std::fs;
8use std::io;
David Brown5f4e1482021-09-16 16:44:09 -06009use std::path::{Path, PathBuf};
David Brown63902772017-07-12 09:47:49 -060010
11fn main() {
12 // Feature flags.
Matthew Dalzell34d5a692023-06-28 09:41:39 +010013 let psa_crypto_api = env::var("CARGO_FEATURE_PSA_CRYPTO_API").is_ok();
David Brown63902772017-07-12 09:47:49 -060014 let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
Fabio Utzig39297432019-05-08 18:51:10 -030015 let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok();
David Brown63902772017-07-12 09:47:49 -060016 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
David Brown641af452021-02-19 12:16:48 -070017 let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok();
Fabio Utzig97710282019-05-24 17:44:49 -030018 let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
David Brown63902772017-07-12 09:47:49 -060019 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzig031eb7d2019-11-28 10:13:14 -030020 let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010021 let validate_primary_slot =
22 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030023 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010024 let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030025 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010026 let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok();
Fabio Utzig90f449e2019-10-24 07:43:53 -030027 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig6c553d62021-05-06 19:56:18 -030028 let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010029 let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -030030 let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010031 let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020032 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070033 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown2ee5f7f2020-01-13 14:04:01 -070034 let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
David Brown7e377ab2021-05-26 16:33:39 -060035 let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok();
David Brown11ffa0a2021-05-26 17:10:47 -060036 let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok();
David Brown1bc106e2021-12-16 13:23:52 -070037 let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok();
Roland Mikheld6703522023-04-27 14:24:30 +020038 let hw_rollback_protection = env::var("CARGO_FEATURE_HW_ROLLBACK_PROTECTION").is_ok();
David Brown63902772017-07-12 09:47:49 -060039
David Brown5f4e1482021-09-16 16:44:09 -060040 let mut conf = CachedBuild::new();
41 conf.conf.define("__BOOTSIM__", None);
42 conf.conf.define("MCUBOOT_HAVE_LOGGING", None);
43 conf.conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
44 conf.conf.define("MCUBOOT_HAVE_ASSERT_H", None);
45 conf.conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030046
David Brown1bc106e2021-12-16 13:23:52 -070047 if max_align_32 {
48 conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("32"));
49 } else {
50 conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("8"));
51 }
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030052
David Brown5f4e1482021-09-16 16:44:09 -060053 conf.conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020054
David Brown2ee5f7f2020-01-13 14:04:01 -070055 if downgrade_prevention && !overwrite_only {
56 panic!("Downgrade prevention requires overwrite only");
57 }
58
Fabio Utzig9b97b132018-12-18 17:21:51 -020059 if bootstrap {
David Brown5f4e1482021-09-16 16:44:09 -060060 conf.conf.define("MCUBOOT_BOOTSTRAP", None);
61 conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
Fabio Utzig9b97b132018-12-18 17:21:51 -020062 }
63
David Vincze2d736ad2019-02-18 11:50:22 +010064 if validate_primary_slot {
David Brown5f4e1482021-09-16 16:44:09 -060065 conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020066 }
David Brown63902772017-07-12 09:47:49 -060067
David Brown2ee5f7f2020-01-13 14:04:01 -070068 if downgrade_prevention {
David Brown5f4e1482021-09-16 16:44:09 -060069 conf.conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
David Brown2ee5f7f2020-01-13 14:04:01 -070070 }
71
David Brown7e377ab2021-05-26 16:33:39 -060072 if ram_load {
David Brown5f4e1482021-09-16 16:44:09 -060073 conf.conf.define("MCUBOOT_RAM_LOAD", None);
David Brown7e377ab2021-05-26 16:33:39 -060074 }
75
David Brown11ffa0a2021-05-26 17:10:47 -060076 if direct_xip {
David Brown5f4e1482021-09-16 16:44:09 -060077 conf.conf.define("MCUBOOT_DIRECT_XIP", None);
David Brown11ffa0a2021-05-26 17:10:47 -060078 }
79
Roland Mikheld6703522023-04-27 14:24:30 +020080 if hw_rollback_protection {
81 conf.conf.define("MCUBOOT_HW_ROLLBACK_PROT", None);
82 conf.file("csupport/security_cnt.c");
83 }
84
Fabio Utzig39297432019-05-08 18:51:10 -030085 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030086 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030087 .fold(0, |sum, &v| sum + v as i32) > 1 {
88 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060089 }
David Brown63902772017-07-12 09:47:49 -060090
Matthew Dalzell34d5a692023-06-28 09:41:39 +010091 if psa_crypto_api {
92 if sig_ecdsa || enc_ec256 || enc_x25519 ||
93 enc_aes256_ec256 || sig_ecdsa_mbedtls || enc_aes256_x25519 ||
94 enc_kw || enc_aes256_kw {
95 conf.file("csupport/psa_crypto_init_stub.c");
96 } else {
97 conf.conf.define("MCUBOOT_USE_PSA_CRYPTO", None);
98 conf.file("../../ext/mbedtls/library/aes.c");
99 conf.file("../../ext/mbedtls/library/aesni.c");
100 conf.file("../../ext/mbedtls/library/aria.c");
101 conf.file("../../ext/mbedtls/library/asn1write.c");
102 conf.file("../../ext/mbedtls/library/base64.c");
103 conf.file("../../ext/mbedtls/library/camellia.c");
104 conf.file("../../ext/mbedtls/library/ccm.c");
105 conf.file("../../ext/mbedtls/library/chacha20.c");
106 conf.file("../../ext/mbedtls/library/chachapoly.c");
107 conf.file("../../ext/mbedtls/library/cipher.c");
108 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
109 conf.file("../../ext/mbedtls/library/ctr_drbg.c");
110 conf.file("../../ext/mbedtls/library/des.c");
111 conf.file("../../ext/mbedtls/library/ecdsa.c");
112 conf.file("../../ext/mbedtls/library/ecp.c");
113 conf.file("../../ext/mbedtls/library/ecp_curves.c");
114 conf.file("../../ext/mbedtls/library/entropy.c");
115 conf.file("../../ext/mbedtls/library/entropy_poll.c");
116 conf.file("../../ext/mbedtls/library/gcm.c");
117 conf.file("../../ext/mbedtls/library/md5.c");
118 conf.file("../../ext/mbedtls/library/nist_kw.c");
119 conf.file("../../ext/mbedtls/library/oid.c");
120 conf.file("../../ext/mbedtls/library/pem.c");
121 conf.file("../../ext/mbedtls/library/pk.c");
122 conf.file("../../ext/mbedtls/library/pkcs5.c");
123 conf.file("../../ext/mbedtls/library/pkcs12.c");
124 conf.file("../../ext/mbedtls/library/pkparse.c");
125 conf.file("../../ext/mbedtls/library/pk_wrap.c");
126 conf.file("../../ext/mbedtls/library/pkwrite.c");
127 conf.file("../../ext/mbedtls/library/poly1305.c");
128 conf.file("../../ext/mbedtls/library/psa_crypto.c");
129 conf.file("../../ext/mbedtls/library/psa_crypto_cipher.c");
130 conf.file("../../ext/mbedtls/library/psa_crypto_client.c");
131 conf.file("../../ext/mbedtls/library/psa_crypto_driver_wrappers.c");
132 conf.file("../../ext/mbedtls/library/psa_crypto_ecp.c");
133 conf.file("../../ext/mbedtls/library/psa_crypto_hash.c");
134 conf.file("../../ext/mbedtls/library/psa_crypto_mac.c");
135 conf.file("../../ext/mbedtls/library/psa_crypto_rsa.c");
136 conf.file("../../ext/mbedtls/library/psa_crypto_slot_management.c");
137 conf.file("../../ext/mbedtls/library/psa_crypto_storage.c");
138 conf.file("../../ext/mbedtls/library/psa_its_file.c");
139 conf.file("../../ext/mbedtls/library/ripemd160.c");
140 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
141 conf.file("../../ext/mbedtls/library/sha1.c");
142 conf.file("../../ext/mbedtls/library/sha512.c");
143 conf.file("../../ext/mbedtls/tests/src/random.c");
144 conf.conf.include("../../ext/mbedtls/library");
145 }
146
147 conf.conf.include("../../ext/mbedtls/tests/include/");
148 conf.file("../../ext/mbedtls/tests/src/fake_external_rng_for_test.c");
149 }
150
Fabio Utzig39297432019-05-08 18:51:10 -0300151 if sig_rsa || sig_rsa3072 {
David Brown5f4e1482021-09-16 16:44:09 -0600152 conf.conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -0300153 // The Kconfig style defines must be added here as well because
154 // they are used internally by "config-rsa.h"
155 if sig_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600156 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
157 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -0300158 } else {
David Brown5f4e1482021-09-16 16:44:09 -0600159 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
160 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -0300161 }
David Brown5f4e1482021-09-16 16:44:09 -0600162 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown63902772017-07-12 09:47:49 -0600163
David Brown5f4e1482021-09-16 16:44:09 -0600164 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800165 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -0300166 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -0600167
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800168 conf.file("../../ext/mbedtls/library/rsa.c");
169 conf.file("../../ext/mbedtls/library/bignum.c");
170 conf.file("../../ext/mbedtls/library/platform.c");
171 conf.file("../../ext/mbedtls/library/platform_util.c");
172 conf.file("../../ext/mbedtls/library/asn1parse.c");
Antonio de Angelis02bf0722022-11-22 15:35:43 +0000173 conf.file("../../ext/mbedtls/library/md.c");
174
David Brown704ac6f2017-07-12 10:14:47 -0600175 } else if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600176 conf.conf.define("MCUBOOT_SIGN_EC256", None);
177 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -0200178
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200179 if !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600180 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200181 }
David Brown5f4e1482021-09-16 16:44:09 -0600182 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200183
Fabio Utzig806af0e2018-04-26 10:53:54 -0300184 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200185
186 conf.file("../../ext/tinycrypt/lib/source/utils.c");
187 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
188 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
189 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
190 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
David Brown5f4e1482021-09-16 16:44:09 -0600191 conf.file("../../ext/mbedtls/library/platform_util.c");
192 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700193 } else if sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600194 conf.conf.define("MCUBOOT_SIGN_EC256", None);
195 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown641af452021-02-19 12:16:48 -0700196
David Brown5f4e1482021-09-16 16:44:09 -0600197 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800198 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700199 conf.file("csupport/keys.c");
200
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800201 conf.file("../../ext/mbedtls/library/asn1parse.c");
202 conf.file("../../ext/mbedtls/library/bignum.c");
203 conf.file("../../ext/mbedtls/library/ecdsa.c");
204 conf.file("../../ext/mbedtls/library/ecp.c");
205 conf.file("../../ext/mbedtls/library/ecp_curves.c");
206 conf.file("../../ext/mbedtls/library/platform.c");
207 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300208 } else if sig_ed25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600209 conf.conf.define("MCUBOOT_SIGN_ED25519", None);
210 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300211
David Brown5f4e1482021-09-16 16:44:09 -0600212 conf.conf.include("../../ext/tinycrypt/lib/include");
213 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
214 conf.conf.include("../../ext/mbedtls/include");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300215 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
216 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
217 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300218 conf.file("csupport/keys.c");
219 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600220 conf.file("../../ext/mbedtls/library/platform_util.c");
221 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300222 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300223 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400224 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300225 // When using ECIES-P256 rely on Tinycrypt.
David Brown5f4e1482021-09-16 16:44:09 -0600226 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
227 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800228 conf.file("../../ext/mbedtls/library/sha256.c");
229 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600230 }
231
232 if overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600233 conf.conf.define("MCUBOOT_OVERWRITE_ONLY", None);
David Brown63902772017-07-12 09:47:49 -0600234 }
235
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300236 if swap_move {
David Brown5f4e1482021-09-16 16:44:09 -0600237 conf.conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Andrzej Puzdrowski137d7972021-05-13 13:39:30 +0200238 } else if !overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600239 conf.conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
240 conf.conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300241 }
242
Salome Thirot6fdbf552021-05-14 16:46:14 +0100243 if enc_rsa || enc_aes256_rsa {
244 if enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600245 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100246 }
David Brown5f4e1482021-09-16 16:44:09 -0600247 conf.conf.define("MCUBOOT_ENCRYPT_RSA", None);
248 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
249 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300250
251 conf.file("../../boot/bootutil/src/encrypted.c");
252 conf.file("csupport/keys.c");
253
David Brown5f4e1482021-09-16 16:44:09 -0600254 conf.conf.include("../../ext/mbedtls/include");
255 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800256 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300257
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800258 conf.file("../../ext/mbedtls/library/platform.c");
259 conf.file("../../ext/mbedtls/library/platform_util.c");
260 conf.file("../../ext/mbedtls/library/rsa.c");
261 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
262 conf.file("../../ext/mbedtls/library/md.c");
263 conf.file("../../ext/mbedtls/library/aes.c");
264 conf.file("../../ext/mbedtls/library/bignum.c");
265 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300266 }
267
Salome Thirot6fdbf552021-05-14 16:46:14 +0100268 if enc_kw || enc_aes256_kw {
269 if enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600270 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100271 }
David Brown5f4e1482021-09-16 16:44:09 -0600272 conf.conf.define("MCUBOOT_ENCRYPT_KW", None);
273 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300274
275 conf.file("../../boot/bootutil/src/encrypted.c");
276 conf.file("csupport/keys.c");
277
Fabio Utzig39297432019-05-08 18:51:10 -0300278 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800279 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200280 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300281
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200282 /* Simulator uses Mbed-TLS to wrap keys */
David Brown5f4e1482021-09-16 16:44:09 -0600283 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800284 conf.file("../../ext/mbedtls/library/platform.c");
David Brown5f4e1482021-09-16 16:44:09 -0600285 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800286 conf.file("../../ext/mbedtls/library/platform_util.c");
287 conf.file("../../ext/mbedtls/library/nist_kw.c");
288 conf.file("../../ext/mbedtls/library/cipher.c");
289 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
290 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200291
292 if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600293 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200294
David Brown5f4e1482021-09-16 16:44:09 -0600295 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200296
297 conf.file("../../ext/tinycrypt/lib/source/utils.c");
298 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
299 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
300 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900301 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200302 }
Fabio Utzig97710282019-05-24 17:44:49 -0300303
304 if sig_ed25519 {
305 panic!("ed25519 does not support image encryption with KW yet");
306 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300307 }
308
Fabio Utzig90f449e2019-10-24 07:43:53 -0300309 if enc_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600310 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
311 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
312 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
313 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300314
315 conf.file("../../boot/bootutil/src/encrypted.c");
316 conf.file("csupport/keys.c");
317
David Brown5f4e1482021-09-16 16:44:09 -0600318 conf.conf.include("../../ext/mbedtls/include");
319 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300320
321 /* FIXME: fail with other signature schemes ? */
322
323 conf.file("../../ext/tinycrypt/lib/source/utils.c");
324 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
325 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
326 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
327 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
328
David Brown5f4e1482021-09-16 16:44:09 -0600329 conf.file("../../ext/mbedtls/library/platform_util.c");
330 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300331
332 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
333 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
334 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
335 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
336 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100337 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
338 if enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600339 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100340 }
David Brown5f4e1482021-09-16 16:44:09 -0600341 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
342 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
343 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
344 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig6c553d62021-05-06 19:56:18 -0300345
David Brown5f4e1482021-09-16 16:44:09 -0600346 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300347
348 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800349 conf.file("../../ext/mbedtls/library/sha256.c");
350 conf.file("../../ext/mbedtls/library/asn1parse.c");
351 conf.file("../../ext/mbedtls/library/bignum.c");
352 conf.file("../../ext/mbedtls/library/ecdh.c");
353 conf.file("../../ext/mbedtls/library/md.c");
354 conf.file("../../ext/mbedtls/library/aes.c");
355 conf.file("../../ext/mbedtls/library/ecp.c");
356 conf.file("../../ext/mbedtls/library/ecp_curves.c");
357 conf.file("../../ext/mbedtls/library/platform.c");
358 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300359 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300360 }
361
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300362 if enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600363 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
364 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
365 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
366 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300367
368 conf.file("../../boot/bootutil/src/encrypted.c");
369 conf.file("csupport/keys.c");
370
David Brown5f4e1482021-09-16 16:44:09 -0600371 conf.conf.include("../../ext/mbedtls/include");
372 conf.conf.include("../../ext/tinycrypt/lib/include");
373 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300374
375 conf.file("../../ext/fiat/src/curve25519.c");
376
377 conf.file("../../ext/tinycrypt/lib/source/utils.c");
378 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
379
David Brown5f4e1482021-09-16 16:44:09 -0600380 conf.file("../../ext/mbedtls/library/platform_util.c");
381 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300382
383 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
384 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
385 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
386 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
387 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300388
Salome Thirot6fdbf552021-05-14 16:46:14 +0100389 else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600390 conf.conf.define("MCUBOOT_AES_256", None);
391 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
392 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
393 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
394 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100395
396 conf.file("../../boot/bootutil/src/encrypted.c");
397 conf.file("csupport/keys.c");
398
David Brown5f4e1482021-09-16 16:44:09 -0600399 conf.conf.include("../../ext/mbedtls/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100400 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600401 conf.file("../../ext/mbedtls/library/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800402 conf.file("../../ext/mbedtls/library/platform.c");
403 conf.file("../../ext/mbedtls/library/platform_util.c");
404 conf.file("../../ext/mbedtls/library/aes.c");
405 conf.file("../../ext/mbedtls/library/sha256.c");
406 conf.file("../../ext/mbedtls/library/md.c");
407 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100408 }
409
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200410 if sig_rsa && enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600411 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100412 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600413 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100414 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600415 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300416 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600417 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300418 } else if sig_ed25519 || enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600419 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100420 } else if enc_kw || enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600421 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100422 } else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600423 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200424 }
425
David Brown704ac6f2017-07-12 10:14:47 -0600426 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300427 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200428 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown641af452021-02-19 12:16:48 -0700429 } else if sig_ecdsa || sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600430 conf.conf.include("../../ext/mbedtls/include");
Antonio de Angelis10529d32023-04-21 21:43:14 +0100431 conf.file("../../boot/bootutil/src/image_ecdsa.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300432 } else if sig_ed25519 {
433 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200434 }
David Brown63902772017-07-12 09:47:49 -0600435 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300436 conf.file("../../boot/bootutil/src/swap_misc.c");
437 conf.file("../../boot/bootutil/src/swap_scratch.c");
438 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600439 conf.file("../../boot/bootutil/src/caps.c");
440 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100441 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300442 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100443 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600444 conf.file("csupport/run.c");
David Brown5f4e1482021-09-16 16:44:09 -0600445 conf.conf.include("../../boot/bootutil/include");
446 conf.conf.include("csupport");
David Brown5f4e1482021-09-16 16:44:09 -0600447 conf.conf.debug(true);
448 conf.conf.flag("-Wall");
449 conf.conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600450
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200451 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
452 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
453 // to build correctly so leaving it here to updated in the future...
David Brown5f4e1482021-09-16 16:44:09 -0600454 conf.conf.flag("-std=c99");
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200455
David Brown5f4e1482021-09-16 16:44:09 -0600456 conf.conf.compile("libbootutil.a");
David Brown63902772017-07-12 09:47:49 -0600457
458 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200459 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600460 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600461 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800462 walk_dir("../../ext/mbedtls/include").unwrap();
463 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600464}
465
466// Output the names of all files within a directory so that Cargo knows when to rebuild.
467fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
468 for ent in fs::read_dir(path.as_ref())? {
469 let ent = ent?;
470 let p = ent.path();
471 if p.is_dir() {
472 walk_dir(p)?;
473 } else {
474 // Note that non-utf8 names will fail.
475 let name = p.to_str().unwrap();
476 if name.ends_with(".c") || name.ends_with(".h") {
477 println!("cargo:rerun-if-changed={}", name);
478 }
479 }
480 }
481
482 Ok(())
483}
David Brown5f4e1482021-09-16 16:44:09 -0600484
485/// Wrap the cc::Build type so that we can make sure that files are only added a single time.
486/// Other methods can be passed through as needed.
487struct CachedBuild {
488 conf: cc::Build,
489 seen: BTreeSet<PathBuf>,
490}
491
492impl CachedBuild {
493 fn new() -> CachedBuild {
494 CachedBuild {
495 conf: cc::Build::new(),
496 seen: BTreeSet::new(),
497 }
498 }
499
500 /// Works like `file` in the Build, but doesn't add a file if the same path has already been
501 /// given.
502 fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut CachedBuild {
503 let p = p.as_ref();
504 if !self.seen.contains(p) {
505 self.conf.file(p);
506 self.seen.insert(p.to_owned());
507 }
508 self
509 }
510}