2020Image signing and management.
2121"""
2222
23- from . import version as versmod
24- from .boot_record import create_sw_component_data
25- import click
2623import copy
27- from enum import Enum
28- import array
29- from intelhex import IntelHex
3024import hashlib
31- import array
3225import os .path
3326import re
3427import struct
3528import uuid
29+ from collections import namedtuple
3630from enum import Enum
3731
3832import click
4640from cryptography .hazmat .primitives .serialization import Encoding , PublicFormat
4741from intelhex import IntelHex
4842
49- from . import version as versmod , keys
43+ from . import keys
44+ from . import version as versmod
5045from .boot_record import create_sw_component_data
51- from .keys import rsa , ecdsa , x25519
52-
53- from collections import namedtuple
46+ from .keys import ecdsa , rsa , x25519
5447
5548IMAGE_MAGIC = 0x96f3b83d
5649IMAGE_HEADER_SIZE = 32
@@ -125,7 +118,7 @@ def align_up(num, align):
125118 return (num + (align - 1 )) & ~ (align - 1 )
126119
127120
128- class TLV () :
121+ class TLV :
129122 def __init__ (self , endian , magic = TLV_INFO_MAGIC ):
130123 self .magic = magic
131124 self .buf = bytearray ()
@@ -141,9 +134,8 @@ def add(self, kind, payload):
141134 e = STRUCT_ENDIAN_DICT [self .endian ]
142135 if isinstance (kind , int ):
143136 if not TLV_VENDOR_RES_MIN <= kind <= TLV_VENDOR_RES_MAX :
144- msg = "Invalid custom TLV type value '0x{:04x}', allowed " \
145- "value should be between 0x{:04x} and 0x{:04x}" .format (
146- kind , TLV_VENDOR_RES_MIN , 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} "
147139 raise click .UsageError (msg )
148140 buf = struct .pack (e + 'HH' , kind , len (payload ))
149141 else :
@@ -153,7 +145,7 @@ def add(self, kind, payload):
153145
154146 def get (self ):
155147 if len (self .buf ) == 0 :
156- return bytes ()
148+ return b""
157149 e = STRUCT_ENDIAN_DICT [self .endian ]
158150 header = struct .pack (e + 'HH' , self .magic , len (self ))
159151 return header + bytes (self .buf )
@@ -177,7 +169,7 @@ def get(self):
177169
178170
179171def is_sha_tlv (tlv ):
180- return tlv in TLV_SHA_TO_SHA_AND_ALG . keys ()
172+ return tlv in TLV_SHA_TO_SHA_AND_ALG
181173
182174
183175def tlv_sha_to_sha (tlv ):
@@ -224,8 +216,8 @@ def key_and_user_sha_to_alg_and_tlv(key, user_sha, is_pure = False):
224216 allowed = allowed_key_ssh [type (key )]
225217
226218 except KeyError :
227- raise click .UsageError ("Colud not find allowed hash algorithms for {}"
228- . format ( type ( key )) )
219+ raise click .UsageError (f "Colud not find allowed hash algorithms for { type ( key ) } "
220+ )
229221
230222 # Pure enforces auto, and user selection is ignored
231223 if user_sha == 'auto' or is_pure :
@@ -234,8 +226,8 @@ def key_and_user_sha_to_alg_and_tlv(key, user_sha, is_pure = False):
234226 if user_sha in allowed :
235227 return USER_SHA_TO_ALG_AND_TLV [user_sha ]
236228
237- raise click .UsageError ("Key {} can not be used with --sha {}; allowed sha are one of {}"
238- . format ( key . sig_type (), user_sha , allowed ) )
229+ raise click .UsageError (f "Key { key . sig_type () } can not be used with --sha { user_sha } ; allowed sha are one of { allowed } "
230+ )
239231
240232
241233def get_digest (tlv_type , hash_region ):
@@ -461,9 +453,8 @@ def check_trailer(self):
461453 self .save_enctlv , self .enctlv_len )
462454 padding = self .slot_size - (len (self .payload ) + tsize )
463455 if padding < 0 :
464- msg = "Image size (0x{:x}) + trailer (0x{:x}) exceeds " \
465- "requested size 0x{:x}" .format (
466- len (self .payload ), tsize , self .slot_size )
456+ msg = f"Image size (0x{ len (self .payload ):x} ) + trailer (0x{ tsize :x} ) exceeds " \
457+ f"requested size 0x{ self .slot_size :x} "
467458 raise click .UsageError (msg )
468459
469460 def ecies_hkdf (self , enckey , plainkey , hmac_sha_alg ):
@@ -550,9 +541,8 @@ def create(self, key, public_key_format, enckey, dependencies=None,
550541
551542 if sw_type is not None :
552543 if len (sw_type ) > MAX_SW_TYPE_LENGTH :
553- msg = "'{}' is too long ({} characters) for sw_type. Its " \
554- "maximum allowed length is 12 characters." .format (
555- sw_type , len (sw_type ))
544+ msg = f"'{ sw_type } ' is too long ({ len (sw_type )} characters) for sw_type. Its " \
545+ "maximum allowed length is 12 characters."
556546 raise click .UsageError (msg )
557547
558548 image_version = (str (self .version .major ) + '.'
@@ -853,8 +843,7 @@ def _trailer_size(self, write_size, max_sectors, overwrite_only, enckey,
853843 return self .max_align * 2 + magic_align_size
854844 else :
855845 if write_size not in set ([1 , 2 , 4 , 8 , 16 , 32 ]):
856- raise click .BadParameter ("Invalid alignment: {}" .format (
857- write_size ))
846+ raise click .BadParameter (f"Invalid alignment: { write_size } " )
858847 m = DEFAULT_MAX_SECTORS if max_sectors is None else max_sectors
859848 trailer = m * 3 * write_size # status area
860849 if enckey is not None :
0 commit comments