<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='./rfc2629.xslt' ?>

<?rfc toc="yes"?>
<?rfc symrefs="no"?>
<?rfc compact="no" ?>
<?rfc sortrefs="yes" ?>
<?rfc strict="yes" ?>
<?rfc linkmailto="yes" ?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" >

<?xml-stylesheet type='text/xsl' href='./rfc2629.xslt' ?>

<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc compact="no" ?>
<?rfc subcompact="yes" ?>
<?rfc sortrefs="yes" ?>
<rfc ipr="trust200902" docName="draft-ietf-tls-encrypt-then-mac-02.txt" category="std">

  <!-- ======================================================================== -->
  <front>
    <title abbrev="Encrypt-then-MAC-for-TLS">Encrypt-then-MAC for TLS and DTLS</title>
    <author initials="P." surname="Gutmann" fullname="Peter Gutmann">
      <organization abbrev="University of Auckland">University of Auckland</organization>
      <address>
        <postal>
          <street>Department of Computer Science</street>
          <city>University of Auckland</city>
          <city>Auckland</city>
          <country>New Zealand</country>
        </postal>
        <email>pgut001@cs.auckland.ac.nz</email>
      </address>
    </author>
    <date year="2014"/>
    <area>Security Area</area>
    <workgroup>TLS Working Group</workgroup>
    <keyword>Internet-Draft</keyword>
    <abstract>
      <t>

This document describes a means of negotiating the use of the encrypt-then-MAC
security mechanism in place of TLS'/DTLS' existing MAC-then-encrypt one, which
has been the subject of a number of security vulnerabilities over a period of
many years.

      </t>
    </abstract>
  </front>
  <!-- ======================================================================== -->
  <middle>

    <!-- ====================================================================== -->
    <section anchor="introduction" title="Introduction">
      <t>

<xref target="TLS">TLS</xref> and <xref target="DTLS">DTLS</xref> use a 
MAC-then-encrypt construction that was regarded as secure at the time the
original SSL protocol was specified in the mid-1990s, but that is no longer
regarded as secure <xref target="EncryptThenMAC"/> 
<xref target="EncryptThenAuth"/>.  This construction, as used in TLS and later
DTLS, has been the subject of numerous security vulnerabilities and attacks
stretching over a period of many years. This document specifies a means of
switching to the more secure encrypt-then-MAC construction as part of the
TLS/DTLS handshake, replacing the current MAC-then-encrypt construction.

      </t>
      <section anchor="mustshouldmay" title="Conventions Used in This Document">
        <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 anchor="negotiation" title="Negotiating Encrypt-then-MAC">
      <t>

The use of encrypt-then-MAC is negotiated via TLS/DTLS extensions as defined
in <xref target="TLS">TLS</xref>.  On connecting, the client includes the
encrypt_then_mac extension in its client_hello if it wishes to use 
encrypt-then-MAC rather than the default MAC-then-encrypt.  If the server is
capable of meeting this requirement, it responds with an encrypt_then_mac in
its server_hello.  The "extension_type" value for this extension SHALL be 22
(0x16) and the "extension_data" field of this extension SHALL be empty.  The
client and server MUST NOT use encrypt-then-MAC unless both sides have
successfully exchanged encrypt_then_mac extensions.

      </t>
      <section anchor="negotiation_rationale" title="Rationale">
        <t>

The use of TLS/DTLS extensions to negotiate an overall switch is preferable to
defining new ciphersuites because the latter would result in a Cartesian
explosion of suites, potentially requiring duplicating every single existing
suite with a new one that uses encrypt-then-MAC.  In contrast the approach
presented here requires just a single new extension type with a corresponding
minimal-length extension sent by client and server.

        </t>
        <t>

Another possibility for introducing encrypt-then-MAC would be to make it part
of TLS 1.3, however this would require the implementation and deployment of
all of TLS 1.2 just to support a trivial code change in the order of
encryption and MAC'ing.  In contrast deploying encrypt-then-MAC via the
TLS/DTLS extension mechanism required changing less than a dozen lines of code
in one implementation (not including the handling for the new extension type,
which was a further 50 or so lines of code).

        </t>
        <t>

The use of extensions precludes use with SSL 3.0, but then it's likely that
anything still using this nearly two decades-old protocol will be vulnerable
to any number of other attacks anyway, so there seems little point in bending
over backwards to accomodate SSL 3.0.

        </t>
      </section>
    </section>
    <!-- ====================================================================== -->
    <section anchor="applying" title="Applying Encrypt-then-MAC">
      <t>

Once the use of encrypt-then-MAC has been negotiated, processing of TLS/DTLS
packets switches from the standard:

        <figure><artwork><![CDATA[
encrypt( data || MAC || pad )
        ]]></artwork></figure>

to the new:

        <figure><artwork><![CDATA[
encrypt( data || pad ) || MAC
        ]]></artwork></figure>

with the MAC covering the entire packet up to the start of the MAC value.  In
<xref target="TLS">TLS</xref> notation the MAC calculation is:

        <figure><artwork><![CDATA[
MAC(MAC_write_key, seq_num +
    TLSCipherText.type +
    TLSCipherText.version +
    TLSCipherText.length +
    ENC(content + padding + padding_length));
        ]]></artwork></figure>

for TLS 1.0 without the explicit IV and:

        <figure><artwork><![CDATA[
MAC(MAC_write_key, seq_num +
    TLSCipherText.type +
    TLSCipherText.version +
    TLSCipherText.length +
    IV +
    ENC(content + padding + padding_length));
        ]]></artwork></figure>

for TLS 1.1 and greater with explicit IV (for DTLS the sequence number is
replaced by the combined epoch and sequence number as per 
<xref target="DTLS">DTLS</xref>).  The final MAC value is then appended to the
encrypted data and padding.  This calculation is identical to the existing one
with the exception that the MAC calculation is run over the payload ciphertext
(the TLSCipherText PDU) rather than the plaintext (the TLSCompressed PDU).

      </t>
      <t>

The overall TLS packet <xref target="TLS"/> is then:

        <figure><artwork><![CDATA[
struct {
       ContentType type;
       ProtocolVersion version;
       uint16 length;
       GenericBlockCipher fragment;
       opaque MAC;
       } TLSCiphertext;
        ]]></artwork></figure>

The equivalent DTLS packet <xref target="DTLS"/> is then:

        <figure><artwork><![CDATA[
struct {
       ContentType type;
       ProtocolVersion version;
       uint16 epoch;
       uint48 sequence_number;
       uint16 length;
       GenericBlockCipher fragment;
       opaque MAC;
       } TLSCiphertext;
        ]]></artwork></figure>

This is identical to the existing TLS/DTLS layout with the only difference
being that the MAC value is moved outside the encrypted data.

      </t>
      <t>

Note from the GenericBlockCipher annotation that this only applies to standard
block ciphers that have distinct encrypt and MAC operations.  It does not
apply to GenericStreamCiphers, or to GenericAEADCiphers that already include
integrity protection with the cipher.  If a server receives an 
encrypt-then-MAC request extension from a client and then selects a stream or
AEAD cipher suite, it MUST NOT send an encrypt-then-MAC response extension
back to the client.

      </t>
      <t>

Decryption reverses this processing.  The MAC SHALL be evaluated before any
further processing such as decryption is performed, and if the MAC
verification fails then processing SHALL terminate immediately.  For TLS, a
fatal bad_record_mac MUST be generated <xref target="TLS"/>.  For DTLS, the
record MUST be discarded and a fatal bad_record_mac MAY be generated 
<xref target="TLS"/>.  This immediate response to a bad MAC eliminates any
timing channels that may be available through the use of manipulated packet
data.

      </t>
      <t>

Some implementations may prefer to use a truncated MAC rather than a 
full-length one.  In this case they MAY negotiate the use of a truncated MAC
through the TLS truncated_hmac extension as defined in 
<xref target="TLS-Ext">TLS-Ext</xref>.

      </t>
      <section anchor="applying_rehandshake" title="Rehandshake Issues">
        <t>

The status of encrypt-then-MAC vs. MAC-then-encrypt can potentially change
during a rehandshake.  Implementations SHOULD retain the current session state
for the renegotiated session (in other words if the mechanism for the current
session is X then the renegotiated session should also use X).  While
implementations SHOULD NOT change the state during a rehandshake, if they wish
to be more flexible then the following rules apply:

        </t>
        <texttable anchor="table_renegotiation" title="Encrypt-then-MAC with Renegotiation">

          <ttcol align='center'>Current Session</ttcol>
          <ttcol align='center'>Renegotiated Session</ttcol>
          <ttcol align='center'>Action to take</ttcol>

          <c>MAC-then-encrypt</c>
          <c>MAC-then-encrypt</c>
          <c>No change</c>

          <c>MAC-then-encrypt</c>
          <c>Encrypt-then-MAC</c>
          <c>Upgrade to Encrypt-then-MAC</c>

          <c>Encrypt-then-MAC</c>
          <c>MAC-then-encrypt</c>
          <c>Error</c>

          <c>Encrypt-then-MAC</c>
          <c>Encrypt-then-MAC</c>
          <c>No change</c>

        </texttable>
        <t>

As the above table points out, implementations MUST NOT renegotiate a
downgrade from Encrypt-then-MAC to MAC-then-encrypt.  Note that a client or
server that doesn't wish to implement the mechanism-change-during-rehandshake
ability can (as a client) not request a mechanism change and (as a server)
deny the mechanism change.

        </t>
        <t>

If an upgrade from MAC-then-encrypt to Encrypt-then-MAC is negotiated as per
the second line in the table above then the change will take place in the
first message that follows the Change Cipher Spec (CCS).  In other words all
messages up to and including the CCS will use MAC-then-encrypt, and then the
message that follows will continue with Encrypt-then-MAC.

        </t>
      </section>
    </section>
    <!-- ====================================================================== -->
    <section anchor="security" title="Security Considerations">
      <t>

This document defines an improved security mechanism encrypt-then-MAC to
replace the current MAC-then-encrypt one.  This is regarded as more secure
than the current mechanism <xref target="EncryptThenMAC"/> 
<xref target="EncryptThenAuth"/>, and should mitigate or eliminate a number of
attacks on the current mechanism, provided that the instructions on MAC
processing given in <xref target="applying"/> are applied.

      </t>
      <t>

An active attacker who can emulate a client or server with extension
intolerance may cause some implementations to fall back to older protocol
versions that don't support extensions, which will in turn force a fallback to
non-Encrypt-then-MAC behaviour.  A straightforward solution to this problem is
to avoid fallback to older, less secure protocol versions.  If fallback
behaviour is unavoidable then mechanisms to address this issue, which affects
all capabilities that are negotiated via TLS extensions, are being developed
by the TLS working group <xref target="DownGradeSCSV"/>.  Anyone concerned
about this type of attack should consult the TLS working group documents for
guidance on appropriate defence mechanisms.

      </t>
    </section>
    <!-- ====================================================================== -->
    <section anchor="iana" title="IANA Considerations">
      <t>

IANA has added the extension code point 22 (0x16) for the encrypt_then_mac
extension to the TLS ExtensionType values registry as specified in
<xref target="TLS">TLS</xref>.

      </t>
    </section>
    <!-- ====================================================================== -->
    <section anchor="ack" title="Acknowledgements">
      <t>

The author would like to thank Martin Rex, Dan Shumow, and the members of the
TLS mailing list for their feedback on this document.

      </t>
    </section>
    <!-- ====================================================================== -->

  </middle>
  <!-- ======================================================================== -->
  <back>

    <references title="Normative References">

      <reference anchor='RFC2119'>
        <front>
          <title abbrev='RFC Key Words'>Key words for use in RFCs to Indicate Requirement Levels</title>
          <author initials='S.' surname='Bradner' fullname='Scott Bradner'>
            <organization>Harvard University</organization>
          </author>
          <date year='1997' month='March' />
          <area>General</area>
          <keyword>keyword</keyword>
        </front>
        <seriesInfo name='BCP' value='14' />
        <seriesInfo name='RFC' value='2119' />
        <format type='TXT' target='ftp://ftp.isi.edu/in-notes/rfc2119.txt' />
        <format type='HTML' target='http://xml.resource.org/public/rfc/html/rfc2119.html' />
        <format type='XML' target='http://xml.resource.org/public/rfc/xml/rfc2119.xml' />
      </reference>

      <reference anchor='TLS'>
        <front>
          <title>The Transport Layer Security (TLS) Protocol Version 1.2</title>
          <author fullname="Tim Dierks" initials="T" surname="Dierks">
            <organization>Independent</organization>
          </author>
          <author fullname="Eric Rescorla" initials="E" surname="Rescorla">
            <organization>RTFM, Inc.</organization>
          </author>
          <date year='2008' month='August' />
        </front>
        <seriesInfo name='RFC' value='5246' />
        <format type='TXT' target='http://www.ietf.org/rfc/rfc5246.txt' />
      </reference>

      <reference anchor='TLS-Ext'>
        <front>
          <title>Transport Layer Security (TLS) Extensions</title>
          <author fullname="Simon Blake-Wilson" initials="S" surname="Blake-Wilson">
            <organization>SafeNet Technologies BV</organization>
          </author>
          <author fullname="Magnus Nystrom" initials="M" surname="Nystrom">
            <organization>RSA Security</organization>
          </author>
          <author fullname="David Hopwood" initials="D" surname="Hopwood">
            <organization>Independent Consultant</organization>
          </author>
          <author fullname="Jan Mikkelsen" initials="J" surname="Mikkelsen">
            <organization>Transactionware</organization>
          </author>
          <author fullname="Tim Wright" initials="T" surname="Wright">
            <organization>Vodafone</organization>
          </author>
          <date year='2006' month='April' />
        </front>
        <seriesInfo name='RFC' value='4366' />
        <format type='TXT' target='http://www.ietf.org/rfc/rfc4366.txt' />
      </reference>

      <reference anchor='DTLS'>
        <front>
          <title>Datagram Transport Layer Security Version 1.2</title>
          <author fullname="Eric Rescorla" initials="E" surname="Rescorla">
            <organization>RTFM, Inc.</organization>
          </author>
          <author fullname="Nagendra Modadugu" initials="N" surname="Modadugu">
            <organization>Stanford University</organization>
          </author>
          <date year='2012' month='January' />
        </front>
        <seriesInfo name='RFC' value='6347' />
        <format type='TXT' target='http://www.ietf.org/rfc/rfc6347.txt' />
      </reference>

    </references>
    <references title="Informative References">

      <reference anchor='EncryptThenMAC'>
        <!-- No-one seems to know how to reference publications like
             conference proceedings in RFC-XML, this is one attempt... -->
        <front>
          <title>Authenticated Encryption: Relations among notions and analysis of the generic composition paradigm</title>
          <author fullname="Mihir Bellare" initials="M" surname="Bellare">
            <organization>UCSD</organization>
          </author>
          <author fullname="Chanathip Namprempre" initials="C" surname="Namprempre">
            <organization>Thammasat University</organization>
          </author>
          <date year='2000' month='December' />
<!--      <note>Proceedings of AsiaCrypt'00, Springer-Verlag LNCS No.1976, p.531</note> -->
        </front>
        <seriesInfo name='Springer-Verlag LNCS' value='1976' />
      </reference>

      <reference anchor='EncryptThenAuth'>
        <!-- No-one seems to know how to reference publications like
             conference proceedings in RFC-XML, this is one attempt... -->
        <front>
          <title>The Order of Encryption and Authentication for Protecting Communications (or: How Secure Is SSL?)</title>
          <author fullname="Hugo Krawczyk" initials="H" surname="Krawczyk">
            <organization abbrev="IBM">IBM Research</organization>
          </author>
          <date year='2001' month='August' />
<!--      <note>Proceedings of Crypto'01, Springer-Verlag LNCS No.2139, p.310</note> -->
        </front>
        <seriesInfo name='Springer-Verlag LNCS' value='2139' />
      </reference>

      <reference anchor='DownGradeSCSV'>
        <front>
          <title>TLS Fallback Signaling Cipher Suite Value (SCSV) for Preventing Protocol Downgrade Attacks</title>
          <author fullname="Bodo Moeller" initials="B" surname="Moeller">
            <organization>Google Switzerland Gmbh</organization>
          </author>
          <author fullname="Adam Langley" initials="A" surname="Langley">
            <organization>Google Inc.</organization>
          </author>
          <date year='2013' month='November' />
        </front>
        <seriesInfo name='RFC' value='XXXX' />
        <format type='TXT' target='http://tools.ietf.org/id/draft-bmoeller-tls-downgrade-scsv-01.txt' />
      </reference>
      
    </references>
    <!-- ====================================================================== -->
  </back>
</rfc>
