44
55import binascii
66import configparser
7+ import hashlib
78import os
89import sys
910from 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