Repository navigation
Do RSA Blinding in Rust. - #372
briansmith wants to merge 1 commit into
Conversation
a9e58d5 to
9947e74
Compare
|
Coverage increased (+0.07%) to 91.925% when pulling 9947e7490acf03afcd78db64f35dceae76c953ba on rsa-signing-more-rust into ab0c65c on master. |
2 similar comments
|
Coverage increased (+0.07%) to 91.925% when pulling 9947e7490acf03afcd78db64f35dceae76c953ba on rsa-signing-more-rust into ab0c65c on master. |
|
Coverage increased (+0.07%) to 91.925% when pulling 9947e7490acf03afcd78db64f35dceae76c953ba on rsa-signing-more-rust into ab0c65c on master. |
b0206b6 to
b2c4619
Compare
b2c4619 to
7f9296e
Compare
|
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 The need to use I would have prefer to allocate all the memory for the |
|
@briansmith, I can look this over tonight! |
| 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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
| Ok(Blinding(r)) | ||
| pub fn new() -> Self { Blinding(None) } | ||
|
|
||
| pub fn blind<F>(&mut self, x: bigint::ElemDecoded<N>, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Looks good! Left a couple of comments/questions, but not sure how much it's
worth massaging the code structure prior to oxidizing away more BIGNUM
stuff.
…On Mon, Dec 12, 2016 at 2:41 PM, Joe Ranweiler ***@***.***> wrote:
@briansmith <https://github.com/briansmith>, I can look this over tonight!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#372 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAY-JpaAbGrzl0mFnKuqXcqFcfa2zgrgks5rHc2qgaJpZM4LJ_I_>
.
|
|
Changes Unknown when pulling 7f9296e on rsa-signing-more-rust into ** on master**. |
1 similar comment
|
Changes Unknown when pulling 7f9296e on rsa-signing-more-rust into ** on master**. |
| }); | ||
| }, | ||
| Err(bigint::InversionError::NoInverse) => {}, // continue | ||
| Err(_) => { return Err(error::Unspecified); } |
There was a problem hiding this comment.
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_exto blind the inversion. GFp_BN_rand_range_exfails 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.
There was a problem hiding this comment.
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.
c04f019 to
8015bca
Compare
e3083ae to
3993c5c
Compare
8015bca to
7a56720
Compare
1 similar comment
3993c5c to
e9149cf
Compare
Sorry, that was a mistake on my part: 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 |
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. |
7a56720 to
d420473
Compare
|
This landed as d420473. |
This is the first draft of doing RSA blinding in Rust.