Skip to content

WIP: Reuse BN_BLINDING multiple times in RSA signing - #258

Closed
ranweiler wants to merge 5 commits into
briansmith:masterfrom
ranweiler:issue234-rsa-blinding-reuse
Closed

ranweiler wants to merge 5 commits into
briansmith:masterfrom
ranweiler:issue234-rsa-blinding-reuse

Conversation

@ranweiler

@ranweiler ranweiler commented Jul 25, 2016 •

Copy link
Copy Markdown
Contributor

Closes #234.

Todo:

  • Save a Mutex-wrapped pointer to BN_BLINDING in RSAKeyPair
  • Export BN_BLINDING_COUNTER as an extern GFp_BN_BLINDING_COUNTER
  • Create a test that uses the same BN_BLINDING at least GFp_BN_BLINDING_COUNTER +1 times, to exercise all the code paths in blinding.c
  • Create a test that is similar to test_agreement_suite_b_ecdh_generate, where the RAND returns unusable values forever, to test the logic where the blinding gives up after a bunch of tries.
  • Ensure BN_BLINDING coverage via coveralls report

Questions:

  • How do we want to handle a poisoned Mutex? Right now this work just propagates a panic with unwrap().

We agreed that a poisoned mutex warrants a panic.

  • Right now the blinding reuse test is inadequate— it only checks that the BN_BLINDING counter is updated. Ideally we'd check that the actual A and Ai blinding values have been regenerated when the counter is reset. What's the "cheapest" way to do this, wrt minimizing extraneous FFI work in this PR?

We are accepting the committed BN_BLINDING sync burden in exchange for the level of assurance offered by checking the counter, and stopping there for now.

  • There is a function-local extern (for GFp_BN_BLINDING_COUNTER) and use (for BN_BLINDING) in test_signature_rsa_pkcs1_sign_blinding_reuse. We should make sure we're happy with that code style.

Reverted to module-level imports.

Comment thread src/rsa.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.

BN_BLINDING_new() might return NULL. We need to check that.

@briansmith

Copy link
Copy Markdown
Owner

How do we want to handle a poisoned Mutex? Right now this work just propagates a panic with unwrap().

The Mutex will only become poisoned if some code panics while it is holding the mutex, right? And, (1) we avoid holding the mutex for a long time, and (2) we avoid panicing as much as possible. So, this can basically never happen, right? If you agree, I am fine with panicing on a poisoned mutex.

@briansmith

Copy link
Copy Markdown
Owner

Right now the blinding reuse test is inadequate— it only checks that the BN_BLINDING counter is updated. Ideally we'd check that the actual A and Ai blinding values have been regenerated when the counter is reset. What's the "cheapest" way to do this, wrt minimizing extraneous FFI work in this PR?

Well, first, I'm not sure such a test is necessary. But, if we want to do it, we could create a C function BN_BLINDING_check_consistency that verifies that a * ai == 1 mod m and which returns the counter. Then (AFAICT) you wouldn't need the #[repr(C)] definition of the struct and you'd need one one FFI declaration for the BN_BLINDING_check_consistency function. WDYT?

@briansmith

Copy link
Copy Markdown
Owner

There is a function-local extern (for GFp_BN_BLINDING_COUNTER) and use (for BN_BLINDING) in test_signature_rsa_pkcs1_sign_blinding_reuse. We should make sure we're happy with that code style.

I usually don't do that. Instead I would do them at the module level in the tests submodule. But, I'm not sure I care much either way.

@coveralls

coveralls commented Jul 25, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.02%) to 89.948% when pulling 99d19e480eec75c7d9af0b35282091a135762c87 on ranweiler:issue234-rsa-blinding-reuse into 08c729e on briansmith:master.

@ranweiler

Copy link
Copy Markdown
Contributor Author

The Mutex will only become poisoned if some code panics while it is holding the mutex, right? And, (1) we avoid holding the mutex for a long time, and (2) we avoid panicing as much as possible. So, this can basically never happen, right? If you agree, I am fine with panicing on a poisoned mutex.

I agree with this.

@ranweiler

Copy link
Copy Markdown
Contributor Author

Well, first, I'm not sure such a test is necessary. But, if we want to do it, we could create a C function BN_BLINDING_check_consistency that verifies that a * ai == 1 mod m and which returns the counter. Then (AFAICT) you wouldn't need the #[repr(C)] definition of the struct and you'd need one one FFI declaration for the BN_BLINDING_check_consistency function. WDYT?

I really like the idea of only touching the BN_BLINDING fields in C code, if possible. On the other hand, creating a new function in C (not used anywhere else) just for the sake of Rust testing feels like adding more overall complexity for less assurance. Unless we also test the BN_BLINDING_check_consistency function, we're now depending on its correctness, too.

Since with either approach we're relying on manual inspection for the correctness of bn_blinding_update, and just using the counter as a means of checking that its paths are being exercised, I'm inclined to leave the filled-in BN_BLINDING (+ comment). I don't expect the C BN_BLINDING struct to change, either, so it seems like a negligible synchronization burden for the Rust code.

@ranweiler

Copy link
Copy Markdown
Contributor Author

There is a function-local extern (for GFp_BN_BLINDING_COUNTER) and use (for BN_BLINDING) in test_signature_rsa_pkcs1_sign_blinding_reuse. We should make sure we're happy with that code style.

I usually don't do that. Instead I would do them at the module level in the tests submodule. But, I'm not sure I care much either way.

I'm going to move them to the module level to be less surprising, as I don't have an actual reason to make them function local.

@coveralls

coveralls commented Jul 26, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.3%) to 89.615% when pulling dacdf61e3a8bab83849ba638465dacfae9bede94 on ranweiler:issue234-rsa-blinding-reuse into 08c729e on briansmith:master.

@briansmith

Copy link
Copy Markdown
Owner

Since with either approach we're relying on manual inspection for the correctness of bn_blinding_update, and just using the counter as a means of checking that its paths are being exercised, I'm inclined to leave the filled-in BN_BLINDING (+ comment). I don't expect the C BN_BLINDING struct to change, either, so it seems like a negligible synchronization burden for the Rust code.

That's fine with me. I think this is a good strategy because I think it will motivate somebody to convert BN_BLINDING into a "native" Rust structure, using PositiveInteger, etc., in the future.

I see that you made a bunch of fixes to the code. Could you please fold them into the original commits that they are relevant to? e.g. Fold "Check for NULL return value from BN_BLINDING_new " into the patch that adds the call to BN_BLINDING_new. That would make it easier to review.

Thanks!

We will use this when testing to ensure that foreign blinding management
and recreation occurs as expected.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
@ranweiler
ranweiler force-pushed the issue234-rsa-blinding-reuse branch from dacdf61 to 4bdeffc Compare July 28, 2016 01:54
@ranweiler

Copy link
Copy Markdown
Contributor Author

I see that you made a bunch of fixes to the code. Could you please fold them into the original commits that they are relevant to?

Squashed and force pushed, continuing with remaining work.

@coveralls

coveralls commented Jul 28, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.3%) to 89.653% when pulling 4bdeffc9c0bb1818387ae94b668d597e07420419 on ranweiler:issue234-rsa-blinding-reuse into f87e2e8 on briansmith:master.

Comment thread src/rsa.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.

Please change the C definition to use uint32_t instead of unsigned just out of an abundance of caution.

In anticipation of testing blinding reuse, we want to be able to detect
field-level updates to the C data. Encode the C fields in our Rust type
so we can observe that the counter is updated correctly.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
We want to save blindings and reuse them, relying on the C code to
update and recreate them as needed. Prepare for this by adding an
`RSAKeyPair` field that lets us save a mutex-protected pointer to the
blinding, and make sure we free its referent when the keypair drops.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
Note: the existing test only checks that the blinding counter is
incremented modulo `BN_BLINDING_COUNTER`, which does not imply that the
actual `A` and `Ai` values are updated when the counter is reset.

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.
@ranweiler
ranweiler force-pushed the issue234-rsa-blinding-reuse branch from d8742d2 to a0f4f69 Compare July 29, 2016 06:25
Comment thread src/rsa.rs
for _ in 0 .. GFp_BN_BLINDING_COUNTER + 1 {
let prev_counter = unsafe {
let blinding = *(key_pair.blinding.lock().unwrap());
(*blinding).counter

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since the type annotations for blinding ended up being unnecessary, we could make this a one-liner, as well as the similar line below. I tried it and found it less readable, but don't feel strongly about it.

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 I commit this, I'm going to move the let blinding... out of the unsafe block. Our convention is to only put lines in the unsafe block if they are required to be unsafe.

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.

Oh...maybe not, because then I'd need another block, I think. Never mind.

@ranweiler

Copy link
Copy Markdown
Contributor Author

@briansmith, comments addressed and squashed into the relevant commits.

@briansmith

Copy link
Copy Markdown
Owner

Great. Are you the copyright holder on this? If not, who is? And do you have permission to license it? (Same questions I ask everybody.)

@ranweiler

Copy link
Copy Markdown
Contributor Author

Great. Are you the copyright holder on this? If not, who is? And do you have permission to license it? (Same questions I ask everybody.)

I am the copyright holder, and I do have permission to license it. I hereby assign copyright in this code to the project, to be licensed under the same terms as the rest of the code.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-5.07%) to 84.896% when pulling a0f4f69 on ranweiler:issue234-rsa-blinding-reuse into 5eecb9d on briansmith:master.

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