blob: ea17d80140c596cea24ef8bcec3057eb8c654ff7 [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();
Roland Mikhel5899fac2023-03-14 13:59:55 +010018 let sig_ecdsa_psa = env::var("CARGO_FEATURE_SIG_ECDSA_PSA").is_ok();
19 let sig_p384 = env::var("CARGO_FEATURE_SIG_P384").is_ok();
Fabio Utzig97710282019-05-24 17:44:49 -030020 let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
David Brown63902772017-07-12 09:47:49 -060021 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzig031eb7d2019-11-28 10:13:14 -030022 let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010023 let validate_primary_slot =
24 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030025 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010026 let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030027 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010028 let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok();
Fabio Utzig90f449e2019-10-24 07:43:53 -030029 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig6c553d62021-05-06 19:56:18 -030030 let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010031 let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -030032 let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010033 let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020034 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070035 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown2ee5f7f2020-01-13 14:04:01 -070036 let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
David Brown7e377ab2021-05-26 16:33:39 -060037 let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok();
David Brown11ffa0a2021-05-26 17:10:47 -060038 let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok();
David Brown1bc106e2021-12-16 13:23:52 -070039 let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok();
Roland Mikheld6703522023-04-27 14:24:30 +020040 let hw_rollback_protection = env::var("CARGO_FEATURE_HW_ROLLBACK_PROTECTION").is_ok();
David Brown63902772017-07-12 09:47:49 -060041
David Brown5f4e1482021-09-16 16:44:09 -060042 let mut conf = CachedBuild::new();
43 conf.conf.define("__BOOTSIM__", None);
44 conf.conf.define("MCUBOOT_HAVE_LOGGING", None);
45 conf.conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
46 conf.conf.define("MCUBOOT_HAVE_ASSERT_H", None);
47 conf.conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030048
David Brown1bc106e2021-12-16 13:23:52 -070049 if max_align_32 {
50 conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("32"));
51 } else {
52 conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("8"));
53 }
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030054
David Brown5f4e1482021-09-16 16:44:09 -060055 conf.conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020056
David Brown2ee5f7f2020-01-13 14:04:01 -070057 if downgrade_prevention && !overwrite_only {
58 panic!("Downgrade prevention requires overwrite only");
59 }
60
Fabio Utzig9b97b132018-12-18 17:21:51 -020061 if bootstrap {
David Brown5f4e1482021-09-16 16:44:09 -060062 conf.conf.define("MCUBOOT_BOOTSTRAP", None);
63 conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
Fabio Utzig9b97b132018-12-18 17:21:51 -020064 }
65
David Vincze2d736ad2019-02-18 11:50:22 +010066 if validate_primary_slot {
David Brown5f4e1482021-09-16 16:44:09 -060067 conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020068 }
David Brown63902772017-07-12 09:47:49 -060069
David Brown2ee5f7f2020-01-13 14:04:01 -070070 if downgrade_prevention {
David Brown5f4e1482021-09-16 16:44:09 -060071 conf.conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
David Brown2ee5f7f2020-01-13 14:04:01 -070072 }
73
David Brown7e377ab2021-05-26 16:33:39 -060074 if ram_load {
David Brown5f4e1482021-09-16 16:44:09 -060075 conf.conf.define("MCUBOOT_RAM_LOAD", None);
David Brown7e377ab2021-05-26 16:33:39 -060076 }
77
David Brown11ffa0a2021-05-26 17:10:47 -060078 if direct_xip {
David Brown5f4e1482021-09-16 16:44:09 -060079 conf.conf.define("MCUBOOT_DIRECT_XIP", None);
David Brown11ffa0a2021-05-26 17:10:47 -060080 }
81
Roland Mikheld6703522023-04-27 14:24:30 +020082 if hw_rollback_protection {
83 conf.conf.define("MCUBOOT_HW_ROLLBACK_PROT", None);
84 conf.file("csupport/security_cnt.c");
85 }
86
Fabio Utzig39297432019-05-08 18:51:10 -030087 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030088 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030089 .fold(0, |sum, &v| sum + v as i32) > 1 {
90 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060091 }
David Brown63902772017-07-12 09:47:49 -060092
Matthew Dalzell34d5a692023-06-28 09:41:39 +010093 if psa_crypto_api {
94 if sig_ecdsa || enc_ec256 || enc_x25519 ||
95 enc_aes256_ec256 || sig_ecdsa_mbedtls || enc_aes256_x25519 ||
96 enc_kw || enc_aes256_kw {
97 conf.file("csupport/psa_crypto_init_stub.c");
98 } else {
99 conf.conf.define("MCUBOOT_USE_PSA_CRYPTO", None);
100 conf.file("../../ext/mbedtls/library/aes.c");
101 conf.file("../../ext/mbedtls/library/aesni.c");
102 conf.file("../../ext/mbedtls/library/aria.c");
103 conf.file("../../ext/mbedtls/library/asn1write.c");
104 conf.file("../../ext/mbedtls/library/base64.c");
105 conf.file("../../ext/mbedtls/library/camellia.c");
106 conf.file("../../ext/mbedtls/library/ccm.c");
107 conf.file("../../ext/mbedtls/library/chacha20.c");
108 conf.file("../../ext/mbedtls/library/chachapoly.c");
109 conf.file("../../ext/mbedtls/library/cipher.c");
110 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
111 conf.file("../../ext/mbedtls/library/ctr_drbg.c");
112 conf.file("../../ext/mbedtls/library/des.c");
113 conf.file("../../ext/mbedtls/library/ecdsa.c");
114 conf.file("../../ext/mbedtls/library/ecp.c");
115 conf.file("../../ext/mbedtls/library/ecp_curves.c");
116 conf.file("../../ext/mbedtls/library/entropy.c");
117 conf.file("../../ext/mbedtls/library/entropy_poll.c");
118 conf.file("../../ext/mbedtls/library/gcm.c");
119 conf.file("../../ext/mbedtls/library/md5.c");
120 conf.file("../../ext/mbedtls/library/nist_kw.c");
121 conf.file("../../ext/mbedtls/library/oid.c");
122 conf.file("../../ext/mbedtls/library/pem.c");
123 conf.file("../../ext/mbedtls/library/pk.c");
124 conf.file("../../ext/mbedtls/library/pkcs5.c");
125 conf.file("../../ext/mbedtls/library/pkcs12.c");
126 conf.file("../../ext/mbedtls/library/pkparse.c");
127 conf.file("../../ext/mbedtls/library/pk_wrap.c");
128 conf.file("../../ext/mbedtls/library/pkwrite.c");
129 conf.file("../../ext/mbedtls/library/poly1305.c");
130 conf.file("../../ext/mbedtls/library/psa_crypto.c");
131 conf.file("../../ext/mbedtls/library/psa_crypto_cipher.c");
132 conf.file("../../ext/mbedtls/library/psa_crypto_client.c");
133 conf.file("../../ext/mbedtls/library/psa_crypto_driver_wrappers.c");
134 conf.file("../../ext/mbedtls/library/psa_crypto_ecp.c");
135 conf.file("../../ext/mbedtls/library/psa_crypto_hash.c");
136 conf.file("../../ext/mbedtls/library/psa_crypto_mac.c");
137 conf.file("../../ext/mbedtls/library/psa_crypto_rsa.c");
138 conf.file("../../ext/mbedtls/library/psa_crypto_slot_management.c");
139 conf.file("../../ext/mbedtls/library/psa_crypto_storage.c");
140 conf.file("../../ext/mbedtls/library/psa_its_file.c");
141 conf.file("../../ext/mbedtls/library/ripemd160.c");
142 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
143 conf.file("../../ext/mbedtls/library/sha1.c");
144 conf.file("../../ext/mbedtls/library/sha512.c");
145 conf.file("../../ext/mbedtls/tests/src/random.c");
146 conf.conf.include("../../ext/mbedtls/library");
147 }
148
149 conf.conf.include("../../ext/mbedtls/tests/include/");
150 conf.file("../../ext/mbedtls/tests/src/fake_external_rng_for_test.c");
151 }
152
Fabio Utzig39297432019-05-08 18:51:10 -0300153 if sig_rsa || sig_rsa3072 {
David Brown5f4e1482021-09-16 16:44:09 -0600154 conf.conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -0300155 // The Kconfig style defines must be added here as well because
156 // they are used internally by "config-rsa.h"
157 if sig_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600158 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
159 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -0300160 } else {
David Brown5f4e1482021-09-16 16:44:09 -0600161 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
162 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -0300163 }
David Brown5f4e1482021-09-16 16:44:09 -0600164 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown63902772017-07-12 09:47:49 -0600165
David Brown5f4e1482021-09-16 16:44:09 -0600166 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800167 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -0300168 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -0600169
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800170 conf.file("../../ext/mbedtls/library/rsa.c");
171 conf.file("../../ext/mbedtls/library/bignum.c");
172 conf.file("../../ext/mbedtls/library/platform.c");
173 conf.file("../../ext/mbedtls/library/platform_util.c");
174 conf.file("../../ext/mbedtls/library/asn1parse.c");
Antonio de Angelis02bf0722022-11-22 15:35:43 +0000175 conf.file("../../ext/mbedtls/library/md.c");
176
David Brown704ac6f2017-07-12 10:14:47 -0600177 } else if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600178 conf.conf.define("MCUBOOT_SIGN_EC256", None);
179 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -0200180
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200181 if !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600182 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200183 }
David Brown5f4e1482021-09-16 16:44:09 -0600184 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200185
Fabio Utzig806af0e2018-04-26 10:53:54 -0300186 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200187
188 conf.file("../../ext/tinycrypt/lib/source/utils.c");
189 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
190 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
191 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
192 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
David Brown5f4e1482021-09-16 16:44:09 -0600193 conf.file("../../ext/mbedtls/library/platform_util.c");
194 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700195 } else if sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600196 conf.conf.define("MCUBOOT_SIGN_EC256", None);
197 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown641af452021-02-19 12:16:48 -0700198
David Brown5f4e1482021-09-16 16:44:09 -0600199 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800200 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700201 conf.file("csupport/keys.c");
202
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800203 conf.file("../../ext/mbedtls/library/asn1parse.c");
204 conf.file("../../ext/mbedtls/library/bignum.c");
205 conf.file("../../ext/mbedtls/library/ecdsa.c");
206 conf.file("../../ext/mbedtls/library/ecp.c");
207 conf.file("../../ext/mbedtls/library/ecp_curves.c");
208 conf.file("../../ext/mbedtls/library/platform.c");
209 conf.file("../../ext/mbedtls/library/platform_util.c");
Roland Mikhel5899fac2023-03-14 13:59:55 +0100210 } else if sig_ecdsa_psa {
211 conf.conf.include("../../ext/mbedtls/include");
212
213 if sig_p384 {
214 conf.conf.define("MCUBOOT_SIGN_EC384", None);
215 conf.file("../../ext/mbedtls/library/sha512.c");
216 } else {
217 conf.conf.define("MCUBOOT_SIGN_EC256", None);
218 conf.file("../../ext/mbedtls/library/sha256.c");
219 }
220
221 conf.file("csupport/keys.c");
222 conf.file("../../ext/mbedtls/library/asn1parse.c");
223 conf.file("../../ext/mbedtls/library/bignum.c");
224 conf.file("../../ext/mbedtls/library/ecp.c");
225 conf.file("../../ext/mbedtls/library/ecp_curves.c");
226 conf.file("../../ext/mbedtls/library/platform.c");
227 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300228 } else if sig_ed25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600229 conf.conf.define("MCUBOOT_SIGN_ED25519", None);
230 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300231
David Brown5f4e1482021-09-16 16:44:09 -0600232 conf.conf.include("../../ext/tinycrypt/lib/include");
233 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
234 conf.conf.include("../../ext/mbedtls/include");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300235 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
236 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
237 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300238 conf.file("csupport/keys.c");
239 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600240 conf.file("../../ext/mbedtls/library/platform_util.c");
241 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300242 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300243 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400244 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300245 // When using ECIES-P256 rely on Tinycrypt.
David Brown5f4e1482021-09-16 16:44:09 -0600246 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
247 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800248 conf.file("../../ext/mbedtls/library/sha256.c");
249 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600250 }
251
252 if overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600253 conf.conf.define("MCUBOOT_OVERWRITE_ONLY", None);
David Brown63902772017-07-12 09:47:49 -0600254 }
255
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300256 if swap_move {
David Brown5f4e1482021-09-16 16:44:09 -0600257 conf.conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Thomas Altenbach60806bc2024-05-02 14:56:21 +0200258 } else if !overwrite_only && !direct_xip && !ram_load {
David Brown5f4e1482021-09-16 16:44:09 -0600259 conf.conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
260 conf.conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300261 }
262
Salome Thirot6fdbf552021-05-14 16:46:14 +0100263 if enc_rsa || enc_aes256_rsa {
264 if enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600265 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100266 }
David Brown5f4e1482021-09-16 16:44:09 -0600267 conf.conf.define("MCUBOOT_ENCRYPT_RSA", None);
268 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
269 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300270
271 conf.file("../../boot/bootutil/src/encrypted.c");
272 conf.file("csupport/keys.c");
273
David Brown5f4e1482021-09-16 16:44:09 -0600274 conf.conf.include("../../ext/mbedtls/include");
275 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800276 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300277
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800278 conf.file("../../ext/mbedtls/library/platform.c");
279 conf.file("../../ext/mbedtls/library/platform_util.c");
280 conf.file("../../ext/mbedtls/library/rsa.c");
281 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
282 conf.file("../../ext/mbedtls/library/md.c");
283 conf.file("../../ext/mbedtls/library/aes.c");
284 conf.file("../../ext/mbedtls/library/bignum.c");
285 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300286 }
287
Salome Thirot6fdbf552021-05-14 16:46:14 +0100288 if enc_kw || enc_aes256_kw {
289 if enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600290 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100291 }
David Brown5f4e1482021-09-16 16:44:09 -0600292 conf.conf.define("MCUBOOT_ENCRYPT_KW", None);
293 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300294
295 conf.file("../../boot/bootutil/src/encrypted.c");
296 conf.file("csupport/keys.c");
297
Fabio Utzig39297432019-05-08 18:51:10 -0300298 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800299 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200300 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300301
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200302 /* Simulator uses Mbed-TLS to wrap keys */
David Brown5f4e1482021-09-16 16:44:09 -0600303 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800304 conf.file("../../ext/mbedtls/library/platform.c");
David Brown5f4e1482021-09-16 16:44:09 -0600305 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800306 conf.file("../../ext/mbedtls/library/platform_util.c");
307 conf.file("../../ext/mbedtls/library/nist_kw.c");
308 conf.file("../../ext/mbedtls/library/cipher.c");
309 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
310 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200311
312 if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600313 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200314
David Brown5f4e1482021-09-16 16:44:09 -0600315 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200316
317 conf.file("../../ext/tinycrypt/lib/source/utils.c");
318 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
319 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
320 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900321 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200322 }
Fabio Utzig97710282019-05-24 17:44:49 -0300323
324 if sig_ed25519 {
325 panic!("ed25519 does not support image encryption with KW yet");
326 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300327 }
328
Fabio Utzig90f449e2019-10-24 07:43:53 -0300329 if enc_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600330 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
331 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
332 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
333 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300334
335 conf.file("../../boot/bootutil/src/encrypted.c");
336 conf.file("csupport/keys.c");
337
David Brown5f4e1482021-09-16 16:44:09 -0600338 conf.conf.include("../../ext/mbedtls/include");
339 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300340
341 /* FIXME: fail with other signature schemes ? */
342
343 conf.file("../../ext/tinycrypt/lib/source/utils.c");
344 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
345 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
346 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
347 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
348
David Brown5f4e1482021-09-16 16:44:09 -0600349 conf.file("../../ext/mbedtls/library/platform_util.c");
350 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300351
352 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
353 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
354 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
355 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
356 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100357 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
358 if enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600359 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100360 }
David Brown5f4e1482021-09-16 16:44:09 -0600361 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
362 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
363 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
364 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig6c553d62021-05-06 19:56:18 -0300365
David Brown5f4e1482021-09-16 16:44:09 -0600366 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300367
368 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800369 conf.file("../../ext/mbedtls/library/sha256.c");
370 conf.file("../../ext/mbedtls/library/asn1parse.c");
371 conf.file("../../ext/mbedtls/library/bignum.c");
372 conf.file("../../ext/mbedtls/library/ecdh.c");
373 conf.file("../../ext/mbedtls/library/md.c");
374 conf.file("../../ext/mbedtls/library/aes.c");
375 conf.file("../../ext/mbedtls/library/ecp.c");
376 conf.file("../../ext/mbedtls/library/ecp_curves.c");
377 conf.file("../../ext/mbedtls/library/platform.c");
378 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300379 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300380 }
381
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300382 if enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600383 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
384 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
385 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
386 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300387
388 conf.file("../../boot/bootutil/src/encrypted.c");
389 conf.file("csupport/keys.c");
390
David Brown5f4e1482021-09-16 16:44:09 -0600391 conf.conf.include("../../ext/mbedtls/include");
392 conf.conf.include("../../ext/tinycrypt/lib/include");
393 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300394
395 conf.file("../../ext/fiat/src/curve25519.c");
396
397 conf.file("../../ext/tinycrypt/lib/source/utils.c");
398 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
399
David Brown5f4e1482021-09-16 16:44:09 -0600400 conf.file("../../ext/mbedtls/library/platform_util.c");
401 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300402
403 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
404 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
405 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
406 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
407 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300408
Salome Thirot6fdbf552021-05-14 16:46:14 +0100409 else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600410 conf.conf.define("MCUBOOT_AES_256", None);
411 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
412 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
413 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
414 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100415
416 conf.file("../../boot/bootutil/src/encrypted.c");
417 conf.file("csupport/keys.c");
418
David Brown5f4e1482021-09-16 16:44:09 -0600419 conf.conf.include("../../ext/mbedtls/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100420 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600421 conf.file("../../ext/mbedtls/library/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800422 conf.file("../../ext/mbedtls/library/platform.c");
423 conf.file("../../ext/mbedtls/library/platform_util.c");
424 conf.file("../../ext/mbedtls/library/aes.c");
425 conf.file("../../ext/mbedtls/library/sha256.c");
426 conf.file("../../ext/mbedtls/library/md.c");
427 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100428 }
429
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200430 if sig_rsa && enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600431 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100432 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600433 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100434 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600435 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300436 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600437 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300438 } else if sig_ed25519 || enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600439 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100440 } else if enc_kw || enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600441 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100442 } else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600443 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Roland Mikhel5899fac2023-03-14 13:59:55 +0100444 } else if sig_ecdsa_psa {
445 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec-psa.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200446 }
447
David Brown704ac6f2017-07-12 10:14:47 -0600448 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300449 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200450 conf.file("../../boot/bootutil/src/image_rsa.c");
Roland Mikhel5899fac2023-03-14 13:59:55 +0100451 } else if sig_ecdsa || sig_ecdsa_mbedtls || sig_ecdsa_psa {
Antonio de Angelis10529d32023-04-21 21:43:14 +0100452 conf.file("../../boot/bootutil/src/image_ecdsa.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300453 } else if sig_ed25519 {
454 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200455 }
Roland Mikhel5899fac2023-03-14 13:59:55 +0100456
David Brown63902772017-07-12 09:47:49 -0600457 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300458 conf.file("../../boot/bootutil/src/swap_misc.c");
459 conf.file("../../boot/bootutil/src/swap_scratch.c");
460 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600461 conf.file("../../boot/bootutil/src/caps.c");
462 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100463 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300464 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100465 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600466 conf.file("csupport/run.c");
David Brown5f4e1482021-09-16 16:44:09 -0600467 conf.conf.include("../../boot/bootutil/include");
468 conf.conf.include("csupport");
David Brown5f4e1482021-09-16 16:44:09 -0600469 conf.conf.debug(true);
470 conf.conf.flag("-Wall");
471 conf.conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600472
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200473 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
474 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
475 // to build correctly so leaving it here to updated in the future...
David Brown5f4e1482021-09-16 16:44:09 -0600476 conf.conf.flag("-std=c99");
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200477
David Brown5f4e1482021-09-16 16:44:09 -0600478 conf.conf.compile("libbootutil.a");
David Brown63902772017-07-12 09:47:49 -0600479
480 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200481 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600482 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600483 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800484 walk_dir("../../ext/mbedtls/include").unwrap();
485 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600486}
487
488// Output the names of all files within a directory so that Cargo knows when to rebuild.
489fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
490 for ent in fs::read_dir(path.as_ref())? {
491 let ent = ent?;
492 let p = ent.path();
493 if p.is_dir() {
494 walk_dir(p)?;
495 } else {
496 // Note that non-utf8 names will fail.
497 let name = p.to_str().unwrap();
498 if name.ends_with(".c") || name.ends_with(".h") {
499 println!("cargo:rerun-if-changed={}", name);
500 }
501 }
502 }
503
504 Ok(())
505}
David Brown5f4e1482021-09-16 16:44:09 -0600506
507/// Wrap the cc::Build type so that we can make sure that files are only added a single time.
508/// Other methods can be passed through as needed.
509struct CachedBuild {
510 conf: cc::Build,
511 seen: BTreeSet<PathBuf>,
512}
513
514impl CachedBuild {
515 fn new() -> CachedBuild {
516 CachedBuild {
517 conf: cc::Build::new(),
518 seen: BTreeSet::new(),
519 }
520 }
521
522 /// Works like `file` in the Build, but doesn't add a file if the same path has already been
523 /// given.
524 fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut CachedBuild {
525 let p = p.as_ref();
526 if !self.seen.contains(p) {
527 self.conf.file(p);
528 self.seen.insert(p.to_owned());
529 }
530 self
531 }
532}