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