Skip to content

Use Rust code to construct and check RSA private keys - #360

Closed
briansmith wants to merge 0 commit into
masterfrom
rsa-in-rust
Closed

briansmith wants to merge 0 commit into
masterfrom
rsa-in-rust

Conversation

@briansmith

Copy link
Copy Markdown
Owner

No description provided.

@briansmith

Copy link
Copy Markdown
Owner Author

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 GFp_rsa_private_transform).

It contains a very lightweight static capability mechanism to help typecheck modular arithmetic. For example, the modulus for mod n arithmetic is of type Modulus<N>, the modulus for mod p arithmetic is of type Modulus<P>, etc. Similarly the multiplication is defined over elements of types Elem<N>, Elem<P>, etc. so that the compiler only allows us to multiple two elements of the same field modulo the modulus for that field. It is neat, but I'm not sure how useful it is, so I might scrap it.

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 GFp_rsa_private_transform and GFp_rsa_public_decrypt are replaced, we'll likely be able to make a substantial simplification (removal) of BIGNUM C code.

/cc @ranweiler @samscott89

@briansmith

Copy link
Copy Markdown
Owner Author

Also, I'm looking for a proof than given:

  • 0 < q < p < n, and
  • ⌈lg p⌉ = ⌈lg q⌉ = ⌈(lg n)/2⌉, i.e. p.bit_length() == q.bit_length() == n_bit_length().half_rounded_up()

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.)

@ranweiler

Copy link
Copy Markdown
Contributor

@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.

@briansmith

Copy link
Copy Markdown
Owner Author

Unfortunately, I don't think that theorem holds. Consider q = 3, p = 4, n = 6.

OIC.

I think maybe we should use ⌈lg n⌉/2 instead of ⌈(lg n)/2⌉. Also, n is always odd, which may help.

A counterexample for prime q, p is given by (p, q) = (7, 5).

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.

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.

Awesome! I think we should be able to figure it out.

@ranweiler

Copy link
Copy Markdown
Contributor

Ha, oops! You're right, my second counterexample is no good— I don't see any small counterexamples with both p and q prime. Also, it's true that we only actually care about prime RSA p and q, right?

@ranweiler

Copy link
Copy Markdown
Contributor

Btw, here's the proof I have, if you want to try to modify it for ⌈lg(n)⌉/2.

Proof. Let 0 < q < p < n and m := max(⌈lg(q)⌉, ⌈lg(p)⌉).
Assume m ≤ lg(n)/2.

Suppose pq = 0 (mod n), so pq = nk for some k > 0.
We want to show that k = 1, so pq = n.

Taking logarithms and applying the homomorphism identity, we have:

      lg pq = lg nk
lg p + lg q = lg n + lg k

Recall that for any x, we have x ≤ ⌈x⌉.
Then, applying our hypotheses, we obtain the bound:

lg n + lg k  = lg p + lg q
             ≤ ⌈lg p⌉ + ⌈lg q⌉
             ≤ m + m
             ≤ lg(n)/2 + lg(n)/2
             = lg(n)

Whence lg n + lg k ≤ lg n, and so lg k ≤ 0.
Since 0 ≤ lg k, we have lg k = 0, and thus k = 1, as required.

@briansmith

Copy link
Copy Markdown
Owner Author

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.

@briansmith

Copy link
Copy Markdown
Owner Author

Ha, oops! You're right, my second counterexample is no good— I don't see any small counterexamples with both p and q prime. Also, it's true that we only actually care about prime RSA p and q, right?

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 p and q are prime. I guess we're allowed to presume so if (and only if?) all the checks pass. One of the checks is that p * q == n.

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.

@briansmith

Copy link
Copy Markdown
Owner Author

I filed #362 about extending the checks to comply with the NIST recommendations.

@briansmith
briansmith force-pushed the rsa-in-rust branch 2 times, most recently from 8b56bb8 to dae267e Compare November 29, 2016 07:55
@samscott89

Copy link
Copy Markdown
Contributor

I think that as long as you ensure p and q are not even you should be alright: #362 (comment).

@briansmith

Copy link
Copy Markdown
Owner Author

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.

@briansmith

Copy link
Copy Markdown
Owner Author

OK, this is actually getting close for real now.

Most noteworthy is that elem_mul_mixed actually has the signature that one would expect:

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 Field as a more sensible name for what was Role. If we were so inclined, it would be relatively straightforward to apply the "kernel" aspect of lightweight static capabilities. However, I'm not planning to do that unless/until we do multi-prime RSA.

I'll double-check the code tomorrow and hopefully land it.

@ranweiler ranweiler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how the code in RSAKeyPair::from_der reads, as well as the use of Field. This is looking good!

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use is_empty() here?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit to use is_empty() throughout the project.

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo at "TODO".

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it to Nonnegative.

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this relates to my earlier comment. Perhaps this is worth macro-izing to share amongst the other bignum structs?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(though I guess this wouldn't be applicable to the modulus bound check without some conversions)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I see now that we just derive this elsewhere.

Comment thread src/rsa/signing.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&mut means “inout,” right? AFAICT /*inout*/ only appears in C code for the declarations of this function.

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I s/n/m/

@briansmith briansmith Dec 1, 2016 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also removed the "assuming.." {a, b} < m is implied by the use of the Elem[Decoded] types.

Comment thread src/rsa/bigint.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't impact correctness, but I think we want this local variable to be named is_one.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed is_zero to is_one as you suggested.

Comment thread src/rsa/signing.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how the Montgomery encoding is captured by the types!

Comment thread crypto/bn/montgomery.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgive me, this is an extreme nit, but I can't unsee it: there's no space before the closing */.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. (No need for such polite qualifiers, BTW.)

@briansmith briansmith closed this Dec 2, 2016
@briansmith
briansmith deleted the rsa-in-rust branch December 2, 2016 01:21
@briansmith

Copy link
Copy Markdown
Owner Author

Landed as:

0c2cffe Check RSA public key parameters in Rust.
ea77b59 Add extra check for minimum minimum RSA public modulus bit length.
f048caf Check RSA private key parameter bounds in Rust.
cd221a9 Construct most RSA private key Montgomery contexts in Rust.
3b188bb Return boolean value instead of pointer from GFp_BN_copy.
80e6231 Check p * q == n in Rust.
1af1b46 Convert iqmp to the Montgomery domain in Rust in RSA signing.
2906a4c Calculate q**2 (mod n) in Rust in RSA signing.
f1ad3ef Remove rsa.c, including the checks on dmp1 and dmq1.
5502bec Manage RSAKeyPair attributes in Rust, not in the C RSA struct.
465bff9 Replace outermost steps of GFp_BN_private_transform with Rust code.
46f59b4 Use is_empty() instead of comparing len() to 0.

@briansmith

Copy link
Copy Markdown
Owner Author

Also, thanks to both @samscott89 and @ranweiler for looking at this!

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

Successfully merging this pull request may close these issues.

3 participants