Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ tools/unit-tests/unit-image-dts-sha3-384
tools/unit-tests/unit-store-sbrk
tools/unit-tests/unit-tpm-blob
tools/unit-tests/unit-update-disk
tools/unit-tests/unit-update-disk-aes-iv-carry
tools/unit-tests/unit-policy-sign
tools/unit-tests/unit-fdt
tools/unit-tests/unit-hal-otp
Expand Down
35 changes: 20 additions & 15 deletions src/update_disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,25 +287,30 @@ static void disk_crypto_set_iv(uint32_t block_offset)
#if defined(ENCRYPT_WITH_CHACHA)
wc_Chacha_SetIV(&chacha, disk_encrypt_nonce, block_offset);
#elif defined(ENCRYPT_WITH_AES128) || defined(ENCRYPT_WITH_AES256)
/* For AES CTR, we need to construct the IV with the counter.
* The sign tool uses the IV directly without byte-reversal,
* so we must match that behavior here. */
/* For AES-CTR, wolfCrypt (and tools/keytools/sign.c) increments the full
* 128-bit big-endian counter across all 16 bytes. Add block_offset with
* an unconditional, branch-free carry across all four 32-bit big-endian
* words (matching aes_set_iv() in src/libwolfboot.c). */
uint8_t iv[ENCRYPT_BLOCK_SIZE];
uint32_t ctr;
uint32_t word, prev, carry;
int i;

/* Copy nonce/IV (first 12 bytes for CTR nonce, last 4 for counter) */
memcpy(iv, disk_encrypt_nonce, ENCRYPT_NONCE_SIZE);

/* Add block offset to the counter portion (last 4 bytes, big-endian) */
/* The IV from sign.c is already in the correct format, we just need
* to add the block offset to the counter portion */
ctr = ((uint32_t)iv[12] << 24) | ((uint32_t)iv[13] << 16) |
((uint32_t)iv[14] << 8) | (uint32_t)iv[15];
ctr += block_offset;
iv[12] = (uint8_t)(ctr >> 24);
iv[13] = (uint8_t)(ctr >> 16);
iv[14] = (uint8_t)(ctr >> 8);
iv[15] = (uint8_t)(ctr);
carry = block_offset;
for (i = 3; i >= 0; i--) {
int off = i * 4;
prev = ((uint32_t)iv[off] << 24) |
((uint32_t)iv[off + 1] << 16) |
((uint32_t)iv[off + 2] << 8) |
((uint32_t)iv[off + 3]);
word = prev + carry;
carry = (uint32_t)(word < prev);
iv[off] = (uint8_t)(word >> 24);
iv[off + 1] = (uint8_t)(word >> 16);
iv[off + 2] = (uint8_t)(word >> 8);
iv[off + 3] = (uint8_t)(word);
}

wc_AesSetIV(&aes_dec, iv);
/* Scrub the stack copy: the counter bytes are derived from the
Expand Down
11 changes: 10 additions & 1 deletion tools/unit-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ TESTS:=unit-parser unit-parser-large-header unit-fdt unit-extflash unit-string \
unit-update-flash-self-update-monolithic \
unit-nsc-update \
unit-update-flash-enc unit-update-flash-enc-cache unit-update-flash-enc-full unit-update-flash-enc-intswap unit-update-ram unit-update-ram-fit unit-update-ram-uboot unit-update-ram-enc unit-update-ram-enc-start unit-update-ram-enc-nopart unit-update-ram-nofixed unit-update-ram-nofixed-noramboot unit-update-ram-noramboot unit-update-ram-custom-trailer unit-update-ram-rollback-unconfirmed unit-compressed-tlv unit-custom-trailer-nopart unit-update-flash-hwswap unit-update-flash-custom-trailer unit-pkcs11_store unit-psa_store unit-wolfhsm_flash_hal unit-disk \
unit-update-disk unit-update-disk-confirm unit-update-disk-golden unit-update-disk-failover unit-update-disk-golden-enckey unit-update-disk-fsp unit-update-disk-oob unit-update-disk-fit unit-update-disk-fit-ramdisk unit-multiboot unit-boot-x86-fsp unit-loader-tpm-init unit-qspi-flash unit-fwtpm-stub unit-tpm-rsa-exp \
unit-update-disk unit-update-disk-confirm unit-update-disk-golden unit-update-disk-failover unit-update-disk-golden-enckey unit-update-disk-aes-iv-carry unit-update-disk-fsp unit-update-disk-oob unit-update-disk-fit unit-update-disk-fit-ramdisk unit-multiboot unit-boot-x86-fsp unit-loader-tpm-init unit-qspi-flash unit-fwtpm-stub unit-tpm-rsa-exp \
unit-image-nopart unit-image-sha384 unit-image-sha3-384 unit-image-dts \
unit-image-dts-sha384 unit-image-dts-sha3-384 unit-store-sbrk \
unit-tpm-blob unit-policy-create unit-policy-sign unit-rot-auth unit-sdhci-response-bits \
Expand Down Expand Up @@ -663,6 +663,9 @@ unit-update-disk-failover:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_
-DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE
unit-update-disk-golden-enckey:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \
-DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE
unit-update-disk-aes-iv-carry:CFLAGS+=-DMOCK_PARTITIONS -DPRINTF_ENABLED -DWOLFBOOT_RAMBOOT_MAX_SIZE=0x400 \
-DEXT_ENCRYPTED -DENCRYPT_WITH_AES256 -DWOLFSSL_AES_DIRECT -DWOLFSSL_AES_256 -DWOLFSSL_AES_COUNTER \
-DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE
# Non-FSP disk-boot OOB regression (CRIT-03). WOLFBOOT_RAMBOOT_MAX_SIZE is the
# cap the loader applies to the unauthenticated header fw_size before loading to
# RAM, the same bound update_disk.c and update_ram.c enforce.
Expand Down Expand Up @@ -1607,6 +1610,12 @@ unit-update-disk-failover: ../../include/target.h unit-update-disk-failover.c
unit-update-disk-golden-enckey: ../../include/target.h unit-update-disk-golden-enckey.c
gcc -o $@ unit-update-disk-golden-enckey.c $(CFLAGS) $(LDFLAGS)

unit-update-disk-aes-iv-carry: ../../include/target.h unit-update-disk-aes-iv-carry.c
gcc -o $@ unit-update-disk-aes-iv-carry.c \
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/aes.c \
$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/memory.c \
$(CFLAGS) $(LDFLAGS)

# WOLFBOOT_FSP (x86) boot path of update_disk.c: the low-memory (tolum)
# size check must reject a slot and fall back to the other one, like every
# other per-slot rejection in the retry loop.
Expand Down
Loading
Loading