Skip to content

Commit a1a4e95

Browse files
Harshal5radimkarnis
authored andcommitted
feat(esp_hsm_sign): Use primitive HSM signing mechanisms
- Closes #1128
1 parent 97e25ef commit a1a4e95

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

espsecure/esp_hsm_sign/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import binascii
66
import configparser
7+
import hashlib
78
import os
89
import sys
910
from esptool.logger import log
@@ -145,8 +146,9 @@ def sign_payload(private_key: pkcs11.Key, payload: bytes) -> bytes:
145146
log.print("Signing payload using the HSM...")
146147
key_type = private_key.key_type
147148
mechanism, mechanism_params = get_mechanism(key_type)
149+
hashed_payload = hashlib.sha256(payload).digest()
148150
signature: bytes = private_key.sign(
149-
data=payload, mechanism=mechanism, mechanism_param=mechanism_params
151+
data=hashed_payload, mechanism=mechanism, mechanism_param=mechanism_params
150152
)
151153

152154
if len(signature) != 0:
@@ -171,13 +173,13 @@ def get_mechanism(
171173
key_type: pkcs11.mechanisms.KeyType,
172174
) -> tuple[pkcs11.mechanisms.Mechanism, tuple | None]:
173175
if key_type == pkcs11.mechanisms.KeyType.RSA:
174-
return pkcs11.mechanisms.Mechanism.SHA256_RSA_PKCS_PSS, (
176+
return pkcs11.mechanisms.Mechanism.RSA_PKCS_PSS, (
175177
pkcs11.mechanisms.Mechanism.SHA256,
176178
pkcs11.MGF.SHA256,
177179
32,
178180
)
179181
elif key_type == pkcs11.mechanisms.KeyType.EC:
180-
return pkcs11.mechanisms.Mechanism.ECDSA_SHA256, None
182+
return pkcs11.mechanisms.Mechanism.ECDSA, None
181183
else:
182184
log.error("Invalid signing key mechanism.")
183185
sys.exit(1)

0 commit comments

Comments
 (0)