Add encrypted image support on sim

This adds new cargo features to allow running tests of encrypted
images with both RSA-OAEP and AES-128-KW.

When installing images on the simulated flash, both a plain and an
encrypted images are created. When encrypted image support is enabled,
verification of images in slot1 match against the encrypted image,
otherwise plain images are used.

PS: Also fixes ImageHeader to match bootutil definition.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/sim/mcuboot-sys/build.rs b/sim/mcuboot-sys/build.rs
index 2c28dbb..ea958f6 100644
--- a/sim/mcuboot-sys/build.rs
+++ b/sim/mcuboot-sys/build.rs
@@ -13,6 +13,8 @@
     let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
     let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
     let validate_slot0 = env::var("CARGO_FEATURE_VALIDATE_SLOT0").is_ok();
+    let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
+    let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
 
     let mut conf = gcc::Build::new();
     conf.define("__BOOTSIM__", None);
@@ -73,6 +75,49 @@
         conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
     }
 
+    if enc_rsa {
+        conf.define("MCUBOOT_ENCRYPT_RSA", None);
+        conf.define("MCUBOOT_ENC_IMAGES", None);
+        conf.define("MCUBOOT_USE_MBED_TLS", None);
+        conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
+
+        conf.file("../../boot/bootutil/src/encrypted.c");
+        conf.file("csupport/keys.c");
+
+        conf.include("mbedtls/include");
+        conf.file("mbedtls/library/sha256.c");
+
+        conf.file("mbedtls/library/platform.c");
+        conf.file("mbedtls/library/platform_util.c");
+        conf.file("mbedtls/library/rsa.c");
+        conf.file("mbedtls/library/rsa_internal.c");
+        conf.file("mbedtls/library/md.c");
+        conf.file("mbedtls/library/md_wrap.c");
+        conf.file("mbedtls/library/aes.c");
+        conf.file("mbedtls/library/bignum.c");
+        conf.file("mbedtls/library/asn1parse.c");
+    }
+
+    if enc_kw {
+        conf.define("MCUBOOT_ENCRYPT_KW", None);
+        conf.define("MCUBOOT_ENC_IMAGES", None);
+        conf.define("MCUBOOT_USE_MBED_TLS", None);
+        conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
+
+        conf.file("../../boot/bootutil/src/encrypted.c");
+        conf.file("csupport/keys.c");
+
+        conf.include("mbedtls/include");
+        conf.file("mbedtls/library/sha256.c");
+
+        conf.file("mbedtls/library/platform.c");
+        conf.file("mbedtls/library/platform_util.c");
+        conf.file("mbedtls/library/nist_kw.c");
+        conf.file("mbedtls/library/cipher.c");
+        conf.file("mbedtls/library/cipher_wrap.c");
+        conf.file("mbedtls/library/aes.c");
+    }
+
     conf.file("../../boot/bootutil/src/image_validate.c");
     if sig_rsa {
         conf.file("../../boot/bootutil/src/image_rsa.c");