David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 1 | // Build mcuboot as a library, based on the requested features. |
| 2 | |
Fabio Utzig | 455cad5 | 2018-10-15 14:36:33 -0700 | [diff] [blame] | 3 | extern crate cc; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 4 | |
| 5 | use std::env; |
| 6 | use std::fs; |
| 7 | use std::io; |
| 8 | use std::path::Path; |
| 9 | |
| 10 | fn main() { |
| 11 | // Feature flags. |
| 12 | let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok(); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 13 | let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 14 | let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok(); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 15 | let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 16 | let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 17 | let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok(); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 18 | let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok(); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 19 | let validate_primary_slot = |
| 20 | env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 21 | let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok(); |
| 22 | let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok(); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 23 | let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok(); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 24 | let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok(); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 25 | let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok(); |
David Brown | 5e6f5e0 | 2019-04-04 10:50:05 +0700 | [diff] [blame] | 26 | let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok(); |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 27 | let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 28 | |
Fabio Utzig | 455cad5 | 2018-10-15 14:36:33 -0700 | [diff] [blame] | 29 | let mut conf = cc::Build::new(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 30 | conf.define("__BOOTSIM__", None); |
Fabio Utzig | 08fcfe9 | 2018-11-26 10:18:18 -0200 | [diff] [blame] | 31 | conf.define("MCUBOOT_HAVE_LOGGING", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 32 | conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None); |
Marti Bolivar | 248da08 | 2018-04-24 15:11:39 -0400 | [diff] [blame] | 33 | conf.define("MCUBOOT_HAVE_ASSERT_H", None); |
Marti Bolivar | f9bfddd | 2018-04-24 14:28:33 -0400 | [diff] [blame] | 34 | conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128")); |
David Brown | 5e6f5e0 | 2019-04-04 10:50:05 +0700 | [diff] [blame] | 35 | conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" })); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 36 | |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 37 | if downgrade_prevention && !overwrite_only { |
| 38 | panic!("Downgrade prevention requires overwrite only"); |
| 39 | } |
| 40 | |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 41 | if bootstrap { |
| 42 | conf.define("MCUBOOT_BOOTSTRAP", None); |
Fabio Utzig | 3c9d5c4 | 2020-10-04 10:12:53 -0300 | [diff] [blame] | 43 | conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 44 | } |
| 45 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 46 | if validate_primary_slot { |
| 47 | conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 48 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 49 | |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 50 | if downgrade_prevention { |
| 51 | conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None); |
| 52 | } |
| 53 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 54 | // Currently no more than one sig type can be used simultaneously. |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 55 | if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter() |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 56 | .fold(0, |sum, &v| sum + v as i32) > 1 { |
| 57 | panic!("mcuboot does not support more than one sig type at the same time"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 58 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 59 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 60 | if sig_rsa || sig_rsa3072 { |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 61 | conf.define("MCUBOOT_SIGN_RSA", None); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 62 | // The Kconfig style defines must be added here as well because |
| 63 | // they are used internally by "config-rsa.h" |
| 64 | if sig_rsa { |
| 65 | conf.define("MCUBOOT_SIGN_RSA_LEN", "2048"); |
Fabio Utzig | 4626853 | 2020-01-04 21:12:55 -0300 | [diff] [blame] | 66 | conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 67 | } else { |
| 68 | conf.define("MCUBOOT_SIGN_RSA_LEN", "3072"); |
Fabio Utzig | 4626853 | 2020-01-04 21:12:55 -0300 | [diff] [blame] | 69 | conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 70 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 71 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 72 | |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 73 | conf.include("../../ext/mbedtls/crypto/include"); |
| 74 | conf.file("../../ext/mbedtls/crypto/library/sha256.c"); |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 75 | conf.file("csupport/keys.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 76 | |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 77 | conf.file("../../ext/mbedtls/crypto/library/rsa.c"); |
| 78 | conf.file("../../ext/mbedtls/crypto/library/bignum.c"); |
| 79 | conf.file("../../ext/mbedtls/crypto/library/platform.c"); |
| 80 | conf.file("../../ext/mbedtls/crypto/library/platform_util.c"); |
| 81 | conf.file("../../ext/mbedtls/crypto/library/asn1parse.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 82 | } else if sig_ecdsa { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 83 | conf.define("MCUBOOT_SIGN_EC256", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 84 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 85 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 86 | if !enc_kw { |
David Brown | b748f6f | 2019-10-11 10:07:31 -0600 | [diff] [blame] | 87 | conf.include("../../ext/mbedtls-asn1/include"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 88 | } |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 89 | conf.include("../../ext/tinycrypt/lib/include"); |
| 90 | |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 91 | conf.file("csupport/keys.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 92 | |
| 93 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 94 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 95 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 96 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 97 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
| 98 | |
David Brown | b748f6f | 2019-10-11 10:07:31 -0600 | [diff] [blame] | 99 | conf.file("../../ext/mbedtls-asn1/src/platform_util.c"); |
| 100 | conf.file("../../ext/mbedtls-asn1/src/asn1parse.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 101 | } else if sig_ecdsa_mbedtls { |
| 102 | conf.define("MCUBOOT_SIGN_EC256", None); |
| 103 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 104 | |
| 105 | conf.include("../../ext/mbedtls/crypto/include"); |
| 106 | conf.file("../../ext/mbedtls/crypto/library/sha256.c"); |
| 107 | conf.file("csupport/keys.c"); |
| 108 | |
| 109 | conf.file("../../ext/mbedtls/crypto/library/asn1parse.c"); |
| 110 | conf.file("../../ext/mbedtls/crypto/library/bignum.c"); |
| 111 | conf.file("../../ext/mbedtls/crypto/library/ecdsa.c"); |
| 112 | conf.file("../../ext/mbedtls/crypto/library/ecp.c"); |
| 113 | conf.file("../../ext/mbedtls/crypto/library/ecp_curves.c"); |
| 114 | conf.file("../../ext/mbedtls/crypto/library/platform.c"); |
| 115 | conf.file("../../ext/mbedtls/crypto/library/platform_util.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 116 | } else if sig_ed25519 { |
| 117 | conf.define("MCUBOOT_SIGN_ED25519", None); |
Fabio Utzig | a1c142d | 2020-01-03 08:28:11 -0300 | [diff] [blame] | 118 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 119 | |
Fabio Utzig | a1c142d | 2020-01-03 08:28:11 -0300 | [diff] [blame] | 120 | conf.include("../../ext/tinycrypt/lib/include"); |
| 121 | conf.include("../../ext/tinycrypt-sha512/lib/include"); |
| 122 | conf.include("../../ext/mbedtls-asn1/include"); |
| 123 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 124 | conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c"); |
| 125 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 126 | conf.file("csupport/keys.c"); |
| 127 | conf.file("../../ext/fiat/src/curve25519.c"); |
Fabio Utzig | a1c142d | 2020-01-03 08:28:11 -0300 | [diff] [blame] | 128 | conf.file("../../ext/mbedtls-asn1/src/platform_util.c"); |
| 129 | conf.file("../../ext/mbedtls-asn1/src/asn1parse.c"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 130 | } else if !enc_ec256 && !enc_x25519 { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 131 | // No signature type, only sha256 validation. The default |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 132 | // configuration file bundled with mbedTLS is sufficient. |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 133 | // When using ECIES-P256 rely on Tinycrypt. |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 134 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 135 | conf.include("../../ext/mbedtls/crypto/include"); |
| 136 | conf.file("../../ext/mbedtls/crypto/library/sha256.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if overwrite_only { |
| 140 | conf.define("MCUBOOT_OVERWRITE_ONLY", None); |
| 141 | } |
| 142 | |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 143 | if swap_move { |
| 144 | conf.define("MCUBOOT_SWAP_USING_MOVE", None); |
| 145 | } |
| 146 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 147 | if enc_rsa { |
| 148 | conf.define("MCUBOOT_ENCRYPT_RSA", None); |
| 149 | conf.define("MCUBOOT_ENC_IMAGES", None); |
| 150 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 151 | |
| 152 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 153 | conf.file("csupport/keys.c"); |
| 154 | |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 155 | conf.include("../../ext/mbedtls/crypto/include"); |
| 156 | conf.file("../../ext/mbedtls/crypto/library/sha256.c"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 157 | |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 158 | conf.file("../../ext/mbedtls/crypto/library/platform.c"); |
| 159 | conf.file("../../ext/mbedtls/crypto/library/platform_util.c"); |
| 160 | conf.file("../../ext/mbedtls/crypto/library/rsa.c"); |
| 161 | conf.file("../../ext/mbedtls/crypto/library/rsa_internal.c"); |
| 162 | conf.file("../../ext/mbedtls/crypto/library/md.c"); |
| 163 | conf.file("../../ext/mbedtls/crypto/library/aes.c"); |
| 164 | conf.file("../../ext/mbedtls/crypto/library/bignum.c"); |
| 165 | conf.file("../../ext/mbedtls/crypto/library/asn1parse.c"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if enc_kw { |
| 169 | conf.define("MCUBOOT_ENCRYPT_KW", None); |
| 170 | conf.define("MCUBOOT_ENC_IMAGES", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 171 | |
| 172 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 173 | conf.file("csupport/keys.c"); |
| 174 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 175 | if sig_rsa || sig_rsa3072 { |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 176 | conf.file("../../ext/mbedtls/crypto/library/sha256.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 177 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 178 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 179 | /* Simulator uses Mbed-TLS to wrap keys */ |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 180 | conf.include("../../ext/mbedtls/crypto/include"); |
| 181 | conf.file("../../ext/mbedtls/crypto/library/platform.c"); |
| 182 | conf.file("../../ext/mbedtls/crypto/library/platform_util.c"); |
| 183 | conf.file("../../ext/mbedtls/crypto/library/nist_kw.c"); |
| 184 | conf.file("../../ext/mbedtls/crypto/library/cipher.c"); |
| 185 | conf.file("../../ext/mbedtls/crypto/library/cipher_wrap.c"); |
| 186 | conf.file("../../ext/mbedtls/crypto/library/aes.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 187 | |
| 188 | if sig_ecdsa { |
| 189 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 190 | |
| 191 | conf.include("../../ext/tinycrypt/lib/include"); |
| 192 | |
| 193 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 194 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 195 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 196 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 197 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 198 | } |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 199 | |
| 200 | if sig_ed25519 { |
| 201 | panic!("ed25519 does not support image encryption with KW yet"); |
| 202 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 203 | } |
| 204 | |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 205 | if enc_ec256 { |
| 206 | conf.define("MCUBOOT_ENCRYPT_EC256", None); |
| 207 | conf.define("MCUBOOT_ENC_IMAGES", None); |
| 208 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | 4b4ed98 | 2020-01-06 09:09:51 -0300 | [diff] [blame] | 209 | conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 210 | |
| 211 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 212 | conf.file("csupport/keys.c"); |
| 213 | |
| 214 | conf.include("../../ext/mbedtls-asn1/include"); |
| 215 | conf.include("../../ext/tinycrypt/lib/include"); |
| 216 | |
| 217 | /* FIXME: fail with other signature schemes ? */ |
| 218 | |
| 219 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 220 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 221 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 222 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 223 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
| 224 | |
| 225 | conf.file("../../ext/mbedtls-asn1/src/platform_util.c"); |
| 226 | conf.file("../../ext/mbedtls-asn1/src/asn1parse.c"); |
| 227 | |
| 228 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 229 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 230 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
| 231 | conf.file("../../ext/tinycrypt/lib/source/hmac.c"); |
| 232 | conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c"); |
| 233 | } |
| 234 | |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 235 | if enc_x25519 { |
| 236 | conf.define("MCUBOOT_ENCRYPT_X25519", None); |
| 237 | conf.define("MCUBOOT_ENC_IMAGES", None); |
| 238 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 239 | conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
| 240 | |
| 241 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 242 | conf.file("csupport/keys.c"); |
| 243 | |
| 244 | conf.include("../../ext/mbedtls-asn1/include"); |
| 245 | conf.include("../../ext/tinycrypt/lib/include"); |
| 246 | conf.include("../../ext/tinycrypt-sha512/lib/include"); |
| 247 | |
| 248 | conf.file("../../ext/fiat/src/curve25519.c"); |
| 249 | |
| 250 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 251 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 252 | |
| 253 | conf.file("../../ext/mbedtls-asn1/src/platform_util.c"); |
| 254 | conf.file("../../ext/mbedtls-asn1/src/asn1parse.c"); |
| 255 | |
| 256 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 257 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 258 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
| 259 | conf.file("../../ext/tinycrypt/lib/source/hmac.c"); |
| 260 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 261 | |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 262 | if sig_rsa && enc_kw { |
| 263 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>")); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 264 | } else if sig_rsa || sig_rsa3072 || enc_rsa { |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 265 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>")); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 266 | } else if sig_ecdsa_mbedtls { |
| 267 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ecdsa.h>")); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 268 | } else if (sig_ecdsa || enc_ec256) && !enc_kw { |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 269 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 270 | } else if sig_ed25519 || enc_x25519 { |
Fabio Utzig | a1c142d | 2020-01-03 08:28:11 -0300 | [diff] [blame] | 271 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 272 | } else if enc_kw { |
| 273 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>")); |
| 274 | } |
| 275 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 276 | conf.file("../../boot/bootutil/src/image_validate.c"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 277 | if sig_rsa || sig_rsa3072 { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 278 | conf.file("../../boot/bootutil/src/image_rsa.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 279 | } else if sig_ecdsa || sig_ecdsa_mbedtls { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 280 | conf.file("../../boot/bootutil/src/image_ec256.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 281 | } else if sig_ed25519 { |
| 282 | conf.file("../../boot/bootutil/src/image_ed25519.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 283 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 284 | conf.file("../../boot/bootutil/src/loader.c"); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 285 | conf.file("../../boot/bootutil/src/swap_misc.c"); |
| 286 | conf.file("../../boot/bootutil/src/swap_scratch.c"); |
| 287 | conf.file("../../boot/bootutil/src/swap_move.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 288 | conf.file("../../boot/bootutil/src/caps.c"); |
| 289 | conf.file("../../boot/bootutil/src/bootutil_misc.c"); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 290 | conf.file("../../boot/bootutil/src/bootutil_public.c"); |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 291 | conf.file("../../boot/bootutil/src/tlv.c"); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 292 | conf.file("../../boot/bootutil/src/fault_injection_hardening.c"); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 293 | conf.file("csupport/run.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 294 | conf.include("../../boot/bootutil/include"); |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 295 | conf.include("csupport"); |
Fabio Utzig | 9a4b9ba | 2018-05-07 08:31:27 -0300 | [diff] [blame] | 296 | conf.include("../../boot/zephyr/include"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 297 | conf.debug(true); |
| 298 | conf.flag("-Wall"); |
David Brown | 0b693c0 | 2017-07-12 12:34:33 -0600 | [diff] [blame] | 299 | conf.flag("-Werror"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 300 | |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 301 | // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90. |
| 302 | // It has incomplete std=c11 and std=c99 support but std=c99 was checked |
| 303 | // to build correctly so leaving it here to updated in the future... |
| 304 | conf.flag("-std=c99"); |
| 305 | |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 306 | conf.compile("libbootutil.a"); |
| 307 | |
| 308 | walk_dir("../../boot").unwrap(); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 309 | walk_dir("../../ext/tinycrypt/lib/source").unwrap(); |
David Brown | b748f6f | 2019-10-11 10:07:31 -0600 | [diff] [blame] | 310 | walk_dir("../../ext/mbedtls-asn1").unwrap(); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 311 | walk_dir("csupport").unwrap(); |
Fabio Utzig | e60b12f | 2020-02-06 07:15:30 -0300 | [diff] [blame] | 312 | walk_dir("../../ext/mbedtls/crypto/include").unwrap(); |
| 313 | walk_dir("../../ext/mbedtls/crypto/library").unwrap(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Output the names of all files within a directory so that Cargo knows when to rebuild. |
| 317 | fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { |
| 318 | for ent in fs::read_dir(path.as_ref())? { |
| 319 | let ent = ent?; |
| 320 | let p = ent.path(); |
| 321 | if p.is_dir() { |
| 322 | walk_dir(p)?; |
| 323 | } else { |
| 324 | // Note that non-utf8 names will fail. |
| 325 | let name = p.to_str().unwrap(); |
| 326 | if name.ends_with(".c") || name.ends_with(".h") { |
| 327 | println!("cargo:rerun-if-changed={}", name); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | Ok(()) |
| 333 | } |