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.

@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.

@IanJ-Arm
Copy link
Author

@d3zd3z Gentle ping on this. Is there anything I can do to help the review? Thanks!

@nordicjm
Copy link
Collaborator

Please rebase

@IanJ-Arm IanJ-Arm force-pushed the imgtool-pkcs11-signing-flat branch from 2474bf7 to 11cb738 Compare October 10, 2025 12:33
@IanJ-Arm
Copy link
Author

@nordicjm Rebased, fixed a couple of minor conflicts.

@nordicjm
Copy link
Collaborator

Commit sign off fix needed

@IanJ-Arm
Copy link
Author

Where sorry? SoB looks right to me.

@nordicjm
Copy link
Collaborator

@IanJ-Arm
Copy link
Author

The checker is case sensitive on the email address?

@IanJ-Arm IanJ-Arm force-pushed the imgtool-pkcs11-signing-flat branch from 11cb738 to 5dc35c0 Compare October 10, 2025 12:49
@IanJ-Arm
Copy link
Author

IanJ-Arm commented Oct 10, 2025

Thanks @fundakol . What about imgtool.nix? Should python-pkcs11 be added there too?
[Edit: added]

@IanJ-Arm IanJ-Arm force-pushed the imgtool-pkcs11-signing-flat branch from 5dc35c0 to 3e6141c Compare October 10, 2025 14:04
@fundakol
Copy link
Collaborator

Thanks @fundakol . What about imgtool.nix? Should python-pkcs11 be added there too? [Edit: added]

I have never used that tool, and I don't know if it is still used in this repo by anyone, but I would add it just in case.

Copy link
Collaborator

@fundakol fundakol left a comment

Choose a reason for hiding this comment

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

LGTM

@davidvincze davidvincze added the 2.3.0 Issues to be brought into 2.3.0 label Oct 13, 2025
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 3e6141c to 8a57afd Compare October 13, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.3.0 Issues to be brought into 2.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants