Skip to content

Conversation

IanJ-Arm
Copy link

@IanJ-Arm IanJ-Arm commented Sep 5, 2025

Based on work submitted in relation to:
Issue #599 ref: #599
particularly these commits:
grandcentrix/mcuboot@82441bd4286
grandcentrix/mcuboot@010ea89f
rretanubun@33c6400a40

Updated and modified to support ECDSA P384 keys.
Tests also updated and fixed, tested with SoftHSMv2.

@IanJ-Arm IanJ-Arm requested a review from d3zd3z as a code owner September 5, 2025 09:51
def __init__(self, uri, env=None):
if env is None:
env = os.environ
if not 'PKCS11_PIN' in env.keys():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not 'PKCS11_PIN' in env.keys():
if 'PKCS11_PIN' not in env:


def unquote_to_bytes(urlencoded_string):
"""Replace %xx escapes by their single-character equivalent,
using the “iso-8859-1” encoding to decode all 8-bit values.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add indentation with 4 spaces.

if not 'PKCS11_PIN' in env.keys():
raise RuntimeError("Environment variable PKCS11_PIN not set. Set it to the user PIN.")
params = get_pkcs11_uri_params(uri)
assert b'serial' in params.keys()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert b'serial' in params.keys()
assert b'serial' in params

raise RuntimeError("Environment variable PKCS11_PIN not set. Set it to the user PIN.")
params = get_pkcs11_uri_params(uri)
assert b'serial' in params.keys()
assert b'id' in params.keys() or b'label' in params.keys()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert b'id' in params.keys() or b'label' in params.keys()
assert b'id' in params or b'label' in params

Comment on lines 76 to 77
lib = ''
try:
lib = pkcs11.lib(pkcs11_module_path)
except RuntimeError:
pass # happens if lib does not exist or is corrupt
if '' == lib:
raise RuntimeError(f"PKCS11 module {pkcs11_module_path} not loaded.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess pkcs11.lib(pkcs11_module_path) does not return an empty string.

Suggested change
lib = ''
try:
lib = pkcs11.lib(pkcs11_module_path)
except RuntimeError:
pass # happens if lib does not exist or is corrupt
if '' == lib:
raise RuntimeError(f"PKCS11 module {pkcs11_module_path} not loaded.")
try:
lib = pkcs11.lib(pkcs11_module_path)
except RuntimeError:
raise RuntimeError(f"PKCS11 module {pkcs11_module_path} not loaded.")

# signatures to be padded to a fixed length. Because the DER
# encoding is done with signed integers, the size of the
# signature will vary depending on whether the high bit is set
# in each value. This padding was done in a
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove double space.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment was copied from here:
https://github.com/mcu-tools/mcuboot/blob/main/scripts/imgtool/keys/ecdsa.py#L161
But sure, will fix this and address all the other feedback, thanks!


def sig_len(self):
# Early versions of MCUboot (< v1.5.0) required ECDSA
# signatures to be padded to a fixed length. Because the DER
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove double space.

# not-easily-reversible way (by just adding zeros).
#
# The signing code no longer requires this padding, and newer
# versions of MCUboot don't require it. But, continue to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove double space.

sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '../..')))

from datetime import datetime
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be grouped in the following order:

  • Standard library imports.
  • Related third party imports.
  • Local application/library specific imports.

)
pubkey.destroy()


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep single line separation between methods.

Based on work submitted in relation to:
Issue mcu-tools#599 ref: mcu-tools#599
particularly these commits:
grandcentrix/mcuboot@82441bd4286
grandcentrix/mcuboot@010ea89f
rretanubun@33c6400a40

Updated and modified to support ECDSA P384 keys.
Tests also updated and fixed, tested with SoftHSMv2.

Signed-off-by: Ian Jamison <[email protected]>
Co-authored-by: Michael Zimmermann <[email protected]>
Co-authored-by: Nils Dagsson Moskopp <[email protected]>
Co-authored-by: Richard Retanubun <[email protected]>
Change-Id: I175b710834bd20a868961634483d43b459959769
@IanJ-Arm IanJ-Arm force-pushed the imgtool-pkcs11-signing-flat branch from 21627be to 2474bf7 Compare September 10, 2025 13:54
@IanJ-Arm
Copy link
Author

@fundakol Pushed a revised set which hopefully addresses all your points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants