bdb: Integrate bdb_test

This patch integrate bdb_test with the root Makefile.

BUG=chrome-os-partner:51908
BRANCH=tot
TEST=make runtests

Change-Id: I6266199d8f3068d2e9349110e8351f20d027ab35
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/342091
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Daisuke Nojiri
2016-05-03 13:24:10 -07:00
committed by chrome-bot
parent ef4ae8953c
commit 825a2f8c73
2 changed files with 150 additions and 108 deletions

View File

@@ -302,6 +302,8 @@ FWLIB20 = ${BUILD}/vboot_fw20.a
# Vboot 2.1 (not yet ready - see firmware/README) # Vboot 2.1 (not yet ready - see firmware/README)
FWLIB21 = ${BUILD}/vboot_fw21.a FWLIB21 = ${BUILD}/vboot_fw21.a
BDBLIB = ${BUILD}/bdb.a
# Firmware library sources needed by VbInit() call # Firmware library sources needed by VbInit() call
VBINIT_SRCS = \ VBINIT_SRCS = \
firmware/lib/crc8.c \ firmware/lib/crc8.c \
@@ -370,6 +372,11 @@ FWLIB21_SRCS = \
firmware/lib21/misc.c \ firmware/lib21/misc.c \
firmware/lib21/packed_key.c firmware/lib21/packed_key.c
BDBLIB_SRCS = \
firmware/bdb/bdb.c \
firmware/bdb/sha.c \
firmware/bdb/rsa.c
# Support real TPM unless BIOS sets MOCK_TPM # Support real TPM unless BIOS sets MOCK_TPM
ifeq (${MOCK_TPM},) ifeq (${MOCK_TPM},)
VBINIT_SRCS += \ VBINIT_SRCS += \
@@ -420,11 +427,14 @@ FWLIB_OBJS = ${FWLIB_SRCS:%.c=${BUILD}/%.o}
FWLIB2X_OBJS = ${FWLIB2X_SRCS:%.c=${BUILD}/%.o} FWLIB2X_OBJS = ${FWLIB2X_SRCS:%.c=${BUILD}/%.o}
FWLIB20_OBJS = ${FWLIB20_SRCS:%.c=${BUILD}/%.o} FWLIB20_OBJS = ${FWLIB20_SRCS:%.c=${BUILD}/%.o}
FWLIB21_OBJS = ${FWLIB21_SRCS:%.c=${BUILD}/%.o} FWLIB21_OBJS = ${FWLIB21_SRCS:%.c=${BUILD}/%.o}
ALL_OBJS += ${FWLIB_OBJS} ${FWLIB2X_OBJS} ${FWLIB20_OBJS} ${FWLIB21_OBJS} BDBLIB_OBJS = ${BDBLIB_SRCS:%.c=${BUILD}/%.o}
ALL_OBJS += ${FWLIB_OBJS} ${FWLIB2X_OBJS} ${FWLIB20_OBJS} ${FWLIB21_OBJS} \
$(BDBLIB_OBJS}
# Intermediate library for the vboot_reference utilities to link against. # Intermediate library for the vboot_reference utilities to link against.
UTILLIB = ${BUILD}/libvboot_util.a UTILLIB = ${BUILD}/libvboot_util.a
UTILLIB21 = ${BUILD}/libvboot_util21.a UTILLIB21 = ${BUILD}/libvboot_util21.a
UTILBDB = ${BUILD}/libvboot_utilbdb.a
UTILLIB_SRCS = \ UTILLIB_SRCS = \
cgpt/cgpt_create.c \ cgpt/cgpt_create.c \
@@ -460,6 +470,12 @@ UTILLIB21_SRCS += \
UTILLIB21_OBJS = ${UTILLIB21_SRCS:%.c=${BUILD}/%.o} UTILLIB21_OBJS = ${UTILLIB21_SRCS:%.c=${BUILD}/%.o}
ALL_OBJS += ${UTILLIB21_OBJS} ALL_OBJS += ${UTILLIB21_OBJS}
UTILBDB_SRCS += \
firmware/bdb/host.c
UTILBDB_OBJS = ${UTILBDB_SRCS:%.c=${BUILD}/%.o}
ALL_OBJS += ${UTILBDB_OBJS}
# Externally exported library for some target userspace apps to link with # Externally exported library for some target userspace apps to link with
# (cryptohome, updater, etc.) # (cryptohome, updater, etc.)
HOSTLIB = ${BUILD}/libvboot_host.a HOSTLIB = ${BUILD}/libvboot_host.a
@@ -765,7 +781,10 @@ TEST21_NAMES = \
tests/vb21_host_misc_tests \ tests/vb21_host_misc_tests \
tests/vb21_host_sig_tests tests/vb21_host_sig_tests
TEST_NAMES += ${TEST2X_NAMES} ${TEST20_NAMES} ${TEST21_NAMES} TESTBDB_NAMES = \
tests/bdb_test
TEST_NAMES += ${TEST2X_NAMES} ${TEST20_NAMES} ${TEST21_NAMES} ${TESTBDB_NAMES}
# And a few more... # And a few more...
TLCL_TEST_NAMES = \ TLCL_TEST_NAMES = \
@@ -791,6 +810,7 @@ TEST_FUTIL_BINS = $(addprefix ${BUILD}/,${TEST_FUTIL_NAMES})
TEST2X_BINS = $(addprefix ${BUILD}/,${TEST2X_NAMES}) TEST2X_BINS = $(addprefix ${BUILD}/,${TEST2X_NAMES})
TEST20_BINS = $(addprefix ${BUILD}/,${TEST20_NAMES}) TEST20_BINS = $(addprefix ${BUILD}/,${TEST20_NAMES})
TEST21_BINS = $(addprefix ${BUILD}/,${TEST21_NAMES}) TEST21_BINS = $(addprefix ${BUILD}/,${TEST21_NAMES})
TESTBDB_BINS = $(addprefix ${BUILD}/,${TESTBDB_NAMES})
# Directory containing test keys # Directory containing test keys
TEST_KEYS = ${SRC_RUN}/tests/testkeys TEST_KEYS = ${SRC_RUN}/tests/testkeys
@@ -858,6 +878,7 @@ ${FWLIB_OBJS}: CFLAGS += -DUNROLL_LOOPS
${FWLIB2X_OBJS}: CFLAGS += -DUNROLL_LOOPS ${FWLIB2X_OBJS}: CFLAGS += -DUNROLL_LOOPS
${FWLIB20_OBJS}: CFLAGS += -DUNROLL_LOOPS ${FWLIB20_OBJS}: CFLAGS += -DUNROLL_LOOPS
${FWLIB21_OBJS}: CFLAGS += -DUNROLL_LOOPS ${FWLIB21_OBJS}: CFLAGS += -DUNROLL_LOOPS
${BDBLIB_OBJS}: CFLAGS += -DUNROLL_LOOPS
# Workaround for coreboot on x86, which will power off asynchronously # Workaround for coreboot on x86, which will power off asynchronously
# without giving us a chance to react. This is not an example of the Right # without giving us a chance to react. This is not an example of the Right
@@ -883,6 +904,7 @@ endif
${FWLIB20_OBJS}: INCLUDES += -Ifirmware/lib20/include ${FWLIB20_OBJS}: INCLUDES += -Ifirmware/lib20/include
${FWLIB21_OBJS}: INCLUDES += -Ifirmware/lib21/include ${FWLIB21_OBJS}: INCLUDES += -Ifirmware/lib21/include
${BDBLIB_OBJS}: INCLUDES += -Ifirmware/bdb
# Linktest ensures firmware lib doesn't rely on outside libraries # Linktest ensures firmware lib doesn't rely on outside libraries
${BUILD}/firmware/linktest/main_vbinit: ${VBINIT_OBJS} ${BUILD}/firmware/linktest/main_vbinit: ${VBINIT_OBJS}
@@ -937,6 +959,15 @@ ${FWLIB21}: ${FWLIB2X_OBJS} ${FWLIB21_OBJS}
@${PRINTF} " AR $(subst ${BUILD}/,,$@)\n" @${PRINTF} " AR $(subst ${BUILD}/,,$@)\n"
${Q}ar qc $@ $^ ${Q}ar qc $@ $^
.PHONY: bdblib
bdblib: ${BDBLIB}
${BDBLIB}: ${BDBLIB_OBJS}
@${PRINTF} " RM $(subst ${BUILD}/,,$@)\n"
${Q}rm -f $@
@${PRINTF} " AR $(subst ${BUILD}/,,$@)\n"
${Q}ar qc $@ $^
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Host library(s) # Host library(s)
@@ -966,6 +997,11 @@ ${UTILLIB21}: ${UTILLIB21_OBJS} ${FWLIB2X_OBJS} ${FWLIB21_OBJS}
@${PRINTF} " AR $(subst ${BUILD}/,,$@)\n" @${PRINTF} " AR $(subst ${BUILD}/,,$@)\n"
${Q}ar qc $@ $^ ${Q}ar qc $@ $^
${UTILBDB}: ${UTILBDB_OBJS} ${BDBLIB_OBJS}
@${PRINTF} " RM $(subst ${BUILD}/,,$@)\n"
${Q}rm -f $@
@${PRINTF} " AR $(subst ${BUILD}/,,$@)\n"
${Q}ar qc $@ $^
# Link tests for external repos # Link tests for external repos
${BUILD}/host/linktest/extern: ${HOSTLIB} ${BUILD}/host/linktest/extern: ${HOSTLIB}
@@ -1153,6 +1189,10 @@ ${TEST21_BINS}: ${UTILLIB21}
${TEST21_BINS}: INCLUDES += -Ihost/lib21/include -Ifirmware/lib21/include ${TEST21_BINS}: INCLUDES += -Ihost/lib21/include -Ifirmware/lib21/include
${TEST21_BINS}: LIBS += ${UTILLIB21} ${TEST21_BINS}: LIBS += ${UTILLIB21}
${TESTBDB_BINS}: ${UTILBDB}
${TESTBDB_BINS}: INCLUDES += -Ifirmware/bdb
${TESTBDB_BINS}: LIBS += ${UTILBDB_OBJS} ${BDBLIB_OBJS}
${TESTLIB}: ${TESTLIB_OBJS} ${TESTLIB}: ${TESTLIB_OBJS}
@${PRINTF} " RM $(subst ${BUILD}/,,$@)\n" @${PRINTF} " RM $(subst ${BUILD}/,,$@)\n"
${Q}rm -f $@ ${Q}rm -f $@
@@ -1219,6 +1259,7 @@ ${BUILD}/tests/vboot_common3_tests: LDLIBS += ${CRYPTO_LIBS}
${BUILD}/tests/vb20_common2_tests: LDLIBS += ${CRYPTO_LIBS} ${BUILD}/tests/vb20_common2_tests: LDLIBS += ${CRYPTO_LIBS}
${BUILD}/tests/vb20_common3_tests: LDLIBS += ${CRYPTO_LIBS} ${BUILD}/tests/vb20_common3_tests: LDLIBS += ${CRYPTO_LIBS}
${BUILD}/tests/verify_kernel: LDLIBS += ${CRYPTO_LIBS} ${BUILD}/tests/verify_kernel: LDLIBS += ${CRYPTO_LIBS}
${BUILD}/tests/bdb_test: LDLIBS += ${CRYPTO_LIBS}
${TEST21_BINS}: LDLIBS += ${CRYPTO_LIBS} ${TEST21_BINS}: LDLIBS += ${CRYPTO_LIBS}
@@ -1302,7 +1343,7 @@ ${FUTIL_CMD_LIST} ${FUTIL_STATIC_CMD_LIST}:
# Frequently-run tests # Frequently-run tests
.PHONY: test_targets .PHONY: test_targets
test_targets:: runcgpttests runmisctests run2tests test_targets:: runcgpttests runmisctests run2tests runbdbtests
ifeq (${MINIMAL},) ifeq (${MINIMAL},)
# Bitmap utility isn't compiled for minimal variant # Bitmap utility isn't compiled for minimal variant
@@ -1421,6 +1462,10 @@ run2tests: test_setup
${RUNTEST} ${BUILD_RUN}/tests/vb21_host_misc_tests ${BUILD} ${RUNTEST} ${BUILD_RUN}/tests/vb21_host_misc_tests ${BUILD}
${RUNTEST} ${BUILD_RUN}/tests/vb21_host_sig_tests ${TEST_KEYS} ${RUNTEST} ${BUILD_RUN}/tests/vb21_host_sig_tests ${TEST_KEYS}
.PHONY: runbdbtests
runbdbtests: test_setup
${RUNTEST} ${BUILD_RUN}/tests/bdb_test ${TEST_KEYS}
.PHONY: runfutiltests .PHONY: runfutiltests
runfutiltests: test_setup runfutiltests: test_setup
tests/futility/run_test_scripts.sh ${TEST_INSTALL_DIR}/bin tests/futility/run_test_scripts.sh ${TEST_INSTALL_DIR}/bin

View File

@@ -1,4 +1,4 @@
/* Copyright (c) 2015 The Chromium OS Authors. All rights reserved. /* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
* *
@@ -11,21 +11,9 @@
#include "bdb.h" #include "bdb.h"
#include "host.h" #include "host.h"
#include "test_common.h"
#define TEST_EQ(got, want) test_eq(got, want, #got, #want, __LINE__) #define TEST_EQ_S(result, expect) TEST_EQ(result, expect, #result "==" #expect)
void test_eq(int got, int want, const char *gotstr, const char *wantstr,
int line)
{
if (got == want)
return;
fprintf(stderr, "Fail(%d): %s != %s\n"
"got: 0x%08x (%d)\n"
"wanted: 0x%08x (%d)\n",
line, gotstr, wantstr, got, got, want, want);
exit(1);
}
void check_header_tests(void) void check_header_tests(void)
{ {
@@ -43,32 +31,32 @@ void check_header_tests(void)
struct bdb_header s; struct bdb_header s;
s = sgood; s = sgood;
TEST_EQ(bdb_check_header(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_SUCCESS);
TEST_EQ(bdb_check_header(&s, ssize - 1), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_header(&s, ssize - 1), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size++; s.struct_size++;
TEST_EQ(bdb_check_header(&s, ssize), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size--; s.struct_size--;
TEST_EQ(bdb_check_header(&s, ssize), BDB_ERROR_STRUCT_SIZE); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_ERROR_STRUCT_SIZE);
s = sgood; s = sgood;
s.struct_magic++; s.struct_magic++;
TEST_EQ(bdb_check_header(&s, ssize), BDB_ERROR_STRUCT_MAGIC); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_ERROR_STRUCT_MAGIC);
s = sgood; s = sgood;
s.struct_major_version++; s.struct_major_version++;
TEST_EQ(bdb_check_header(&s, ssize), BDB_ERROR_STRUCT_VERSION); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_ERROR_STRUCT_VERSION);
s = sgood; s = sgood;
s.oem_area_0_size++; s.oem_area_0_size++;
TEST_EQ(bdb_check_header(&s, ssize), BDB_ERROR_OEM_AREA_SIZE); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_ERROR_OEM_AREA_SIZE);
s = sgood; s = sgood;
s.bdb_size = ssize - 1; s.bdb_size = ssize - 1;
TEST_EQ(bdb_check_header(&s, ssize), BDB_ERROR_BDB_SIZE); TEST_EQ_S(bdb_check_header(&s, ssize), BDB_ERROR_BDB_SIZE);
} }
void check_key_tests(void) void check_key_tests(void)
@@ -88,48 +76,48 @@ void check_key_tests(void)
struct bdb_key s; struct bdb_key s;
s = sgood; s = sgood;
TEST_EQ(bdb_check_key(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_SUCCESS);
TEST_EQ(bdb_check_key(&s, ssize - 1), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_key(&s, ssize - 1), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size++; s.struct_size++;
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size--; s.struct_size--;
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_STRUCT_SIZE); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_STRUCT_SIZE);
s = sgood; s = sgood;
s.struct_magic++; s.struct_magic++;
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_STRUCT_MAGIC); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_STRUCT_MAGIC);
s = sgood; s = sgood;
s.struct_major_version++; s.struct_major_version++;
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_STRUCT_VERSION); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_STRUCT_VERSION);
/* Description must contain a null */ /* Description must contain a null */
s = sgood; s = sgood;
memset(s.description, 'x', sizeof(s.description)); memset(s.description, 'x', sizeof(s.description));
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_DESCRIPTION); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_DESCRIPTION);
/* Data AFTER the null is explicitly allowed, though */ /* Data AFTER the null is explicitly allowed, though */
s = sgood; s = sgood;
s.description[100] = 'x'; s.description[100] = 'x';
TEST_EQ(bdb_check_key(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_SUCCESS);
/* Limited algorithm choices at present */ /* Limited algorithm choices at present */
s = sgood; s = sgood;
s.hash_alg = BDB_HASH_ALG_INVALID; s.hash_alg = BDB_HASH_ALG_INVALID;
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_HASH_ALG); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_HASH_ALG);
/* This works because ECDSA521 signatures are smaller than RSA4096 */ /* This works because ECDSA521 signatures are smaller than RSA4096 */
s = sgood; s = sgood;
s.sig_alg = BDB_SIG_ALG_ECSDSA521; s.sig_alg = BDB_SIG_ALG_ECSDSA521;
TEST_EQ(bdb_check_key(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_SUCCESS);
s = sgood; s = sgood;
s.sig_alg = BDB_SIG_ALG_INVALID; s.sig_alg = BDB_SIG_ALG_INVALID;
TEST_EQ(bdb_check_key(&s, ssize), BDB_ERROR_SIG_ALG); TEST_EQ_S(bdb_check_key(&s, ssize), BDB_ERROR_SIG_ALG);
} }
void check_sig_tests(void) void check_sig_tests(void)
@@ -148,48 +136,48 @@ void check_sig_tests(void)
struct bdb_sig s; struct bdb_sig s;
s = sgood; s = sgood;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_SUCCESS);
TEST_EQ(bdb_check_sig(&s, ssize - 1), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_sig(&s, ssize - 1), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size++; s.struct_size++;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size--; s.struct_size--;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_STRUCT_SIZE); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_STRUCT_SIZE);
s = sgood; s = sgood;
s.struct_magic++; s.struct_magic++;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_STRUCT_MAGIC); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_STRUCT_MAGIC);
s = sgood; s = sgood;
s.struct_major_version++; s.struct_major_version++;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_STRUCT_VERSION); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_STRUCT_VERSION);
/* Description must contain a null */ /* Description must contain a null */
s = sgood; s = sgood;
memset(s.description, 'x', sizeof(s.description)); memset(s.description, 'x', sizeof(s.description));
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_DESCRIPTION); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_DESCRIPTION);
/* Data AFTER the null is explicitly allowed, though */ /* Data AFTER the null is explicitly allowed, though */
s = sgood; s = sgood;
s.description[100] = 'x'; s.description[100] = 'x';
TEST_EQ(bdb_check_sig(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_SUCCESS);
/* Limited algorithm choices at present */ /* Limited algorithm choices at present */
s = sgood; s = sgood;
s.hash_alg = BDB_HASH_ALG_INVALID; s.hash_alg = BDB_HASH_ALG_INVALID;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_HASH_ALG); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_HASH_ALG);
/* This works because ECDSA521 signatures are smaller than RSA4096 */ /* This works because ECDSA521 signatures are smaller than RSA4096 */
s = sgood; s = sgood;
s.sig_alg = BDB_SIG_ALG_ECSDSA521; s.sig_alg = BDB_SIG_ALG_ECSDSA521;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_SUCCESS);
s = sgood; s = sgood;
s.sig_alg = BDB_SIG_ALG_INVALID; s.sig_alg = BDB_SIG_ALG_INVALID;
TEST_EQ(bdb_check_sig(&s, ssize), BDB_ERROR_SIG_ALG); TEST_EQ_S(bdb_check_sig(&s, ssize), BDB_ERROR_SIG_ALG);
} }
void check_data_tests(void) void check_data_tests(void)
@@ -210,46 +198,46 @@ void check_data_tests(void)
struct bdb_data s; struct bdb_data s;
s = sgood; s = sgood;
TEST_EQ(bdb_check_data(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_SUCCESS);
TEST_EQ(bdb_check_data(&s, ssize - 1), BDB_ERROR_BUF_SIZE); TEST_EQ_S(bdb_check_data(&s, ssize - 1), BDB_ERROR_BUF_SIZE);
s = sgood; s = sgood;
s.struct_size--; s.struct_size--;
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_STRUCT_SIZE); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_STRUCT_SIZE);
s = sgood; s = sgood;
s.struct_magic++; s.struct_magic++;
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_STRUCT_MAGIC); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_STRUCT_MAGIC);
s = sgood; s = sgood;
s.struct_major_version++; s.struct_major_version++;
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_STRUCT_VERSION); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_STRUCT_VERSION);
/* Description must contain a null */ /* Description must contain a null */
s = sgood; s = sgood;
memset(s.description, 'x', sizeof(s.description)); memset(s.description, 'x', sizeof(s.description));
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_DESCRIPTION); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_DESCRIPTION);
/* Data AFTER the null is explicitly allowed, though */ /* Data AFTER the null is explicitly allowed, though */
s = sgood; s = sgood;
s.description[100] = 'x'; s.description[100] = 'x';
TEST_EQ(bdb_check_data(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_SUCCESS);
s = sgood; s = sgood;
s.hash_entry_size--; s.hash_entry_size--;
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_HASH_ENTRY_SIZE); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_HASH_ENTRY_SIZE);
s = sgood; s = sgood;
s.oem_area_1_size++; s.oem_area_1_size++;
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_OEM_AREA_SIZE); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_OEM_AREA_SIZE);
/* Check exact size needed */ /* Check exact size needed */
s = sgood; s = sgood;
s.signed_size = sizeof(s) + s.num_hashes * sizeof(struct bdb_hash) + s.signed_size = sizeof(s) + s.num_hashes * sizeof(struct bdb_hash) +
s.oem_area_1_size; s.oem_area_1_size;
TEST_EQ(bdb_check_data(&s, ssize), BDB_SUCCESS); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_SUCCESS);
s.signed_size--; s.signed_size--;
TEST_EQ(bdb_check_data(&s, ssize), BDB_ERROR_SIGNED_SIZE); TEST_EQ_S(bdb_check_data(&s, ssize), BDB_ERROR_SIGNED_SIZE);
/* /*
* TODO: Verify wraparound check works. That can only be tested on a * TODO: Verify wraparound check works. That can only be tested on a
@@ -261,10 +249,11 @@ void check_data_tests(void)
/** /**
* Test bdb_verify() and bdb_create() * Test bdb_verify() and bdb_create()
*/ */
void check_bdb_verify(void) void check_bdb_verify(const char *key_dir)
{ {
uint8_t oem_area_0[32] = "Some OEM area."; uint8_t oem_area_0[32] = "Some OEM area.";
uint8_t oem_area_1[64] = "Some other OEM area."; uint8_t oem_area_1[64] = "Some other OEM area.";
char filename[1024];
struct bdb_hash hash[2] = { struct bdb_hash hash[2] = {
{ {
@@ -304,10 +293,14 @@ void check_bdb_verify(void)
size_t hsize; size_t hsize;
/* Load keys */ /* Load keys */
p.bdbkey = bdb_create_key("testkeys/bdbkey.keyb", 100, "BDB key"); sprintf(filename, "%s/bdbkey.keyb", key_dir);
p.subkey = bdb_create_key("testkeys/subkey.keyb", 200, "Subkey"); p.bdbkey = bdb_create_key(filename, 100, "BDB key");
p.private_bdbkey = read_pem("testkeys/bdbkey.pem"); sprintf(filename, "%s/subkey.keyb", key_dir);
p.private_subkey = read_pem("testkeys/subkey.pem"); p.subkey = bdb_create_key(filename, 200, "Subkey");
sprintf(filename, "%s/bdbkey.pem", key_dir);
p.private_bdbkey = read_pem(filename);
sprintf(filename, "%s/subkey.pem", key_dir);
p.private_subkey = read_pem(filename);
if (!p.bdbkey || !p.subkey || !p.private_bdbkey || !p.private_subkey) { if (!p.bdbkey || !p.subkey || !p.private_bdbkey || !p.private_subkey) {
fprintf(stderr, "Unable to load test keys\n"); fprintf(stderr, "Unable to load test keys\n");
exit(2); exit(2);
@@ -328,95 +321,95 @@ void check_bdb_verify(void)
/* As created, it should pass */ /* As created, it should pass */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_SUCCESS); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_SUCCESS);
/* Mangle each component in turn */ /* Mangle each component in turn */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
h->struct_magic++; h->struct_magic++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_key *)bdb_get_bdbkey(h))->struct_magic++; ((struct bdb_key *)bdb_get_bdbkey(h))->struct_magic++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_BDBKEY); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_BDBKEY);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_key *)bdb_get_bdbkey(h))->key_version++; ((struct bdb_key *)bdb_get_bdbkey(h))->key_version++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_GOOD_OTHER_THAN_KEY); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_GOOD_OTHER_THAN_KEY);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
h->oem_area_0_size += hsize; h->oem_area_0_size += hsize;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_OEM_AREA_0); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_OEM_AREA_0);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_key *)bdb_get_subkey(h))->struct_magic++; ((struct bdb_key *)bdb_get_subkey(h))->struct_magic++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_SUBKEY); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_SUBKEY);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_key *)bdb_get_subkey(h))->struct_size += 4; ((struct bdb_key *)bdb_get_subkey(h))->struct_size += 4;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_BDB_SIGNED_SIZE); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_BDB_SIGNED_SIZE);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_sig *)bdb_get_header_sig(h))->struct_magic++; ((struct bdb_sig *)bdb_get_header_sig(h))->struct_magic++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_sig *)bdb_get_header_sig(h))->signed_size--; ((struct bdb_sig *)bdb_get_header_sig(h))->signed_size--;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_sig *)bdb_get_header_sig(h))->sig_data[0] ^= 0x42; ((struct bdb_sig *)bdb_get_header_sig(h))->sig_data[0] ^= 0x42;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG);
/* Also make sure the header sig really covers all the fields */ /* Also make sure the header sig really covers all the fields */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_key *)bdb_get_subkey(h))->key_version++; ((struct bdb_key *)bdb_get_subkey(h))->key_version++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((uint8_t *)bdb_get_oem_area_0(h))[0] ^= 0x42; ((uint8_t *)bdb_get_oem_area_0(h))[0] ^= 0x42;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((uint8_t *)bdb_get_oem_area_0(h))[p.oem_area_0_size - 1] ^= 0x24; ((uint8_t *)bdb_get_oem_area_0(h))[p.oem_area_0_size - 1] ^= 0x24;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_HEADER_SIG);
/* Check data header */ /* Check data header */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_data *)bdb_get_data(h))->struct_magic++; ((struct bdb_data *)bdb_get_data(h))->struct_magic++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_sig *)bdb_get_data_sig(h))->struct_magic++; ((struct bdb_sig *)bdb_get_data_sig(h))->struct_magic++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_sig *)bdb_get_data_sig(h))->signed_size--; ((struct bdb_sig *)bdb_get_data_sig(h))->signed_size--;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_sig *)bdb_get_data_sig(h))->sig_data[0] ^= 0x42; ((struct bdb_sig *)bdb_get_data_sig(h))->sig_data[0] ^= 0x42;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
/* Also make sure the data sig really covers all the fields */ /* Also make sure the data sig really covers all the fields */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_data *)bdb_get_data(h))->data_version--; ((struct bdb_data *)bdb_get_data(h))->data_version--;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((uint8_t *)bdb_get_oem_area_1(h))[0] ^= 0x42; ((uint8_t *)bdb_get_oem_area_1(h))[0] ^= 0x42;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((uint8_t *)bdb_get_oem_area_1(h))[p.oem_area_1_size - 1] ^= 0x24; ((uint8_t *)bdb_get_oem_area_1(h))[p.oem_area_1_size - 1] ^= 0x24;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_hash *)bdb_get_hash(h, BDB_DATA_SP_RW))->offset++; ((struct bdb_hash *)bdb_get_hash(h, BDB_DATA_SP_RW))->offset++;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
((struct bdb_hash *)bdb_get_hash(h, BDB_DATA_AP_RW))->digest[0] ^= 0x96; ((struct bdb_hash *)bdb_get_hash(h, BDB_DATA_AP_RW))->digest[0] ^= 0x96;
TEST_EQ(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG); TEST_EQ_S(bdb_verify(h, hsize, bdbkey_digest), BDB_ERROR_DATA_SIG);
/* /*
* This is also a convenient place to test that all the parameters we * This is also a convenient place to test that all the parameters we
@@ -424,40 +417,40 @@ void check_bdb_verify(void)
* bdb_get_*() functions. * bdb_get_*() functions.
*/ */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
TEST_EQ(h->bdb_load_address, p.bdb_load_address); TEST_EQ_S(h->bdb_load_address, p.bdb_load_address);
TEST_EQ(strcmp(bdb_get_bdbkey(h)->description, TEST_EQ_S(strcmp(bdb_get_bdbkey(h)->description, p.bdbkey->description),
p.bdbkey->description), 0); 0);
TEST_EQ(bdb_get_bdbkey(h)->key_version, p.bdbkey->key_version); TEST_EQ_S(bdb_get_bdbkey(h)->key_version, p.bdbkey->key_version);
TEST_EQ(h->oem_area_0_size, p.oem_area_0_size); TEST_EQ_S(h->oem_area_0_size, p.oem_area_0_size);
TEST_EQ(memcmp(bdb_get_oem_area_0(h), oem_area_0, sizeof(oem_area_0)), TEST_EQ_S(memcmp(bdb_get_oem_area_0(h), oem_area_0, sizeof(oem_area_0)),
0); 0);
TEST_EQ(strcmp(bdb_get_subkey(h)->description, p.subkey->description), TEST_EQ_S(strcmp(bdb_get_subkey(h)->description, p.subkey->description),
0); 0);
TEST_EQ(bdb_get_subkey(h)->key_version, p.subkey->key_version); TEST_EQ_S(bdb_get_subkey(h)->key_version, p.subkey->key_version);
TEST_EQ(strcmp(bdb_get_header_sig(h)->description, TEST_EQ_S(strcmp(bdb_get_header_sig(h)->description,
p.header_sig_description), 0); p.header_sig_description), 0);
TEST_EQ(strcmp(bdb_get_data(h)->description, p.data_description), 0); TEST_EQ_S(strcmp(bdb_get_data(h)->description, p.data_description), 0);
TEST_EQ(bdb_get_data(h)->data_version, p.data_version); TEST_EQ_S(bdb_get_data(h)->data_version, p.data_version);
TEST_EQ(bdb_get_data(h)->num_hashes, p.num_hashes); TEST_EQ_S(bdb_get_data(h)->num_hashes, p.num_hashes);
TEST_EQ(bdb_get_data(h)->oem_area_1_size, p.oem_area_1_size); TEST_EQ_S(bdb_get_data(h)->oem_area_1_size, p.oem_area_1_size);
TEST_EQ(memcmp(bdb_get_oem_area_1(h), oem_area_1, sizeof(oem_area_1)), TEST_EQ_S(memcmp(bdb_get_oem_area_1(h), oem_area_1, sizeof(oem_area_1)),
0); 0);
TEST_EQ(strcmp(bdb_get_data_sig(h)->description, TEST_EQ_S(strcmp(bdb_get_data_sig(h)->description,
p.data_sig_description), 0); p.data_sig_description), 0);
/* Test getting hash entries */ /* Test getting hash entries */
memcpy(h, hgood, hsize); memcpy(h, hgood, hsize);
TEST_EQ(bdb_get_hash(h, BDB_DATA_SP_RW)->offset, hash[0].offset); TEST_EQ_S(bdb_get_hash(h, BDB_DATA_SP_RW)->offset, hash[0].offset);
TEST_EQ(bdb_get_hash(h, BDB_DATA_AP_RW)->offset, hash[1].offset); TEST_EQ_S(bdb_get_hash(h, BDB_DATA_AP_RW)->offset, hash[1].offset);
/* And a non-existent one */ /* And a non-existent one */
TEST_EQ(bdb_get_hash(h, BDB_DATA_MCU)!=NULL, 0); TEST_EQ_S(bdb_get_hash(h, BDB_DATA_MCU)!=NULL, 0);
/* /*
* TODO: Verify wraparound checks works. That can only be tested on a * TODO: Verify wraparound checks works. That can only be tested on a
@@ -476,17 +469,21 @@ void check_bdb_verify(void)
/*****************************************************************************/ /*****************************************************************************/
int main(void) int main(int argc, char *argv[])
{ {
printf("Running tests...\n"); if (argc != 2) {
fprintf(stderr, "Usage: %s <keys_dir>", argv[0]);
return -1;
}
printf("Running BDB tests...\n");
check_header_tests(); check_header_tests();
check_key_tests(); check_key_tests();
check_sig_tests(); check_sig_tests();
check_data_tests(); check_data_tests();
check_bdb_verify(); check_bdb_verify(argv[1]);
printf("All tests passed!\n"); printf("All tests passed!\n");
return 0; return gTestSuccess ? 0 : 255;
} }