From d9c8e85db5eb1f4646e2c0989ec9670d11d72c9f Mon Sep 17 00:00:00 2001 From: Pascal Nasahl Date: Wed, 7 Feb 2024 08:26:20 +0000 Subject: [PATCH] [capture] Add trigger source command This PR adds the SCA trigger source command to the capture scripts. Needed to start penetration tests at ASIC as there is no HW trigger available. Signed-off-by: Pascal Nasahl --- capture/capture_aes.py | 43 ++++++++++++---- capture/capture_kmac.py | 49 ++++++++++++++----- capture/capture_otbn.py | 44 ++++++++++++++--- capture/capture_sha3.py | 43 ++++++++++++---- capture/configs/aes_sca_chip.yaml | 4 ++ capture/configs/aes_sca_cw305.yaml | 4 ++ capture/configs/aes_sca_cw310.yaml | 4 ++ capture/configs/kmac_sca_cw310.yaml | 4 ++ .../otbn_vertical_keygen_sca_cw310.yaml | 4 ++ .../otbn_vertical_modinv_sca_cw310.yaml | 4 ++ capture/configs/sha3_sca_chip_masks_off.yaml | 4 ++ capture/configs/sha3_sca_cw310.yaml | 4 ++ capture/configs/sha3_sca_cw310_masks_off.yaml | 4 ++ ci/cfg/ci_aes_sca_fvsr_cw305.yaml | 4 ++ ci/cfg/ci_aes_sca_fvsr_cw310.yaml | 4 ++ ci/cfg/ci_aes_sca_random_cw305.yaml | 4 ++ ci/cfg/ci_aes_sca_random_cw310.yaml | 4 ++ ci/cfg/ci_ibex_fi_vcc_dummy.yaml | 4 ++ .../ci_kmac_sca_fvsr_cw310_simpleserial.yaml | 4 ++ ci/cfg/ci_kmac_sca_fvsr_cw310_ujson.yaml | 4 ++ ...ci_kmac_sca_random_cw310_simpleserial.yaml | 4 ++ ci/cfg/ci_kmac_sca_random_cw310_ujson.yaml | 4 ++ .../ci_sha3_sca_fvsr_cw310_simpleserial.yaml | 4 ++ ci/cfg/ci_sha3_sca_fvsr_cw310_ujson.yaml | 4 ++ ...ci_sha3_sca_random_cw310_simpleserial.yaml | 4 ++ ci/cfg/ci_sha3_sca_random_cw310_ujson.yaml | 4 ++ target/communication/sca_kmac_commands.py | 18 +++---- target/communication/sca_sha3_commands.py | 3 +- target/communication/sca_trigger_commands.py | 40 +++++++++++++++ 29 files changed, 280 insertions(+), 48 deletions(-) create mode 100644 target/communication/sca_trigger_commands.py diff --git a/capture/capture_aes.py b/capture/capture_aes.py index 2f9b06ed..1f2dad2a 100755 --- a/capture/capture_aes.py +++ b/capture/capture_aes.py @@ -25,6 +25,7 @@ import util.helpers as helpers from target.communication.sca_aes_commands import OTAES from target.communication.sca_prng_commands import OTPRNG +from target.communication.sca_trigger_commands import OTTRIGGER from target.targets import Target, TargetConfig from util import check_version from util import data_generator as dg @@ -149,18 +150,17 @@ def setup(cfg: dict, project: Path): return target, scope, project -def configure_cipher(cfg, target, capture_cfg) -> OTAES: - """ Configure the AES cipher. - - Establish communication with the AES cipher and configure the seed. +def establish_communication(target, capture_cfg: CaptureConfig): + """ Establish communication with the target device. Args: - cfg: The project config. target: The OT target. capture_cfg: The capture config. Returns: - The communication interface to the AES cipher. + ot_aes: The communication interface to the AES SCA application. + ot_prng: The communication interface to the PRNG SCA application. + ot_trig: The communication interface to the SCA trigger. """ # Create communication interface to OT AES. ot_aes = OTAES(target=target, protocol=capture_cfg.protocol) @@ -168,6 +168,23 @@ def configure_cipher(cfg, target, capture_cfg) -> OTAES: # Create communication interface to OT PRNG. ot_prng = OTPRNG(target=target, protocol=capture_cfg.protocol) + # Create communication interface to SCA trigger. + ot_trig = OTTRIGGER(target=target, protocol=capture_cfg.protocol) + + return ot_aes, ot_prng, ot_trig + + +def configure_cipher(cfg, capture_cfg, ot_aes, ot_prng): + """ Configure the AES cipher. + + Establish communication with the AES cipher and configure the seed. + + Args: + cfg: The project config. + capture_cfg: The capture config. + ot_aes: The communication interface to the AES SCA application. + ot_prng: The communication interface to the PRNG SCA application. + """ # Configure PRNGs. # Seed the software LFSR used for initial key masking and additionally # turning off the masking when '0'. @@ -180,8 +197,6 @@ def configure_cipher(cfg, target, capture_cfg) -> OTAES: # Seed the target's PRNG. ot_prng.seed_prng(cfg["test"]["batch_prng_seed"].to_bytes(4, "little")) - return ot_aes - def generate_ref_crypto(sample_fixed, mode, key, plaintext): """ Generate cipher material for the encryption. @@ -413,8 +428,18 @@ def main(argv=None): port = cfg["target"].get("port")) logger.info(f"Setting up capture {capture_cfg.capture_mode} batch={capture_cfg.batch_mode}...") + # Open communication with target. + ot_aes, ot_prng, ot_trig = establish_communication(target, capture_cfg) + # Configure cipher. - ot_aes = configure_cipher(cfg, target, capture_cfg) + configure_cipher(cfg, capture_cfg, ot_aes, ot_prng) + + # Configure trigger source. + # 0 for HW, 1 for SW. + trigger_source = 1 + if "hw" in cfg["target"].get("trigger"): + trigger_source = 0 + ot_trig.select_trigger(trigger_source) # Capture traces. capture(scope, ot_aes, capture_cfg, project, target) diff --git a/capture/capture_kmac.py b/capture/capture_kmac.py index da158894..8c923a77 100755 --- a/capture/capture_kmac.py +++ b/capture/capture_kmac.py @@ -29,6 +29,7 @@ import util.helpers as helpers from target.communication.sca_kmac_commands import OTKMAC from target.communication.sca_prng_commands import OTPRNG +from target.communication.sca_trigger_commands import OTTRIGGER from target.targets import Target, TargetConfig from util import check_version from util import data_generator as dg @@ -153,22 +154,41 @@ def setup(cfg: dict, project: Path): return target, scope, project -def configure_cipher(cfg, target, capture_cfg) -> OTKMAC: - """ Configure the KMAC cipher. - - Establish communication with the KMAC cipher and configure the seed. +def establish_communication(target, capture_cfg: CaptureConfig): + """ Establish communication with the target device. Args: - cfg: The project config. target: The OT target. capture_cfg: The capture config. Returns: - The communication interface to the KMAC cipher. + ot_kmac: The communication interface to the KMAC SCA application. + ot_prng: The communication interface to the PRNG SCA application. + ot_trig: The communication interface to the SCA trigger. """ # Create communication interface to OT KMAC. ot_kmac = OTKMAC(target=target, protocol=capture_cfg.protocol) + # Create communication interface to OT PRNG. + ot_prng = OTPRNG(target=target, protocol=capture_cfg.protocol) + + # Create communication interface to SCA trigger. + ot_trig = OTTRIGGER(target=target, protocol=capture_cfg.protocol) + + return ot_kmac, ot_prng, ot_trig + + +def configure_cipher(cfg, capture_cfg, ot_kmac, ot_prng): + """ Configure the KMAC cipher. + + Establish communication with the KMAC cipher and configure the seed. + + Args: + cfg: The project config. + capture_cfg: The capture config. + ot_kmac: The communication interface to the KMAC SCA application. + ot_prng: The communication interface to the PRNG SCA application. + """ # Check if we want to run KMAC SCA for FPGA or discrete. On the FPGA, we # can use functionality helping us to capture cleaner traces. fpga_mode_bit = 0 @@ -177,9 +197,6 @@ def configure_cipher(cfg, target, capture_cfg) -> OTKMAC: # Initialize KMAC on the target. ot_kmac.init(fpga_mode_bit) - # Create communication interface to OT PRNG. - ot_prng = OTPRNG(target=target, protocol=capture_cfg.protocol) - # Configure PRNGs. # Seed the software LFSR used for initial key masking. ot_kmac.write_lfsr_seed(cfg["test"]["lfsr_seed"].to_bytes(4, "little")) @@ -192,8 +209,6 @@ def configure_cipher(cfg, target, capture_cfg) -> OTKMAC: # Seed the target's PRNG. ot_prng.seed_prng(cfg["test"]["batch_prng_seed"].to_bytes(4, "little")) - return ot_kmac - def generate_ref_crypto(sample_fixed, mode, batch, key, key_fixed, plaintext, plaintext_fixed, key_length): @@ -455,8 +470,18 @@ def main(argv=None): port = cfg["target"].get("port")) logger.info(f"Setting up capture {capture_cfg.capture_mode} batch={capture_cfg.batch_mode}...") + # Open communication with target. + ot_kmac, ot_prng, ot_trig = establish_communication(target, capture_cfg) + # Configure cipher. - ot_kmac = configure_cipher(cfg, target, capture_cfg) + configure_cipher(cfg, capture_cfg, ot_kmac, ot_prng) + + # Configure trigger source. + # 0 for HW, 1 for SW. + trigger_source = 1 + if "hw" in cfg["target"].get("trigger"): + trigger_source = 0 + ot_trig.select_trigger(trigger_source) # Capture traces. capture(scope, ot_kmac, capture_cfg, project, target) diff --git a/capture/capture_otbn.py b/capture/capture_otbn.py index 06f76b68..82c7e35d 100755 --- a/capture/capture_otbn.py +++ b/capture/capture_otbn.py @@ -22,6 +22,7 @@ import util.helpers as helpers from target.communication.sca_otbn_commands import OTOTBNVERT +from target.communication.sca_trigger_commands import OTTRIGGER from target.targets import Target, TargetConfig from util import check_version from util import data_generator as dg @@ -178,8 +179,28 @@ def setup(cfg: dict, project: Path): return target, scope, project -def configure_cipher(cfg: dict, target, - capture_cfg: CaptureConfig) -> OTOTBNVERT: +def establish_communication(target, capture_cfg: CaptureConfig): + """ Establish communication with the target device. + + Args: + target: The OT target. + curve_cfg: The capture config + + Returns: + ot_otbn_vert: The communication interface to the OTBN app. + ot_trig: The communication interface to the SCA trigger. + """ + # Create communication interface to OTBN. + ot_otbn_vert = OTOTBNVERT(target=target, protocol=capture_cfg.protocol) + + # Create communication interface to SCA trigger. + ot_trig = OTTRIGGER(target=target, protocol=capture_cfg.protocol) + + return ot_otbn_vert, ot_trig + + +def configure_cipher(cfg: dict, target, capture_cfg: CaptureConfig, + ot_otbn_vert) -> OTOTBNVERT: """ Configure the OTBN app. Establish communication with the OTBN keygen app and configure the seed. @@ -189,14 +210,11 @@ def configure_cipher(cfg: dict, target, target: The OT target. curve_cfg: The curve config. capture_cfg: The configuration of the capture. + ot_otbn_vert: The communication interface to the OTBN app. Returns: - ot_otbn_vert: The communication interface to the OTBN app. curve_cfg: The curve configuration values. """ - # Create communication interface to OTBN. - ot_otbn_vert = OTOTBNVERT(target=target, protocol=capture_cfg.protocol) - # Seed host's PRNG. random.seed(cfg["test"]["batch_prng_seed"]) @@ -286,7 +304,7 @@ def configure_cipher(cfg: dict, target, capture_cfg.expected_fixed_output = pow(k_fixed_int, -1, curve_cfg.curve_order_n) - return ot_otbn_vert, curve_cfg + return curve_cfg def generate_ref_crypto_keygen(cfg: dict, sample_fixed, curve_cfg: CurveConfig, @@ -717,8 +735,18 @@ def main(argv=None): f"Setting up capture {capture_cfg.capture_mode} batch={capture_cfg.batch_mode}..." ) + # Open communication with target. + ot_otbn_vert, ot_trig = establish_communication(target, capture_cfg) + # Configure cipher. - ot_otbn_vert, curve_cfg = configure_cipher(cfg, target, capture_cfg) + curve_cfg = configure_cipher(cfg, target, capture_cfg, ot_otbn_vert) + + # Configure trigger source. + # 0 for HW, 1 for SW. + trigger_source = 1 + if "hw" in cfg["target"].get("trigger"): + trigger_source = 0 + ot_trig.select_trigger(trigger_source) # Capture traces. if mode == "keygen": diff --git a/capture/capture_sha3.py b/capture/capture_sha3.py index 4fd5878b..541ee658 100755 --- a/capture/capture_sha3.py +++ b/capture/capture_sha3.py @@ -28,6 +28,7 @@ import util.helpers as helpers from target.communication.sca_prng_commands import OTPRNG from target.communication.sca_sha3_commands import OTSHA3 +from target.communication.sca_trigger_commands import OTTRIGGER from target.targets import Target, TargetConfig from util import check_version from util import data_generator as dg @@ -138,25 +139,41 @@ def setup(cfg: dict, project: Path): return target, scope, project -def configure_cipher(cfg, target, capture_cfg) -> OTSHA3: - """ Configure the SHA3 cipher. - - Establish communication with the SHA3 cipher and configure the seed and mask. +def establish_communication(target, capture_cfg: CaptureConfig): + """ Establish communication with the target device. Args: - cfg: The project config. target: The OT target. capture_cfg: The capture config. Returns: - The communication interface to the SHA3 cipher. + ot_sha3: The communication interface to the SHA3 SCA application. + ot_prng: The communication interface to the PRNG SCA application. + ot_trig: The communication interface to the SCA trigger. """ # Create communication interface to OT SHA3. ot_sha3 = OTSHA3(target=target, protocol=capture_cfg.protocol) + # Create communication interface to SCA trigger. + ot_trig = OTTRIGGER(target=target, protocol=capture_cfg.protocol) + # Create communication interface to OT PRNG. ot_prng = OTPRNG(target=target, protocol=capture_cfg.protocol) + return ot_sha3, ot_prng, ot_trig + + +def configure_cipher(cfg, capture_cfg, ot_sha3, ot_prng): + """ Configure the SHA3 cipher. + + Establish communication with the SHA3 cipher and configure the seed and mask. + + Args: + cfg: The project config. + capture_cfg: The capture config. + ot_sha3: The communication interface to the SHA3 SCA application. + ot_prng: The communication interface to the PRNG SCA application. + """ # Check if we want to run KMAC SCA for FPGA or discrete. On the FPGA, we # can use functionality helping us to capture cleaner traces. fpga_mode_bit = 0 @@ -183,8 +200,6 @@ def configure_cipher(cfg, target, capture_cfg) -> OTSHA3: # Seed the target's PRNG. ot_prng.seed_prng(cfg["test"]["batch_prng_seed"].to_bytes(4, "little")) - return ot_sha3 - def generate_ref_crypto(sample_fixed, mode, batch, plaintext, plaintext_fixed, text_len_bytes): @@ -427,8 +442,18 @@ def main(argv=None): port = cfg["target"].get("port")) logger.info(f"Setting up capture {capture_cfg.capture_mode} batch={capture_cfg.batch_mode}...") + # Open communication with target. + ot_sha3, ot_prng, ot_trig = establish_communication(target, capture_cfg) + # Configure cipher. - ot_sha3 = configure_cipher(cfg, target, capture_cfg) + configure_cipher(cfg, capture_cfg, ot_sha3, ot_prng) + + # Configure trigger source. + # 0 for HW, 1 for SW. + trigger_source = 1 + if "hw" in cfg["target"].get("trigger"): + trigger_source = 0 + ot_trig.select_trigger(trigger_source) # Capture traces. capture(scope, ot_sha3, capture_cfg, project, target) diff --git a/capture/configs/aes_sca_chip.yaml b/capture/configs/aes_sca_chip.yaml index f5ff21ab..0c9f6519 100644 --- a/capture/configs/aes_sca_chip.yaml +++ b/capture/configs/aes_sca_chip.yaml @@ -7,6 +7,10 @@ target: output_len_bytes: 16 protocol: "ujson" port: "/dev/ttyUSB1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "sw" waverunner: waverunner_ip: 100.107.71.10 num_segments: 20 diff --git a/capture/configs/aes_sca_cw305.yaml b/capture/configs/aes_sca_cw305.yaml index a504e0b6..2af5b9eb 100644 --- a/capture/configs/aes_sca_cw305.yaml +++ b/capture/configs/aes_sca_cw305.yaml @@ -9,6 +9,10 @@ target: baudrate: 115200 output_len_bytes: 16 protocol: "simpleserial" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 80 diff --git a/capture/configs/aes_sca_cw310.yaml b/capture/configs/aes_sca_cw310.yaml index 51af021d..23ca947b 100644 --- a/capture/configs/aes_sca_cw310.yaml +++ b/capture/configs/aes_sca_cw310.yaml @@ -12,6 +12,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/capture/configs/kmac_sca_cw310.yaml b/capture/configs/kmac_sca_cw310.yaml index a5be6bcf..e1b0d1a1 100644 --- a/capture/configs/kmac_sca_cw310.yaml +++ b/capture/configs/kmac_sca_cw310.yaml @@ -12,6 +12,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/capture/configs/otbn_vertical_keygen_sca_cw310.yaml b/capture/configs/otbn_vertical_keygen_sca_cw310.yaml index f80dbd1e..85dbe96e 100644 --- a/capture/configs/otbn_vertical_keygen_sca_cw310.yaml +++ b/capture/configs/otbn_vertical_keygen_sca_cw310.yaml @@ -11,6 +11,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/capture/configs/otbn_vertical_modinv_sca_cw310.yaml b/capture/configs/otbn_vertical_modinv_sca_cw310.yaml index 47df6388..2a54bc51 100644 --- a/capture/configs/otbn_vertical_modinv_sca_cw310.yaml +++ b/capture/configs/otbn_vertical_modinv_sca_cw310.yaml @@ -11,6 +11,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/capture/configs/sha3_sca_chip_masks_off.yaml b/capture/configs/sha3_sca_chip_masks_off.yaml index 687f2079..7854451b 100644 --- a/capture/configs/sha3_sca_chip_masks_off.yaml +++ b/capture/configs/sha3_sca_chip_masks_off.yaml @@ -7,6 +7,10 @@ target: output_len_bytes: 16 protocol: "ujson" port: "/dev/ttyUSB1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "sw" waverunner: waverunner_ip: 100.107.71.10 num_segments: 1 diff --git a/capture/configs/sha3_sca_cw310.yaml b/capture/configs/sha3_sca_cw310.yaml index 10c6fc25..ca674722 100644 --- a/capture/configs/sha3_sca_cw310.yaml +++ b/capture/configs/sha3_sca_cw310.yaml @@ -11,6 +11,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: samling_rate: 200000000 num_segments: 20 diff --git a/capture/configs/sha3_sca_cw310_masks_off.yaml b/capture/configs/sha3_sca_cw310_masks_off.yaml index d334f284..d892cffc 100644 --- a/capture/configs/sha3_sca_cw310_masks_off.yaml +++ b/capture/configs/sha3_sca_cw310_masks_off.yaml @@ -11,6 +11,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: samling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_aes_sca_fvsr_cw305.yaml b/ci/cfg/ci_aes_sca_fvsr_cw305.yaml index a2f66927..d37f6a0f 100644 --- a/ci/cfg/ci_aes_sca_fvsr_cw305.yaml +++ b/ci/cfg/ci_aes_sca_fvsr_cw305.yaml @@ -9,6 +9,10 @@ target: baudrate: 115200 output_len_bytes: 16 protocol: "simpleserial" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 80 diff --git a/ci/cfg/ci_aes_sca_fvsr_cw310.yaml b/ci/cfg/ci_aes_sca_fvsr_cw310.yaml index 76d3629e..df24a4e9 100644 --- a/ci/cfg/ci_aes_sca_fvsr_cw310.yaml +++ b/ci/cfg/ci_aes_sca_fvsr_cw310.yaml @@ -10,6 +10,10 @@ target: output_len_bytes: 16 # protocol: "ujson" protocol: "simpleserial" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_aes_sca_random_cw305.yaml b/ci/cfg/ci_aes_sca_random_cw305.yaml index 46e44624..c3fbff5c 100644 --- a/ci/cfg/ci_aes_sca_random_cw305.yaml +++ b/ci/cfg/ci_aes_sca_random_cw305.yaml @@ -9,6 +9,10 @@ target: baudrate: 115200 output_len_bytes: 16 protocol: "simpleserial" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 80 diff --git a/ci/cfg/ci_aes_sca_random_cw310.yaml b/ci/cfg/ci_aes_sca_random_cw310.yaml index 87a3a4d3..24fe1ac6 100644 --- a/ci/cfg/ci_aes_sca_random_cw310.yaml +++ b/ci/cfg/ci_aes_sca_random_cw310.yaml @@ -10,6 +10,10 @@ target: output_len_bytes: 16 # protocol: "ujson" protocol: "simpleserial" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_ibex_fi_vcc_dummy.yaml b/ci/cfg/ci_ibex_fi_vcc_dummy.yaml index 3f9678d6..fc841679 100644 --- a/ci/cfg/ci_ibex_fi_vcc_dummy.yaml +++ b/ci/cfg/ci_ibex_fi_vcc_dummy.yaml @@ -9,6 +9,10 @@ target: baudrate: 115200 protocol: "ujson" port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" fisetup: fi_gear: "dummy" fi_type: "voltage_glitch" diff --git a/ci/cfg/ci_kmac_sca_fvsr_cw310_simpleserial.yaml b/ci/cfg/ci_kmac_sca_fvsr_cw310_simpleserial.yaml index fd6cfc69..8076be0e 100644 --- a/ci/cfg/ci_kmac_sca_fvsr_cw310_simpleserial.yaml +++ b/ci/cfg/ci_kmac_sca_fvsr_cw310_simpleserial.yaml @@ -12,6 +12,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_kmac_sca_fvsr_cw310_ujson.yaml b/ci/cfg/ci_kmac_sca_fvsr_cw310_ujson.yaml index a8cafd86..f9f0d22b 100644 --- a/ci/cfg/ci_kmac_sca_fvsr_cw310_ujson.yaml +++ b/ci/cfg/ci_kmac_sca_fvsr_cw310_ujson.yaml @@ -12,6 +12,10 @@ target: # protocol: "simpleserial" protocol: "ujson" port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_kmac_sca_random_cw310_simpleserial.yaml b/ci/cfg/ci_kmac_sca_random_cw310_simpleserial.yaml index 214d4f70..22593a54 100644 --- a/ci/cfg/ci_kmac_sca_random_cw310_simpleserial.yaml +++ b/ci/cfg/ci_kmac_sca_random_cw310_simpleserial.yaml @@ -12,6 +12,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 1 diff --git a/ci/cfg/ci_kmac_sca_random_cw310_ujson.yaml b/ci/cfg/ci_kmac_sca_random_cw310_ujson.yaml index 57d0f7d4..fc99ed44 100644 --- a/ci/cfg/ci_kmac_sca_random_cw310_ujson.yaml +++ b/ci/cfg/ci_kmac_sca_random_cw310_ujson.yaml @@ -12,6 +12,10 @@ target: # protocol: "simpleserial" protocol: "ujson" port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: sampling_rate: 200000000 num_segments: 1 diff --git a/ci/cfg/ci_sha3_sca_fvsr_cw310_simpleserial.yaml b/ci/cfg/ci_sha3_sca_fvsr_cw310_simpleserial.yaml index 0b28e56b..0ab64560 100644 --- a/ci/cfg/ci_sha3_sca_fvsr_cw310_simpleserial.yaml +++ b/ci/cfg/ci_sha3_sca_fvsr_cw310_simpleserial.yaml @@ -11,6 +11,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: samling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_sha3_sca_fvsr_cw310_ujson.yaml b/ci/cfg/ci_sha3_sca_fvsr_cw310_ujson.yaml index e80ef9ac..d431b849 100644 --- a/ci/cfg/ci_sha3_sca_fvsr_cw310_ujson.yaml +++ b/ci/cfg/ci_sha3_sca_fvsr_cw310_ujson.yaml @@ -11,6 +11,10 @@ target: # protocol: "simpleserial" protocol: "ujson" port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: samling_rate: 200000000 num_segments: 20 diff --git a/ci/cfg/ci_sha3_sca_random_cw310_simpleserial.yaml b/ci/cfg/ci_sha3_sca_random_cw310_simpleserial.yaml index 91f3fb62..c6a41b97 100644 --- a/ci/cfg/ci_sha3_sca_random_cw310_simpleserial.yaml +++ b/ci/cfg/ci_sha3_sca_random_cw310_simpleserial.yaml @@ -11,6 +11,10 @@ target: protocol: "simpleserial" # protocol: "ujson" # port: "/dev/ttyACM4" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: samling_rate: 200000000 num_segments: 1 diff --git a/ci/cfg/ci_sha3_sca_random_cw310_ujson.yaml b/ci/cfg/ci_sha3_sca_random_cw310_ujson.yaml index d59b1c96..ef3d52e1 100644 --- a/ci/cfg/ci_sha3_sca_random_cw310_ujson.yaml +++ b/ci/cfg/ci_sha3_sca_random_cw310_ujson.yaml @@ -11,6 +11,10 @@ target: # protocol: "simpleserial" protocol: "ujson" port: "/dev/ttyACM_CW310_1" + # Trigger source. + # hw: Precise, hardware-generated trigger - FPGA only. + # sw: Fully software-controlled trigger. + trigger: "hw" husky: samling_rate: 200000000 num_segments: 1 diff --git a/target/communication/sca_kmac_commands.py b/target/communication/sca_kmac_commands.py index a39578b5..72614e00 100644 --- a/target/communication/sca_kmac_commands.py +++ b/target/communication/sca_kmac_commands.py @@ -18,6 +18,12 @@ def __init__(self, target, protocol: str) -> None: if protocol == "ujson": self.simple_serial = False + def _ujson_kmac_sca_cmd(self): + # TODO: without the delay, the device uJSON command handler program + # does not recognize the commands. Tracked in issue #256. + time.sleep(0.01) + self.target.write(json.dumps("KmacSca").encode("ascii")) + def init(self, fpga_mode_bit: int): """ Initializes KMAC on the target. Args: @@ -27,17 +33,11 @@ def init(self, fpga_mode_bit: int): # KmacSca command. self._ujson_kmac_sca_cmd() # Init command. - self.port.write(json.dumps("Init").encode("ascii")) + self.target.write(json.dumps("Init").encode("ascii")) # FPGA mode. time.sleep(0.01) - fpga_mode = {"fpga_mode": [fpga_mode_bit]} - self.port.write(json.dumps(fpga_mode).encode("ascii")) - - def _ujson_kmac_sca_cmd(self): - # TODO: without the delay, the device uJSON command handler program - # does not recognize the commands. Tracked in issue #256. - time.sleep(0.01) - self.target.write(json.dumps("KmacSca").encode("ascii")) + fpga_mode = {"fpga_mode": fpga_mode_bit} + self.target.write(json.dumps(fpga_mode).encode("ascii")) def write_key(self, key: list[int]): """ Write the key to KMAC. diff --git a/target/communication/sca_sha3_commands.py b/target/communication/sca_sha3_commands.py index 5f182842..19c9e362 100644 --- a/target/communication/sca_sha3_commands.py +++ b/target/communication/sca_sha3_commands.py @@ -30,8 +30,9 @@ def init(self, fpga_mode_bit: int): fpga_mode_bit: Indicates whether FPGA specific KMAC test is started. """ if not self.simple_serial: + # Sha3Sca command. + self._ujson_sha3_sca_cmd() # Init the SHA3 core. - self.target.write(json.dumps("Sha3Sca").encode("ascii")) self.target.write(json.dumps("Init").encode("ascii")) # FPGA mode. time.sleep(0.01) diff --git a/target/communication/sca_trigger_commands.py b/target/communication/sca_trigger_commands.py new file mode 100644 index 00000000..8e4f04a7 --- /dev/null +++ b/target/communication/sca_trigger_commands.py @@ -0,0 +1,40 @@ +# Copyright lowRISC contributors. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +"""Communication interface for the SHA3 SCA application on OpenTitan. + +Communication with OpenTitan either happens over simpleserial or the uJson +command interface. +""" +import json +import time +from typing import Optional + + +class OTTRIGGER: + def __init__(self, target, protocol: str) -> None: + self.target = target + self.simple_serial = True + if protocol == "ujson": + self.simple_serial = False + + def select_trigger(self, trigger_source: Optional[int] = 0): + """ Select the trigger source for SCA. + Args: + trigger_source: + - 0: Precise, hardware-generated trigger - FPGA only. + - 1: Fully software-controlled trigger. + """ + if self.simple_serial: + self.target.write(cmd="t", data=bytearray([trigger_source])) + else: + # TODO(#256): without the delay, the device uJSON command handler program + # does not recognize the commands. + time.sleep(0.01) + self.target.write(json.dumps("TriggerSca").encode("ascii")) + # SelectTriggerSource command. + self.target.write(json.dumps("SelectTriggerSource").encode("ascii")) + # Source payload. + time.sleep(0.01) + src = {"source": trigger_source} + self.target.write(json.dumps(src).encode("ascii"))