Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport 2.2x: range-based constant-flow base64 #4835

Merged
merged 21 commits into from
Oct 27, 2021

Conversation

gilles-peskine-arm
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm commented Aug 3, 2021

Fix #4814. See #4819.

Compared to the 2.16 version:

  • The commit "New sample program to benchmark certificate loading" had some conflicts in build scripts due to refactorings that happened after 2.16. In all cases it was just a matter of adding the new file in the right place.
  • There are extra commits to unit-test the new internal functions (which isn't something we did in 2.16). The unit tests validate that the new internal functions are constant-flow when running under a properly instrumented valgrind or msan. There is still no validation of constant flow for mbedtls_base64_decode itself due to its logic relying on equality tests of string contents against non-digit values (e.g. src[i] == ' '); I haven't found a way to get rid of those equality tests without a significant cost in both performance and code size.

I expect the 3.0 forward-port to be identical except for further build script tweaks and for timing in load_roots needing adjustments (or maybe we can do the easy thing and remove it). I'll make it after the 2.2x version has at least one approval.

@gilles-peskine-arm gilles-peskine-arm added bug needs-review Every commit must be reviewed by at least two team members, component-crypto Crypto primitives and low-level interfaces needs-reviewer This PR needs someone to pick it up for review labels Aug 3, 2021
@gilles-peskine-arm gilles-peskine-arm requested a review from mpg August 3, 2021 10:46
@gilles-peskine-arm gilles-peskine-arm added needs-ci Needs to pass CI tests needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Aug 3, 2021
@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review and removed needs-ci Needs to pass CI tests needs-work labels Aug 3, 2021
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified that the first commits (up to 4ac01d2) are a forward-port of the 2.16 PR, with the obvious conflict resolution, and the manually reviewed the other commits. I'm generally happy with the testing strategy, but have a few reservations about specific points of its implementation.

include/mbedtls/base64.h Outdated Show resolved Hide resolved
tests/suites/test_suite_base64.function Outdated Show resolved Hide resolved
tests/suites/test_suite_base64.function Outdated Show resolved Hide resolved
@daverodgman daverodgman added needs-work and removed needs-review Every commit must be reviewed by at least two team members, labels Aug 6, 2021
@gilles-peskine-arm gilles-peskine-arm requested a review from mpg August 6, 2021 12:57
@gilles-peskine-arm gilles-peskine-arm added needs-review Every commit must be reviewed by at least two team members, and removed needs-work labels Aug 6, 2021
mpg
mpg previously approved these changes Aug 9, 2021
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@davidhorstmann-arm davidhorstmann-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good apart from 2 nits that I also put on the other backport (since they apply to both I was unsure where to raise them).

Changes
* Improve the performance of base64 constant-flow code. The result is still
slower than the original non-constant-flow implementation, but much faster
than the previous constant-flow implemenation. Fixes #4814.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "implemenation" -> "implementation"

library/base64.c Outdated
return (unsigned char) ( 1 ^ difference );
/* low_mask is: 0 if low <= c, 0x...ff if low > c */
unsigned low_mask = ( (unsigned) c - low ) >> 8;
/* high_mask is: 0 if c <= high, 0x...ff if high > c */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is wrong: high > c should be c > high or high < c

@mpg mpg removed the needs-reviewer This PR needs someone to pick it up for review label Aug 19, 2021
n was used for two different purposes. Give it a different name the second
time. This does not seem to change the generated code when compiling with
optimization for size or performance.

Signed-off-by: Gilles Peskine <[email protected]>
To test c <= high, instead of testing the sign of (high + 1) - c, negate the
sign of high - c (as we're doing for c - low). This is a little easier to
read and shaves 2 instructions off the arm thumb build with
arm-none-eabi-gcc 7.3.1.

Signed-off-by: Gilles Peskine <[email protected]>
I had originally thought to support directories with
mbedtls_x509_crt_parse_path but it would have complicated the code more than
I cared for. Remove a remnant of the original project in the documentation.

Signed-off-by: Gilles Peskine <[email protected]>
Add unit tests for mask_of_range(), enc_char() and dec_value().

When constant-flow testing is enabled, verify that these functions are
constant-flow.

Signed-off-by: Gilles Peskine <[email protected]>
This is part of the definition of the encoding, not a choice of test
parameter, so keep it with the test code.

Signed-off-by: Gilles Peskine <[email protected]>
digits is also a local variable in host_test.function, leading to compilers
complaining about that shadowing the global variable in
test_suite_base64.function.

Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Signed-off-by: Gilles Peskine <[email protected]>
Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after rebase - I checked that the only differences are the ones announced.

Copy link
Contributor

@davidhorstmann-arm davidhorstmann-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks!

@mpg mpg added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Oct 27, 2021
@mpg mpg merged commit 8e8251a into Mbed-TLS:development_2.x Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports bug component-crypto Crypto primitives and low-level interfaces
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Huge certificate parsing speed regression between mbedtls 2.16.9 and 2.16.10 (constant time base64).
5 participants