Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten nk3 and trussed modules #17

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# E501 (line length) disabled as this is handled by black which takes better care of edge cases
extend-ignore = E203,E501,E701
max-complexity = 18
extend-exclude = src/nitrokey/trussed/bootloader/_nrf52_upload
extend-exclude = src/nitrokey/trussed/_bootloader/nrf52_upload
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ strict = true

# nrf52_upload is only temporary in this package
[[tool.mypy.overrides]]
module = "nitrokey.trussed.bootloader._nrf52_upload.*"
module = "nitrokey.trussed._bootloader.nrf52_upload.*"
ignore_errors = true

# nrf52 has to use the untyped nrf52_upload module
[[tool.mypy.overrides]]
module = "nitrokey.trussed.bootloader.nrf52"
module = "nitrokey.trussed._bootloader.nrf52"
disallow_untyped_calls = false

# libraries without annotations
Expand Down
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
24 changes: 10 additions & 14 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.bootloader.nrf52 import SignatureKey
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
12 changes: 4 additions & 8 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.trussed.admin_app import BootMode
from nitrokey.trussed.base import NitrokeyTrussedBase
from nitrokey.trussed.bootloader import (
from nitrokey.nk3 import NK3_DATA, Nitrokey3Bootloader, Nitrokey3Device
from nitrokey.trussed import NitrokeyTrussedBase, TimeoutException, Version
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.trussed.admin_app import BootMode
from nitrokey.updates import Asset, Release

logger = logging.getLogger(__name__)
Expand Down
30 changes: 17 additions & 13 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.trussed.bootloader.nrf52 import (
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
23 changes: 20 additions & 3 deletions src/nitrokey/trussed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,27 @@

from nitrokey.updates import Repository

if TYPE_CHECKING:
from .bootloader.nrf52 import SignatureKey
from ._base import NitrokeyTrussedBase as NitrokeyTrussedBase # noqa: F401
from ._bootloader import Device as Device # noqa: F401
from ._bootloader import FirmwareContainer as FirmwareContainer # noqa: F401
from ._bootloader import FirmwareMetadata as FirmwareMetadata # noqa: F401
from ._bootloader import ( # noqa: F401
NitrokeyTrussedBootloader as NitrokeyTrussedBootloader,
)
from ._bootloader import Variant as Variant # noqa: F401
from ._bootloader import parse_firmware_image as parse_firmware_image # 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

VID_NITROKEY = 0x20A0
if TYPE_CHECKING:
from ._bootloader.nrf52 import SignatureKey


@dataclass
Expand Down
Loading