spsdk-iped 0.1.0__tar.gz
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.
- spsdk_iped-0.1.0/LICENSE +28 -0
- spsdk_iped-0.1.0/MANIFEST.in +3 -0
- spsdk_iped-0.1.0/PKG-INFO +85 -0
- spsdk_iped-0.1.0/README.md +59 -0
- spsdk_iped-0.1.0/pyproject.toml +111 -0
- spsdk_iped-0.1.0/setup.cfg +4 -0
- spsdk_iped-0.1.0/setup.py +54 -0
- spsdk_iped-0.1.0/spsdk_iped/__init__.py +16 -0
- spsdk_iped-0.1.0/spsdk_iped/iped.py +213 -0
- spsdk_iped-0.1.0/spsdk_iped.egg-info/PKG-INFO +85 -0
- spsdk_iped-0.1.0/spsdk_iped.egg-info/SOURCES.txt +19 -0
- spsdk_iped-0.1.0/spsdk_iped.egg-info/dependency_links.txt +1 -0
- spsdk_iped-0.1.0/spsdk_iped.egg-info/requires.txt +1 -0
- spsdk_iped-0.1.0/spsdk_iped.egg-info/top_level.txt +1 -0
- spsdk_iped-0.1.0/src/PrinceCore.cpp +1098 -0
- spsdk_iped-0.1.0/src/PrinceCore.h +64 -0
- spsdk_iped-0.1.0/src/ip_prince_ctr.cpp +290 -0
- spsdk_iped-0.1.0/src/ip_prince_ctr.h +91 -0
- spsdk_iped-0.1.0/src/ip_prince_model.cpp +636 -0
- spsdk_iped-0.1.0/src/iped_model_c.h +50 -0
- spsdk_iped-0.1.0/tests/test_iped.py +131 -0
spsdk_iped-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026, NXP
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spsdk-iped
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast C++ PRINCE cipher backend for SPSDK IPED encryption.
|
|
5
|
+
Author: NXP
|
|
6
|
+
Maintainer-email: NXP <spsdk@nxp.com>
|
|
7
|
+
License: BSD-3-Clause
|
|
8
|
+
Project-URL: Homepage, https://github.com/nxp-mcuxpresso/spsdk_plugins/tree/master/iped
|
|
9
|
+
Project-URL: Issues, https://github.com/nxp-mcuxpresso/spsdk_plugins/issues
|
|
10
|
+
Keywords: NXP,SPSDK,IPED,PRINCE,encryption
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: spsdk<4,>=3.10.0.dev0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# SPSDK IPED - PRINCE Cipher Backend
|
|
28
|
+
|
|
29
|
+
High-performance C++ PRINCE cipher backend for SPSDK's IPED
|
|
30
|
+
(Inline PRINCE Encryption/Decryption) module.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
### Prerequisites
|
|
35
|
+
|
|
36
|
+
A C++ compiler is required to build the native PRINCE cipher library:
|
|
37
|
+
- **Linux/macOS**: GCC or Clang (install via your package manager)
|
|
38
|
+
- **Windows**: MS Build Tools ([instructions](https://stackoverflow.com/questions/40504552/how-to-install-visual-c-build-tools))
|
|
39
|
+
|
|
40
|
+
### Install from source
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install -e path/to/spsdk_plugins/iped
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Install from Bitbucket
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install git+ssh://git@bitbucket.sw.nxp.com/spsdk/spsdk_plugins.git#subdirectory=iped
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage with SPSDK
|
|
53
|
+
|
|
54
|
+
When this package is installed, SPSDK's IPED module automatically detects the C++ backend
|
|
55
|
+
and uses it for ~100-150x faster PRINCE cipher operations compared to the built-in
|
|
56
|
+
pure-Python fallback.
|
|
57
|
+
|
|
58
|
+
No additional configuration is needed — just install the package alongside SPSDK.
|
|
59
|
+
|
|
60
|
+
## Standalone usage
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from spsdk_iped import IPED
|
|
64
|
+
|
|
65
|
+
# CTR mode encryption
|
|
66
|
+
cipher = IPED(key=0x01, address=0x80003000, iv=0x79EAFAB3A72412A1)
|
|
67
|
+
encrypted = cipher.encrypt(plaintext_bytes)
|
|
68
|
+
|
|
69
|
+
# Decryption
|
|
70
|
+
cipher2 = IPED(key=0x01, address=0x80003000, iv=0x79EAFAB3A72412A1)
|
|
71
|
+
decrypted = cipher2.decrypt(encrypted)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Performance
|
|
75
|
+
|
|
76
|
+
The C++ backend provides approximately 100-150x speedup over the pure-Python implementation:
|
|
77
|
+
|
|
78
|
+
| Backend | Throughput |
|
|
79
|
+
|---------|-----------|
|
|
80
|
+
| Python (built-in) | ~0.03 MB/s |
|
|
81
|
+
| C++ (this package) | ~5 MB/s |
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
BSD-3-Clause
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# SPSDK IPED - PRINCE Cipher Backend
|
|
2
|
+
|
|
3
|
+
High-performance C++ PRINCE cipher backend for SPSDK's IPED
|
|
4
|
+
(Inline PRINCE Encryption/Decryption) module.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
### Prerequisites
|
|
9
|
+
|
|
10
|
+
A C++ compiler is required to build the native PRINCE cipher library:
|
|
11
|
+
- **Linux/macOS**: GCC or Clang (install via your package manager)
|
|
12
|
+
- **Windows**: MS Build Tools ([instructions](https://stackoverflow.com/questions/40504552/how-to-install-visual-c-build-tools))
|
|
13
|
+
|
|
14
|
+
### Install from source
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install -e path/to/spsdk_plugins/iped
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Install from Bitbucket
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install git+ssh://git@bitbucket.sw.nxp.com/spsdk/spsdk_plugins.git#subdirectory=iped
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage with SPSDK
|
|
27
|
+
|
|
28
|
+
When this package is installed, SPSDK's IPED module automatically detects the C++ backend
|
|
29
|
+
and uses it for ~100-150x faster PRINCE cipher operations compared to the built-in
|
|
30
|
+
pure-Python fallback.
|
|
31
|
+
|
|
32
|
+
No additional configuration is needed — just install the package alongside SPSDK.
|
|
33
|
+
|
|
34
|
+
## Standalone usage
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from spsdk_iped import IPED
|
|
38
|
+
|
|
39
|
+
# CTR mode encryption
|
|
40
|
+
cipher = IPED(key=0x01, address=0x80003000, iv=0x79EAFAB3A72412A1)
|
|
41
|
+
encrypted = cipher.encrypt(plaintext_bytes)
|
|
42
|
+
|
|
43
|
+
# Decryption
|
|
44
|
+
cipher2 = IPED(key=0x01, address=0x80003000, iv=0x79EAFAB3A72412A1)
|
|
45
|
+
decrypted = cipher2.decrypt(encrypted)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Performance
|
|
49
|
+
|
|
50
|
+
The C++ backend provides approximately 100-150x speedup over the pure-Python implementation:
|
|
51
|
+
|
|
52
|
+
| Backend | Throughput |
|
|
53
|
+
|---------|-----------|
|
|
54
|
+
| Python (built-in) | ~0.03 MB/s |
|
|
55
|
+
| C++ (this package) | ~5 MB/s |
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
BSD-3-Clause
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "spsdk-iped"
|
|
3
|
+
description = "Fast C++ PRINCE cipher backend for SPSDK IPED encryption."
|
|
4
|
+
dynamic = ["version"]
|
|
5
|
+
requires-python = ">=3.9"
|
|
6
|
+
dependencies = ["spsdk>=3.10.0.dev0,<4"]
|
|
7
|
+
authors = [{ name = "NXP" }]
|
|
8
|
+
maintainers = [{ name = "NXP", email = "spsdk@nxp.com" }]
|
|
9
|
+
readme = { file = "README.md", content-type = "text/markdown" }
|
|
10
|
+
license = { text = "BSD-3-Clause" }
|
|
11
|
+
keywords = ["NXP", "SPSDK", "IPED", "PRINCE", "encryption"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: BSD License",
|
|
16
|
+
"Natural Language :: English",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.9",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/nxp-mcuxpresso/spsdk_plugins/tree/master/iped"
|
|
27
|
+
Issues = "https://github.com/nxp-mcuxpresso/spsdk_plugins/issues"
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["setuptools >= 61.0", "wheel"]
|
|
31
|
+
build-backend = "setuptools.build_meta"
|
|
32
|
+
|
|
33
|
+
[tool.setuptools]
|
|
34
|
+
packages = ["spsdk_iped"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.dynamic]
|
|
37
|
+
version = { attr = "spsdk_iped.__version__" }
|
|
38
|
+
|
|
39
|
+
[tool.bumpversion]
|
|
40
|
+
current_version = "0.1.0"
|
|
41
|
+
|
|
42
|
+
[[tool.bumpversion.files]]
|
|
43
|
+
filename = "spsdk_iped/__init__.py"
|
|
44
|
+
search = '__version__ = "{current_version}"'
|
|
45
|
+
replace = '__version__ = "{new_version}"'
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
testpaths = ["tests"]
|
|
49
|
+
|
|
50
|
+
[tool.coverage.report]
|
|
51
|
+
exclude_lines = ["pragma: no cover"]
|
|
52
|
+
|
|
53
|
+
[tool.coverage.run]
|
|
54
|
+
relative_files = true
|
|
55
|
+
|
|
56
|
+
[tool.black]
|
|
57
|
+
line-length = 100
|
|
58
|
+
target-version = ["py39", "py310", "py311", "py312"]
|
|
59
|
+
include = '\.pyi?$'
|
|
60
|
+
|
|
61
|
+
[tool.isort]
|
|
62
|
+
multi_line_output = 3
|
|
63
|
+
include_trailing_comma = true
|
|
64
|
+
force_grid_wrap = 0
|
|
65
|
+
use_parentheses = true
|
|
66
|
+
ensure_newline_before_comments = true
|
|
67
|
+
line_length = 100
|
|
68
|
+
|
|
69
|
+
[tool.mypy]
|
|
70
|
+
disallow_untyped_defs = true
|
|
71
|
+
ignore_missing_imports = true
|
|
72
|
+
warn_unused_ignores = true
|
|
73
|
+
|
|
74
|
+
[tool.pydocstyle]
|
|
75
|
+
convention = "google"
|
|
76
|
+
add_ignore = "D105,D301"
|
|
77
|
+
match = '(?!test_|__version__).*\.py'
|
|
78
|
+
|
|
79
|
+
[tool.pylint]
|
|
80
|
+
format = { max-line-length = 120 }
|
|
81
|
+
"messages control" = { disable = ["logging-fstring-interpolation", "attribute-defined-outside-init"] }
|
|
82
|
+
|
|
83
|
+
[tool.nxp_codecheck]
|
|
84
|
+
git_parent_branch = "origin/master"
|
|
85
|
+
output_directory = "reports"
|
|
86
|
+
default_check_paths = ["spsdk_iped"]
|
|
87
|
+
checkers = [
|
|
88
|
+
{ "pytest" = { kwargs = { disable_xdist = true } } },
|
|
89
|
+
{ "gitcov" = { info_only = true } },
|
|
90
|
+
"pylint",
|
|
91
|
+
"mypy",
|
|
92
|
+
{ "dependencies" = { info_only = true } },
|
|
93
|
+
"pydocstyle",
|
|
94
|
+
"ruff",
|
|
95
|
+
{ "radon_c" = { info_only = true } },
|
|
96
|
+
"radon_d",
|
|
97
|
+
"black",
|
|
98
|
+
"isort",
|
|
99
|
+
"copyright",
|
|
100
|
+
"py_headers",
|
|
101
|
+
{ "cspell" = { info_only = true } },
|
|
102
|
+
{ "bandit" = { info_only = false, kwargs = { "severity-level" = "medium" } } },
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
[tool.release-tools.blackduck_sca]
|
|
106
|
+
project_name = "spsdk_plugins_iped"
|
|
107
|
+
include_detector_types = ["setuptools"]
|
|
108
|
+
sbom_location = "SBOM-spsdk_plugins_iped.spdx.json"
|
|
109
|
+
|
|
110
|
+
[tool.release-tools.scr]
|
|
111
|
+
scr_file = "SW_Content_Register_spsdk_iped.txt"
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# Copyright 2024-2026 NXP
|
|
5
|
+
#
|
|
6
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
"""Setup script with C++ extension compilation for PRINCE cipher library."""
|
|
9
|
+
|
|
10
|
+
from distutils.command.build_ext import build_ext
|
|
11
|
+
|
|
12
|
+
from setuptools import Extension, setup
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class CTypesExtension(Extension):
|
|
16
|
+
"""Extension subclass for ctypes shared libraries."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BuildCtypesExtension(build_ext):
|
|
20
|
+
"""Custom build_ext that produces a plain .so instead of Python extension module."""
|
|
21
|
+
|
|
22
|
+
def build_extension(self, ext) -> None: # type: ignore[override]
|
|
23
|
+
"""Build the extension module."""
|
|
24
|
+
self._ctypes = isinstance(ext, CTypesExtension)
|
|
25
|
+
return super().build_extension(ext)
|
|
26
|
+
|
|
27
|
+
def get_ext_filename(self, ext_name: str) -> str:
|
|
28
|
+
"""Get output filename without Python ABI suffix for ctypes libs."""
|
|
29
|
+
if self._ctypes:
|
|
30
|
+
return ext_name.replace(".", "/") + ".so"
|
|
31
|
+
return super().get_ext_filename(ext_name)
|
|
32
|
+
|
|
33
|
+
def get_export_symbols(self, ext: Extension) -> list:
|
|
34
|
+
"""Get export symbols for ctypes extension."""
|
|
35
|
+
if self._ctypes:
|
|
36
|
+
return ext.export_symbols # type: ignore[return-value]
|
|
37
|
+
return super().get_export_symbols(ext)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
prince_lib = CTypesExtension(
|
|
41
|
+
name="spsdk_iped.prince",
|
|
42
|
+
sources=[
|
|
43
|
+
"src/ip_prince_ctr.cpp",
|
|
44
|
+
"src/ip_prince_model.cpp",
|
|
45
|
+
"src/PrinceCore.cpp",
|
|
46
|
+
],
|
|
47
|
+
include_dirs=["src"],
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
setup(
|
|
52
|
+
ext_modules=[prince_lib],
|
|
53
|
+
cmdclass={"build_ext": BuildCtypesExtension},
|
|
54
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# Copyright 2024-2026 NXP
|
|
5
|
+
#
|
|
6
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
"""SPSDK IPED - Fast C++ PRINCE cipher backend for IPED encryption."""
|
|
9
|
+
|
|
10
|
+
__author__ = "NXP"
|
|
11
|
+
__email__ = "spsdk@nxp.com"
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
|
|
14
|
+
from .iped import IPED, IPEDError
|
|
15
|
+
|
|
16
|
+
__all__ = ["IPED", "IPEDError"]
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# Copyright 2024-2026 NXP
|
|
5
|
+
#
|
|
6
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
7
|
+
|
|
8
|
+
"""IPED PRINCE cipher engine using C++ shared library via ctypes."""
|
|
9
|
+
|
|
10
|
+
import ctypes
|
|
11
|
+
import logging
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import Optional, Union
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class IPEDError(Exception):
|
|
19
|
+
"""IPED operation error."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class _Transaction( # pylint: disable=too-few-public-methods,too-many-instance-attributes
|
|
23
|
+
ctypes.Structure
|
|
24
|
+
):
|
|
25
|
+
"""C struct matching the prince_mem_enc transaction layout."""
|
|
26
|
+
|
|
27
|
+
_fields_ = [
|
|
28
|
+
("mode", ctypes.c_uint32),
|
|
29
|
+
("dec", ctypes.c_uint32),
|
|
30
|
+
("run_dbl_enc", ctypes.c_uint32),
|
|
31
|
+
("i_data", ctypes.POINTER(ctypes.c_uint64)),
|
|
32
|
+
("iv", ctypes.c_uint64),
|
|
33
|
+
("address", ctypes.POINTER(ctypes.c_uint64)),
|
|
34
|
+
("data_key0", ctypes.c_uint64),
|
|
35
|
+
("data_key1", ctypes.c_uint64),
|
|
36
|
+
("o_data", ctypes.POINTER(ctypes.c_uint64)),
|
|
37
|
+
("in_ad", ctypes.c_uint64),
|
|
38
|
+
("in_auth_tag", ctypes.c_uint64),
|
|
39
|
+
("o_auth_tag", ctypes.c_uint64),
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class IPED: # pylint: disable=too-many-instance-attributes
|
|
44
|
+
"""IPED PRINCE cipher wrapper over native C++ shared library.
|
|
45
|
+
|
|
46
|
+
Provides CTR and GCM encryption/decryption using a compiled PRINCE
|
|
47
|
+
cipher core (``prince.so`` / ``prince.dll``).
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
BLOCK_SIZE = 8
|
|
51
|
+
KEY_SIZE = 16
|
|
52
|
+
|
|
53
|
+
def __init__( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
54
|
+
self,
|
|
55
|
+
key: Union[int, bytes],
|
|
56
|
+
address: Union[int, bytes],
|
|
57
|
+
iv: Union[int, bytes],
|
|
58
|
+
use_gcm: bool = False,
|
|
59
|
+
aad: Union[int, bytes, None] = None,
|
|
60
|
+
tag: Union[int, bytes] = 0,
|
|
61
|
+
double_encrypt: bool = False,
|
|
62
|
+
) -> None:
|
|
63
|
+
"""Initialize IPED cipher instance.
|
|
64
|
+
|
|
65
|
+
:param key: 128-bit encryption key (int or 16 bytes).
|
|
66
|
+
:param address: Starting memory address for encryption.
|
|
67
|
+
:param iv: 64-bit initialization vector.
|
|
68
|
+
:param use_gcm: Use GCM mode (True) or CTR mode (False).
|
|
69
|
+
:param aad: Additional Authentication Data for GCM mode.
|
|
70
|
+
:param tag: Authentication tag for GCM decryption verification.
|
|
71
|
+
:param double_encrypt: Enable double-encryption (22 effective rounds).
|
|
72
|
+
:raises IPEDError: If parameters are invalid or library cannot be loaded.
|
|
73
|
+
"""
|
|
74
|
+
self.key = (
|
|
75
|
+
key if isinstance(key, bytes) else key.to_bytes(length=self.KEY_SIZE, byteorder="big")
|
|
76
|
+
)
|
|
77
|
+
self.key0 = int.from_bytes(self.key[: self.BLOCK_SIZE], byteorder="big")
|
|
78
|
+
self.key1 = int.from_bytes(self.key[self.BLOCK_SIZE :], byteorder="big")
|
|
79
|
+
|
|
80
|
+
if len(self.key) != 2 * self.BLOCK_SIZE:
|
|
81
|
+
raise IPEDError(
|
|
82
|
+
f"Invalid key length. Expected {2 * self.BLOCK_SIZE}B, got {len(self.key)}B."
|
|
83
|
+
)
|
|
84
|
+
self.iv = iv if isinstance(iv, int) else int.from_bytes(iv, byteorder="big")
|
|
85
|
+
if self.iv.bit_length() > 8 * self.BLOCK_SIZE:
|
|
86
|
+
raise IPEDError(f"IV is too big. Expected up to {self.BLOCK_SIZE}B.")
|
|
87
|
+
|
|
88
|
+
self.next_address = (
|
|
89
|
+
address if isinstance(address, int) else int.from_bytes(address, byteorder="big")
|
|
90
|
+
)
|
|
91
|
+
self.double_encrypt = 0x1 if double_encrypt else 0x0
|
|
92
|
+
|
|
93
|
+
self.mode = 0x1 if use_gcm else 0x0
|
|
94
|
+
if use_gcm:
|
|
95
|
+
if aad is None:
|
|
96
|
+
raise IPEDError("GCM encryption requires AAD (Additional Authentication Data)")
|
|
97
|
+
self.aad = aad if isinstance(aad, int) else int.from_bytes(aad, byteorder="big")
|
|
98
|
+
else:
|
|
99
|
+
self.aad = 0
|
|
100
|
+
self.tag = tag if isinstance(tag, int) else int.from_bytes(tag, byteorder="big")
|
|
101
|
+
|
|
102
|
+
self.lib_file = Path(__file__).parent / "prince.so"
|
|
103
|
+
if not self.lib_file.exists():
|
|
104
|
+
raise IPEDError(
|
|
105
|
+
f"PRINCE shared library not found at {self.lib_file}. "
|
|
106
|
+
"Make sure the package was built with C++ extension compilation."
|
|
107
|
+
)
|
|
108
|
+
self._lib = ctypes.CDLL(str(self.lib_file))
|
|
109
|
+
self.prince_mem_enc = self._lib["prince_mem_enc"]
|
|
110
|
+
self.prince_mem_enc.argtypes = [ctypes.c_int, ctypes.POINTER(_Transaction)]
|
|
111
|
+
self.prince_mem_enc.restype = ctypes.c_int
|
|
112
|
+
|
|
113
|
+
def decrypt(self, data: Union[bytes, int], address: Optional[int] = None) -> bytes:
|
|
114
|
+
"""Decrypt data using PRINCE cipher.
|
|
115
|
+
|
|
116
|
+
:param data: Encrypted data (bytes or single 64-bit int).
|
|
117
|
+
:param address: Optional override address for this block.
|
|
118
|
+
:return: Decrypted data bytes.
|
|
119
|
+
"""
|
|
120
|
+
if isinstance(data, int):
|
|
121
|
+
data = data.to_bytes(length=8, byteorder="big")
|
|
122
|
+
return self._transaction(decrypt=True, data=data, address=address)
|
|
123
|
+
|
|
124
|
+
def encrypt(self, data: Union[bytes, int], address: Optional[int] = None) -> bytes:
|
|
125
|
+
"""Encrypt data using PRINCE cipher.
|
|
126
|
+
|
|
127
|
+
:param data: Plaintext data (bytes or single 64-bit int).
|
|
128
|
+
:param address: Optional override address for this block.
|
|
129
|
+
:return: Encrypted data bytes.
|
|
130
|
+
"""
|
|
131
|
+
if isinstance(data, int):
|
|
132
|
+
data = data.to_bytes(length=8, byteorder="big")
|
|
133
|
+
return self._transaction(decrypt=False, data=data, address=address)
|
|
134
|
+
|
|
135
|
+
def is_gcm(self) -> bool:
|
|
136
|
+
"""Check if GCM mode is enabled.
|
|
137
|
+
|
|
138
|
+
:return: True if GCM mode, False if CTR mode.
|
|
139
|
+
"""
|
|
140
|
+
return self.mode == 1
|
|
141
|
+
|
|
142
|
+
def _transaction(self, decrypt: bool, data: bytes, address: Optional[int] = None) -> bytes:
|
|
143
|
+
"""Execute PRINCE cipher transaction via native library.
|
|
144
|
+
|
|
145
|
+
:param decrypt: True for decryption, False for encryption.
|
|
146
|
+
:param data: Input data bytes.
|
|
147
|
+
:param address: Optional override starting address.
|
|
148
|
+
:return: Output data bytes.
|
|
149
|
+
:raises IPEDError: If native library returns error code.
|
|
150
|
+
"""
|
|
151
|
+
transaction = _Transaction()
|
|
152
|
+
transaction.mode = self.mode
|
|
153
|
+
transaction.run_dbl_enc = self.double_encrypt
|
|
154
|
+
transaction.dec = 0x1 if decrypt else 0x0
|
|
155
|
+
transaction.iv = self.iv
|
|
156
|
+
transaction.data_key0 = self.key0
|
|
157
|
+
transaction.data_key1 = self.key1
|
|
158
|
+
transaction.in_ad = self.aad
|
|
159
|
+
transaction.in_auth_tag = self.tag
|
|
160
|
+
transaction.o_auth_tag = 0x0
|
|
161
|
+
|
|
162
|
+
address = address if address is not None else self.next_address
|
|
163
|
+
|
|
164
|
+
n_blocks = (len(data) + self.BLOCK_SIZE - 1) // self.BLOCK_SIZE
|
|
165
|
+
padded_size = n_blocks * self.BLOCK_SIZE
|
|
166
|
+
data = data.ljust(padded_size, b"\x00")
|
|
167
|
+
|
|
168
|
+
if self.is_gcm():
|
|
169
|
+
o_data = (ctypes.c_uint64 * n_blocks)()
|
|
170
|
+
i_address = (ctypes.c_uint64 * n_blocks)()
|
|
171
|
+
i_data = (ctypes.c_uint64 * n_blocks)()
|
|
172
|
+
|
|
173
|
+
for i in range(n_blocks):
|
|
174
|
+
i_address[i] = address + i * 8
|
|
175
|
+
i_data[i] = int.from_bytes(data[8 * i : 8 * (i + 1)], byteorder="big")
|
|
176
|
+
|
|
177
|
+
transaction.address = i_address
|
|
178
|
+
transaction.i_data = i_data
|
|
179
|
+
transaction.o_data = o_data
|
|
180
|
+
|
|
181
|
+
status_code = self.prince_mem_enc(n_blocks, ctypes.byref(transaction))
|
|
182
|
+
|
|
183
|
+
if status_code != 1:
|
|
184
|
+
raise IPEDError(f"Encryption failed with error code: {status_code}")
|
|
185
|
+
|
|
186
|
+
self.tag = transaction.o_auth_tag
|
|
187
|
+
self.next_address = address + padded_size
|
|
188
|
+
return b"".join(o.to_bytes(8, "big") for o in o_data)
|
|
189
|
+
|
|
190
|
+
# CTR mode: process block by block
|
|
191
|
+
o_data = (ctypes.c_uint64 * 1)()
|
|
192
|
+
i_address = (ctypes.c_uint64 * 1)()
|
|
193
|
+
i_data = (ctypes.c_uint64 * 1)()
|
|
194
|
+
|
|
195
|
+
result = bytearray()
|
|
196
|
+
|
|
197
|
+
for i in range(n_blocks):
|
|
198
|
+
i_data[0] = int.from_bytes(data[8 * i : 8 * (i + 1)], byteorder="big")
|
|
199
|
+
i_address[0] = address + 8 * i
|
|
200
|
+
|
|
201
|
+
transaction.i_data = i_data
|
|
202
|
+
transaction.address = i_address
|
|
203
|
+
transaction.o_data = o_data
|
|
204
|
+
|
|
205
|
+
status_code = self.prince_mem_enc(1, ctypes.byref(transaction))
|
|
206
|
+
|
|
207
|
+
if status_code != 1:
|
|
208
|
+
raise IPEDError(f"Encryption failed with error code: {status_code}")
|
|
209
|
+
|
|
210
|
+
result.extend(o_data[0].to_bytes(8, "big"))
|
|
211
|
+
|
|
212
|
+
self.next_address = address + padded_size
|
|
213
|
+
return bytes(result)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spsdk-iped
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast C++ PRINCE cipher backend for SPSDK IPED encryption.
|
|
5
|
+
Author: NXP
|
|
6
|
+
Maintainer-email: NXP <spsdk@nxp.com>
|
|
7
|
+
License: BSD-3-Clause
|
|
8
|
+
Project-URL: Homepage, https://github.com/nxp-mcuxpresso/spsdk_plugins/tree/master/iped
|
|
9
|
+
Project-URL: Issues, https://github.com/nxp-mcuxpresso/spsdk_plugins/issues
|
|
10
|
+
Keywords: NXP,SPSDK,IPED,PRINCE,encryption
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: spsdk<4,>=3.10.0.dev0
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# SPSDK IPED - PRINCE Cipher Backend
|
|
28
|
+
|
|
29
|
+
High-performance C++ PRINCE cipher backend for SPSDK's IPED
|
|
30
|
+
(Inline PRINCE Encryption/Decryption) module.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
### Prerequisites
|
|
35
|
+
|
|
36
|
+
A C++ compiler is required to build the native PRINCE cipher library:
|
|
37
|
+
- **Linux/macOS**: GCC or Clang (install via your package manager)
|
|
38
|
+
- **Windows**: MS Build Tools ([instructions](https://stackoverflow.com/questions/40504552/how-to-install-visual-c-build-tools))
|
|
39
|
+
|
|
40
|
+
### Install from source
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install -e path/to/spsdk_plugins/iped
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Install from Bitbucket
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install git+ssh://git@bitbucket.sw.nxp.com/spsdk/spsdk_plugins.git#subdirectory=iped
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage with SPSDK
|
|
53
|
+
|
|
54
|
+
When this package is installed, SPSDK's IPED module automatically detects the C++ backend
|
|
55
|
+
and uses it for ~100-150x faster PRINCE cipher operations compared to the built-in
|
|
56
|
+
pure-Python fallback.
|
|
57
|
+
|
|
58
|
+
No additional configuration is needed — just install the package alongside SPSDK.
|
|
59
|
+
|
|
60
|
+
## Standalone usage
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from spsdk_iped import IPED
|
|
64
|
+
|
|
65
|
+
# CTR mode encryption
|
|
66
|
+
cipher = IPED(key=0x01, address=0x80003000, iv=0x79EAFAB3A72412A1)
|
|
67
|
+
encrypted = cipher.encrypt(plaintext_bytes)
|
|
68
|
+
|
|
69
|
+
# Decryption
|
|
70
|
+
cipher2 = IPED(key=0x01, address=0x80003000, iv=0x79EAFAB3A72412A1)
|
|
71
|
+
decrypted = cipher2.decrypt(encrypted)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Performance
|
|
75
|
+
|
|
76
|
+
The C++ backend provides approximately 100-150x speedup over the pure-Python implementation:
|
|
77
|
+
|
|
78
|
+
| Backend | Throughput |
|
|
79
|
+
|---------|-----------|
|
|
80
|
+
| Python (built-in) | ~0.03 MB/s |
|
|
81
|
+
| C++ (this package) | ~5 MB/s |
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
BSD-3-Clause
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.py
|
|
6
|
+
spsdk_iped/__init__.py
|
|
7
|
+
spsdk_iped/iped.py
|
|
8
|
+
spsdk_iped.egg-info/PKG-INFO
|
|
9
|
+
spsdk_iped.egg-info/SOURCES.txt
|
|
10
|
+
spsdk_iped.egg-info/dependency_links.txt
|
|
11
|
+
spsdk_iped.egg-info/requires.txt
|
|
12
|
+
spsdk_iped.egg-info/top_level.txt
|
|
13
|
+
src/PrinceCore.cpp
|
|
14
|
+
src/PrinceCore.h
|
|
15
|
+
src/ip_prince_ctr.cpp
|
|
16
|
+
src/ip_prince_ctr.h
|
|
17
|
+
src/ip_prince_model.cpp
|
|
18
|
+
src/iped_model_c.h
|
|
19
|
+
tests/test_iped.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
spsdk<4,>=3.10.0.dev0
|