nitrokey 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. nitrokey/__init__.py +1 -0
  2. nitrokey/_helpers.py +36 -0
  3. nitrokey/nk3/__init__.py +54 -0
  4. nitrokey/nk3/_bootloader.py +82 -0
  5. nitrokey/nk3/_device.py +48 -0
  6. nitrokey/nk3/secrets_app.py +879 -0
  7. nitrokey/nk3/updates.py +367 -0
  8. nitrokey/nkpk.py +102 -0
  9. nitrokey/py.typed +0 -0
  10. nitrokey/trussed/__init__.py +21 -0
  11. nitrokey/trussed/_base.py +60 -0
  12. nitrokey/trussed/_bootloader/__init__.py +221 -0
  13. nitrokey/trussed/_bootloader/lpc55.py +146 -0
  14. nitrokey/trussed/_bootloader/nrf52.py +228 -0
  15. nitrokey/trussed/_bootloader/nrf52_upload/README.md +6 -0
  16. nitrokey/trussed/_bootloader/nrf52_upload/__init__.py +38 -0
  17. nitrokey/trussed/_bootloader/nrf52_upload/dfu/__init__.py +38 -0
  18. nitrokey/trussed/_bootloader/nrf52_upload/dfu/crc16.py +54 -0
  19. nitrokey/trussed/_bootloader/nrf52_upload/dfu/dfu_cc_pb2.py +866 -0
  20. nitrokey/trussed/_bootloader/nrf52_upload/dfu/dfu_transport.py +173 -0
  21. nitrokey/trussed/_bootloader/nrf52_upload/dfu/dfu_transport_serial.py +589 -0
  22. nitrokey/trussed/_bootloader/nrf52_upload/dfu/dfu_trigger.py +229 -0
  23. nitrokey/trussed/_bootloader/nrf52_upload/dfu/init_packet_pb.py +193 -0
  24. nitrokey/trussed/_bootloader/nrf52_upload/dfu/manifest.py +212 -0
  25. nitrokey/trussed/_bootloader/nrf52_upload/dfu/model.py +57 -0
  26. nitrokey/trussed/_bootloader/nrf52_upload/dfu/nrfhex.py +188 -0
  27. nitrokey/trussed/_bootloader/nrf52_upload/dfu/package.py +763 -0
  28. nitrokey/trussed/_bootloader/nrf52_upload/dfu/signing.py +256 -0
  29. nitrokey/trussed/_bootloader/nrf52_upload/exceptions.py +9 -0
  30. nitrokey/trussed/_bootloader/nrf52_upload/lister/__init__.py +38 -0
  31. nitrokey/trussed/_bootloader/nrf52_upload/lister/device_lister.py +88 -0
  32. nitrokey/trussed/_bootloader/nrf52_upload/lister/enumerated_device.py +71 -0
  33. nitrokey/trussed/_bootloader/nrf52_upload/lister/lister_backend.py +45 -0
  34. nitrokey/trussed/_bootloader/nrf52_upload/lister/unix/__init__.py +0 -0
  35. nitrokey/trussed/_bootloader/nrf52_upload/lister/unix/unix_lister.py +74 -0
  36. nitrokey/trussed/_bootloader/nrf52_upload/lister/windows/__init__.py +0 -0
  37. nitrokey/trussed/_bootloader/nrf52_upload/lister/windows/constants.py +90 -0
  38. nitrokey/trussed/_bootloader/nrf52_upload/lister/windows/lister_win32.py +318 -0
  39. nitrokey/trussed/_bootloader/nrf52_upload/lister/windows/structures.py +145 -0
  40. nitrokey/trussed/_device.py +135 -0
  41. nitrokey/trussed/_exceptions.py +15 -0
  42. nitrokey/trussed/_utils.py +245 -0
  43. nitrokey/trussed/admin_app.py +290 -0
  44. nitrokey/trussed/provisioner_app.py +57 -0
  45. nitrokey/updates.py +163 -0
  46. nitrokey-0.1.0.dist-info/LICENSES/Apache-2.0.txt +73 -0
  47. nitrokey-0.1.0.dist-info/LICENSES/MIT.txt +9 -0
  48. nitrokey-0.1.0.dist-info/METADATA +133 -0
  49. nitrokey-0.1.0.dist-info/RECORD +50 -0
  50. nitrokey-0.1.0.dist-info/WHEEL +4 -0
nitrokey/__init__.py ADDED
@@ -0,0 +1 @@
1
+ _VID_NITROKEY = 0x20A0
nitrokey/_helpers.py ADDED
@@ -0,0 +1,36 @@
1
+ import time
2
+
3
+
4
+ class Try:
5
+ """Utility class for an execution of a repeated action with Retries."""
6
+
7
+ def __init__(self, i: int, retries: int) -> None:
8
+ self.i = i
9
+ self.retries = retries
10
+
11
+ def __str__(self) -> str:
12
+ return f"try {self.i + 1} of {self.retries}"
13
+
14
+ def __repr__(self) -> str:
15
+ return f"Try(i={self.i}, retries={self.retries})"
16
+
17
+
18
+ class Retries:
19
+ """Utility class for repeating an action multiple times until it succeeds."""
20
+
21
+ def __init__(self, retries: int, timeout: float = 0.5) -> None:
22
+ self.retries = retries
23
+ self.i = 0
24
+ self.timeout = timeout
25
+
26
+ def __iter__(self) -> "Retries":
27
+ return self
28
+
29
+ def __next__(self) -> Try:
30
+ if self.i >= self.retries:
31
+ raise StopIteration
32
+ if self.i > 0:
33
+ time.sleep(self.timeout)
34
+ t = Try(self.i, self.retries)
35
+ self.i += 1
36
+ return t
@@ -0,0 +1,54 @@
1
+ # Copyright 2021-2022 Nitrokey Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4
+ # http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5
+ # http://opensource.org/licenses/MIT>, at your option. This file may not be
6
+ # copied, modified, or distributed except according to those terms.
7
+
8
+ from typing import List, Optional, Union
9
+
10
+ from nitrokey.trussed._bootloader import ModelData
11
+ from nitrokey.trussed._bootloader.nrf52 import SignatureKey
12
+
13
+ from ._bootloader import NK3Bootloader as NK3Bootloader # noqa: F401
14
+ from ._device import NK3 as NK3 # noqa: F401
15
+
16
+ _PID_NK3_DEVICE = 0x42B2
17
+ _PID_NK3_LPC55_BOOTLOADER = 0x42DD
18
+ _PID_NK3_NRF52_BOOTLOADER = 0x42E8
19
+
20
+ _NK3_DATA = ModelData(
21
+ firmware_repository_name="nitrokey-3-firmware",
22
+ firmware_pattern_string="firmware-nk3-v.*\\.zip$",
23
+ nrf52_signature_keys=[
24
+ SignatureKey(
25
+ name="Nitrokey",
26
+ is_official=True,
27
+ der="3059301306072a8648ce3d020106082a8648ce3d03010703420004a0849b19007ccd4661c01c533804b7fd0c4d8c0e7583653f1f36a8331afff298b542bd00a3dc47c16bf428ac4d2864137d63f702d89e5b42674e0549b4232618",
28
+ ),
29
+ SignatureKey(
30
+ name="Nitrokey Test",
31
+ is_official=False,
32
+ der="3059301306072a8648ce3d020106082a8648ce3d0301070342000493e461ab0582bda1f45b0ce47d66bc4e8623e289c31af2098cde6ebd8631da85acf17e412d406c1e38c2de654a8fd0196506a85b169a756aeac2505a541cdd5d",
33
+ ),
34
+ ],
35
+ )
36
+
37
+
38
+ def list() -> List[Union[NK3, NK3Bootloader]]:
39
+ devices: List[Union[NK3, NK3Bootloader]] = []
40
+ devices.extend(NK3Bootloader.list())
41
+ devices.extend(NK3.list())
42
+ return devices
43
+
44
+
45
+ def open(path: str) -> Optional[Union[NK3, NK3Bootloader]]:
46
+ device = NK3.open(path)
47
+ bootloader_device = NK3Bootloader.open(path)
48
+ if device and bootloader_device:
49
+ raise Exception(f"Found multiple devices at path {path}")
50
+ if device:
51
+ return device
52
+ if bootloader_device:
53
+ return bootloader_device
54
+ return None
@@ -0,0 +1,82 @@
1
+ # Copyright 2021-2022 Nitrokey Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4
+ # http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5
+ # http://opensource.org/licenses/MIT>, at your option. This file may not be
6
+ # copied, modified, or distributed except according to those terms.
7
+
8
+ from typing import List, Optional, Sequence
9
+
10
+ from nitrokey import _VID_NITROKEY
11
+ from nitrokey.trussed._bootloader import TrussedBootloader
12
+ from nitrokey.trussed._bootloader.lpc55 import TrussedBootloaderLpc55
13
+ from nitrokey.trussed._bootloader.nrf52 import SignatureKey, TrussedBootloaderNrf52
14
+
15
+
16
+ class NK3Bootloader(TrussedBootloader):
17
+ @staticmethod
18
+ def list() -> List["NK3Bootloader"]:
19
+ devices: List[NK3Bootloader] = []
20
+ devices.extend(NK3BootloaderLpc55._list())
21
+ devices.extend(NK3BootloaderNrf52._list())
22
+ return devices
23
+
24
+ @staticmethod
25
+ def open(path: str) -> Optional["NK3Bootloader"]:
26
+ lpc55 = NK3BootloaderLpc55._open(path)
27
+ if lpc55:
28
+ return lpc55
29
+
30
+ nrf52 = NK3BootloaderNrf52._open(path)
31
+ if nrf52:
32
+ return nrf52
33
+
34
+ return None
35
+
36
+
37
+ class NK3BootloaderLpc55(TrussedBootloaderLpc55, NK3Bootloader):
38
+ @property
39
+ def name(self) -> str:
40
+ return "Nitrokey 3 Bootloader (LPC55)"
41
+
42
+ @property
43
+ def pid(self) -> int:
44
+ from . import _PID_NK3_LPC55_BOOTLOADER
45
+
46
+ return _PID_NK3_LPC55_BOOTLOADER
47
+
48
+ @classmethod
49
+ def _list(cls) -> List["NK3BootloaderLpc55"]:
50
+ from . import _PID_NK3_LPC55_BOOTLOADER
51
+
52
+ return cls._list_vid_pid(_VID_NITROKEY, _PID_NK3_LPC55_BOOTLOADER)
53
+
54
+
55
+ class NK3BootloaderNrf52(TrussedBootloaderNrf52, NK3Bootloader):
56
+ @property
57
+ def name(self) -> str:
58
+ return "Nitrokey 3 Bootloader (NRF52)"
59
+
60
+ @property
61
+ def pid(self) -> int:
62
+ from . import _PID_NK3_NRF52_BOOTLOADER
63
+
64
+ return _PID_NK3_NRF52_BOOTLOADER
65
+
66
+ @classmethod
67
+ def _list(cls) -> List["NK3BootloaderNrf52"]:
68
+ from . import _PID_NK3_NRF52_BOOTLOADER
69
+
70
+ return cls._list_vid_pid(_VID_NITROKEY, _PID_NK3_NRF52_BOOTLOADER)
71
+
72
+ @classmethod
73
+ def _open(cls, path: str) -> Optional["NK3BootloaderNrf52"]:
74
+ from . import _PID_NK3_NRF52_BOOTLOADER
75
+
76
+ return cls._open_vid_pid(_VID_NITROKEY, _PID_NK3_NRF52_BOOTLOADER, path)
77
+
78
+ @property
79
+ def _signature_keys(self) -> Sequence[SignatureKey]:
80
+ from . import _NK3_DATA
81
+
82
+ return _NK3_DATA.nrf52_signature_keys
@@ -0,0 +1,48 @@
1
+ # Copyright 2021 Nitrokey Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4
+ # http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5
+ # http://opensource.org/licenses/MIT>, at your option. This file may not be
6
+ # copied, modified, or distributed except according to those terms.
7
+
8
+ from fido2.hid import CtapHidDevice
9
+
10
+ from nitrokey.trussed import Fido2Certs, TrussedDevice, Version
11
+
12
+ FIDO2_CERTS = [
13
+ Fido2Certs(
14
+ start=Version(0, 1, 0),
15
+ hashes=[
16
+ "ad8fd1d16f59104b9e06ef323cc03f777ed5303cd421a101c9cb00bb3fdf722d",
17
+ ],
18
+ ),
19
+ Fido2Certs(
20
+ start=Version(1, 0, 3),
21
+ hashes=[
22
+ "aa1cb760c2879530e7d7fed3da75345d25774be9cfdbbcbd36fdee767025f34b", # NK3xN/lpc55
23
+ "4c331d7af869fd1d8217198b917a33d1fa503e9778da7638504a64a438661ae0", # NK3AM/nrf52
24
+ "f1ed1aba24b16e8e3fabcda72b10cbfa54488d3b778bda552162d60c6dd7b4fa", # NK3AM/nrf52 test
25
+ ],
26
+ ),
27
+ ]
28
+
29
+
30
+ class NK3(TrussedDevice):
31
+ """A Nitrokey 3 device running the firmware."""
32
+
33
+ def __init__(self, device: CtapHidDevice) -> None:
34
+ super().__init__(device, FIDO2_CERTS)
35
+
36
+ @property
37
+ def pid(self) -> int:
38
+ from . import _PID_NK3_DEVICE
39
+
40
+ return _PID_NK3_DEVICE
41
+
42
+ @property
43
+ def name(self) -> str:
44
+ return "Nitrokey 3"
45
+
46
+ @classmethod
47
+ def from_device(cls, device: CtapHidDevice) -> "NK3":
48
+ return cls(device)