Commit 4878c27c authored by David Brown's avatar David Brown Committed by David Brown
Browse files

imgtool: Remove default padding of ECDSA sigs

Since commit:

    commit a3608266
    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: default avatarDavid Brown <david.brown@linaro.org>
parent a4c58642
Loading
Loading
Loading
Loading
+16 −7
Changes for scripts/imgtool/keys/ecdsa.py: 16 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -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)
        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
+8 −1
Changes for scripts/imgtool/main.py: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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)