RustCrypto / RSA

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

Why `pss::Signature` implement `TryFrom<&[u8]>` but don't raise any error ? #349

Closed FirelightFlagboy closed 1 year ago

FirelightFlagboy commented 1 year ago

Hello,

I have a question concerning the implementation of pss::Signature when we try to build a new signature from raw bytes.

Signature implement TryFrom<&[u8]> for that, indicating that it may fail but the implementation always return Ok.

Is there a reason for Signature to impl TryFrom<&[u8]> and not From<&[u8]> ?

https://github.com/RustCrypto/RSA/blob/40069a5408dc9eb531d68a50b3ada5c9ab47466d/src/pss/signature.rs#L35-L44

tarcieri commented 1 year ago

A TryFrom<&[u8]> bound is required by the SignatureEncoding trait.

It doesn't mandate an error type though, so Infallible and with it the blanket impl of TryFrom for types which impl From should be allowed, so it could potentially be replaced with From<&[u8]>.

However, it would probably make sense to set an upper bound on the size of a PSS signature (e.g. 2048-bytes) to prevent it from parsing some extremely large value as a BigUint.

FirelightFlagboy commented 1 year ago

Thank you for the clarification, I'll close this issue since my question was answered :smile: