Repository navigation
Use Rust code to construct and check RSA private keys - #360
briansmith wants to merge 0 commit into
Conversation
|
The current version of this PR is a sketch of what the new RSA private key construction code should look like. It also moves ring towards doing the RSA signing operation in Rust (it is currently done in C by It contains a very lightweight static capability mechanism to help typecheck modular arithmetic. For example, the modulus for mod n arithmetic is of type The naming of things is based on the naming for the similar field arithmetic code in src/ec/suite_b/ops. At some point in the future we might be able to define some traits that abstract over both the RSA and the ECC code. There is still a substantial amount of cleanup to do, but it would be good to get some early feedback. After this, and after |
|
Also, I'm looking for a proof than given:
then checking p * q == 0 (mod n) is equivalent to checking p * q == n. (Our goal is to use only Montgomery modular multiplication, never non-modular arithmetic.) |
|
@briansmith Excellent! I'll check out the implementation tonight. Unfortunately, I don't think that theorem holds. Consider q = 3, p = 4, n = 6. A counterexample for prime q, p is given by (p, q) = (7, 5). I do have a proof if we strengthen the log-ceiling equalities to ⌈lg p⌉ = ⌈lg q⌉ = (lg n)/2, and essentially the same proof also works for max(⌈lg p⌉, ⌈lg q⌉) ≤ (lg n)/2. |
OIC. I think maybe we should use ⌈lg n⌉/2 instead of ⌈(lg n)/2⌉. Also, n is always odd, which may help.
I think you left out n here. It can't be 6 like in the previous example since it needs to be about twice the size of either p and q.
Awesome! I think we should be able to figure it out. |
|
Ha, oops! You're right, my second counterexample is no good— I don't see any small counterexamples with both |
|
Btw, here's the proof I have, if you want to try to modify it for Proof. Let Suppose Taking logarithms and applying the homomorphism identity, we have: Recall that for any Whence |
41b4ac8 to
f25107d
Compare
|
I've landed some of the prerequisite changes from the first iteration. This second set of commits is closer to what the final form of this PR will be. |
We are trying to implement the crt_pkv validation from NIST SP.800-56B rev1, section 6.4.1.4.3, RSA Key-Pair Validation (Intended Exponent-Creation Method Unknown) using a CRT-format key. We don't know that We also need to verify dmp1 · e mod (p – 1) == 1 and 1 == dmq1 * e mod (q - 1), which will also be non-trivial because I'm hoping to remove the ability to calculate modulos with even moduli. |
|
I filed #362 about extending the checks to comply with the NIST recommendations. |
8b56bb8 to
dae267e
Compare
|
I think that as long as you ensure p and q are not even you should be alright: #362 (comment). |
I think #362 is the best place to continue shoring up these checks. My plan is to clean up the commits in this PR and land them soon. I am hoping that this PR will be enough of a model to help people to complete the oxidation of crypto/rsa. |
dae267e to
0fc876e
Compare
|
OK, this is actually getting close for real now. Most noteworthy is that pub fn elem_mul_mixed<F: Field>(a: &Elem<F>, b: &ElemDecoded<F>, m: &Modulus<F>)
-> Result<ElemDecoded<F>, error::Unspecified> {This lets us use I'll double-check the code tomorrow and hopefully land it. |
ranweiler
left a comment
There was a problem hiding this comment.
I really like how the code in RSAKeyPair::from_der reads, as well as the use of Field. This is looking good!
There was a problem hiding this comment.
Any reason not to use is_empty() here?
There was a problem hiding this comment.
I added a commit to use is_empty() throughout the project.
There was a problem hiding this comment.
In both into_elem and into_elem_decoded, we call into C to compute cmp, then check its value to ensure that our Positive is less than the field modulus. Maybe factor this out into a private, inlined helper function -> bool that can used by both methods?
More generally, is there value in providing [Partial]Ord instances that use GFp_BN_cmp under the hood? That might be overkill, and these blocks are short enough where the noise/context switch to C doesn't impair readability too much, but it still might be nice to more fully hide the C _cmp API.
After I wrote the above, I realized we're already deriving PartialEq from Whole. Adding code to cast a Modulus to a Whole seems gratuitous, so I think this is fine as-is.
There was a problem hiding this comment.
eventually Modulus will be defined as something like:
struct Modulus<M> {
n0: Limb,
rr: Elem<M>, // Not zero or one.
value: OddPositive,
}So eventually we'll do what you're suggesting.
There was a problem hiding this comment.
Bikeshedding the choice of "Whole": in the world outside of this library, I think Whole suffers the same ambiguity as Natural or Counting— I personally would expect it to specifically exclude zero! At that point, we might as well just one of the standard names.
An unambiguous, slightly cumbersome alternative, in the same vein as Positive, is simply to useNonnegative for this struct. I don't think it is a big deal, though.
There was a problem hiding this comment.
I renamed it to Nonnegative.
There was a problem hiding this comment.
Ah, this relates to my earlier comment. Perhaps this is worth macro-izing to share amongst the other bignum structs?
There was a problem hiding this comment.
(though I guess this wouldn't be applicable to the modulus bound check without some conversions)
There was a problem hiding this comment.
Update: I see now that we just derive this elsewhere.
There was a problem hiding this comment.
In earlier C functions where we replacedinout, len args by a single base arg, we left an inline comment /*inout*/. Did we want to do that here, too, or perhaps get rid of the other comments?
There was a problem hiding this comment.
&mut means “inout,” right? AFAICT /*inout*/ only appears in C code for the declarations of this function.
There was a problem hiding this comment.
This comment is a bit confusing— m refers to a Modulus struct, which wraps a Montgomery context. That means the ctx->N (to abuse notation) is the actual modulus— this is what I assume "n" is referring to in the comment. However, we're intentionally equivocating between the Montgomery context and its modulus, so m and n, interpreted as moduli, are the same modulus, right?
Assuming the above is correct, maybe just change the first n in the comment to m, to embrace the equivocation and remove any reason for doubt. Or, if I'm confused, add something to clarify the relationship between n and m.
There was a problem hiding this comment.
I also removed the "assuming.." {a, b} < m is implied by the use of the Elem[Decoded] types.
There was a problem hiding this comment.
Doesn't impact correctness, but I think we want this local variable to be named is_one.
There was a problem hiding this comment.
I renamed is_zero to is_one as you suggested.
There was a problem hiding this comment.
I really like how the Montgomery encoding is captured by the types!
There was a problem hiding this comment.
Forgive me, this is an extreme nit, but I can't unsee it: there's no space before the closing */.
There was a problem hiding this comment.
Fixed. (No need for such polite qualifiers, BTW.)
0fc876e to
46f59b4
Compare
|
Landed as: 0c2cffe Check RSA public key parameters in Rust. |
|
Also, thanks to both @samscott89 and @ranweiler for looking at this! |
No description provided.