Skip to content

implement NIST SP-800-56B-rev1-compliant RSA private key validation. #362

Closed as not planned

Description

@briansmith

See #360, where we've done some work on private key validation in Rust, replacing some similar code written in C.

We need to extend that work to comply with NIST SP-800-56B-rev1. Specifically, since we don't know how the exponent is determined, we need to implement the crt_pkv validation mechanism for the RSA Key-Pair Validation (Intended Exponent-Creation Method Unknown) case. That is in section 6.4.1.4.3.

At a minimum, we need to:

  • Annotate the code that does the private key validation with references to the steps given in the NIST document.
  • Restrict RSA private keys (but not public keys), to having e >= 65536. Note that the document allows for e to be up to 256 bits, but we're only going to maintain our 33 bit limit for now. (This is assuming the NIST document allows us to be more strict than what it specifies.)
  • Verify that (ke)d) == 1 for some 1 < k < n of our choosing.
  • Document the proof that our check p * q == 0 (mod n) is equivalent mathematically to p * q == n, or if it isn't, then replace it with a compliant check.
  • Add the checks on the consistency of e and dmp1 and dmq1 that we've skipped.
  • Add the public key as a parameter to the validation. (I suggest doing this step last.) I haven't read the document carefully but presumably the point of this check is to make sure that the public key in the certificate is the same as the public key in the private key bundle; i.e. it is a check that the application is using a private key consistent with the public key it sends its peers.

Activity

  1. briansmith commented on Nov 29, 2016

    @briansmith
    OwnerAuthor

    Note that computing e * dmp1 (mod p-1) and e * dmq1 (mod q-1) requires doing modular multiplication with an even modulus. BoringSSL and OpenSSL use BN_mod (a macro around BN_div) for that, but we're hoping to remove BN_div completely. Accordingly, we can't do the reduction using the even modulus directly. However, we can use the CRT to do it; see http://www.people.vcu.edu/~jwang3/CMSC691/j34monex.pdf

    This issue has many steps to it. I recommend that anybody taking it on should break it up into multiple commits and even multiple PRs.

  2. briansmith commented on Nov 29, 2016

    @briansmith
    OwnerAuthor

    Notes on the proof for pq == 0 mod n being equivalent to p * q == n. @ranweiler did some math in #360 (comment).

    Also, it may be easier to think about the relationship between p and q if we realize that q < p < 2q since q < p and they have the same bit length, so we have 0 < q < p < 2q < n.

  3. samscott89 commented on Nov 29, 2016

    @samscott89
    Contributor

    Sorry, I posted a comment on this and realised my logic was the wrong way around. Having a re-think. (Updated proof below)

  4. samscott89 commented on Nov 29, 2016

    @samscott89
    Contributor

    Unfortunately, I don't think @ranweiler's proof is correct. It assumes that max(⌈lg(q)⌉, ⌈lg(p)⌉) ≤ lg(n)/2, which means that both of log(p), log(q) ≤ log(n)/2 = log(√n) and therefore both p,q are ≤√n which is not possible (unless p=q).

    Suppose kn = pq for some k. Then log(nk) = log(pq) = log(p) + log(q).

    Since q < p and ⌈log p⌉ = ⌈log q⌉, using x ≤ ⌈x⌉ < x+1 we get:

    ⌈log n⌉ + log(k) < log(n) + log(k) + 1 = log(nk) + 1 
                     = log(pq) + 1
                     < log(p^2) + 1 = 2log(p) +1
                     ≤ 2⌈log p⌉ + 1
    

    ⌈log p⌉ will either be precisely ⌈log n⌉/2 (when ⌈log n⌉ is even), or ⌈log n⌉/2 + 1/2. In the former case, we get log(k) < 1 so k=0. In the latter, log(k) < 1.5 and therefore k=2 is a possible solution. Therefore, you need to check that both p or q are odd if n has an odd bit length.

    For example (using sage):

    p = next_prime(2^1024.5)
    p = 2*p
    q = next_prime(p)
    n = p*q
    n_prime = p*q/2
    print int(p).bit_length(), int(q).bit_length(), int(n).bit_length(), int(n_prime).bit_length()

    Outputs:
    1026 1026 2052 2051
    So p*q = 2n = 0 (mod n) but the bit lengths appear to agree.

  5. ranweiler commented on Nov 30, 2016

    @ranweiler
    Contributor

    @samscott89, nice catch re my proof! You are right: I made the log-ceiling bounding hypothesis too strong, and so the result only applies to equal p, q (which are excluded by the choice of p, q), so the result is trivial.

    Unless I'm mistaken, the line of argument is otherwise valid, though, so we can salvage something from it. If, in that earlier proof, we instead assume m := max(⌈lg(q)⌉, ⌈lg(p)⌉) ≤ ⌈lg(n)⌉/2, we can get a non-trivial result like so:

    Proof. In the context of the earlier proof, suppose the log-ceiling bound is weakened to m ≤ ⌈lg n⌉ / 2. Then the 2nd chain of inequalities yields lg n + lg k ≤ ⌈lg n⌉. But for any x, we have ⌈x⌉ < x + 1, so we can derive:

    lg n + lg k ≤ ⌈lg n⌉
                < 1 + lg n 
    

    Whence lg k < 1. But k is a positive integer, and since lg 2 = 1 and lg is an increasing function, we must have k < 2, and so k = 1, as required.

    Comment. @samscott89, how's that modified argument look to you? I also wrote a little Python script and checked that there are interesting solutions (where p != q).

  6. samscott89 commented on Nov 30, 2016

    @samscott89
    Contributor

    I think the bound might still be too strong based on the implementation of the length checking.
    I think the length check is actually m = ⌈⌈lg n⌉ / 2⌉ (check out the half_rounded_up function used: https://github.com/briansmith/ring/pull/360/files#diff-4c2d359d551a3a9316f813f4fd14f845R36).

    In the example I gave, I generated p,q which were both 1026 bits, and an n which was 2051 bits, but p*q=2*n. So n.bit_length().half_rounded_up() would output 1026 so it would look like p,q are valid. Hence needing to check the extra factor of 2.

  7. briansmith commented on Nov 30, 2016

    @briansmith
    OwnerAuthor

    A simple way to do the check, still requiring only (Montgomery) Modular Mutliplication and not "regular" non-modular multiplication would be to verify that p * q == n (mod n+2), right?

    It just happens to be inconvenient to need the bignum +2 operation.

  8. samscott89 commented on Nov 30, 2016

    @samscott89
    Contributor

    I guess that would do it. Although looking at the code again, it already makes sure that p,q are odd, so you don't actually need any additional checks.

  9. briansmith commented on Nov 30, 2016

    @briansmith
    OwnerAuthor

    I guess that would do it. Although looking at the code again, it already makes sure that p,q are odd, so you don't actually need any additional checks.

    Yes, it is definitely better to use the modular arithmetic (mod n) instead of (mod n+2) as long as we're confident in the proof for the (mod n) arithmetic.

  10. samscott89 commented on Nov 30, 2016

    @samscott89
    Contributor

    I'm very confident in my above proof. That shows that if p*q = k*n then k can be at most 2, but if p and q are odd then all that remains is pq=n.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions