openssl/crypto/ec
Nicola Tuveri 0401d766af [EC] harden EC_KEY against leaks from memory accesses
We should never leak the bit length of the secret scalar in the key,
so we always set the `BN_FLG_CONSTTIME` flag on the internal `BIGNUM`
holding the secret scalar.

This is important also because `BN_dup()` (and `BN_copy()`) do not
propagate the `BN_FLG_CONSTTIME` flag from the source `BIGNUM`, and
this brings an extra risk of inadvertently losing the flag, even when
the called specifically set it.

The propagation has been turned on and off a few times in the past
years because in some conditions has shown unintended consequences in
some code paths, so at the moment we can't fix this in the BN layer.

In `EC_KEY_set_private_key()` we can work around the propagation by
manually setting the flag after `BN_dup()` as we know for sure that
inside the EC module the `BN_FLG_CONSTTIME` is always treated
correctly and should not generate unintended consequences.

Setting the `BN_FLG_CONSTTIME` flag alone is never enough, we also have
to preallocate the `BIGNUM` internal buffer to a fixed public size big
enough that operations performed during the processing never trigger
a realloc which would leak the size of the scalar through memory
accesses.

Fixed Length
------------

The order of the large prime subgroup of the curve is our choice for
a fixed public size, as that is generally the upper bound for
generating a private key in EC cryptosystems and should fit all valid
secret scalars.

For preallocating the `BIGNUM` storage we look at the number of "words"
required for the internal representation of the order, and we
preallocate 2 extra "words" in case any of the subsequent processing
might temporarily overflow the order length.

Future work
-----------

A separate commit addresses further hardening of `BN_copy()` (and
indirectly `BN_dup()`).

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/10631)
2020-02-18 19:11:10 +02:00
..
asm Also check for errors in x86_64-xlate.pl. 2020-02-17 12:17:53 +10:00
curve448 Implement a stricter ECX_KEY type 2020-02-11 22:32:47 +00:00
build.info [PROV][KMGMT][KEXCH][EC] Implement EC keymgtm and ECDH 2020-02-18 19:11:10 +02:00
curve25519.c Implement a stricter ECX_KEY type 2020-02-11 22:32:47 +00:00
ec2_oct.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec2_smpl.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_ameth.c [PROV][KEYMGMT][EC] Import/export of priv_key as padded const time BN 2020-02-18 19:11:10 +02:00
ec_asn1.c [PROV][KMGMT][KEXCH][EC] Implement EC keymgtm and ECDH 2020-02-18 19:11:10 +02:00
ec_check.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_curve.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_cvt.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_err.c util/mkerr.pl: make it not depend on the function code 2019-09-12 17:59:52 +02:00
ec_evp_lib.c [PROV][KMGMT][KEXCH][EC] Implement EC keymgtm and ECDH 2020-02-18 19:11:10 +02:00
ec_key.c [EC] harden EC_KEY against leaks from memory accesses 2020-02-18 19:11:10 +02:00
ec_kmeth.c Deprecate the ECDH functions. 2020-02-04 20:02:55 +10:00
ec_lib.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_local.h [PROV][KMGMT][KEXCH][EC] Implement EC keymgtm and ECDH 2020-02-18 19:11:10 +02:00
ec_mult.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_oct.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ec_pmeth.c Deprecate the ECDH functions. 2020-02-04 20:02:55 +10:00
ec_print.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecdh_kdf.c Params: change UTF8 construct calls to avoid explicit strlen(3) calls. 2020-02-06 16:22:00 +10:00
ecdh_ossl.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecdsa_ossl.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecdsa_sign.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecdsa_vrf.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
eck_prn.c Following the license change, modify the boilerplates in crypto/ec/ 2018-12-06 14:51:47 +01:00
ecp_mont.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_nist.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_nistp224.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_nistp256.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_nistp521.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_nistputil.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_nistz256_table.c Following the license change, modify the boilerplates in crypto/ec/ 2018-12-06 14:51:47 +01:00
ecp_nistz256.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_oct.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecp_s390x_nistp.c Reorganize local header files 2019-09-28 20:26:35 +02:00
ecp_smpl.c Deprecate the ECDSA and EV_KEY_METHOD functions. 2020-02-04 20:02:55 +10:00
ecx_key.c Implement a stricter ECX_KEY type 2020-02-11 22:32:47 +00:00
ecx_meth.c Add S390 support for provider based X25519/X448 2020-02-11 22:32:56 +00:00