aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/sca/target
diff options
context:
space:
mode:
authorAndrej Bátora2023-09-27 21:31:06 +0200
committerAndrej Bátora2023-09-27 21:31:06 +0200
commit570ec60fc4e365d8dc5c7eef9ff2dc784ec7318b (patch)
treec53bba33bdd8a6fe389879262f35166975dfc2b1 /pyecsca/sca/target
parent2e97eae75817d6a002e2a8797102cfd149436736 (diff)
downloadpyecsca-570ec60fc4e365d8dc5c7eef9ff2dc784ec7318b.tar.gz
pyecsca-570ec60fc4e365d8dc5c7eef9ff2dc784ec7318b.tar.zst
pyecsca-570ec60fc4e365d8dc5c7eef9ff2dc784ec7318b.zip
Diffstat (limited to 'pyecsca/sca/target')
-rw-r--r--pyecsca/sca/target/emulator.py123
1 files changed, 123 insertions, 0 deletions
diff --git a/pyecsca/sca/target/emulator.py b/pyecsca/sca/target/emulator.py
new file mode 100644
index 0000000..a0b30e1
--- /dev/null
+++ b/pyecsca/sca/target/emulator.py
@@ -0,0 +1,123 @@
+from pyecsca.ec.coordinates import CoordinateModel
+from pyecsca.ec.mod import Mod
+from pyecsca.ec.model import CurveModel
+from pyecsca.ec.params import DomainParameters
+from pyecsca.ec.point import Point, InfinityPoint
+from pyecsca.ec.mult import ScalarMultiplier
+from pyecsca.ec.key_generation import KeyGeneration
+from pyecsca.ec.key_agreement import KeyAgreement
+from pyecsca.ec.signature import Signature
+from pyecsca.ec.formula import FormulaAction
+from pyecsca.ec.context import Context, DefaultContext, local
+from pyecsca.sca.attack import LeakageModel
+from pyecsca.sca.trace import Trace, average, subtract
+from typing import Mapping, Union, Optional, Tuple
+from public import public
+from .base import Target
+import numpy as np
+
+@public
+class EmulatorTarget(Target):
+
+ model: CurveModel
+ coords: CoordinateModel
+ mult: ScalarMultiplier
+ params: Optional[DomainParameters]
+ leakage_model: LeakageModel
+ privkey: Optional[int]
+ pubkey: Optional[Point]
+
+ def __init__(self, model: CurveModel, coords: CoordinateModel, mult: ScalarMultiplier):
+ super().__init__()
+ self.model = model
+ self.coords = coords
+ self.mult = mult
+ self.params = None
+ self.leakage_model = None
+ self.privkey = None
+ self.pubkey = None
+
+ def get_trace(self, context: DefaultContext) -> Trace:
+ def callback(action):
+ if isinstance(action, FormulaAction):
+ for intermediate in action.op_results:
+ leak = self.leakage_model(intermediate.value)
+ temp_trace.append(leak)
+ temp_trace = []
+ context.actions.walk(callback)
+ return Trace(np.array(temp_trace))
+
+ def emulate_scalar_mult_traces(self, num_of_traces: int, scalar: int) -> Tuple[list[Point], list[Trace]]:
+ points = [self.params.curve.affine_random().to_model(self.coords, self.params.curve) for _ in range(num_of_traces)]
+ traces = []
+ for point in points:
+ _, trace = self.scalar_mult(scalar, point)
+ traces.append(trace)
+ return points, traces
+
+ def emulate_ecdh_traces(self, num_of_traces: int) -> Tuple[list[Trace], list[Trace]]:
+ other_pubs = [self.params.curve.affine_random().to_model(self.coords, self.params.curve) for _ in range(num_of_traces)]
+ traces = []
+ for pub in other_pubs:
+ _, trace = self.ecdh(pub, None)
+ traces.append(trace)
+ return other_pubs, traces
+
+ def set_params(self, params: DomainParameters) -> None:
+ self.params = params
+
+ def set_leakage_model(self, leakage_model: LeakageModel) -> None:
+ self.leakage_model = leakage_model
+
+ def scalar_mult(self, scalar: int, point: Point) -> Tuple[Point, Trace]:
+ with local(DefaultContext()) as ctx:
+ self.mult.init(self.params, point)
+ res_point = self.mult.multiply(scalar)
+ return res_point, self.get_trace(ctx)
+
+ def generate(self) -> Tuple[Tuple[Mod, Point], Trace]:
+ with local(DefaultContext()) as ctx:
+ keygen = KeyGeneration(self.mult, self.params, False)
+ priv, pub = keygen.generate()
+ return (priv, pub), self.get_trace(ctx)
+
+ def set_privkey(self, privkey: int) -> None:
+ self.privkey = privkey
+
+ def set_pubkey(self, pubkey: Point) -> None:
+ self.pubkey = pubkey
+
+ def ecdh(self, other_pubkey: Point, hash_algo=None) -> Tuple[bytes, Trace]:
+ with local(DefaultContext()) as ctx:
+ ecdh = KeyAgreement(self.mult, self.params, other_pubkey, self.privkey, hash_algo)
+ shared_secret = ecdh.perform()
+ return shared_secret, self.get_trace(ctx)
+
+ def ecdsa_sign(self, data: bytes, hash_algo=None) -> Tuple[bytes, Trace]:
+ with local(DefaultContext()) as ctx:
+ ecdsa = Signature(self.mult, self.params, self.mult.formulas["add"], self.pubkey, self.privkey, hash_algo)
+ signed_data = ecdsa.sign_data(data)
+ return signed_data, self.get_trace(ctx)
+
+ def ecdsa_verify(self, data: bytes, signature: bytes, hash_algo=None) -> Tuple[bool, Trace]:
+ with local(DefaultContext()) as ctx:
+ ecdsa = Signature(self.mult, self.params, self.mult.formulas["add"], self.pubkey, hash_algo)
+ verified = ecdsa.verify_data(signature, data)
+ return verified, self.get_trace(ctx)
+
+ def debug(self):
+ return self.model.shortname, self.coords.name
+
+ def connect(self):
+ super().connect()
+
+ def disconnect(self):
+ super().disconnect()
+
+ def set_strigger(self):
+ pass
+
+ def quit(self):
+ pass
+
+