@@ -188,7 +188,15 @@ def tlv_sha_to_sha(tlv):
188188 keys .X25519 : ['256' , '512' ]
189189}
190190
191- def key_and_user_sha_to_alg_and_tlv (key , user_sha ):
191+ ALLOWED_PURE_KEY_SHA = {
192+ keys .Ed25519 : ['512' ]
193+ }
194+
195+ ALLOWED_PURE_SIG_TLVS = [
196+ TLV_VALUES ['ED25519' ]
197+ ]
198+
199+ def key_and_user_sha_to_alg_and_tlv (key , user_sha , is_pure = False ):
192200 """Matches key and user requested sha to sha alogrithm and TLV name.
193201
194202 The returned tuple will contain hash functions and TVL name.
@@ -202,12 +210,16 @@ def key_and_user_sha_to_alg_and_tlv(key, user_sha):
202210
203211 # If key is not None, then we have to filter hash to only allowed
204212 allowed = None
213+ allowed_key_ssh = ALLOWED_PURE_KEY_SHA if is_pure else ALLOWED_KEY_SHA
205214 try :
206- allowed = ALLOWED_KEY_SHA [type (key )]
215+ allowed = allowed_key_ssh [type (key )]
216+
207217 except KeyError :
208218 raise click .UsageError ("Colud not find allowed hash algorithms for {}"
209219 .format (type (key )))
210- if user_sha == 'auto' :
220+
221+ # Pure enforces auto, and user selection is ignored
222+ if user_sha == 'auto' or is_pure :
211223 return USER_SHA_TO_ALG_AND_TLV [allowed [0 ]]
212224
213225 if user_sha in allowed :
@@ -445,12 +457,13 @@ def ecies_hkdf(self, enckey, plainkey):
445457 def create (self , key , public_key_format , enckey , dependencies = None ,
446458 sw_type = None , custom_tlvs = None , compression_tlvs = None ,
447459 compression_type = None , encrypt_keylen = 128 , clear = False ,
448- fixed_sig = None , pub_key = None , vector_to_sign = None , user_sha = 'auto' ):
460+ fixed_sig = None , pub_key = None , vector_to_sign = None ,
461+ user_sha = 'auto' , is_pure = False ):
449462 self .enckey = enckey
450463
451464 # key decides on sha, then pub_key; of both are none default is used
452465 check_key = key if key is not None else pub_key
453- hash_algorithm , hash_tlv = key_and_user_sha_to_alg_and_tlv (check_key , user_sha )
466+ hash_algorithm , hash_tlv = key_and_user_sha_to_alg_and_tlv (check_key , user_sha , is_pure )
454467
455468 # Calculate the hash of the public key
456469 if key is not None :
@@ -590,9 +603,16 @@ def create(self, key, public_key_format, enckey, dependencies=None,
590603 sha = hash_algorithm ()
591604 sha .update (self .payload )
592605 digest = sha .digest ()
593- message = digest ;
594606 tlv .add (hash_tlv , digest )
595- self .image_hash = digest
607+ # Unless pure, we are signing digest.
608+ message = digest
609+
610+ if is_pure :
611+ # Note that when Pure signature is used, hash TLV is not present.
612+ message = bytes (self .payload )
613+ e = STRUCT_ENDIAN_DICT [self .endian ]
614+ sig_pure = struct .pack (e + '?' , True )
615+ tlv .add ('SIG_PURE' , sig_pure )
596616
597617 if vector_to_sign == 'payload' :
598618 # Stop amending data to the image
@@ -784,7 +804,7 @@ def verify(imgfile, key):
784804 version = struct .unpack ('BBHI' , b [20 :28 ])
785805
786806 if magic != IMAGE_MAGIC :
787- return VerifyResult .INVALID_MAGIC , None , None
807+ return VerifyResult .INVALID_MAGIC , None , None , None
788808
789809 tlv_off = header_size + img_size
790810 tlv_info = b [tlv_off :tlv_off + TLV_INFO_SIZE ]
@@ -795,27 +815,43 @@ def verify(imgfile, key):
795815 magic , tlv_tot = struct .unpack ('HH' , tlv_info )
796816
797817 if magic != TLV_INFO_MAGIC :
798- return VerifyResult .INVALID_TLV_INFO_MAGIC , None , None
818+ return VerifyResult .INVALID_TLV_INFO_MAGIC , None , None , None
819+
820+ # This is set by existence of TLV SIG_PURE
821+ is_pure = False
799822
800823 prot_tlv_size = tlv_off
801824 hash_region = b [:prot_tlv_size ]
825+ tlv_end = tlv_off + tlv_tot
826+ tlv_off += TLV_INFO_SIZE # skip tlv info
827+
828+ # First scan all TLVs in search of SIG_PURE
829+ while tlv_off < tlv_end :
830+ tlv = b [tlv_off :tlv_off + TLV_SIZE ]
831+ tlv_type , _ , tlv_len = struct .unpack ('BBH' , tlv )
832+ if tlv_type == TLV_VALUES ['SIG_PURE' ]:
833+ is_pure = True
834+ break
835+ tlv_off += TLV_SIZE + tlv_len
836+
802837 digest = None
838+ tlv_off = header_size + img_size
803839 tlv_end = tlv_off + tlv_tot
804840 tlv_off += TLV_INFO_SIZE # skip tlv info
805841 while tlv_off < tlv_end :
806842 tlv = b [tlv_off :tlv_off + TLV_SIZE ]
807843 tlv_type , _ , tlv_len = struct .unpack ('BBH' , tlv )
808844 if is_sha_tlv (tlv_type ):
809845 if not tlv_matches_key_type (tlv_type , key ):
810- return VerifyResult .KEY_MISMATCH , None , None
846+ return VerifyResult .KEY_MISMATCH , None , None , None
811847 off = tlv_off + TLV_SIZE
812848 digest = get_digest (tlv_type , hash_region )
813849 if digest == b [off :off + tlv_len ]:
814850 if key is None :
815- return VerifyResult .OK , version , digest
851+ return VerifyResult .OK , version , digest , None
816852 else :
817- return VerifyResult .INVALID_HASH , None , None
818- elif key is not None and tlv_type == TLV_VALUES [key .sig_tlv ()]:
853+ return VerifyResult .INVALID_HASH , None , None , None
854+ elif not is_pure and key is not None and tlv_type == TLV_VALUES [key .sig_tlv ()]:
819855 off = tlv_off + TLV_SIZE
820856 tlv_sig = b [off :off + tlv_len ]
821857 payload = b [:prot_tlv_size ]
@@ -824,9 +860,18 @@ def verify(imgfile, key):
824860 key .verify (tlv_sig , payload )
825861 else :
826862 key .verify_digest (tlv_sig , digest )
827- return VerifyResult .OK , version , digest
863+ return VerifyResult .OK , version , digest , None
864+ except InvalidSignature :
865+ # continue to next TLV
866+ pass
867+ elif is_pure and key is not None and tlv_type in ALLOWED_PURE_SIG_TLVS :
868+ off = tlv_off + TLV_SIZE
869+ tlv_sig = b [off :off + tlv_len ]
870+ try :
871+ key .verify_digest (tlv_sig , hash_region )
872+ return VerifyResult .OK , version , None , tlv_sig
828873 except InvalidSignature :
829874 # continue to next TLV
830875 pass
831876 tlv_off += TLV_SIZE + tlv_len
832- return VerifyResult .INVALID_SIGNATURE , None , None
877+ return VerifyResult .INVALID_SIGNATURE , None , None , None
0 commit comments