Skip to content

Tolerate differences in RSA private key libraries #383

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

Merged
merged 2 commits into from
Dec 20, 2024

Conversation

chrisfenner
Copy link
Member

@chrisfenner chrisfenner commented Dec 20, 2024

GOEXPERIMENT=boringcrypto go test ./... --test.run TestPriv/valid_rsa fails occasionally due to the fact that the boringcrypto implementation uses the Carmichael function (lambda(N)) instead of Euler's totient function (phi(N)). Sometimes we get lucky (where phi(N) == lambda(N)) and sometimes we don't.

Unfortunately, the TPM RSA private key structure only contains a prime, for compactness. This means that when importing TPM RSA private keys, we have to compute D from the factorization of N ourselves (see crypto.go). This test generates a key in software, converts it into a TPM private key structure, and then checks that the import function returns the same key. Unfortunately, the software key-generation algorithm may disagree with crypto.go's technique (which is currently to use phi(N)).

This change introduces a helper function to the test for comparing RSA private keys by public exponent, modulus, and primes only.

@chrisfenner chrisfenner marked this pull request as ready for review December 20, 2024 16:03
@chrisfenner chrisfenner requested review from alexmwu, jkl73 and a team as code owners December 20, 2024 16:03
@chrisfenner
Copy link
Member Author

+CC @AlexandreEXFO FYI, in case you find this helpful for your environment

@rsc
Copy link

rsc commented Dec 20, 2024

Not sure you need the import of cmp to handle slice order here. You passed Primes[0] into the TPM struct and then used that field as Primes[0] on the way out too. If they came out reversed that would be a surprise indeed!

Otherwise LGTM.

@chrisfenner
Copy link
Member Author

Not sure you need the import of cmp to handle slice order here. You passed Primes[0] into the TPM struct and then used that field as Primes[0] on the way out too. If they came out reversed that would be a surprise indeed!

Otherwise LGTM.

Ha, of course. Too paranoid. I've simplified this, thanks!

@chrisfenner chrisfenner merged commit 127c99b into google:main Dec 20, 2024
4 checks passed
gopherbot pushed a commit to golang/go that referenced this pull request Jan 7, 2025
This has no practical advantage, and requires extra variable time code,
but is an explicit FIPS 186-5 requirement.

Note that the new behavior is consistent with Go+BoringCrypto, but not
with Go 1.23. The resulting keys are essentially interchangeable, but
it's not impossible for applications to notice (google/go-tpm#383).

gcd_lcm_tests.txt is from BoringSSL.

Change-Id: I6a6a4656fd5e92912c87bedc667456d0e8787023
Reviewed-on: https://go-review.googlesource.com/c/go/+/639936
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
FiloSottile added a commit to FiloSottile/bigmod that referenced this pull request May 8, 2025
This has no practical advantage, and requires extra variable time code,
but is an explicit FIPS 186-5 requirement.

Note that the new behavior is consistent with Go+BoringCrypto, but not
with Go 1.23. The resulting keys are essentially interchangeable, but
it's not impossible for applications to notice (google/go-tpm#383).

gcd_lcm_tests.txt is from BoringSSL.

Change-Id: I6a6a4656fd5e92912c87bedc667456d0e8787023
Reviewed-on: https://go-review.googlesource.com/c/go/+/639936
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>

# Conflicts:
#	nat.go
#	src/crypto/internal/fips140/rsa/keygen.go
#	src/crypto/internal/fips140/rsa/keygen_test.go
#	src/crypto/internal/fips140/rsa/rsa.go
FiloSottile added a commit to FiloSottile/bigmod that referenced this pull request May 8, 2025
This has no practical advantage, and requires extra variable time code,
but is an explicit FIPS 186-5 requirement.

Note that the new behavior is consistent with Go+BoringCrypto, but not
with Go 1.23. The resulting keys are essentially interchangeable, but
it's not impossible for applications to notice (google/go-tpm#383).

gcd_lcm_tests.txt is from BoringSSL.

Change-Id: I6a6a4656fd5e92912c87bedc667456d0e8787023
Reviewed-on: https://go-review.googlesource.com/c/go/+/639936
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>

# Conflicts:
#	nat.go
#	src/crypto/internal/fips140/rsa/keygen.go
#	src/crypto/internal/fips140/rsa/keygen_test.go
#	src/crypto/internal/fips140/rsa/rsa.go
FiloSottile added a commit to FiloSottile/bigmod that referenced this pull request May 8, 2025
This has no practical advantage, and requires extra variable time code,
but is an explicit FIPS 186-5 requirement.

Note that the new behavior is consistent with Go+BoringCrypto, but not
with Go 1.23. The resulting keys are essentially interchangeable, but
it's not impossible for applications to notice (google/go-tpm#383).

gcd_lcm_tests.txt is from BoringSSL.

Change-Id: I6a6a4656fd5e92912c87bedc667456d0e8787023
Reviewed-on: https://go-review.googlesource.com/c/go/+/639936
Reviewed-by: Russ Cox <rsc@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>

# Conflicts:
#	nat.go
#	src/crypto/internal/fips140/rsa/keygen.go
#	src/crypto/internal/fips140/rsa/keygen_test.go
#	src/crypto/internal/fips140/rsa/rsa.go
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