imgtool: Remove default padding of ECDSA sigs

Since commit:

    commit a36082664e
    Author: David Brown <david.brown@linaro.org>
    Date:   Thu Dec 12 15:35:31 2019 -0700

        ecdsa: Allow ECDSA signatures to be actual length

MCUboot no longer requires ECDSA signatures to be padded to a fixed
length.  This change makes imgtool, by default, also no longer add this
padding to images.  There is an option `--pad-sig` that can be given to
the sign command to re-instate this padding.  This flag will be needed
to make images that will work with older (pre 1.5.0) versions of
MCUboot.

Signed-off-by: David Brown <david.brown@linaro.org>
This commit is contained in:
David Brown 2020-03-10 16:23:56 -06:00 committed by David Brown
parent a4c586421a
commit 4878c27c82
2 changed files with 26 additions and 10 deletions

View File

@ -52,12 +52,17 @@ class ECDSA256P1Public(KeyClass):
return "ECDSA256"
def sig_len(self):
# The DER encoding depends on the high bit, and can be
# anywhere from 70 to 72 bytes. Because we have to fill in
# the length field before computing the signature, however,
# we'll give the largest, and the sig checking code will allow
# for it to be up to two bytes larger than the actual
# signature.
# Early versions of MCUboot (< v1.5.0) required ECDSA
# signatures to be padded to 72 bytes. Because the DER
# encoding is done with signed integers, the size of the
# signature will vary depending on whether the high bit is set
# in each value. This padding was done in a
# not-easily-reversible way (by just adding zeros).
#
# The signing code no longer requires this padding, and newer
# versions of MCUboot don't require it. But, continue to
# return the total length so that the padding can be done if
# requested.
return 72
def verify(self, signature, payload):
@ -78,6 +83,7 @@ class ECDSA256P1(ECDSA256P1Public):
def __init__(self, key):
"""key should be an instance of EllipticCurvePrivateKey"""
self.key = key
self.pad_sig = False
@staticmethod
def generate():
@ -142,7 +148,10 @@ class ECDSA256P1(ECDSA256P1Public):
signature_algorithm=ec.ECDSA(SHA256()))
def sign(self, payload):
# To make fixed length, pad with one or two zeros.
sig = self.raw_sign(payload)
sig += b'\000' * (self.sig_len() - len(sig))
return sig
if self.pad_sig:
# To make fixed length, pad with one or two zeros.
sig += b'\000' * (self.sig_len() - len(sig))
return sig
else:
return sig

View File

@ -244,6 +244,9 @@ class BasedIntParamType(click.ParamType):
'image')
@click.option('-H', '--header-size', callback=validate_header_size,
type=BasedIntParamType(), required=True)
@click.option('--pad-sig', default=False, is_flag=True,
help='Add 0-2 bytes of padding to ECDSA signature '
'(for mcuboot <1.5)')
@click.option('-d', '--dependencies', callback=get_dependencies,
required=False, help='''Add dependence on another image, format:
"(<image_ID>,<image_version>), ... "''')
@ -257,7 +260,7 @@ class BasedIntParamType(click.ParamType):
@click.command(help='''Create a signed or unsigned image\n
INFILE and OUTFILE are parsed as Intel HEX if the params have
.hex extension, otherwise binary format is used''')
def sign(key, align, version, header_size, pad_header, slot_size, pad, confirm,
def sign(key, align, version, pad_sig, header_size, pad_header, slot_size, pad, confirm,
max_sectors, overwrite_only, endian, encrypt, infile, outfile,
dependencies, load_addr, hex_addr, erased_val, save_enctlv,
security_counter):
@ -279,6 +282,10 @@ def sign(key, align, version, header_size, pad_header, slot_size, pad, confirm,
# FIXME
raise click.UsageError("Signing and encryption must use the same "
"type of key")
if pad_sig and hasattr(key, 'pad_sig'):
key.pad_sig = True
img.create(key, enckey, dependencies)
img.save(outfile, hex_addr)