blob: 27da2e3eea63a2ef39bec6146e7a8809f1a6ab8b [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.
13 let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
Fabio Utzig39297432019-05-08 18:51:10 -030014 let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok();
David Brown63902772017-07-12 09:47:49 -060015 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
David Brown641af452021-02-19 12:16:48 -070016 let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok();
Fabio Utzig97710282019-05-24 17:44:49 -030017 let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
David Brown63902772017-07-12 09:47:49 -060018 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzig031eb7d2019-11-28 10:13:14 -030019 let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010020 let validate_primary_slot =
21 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030022 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010023 let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030024 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010025 let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok();
Fabio Utzig90f449e2019-10-24 07:43:53 -030026 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig6c553d62021-05-06 19:56:18 -030027 let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010028 let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -030029 let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010030 let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020031 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070032 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown2ee5f7f2020-01-13 14:04:01 -070033 let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
David Brown7e377ab2021-05-26 16:33:39 -060034 let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok();
David Brown11ffa0a2021-05-26 17:10:47 -060035 let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok();
David Brown1bc106e2021-12-16 13:23:52 -070036 let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok();
David Brown63902772017-07-12 09:47:49 -060037
David Brown5f4e1482021-09-16 16:44:09 -060038 let mut conf = CachedBuild::new();
39 conf.conf.define("__BOOTSIM__", None);
40 conf.conf.define("MCUBOOT_HAVE_LOGGING", None);
41 conf.conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
42 conf.conf.define("MCUBOOT_HAVE_ASSERT_H", None);
43 conf.conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030044
David Brown1bc106e2021-12-16 13:23:52 -070045 if max_align_32 {
46 conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("32"));
47 } else {
48 conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("8"));
49 }
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030050
David Brown5f4e1482021-09-16 16:44:09 -060051 conf.conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020052
David Brown2ee5f7f2020-01-13 14:04:01 -070053 if downgrade_prevention && !overwrite_only {
54 panic!("Downgrade prevention requires overwrite only");
55 }
56
Fabio Utzig9b97b132018-12-18 17:21:51 -020057 if bootstrap {
David Brown5f4e1482021-09-16 16:44:09 -060058 conf.conf.define("MCUBOOT_BOOTSTRAP", None);
59 conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
Fabio Utzig9b97b132018-12-18 17:21:51 -020060 }
61
David Vincze2d736ad2019-02-18 11:50:22 +010062 if validate_primary_slot {
David Brown5f4e1482021-09-16 16:44:09 -060063 conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020064 }
David Brown63902772017-07-12 09:47:49 -060065
David Brown2ee5f7f2020-01-13 14:04:01 -070066 if downgrade_prevention {
David Brown5f4e1482021-09-16 16:44:09 -060067 conf.conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
David Brown2ee5f7f2020-01-13 14:04:01 -070068 }
69
David Brown7e377ab2021-05-26 16:33:39 -060070 if ram_load {
David Brown5f4e1482021-09-16 16:44:09 -060071 conf.conf.define("MCUBOOT_RAM_LOAD", None);
David Brown7e377ab2021-05-26 16:33:39 -060072 }
73
David Brown11ffa0a2021-05-26 17:10:47 -060074 if direct_xip {
David Brown5f4e1482021-09-16 16:44:09 -060075 conf.conf.define("MCUBOOT_DIRECT_XIP", None);
David Brown11ffa0a2021-05-26 17:10:47 -060076 }
77
Fabio Utzig39297432019-05-08 18:51:10 -030078 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030079 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030080 .fold(0, |sum, &v| sum + v as i32) > 1 {
81 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060082 }
David Brown63902772017-07-12 09:47:49 -060083
Fabio Utzig39297432019-05-08 18:51:10 -030084 if sig_rsa || sig_rsa3072 {
David Brown5f4e1482021-09-16 16:44:09 -060085 conf.conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030086 // The Kconfig style defines must be added here as well because
87 // they are used internally by "config-rsa.h"
88 if sig_rsa {
David Brown5f4e1482021-09-16 16:44:09 -060089 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
90 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030091 } else {
David Brown5f4e1482021-09-16 16:44:09 -060092 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
93 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -030094 }
David Brown5f4e1482021-09-16 16:44:09 -060095 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown63902772017-07-12 09:47:49 -060096
David Brown5f4e1482021-09-16 16:44:09 -060097 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +080098 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030099 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -0600100
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800101 conf.file("../../ext/mbedtls/library/rsa.c");
102 conf.file("../../ext/mbedtls/library/bignum.c");
103 conf.file("../../ext/mbedtls/library/platform.c");
104 conf.file("../../ext/mbedtls/library/platform_util.c");
105 conf.file("../../ext/mbedtls/library/asn1parse.c");
Antonio de Angelis02bf0722022-11-22 15:35:43 +0000106 conf.file("../../ext/mbedtls/library/md.c");
107
David Brown704ac6f2017-07-12 10:14:47 -0600108 } else if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600109 conf.conf.define("MCUBOOT_SIGN_EC256", None);
110 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -0200111
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200112 if !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600113 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200114 }
David Brown5f4e1482021-09-16 16:44:09 -0600115 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200116
Fabio Utzig806af0e2018-04-26 10:53:54 -0300117 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200118
119 conf.file("../../ext/tinycrypt/lib/source/utils.c");
120 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
121 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
122 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
123 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
David Brown5f4e1482021-09-16 16:44:09 -0600124 conf.file("../../ext/mbedtls/library/platform_util.c");
125 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700126 } else if sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600127 conf.conf.define("MCUBOOT_SIGN_EC256", None);
128 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown641af452021-02-19 12:16:48 -0700129
David Brown5f4e1482021-09-16 16:44:09 -0600130 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800131 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700132 conf.file("csupport/keys.c");
133
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800134 conf.file("../../ext/mbedtls/library/asn1parse.c");
135 conf.file("../../ext/mbedtls/library/bignum.c");
136 conf.file("../../ext/mbedtls/library/ecdsa.c");
137 conf.file("../../ext/mbedtls/library/ecp.c");
138 conf.file("../../ext/mbedtls/library/ecp_curves.c");
139 conf.file("../../ext/mbedtls/library/platform.c");
140 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300141 } else if sig_ed25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600142 conf.conf.define("MCUBOOT_SIGN_ED25519", None);
143 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300144
David Brown5f4e1482021-09-16 16:44:09 -0600145 conf.conf.include("../../ext/tinycrypt/lib/include");
146 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
147 conf.conf.include("../../ext/mbedtls/include");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300148 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
149 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
150 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300151 conf.file("csupport/keys.c");
152 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600153 conf.file("../../ext/mbedtls/library/platform_util.c");
154 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300155 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300156 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400157 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300158 // When using ECIES-P256 rely on Tinycrypt.
David Brown5f4e1482021-09-16 16:44:09 -0600159 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
160 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800161 conf.file("../../ext/mbedtls/library/sha256.c");
162 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600163 }
164
165 if overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600166 conf.conf.define("MCUBOOT_OVERWRITE_ONLY", None);
David Brown63902772017-07-12 09:47:49 -0600167 }
168
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300169 if swap_move {
David Brown5f4e1482021-09-16 16:44:09 -0600170 conf.conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Andrzej Puzdrowski137d7972021-05-13 13:39:30 +0200171 } else if !overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600172 conf.conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
173 conf.conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300174 }
175
Salome Thirot6fdbf552021-05-14 16:46:14 +0100176 if enc_rsa || enc_aes256_rsa {
177 if enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600178 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100179 }
David Brown5f4e1482021-09-16 16:44:09 -0600180 conf.conf.define("MCUBOOT_ENCRYPT_RSA", None);
181 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
182 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300183
184 conf.file("../../boot/bootutil/src/encrypted.c");
185 conf.file("csupport/keys.c");
186
David Brown5f4e1482021-09-16 16:44:09 -0600187 conf.conf.include("../../ext/mbedtls/include");
188 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800189 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300190
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800191 conf.file("../../ext/mbedtls/library/platform.c");
192 conf.file("../../ext/mbedtls/library/platform_util.c");
193 conf.file("../../ext/mbedtls/library/rsa.c");
194 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
195 conf.file("../../ext/mbedtls/library/md.c");
196 conf.file("../../ext/mbedtls/library/aes.c");
197 conf.file("../../ext/mbedtls/library/bignum.c");
198 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300199 }
200
Salome Thirot6fdbf552021-05-14 16:46:14 +0100201 if enc_kw || enc_aes256_kw {
202 if enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600203 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100204 }
David Brown5f4e1482021-09-16 16:44:09 -0600205 conf.conf.define("MCUBOOT_ENCRYPT_KW", None);
206 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300207
208 conf.file("../../boot/bootutil/src/encrypted.c");
209 conf.file("csupport/keys.c");
210
Fabio Utzig39297432019-05-08 18:51:10 -0300211 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800212 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200213 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300214
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200215 /* Simulator uses Mbed-TLS to wrap keys */
David Brown5f4e1482021-09-16 16:44:09 -0600216 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800217 conf.file("../../ext/mbedtls/library/platform.c");
David Brown5f4e1482021-09-16 16:44:09 -0600218 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800219 conf.file("../../ext/mbedtls/library/platform_util.c");
220 conf.file("../../ext/mbedtls/library/nist_kw.c");
221 conf.file("../../ext/mbedtls/library/cipher.c");
222 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
223 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200224
225 if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600226 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200227
David Brown5f4e1482021-09-16 16:44:09 -0600228 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200229
230 conf.file("../../ext/tinycrypt/lib/source/utils.c");
231 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
232 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
233 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900234 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200235 }
Fabio Utzig97710282019-05-24 17:44:49 -0300236
237 if sig_ed25519 {
238 panic!("ed25519 does not support image encryption with KW yet");
239 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300240 }
241
Fabio Utzig90f449e2019-10-24 07:43:53 -0300242 if enc_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600243 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
244 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
245 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
246 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300247
248 conf.file("../../boot/bootutil/src/encrypted.c");
249 conf.file("csupport/keys.c");
250
David Brown5f4e1482021-09-16 16:44:09 -0600251 conf.conf.include("../../ext/mbedtls/include");
252 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300253
254 /* FIXME: fail with other signature schemes ? */
255
256 conf.file("../../ext/tinycrypt/lib/source/utils.c");
257 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
258 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
259 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
260 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
261
David Brown5f4e1482021-09-16 16:44:09 -0600262 conf.file("../../ext/mbedtls/library/platform_util.c");
263 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300264
265 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
266 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
267 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
268 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
269 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100270 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
271 if enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600272 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100273 }
David Brown5f4e1482021-09-16 16:44:09 -0600274 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
275 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
276 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
277 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig6c553d62021-05-06 19:56:18 -0300278
David Brown5f4e1482021-09-16 16:44:09 -0600279 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300280
281 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800282 conf.file("../../ext/mbedtls/library/sha256.c");
283 conf.file("../../ext/mbedtls/library/asn1parse.c");
284 conf.file("../../ext/mbedtls/library/bignum.c");
285 conf.file("../../ext/mbedtls/library/ecdh.c");
286 conf.file("../../ext/mbedtls/library/md.c");
287 conf.file("../../ext/mbedtls/library/aes.c");
288 conf.file("../../ext/mbedtls/library/ecp.c");
289 conf.file("../../ext/mbedtls/library/ecp_curves.c");
290 conf.file("../../ext/mbedtls/library/platform.c");
291 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300292 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300293 }
294
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300295 if enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600296 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
297 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
298 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
299 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300300
301 conf.file("../../boot/bootutil/src/encrypted.c");
302 conf.file("csupport/keys.c");
303
David Brown5f4e1482021-09-16 16:44:09 -0600304 conf.conf.include("../../ext/mbedtls/include");
305 conf.conf.include("../../ext/tinycrypt/lib/include");
306 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300307
308 conf.file("../../ext/fiat/src/curve25519.c");
309
310 conf.file("../../ext/tinycrypt/lib/source/utils.c");
311 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
312
David Brown5f4e1482021-09-16 16:44:09 -0600313 conf.file("../../ext/mbedtls/library/platform_util.c");
314 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300315
316 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
317 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
318 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
319 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
320 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300321
Salome Thirot6fdbf552021-05-14 16:46:14 +0100322 else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600323 conf.conf.define("MCUBOOT_AES_256", None);
324 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
325 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
326 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
327 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100328
329 conf.file("../../boot/bootutil/src/encrypted.c");
330 conf.file("csupport/keys.c");
331
David Brown5f4e1482021-09-16 16:44:09 -0600332 conf.conf.include("../../ext/mbedtls/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100333 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600334 conf.file("../../ext/mbedtls/library/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800335 conf.file("../../ext/mbedtls/library/platform.c");
336 conf.file("../../ext/mbedtls/library/platform_util.c");
337 conf.file("../../ext/mbedtls/library/aes.c");
338 conf.file("../../ext/mbedtls/library/sha256.c");
339 conf.file("../../ext/mbedtls/library/md.c");
340 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100341 }
342
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200343 if sig_rsa && enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600344 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100345 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600346 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100347 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600348 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300349 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600350 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300351 } else if sig_ed25519 || enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600352 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100353 } else if enc_kw || enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600354 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100355 } else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600356 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200357 }
358
David Brown704ac6f2017-07-12 10:14:47 -0600359 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300360 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200361 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown641af452021-02-19 12:16:48 -0700362 } else if sig_ecdsa || sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600363 conf.conf.include("../../ext/mbedtls/include");
Antonio de Angelis10529d32023-04-21 21:43:14 +0100364 conf.file("../../boot/bootutil/src/image_ecdsa.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300365 } else if sig_ed25519 {
366 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200367 }
David Brown63902772017-07-12 09:47:49 -0600368 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300369 conf.file("../../boot/bootutil/src/swap_misc.c");
370 conf.file("../../boot/bootutil/src/swap_scratch.c");
371 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600372 conf.file("../../boot/bootutil/src/caps.c");
373 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100374 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300375 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100376 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600377 conf.file("csupport/run.c");
David Brown5f4e1482021-09-16 16:44:09 -0600378 conf.conf.include("../../boot/bootutil/include");
379 conf.conf.include("csupport");
David Brown5f4e1482021-09-16 16:44:09 -0600380 conf.conf.debug(true);
381 conf.conf.flag("-Wall");
382 conf.conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600383
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200384 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
385 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
386 // to build correctly so leaving it here to updated in the future...
David Brown5f4e1482021-09-16 16:44:09 -0600387 conf.conf.flag("-std=c99");
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200388
David Brown5f4e1482021-09-16 16:44:09 -0600389 conf.conf.compile("libbootutil.a");
David Brown63902772017-07-12 09:47:49 -0600390
391 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200392 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600393 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600394 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800395 walk_dir("../../ext/mbedtls/include").unwrap();
396 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600397}
398
399// Output the names of all files within a directory so that Cargo knows when to rebuild.
400fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
401 for ent in fs::read_dir(path.as_ref())? {
402 let ent = ent?;
403 let p = ent.path();
404 if p.is_dir() {
405 walk_dir(p)?;
406 } else {
407 // Note that non-utf8 names will fail.
408 let name = p.to_str().unwrap();
409 if name.ends_with(".c") || name.ends_with(".h") {
410 println!("cargo:rerun-if-changed={}", name);
411 }
412 }
413 }
414
415 Ok(())
416}
David Brown5f4e1482021-09-16 16:44:09 -0600417
418/// Wrap the cc::Build type so that we can make sure that files are only added a single time.
419/// Other methods can be passed through as needed.
420struct CachedBuild {
421 conf: cc::Build,
422 seen: BTreeSet<PathBuf>,
423}
424
425impl CachedBuild {
426 fn new() -> CachedBuild {
427 CachedBuild {
428 conf: cc::Build::new(),
429 seen: BTreeSet::new(),
430 }
431 }
432
433 /// Works like `file` in the Build, but doesn't add a file if the same path has already been
434 /// given.
435 fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut CachedBuild {
436 let p = p.as_ref();
437 if !self.seen.contains(p) {
438 self.conf.file(p);
439 self.seen.insert(p.to_owned());
440 }
441 self
442 }
443}