aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/sca/target/chipwhisperer.py
blob: 4037f120b49f42918e04124e43070c844c4a7c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
This module provides a `ChipWhisperer <https://github.com/newaetech/chipwhisperer/>`_ target class.
ChipWhisperer is a side-channel analysis tool and framework. A ChipWhisperer target is one
that uses the ChipWhisperer's SimpleSerial communication protocol and is communicated with
using ChipWhisperer-Lite or Pro.
"""
from time import sleep

import chipwhisperer as cw
from chipwhisperer.capture.scopes.base import ScopeTemplate
from chipwhisperer.capture.targets import SimpleSerial
from public import public

from .flash import Flashable
from .simpleserial import SimpleSerialTarget


@public
class ChipWhispererTarget(Flashable, SimpleSerialTarget):  # pragma: no cover
    """
    A ChipWhisperer-based target, using the SimpleSerial protocol and communicating
    using ChipWhisperer-Lite/Pro.
    """

    def __init__(
        self, target: SimpleSerial, scope: ScopeTemplate, programmer, **kwargs
    ):
        super().__init__()
        self.target = target
        self.scope = scope
        self.programmer = programmer

    def connect(self):
        self.target.con(self.scope)
        self.target.baud = 115200
        sleep(0.5)

    def flash(self, fw_path: str) -> bool:
        try:
            cw.program_target(self.scope, self.programmer, fw_path)
        except Exception as e:
            print(e)
            return False
        return True

    def write(self, data: bytes) -> None:
        self.target.flush()
        self.target.write(data.decode())

    def read(self, num: int = 0, timeout: int = 0) -> bytes:
        return self.target.read(num, timeout).encode()

    def reset(self):
        self.scope.io.nrst = "low"
        sleep(0.05)
        self.scope.io.nrst = "high"
        sleep(0.05)

    def disconnect(self):
        self.target.dis()