aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/sca/target/chipwhisperer.py
blob: 0528618df40242c728d1818cc1854ea1f1f88cf0 (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
from binascii import unhexlify
from typing import Optional
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

    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):
        self.target.flush()
        self.target.write(data.decode())

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

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