Skip to content

Commit c265a55

Browse files
Siemen Dhooghesiemen11
authored andcommitted
[FiSim] ROM_EXT Rollback Flash Testing
This adds a test where a higher manifest version BL0 is flashed, afterwards, a low manifest version BL0 is flashed where GDB instruction skips are used to test rollback security. Signed-off-by: Siemen Dhooghe <[email protected]>
1 parent 81f7e0f commit c265a55

File tree

7 files changed

+533
-14
lines changed

7 files changed

+533
-14
lines changed

sw/device/tests/penetrationtests/BUILD

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
33
# SPDX-License-Identifier: Apache-2.0
44

5-
load(":pentest.bzl", "pentest_cryptolib_fi_asym", "pentest_cryptolib_fi_gdb_asym", "pentest_cryptolib_fi_gdb_sym", "pentest_cryptolib_fi_sym", "pentest_cryptolib_sca_asym", "pentest_cryptolib_sca_sym", "pentest_fi", "pentest_fi_ibex", "pentest_fi_otbn", "pentest_gdb_unit", "pentest_rom_ext_fi_gdb", "pentest_rom_ext_rollback_fi_gdb", "pentest_rom_fi_gdb", "pentest_rom_rollback_fi_gdb", "pentest_sca")
5+
load(":pentest.bzl", "pentest_bl0_sec_ver_incr", "pentest_cryptolib_fi_asym", "pentest_cryptolib_fi_gdb_asym", "pentest_cryptolib_fi_gdb_sym", "pentest_cryptolib_fi_sym", "pentest_cryptolib_sca_asym", "pentest_cryptolib_sca_sym", "pentest_fi", "pentest_fi_ibex", "pentest_fi_otbn", "pentest_gdb_unit", "pentest_rom_ext_fi_gdb", "pentest_rom_ext_rollback_flash_fi_gdb", "pentest_rom_ext_rollback_otp_fi_gdb", "pentest_rom_fi_gdb", "pentest_rom_rollback_fi_gdb", "pentest_sca")
66
load("@ot_python_deps//:requirements.bzl", "requirement")
77

88
package(default_visibility = ["//visibility:public"])
@@ -507,14 +507,33 @@ pentest_rom_ext_fi_gdb(
507507
test_vectors = [],
508508
)
509509

510-
pentest_rom_ext_rollback_fi_gdb(
511-
name = "fi_rom_ext_rollback_python_gdb_test",
510+
pentest_rom_ext_rollback_flash_fi_gdb(
511+
name = "fi_rom_ext_rollback_flash_python_gdb_test",
512512
tags = [
513513
"manual",
514514
"skip_in_ci",
515515
],
516516
test_args = "",
517-
test_harness = "//sw/host/penetrationtests/python/fi:fi_rom_ext_rollback_python_gdb_test",
517+
test_harness = "//sw/host/penetrationtests/python/fi:fi_rom_ext_rollback_flash_python_gdb_test",
518+
test_vectors = [],
519+
)
520+
521+
pentest_bl0_sec_ver_incr(
522+
name = "bl0_sec_ver_incr",
523+
tags = [
524+
"manual",
525+
"skip_in_ci",
526+
],
527+
)
528+
529+
pentest_rom_ext_rollback_otp_fi_gdb(
530+
name = "fi_rom_ext_rollback_otp_python_gdb_test",
531+
tags = [
532+
"manual",
533+
"skip_in_ci",
534+
],
535+
test_args = "",
536+
test_harness = "//sw/host/penetrationtests/python/fi:fi_rom_ext_rollback_otp_python_gdb_test",
518537
test_vectors = [],
519538
)
520539

sw/device/tests/penetrationtests/firmware/testdata/BUILD

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ offline_signature_attach(
7171

7272
# For when building specialized manifests for security testing.
7373
manifest(d = {
74-
"name": "rom_ext_rollback_manifest",
74+
"name": "rom_ext_rollback_manifest_low",
7575
"identifier": hex(CONST.OWNER),
7676
"security_version": "0",
7777
"visibility": ["//visibility:public"],
7878
})
7979

80+
manifest(d = {
81+
"name": "rom_ext_rollback_manifest_high",
82+
"identifier": hex(CONST.OWNER),
83+
"security_version": "2",
84+
"visibility": ["//visibility:public"],
85+
})
86+
8087
otp_json(
8188
name = "otp_json_rom_ext_fi",
8289
partitions = [
@@ -192,3 +199,7 @@ otp_image(
192199
":otp_json_rom_ext_rollback_fi",
193200
],
194201
)
202+
203+
exports_files([
204+
"bl0_sec_ver_incr.c",
205+
])
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright lowRISC contributors (OpenTitan project).
2+
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
#include <stdbool.h>
6+
7+
#include "sw/device/lib/runtime/log.h"
8+
#include "sw/device/lib/testing/test_framework/ottf_main.h"
9+
#include "sw/device/silicon_creator/lib/boot_data.h"
10+
#include "sw/device/silicon_creator/lib/drivers/lifecycle.h"
11+
#include "sw/device/silicon_creator/lib/drivers/otp.h"
12+
#include "sw/device/silicon_creator/lib/drivers/rstmgr.h"
13+
#include "sw/device/silicon_creator/lib/manifest_def.h"
14+
15+
#include "hw/top/otp_ctrl_regs.h"
16+
17+
OTTF_DEFINE_TEST_CONFIG();
18+
19+
bool test_main(void) {
20+
boot_data_t boot_data;
21+
RETURN_IF_ERROR(boot_data_read(lifecycle_state_get(), &boot_data));
22+
++boot_data.min_security_version_bl0;
23+
RETURN_IF_ERROR(boot_data_write(&boot_data));
24+
LOG_INFO("Boot data written, bl0 version %u\n",
25+
boot_data.min_security_version_bl0);
26+
return true;
27+
}

sw/device/tests/penetrationtests/pentest.bzl

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def pentest_rom_ext_fi_gdb(name, test_vectors, test_args, test_harness, tags):
856856
),
857857
)
858858

859-
def pentest_rom_ext_rollback_fi_gdb(name, test_vectors, test_args, test_harness, tags):
859+
def pentest_rom_ext_rollback_flash_fi_gdb(name, test_vectors, test_args, test_harness, tags):
860860
"""A macro for defining a CryptoTest test case.
861861
862862
Args:
@@ -874,7 +874,40 @@ def pentest_rom_ext_rollback_fi_gdb(name, test_vectors, test_args, test_harness,
874874
# Provide a correctly signed binary
875875
srcs = ["//sw/device/tests/penetrationtests/firmware:firmware_cryptolib_fi_asym.c"],
876876
# Provide a manifest with a low security version
877-
manifest = "//sw/device/tests/penetrationtests/firmware/testdata:rom_ext_rollback_manifest",
877+
manifest = "//sw/device/tests/penetrationtests/firmware/testdata:rom_ext_rollback_manifest_low",
878+
fpga = fpga_params(
879+
timeout = "eternal",
880+
# We set an OTP with the RMA lifecycle, ensure ROM_EXT can still boot, standard security versions
881+
otp = "//sw/device/tests/penetrationtests/firmware/testdata:otp_img_rom_ext_fi",
882+
data = test_vectors,
883+
tags = tags,
884+
test_cmd = """
885+
--bootstrap={firmware} --rom_ext={rom_ext} --rom={rom}
886+
""" + test_args,
887+
test_harness = test_harness,
888+
),
889+
deps = FIRMWARE_DEPS_CRYPTOLIB_FI_ASYM,
890+
)
891+
892+
def pentest_rom_ext_rollback_otp_fi_gdb(name, test_vectors, test_args, test_harness, tags):
893+
"""A macro for defining a CryptoTest test case.
894+
895+
Args:
896+
name: the name of the test.
897+
test_vectors: the test vectors to use.
898+
test_args: additional arguments to pass to the test.
899+
test_harness: the test harness to use.
900+
tags: indicate the tags for CI.
901+
"""
902+
opentitan_test(
903+
name = name,
904+
exec_env = {
905+
"//hw/top_earlgrey:fpga_cw340_rom_ext": None,
906+
},
907+
# Provide a correctly signed binary
908+
srcs = ["//sw/device/tests/penetrationtests/firmware:firmware_cryptolib_fi_asym.c"],
909+
# Provide a manifest with a low security version
910+
manifest = "//sw/device/tests/penetrationtests/firmware/testdata:rom_ext_rollback_manifest_low",
878911
fpga = fpga_params(
879912
timeout = "eternal",
880913
# We set an OTP with the RMA lifecycle, ensure ROM_EXT can still boot, set a high security version
@@ -888,3 +921,35 @@ def pentest_rom_ext_rollback_fi_gdb(name, test_vectors, test_args, test_harness,
888921
),
889922
deps = FIRMWARE_DEPS_CRYPTOLIB_FI_ASYM,
890923
)
924+
925+
def pentest_bl0_sec_ver_incr(name, tags):
926+
opentitan_test(
927+
name = name,
928+
srcs = ["//sw/device/tests/penetrationtests/firmware/testdata:bl0_sec_ver_incr.c"],
929+
exec_env = {
930+
"//hw/top_earlgrey:fpga_cw340_rom_with_fake_keys": None,
931+
},
932+
fpga = fpga_params(
933+
timeout = "eternal",
934+
otp = "//sw/device/tests/penetrationtests/firmware/testdata:otp_img_rom_ext_fi",
935+
tags = tags,
936+
exit_success = "PASS!",
937+
test_cmd = """
938+
--exec="transport init"
939+
--exec="fpga load-bitstream {bitstream}"
940+
--exec="bootstrap {firmware}"
941+
--exec="console --non-interactive --exit-success='{exit_success}'"
942+
--exec="fpga clear-bitstream\"
943+
no-op
944+
""",
945+
),
946+
deps = [
947+
"//hw/top:otp_ctrl_c_regs",
948+
"//hw/top/dt",
949+
"//sw/device/lib/testing/test_framework:ottf_main",
950+
"//sw/device/silicon_creator/lib:boot_data",
951+
"//sw/device/silicon_creator/lib/drivers:lifecycle",
952+
"//sw/device/silicon_creator/lib/drivers:otp",
953+
"//sw/device/silicon_creator/lib/drivers:rstmgr",
954+
],
955+
)

sw/host/penetrationtests/python/fi/BUILD

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,29 @@ py_binary(
208208
)
209209

210210
py_binary(
211-
name = "fi_rom_ext_rollback_python_gdb_test",
211+
name = "fi_rom_ext_rollback_flash_python_gdb_test",
212212
testonly = True,
213-
srcs = ["gdb_testing/fi_rom_ext_rollback_python_gdb_test.py"],
213+
srcs = ["gdb_testing/fi_rom_ext_rollback_flash_python_gdb_test.py"],
214+
data = [
215+
"//sw/device/tests/penetrationtests:bl0_sec_ver_incr_fpga_cw340_rom_with_fake_keys",
216+
"//sw/host/opentitantool",
217+
"//third_party/openocd:jtag_cmsis_dap_adapter_cfg",
218+
"//third_party/openocd:openocd_bin",
219+
"//util/openocd/target:lowrisc-earlgrey.cfg",
220+
"@lowrisc_rv32imcb_toolchain//:bin/riscv32-unknown-elf-gdb",
221+
],
222+
deps = [
223+
"//sw/host/penetrationtests/python/util:dis_parser",
224+
"//sw/host/penetrationtests/python/util:gdb_controller",
225+
"//sw/host/penetrationtests/python/util:targets",
226+
"@rules_python//python/runfiles",
227+
],
228+
)
229+
230+
py_binary(
231+
name = "fi_rom_ext_rollback_otp_python_gdb_test",
232+
testonly = True,
233+
srcs = ["gdb_testing/fi_rom_ext_rollback_otp_python_gdb_test.py"],
214234
data = [
215235
"//sw/host/opentitantool",
216236
"//third_party/openocd:jtag_cmsis_dap_adapter_cfg",

0 commit comments

Comments
 (0)