188 Commits

Author SHA1 Message Date
Brian Smith
7886603cee Use some variant of "ring core" instead of "GFp" as a prefix for everything.
"GFp_" isn't in the code at all anymore.
2021-05-02 22:09:07 -07:00
Brian Smith
384f7d056b Replace manual FFI symbol prefixing with automatic symbol prefixing.
Revert the names used in the BoringSSL C/asm code to the names used in
BoringSSL. This substantially reduces the diff between *ring* and
BoringSSL for these files.

Use a variant of BoringSSL's symbol prefixing machinery to semi-
automatically prefix FFI symbols with the `GFp_` prefix. The names aren't
all exactly the same as before, because previously we *replaced* a
symbol's original prefix with the `GFp_` prefix; now we're prepending
`GFp_`. In the future we'll use a different prefix entirely.

This paves the way for using different prefixes for each version so that
multiple versions of *ring* can be linked into an executable at once.
2021-05-02 22:09:07 -07:00
Brian Smith
501fc4eeaa Replace *ring*'s P-256 arithmetic with BoringSSL's P-256 arithmetic.
Use Fiat Crypto for non-x86_64 platforms, like BoringSSL. Continue
using the nistz256 code on Windows, differently from BoringSSL.

Make *ring* more consistent with BoringSSL.
2021-02-10 12:20:26 -08:00
Brian Smith
034bfe7143 Avoid opportunistically including system header files.
Don't use the presence of a header to determine whether to include it. Instead,
communicate from build.rs whether system header files should be used.
2020-11-24 09:22:50 -08:00
Brian Smith
46d39428cb Clean up memcpy/memset patterns.
Define `GFp_memcpy` and `GFp_memset` with fallback implementations. Sync up
some code that diverged from BoringSSL due to the lack of these functions.
2020-11-17 08:23:40 -08:00
Brian Smith
0e7fdf5e91 Split ASSERT into dev_assert_secret and debug_assert_nonsecret. 2020-05-29 13:23:29 -05:00
Brian Smith
d17e23058b Require source code modification to enable assertions in C code. 2020-05-29 11:41:42 -05:00
Brian Smith
0c5f61b14f Merge BoringSSL c556d87: Add aes_nohw.c fallback AES implementation.
Add the BoringSSL aes_nohw implementation with minimal changes needed to
build. Enable the AES-GCM AEAD tests for wasm32 targets gated on the "wasm_c"
feature.
2020-05-28 10:51:03 -05:00
Brian Smith
742f0ebb11 Implement ASSERT() for wasm32 targets. 2020-05-27 15:09:15 -05:00
Brian Smith
192d946b82 Replace gcm_nohw.c with gcm_nohw.rs. 2020-05-06 10:57:33 -05:00
Brian Smith
d3cab43a4a Merge BoringSSL 9855c1c: Add a constant-time fallback GHASH
implementation.

*ring* tries to work without type-punning `memcpy`, so the use of that
in `GFp_gcm_ghash_nohw` was replaced by the use of `u64_from_be_bytes`.
This will (I hope) also help with the eventual support for big-endian
targets. Here's the diff from BoringSSL in that function:

```diff
-void gcm_ghash_nohw(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
-                    size_t len) {
+void GFp_gcm_ghash_nohw(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
+                        size_t len) {
   uint64_t swapped[2];
   swapped[0] = CRYPTO_bswap8(Xi[1]);
   swapped[1] = CRYPTO_bswap8(Xi[0]);

   while (len >= 16) {
-    uint64_t block[2];
-    OPENSSL_memcpy(block, inp, 16);
-    swapped[0] ^= CRYPTO_bswap8(block[1]);
-    swapped[1] ^= CRYPTO_bswap8(block[0]);
+    swapped[0] ^= u64_from_be_bytes(&inp[8]);
+    swapped[1] ^= u64_from_be_bytes(inp);
     gcm_polyval_nohw(swapped, &Htable[0]);
     inp += 16;
     len -= 16;
```

I also had to add a couple of (uint32_t) truncating casts where
BoringSSL expects an implicit truncation to occur, to avoid
`-Werror=conversion`.

During the merge, I found that `GFp_gcm_gmult_clmul` had its
`.cfi_startproc` on the wrong line. I fixed that as part of the merge.

During my review of the BoringSSL changes, I noticed that BoringSSL had
left some of the dead code in ghash-x86_64.pl, which had previously been
removed in *ring*. That removal is being done in BoringSSL in [1].

[1] https://boringssl-review.googlesource.com/c/boringssl/+/41144
2020-05-04 10:54:19 -05:00
David Benjamin
6887d5e81b Add an option to disable SSE2 intrinsics for testing.
We have some code which uses SSE2 intrinsics which, since they don't
have complicated build requirements, is enabled even with
OPENSSL_NO_ASM. x86_64 mandates SSE2 and people building for x86 tend to
mandate it anyway these days. This is great, but we still have generic
32-bit and 64-bit code configurations for other platforms.

32-bit generic code is covered by testing 32-bit ARM with NEON disabled.
However, 64-bit ARM always has NEON available, so we have no SIMD-less
64-bit platforms in our CI.

The immediate motivation is some bitsliced AES code I'm working on,
however I believe this also applies to the existing HRSS code. This also
fixes the HRSS feature checks to only look at __SSE2__, not __SSE__.
__SSE__ isn't sufficient and we don't compile if GCC or Clang is told
-msse -mno-sse2.

Change-Id: Iebb23f1664a2f62e0b4333e0e99f7d5f6c7f384d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/39204
Reviewed-by: Adam Langley <agl@google.com>
2019-12-20 18:29:26 +00:00
Adam Langley
9638f8fba9 Use a smaller hex digest in FIPS flag files when SHA-256 used.
1458b49a9e5 switched to using HMAC-SHA256 for FIPS integrity checks on
Android. However, the flag file was named after a full 64-byte hex
digest. The additional 32 bytes weren't uninitialised, but are still
superfluous. This change gets rid of them.

Change-Id: I192af9eb2b94833cdea3620a153d4fd05c7265b9
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/37864
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2019-10-01 19:18:33 +00:00
Adam Langley
fbebe833b1 Limit __attribute__ ((fallthrough)) to Clang >= 5.
With Clang 3.5, this fails and breaks gRPC's build:

third_party/boringssl/crypto/bio/fd.c:196:7: error: declaration does not declare anything [-Werror,-Wmissing-declarations]
      OPENSSL_FALLTHROUGH;
      ^~~~~~~~~~~~~~~~~~~
third_party/boringssl/crypto/bio/../internal.h:192:29: note: expanded from macro 'OPENSSL_FALLTHROUGH'

Clang 5, empirically, is happy, so limit this to Clang >= 5.

Change-Id: I82430b415955ec7d664abe3ffe024e6bb28346c2
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/37246
Commit-Queue: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2019-08-26 21:35:02 +00:00
Manoj Gupta
05cd93068b Add Fallthru support for clang 10.
clang has gained supoprted for __attribute__ ((fallthrough))
in https://reviews.llvm.org/rL369414.
Detect the support in clang and enable it as OPENSSL_FALLTHROUGH.
This is needed to fix ToT clang builds.

Bug: chromium:997709
Test: CQ
Change-Id: Iefa17687f6b5e8c95f359f167e9049d9a69c5302
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/37244
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
2019-08-26 16:48:48 +00:00
Brian Smith
ed47ae8c67 Support clang-cl better. 2019-07-22 12:08:35 -10:00
Brian Smith
4c392ad338 Remove the libc dependency for most platforms. 2019-07-17 14:44:28 -10:00
Brian Smith
4399add9cf Don't require the C toolchain to provide assert.h for non-debug builds. 2019-07-15 21:33:55 -10:00
Brian Smith
eb93d699e8 Remove redundant GCM code. 2019-07-12 18:59:23 -10:00
Brian Smith
4bbba957f8 Merge BoringSSL 92b7c89: Add a value barrier to constant-time selects. 2019-07-02 17:01:41 -10:00
David Benjamin
92b7c89e6e Add a value barrier to constant-time selects.
Clang recognizes the (mask & a) | (~mask & b) pattern as a select. While
it often optimizes this into a cmov, it sometimes inserts branches
instead, particularly when it detects a string of cmovs with the same
condition.

In the long term, we need language-level support for expressing our
constraints. In the short term, introduce value barriers to prevent the
compiler from reasoning about our bit tricks. Thanks to Chandler Carruth
for suggesting this pattern. It should be reasonably robust, short of
value-based PGO or the compiler learning to reason about empty inline
assembly blocks.

Apply barriers to our various constant-time selects. We should invest
more in the valgrind-based tooling to figure out if there are other
instances.

Change-Id: Icc24ce36a61f7fec021a762c27197b9c5bd28c5d
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/36484
Reviewed-by: Chandler Carruth <chandlerc@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2019-06-19 17:19:13 +00:00
Brian Smith
6960614893 Remove #includes <string.h> in Curve25519. 2019-06-14 13:48:57 -10:00
Brian Smith
ae6175f59e Remove unnecessary declaration of GFp_cpuid_setup. 2019-03-14 10:52:25 -10:00
Brian Smith
a8cc810154 Remove unused BORINGSSL_CONSTANT_TIME_VALIDATION. 2019-03-14 10:44:40 -10:00
Brian Smith
b2b15a61aa Remove unnecessary #if defined(_MSC_VER). 2019-03-14 10:44:36 -10:00
Brian Smith
78b18778c7 Remove unused alignof macros. 2019-02-06 14:40:56 -10:00
Brian Smith
dbdc510653 Simplify endian conversion in C code.
The relevant Clang bugs have been fixed and the code that uses these
functions is basically unused on Windows so MSVC's optimizer is
irrelevant to it.
2019-02-06 14:39:49 -10:00
Brian Smith
3dfbe3bf6b Do GCM CPU feature detection in Rust.
Rename some GCM assembly functions so that all functions that do the
same thing the same way have the same name, to make the dispatching
logic simpler.

Thread CPU feature caching witnesses through the GCM dispatching logic
to make feature detection less error-prone.

Start an internal Rust API for feature detection.
2019-01-28 14:33:31 -10:00
Tom Tan
de3c1f69cc Fix header file for _byteswap_ulong and _byteswap_uint64 from MSVC CRT
_byteswap_ulong and _byteswap_uint64 are documented (see below link) as coming from stdlib.h.
 On some build configurations stdlib.h is pulled in by intrin.h but that is not guaranteed. In particular,
this assumption causes build breaks when building Chromium for Windows ARM64 with clang-cl. This
 change switches the #include to use the documented header file, thus fixing Windows ARM64 with clang-cl.


https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/byteswap-uint64-byteswap-ulong-byteswap-ushort

Bug: chromium:893460
Change-Id: I738c7227a9e156c894c2be62b52228a5bbd88414
Reviewed-on: https://boringssl-review.googlesource.com/c/34244
Reviewed-by: David Benjamin <davidben@google.com>
Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
Commit-Queue: David Benjamin <davidben@google.com>
2019-01-14 19:49:39 +00:00
Brian Smith
d736b9865b Merge BoringSSL a6a049a: Add start of infrastructure for checking constant-time properties. 2019-01-04 14:44:04 -10:00
Adam Langley
8e8f250422 Use thread-local storage for PRNG states if fork-unsafe buffering is enabled.
We switched from thread-local storage to a mutex-pool in 82639e6f53
because, for highly-threaded processes, the memory used by all the
states could be quite large. I had judged that a mutex-pool should be
fine, but had underestimated the PRNG requirements of some of our jobs.

This change makes rand.c support using either thread-locals or a
mutex-pool. Thread-locals are used if fork-unsafe buffering is enabled.
While not strictly related to fork-safety, we already have the
fork-unsafe control, and it's already set by jobs that care a lot about
PRNG performance, so fits quite nicely here.

Change-Id: Iaf1e0171c70d4c8dbe1e42283ea13df5b613cb2d
Reviewed-on: https://boringssl-review.googlesource.com/c/31564
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
2018-12-28 18:05:18 +00:00
Adam Langley
a6a049a6fb Add start of infrastructure for checking constant-time properties.
Valgrind's checking of uninitialised memory behaves very much like a
check for constant-time code: branches and memory indexes based on
uninitialised memory trigger warnings. Therefore, if we can tell
Valgrind that some secret is “uninitialised”, it'll give us a warning if
we do something non-constant-time with it.

This was the idea behind https://github.com/agl/ctgrind. But tricks like
that are no longer needed because Valgrind now comes with support for
marking regions of memory as defined or not. Therefore we can use that
API to check constant-time code.

This CL defines |CONSTTIME_SECRET| and |CONSTTIME_DECLASSIFY|, which are
no-ops unless the code is built with
|BORINGSSL_CONSTANT_TIME_VALIDATION| defined, which it isn't by default.
So this CL is a no-op itself so far. But it does show that a couple of
bits of constant-time time are, in fact, constant-time—seemingly even
when compiled with optimisations, which is nice.

The annotations in the RSA code are a) probably not marking all the
secrets as secret, and b) triggers warnings that are a little
interesting:

The anti-glitch check calls |BN_mod_exp_mont| which checks that the
input is less than the modulus. Of course, it is because the input is
the RSA plaintext that we just decrypted, but the plaintext is supposed
to be secret and so branching based on its contents isn't allows by
Valgrind. The answer isn't totally clear, but I've run out of time on
this for now.

Change-Id: I1608ed0b22d201e97595fafe46127159e02d5b1b
Reviewed-on: https://boringssl-review.googlesource.com/c/33504
Reviewed-by: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
2018-12-18 22:43:02 +00:00
Brian Smith
b989d3343d Implement high-level AES-GCM logic to Rust. 2018-12-14 11:39:34 -10:00
Brian Smith
cda9d33d31 Use Rust for RSA exponentiation. 2018-11-29 16:01:00 -10:00
Brian Smith
740032b9b7 Fix build with MSVC 2017 15.9.1. 2018-11-15 16:17:40 -10:00
Brian Smith
be59434c42 Remove redundant includes of <stdint.h> and <stddef.h>. 2018-11-15 16:17:40 -10:00
Brian Smith
975a2c29f1 Remove unneeded __cplusplus ifdefs. 2018-11-15 16:17:39 -10:00
David Benjamin
6ce93ccb80 Simulate other ARM CPUs when running tests.
We test all Intel variants via SDE. For ARM, we can do the next best
thing and tweak with OPENSSL_armcap_P. If the host CPU does not support
the instructions we wish to test, skip it, but print something so we
know whether we need a more featureful test device.

Also fix the "CRASHED" status to "CRASH", to match
https://chromium.googlesource.com/chromium/src/+/master/docs/testing/json_test_results_format.md
(It's unclear if anything actually parses that JSON very carefully...)

Bug: 19
Change-Id: I811cc00a0d210a454287ac79c06f18fbc54f96dd
Reviewed-on: https://boringssl-review.googlesource.com/c/33204
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
2018-11-15 00:58:09 +00:00
Brian Smith
fd118ce114 Merge BoringSSL 2e74fda: Don't redefine alignas in C++. 2018-11-13 16:23:23 -10:00
David Benjamin
cbfe4f5a8e Remove support for GCC 4.7.
This reverts https://boringssl-review.googlesource.com/24924. As noted
there, GCC 4.7 support ends 2018-03-23, which has passed. GCC 4.8.0 was
released 2013-03-22, so we are now past the five year mark, matching
Abseil's guidelines.

Abseil also now explicitly lists supported compilers and explicitly
requires GCC 4.8+. https://abseil.io/docs/cpp/platforms/platforms

gRPC also now requires 4.8 per
https://github.com/grpc/grpc/issues/10036#issuecomment-290248204

Update-Note: On the off chance someone was using GCC 4.7, which only
started working in January, that'll no longer work.

Change-Id: Ie017822e903f98293e7b5e9bda10f104f17be7b3
Reviewed-on: https://boringssl-review.googlesource.com/c/32564
Commit-Queue: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-10-18 19:01:20 +00:00
David Benjamin
5b33effa72 Rename OPENSSL_NO_THREADS, part 1.
BoringSSL depends on the platform's locking APIs to make internal global
state thread-safe, including the PRNG. On some single-threaded embedded
platforms, locking APIs may not exist, so this dependency may be disabled
with a build flag.

Doing so means the consumer promises the library will never be used in any
multi-threaded address space. It causes BoringSSL to be globally thread-unsafe.
Setting it inappropriately will subtly and unpredictably corrupt memory and
leak secret keys.

Unfortunately, folks sometimes misinterpreted OPENSSL_NO_THREADS as skipping an
internal thread pool or disabling an optionally extra-thread-safe mode. This is
not and has never been the case. Rename it to
OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED to clarify what
this option does.

Update-Note: As a first step, this CL makes both OPENSSL_NO_THREADS and
OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED work. A later CL
will remove the old name, so migrate callers after or at the same time as
picking up this CL.

Change-Id: Ibe4964ae43eb7a52f08fd966fccb330c0cc11a8c
Reviewed-on: https://boringssl-review.googlesource.com/32084
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
2018-09-26 19:10:02 +00:00
Joshua Liebow-Feeser
8c7c6356e6 Support symbol prefixes
- In base.h, if BORINGSSL_PREFIX is defined, include
  boringssl_prefix_symbols.h
- In all .S files, if BORINGSSL_PREFIX is defined, include
  boringssl_prefix_symbols_asm.h
- In base.h, BSSL_NAMESPACE_BEGIN and BSSL_NAMESPACE_END are
  defined with appropriate values depending on whether
  BORINGSSL_PREFIX is defined; these macros are used in place
  of 'namespace bssl {' and '}'
- Add util/make_prefix_headers.go, which takes a list of symbols
  and auto-generates the header files mentioned above
- In CMakeLists.txt, if BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS
  are defined, run util/make_prefix_headers.go to generate header
  files
- In various CMakeLists.txt files, add "global_target" that all
  targets depend on to give us a place to hook logic that must run
  before all other targets (in particular, the header file generation
  logic)
- Document this in BUILDING.md, including the fact that it is
  the caller's responsibility to provide the symbol list and keep it
  up to date
- Note that this scheme has not been tested on Windows, and likely
  does not work on it; Windows support will need to be added in a
  future commit

Change-Id: If66a7157f46b5b66230ef91e15826b910cf979a2
Reviewed-on: https://boringssl-review.googlesource.com/31364
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: David Benjamin <davidben@google.com>
2018-09-06 20:07:52 +00:00
David Benjamin
ed09f2d5cd Move the MSan sanity check to a source file.
OSS-Fuzz builds fuzz/*.c without matching config, which pulls in
crypto/internal.h. See
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=9583.

Change-Id: I4bd16f8741816ebef00d8102fd1f79b0cb16f6a3
Reviewed-on: https://boringssl-review.googlesource.com/30024
Reviewed-by: Adam Langley <agl@google.com>
2018-07-25 15:15:19 +00:00
David Benjamin
22ac2d9b25 Fail the build if MSan is built with assembly.
MSan works by instrumenting memory accesses in the compiler. Accesses from
uninstrumented code, such as assembly, are invisible to it. MSan will
incorrectly report reads from assembly-initialized memory as uninitialized.

To avoid confusing downstream consumers with false positives, catch this at
compile-time with a more useful error.

Update-Note: BoringSSL with MSan and assembly doesn't work, but now rather than
crashing at runtime, it will fail to build altogether. It's possible someone
was building BoringSSL with MSan and either not running it at all or just not
exercising the codepaths that break.

Bug: 252
Change-Id: I0c8b0fa3c2d1e584b3f40d532a668a8c9be06cb7
Reviewed-on: https://boringssl-review.googlesource.com/29928
Reviewed-by: Adam Langley <agl@google.com>
2018-07-23 19:07:41 +00:00
Adam Langley
82639e6f53 Use a pool of |rand_state| objects.
Previously we used thread-local state objects in rand.c. However, for
applications with large numbers of threads, this can lead to excessive
memory usage.

This change causes us to maintain a mutex-protected pool of state
objects where the size of the pool equals the maximum concurrency of
|RAND_bytes|. This might lead to state objects bouncing between CPUs
more often, but should help the memory usage problem.

Change-Id: Ie83763d3bc139e64ac17bf7e015ad082b2f8a81a
Reviewed-on: https://boringssl-review.googlesource.com/29565
Commit-Queue: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: David Benjamin <davidben@google.com>
2018-07-06 21:25:37 +00:00
David Benjamin
2e74fdaa4a Don't redefine alignas in C++.
alignas in C++11 is a bit more flexible than
__attribute__((aligned(x))), and we already require C++11 in tests.

Change-Id: If61c35daa5fcaaca5119dcc6808a3e746befc170
Reviewed-on: https://boringssl-review.googlesource.com/29544
Reviewed-by: Adam Langley <agl@google.com>
2018-07-03 22:11:32 +00:00
Brian Smith
75cc4b9ab7 Avoid some uint8_t * casts.
They are undefined behavior if `uint8_t` isn't `unsigned char`.
2018-05-18 13:32:50 -10:00
Brian Smith
3d9c4001c5 Remove unused constant_time_is_zero_8. 2018-05-18 13:32:49 -10:00
Brian Smith
0027633cc9 Rename crypto_word_t to crypto_word.
Avoid using the `_t` suffix since that's undefined behavior in C.
2018-05-18 13:32:49 -10:00
Brian Smith
7381522944 Remove BN_ULONG references from crypto_word_t docs. 2018-05-16 13:13:46 -10:00