openssl/crypto/ec
Nicola Tuveri 9a43a73380 [ec] Match built-in curves on EC_GROUP_new_from_ecparameters
Description
-----------

Upon `EC_GROUP_new_from_ecparameters()` check if the parameters match any
of the built-in curves. If that is the case, return a new
`EC_GROUP_new_by_curve_name()` object instead of the explicit parameters
`EC_GROUP`.

This affects all users of `EC_GROUP_new_from_ecparameters()`:
- direct calls to `EC_GROUP_new_from_ecparameters()`
- direct calls to `EC_GROUP_new_from_ecpkparameters()` with an explicit
  parameters argument
- ASN.1 parsing of explicit parameters keys (as it eventually
  ends up calling `EC_GROUP_new_from_ecpkparameters()`)

A parsed explicit parameter key will still be marked with the
`OPENSSL_EC_EXPLICIT_CURVE` ASN.1 flag on load, so, unless
programmatically forced otherwise, if the key is eventually serialized
the output will still be encoded with explicit parameters, even if
internally it is treated as a named curve `EC_GROUP`.

Before this change, creating any `EC_GROUP` object using
`EC_GROUP_new_from_ecparameters()`, yielded an object associated with
the default generic `EC_METHOD`, but this was never guaranteed in the
documentation.
After this commit, users of the library that intentionally want to
create an `EC_GROUP` object using a specific `EC_METHOD` can still
explicitly call `EC_GROUP_new(foo_method)` and then manually set the
curve parameters using `EC_GROUP_set_*()`.

Motivation
----------

This has obvious performance benefits for the built-in curves with
specialized `EC_METHOD`s and subtle but important security benefits:
- the specialized methods have better security hardening than the
  generic implementations
- optional fields in the parameter encoding, like the `cofactor`, cannot
  be leveraged by an attacker to force execution of the less secure
  code-paths for single point scalar multiplication
- in general, this leads to reducing the attack surface

Check the manuscript at https://arxiv.org/abs/1909.01785 for an in depth
analysis of the issues related to this commit.

It should be noted that `libssl` does not allow to negotiate explicit
parameters (as per RFC 8422), so it is not directly affected by the
consequences of using explicit parameters that this commit fixes.
On the other hand, we detected external applications and users in the
wild that use explicit parameters by default (and sometimes using 0 as
the cofactor value, which is technically not a valid value per the
specification, but is tolerated by parsers for wider compatibility given
that the field is optional).
These external users of `libcrypto` are exposed to these vulnerabilities
and their security will benefit from this commit.

Related commits
---------------

While this commit is beneficial for users using built-in curves and
explicit parameters encoding for serialized keys, commit
b783beeadf6b80bc431e6f3230b5d5585c87ef87 (and its equivalents for the
1.0.2, 1.1.0 and 1.1.1 stable branches) fixes the consequences of the
invalid cofactor values more in general also for other curves
(CVE-2019-1547).

The following list covers commits in `master` that are related to the
vulnerabilities presented in the manuscript motivating this commit:

- d2baf88c43 [crypto/rsa] Set the constant-time flag in multi-prime RSA too
- 311e903d84 [crypto/asn1] Fix multiple SCA vulnerabilities during RSA key validation.
- b783beeadf [crypto/ec] for ECC parameters with NULL or zero cofactor, compute it
- 724339ff44 Fix SCA vulnerability when using PVK and MSBLOB key formats

Note that the PRs that contributed the listed commits also include other
commits providing related testing and documentation, in addition to
links to PRs and commits backporting the fixes to the 1.0.2, 1.1.0 and
1.1.1 branches.

This commit includes a partial backport of
https://github.com/openssl/openssl/pull/8555
(commit 8402cd5f75)
for which the main author is Shane Lontis.

Responsible Disclosure
----------------------

This and the other issues presented in https://arxiv.org/abs/1909.01785
were reported by Cesar Pereida García, Sohaib ul Hassan, Nicola Tuveri,
Iaroslav Gridin, Alejandro Cabrera Aldaya and Billy Bob Brumley from the
NISEC group at Tampere University, FINLAND.

The OpenSSL Security Team evaluated the security risk for this
vulnerability as low, and encouraged to propose fixes using public Pull
Requests.

_______________________________________________________________________________

Co-authored-by: Shane Lontis <shane.lontis@oracle.com>

(Backport from https://github.com/openssl/openssl/pull/9808)

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9809)
2019-09-09 14:16:18 +03:00
..
asm make ecp_nistz256_point_add_vis3() local 2019-08-09 09:12:42 +01:00
curve448 Update copyright year 2019-05-28 14:49:38 +02:00
build.info Add ec/asm/x25519-ppc64.pl module. 2018-07-26 14:01:49 +02:00
curve25519.c Fixed linux_x86_icc compiler errors in EC code related to __uint128_t/__int128_t 2019-04-11 08:42:10 +10:00
ec2_oct.c Update copyright year 2019-05-28 14:49:38 +02:00
ec2_smpl.c Added NULL check to BN_clear() & BN_CTX_end() 2019-03-19 07:28:39 +01:00
ec_ameth.c Update copyright year 2019-05-28 14:49:38 +02:00
ec_asn1.c [ec] Match built-in curves on EC_GROUP_new_from_ecparameters 2019-09-09 14:16:18 +03:00
ec_check.c
ec_curve.c [ec] Match built-in curves on EC_GROUP_new_from_ecparameters 2019-09-09 14:16:18 +03:00
ec_cvt.c Update copyright year 2018-09-11 13:45:17 +01:00
ec_err.c SCA hardening for mod. field inversion in EC_GROUP 2019-02-20 19:54:19 +02:00
ec_key.c Fix segfault in RSA_free() (and DSA/DH/EC_KEY) 2018-09-05 15:22:35 +03:00
ec_kmeth.c Update copyright year 2018-09-11 13:45:17 +01:00
ec_lcl.h [ec] Match built-in curves on EC_GROUP_new_from_ecparameters 2019-09-09 14:16:18 +03:00
ec_lib.c [crypto/ec] for ECC parameters with NULL or zero cofactor, compute it 2019-09-07 03:57:52 +03:00
ec_mult.c Update copyright year 2019-05-28 14:49:38 +02:00
ec_oct.c Deprecate the EC curve type specific functions in 1.2.0 2018-07-31 09:08:50 +01:00
ec_pmeth.c Update copyright year 2019-05-28 14:49:38 +02:00
ec_print.c Update copyright year 2018-04-17 15:18:40 +02:00
ecdh_kdf.c EVP module documentation pass 2018-10-17 13:31:59 +03:00
ecdh_ossl.c make RSA and DSA operations throw MISSING_PRIVATE_KEY if needed, adapt ECDSA 2019-07-31 17:07:44 +03:00
ecdsa_ossl.c make RSA and DSA operations throw MISSING_PRIVATE_KEY if needed, adapt ECDSA 2019-07-31 17:07:44 +03:00
ecdsa_sign.c
ecdsa_vrf.c
eck_prn.c Update copyright year 2018-09-11 13:45:17 +01:00
ecp_mont.c SCA hardening for mod. field inversion in EC_GROUP 2019-02-20 19:54:19 +02:00
ecp_nist.c SCA hardening for mod. field inversion in EC_GROUP 2019-02-20 19:54:19 +02:00
ecp_nistp224.c [ec/ecp_nistp*.c] restyle: use {} around else too 2019-09-07 02:21:13 +03:00
ecp_nistp256.c [ec/ecp_nistp*.c] restyle: use {} around else too 2019-09-07 02:21:13 +03:00
ecp_nistp521.c [ec/ecp_nistp*.c] restyle: use {} around else too 2019-09-07 02:21:13 +03:00
ecp_nistputil.c Fix various mistakes in ec_GFp_nistp_recode_scalar_bits comment. 2019-05-31 18:26:19 -04:00
ecp_nistz256.c Added NULL check to BN_clear() & BN_CTX_end() 2019-03-19 07:28:39 +01:00
ecp_nistz256_table.c
ecp_oct.c Update copyright year 2018-09-11 13:45:17 +01:00
ecp_smpl.c Added NULL check to BN_clear() & BN_CTX_end() 2019-03-19 07:28:39 +01:00
ecx_meth.c Fix Typos 2019-07-31 19:48:30 +02:00