103103TLV_VENDOR_RES_MIN = 0x00a0
104104TLV_VENDOR_RES_MAX = 0xfffe
105105
106- STRUCT_ENDIAN_DICT = {
107- 'little' : '<' ,
108- 'big' : '>'
109- }
106+ STRUCT_ENDIAN_DICT = {'little' : '<' , 'big' : '>' }
110107
111- VerifyResult = Enum ('VerifyResult' ,
112- ['OK' , 'INVALID_MAGIC' , 'INVALID_TLV_INFO_MAGIC' , 'INVALID_HASH' , 'INVALID_SIGNATURE' ,
113- 'KEY_MISMATCH' ])
108+ VerifyResult = Enum (
109+ 'VerifyResult' ,
110+ [
111+ 'OK' ,
112+ 'INVALID_MAGIC' ,
113+ 'INVALID_TLV_INFO_MAGIC' ,
114+ 'INVALID_HASH' ,
115+ 'INVALID_SIGNATURE' ,
116+ 'KEY_MISMATCH' ,
117+ ],
118+ )
114119
115120
116121def align_up (num , align ):
@@ -134,8 +139,11 @@ def add(self, kind, payload):
134139 e = STRUCT_ENDIAN_DICT [self .endian ]
135140 if isinstance (kind , int ):
136141 if not TLV_VENDOR_RES_MIN <= kind <= TLV_VENDOR_RES_MAX :
137- msg = f"Invalid custom TLV type value '0x{ kind :04x} ', allowed " \
138- f"value should be between 0x{ TLV_VENDOR_RES_MIN :04x} and 0x{ TLV_VENDOR_RES_MAX :04x} "
142+ msg = (
143+ f"Invalid custom TLV type value '0x{ kind :04x} ', allowed "
144+ f"value should be between 0x{ TLV_VENDOR_RES_MIN :04x} and "
145+ "0x{TLV_VENDOR_RES_MAX:04x}"
146+ )
139147 raise click .UsageError (msg )
140148 buf = struct .pack (e + 'HH' , kind , len (payload ))
141149 else :
@@ -216,8 +224,8 @@ def key_and_user_sha_to_alg_and_tlv(key, user_sha, is_pure = False):
216224 allowed = allowed_key_ssh [type (key )]
217225
218226 except KeyError :
219- raise click .UsageError (f"Colud not find allowed hash algorithms for { type ( key ) } "
220- )
227+ raise click .UsageError (
228+ f"Could not find allowed hash algorithms for { type ( key ) } " ) from None
221229
222230 # Pure enforces auto, and user selection is ignored
223231 if user_sha == 'auto' or is_pure :
@@ -226,8 +234,10 @@ def key_and_user_sha_to_alg_and_tlv(key, user_sha, is_pure = False):
226234 if user_sha in allowed :
227235 return USER_SHA_TO_ALG_AND_TLV [user_sha ]
228236
229- raise click .UsageError (f"Key { key .sig_type ()} can not be used with --sha { user_sha } ; allowed sha are one of { allowed } "
230- )
237+ raise click .UsageError (
238+ f"Key { key .sig_type ()} can not be used with --sha { user_sha } ; "
239+ "allowed sha are one of {allowed}"
240+ ) from None
231241
232242
233243def get_digest (tlv_type , hash_region ):
@@ -245,11 +255,12 @@ def tlv_matches_key_type(tlv_type, key):
245255 # return True, on exception we return False.
246256 _ , _ = key_and_user_sha_to_alg_and_tlv (key , tlv_sha_to_sha (tlv_type ))
247257 return True
248- except :
258+ except Exception :
249259 pass
250260
251261 return False
252262
263+
253264def parse_uuid (namespace , value ):
254265 # Check if UUID is in the RAW format (12345678-1234-5678-1234-567812345678)
255266 uuid_re = r'[0-9A-f]{8}-[0-9A-f]{4}-[0-9A-f]{4}-[0-9A-f]{4}-[0-9A-f]{12}'
@@ -371,7 +382,7 @@ def load(self, path):
371382 self .infile_data = f .read ()
372383 self .payload = copy .copy (self .infile_data )
373384 except FileNotFoundError :
374- raise click .UsageError ("Input file not found" )
385+ raise click .UsageError ("Input file not found" ) from None
375386
376387 # Add the image header if needed.
377388 if self .pad_header and self .header_size > 0 :
@@ -444,10 +455,13 @@ def save(self, path, hex_addr=None):
444455 f .write (self .payload )
445456
446457 def check_header (self ):
447- if self .header_size > 0 and not self .pad_header :
448- if any (v != 0 for v in self .payload [0 :self .header_size ]):
449- raise click .UsageError ("Header padding was not requested and "
450- "image does not start with zeros" )
458+ if (
459+ self .header_size > 0 and not self .pad_header
460+ and any (v != 0 for v in self .payload [0 : self .header_size ])
461+ ):
462+ raise click .UsageError (
463+ "Header padding was not requested and image does not start with zeros"
464+ )
451465
452466 def check_trailer (self ):
453467 if self .slot_size > 0 :
@@ -719,7 +733,9 @@ def create(self, key, public_key_format, enckey, dependencies=None,
719733 tlv .add (pub_key .sig_tlv (), fixed_sig ['value' ])
720734 self .signature = fixed_sig ['value' ]
721735 else :
722- raise click .UsageError ("Can not sign using key and provide fixed-signature at the same time" )
736+ raise click .UsageError (
737+ "Can not sign using key and provide fixed-signature at the same time"
738+ )
723739
724740 # At this point the image was hashed + signed, we can remove the
725741 # protected TLVs from the payload (will be re-added later)
@@ -738,7 +754,8 @@ def create(self, key, public_key_format, enckey, dependencies=None,
738754 hmac_sha_alg = hashes .SHA256 ()
739755 elif hmac_sha == '512' :
740756 if not isinstance (enckey , x25519 .X25519Public ):
741- raise click .UsageError ("Currently only ECIES-X25519 supports HMAC-SHA512" )
757+ raise click .UsageError (
758+ "Currently only ECIES-X25519 supports HMAC-SHA512" )
742759 hmac_sha_alg = hashes .SHA512 ()
743760 else :
744761 raise click .UsageError ("Unsupported HMAC-SHA" )
@@ -886,7 +903,7 @@ def verify(imgfile, key):
886903 with open (imgfile , 'rb' ) as f :
887904 b = f .read ()
888905 except FileNotFoundError :
889- raise click .UsageError (f"Image file { imgfile } not found" )
906+ raise click .UsageError (f"Image file { imgfile } not found" ) from None
890907
891908 magic , _ , header_size , _ , img_size = struct .unpack ('IIHHI' , b [:16 ])
892909 version = struct .unpack ('BBHI' , b [20 :28 ])
0 commit comments