<?xml version="1.0" encoding="US-ASCII"?>

<!-- xml2rfc v1 -->

<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes"?>
<?rfc comments="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc rfcedstyle="yes"?>

<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
<!ENTITY rfc2119 PUBLIC "" "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2119.xml">
<!ENTITY rfc5996 PUBLIC "" "http://xml.resource.org/public/rfc/bibxml/reference.RFC.5996.xml">
<!ENTITY rfc2412 PUBLIC "" "http://xml.resource.org/public/rfc/bibxml/reference.RFC.2412.xml">
<!ENTITY rfc3526 PUBLIC "" "http://xml.resource.org/public/rfc/bibxml/reference.RFC.3526.xml">
<!ENTITY rfc5114 PUBLIC "" "http://xml.resource.org/public/rfc/bibxml/reference.RFC.5114.xml">
<!ENTITY rfc5903 PUBLIC "" "http://xml.resource.org/public/rfc/bibxml/reference.RFC.5903.xml">
]>

<rfc number="6989" ipr="trust200902" category="std" updates="5996" submissionType="IETF" consensus="yes">

  <front>
    <title abbrev="DH Tests">Additional Diffie-Hellman Tests
for&nbsp;the&nbsp;Internet&nbsp;Key&nbsp;Exchange&nbsp;Protocol&nbsp;Version&nbsp;2&nbsp;(IKEv2)</title>

    <author initials="Y." surname="Sheffer" fullname="Yaron Sheffer">
      <organization abbrev="Porticor">Porticor</organization>
      <address>
        <email>yaronf.ietf@gmail.com</email>
      </address>
    </author>

    <author initials="S." surname="Fluhrer" fullname="Scott Fluhrer">
      <organization abbrev="Cisco">Cisco Systems</organization>
      <address>
        <postal>
          <street>1414 Massachusetts Ave.</street>
          <city>Boxborough</city>
          <region>MA</region>
          <code>01719</code>
          <country>USA</country>
        </postal>
        <email>sfluhrer@cisco.com</email>
      </address>
    </author>

    <date month="July" year="2013"/>

    <workgroup>ipsecme</workgroup>

<keyword>Elliptic Curve cryptography, secret key reuse, recipient tests</keyword>

    <abstract>
      <t>
This document adds a small number of mandatory tests required for the secure
operation of the Internet Key Exchange Protocol version 2 (IKEv2) with
elliptic curve groups. No change is required to IKE implementations that
use modular exponential groups, other than a few rarely used so-called
Digital Signature Algorithm (DSA) groups. This document updates the
IKEv2 protocol, RFC 5996.</t>

    </abstract>
  </front>
  <middle>
    <section title="Introduction" anchor="d1e393">
      <t>
IKEv2 <xref target="RFC5996"/> consists of the establishment of a shared secret
using the Diffie-Hellman (DH) protocol, followed by authentication of the two
peers. Existing implementations typically use modular exponential (MODP) DH
groups, such as those defined in <xref target="RFC3526"/>.</t>

      <t>
IKEv2 does not require that any tests be performed by a peer receiving a public
Diffie-Hellman key from the other peer. This is fine for the common case of
MODP groups. For other DH groups, when peers reuse DH values across multiple
IKE sessions, the lack of tests by the recipient results in a potential
vulnerability (see  <xref target="sub_DH_Key_Reuse"/> for more details). In
particular, this is true for Elliptic Curve (EC) groups, whose use is becoming
ever more popular. This document defines such tests for several types of DH
groups.</t>

      <t>
In addition, this document describes another potential attack related to
the reuse of DH keys: a timing attack. This additional material is taken
from <xref target="RFC2412"/>.</t>

      <t>
This document updates <xref target="RFC5996"/> by adding security requirements
that apply to many of the protocol's implementations.</t>

      <section title="Conventions Used in This Document" anchor="d1e439">
        <t>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be
interpreted as described in <xref target="RFC2119"/>.</t>

      </section>
    </section>

    <section title="Group Membership Tests" anchor="d1e454">
      <t>
This section describes the tests that need to be performed by IKE peers
receiving a Key Exchange (KE) payload. The tests are RECOMMENDED for all
implementations but only REQUIRED for those that reuse DH private keys (as
defined in <xref target="RFC5996"/>, Section 2.12). The tests apply to the
recipient of a KE payload and describe how it should check the received
payload. They are listed here according to the DH group being used.</t>

      <section title="Sophie Germain Prime MODP Groups" anchor="sub_Regular_MODP_Groups_">
        <t>
These are currently the most commonly used groups; all these groups have the
property that (p-1)/2 is also prime; this section applies to any such MODP
group. Each recipient MUST verify that the peer's public value r is in the
legal range (1 &lt; r &lt; p-1). According to <xref target="Menezes"/>, Section
2.2, even with this check there remains the possibility of leaking a single bit
of the secret exponent when DH keys are reused; this amount of leakage is
insignificant.</t>

        <t>
See  <xref target="sec_IANA_Considerations"/> for the specific groups covered
by this section.</t>
      </section>

      <section title="MODP Groups with Small Subgroups" anchor="sub_MODP_Groups_with">
        <t><xref target="RFC5114"/> defines modular exponential groups with
small subgroups; these are modular exponential groups with comparatively small
subgroups, and all have (p-1)/2 composite. Section 2.1 of <xref
target="Menezes"/> describes some informational leakage from a small-subgroup
attack on these groups if the DH private value is reused. </t>

        <t>
This leakage can be prevented if the recipient performs a test on the peer's
public value; however, this test is expensive (approximately as expensive as
what reusing DH private values saves). In addition, the NIST standard (<xref
target="NIST-800-56A"/>, Section 5.6.2.4) requires that test; hence,
anyone needing to conform to that standard will need to implement the
test anyway.</t>

        <t>
Because of the above, the IKE implementation MUST choose between one of the
following two options:</t>

        <t>
          <list style="symbols">
            <t>
It MUST check both that the peer's public value is in range (1 &lt; r &lt; p-1)
and that r^q = 1 mod p (where q is the size of the subgroup, as listed in
the RFC defining the group).

 DH private values MAY then be reused. This option is appropriate if
conformance to <xref target="NIST-800-56A"/> is required.</t>

            <t>
It MUST NOT reuse DH private values (that is, the DH private value for each DH
exchange MUST be generated from a fresh output of a cryptographically secure
random number generator), and it MUST check that the peer's public value is in
range (1 &lt; r &lt; p-1). This option is more appropriate if conformance to
<xref target="NIST-800-56A"/> is not required. </t>

          </list>
        </t>
        <t>
See  <xref target="sec_IANA_Considerations"/> for the specific groups covered
by this section.</t>

      </section>

      <section title="Elliptic Curve Groups" anchor="sub_Elliptic_Curve_Groups_">
        <t>
IKEv2 can be used with elliptic curve groups defined over a field GF(p) <xref
target="RFC5903"/> <xref target="RFC5114"/>. According to <xref
target="Menezes"/>, Section 2.3, there is some informational leakage
possible. A receiving peer MUST check that its peer's public value is valid;
that is, the x and y parameters from the peer's public value satisfy the curve
equation, y^2 = x^3 + ax + b mod p (where for groups 19, 20, and 21,
a=-3 (mod p), and all other values of a, b, and p for the group are listed in
the RFC defining the group).</t>

        <t>
We note that an additional check to ensure that the public value is not the
point at infinity is not needed, because IKE (see Section 7 of <xref
target="RFC5903"/>) does not allow for encoding this value.</t>

        <t>
See  <xref target="sec_IANA_Considerations"/> for the specific groups covered
by this section.</t>

      </section>

      <section title="Transition" anchor="d1e597">
        <t>
Existing implementations of IKEv2 with Elliptic Curve Diffie-Hellman (ECDH)
groups may be modified to include the tests described in the current
document, even if they do not reuse DH keys. The tests can be considered
as sanity checks and will prevent the code having to handle inputs
that it may not have been designed to handle.</t>

        <t>
ECDH implementations that do reuse DH keys MUST be enhanced to include the
above tests.</t>

      </section>

      <section title="Protocol Behavior" anchor="sub_Protocol_Behavior">
        <t>
The recipient of a DH public key that fails one of the above tests must assume
that the sender is either truly malicious or has a bug in its implementation.
The behavior defined below attempts to balance resistance to attackers that
are trying to disrupt the IKE exchange, against the need to help a badly
implemented peer by providing useful error indications. </t>

        <t>
If this error happens during the IKE_SA_INIT exchange, then the recipient MUST
drop the message that contains an invalid KE payload and MUST NOT use that
message when creating the IKE security association (SA).</t>

        <t>
If the implementation employs the DoS-resistant behavior proposed in Section
2.4 of <xref target="RFC5996"/>, it may simply ignore the erroneous request or
response message, and continue waiting for a later message containing a
legitimate KE payload.</t>

        <t>
If DoS-resistant behavior is not implemented and the invalid KE payload was in
the IKE_SA_INIT request, the implementation MAY send an INVALID_SYNTAX error
notification back and remove the in-progress IKE SA; if the invalid KE payload
was in the IKE_SA_INIT response, then the implementation MAY simply delete the
half-created IKE SA and re-initiate the exchange.</t>

        <t>
If the invalid KE payload is received during the CREATE_CHILD_SA exchange (or
any other exchange after the IKE SA has been established) and the invalid KE
payload is in the request message, the Responder MUST reply with an
INVALID_SYNTAX error notification and drop the IKE SA. If the invalid KE
payload is in a response, the Initiator getting this reply MUST immediately
delete the IKE SA by sending an IKE SA Delete notification as a new
exchange. In this case, the sender evidently has an implementation bug, and
dropping the IKE SA makes it easier to detect.</t>

      </section>
    </section>

    <section title="Side-Channel Attacks" anchor="sec_Side_Channel_Attacks">
      <t>
In addition to the small-subgroup attack, there is also a potential timing
attack on IKE peers when they are reusing Diffie-Hellman secret values. This is
a side-channel attack, which means that it may or may not be a vulnerability in
certain cases, depending on implementation details and the threat model.</t>

      <t>
The remainder of this section is quoted from <xref target="RFC2412"/>, Section
5, with a few minor clarifications. This attack still applies to IKEv2
implementations, and both to MODP groups and ECDH groups. We also note that
more efficient countermeasures are available for EC groups represented in
projective form, but these are outside the scope of the current document.</t>

      <t>
Timing attacks that are capable of recovering the exponent value used in
Diffie-Hellman calculations have been described by Paul Kocher <xref
target="Kocher"/>. In order to nullify the attack, implementors must take pains
to obscure the sequence of operations involved in carrying out modular
exponentiations.</t>

      <t>
One potential method to foil these timing attacks is to use a "blinding
factor". In this method, a group element, r, is chosen at random, and its
multiplicative inverse modulo p is computed, which we'll
call r_inv. &nbsp;r_inv can be computed by the Extended Euclidean Method,
using r and p as inputs. When an exponent x is chosen, the value r_inv^x
is also calculated. Then, when calculating (g^y)^x, the implementation
will calculate this sequence:</t>

      <figure><artwork><![CDATA[
   A = r*g^y
   B = A^x = (r*g^y)^x = (r^x)(g^(xy))
   C = B*r_inv^x = (r^x)(r^(-1*x))(g^(xy)) = g^(xy)
]]></artwork></figure>

      <t>
The blinding factor is only necessary if the exponent x is used more than 100
times.</t>

    </section>

    <section title="Security Considerations" anchor="d1e691">
      <t>
This entire document is concerned with the IKEv2 security protocol and the need
to harden it in some cases.</t>

      <section title="DH Key Reuse and Multiple Peers" anchor="sub_DH_Key_Reuse">
        <t>
This section describes one variant of the attack prevented by the tests defined
above.</t>

        <t>
Suppose that IKE peer Alice maintains IKE security associations with peers Bob
and Eve. &nbsp;Alice uses the same secret ECDH key for both SAs, which is
allowed with some restrictions. If Alice does not implement these tests,
Eve will be able to send a malformed public key, which would allow her
to efficiently determine Alice's private key (as described in
Section 2 of <xref target="Menezes"/>). Since the key is shared, Eve
will be able to obtain Alice's shared IKE SA key with Bob.</t>

      </section>
      <section title="DH Key Reuse: Variants" anchor="d1e720">
        <t>
Private DH keys can be reused in different ways, with subtly different security
implications. For example:</t>

        <t>
          <list style="numbers">
            <t>
DH keys are reused for multiple connections (IKE SAs) to the same peer and for
connections to different peers.</t>

            <t>
DH keys are reused for multiple connections to the same peer (e.g., when the
peer is identified by its IP address) but not for different peers.</t>

            <t>
DH keys are reused only when they had not been used to complete an exchange,
e.g., when the peer replies with an INVALID_KE_PAYLOAD notification.</t>

          </list>
        </t>

        <t>
Both the small-subgroup attack and the timing attack described in this document
apply at least to options #1 and #2.</t>

      </section>
      <section title="Groups Not Covered by This RFC" anchor="d1e742">
        <t>
There are a number of group types that are not specifically addressed by this
RFC. A document that defines such a group MUST describe the tests required by
that group.</t>

        <t>
One specific type of group would be an even-characteristic elliptic curve
group. Now, these curves have cofactors greater than 1; this leads to a
possibility of some information leakage. There are several ways to address this
information leakage, such as performing a test analogous to the test in
<xref target="sub_MODP_Groups_with"/> or adjusting the ECDH operation to
avoid this leakage (such as Elliptic Curve Cryptography Cofactor
Diffie-Hellman (ECC CDH), where the shared secret really is hxyG).
Because the appropriate test depends on how the group is defined, we
cannot document it in advance.</t>

      </section>

      <section title="Behavior upon Test Failure" anchor="d1e755">
        <t>
The behavior recommended in  <xref target="sub_Protocol_Behavior"/> is in line
with generic error treatment during the IKE_SA_INIT exchange, per
Section 2.21.1 of <xref target="RFC5996"/>. The sender is not required
to send back an error notification, and the recipient cannot depend
on this notification because it is unauthenticated and may in fact
have been sent by an attacker trying to launch a DoS attack on the connection. Thus,
the notification is only useful to debug implementation errors.</t>

        <t>
On the other hand, the error notification is secure in the sense that no secret
information is leaked. All IKEv2 Diffie-Hellman groups are publicly known, and
none of the tests defined here depend on any private key. In fact, the
tests can all be performed by an eavesdropper.</t>

        <t>
The situation when the failure occurs in the CREATE_CHILD_SA exchange is
different, since everything is protected by an IKE SA. The peers are
authenticated, and error notifications can be relied on. See Section 2.21.3 of
<xref target="RFC5996"/> for more details on error handling in this case.</t>

      </section>
    </section>

    <section title="IANA Considerations" anchor="sec_IANA_Considerations">
      <t>
IANA has added a column named "Recipient Tests" to
the Transform Type 4 - Diffie-Hellman Group Transform IDs registry for IKEv2 <xref target="IANA-IKEv2-Registry"/>.
</t>

      <t>
This column has been initially populated as follows.</t>

      <texttable>
        <ttcol align="center">
Number</ttcol>
        <ttcol align="center">
Recipient Tests</ttcol>
        <c>
1, 2, 5, 14, 15, 16, 17, 18</c>
        <c>
RFC 6989, Section 2.1</c>
        <c>
22, 23, 24</c>
        <c>
RFC 6989, Section 2.2</c>
        <c>
19, 20, 21, 25, 26, 27, 28, 29, 30</c>
        <c>
RFC 6989, Section 2.3</c>
      </texttable>

      <t>
Groups 27-30 are defined in <xref target="RFC6954"/>.</t>

      <t>
Future documents that define new DH groups for IKEv2 are REQUIRED to provide
this information for each new group, possibly by referring to the current
document.</t>

    </section>
    <section title="Acknowledgements" anchor="d1e877">
      <t>
We would like to thank Dan Harkins, who initially raised this issue on the
IPsec mailing list. Thanks to Tero Kivinen and Rene Struik for their useful
comments. Much of the text in <xref target="sec_Side_Channel_Attacks"/> is
taken from <xref target="RFC2412"/>, and we would like to thank its author,
Hilarie Orman.</t>

<t>
   The document was originally prepared using the lyx2rfc tool, created by Nico
   Williams.
</t>

    </section>
  </middle>

  <back>
<?rfc rfcedstyle="no"?>
    <references title="Normative References">
&rfc2119;
&rfc5996;
</references>

    <references title="Informative References">
&rfc2412;
&rfc3526;
&rfc5114;
&rfc5903;

<!-- draft-merkle-ikev2-ke-brainpool (RFC 6954) -->
<reference anchor="RFC6954">
<front>
<title>Using the Elliptic Curve Cryptography (ECC) Brainpool Curves for IKEv2
Key Exchange</title>
<author initials='J' surname='Merkle' fullname='Johannes Merkle'>
    <organization />
</author>
<author initials='M' surname='Lochter' fullname='Manfred Lochter'>
    <organization />
</author>
<date month='July' year='2013' />
</front>
<seriesInfo name='RFC' value='6954' />
</reference>

<reference anchor="NIST-800-56A">
<front>
<title>Recommendation for Pair-Wise Key
Establishment Schemes Using Discrete Logarithm Cryptography
(Revised)</title>
<author><organization>National Institute of Standards and
Technology (NIST) </organization></author>
<date month="March" year="2007"/>
</front>
<seriesInfo name="NIST" value="PUB 800-56A"/>
</reference>

<reference anchor="Kocher" target="http://www.cryptography.com/timingattack/paper.html">
<front>
<title>Timing Attacks on Implementations of Diffie-Hellman, RSA, DSS, and Other
Systems</title>
<author initials="P. C." surname="Kocher" fullname="Paul C. Kocher">
<organization/></author>
<date year="1996" month="December"/>
</front>
</reference> 

<reference anchor="Menezes"
 target="http://www.cacr.math.uwaterloo.ca/techreports/2008/cacr2008-24.pdf">
<front>
<title>On Reusing Ephemeral Keys In Diffie-Hellman Key Agreement Protocols</title>
<author initials="A." surname="Menezes" fullname="A. Menezes">
<organization/></author>
<author initials="B." surname="Ustaoglu" fullname="B. Ustaoglu">
<organization/></author>
<date year="2008" month="December"/>
</front>
</reference> 

<reference anchor="IANA-IKEv2-Registry"
target="http://www.iana.org/assignments/ikev2-parameters/">
<front>
<title>Internet Key Exchange Version 2 (IKEv2) Parameters</title>
<author><organization>IANA</organization></author>
</front>
</reference>

</references>

  </back>
<?rfc rfcedstyle="yes"?>
</rfc>
