Added unit test for encrypted external flash

pull/288/head
Daniele Lacamera 2023-03-13 17:10:54 +01:00
parent 51e6a1a28c
commit 0b06efd347
8 changed files with 414 additions and 12 deletions

View File

@ -0,0 +1,33 @@
name: Unit tests
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
footprint_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: make clean
run: |
make keysclean && make -C tools/keytools clean && rm -f include/target.h
- name: Build wolfboot and test footprint
run: |
make -C tools/unit-tests
- name: Run manifest header parser unit tests
run: |
./tools/unit-tests/unit-parser
- name: Run encrypted ext_flash unit tests
run: |
./tools/unit-tests/unit-extflash

View File

@ -23,7 +23,7 @@
#ifndef ENCRYPT_H_INCLUDED
#define ENCRYPT_H_INCLUDED
#ifdef __WOLFBOOT
#if defined(__WOLFBOOT) || defined(UNIT_TEST)
#include <stdint.h>
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/sha256.h>
@ -67,5 +67,5 @@ void aes_set_iv(uint8_t *nonce, uint32_t address);
int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len);
int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len);
#endif /* __WOLFBOOT */
#endif /* __WOLFBOOT || UNIT_TEST */
#endif /* ENCRYPT_H_INCLUDED */

View File

@ -579,7 +579,7 @@ uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr);
#include "hal.h"
#if defined(EXT_ENCRYPTED) && defined(__WOLFBOOT)
#if defined(EXT_ENCRYPTED) && (defined(__WOLFBOOT) || defined(UNIT_TEST))
#include "encrypt.h"
#define ext_flash_check_write ext_flash_encrypt_write
#define ext_flash_check_read ext_flash_decrypt_read

View File

@ -44,7 +44,7 @@ static int encrypt_initialized = 0;
static uint8_t encrypt_iv_nonce[ENCRYPT_NONCE_SIZE];
#if defined(__WOLFBOOT)
#include "encrypt.h"
#else
#elif !defined(XMEMSET)
#include <string.h>
#define XMEMSET memset
#define XMEMCPY memcpy
@ -63,7 +63,7 @@ static uint8_t encrypt_iv_nonce[ENCRYPT_NONCE_SIZE];
#define ENCRYPT_TMP_SECRET_OFFSET (WOLFBOOT_PARTITION_SIZE - (TRAILER_SKIP))
#endif /* EXT_FLASH && EXT_ENCRYPTED */
#if !defined(__WOLFBOOT)
#if !defined(__WOLFBOOT) && !defined(UNIT_TEST)
#define XMEMSET memset
#define XMEMCPY memcpy
#define XMEMCMP memcmp
@ -89,7 +89,7 @@ static uint32_t ext_cache;
#endif
#ifdef __WOLFBOOT
#if defined(__WOLFBOOT) || defined (UNIT_TEST)
/* Inline use of ByteReverseWord32 */
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
@ -969,6 +969,7 @@ int RAMFUNCTION wolfBoot_set_encrypt_key(const uint8_t *key,
return 0;
}
#ifndef UNIT_TEST
int RAMFUNCTION wolfBoot_get_encrypt_key(uint8_t *k, uint8_t *nonce)
{
#if defined(MMU)
@ -987,6 +988,7 @@ int RAMFUNCTION wolfBoot_get_encrypt_key(uint8_t *k, uint8_t *nonce)
#endif
return 0;
}
#endif
int RAMFUNCTION wolfBoot_erase_encrypt_key(void)
{
@ -1008,7 +1010,7 @@ int RAMFUNCTION wolfBoot_erase_encrypt_key(void)
return 0;
}
#ifdef __WOLFBOOT
#if defined(__WOLFBOOT) || defined(UNIT_TEST)
#ifdef ENCRYPT_WITH_CHACHA
@ -1048,7 +1050,7 @@ Aes aes_dec, aes_enc;
int aes_init(void)
{
#if defined(MMU)
#if defined(MMU) || defined(UNIT_TEST)
uint8_t *key = ENCRYPT_KEY;
#else
uint8_t *key = (uint8_t *)(WOLFBOOT_PARTITION_BOOT_ADDRESS +

View File

@ -5,16 +5,24 @@ ifneq ($(UNAME_S),Darwin)
LDFLAGS+=-lrt -lsubunit
endif
CFLAGS=-I../../src -I../../include
CFLAGS=-I. -I../../src -I../../include -I../../lib/wolfssl
CFLAGS+=-g -ggdb
all: unit-parser
all: unit-parser unit-extflash
../../include/target.h: FORCE
cp -f target.h $@
unit-parser: unit-parser.o
unit-parser: ../../include/target.h unit-parser.o
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
unit-extflash: ../../include/target.h unit-extflash.o
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
%.o:%.c
gcc -c -o $@ $^ $(CFLAGS)
clean:
rm -f unit-parser unit-parser.o
rm -f unit-parser unit-extflash *.o
FORCE:

View File

@ -0,0 +1,71 @@
/* target.h
*
* User configurable build-time options for bootloader and application offsets
*
* target.h is automatically generated using the template in target.h.in by running
* "make config".
*
* Copyright (C) 2021 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef H_TARGETS_TARGET_
#define H_TARGETS_TARGET_
#ifndef WOLFBOOT_NO_PARTITIONS
# define WOLFBOOT_FIXED_PARTITIONS
#endif
#define WOLFBOOT_SECTOR_SIZE 0x400
#ifdef WOLFBOOT_FIXED_PARTITIONS
#ifdef PULL_LINKER_DEFINES
#include <stdint.h>
/* linker script variables */
extern const uint32_t _wolfboot_partition_boot_address[];
extern const uint32_t _wolfboot_partition_size[];
extern const uint32_t _wolfboot_partition_update_address[];
extern const uint32_t _wolfboot_partition_swap_address[];
/* create plain integers from linker script variables */
static const uint32_t WOLFBOOT_PARTITION_BOOT_ADDRESS = (uint32_t)_wolfboot_partition_boot_address;
static const uint32_t WOLFBOOT_PARTITION_SIZE = (uint32_t)_wolfboot_partition_size;
static const uint32_t WOLFBOOT_PARTITION_UPDATE_ADDRESS = (uint32_t)_wolfboot_partition_update_address;
static const uint32_t WOLFBOOT_PARTITION_SWAP_ADDRESS = (uint32_t)_wolfboot_partition_swap_address;
#else
/* use values provided on input template parsing */
#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0x08000000
#define WOLFBOOT_PARTITION_SIZE 0x8000
#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0x00000000
#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0x00008000
#endif
#define WOLFBOOT_DTS_BOOT_ADDRESS 0x30000
#define WOLFBOOT_DTS_UPDATE_ADDRESS 0x50000
#endif /* WOLFBOOT_FIXED_PARTITIONS */
/* Load address in RAM for staged OS (update_ram only) */
#define WOLFBOOT_LOAD_ADDRESS 0x200000
#define WOLFBOOT_LOAD_DTS_ADDRESS 0x400000
#endif /* !H_TARGETS_TARGET_ */

View File

@ -0,0 +1,287 @@
/* unit-extflash.c
*
* Unit test for parser functions in libwolfboot.c
*
*
* Copyright (C) 2023 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Option to enable sign tool debugging */
/* Must also define DEBUG_WOLFSSL in user_settings.h */
#define FLASH_SIZE (33 * 1024)
#define WOLFBOOT_HASH_SHA256
#define IMAGE_HEADER_SIZE 256
#define EXT_ENCRYPTED
#define EXT_FLASH
#define ENCRYPT_WITH_AES256
#define WOLFSSL_AES_COUNTER
#define WOLFSSL_AES_DIRECT
#define WOLFSSL_AES_256
#define WC_NO_HARDEN
#define WOLFSSL_USER_SETTINGS
#define ENCRYPT_KEY "123456789abcdef0123456789abcdef0123456789abcdef"
#define UNIT_TEST
#include <stdio.h>
#include <check.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "user_settings.h"
#include "wolfcrypt/src/aes.c"
#include "libwolfboot.c"
/* Mocks */
static int locked = 0;
void hal_init(void)
{
}
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
return 0;
}
int hal_flash_erase(uint32_t address, int len)
{
return 0;
}
void hal_flash_unlock(void)
{
//fail_unless(locked, "Double unlock detected\n");
locked--;
}
void hal_flash_lock(void)
{
//fail_if(locked, "Double lock detected\n");
locked++;
}
void hal_prepare_boot(void)
{
}
/* Emulation of external flash with a static buffer of 32KB (update) + 1KB (swap) */
uint8_t flash[FLASH_SIZE];
/* Mocks for ext_flash_read, ext_flash_write, and ext_flash_erase functions */
int ext_flash_read(uintptr_t address, uint8_t *data, int len) {
printf("Called ext_flash_read %p %p %d\n", address, data, len);
/* Check that the read address and size are within the bounds of the flash memory */
ck_assert_int_le(address + len, FLASH_SIZE);
/* Copy the data from the flash memory to the output buffer */
memcpy(data, &flash[address], len);
return len;
}
int ext_flash_write(uintptr_t address, const uint8_t *data, int len) {
printf("Called ext_flash_write %p %p %d\n", address, data, len);
/* Check that the write address and size are within the bounds of the flash memory */
ck_assert_int_le(address + len, FLASH_SIZE);
/* Copy the data from the input buffer to the flash memory */
memcpy(&flash[address], data, len);
return 0;
}
int ext_flash_erase(uintptr_t address, int len) {
/* Check that the erase address and size are within the bounds of the flash memory */
ck_assert_int_le(address + len, FLASH_SIZE);
/* Erase the flash memory by setting each byte to 0xFF, WOLFBOOT_SECTOR_SIZE bytes at a time */
uint32_t i;
for (i = address; i < address + len; i += WOLFBOOT_SECTOR_SIZE) {
memset(&flash[i], 0xFF, WOLFBOOT_SECTOR_SIZE);
}
return 0;
}
/* Matches all keys:
* - chacha (32 + 12)
* - aes128 (16 + 16)
* - aes256 (32 + 16)
*/
/* Longest key possible: AES256 (32 key + 16 IV = 48) */
static const char enc_key[] = "0123456789abcdef0123456789abcdef"
"0123456789abcdef";
static uint8_t test_buffer[512] = {
'W', 'O', 'L', 'F', 0x00, 0x00, 0x01, 0x00,
0x01, 0x00, 0x04, 0x00, 0x0d, 0x0c, 0x0b, 0x0a,
0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x08, 0x00,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x20, 0x00,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /*<-- end of options */
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
/* End HDR */
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
};
int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce)
{
printf("Called get_encrypt_key mock\r\n");
memcpy(key, enc_key, ENCRYPT_KEY_SIZE);
memcpy(key, enc_key + ENCRYPT_KEY_SIZE, ENCRYPT_NONCE_SIZE);
return 0;
}
START_TEST(test_ext_flash_operations) {
uint32_t address = 0x1000;
uint32_t size = 512;
uint8_t data[2 * WOLFBOOT_SECTOR_SIZE];
uint8_t empty_sector[WOLFBOOT_SECTOR_SIZE];
int rres, wres, eres;
memset(empty_sector, 0xFF, WOLFBOOT_SECTOR_SIZE);
/* Write data to the flash memory */
wres = ext_flash_write(address, test_buffer, size);
ck_assert_int_eq(wres, 0);
/* Read data from the flash memory */
rres = ext_flash_read(address, data, size);
ck_assert_int_eq(rres, size);
/* Check that the data read from the flash memory matches the data that was written */
ck_assert_mem_eq(data, test_buffer, size);
/* Erase the first sector */
eres = ext_flash_erase(address, WOLFBOOT_SECTOR_SIZE);
ck_assert_int_eq(eres, 0);
/* Read first sector from the flash memory */
rres = ext_flash_read(address, data, WOLFBOOT_SECTOR_SIZE);
ck_assert_int_eq(rres, WOLFBOOT_SECTOR_SIZE);
/* Check that the first sector read is empty */
ck_assert_mem_eq(data, empty_sector, WOLFBOOT_SECTOR_SIZE);
}
END_TEST
START_TEST(test_ext_enc_flash_operations) {
uint32_t address = 0x1000;
uint32_t size = 512;
uint8_t data[2 * WOLFBOOT_SECTOR_SIZE];
uint8_t dataw[2 * WOLFBOOT_SECTOR_SIZE];
int rres, wres, eres;
/* Write data to the flash memory */
memcpy(dataw, test_buffer, size);
wres = ext_flash_check_write(address, dataw, size);
ck_assert_int_eq(wres, 0);
/* Read data from the flash memory */
rres = ext_flash_check_read(address, data, size);
ck_assert_int_eq(rres, size);
/* Check that the data read from the flash memory matches the data that was written */
ck_assert_mem_eq(data, test_buffer, size);
address = 0x07FF0;
size = 16;
/* Write data to the flash memory */
memcpy(dataw, test_buffer, size);
wres = ext_flash_check_write(address, dataw, size);
ck_assert_int_eq(wres, 0);
/* Read data from the flash memory */
rres = ext_flash_check_read(address, data, size);
ck_assert_int_eq(rres, size);
/* Check that the data read from the flash memory matches the data that was written */
ck_assert_mem_eq(&flash[address], test_buffer, size);
}
END_TEST
/* End Mocks */
Suite *wolfboot_suite(void)
{
/* Suite initialization */
Suite *s = suite_create("wolfBoot");
/* Test cases */
TCase *ext_flash_operations = tcase_create("External flash operations: API");
TCase *ext_enc_flash_operations = tcase_create("External encrypted flash operations");
/* Set parameters + add to suite */
tcase_add_test(ext_flash_operations, test_ext_flash_operations);
tcase_add_test(ext_enc_flash_operations, test_ext_enc_flash_operations);
tcase_set_timeout(ext_flash_operations, 20);
tcase_set_timeout(ext_enc_flash_operations, 20);
suite_add_tcase(s, ext_flash_operations);
suite_add_tcase(s, ext_enc_flash_operations);
return s;
}
int main(void)
{
int fails;
Suite *s = wolfboot_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
fails = srunner_ntests_failed(sr);
srunner_free(sr);
return fails;
}

View File

@ -27,6 +27,7 @@
#define WOLFBOOT_HASH_SHA256
#define IMAGE_HEADER_SIZE 256
#define UNIT_TEST
#define WC_NO_HARDEN
#include <stdio.h>
#include "libwolfboot.c"
#include <check.h>