2222
2323import util .helpers as helpers
2424from target .communication .sca_otbn_commands import OTOTBNVERT
25+ from target .communication .sca_prng_commands import OTPRNG
2526from target .communication .sca_trigger_commands import OTTRIGGER
2627from target .targets import Target , TargetConfig
2728from util import check_version
@@ -191,39 +192,45 @@ def establish_communication(target, capture_cfg: CaptureConfig):
191192
192193 Returns:
193194 ot_otbn_vert: The communication interface to the OTBN app.
195+ ot_prng: The communication interface to the PRNG SCA application.
194196 ot_trig: The communication interface to the SCA trigger.
195197 """
196198 # Create communication interface to OTBN.
197199 ot_otbn_vert = OTOTBNVERT (target = target , protocol = capture_cfg .protocol )
198200
201+ # Create communication interface to OT PRNG.
202+ ot_prng = OTPRNG (target = target , protocol = capture_cfg .protocol )
203+
199204 # Create communication interface to SCA trigger.
200205 ot_trig = OTTRIGGER (target = target , protocol = capture_cfg .protocol )
201206
202- return ot_otbn_vert , ot_trig
207+ return ot_otbn_vert , ot_prng , ot_trig
203208
204209
205- def configure_cipher (cfg : dict , target , capture_cfg : CaptureConfig ,
206- ot_otbn_vert ) -> OTOTBNVERT :
210+ def configure_cipher (cfg : dict , capture_cfg : CaptureConfig , ot_otbn_vert ,
211+ ot_prng ) -> OTOTBNVERT :
207212 """ Configure the OTBN app.
208213
209214 Establish communication with the OTBN keygen app and configure the seed.
210215
211216 Args:
212217 cfg: The configuration for the current experiment.
213- target: The OT target.
214218 curve_cfg: The curve config.
215219 capture_cfg: The configuration of the capture.
216220 ot_otbn_vert: The communication interface to the OTBN app.
221+ ot_prng: The communication interface to the PRNG SCA application.
217222
218223 Returns:
219224 curve_cfg: The curve configuration values.
220225 """
226+ # Initialize OTBN on the target.
227+ ot_otbn_vert .init ()
228+
221229 # Seed host's PRNG.
222230 random .seed (cfg ["test" ]["batch_prng_seed" ])
223231
224232 # Seed the target's PRNGs
225- ot_otbn_vert .write_batch_prng_seed (cfg ["test" ]["batch_prng_seed" ].to_bytes (
226- 4 , "little" ))
233+ ot_prng .seed_prng (cfg ["test" ]["batch_prng_seed" ].to_bytes (4 , "little" ))
227234
228235 # select the otbn app on the device (0 -> keygen, 1 -> modinv)
229236 ot_otbn_vert .choose_otbn_app (cfg ["test" ]["app" ])
@@ -474,12 +481,7 @@ def check_ciphertext_keygen(ot_otbn_vert: OTOTBNVERT, expected_key,
474481 """
475482 # Read the output, unmask the key, and check if it matches
476483 # expectations.
477- share0 = ot_otbn_vert .read_output (curve_cfg .seed_bytes )
478- share1 = ot_otbn_vert .read_output (curve_cfg .seed_bytes )
479- if share0 is None :
480- raise RuntimeError ('Random share0 is none' )
481- if share1 is None :
482- raise RuntimeError ('Random share1 is none' )
484+ share0 , share1 = ot_otbn_vert .read_seeds (curve_cfg .seed_bytes )
483485
484486 d0 = int .from_bytes (share0 , byteorder = 'little' )
485487 d1 = int .from_bytes (share1 , byteorder = 'little' )
@@ -505,8 +507,7 @@ def check_ciphertext_modinv(ot_otbn_vert: OTOTBNVERT, expected_output,
505507 actual_output: The received output of the modinv operation.
506508 """
507509 # Read the output, unmask it, and check if it matches expectations.
508- kalpha_inv = ot_otbn_vert .read_output (curve_cfg .key_bytes )
509- alpha = ot_otbn_vert .read_output (curve_cfg .modinv_mask_bytes )
510+ kalpha_inv , alpha = ot_otbn_vert .read_alpha (curve_cfg .key_bytes , curve_cfg .modinv_mask_bytes )
510511 if kalpha_inv is None :
511512 raise RuntimeError ('kaplpha_inv is none' )
512513 if alpha is None :
@@ -589,8 +590,8 @@ def capture_keygen(cfg: dict, scope: Scope, ot_otbn_vert: OTOTBNVERT,
589590
590591 # Store trace into database.
591592 project .append_trace (wave = waves [0 , :],
592- plaintext = mask ,
593- ciphertext = share0 + share1 ,
593+ plaintext = bytearray ( mask ) ,
594+ ciphertext = bytearray ( share0 + share1 ) ,
594595 key = seed_used )
595596
596597 # Memory allocation optimization for CW trace library.
@@ -660,11 +661,11 @@ def capture_modinv(cfg: dict, scope: Scope, ot_otbn_vert: OTOTBNVERT,
660661
661662 # Store trace into database.
662663 project .append_trace (wave = waves [0 , :],
663- plaintext = k_used ,
664+ plaintext = bytearray ( k_used ) ,
664665 ciphertext = bytearray (
665666 actual_output .to_bytes (
666667 curve_cfg .key_bytes , 'little' )),
667- key = k_used )
668+ key = bytearray ( k_used ) )
668669
669670 # Memory allocation optimization for CW trace library.
670671 num_segments_storage = project .optimize_capture (
@@ -729,6 +730,7 @@ def main(argv=None):
729730 key_len_bytes = cfg ["test" ]["key_len_bytes" ],
730731 text_len_bytes = cfg ["test" ]["text_len_bytes" ],
731732 protocol = cfg ["target" ]["protocol" ],
733+ port = cfg ["target" ].get ("port" ),
732734 C = bytearray (),
733735 seed_fixed = bytearray (),
734736 expected_fixed_key = bytearray (),
@@ -739,10 +741,10 @@ def main(argv=None):
739741 )
740742
741743 # Open communication with target.
742- ot_otbn_vert , ot_trig = establish_communication (target , capture_cfg )
744+ ot_otbn_vert , ot_prng , ot_trig = establish_communication (target , capture_cfg )
743745
744746 # Configure cipher.
745- curve_cfg = configure_cipher (cfg , target , capture_cfg , ot_otbn_vert )
747+ curve_cfg = configure_cipher (cfg , capture_cfg , ot_otbn_vert , ot_prng )
746748
747749 # Configure trigger source.
748750 # 0 for HW, 1 for SW.
0 commit comments