diff options
| author | Ján Jančár | 2023-10-18 18:40:51 +0200 |
|---|---|---|
| committer | GitHub | 2023-10-18 18:40:51 +0200 |
| commit | 73aecd91317db24f2e2e603eb97c1aa70f25cba8 (patch) | |
| tree | 965bccac4ecac171fcda66f9f07b9ded4b1c7eec | |
| parent | 9987dc7dd762c44c56cca8b80379bfdc0b515bb3 (diff) | |
| parent | a1911ee8acbcf875ed8889f5686cc34b05c236c2 (diff) | |
| download | pyecsca-codegen-73aecd91317db24f2e2e603eb97c1aa70f25cba8.tar.gz pyecsca-codegen-73aecd91317db24f2e2e603eb97c1aa70f25cba8.tar.zst pyecsca-codegen-73aecd91317db24f2e2e603eb97c1aa70f25cba8.zip | |
Merge pull request #6 from andrr3j/bugfix/simulator
Bugfix/simulator
| -rw-r--r-- | pyecsca/codegen/client.py | 59 | ||||
| -rw-r--r-- | test/test_simulator.py | 1 |
2 files changed, 34 insertions, 26 deletions
diff --git a/pyecsca/codegen/client.py b/pyecsca/codegen/client.py index eb56276..740cb57 100644 --- a/pyecsca/codegen/client.py +++ b/pyecsca/codegen/client.py @@ -226,15 +226,9 @@ class SimulatorTarget(Target): self.trace.extend(self.simulator.trace) self.simulator.reset() - def hook_result(self, simulator) -> None: - self.result.append(simulator['r0']) - self.result.append(simulator['r1']) - self.result.append(simulator['r2']) - def connect(self, **kwargs) -> None: self.simulator.load(kwargs["binary"]) self.simulator.setup() - self.simulator.hook_bypass("simpleserial_put", self.hook_result) self.simulator.start(self.simulator.functions['init_implementation'] | 1, 0) self.simulator.reset() @@ -243,30 +237,40 @@ class SimulatorTarget(Target): self.__simulate(command, 'cmd_set_params') self.params = params - def scalar_mult(self, scalar: int, point: Point) -> Point: - command = cmd_scalar_mult(scalar, point) - self.__simulate(command, 'cmd_scalar_mult') - res_adress = self.result[2] - point_length = self.result[1] // len(self.coords.variables) - params = {var: Mod(int.from_bytes(self.simulator[res_adress + i * point_length: + def __scalar_mult_hook(self, simulator) -> None: + point_length = simulator['r1'] // len(self.coords.variables) + res_adress = simulator['r2'] + self.result.append({var: Mod(int.from_bytes(simulator[res_adress + i * point_length: res_adress + (i + 1) * point_length], 'big'), self.params.curve.prime) - for i, var in enumerate(self.coords.variables)} + for i, var in enumerate(self.coords.variables)}) + + def scalar_mult(self, scalar: int, point: Point) -> Point: self.result = [] - return Point(self.coords, **params) + self.simulator.hook_bypass("simpleserial_put", self.__scalar_mult_hook) + command = cmd_scalar_mult(scalar, point) + self.__simulate(command, 'cmd_scalar_mult') + return Point(self.coords, **self.result[0]) def init_prng(self, seed: bytes) -> None: command = cmd_init_prng(seed) self.__simulate(command, 'cmd_init_prng') self.seed = seed + def __generate_hook(self, simulator) -> None: + key_length = simulator['r1'] + key_bytes = simulator[simulator['r2']: simulator['r2'] + key_length] + self.result.append(key_length) + self.result.append(key_bytes) + def generate(self) -> Tuple[int, Point]: + self.result = [] + self.simulator.hook_bypass("simpleserial_put", self.__generate_hook) command = cmd_generate() self.__simulate(command, 'cmd_generate') - priv = int.from_bytes(self.simulator[self.result[2]:self.result[2] + self.result[1]], 'big') - pub_x = int.from_bytes(self.simulator[self.result[5]:self.result[5] + self.result[4] // 2], 'big') - pub_y = int.from_bytes(self.simulator[self.result[5] + self.result[4] // 2:self.result[5] + self.result[4]] ,'big') - self.result = [] + priv = int.from_bytes(self.result[1], 'big') + pub_x = int.from_bytes(self.result[3][0:self.result[2] // 2], 'big') + pub_y = int.from_bytes(self.result[3][self.result[2] // 2:self.result[2]] ,'big') return priv, Point(AffineCoordinateModel(self.model), x = Mod(pub_x, self.params.curve.prime), y = Mod(pub_y, self.params.curve.prime)) @@ -280,26 +284,31 @@ class SimulatorTarget(Target): self.__simulate(command, 'cmd_set_pubkey') self.pubkey = pubkey + def __ec_hook(self, simulator) -> None: + self.result.append(simulator[simulator['r2']:simulator['r2'] + simulator['r1']]) + def ecdh(self, other_pubkey: Point) -> bytes: + self.result = [] + self.simulator.hook_bypass("simpleserial_put", self.__ec_hook) command = cmd_ecdh(other_pubkey) self.__simulate(command, 'cmd_ecdh') - shared_secret = self.simulator[self.result[2]:self.result[2] + self.result[1]] - self.result = [] + shared_secret = self.result[0] return shared_secret def ecdsa_sign(self, data: bytes) -> bytes: + self.result = [] + self.simulator.hook_bypass("simpleserial_put", self.__ec_hook) command = cmd_ecdsa_sign(data) self.__simulate(command, 'cmd_ecdsa_sign') - signature = self.simulator[self.result[2]:self.result[2] + self.result[1]] - self.result = [] + signature = self.result[0] return signature def ecdsa_verify(self, data: bytes, signature: bytes) -> bool: + self.result = [] + self.simulator.hook_bypass("simpleserial_put", self.__ec_hook) command = cmd_ecdsa_verify(data, signature) self.__simulate(command, 'cmd_ecdsa_verify') - res = self.simulator[self.result[2]:self.result[2] + self.result[1]] - self.result = [] - return bool(int.from_bytes(res, 'big')) + return bool(int.from_bytes(self.result[0], 'big')) def set_strigger(self): pass diff --git a/test/test_simulator.py b/test/test_simulator.py index 89819b1..8ce38e4 100644 --- a/test/test_simulator.py +++ b/test/test_simulator.py @@ -198,7 +198,6 @@ def test_ecdh(mult_name, mult_class, cli_runner, curve32): ) -@pytest.mark.xfail(reason="Simulator bug #3") @pytest.mark.parametrize( "mult_name,mult_class", [("ltr", LTRMultiplier), ("rtl", RTLMultiplier)] ) |
