boringssl/crypto/fipsmodule/bn
Brian Smith fee8709f69 Replace |alloca| in |BN_mod_exp_mont_consttime|.
|alloca| is dangerous and poorly specified, according to any
description of |alloca|. It's also hard for some analysis tools to
reason about.

The code here assumed |alloca| is a macro, which isn't a valid
assumption. Depending on what which headers are included and what
toolchain is being used, |alloca| may or may not be defined as a macro,
and this might change over time if/when toolchains are updated. Or, we
might be doing static analysis and/or dynamic analysis with a different
configuration w.r.t. the availability of |alloca| than production
builds use.

Regardless, the |alloca| code path only kicked in when the inputs are
840 bits or smaller. Since the multi-prime RSA support was removed, for
interesting RSA key sizes the input will be at least 1024 bits and this
code path won't be triggered since powerbufLen will be larger than 3072
bytes in those cases. ECC inversion via Fermat's Little Theorem has its
own constant-time exponentiation so there are no cases where smaller
inputs need to be fast.

The RSAZ code avoids the |OPENSSL_malloc| for 2048-bit RSA keys.
Increasingly the RSAZ code won't be used though, since it will be
skipped over on Broadwell+ CPUs. Generalize the RSAZ stack allocation
to work for non-RSAZ code paths. In order to ensure this doesn't cause
too much stack usage on platforms where RSAZ wasn't already being used,
only do so on x86-64, which already has this large stack size
requirement due to RSAZ.

This change will make it easier to refactor |BN_mod_exp_mont_consttime|
to do that more safely and in a way that's more compatible with various
analysis tools.

This is also a step towards eliminating the |uintptr_t|-based alignment
hack.

Since this change increases the number of times |OPENSSL_free| is
skipped, I've added an explicit |OPENSSL_cleanse| to ensure the
zeroization is done. This should be done regardless of the other changes
here.

Change-Id: I8a161ce2720a26127e85fff7513f394883e50b2e
Reviewed-on: https://boringssl-review.googlesource.com/28584
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-05-21 19:43:05 +00:00
..
asm Enable ADX assembly. 2018-05-11 21:57:13 +00:00
add.c
bn_test_to_fuzzer.go
bn_test.cc Require BN_mod_exp_mont* inputs be reduced. 2018-04-24 18:29:29 +00:00
bn_tests.txt
bn.c
bytes.c
check_bn_tests.go
cmp.c
ctx.c
div_extra.c Move some RSA keygen support code into separate files. 2018-05-08 21:25:46 +00:00
div.c Add EC_FELEM for EC_POINTs and related temporaries. 2018-04-25 16:39:58 +00:00
exponentiation.c Replace |alloca| in |BN_mod_exp_mont_consttime|. 2018-05-21 19:43:05 +00:00
gcd_extra.c Move some RSA keygen support code into separate files. 2018-05-08 21:25:46 +00:00
gcd.c Move some RSA keygen support code into separate files. 2018-05-08 21:25:46 +00:00
generic.c
internal.h Replace |alloca| in |BN_mod_exp_mont_consttime|. 2018-05-21 19:43:05 +00:00
jacobi.c
montgomery_inv.c Restore the BN_mod codepath for public Montgomery moduli. 2018-04-20 20:50:15 +00:00
montgomery.c Remove return values from bn_*_small. 2018-04-24 15:34:32 +00:00
mul.c Remove return values from bn_*_small. 2018-04-24 15:34:32 +00:00
prime.c Move some RSA keygen support code into separate files. 2018-05-08 21:25:46 +00:00
random.c
rsaz_exp.c Replace |alloca| in |BN_mod_exp_mont_consttime|. 2018-05-21 19:43:05 +00:00
rsaz_exp.h Replace |alloca| in |BN_mod_exp_mont_consttime|. 2018-05-21 19:43:05 +00:00
shift.c
sqrt.c