You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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 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.
@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).
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.
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.
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.
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.
Activity
briansmith commentedon Nov 29, 2016
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 aroundBN_div) for that, but we're hoping to removeBN_divcompletely. 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.pdfThis issue has many steps to it. I recommend that anybody taking it on should break it up into multiple commits and even multiple PRs.
briansmith commentedon Nov 29, 2016
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.
samscott89 commentedon Nov 29, 2016
Sorry, I posted a comment on this and realised my logic was the wrong way around. Having a re-think. (Updated proof below)
samscott89 commentedon Nov 29, 2016
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 oflog(p), log(q) ≤ log(n)/2 = log(√n)and therefore both p,q are≤√nwhich is not possible (unless p=q).Suppose
kn = pqfor somek. Thenlog(nk) = log(pq) = log(p) + log(q).Since
q < pand⌈log p⌉ = ⌈log q⌉, usingx ≤ ⌈x⌉ < x+1we get:⌈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 getlog(k) < 1sok=0. In the latter,log(k) < 1.5and thereforek=2is a possible solution. Therefore, you need to check that bothporqare odd ifnhas an odd bit length.For example (using sage):
Outputs:
1026 1026 2052 2051So
p*q = 2n = 0 (mod n)but the bit lengths appear to agree.ranweiler commentedon Nov 30, 2016
@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 ofp,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 yieldslg n + lg k ≤ ⌈lg n⌉. But for anyx, we have⌈x⌉ < x + 1, so we can derive:Whence
lg k < 1. Butkis a positive integer, and sincelg 2 = 1andlgis an increasing function, we must havek < 2, and sok = 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).samscott89 commentedon Nov 30, 2016
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 thehalf_rounded_upfunction used: https://github.com/briansmith/ring/pull/360/files#diff-4c2d359d551a3a9316f813f4fd14f845R36).In the example I gave, I generated
p,qwhich were both 1026 bits, and annwhich was 2051 bits, butp*q=2*n. Son.bit_length().half_rounded_up()would output 1026 so it would look likep,qare valid. Hence needing to check the extra factor of 2.briansmith commentedon Nov 30, 2016
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.
samscott89 commentedon Nov 30, 2016
I guess that would do it. Although looking at the code again, it already makes sure that
p,qare odd, so you don't actually need any additional checks.briansmith commentedon Nov 30, 2016
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.
samscott89 commentedon Nov 30, 2016
I'm very confident in my above proof. That shows that if
p*q = k*nthenkcan be at most 2, but if p and q are odd then all that remains ispq=n.