Skip to content

Commit e312fa2

Browse files
committed
synch with upstream 296949e
Synchronized up to: https://github.com/JuulLabs-OSS/mcuboot/commit/@296949e Improvements: - hardening against hardware level fault injection and timing attacks - Abstract crypto primitives to simplify porting. - boot: Add ram-load upgrade mode - renamed single-image mode to single-slot mode - kconfig: provide logic for setting key file, simplify prj.conf - imgtool: Print image digest during verify - imgtool: Add possibility to set confirm flag for hex files as well - imgtool: --confirm implies --pad - Added single-slot Zephyr-RTOS test build fixes: - bootutil: fix boostrapping in swap-move - bootutil: fix swap-move brick with padded image_0 - Disable HW stack protection (temporary hack) - reset SPLIM registers before boot - fixes build issue that occurs if CONF_FILE contains multiple file paths instead of single file path. - imgtool: Fix 'custom_tlvs' argument handling - Turn off cache for Cortex M7 before chain-loading.- hardening against hardware level fault injection and timing attacks Conflicts: - took upsteram boot/zephyr/sample.yaml Signed-off-by: Andrzej Puzdrowski <[email protected]>
2 parents e8bfc03 + 296949e commit e312fa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+3162
-1009
lines changed

.github/workflows/mynewt.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# For development, trigger this on any push.
2+
on: [pull_request]
3+
4+
jobs:
5+
environment:
6+
name: Mynewt build
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
with:
11+
fetch-depth: 0
12+
- name: Print the environment
13+
run: |
14+
uname -a
15+
lscpu
16+
free
17+
pwd
18+
- name: Signed commit check
19+
run: |
20+
./ci/check-signed-off-by.sh
21+
- name: Mynewt install
22+
run: |
23+
./ci/mynewt_install.sh
24+
- name: Mynewt run
25+
run: |
26+
./ci/mynewt_run.sh

.github/workflows/sim.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# For development, trigger this on any push.
2+
on: [pull_request]
3+
4+
jobs:
5+
environment:
6+
strategy:
7+
matrix:
8+
features:
9+
- "sig-ecdsa,sig-ed25519,enc-kw,bootstrap"
10+
- "sig-rsa,sig-rsa3072,overwrite-only,validate-primary-slot,swap-move"
11+
- "enc-rsa"
12+
- "enc-ec256"
13+
- "enc-x25519"
14+
- "sig-rsa overwrite-only large-write,sig-ecdsa overwrite-only large-write,multiimage overwrite-only large-write"
15+
- "sig-rsa validate-primary-slot,sig-ecdsa validate-primary-slot,sig-rsa multiimage validate-primary-slot"
16+
- "enc-kw overwrite-only large-write,enc-rsa overwrite-only large-write"
17+
- "sig-rsa enc-rsa validate-primary-slot,swap-move enc-rsa sig-rsa validate-primary-slot bootstrap"
18+
- "sig-rsa enc-kw validate-primary-slot bootstrap,sig-ed25519 enc-x25519 validate-primary-slot"
19+
- "sig-ecdsa enc-kw validate-primary-slot"
20+
- "sig-rsa validate-primary-slot overwrite-only large-write"
21+
- "sig-ecdsa enc-ec256 validate-primary-slot"
22+
- "sig-rsa validate-primary-slot overwrite-only downgrade-prevention"
23+
name: Sim
24+
runs-on: ubuntu-latest
25+
env:
26+
MULTI_FEATURES: ${{ matrix.features }}
27+
steps:
28+
- uses: actions/checkout@v2
29+
with:
30+
fetch-depth: 0
31+
submodules: recursive
32+
- name: Print the environment
33+
run: |
34+
uname -a
35+
lscpu
36+
free
37+
pwd
38+
- name: Signed commit check
39+
run: |
40+
./ci/check-signed-off-by.sh
41+
- name: Install stable Rust
42+
uses: actions-rs/toolchain@v1
43+
with:
44+
toolchain: stable
45+
- name: Sim install
46+
run: |
47+
./ci/sim_install.sh
48+
- name: Sim run
49+
run: |
50+
./ci/sim_run.sh

.travis.yml

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,39 @@ matrix:
1616
include:
1717
# Runs each value defined in $SINGLE_FEATURES by itself in the order
1818
# the were defined.
19-
- os: linux
20-
env: SINGLE_FEATURES="sig-ecdsa sig-ed25519 enc-kw bootstrap" TEST=sim
21-
- os: linux
22-
env: SINGLE_FEATURES="none sig-rsa sig-rsa3072 overwrite-only validate-primary-slot swap-move" TEST=sim
23-
- os: linux
24-
env: SINGLE_FEATURES="enc-rsa enc-ec256 enc-x25519" TEST=sim
19+
# - os: linux
20+
# env: SINGLE_FEATURES="sig-ecdsa sig-ed25519 enc-kw bootstrap" TEST=sim
21+
# - os: linux
22+
# env: SINGLE_FEATURES="none sig-rsa sig-rsa3072 overwrite-only validate-primary-slot swap-move" TEST=sim
23+
# - os: linux
24+
# env: SINGLE_FEATURES="enc-rsa enc-ec256 enc-x25519" TEST=sim
2525

2626
# Values defined in $MULTI_FEATURES consist of any number of features
2727
# to be enabled at the same time. The list of multi-values should be
2828
# separated by ',' and each list of values is run sequentially in the
2929
# defined order.
30-
- os: linux
31-
env: MULTI_FEATURES="sig-rsa overwrite-only large-write,sig-ecdsa overwrite-only large-write,multiimage overwrite-only large-write" TEST=sim
32-
- os: linux
33-
env: MULTI_FEATURES="sig-rsa validate-primary-slot,sig-ecdsa validate-primary-slot,sig-rsa multiimage validate-primary-slot" TEST=sim
34-
- os: linux
35-
env: MULTI_FEATURES="enc-kw overwrite-only large-write,enc-rsa overwrite-only large-write" TEST=sim
36-
- os: linux
37-
env: MULTI_FEATURES="sig-rsa enc-rsa validate-primary-slot,swap-move enc-rsa sig-rsa validate-primary-slot" TEST=sim
38-
- os: linux
39-
env: MULTI_FEATURES="sig-rsa enc-kw validate-primary-slot bootstrap,sig-ed25519 enc-x25519 validate-primary-slot" TEST=sim
40-
- os: linux
41-
env: MULTI_FEATURES="sig-ecdsa enc-kw validate-primary-slot" TEST=sim
42-
- os: linux
43-
env: MULTI_FEATURES="sig-rsa validate-primary-slot overwrite-only large-write,sig-ecdsa enc-ec256 validate-primary-slot" TEST=sim
44-
- os: linux
45-
env: MULTI_FEATURES="sig-rsa validate-primary-slot overwrite-only downgrade-prevention" TEST=sim
30+
# - os: linux
31+
# env: MULTI_FEATURES="sig-rsa overwrite-only large-write,sig-ecdsa overwrite-only large-write,multiimage overwrite-only large-write" TEST=sim
32+
# - os: linux
33+
# env: MULTI_FEATURES="sig-rsa validate-primary-slot,sig-ecdsa validate-primary-slot,sig-rsa multiimage validate-primary-slot" TEST=sim
34+
# - os: linux
35+
# env: MULTI_FEATURES="enc-kw overwrite-only large-write,enc-rsa overwrite-only large-write" TEST=sim
36+
# - os: linux
37+
# env: MULTI_FEATURES="sig-rsa enc-rsa validate-primary-slot,swap-move enc-rsa sig-rsa validate-primary-slot" TEST=sim
38+
# - os: linux
39+
# env: MULTI_FEATURES="sig-rsa enc-kw validate-primary-slot bootstrap,sig-ed25519 enc-x25519 validate-primary-slot" TEST=sim
40+
# - os: linux
41+
# env: MULTI_FEATURES="sig-ecdsa enc-kw validate-primary-slot" TEST=sim
42+
# - os: linux
43+
# env: MULTI_FEATURES="sig-rsa validate-primary-slot overwrite-only large-write,sig-ecdsa enc-ec256 validate-primary-slot" TEST=sim
44+
# - os: linux
45+
# env: MULTI_FEATURES="sig-rsa validate-primary-slot overwrite-only downgrade-prevention" TEST=sim
4646

47-
- os: linux
48-
language: go
49-
env: TEST=mynewt
50-
go:
51-
- "1.12"
47+
# - os: linux
48+
# language: go
49+
# env: TEST=mynewt
50+
# go:
51+
# - "1.12"
5252

5353
- os: linux
5454
language: python

boot/bootutil/include/bootutil/bootutil.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2017-2019 Linaro LTD
55
* Copyright (c) 2016-2019 JUUL Labs
6-
* Copyright (c) 2019 Arm Limited
6+
* Copyright (c) 2019-2020 Arm Limited
77
*
88
* Original license:
99
*
@@ -29,6 +29,7 @@
2929
#define H_BOOTUTIL_
3030

3131
#include <inttypes.h>
32+
#include "bootutil/fault_injection_hardening.h"
3233

3334
#ifdef __cplusplus
3435
extern "C" {
@@ -91,10 +92,10 @@ struct image_trailer {
9192
};
9293

9394
/* you must have pre-allocated all the entries within this structure */
94-
int boot_go(struct boot_rsp *rsp);
95+
fih_int boot_go(struct boot_rsp *rsp);
9596

9697
struct boot_loader_state;
97-
int context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp);
98+
fih_int context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp);
9899

99100
int boot_swap_type_multi(int image_index);
100101
int boot_swap_type(void);
@@ -105,8 +106,8 @@ int boot_set_confirmed(void);
105106
#define SPLIT_GO_OK (0)
106107
#define SPLIT_GO_NON_MATCHING (-1)
107108
#define SPLIT_GO_ERR (-2)
108-
int
109-
split_go(int loader_slot, int split_slot, void **entry);
109+
110+
fih_int split_go(int loader_slot, int split_slot, void **entry);
110111

111112
#ifdef __cplusplus
112113
}

boot/bootutil/include/bootutil/caps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ uint32_t bootutil_get_caps(void);
4646
#define BOOTUTIL_CAP_SWAP_USING_MOVE (1<<11)
4747
#define BOOTUTIL_CAP_DOWNGRADE_PREVENTION (1<<12)
4848
#define BOOTUTIL_CAP_ENC_X25519 (1<<13)
49+
#define BOOTUTIL_CAP_BOOTSTRAP (1<<14)
4950

5051
/*
5152
* Query the number of images this bootloader is configured for. This
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* This module provides a thin abstraction over some of the crypto
3+
* primitives to make it easier to swap out the used crypto library.
4+
*
5+
* At this point, there are two choices: MCUBOOT_USE_MBED_TLS, or
6+
* MCUBOOT_USE_TINYCRYPT. It is a compile error there is not exactly
7+
* one of these defined.
8+
*/
9+
10+
#ifndef __BOOTUTIL_CRYPTO_AES_CTR_H_
11+
#define __BOOTUTIL_CRYPTO_AES_CTR_H_
12+
13+
#include <string.h>
14+
15+
#include "mcuboot_config/mcuboot_config.h"
16+
17+
#if (defined(MCUBOOT_USE_MBED_TLS) + \
18+
defined(MCUBOOT_USE_TINYCRYPT)) != 1
19+
#error "One crypto backend must be defined: either MBED_TLS or TINYCRYPT"
20+
#endif
21+
22+
#if defined(MCUBOOT_USE_MBED_TLS)
23+
#include <mbedtls/aes.h>
24+
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE (16)
25+
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE (16)
26+
#endif /* MCUBOOT_USE_MBED_TLS */
27+
28+
#if defined(MCUBOOT_USE_TINYCRYPT)
29+
#include <string.h>
30+
#include <tinycrypt/aes.h>
31+
#include <tinycrypt/ctr_mode.h>
32+
#include <tinycrypt/constants.h>
33+
#define BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE TC_AES_KEY_SIZE
34+
#define BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE TC_AES_BLOCK_SIZE
35+
#endif /* MCUBOOT_USE_TINYCRYPT */
36+
37+
#include <stdint.h>
38+
39+
#ifdef __cplusplus
40+
extern "C" {
41+
#endif
42+
43+
#if defined(MCUBOOT_USE_MBED_TLS)
44+
typedef mbedtls_aes_context bootutil_aes_ctr_context;
45+
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
46+
{
47+
(void)mbedtls_aes_init(ctx);
48+
}
49+
50+
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
51+
{
52+
/* XXX: config defines MBEDTLS_PLATFORM_NO_STD_FUNCTIONS so no need to free */
53+
/* (void)mbedtls_aes_free(ctx); */
54+
(void)ctx;
55+
}
56+
57+
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
58+
{
59+
return mbedtls_aes_setkey_enc(ctx, k, BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE * 8);
60+
}
61+
62+
static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, size_t blk_off, uint8_t *c)
63+
{
64+
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
65+
int rc;
66+
rc = mbedtls_aes_crypt_ctr(ctx, mlen, &blk_off, counter, stream_block, m, c);
67+
memset(stream_block, 0, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
68+
return rc;
69+
}
70+
71+
static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, size_t blk_off, uint8_t *m)
72+
{
73+
uint8_t stream_block[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
74+
int rc;
75+
rc = mbedtls_aes_crypt_ctr(ctx, clen, &blk_off, counter, stream_block, c, m);
76+
memset(stream_block, 0, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
77+
return rc;
78+
}
79+
#endif /* MCUBOOT_USE_MBED_TLS */
80+
81+
#if defined(MCUBOOT_USE_TINYCRYPT)
82+
typedef struct tc_aes_key_sched_struct bootutil_aes_ctr_context;
83+
static inline void bootutil_aes_ctr_init(bootutil_aes_ctr_context *ctx)
84+
{
85+
(void)ctx;
86+
}
87+
88+
static inline void bootutil_aes_ctr_drop(bootutil_aes_ctr_context *ctx)
89+
{
90+
(void)ctx;
91+
}
92+
93+
static inline int bootutil_aes_ctr_set_key(bootutil_aes_ctr_context *ctx, const uint8_t *k)
94+
{
95+
int rc;
96+
rc = tc_aes128_set_encrypt_key(ctx, k);
97+
if (rc != TC_CRYPTO_SUCCESS) {
98+
return -1;
99+
}
100+
return 0;
101+
}
102+
103+
static int _bootutil_aes_ctr_crypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *in, uint32_t inlen, uint32_t blk_off, uint8_t *out)
104+
{
105+
uint8_t buf[16];
106+
uint32_t buflen;
107+
int rc;
108+
if (blk_off == 0) {
109+
rc = tc_ctr_mode(out, inlen, in, inlen, counter, ctx);
110+
if (rc != TC_CRYPTO_SUCCESS) {
111+
return -1;
112+
}
113+
} else if (blk_off < 16) {
114+
buflen = ((inlen + blk_off <= 16) ? inlen : (16 - blk_off));
115+
inlen -= buflen;
116+
memcpy(&buf[blk_off], &in[0], buflen);
117+
rc = tc_ctr_mode(buf, 16, buf, 16, counter, ctx);
118+
if (rc != TC_CRYPTO_SUCCESS) {
119+
return -1;
120+
}
121+
memcpy(&out[0], &buf[blk_off], buflen);
122+
memset(&buf[0], 0, 16);
123+
if (inlen > 0) {
124+
rc = tc_ctr_mode(&out[buflen], inlen, &in[buflen], inlen, counter, ctx);
125+
}
126+
if (rc != TC_CRYPTO_SUCCESS) {
127+
return -1;
128+
}
129+
} else {
130+
return -1;
131+
}
132+
return 0;
133+
}
134+
135+
static inline int bootutil_aes_ctr_encrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *m, uint32_t mlen, uint32_t blk_off, uint8_t *c)
136+
{
137+
return _bootutil_aes_ctr_crypt(ctx, counter, m, mlen, blk_off, c);
138+
}
139+
140+
static inline int bootutil_aes_ctr_decrypt(bootutil_aes_ctr_context *ctx, uint8_t *counter, const uint8_t *c, uint32_t clen, uint32_t blk_off, uint8_t *m)
141+
{
142+
return _bootutil_aes_ctr_crypt(ctx, counter, c, clen, blk_off, m);
143+
}
144+
#endif /* MCUBOOT_USE_TINYCRYPT */
145+
146+
#ifdef __cplusplus
147+
}
148+
#endif
149+
150+
#endif /* __BOOTUTIL_CRYPTO_AES_CTR_H_ */

0 commit comments

Comments
 (0)