Skip to content

Replace BN_rand_range C implementation with Rust - #254

Closed
djudd wants to merge 3 commits into
briansmith:masterfrom
djudd:oxidize-bn-rand-range
Closed

djudd wants to merge 3 commits into
briansmith:masterfrom
djudd:oxidize-bn-rand-range

Conversation

@djudd

@djudd djudd commented Jul 19, 2016 •

Copy link
Copy Markdown

Fixes #239

Comment thread src/limb.rs Outdated

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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:

  1. Generate an array of bytes.
  2. Convert that array of bytes into limbs, viewing it as big-endian, to see if it is in range.
  3. Store the big-endian array of bytes in ec::PrivateKey.
  4. During ECDH (and eventually Signing), convert the big-endian bytes into an array of Limbs.
  5. Then, convert the array of Limbs into a little-endian array of bytes.
  6. 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.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

@briansmith

Copy link
Copy Markdown
Owner

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.

@djudd
djudd force-pushed the oxidize-bn-rand-range branch from 5359483 to a442ee5 Compare July 19, 2016 01:43
@djudd

djudd commented Jul 19, 2016

Copy link
Copy Markdown
Author

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.

Comment thread src/rsa.rs Outdated

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

@briansmith

Copy link
Copy Markdown
Owner

I added some more comments. Nothing definitive, but just my gut reaction. LMK what you think.

@coveralls

coveralls commented Jul 19, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 88.873% when pulling a442ee55bcf9cb5d4d2db9a077ff8b134384b656 on djudd:oxidize-bn-rand-range into 868fad9 on briansmith:master.

@coveralls

coveralls commented Jul 19, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 88.873% when pulling a442ee55bcf9cb5d4d2db9a077ff8b134384b656 on djudd:oxidize-bn-rand-range into 868fad9 on briansmith:master.

@djudd
djudd force-pushed the oxidize-bn-rand-range branch from a442ee5 to 1133acb Compare July 20, 2016 02:11
@djudd

djudd commented Jul 20, 2016

Copy link
Copy Markdown
Author

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

Comment thread crypto/bn/random.c Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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

@coveralls

coveralls commented Jul 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.3%) to 88.487% when pulling d3e13e75c2d192e6d2ffb8eab7b359efe92fe0bc on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master.

Comment thread src/limb.rs Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

NIT: No blank line here.

@coveralls

coveralls commented Jul 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.3%) to 88.487% when pulling d3e13e75c2d192e6d2ffb8eab7b359efe92fe0bc on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master.

@coveralls

coveralls commented Jul 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.4%) to 88.448% when pulling dce450fc3d2fb7b3180842dc45d840eb1696e7b7 on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master.

@briansmith

Copy link
Copy Markdown
Owner

I have now done the BoringSSL merge so that BN_BLINDING is now the only users of BN_rand_range. Also, the only user of BN_rand is now TestBN2BinPadded, which means we're not far away from nuking it; for now we'll just have to make move it to bn_test_lib.c. Note that I deleted bn_test_lib.c during the merge as the contents were no longer being used, so when you merge your PR on top of the current master, you'll have to add it back (basically revert most of that commit).

It will be a day or two before I can look at this again.

@coveralls

coveralls commented Jul 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 88.621% when pulling f8e1b4c77a15b41e54d396db18830736cf52a2c4 on djudd:oxidize-bn-rand-range into 2c3a95e on briansmith:master.

@briansmith

Copy link
Copy Markdown
Owner

I'm curious about whetehr the special case logic for the case where range is 0b100... might be really important for RSA blinding? Because, n is the product of two primes and it seems like values of n of the form 0b100 might be really common? Do you have any thoughts about that?

Sorry about the delay. I hope to get to this during the week.

@djudd

djudd commented Jul 24, 2016

Copy link
Copy Markdown
Author

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.

Comment thread src/limb.rs Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

It seems like the various refactorings have left us with something that can be further simplified:

if self.are_limbs_within(&dest) { 
    return Ok(());
}
```.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

3 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.3%) to 90.275% when pulling 43c1d666f292ab4aca6be7d7a7b7036f66ece1a0 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

Comment thread crypto/bn/bn_test.cc Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

We should keep the ones where top == 0 and bottom == 0 until we remove BN_rand.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It seems a little silly to me to test a test library that's going away soonish, but your call.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok, fair enough.

@briansmith

Copy link
Copy Markdown
Owner

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:

/// Copyright 2016 David Judd
/// Copyright 2016 Brian Smith
///
/// <normal license>

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.
@djudd
djudd force-pushed the oxidize-bn-rand-range branch from 43c1d66 to f8ded5c Compare August 21, 2016 05:20
@djudd

djudd commented Aug 21, 2016

Copy link
Copy Markdown
Author

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.

David Judd added 2 commits August 20, 2016 22:56
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.
@djudd
djudd force-pushed the oxidize-bn-rand-range branch from f8ded5c to 15871c9 Compare August 21, 2016 06:00
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 90.257% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

3 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 90.257% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 90.257% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 90.257% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@coveralls

coveralls commented Aug 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 90.257% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 90.257% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@coveralls

coveralls commented Aug 21, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-1.07%) to 88.95% when pulling 15871c9 on djudd:oxidize-bn-rand-range into e7cd8ee on briansmith:master.

@briansmith

Copy link
Copy Markdown
Owner

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

@djudd

djudd commented Aug 22, 2016

Copy link
Copy Markdown
Author

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!

@djudd

djudd commented Aug 22, 2016

Copy link
Copy Markdown
Author

BTW, microbenchmark results suggest the 0b100 special case is worth keeping. Sampling to a bound of [0, 0, 0, 0, 0, 0, 0, 16] takes 7 microseconds with it, vs 10 without, on my Macbook. And that's with first-pass code and a constant-time subtraction. (Not the result I was rooting for :/).

@briansmith

Copy link
Copy Markdown
Owner

Closed via:

9cf1e16 Replace scalar_is_in_range with direct use of Range.
1bc7c7f Adapt the ECDH key generation tests to test Range::sample_into_limbs.
36a91ba Expand Range::are_limbs_within tests.
bb52036 Simplify conversions from masks => bool => Err to masks => Err.
01fea0f Expand and clarify comments regarding origin of sample_into_limbs.
ee97b43 Factor out most-significant-limb masking and test it.
d25ceb6 Make RAND_bytes test-only.
f96d640 Make BN_rand test-only.
0a8efed Use Limb::max_value() instead of defining MAX_LIMB.
ef7a07c Describe limb ordering in just one place.
7cbe5d7 Replace BN_rand_range C implementation with call to Rust.

Thanks for your work on this!

@briansmith briansmith closed this Aug 22, 2016
@djudd

djudd commented Aug 23, 2016

Copy link
Copy Markdown
Author

Thanks for your time and patience in helping me get it right!

@briansmith

Copy link
Copy Markdown
Owner

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.

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