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

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

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

<?rfc symrefs="yes"?>
<?rfc compact="yes" ?>
<?rfc subcompact="yes" ?>
<?rfc sortrefs="yes" ?>
<rfc ipr="trust200902" docName="draft-nir-ipsecme-chacha20-poly1305-00" category="exp">
  <front>
    <title abbrev="ChaCha20 & Poly1305 for IPsec">ChaCha20 and Poly1305 and their use in IPsec</title>
    <author initials="Y." surname="Nir" fullname="Yoav Nir">
      <organization abbrev="Check Point">Check Point Software Technologies Ltd.</organization>
      <address>
        <postal>
          <street>5 Hasolelim st.</street>
          <city>Tel Aviv</city>
          <code>6789735</code>
          <country>Israel</country>
        </postal>
        <email>ynir@checkpoint.com</email>
      </address>
    </author>
    <date year="2014"/>
    <area>Security Area</area>
    <keyword>Internet-Draft</keyword>
    <abstract>
      <t> This document describes the use of the ChaCha20 stream cipher in IPsec, as well as the use of the Poly1305 authenticator, both as stand-alone algorithms, and as a combined mode AEAD algorithm.</t>
    </abstract>
  </front>
  <middle>
    <!-- ====================================================================== -->
    <section anchor="introduction" title="Introduction">
      <t> The Advanced Encryption Standard (AES - <xref target="FIPS-197"/>) has become the gold standard in encryption. Its efficient design, wide implementation, and hardware support allow for high performance in many areas, including IPsec VPNs. On most modern platforms, AES is anywhere from 4x to 10x as fast as the previous most-used cipher, 3-key Data Encryption Standard (3DES - <xref target="FIPS-46"/>), which makes it not only the best choice, but the only choice.</t>
      <t> The problem is that if future advances in cryptanalysis reveal a weakness in AES, VPN users will be in an unenviable position. With the only other widely supported cipher being the much slower 3DES, it is not feasible to re-configure IPsec implementations to use 3DES. <xref target="standby-cipher"/> describes this issue and the need for a standby cipher in greater detail.</t>
      <t> This document proposes such a standby cipher for IPsec. We use ChaCha20 (<xref target="chacha"/>) with or without the Poly1305 (<xref target="poly1305"/>) authenticator.</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="algs" title="The Algorithms">
      <t> The subsections below describe the algorithms used and the AEAD construction.</t>
      <section anchor="chacha_block" title="The ChaCha20 block function">
        <t> The ChaCha20 block function is part of the ChaCha20 stream cipher (<xref target="chacha20"/>). It uses a 256-bit key. There are some variations defined, such as 128-bit keys, and 8- and 12-round variants, but this document defines only a 256-bit key and a 20-round ChaCha. ChaCha20 works on 512-bit blocks (64 bytes, or 16 32-bit words). In this section we will describe the ChaCha block function.</t>
        <t> ChaCha maps an array of sixteen 32-bit input words to sixteen 32-bit output words. The input words are partitioned as follows: <list style="symbols">
          <t> The first 4 words (0-3) are constants: 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574.</t>
          <t> The next 8 words (4-11) are taken from the 256-bit key by reading the bytes in little-endian order, in 4-byte chunks.</t>
          <t> Word 12 is a block counter. Since each block is 64-byte, a 32-bit word is enough for 256 Gigabytes of data.</t>
          <t> Word 13 is called Sender ID, or SID. Note that in the original ChaCha20 word 13 is also part of the block count, in case the encrypted data exceeds 256 gigabytes. That is not necessary for IPsec.</t>
          <t> Words 14-15 are a nonce, which should not be repeated for the same combination of key and sender ID. The 14th word is the least significant 32 bits of the input nonce (nonce | 0xffffffff), while the 15th word is the most significant 32 bits (nonce >> 32). </t></list></t>
        <t> ChaCha20 runs 20 rounds, alternating between "column" and "diagonal" rounds. At the end of 20 rounds, the original input words are added to the output words, and the result is serialized by serializing the numbers in order, with the most significant octet of every 32-bit integer preceding the others. See <xref target="example_block"/> for an example of this serialization.</t>
        <t> The inputs to ChaCha20 are: <list style="symbols">
          <t> A 256-bit key</t>
          <t> A 32-bit sender ID </t>
          <t> A 64-bit nonce, treated as a number </t>
          <t> A 32-bit block count parameter </t></list></t>
        <t> The output is 64 random-looking bytes.</t>
      </section>
      <section anchor="chacha20" title="The ChaCha20 encryption algorithm">
        <t> ChaCha20 is a stream cipher designed by D. J. Bernstein. It is a refinement of the Salsa20 algorithm, and uses a 256-bit key. There are some variations defined, such as 128-bit keys, and 8- and 12-round variants, but this document defines only a 256-bit key and a 20-round ChaCha.</t>
        <t> ChaCha20 successively calls the ChaCha20 block function, with the same key, sender ID and nonce, and with successively increasing block counter parameters. Concatenating the results forms a key stream, which is then XOR-ed with the plaintext. There is no requirement for the plaintext to be an integral multiple of 512-bits. If there is extra keystream from the last block, it is discarded.</t>
        <t> The inputs to ChaCha20 are: <list style="symbols">
          <t> A 256-bit key</t>
          <t> A 32-bit sender ID </t>
          <t> A 32-bit initial counter </t>
          <t> A 64-bit nonce </t>
          <t> an arbitrary-length plaintext</t></list></t>
        <t> The output is an encrypted message of the same length.</t>
      </section>
      <section anchor="_poly1305" title="The Poly1305 algorithm">
        <t> Poly1305 is a one-time authenticator designed by D. J. Bernstein. Poly1305 takes a 32-byte one-time key and a message and produces a 16-byte tag.</t>
        <t> The key MUST NOT be re-used.</t>
        <t> The inputs to Poly1305 are:<list style="symbols">
          <t> A 256-bit one-time key</t>
          <t> An arbitrary length message </t></list></t>
        <t> The output is a 128-bit tag.</t>  
      </section>
      <section anchor="aead" title="AEAD Construction">
        <t> Note: Much of the content of this document, including this AEAD construction is taken from Adam Langley's draft (<xref target="agl-draft"/>) for the use of these algorithms in TLS. The AEAD construction described here is called CHACHA20-POLY1305-ESP.</t>
        <t> The inputs to CHACHA20-POLY1305-ESP are:<list style="symbols">
          <t> A 256-bit key </t>
          <t> A 32-bit sender ID</t>
          <t> A 64-bit nonce</t>
          <t> An arbitrary length plaintext</t>
          <t> Arbitrary length additional data</t></list></t>
        <t> The ChaCha20 and Poly1305 primitives are combined into an AEAD that takes a 256-bit key and 64-bit IV as follows:<list style="symbols">
          <t> First the ChaCha20 block function with the key, sender ID, nonce, and zero for the block counter. From the 64-byte output, the first 32 bytes are kept as the Poly1305 256-bit key.</t>
          <t> The ChaCha20 encryption function is called to encrypt the plaintext, using the same key, sender ID, nonce and plaintext, and with the initial counter set to 1.</t>
          <t> The Poly1305 function is called with the Poly1305 key calculated above, and a message constructed as a concatenation of the following:<list>
            <t> The additional data</t>
            <t> The length of the additional data in octets (as a 32-bit network order integer)</t>
            <t> The ciphertext</t>
            <t> The length of the ciphertext in octets (as a 32-bit network order integer)</t></list></t> </list></t>
        <t> Decryption is pretty much the same, but will be expanded later on.</t>    
        <t>The output from the AEAD is twofold:<list style="symbols">
          <t> A ciphertext of the same length as the plaintext.</t>
          <t> A 128-bit tag (the result of the Poly1305 function.</t></list></t>  
      </section>
    </section>
    <section anchor="foripsec" title="Algorithms for ESP & AH">
      <t> This document defines three algorithms for use with the Encapsulated Security Protocol (ESP - <xref target="RFC4303"/>) and Authentication Header (AH - <xref target="RFC4302"/>):<list style="symbols">
        <t> ChaCha20 for use as an encryption algorithms for ESP.</t>
        <t> Poly1305-MAC for use as a message authentication algorithm for ESP and AH.</t>
        <t> ChaCha20-Poly1305-ESP as an AEAD algorithm for ESP.</t></list></t>
      <section anchor="esp_chacha" title="ENCR_ChaCha20 for ESP">
        <t> For ChaCha20 used in ESP, the following parameters are used:<list style="symbols">
          <t> The IV is 64-bit. Since this is used as the nonce for the ChaCha20 function, this IV MUST NOT repeat. The most natural way to implement this is with a counter, but anything that guarantees uniqueness can be used, such as a linear feedback shift register (LFSR).  Note that the encrypter can use any IV generation method that meets the uniqueness requirement, without coordinating with the decrypter.</t>
          <t> The Internet Key Exchange protocol (IKE - <xref target="RFC5996"/>) generates a bitstring called KEYMAT that is generated from a PRF. That KEYMAT is divided into keys for encryption, message authentication and whatever else is needed. For the ChaCha20 algorithm, 256 bits are used for the key.</t>
          <t> The ChaCha20 encryption algorithm is called with the 256-bit key, one (1) for the initial counter, the packet IV as a nonce, and the plaintext. For regular IPsec, the Sender ID is set to zero. For multi-sender SAs, such as described in <xref target="RFC6054"/>, there sender ID can be set to a different value for each sender. The 64-bit IV is treated as a 64-bit network order integer, and passed to the ChaCha20 function as a number. The reason that one (1) is used for the initial counter rather than zero is that one is used for encryption in the AEAD algorithm (because zero is reserved for generating the one-time Poly1305 key), so one was used here for consistency.</t>
          <t> As ChaCha20 is not a block cipher, no padding should be necessary. However, in keeping with the specification in RFC 4303, the ESP does have padding, so as to align the buffer to an integral multiple of 4 octets.</t></list></t>
        <t> The encryption algorithm transform ID for negotiating this algorithm in IKE is TBA by IANA.</t> 
      </section>  
      <section anchor="esp_ah_poly1305" title="AUTH_Poly1305 for ESP and AH">
        <t> You cannot use a part of the keying material directly, because Poly1305 requires a different key for each authenticated message. So the Poly1305 key for each message is generated as follows:<list style="symbols">
          <t> The key for AUTH_Poly1305 is 256-bits.</t>
          <t> To determine the per-packet Poly1305 key, the ChaCha20 block function is called with the following parameters:<list style="symbols">
            <t> The AUTH_Poly1305 256-bit key is used as the key.</t>
            <t> The sender ID is set to zero.</t>
            <t> The packet replay counter is used as the nonce. If Extended Sequence Numbers (ESN) are employed, then the full 64-bit is used for the nonce.</t>
            <t> Zero is used for the block counter.</t></list></t>
          <t> The 512-bit result is then truncated. The top 256 bits are used as the one-time key for Poly1305 to calculate the message authentication code (MAC).</t>
          <t> The 128-bit output serves as the MAC for the packet. All 16 bytes are included in the packet.</t></list></t>
        <t> The integrity algorithm transform ID for negotiating this algorithm in IKE is TBA by IANA.</t> 
      </section>  
      <section anchor="esp_chacha_poly" title="ESP_ChaCha20-Poly1305 for ESP">
        <t> ESP_ChaCha20-Poly1305 is a combined mode algorithm, or AEAD. The construction follows the AEAD construction in <xref target="aead"/>:<list style="symbols"> 
          <t> As in <xref target="esp_chacha"/>, the IV is 64-bit, and is used as a nonce. </t>
          <t> Also as in <xref target="esp_chacha"/>, the encryption key is 256-bit.</t>
          <t> As in <xref target="esp_ah_poly1305"/>, the nonce, along with a block counter of zero is passed to the ChaCha20 block function, and the top part of the result used as the Poly1305 key.</t>
          <t> The ChaCha20 encryption function is then called with the nonce, the key, and an initial counter of zero.</t>
          <t> Finally, the Poly1305 function is run on the data to be authenticated, which is, as specified in <xref target="aead"/> a concatenation of the following:<list style="symbols">
            <t> The Authenticated Additional Data (AAD) - see <xref target="aad"/>.</t>
            <t> The AAD length in bytes as a 32-bit network order quantity.</t>
            <t> The ciphertext</t>
            <t> The length of the ciphertext as a 32-bit network order quantity.</t></list></t>
          <t> The 128-bit output of Poly1305 is used as the tag. All 16 bytes are included in the packet.</t></list></t>
        <t> The encryption algorithm transform ID for negotiating this algorithm in IKE is TBA by IANA.</t> 
        <section anchor="aad" title="AAD Construction">
          <t> The construction of the Additional Authenticated Data (AAD) is similar to the one in <xref target="RFC4106"/>. For security associations (SAs) with 32-bit sequence numbers the AAD is 8 bytes: 4-byte SPI followed by 4-byte sequence number ordered exactly as it is in the packet. For SAs with ESN the AAD is 12 bytes: 4-byte SPI followed by an 8-byte sequence number as a 64-bit network order integer.</t>
        </section>
      </section>  
    </section>
      <section anchor="security" title="Security Considerations">
        <t> The ChaCha20 cipher is designed to provide 256-bit security.</t>
        <t> The Poly1305 authenticator is designed to ensure that forged messages are rejected with a probability of 1-(n/(2^102)) for a 16n-byte message, even after sending 2^64 legitimate messages, so it is SUF-CMA in the terminology of <xref target="AE"/>.</t>
        <t> The most important security consideration in implementing this draft is the uniqueness of the nonce used in ChaCha20. This is trivial in AUTH_Poly1305, because the packet sequence number is used as a nonce, but is required for ChaCha20 as well. As said in <xref target="esp_chacha"/>, the nonce should be selected uniquely for a particular key. counters and LFSRs are both acceptable ways of generating unique nonces, as is encrypting a counter using a 64-bit cipher such as DES. Note that it is not acceptable to use a truncation of a counter encrypted with a 128-bit or 256-bit cipher, because such a truncation may repeat after a short time.</t>
        <t> The same kind of collision risk as in the above paragraph also exists in the selection of the Poly1305 key in both the AEAD construction (<xref target="aead"/>) and in the AUTH_Poly1305 algorithm (<xref target="esp_ah_poly1305"/>). ChaCha20 is used to generate a 512-bit value, which is unique to each packet, but this uniqueness is not guaranteed for its 256-bit truncation, which serves as the actual key for Poly1305. While uniqueness is not guaranteed, it is still very likely. Birthday paradox calculations show that generating Poly1305 keys needs to happen over 2^101 times (way more than the 2^64 allowed by IPsec) to drive the chances of collision up to 0.00000000000001%. This is why we may safely ignore the possibility of collision. </t>
      </section>
      <section anchor="iana" title="IANA Considerations">
        <t> IANA is requested to assign two values from the IKEv2 "Transform Type 1 - Encryption Algorithm Transform IDs" registry, as follows:<list style="symbols">
          <t>ENCR_ChaCha20</t>
          <t>ESP_ChaCha20-Poly1305</t></list></t>
        <t> IANA is also requested to assign one value from the IKEv2 "Transform Type 3 - Integrity Algorithm Transform IDs" registry with name "AUTH_Poly1305".</t>
      </section>  
      <section anchor="ack" title="Acknowledgements">
        <t> Much of the text in this document was inspired by Adam Langley's draft for TLS, and by "inspired" I mean "shamelessly copied". The author would also like to thank Adam, Watson Ladd and Dave McGrew for allaying his fears about writing a document describing crypto.</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>
            <address>
              <postal>
                <street>1350 Mass. Ave.</street>
                <street>Cambridge</street>
                <street>MA 02138</street>
              </postal>
              <phone>- +1 617 495 3864</phone>
              <email>sob@harvard.edu</email>
            </address>
          </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' octets='4723' target='ftp://ftp.isi.edu/in-notes/rfc2119.txt' />
        <format type='HTML' octets='16553' target='http://tools.ietf.org/html/rfc2119' />
      </reference>
      <reference anchor="RFC5996">
        <front>
          <title>Internet Key Exchange Protocol Version 2 (IKEv2)</title>
          <author initials="C." surname="Kaufman" fullname="C. Kaufman">
            <organization/>
          </author>
          <author initials="P." surname="Hoffman" fullname="P. Hoffman">
            <organization/>
          </author>
          <author initials="Y." surname="Nir" fullname="Y. Nir">
            <organization/>
          </author>
          <author initials="P." surname="Eronen" fullname="P. Eronen">
            <organization/>
          </author>
          <date year="2010" month="September"/>
        </front>
        <seriesInfo name="RFC" value="5996"/>
        <format type="TXT" octets="346466" target="http://www.rfc-editor.org/rfc/rfc5996.txt"/>
      </reference>
      <reference anchor='chacha'>
        <front>
          <title>ChaCha, a variant of Salsa20</title>
          <author initials='D.J' surname='Bernstein' fullname='Daniel J Bernstein'>
            <organization>The University of Illinois at Chicago</organization>
          </author>  
          <date month='Jan' day='28' year='2008' />
        </front>
        <format type='PDF' target='http://cr.yp.to/chacha/chacha-20080128.pdf' />
      </reference>  
      <reference anchor='poly1305'>
        <front>
          <title>The Poly1305-AES message-authentication code</title>
          <author initials='D.J' surname='Bernstein' fullname='Daniel J Bernstein'>
            <organization>The University of Illinois at Chicago</organization>
          </author>  
          <date month='Mar' day='29' year='2005' />
        </front>
        <format type='PDF' target='http://cr.yp.to/mac/poly1305-20050329.pdf' />
      </reference> 
      <reference anchor="RFC4302">
        <front>
          <title>IP Authentication Header</title>
          <author initials="S." surname="Kent" fullname="S. Kent">
            <organization/>
          </author>
          <date year="2005" month="December"/>
        </front>
        <seriesInfo name="RFC" value="4302"/>
        <format type="TXT" octets="82328" target="http://www.rfc-editor.org/rfc/rfc4302.txt"/>
      </reference>
      <reference anchor="RFC4303">
        <front>
          <title>IP Encapsulating Security Payload (ESP)</title>
          <author initials="S." surname="Kent" fullname="S. Kent">
            <organization/>
          </author>
          <date year="2005" month="December"/>
        </front>
        <seriesInfo name="RFC" value="4303"/>
        <format type="TXT" octets="114315" target="http://www.rfc-editor.org/rfc/rfc4303.txt"/>
      </reference> 
      <reference anchor="RFC6054">
        <front>
          <title>Using Counter Modes with Encapsulating Security Payload (ESP) and Authentication Header (AH) to Protect Group Traffic</title>
          <author initials="D." surname="McGrew" fullname="D. McGrew">
            <organization/>
          </author>
          <author initials="B." surname="Weis" fullname="B. Weis">
            <organization/>
          </author>
          <date year="2010" month="November"/>
        </front>
        <seriesInfo name="RFC" value="6054"/>
        <format type="TXT" octets="22672" target="http://www.rfc-editor.org/rfc/rfc6054.txt"/>
      </reference>
    </references>
    <references title="Informative References"> 
      <reference anchor="FIPS-197" target="http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf">
        <front>
          <title>Advanced Encryption Standard (AES)</title>
          <author>
            <organization>National Institute of Standards and Technology</organization>
          </author>
          <date month="November" year="2001" />
        </front>
        <seriesInfo name="FIPS" value="PUB 197" />
      </reference>
      <reference anchor="FIPS-46" target="http://www.itl.nist.gov/fipspubs/fip46-2.htm">
        <front>
          <title>Data Encryption Standard</title>
          <author>
            <organization>National Institute of Standards and Technology</organization>
          </author>
          <date month="December" year="1993" />
        </front>
        <seriesInfo name="FIPS" value="PUB 46-2" />
      </reference>
      <reference anchor="standby-cipher">
        <front>
          <title>Selection of Future Cryptographic Standards</title>
          <author initials='D' surname='McGrew' fullname='David McGrew'>
            <organization>Cisco Systems, Inc.</organization>
          </author>  
          <author initials='A' surname='Grieco' fullname='Anthony Grieco'>
            <organization>Cisco Systems, Inc.</organization>
          </author>  
          <author initials='Y' surname='Sheffer' fullname='Yaron Sheffer'>
            <organization>Porticor</organization>
          </author>  
        </front>
        <seriesInfo name="Internet-Draft" value="draft-mcgrew-standby-cipher" />
        <format type="HTML" target="http://tools.ietf.org/html/draft-mcgrew-standby-cipher-00" />
      </reference>
      <reference anchor="agl-draft">
        <front>
          <title>ChaCha20 and Poly1305 based Cipher Suites for TLS</title>
          <author initials="A" surname="Langley" fullname="Adam Langley">
            <organization/>
          </author>
          <author initials="W" surname="Chang" fullname="Wan-Teh Chang">
            <organization/>
          </author>
          <date month="November" day="22" year="2013"/>
        </front>
        <seriesInfo name="Internet-Draft" value="draft-agl-tls-chacha20poly1305-04"/>
        <format type="HTML" target="http://tools.ietf.org/id/draft-agl-tls-chacha20poly1305-04.html"/>
      </reference>
      <reference anchor="RFC4106">
        <front>
          <title>The Use of Galois/Counter Mode (GCM) in IPsec Encapsulating Security Payload (ESP)</title>
          <author initials="J." surname="Viega" fullname="J. Viega">
            <organization/>
          </author>
          <author initials="D." surname="McGrew" fullname="D. McGrew">
            <organization/>
          </author>
          <date year="2005" month="June"/>
        </front>
        <seriesInfo name="RFC" value="4106"/>
        <format type="TXT" octets="23399" target="http://www.rfc-editor.org/rfc/rfc4106.txt"/>
      </reference>
      <reference anchor="AE" target="http://cseweb.ucsd.edu/~mihir/papers/oem.html">
        <front>
          <title>Authenticated Encryption: Relations among notions and analysis of the generic composition paradigm</title>
          <author initials="M." surname="Bellare"/>
          <author initials="C." surname="Namprempre"/>
        </front>
      </reference>
    </references>
        <section anchor="examples" title="Examples of the algorithms">
      <section anchor="example_block" title="Example of the block function">
        <t> In this example, we show a single run of the ChaCha20 block. For our example, we choose<list style="symbols">
          <t> Key: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f</t>
          <t> Sender ID: 0</t>
          <t> Nonce: fedcba9876543210</t>
          <t> Block Count: 5</t></list></t>
        <t> The block is set up as follows: <figure>
            <artwork><![CDATA[
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000005 00000000 76543210 fedcba98
]]></artwork>
          </figure></t>
        <t> After running the block function, the result is as follows: <figure>
            <artwork><![CDATA[
   a435cd80 a16b9b89 9112c0a4 487b4230
   0d98f8cf b5b59396 4cce17a6 d17db31a
   6dd35e45 658dff64 52b615fb b3c17e96
   31d2725f ecd5246f 739bdab7 07cf7593
]]></artwork>
          </figure></t>
        <t> The resulting octet string looks like this:  <figure>
            <artwork><![CDATA[
a435cd80a16b9b899112c0a4487b42300d98f8cfb5b593964cce17a6d17db31a
6dd35e45658dff6452b615fbb3c17e9631d2725fecd5246f739bdab707cf7593
]]></artwork>
          </figure></t>
      </section>
      <section anchor="example_chacha" title="Example of ChaCha20 Encryption in IPsec">
        <t> In this example, we show a run of the ChaCha20 encryption function. For our example, we chose some arbitrary data. Also,<list style="symbols">
          <t> Key: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f</t>
          <t> Sender ID: 0</t>
          <t> IV: 0x000000000000004a (74)</t>
          <t> Message Length: 80 octets</t>
          <t> The message itself:<figure>
            <artwork><![CDATA[
35:6f:16:49:d0:2a:7f:19:30:f1:8f:af:e6:d1:69:d3:22:12:1f:76:78:be:6c
af:90:17:f3:fb:8e:12:d3:f1:f7:dc:7c:31:02:70:36:54:0c:5a:37:aa:31:fd
8a:e2:31:73:8a:ff:cc:77:8c:ba:c4:7b:6b:d1:17:6c:ce:cc:90:56:ae:65:b9
c6:af:40:e6:9d:f4:37:8f:4a:a9:f3
]]></artwork>
          </figure></t></list></t>
        <t> First, we need to apply padding. We will use an ESP-style padding, rounding up the length of the packet to 84 octets, with two padding bytes, a pad length, and a Next Header field set to 0x04:<figure>
            <artwork><![CDATA[
35:6f:16:49:d0:2a:7f:19:30:f1:8f:af:e6:d1:69:d3:22:12:1f:76:78:be:6c
af:90:17:f3:fb:8e:12:d3:f1:f7:dc:7c:31:02:70:36:54:0c:5a:37:aa:31:fd
8a:e2:31:73:8a:ff:cc:77:8c:ba:c4:7b:6b:d1:17:6c:ce:cc:90:56:ae:65:b9
c6:af:40:e6:9d:f4:37:8f:4a:a9:f3:01:02:02:04
]]></artwork>
          </figure></t>
        <t> For 84 octets, we need two blocks. We call the block function twice, once with the block counter set to 1, and then again with the block counter set to two. The results are as follows:<figure>
            <artwork><![CDATA[
Block 1 before rounds:
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000001 00000000 0000004a 00000000

Block 1 after rounds:
   9944f9d8 0f69b9cc 9002b600 559b9586
   04579a7b a9a146c0 a601d9dd 8a141b06
   d08a69bb e737527d 0ee7970d ff8512d8
   d959240f 0f09eb18 eb0f2a3a ee0fbcf2
      
Block 2 before rounds:
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000002 00000000 0000004a 00000000

Block 2 after rounds:
   213bebc0 720ce566 26195c71 84e2ee1d
   511f64c2 b8ed11da ede4e571 d9b57c34
   0aa05765 dd073b1c 22d7dc84 86c91bc0
   a9b89717 dbf8c56f 5822cafd deec62ff
   
84 bytes of key stream:
99:44:f9:d8:0f:69:b9:cc:90:02:b6:00:55:9b:95:86:04:57:9a:7b:a9:a1:46
c0:a6:01:d9:dd:8a:14:1b:06:d0:8a:69:bb:e7:37:52:7d:0e:e7:97:0d:ff:85
12:d8:d9:59:24:0f:0f:09:eb:18:eb:0f:2a:3a:ee:0f:bc:f2:21:3b:eb:c0:72
0c:e5:66:26:19:5c:71:84:e2:ee:1d:51:1f:64:c2

XOR'd with the plaintext:
ac:2b:ef:91:df:43:c6:d5:a0:f3:39:af:b3:4a:fc:55:26:45:85:0d:d1:1f:2a
6f:36:16:2a:26:04:06:c8:f7:27:56:15:8a:e5:47:64:29:02:bd:a0:a7:ce:78
98:3a:e8:2a:ae:f0:c3:7e:67:a2:2f:74:41:eb:f9:63:72:3e:b1:6d:45:a5:cb
ca:4a:26:c0:84:a8:46:0b:a8:47:ee:50:1d:66:c6
]]></artwork>
          </figure></t>
      </section>
      <section anchor="example_poly" title="Example of the AUTH_Poly1305">
        <t> In this example, we show a run of the AUTH_Poly1305 message authentication function. For our example, we choose some arbitrary data - the same data as in the ENCH_ChaCha20 example above. Also,<list style="symbols">
          <t> Key: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f</t>
          <t> Replay Counter: 0x0000004a (74)</t>
          <t> Message Length: 80 octets</t>
          <t> The message itself:<figure>
            <artwork><![CDATA[
35:6f:16:49:d0:2a:7f:19:30:f1:8f:af:e6:d1:69:d3:22:12:1f:76:78:be:6c
af:90:17:f3:fb:8e:12:d3:f1:f7:dc:7c:31:02:70:36:54:0c:5a:37:aa:31:fd
8a:e2:31:73:8a:ff:cc:77:8c:ba:c4:7b:6b:d1:17:6c:ce:cc:90:56:ae:65:b9
c6:af:40:e6:9d:f4:37:8f:4a:a9:f3
]]></artwork>
          </figure></t></list></t>
        <t> First, we run the ChaCha20 algorithm to generate the one-time Poly1305 key:<figure>
            <artwork><![CDATA[
Key block before the rounds:
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000000 00000000 0000004a 00000000

Key block after rounds:
   64345099 66639817 2113da66 371489fa
   091f5ff3 c681552b b0f0a4b5 d0d7e6dd
   5401d1e1 b6ff30e5 25a5a57c b1af2f06
   8a064ae1 39b28c25 9698451b c688b187

256-bit one-time Poly1305 key:
64345099666398172113da66371489fa091f5ff3c681552bb0f0a4b5d0d7e6dd

Tag: 43dc3ef779160bb54943a2279570d679
]]></artwork>
          </figure></t>
      </section>
      <section anchor="example_aead" title="Example of ESP_ChaCha20-Poly1305 in IPsec">
        <t> In this example, we encrypt the same data as in <xref target="example_chacha"/>, and also authenticate it. For the purposes of this example, we will assume that ESN is not used.<list style="symbols">
          <t> Key: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f</t>
          <t> Sender ID: 0</t>
          <t> SPI: 0x7890abcd</t>
          <t> IV: 0x000000000000004a (74)</t>
          <t> Message Length: 80 octets</t>
          <t> The message itself:<figure>
            <artwork><![CDATA[
35:6f:16:49:d0:2a:7f:19:30:f1:8f:af:e6:d1:69:d3:22:12:1f:76:78:be:6c
af:90:17:f3:fb:8e:12:d3:f1:f7:dc:7c:31:02:70:36:54:0c:5a:37:aa:31:fd
8a:e2:31:73:8a:ff:cc:77:8c:ba:c4:7b:6b:d1:17:6c:ce:cc:90:56:ae:65:b9
c6:af:40:e6:9d:f4:37:8f:4a:a9:f3
]]></artwork>
          </figure></t></list></t>
        <t> First, we repeat the calculations in <xref target="example_poly"/> to get the one-time Poly1305 key:<figure>
            <artwork><![CDATA[
Key block before the rounds:
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000000 00000000 0000004a 00000000

Key block after rounds:
   64345099 66639817 2113da66 371489fa
   091f5ff3 c681552b b0f0a4b5 d0d7e6dd
   5401d1e1 b6ff30e5 25a5a57c b1af2f06
   8a064ae1 39b28c25 9698451b c688b187

256-bit one-time Poly1305 key:
64345099666398172113da66371489fa091f5ff3c681552bb0f0a4b5d0d7e6dd
]]></artwork>
          </figure></t>
        <t> Next, we need to apply padding. We will use an ESP-style padding, rounding up the length of the packet to 84 octets, with two padding bytes, a pad length, and a Next Header field set to 0x04:<figure>
            <artwork><![CDATA[
35:6f:16:49:d0:2a:7f:19:30:f1:8f:af:e6:d1:69:d3:22:12:1f:76:78:be:6c
af:90:17:f3:fb:8e:12:d3:f1:f7:dc:7c:31:02:70:36:54:0c:5a:37:aa:31:fd
8a:e2:31:73:8a:ff:cc:77:8c:ba:c4:7b:6b:d1:17:6c:ce:cc:90:56:ae:65:b9
c6:af:40:e6:9d:f4:37:8f:4a:a9:f3:01:02:02:04
]]></artwork>
          </figure></t>
        <t> Step 3, we run the ChaCha20 block twice to create sufficient keystream to XOR with all the (padded) plaintext.<figure>
            <artwork><![CDATA[
Block 1 before rounds:
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000001 00000000 0000004a 00000000

Block 1 after rounds:
   9944f9d8 0f69b9cc 9002b600 559b9586
   04579a7b a9a146c0 a601d9dd 8a141b06
   d08a69bb e737527d 0ee7970d ff8512d8
   d959240f 0f09eb18 eb0f2a3a ee0fbcf2
   
Block 2 before rounds:
   61707865 3320646e 79622d32 6b206574
   03020100 07060504 0b0a0908 0f0e0d0c
   13121110 17161514 1b1a1918 1f1e1d1c
   00000002 00000000 0000004a 00000000

Block 2 after rounds:
   213bebc0 720ce566 26195c71 84e2ee1d
   511f64c2 b8ed11da ede4e571 d9b57c34
   0aa05765 dd073b1c 22d7dc84 86c91bc0
   a9b89717 dbf8c56f 5822cafd deec62ff
   
84 bytes of key stream:
99:44:f9:d8:0f:69:b9:cc:90:02:b6:00:55:9b:95:86:04:57:9a:7b:a9:a1:46
c0:a6:01:d9:dd:8a:14:1b:06:d0:8a:69:bb:e7:37:52:7d:0e:e7:97:0d:ff:85
12:d8:d9:59:24:0f:0f:09:eb:18:eb:0f:2a:3a:ee:0f:bc:f2:21:3b:eb:c0:72
0c:e5:66:26:19:5c:71:84:e2:ee:1d:51:1f:64:c2

XOR'd with the plaintext:
ac:2b:ef:91:df:43:c6:d5:a0:f3:39:af:b3:4a:fc:55:26:45:85:0d:d1:1f:2a
6f:36:16:2a:26:04:06:c8:f7:27:56:15:8a:e5:47:64:29:02:bd:a0:a7:ce:78
98:3a:e8:2a:ae:f0:c3:7e:67:a2:2f:74:41:eb:f9:63:72:3e:b1:6d:45:a5:cb
ca:4a:26:c0:84:a8:46:0b:a8:47:ee:50:1d:66:c6
]]></artwork>
          </figure></t>
        <t> next, we construct the AEAD. As described in <xref target="aad"/>, the AAD is a concatenation of the 4-byte SPI, followed by a 4-byte sequence number. In this case: 78:90:ab:cd:00:00:00:4a. The Poly1305 function is run over a concatenation of the AAD, the length of the AAD as a 32-bit integer (8), The ciphertext, and the length of the ciphertext (84):<figure>
            <artwork><![CDATA[
Buffer to be authenticated:
78:90:ab:cd:00:00:00:4a:00:00:00:08:ac:2b:ef:91:df:43:c6:d5:a0:f3:39
af:b3:4a:fc:55:26:45:85:0d:d1:1f:2a:6f:36:16:2a:26:04:06:c8:f7:27:56
15:8a:e5:47:64:29:02:bd:a0:a7:ce:78:98:3a:e8:2a:ae:f0:c3:7e:67:a2:2f
74:41:eb:f9:63:72:3e:b1:6d:45:a5:cb:ca:4a:26:c0:84:a8:46:0b:a8:47:ee
50:1d:66:c6:00:00:00:54

Tag: 7af839e8f4961cd99f0600dc7b8ad8d6
]]></artwork>
          </figure></t>
      </section>
    </section>

    <!-- ====================================================================== -->
  </back>
</rfc>
