blob: 410d51910dfd60e6493f82388c3e16acbd716426 [file] [log] [blame] [view]
David Brown8f057ca2019-12-12 16:19:55 -07001# ECDSA signature format
2
3When ECDSA SECP256R1 (EC256) signature support was added to MCUboot, a
4shortcut was taken, and these signatures were padded to make them
5always a fixed length. Unfortunately, this padding was done in a way
6that is not easily reversible. Some crypto libraries are fairly
7strict about the formatting of the ECDSA signature (specifically, mbed
8TLS). This currently means that the ECDSA SECP224R1 (EC) signature
9checking code will fail to boot about 1 out of every 256 images,
10because the signature itself will end in a 0x00 byte, and the code
11will remove too much data, invalidating the signature.
12
13There are a couple of ways to fix this:
14
15 1. Use a reversible padding scheme. This will work, but requires
16 at least one pad byte always be added (to set the length). This
17 padding would be somewhat incompatible across versions (older
18 EC256 would work, newer mcuboot code would reject old
19 signatures. EC code would only reliably work in the new
20 combination).
21
22 2. Remove the padding entirely. Depending on which tool, this will
23 require some rethinking of how TLV generation is implemented so
24 that the length does not need to be known until the signature is
25 generated. These tools are all written in higher-level
26 languages and this change should not be difficult.
27
28 However, this will also break compatibility with older version,
29 significantly in that images generated with newer tools will not
30 work with older versions of MCUboot.
31
32This document proposes a multi-stage approach, to give a transition
33period.
34
35 - First, add a `--valid-ecdsa` argument to the sign command in
36 `imgtool.py`. Without this, the images will be padded with the
37 existing scheme, and with the argument, the ecdsa will be encoded
38 without any padding.
39
40 - MCUboot will be modified to allow unpadded signatures right away.
41 The existing EC256 implementations will still work (with or
42 without padding), and the existing EC implementation will begin
43 accepting all signatures.
44
45 - An mbed TLS implementation of EC256 can be added, but will require
46 the `--valid-ecdsa` signature to be able to boot all generated
47 images (without the argument 3 of out 4 images generated will have
48 padding, and be considered invalid).
49
50After one or more MCUboot release cycles, and announcements over
51relevant channels, the arguments to mcuboot will change:
52
53 - `--valid-ecdsa` will still be accepted, but have no effect.
54
55 - `--invalid-ecdsa` will now bring back the old padding behavior.
56
57This will require a change to any scripts that are relying on this
58behavior, but not specifying a specific version of imgtool.
59
60The signature generation in the simulator can be changed at the same
61time the boot code begins to accept unpadded signatures. The sim is
62always run out of the same tree as the mcuboot code, so there should
63not be any compatibility issues.
64
65## Background
66
67ECDSA signatures are encoded as ASN.1, notably with the signature
68itself being encoded as:
69
70 ECDSA-Sig-Value ::= SEQUENCE {
71 r INTEGER,
72 s INTEGER
73 }
74
75where both `r` and `s` are 256-bit numbers. Because these are
76unsigned numbers that are being encoded in ASN.1 as signed values, if
77the high bit of the number is set, the DER encoded representation will
78require 33 bytes instead of 32. This means that the length of the
79signature will vary by a couple of bytes, depending on whether on of
80both of these numbers has the high bit set.
81
82Originally, MCUboot added padding to the entire signature, and just
83removed 0 bytes from the data block. This would be fine 255/256
84times, when the last byte of the signature was non-zero, but if the
85signature ended in a zero, it would remove too many bytes, and the
86signature would be considered invalid.
87
88The correct approach here is to accept that ECDSA signatures are
89variable length, and make sure that we can handle them as such.