Skip to content

Commit 5857311

Browse files
committed
[nrf fromlist] imgtool: Add pure signature support
Adds PureEdDSA signature support. The change includes implementation of SIG_PURE TLV that, when present, indicates the signature that is present is Pure type. Upstream PR: mcu-tools/mcuboot#2063 Signed-off-by: Dominik Ermel <[email protected]>
1 parent 70df515 commit 5857311

File tree

2 files changed

+86
-24
lines changed

2 files changed

+86
-24
lines changed

scripts/imgtool/image.py

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,15 @@ def tlv_sha_to_sha(tlv):
187187
keys.X25519 : ['256', '512']
188188
}
189189

190-
def key_and_user_sha_to_alg_and_tlv(key, user_sha):
190+
ALLOWED_PURE_KEY_SHA = {
191+
keys.Ed25519 : ['512']
192+
}
193+
194+
ALLOWED_PURE_SIG_TLVS = [
195+
TLV_VALUES['ED25519']
196+
]
197+
198+
def key_and_user_sha_to_alg_and_tlv(key, user_sha, is_pure = False):
191199
"""Matches key and user requested sha to sha alogrithm and TLV name.
192200
193201
The returned tuple will contain hash functions and TVL name.
@@ -202,11 +210,17 @@ def key_and_user_sha_to_alg_and_tlv(key, user_sha):
202210
# If key is not None, then we have to filter hash to only allowed
203211
allowed = None
204212
try:
205-
allowed = ALLOWED_KEY_SHA[type(key)]
213+
if is_pure:
214+
allowed = ALLOWED_PURE_KEY_SHA[type(key)]
215+
else:
216+
allowed = ALLOWED_KEY_SHA[type(key)]
217+
206218
except KeyError:
207219
raise click.UsageError("Colud not find allowed hash algorithms for {}"
208220
.format(type(key)))
209-
if user_sha == 'auto':
221+
222+
# Pure enforces auto, and user selection is ignored
223+
if user_sha == 'auto' or is_pure:
210224
return USER_SHA_TO_ALG_AND_TLV[allowed[0]]
211225

212226
if user_sha in allowed:
@@ -444,12 +458,13 @@ def ecies_hkdf(self, enckey, plainkey):
444458
def create(self, key, public_key_format, enckey, dependencies=None,
445459
sw_type=None, custom_tlvs=None, compression_tlvs=None,
446460
compression_type=None, encrypt_keylen=128, clear=False,
447-
fixed_sig=None, pub_key=None, vector_to_sign=None, user_sha='auto'):
461+
fixed_sig=None, pub_key=None, vector_to_sign=None,
462+
user_sha='auto', is_pure=False):
448463
self.enckey = enckey
449464

450465
# key decides on sha, then pub_key; of both are none default is used
451466
check_key = key if key is not None else pub_key
452-
hash_algorithm, hash_tlv = key_and_user_sha_to_alg_and_tlv(check_key, user_sha)
467+
hash_algorithm, hash_tlv = key_and_user_sha_to_alg_and_tlv(check_key, user_sha, is_pure)
453468

454469
# Calculate the hash of the public key
455470
if key is not None:
@@ -584,12 +599,18 @@ def create(self, key, public_key_format, enckey, dependencies=None,
584599
# EC signatures so called Pure algorithm, designated to be run
585600
# over entire message is used with sha of image as message,
586601
# so, for example, in case of ED25519 we have here SHAxxx-ED25519-SHA512.
587-
sha = hash_algorithm()
588-
sha.update(self.payload)
589-
digest = sha.digest()
590-
message = digest;
591-
tlv.add(hash_tlv, digest)
592-
self.image_hash = digest
602+
if not is_pure:
603+
sha = hash_algorithm()
604+
sha.update(self.payload)
605+
digest = sha.digest()
606+
message = digest
607+
tlv.add(hash_tlv, digest)
608+
else:
609+
# Note that when Pure signature is used, there is not hash encoded
610+
message = bytes(self.payload)
611+
e = STRUCT_ENDIAN_DICT[self.endian]
612+
sig_pure = struct.pack(e + '?', True)
613+
tlv.add('SIG_PURE', sig_pure)
593614

594615
if vector_to_sign == 'payload':
595616
# Stop amending data to the image
@@ -781,7 +802,7 @@ def verify(imgfile, key):
781802
version = struct.unpack('BBHI', b[20:28])
782803

783804
if magic != IMAGE_MAGIC:
784-
return VerifyResult.INVALID_MAGIC, None, None
805+
return VerifyResult.INVALID_MAGIC, None, None, None
785806

786807
tlv_off = header_size + img_size
787808
tlv_info = b[tlv_off:tlv_off + TLV_INFO_SIZE]
@@ -792,27 +813,46 @@ def verify(imgfile, key):
792813
magic, tlv_tot = struct.unpack('HH', tlv_info)
793814

794815
if magic != TLV_INFO_MAGIC:
795-
return VerifyResult.INVALID_TLV_INFO_MAGIC, None, None
816+
return VerifyResult.INVALID_TLV_INFO_MAGIC, None, None, None
817+
818+
# This is set by existence of TLV SIG_PURE
819+
is_pure = False
796820

797821
prot_tlv_size = tlv_off
798822
hash_region = b[:prot_tlv_size]
823+
tlv_end = tlv_off + tlv_tot
824+
tlv_off += TLV_INFO_SIZE # skip tlv info
825+
826+
# First scan all TLVs in search of SIG_PURE
827+
while tlv_off < tlv_end:
828+
tlv = b[tlv_off:tlv_off + TLV_SIZE]
829+
tlv_type, _, tlv_len = struct.unpack('BBH', tlv)
830+
if tlv_type == TLV_VALUES['SIG_PURE']:
831+
is_pure = True
832+
break
833+
tlv_off += TLV_SIZE + tlv_len
834+
799835
digest = None
836+
tlv_off = header_size + img_size
800837
tlv_end = tlv_off + tlv_tot
801838
tlv_off += TLV_INFO_SIZE # skip tlv info
802839
while tlv_off < tlv_end:
803840
tlv = b[tlv_off:tlv_off + TLV_SIZE]
804841
tlv_type, _, tlv_len = struct.unpack('BBH', tlv)
805842
if is_sha_tlv(tlv_type):
843+
if is_pure:
844+
print("SHA is not expected for signature over image (pure), ignoring")
845+
continue
806846
if not tlv_matches_key_type(tlv_type, key):
807-
return VerifyResult.KEY_MISMATCH, None, None
847+
return VerifyResult.KEY_MISMATCH, None, None, None
808848
off = tlv_off + TLV_SIZE
809849
digest = get_digest(tlv_type, hash_region)
810850
if digest == b[off:off + tlv_len]:
811851
if key is None:
812-
return VerifyResult.OK, version, digest
852+
return VerifyResult.OK, version, digest, None
813853
else:
814-
return VerifyResult.INVALID_HASH, None, None
815-
elif key is not None and tlv_type == TLV_VALUES[key.sig_tlv()]:
854+
return VerifyResult.INVALID_HASH, None, None, None
855+
elif not is_pure and key is not None and tlv_type == TLV_VALUES[key.sig_tlv()]:
816856
off = tlv_off + TLV_SIZE
817857
tlv_sig = b[off:off + tlv_len]
818858
payload = b[:prot_tlv_size]
@@ -821,9 +861,18 @@ def verify(imgfile, key):
821861
key.verify(tlv_sig, payload)
822862
else:
823863
key.verify_digest(tlv_sig, digest)
824-
return VerifyResult.OK, version, digest
864+
return VerifyResult.OK, version, digest, None
865+
except InvalidSignature:
866+
# continue to next TLV
867+
pass
868+
elif is_pure and key is not None and tlv_type in ALLOWED_PURE_SIG_TLVS:
869+
off = tlv_off + TLV_SIZE
870+
tlv_sig = b[off:off + tlv_len]
871+
try:
872+
key.verify_digest(tlv_sig, hash_region)
873+
return VerifyResult.OK, version, None, tlv_sig
825874
except InvalidSignature:
826875
# continue to next TLV
827876
pass
828877
tlv_off += TLV_SIZE + tlv_len
829-
return VerifyResult.INVALID_SIGNATURE, None, None
878+
return VerifyResult.INVALID_SIGNATURE, None, None, None

scripts/imgtool/main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,14 @@ def getpriv(key, minimal, format):
226226
@click.command(help="Check that signed image can be verified by given key")
227227
def verify(key, imgfile):
228228
key = load_key(key) if key else None
229-
ret, version, digest = image.Image.verify(imgfile, key)
229+
ret, version, digest, signature = image.Image.verify(imgfile, key)
230230
if ret == image.VerifyResult.OK:
231231
print("Image was correctly validated")
232232
print("Image version: {}.{}.{}+{}".format(*version))
233-
print("Image digest: {}".format(digest.hex()))
233+
if digest:
234+
print("Image digest: {}".format(digest.hex()))
235+
if signature and digest is None:
236+
print("Image signature over image: {}".format(signature.hex()))
234237
return
235238
elif ret == image.VerifyResult.INVALID_MAGIC:
236239
print("Invalid image magic; is this an MCUboot image?")
@@ -423,6 +426,10 @@ def convert(self, value, param, ctx):
423426
'the signature calculated using the public key')
424427
@click.option('--fix-sig-pubkey', metavar='filename',
425428
help='public key relevant to fixed signature')
429+
@click.option('--pure', 'is_pure', is_flag=True, default=False, show_default=True,
430+
help='Expected Pure variant of signature; the Pure variant is '
431+
'expected to be signature done over an image rather than hash of '
432+
'that image.')
426433
@click.option('--sig-out', metavar='filename',
427434
help='Path to the file to which signature will be written. '
428435
'The image signature will be encoded as base64 formatted string')
@@ -441,8 +448,8 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
441448
endian, encrypt_keylen, encrypt, compression, infile, outfile,
442449
dependencies, load_addr, hex_addr, erased_val, save_enctlv,
443450
security_counter, boot_record, custom_tlv, rom_fixed, max_align,
444-
clear, fix_sig, fix_sig_pubkey, sig_out, user_sha, vector_to_sign,
445-
non_bootable):
451+
clear, fix_sig, fix_sig_pubkey, sig_out, user_sha, is_pure,
452+
vector_to_sign, non_bootable):
446453

447454
if confirm:
448455
# Confirmed but non-padded images don't make much sense, because
@@ -509,9 +516,15 @@ def sign(key, public_key_format, align, version, pad_sig, header_size,
509516
'value': raw_signature
510517
}
511518

519+
if is_pure and user_sha != 'auto':
520+
raise click.UsageError(
521+
'Pure signatures, currently, enforces preferred hash algorithm, '
522+
'and forbids sha selection by user.')
523+
512524
img.create(key, public_key_format, enckey, dependencies, boot_record,
513525
custom_tlvs, compression_tlvs, int(encrypt_keylen), clear,
514-
baked_signature, pub_key, vector_to_sign, user_sha)
526+
baked_signature, pub_key, vector_to_sign, user_sha=user_sha,
527+
is_pure=is_pure)
515528

516529
if compression == "lzma2":
517530
compressed_img = image.Image(version=decode_version(version),

0 commit comments

Comments
 (0)