236 Commits

Author SHA1 Message Date
Jessica Hamilton
f5c5014f12 Enable Haiku platform. 2023-09-30 14:34:26 -07:00
Brian Smith
7bd536e9df Replicate BoringSSL's test for constant_time_conditional_memxor. 2023-09-30 13:40:06 -07:00
David Benjamin
76e98c4351 Always end BN_mod_exp_mont_consttime with normal Montgomery reduction.
This partially fixes a bug where, on x86_64, BN_mod_exp_mont_consttime
would sometimes return m, the modulus, when it should have returned
zero. Thanks to Guido Vranken for reporting it. It is only a partial fix
because the same bug also exists in the "rsaz" codepath. That will be
fixed in the subsequent CL. (See the commented out test.)

The bug only affects zero outputs (with non-zero inputs), so we believe
it has no security impact on our cryptographic functions. BoringSSL
calls BN_mod_exp_mont_consttime in the following cases:

- RSA private key operations
- Primality testing, raising the witness to the odd part of p-1
- DSA keygen and key import, pub = g^priv (mod p)
- DSA signing, r = g^k (mod p)
- DH keygen, pub = g^priv (mod p)
- Diffie-Hellman, secret = peer^priv (mod p)

It is not possible in the RSA private key operation, provided p and q
are primes. If using CRT, we are working modulo a prime, so zero output
with non-zero input is impossible. If not using CRT, we work mod n.
While there are nilpotent values mod n, none of them hit zero by
exponentiating. (Both p and q would need to divide the input, which
means n divides the input.)

In primality testing, this can only be hit when the input was composite.
But as the rest of the loop cannot then hit 1, we'll correctly report it
as composite anyway.

DSA and DH work modulo a prime, where this case cannot happen.

Analysis:

This bug is the result of sloppiness with the looser bounds from "almost
Montgomery multiplication", described in
https://eprint.iacr.org/2011/239. Prior to upstream's
ec9cc70f72454b8d4a84247c86159613cee83b81, I believe x86_64-mont5.pl
implemented standard Montgomery reduction (the left half of figure 3 in
the paper).

Though it did not document this, ec9cc70f7245 changed it to implement
the "almost" variant (the right half of the figure.) The difference is
that, rather than subtracting if T >= m, it subtracts if T >= R. In
code, it is the difference between something like our bn_reduce_once,
vs. subtracting based only on T's carry bit. (Interestingly, the
.Lmul_enter branch of bn_mul_mont_gather5 seems to still implement
normal reduction, but the .Lmul4x_enter branch is an almost reduction.)

That means none of the intermediate values here are bounded by m. They
are only bounded by R. Accordingly, Figure 2 in the paper ends with
step 10: REDUCE h modulo m. BN_mod_exp_mont_consttime is missing this
step. The bn_from_montgomery call only implements step 9, AMM(h, 1).
(x86_64-mont5.pl's bn_from_montgomery only implements an almost
reduction.)

The impact depends on how unreduced AMM(h, 1) can be. Remark 1 of the
paper discusses this, but is ambiguous about the scope of its 2^(n-1) <
m < 2^n precondition. The m+1 bound appears to be unconditional:

Montgomery reduction ultimately adds some 0 <= Y < m*R to T, to get a
multiple of R, and then divides by R. The output, pre-subtraction, is
thus less than m + T/R. MM works because T < mR => T' < m + mR/R = 2m.
A single subtraction of m if T' >= m gives T'' < m. AMM works because
T < R^2 => T' < m + R^2/R = m + R. A single subtraction of m if T' >= R
gives T'' < R. See also Lemma 1, Section 3 and Section 4 of the paper,
though their formulation is more complicated to capture the word-by-word
algorithm. It's ultimately the same adjustment to T.

But in AMM(h, 1), T = h*1 = h < R, so AMM(h, 1) < m + R/R = m + 1. That
is, AMM(h, 1) <= m. So the only case when AMM(h, 1) isn't fully reduced
is if it outputs m. Thus, our limited impact. Indeed, Remark 1 mentions
step 10 isn't necessary because m is a prime and the inputs are
non-zero. But that doesn't apply here because BN_mod_exp_mont_consttime
may be called elsewhere.

Fix:

To fix this, we could add the missing step 10, but a full division would
not be constant-time. The analysis above says it could be a single
subtraction, bn_reduce_once, but then we could integrate it into
the subtraction already in plain Montgomery reduction, implemented by
uppercase BN_from_montgomery. h*1 = h < R <= m*R, so we are within
bounds.

Thus, we delete lowercase bn_from_montgomery altogether, and have the
mont5 path use the same BN_from_montgomery ending as the non-mont5 path.
This only impacts the final step of the whole exponentiation and has no
measurable perf impact.

In doing so, add comments describing these looser bounds.  This includes
one subtlety that BN_mod_exp_mont_consttime actually mixes bn_mul_mont
(MM) with bn_mul_mont_gather5/bn_power5 (AMM). But this is fine because
MM is AMM-compatible; when passed AMM's looser inputs, it will still
produce a correct looser output.

Ideally we'd drop the "almost" reduction and stick to the more
straightforward bounds. As this only impacts the final subtraction in
each reduction, I would be surprised if it actually had a real
performance impact. But this would involve deeper change to
x86_64-mont5.pl, so I haven't tried this yet.

I believe this is basically the same bug as
https://github.com/golang/go/issues/13907 from Go.

Change-Id: I06f879777bb2ef181e9da7632ec858582e2afa38
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/52825
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2023-09-15 17:01:39 -07:00
Brian Smith
9ee8cf8e7e build.rs: Address new clippy warning. 2023-02-15 10:36:23 -08:00
Brian Smith
54520a3652 Adjust MIPSEL CI build system changes.
Take MIPSEL out of the GitHub Actions configuration because it fails to
link, and because it makes the build matrix too large.
2022-11-11 16:57:21 -08:00
Brian Smith
d0513bd767 Merge BoringSSL 53a87b7: ChaCha20-Poly1305 for Armv8 (AArch64). 2022-11-03 16:30:40 -07:00
Brian Smith
0fcbf92390 build: Fix recent symbol renaming to work on macOS. 2022-11-02 15:59:29 -07:00
Brian Smith
feae54128f Merge BoringSSL fa3fbda: P-256 assembly optimisations for Aarch64. 2022-11-02 13:15:58 -07:00
Brian Smith
1e3edb07eb ec: Avoid manual renaming of nistz256 symbols.
Make it easier to integrate the AArch64 nistz256 implementation.
2022-11-02 12:48:04 -07:00
Brian Smith
6b0050f08c Merge BoringSSL 295b313: Rename CPU feature files with underscores. 2022-10-31 16:17:20 -07:00
Brian Smith
9098bd6e30 Address latest clippy warnings. 2022-10-19 23:49:17 -07:00
Brian Smith
b2d1d00f3f Address clippy single_char_pattern in build.rs. 2022-04-07 17:40:03 -07:00
Brian Smith
155231fb01 signature: Enable Ed25519 support for wasm32 targets.
Ed25519 was disabled for WebAssembly due to some unrelated issues with
getting the X25519 code working in WebAssembly. Temporarily remove the
`agreement` API when targetting WebAssembly to work around those issues
in a way that lets us enabled Ed25519.
2022-04-06 12:54:49 -07:00
Brian Smith
d97ae1fd08 wasm32: Make wasm32_c the default and only mode; remove the "wasm32_c" feature.
Always require a C compilare for wasm32, instead of trying to provide a subset
of the functionality.
2021-10-06 15:53:02 -07:00
Brian Smith
4483f8ee55 build.rs: Don't package the intermediate files in the Cargo crate.
Don't package the inputs of the preassembly; just package the outputs.

Clarify how `mk/package.sh` interacts with `.gitignore`.

Eliminate unnecessary conditional logic in preassembly process.
2021-08-18 12:37:57 -07:00
Brian Smith
03ef33fc3b build.rs: Clarify include_dir and out_file function arguments.
Consistently use `out_file` as the argument name. Place all input arguments ahead of
output arguments.
2021-08-18 12:37:57 -07:00
Brian Smith
043c423a49 build.rs: Don't generate the C header for symbol prefixing during package.
The assembly headers used for Windows targets need to be generated during packaging so
that the assembly can be preassembled for those targets, but the C header isn't used
during packaging.
2021-08-18 12:37:57 -07:00
Brian Smith
c6f6be99f8 build.rs: Inline generate_prefix_symbols_nasm into its one caller.
Since there is only one caller, it doesn't need to be a separate function.
2021-08-18 12:37:57 -07:00
Brian Smith
74bf655d83 build.rs: Remove misleading comment.
On non-{x86,x86_64} Windows targets we also assume the C compiler
assembles. Remove the now-misleading comment.
2021-08-18 12:37:57 -07:00
Jeremy Soller
cc908a734b Add redox to LINUX_ABI 2021-08-16 12:54:43 -07:00
Alexander Ovchinnikov
ceb5b90f6e Enable building and running on Windows ARM64
I agree to license my contributions to each file under the terms given at the top of each file I changed.

Co-authored-by: Marc-André Moreau <marcandre.moreau@gmail.com>
2021-08-16 12:28:42 -07:00
Brian Smith
8fe3633ab9 build.rs: Refactor preassembly in package process in preparation for aarch64-pc-windows-msvc. 2021-08-15 16:01:32 -07:00
Brian Smith
b88024a96b build.rs: Replace Target::env with Target::is_musl.
Make the limited role of the `env` part of the target triple more clear.
2021-08-13 17:39:15 -07:00
Brian Smith
31e9f68406 build.rs: Refactor away Target::is_git to clarify logic.
Localize the use of the "is git" check to a single place and use more
descriptive variables for the various effects that this check has.

As a nice side effect, the loosly-typed `warnings_are_errors` parameters
that were threaded through functions are now gone.
2021-08-13 12:29:34 -07:00
Brian Smith
f43c0a9f65 build.rs: Don't assume MSVC targets are compiled with MSVC. 2021-08-13 10:04:47 -07:00
Brian Smith
7f5cd78190 build.rs: Remove unneeded logic to filter out .S files for -msvc
targets.

There is no case where we have .S files for -msvc targets, so this
filter doesn't filter anything out. IIRC, this is stale code from when
the situation was different in the past.

This reduces the amount of logic that is specific to "-msvc" targets and
makes the work to support clang and clang-cl for Windows, and to support
AAarch64 Windows, easier to review.
2021-08-13 10:04:47 -07:00
Brian Smith
31a32711c3 build.rs: Always use ".o" for object files.
Take a step towards supporting clang as the C compiler for -msvc
targets.
2021-08-13 08:18:14 -07:00
Brian Smith
b21ccc97a2 build.rs: Use cc-rs's built-in logic for warnings-as-errors.
Take a step towards supporting Clang as the compiler for -msvc targets.
2021-08-12 20:16:25 -07:00
Brian Smith
be3443f5c6 Appease Clippy 1.54. 2021-08-02 21:46:17 -07:00
Brian Smith
ce0b0a6b9b Undo some unnecessary differences from BoringSSL. 2021-05-12 19:06:16 -07:00
Brian Smith
b86067da4e Address clippy::vec_init_then_push. 2021-05-11 18:34:48 -07:00
Brian Smith
e898b00d53 Build: Derive FFI symbol prefix and FFI lib prefix from Cargo.toml "links".
Now "links" in Cargo.toml is the only thing that needs to be manually modified
when the prefix changes.

build.rs enforces that the package name and version are consistent with the
"links" field.
2021-05-03 16:28:56 -07:00
Brian Smith
badca90d14 Simplify assembly pre-generation mechanism.
Eliminate the extra Cargo.toml that was used just for this feature. It was
too error-prone to keep it in sync with the real Cargo.toml. Having one
Cargo.toml will allow us to reliably use the `CARGO_MANIFEST_LINKS` value
to keep the symbol prefix in sync with the `links` field in Cargo.toml in
the near future.
2021-05-03 13:36:20 -07:00
Brian Smith
36854dee45 build.rs: Tell Cargo about some environment variables that are used.
This doesn't tell Cargo about environment variables used by cc-rs and
the like, but it is a step in the right direction.
2021-05-03 13:36:20 -07:00
Brian Smith
0df1844530 Pass prefix from build.rs to src/prefixed.rs through cargo:rustc-env.
Eliminate one place where the prefix would have to be manually updated.
2021-05-03 12:39:35 -07:00
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
0e800b80ce build.rs: Replace manual dirty file checking with cargo:rerun-if-changed.
Simplify build.rs to make it easier to maintain. It seemed like the logic
being removed here wasn't always correct.
2021-04-30 13:37:46 -07:00
Brian Smith
508f5a3293 CI/CD: Use a GitHub-hosted copy of nasm instead of downloading it from nasm.us.
Downloading from nasm.us frequently fails. Avoid depending on it.

Change the expected location of nasm in build.rs.
2021-04-29 15:10:32 -07:00
Brian Smith
f37b8a2f3e Only use assembly code on known-compatible OS+Arch combinations.
Assume by default that an operating system does not have an ABI compatible
with the PerlAsm sources. Add all the operating systems that we've
explicitly added support for to the allowlist. Avoid trying to build or
use the PerlAsm code for those targets.

On top of this, we can build fallback logic for using Rust (or C)
implementations for those targets that aren't compatible with the
assembly.
2021-04-20 15:45:28 -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
Vlad Krasnov
34424d829d Enable the integrated assembly x86-64 ChaCha20-Poly1305 implementation from BoringSSL 2021-01-26 10:12:14 -08:00
Brian Smith
b9d7d089d9 Remove #![forbid(warnings)]; reply on Clippy in CI/CD instead.
The newest Rust Nightly is getting stricter about `forbid(warnings)`
which breaks the build.

Use "deny" instead of "forbid". And only deny when running Clippy in
CI/CD, so that when hacking on *ring* we don't have to deal with
warnings right away; we now only have to deal with them when we're ready
to submit a change to be merged.
2020-12-03 17:40:33 -08:00
Brian Smith
15c823a571 Build: Replace use of Yasm with use of Nasm for Windows assembly.
Match BoringSSL. According to the Chromium discussion about nasm, it
is also substantially faster.
2020-11-25 16:32:53 -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
7bac725679 Allow cross-compiling to *-linux-musl (except x86_64) w/o a sysroot.
Avoid requiring a sysroot for *-linux-musl targets when using Clang.

Add one AAarch64 and one 32-bit ARM MUSL target to GitHub Actions.

Use Rust 1.48's `-Clink-self-contained=yes` in CI for musl targets.

Support the non-default variants of the *-musl targets.
2020-11-24 09:22:50 -08:00
Brian Smith
f26bae0a6a Add support for aarch64-apple-darwin.
Change the static CPU feature detection logic to assume all aarch64-apple-* targets
have the same capabilities as far as the features we use are concerned.

Use the "ios64" PerlAsm flavour for aarch64-apple-darwin, like OpenSSL upstream does.

Add (build-only) cross-compilation jobs to GitHub Actions.
2020-11-18 00:00:12 -08:00
Brian Smith
f19c4f626b Remove dead crypto/fipsmodule/modes/internal.h. 2020-11-17 23:55:28 -08:00
Brian Smith
bbf935c17b Switch Poly1305 implementation to the BoringSSL implementation.
Previously the OpenSSL implementation was being used. Switch to the BoringSSL
version.

Switching to the BoringSSL implementation will make it easier to refactor the CPU feature
detection, which is important for upcoming ports.

This switch will also implicitly add support for BTI and pointer authentication for
Poly1305.

This is based on BoringSSL 63d06626d3a104868eee622e8e56d9f2dd643366.
2020-11-17 10:19:30 -08:00
Brian Smith
72abf71710 build.rs: Sort header files alphabetically 2020-11-17 10:19:30 -08:00