blob: d3a5a25800610f3fcd2d70eaa8292680a17aca10 [file] [log] [blame] [view]
David Brown8f057ca2019-12-12 16:19:55 -07001# ECDSA signature format
2
Francesco Domenico Servidio58511502022-01-11 17:07:13 +01003When the ECDSA SECP256R1 (EC256) signature support was added to MCUboot, a
David Brown8f057ca2019-12-12 16:19:55 -07004shortcut was taken, and these signatures were padded to make them
Francesco Domenico Servidio58511502022-01-11 17:07:13 +01005always a fixed length. Unfortunately, this padding was done in a way
6that is not easily reversible. Some crypto libraries (specifically, Mbed
7TLS) are fairly strict about the formatting of the ECDSA signature.
David Brown8f057ca2019-12-12 16:19:55 -07008
Francesco Domenico Servidio58511502022-01-11 17:07:13 +01009There are two ways to fix this:
David Brown8f057ca2019-12-12 16:19:55 -070010
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010011 - Use a reversible padding scheme. This solution requires
12 at least one pad byte to always be added (to set the length). This
13 padding would be somewhat incompatible across versions (older
14 EC256 would work, while newer MCUboot code would reject old
15 signatures. The EC code would work reliably only in the new
16 combination).
David Brown8f057ca2019-12-12 16:19:55 -070017
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010018 - Remove the padding entirely. Depending on the tool used, this solution
19 requires some rethinking of how TLV generation is implemented so
20 that the length does not need to be known until the signature is
21 generated. These tools are usually written in higher-level
22 languages, so this change should not be difficult.
David Brown8f057ca2019-12-12 16:19:55 -070023
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010024 However, this will also break compatibility with older versions,
25 because images generated with newer tools will not
26 work with older versions of MCUboot.
David Brown8f057ca2019-12-12 16:19:55 -070027
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010028This document proposes a multi-stage approach to give a transition
29period:
David Brown8f057ca2019-12-12 16:19:55 -070030
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010031 1. Add a `--no-pad-sig` argument to the sign command in
32 `imgtool.py`.
David Brown8f057ca2019-12-12 16:19:55 -070033
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010034 Without this argument, the images are padded with the
35 existing scheme. With this argument, the ECDSA is encoded
36 without any padding. The `--pad-sig` argument is also
37 accepted, but it is already the default.
David Brown8f057ca2019-12-12 16:19:55 -070038
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010039 2. MCUboot will be modified to allow unpadded signatures right away.
40 The existing EC256 implementations will still work (with or
41 without padding), and the existing EC implementation will be able
42 to accept padded and unpadded signatures.
David Brown8f057ca2019-12-12 16:19:55 -070043
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010044 3. An Mbed TLS implementation of EC256 can be added, but it will require
45 the `--no-pad-sig` signature to be able to boot all generated
46 images. Without the argument, 3 out of 4 images generated will have
47 padding and will be considered invalid.
48
49After one or more MCUboot release cycles and announcements in the
David Brownbf3a3a92019-12-17 16:08:33 -070050relevant channels, the arguments to `imgtool.py` will change:
David Brown8f057ca2019-12-12 16:19:55 -070051
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010052 - `--no-pad-sig` will still be accepted but will have no effect.
David Brown8f057ca2019-12-12 16:19:55 -070053
David Brown3639aca2019-12-17 16:10:49 -070054 - `--pad-sig` will now bring back the old padding behavior.
David Brown8f057ca2019-12-12 16:19:55 -070055
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010056This will require an update to any scripts that will rely on the default
57behavior, but will not specify a specific version of imgtool.
David Brown8f057ca2019-12-12 16:19:55 -070058
59The signature generation in the simulator can be changed at the same
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010060time the boot code begins to accept unpadded signatures. The simulator is
Francesco Servidio4ff0c182021-10-20 15:27:16 +020061always run out of the same tree as the MCUboot code, so there should
David Brown8f057ca2019-12-12 16:19:55 -070062not be any compatibility issues.
63
64## Background
65
66ECDSA signatures are encoded as ASN.1, notably with the signature
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010067itself encoded as follows:
David Brown8f057ca2019-12-12 16:19:55 -070068
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010069```
David Brown8f057ca2019-12-12 16:19:55 -070070 ECDSA-Sig-Value ::= SEQUENCE {
71 r INTEGER,
72 s INTEGER
73 }
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010074```
David Brown8f057ca2019-12-12 16:19:55 -070075
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010076Both `r` and `s` are 256-bit numbers. Because these are
David Brown8f057ca2019-12-12 16:19:55 -070077unsigned numbers that are being encoded in ASN.1 as signed values, if
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010078the high bit of the number is set, the DER-encoded representation will
79require 33 bytes instead of 32. This means that the length of the
80signature will vary by a couple of bytes, depending on whether one or
81both of these numbers have the high bit set.
David Brown8f057ca2019-12-12 16:19:55 -070082
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010083Originally, MCUboot added padding to the entire signature and just
84removed any trailing 0 bytes from the data block. This turned out to be fine 255 out of 256
85times, each time the last byte of the signature was non-zero, but if the
86signature ended in a zero, MCUboot would remove too many bytes and render the
87signature invalid.
David Brown8f057ca2019-12-12 16:19:55 -070088
Francesco Domenico Servidio58511502022-01-11 17:07:13 +010089The correct approach here is to accept that ECDSA signatures are of
90variable length, and to make sure that we can handle them as such.