sim: Ensure the estimated TLV area size is always equal to the actual size #2215
+55
−35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To generate largest possible and oversized images, the simulator needs to determine first the maximum size of an image. When signing the images using ECDSA, the actual size of the generated image was not exactly always the desired size. Indeed, in that case, the size of the TLV area estimated during the computation of the maximum image size might not be always equal to the actual TLV area size. This is because the ECDSA signatures are encoded as ASN.1 and the size of the ASN.1 representation can vary depending on the value of the two integers the signature is composed of.
This was not a big deal but was adding a bit of randomness in the simulation and was for example making difficult to generate reliably an oversized image, possibly leading to occasional false failures of the tests using oversized images.
To avoid the issue, a very simple workaround is implemented: when using ECDSA, each image is signed repeatedly until a signature with the maximum size is obtained. This is possible without modifying the image because ECDSA signing is not a deterministic process and gives a completely different signature at each try. On average, four attempts should be required to get the desired signature size.