270 Commits

Author SHA1 Message Date
Brian Smith
e8a5d1138c build.rs: More consistently use named constants for OS names. 2024-01-18 14:33:06 -08:00
Brian Smith
4e37622614 build.rs: Consistently use named constants for architecture names. 2024-01-18 14:33:06 -08:00
Travis Finkenauer
57f58c98f1 build.rs: don't use -pedantic flag
In some build systems, target sysroots may use non-standard C extentions
like `#include_next`. In such cases, the `-pedantic` flag breaks the
compilation.

Resolves issue #1923.
2024-01-18 14:02:40 -08:00
Brian Smith
27aa5386a0 EC P-384: Use array types for crossing Rust<->C boundary.
Avoid using the P384_POINT type on the C side. It seems to work for all
the targets we support, for P-384, but this pattern probably doesn't
work in general. Especially due to alignment issues for 32-bit targets,
it is doubtful it would work for P-521.
2023-10-18 11:39:04 -07:00
Brian Smith
9a49f379e3 P-256 ECDSA verification: Clarify multiplication.
Move more of the logic for the nistz256 multiplication into Rust.
2023-10-18 09:49:30 -07:00
Brian Smith
6283ca087f P-256 ECDSA verification: Use optimized nistz256 verification.
Import the optimized nistz256 verification from BoringSSL.
2023-10-18 09:49:30 -07:00
Brian Smith
9d4bb22ef8 build.rs: Sort non-MSVC C compiler flags alphabetically.
All the `-W...` flags make do difference in packaged releases,
but `-fvisibility=hidden` is a big deal, so don't hide it after
all the warning stuff.
2023-10-14 22:03:50 -07:00
Brian Smith
8909e6eddf build.rs: Don't enable MSVC SDL warnings.
They hardly matter for this codebase any more.
2023-10-14 22:03:50 -07:00
Brian Smith
65941d014f build.rs: Don't override Windows exception/RTTI/C++ compiler flags.
Use whatever cc-rs does by default. This shouldn't impact us as we
don't use C++.
2023-10-14 22:03:50 -07:00
Brian Smith
2201721cd3 build.rs: Let cc-rs choose the hardening flags, if any.
Now that we have almost no C code in *ring* there's little
need for these extra hardening flags.
2023-10-14 22:03:50 -07:00
Brian Smith
2986d15484 Use -nostdlibinc for wasm32-wasi like wasm32-unknown-unknown. 2023-10-14 16:50:18 -07:00
andoalon
08413016bf Don't manually specify optimization flags for MSVC (cc-rs takes care)
I agree to license my contributions to each file under the terms given
at the top of each file I changed.
2023-10-13 16:56:03 -07:00
Brian Smith
3eb07737f1 build.rs: Address clippy::ptr_arg. 2023-10-13 15:03:55 -07:00
Brian Smith
639ab71e64 Use CRYPTO_memcmp name the same as BoringSSL.
*ring* defined a function named `OPENSSL_memcmp` that did what
`CRYPTO_memcmp` does in BoringSSL, and BoringSSL has a different
function called `OPENSSL_memcmp`. *ring* doesn't need
`OPENSSL_memcmp` so sync the `CRYPTO_memcmp` stuff with BoringSSL.

This eliminates unnecessary differences from BoringSSL.
2023-10-09 15:17:19 -07:00
Brian Smith
9c7b114272 build.rs: Remove -fno-strict-aliasing from the C compiler configuration.
Enable further compiler optimizations.

BoringSSL does not include `-fno-strict-aliasing` in its builds.
2023-10-09 11:54:35 -07:00
Brian Smith
bbfb0cdba9 build.rs: Remove -pedantic-errors from compiler configuration.
Our policy is to set warnings-as-errors only when buildingt from Git,
not when building from a packaged release. This flag is another aspect
of warnings-as-errors.
2023-10-08 21:15:23 -07:00
Brian Smith
f3273afc0a build.rs: Use cc-rs in a more canonical way in most cases. 2023-10-06 17:45:28 -07:00
Brian Smith
f85335da98 build.rs: Treat sources uniformly, and distinct from object files. 2023-10-06 17:45:28 -07:00
Jiahao XU
2e0655867b Fix fn cc in build.rs
Do not convert `Path`/`OsStr` to `String` in `cc`, instead creates an
`OsString` and push to it.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-10-06 10:06:22 -07:00
Jiahao XU
ccb1e9eb58 Use Path::display instead of Path::to_str in fn cc
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-10-06 10:06:22 -07:00
Jiahao XU
d949e6ffc7 Ret PathBuf instead of String for fn compile
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-10-06 10:06:22 -07:00
Jiahao XU
a65da9d260 Fix getting CARGO_MANIFEST_DIR: Use env::var_os
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-10-06 10:06:22 -07:00
Jiahao XU
d8aca487de Fix retrieveing OUT_DIR in build.rs
Use `env::var_os` instead of `env::var` to handle non utf-8 path
correctly.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
2023-10-06 10:06:22 -07:00
Brian Smith
af7c37f8af Enable FE25519_ADX Curve25519 base point mult. except on Windows. 2023-10-06 09:54:59 -07:00
Brian Smith
fa6631463c build.rs: Skip .S files on x86 and x86-64 Windows.
So far .S files are only used on non-x86, non-x86_64 targets. That
will change soon, so prepare for that by filtering them out so that
we don't feed them to MSVC.
2023-10-06 09:54:59 -07:00
Brian Smith
91b0d90f2c build.rs: Use OsStr/OsString more to handle non-Unicode paths. 2023-10-05 21:34:57 -07:00
Brian Smith
3a650b3354 build.rs: Don't try to set linker flags since we're not linking.
First, we were passing `-Wl,--gc-sections` to the compiler regardless
of whether it is MSVC, which didn't make any sense on its own.

But, even more generally, it doesn't make sense for us to try to tell
the linker what to do when we aren't actually linking. (We're building
static libraries of the C and assembly code.)
2023-10-05 20:10:52 -07:00
Brian Smith
414e7a32ff build.rs: Don't try to manually set -fPIC.
cc-rs's documentation says that it already handles this automatically,
which is why we'd already removed it for other targets.
2023-10-05 20:10:52 -07:00
Brian Smith
12d8178ab6 build.rs: Clarify compiler configuration.
Clarify which parts of the build script modify the compiler
configuration (`configure_cc`) and which don't (`cc`). Ensure that the
configuration is only done once per library, instead of once per source
file, as each `cc` invocation can reuse the configuration work done by
a single `configure_cc` call.
2023-10-05 20:10:52 -07:00
Brian Smith
c2910b0af4 build.rs: Remove empty and useless LD_FLAGS. 2023-10-05 20:10:52 -07:00
Brian Smith
9d2abfcf52 build.rs: Set compiler C and CPP (preprocessor) flags in one place.
Apparently it is OK to set `-std=c1x` even when compiling assembly
code, so just set it no matter what we're compiling. This simplifies
the code and allows future simplification.

It's not clear why certain warnings were separated from the others.
Combine them too, for the same reasons.
2023-10-05 10:17:08 -07:00
Luis Cruz
8387c3953e Add apple tvos support 2023-10-03 11:54:18 -07:00
Brian Smith
93d1807dd0 0.17.0-beta.3 2023-10-02 01:18:18 -07:00
Brian Smith
851f9f339b 0.17.0-beta.2 2023-10-02 01:18:18 -07:00
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