Add Zephyr config for RSA-3072

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig
index 241b201..3de379f 100644
--- a/boot/zephyr/Kconfig
+++ b/boot/zephyr/Kconfig
@@ -66,6 +66,13 @@
 	select BOOT_USE_MBEDTLS
 	select MBEDTLS
 
+if BOOT_SIGNATURE_TYPE_RSA
+config BOOT_SIGNATURE_TYPE_RSA_LEN
+	int "RSA signature length"
+	range 2048 3072
+	default 2048
+endif
+
 config BOOT_SIGNATURE_TYPE_ECDSA_P256
 	bool "Elliptic curve digital signatures with curve P-256"
 
diff --git a/boot/zephyr/include/config-rsa.h b/boot/zephyr/include/config-rsa.h
index 2e1153d..3e05692 100644
--- a/boot/zephyr/include/config-rsa.h
+++ b/boot/zephyr/include/config-rsa.h
@@ -66,7 +66,12 @@
 
 /* Save RAM by adjusting to our exact needs */
 #define MBEDTLS_ECP_MAX_BITS             2048
+
+#if (CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN == 3072)
+#define MBEDTLS_MPI_MAX_SIZE              384
+#else
 #define MBEDTLS_MPI_MAX_SIZE              256
+#endif
 
 #define MBEDTLS_SSL_MAX_CONTENT_LEN 1024
 
diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h
index 8da288f..7f110dd 100644
--- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h
+++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h
@@ -20,6 +20,12 @@
 
 #ifdef CONFIG_BOOT_SIGNATURE_TYPE_RSA
 #define MCUBOOT_SIGN_RSA
+#  if (CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN != 2048 && \
+       CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN != 3072)
+#    error "Invalid RSA key size (must be 2048 or 3072)"
+#  else
+#    define MCUBOOT_SIGN_RSA_LEN CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN
+#  endif
 #elif defined(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
 #define MCUBOOT_SIGN_EC256
 #endif
diff --git a/boot/zephyr/os.c b/boot/zephyr/os.c
index 48fb4de..2d9faae 100644
--- a/boot/zephyr/os.c
+++ b/boot/zephyr/os.c
@@ -29,11 +29,15 @@
 
 /*
  * This is the heap for mbed TLS.  The value needed depends on the key
- * size and algorithm used.  For RSA-2048 signing, 6144 bytes seems to be
- * enough. When using RSA-2048-OAEP encryption + RSA-2048 signing, 10240
- * bytes seem to be enough.
+ * size and algorithm used.
+ *
+ * - RSA-2048 signing without encryption is known to work well with 6144 bytes;
+ * - When using RSA-2048-OAEP encryption + RSA-2048 signing, or RSA-3072
+ *   signing (no encryption) 10240 bytes seems to be enough.
+ *
+ * NOTE: RSA-3072 signing + RSA-2048-OAEP might require growing the size...
  */
-#if !defined(CONFIG_BOOT_ENCRYPT_RSA)
+#if (CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN == 2048) && !defined(CONFIG_BOOT_ENCRYPT_RSA)
 #define CRYPTO_HEAP_SIZE 6144
 #else
 #define CRYPTO_HEAP_SIZE 10240
diff --git a/boot/zephyr/prj.conf b/boot/zephyr/prj.conf
index 23c462b..4a179d3 100644
--- a/boot/zephyr/prj.conf
+++ b/boot/zephyr/prj.conf
@@ -12,6 +12,7 @@
 
 ### Default to RSA
 CONFIG_BOOT_SIGNATURE_TYPE_RSA=y
+CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN=2048
 CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256=n
 
 ### The bootloader generates its own signature verification based on a
@@ -19,6 +20,7 @@
 ### algorithm (CONFIG_BOOT_SIGNATURE_TYPE_).
 ### The PEM files below are provided as examples.
 CONFIG_BOOT_SIGNATURE_KEY_FILE="root-rsa-2048.pem"
+#CONFIG_BOOT_SIGNATURE_KEY_FILE="root-rsa-3072.pem"
 #CONFIG_BOOT_SIGNATURE_KEY_FILE="root-ec-p256.pem"
 
 ### mbedTLS has its own heap