Skip to content

Commit 6fdbf55

Browse files
salomethirot-armutzig
authored andcommitted
Sim: Add testcases for AES256 image encryption
Signed-off-by: Salome Thirot <salome.thirot@arm.com>
1 parent 0f64197 commit 6fdbf55

File tree

10 files changed

+237
-71
lines changed

10 files changed

+237
-71
lines changed

sim/Cargo.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sim/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ overwrite-only = ["mcuboot-sys/overwrite-only"]
1616
swap-move = ["mcuboot-sys/swap-move"]
1717
validate-primary-slot = ["mcuboot-sys/validate-primary-slot"]
1818
enc-rsa = ["mcuboot-sys/enc-rsa"]
19+
enc-aes256-rsa = ["mcuboot-sys/enc-aes256-rsa"]
1920
enc-kw = ["mcuboot-sys/enc-kw"]
21+
enc-aes256-kw = ["mcuboot-sys/enc-aes256-kw"]
2022
enc-ec256 = ["mcuboot-sys/enc-ec256"]
2123
enc-ec256-mbedtls = ["mcuboot-sys/enc-ec256-mbedtls"]
24+
enc-aes256-ec256 = ["mcuboot-sys/enc-aes256-ec256"]
2225
enc-x25519 = ["mcuboot-sys/enc-x25519"]
26+
enc-aes256-x25519 = ["mcuboot-sys/enc-aes256-x25519"]
2327
bootstrap = ["mcuboot-sys/bootstrap"]
2428
multiimage = ["mcuboot-sys/multiimage"]
2529
large-write = []
@@ -41,6 +45,7 @@ untrusted = "0.7"
4145
pem = "0.8"
4246
aes-ctr = "0.4.0"
4347
base64 = "0.12.0"
48+
typenum = "1.13.0"
4449

4550
# The simulator runs very slowly without optimization. A value of 1
4651
# compiles in about half the time, but runs about 5-6 times slower. 2

sim/mcuboot-sys/Cargo.toml

+13
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,30 @@ validate-primary-slot = []
3838
# Encrypt image in the secondary slot using RSA-OAEP-2048
3939
enc-rsa = []
4040

41+
# Encrypt image in the secondary slot using AES-256-CTR and RSA-OAEP-2048
42+
enc-aes256-rsa = []
43+
4144
# Encrypt image in the secondary slot using AES-KW-128
4245
enc-kw = []
4346

47+
# Encrypt image in the secondary slot using AES-256-CTR and AES-KW-256
48+
enc-aes256-kw = []
49+
4450
# Encrypt image in the secondary slot using ECIES-P256
4551
enc-ec256 = []
4652

53+
# Encrypt image in the secondary slot using AES-256-CTR and ECIES-P256
54+
enc-aes256-ec256 = []
55+
4756
# Encrypt image in the secondary slot using ECIES-P256 using Mbed TLS
4857
enc-ec256-mbedtls = []
4958

5059
# Encrypt image in the secondary slot using ECIES-X25519
5160
enc-x25519 = []
5261

62+
# Encrypt image in the secondary slot using AES-256-CTR and ECIES-X25519
63+
enc-aes256-x25519 = []
64+
5365
# Allow bootstrapping an empty/invalid primary slot from a valid secondary slot
5466
bootstrap = []
5567

@@ -59,6 +71,7 @@ multiimage = []
5971
# Check (in software) against version downgrades.
6072
downgrade-prevention = []
6173

74+
6275
[build-dependencies]
6376
cc = "1.0.25"
6477

sim/mcuboot-sys/build.rs

+43-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ fn main() {
1919
let validate_primary_slot =
2020
env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
2121
let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
22+
let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok();
2223
let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
24+
let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok();
2325
let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
2426
let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok();
27+
let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok();
2528
let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok();
29+
let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok();
2630
let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
2731
let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
2832
let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
@@ -148,7 +152,10 @@ fn main() {
148152
conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
149153
}
150154

151-
if enc_rsa {
155+
if enc_rsa || enc_aes256_rsa {
156+
if enc_aes256_rsa {
157+
conf.define("MCUBOOT_AES_256", None);
158+
}
152159
conf.define("MCUBOOT_ENCRYPT_RSA", None);
153160
conf.define("MCUBOOT_ENC_IMAGES", None);
154161
conf.define("MCUBOOT_USE_MBED_TLS", None);
@@ -169,7 +176,10 @@ fn main() {
169176
conf.file("../../ext/mbedtls/crypto/library/asn1parse.c");
170177
}
171178

172-
if enc_kw {
179+
if enc_kw || enc_aes256_kw {
180+
if enc_aes256_kw {
181+
conf.define("MCUBOOT_AES_256", None);
182+
}
173183
conf.define("MCUBOOT_ENCRYPT_KW", None);
174184
conf.define("MCUBOOT_ENC_IMAGES", None);
175185

@@ -234,7 +244,10 @@ fn main() {
234244
conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
235245
conf.file("../../ext/tinycrypt/lib/source/hmac.c");
236246
conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
237-
} else if enc_ec256_mbedtls {
247+
} else if enc_ec256_mbedtls || enc_aes256_ec256 {
248+
if enc_aes256_ec256 {
249+
conf.define("MCUBOOT_AES_256", None);
250+
}
238251
conf.define("MCUBOOT_ENCRYPT_EC256", None);
239252
conf.define("MCUBOOT_ENC_IMAGES", None);
240253
conf.define("MCUBOOT_USE_MBED_TLS", None);
@@ -283,18 +296,42 @@ fn main() {
283296
conf.file("../../ext/tinycrypt/lib/source/hmac.c");
284297
}
285298

299+
else if enc_aes256_x25519 {
300+
conf.define("MCUBOOT_AES_256", None);
301+
conf.define("MCUBOOT_ENCRYPT_X25519", None);
302+
conf.define("MCUBOOT_ENC_IMAGES", None);
303+
conf.define("MCUBOOT_USE_MBED_TLS", None);
304+
conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
305+
306+
conf.file("../../boot/bootutil/src/encrypted.c");
307+
conf.file("csupport/keys.c");
308+
309+
conf.include("../../ext/mbedtls/crypto/include");
310+
conf.file("../../ext/fiat/src/curve25519.c");
311+
conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
312+
conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
313+
conf.file("../../ext/mbedtls/crypto/library/platform.c");
314+
conf.file("../../ext/mbedtls/crypto/library/platform_util.c");
315+
conf.file("../../ext/mbedtls/crypto/library/aes.c");
316+
conf.file("../../ext/mbedtls/crypto/library/sha256.c");
317+
conf.file("../../ext/mbedtls/crypto/library/md.c");
318+
conf.file("../../ext/mbedtls/crypto/library/sha512.c");
319+
}
320+
286321
if sig_rsa && enc_kw {
287322
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
288-
} else if sig_rsa || sig_rsa3072 || enc_rsa {
323+
} else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
289324
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
290-
} else if sig_ecdsa_mbedtls || enc_ec256_mbedtls {
325+
} else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
291326
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
292327
} else if (sig_ecdsa || enc_ec256) && !enc_kw {
293328
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
294329
} else if sig_ed25519 || enc_x25519 {
295330
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
296-
} else if enc_kw {
331+
} else if enc_kw || enc_aes256_kw {
297332
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
333+
} else if enc_aes256_x25519 {
334+
conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
298335
}
299336

300337
conf.file("../../boot/bootutil/src/image_validate.c");

sim/mcuboot-sys/csupport/keys.c

+9
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,20 @@ const struct bootutil_key bootutil_enc_key = {
256256
#endif
257257

258258
#if defined(MCUBOOT_ENCRYPT_KW)
259+
#if defined(MCUBOOT_AES_256)
260+
unsigned char enc_key[] = {
261+
0xE4, 0x5C, 0x51, 0x46, 0xD2, 0x1C, 0x82, 0x35, 0xCC, 0x1A, 0x19, 0xAF,
262+
0xA1, 0xF2, 0xAA, 0x20, 0xC8, 0x8C, 0x7F, 0x40, 0x6C, 0xDB, 0x22, 0xAA,
263+
0x6A, 0xB5, 0xCB, 0xAA, 0xF8, 0xB1, 0x5B, 0xB4
264+
};
265+
static unsigned int enc_key_len = 32;
266+
#else
259267
unsigned char enc_key[] = {
260268
0xd1, 0x5a, 0x04, 0x95, 0xc4, 0xc2, 0xa8, 0xff, 0x30, 0x78, 0xce, 0x49,
261269
0xb5, 0xfc, 0xb2, 0xdd
262270
};
263271
static unsigned int enc_key_len = 16;
272+
#endif
264273
const struct bootutil_key bootutil_enc_key = {
265274
.key = enc_key,
266275
.len = &enc_key_len,

sim/mcuboot-sys/csupport/run.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ int rsa_oaep_encrypt_(const uint8_t *pubkey, unsigned pubkey_len,
174174
int kw_encrypt_(const uint8_t *kek, const uint8_t *seckey, uint8_t *encbuf)
175175
{
176176
#ifdef MCUBOOT_ENCRYPT_KW
177+
#ifdef MCUBOOT_AES_256
178+
int key_len = 256;
179+
int out_size = 40;
180+
int in_len = 32;
181+
#else
182+
int key_len = 128;
183+
int out_size = 24;
184+
int in_len = 16;
185+
#endif
177186
mbedtls_nist_kw_context kw;
178187
size_t olen;
179188
int rc;
@@ -182,13 +191,13 @@ int kw_encrypt_(const uint8_t *kek, const uint8_t *seckey, uint8_t *encbuf)
182191

183192
mbedtls_nist_kw_init(&kw);
184193

185-
rc = mbedtls_nist_kw_setkey(&kw, MBEDTLS_CIPHER_ID_AES, kek, 128, 1);
194+
rc = mbedtls_nist_kw_setkey(&kw, MBEDTLS_CIPHER_ID_AES, kek, key_len, 1);
186195
if (rc) {
187196
goto done;
188197
}
189198

190-
rc = mbedtls_nist_kw_wrap(&kw, MBEDTLS_KW_MODE_KW, seckey, 16, encbuf,
191-
&olen, 24);
199+
rc = mbedtls_nist_kw_wrap(&kw, MBEDTLS_KW_MODE_KW, seckey, in_len, encbuf,
200+
&olen, out_size);
192201

193202
done:
194203
mbedtls_nist_kw_free(&kw);
@@ -232,7 +241,8 @@ int invoke_boot_go(struct sim_context *ctx, struct area_desc *adesc)
232241

233242
#if defined(MCUBOOT_SIGN_RSA) || \
234243
(defined(MCUBOOT_SIGN_EC256) && defined(MCUBOOT_USE_MBED_TLS)) ||\
235-
(defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS))
244+
(defined(MCUBOOT_ENCRYPT_EC256) && defined(MCUBOOT_USE_MBED_TLS)) ||\
245+
(defined(MCUBOOT_ENCRYPT_X25519) && defined(MCUBOOT_USE_MBED_TLS))
236246
mbedtls_platform_set_calloc_free(calloc, free);
237247
#endif
238248

sim/mcuboot-sys/src/c.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2017-2019 Linaro LTD
22
// Copyright (c) 2017-2019 JUUL Labs
3-
// Copyright (c) 2019 Arm Limited
3+
// Copyright (c) 2019-2021 Arm Limited
44
//
55
// SPDX-License-Identifier: Apache-2.0
66

@@ -67,9 +67,12 @@ pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'sta
6767
}
6868
}
6969

70-
pub fn kw_encrypt(kek: &[u8], seckey: &[u8]) -> Result<[u8; 24], &'static str> {
70+
pub fn kw_encrypt(kek: &[u8], seckey: &[u8], keylen: u32) -> Result<Vec<u8>, &'static str> {
7171
unsafe {
72-
let mut encbuf = [0u8; 24];
72+
let mut encbuf = vec![0u8; 24];
73+
if keylen == 32 {
74+
encbuf = vec![0u8; 40];
75+
}
7376
if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 {
7477
return Ok(encbuf);
7578
}

sim/src/caps.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2017-2019 Linaro LTD
22
// Copyright (c) 2019 JUUL Labs
3-
// Copyright (c) 2019 Arm Limited
3+
// Copyright (c) 2019-2021 Arm Limited
44
//
55
// SPDX-License-Identifier: Apache-2.0
66

@@ -25,6 +25,7 @@ pub enum Caps {
2525
DowngradePrevention = (1 << 12),
2626
EncX25519 = (1 << 13),
2727
Bootstrap = (1 << 14),
28+
Aes256 = (1 << 15),
2829
}
2930

3031
impl Caps {

sim/src/image.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) 2019 Linaro LTD
22
// Copyright (c) 2019-2020 JUUL Labs
3-
// Copyright (c) 2019 Arm Limited
3+
// Copyright (c) 2019-2021 Arm Limited
44
//
55
// SPDX-License-Identifier: Apache-2.0
66

@@ -26,6 +26,7 @@ use std::{
2626
};
2727
use aes_ctr::{
2828
Aes128Ctr,
29+
Aes256Ctr,
2930
stream_cipher::{
3031
generic_array::GenericArray,
3132
NewStreamCipher,
@@ -50,6 +51,7 @@ use crate::depends::{
5051
UpgradeInfo,
5152
};
5253
use crate::tlv::{ManifestGen, TlvGen, TlvFlags};
54+
use typenum::{U32, U16};
5355

5456
/// A builder for Images. This describes a single run of the simulator,
5557
/// capturing the configuration of a particular set of devices, including
@@ -1294,17 +1296,25 @@ fn install_image(flash: &mut SimMultiFlash, slot: &SlotInfo, len: usize,
12941296
tlv.add_bytes(&b_img);
12951297

12961298
// Generate encrypted images
1297-
let flag = TlvFlags::ENCRYPTED as u32;
1298-
let is_encrypted = (tlv.get_flags() & flag) == flag;
1299+
let flag = TlvFlags::ENCRYPTED_AES128 as u32 | TlvFlags::ENCRYPTED_AES256 as u32;
1300+
let is_encrypted = (tlv.get_flags() & flag) != 0;
12991301
let mut b_encimg = vec![];
13001302
if is_encrypted {
1303+
let flag = TlvFlags::ENCRYPTED_AES256 as u32;
1304+
let aes256 = (tlv.get_flags() & flag) == flag;
13011305
tlv.generate_enc_key();
13021306
let enc_key = tlv.get_enc_key();
1303-
let key = GenericArray::from_slice(enc_key.as_slice());
13041307
let nonce = GenericArray::from_slice(&[0; 16]);
1305-
let mut cipher = Aes128Ctr::new(&key, &nonce);
13061308
b_encimg = b_img.clone();
1307-
cipher.apply_keystream(&mut b_encimg);
1309+
if aes256 {
1310+
let key: &GenericArray<u8, U32> = GenericArray::from_slice(enc_key.as_slice());
1311+
let mut cipher = Aes256Ctr::new(&key, &nonce);
1312+
cipher.apply_keystream(&mut b_encimg);
1313+
} else {
1314+
let key: &GenericArray<u8, U16> = GenericArray::from_slice(enc_key.as_slice());
1315+
let mut cipher = Aes128Ctr::new(&key, &nonce);
1316+
cipher.apply_keystream(&mut b_encimg);
1317+
}
13081318
}
13091319

13101320
// Build the TLV itself.
@@ -1408,32 +1418,36 @@ fn make_tlv() -> TlvGen {
14081418
if Caps::EcdsaP224.present() {
14091419
panic!("Ecdsa P224 not supported in Simulator");
14101420
}
1421+
let mut aes_key_size = 128;
1422+
if Caps::Aes256.present() {
1423+
aes_key_size = 256;
1424+
}
14111425

14121426
if Caps::EncKw.present() {
14131427
if Caps::RSA2048.present() {
1414-
TlvGen::new_rsa_kw()
1428+
TlvGen::new_rsa_kw(aes_key_size)
14151429
} else if Caps::EcdsaP256.present() {
1416-
TlvGen::new_ecdsa_kw()
1430+
TlvGen::new_ecdsa_kw(aes_key_size)
14171431
} else {
1418-
TlvGen::new_enc_kw()
1432+
TlvGen::new_enc_kw(aes_key_size)
14191433
}
14201434
} else if Caps::EncRsa.present() {
14211435
if Caps::RSA2048.present() {
1422-
TlvGen::new_sig_enc_rsa()
1436+
TlvGen::new_sig_enc_rsa(aes_key_size)
14231437
} else {
1424-
TlvGen::new_enc_rsa()
1438+
TlvGen::new_enc_rsa(aes_key_size)
14251439
}
14261440
} else if Caps::EncEc256.present() {
14271441
if Caps::EcdsaP256.present() {
1428-
TlvGen::new_ecdsa_ecies_p256()
1442+
TlvGen::new_ecdsa_ecies_p256(aes_key_size)
14291443
} else {
1430-
TlvGen::new_ecies_p256()
1444+
TlvGen::new_ecies_p256(aes_key_size)
14311445
}
14321446
} else if Caps::EncX25519.present() {
14331447
if Caps::Ed25519.present() {
1434-
TlvGen::new_ed25519_ecies_x25519()
1448+
TlvGen::new_ed25519_ecies_x25519(aes_key_size)
14351449
} else {
1436-
TlvGen::new_ecies_x25519()
1450+
TlvGen::new_ecies_x25519(aes_key_size)
14371451
}
14381452
} else {
14391453
// The non-encrypted configuration.

0 commit comments

Comments
 (0)