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

PKCS#1 v1.5 signatures example on docs.rs doesn't work #253

Closed
rster2002 opened this issue Jan 19, 2023 · 2 comments
Closed

PKCS#1 v1.5 signatures example on docs.rs doesn't work #253

rster2002 opened this issue Jan 19, 2023 · 2 comments

Comments

@rster2002
Copy link

rster2002 commented Jan 19, 2023

I've been trying to get signing to work, but did not have any luck trying. One of my trouble shooting steps is to create a new rust binary package and try to run this example but it doesn't seem to compile due to an unsatisfied trait bound.

main.rs:

use rsa::RsaPrivateKey;
use rsa::pkcs1v15::{SigningKey, VerifyingKey};
use rsa::signature::{Keypair, RandomizedSigner, SignatureEncoding, Verifier};
use sha2::{Digest, Sha256};

fn main() {
    let mut rng = rand::thread_rng();

    let bits = 2048;
    let private_key = RsaPrivateKey::new(&mut rng, bits).expect("failed to generate a key");
    let signing_key = SigningKey::<Sha256>::new_with_prefix(private_key);
    let verifying_key = signing_key.verifying_key();

    // Sign
    let data = b"hello world";
    let signature = signing_key.sign_with_rng(&mut rng, data);
    assert_ne!(signature.to_bytes().as_ref(), data.as_slice());

    // Verify
    verifying_key.verify(data, &signature).expect("failed to verify");
}

cargo.toml:

[package]
name = "rsa-test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
rsa = "0.8.0"
sha2 = "0.10.6"

Environment:

  • Windows 10: Version 10.0.19045 Build 19045
  • cargo --version: cargo 1.65.0 (4bc8f24d3 2022-10-20)

Is there something I'm missing here or is the example on docs.rs outdated? Thanks in advance!

@tarcieri
Copy link
Member

Note that all of the docs on https://docs.rs are doctested in CI, so in general they will not ever be out-of-date for a given version.

The problem is you need the oid feature of the sha2 crate enabled:

sha2 = { version = "0.10.6", features = ["oid"] }

We should probably add an explicit dependency on sha2 with the oid feature to alleviate this problem.

@rster2002
Copy link
Author

I couldn't find that in the docs. Thanks for the help.

tarcieri added a commit that referenced this issue Jan 20, 2023
We seem to be running into a lot of people who are having trouble with
PKCS#1 v1.5 signatures because the failure mode for the `oid` feature of
the `sha2` crate being disabled is fairly unscrutable.

See #234, #253, and the semi-related tracking issue for #238.

If `rsa` has a `sha2` feature, we can always ensure `oid` is enabled,
and this can be used in code examples. It also means users don't need
two crates to create/verify PKCS#1 v1.5 signatures.

RSA is used commonly enough with the SHA2 family that this integration
probably makes sense.
tarcieri added a commit that referenced this issue Jan 20, 2023
We seem to be running into a lot of people who are having trouble with
PKCS#1 v1.5 signatures because the failure mode for the `oid` feature of
the `sha2` crate being disabled is fairly unscrutable.

See #234, #253, and the semi-related tracking issue for #238.

If `rsa` has a `sha2` feature, we can always ensure `oid` is enabled,
and this can be used in code examples. It also means users don't need
two crates to create/verify PKCS#1 v1.5 signatures.

RSA is used commonly enough with the SHA2 family that this integration
probably makes sense.
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