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

Non intuitive behavior of From<RsaPublicKey> for VeryfyingKey<D> #431

Closed
dyatelok opened this issue May 26, 2024 · 1 comment
Closed

Non intuitive behavior of From<RsaPublicKey> for VeryfyingKey<D> #431

dyatelok opened this issue May 26, 2024 · 1 comment

Comments

@dyatelok
Copy link

I had a problem with signature verification. I narrowed problem down to the following test:

#[test]
fn test_signing_verifying() {
    use rsa::{
        pkcs1v15::{Signature, SigningKey, VerifyingKey},
        sha2::Sha256,
        signature::{RandomizedSigner, SignatureEncoding, Verifier},
        RsaPrivateKey,
    };

    const BIT_SIZE: usize = 2048;

    let mut rng = rand::thread_rng();
    let private_key = RsaPrivateKey::new(&mut rng, BIT_SIZE).expect("failed to generate a key");

    let signing_key = SigningKey::<Sha256>::new(private_key.clone());
    let public_key = private_key.to_public_key();
    let verifying_key: VerifyingKey<Sha256> = public_key.into();

    let data = vec![123u8; 255];
    let signature = signing_key.sign_with_rng(&mut rng, &data).to_bytes();
    let signature = signature.as_ref();
    let Ok(signature) = Signature::try_from(signature) else {
        panic!("Failed to convert back to signature")
    };

    let is_valid = verifying_key.verify(&data, &signature).is_ok();

    assert!(is_valid);
}

It fails. However if I change lines

    let signing_key = SigningKey::<Sha256>::new(private_key.clone());
    let public_key = private_key.to_public_key();
    let verifying_key: VerifyingKey<Sha256> = public_key.into();

to

    let signing_key = SigningKey::<Sha256>::new(private_key.clone());
    let verifying_key: VerifyingKey<Sha256> = signing_key.verifying_key();

It works. Very unintuitive. Reason for this From trait implementation which uses new_unprefixed instead of new. https://docs.rs/rsa/latest/src/rsa/pkcs1v15/verifying_key.rs.html#163-170

In the docs to new_unprefixed it even says that you most likely wanna use new. So I consider this being a bug.

@tarcieri
Copy link
Member

Dup of #341

@tarcieri tarcieri closed this as not planned Won't fix, can't repro, duplicate, stale May 26, 2024
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

No branches or pull requests

2 participants