Skip to content

Commit

Permalink
Flatten nk3 and trussed modules
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-nitrokey committed Jul 25, 2024
1 parent d85dc6f commit daee003
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 142 deletions.
1 change: 1 addition & 0 deletions src/nitrokey/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_VID_NITROKEY = 0x20A0
22 changes: 9 additions & 13 deletions src/nitrokey/nk3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

from typing import List, Optional

from nitrokey.trussed import DeviceData
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed import DeviceData, NitrokeyTrussedBase
from nitrokey.trussed.bootloader.nrf52 import SignatureKey

PID_NITROKEY3_DEVICE = 0x42B2
PID_NITROKEY3_LPC55_BOOTLOADER = 0x42DD
PID_NITROKEY3_NRF52_BOOTLOADER = 0x42E8
from ._bootloader import Nitrokey3Bootloader as Nitrokey3Bootloader # noqa: F401
from ._device import Nitrokey3Device as Nitrokey3Device # noqa: F401

_PID_NITROKEY3_DEVICE = 0x42B2
_PID_NITROKEY3_LPC55_BOOTLOADER = 0x42DD
_PID_NITROKEY3_NRF52_BOOTLOADER = 0x42E8

NK3_DATA = DeviceData(
name="Nitrokey 3",
Expand All @@ -35,21 +37,15 @@


def list() -> List[NitrokeyTrussedBase]:
from . import bootloader
from .device import Nitrokey3Device

devices: List[NitrokeyTrussedBase] = []
devices.extend(bootloader.list())
devices.extend(Nitrokey3Bootloader.list())
devices.extend(Nitrokey3Device.list())
return devices


def open(path: str) -> Optional[NitrokeyTrussedBase]:
from . import bootloader
from .device import Nitrokey3Device

device = Nitrokey3Device.open(path)
bootloader_device = bootloader.open(path)
bootloader_device = Nitrokey3Bootloader.open(path)
if device and bootloader_device:
raise Exception(f"Found multiple devices at path {path}")
if device:
Expand Down
85 changes: 85 additions & 0 deletions src/nitrokey/nk3/_bootloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2021-2022 Nitrokey Developers
#
# Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
# http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
# http://opensource.org/licenses/MIT>, at your option. This file may not be
# copied, modified, or distributed except according to those terms.

from typing import List, Optional, Sequence

from nitrokey import _VID_NITROKEY
from nitrokey.trussed.bootloader import NitrokeyTrussedBootloader
from nitrokey.trussed.bootloader.lpc55 import NitrokeyTrussedBootloaderLpc55
from nitrokey.trussed.bootloader.nrf52 import (
NitrokeyTrussedBootloaderNrf52,
SignatureKey,
)


class Nitrokey3Bootloader(NitrokeyTrussedBootloader):
@staticmethod
def list() -> List["Nitrokey3Bootloader"]:
devices: List[Nitrokey3Bootloader] = []
devices.extend(Nitrokey3BootloaderLpc55._list())
devices.extend(Nitrokey3BootloaderNrf52._list())
return devices

@staticmethod
def open(path: str) -> Optional["Nitrokey3Bootloader"]:
lpc55 = Nitrokey3BootloaderLpc55._open(path)
if lpc55:
return lpc55

nrf52 = Nitrokey3BootloaderNrf52._open(path)
if nrf52:
return nrf52

return None


class Nitrokey3BootloaderLpc55(NitrokeyTrussedBootloaderLpc55, Nitrokey3Bootloader):
@property
def name(self) -> str:
return "Nitrokey 3 Bootloader (LPC55)"

@property
def pid(self) -> int:
from . import _PID_NITROKEY3_LPC55_BOOTLOADER

return _PID_NITROKEY3_LPC55_BOOTLOADER

@classmethod
def _list(cls) -> List["Nitrokey3BootloaderLpc55"]:
from . import _PID_NITROKEY3_LPC55_BOOTLOADER

return cls.list_vid_pid(_VID_NITROKEY, _PID_NITROKEY3_LPC55_BOOTLOADER)


class Nitrokey3BootloaderNrf52(NitrokeyTrussedBootloaderNrf52, Nitrokey3Bootloader):
@property
def name(self) -> str:
return "Nitrokey 3 Bootloader (NRF52)"

@property
def pid(self) -> int:
from . import _PID_NITROKEY3_NRF52_BOOTLOADER

return _PID_NITROKEY3_NRF52_BOOTLOADER

@classmethod
def _list(cls) -> List["Nitrokey3BootloaderNrf52"]:
from . import _PID_NITROKEY3_NRF52_BOOTLOADER

return cls.list_vid_pid(_VID_NITROKEY, _PID_NITROKEY3_NRF52_BOOTLOADER)

@classmethod
def _open(cls, path: str) -> Optional["Nitrokey3BootloaderNrf52"]:
from . import _PID_NITROKEY3_NRF52_BOOTLOADER

return cls.open_vid_pid(_VID_NITROKEY, _PID_NITROKEY3_NRF52_BOOTLOADER, path)

@property
def signature_keys(self) -> Sequence[SignatureKey]:
from . import NK3_DATA

return NK3_DATA.nrf52_signature_keys
7 changes: 3 additions & 4 deletions src/nitrokey/nk3/device.py → src/nitrokey/nk3/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

from fido2.hid import CtapHidDevice

from nitrokey.trussed.device import NitrokeyTrussedDevice
from nitrokey.trussed.utils import Fido2Certs, Version
from nitrokey.trussed import Fido2Certs, NitrokeyTrussedDevice, Version

FIDO2_CERTS = [
Fido2Certs(
Expand Down Expand Up @@ -36,9 +35,9 @@ def __init__(self, device: CtapHidDevice) -> None:

@property
def pid(self) -> int:
from . import PID_NITROKEY3_DEVICE
from . import _PID_NITROKEY3_DEVICE

return PID_NITROKEY3_DEVICE
return _PID_NITROKEY3_DEVICE

@property
def name(self) -> str:
Expand Down
87 changes: 0 additions & 87 deletions src/nitrokey/nk3/bootloader.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/nitrokey/nk3/secrets_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import tlv8
from semver.version import Version

from nitrokey.nk3.device import Nitrokey3Device
from nitrokey.trussed.device import App
from nitrokey.nk3 import Nitrokey3Device
from nitrokey.trussed import App

LogFn = Callable[[str], Any]
WriteCorpusFn = Callable[[typing.Union["Instruction", "CCIDInstruction"], bytes], Any]
Expand Down
8 changes: 2 additions & 6 deletions src/nitrokey/nk3/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
from spsdk.mboot.exceptions import McuBootConnectionError

from nitrokey._helpers import Retries
from nitrokey.nk3 import NK3_DATA
from nitrokey.nk3.bootloader import Nitrokey3Bootloader
from nitrokey.nk3.device import Nitrokey3Device
from nitrokey.nk3 import NK3_DATA, Nitrokey3Bootloader, Nitrokey3Device
from nitrokey.trussed import NitrokeyTrussedBase, TimeoutException, Version
from nitrokey.trussed.admin_app import BootMode
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.bootloader import (
Device,
FirmwareContainer,
Variant,
validate_firmware_image,
)
from nitrokey.trussed.exceptions import TimeoutException
from nitrokey.trussed.utils import Version
from nitrokey.updates import Asset, Release

logger = logging.getLogger(__name__)
Expand Down
28 changes: 16 additions & 12 deletions src/nitrokey/nkpk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@

from fido2.hid import CtapHidDevice

from nitrokey.trussed import VID_NITROKEY, DeviceData
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey import _VID_NITROKEY
from nitrokey.trussed import (
DeviceData,
Fido2Certs,
NitrokeyTrussedBase,
NitrokeyTrussedDevice,
Version,
)
from nitrokey.trussed.bootloader.nrf52 import (
NitrokeyTrussedBootloaderNrf52,
SignatureKey,
)
from nitrokey.trussed.device import NitrokeyTrussedDevice
from nitrokey.trussed.utils import Fido2Certs, Version

PID_NITROKEY_PASSKEY_DEVICE = 0x42F3
PID_NITROKEY_PASSKEY_BOOTLOADER = 0x42F4
_PID_NITROKEY_PASSKEY_DEVICE = 0x42F3
_PID_NITROKEY_PASSKEY_BOOTLOADER = 0x42F4

FIDO2_CERTS = [
_FIDO2_CERTS = [
Fido2Certs(
start=Version(0, 1, 0),
hashes=[
Expand Down Expand Up @@ -51,11 +55,11 @@

class NitrokeyPasskeyDevice(NitrokeyTrussedDevice):
def __init__(self, device: CtapHidDevice) -> None:
super().__init__(device, FIDO2_CERTS)
super().__init__(device, _FIDO2_CERTS)

@property
def pid(self) -> int:
return PID_NITROKEY_PASSKEY_DEVICE
return _PID_NITROKEY_PASSKEY_DEVICE

@property
def name(self) -> str:
Expand All @@ -73,15 +77,15 @@ def name(self) -> str:

@property
def pid(self) -> int:
return PID_NITROKEY_PASSKEY_BOOTLOADER
return _PID_NITROKEY_PASSKEY_BOOTLOADER

@classmethod
def list(cls) -> List["NitrokeyPasskeyBootloader"]:
return cls.list_vid_pid(VID_NITROKEY, PID_NITROKEY_PASSKEY_BOOTLOADER)
return cls.list_vid_pid(_VID_NITROKEY, _PID_NITROKEY_PASSKEY_BOOTLOADER)

@classmethod
def open(cls, path: str) -> Optional["NitrokeyPasskeyBootloader"]:
return cls.open_vid_pid(VID_NITROKEY, PID_NITROKEY_PASSKEY_BOOTLOADER, path)
return cls.open_vid_pid(_VID_NITROKEY, _PID_NITROKEY_PASSKEY_BOOTLOADER, path)

@property
def signature_keys(self) -> Sequence[SignatureKey]:
Expand Down
13 changes: 11 additions & 2 deletions src/nitrokey/trussed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@

from nitrokey.updates import Repository

from ._base import NitrokeyTrussedBase as NitrokeyTrussedBase # noqa: F401
from ._device import App as App # noqa: F401
from ._device import NitrokeyTrussedDevice as NitrokeyTrussedDevice # noqa: F401
from ._exceptions import ( # noqa: F401
NitrokeyTrussedException as NitrokeyTrussedException,
)
from ._exceptions import TimeoutException as TimeoutException # noqa: F401
from ._utils import Fido2Certs as Fido2Certs # noqa: F401
from ._utils import Uuid as Uuid # noqa: F401
from ._utils import Version as Version # noqa: F401

if TYPE_CHECKING:
from .bootloader.nrf52 import SignatureKey

VID_NITROKEY = 0x20A0


@dataclass
class DeviceData:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from abc import ABC, abstractmethod
from typing import Optional, TypeVar

from . import VID_NITROKEY
from .utils import Uuid
from nitrokey import _VID_NITROKEY

from ._utils import Uuid

T = TypeVar("T", bound="NitrokeyTrussedBase")

Expand All @@ -35,7 +36,7 @@ def validate_vid_pid(self, vid: int, pid: int) -> None:

@property
def vid(self) -> int:
return VID_NITROKEY
return _VID_NITROKEY

@property
@abstractmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

from fido2.hid import CtapHidDevice, open_device

from .base import NitrokeyTrussedBase
from .utils import Fido2Certs, Uuid
from ._base import NitrokeyTrussedBase
from ._utils import Fido2Certs, Uuid

T = TypeVar("T", bound="NitrokeyTrussedDevice")

Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit daee003

Please sign in to comment.