diff options
| author | andrr3j | 2023-04-27 15:59:30 +0200 |
|---|---|---|
| committer | andrr3j | 2023-04-27 17:13:11 +0200 |
| commit | 3d57322075d858199122a928fe1e7db9b22fa3ad (patch) | |
| tree | 37394721606fe7e9355ac6caf3ed1e8920bc5b71 /pyecsca | |
| parent | 75a8b9349890668099cb4ac296eb52f20e931b24 (diff) | |
| download | pyecsca-codegen-3d57322075d858199122a928fe1e7db9b22fa3ad.tar.gz pyecsca-codegen-3d57322075d858199122a928fe1e7db9b22fa3ad.tar.zst pyecsca-codegen-3d57322075d858199122a928fe1e7db9b22fa3ad.zip | |
added simulator config, scalar_mult and generate_key fix
Diffstat (limited to 'pyecsca')
| -rw-r--r-- | pyecsca/codegen/client.py | 34 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/main.c | 4 |
2 files changed, 24 insertions, 14 deletions
diff --git a/pyecsca/codegen/client.py b/pyecsca/codegen/client.py index 6caa85c..add7e6f 100644 --- a/pyecsca/codegen/client.py +++ b/pyecsca/codegen/client.py @@ -22,7 +22,7 @@ from pyecsca.sca.target import (Target, SimpleSerialTarget, ChipWhispererTarget, from .common import wrap_enum, Platform, get_model, get_coords from rainbow.devices import rainbow_stm32f215 -from rainbow import TraceConfig, HammingWeight +from rainbow import TraceConfig, Print class Triggers(IntFlag): @@ -199,11 +199,15 @@ class SimulatorTarget(Target): params: Optional[DomainParameters] privkey: Optional[int] pubkey: Optional[Point] + trace: list - def __init__(self, model: CurveModel, coords: CoordinateModel): + def __init__(self, model: CurveModel, coords: CoordinateModel, print_config: Print = Print(0), + trace_config: TraceConfig = TraceConfig(), allow_breakpoints: bool = False): super().__init__() - self.simulator = rainbow_stm32f215(allow_stubs=True) + self.simulator = rainbow_stm32f215(print_config=print_config, trace_config=trace_config, + allow_stubs=True, allow_breakpoints=allow_breakpoints) self.result = [] + self.trace = [] self.model = model self.coords = coords self.seed = None @@ -219,6 +223,7 @@ class SimulatorTarget(Target): self.simulator['r0'] = data_adress self.simulator['r1'] = length self.simulator.start(self.simulator.functions[function] | 1, 0) + self.trace.extend(self.simulator.trace) self.simulator.reset() def hook_result(self, simulator) -> None: @@ -230,7 +235,7 @@ class SimulatorTarget(Target): self.simulator.load(kwargs["binary"]) self.simulator.setup() self.simulator.hook_bypass("simpleserial_put", self.hook_result) - self.simulator.start(self.simulator.functions['main'] | 1, 0) + self.simulator.start(self.simulator.functions['init_implementation'] | 1, 0) self.simulator.reset() def set_params(self, params: DomainParameters) -> None: @@ -242,13 +247,13 @@ class SimulatorTarget(Target): command = cmd_scalar_mult(scalar, point) self.__simulate(command, 'cmd_scalar_mult') res_adress = self.result[2] - res_length = self.result[1] // 3 - x = int.from_bytes(self.simulator[res_adress: res_adress + res_length], 'big') - y = int.from_bytes(self.simulator[res_adress + res_length: res_adress + 2 * res_length], 'big') - res_point = Point(AffineCoordinateModel(self.model), x = Mod(x, self.params.curve.prime), - y = Mod(y, self.params.curve.prime)) + point_length = self.result[1] // len(self.coords.variables) + params = {var: Mod(int.from_bytes(self.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)} self.result = [] - return res_point + return Point(self.coords, **params) def init_prng(self, seed: bytes) -> None: command = cmd_init_prng(seed) @@ -258,9 +263,9 @@ class SimulatorTarget(Target): def generate(self) -> Tuple[int, Point]: command = cmd_generate() self.__simulate(command, 'cmd_generate') - priv = int(hexlify(self.simulator[self.result[2]:self.result[2] + self.result[1]]) ,16) - pub_x = int(hexlify(self.simulator[self.result[5]:self.result[5] + self.result[4] // 2]), 16) - pub_y = int(hexlify(self.simulator[self.result[5] + self.result[4] // 2:self.result[5] + self.result[4]]) ,16) + 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 = [] return priv, Point(AffineCoordinateModel(self.model), x = Mod(pub_x, self.params.curve.prime), y = Mod(pub_y, self.params.curve.prime)) @@ -303,7 +308,8 @@ class SimulatorTarget(Target): pass def disconnect(self): - pass + self.simulator.start(self.simulator.functions['deinit'] | 1, 0) + self.simulator.reset() diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c index af82603..4d50cc2 100644 --- a/pyecsca/codegen/templates/main.c +++ b/pyecsca/codegen/templates/main.c @@ -568,6 +568,10 @@ __attribute__((noinline)) void init(void) { init_uart(); trigger_setup(); + init_implementation(); +} + +__attribute__((noinline)) void init_implementation(void) { // Initialize some components that preallocate stuff. prng_init(); formulas_init(); |
