Commit b408b43a authored by David Brown's avatar David Brown Committed by David Brown
Browse files

sim: Reduce most false failures due to ECDSA size change

The encoding of ECDSA signatures is variable sized.  Better support this
by allowing the TLV size estimate to be off by as much as 6, but only in
the case of ECDSA signature.

Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent c8d62018
Loading
Loading
Loading
Loading
+5 −0
Changes for sim/src/caps.rs: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -36,6 +36,11 @@ impl Caps {
        (caps as u32) & (self as u32) != 0
    }

    /// Does this build have ECDSA of some type enabled for signatures.
    pub fn has_ecdsa() -> bool {
        Caps::EcdsaP256.present() || Caps::EcdsaP224.present()
    }

    /// Query for the number of images that have been configured into this
    /// MCUboot build.
    pub fn get_num_images() -> usize {
+13 −2
Changes for sim/src/tlv.rs: 13 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -727,10 +727,21 @@ impl ManifestGen for TlvGen {
        let mut size_buf = &mut result[npro_pos + 2 .. npro_pos + 4];
        size_buf.write_u16::<LittleEndian>(size).unwrap();

        // The size estimate must not be less than the given size, and no more than 3 bytes larger.
        if size_estimate < result.len() || size_estimate > result.len() + 3 {
        // ECDSA is stored as an ASN.1 integer.  For a 128-bit value, this maximally results in 33
        // bytes of storage for each of the two values.  If the high bit is zero, it will take 32
        // bytes, if the top 8 bits are zero, it will take 31 bits, and so on.  The smaller size
        // will occur with decreasing likelihood.  We'll allow this to get a bit smaller, hopefully
        // allowing the tests to pass with false failures rare.  For this case, we'll handle up to
        // the top 16 bits of both numbers being all zeros (1 in 2^32).
        if !Caps::has_ecdsa() {
            if size_estimate != result.len() {
                panic!("Incorrect size estimate: {} (actual {})", size_estimate, result.len());
            }
        } else {
            if size_estimate < result.len() || size_estimate > result.len() + 6 {
                panic!("Incorrect size estimate: {} (actual {})", size_estimate, result.len());
            }
        }
        if size_estimate != result.len() {
            log::warn!("Size off: {} actual {}", size_estimate, result.len());
        }