239 Commits

Author SHA1 Message Date
Brian Smith
6f122c7f89 Add test vectors for minimum/maximum RSA key size checks.
Note that there are *two* maximum key size checks in the code, and the
one in rsa.rs subsumes the one in rsa_impl.c.
2016-06-21 12:35:45 -10:00
Brian Smith
b5ec71aecd Refactor RSA verification C code for line-based code coverage.
Line-based code coverage tools often don't handle the `||` operator
in conditions smartly. Separating out each condition to test for helps
them report more useful results.
2016-06-21 08:52:34 -10:00
Brian Smith
1ac871ea0d Remove unused RSA PKCS#1 padding code. 2016-06-19 15:41:43 -10:00
Dirkjan Ochtman
0aade20148 Fix typo in comment for GFp_rsa_private_transform prototype.
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2016-06-19 14:01:19 +02:00
Dirkjan Ochtman
9b75408c88 Change GFp_rsa_private_transform() to take a single inout argument.
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2016-06-19 14:01:19 +02:00
Dirkjan Ochtman
b61aa8117c Change RSA_size() to return size_t.
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2016-06-19 14:01:19 +02:00
Dirkjan Ochtman
6b7245d7b9 Allocate BN_CTX inside rsa_new_end().
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2016-06-19 14:01:17 +02:00
Dirkjan Ochtman
735d1f708a Get rid of unused RSA struct flags.
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2016-06-19 13:58:52 +02:00
Brian Smith
4bb822342b Fix build. 2016-05-24 21:40:33 -10:00
Brian Smith
556ea4e7b2 Remove C RSA signing, encryption, and padding code. 2016-05-24 21:09:31 -10:00
Brian Smith
c715504dab Parse RSA PKCS#1 1.5 decoded signatures using ring::input. 2016-05-24 15:45:21 -10:00
Brian Smith
c71252e322 Move all BIGNUM stuff from rsa_verify to rsa_public_decrypt.
This is being done in preparation for replacing `rsa_verify` with Rust
code.
2016-05-24 13:49:52 -10:00
Brian Smith
abf9bd8605 Remove crypto/rsa/rsa_asn1.c and crypto/rsa/rsa_test.cc. 2016-05-24 07:17:49 -10:00
Brian Smith
428c0b8572 Parse RSA public keys using ring::der. 2016-05-24 06:59:11 -10:00
Brian Smith
deac07f26c Fix benign typo in crypto/rsa/rsa_impl.
This worked because `OPENSSL_PUT_ERROR` is defined as an empty macro,
but if `OPENSSL_PUT_ERROR` is redefined to a non-empty value then the
result is a syntax error.
2016-05-23 16:45:42 -10:00
Brian Smith
b39b8e32b9 Make RSA_marshal_private_key static. 2016-05-19 10:11:13 -10:00
Brian Smith
f9dd8b13de Make RSA_marshal_public_key static. 2016-05-19 10:02:18 -10:00
Brian Smith
07f63207e7 Remove mutex and blinding stuff from the |RSA| struct.
Signing with RSA isn't exposed in the API yet, but when it is, the way
`BN_BLINDING`s are managed will be different than what BoringSSL does.
Doing this now lets us remove `CRYPTO_MUTEX` completely.
2016-04-25 18:26:43 -10:00
Brian Smith
c526383058 Fix error handling in rsa_private_transform. 2016-04-25 18:25:07 -10:00
Brian Smith
006d93a203 Avoid static initializers, lazily load CPUID info from Rust.
Avoid all the messiness involved with static initializers by doing it
the dumb way. Static initializers are commonly banned from applications
because they add latency to application startup.

This implementation needs to be fine-tuned to account for cases where
we don't need CPUID info (e.g. hard-coded ARM target profiles, NO_ASM
builds, platforms where we have no ASM code).

Also, it is worth investigating whether the Rust implementation of
`call_once` has optimal performance, but for now we punt on that issue.
2016-04-25 17:17:56 -10:00
Brian Smith
c97365e774 Refactor ring::rand.
Stop using `CRYPTO_once`. Instead, require that the CSPRNG is
explicitly threaded through every function that requires a CSPRNG. The
constructor of `SystemRandom` will then initialize the file handle for
/dev/urandom as necessary.
2016-04-25 10:12:18 -10:00
Brian Smith
f483fc04ad Fix debug build linking on Windows with a workaround. 2016-04-21 11:45:11 -10:00
Brian Smith
26029f220c Merge BoringSSL 2a92031: Clarify |RSA_verify_raw| error handling & cleanup. 2016-04-21 10:05:12 -10:00
Brian Smith
d9234e5deb Merge commit '9902262': Remove redundant check of |sig_len| in |RSA_verify|.
This was already done in *ring*, so just tweak the formatting to match
the BoringSSL version.
2016-04-21 10:01:01 -10:00
Brian Smith
d862bf4635 Rename obj_mac.h to nid.h. 2016-04-21 09:29:39 -10:00
Brian Smith
b26ce747a9 Fix assertion in |bn_blinding_create_param|. 2016-04-21 09:23:03 -10:00
Brian Smith
5db711cd56 Merge BoringSSL d879e29: Further optimize Montgomery math in RSA blinding.
*ring* had already made this change. This merge just makes the *ring*
code more consistent with the BoringSSL code.
2016-04-21 09:22:49 -10:00
Brian Smith
2a92031bb4 Clarify |RSA_verify_raw| error handling & cleanup.
Use the common pattern of returning early instead of |goto err;| when
there's no cleanup to do yet. Also, move the error checking of
|BN_CTX_get| failure closer to the the calls to |BN_CTX_get|. Avoid
calling |OPENSSL_cleanse| on public data. Clarify when/why |buf| is not
freed.

Change-Id: I9df833db7eb7041c5af9349c461297372b988f98
Reviewed-on: https://boringssl-review.googlesource.com/7464
Reviewed-by: David Benjamin <davidben@google.com>
2016-04-18 20:42:15 +00:00
Brian Smith
9902262af6 Remove redundant check of |sig_len| in |RSA_verify|.
The same check is already done in |RSA_verify_raw|, so |RSA_verify|
doesn't need to do it.

Also, move the |RSA_verify_raw| check earlier.

Change-Id: I15f7db0aad386c0f764bba53e77dfc46574f7635
Reviewed-on: https://boringssl-review.googlesource.com/7463
Reviewed-by: David Benjamin <davidben@google.com>
2016-04-18 20:40:41 +00:00
Brian Smith
c0b196d4eb Drop support for engines-provided signature verification.
We do not need to support engine-provided verification methods.

Change-Id: Iaad8369d403082b728c831167cc386fdcabfb067
Reviewed-on: https://boringssl-review.googlesource.com/7311
Reviewed-by: David Benjamin <davidben@google.com>
2016-04-18 20:40:17 +00:00
Brian Smith
4165289b36 Use one process for all tests.
Instead of building the test suites inherited from BoringSSL as
seperate executables, link them all together into one executable,
giving all their `main` functions unique names.

This allows all the tests to be run, even on platforms that don't have
traditional process spawning, and avoids the need to keep track of
directory names even on platforms that do support process spawning.

This also makes it easier to integrate new BoringSSL test suites on
Windows, because we don't need to create a new `vcxproj` file for each
one.

Having one test executable may also make code coverage easier.
2016-04-16 18:55:05 -10:00
Ms2ger
e92cc0afab Remove RSA_decrypt.
I agree to license my contributions to each file under the
same terms given at the top of each file I changed.
2016-04-14 17:28:54 -10:00
Brian Smith
b78a8d06d2 Add a comment describing one of the Montgomery tricks in RSA. 2016-03-31 12:07:34 -10:00
Brian Smith
aca7793e9d Use constant-time-ish math in RSA CRT.
The Montgomery math functions and |BN_mod_sub_quick| are
constant-time-ish and can eventually be made constant-time. The
Montgomery math should also be faster.
2016-03-31 11:29:19 -10:00
Brian Smith
f8e64e5a87 Require p > q in RSA private keys.
This simplifies |mod_exp| slightly, but in a way that's important for
future changes to make the entire CRT calculation constant-time.
2016-03-31 11:22:36 -10:00
David Benjamin
981936791e Remove some easy obj.h dependencies.
A lot of consumers of obj.h only want the NID values. Others didn't need
it at all. This also removes some OBJ_nid2sn and OBJ_nid2ln calls in EVP
error paths which isn't worth pulling a large table in for.

BUG=chromium:499653

Change-Id: Id6dff578f993012e35b740a13b8e4f9c2edc0744
Reviewed-on: https://boringssl-review.googlesource.com/7563
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-31 20:50:33 +00:00
Brian Smith
d879e29936 Further optimize Montgomery math in RSA blinding.
Change-Id: I830c6115ce2515a7b9d1dcb153c4cd8928fb978f
Reviewed-on: https://boringssl-review.googlesource.com/7591
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-31 19:35:33 +00:00
Brian Smith
7394791a74 Verify RSA private key operation regardless of whether CRT is used.
Previously, the verification was only done when using the CRT method,
as the CRT method has been shown to be extremely sensitive to fault
attacks. However, there's no reason to avoid doing the verification
when the non-CRT method is used (performance-sensitive applications
should always be using the CRT-capable keys).

Previously, when we detected a fault (attack) through this verification,
libcrypto would fall back to the non-CRT method and assume that the
non-CRT method would give a correct result, despite having just
detecting corruption that is likely from an attack. Instead, just give
up, like NSS does.

Previously, the code tried to handle the case where the input was not
reduced mod rsa->n. This is (was) not possible, so avoid trying to
handle that. This simplifies the equality check and lets us use
|CRYPTO_memcmp|.
2016-03-31 00:03:40 -10:00
Brian Smith
a4171b6e73 Drop support for incomplete RSA keys.
Drop support for keys that don't have a complete set of CRT parameters.
In the future we'll probably resurrect `RSA_recovert_crt_params` to
support things like WebCrypto that need to support keys that have only
(n, e, d).
2016-03-29 16:25:33 -10:00
Brian Smith
bfb82a6a84 Simplify RSA_generate.
All of the key parameters are always NULL in an `RSA` returned by
rsa_new_begin so don't bother checking if they are NULL. Also, move the
construction of each parameter closer to the point at which it is
initialized.
2016-03-29 15:46:30 -10:00
Brian Smith
55d24e7536 No-op merge of BoringSSL f08c1c6: Drop support for custom |mod_exp| hooks in |RSA_METHOD|.
This was done a while ago in *ring*.
2016-03-29 14:36:26 -10:00
Brian Smith
1144794892 Merge BoringSSL 3426d10: Convert RSA blinding to use Montgomery multiplication.
This change was already made in *ring*, but some tweaks made to the BoringSSL
version were merged in.
2016-03-29 14:23:42 -10:00
Corey Farwell
e37d03107d Replace usages of BN_with_flags with flag assignment.
Instead of using `BN_with_flags` (which relies on an extra allocated
`BIGNUM`), we'll make sure that all `BIGNUM`s containing secret values
have the `BN_FLG_CONSTTIME` flag assigned when they are created.

I agree to license my contributions to each file under the
same terms given at the top of each file I changed.
2016-03-29 12:34:42 -10:00
Brian Smith
f08c1c6895 Drop support for custom |mod_exp| hooks in |RSA_METHOD|.
The documentation in |RSA_METHOD| says that the |ctx| parameter to
|mod_exp| can be NULL, however the default implementation doesn't
handle that case. That wouldn't matter since internally it is always
called with a non-NULL |ctx| and it is static, but an external
application could get a pointer to |mod_exp| by extracting it from
the default |RSA_METHOD|. That's unlikely, but making that impossible
reduces the chances that future refactorings will cause unexpected
trouble.

Change-Id: Ie0e35e9f107551a16b49c1eb91d0d3386604e594
Reviewed-on: https://boringssl-review.googlesource.com/7580
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-29 22:20:48 +00:00
Brian Smith
3426d10119 Convert RSA blinding to use Montgomery multiplication.
|BN_mod_mul_montgomery| has better constant-time behavior (usually)
than |BN_mod_mul| and |BN_mod_sqr| and on platforms where we have
assembly language optimizations (when |OPENSSL_BN_ASM_MONT| is set in
crypto/bn/montgomery.c) it is faster. While doing so, reorder and
rename the |BN_MONT_CTX| parameters of the blinding functions to match
the order normally used in Montgomery math functions.

As a bonus, remove a redundant copy of the RSA public modulus from the
|BN_BLINDING| structure, which reduces memory usage.

Change-Id: I70597e40246429c7964947a1dc46d0d81c7530ef
Reviewed-on: https://boringssl-review.googlesource.com/7524
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-29 22:07:22 +00:00
Brian Smith
f51f5e9d39 No-op merge of BoringSSL 2aca226: Fix typo in comment.
This was already fixed when the patch that introduced it was merged.
2016-03-28 17:48:50 -10:00
David Benjamin
2aca226412 Fix typo in comment.
Change-Id: I0effe99d244c4ccdbb0e34db6e01a59c9463cb15
Reviewed-on: https://boringssl-review.googlesource.com/7572
Reviewed-by: Steven Valdez <svaldez@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-28 15:57:00 +00:00
Brian Smith
cef1fd8b5f Merge BoringSSL db50299: Add tests for RSA objects with only n and d.
We really don't need or want to support such keys, and eventually
won't, but for now merge the test as-is since it actually passes.
2016-03-26 23:40:37 -10:00
David Benjamin
db50299b24 Add tests for RSA objects with only n and d.
Conscrypt, thanks to Java's RSAPrivateKeySpec API, must be able to use RSA keys
with only modulus and exponent. This is kind of silly and breaks the blinding
code so they, both in OpenSSL and BoringSSL, had to explicitly turn blinding
off.

Add a test for this as we're otherwise sure to break it on accident.

We may wish to avoid the silly rsa->flags modification, I'm not sure. For now,
keep the requirement in so other consumers do not accidentally rely on this.

(Also add a few missing ERR_clear_error calls. Functions which are expected to
fail should be followed by an ERR_clear_error so later unexpected failures
don't get confused.)

BUG=boringssl:12

Change-Id: I674349821f1f59292b8edd085f21dc37e8bcaa75
Reviewed-on: https://boringssl-review.googlesource.com/7560
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-25 20:52:17 +00:00
Brian Smith
cbf56a5683 Clarify lifecycle of |BN_BLINDING|.
In |bn_blinding_update| the condition |b->e != NULL| would never be
true (probably), but the test made reasoning about the correctness of
the code confusing. That confusion was amplified by the circuitous and
unusual way in which |BN_BLINDING|s are constructed. Clarify all this
by simplifying the construction of |BN_BLINDING|s, making it more like
the construction of other structures.

Also, make counter unsigned as it is no longer ever negative.

Change-Id: I6161dcfeae19a80c780ccc6762314079fca1088b
Reviewed-on: https://boringssl-review.googlesource.com/7530
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-25 20:08:04 +00:00