Skip to content

Do RSA Blinding in Rust. - #372

Closed
briansmith wants to merge 1 commit into
rsa-public-decryptfrom
rsa-signing-more-rust
Closed

briansmith wants to merge 1 commit into
rsa-public-decryptfrom
rsa-signing-more-rust

Conversation

@briansmith

Copy link
Copy Markdown
Owner

This is the first draft of doing RSA blinding in Rust.

@briansmith briansmith self-assigned this Dec 11, 2016
@briansmith
briansmith force-pushed the rsa-signing-more-rust branch 3 times, most recently from a9e58d5 to 9947e74 Compare December 12, 2016 07:00
@coveralls

coveralls commented Dec 12, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 91.925% when pulling 9947e7490acf03afcd78db64f35dceae76c953ba on rsa-signing-more-rust into ab0c65c on master.

2 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 91.925% when pulling 9947e7490acf03afcd78db64f35dceae76c953ba on rsa-signing-more-rust into ab0c65c on master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.07%) to 91.925% when pulling 9947e7490acf03afcd78db64f35dceae76c953ba on rsa-signing-more-rust into ab0c65c on master.

@briansmith
briansmith force-pushed the rsa-signing-more-rust branch 2 times, most recently from b0206b6 to b2c4619 Compare December 12, 2016 21:37
@briansmith briansmith mentioned this pull request Dec 12, 2016
4 of 9 tasks
@briansmith
briansmith force-pushed the rsa-signing-more-rust branch from b2c4619 to 7f9296e Compare December 12, 2016 21:54
@briansmith

Copy link
Copy Markdown
Owner Author

I think this is good to go, pending the CI results.

@samscott89 , @ranweiler, @djudd Do any of you have time to look at this? I think this is pretty much the key to making it straightforward to oxidize the rest of the RSA code and also to start oxidizing away BIGNUM.

The need to use core::mem::replace makes things a bit awkward. OTOH, it does prevent us from using a corrupted state where the blinding factor and its inverse are out of sync. That is a real bug in OpenSSL and LibreSSL (but not BoringSSL and ring because of an earlier commit).

I would have prefer to allocate all the memory for the Blinding in Blinding::new(), but it would have made Blinding::new() more complicated without making blind() less complicated, so I'll give up on that for now. That's something to revisit during oxidizing the BIGNUM code.

@briansmith briansmith added this to the 0.6 milestone Dec 12, 2016
@briansmith briansmith added the rsa label Dec 12, 2016
@ranweiler

Copy link
Copy Markdown
Contributor

@briansmith, I can look this over tonight!

Comment thread src/rsa/blinding.rs Outdated
rng: &rand::SecureRandom) -> Result<Contents, error::Unspecified> {
// Use `into_elem_decoded_montgomery_encoded` to grab the underling
// `BIGNUM` to avoid a superfluous `malloc()` & `free()`.
let mut random = arbitrary1.into_elem_decoded_montgomery_encoded();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I wonder if an Arbitrary<N> struct - with no-op conversions to Elem and ElemDecoded - would be a good complement to the Elem* structs and make this less awkward-looking

@briansmith briansmith Dec 13, 2016 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Perhaps Storage might be a better name than Arbitrary, since really it's entire purpose is to allow the memory inside the pointed-at BIGNUM to be reused.

Then we could have elem_random<F: Field>(storage: Storage<F>, m: &Modulus<F>) -> ElemDecoded<F> and elem_inverse<F>(storage: Storage<F>, a: &ElemDecoded<F>, m: &Modulus<F>) -> ElemDecoded<F>. As a bonus, those functions would become pure functions,.

In contrast, we could use the name Arbitrary but it might be misleading to pass an "arbitrary" value to these functions. Especially the random generation taking an Arbitrary kind of implies that values is used for (re)seeding or something.

WDYT?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like Storage or ElemStorage.

Comment thread src/rsa/blinding.rs
Ok(Blinding(r))
pub fn new() -> Self { Blinding(None) }

pub fn blind<F>(&mut self, x: bigint::ElemDecoded<N>,

@djudd djudd Dec 13, 2016 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a reason not to pass some of the arguments here which will be constant across invocations into the constructor, and hold on to a reference? If I understand correctly, we are relying on e and n to always be the same for any given Blinding, and passing them into the constructor would make that clearer.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Thanks for the feedback.

The distinction between RSASigningState and RSAKeyPair is that RSASigningState is per-thread and RSAKeyPair is the state shared (immutably) across all threads by all the RSASigningState instances for the same key. The idea is that a high-performance server would likely create one RSASigningState per core (perhaps in a thread-local) per key. So, we generally want to make the RSASigningStates small.

Here's the definition of RSASigningState:

pub struct RSASigningState {
    key_pair: std::sync::Arc<RSAKeyPair>,
    blinding: blinding::Blinding,
}

We could instead define RSASigningState as a newtype:

pub struct RSASigningState(blinding::Blinding)

Then we'd move the key_pair field to the blinding, without increasing the size of RSASigningState. After all, the idea of RSASigningState is to be the abstraction "a thing you need exclusive access to to sign with an RSA key, for some reason", and Blinding is the reason you need some extra thing. That would make it clear that the blinding values are specific to that key pair and that there's no mixing of key pairs for the same blinding. Then RSASigningState::sign() would borrow the key_pair out of the Blinding.

OTOH, it's a bit weird that the Blinding should know about the entire contents of the RSAKeyPair when only n and e are relevant to it. And it's also weird that it would need to know about Arc and threading behavior. What I'm hoping is that instead we'll be able to expand on the use of the field: PhantomData<Field> construct using the "lightweight static capabilities" idea to ensure that one can never mix two different tuples of (N, P, Q, ...). But we haven't implemented the important parts that actually enforce that yet, so maybe it is presumptuous to act like we did (or that we even know how to yet!).

Anyway, I'd love to hear what people think about this.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

BTW, the entire bigint API already assumes that we can't mix different (N, P, Q, ...) values, and that assumption is made specifically to avoid needing to compare moduli for equality on every multi-operand function call. That doesn't mean that blinding must do things the same way, of course.

@djudd

djudd commented Dec 13, 2016 via email

Copy link
Copy Markdown

@coveralls

coveralls commented Dec 13, 2016 •

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 7f9296e on rsa-signing-more-rust into ** on master**.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Changes Unknown when pulling 7f9296e on rsa-signing-more-rust into ** on master**.

Comment thread src/rsa/blinding.rs
});
},
Err(bigint::InversionError::NoInverse) => {}, // continue
Err(_) => { return Err(error::Unspecified); }

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The coverage report shows that these two error cases are never hit. They aren't hit because:

  • The coverage test, test_signature_rsa_pkcs1_sign_blinding_creation_failure, is fixing the output of the RNG to be all zeros.
  • The inversion is done using GFp_BN_mod_inverse_blinded.
  • That function uses GFp_BN_rand_range_ex to blind the inversion.
  • GFp_BN_rand_range_ex fails because the RNG is consistently returning 0, which is the allowed out of range.

Thus, we never even get to the point where we call GFp_BN_mod_inverse_odd.

Further, even if we did, GFp_BN_mod_inverse_odd starts with:

  if (!GFp_BN_is_odd(n)) {
    OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
    return 0;
  }

When n is zero, GFp_BN_is_odd will return false and so GFp_BN_mod_inverse_odd will fail without setting out_no_inverse to 1.

To fix the code coverage, I propose that we modify GFp_BN_mod_inverse_odd to require non-negative, non-positive inputs that are always in range, because those are the only values that GFp_BN_mod_inverse_blinded supplies to it, and GFp_BN_mod_inverse_blinded is the only non-test caller.

Then, we should extend the tests so that the RNG returns p or q for a composite n == p * q, since those would be the only non-invertible elements.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Actually, we have:

        try!(bigint::elem_randomize(&mut random, n, rng));
        match bigint::elem_set_to_inverse_blinded(&mut random_inv, &random, n,
                                                  rng) {

So, we never even call elem_set_to_inverse_blinded(), since elem_randomize() will fail if the RNG only returns zeros.

@briansmith briansmith modified the milestones: 0.7.1, 0.6 Dec 14, 2016
@briansmith

Copy link
Copy Markdown
Owner Author

I'm going to punt this to a slightly later release. I'll factor out the stuff that is needed by #373 and move it to #373 for 0.6 and leave the rest to 0.7.1 or so.

@briansmith
briansmith force-pushed the rsa-signing-more-rust branch from c04f019 to 8015bca Compare December 23, 2016 12:42
@briansmith
briansmith changed the base branch from master to rsa-public-decrypt December 23, 2016 12:43
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.045% when pulling 8015bcae9fe376828f5c517f19c43e4c74d3c89a on rsa-signing-more-rust into e3083aedd2c646442a0fef56557c63ba92c59b0a on rsa-public-decrypt.

@coveralls

coveralls commented Dec 23, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.045% when pulling 8015bcae9fe376828f5c517f19c43e4c74d3c89a on rsa-signing-more-rust into e3083aedd2c646442a0fef56557c63ba92c59b0a on rsa-public-decrypt.

@briansmith
briansmith force-pushed the rsa-signing-more-rust branch from 8015bca to 7a56720 Compare December 25, 2016 07:59
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.045% when pulling 7a56720 on rsa-signing-more-rust into 3993c5c on rsa-public-decrypt.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.045% when pulling 7a56720 on rsa-signing-more-rust into 3993c5c on rsa-public-decrypt.

@briansmith

Copy link
Copy Markdown
Owner Author

When n is zero, GFp_BN_is_odd will return false and so GFp_BN_mod_inverse_odd will fail without setting out_no_inverse to 1.

Sorry, that was a mistake on my part: n is the modulus, not the value being inverted.

The updated version of the PR does fix the above coverage problem, but there's coverage issues: We never test what happens when we fail to do the blinding 32 times and give up. This is unfortunate, but the code is clear enough that I'm not going to bother with trying to get 100% coverage at this time. Also we never test what happens when we give up blinding once and then try blinding again. But the code is pretty clear that the state is reset to None and None is the initial state, so this is not worth bothering with now. The effort trying to improve the test coverage here would probably better be spent on formal proofs of correctness.

@briansmith

Copy link
Copy Markdown
Owner Author

I wonder if an Arbitrary struct - with no-op conversions to Elem and ElemDecoded - would be a good complement to the Elem* structs and make this less awkward-looking

David, it turns out that there is probably a straightforward way to achieve what you suggest here after about 50 other commits have landed. Conversely, it is quite awkward to do it the way the code is now. So, I'm going to punt this down the road.

@briansmith
briansmith force-pushed the rsa-signing-more-rust branch from 7a56720 to d420473 Compare December 31, 2016 05:48
@briansmith

Copy link
Copy Markdown
Owner Author

This landed as d420473.

@briansmith briansmith closed this Dec 31, 2016
@briansmith briansmith modified the milestones: 0.6.n, 0.7.n Dec 31, 2016
@coveralls

coveralls commented Dec 31, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.362% when pulling d420473 on rsa-signing-more-rust into e9149cf on rsa-public-decrypt.

2 similar comments
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.362% when pulling d420473 on rsa-signing-more-rust into e9149cf on rsa-public-decrypt.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 93.362% when pulling d420473 on rsa-signing-more-rust into e9149cf on rsa-public-decrypt.

@briansmith
briansmith deleted the rsa-signing-more-rust branch February 16, 2017 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants