Skip to content

Commit f26d3e0

Browse files
committed
Encryption changes needed for NCS build system
Ref: NCSDK-30935 Signed-off-by: Artur Hadasz <[email protected]>
1 parent f07d308 commit f26d3e0

File tree

5 files changed

+86
-212
lines changed

5 files changed

+86
-212
lines changed

ncs/Kconfig

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,50 @@ config SUIT_DFU_CACHE_EXTRACT_IMAGE_URI
5959
default "cache://rad_recovery.bin" if (SOC_NRF54H20_CPURAD_COMMON || SOC_NRF9230_ENGB_CPURAD) && SUIT_RECOVERY
6060

6161
endif # SUIT_DFU_CACHE_EXTRACT_IMAGE
62+
63+
config SUIT_ENVELOPE_TARGET_ENCRYPT
64+
bool "Encrypt the target image"
65+
66+
if SUIT_ENVELOPE_TARGET_ENCRYPT
67+
68+
config SUIT_ENVELOPE_TARGET_ENCRYPT_STRING_KEY_ID
69+
string "The string key ID used to identify the encryption key on the device"
70+
default "FWENC_APPLICATION_GEN1" if SOC_NRF54H20_CPUAPP_COMMON
71+
default "FWENC_RADIOCORE_GEN1" if SOC_NRF54H20_CPURAD_COMMON
72+
help
73+
This string is translated to the numeric KEY ID by the encryption script
74+
75+
config SUIT_ENVELOPE_TARGET_ENCRYPT_KEY_NAME
76+
string "Name of the key used for encryption - to identify the key in the KMS"
77+
default SUIT_ENVELOPE_TARGET_ENCRYPT_STRING_KEY_ID
78+
79+
choice SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG
80+
prompt "Algorithm used to calculate the digest of the plaintext firmware"
81+
default SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA256
82+
83+
config SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA256
84+
bool "Use the SHA-256 algorithm"
85+
86+
config SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA384
87+
bool "Use the SHA-384 algorithm"
88+
89+
config SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA512
90+
bool "Use the SHA-512 algorithm"
91+
92+
config SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHAKE128
93+
bool "Use the SHAKE128 algorithm"
94+
95+
config SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHAKE256
96+
bool "Use the SHAKE256 algorithm"
97+
98+
endchoice
99+
100+
config SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_NAME
101+
string
102+
default "sha-256" if SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA256
103+
default "sha-384" if SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA384
104+
default "sha-512" if SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHA512
105+
default "shake128" if SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHAKE128
106+
default "shake256" if SUIT_ENVELOPE_TARGET_ENCRYPT_PLAINTEXT_HASH_ALG_SHAKE256
107+
108+
endif # SUIT_ENVELOPE_TARGET_ENCRYPT

ncs/app_envelope_encrypted.yaml.jinja2

Lines changed: 0 additions & 170 deletions
This file was deleted.

ncs/basic_kms.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@ def parse_context(self, context):
2222
self.keys_directory = Path(__file__).parent
2323
return None
2424

25-
context_loaded = json.loads(context)
26-
self.keys_directory = Path(context_loaded["keys_directory"])
25+
# Check if context is a valid path
26+
context_path = Path(context)
27+
if context_path.is_dir():
28+
self.keys_directory = context_path
29+
return
30+
31+
try:
32+
context_loaded = json.loads(context)
33+
except json.JSONDecodeError:
34+
raise ValueError(f"The provided context '{context}' is neither a valid path nor a valid JSON string.")
35+
36+
try:
37+
self.keys_directory = Path(context_loaded["keys_directory"])
38+
except KeyError:
39+
raise ValueError(f"The provided json context '{context}' does not contain the 'keys_directory' key.")
2740

2841
def init_kms(self, context) -> None:
2942
"""
@@ -35,13 +48,13 @@ def init_kms(self, context) -> None:
3548

3649
def encrypt(self, plaintext, key_name, context, aad) -> tuple[bytes, bytes, bytes]:
3750
"""
38-
Encrypt the plainext with an AES key.
51+
Encrypt the plaintext with an AES key.
3952
4053
:param plaintext: The plaintext to be encrypted.
4154
:param key_name: The name of the key to be used.
4255
:param context: The context to be used
4356
If it is passed, it is used to point to the directory where the keys are stored.
44-
In this case, it must be a JSON string in te format '{ "keys_directory":"<path>" }'.
57+
It can either be a path or a JSON string in the format '{ "keys_directory":"<path>" }'.
4558
:param aad: The additional authenticated data to be used.
4659
:return: The nonce, tag and ciphertext.
4760
:rtype: tuple[bytes, bytes, bytes]

ncs/build.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@ def read_configurations(configurations):
4040
# Parse obligatory arguments
4141
name, binary, edt, kconfig = args[:4]
4242

43-
# Parse optional arguments
44-
if len(args) > 4:
45-
encryption_artifacts_dir = args[4]
46-
else:
47-
encryption_artifacts_dir = None
48-
4943
edt_data = None
5044
if edt:
5145
with open(edt, "rb") as edt_handler:
@@ -69,8 +63,6 @@ def read_configurations(configurations):
6963
if binary:
7064
data[image_name]["filename"] = pathlib.Path(binary).name
7165
data[image_name]["binary"] = binary
72-
if encryption_artifacts_dir:
73-
data[image_name]["encryption_artifacts_dir"] = encryption_artifacts_dir
7466
data["get_absolute_address"] = get_absolute_address
7567
return data
7668

0 commit comments

Comments
 (0)