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

aes: add GCM-TLS checks and improve performance #21

Merged
merged 11 commits into from
Mar 22, 2022
Merged

Conversation

qmuntal
Copy link
Member

@qmuntal qmuntal commented Mar 8, 2022

This PR can be reviewed commit by commit.

There are two main changes:

  • First commit: Long story short, it adds some security checks to AES-GCM Seal method when sealing a TLS payload. I've added a new document with the detailed description of why we weren't implementing those checks before and how we are implementing them now.
  • Following commits: While investigating the missing checks issue I noticed that Go, OpenSSL and BoringSSL have highly optimized routines for AES-GCM, but we were just doing the bare minimum. Doing a quick web search one can find many reports, such as this one, showing that AES-GCM accounts for almost 95% of all TLS 1.2 and 1.3 traffic. So it is a no-brainer to optimize it as much as reasonable.
    This I what I did:
    • Reuse the same cipher context in all the Go aesGCM lifetime, which reduces cgo calls and helps reusing OpenSSL cipher context backing memory buffers.
    • Batch all Seal/Open cgo calls into one, which means going from 5 calls to 1.

And the results speak by themselves:

name           old time/op    new time/op     delta
AESGCM_Open-4    1.92µs ± 4%     0.40µs ± 7%   -78.92%  (p=0.000 n=10+10)
AESGCM_Seal-4    1.89µs ± 2%     0.41µs ± 4%   -78.49%  (p=0.000 n=9+10)

name           old speed      new speed       delta
AESGCM_Open-4  33.4MB/s ± 4%  158.4MB/s ± 6%  +374.66%  (p=0.000 n=10+10)
AESGCM_Seal-4  33.9MB/s ± 2%  157.7MB/s ± 4%  +365.16%  (p=0.000 n=9+10)

name           old B/op       new B/op        delta
AESGCM_Open-4     48.0B ± 0%       0.0B       -100.00%  (p=0.000 n=10+10)
AESGCM_Seal-4     48.0B ± 0%       0.0B       -100.00%  (p=0.000 n=10+10)

name           old allocs/op  new allocs/op   delta
AESGCM_Open-4      4.00 ± 0%       0.00       -100.00%  (p=0.000 n=10+10)
AESGCM_Seal-4      4.00 ± 0%       0.00       -100.00%  (p=0.000 n=10+10)

@qmuntal qmuntal requested review from jaredpar, dagood and chsienki March 8, 2022 16:07
Copy link
Member

@dagood dagood left a comment

Choose a reason for hiding this comment

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

Reviewed doc, mostly small suggestions. Agree with the Resolution part, for sure seems like the most reasonable way to go!

docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
openssl/aes.go Outdated Show resolved Hide resolved
openssl/aes.go Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Show resolved Hide resolved
openssl/aes.go Show resolved Hide resolved
t.Fatal(err)
}
nonce := []byte{0x91, 0xc7, 0xa7, 0x54, 0, 0, 0, 0, 0, 0, 0, 0}
nonce1 := []byte{0x91, 0xc7, 0xa7, 0x54, 0, 0, 0, 0, 0, 0, 0, 1}
Copy link
Member

Choose a reason for hiding this comment

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

Are there any tests to make sure bad nonces are properly caught in the Go standard library test suite?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are no explicit tests that verifies IVs construction follows the 800-38D 8.3 spec.

Still, Go's IV generation currently follows that spec, and there are some tests in crypto/tls (the ones starting with TestHandshake*) that compare TLS flows against prerecorded golden flows, so if there is an unintentional change in the IV generation algorithm, all those tests will fail.

I'll try to submit a test upstream that directly covers the IV generation so our expectations don't rely on a catch-all regression test nor in dev.boringcrypto tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try to submit a test upstream that directly covers the IV generation so our expectations don't rely on a catch-all regression test nor in dev.boringcrypto tests.

Here it is: https://go-review.googlesource.com/c/go/+/391495

@qmuntal qmuntal force-pushed the dev/qmuntal/gcmtls branch 2 times, most recently from b7eaf17 to c99845a Compare March 10, 2022 08:13
@qmuntal qmuntal force-pushed the dev/qmuntal/gcmtls branch from c99845a to 0b886f0 Compare March 10, 2022 16:19
openssl/aes.go Outdated Show resolved Hide resolved
docs/go-openssl-compat.md Outdated Show resolved Hide resolved
Co-authored-by: Chris Sienkiewicz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants