Repository navigation
Conversation
There was a problem hiding this comment.
I think this has to be constant-time, and AFAICT it is. Am I right? If so, should that be in the name and/or doc comment?
There was a problem hiding this comment.
We already have a very similar function named parse_big_endian_value. I think we should unify these together. If the existing function isn't constant time, we can use this new definition.
See also the discussion in #248. Here's what we do now:
- Generate an array of bytes.
- Convert that array of bytes into limbs, viewing it as big-endian, to see if it is in range.
- Store the big-endian array of bytes in
ec::PrivateKey. - During ECDH (and eventually Signing), convert the big-endian bytes into an array of
Limbs. - Then, convert the array of
Limbs into a little-endian array of bytes. - Use that little-endian array of bytes to drive the scalar multiplication.
Meanwhile, for X25519/Ed25519 keys, we store the little-endian bytes in ec::PrivateKey and then use the little-endian bytes to drive the scalar multiplication. Much simpler.
So, I'd like to change the code for Suite B curves to work like the Curve25519 code. If/when we do that, we won't need this set_limbs_from_big_endian_bytes function for ECC, which seems good.
There was a problem hiding this comment.
parse_big_endian_value. I think we should unify these together.
Actually, I guess the point of this function to to be more clearly constant time, which is presumably why there were two functions to start with. So, it is OK if we don't unify them together. Again, see my other comment; I'm kind of hoping we can just avoid this function completely.
|
Thanks for submitting. I added a couple of comments about where to move the now-test-only code. Looking at the second commit, I can see that this will take me some time to review. |
5359483 to
a442ee5
Compare
|
Thanks for taking a look! I understand it will take some time to look over, it ended up being bigger than I was expecting. Just rebased to add licensing comments to the commit messages. |
There was a problem hiding this comment.
In general I went with stack-allocated buffers that are as big as the maximum key we want to use, even where using the heap might be more natural. Tracking down those maximums took some inference, and it's not obvious they'll never change, so I'm not sure this is the right strategy.
|
I added some more comments. Nothing definitive, but just my gut reaction. LMK what you think. |
|
Coverage increased (+0.1%) to 88.873% when pulling a442ee55bcf9cb5d4d2db9a077ff8b134384b656 on djudd:oxidize-bn-rand-range into 868fad9 on briansmith:master. |
|
Coverage increased (+0.1%) to 88.873% when pulling a442ee55bcf9cb5d4d2db9a077ff8b134384b656 on djudd:oxidize-bn-rand-range into 868fad9 on briansmith:master. |
a442ee5 to
1133acb
Compare
|
@briansmith I pushed a revised version which follows the strategy you suggested. The first commit re-implements the guts of BN_rand_range in Rust, copying the code from EC key generation. The second commit switches EC key generation to use the code path created in the first commit. FYI I'll be traveling & then back at my day job starting tomorrow so expect the pace of work & responses to slow down. |
There was a problem hiding this comment.
This needs to be a call to bn_correct_top. BIGNUMs always need to have the minimal value of top so that the top-level limb is never zero. (This destroys the constant-time property of BIGNUM, which is why we're getting rid of it. But certain other code depends on it, so whenever we have a BIGNUM we have to maintain that invariant.)
|
Coverage decreased (-0.3%) to 88.487% when pulling d3e13e75c2d192e6d2ffb8eab7b359efe92fe0bc on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master. |
|
Coverage decreased (-0.3%) to 88.487% when pulling d3e13e75c2d192e6d2ffb8eab7b359efe92fe0bc on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master. |
|
Coverage decreased (-0.4%) to 88.448% when pulling dce450fc3d2fb7b3180842dc45d840eb1696e7b7 on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master. |
|
I have now done the BoringSSL merge so that It will be a day or two before I can look at this again. |
|
Coverage decreased (-0.2%) to 88.621% when pulling f8e1b4c77a15b41e54d396db18830736cf52a2c4 on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master. |
|
I'm curious about whetehr the special case logic for the case where Sorry about the delay. I hope to get to this during the week. |
|
As far as I can tell, the purpose of that logic is to increase the odds that any given iteration finds a value in the range. The comment says it makes the probability of succeeding on a given iteration >= 75%. With the current Rust logic, by contrast, the probability of success is >= 50% (since we know we succeed if the first bit is zero). I think that still makes the probability of failing for 100 iterations in a row sufficiently low (1 / 2^100), so it's a question of performance. If we think this code is performance critical, I could see a case for introducing that extra complication. It seems like that should come with benchmarks (since we're adding subtraction, no guarantee it's actually faster on average) and possibly a separate pull request, though. BTW I don't see how that deleted logic avoids the bias towards small values that you mention in your comment on the NSA guide's alternative method. |
There was a problem hiding this comment.
It seems like the various refactorings have left us with something that can be further simplified:
if self.are_limbs_within(&dest) {
return Ok(());
}
```.
|
Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master. |
3 similar comments
|
Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master. |
|
Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master. |
|
Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master. |
There was a problem hiding this comment.
We should keep the ones where top == 0 and bottom == 0 until we remove BN_rand.
There was a problem hiding this comment.
It seems a little silly to me to test a test library that's going away soonish, but your call.
There was a problem hiding this comment.
I agree, it is unfortunate. But, the test is already there so it's just a matter of deleting less of it. The test for BN_rand partially establishes the validity of the BN_bn2bin_padded test. Ideally we'd rewrite the BN_bn2bin_padded test "soonish" but I kind of suspect it's likely to stay like it is for a while as it's "just a test" and so likely not very urgent to everybody. Especially since we've already remove BN_rand_range and BN_rand from the main lib, which is the main win.
|
Please see https://github.com/briansmith/ring/commits/oxidize-bn-rand-range. This attempts to address all my review comments plus a few more, plus factors out the masking of the top limb and adds tests for it. Please add this to the top of the new Rust source file you added: I didn't change the second commit; please add back the BN_rand tests for the (0 ,0) case. Also, please add the "I agree..." statement to the end of that commit's commit message. Also, please verify that you are the sole copyright holder for your contribution. Thanks! |
The random key generation code is adapted from that used for EC, but temporarily duplicated, with the following changes: - A Range struct wrapping Limb slices used as ranges - Instead of converting bytes to limbs, generate random bytes directly into the Limb slice, temporarily casting it to u8 - Add support for ranges with zero bits in the most significant limb by masking those bits in the generated random bytes - Add support for ranges with a small non-zero lower bound Also remove the `min_inclusive` parameter from BN_rand_range_ex, since in practice it is always 1. I agree to license my contributions to each file under the terms given at the top of each file I changed.
43c1d66 to
f8ded5c
Compare
|
Addressed some of your review comments before seeing your link above, whoops. Shouldn't matter, but I'm not sure what you want me to do with the commits in your version of the branch - pull into mine, but leave as separate commits? I'm the sole copyright holder for everything I've committed, with the exception of code copied and pasted from within ring and presumably copyrighted by you. |
It was already only used in tests, so only compile it in tests, remove tests for the function itself, and remove now-unused modes I agree to license my contributions to each file under the terms given at the top of each file I changed.
I agree to license my contributions to each file under the terms given at the top of each file I changed.
f8ded5c to
15871c9
Compare
3 similar comments
1 similar comment
|
@djudd I rebased my branch on top of your updated branch and folded some of the commits I added into them. I left some of my commits as separate commits. Could you please take a look at them and let me know if anything needs improving?: https://github.com/briansmith/ring/commits/oxidize-bn-rand-range Thanks! |
|
I left a couple of comments but otherwise looks good. I was thinking about adapting the ECDH tests myself but didn't because on that iteration of the PR they were testing the same codepath. Now that these are separate codepaths, definitely good to have them! |
|
BTW, microbenchmark results suggest the 0b100 special case is worth keeping. Sampling to a bound of |
|
Closed via: 9cf1e16 Replace scalar_is_in_range with direct use of Thanks for your work on this! |
|
Thanks for your time and patience in helping me get it right! |
|
Thanks again! Please submit your other PR, for dealing with the 0b100... case, when it is ready. It will probably be ~1 week before I'll be able to look at it because I'm working on clearing a backlog of stuff. |
Fixes #239