RustCrypto / RSA

RSA implementation in pure Rust
Apache License 2.0
536 stars 146 forks source link

Add signing and verifying to README.md #417

Closed coder0xff closed 5 months ago

coder0xff commented 6 months ago

Recent commits brokes some signing and verifying code that I have:

    let private_key: RsaPrivateKey = ...
    let signing_key = SigningKey::<Sha256>::new(private_key);

Error

error[E0599]: the function or associated item `new` exists for struct `SigningKey<CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, OidSha256>>>`, but its trait bounds were not satisfied
  --> /home/coder0xff/Dropbox/Documents/Projects/Community/src/community/libcommunity/src/crypt.rs:72:45
   |
72 |     let signing_key = SigningKey::<Sha256>::new(private_key);
   |                                             ^^^ function or associated item cannot be called due to unsatisfied trait bounds
   |
  ::: /home/coder0xff/.cargo/git/checkouts/rsa-7c6de05781696f1f/d011ca5/src/pkcs1v15/signing_key.rs:23:1
   |
23 | pub struct SigningKey<D>
   | ------------------------
   | |
   | doesn't satisfy `_: InnerInit`
   | doesn't satisfy `_: InnerUser`
   | doesn't satisfy `_: KeyInit`
   |
  ::: /home/coder0xff/.cargo/registry/src/index.crates.io-6f17d22bba15001f/digest-0.10.7/src/core_api/wrapper.rs:24:1
   |
24 | pub struct CoreWrapper<T>
   | -------------------------
   | |
   | doesn't satisfy `_: AssociatedOid`
   | doesn't satisfy `_: Digest`
   | doesn't satisfy `_: OutputSizeUser`
   |
   = note: the full type name has been written to '/home/coder0xff/Dropbox/Documents/Projects/Community/src/community/esp32s3-headless/target/xtensa-esp32s3-espidf/debug/deps/libcommunity-c3213a29264c7117.long-type-16930127551359609831.txt'
   = note: the following trait bounds were not satisfied:
           `sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>: rsa::sha2::Digest`
           `sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>: rsa::signature::digest::OutputSizeUser`
           which is required by `sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>: rsa::sha2::Digest`
           `sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>: AssociatedOid`
           `rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: sha2::digest::InnerInit`
           which is required by `rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: aes_gcm::KeyInit`
           `rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: sha2::digest::crypto_common::InnerUser`
           which is required by `rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: aes_gcm::KeyInit`
           `&rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: sha2::digest::InnerInit`
           which is required by `&rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: aes_gcm::KeyInit`
           `&rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: sha2::digest::crypto_common::InnerUser`
           which is required by `&rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: aes_gcm::KeyInit`
           `&mut rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: sha2::digest::InnerInit`
           which is required by `&mut rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: aes_gcm::KeyInit`
           `&mut rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: sha2::digest::crypto_common::InnerUser`
           which is required by `&mut rsa::pkcs1v15::SigningKey<sha2::digest::core_api::CoreWrapper<sha2::digest::core_api::CtVariableCoreWrapper<sha2::Sha256VarCore, sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<sha2::digest::typenum::UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, sha2::OidSha256>>>: aes_gcm::KeyInit`

It may just be that the API changed and I don't understand how to use it. An example of signing and verifying would be helpful to be sure that I'm using it correctly.

tarcieri commented 6 months ago

There are examples here which are doctested in CI:

Can you provide a complete reproduction of your problem, showing where the alleged regression occurred?

coder0xff commented 6 months ago

Thanks! Looking at the examples it does look like the code I have is still in line with the API. I'll work on figuring out a reproduction I can share.

coder0xff commented 5 months ago

I had to use rsa::sha2::Sha256 instead of use sha2::Sha256.