diff options
author | Joel Kitching <kitching@google.com> | 2019-08-27 17:13:55 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-08-31 20:49:16 +0000 |
commit | fbde3aa0af045021c2bfd315ad59f10aab2543fc (patch) | |
tree | abe4d0373a2a2b596f247a946aee5be51664ca0f /tests | |
parent | f4a9bfb303b034639469f1f1fcf18d61357bd4fe (diff) | |
download | vboot-fbde3aa0af045021c2bfd315ad59f10aab2543fc.tar.gz |
vboot/secdata: rename secdata and secdatak
For clarity's sake, rename:
secdata -> secdata_firmware
secdatak -> secdata_kernel
secdata is now the general term to refer to any secure data
spaces: firmware, kernel, and FWMP.
Once coreboot code has been updated, the sections in 2api.h
and 2constants.h may be removed.
BUG=b:124141368, chromium:972956
TEST=make clean && make runtests
BRANCH=none
Change-Id: I376acee552e8be37c75c340626a95462f81e198b
Signed-off-by: Joel Kitching <kitching@google.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/1773079
Reviewed-by: Joel Kitching <kitching@chromium.org>
Commit-Queue: Joel Kitching <kitching@chromium.org>
Tested-by: Joel Kitching <kitching@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/vb20_api_kernel_tests.c | 38 | ||||
-rw-r--r-- | tests/vb20_api_tests.c | 4 | ||||
-rw-r--r-- | tests/vb20_kernel_tests.c | 8 | ||||
-rw-r--r-- | tests/vb20_misc_tests.c | 13 | ||||
-rw-r--r-- | tests/vb20_verify_fw.c | 9 | ||||
-rw-r--r-- | tests/vb21_api_tests.c | 4 | ||||
-rw-r--r-- | tests/vb21_misc_tests.c | 13 | ||||
-rw-r--r-- | tests/vb2_api_tests.c | 47 | ||||
-rw-r--r-- | tests/vb2_misc_tests.c | 81 | ||||
-rw-r--r-- | tests/vb2_secdata_firmware_tests.c | 150 | ||||
-rw-r--r-- | tests/vb2_secdata_kernel_tests.c | 135 | ||||
-rw-r--r-- | tests/vb2_secdata_tests.c | 134 | ||||
-rw-r--r-- | tests/vb2_secdatak_tests.c | 128 | ||||
-rw-r--r-- | tests/vboot_kernel_tests.c | 2 |
14 files changed, 404 insertions, 362 deletions
diff --git a/tests/vb20_api_kernel_tests.c b/tests/vb20_api_kernel_tests.c index cdb87e88..9f68a1fd 100644 --- a/tests/vb20_api_kernel_tests.c +++ b/tests/vb20_api_kernel_tests.c @@ -64,9 +64,9 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdatak_create(&ctx); - vb2_secdatak_init(&ctx); - vb2_secdatak_set(&ctx, VB2_SECDATAK_VERSIONS, 0x20002); + vb2api_secdata_kernel_create(&ctx); + vb2_secdata_kernel_init(&ctx); + vb2_secdata_kernel_set(&ctx, VB2_SECDATA_KERNEL_VERSIONS, 0x20002); mock_read_res_fail_on_call = 0; mock_unpack_key_retval = VB2_SUCCESS; @@ -144,7 +144,7 @@ static void reset_common_data(enum reset_type t) } else { /* Set flags and versions for roll-forward */ sd->kernel_version = 0x20004; - sd->kernel_version_secdatak = 0x20002; + sd->kernel_version_secdata = 0x20002; sd->flags |= VB2_SD_FLAG_KERNEL_SIGNED; ctx.flags |= VB2_CONTEXT_ALLOW_KERNEL_ROLL_FORWARD; } @@ -241,7 +241,8 @@ static void phase1_tests(void) TEST_EQ(k->key_size, sizeof(fw_kernel_key_data), " key_size"); TEST_EQ(memcmp((uint8_t *)k + k->key_offset, fw_kernel_key_data, k->key_size), 0, " key data"); - TEST_EQ(sd->kernel_version_secdatak, 0x20002, " secdatak version"); + TEST_EQ(sd->kernel_version_secdata, 0x20002, + " secdata_kernel version"); /* Test successful call in recovery mode */ reset_common_data(FOR_PHASE1); @@ -266,19 +267,20 @@ static void phase1_tests(void) TEST_EQ(memcmp((uint8_t *)k + k->key_offset, mock_gbb.recovery_key_data, k->key_size), 0, " key data"); - TEST_EQ(sd->kernel_version_secdatak, 0x20002, " secdatak version"); + TEST_EQ(sd->kernel_version_secdata, 0x20002, + " secdata_kernel version"); - /* Bad secdatak causes failure in normal mode only */ + /* Bad secdata_kernel causes failure in normal mode only */ reset_common_data(FOR_PHASE1); - ctx.secdatak[0] ^= 0x33; - TEST_EQ(vb2api_kernel_phase1(&ctx), VB2_ERROR_SECDATAK_CRC, + ctx.secdata_kernel[0] ^= 0x33; + TEST_EQ(vb2api_kernel_phase1(&ctx), VB2_ERROR_SECDATA_KERNEL_CRC, "phase1 bad secdata"); reset_common_data(FOR_PHASE1); - ctx.secdatak[0] ^= 0x33; + ctx.secdata_kernel[0] ^= 0x33; ctx.flags |= VB2_CONTEXT_RECOVERY_MODE; TEST_SUCC(vb2api_kernel_phase1(&ctx), "phase1 bad secdata rec"); - TEST_EQ(sd->kernel_version_secdatak, 0, " secdatak version"); + TEST_EQ(sd->kernel_version_secdata, 0, " secdata_kernel version"); /* Failures while reading recovery key */ reset_common_data(FOR_PHASE1); @@ -415,37 +417,37 @@ static void phase3_tests(void) reset_common_data(FOR_PHASE3); TEST_SUCC(vb2api_kernel_phase3(&ctx), "phase3 good"); - vb2_secdatak_get(&ctx, VB2_SECDATAK_VERSIONS, &v); + vb2_secdata_kernel_get(&ctx, VB2_SECDATA_KERNEL_VERSIONS, &v); TEST_EQ(v, 0x20004, " version"); reset_common_data(FOR_PHASE3); sd->kernel_version = 0x20001; TEST_SUCC(vb2api_kernel_phase3(&ctx), "phase3 no rollback"); - vb2_secdatak_get(&ctx, VB2_SECDATAK_VERSIONS, &v); + vb2_secdata_kernel_get(&ctx, VB2_SECDATA_KERNEL_VERSIONS, &v); TEST_EQ(v, 0x20002, " version"); reset_common_data(FOR_PHASE3); sd->flags &= ~VB2_SD_FLAG_KERNEL_SIGNED; TEST_SUCC(vb2api_kernel_phase3(&ctx), "phase3 unsigned kernel"); - vb2_secdatak_get(&ctx, VB2_SECDATAK_VERSIONS, &v); + vb2_secdata_kernel_get(&ctx, VB2_SECDATA_KERNEL_VERSIONS, &v); TEST_EQ(v, 0x20002, " version"); reset_common_data(FOR_PHASE3); ctx.flags |= VB2_CONTEXT_RECOVERY_MODE; TEST_SUCC(vb2api_kernel_phase3(&ctx), "phase3 recovery"); - vb2_secdatak_get(&ctx, VB2_SECDATAK_VERSIONS, &v); + vb2_secdata_kernel_get(&ctx, VB2_SECDATA_KERNEL_VERSIONS, &v); TEST_EQ(v, 0x20002, " version"); reset_common_data(FOR_PHASE3); ctx.flags &= ~VB2_CONTEXT_ALLOW_KERNEL_ROLL_FORWARD; TEST_SUCC(vb2api_kernel_phase3(&ctx), "phase3 no rollforward"); - vb2_secdatak_get(&ctx, VB2_SECDATAK_VERSIONS, &v); + vb2_secdata_kernel_get(&ctx, VB2_SECDATA_KERNEL_VERSIONS, &v); TEST_EQ(v, 0x20002, " version"); reset_common_data(FOR_PHASE3); - sd->status &= ~VB2_SD_STATUS_SECDATAK_INIT; + sd->status &= ~VB2_SD_STATUS_SECDATA_KERNEL_INIT; TEST_EQ(vb2api_kernel_phase3(&ctx), - VB2_ERROR_SECDATAK_SET_UNINITIALIZED, "phase3 set fail"); + VB2_ERROR_SECDATA_KERNEL_SET_UNINITIALIZED, "phase3 set fail"); } int main(int argc, char* argv[]) diff --git a/tests/vb20_api_tests.c b/tests/vb20_api_tests.c index c3b48b78..8db477e5 100644 --- a/tests/vb20_api_tests.c +++ b/tests/vb20_api_tests.c @@ -66,8 +66,8 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdata_create(&ctx); - vb2_secdata_init(&ctx); + vb2api_secdata_firmware_create(&ctx); + vb2_secdata_firmware_init(&ctx); retval_vb2_load_fw_keyblock = VB2_SUCCESS; retval_vb2_load_fw_preamble = VB2_SUCCESS; diff --git a/tests/vb20_kernel_tests.c b/tests/vb20_kernel_tests.c index 52899cca..d182e4c3 100644 --- a/tests/vb20_kernel_tests.c +++ b/tests/vb20_kernel_tests.c @@ -90,8 +90,8 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdatak_create(&ctx); - vb2_secdatak_init(&ctx); + vb2api_secdata_kernel_create(&ctx); + vb2_secdata_kernel_init(&ctx); mock_read_res_fail_on_call = 0; mock_unpack_key_retval = VB2_SUCCESS; @@ -99,8 +99,8 @@ static void reset_common_data(enum reset_type t) mock_verify_preamble_retval = VB2_SUCCESS; /* Set up mock data for verifying keyblock */ - sd->kernel_version_secdatak = 0x20002; - vb2_secdatak_set(&ctx, VB2_SECDATAK_VERSIONS, 0x20002); + sd->kernel_version_secdata = 0x20002; + vb2_secdata_kernel_set(&ctx, VB2_SECDATA_KERNEL_VERSIONS, 0x20002); mock_gbb.recovery_key.algorithm = 11; mock_gbb.recovery_key.key_offset = diff --git a/tests/vb20_misc_tests.c b/tests/vb20_misc_tests.c index 1d23a6d6..cc592226 100644 --- a/tests/vb20_misc_tests.c +++ b/tests/vb20_misc_tests.c @@ -76,8 +76,8 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdata_create(&ctx); - vb2_secdata_init(&ctx); + vb2api_secdata_firmware_create(&ctx); + vb2_secdata_firmware_init(&ctx); mock_read_res_fail_on_call = 0; mock_unpack_key_retval = VB2_SUCCESS; @@ -86,7 +86,8 @@ static void reset_common_data(enum reset_type t) /* Set up mock data for verifying keyblock */ sd->fw_version_secdata = 0x20002; - vb2_secdata_set(&ctx, VB2_SECDATA_VERSIONS, sd->fw_version_secdata); + vb2_secdata_firmware_set(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, + sd->fw_version_secdata); gbb.rootkey_offset = vb2_offset_of(&mock_gbb, &mock_gbb.rootkey); gbb.rootkey_size = sizeof(mock_gbb.rootkey_data); @@ -366,7 +367,7 @@ static void verify_preamble_tests(void) pre->firmware_version = 3; TEST_SUCC(vb2_load_fw_preamble(&ctx), "preamble version roll forward"); - vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, &v); TEST_EQ(v, 0x20003, "roll forward"); /* Newer version without result success doesn't roll forward */ @@ -375,7 +376,7 @@ static void verify_preamble_tests(void) sd->last_fw_result = VB2_FW_RESULT_UNKNOWN; TEST_SUCC(vb2_load_fw_preamble(&ctx), "preamble version no roll forward 1"); - vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, &v); TEST_EQ(v, 0x20002, "no roll forward"); /* Newer version with success but for other slot doesn't roll forward */ @@ -384,7 +385,7 @@ static void verify_preamble_tests(void) sd->last_fw_slot = 1; TEST_SUCC(vb2_load_fw_preamble(&ctx), "preamble version no roll forward 2"); - vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, &v); TEST_EQ(v, 0x20002, "no roll forward"); } diff --git a/tests/vb20_verify_fw.c b/tests/vb20_verify_fw.c index 850d4b01..5cf2b765 100644 --- a/tests/vb20_verify_fw.c +++ b/tests/vb20_verify_fw.c @@ -75,9 +75,9 @@ static void save_if_needed(struct vb2_context *c) c->flags &= ~VB2_CONTEXT_NVDATA_CHANGED; } - if (c->flags & VB2_CONTEXT_SECDATA_CHANGED) { + if (c->flags & VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED) { // TODO: implement - c->flags &= ~VB2_CONTEXT_SECDATA_CHANGED; + c->flags &= ~VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED; } } @@ -164,10 +164,11 @@ int main(int argc, char *argv[]) ctx.workbuf_size = sizeof(workbuf); /* Initialize secure context */ - rv = vb2api_secdata_create(&ctx); + rv = vb2api_secdata_firmware_create(&ctx); if (rv) { fprintf(stderr, - "error: vb2api_secdata_create() failed (%d)\n", rv); + "error: vb2api_secdata_firmware_create() failed (%d)\n", + rv); return 1; } diff --git a/tests/vb21_api_tests.c b/tests/vb21_api_tests.c index 8ea9bf3a..b7df9449 100644 --- a/tests/vb21_api_tests.c +++ b/tests/vb21_api_tests.c @@ -81,8 +81,8 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdata_create(&ctx); - vb2_secdata_init(&ctx); + vb2api_secdata_firmware_create(&ctx); + vb2_secdata_firmware_init(&ctx); memset(&hwcrypto_emulation_dc, 0, sizeof(hwcrypto_emulation_dc)); retval_hwcrypto = VB2_SUCCESS; diff --git a/tests/vb21_misc_tests.c b/tests/vb21_misc_tests.c index 383e3d02..fa65eb00 100644 --- a/tests/vb21_misc_tests.c +++ b/tests/vb21_misc_tests.c @@ -78,8 +78,8 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdata_create(&ctx); - vb2_secdata_init(&ctx); + vb2api_secdata_firmware_create(&ctx); + vb2_secdata_firmware_init(&ctx); mock_read_res_fail_on_call = 0; mock_unpack_key_retval = VB2_SUCCESS; @@ -88,7 +88,8 @@ static void reset_common_data(enum reset_type t) /* Set up mock data for verifying keyblock */ sd->fw_version_secdata = 0x20002; - vb2_secdata_set(&ctx, VB2_SECDATA_VERSIONS, sd->fw_version_secdata); + vb2_secdata_firmware_set(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, + sd->fw_version_secdata); gbb.rootkey_offset = vb2_offset_of(&mock_gbb, &mock_gbb.rootkey); gbb.rootkey_size = sizeof(mock_gbb.rootkey_data); @@ -373,7 +374,7 @@ static void load_preamble_tests(void) pre->fw_version = 3; TEST_SUCC(vb21_load_fw_preamble(&ctx), "preamble version roll forward"); - vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, &v); TEST_EQ(v, 0x20003, "roll forward"); /* Newer version without result success doesn't roll forward */ @@ -382,7 +383,7 @@ static void load_preamble_tests(void) sd->last_fw_result = VB2_FW_RESULT_UNKNOWN; TEST_SUCC(vb21_load_fw_preamble(&ctx), "preamble version no roll forward 1"); - vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, &v); TEST_EQ(v, 0x20002, "no roll forward"); /* Newer version with success but for other slot doesn't roll forward */ @@ -391,7 +392,7 @@ static void load_preamble_tests(void) sd->last_fw_slot = 1; TEST_SUCC(vb21_load_fw_preamble(&ctx), "preamble version no roll forward 2"); - vb2_secdata_get(&ctx, VB2_SECDATA_VERSIONS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_VERSIONS, &v); TEST_EQ(v, 0x20002, "no roll forward"); } diff --git a/tests/vb2_api_tests.c b/tests/vb2_api_tests.c index b4db22b8..6429ca31 100644 --- a/tests/vb2_api_tests.c +++ b/tests/vb2_api_tests.c @@ -59,8 +59,8 @@ static void reset_common_data(enum reset_type t) vb2_nv_init(&ctx); - vb2api_secdata_create(&ctx); - vb2_secdata_init(&ctx); + vb2api_secdata_firmware_create(&ctx); + vb2_secdata_firmware_init(&ctx); force_dev_mode = 0; retval_vb2_fw_parse_gbb = VB2_SUCCESS; @@ -104,14 +104,17 @@ vb2_error_t vb2_select_fw_slot(struct vb2_context *c) static void misc_tests(void) { - /* Test secdata passthru functions */ + /* Test secdata_firmware passthru functions */ reset_common_data(FOR_MISC); - /* Corrupt secdata so initial check will fail */ - ctx.secdata[0] ^= 0x42; - TEST_EQ(vb2api_secdata_check(&ctx), VB2_ERROR_SECDATA_CRC, - "secdata check"); - TEST_SUCC(vb2api_secdata_create(&ctx), "secdata create"); - TEST_SUCC(vb2api_secdata_check(&ctx), "secdata check 2"); + /* Corrupt secdata_firmware so initial check will fail */ + ctx.secdata_firmware[0] ^= 0x42; + TEST_EQ(vb2api_secdata_firmware_check(&ctx), + VB2_ERROR_SECDATA_FIRMWARE_CRC, + "secdata_firmware check"); + TEST_SUCC(vb2api_secdata_firmware_create(&ctx), + "secdata_firmware create"); + TEST_SUCC(vb2api_secdata_firmware_check(&ctx), + "secdata_firmware check 2"); /* Test fail passthru */ reset_common_data(FOR_MISC); @@ -165,19 +168,19 @@ static void phase1_tests(void) 0, " display available SD flag"); reset_common_data(FOR_MISC); - ctx.secdata[0] ^= 0x42; + ctx.secdata_firmware[0] ^= 0x42; TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_RECOVERY, - "phase1 secdata"); - TEST_EQ(sd->recovery_reason, VB2_RECOVERY_SECDATA_INIT, + "phase1 secdata_firmware"); + TEST_EQ(sd->recovery_reason, VB2_RECOVERY_SECDATA_FIRMWARE_INIT, " recovery reason"); TEST_NEQ(ctx.flags & VB2_CONTEXT_RECOVERY_MODE, 0, " recovery flag"); TEST_NEQ(ctx.flags & VB2_CONTEXT_CLEAR_RAM, 0, " clear ram flag"); - /* Test secdata-requested reboot */ + /* Test secdata_firmware-requested reboot */ reset_common_data(FOR_MISC); ctx.flags |= VB2_CONTEXT_SECDATA_WANTS_REBOOT; TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_SECDATA_REBOOT, - "phase1 secdata reboot normal"); + "phase1 secdata_firmware reboot normal"); TEST_EQ(sd->recovery_reason, 0, " recovery reason"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), 1, " tpm reboot request"); @@ -186,7 +189,8 @@ static void phase1_tests(void) reset_common_data(FOR_MISC); vb2_nv_set(&ctx, VB2_NV_TPM_REQUESTED_REBOOT, 1); - TEST_SUCC(vb2api_fw_phase1(&ctx), "phase1 secdata reboot back normal"); + TEST_SUCC(vb2api_fw_phase1(&ctx), + "phase1 secdata_firmware reboot back normal"); TEST_EQ(sd->recovery_reason, 0, " recovery reason"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), 0, " tpm reboot request"); @@ -195,9 +199,10 @@ static void phase1_tests(void) reset_common_data(FOR_MISC); ctx.flags |= VB2_CONTEXT_SECDATA_WANTS_REBOOT; - memset(ctx.secdata, 0, sizeof(ctx.secdata)); + memset(ctx.secdata_firmware, 0, sizeof(ctx.secdata_firmware)); TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_SECDATA_REBOOT, - "phase1 secdata reboot normal, secdata blank"); + "phase1 secdata_firmware reboot normal, " + "secdata_firmware blank"); TEST_EQ(sd->recovery_reason, 0, " recovery reason"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), 1, " tpm reboot request"); @@ -208,7 +213,7 @@ static void phase1_tests(void) ctx.flags |= VB2_CONTEXT_SECDATA_WANTS_REBOOT; vb2_nv_set(&ctx, VB2_NV_TPM_REQUESTED_REBOOT, 1); TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_RECOVERY, - "phase1 secdata reboot normal again"); + "phase1 secdata_firmware reboot normal again"); TEST_EQ(sd->recovery_reason, VB2_RECOVERY_RO_TPM_REBOOT, " recovery reason"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), @@ -220,7 +225,7 @@ static void phase1_tests(void) ctx.flags |= VB2_CONTEXT_SECDATA_WANTS_REBOOT; vb2_nv_set(&ctx, VB2_NV_RECOVERY_REQUEST, VB2_RECOVERY_RO_UNSPECIFIED); TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_SECDATA_REBOOT, - "phase1 secdata reboot recovery"); + "phase1 secdata_firmware reboot recovery"); /* Recovery reason isn't set this boot because we're rebooting first */ TEST_EQ(sd->recovery_reason, 0, " recovery reason not set THIS boot"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), @@ -232,7 +237,7 @@ static void phase1_tests(void) vb2_nv_set(&ctx, VB2_NV_TPM_REQUESTED_REBOOT, 1); vb2_nv_set(&ctx, VB2_NV_RECOVERY_REQUEST, VB2_RECOVERY_RO_UNSPECIFIED); TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_RECOVERY, - "phase1 secdata reboot back recovery"); + "phase1 secdata_firmware reboot back recovery"); TEST_EQ(sd->recovery_reason, VB2_RECOVERY_RO_UNSPECIFIED, " recovery reason"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), @@ -245,7 +250,7 @@ static void phase1_tests(void) vb2_nv_set(&ctx, VB2_NV_TPM_REQUESTED_REBOOT, 1); vb2_nv_set(&ctx, VB2_NV_RECOVERY_REQUEST, VB2_RECOVERY_RO_UNSPECIFIED); TEST_EQ(vb2api_fw_phase1(&ctx), VB2_ERROR_API_PHASE1_RECOVERY, - "phase1 secdata reboot recovery again"); + "phase1 secdata_firmware reboot recovery again"); TEST_EQ(sd->recovery_reason, VB2_RECOVERY_RO_UNSPECIFIED, " recovery reason"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_TPM_REQUESTED_REBOOT), diff --git a/tests/vb2_misc_tests.c b/tests/vb2_misc_tests.c index 8e96f1c0..43c97e4e 100644 --- a/tests/vb2_misc_tests.c +++ b/tests/vb2_misc_tests.c @@ -44,8 +44,8 @@ static void reset_common_data(void) vb2_nv_init(&ctx); - vb2api_secdata_create(&ctx); - vb2_secdata_init(&ctx); + vb2api_secdata_firmware_create(&ctx); + vb2_secdata_firmware_init(&ctx); mock_tpm_clear_called = 0; mock_tpm_clear_retval = VB2_SUCCESS; @@ -355,9 +355,10 @@ static void dev_switch_tests(void) /* Dev mode */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, - (VB2_SECDATA_FLAG_DEV_MODE | - VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER)); + vb2_secdata_firmware_set( + &ctx, VB2_SECDATA_FIRMWARE_FLAGS, + (VB2_SECDATA_FIRMWARE_FLAG_DEV_MODE | + VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER)); TEST_SUCC(vb2_check_dev_switch(&ctx), "dev mode on"); TEST_NEQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd in dev"); TEST_NEQ(ctx.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, " ctx in dev"); @@ -381,28 +382,30 @@ static void dev_switch_tests(void) /* Normal-dev transition clears TPM */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, VB2_SECDATA_FLAG_DEV_MODE); + vb2_secdata_firmware_set(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, + VB2_SECDATA_FIRMWARE_FLAG_DEV_MODE); TEST_SUCC(vb2_check_dev_switch(&ctx), "to dev mode"); TEST_EQ(mock_tpm_clear_called, 1, " tpm clear"); - vb2_secdata_get(&ctx, VB2_SECDATA_FLAGS, &v); - TEST_EQ(v, (VB2_SECDATA_FLAG_DEV_MODE | - VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER), + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, &v); + TEST_EQ(v, (VB2_SECDATA_FIRMWARE_FLAG_DEV_MODE | + VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER), " last boot developer now"); /* Dev-normal transition clears TPM too */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, - VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER); + vb2_secdata_firmware_set(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, + VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER); TEST_SUCC(vb2_check_dev_switch(&ctx), "from dev mode"); TEST_EQ(mock_tpm_clear_called, 1, " tpm clear"); - vb2_secdata_get(&ctx, VB2_SECDATA_FLAGS, &v); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, &v); TEST_EQ(v, 0, " last boot not developer now"); /* Disable dev mode */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, - (VB2_SECDATA_FLAG_DEV_MODE | - VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER)); + vb2_secdata_firmware_set( + &ctx, VB2_SECDATA_FIRMWARE_FLAGS, + (VB2_SECDATA_FIRMWARE_FLAG_DEV_MODE | + VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER)); vb2_nv_set(&ctx, VB2_NV_DISABLE_DEV_REQUEST, 1); TEST_SUCC(vb2_check_dev_switch(&ctx), "disable dev request"); TEST_EQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd not in dev"); @@ -414,30 +417,32 @@ static void dev_switch_tests(void) gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON; TEST_SUCC(vb2_check_dev_switch(&ctx), "dev on via gbb"); TEST_NEQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd in dev"); - vb2_secdata_get(&ctx, VB2_SECDATA_FLAGS, &v); - TEST_EQ(v, VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER, - " doesn't set dev on in secdata but does set last boot dev"); + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, &v); + TEST_EQ(v, VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER, + " doesn't set dev on in secdata_firmware " + "but does set last boot dev"); TEST_EQ(mock_tpm_clear_called, 1, " tpm clear"); /* Request disable by ctx flag */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, - (VB2_SECDATA_FLAG_DEV_MODE | - VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER)); + vb2_secdata_firmware_set( + &ctx, VB2_SECDATA_FIRMWARE_FLAGS, + (VB2_SECDATA_FIRMWARE_FLAG_DEV_MODE | + VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER)); ctx.flags |= VB2_CONTEXT_DISABLE_DEVELOPER_MODE; TEST_SUCC(vb2_check_dev_switch(&ctx), "disable dev on ctx request"); TEST_EQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd not in dev"); /* Simulate clear owner failure */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, - VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER); + vb2_secdata_firmware_set(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, + VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER); mock_tpm_clear_retval = VB2_ERROR_EX_TPM_CLEAR_OWNER; TEST_EQ(vb2_check_dev_switch(&ctx), VB2_ERROR_EX_TPM_CLEAR_OWNER, "tpm clear fail"); TEST_EQ(mock_tpm_clear_called, 1, " tpm clear"); - vb2_secdata_get(&ctx, VB2_SECDATA_FLAGS, &v); - TEST_EQ(v, VB2_SECDATA_FLAG_LAST_BOOT_DEVELOPER, + vb2_secdata_firmware_get(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, &v); + TEST_EQ(v, VB2_SECDATA_FIRMWARE_FLAG_LAST_BOOT_DEVELOPER, " last boot still developer"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_RECOVERY_REQUEST), VB2_RECOVERY_TPM_CLEAR_OWNER, " requests recovery"); @@ -446,30 +451,33 @@ static void dev_switch_tests(void) /* * Secdata failure in normal mode fails and shows dev=0 even if dev - * mode was on in the (inaccessible) secdata. + * mode was on in the (inaccessible) secdata_firmware. */ reset_common_data(); - vb2_secdata_set(&ctx, VB2_SECDATA_FLAGS, VB2_SECDATA_FLAG_DEV_MODE); - sd->status &= ~VB2_SD_STATUS_SECDATA_INIT; - TEST_EQ(vb2_check_dev_switch(&ctx), VB2_ERROR_SECDATA_GET_UNINITIALIZED, - "secdata fail normal"); + vb2_secdata_firmware_set(&ctx, VB2_SECDATA_FIRMWARE_FLAGS, + VB2_SECDATA_FIRMWARE_FLAG_DEV_MODE); + sd->status &= ~VB2_SD_STATUS_SECDATA_FIRMWARE_INIT; + TEST_EQ(vb2_check_dev_switch(&ctx), + VB2_ERROR_SECDATA_FIRMWARE_GET_UNINITIALIZED, + "secdata_firmware fail normal"); TEST_EQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd not in dev"); TEST_EQ(ctx.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, " ctx not in dev"); /* Secdata failure in recovery mode continues */ reset_common_data(); ctx.flags |= VB2_CONTEXT_RECOVERY_MODE; - sd->status &= ~VB2_SD_STATUS_SECDATA_INIT; - TEST_SUCC(vb2_check_dev_switch(&ctx), "secdata fail recovery"); + sd->status &= ~VB2_SD_STATUS_SECDATA_FIRMWARE_INIT; + TEST_SUCC(vb2_check_dev_switch(&ctx), "secdata_firmware fail recovery"); TEST_EQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd not in dev"); TEST_EQ(ctx.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, " ctx not in dev"); /* And doesn't check or clear dev disable request */ reset_common_data(); ctx.flags |= VB2_CONTEXT_RECOVERY_MODE; - sd->status &= ~VB2_SD_STATUS_SECDATA_INIT; + sd->status &= ~VB2_SD_STATUS_SECDATA_FIRMWARE_INIT; vb2_nv_set(&ctx, VB2_NV_DISABLE_DEV_REQUEST, 1); - TEST_SUCC(vb2_check_dev_switch(&ctx), "secdata fail recovery disable"); + TEST_SUCC(vb2_check_dev_switch(&ctx), + "secdata_firmware fail recovery disable"); TEST_EQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd not in dev"); TEST_EQ(ctx.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, " ctx not in dev"); TEST_EQ(vb2_nv_get(&ctx, VB2_NV_DISABLE_DEV_REQUEST), @@ -478,9 +486,10 @@ static void dev_switch_tests(void) /* Can still override with GBB flag */ reset_common_data(); ctx.flags |= VB2_CONTEXT_RECOVERY_MODE; - sd->status &= ~VB2_SD_STATUS_SECDATA_INIT; + sd->status &= ~VB2_SD_STATUS_SECDATA_FIRMWARE_INIT; gbb.flags |= VB2_GBB_FLAG_FORCE_DEV_SWITCH_ON; - TEST_SUCC(vb2_check_dev_switch(&ctx), "secdata fail recovery gbb"); + TEST_SUCC(vb2_check_dev_switch(&ctx), + "secdata_firmware fail recovery gbb"); TEST_NEQ(sd->flags & VB2_SD_FLAG_DEV_MODE_ENABLED, 0, " sd in dev"); TEST_NEQ(ctx.flags & VB2_CONTEXT_DEVELOPER_MODE, 0, " ctx in dev"); TEST_EQ(mock_tpm_clear_called, 1, " tpm clear"); diff --git a/tests/vb2_secdata_firmware_tests.c b/tests/vb2_secdata_firmware_tests.c new file mode 100644 index 00000000..3564cfa5 --- /dev/null +++ b/tests/vb2_secdata_firmware_tests.c @@ -0,0 +1,150 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Tests for firmware secure storage library. + */ + +#include "2api.h" +#include "2common.h" +#include "2crc8.h" +#include "2misc.h" +#include "2secdata.h" +#include "2sysincludes.h" +#include "test_common.h" +#include "vboot_common.h" + +static void test_changed(struct vb2_context *c, int changed, const char *why) +{ + if (changed) + TEST_NEQ(c->flags & VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED, + 0, why); + else + TEST_EQ(c->flags & VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED, + 0, why); + + c->flags &= ~VB2_CONTEXT_SECDATA_FIRMWARE_CHANGED; +}; + +static void secdata_firmware_test(void) +{ + uint8_t workbuf[VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE] + __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); + struct vb2_context c = { + .flags = 0, + .workbuf = workbuf, + .workbuf_size = sizeof(workbuf), + }; + struct vb2_secdata_firmware *sec = + (struct vb2_secdata_firmware *)c.secdata_firmware; + struct vb2_shared_data *sd = vb2_get_sd(&c); + uint32_t v = 1; + + /* Check size constant */ + TEST_EQ(VB2_SECDATA_FIRMWARE_SIZE, sizeof(struct vb2_secdata_firmware), + "Struct size constant"); + + /* Blank data is invalid */ + memset(c.secdata_firmware, 0xa6, sizeof(c.secdata_firmware)); + TEST_EQ(vb2api_secdata_firmware_check(&c), + VB2_ERROR_SECDATA_FIRMWARE_CRC, "Check blank CRC"); + TEST_EQ(vb2_secdata_firmware_init(&c), + VB2_ERROR_SECDATA_FIRMWARE_CRC, "Init blank CRC"); + + /* Ensure zeroed buffers are invalid (coreboot relies on this) */ + memset(c.secdata_firmware, 0, sizeof(c.secdata_firmware)); + TEST_EQ(vb2_secdata_firmware_init(&c), + VB2_ERROR_SECDATA_FIRMWARE_VERSION, + "Zeroed buffer (invalid version)"); + + /* Try with bad version */ + TEST_SUCC(vb2api_secdata_firmware_create(&c), "Create"); + sec->struct_version -= 1; + sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdata_firmware, crc8)); + TEST_EQ(vb2api_secdata_firmware_check(&c), + VB2_ERROR_SECDATA_FIRMWARE_VERSION, "Check invalid version"); + TEST_EQ(vb2_secdata_firmware_init(&c), + VB2_ERROR_SECDATA_FIRMWARE_VERSION, "Init invalid version"); + + /* Create good data */ + TEST_SUCC(vb2api_secdata_firmware_create(&c), "Create"); + TEST_SUCC(vb2api_secdata_firmware_check(&c), "Check created CRC"); + TEST_SUCC(vb2_secdata_firmware_init(&c), "Init created CRC"); + TEST_NEQ(sd->status & VB2_SD_STATUS_SECDATA_FIRMWARE_INIT, 0, + "Init set SD status"); + sd->status &= ~VB2_SD_STATUS_SECDATA_FIRMWARE_INIT; + test_changed(&c, 1, "Create changes data"); + + /* Now corrupt it */ + c.secdata_firmware[2]++; + TEST_EQ(vb2api_secdata_firmware_check(&c), + VB2_ERROR_SECDATA_FIRMWARE_CRC, "Check invalid CRC"); + TEST_EQ(vb2_secdata_firmware_init(&c), + VB2_ERROR_SECDATA_FIRMWARE_CRC, "Init invalid CRC"); + + /* Read/write flags */ + vb2api_secdata_firmware_create(&c); + vb2_secdata_firmware_init(&c); + c.flags = 0; + TEST_SUCC(vb2_secdata_firmware_get(&c, VB2_SECDATA_FIRMWARE_FLAGS, &v), + "Get flags"); + TEST_EQ(v, 0, "Flags created 0"); + test_changed(&c, 0, "Get doesn't change data"); + TEST_SUCC(vb2_secdata_firmware_set(&c, VB2_SECDATA_FIRMWARE_FLAGS, + 0x12), + "Set flags"); + test_changed(&c, 1, "Set changes data"); + TEST_SUCC(vb2_secdata_firmware_set(&c, VB2_SECDATA_FIRMWARE_FLAGS, + 0x12), + "Set flags 2"); + test_changed(&c, 0, "Set again doesn't change data"); + TEST_SUCC(vb2_secdata_firmware_get(&c, VB2_SECDATA_FIRMWARE_FLAGS, &v), + "Get flags 2"); + TEST_EQ(v, 0x12, "Flags changed"); + TEST_EQ(vb2_secdata_firmware_set(&c, VB2_SECDATA_FIRMWARE_FLAGS, 0x100), + VB2_ERROR_SECDATA_FIRMWARE_SET_FLAGS, "Bad flags"); + + /* Read/write versions */ + TEST_SUCC(vb2_secdata_firmware_get(&c, VB2_SECDATA_FIRMWARE_VERSIONS, + &v), + "Get versions"); + TEST_EQ(v, 0, "Versions created 0"); + test_changed(&c, 0, "Get doesn't change data"); + TEST_SUCC(vb2_secdata_firmware_set(&c, VB2_SECDATA_FIRMWARE_VERSIONS, + 0x123456ff), + "Set versions"); + test_changed(&c, 1, "Set changes data"); + TEST_SUCC(vb2_secdata_firmware_set(&c, VB2_SECDATA_FIRMWARE_VERSIONS, + 0x123456ff), + "Set versions 2"); + test_changed(&c, 0, "Set again doesn't change data"); + TEST_SUCC(vb2_secdata_firmware_get(&c, VB2_SECDATA_FIRMWARE_VERSIONS, &v), + "Get versions 2"); + TEST_EQ(v, 0x123456ff, "Versions changed"); + + /* Invalid field fails */ + TEST_EQ(vb2_secdata_firmware_get(&c, -1, &v), + VB2_ERROR_SECDATA_FIRMWARE_GET_PARAM, "Get invalid"); + TEST_EQ(vb2_secdata_firmware_set(&c, -1, 456), + VB2_ERROR_SECDATA_FIRMWARE_SET_PARAM, "Set invalid"); + test_changed(&c, 0, "Set invalid field doesn't change data"); + + /* Read/write uninitialized data fails */ + sd->status &= ~VB2_SD_STATUS_SECDATA_FIRMWARE_INIT; + TEST_EQ(vb2_secdata_firmware_get(&c, VB2_SECDATA_FIRMWARE_VERSIONS, &v), + VB2_ERROR_SECDATA_FIRMWARE_GET_UNINITIALIZED, + "Get uninitialized"); + test_changed(&c, 0, "Get uninitialized doesn't change data"); + TEST_EQ(vb2_secdata_firmware_set(&c, VB2_SECDATA_FIRMWARE_VERSIONS, + 0x123456ff), + VB2_ERROR_SECDATA_FIRMWARE_SET_UNINITIALIZED, + "Set uninitialized"); + test_changed(&c, 0, "Set uninitialized doesn't change data"); +} + +int main(int argc, char* argv[]) +{ + secdata_firmware_test(); + + return gTestSuccess ? 0 : 255; +} diff --git a/tests/vb2_secdata_kernel_tests.c b/tests/vb2_secdata_kernel_tests.c new file mode 100644 index 00000000..c1ae2e16 --- /dev/null +++ b/tests/vb2_secdata_kernel_tests.c @@ -0,0 +1,135 @@ +/* Copyright 2015 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Tests for kernel secure storage library. + */ + +#include "2api.h" +#include "2common.h" +#include "2crc8.h" +#include "2misc.h" +#include "2secdata.h" +#include "2sysincludes.h" +#include "test_common.h" +#include "vboot_common.h" + +static void test_changed(struct vb2_context *c, int changed, const char *why) +{ + if (changed) + TEST_NEQ(c->flags & VB2_CONTEXT_SECDATA_KERNEL_CHANGED, 0, why); + else + TEST_EQ(c->flags & VB2_CONTEXT_SECDATA_KERNEL_CHANGED, 0, why); + + c->flags &= ~VB2_CONTEXT_SECDATA_KERNEL_CHANGED; +}; + +static void secdata_kernel_test(void) +{ + uint8_t workbuf[VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE] + __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); + struct vb2_context c = { + .flags = 0, + .workbuf = workbuf, + .workbuf_size = sizeof(workbuf), + }; + struct vb2_secdata_kernel *sec = + (struct vb2_secdata_kernel *)c.secdata_kernel; + struct vb2_shared_data *sd = vb2_get_sd(&c); + uint32_t v = 1; + + /* Check size constant */ + TEST_EQ(VB2_SECDATA_KERNEL_SIZE, sizeof(struct vb2_secdata_kernel), + "Struct size constant"); + + /* Blank data is invalid */ + memset(c.secdata_kernel, 0xa6, sizeof(c.secdata_kernel)); + TEST_EQ(vb2api_secdata_kernel_check(&c), + VB2_ERROR_SECDATA_KERNEL_CRC, "Check blank CRC"); + TEST_EQ(vb2_secdata_kernel_init(&c), + VB2_ERROR_SECDATA_KERNEL_CRC, "Init blank CRC"); + + /* Ensure zeroed buffers are invalid */ + memset(c.secdata_kernel, 0, sizeof(c.secdata_kernel)); + TEST_EQ(vb2_secdata_kernel_init(&c), VB2_ERROR_SECDATA_KERNEL_VERSION, + "Zeroed buffer (invalid version)"); + + /* Try with bad version */ + TEST_SUCC(vb2api_secdata_kernel_create(&c), "Create"); + sec->struct_version -= 1; + sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdata_kernel, crc8)); + TEST_EQ(vb2api_secdata_kernel_check(&c), + VB2_ERROR_SECDATA_KERNEL_VERSION, "Check invalid version"); + TEST_EQ(vb2_secdata_kernel_init(&c), + VB2_ERROR_SECDATA_KERNEL_VERSION, "Init invalid version"); + + /* Create good data */ + TEST_SUCC(vb2api_secdata_kernel_create(&c), "Create"); + TEST_SUCC(vb2api_secdata_kernel_check(&c), "Check created CRC"); + TEST_SUCC(vb2_secdata_kernel_init(&c), "Init created CRC"); + TEST_NEQ(sd->status & VB2_SD_STATUS_SECDATA_KERNEL_INIT, 0, + "Init set SD status"); + sd->status &= ~VB2_SD_STATUS_SECDATA_KERNEL_INIT; + test_changed(&c, 1, "Create changes data"); + + /* Now corrupt it */ + c.secdata_kernel[2]++; + TEST_EQ(vb2api_secdata_kernel_check(&c), + VB2_ERROR_SECDATA_KERNEL_CRC, "Check invalid CRC"); + TEST_EQ(vb2_secdata_kernel_init(&c), + VB2_ERROR_SECDATA_KERNEL_CRC, "Init invalid CRC"); + + /* Make sure UID is checked */ + + vb2api_secdata_kernel_create(&c); + sec->uid++; + sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdata_kernel, crc8)); + TEST_EQ(vb2_secdata_kernel_init(&c), VB2_ERROR_SECDATA_KERNEL_UID, + "Init invalid struct UID"); + + /* Read/write versions */ + vb2api_secdata_kernel_create(&c); + vb2_secdata_kernel_init(&c); + c.flags = 0; + TEST_SUCC(vb2_secdata_kernel_get(&c, VB2_SECDATA_KERNEL_VERSIONS, &v), + "Get versions"); + TEST_EQ(v, 0, "Versions created 0"); + test_changed(&c, 0, "Get doesn't change data"); + TEST_SUCC(vb2_secdata_kernel_set(&c, VB2_SECDATA_KERNEL_VERSIONS, + 0x123456ff), + "Set versions"); + test_changed(&c, 1, "Set changes data"); + TEST_SUCC(vb2_secdata_kernel_set(&c, VB2_SECDATA_KERNEL_VERSIONS, + 0x123456ff), + "Set versions 2"); + test_changed(&c, 0, "Set again doesn't change data"); + TEST_SUCC(vb2_secdata_kernel_get(&c, VB2_SECDATA_KERNEL_VERSIONS, &v), + "Get versions 2"); + TEST_EQ(v, 0x123456ff, "Versions changed"); + + /* Invalid field fails */ + TEST_EQ(vb2_secdata_kernel_get(&c, -1, &v), + VB2_ERROR_SECDATA_KERNEL_GET_PARAM, "Get invalid"); + TEST_EQ(vb2_secdata_kernel_set(&c, -1, 456), + VB2_ERROR_SECDATA_KERNEL_SET_PARAM, "Set invalid"); + test_changed(&c, 0, "Set invalid field doesn't change data"); + + /* Read/write uninitialized data fails */ + sd->status &= ~VB2_SD_STATUS_SECDATA_KERNEL_INIT; + TEST_EQ(vb2_secdata_kernel_get(&c, VB2_SECDATA_KERNEL_VERSIONS, &v), + VB2_ERROR_SECDATA_KERNEL_GET_UNINITIALIZED, + "Get uninitialized"); + test_changed(&c, 0, "Get uninitialized doesn't change data"); + TEST_EQ(vb2_secdata_kernel_set(&c, VB2_SECDATA_KERNEL_VERSIONS, + 0x123456ff), + VB2_ERROR_SECDATA_KERNEL_SET_UNINITIALIZED, + "Set uninitialized"); + test_changed(&c, 0, "Set uninitialized doesn't change data"); +} + +int main(int argc, char* argv[]) +{ + secdata_kernel_test(); + + return gTestSuccess ? 0 : 255; +} diff --git a/tests/vb2_secdata_tests.c b/tests/vb2_secdata_tests.c deleted file mode 100644 index fca31d4d..00000000 --- a/tests/vb2_secdata_tests.c +++ /dev/null @@ -1,134 +0,0 @@ -/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - * - * Tests for firmware secure storage library. - */ - -#include "2api.h" -#include "2common.h" -#include "2crc8.h" -#include "2misc.h" -#include "2secdata.h" -#include "2sysincludes.h" -#include "test_common.h" -#include "vboot_common.h" - -static void test_changed(struct vb2_context *c, int changed, const char *why) -{ - if (changed) - TEST_NEQ(c->flags & VB2_CONTEXT_SECDATA_CHANGED, 0, why); - else - TEST_EQ(c->flags & VB2_CONTEXT_SECDATA_CHANGED, 0, why); - - c->flags &= ~VB2_CONTEXT_SECDATA_CHANGED; -}; - -static void secdata_test(void) -{ - uint8_t workbuf[VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE] - __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); - struct vb2_context c = { - .flags = 0, - .workbuf = workbuf, - .workbuf_size = sizeof(workbuf), - }; - struct vb2_secdata *sec = (struct vb2_secdata *)c.secdata; - struct vb2_shared_data *sd = vb2_get_sd(&c); - uint32_t v = 1; - - /* Check size constant */ - TEST_EQ(VB2_SECDATA_SIZE, sizeof(struct vb2_secdata), - "Struct size constant"); - - /* Blank data is invalid */ - memset(c.secdata, 0xa6, sizeof(c.secdata)); - TEST_EQ(vb2api_secdata_check(&c), - VB2_ERROR_SECDATA_CRC, "Check blank CRC"); - TEST_EQ(vb2_secdata_init(&c), - VB2_ERROR_SECDATA_CRC, "Init blank CRC"); - - /* Ensure zeroed buffers are invalid (coreboot relies on this) */ - memset(c.secdata, 0, sizeof(c.secdata)); - TEST_EQ(vb2_secdata_init(&c), VB2_ERROR_SECDATA_VERSION, - "Zeroed buffer (invalid version)"); - - /* Try with bad version */ - TEST_SUCC(vb2api_secdata_create(&c), "Create"); - sec->struct_version -= 1; - sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdata, crc8)); - TEST_EQ(vb2api_secdata_check(&c), - VB2_ERROR_SECDATA_VERSION, "Check invalid version"); - TEST_EQ(vb2_secdata_init(&c), - VB2_ERROR_SECDATA_VERSION, "Init invalid version"); - - /* Create good data */ - TEST_SUCC(vb2api_secdata_create(&c), "Create"); - TEST_SUCC(vb2api_secdata_check(&c), "Check created CRC"); - TEST_SUCC(vb2_secdata_init(&c), "Init created CRC"); - TEST_NEQ(sd->status & VB2_SD_STATUS_SECDATA_INIT, 0, - "Init set SD status"); - sd->status &= ~VB2_SD_STATUS_SECDATA_INIT; - test_changed(&c, 1, "Create changes data"); - - /* Now corrupt it */ - c.secdata[2]++; - TEST_EQ(vb2api_secdata_check(&c), - VB2_ERROR_SECDATA_CRC, "Check invalid CRC"); - TEST_EQ(vb2_secdata_init(&c), - VB2_ERROR_SECDATA_CRC, "Init invalid CRC"); - - /* Read/write flags */ - vb2api_secdata_create(&c); - vb2_secdata_init(&c); - c.flags = 0; - TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_FLAGS, &v), "Get flags"); - TEST_EQ(v, 0, "Flags created 0"); - test_changed(&c, 0, "Get doesn't change data"); - TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x12), "Set flags"); - test_changed(&c, 1, "Set changes data"); - TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x12), "Set flags 2"); - test_changed(&c, 0, "Set again doesn't change data"); - TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_FLAGS, &v), "Get flags 2"); - TEST_EQ(v, 0x12, "Flags changed"); - TEST_EQ(vb2_secdata_set(&c, VB2_SECDATA_FLAGS, 0x100), - VB2_ERROR_SECDATA_SET_FLAGS, "Bad flags"); - - /* Read/write versions */ - TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v), - "Get versions"); - TEST_EQ(v, 0, "Versions created 0"); - test_changed(&c, 0, "Get doesn't change data"); - TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff), - "Set versions"); - test_changed(&c, 1, "Set changes data"); - TEST_SUCC(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff), - "Set versions 2"); - test_changed(&c, 0, "Set again doesn't change data"); - TEST_SUCC(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v), - "Get versions 2"); - TEST_EQ(v, 0x123456ff, "Versions changed"); - - /* Invalid field fails */ - TEST_EQ(vb2_secdata_get(&c, -1, &v), - VB2_ERROR_SECDATA_GET_PARAM, "Get invalid"); - TEST_EQ(vb2_secdata_set(&c, -1, 456), - VB2_ERROR_SECDATA_SET_PARAM, "Set invalid"); - test_changed(&c, 0, "Set invalid field doesn't change data"); - - /* Read/write uninitialized data fails */ - sd->status &= ~VB2_SD_STATUS_SECDATA_INIT; - TEST_EQ(vb2_secdata_get(&c, VB2_SECDATA_VERSIONS, &v), - VB2_ERROR_SECDATA_GET_UNINITIALIZED, "Get uninitialized"); - test_changed(&c, 0, "Get uninitialized doesn't change data"); - TEST_EQ(vb2_secdata_set(&c, VB2_SECDATA_VERSIONS, 0x123456ff), - VB2_ERROR_SECDATA_SET_UNINITIALIZED, "Set uninitialized"); - test_changed(&c, 0, "Set uninitialized doesn't change data"); -} - -int main(int argc, char* argv[]) -{ - secdata_test(); - - return gTestSuccess ? 0 : 255; -} diff --git a/tests/vb2_secdatak_tests.c b/tests/vb2_secdatak_tests.c deleted file mode 100644 index 45803866..00000000 --- a/tests/vb2_secdatak_tests.c +++ /dev/null @@ -1,128 +0,0 @@ -/* Copyright 2015 The Chromium OS Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - * - * Tests for kernel secure storage library. - */ - -#include "2api.h" -#include "2common.h" -#include "2crc8.h" -#include "2misc.h" -#include "2secdata.h" -#include "2sysincludes.h" -#include "test_common.h" -#include "vboot_common.h" - -static void test_changed(struct vb2_context *c, int changed, const char *why) -{ - if (changed) - TEST_NEQ(c->flags & VB2_CONTEXT_SECDATAK_CHANGED, 0, why); - else - TEST_EQ(c->flags & VB2_CONTEXT_SECDATAK_CHANGED, 0, why); - - c->flags &= ~VB2_CONTEXT_SECDATAK_CHANGED; -}; - -static void secdatak_test(void) -{ - uint8_t workbuf[VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE] - __attribute__ ((aligned (VB2_WORKBUF_ALIGN))); - struct vb2_context c = { - .flags = 0, - .workbuf = workbuf, - .workbuf_size = sizeof(workbuf), - }; - struct vb2_secdatak *sec = (struct vb2_secdatak *)c.secdatak; - struct vb2_shared_data *sd = vb2_get_sd(&c); - uint32_t v = 1; - - /* Check size constant */ - TEST_EQ(VB2_SECDATAK_SIZE, sizeof(struct vb2_secdatak), - "Struct size constant"); - - /* Blank data is invalid */ - memset(c.secdatak, 0xa6, sizeof(c.secdatak)); - TEST_EQ(vb2api_secdatak_check(&c), - VB2_ERROR_SECDATAK_CRC, "Check blank CRC"); - TEST_EQ(vb2_secdatak_init(&c), - VB2_ERROR_SECDATAK_CRC, "Init blank CRC"); - - /* Ensure zeroed buffers are invalid */ - memset(c.secdatak, 0, sizeof(c.secdatak)); - TEST_EQ(vb2_secdatak_init(&c), VB2_ERROR_SECDATAK_VERSION, - "Zeroed buffer (invalid version)"); - - /* Try with bad version */ - TEST_SUCC(vb2api_secdatak_create(&c), "Create"); - sec->struct_version -= 1; - sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8)); - TEST_EQ(vb2api_secdatak_check(&c), - VB2_ERROR_SECDATAK_VERSION, "Check invalid version"); - TEST_EQ(vb2_secdatak_init(&c), - VB2_ERROR_SECDATAK_VERSION, "Init invalid version"); - - /* Create good data */ - TEST_SUCC(vb2api_secdatak_create(&c), "Create"); - TEST_SUCC(vb2api_secdatak_check(&c), "Check created CRC"); - TEST_SUCC(vb2_secdatak_init(&c), "Init created CRC"); - TEST_NEQ(sd->status & VB2_SD_STATUS_SECDATAK_INIT, 0, - "Init set SD status"); - sd->status &= ~VB2_SD_STATUS_SECDATAK_INIT; - test_changed(&c, 1, "Create changes data"); - - /* Now corrupt it */ - c.secdatak[2]++; - TEST_EQ(vb2api_secdatak_check(&c), - VB2_ERROR_SECDATAK_CRC, "Check invalid CRC"); - TEST_EQ(vb2_secdatak_init(&c), - VB2_ERROR_SECDATAK_CRC, "Init invalid CRC"); - - /* Make sure UID is checked */ - vb2api_secdatak_create(&c); - sec->uid++; - sec->crc8 = vb2_crc8(sec, offsetof(struct vb2_secdatak, crc8)); - TEST_EQ(vb2_secdatak_init(&c), VB2_ERROR_SECDATAK_UID, - "Init invalid struct UID"); - - /* Read/write versions */ - vb2api_secdatak_create(&c); - vb2_secdatak_init(&c); - c.flags = 0; - TEST_SUCC(vb2_secdatak_get(&c, VB2_SECDATAK_VERSIONS, &v), - "Get versions"); - TEST_EQ(v, 0, "Versions created 0"); - test_changed(&c, 0, "Get doesn't change data"); - TEST_SUCC(vb2_secdatak_set(&c, VB2_SECDATAK_VERSIONS, 0x123456ff), - "Set versions"); - test_changed(&c, 1, "Set changes data"); - TEST_SUCC(vb2_secdatak_set(&c, VB2_SECDATAK_VERSIONS, 0x123456ff), - "Set versions 2"); - test_changed(&c, 0, "Set again doesn't change data"); - TEST_SUCC(vb2_secdatak_get(&c, VB2_SECDATAK_VERSIONS, &v), - "Get versions 2"); - TEST_EQ(v, 0x123456ff, "Versions changed"); - - /* Invalid field fails */ - TEST_EQ(vb2_secdatak_get(&c, -1, &v), - VB2_ERROR_SECDATAK_GET_PARAM, "Get invalid"); - TEST_EQ(vb2_secdatak_set(&c, -1, 456), - VB2_ERROR_SECDATAK_SET_PARAM, "Set invalid"); - test_changed(&c, 0, "Set invalid field doesn't change data"); - - /* Read/write uninitialized data fails */ - sd->status &= ~VB2_SD_STATUS_SECDATAK_INIT; - TEST_EQ(vb2_secdatak_get(&c, VB2_SECDATAK_VERSIONS, &v), - VB2_ERROR_SECDATAK_GET_UNINITIALIZED, "Get uninitialized"); - test_changed(&c, 0, "Get uninitialized doesn't change data"); - TEST_EQ(vb2_secdatak_set(&c, VB2_SECDATAK_VERSIONS, 0x123456ff), - VB2_ERROR_SECDATAK_SET_UNINITIALIZED, "Set uninitialized"); - test_changed(&c, 0, "Set uninitialized doesn't change data"); -} - -int main(int argc, char* argv[]) -{ - secdatak_test(); - - return gTestSuccess ? 0 : 255; -} diff --git a/tests/vboot_kernel_tests.c b/tests/vboot_kernel_tests.c index da169cd4..69789105 100644 --- a/tests/vboot_kernel_tests.c +++ b/tests/vboot_kernel_tests.c @@ -184,7 +184,7 @@ static void ResetMocks(void) struct vb2_shared_data *sd = vb2_get_sd(&ctx); sd->vbsd = shared; - // TODO: more workbuf fields - flags, secdata, secdatak + // TODO: more workbuf fields - flags, secdata_firmware, secdata_kernel } /* Mocks */ |