-
Notifications
You must be signed in to change notification settings - Fork 157
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
RsaPrivateKey::from_components
might panic
#163
Comments
I just merged #162 which is a breaking change and puts This means
One option here would be to drop multiprime RSA support entirely. It's rarely used and provides little performance benefit. The simple solution for better RSA performance is to use a smaller key size, i.e. two smaller primes, rather than using more primes (multiprime RSA provides a minutely better tradeoff for security bounds and prime sizes here). If we dropped multiprime RSA, then the two primes could be provided as two explicit arguments, which would be straightforward and type safe. However, if |
Sadly, I'm not that into RSA to have a good opinion but as long as it stays compatible with other RSA implementations, I would agree.
I'm also fine with that as long as we remove the panic. I also think that this decision should be part of #34 as the overall feel of this crate could be better. |
Revisiting this with an eye on our use case: According to RFC 7518 section 6.3.2 (Algorithms for Json Web Keys), a Json Web Key using RSA is only required to contain the private exponent Furthermore, I'd suggest something like a separate mode for RSA with optimisations. In this case, I'd suggest to have a constructor which takes a struct with the different values (p, q, dp, dq, qi) needed for optimisation similar how the PrecomputedValues struct is currently used internally. One way to do this would be to make an RsaMode enum with the variant enum RsaMode {
Default,
Optimised(OptimisationValues),
}
struct OptimisationValues {
p: BigUInt,
q: BigUInt
dp: BigUint
// ...
} The The constructor for the impl RsaPrivateKey {
// result cuz the constructor should validate the provided parameters.
pub fn new(n: BigUInt, d: BugUInt, mode: RsaMode) -> Result<Self>
} This way, someone who doesn't care about the possible optimisations can still just use the functions for signing / decryption, but those who care are able to match the |
NOTE: accidentally edited your comment trying to quote it @Erik1000. Hopefully it's back to how you had it originally.
You'll need The algorithm described in Appendix C of https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Br1.pdf can be used to recover
|
Yes, I assumed they are always present since they are the public part of the key. At least according to the JWA RFC |
API wise, I'd just suggest inherent static functions of The recovery algorithm could be provided as something like |
Adds an error case in the event the number of `primes` provides is fewer than 2, which prevents panics when invoking methods which expect primes to always be present at indices 0 and 1 (i.e. `p` and `q`) Fixes #163
Went ahead and fixed this in #167. I noted that the recovery function could also be implemented in |
Adds an error case in the event the number of `primes` provides is fewer than 2, which prevents panics when invoking methods which expect primes to always be present at indices 0 and 1 (i.e. `p` and `q`) Fixes #163
Some internals of the
num-bigint-dig
crate might panic under some conditions andrsa
does not check for these. Therefore, users of thersa
crate experience panics.I've stumbled upon this while implementing some jose stuff, so this examples uses a key in JsonWebKey format:
This can lead to denial of service attacks if untrusted keys are parsed and isn't mention in the docs at all.
I haven't tried malicious pem/der encoded keys, but it seems realistic that they suffer from the same problem.
I've also noticed that the
sign
method might loop if an incorrect parameter is used but that can probably be prevented by usingRsaPrivateKey::validate
.Also, I really don't like the way
primes
are passed toRsaPrivateKey::from_components
since passing an array with a length < 2 leads to panic because of out of bounds access and that isn't mention in the docs either.The text was updated successfully, but these errors were encountered: