Skip to content

Oxidize/restore special-case handling for 0b100... range bounds - #283

Closed
djudd wants to merge 3 commits into
briansmith:masterfrom
djudd:rsa-blinding-0b100
Closed

djudd wants to merge 3 commits into
briansmith:masterfrom
djudd:rsa-blinding-0b100

Conversation

@djudd

@djudd djudd commented Aug 24, 2016 •

Copy link
Copy Markdown

... in RSA blinding keygen, lost when OpenSSL code was oxidized

There are some TODOs in the code regarding as, but also a larger one, where I want to agree on a strategy before moving forward.

I pulled the 0b100 check and mask calculation for Range sampling into the Range constructor, where it's easier to test and, I think, clearer. However, this means that the EC code which uses Range just for the are_limbs_within check now is unnecessarily initializing those values too. Even though it skips the complex path, I'm not all that happy with it. Two options:

  • Remove any dependency on EC code on Range and give up on being DRY;
  • Use Range code for EC private key generation too (as in an earlier iteration of my prior PR).

In the former case, we'd be able to move Range and everything associated out into rsa land.

In the latter case, we'll still in some sense be making the EC keygen code more complex than it is now, but using a different constructor with explicit flags should make it fairly clear that the complex paths (3 * 0b100 reduction and mask calculation) are not used.

Thoughts?

I am the sole copyright holder of all added code, and agree to license it per the terms in each file changed.

Comment thread src/ec/suite_b/ops/ops.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.

Are you sure? Doesn't let mut r = *a copy (via Copy) a?

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.

You're right. I was confused and thinking of *a as if it was taking a raw pointer, but of course it's not.

@briansmith

Copy link
Copy Markdown
Owner

I pulled the 0b100 check and mask calculation for Range sampling into the Range constructor, where it's easier to test and, I think, clearer. However, this means that the EC code which uses Range just for the are_limbs_within check now is unnecessarily initializing those values too. Even though it skips the complex path, I'm not all that happy with it. Two options:

  1. Remove any dependency on EC code on Range and give up on being DRY;
  2. Use Range code for EC private key generation too (as in an earlier iteration of my prior PR).
    In the former case, we'd be able to move Range and everything associated out into rsa land.

I prefer #1. I like the DRY principle but IMO it is more important that the ECC code be as clear and as small as possible. I eventually want to literally prove the ECC code is correct.

I am the sole copyright holder of all added code, and agree to license it per the terms in each file changed.

No need to put this in the PR comments, just in the commit messages.

@djudd
djudd force-pushed the rsa-blinding-0b100 branch from 1c339ac to 7490a0b Compare August 28, 2016 18:23
@djudd

djudd commented Aug 28, 2016

Copy link
Copy Markdown
Author

@briansmith I believe this is ready for re-review.

Comment thread src/ec/suite_b/ops/ops.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.

Awesome!

@djudd
djudd force-pushed the rsa-blinding-0b100 branch from 69f8c63 to ffbac22 Compare September 17, 2016 04:38
@djudd

djudd commented Sep 17, 2016

Copy link
Copy Markdown
Author

Ready for re-review, unless you'd like me to squash the new (/ all) commits first.

I'll do that before I merge in any case, and add the licensing agreement.

@coveralls

coveralls commented Sep 17, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 91.664% when pulling 5abea9c on djudd:rsa-blinding-0b100 into 8f14460 on briansmith:master.

2 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 91.664% when pulling 5abea9c on djudd:rsa-blinding-0b100 into 8f14460 on briansmith:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 91.664% when pulling 5abea9c on djudd:rsa-blinding-0b100 into 8f14460 on briansmith:master.

@coveralls

coveralls commented Sep 17, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 91.664% when pulling 5abea9c on djudd:rsa-blinding-0b100 into 8f14460 on briansmith:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.05%) to 91.664% when pulling 5abea9c on djudd:rsa-blinding-0b100 into 8f14460 on briansmith:master.

@briansmith briansmith left a 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.

Please always squash all changes so that the commits are exactly what you want merged. I'll deal with interdiffing myself as needed. Or, at least, make sure to include the license blurb at the end of all commits so that I can do the squashing myself.

Earlier you said:

If we always copy into the extended buffers, that actually complicates our Range code, because it requires us to decide inside Range whether we are going to apply the mask to the most-significant limb or the next-most-significant limb. (Or pass in two masks, or something.)

But, I think maybe I'm not understanding what you mean or maybe I didn't make my idea clear. Please see https://github.com/briansmith/ring/commits/rsa-blinding-0b100, particularly the commit I added, "Simplify set_to_rand_mod." Is there something wrong with doing it this way that avoids the code duplication and branching? It's something that normally doesn't matter, but as a general rule in ring I'm trying to eliminate branches, not for constant-timedness, but also to simplify the analysis of the code. So, if this kind of simplification works I'd like to do it this way. But, again, maybe I'm missing something.

Finally, when I was making that edit I realized that the ordering of the output parameter dest relative to the other parameters of set_to_rand_mod() doesn't match the convention in the rest of the code. I see we have some conflicting conventions:

  • The BoringSSL C/C++ code puts output parameters first.
  • Rust code seems to put output parameters last.
  • The rng parameter is supposed to be last.

I guess the rule we should use is this:

  • C/C++ code does whatever Google wants, which means output parameters first and rand parameter last.
  • Rust code should have input parameters first, then rng, then output parameters.

I kind of just made this up but it seems to make the most sense. WDYT?

Finally, usually in ring we name output parameters with an out_ prefix or we use the name out if there is just one parameter. So, I think dest should be out.

Earlier today I made it so you don't have to use #![allow(unsafe_code)] any more; unsafe { } is allowed everywhere.

Comment thread src/rsa/blinding.rs Outdated
if most_sig >> (LIMB_BITS - 3) == 0b100 {
SamplingParams {
most_sig_limb_mask: 1, // effectively a carry into a new, more-significant limb
most_sig_limb_mask: 1, // effectively a carry over into a new, more-significant limb

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: I think the added word "over" is redundant.

Comment thread src/rsa/blinding.rs Outdated

limbs_reduce_once_constant_time(dest, self.max_exclusive);
if self.are_limbs_within(&dest) {
// `dest` was in [max, 2*max)

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 should be (max, 2*max), right? If the value were max then are_limbs_within(&dest) would return false because max - max == 0, which isn't allowed.

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.

Yes, good catch

Comment thread src/rsa/blinding.rs Outdated

limbs_reduce_once_constant_time(dest, self.max_exclusive);
if self.are_limbs_within(&dest) {
// `dest` was in [2*max, 3*max)

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.

(2*max, 3*max), right?

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.

Should we change are_limbs_with() to work like this?:

if limbs_less_than_limbs_constant_time(limbs, self.max_exclusive) != LimbMask::True {
    return false;
}
return limbs_are_zero_constant_time(limbs) == LimbMask::False;

In particular this would reduce the number of calls to limbs_are_zero_constant_time for the cases where we have to do one or more reductions.

Comment thread src/rsa/blinding.rs Outdated
return Ok(());
}

// `dest` was in [3*max, 2**(n+1)) or zero

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 is either in that subrange, or it was zero, or it was max or 2*max, right?

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 suggest that you append ". Continue looping to generate a new random value." Especially because the period is missing, it looks like there might be some code unintentionally missing here.

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.

Makes sense. I'd actually already gone back and forth on whether expanding this comment was redundant or not

Comment thread src/rsa/blinding.rs Outdated
}

impl <'a> Range<'a> {

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.

Remove extra blank line.

Comment thread src/rsa/blinding.rs Outdated
/// and stores it into `dest`.
fn set_to_rand_mod(dest: &mut [Limb], max_exclusive: &[Limb], rng: &rand::SecureRandom)
-> Result<(), error::Unspecified> {

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.

Remove extra blank line.

Comment thread src/rsa/blinding.rs Outdated
use {c, core, rand, error, rsa};
use limb::*;

const RSA_KEY_MAX_LIMBS: usize = rsa::RSA_PUBLIC_KEY_MODULUS_BITS_MAX / LIMB_BITS;

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.

When re-reviewing this, I realized that this should be defined next to RSA_PUBLIC_KEY_MODULUS_BITS_MAX and we should add a comment explaining that we assume that RSA_PUBLIC_KEY_MODULUS_BITS_MAX is always a multiple of LIMB_BITS.

Preferably, use the rounding calculation:

(rsa::RSA_PUBLIC_KEY_MODULUS_BITS_MAX + LIMB_BITS - 1) / LIMB_BITS.

Comment thread src/rsa/blinding.rs Outdated
return Ok(());
}

// `dest` was in [3*max, 2**(n+1)) or zero

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 suggest that you append ". Continue looping to generate a new random value." Especially because the period is missing, it looks like there might be some code unintentionally missing here.

Comment thread src/rsa/blinding.rs Outdated

/// Chooses a positive integer less than `max_exclusive` uniformly at random
/// and stores it into `dest`.
fn set_to_rand_mod(dest: &mut [Limb], max_exclusive: &[Limb], rng: &rand::SecureRandom)

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.

Double-check line lengths are <= 80 characters. This one, at least, is too long.

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.

Fixed by cherry-picking in your rewrite of the function.

Comment thread src/rsa/blinding.rs Outdated
}
}

#[allow(unsafe_code)]

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.

You can drop this #[allow(unsafe_code)] now.

Comment thread src/rsa/blinding.rs Outdated
}

#[test]
#[allow(unused_results)]

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.

Let's not use allow(unused_results), even in tests. Instead we can use the let _ = pattern.

Comment thread src/rsa/blinding.rs
use limb::*;

#[test]
fn test_select_sampling_params() {

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.

All of these test cases are one limb long, so they are not verifying that we're looking at the high limb instead of, say, the low limb.

Comment thread src/rsa/blinding.rs Outdated

assert_normal!(i, l);
assert_normal!(i, l | 1);
assert_normal!(i, l | l >> 3);

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 don't understand the significance of these test cases, especially the last one. Add some comments to show why these values are important to test.

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.

The last is testing that we don't treat 0b001001, 0b0001001, etc like we do 0b1001 and 0b01001. But every case I pick from the wide set of non-0b100 numbers feels a bit arbitrary to me; if you find these redundant, I'm happy to remove them.

Comment thread src/rsa/blinding.rs Outdated
use super::{RSA_KEY_MAX_LIMBS, Range, SamplingParams, with_extended_buffers};

#[bench]
fn bench_sample_into_limbs_simple(b: &mut bench::Bencher) {

@briansmith briansmith Sep 19, 2016 •

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.

Some comments about the purpose of this benchmark would be useful. In particular, why does the benchmark fill in its own SamplingParams and Range instead of having the real library code calculate them? What is meant by "simple" in the function name?

Comment thread src/rsa/blinding.rs Outdated
}

#[bench]
fn bench_sample_into_limbs_reduce_no_copy(b: &mut bench::Bencher) {

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.

Again, please add a comment about the purpose of this benchmark.

Comment thread src/rsa/blinding.rs Outdated
#[bench]
fn bench_sample_into_limbs_simple(b: &mut bench::Bencher) {
let mut max: [Limb; RSA_KEY_MAX_LIMBS] = [0; RSA_KEY_MAX_LIMBS];
max[RSA_KEY_MAX_LIMBS - 1] = 1 << (LIMB_BITS - 1);

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.

Add a comment such as "construct a value such that the high limb starts with 0b100...

Comment thread src/rsa/blinding.rs Outdated
#[bench]
fn bench_sample_into_limbs_reduce_no_copy(b: &mut bench::Bencher) {
let mut max: [Limb; RSA_KEY_MAX_LIMBS] = [0; RSA_KEY_MAX_LIMBS];
max[RSA_KEY_MAX_LIMBS - 1] = 1 << (LIMB_BITS - 2);

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.

Please add a comment and an assertion such as assert_eq!(max[RSA_KEY_MAX_LIMBS - 1] >> LIMB_BITS - 3, 0b100) to indicate the expected form of max.

Comment thread src/rsa/blinding.rs Outdated
#[bench]
fn bench_sample_into_limbs_reduce_with_copy(b: &mut bench::Bencher) {
let mut max: [Limb; RSA_KEY_MAX_LIMBS] = [0; RSA_KEY_MAX_LIMBS];
max[RSA_KEY_MAX_LIMBS - 1] = 1 << (LIMB_BITS - 1);

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.

Please add a comment and an assertion such as assert_eq!(max[RSA_KEY_MAX_LIMBS - 1] >> LIMB_BITS - 3, 0b100) to indicate the expected form of max.

@briansmith

Copy link
Copy Markdown
Owner

Rust code should have input parameters first, then rng, then output parameters.

Just to follow-up on this: I went and looked and we're already not as consistent as I was hoping. In particular, it's more the case that we put the modulus/max parameter and rng parameter last, no matter what. So, it's fine to continue to do that in this PR.

@djudd

djudd commented Sep 20, 2016

Copy link
Copy Markdown
Author

I'll squash the changes before requesting re-review, and I cherry-picked in your commit (I hadn't thought of your extra_limb solution to the branch, or hadn't understood it if you had suggested it.)

Re line length comments & some others, should I be running rustfmt on the files I'm touching? I recall a comment on my first PR that said this project follows BoringSSL style at least in some respects, not rustfmt style, but I've seen some more references to rustfmt going by recently.

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 rsa-blinding-0b100 branch 2 times, most recently from 0b11350 to b794e50 Compare September 20, 2016 05:13
@djudd

djudd commented Sep 20, 2016

Copy link
Copy Markdown
Author

Ready for re-review, modulo possible rustfmt. (I went ahead and removed some of the probably-redundant test cases that I'd asked about.)

Oxidize/restore special-case handling for `0b100...` range bounds,
lost when OpenSSL code was oxidized, and move RSA blinding param
generation out of limb.rs and into its own module

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 rsa-blinding-0b100 branch from b794e50 to 7626a91 Compare September 20, 2016 05:16
@briansmith

Copy link
Copy Markdown
Owner

We're testing 0b11..., 0b101..., and 0b111..., any of which, per above, I'm happy to remove if you find them redundant, since I don't have any particular justification. Would it make more sense to just test 0b11, 0b101, and 0b001... and drop the last case here and last two cases above?

No, it's not that I want you to remove the tests. More, it would be nice to find a way to make the tests more obvious, by naming some of the calculated values with clear names and/or adding comments.

@briansmith

Copy link
Copy Markdown
Owner

Re line length comments & some others, should I be running rustfmt on the files I'm touching? I recall a comment on my first PR that said this project follows BoringSSL style at least in some respects, not rustfmt style, but I've seen some more references to rustfmt going by recently.

I have a rustfmt configuration file that makes things more like the BoringSSL style. Unfortunately, rustfmt likes to put function arguments each on their own line, so I end up having to reformat some things after running rustfmt. So, it isn't quite 100% formatted according to rustfmt but it might be helpful to use it.

@coveralls

coveralls commented Sep 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 91.616% when pulling 7626a91 on djudd:rsa-blinding-0b100 into 7d18737 on briansmith:master.

2 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 91.616% when pulling 7626a91 on djudd:rsa-blinding-0b100 into 7d18737 on briansmith:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 91.616% when pulling 7626a91 on djudd:rsa-blinding-0b100 into 7d18737 on briansmith:master.

@coveralls

coveralls commented Sep 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 91.616% when pulling 7626a91 on djudd:rsa-blinding-0b100 into 7d18737 on briansmith:master.

@coveralls

coveralls commented Sep 20, 2016 •

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 7626a91 on djudd:rsa-blinding-0b100 into ** on briansmith:master**.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.04%) to 91.616% when pulling 7626a91 on djudd:rsa-blinding-0b100 into 7d18737 on briansmith:master.

@djudd

djudd commented Oct 8, 2016

Copy link
Copy Markdown
Author

Bump - AFAIK this is ready (apologies if I missed some comment).

@briansmith

Copy link
Copy Markdown
Owner

Yes, I've already reviewed it earlier this week and I'm planning to push it with minor changes sometime this weekend.

@djudd

djudd commented Oct 8, 2016

Copy link
Copy Markdown
Author

Great. No hurry, just wasn't sure of the status. Thanks!

@briansmith

Copy link
Copy Markdown
Owner

Please see the tweaks at https://github.com/briansmith/ring/commits/rsa-blinding-0b100. The first commit I stacked on top of yours is just reordering your code so that the code is in top-down(-ish) order. The second commit is just a bunch of tweaks. LMK if I created any problem.

@djudd

djudd commented Nov 18, 2016

Copy link
Copy Markdown
Author

Aside from a typo in a comment, looks good. Thanks!

On Thu, Nov 17, 2016 at 2:19 PM, Brian Smith [email protected]
wrote:

Please see the tweaks at https://github.com/briansmith/
ring/commits/rsa-blinding-0b100. The first commit I stacked on top of
yours is just reordering your code so that the code is in top-down(-ish)
order. The second commit is just a bunch of tweaks. LMK if I created any
problem.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#283 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAY-Jk0bgXKNwQ39MObNlZp-s18RXOdnks5q_NMNgaJpZM4JsIz9
.

@briansmith

Copy link
Copy Markdown
Owner

Thanks for doing this. This landed with some tweaks as f014d94. (I corrected the typo you pointed out.)

I also filed #353 to improve the performance further.

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.

4 participants