Skip to content

Commit

Permalink
[capture] Add trigger source command
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
nasahlpa committed Jan 29, 2024
1 parent 7b6f6c0 commit 9c01397
Show file tree
Hide file tree
Showing 25 changed files with 258 additions and 35 deletions.
43 changes: 34 additions & 9 deletions capture/capture_aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -149,25 +150,41 @@ 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)

# 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'.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 34 additions & 9 deletions capture/capture_kmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,25 +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.
"""
# Configure PRNGs.
# Seed the software LFSR used for initial key masking.
ot_kmac.write_lfsr_seed(cfg["test"]["lfsr_seed"].to_bytes(4, "little"))
Expand All @@ -184,8 +201,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):
Expand Down Expand Up @@ -447,8 +462,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)
Expand Down
44 changes: 36 additions & 8 deletions capture/capture_otbn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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"])

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
43 changes: 34 additions & 9 deletions capture/capture_sha3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
if cfg["test"]["masks_off"] is True:
logger.info("Configure device to use constant, fast entropy!")
ot_sha3.set_mask_off()
Expand All @@ -175,8 +192,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):
Expand Down Expand Up @@ -419,8 +434,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)
Expand Down
4 changes: 4 additions & 0 deletions capture/configs/aes_sca_cw305.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions capture/configs/aes_sca_cw310.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions capture/configs/kmac_sca_cw310.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions capture/configs/otbn_vertical_keygen_sca_cw310.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions capture/configs/otbn_vertical_modinv_sca_cw310.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 9c01397

Please sign in to comment.