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

Add Pk::private_from_rsa_components and Pk::public_from_rsa_components #347

Merged
merged 1 commit into from
Feb 9, 2024

Conversation

mzohreva
Copy link
Contributor

@mzohreva mzohreva commented Feb 9, 2024

No description provided.

@mzohreva mzohreva force-pushed the mz/rsa-key-from-components branch from 521bdb6 to 31c795e Compare February 9, 2024 02:15
mbedtls/src/pk/mod.rs Show resolved Hide resolved
@Taowyoo
Copy link
Collaborator

Taowyoo commented Feb 9, 2024

Because of default branch name change, the CI is not triggerred now.
I will raise a PR to fix it

@Taowyoo
Copy link
Collaborator

Taowyoo commented Feb 9, 2024

Hi @mzohreva , you might need to rebase this PR to trigger the CI .

@mzohreva mzohreva force-pushed the mz/rsa-key-from-components branch from 31c795e to 34f15f6 Compare February 9, 2024 21:33
@mzohreva mzohreva merged commit 75cb9b7 into master Feb 9, 2024
11 checks passed
@mzohreva mzohreva deleted the mz/rsa-key-from-components branch February 9, 2024 21:45
/// Private exponent
d: &'a Mpi,
/// Public exponent
e: &'a Mpi,
Copy link
Contributor

@zugzwang zugzwang Feb 12, 2024

Choose a reason for hiding this comment

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

I understand that these values are used by upstream in rsa_import, but e is redundant here.
EDIT: Not redundant, as λ(n) is not easy to compute.

What is the behavior if we pass inconsistent d, e here (with respect to n)? Perhaps we should add a test, since I don't see an explicit d, n check in rsa_check_context or in rsa_complete which we are rightfully calling on import. It looks like it assumes that d, n are consistent.

Copy link
Contributor

Choose a reason for hiding this comment

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

I checked, and rsa_check_context does not guarantee consistency of parameters, and rsa_complete does not check this specifically.

Moreover I think it is an important check, for some implementations use ed = 1 mod λ(n) and others ed = 1 mod φ(n) (see e.g. https://gitlab.com/sequoia-pgp/sequoia/-/issues/792).

Copy link
Contributor

Choose a reason for hiding this comment

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

After further research, MbedTLS returns RSA_BAD_INPUT_DATA when trying to deduce_primes, see here.

This check happens a bit too late and in principle inside a loop that aborts early or late depending on the input. Arguably this is upstream but we might want to check for consistency in this struct at least. I am now wondering if this loop be used by an attacker controlling e, n somehow to learn information about d, but this is upstream concern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we use mbedtls_rsa_check_privkey after rsa_complete to catch any issue with the parameters?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that specifically checks for de == 1 mod (p-1) and de == 1 mod (q-1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in #352

e: &Mpi::new(pk.rsa_public_exponent().unwrap() as _).unwrap(),
};
let mut pk3 = Pk::private_from_rsa_components(components).unwrap();
assert_eq!(pk.write_private_der_vec().unwrap(), pk3.write_private_der_vec().unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

This test fails when I don't use the test rng:

        use crate::rng::{CtrDrbg, OsEntropy};
        let entropy = OsEntropy::new();
        let mut rng = CtrDrbg::new(entropy.into(), None).unwrap();
        let mut pk = Pk::generate_rsa(&mut rng, 2048, 0x10001).unwrap();

It fails in the last assertion. I think it swaps parameter order on DER serialization?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should refactor the test RNG so that it randomizes tests, or have another test RNG. (cc. @jethrogb)

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