1*3d322d52SAndroid Build Coastguard Worker# Diffie-Hellman 2*3d322d52SAndroid Build Coastguard Worker 3*3d322d52SAndroid Build Coastguard Worker## Subgroup confinement attacks 4*3d322d52SAndroid Build Coastguard Worker 5*3d322d52SAndroid Build Coastguard WorkerThe papers by van Oorshot and Wiener [OW96] rsp. Lim and Lee [LL98] show that 6*3d322d52SAndroid Build Coastguard WorkerDiffie-Hellman keys can be found much faster if the short exponents are used and 7*3d322d52SAndroid Build Coastguard Workerif the multiplicative group modulo p contains small subgroups. In particular an 8*3d322d52SAndroid Build Coastguard Workerattacker can try to send a public key that is an element of a small subgroup. If 9*3d322d52SAndroid Build Coastguard Workerthe receiver does not check for such elements then may be possible to find the 10*3d322d52SAndroid Build Coastguard Workerprivate key modulo the order of the small subgroup. Several countermeasures 11*3d322d52SAndroid Build Coastguard Workeragainst such attacks have been proposed: For example IKE uses fields of order p 12*3d322d52SAndroid Build Coastguard Workerwhere p is a safe prime (i.e. $$q=(p-1)/2),$$ hence the only elements of small 13*3d322d52SAndroid Build Coastguard Workerorder are 1 and p-1. 14*3d322d52SAndroid Build Coastguard Worker 15*3d322d52SAndroid Build Coastguard Worker[NIST SP 800-56A] rev. 2, Section 5.5.1.1 only requires that the size of the 16*3d322d52SAndroid Build Coastguard Workersubgroup generated by the generator g is big enough to prevent the baby-step 17*3d322d52SAndroid Build Coastguard Workergiant-step algorithm. I.e. for 80-bit security p must be at least 1024 bits long 18*3d322d52SAndroid Build Coastguard Workerand the prime q must be at least 160 bits long. A 2048 bit prime p and a 224 bit 19*3d322d52SAndroid Build Coastguard Workerprime q are sufficient for 112 bit security. To avoid subgroup confinment 20*3d322d52SAndroid Build Coastguard Workerattacks NIST requires that public keys are validated, i.e. by checking that a 21*3d322d52SAndroid Build Coastguard Workerpublic key y satisfies the conditions $$2 \leq y \leq p-2$$ and $$y^q \mod p = 22*3d322d52SAndroid Build Coastguard Worker1$$ (Section 5.6.2.3.1). Further, after generating the shared secret $$z = 23*3d322d52SAndroid Build Coastguard Workery_a^{x_b} \mod p$$ each party should check that $$z \neq 1.$$ RFC 2785 contains 24*3d322d52SAndroid Build Coastguard Workersimilar recommendations. The public key validation described by NIST requires 25*3d322d52SAndroid Build Coastguard Workerthat the order q of the generator g is known to the verifier. Unfortunately, the 26*3d322d52SAndroid Build Coastguard Workerorder q is missing in [PKCS #3]. [PKCS #3] describes the Diffie-Hellman 27*3d322d52SAndroid Build Coastguard Workerparameters only by the values p, g and optionally the key size in bits. 28*3d322d52SAndroid Build Coastguard Worker 29*3d322d52SAndroid Build Coastguard WorkerThe class DHParameterSpec that defines the Diffie-Hellman parameters in JCE 30*3d322d52SAndroid Build Coastguard Workercontains the same values as [PKCS #3]. In particular, it does not contain the 31*3d322d52SAndroid Build Coastguard Workerorder of the subgroup q. Moreover, the SUN provider uses the minimal sizes 32*3d322d52SAndroid Build Coastguard Workerspecified by NIST for q. Essentially the provider reuses the parameters for DSA. 33*3d322d52SAndroid Build Coastguard Worker 34*3d322d52SAndroid Build Coastguard WorkerTherefore, there is no guarantee that an implementation of Diffie-Hellman is secure against 35*3d322d52SAndroid Build Coastguard Workersubgroup confinement attacks. Without a key validation it is insecure to use the key-pair 36*3d322d52SAndroid Build Coastguard Workergeneration from [NIST SP 800-56A] Section 5.6.1.1 (The key-pair generation there only requires that 37*3d322d52SAndroid Build Coastguard Workerstatic and ephemeral private keys are randomly chosen in the range \\(1..q-1)\\). 38*3d322d52SAndroid Build Coastguard Worker 39*3d322d52SAndroid Build Coastguard WorkerTo avoid big disasters the tests below require that key sizes are not minimal. I.e., currently 40*3d322d52SAndroid Build Coastguard Workerthe tests require at least 512 bit keys for 1024 bit fields. We use this lower limit because that 41*3d322d52SAndroid Build Coastguard Workeris what the SUN provider is currently doing. 42*3d322d52SAndroid Build Coastguard Worker 43*3d322d52SAndroid Build Coastguard WorkerTODO(bleichen): Find a reference supporting or disproving that decision. 44*3d322d52SAndroid Build Coastguard Worker 45*3d322d52SAndroid Build Coastguard Worker## Weak parameters 46*3d322d52SAndroid Build Coastguard Worker 47*3d322d52SAndroid Build Coastguard WorkerThe DH parameters must be carefully chosen to avoid security issues. A panel at 48*3d322d52SAndroid Build Coastguard WorkerEurocrypt'92 discussed the possiblity of trapdoors in DL based primitives 49*3d322d52SAndroid Build Coastguard Worker[Eurocrypt92 panel]. A. Lenstra pointed out that the primes chould be chosen 50*3d322d52SAndroid Build Coastguard Workersuch that the special number field sieve can be used to compute discrete 51*3d322d52SAndroid Build Coastguard Workerlogarithms. Gordon has analyzed methods to generate and detect weak parameters 52*3d322d52SAndroid Build Coastguard Worker[G92]. Section 4 of Gordons paper describes a method that can detect some 53*3d322d52SAndroid Build Coastguard Workerspecial cases, but no general method was given. Recently Fried et al. showed 54*3d322d52SAndroid Build Coastguard Workerthat 1024 bit discrete logarithms with the special number field sieve are 55*3d322d52SAndroid Build Coastguard Workerfeasible [FGHT16]. Moreover some libraries use primes that are susceptible to 56*3d322d52SAndroid Build Coastguard Workerthis attack [FGHT16]. 57*3d322d52SAndroid Build Coastguard Worker 58*3d322d52SAndroid Build Coastguard WorkerTODO(bleichen): So far not test for weak DH parameters has been implemented. 59*3d322d52SAndroid Build Coastguard WorkerPossibly we should at least implement a test that detects special cases, so 60*3d322d52SAndroid Build Coastguard Workerthat weak primes (such as the one used in libtomcrypt) are detected. 61*3d322d52SAndroid Build Coastguard Worker 62*3d322d52SAndroid Build Coastguard WorkerDH implementations are sometimes misconfigured. Adrian et al. [WeakDh] analyzed 63*3d322d52SAndroid Build Coastguard Workervarious implementations and found for example the following problems in the 64*3d322d52SAndroid Build Coastguard Workerparameters: p is sometimes composite, p-1 contains no large prime factor, q is 65*3d322d52SAndroid Build Coastguard Workerused instead of the generator g. 66*3d322d52SAndroid Build Coastguard Worker 67*3d322d52SAndroid Build Coastguard Worker## References 68*3d322d52SAndroid Build Coastguard Worker[Eurocrypt92 panel]: "The Eurocrypt'92 Controversial Issue Trapdoor Primes and Moduli", 69*3d322d52SAndroid Build Coastguard WorkerEUROCRYPT '92, LNCS 658, pp. 194-199. 70*3d322d52SAndroid Build Coastguard Worker 71*3d322d52SAndroid Build Coastguard Worker[G92]: D. M. Gordon. "Designing and detecting trapdoors for discrete log 72*3d322d52SAndroid Build Coastguard Workercryptosystems." CRYPTO’92, pp. 66–75. 73*3d322d52SAndroid Build Coastguard Worker 74*3d322d52SAndroid Build Coastguard Worker\[FGHT16]: J. Fried, P. Gaudry, N. Heininger, E. Thome. "A kilobit hidden SNFS 75*3d322d52SAndroid Build Coastguard Workerdiscrete logarithm computation". http://eprint.iacr.org/2016/961.pdf 76*3d322d52SAndroid Build Coastguard Worker 77*3d322d52SAndroid Build Coastguard Worker[OW96]: P. C. van Oorschot, M. J. Wiener, "On Diffie-Hellman key agreement with short exponents", 78*3d322d52SAndroid Build Coastguard WorkerEurocrypt 96, pp 332–343. 79*3d322d52SAndroid Build Coastguard Worker 80*3d322d52SAndroid Build Coastguard Worker[LL98]: C.H. Lim and P.J. Lee, 81*3d322d52SAndroid Build Coastguard Worker"A key recovery attack on discrete log-based schemes using a prime order subgroup", 82*3d322d52SAndroid Build Coastguard WorkerCRYPTO' 98, pp 249–263. 83*3d322d52SAndroid Build Coastguard Worker 84*3d322d52SAndroid Build Coastguard Worker[WeakDh]: D. Adrian, K. Bhargavan, Z. Durumeric, P. Gaudry, M. Green, 85*3d322d52SAndroid Build Coastguard WorkerJ. A. Halderman, N. Heninger, D. Springall, E. Thomé, Luke Valenta, 86*3d322d52SAndroid Build Coastguard WorkerB. VanderSloot, E. Wustrow, S. Zanella-Béguelink, P. Zimmermann, 87*3d322d52SAndroid Build Coastguard Worker"Imperfect Forward Secrecy: How Diffie-Hellman Fails in Practice" 88*3d322d52SAndroid Build Coastguard Workerhttps://weakdh.org/imperfect-forward-secrecy-ccs15.pdf 89*3d322d52SAndroid Build Coastguard Worker 90*3d322d52SAndroid Build Coastguard Worker[NIST SP 800-56A], revision 2, May 2013 91*3d322d52SAndroid Build Coastguard Workerhttp://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf 92*3d322d52SAndroid Build Coastguard Worker 93*3d322d52SAndroid Build Coastguard Worker[PKCS #3]: "Diffie–Hellman Key Agreement", 94*3d322d52SAndroid Build Coastguard Workerhttp://uk.emc.com/emc-plus/rsa-labs/standards-initiatives/pkcs-3-diffie-hellman-key-agreement-standar.htm 95*3d322d52SAndroid Build Coastguard Worker 96*3d322d52SAndroid Build Coastguard Worker[RFC 2785]: R. Zuccherato, 97*3d322d52SAndroid Build Coastguard Worker"Methods for Avoiding 'Small-Subgroup' Attacks on the Diffie-Hellman Key Agreement Method for S/MIME", 98*3d322d52SAndroid Build Coastguard WorkerMarch 2000 99*3d322d52SAndroid Build Coastguard Workerhttps://www.ietf.org/rfc/rfc2785.txt 100*3d322d52SAndroid Build Coastguard Worker 101*3d322d52SAndroid Build Coastguard Worker<!-- 102*3d322d52SAndroid Build Coastguard Worker## Sources that might be used for additional tests: 103*3d322d52SAndroid Build Coastguard Worker 104*3d322d52SAndroid Build Coastguard WorkerCVE-2015-3193: The Montgomery squaring implementation in crypto/bn/asm/x86_64-mont5.pl 105*3d322d52SAndroid Build Coastguard Workerin OpenSSL 1.0.2 before 1.0.2e on the x86_64 platform, as used by the BN_mod_exp function, 106*3d322d52SAndroid Build Coastguard Workermishandles carry propagation 107*3d322d52SAndroid Build Coastguard Workerhttps://blog.fuzzing-project.org/31-Fuzzing-Math-miscalculations-in-OpenSSLs-BN_mod_exp-CVE-2015-3193.html 108*3d322d52SAndroid Build Coastguard Worker 109*3d322d52SAndroid Build Coastguard WorkerCVE-2016-0739: libssh before 0.7.3 improperly truncates ephemeral secrets generated for the 110*3d322d52SAndroid Build Coastguard Worker(1) diffie-hellman-group1 and (2) diffie-hellman-group14 key exchange methods to 128 bits ... 111*3d322d52SAndroid Build Coastguard Worker 112*3d322d52SAndroid Build Coastguard WorkerCVE-2015-1787 The ssl3_get_client_key_exchange function in s3_srvr.c in OpenSSL 1.0.2 before 113*3d322d52SAndroid Build Coastguard Worker1.0.2a, when client authentication and an ephemeral Diffie-Hellman ciphersuite are enabled, 114*3d322d52SAndroid Build Coastguard Workerallows remote attackers to cause a denial of service (daemon crash) via a ClientKeyExchange 115*3d322d52SAndroid Build Coastguard Workermessage with a length of zero. 116*3d322d52SAndroid Build Coastguard Worker 117*3d322d52SAndroid Build Coastguard WorkerCVE-2015-0205 The ssl3_get_cert_verify function in s3_srvr.c in OpenSSL 1.0.0 before 1.0.0p 118*3d322d52SAndroid Build Coastguard Workerand 1.0.1 before 1.0.1k accepts client authentication with a Diffie-Hellman (DH) certificate 119*3d322d52SAndroid Build Coastguard Workerwithout requiring a CertificateVerify message, which allows remote attackers to obtain access 120*3d322d52SAndroid Build Coastguard Workerwithout knowledge of a private key via crafted TLS Handshake Protocol traffic to a server that 121*3d322d52SAndroid Build Coastguard Workerrecognizes a Certification Authority with DH support. 122*3d322d52SAndroid Build Coastguard Worker 123*3d322d52SAndroid Build Coastguard WorkerCVE-2016-0701 The DH_check_pub_key function in crypto/dh/dh_check.c in OpenSSL 1.0.2 before 124*3d322d52SAndroid Build Coastguard Worker1.0.2f does not ensure that prime numbers are appropriate for Diffie-Hellman (DH) key exchange, 125*3d322d52SAndroid Build Coastguard Workerwhich makes it easier for remote attackers to discover a private DH exponent by making multiple 126*3d322d52SAndroid Build Coastguard Workerhandshakes with a peer that chose an inappropriate number, as demonstrated by a number in an 127*3d322d52SAndroid Build Coastguard WorkerX9.42 file. 128*3d322d52SAndroid Build Coastguard Worker 129*3d322d52SAndroid Build Coastguard WorkerCVE-2006-1115 nCipher HSM before 2.22.6, when generating a Diffie-Hellman public/private key 130*3d322d52SAndroid Build Coastguard Workerpair without any specified DiscreteLogGroup parameters, chooses random parameters that could 131*3d322d52SAndroid Build Coastguard Workerallow an attacker to crack the private key in significantly less time than a brute force attack. 132*3d322d52SAndroid Build Coastguard Worker 133*3d322d52SAndroid Build Coastguard WorkerCVE-2015-1716 Schannel in Microsoft Windows Server 2003 SP2, Windows Vista SP2, Windows Server 134*3d322d52SAndroid Build Coastguard Worker2008 SP2 and R2 SP1, Windows 7 SP1, Windows 8, Windows 8.1, Windows Server 2012 Gold and R2, and 135*3d322d52SAndroid Build Coastguard WorkerWindows RT Gold and 8.1 does not properly restrict Diffie-Hellman Ephemeral (DHE) key lengths, 136*3d322d52SAndroid Build Coastguard Workerwhich makes it easier for remote attackers to defeat cryptographic protection mechanisms via 137*3d322d52SAndroid Build Coastguard Workerunspecified vectors, aka "Schannel Information Disclosure Vulnerability. 138*3d322d52SAndroid Build Coastguard Worker 139*3d322d52SAndroid Build Coastguard WorkerCVE-2015-2419: Random generation of the prime p allows Pohlig-Hellman and probably other 140*3d322d52SAndroid Build Coastguard Workerstuff. 141*3d322d52SAndroid Build Coastguard Worker--> 142