Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fi] Adapt FI code to latest changes #368

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/cfg/ci_crypto_aes_vcc_dummy_cw310.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fiproject:
plot_y_axis: "glitch_width"
plot_y_axis_legend: "[ns]"
test:
which_test: "crypto_fi_aes_encrypt"
which_test: "crypto_aes_encrypt"
expected_result: '{"ciphertext":[141,145,88,155,234,129,16,92,221,12,69,21,69,208,99,12],"alerts":0}'
# Set to true if the test should ignore alerts returned by the test. As the
# alert handler on the device could sometime fire alerts that are not
Expand Down
8 changes: 4 additions & 4 deletions fault_injection/configs/pen.global_fi.crypto.aes.cw310.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ fiproject:
plot_y_axis: "glitch_width"
plot_y_axis_legend: "[cycles]"
test:
# which_test: "crypto_fi_aes_key"
# which_test: "crypto_fi_aes_plaintext"
which_test: "crypto_fi_aes_encrypt"
# which_test: "crypto_aes_key"
# which_test: "crypto_aes_plaintext"
which_test: "crypto_aes_encrypt"
# which_test: "crypto_fi_aes_ciphertext"
expected_result: '{"ciphertext":[141,145,88,155,234,129,16,92,221,12,69,21,69,208,99,12],"alerts":0}'
expected_result: '{"ciphertext":[141,145,88,155,234,129,16,92,221,12,69,21,69,208,99,12],"alerts":0,"err_status":0}'
# Set to true if the test should ignore alerts returned by the test. As the
# alert handler on the device could sometime fire alerts that are not
# related to the FI, ignoring is by default set to true. A manual analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ fiproject:
plot_y_axis: "glitch_width"
plot_y_axis_legend: "[cycles]"
test:
# which_test: "crypto_fi_kmac_key"
which_test: "crypto_fi_kmac_absorb"
# which_test: "crypto_fi_kmac_squeeze"
expected_result: '{"ciphertext":[184,34,91,108,231,47,251,27],"alerts":0}'
# which_test: "crypto_kmac_key"
which_test: "crypto_kmac_absorb"
# which_test: "crypto_kmac_squeeze"
# which_test: "crypto_kmac_static"
expected_result: '{"digest":[184,34,91,108,231,47,251,27],"digest_2nd":[142,188,186,201,216,47,203,192],"alerts":[0,0,0],"err_status":0}'
# Set to true if the test should ignore alerts returned by the test. As the
# alert handler on the device could sometime fire alerts that are not
# related to the FI, ignoring is by default set to true. A manual analysis
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
target:
target_type: cw310
fpga_bitstream: "../objs/lowrisc_systems_chip_earlgrey_cw310_0.1.bit"
force_program_bitstream: False
fw_bin: "../objs/sca_ujson_fpga_cw310.bin"
output_len_bytes: 16
target_clk_mult: 0.24
target_freq: 24000000
baudrate: 115200
protocol: "ujson"
port: "/dev/ttyACM4"
fisetup:
fi_gear: "husky"
fi_type: "voltage_glitch"
parameter_generation: "random"
# Voltage glitch width in cycles.
glitch_width_min: 5
glitch_width_max: 150
glitch_width_step: 3
# Range for trigger delay in cycles.
trigger_delay_min: 0
trigger_delay_max: 500
trigger_step: 10
# Number of iterations for the parameter sweep.
num_iterations: 100
fiproject:
# Project database type and memory threshold.
project_db: "ot_fi_project"
project_mem_threshold: 10000
# Store FI plot.
show_plot: True
num_plots: 10
plot_x_axis: "trigger_delay"
plot_x_axis_legend: "[cycles]"
plot_y_axis: "glitch_width"
plot_y_axis_legend: "[cycles]"
test:
which_test: "ibex_char_unconditional_branch_nop"
expected_result: '{"result":0,"err_status":0,"alerts":0}'
# Set to true if the test should ignore alerts returned by the test. As the
# alert handler on the device could sometime fire alerts that are not
# related to the FI, ignoring is by default set to true. A manual analysis
# still can be performed as the alerts are stored in the database.
ignore_alerts: True
8 changes: 6 additions & 2 deletions fault_injection/fi_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ def fi_parameter_sweep(cfg: dict, target: Target, fi_gear,
fi_gear: The FI gear to use.
project: The project to store the results.
ot_communication: The OpenTitan Crypto FI communication interface.
Returns:
device_id: The ID of the target device.
"""
# Configure the Crypto FI code on the target.
ot_communication.init()
device_id = ot_communication.init()
# Store results in array for a quick access.
fi_results = []
# Start the parameter sweep.
Expand Down Expand Up @@ -174,6 +176,7 @@ def fi_parameter_sweep(cfg: dict, target: Target, fi_gear,
remaining_iterations -= 1
pbar.update(1)
print_fi_statistic(fi_results)
return device_id


def print_plot(project: FIProject, config: dict, file: Path) -> None:
Expand Down Expand Up @@ -213,14 +216,15 @@ def main(argv=None):
ot_communication = OTFICrypto(target)

# FI parameter sweep.
fi_parameter_sweep(cfg, target, fi_gear, project, ot_communication)
device_id = fi_parameter_sweep(cfg, target, fi_gear, project, ot_communication)

# Print plot.
print_plot(project.get_firesults(start=0, end=cfg["fiproject"]["num_plots"]),
cfg, args.project)

# Save metadata.
metadata = {}
metadata["device_id"] = device_id
metadata["datetime"] = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
# Store bitstream information.
metadata["fpga_bitstream_path"] = cfg["target"].get("fpga_bitstream")
Expand Down
56 changes: 37 additions & 19 deletions target/communication/fi_crypto_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ class OTFICrypto:
def __init__(self, target) -> None:
self.target = target

def _ujson_crypto_fi_cmd(self) -> None:
def _ujson_crypto_cmd(self) -> None:
time.sleep(0.01)
self.target.write(json.dumps("CryptoFi").encode("ascii"))
time.sleep(0.01)

def init(self) -> None:
""" Initialize the Crypto FI code on the chip.
Returns:
The device ID of the device.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Init command.
time.sleep(0.01)
self.target.write(json.dumps("Init").encode("ascii"))
# Read back device ID from device.
return self.read_response(max_tries=30)

def crypto_fi_aes_key(self) -> None:
def crypto_aes_key(self) -> None:
""" Starts the crypto.fi.aes_key test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Aes command.
time.sleep(0.01)
self.target.write(json.dumps("Aes").encode("ascii"))
Expand All @@ -42,11 +46,11 @@ def crypto_fi_aes_key(self) -> None:
"encrypt_trigger": False, "ciphertext_trigger": False}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_fi_aes_plaintext(self) -> None:
def crypto_aes_plaintext(self) -> None:
""" Starts the crypto.fi.aes_plaintext test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Aes command.
time.sleep(0.01)
self.target.write(json.dumps("Aes").encode("ascii"))
Expand All @@ -56,11 +60,11 @@ def crypto_fi_aes_plaintext(self) -> None:
"encrypt_trigger": False, "ciphertext_trigger": False}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_fi_aes_encrypt(self) -> None:
def crypto_aes_encrypt(self) -> None:
""" Starts the crypto.fi.aes_encrypt test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Aes command.
time.sleep(0.01)
self.target.write(json.dumps("Aes").encode("ascii"))
Expand All @@ -70,11 +74,11 @@ def crypto_fi_aes_encrypt(self) -> None:
"encrypt_trigger": True, "ciphertext_trigger": False}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_fi_aes_ciphertext(self) -> None:
def crypto_aes_ciphertext(self) -> None:
""" Starts the crypto.fi.aes_ciphertext test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Aes command.
time.sleep(0.01)
self.target.write(json.dumps("Aes").encode("ascii"))
Expand All @@ -84,46 +88,60 @@ def crypto_fi_aes_ciphertext(self) -> None:
"encrypt_trigger": False, "ciphertext_trigger": True}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_fi_kmac_key(self) -> None:
def crypto_kmac_key(self) -> None:
""" Starts the crypto.fi.kmac_key test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Kmac command.
time.sleep(0.01)
self.target.write(json.dumps("Kmac").encode("ascii"))
# Mode payload.
time.sleep(0.01)
mode = {"key_trigger": True, "absorb_trigger": False,
"squeeze_trigger": False}
"static_trigger": False, "squeeze_trigger": False}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_fi_kmac_absorb(self) -> None:
def crypto_kmac_absorb(self) -> None:
""" Starts the crypto.fi.kmac_absorb test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Kmac command.
time.sleep(0.01)
self.target.write(json.dumps("Kmac").encode("ascii"))
# Mode payload.
time.sleep(0.01)
mode = {"key_trigger": False, "absorb_trigger": True,
"squeeze_trigger": False}
"static_trigger": False, "squeeze_trigger": False}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_fi_kmac_squeeze(self) -> None:
def crypto_kmac_squeeze(self) -> None:
""" Starts the crypto.fi.kmac_squeeze test.
"""
# CryptoFi command.
self._ujson_crypto_fi_cmd()
self._ujson_crypto_cmd()
# Kmac command.
time.sleep(0.01)
self.target.write(json.dumps("Kmac").encode("ascii"))
# Mode payload.
time.sleep(0.01)
mode = {"key_trigger": False, "absorb_trigger": False,
"static_trigger": False, "squeeze_trigger": True}
self.target.write(json.dumps(mode).encode("ascii"))

def crypto_kmac_static(self) -> None:
""" Starts the crypto.fi.kmac_static test.
"""
# CryptoFi command.
self._ujson_crypto_cmd()
# Kmac command.
time.sleep(0.01)
self.target.write(json.dumps("Kmac").encode("ascii"))
# Mode payload.
time.sleep(0.01)
mode = {"key_trigger": False, "absorb_trigger": False,
"squeeze_trigger": True}
"static_trigger": True, "squeeze_trigger": False}
self.target.write(json.dumps(mode).encode("ascii"))

def start_test(self, cfg: dict) -> None:
Expand Down
11 changes: 9 additions & 2 deletions target/communication/fi_ibex_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def ibex_char_unconditional_branch(self) -> None:
time.sleep(0.01)
self.target.write(json.dumps("CharUncondBranch").encode("ascii"))

def ibex_char_unconditional_branch_nop(self) -> None:
""" Starts the ibex.char.unconditional_branch_nop test.
"""
# IbexFi command.
self._ujson_ibex_fi_cmd()
# CharUncondBranchNop command.
time.sleep(0.01)
self.target.write(json.dumps("CharUncondBranchNop").encode("ascii"))

def ibex_char_register_file(self) -> None:
""" Starts the ibex.char.register_file test.
"""
Expand All @@ -200,8 +209,6 @@ def ibex_char_register_file_read(self) -> None:

def init(self) -> list:
""" Initialize the Ibex FI code on the chip.
Args:
cfg: Config dict containing the selected test.
Returns:
The device ID of the device.
"""
Expand Down
Loading