Debian Codegraph @

rfc7539.html

raw
1 <pre>Internet Research Task Force (IRTF) Y. Nir
2 Request for Comments: 7539 Check Point
3 Category: Informational A. Langley
4 ISSN: 2070-1721 Google, Inc.
5 May 2015
6
7
8 <span class="h1">ChaCha20 and Poly1305 for IETF Protocols</span>
9
10 Abstract
11
12 This document defines the ChaCha20 stream cipher as well as the use
13 of the Poly1305 authenticator, both as stand-alone algorithms and as
14 a "combined mode", or Authenticated Encryption with Associated Data
15 (AEAD) algorithm.
16
17 This document does not introduce any new crypto, but is meant to
18 serve as a stable reference and an implementation guide. It is a
19 product of the Crypto Forum Research Group (CFRG).
20
21 Status of This Memo
22
23 This document is not an Internet Standards Track specification; it is
24 published for informational purposes.
25
26 This document is a product of the Internet Research Task Force
27 (IRTF). The IRTF publishes the results of Internet-related research
28 and development activities. These results might not be suitable for
29 deployment. This RFC represents the consensus of the Crypto Forum
30 Research Group of the Internet Research Task Force (IRTF). Documents
31 approved for publication by the IRSG are not a candidate for any
32 level of Internet Standard; see <a href="./rfc5741#section-2">Section&nbsp;2 of RFC 5741</a>.
33
34 Information about the current status of this document, any errata,
35 and how to provide feedback on it may be obtained at
36 <a href="http://www.rfc-editor.org/info/rfc7539">http://www.rfc-editor.org/info/rfc7539</a>.
37
38 Copyright Notice
39
40 Copyright (c) 2015 IETF Trust and the persons identified as the
41 document authors. All rights reserved.
42
43 This document is subject to <a href="https://www.rfc-editor.org/bcp/bcp78">BCP 78</a> and the IETF Trust's Legal
44 Provisions Relating to IETF Documents
45 (<a href="http://trustee.ietf.org/license-info">http://trustee.ietf.org/license-info</a>) in effect on the date of
46 publication of this document. Please review these documents
47 carefully, as they describe your rights and restrictions with respect
48 to this document.
49
50
51
52 <span class="grey">Nir &amp; Langley Informational [Page 1]</span></pre>
53 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-2" ></span>
54 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
55
56
57 Table of Contents
58
59 <a href="#section-1">1</a>. Introduction ....................................................<a href="#page-3">3</a>
60 <a href="#section-1.1">1.1</a>. Conventions Used in This Document ..........................<a href="#page-4">4</a>
61 <a href="#section-2">2</a>. The Algorithms ..................................................<a href="#page-4">4</a>
62 <a href="#section-2.1">2.1</a>. The ChaCha Quarter Round ...................................<a href="#page-4">4</a>
63 <a href="#section-2.1.1">2.1.1</a>. Test Vector for the ChaCha Quarter Round ............<a href="#page-5">5</a>
64 <a href="#section-2.2">2.2</a>. A Quarter Round on the ChaCha State ........................<a href="#page-5">5</a>
65 2.2.1. Test Vector for the Quarter Round on the
66 ChaCha State ........................................<a href="#page-6">6</a>
67 <a href="#section-2.3">2.3</a>. The ChaCha20 Block Function ................................<a href="#page-6">6</a>
68 <a href="#section-2.3.1">2.3.1</a>. The ChaCha20 Block Function in Pseudocode ...........<a href="#page-8">8</a>
69 <a href="#section-2.3.2">2.3.2</a>. Test Vector for the ChaCha20 Block Function .........<a href="#page-9">9</a>
70 <a href="#section-2.4">2.4</a>. The ChaCha20 Encryption Algorithm .........................<a href="#page-10">10</a>
71 <a href="#section-2.4.1">2.4.1</a>. The ChaCha20 Encryption Algorithm in Pseudocode ....<a href="#page-11">11</a>
72 <a href="#section-2.4.2">2.4.2</a>. Example and Test Vector for the ChaCha20 Cipher ....<a href="#page-11">11</a>
73 <a href="#section-2.5">2.5</a>. The Poly1305 Algorithm ....................................<a href="#page-13">13</a>
74 <a href="#section-2.5.1">2.5.1</a>. The Poly1305 Algorithms in Pseudocode ..............<a href="#page-15">15</a>
75 <a href="#section-2.5.2">2.5.2</a>. Poly1305 Example and Test Vector ...................<a href="#page-15">15</a>
76 <a href="#section-2.6">2.6</a>. Generating the Poly1305 Key Using ChaCha20 ................<a href="#page-17">17</a>
77 <a href="#section-2.6.1">2.6.1</a>. Poly1305 Key Generation in Pseudocode ..............<a href="#page-18">18</a>
78 <a href="#section-2.6.2">2.6.2</a>. Poly1305 Key Generation Test Vector ................<a href="#page-18">18</a>
79 2.7. A Pseudorandom Function for Crypto Suites based on
80 ChaCha/Poly1305 ...........................................<a href="#page-18">18</a>
81 <a href="#section-2.8">2.8</a>. AEAD Construction .........................................<a href="#page-19">19</a>
82 <a href="#section-2.8.1">2.8.1</a>. Pseudocode for the AEAD Construction ...............<a href="#page-21">21</a>
83 2.8.2. Example and Test Vector for
84 AEAD_CHACHA20_POLY1305 .............................<a href="#page-22">22</a>
85 <a href="#section-3">3</a>. Implementation Advice ..........................................<a href="#page-24">24</a>
86 <a href="#section-4">4</a>. Security Considerations ........................................<a href="#page-24">24</a>
87 <a href="#section-5">5</a>. IANA Considerations ............................................<a href="#page-26">26</a>
88 <a href="#section-6">6</a>. References .....................................................<a href="#page-26">26</a>
89 <a href="#section-6.1">6.1</a>. Normative References ......................................<a href="#page-26">26</a>
90 <a href="#section-6.2">6.2</a>. Informative References ....................................<a href="#page-26">26</a>
91 <a href="#appendix-A">Appendix A</a>. Additional Test Vectors ...............................<a href="#page-29">29</a>
92 <a href="#appendix-A.1">A.1</a>. The ChaCha20 Block Functions ...............................<a href="#page-29">29</a>
93 <a href="#appendix-A.2">A.2</a>. ChaCha20 Encryption ........................................<a href="#page-32">32</a>
94 <a href="#appendix-A.3">A.3</a>. Poly1305 Message Authentication Code .......................<a href="#page-34">34</a>
95 <a href="#appendix-A.4">A.4</a>. Poly1305 Key Generation Using ChaCha20 .....................<a href="#page-40">40</a>
96 <a href="#appendix-A.5">A.5</a>. ChaCha20-Poly1305 AEAD Decryption ..........................<a href="#page-41">41</a>
97 <a href="#appendix-B">Appendix B</a>. Performance Measurements of ChaCha20 ..................<a href="#page-44">44</a>
98 Acknowledgements ..................................................<a href="#page-45">45</a>
99 Authors' Addresses ................................................<a href="#page-45">45</a>
100
101
102
103
104
105
106
107
108 <span class="grey">Nir &amp; Langley Informational [Page 2]</span></pre>
109 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-3" ></span>
110 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
111
112
113 <span class="h2"><a class="selflink" id="section-1" href="#section-1">1</a>. Introduction</span>
114
115 The Advanced Encryption Standard (AES -- [<a href="#ref-FIPS-197" title="&quot;Advanced Encryption Standard (AES)&quot;">FIPS-197</a>]) has become the
116 gold standard in encryption. Its efficient design, widespread
117 implementation, and hardware support allow for high performance in
118 many areas. On most modern platforms, AES is anywhere from four to
119 ten times as fast as the previous most-used cipher, Triple Data
120 Encryption Standard (3DES -- [<a href="#ref-SP800-67" title="&quot;Recommendation for the Triple Data Encryption Algorithm (TDEA) Block Cipher&quot;">SP800-67</a>]), which makes it not only the
121 best choice, but the only practical choice.
122
123 There are several problems with this. If future advances in
124 cryptanalysis reveal a weakness in AES, users will be in an
125 unenviable position. With the only other widely supported cipher
126 being the much slower 3DES, it is not feasible to reconfigure
127 deployments to use 3DES. [<a href="#ref-Standby-Cipher">Standby-Cipher</a>] describes this issue and
128 the need for a standby cipher in greater detail. Another problem is
129 that while AES is very fast on dedicated hardware, its performance on
130 platforms that lack such hardware is considerably lower. Yet another
131 problem is that many AES implementations are vulnerable to cache-
132 collision timing attacks ([<a href="#ref-Cache-Collisions">Cache-Collisions</a>]).
133
134 This document provides a definition and implementation guide for
135 three algorithms:
136
137 1. The ChaCha20 cipher. This is a high-speed cipher first described
138 in [<a href="#ref-ChaCha" title="&quot;ChaCha, a variant of Salsa20&quot;">ChaCha</a>]. It is considerably faster than AES in software-only
139 implementations, making it around three times as fast on
140 platforms that lack specialized AES hardware. See <a href="#appendix-B">Appendix B</a> for
141 some hard numbers. ChaCha20 is also not sensitive to timing
142 attacks (see the security considerations in <a href="#section-4">Section 4</a>). This
143 algorithm is described in <a href="#section-2.4">Section 2.4</a>
144
145 2. The Poly1305 authenticator. This is a high-speed message
146 authentication code. Implementation is also straightforward and
147 easy to get right. The algorithm is described in <a href="#section-2.5">Section 2.5</a>.
148
149 3. The CHACHA20-POLY1305 Authenticated Encryption with Associated
150 Data (AEAD) construction, described in <a href="#section-2.8">Section 2.8</a>.
151
152 This document does not introduce these new algorithms for the first
153 time. They have been defined in scientific papers by
154 D. J. Bernstein, which are referenced by this document. The purpose
155 of this document is to serve as a stable reference for IETF documents
156 making use of these algorithms.
157
158 These algorithms have undergone rigorous analysis. Several papers
159 discuss the security of Salsa and ChaCha ([<a href="#ref-LatinDances">LatinDances</a>],
160 [<a href="#ref-LatinDances2">LatinDances2</a>], [<a href="#ref-Zhenqing2012">Zhenqing2012</a>]).
161
162
163
164 <span class="grey">Nir &amp; Langley Informational [Page 3]</span></pre>
165 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-4" ></span>
166 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
167
168
169 This document represents the consensus of the Crypto Forum Research
170 Group (CFRG).
171
172 <span class="h3"><a class="selflink" id="section-1.1" href="#section-1.1">1.1</a>. Conventions Used in This Document</span>
173
174 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
175 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
176 document are to be interpreted as described in [<a href="./rfc2119" title="&quot;Key words for use in RFCs to Indicate Requirement Levels&quot;">RFC2119</a>].
177
178 The description of the ChaCha algorithm will at various time refer to
179 the ChaCha state as a "vector" or as a "matrix". This follows the
180 use of these terms in Professor Bernstein's paper. The matrix
181 notation is more visually convenient and gives a better notion as to
182 why some rounds are called "column rounds" while others are called
183 "diagonal rounds". Here's a diagram of how the matrices relate to
184 vectors (using the C language convention of zero being the index
185 origin).
186
187 0 1 2 3
188 4 5 6 7
189 8 9 10 11
190 12 13 14 15
191
192 The elements in this vector or matrix are 32-bit unsigned integers.
193
194 The algorithm name is "ChaCha". "ChaCha20" is a specific instance
195 where 20 "rounds" (or 80 quarter rounds -- see <a href="#section-2.1">Section 2.1</a>) are used.
196 Other variations are defined, with 8 or 12 rounds, but in this
197 document we only describe the 20-round ChaCha, so the names "ChaCha"
198 and "ChaCha20" will be used interchangeably.
199
200 <span class="h2"><a class="selflink" id="section-2" href="#section-2">2</a>. The Algorithms</span>
201
202 The subsections below describe the algorithms used and the AEAD
203 construction.
204
205 <span class="h3"><a class="selflink" id="section-2.1" href="#section-2.1">2.1</a>. The ChaCha Quarter Round</span>
206
207 The basic operation of the ChaCha algorithm is the quarter round. It
208 operates on four 32-bit unsigned integers, denoted a, b, c, and d.
209 The operation is as follows (in C-like notation):
210
211 1. a += b; d ^= a; d &lt;&lt;&lt;= 16;
212 2. c += d; b ^= c; b &lt;&lt;&lt;= 12;
213 3. a += b; d ^= a; d &lt;&lt;&lt;= 8;
214 4. c += d; b ^= c; b &lt;&lt;&lt;= 7;
215
216
217
218
219
220 <span class="grey">Nir &amp; Langley Informational [Page 4]</span></pre>
221 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-5" ></span>
222 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
223
224
225 Where "+" denotes integer addition modulo 2^32, "^" denotes a bitwise
226 Exclusive OR (XOR), and "&lt;&lt;&lt; n" denotes an n-bit left rotation
227 (towards the high bits).
228
229 For example, let's see the add, XOR, and roll operations from the
230 fourth line with sample numbers:
231
232 o a = 0x11111111
233 o b = 0x01020304
234 o c = 0x77777777
235 o d = 0x01234567
236 o c = c + d = 0x77777777 + 0x01234567 = 0x789abcde
237 o b = b ^ c = 0x01020304 ^ 0x789abcde = 0x7998bfda
238 o b = b &lt;&lt;&lt; 7 = 0x7998bfda &lt;&lt;&lt; 7 = 0xcc5fed3c
239
240 <span class="h4"><a class="selflink" id="section-2.1.1" href="#section-2.1.1">2.1.1</a>. Test Vector for the ChaCha Quarter Round</span>
241
242 For a test vector, we will use the same numbers as in the example,
243 adding something random for c.
244
245 o a = 0x11111111
246 o b = 0x01020304
247 o c = 0x9b8d6f43
248 o d = 0x01234567
249
250 After running a Quarter Round on these four numbers, we get these:
251
252 o a = 0xea2a92f4
253 o b = 0xcb1cf8ce
254 o c = 0x4581472e
255 o d = 0x5881c4bb
256
257 <span class="h3"><a class="selflink" id="section-2.2" href="#section-2.2">2.2</a>. A Quarter Round on the ChaCha State</span>
258
259 The ChaCha state does not have four integer numbers: it has 16. So
260 the quarter-round operation works on only four of them -- hence the
261 name. Each quarter round operates on four predetermined numbers in
262 the ChaCha state. We will denote by QUARTERROUND(x,y,z,w) a quarter-
263 round operation on the numbers at indices x, y, z, and w of the
264 ChaCha state when viewed as a vector. For example, if we apply
265 QUARTERROUND(1,5,9,13) to a state, this means running the quarter-
266 round operation on the elements marked with an asterisk, while
267 leaving the others alone:
268
269 0 *a 2 3
270 4 *b 6 7
271 8 *c 10 11
272 12 *d 14 15
273
274
275
276 <span class="grey">Nir &amp; Langley Informational [Page 5]</span></pre>
277 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-6" ></span>
278 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
279
280
281 Note that this run of quarter round is part of what is called a
282 "column round".
283
284 <span class="h4"><a class="selflink" id="section-2.2.1" href="#section-2.2.1">2.2.1</a>. Test Vector for the Quarter Round on the ChaCha State</span>
285
286 For a test vector, we will use a ChaCha state that was generated
287 randomly:
288
289 Sample ChaCha State
290
291 879531e0 c5ecf37d 516461b1 c9a62f8a
292 44c20ef3 3390af7f d9fc690b 2a5f714c
293 53372767 b00a5631 974c541a 359e9963
294 5c971061 3d631689 2098d9d6 91dbd320
295
296 We will apply the QUARTERROUND(2,7,8,13) operation to this state.
297 For obvious reasons, this one is part of what is called a "diagonal
298 round":
299
300 After applying QUARTERROUND(2,7,8,13)
301
302 879531e0 c5ecf37d *bdb886dc c9a62f8a
303 44c20ef3 3390af7f d9fc690b *cfacafd2
304 *e46bea80 b00a5631 974c541a 359e9963
305 5c971061 *ccc07c79 2098d9d6 91dbd320
306
307 Note that only the numbers in positions 2, 7, 8, and 13 changed.
308
309 <span class="h3"><a class="selflink" id="section-2.3" href="#section-2.3">2.3</a>. The ChaCha20 Block Function</span>
310
311 The ChaCha block function transforms a ChaCha state by running
312 multiple quarter rounds.
313
314 The inputs to ChaCha20 are:
315
316 o A 256-bit key, treated as a concatenation of eight 32-bit little-
317 endian integers.
318
319 o A 96-bit nonce, treated as a concatenation of three 32-bit little-
320 endian integers.
321
322 o A 32-bit block count parameter, treated as a 32-bit little-endian
323 integer.
324
325 The output is 64 random-looking bytes.
326
327
328
329
330
331
332 <span class="grey">Nir &amp; Langley Informational [Page 6]</span></pre>
333 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-7" ></span>
334 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
335
336
337 The ChaCha algorithm described here uses a 256-bit key. The original
338 algorithm also specified 128-bit keys and 8- and 12-round variants,
339 but these are out of scope for this document. In this section, we
340 describe the ChaCha block function.
341
342 Note also that the original ChaCha had a 64-bit nonce and 64-bit
343 block count. We have modified this here to be more consistent with
344 recommendations in <a href="./rfc5116#section-3.2">Section&nbsp;3.2 of [RFC5116]</a>. This limits the use of
345 a single (key,nonce) combination to 2^32 blocks, or 256 GB, but that
346 is enough for most uses. In cases where a single key is used by
347 multiple senders, it is important to make sure that they don't use
348 the same nonces. This can be assured by partitioning the nonce space
349 so that the first 32 bits are unique per sender, while the other 64
350 bits come from a counter.
351
352 The ChaCha20 state is initialized as follows:
353
354 o The first four words (0-3) are constants: 0x61707865, 0x3320646e,
355 0x79622d32, 0x6b206574.
356
357 o The next eight words (4-11) are taken from the 256-bit key by
358 reading the bytes in little-endian order, in 4-byte chunks.
359
360 o Word 12 is a block counter. Since each block is 64-byte, a 32-bit
361 word is enough for 256 gigabytes of data.
362
363 o Words 13-15 are a nonce, which should not be repeated for the same
364 key. The 13th word is the first 32 bits of the input nonce taken
365 as a little-endian integer, while the 15th word is the last 32
366 bits.
367
368 cccccccc cccccccc cccccccc cccccccc
369 kkkkkkkk kkkkkkkk kkkkkkkk kkkkkkkk
370 kkkkkkkk kkkkkkkk kkkkkkkk kkkkkkkk
371 bbbbbbbb nnnnnnnn nnnnnnnn nnnnnnnn
372
373 c=constant k=key b=blockcount n=nonce
374
375 ChaCha20 runs 20 rounds, alternating between "column rounds" and
376 "diagonal rounds". Each round consists of four quarter-rounds, and
377 they are run as follows. Quarter rounds 1-4 are part of a "column"
378 round, while 5-8 are part of a "diagonal" round:
379
380 1. QUARTERROUND ( 0, 4, 8,12)
381 2. QUARTERROUND ( 1, 5, 9,13)
382 3. QUARTERROUND ( 2, 6,10,14)
383 4. QUARTERROUND ( 3, 7,11,15)
384 5. QUARTERROUND ( 0, 5,10,15)
385
386
387
388 <span class="grey">Nir &amp; Langley Informational [Page 7]</span></pre>
389 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-8" ></span>
390 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
391
392
393 6. QUARTERROUND ( 1, 6,11,12)
394 7. QUARTERROUND ( 2, 7, 8,13)
395 8. QUARTERROUND ( 3, 4, 9,14)
396
397 At the end of 20 rounds (or 10 iterations of the above list), we add
398 the original input words to the output words, and serialize the
399 result by sequencing the words one-by-one in little-endian order.
400
401 Note: "addition" in the above paragraph is done modulo 2^32. In some
402 machine languages, this is called carryless addition on a 32-bit
403 word.
404
405 <span class="h4"><a class="selflink" id="section-2.3.1" href="#section-2.3.1">2.3.1</a>. The ChaCha20 Block Function in Pseudocode</span>
406
407 Note: This section and a few others contain pseudocode for the
408 algorithm explained in a previous section. Every effort was made for
409 the pseudocode to accurately reflect the algorithm as described in
410 the preceding section. If a conflict is still present, the textual
411 explanation and the test vectors are normative.
412
413 inner_block (state):
414 Qround(state, 0, 4, 8,12)
415 Qround(state, 1, 5, 9,13)
416 Qround(state, 2, 6,10,14)
417 Qround(state, 3, 7,11,15)
418 Qround(state, 0, 5,10,15)
419 Qround(state, 1, 6,11,12)
420 Qround(state, 2, 7, 8,13)
421 Qround(state, 3, 4, 9,14)
422 end
423
424 chacha20_block(key, counter, nonce):
425 state = constants | key | counter | nonce
426 working_state = state
427 for i=1 upto 10
428 inner_block(working_state)
429 end
430 state += working_state
431 return serialize(state)
432 end
433
434
435
436
437
438
439
440
441
442
443
444 <span class="grey">Nir &amp; Langley Informational [Page 8]</span></pre>
445 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-9" ></span>
446 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
447
448
449 <span class="h4"><a class="selflink" id="section-2.3.2" href="#section-2.3.2">2.3.2</a>. Test Vector for the ChaCha20 Block Function</span>
450
451 For a test vector, we will use the following inputs to the ChaCha20
452 block function:
453
454 o Key = 00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f:10:11:12:13:
455 14:15:16:17:18:19:1a:1b:1c:1d:1e:1f. The key is a sequence of
456 octets with no particular structure before we copy it into the
457 ChaCha state.
458
459 o Nonce = (00:00:00:09:00:00:00:4a:00:00:00:00)
460
461 o Block Count = 1.
462
463 After setting up the ChaCha state, it looks like this:
464
465 ChaCha state with the key setup.
466
467 61707865 3320646e 79622d32 6b206574
468 03020100 07060504 0b0a0908 0f0e0d0c
469 13121110 17161514 1b1a1918 1f1e1d1c
470 00000001 09000000 4a000000 00000000
471
472 After running 20 rounds (10 column rounds interleaved with 10
473 "diagonal rounds"), the ChaCha state looks like this:
474
475 ChaCha state after 20 rounds
476
477 837778ab e238d763 a67ae21e 5950bb2f
478 c4f2d0c7 fc62bb2f 8fa018fc 3f5ec7b7
479 335271c2 f29489f3 eabda8fc 82e46ebd
480 d19c12b4 b04e16de 9e83d0cb 4e3c50a2
481
482 Finally, we add the original state to the result (simple vector or
483 matrix addition), giving this:
484
485 ChaCha state at the end of the ChaCha20 operation
486
487 e4e7f110 15593bd1 1fdd0f50 c47120a3
488 c7f4d1c7 0368c033 9aaa2204 4e6cd4c3
489 466482d2 09aa9f07 05d7c214 a2028bd9
490 d19c12b5 b94e16de e883d0cb 4e3c50a2
491
492
493
494
495
496
497
498
499
500 <span class="grey">Nir &amp; Langley Informational [Page 9]</span></pre>
501 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-10" ></span>
502 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
503
504
505 After we serialize the state, we get this:
506
507 Serialized Block:
508 000 10 f1 e7 e4 d1 3b 59 15 50 0f dd 1f a3 20 71 c4 .....;Y.P.... q.
509 016 c7 d1 f4 c7 33 c0 68 03 04 22 aa 9a c3 d4 6c 4e ....3.h.."....lN
510 032 d2 82 64 46 07 9f aa 09 14 c2 d7 05 d9 8b 02 a2 ..dF............
511 048 b5 12 9c d1 de 16 4e b9 cb d0 83 e8 a2 50 3c 4e ......N......P&lt;N
512
513 <span class="h3"><a class="selflink" id="section-2.4" href="#section-2.4">2.4</a>. The ChaCha20 Encryption Algorithm</span>
514
515 ChaCha20 is a stream cipher designed by D. J. Bernstein. It is a
516 refinement of the Salsa20 algorithm, and it uses a 256-bit key.
517
518 ChaCha20 successively calls the ChaCha20 block function, with the
519 same key and nonce, and with successively increasing block counter
520 parameters. ChaCha20 then serializes the resulting state by writing
521 the numbers in little-endian order, creating a keystream block.
522
523 Concatenating the keystream blocks from the successive blocks forms a
524 keystream. The ChaCha20 function then performs an XOR of this
525 keystream with the plaintext. Alternatively, each keystream block
526 can be XORed with a plaintext block before proceeding to create the
527 next block, saving some memory. There is no requirement for the
528 plaintext to be an integral multiple of 512 bits. If there is extra
529 keystream from the last block, it is discarded. Specific protocols
530 MAY require that the plaintext and ciphertext have certain length.
531 Such protocols need to specify how the plaintext is padded and how
532 much padding it receives.
533
534 The inputs to ChaCha20 are:
535
536 o A 256-bit key
537
538 o A 32-bit initial counter. This can be set to any number, but will
539 usually be zero or one. It makes sense to use one if we use the
540 zero block for something else, such as generating a one-time
541 authenticator key as part of an AEAD algorithm.
542
543 o A 96-bit nonce. In some protocols, this is known as the
544 Initialization Vector.
545
546 o An arbitrary-length plaintext
547
548 The output is an encrypted message, or "ciphertext", of the same
549 length.
550
551
552
553
554
555
556 <span class="grey">Nir &amp; Langley Informational [Page 10]</span></pre>
557 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-11" ></span>
558 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
559
560
561 Decryption is done in the same way. The ChaCha20 block function is
562 used to expand the key into a keystream, which is XORed with the
563 ciphertext giving back the plaintext.
564
565 <span class="h4"><a class="selflink" id="section-2.4.1" href="#section-2.4.1">2.4.1</a>. The ChaCha20 Encryption Algorithm in Pseudocode</span>
566
567 chacha20_encrypt(key, counter, nonce, plaintext):
568 for j = 0 upto floor(len(plaintext)/64)-1
569 key_stream = chacha20_block(key, counter+j, nonce)
570 block = plaintext[(j*64)..(j*64+63)]
571 encrypted_message += block ^ key_stream
572 end
573 if ((len(plaintext) % 64) != 0)
574 j = floor(len(plaintext)/64)
575 key_stream = chacha20_block(key, counter+j, nonce)
576 block = plaintext[(j*64)..len(plaintext)-1]
577 encrypted_message += (block^key_stream)[0..len(plaintext)%64]
578 end
579 return encrypted_message
580 end
581
582 <span class="h4"><a class="selflink" id="section-2.4.2" href="#section-2.4.2">2.4.2</a>. Example and Test Vector for the ChaCha20 Cipher</span>
583
584 For a test vector, we will use the following inputs to the ChaCha20
585 block function:
586
587 o Key = 00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f:10:11:12:13:
588 14:15:16:17:18:19:1a:1b:1c:1d:1e:1f.
589
590 o Nonce = (00:00:00:00:00:00:00:4a:00:00:00:00).
591
592 o Initial Counter = 1.
593
594 We use the following for the plaintext. It was chosen to be long
595 enough to require more than one block, but not so long that it would
596 make this example cumbersome (so, less than 3 blocks):
597
598 Plaintext Sunscreen:
599 000 4c 61 64 69 65 73 20 61 6e 64 20 47 65 6e 74 6c Ladies and Gentl
600 016 65 6d 65 6e 20 6f 66 20 74 68 65 20 63 6c 61 73 emen of the clas
601 032 73 20 6f 66 20 27 39 39 3a 20 49 66 20 49 20 63 s of '99: If I c
602 048 6f 75 6c 64 20 6f 66 66 65 72 20 79 6f 75 20 6f ould offer you o
603 064 6e 6c 79 20 6f 6e 65 20 74 69 70 20 66 6f 72 20 nly one tip for
604 080 74 68 65 20 66 75 74 75 72 65 2c 20 73 75 6e 73 the future, suns
605 096 63 72 65 65 6e 20 77 6f 75 6c 64 20 62 65 20 69 creen would be i
606 112 74 2e t.
607
608
609
610
611
612 <span class="grey">Nir &amp; Langley Informational [Page 11]</span></pre>
613 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-12" ></span>
614 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
615
616
617 The following figure shows four ChaCha state matrices:
618
619 1. First block as it is set up.
620
621 2. Second block as it is set up. Note that these blocks are only
622 two bits apart -- only the counter in position 12 is different.
623
624 3. Third block is the first block after the ChaCha20 block
625 operation.
626
627 4. Final block is the second block after the ChaCha20 block
628 operation was applied.
629
630 After that, we show the keystream.
631
632 First block setup:
633 61707865 3320646e 79622d32 6b206574
634 03020100 07060504 0b0a0908 0f0e0d0c
635 13121110 17161514 1b1a1918 1f1e1d1c
636 00000001 00000000 4a000000 00000000
637
638 Second block setup:
639 61707865 3320646e 79622d32 6b206574
640 03020100 07060504 0b0a0908 0f0e0d0c
641 13121110 17161514 1b1a1918 1f1e1d1c
642 00000002 00000000 4a000000 00000000
643
644 First block after block operation:
645 f3514f22 e1d91b40 6f27de2f ed1d63b8
646 821f138c e2062c3d ecca4f7e 78cff39e
647 a30a3b8a 920a6072 cd7479b5 34932bed
648 40ba4c79 cd343ec6 4c2c21ea b7417df0
649
650 Second block after block operation:
651 9f74a669 410f633f 28feca22 7ec44dec
652 6d34d426 738cb970 3ac5e9f3 45590cc4
653 da6e8b39 892c831a cdea67c1 2b7e1d90
654 037463f3 a11a2073 e8bcfb88 edc49139
655
656 Keystream:
657 22:4f:51:f3:40:1b:d9:e1:2f:de:27:6f:b8:63:1d:ed:8c:13:1f:82:3d:2c:06
658 e2:7e:4f:ca:ec:9e:f3:cf:78:8a:3b:0a:a3:72:60:0a:92:b5:79:74:cd:ed:2b
659 93:34:79:4c:ba:40:c6:3e:34:cd:ea:21:2c:4c:f0:7d:41:b7:69:a6:74:9f:3f
660 63:0f:41:22:ca:fe:28:ec:4d:c4:7e:26:d4:34:6d:70:b9:8c:73:f3:e9:c5:3a
661 c4:0c:59:45:39:8b:6e:da:1a:83:2c:89:c1:67:ea:cd:90:1d:7e:2b:f3:63
662
663
664
665
666
667
668 <span class="grey">Nir &amp; Langley Informational [Page 12]</span></pre>
669 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-13" ></span>
670 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
671
672
673 Finally, we XOR the keystream with the plaintext, yielding the
674 ciphertext:
675
676 Ciphertext Sunscreen:
677 000 6e 2e 35 9a 25 68 f9 80 41 ba 07 28 dd 0d 69 81 n.5.%h..A..(..i.
678 016 e9 7e 7a ec 1d 43 60 c2 0a 27 af cc fd 9f ae 0b .~z..C`..'......
679 032 f9 1b 65 c5 52 47 33 ab 8f 59 3d ab cd 62 b3 57 ..e.RG3..Y=..b.W
680 048 16 39 d6 24 e6 51 52 ab 8f 53 0c 35 9f 08 61 d8 .9.$.QR..S.5..a.
681 064 07 ca 0d bf 50 0d 6a 61 56 a3 8e 08 8a 22 b6 5e ....P.jaV....".^
682 <a href="#section-080">080</a> 52 bc 51 4d 16 cc f8 06 81 8c e9 1a b7 79 37 36 R.QM.........y76
683 096 5a f9 0b bf 74 a3 5b e6 b4 0b 8e ed f2 78 5e 42 Z...t.[......x^B
684 112 87 4d .M
685
686 <span class="h3"><a class="selflink" id="section-2.5" href="#section-2.5">2.5</a>. The Poly1305 Algorithm</span>
687
688 Poly1305 is a one-time authenticator designed by D. J. Bernstein.
689 Poly1305 takes a 32-byte one-time key and a message and produces a
690 16-byte tag. This tag is used to authenticate the message.
691
692 The original article ([<a href="#ref-Poly1305" title="&quot;The Poly1305-AES message-authentication code&quot;">Poly1305</a>]) is titled "The Poly1305-AES
693 message-authentication code", and the MAC function there requires a
694 128-bit AES key, a 128-bit "additional key", and a 128-bit (non-
695 secret) nonce. AES is used there for encrypting the nonce, so as to
696 get a unique (and secret) 128-bit string, but as the paper states,
697 "There is nothing special about AES here. One can replace AES with
698 an arbitrary keyed function from an arbitrary set of nonces to
699 16-byte strings."
700
701 Regardless of how the key is generated, the key is partitioned into
702 two parts, called "r" and "s". The pair (r,s) should be unique, and
703 MUST be unpredictable for each invocation (that is why it was
704 originally obtained by encrypting a nonce), while "r" MAY be
705 constant, but needs to be modified as follows before being used: ("r"
706 is treated as a 16-octet little-endian number):
707
708 o r[3], r[7], r[11], and r[15] are required to have their top four
709 bits clear (be smaller than 16)
710
711 o r[4], r[8], and r[12] are required to have their bottom two bits
712 clear (be divisible by 4)
713
714
715
716
717
718
719
720
721
722
723
724 <span class="grey">Nir &amp; Langley Informational [Page 13]</span></pre>
725 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-14" ></span>
726 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
727
728
729 The following sample code clamps "r" to be appropriate:
730
731 /*
732 Adapted from poly1305aes_test_clamp.c version 20050207
733 D. J. Bernstein
734 Public domain.
735 */
736
737 #include "poly1305aes_test.h"
738
739 void poly1305aes_test_clamp(unsigned char r[16])
740 {
741 r[3] &amp;= 15;
742 r[7] &amp;= 15;
743 r[11] &amp;= 15;
744 r[15] &amp;= 15;
745 r[4] &amp;= 252;
746 r[8] &amp;= 252;
747 r[12] &amp;= 252;
748 }
749
750 The "s" should be unpredictable, but it is perfectly acceptable to
751 generate both "r" and "s" uniquely each time. Because each of them
752 is 128 bits, pseudorandomly generating them (see <a href="#section-2.6">Section 2.6</a>) is also
753 acceptable.
754
755 The inputs to Poly1305 are:
756
757 o A 256-bit one-time key
758
759 o An arbitrary length message
760
761 The output is a 128-bit tag.
762
763 First, the "r" value should be clamped.
764
765 Next, set the constant prime "P" be 2^130-5:
766 3fffffffffffffffffffffffffffffffb. Also set a variable "accumulator"
767 to zero.
768
769 Next, divide the message into 16-byte blocks. The last one might be
770 shorter:
771
772 o Read the block as a little-endian number.
773
774
775
776
777
778
779
780 <span class="grey">Nir &amp; Langley Informational [Page 14]</span></pre>
781 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-15" ></span>
782 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
783
784
785 o Add one bit beyond the number of octets. For a 16-byte block,
786 this is equivalent to adding 2^128 to the number. For the shorter
787 block, it can be 2^120, 2^112, or any power of two that is evenly
788 divisible by 8, all the way down to 2^8.
789
790 o If the block is not 17 bytes long (the last block), pad it with
791 zeros. This is meaningless if you are treating the blocks as
792 numbers.
793
794 o Add this number to the accumulator.
795
796 o Multiply by "r".
797
798 o Set the accumulator to the result modulo p. To summarize: Acc =
799 ((Acc+block)*r) % p.
800
801 Finally, the value of the secret key "s" is added to the accumulator,
802 and the 128 least significant bits are serialized in little-endian
803 order to form the tag.
804
805 <span class="h4"><a class="selflink" id="section-2.5.1" href="#section-2.5.1">2.5.1</a>. The Poly1305 Algorithms in Pseudocode</span>
806
807 clamp(r): r &amp;= 0x0ffffffc0ffffffc0ffffffc0fffffff
808 poly1305_mac(msg, key):
809 r = (le_bytes_to_num(key[0..15])
810 clamp(r)
811 s = le_num(key[16..31])
812 accumulator = 0
813 p = (1&lt;&lt;130)-5
814 for i=1 upto ceil(msg length in bytes / 16)
815 n = le_bytes_to_num(msg[((i-1)*16)..(i*16)] | [0x01])
816 a += n
817 a = (r * a) % p
818 end
819 a += s
820 return num_to_16_le_bytes(a)
821 end
822
823 <span class="h4"><a class="selflink" id="section-2.5.2" href="#section-2.5.2">2.5.2</a>. Poly1305 Example and Test Vector</span>
824
825 For our example, we will dispense with generating the one-time key
826 using AES, and assume that we got the following keying material:
827
828 o Key Material: 85:d6:be:78:57:55:6d:33:7f:44:52:fe:42:d5:06:a8:01:0
829 3:80:8a:fb:0d:b2:fd:4a:bf:f6:af:41:49:f5:1b
830
831 o s as an octet string:
832 01:03:80:8a:fb:0d:b2:fd:4a:bf:f6:af:41:49:f5:1b
833
834
835
836 <span class="grey">Nir &amp; Langley Informational [Page 15]</span></pre>
837 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-16" ></span>
838 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
839
840
841 o s as a 128-bit number: 1bf54941aff6bf4afdb20dfb8a800301
842
843 o r before clamping: 85:d6:be:78:57:55:6d:33:7f:44:52:fe:42:d5:06:a8
844
845 o Clamped r as a number: 806d5400e52447c036d555408bed685
846
847 For our message, we'll use a short text:
848
849 Message to be Authenticated:
850 000 43 72 79 70 74 6f 67 72 61 70 68 69 63 20 46 6f Cryptographic Fo
851 016 72 75 6d 20 52 65 73 65 61 72 63 68 20 47 72 6f rum Research Gro
852 032 75 70 up
853
854 Since Poly1305 works in 16-byte chunks, the 34-byte message divides
855 into three blocks. In the following calculation, "Acc" denotes the
856 accumulator and "Block" the current block:
857
858 Block #1
859
860 Acc = 00
861 Block = 6f4620636968706172676f7470797243
862 Block with 0x01 byte = 016f4620636968706172676f7470797243
863 Acc + block = 016f4620636968706172676f7470797243
864 (Acc+Block) * r =
865 b83fe991ca66800489155dcd69e8426ba2779453994ac90ed284034da565ecf
866 Acc = ((Acc+Block)*r) % P = 2c88c77849d64ae9147ddeb88e69c83fc
867
868 Block #2
869
870 Acc = 2c88c77849d64ae9147ddeb88e69c83fc
871 Block = 6f7247206863726165736552206d7572
872 Block with 0x01 byte = 016f7247206863726165736552206d7572
873 Acc + block = 437febea505c820f2ad5150db0709f96e
874 (Acc+Block) * r =
875 21dcc992d0c659ba4036f65bb7f88562ae59b32c2b3b8f7efc8b00f78e548a26
876 Acc = ((Acc+Block)*r) % P = 2d8adaf23b0337fa7cccfb4ea344b30de
877
878 Last Block
879
880 Acc = 2d8adaf23b0337fa7cccfb4ea344b30de
881 Block = 7075
882 Block with 0x01 byte = 017075
883 Acc + block = 2d8adaf23b0337fa7cccfb4ea344ca153
884 (Acc + Block) * r =
885 16d8e08a0f3fe1de4fe4a15486aca7a270a29f1e6c849221e4a6798b8e45321f
886 ((Acc + Block) * r) % P = 28d31b7caff946c77c8844335369d03a7
887
888
889
890
891
892 <span class="grey">Nir &amp; Langley Informational [Page 16]</span></pre>
893 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-17" ></span>
894 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
895
896
897 Adding s, we get this number, and serialize if to get the tag:
898
899 Acc + s = 2a927010caf8b2bc2c6365130c11d06a8
900
901 Tag: a8:06:1d:c1:30:51:36:c6:c2:2b:8b:af:0c:01:27:a9
902
903 <span class="h3"><a class="selflink" id="section-2.6" href="#section-2.6">2.6</a>. Generating the Poly1305 Key Using ChaCha20</span>
904
905 As said in <a href="#section-2.5">Section 2.5</a>, it is acceptable to generate the one-time
906 Poly1305 pseudorandomly. This section defines such a method.
907
908 To generate such a key pair (r,s), we will use the ChaCha20 block
909 function described in <a href="#section-2.3">Section 2.3</a>. This assumes that we have a
910 256-bit session key for the Message Authentication Code (MAC)
911 function, such as SK_ai and SK_ar in Internet Key Exchange Protocol
912 version 2 (IKEv2) ([<a href="./rfc7296" title="&quot;Internet Key Exchange Protocol Version 2 (IKEv2)&quot;">RFC7296</a>]), the integrity key in the Encapsulating
913 Security Payload (ESP) and Authentication Header (AH), or the
914 client_write_MAC_key and server_write_MAC_key in TLS. Any document
915 that specifies the use of Poly1305 as a MAC algorithm for some
916 protocol must specify that 256 bits are allocated for the integrity
917 key. Note that in the AEAD construction defined in <a href="#section-2.8">Section 2.8</a>, the
918 same key is used for encryption and key generation, so the use of
919 SK_a* or *_write_MAC_key is only for stand-alone Poly1305.
920
921 The method is to call the block function with the following
922 parameters:
923
924 o The 256-bit session integrity key is used as the ChaCha20 key.
925
926 o The block counter is set to zero.
927
928 o The protocol will specify a 96-bit or 64-bit nonce. This MUST be
929 unique per invocation with the same key, so it MUST NOT be
930 randomly generated. A counter is a good way to implement this,
931 but other methods, such as a Linear Feedback Shift Register (LFSR)
932 are also acceptable. ChaCha20 as specified here requires a 96-bit
933 nonce. So if the provided nonce is only 64-bit, then the first 32
934 bits of the nonce will be set to a constant number. This will
935 usually be zero, but for protocols with multiple senders it may be
936 different for each sender, but should be the same for all
937 invocations of the function with the same key by a particular
938 sender.
939
940 After running the block function, we have a 512-bit state. We take
941 the first 256 bits or the serialized state, and use those as the one-
942 time Poly1305 key: the first 128 bits are clamped and form "r", while
943 the next 128 bits become "s". The other 256 bits are discarded.
944
945
946
947
948 <span class="grey">Nir &amp; Langley Informational [Page 17]</span></pre>
949 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-18" ></span>
950 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
951
952
953 Note that while many protocols have provisions for a nonce for
954 encryption algorithms (often called Initialization Vectors, or IVs),
955 they usually don't have such a provision for the MAC function. In
956 that case, the per-invocation nonce will have to come from somewhere
957 else, such as a message counter.
958
959 <span class="h4"><a class="selflink" id="section-2.6.1" href="#section-2.6.1">2.6.1</a>. Poly1305 Key Generation in Pseudocode</span>
960
961 poly1305_key_gen(key,nonce):
962 counter = 0
963 block = chacha20_block(key,counter,nonce)
964 return block[0..31]
965 end
966
967 <span class="h4"><a class="selflink" id="section-2.6.2" href="#section-2.6.2">2.6.2</a>. Poly1305 Key Generation Test Vector</span>
968
969 For this example, we'll set:
970
971 Key:
972 000 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................
973 016 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................
974
975 Nonce:
976 000 00 00 00 00 00 01 02 03 04 05 06 07 ............
977
978 The ChaCha state setup with key, nonce, and block counter zero:
979 61707865 3320646e 79622d32 6b206574
980 83828180 87868584 8b8a8988 8f8e8d8c
981 93929190 97969594 9b9a9998 9f9e9d9c
982 00000000 00000000 03020100 07060504
983
984 The ChaCha state after 20 rounds:
985 8ba0d58a cc815f90 27405081 7194b24a
986 37b633a8 a50dfde3 e2b8db08 46a6d1fd
987 7da03782 9183a233 148ad271 b46773d1
988 3cc1875a 8607def1 ca5c3086 7085eb87
989
990 Output bytes:
991 000 8a d5 a0 8b 90 5f 81 cc 81 50 40 27 4a b2 94 71 ....._...P@'J..q
992 016 a8 33 b6 37 e3 fd 0d a5 08 db b8 e2 fd d1 a6 46 .3.7...........F
993
994 And that output is also the 32-byte one-time key used for Poly1305.
995
996 <span class="h3"><a class="selflink" id="section-2.7" href="#section-2.7">2.7</a>. A Pseudorandom Function for Crypto Suites based on ChaCha/Poly1305</span>
997
998 Some protocols, such as IKEv2 ([<a href="./rfc7296" title="&quot;Internet Key Exchange Protocol Version 2 (IKEv2)&quot;">RFC7296</a>]), require a Pseudorandom
999 Function (PRF), mostly for key derivation. In the IKEv2 definition,
1000 a PRF is a function that accepts a variable-length key and a
1001
1002
1003
1004 <span class="grey">Nir &amp; Langley Informational [Page 18]</span></pre>
1005 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-19" ></span>
1006 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1007
1008
1009 variable-length input, and returns a fixed-length output. Most
1010 commonly, Hashed MAC (HMAC) constructions are used for this purpose,
1011 and often the same function is used for both message authentication
1012 and PRF.
1013
1014 Poly1305 is not a suitable choice for a PRF. Poly1305 prohibits
1015 using the same key twice, whereas the PRF in IKEv2 is used multiple
1016 times with the same key. Additionally, unlike HMAC, Poly1305 is
1017 biased, so using it for key derivation would reduce the security of
1018 the symmetric encryption.
1019
1020 Chacha20 could be used as a key-derivation function, by generating an
1021 arbitrarily long keystream. However, that is not what protocols such
1022 as IKEv2 require.
1023
1024 For this reason, this document does not specify a PRF and recommends
1025 that crypto suites use some other PRF such as PRF_HMAC_SHA2_256 (see
1026 <a href="./rfc4868#section-2.1.2">Section&nbsp;2.1.2 of [RFC4868]</a>).
1027
1028 <span class="h3"><a class="selflink" id="section-2.8" href="#section-2.8">2.8</a>. AEAD Construction</span>
1029
1030 AEAD_CHACHA20_POLY1305 is an authenticated encryption with additional
1031 data algorithm. The inputs to AEAD_CHACHA20_POLY1305 are:
1032
1033 o A 256-bit key
1034
1035 o A 96-bit nonce -- different for each invocation with the same key
1036
1037 o An arbitrary length plaintext
1038
1039 o Arbitrary length additional authenticated data (AAD)
1040
1041 Some protocols may have unique per-invocation inputs that are not 96
1042 bits in length. For example, IPsec may specify a 64-bit nonce. In
1043 such a case, it is up to the protocol document to define how to
1044 transform the protocol nonce into a 96-bit nonce, for example, by
1045 concatenating a constant value.
1046
1047 The ChaCha20 and Poly1305 primitives are combined into an AEAD that
1048 takes a 256-bit key and 96-bit nonce as follows:
1049
1050 o First, a Poly1305 one-time key is generated from the 256-bit key
1051 and nonce using the procedure described in <a href="#section-2.6">Section 2.6</a>.
1052
1053 o Next, the ChaCha20 encryption function is called to encrypt the
1054 plaintext, using the same key and nonce, and with the initial
1055 counter set to 1.
1056
1057
1058
1059
1060 <span class="grey">Nir &amp; Langley Informational [Page 19]</span></pre>
1061 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-20" ></span>
1062 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1063
1064
1065 o Finally, the Poly1305 function is called with the Poly1305 key
1066 calculated above, and a message constructed as a concatenation of
1067 the following:
1068
1069 * The AAD
1070
1071 * padding1 -- the padding is up to 15 zero bytes, and it brings
1072 the total length so far to an integral multiple of 16. If the
1073 length of the AAD was already an integral multiple of 16 bytes,
1074 this field is zero-length.
1075
1076 * The ciphertext
1077
1078 * padding2 -- the padding is up to 15 zero bytes, and it brings
1079 the total length so far to an integral multiple of 16. If the
1080 length of the ciphertext was already an integral multiple of 16
1081 bytes, this field is zero-length.
1082
1083 * The length of the additional data in octets (as a 64-bit
1084 little-endian integer).
1085
1086 * The length of the ciphertext in octets (as a 64-bit little-
1087 endian integer).
1088
1089 The output from the AEAD is twofold:
1090
1091 o A ciphertext of the same length as the plaintext.
1092
1093 o A 128-bit tag, which is the output of the Poly1305 function.
1094
1095 Decryption is similar with the following differences:
1096
1097 o The roles of ciphertext and plaintext are reversed, so the
1098 ChaCha20 encryption function is applied to the ciphertext,
1099 producing the plaintext.
1100
1101 o The Poly1305 function is still run on the AAD and the ciphertext,
1102 not the plaintext.
1103
1104 o The calculated tag is bitwise compared to the received tag. The
1105 message is authenticated if and only if the tags match.
1106
1107 A few notes about this design:
1108
1109 1. The amount of encrypted data possible in a single invocation is
1110 2^32-1 blocks of 64 bytes each, because of the size of the block
1111 counter field in the ChaCha20 block function. This gives a total
1112 of 247,877,906,880 bytes, or nearly 256 GB. This should be
1113
1114
1115
1116 <span class="grey">Nir &amp; Langley Informational [Page 20]</span></pre>
1117 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-21" ></span>
1118 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1119
1120
1121 enough for traffic protocols such as IPsec and TLS, but may be
1122 too small for file and/or disk encryption. For such uses, we can
1123 return to the original design, reduce the nonce to 64 bits, and
1124 use the integer at position 13 as the top 32 bits of a 64-bit
1125 block counter, increasing the total message size to over a
1126 million petabytes (1,180,591,620,717,411,303,360 bytes to be
1127 exact).
1128
1129 2. Despite the previous item, the ciphertext length field in the
1130 construction of the buffer on which Poly1305 runs limits the
1131 ciphertext (and hence, the plaintext) size to 2^64 bytes, or
1132 sixteen thousand petabytes (18,446,744,073,709,551,616 bytes to
1133 be exact).
1134
1135 The AEAD construction in this section is a novel composition of
1136 ChaCha20 and Poly1305. A security analysis of this composition is
1137 given in [<a href="#ref-Procter" title="&quot;A Security Analysis of the Composition of ChaCha20 and Poly1305&quot;">Procter</a>].
1138
1139 Here is a list of the parameters for this construction as defined in
1140 <a href="./rfc5116#section-4">Section&nbsp;4 of RFC 5116</a>:
1141
1142 o K_LEN (key length) is 32 octets.
1143
1144 o P_MAX (maximum size of the plaintext) is 247,877,906,880 bytes, or
1145 nearly 256 GB.
1146
1147 o A_MAX (maximum size of the associated data) is set to 2^64-1
1148 octets by the length field for associated data.
1149
1150 o N_MIN = N_MAX = 12 octets.
1151
1152 o C_MAX = P_MAX + tag length = 247,877,906,896 octets.
1153
1154 Distinct AAD inputs (as described in <a href="./rfc5116#section-3.3">Section&nbsp;3.3 of RFC 5116</a>) shall
1155 be concatenated into a single input to AEAD_CHACHA20_POLY1305. It is
1156 up to the application to create a structure in the AAD input if it is
1157 needed.
1158
1159 <span class="h4"><a class="selflink" id="section-2.8.1" href="#section-2.8.1">2.8.1</a>. Pseudocode for the AEAD Construction</span>
1160
1161 pad16(x):
1162 if (len(x) % 16)==0
1163 then return NULL
1164 else return copies(0, 16-(len(x)%16))
1165 end
1166
1167
1168
1169
1170
1171
1172 <span class="grey">Nir &amp; Langley Informational [Page 21]</span></pre>
1173 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-22" ></span>
1174 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1175
1176
1177 chacha20_aead_encrypt(aad, key, iv, constant, plaintext):
1178 nonce = constant | iv
1179 otk = poly1305_key_gen(key, nonce)
1180 ciphertext = chacha20_encrypt(key, 1, nonce, plaintext)
1181 mac_data = aad | pad16(aad)
1182 mac_data |= ciphertext | pad16(ciphertext)
1183 mac_data |= num_to_4_le_bytes(aad.length)
1184 mac_data |= num_to_4_le_bytes(ciphertext.length)
1185 tag = poly1305_mac(mac_data, otk)
1186 return (ciphertext, tag)
1187
1188 <span class="h4"><a class="selflink" id="section-2.8.2" href="#section-2.8.2">2.8.2</a>. Example and Test Vector for AEAD_CHACHA20_POLY1305</span>
1189
1190 For a test vector, we will use the following inputs to the
1191 AEAD_CHACHA20_POLY1305 function:
1192
1193 Plaintext:
1194 000 4c 61 64 69 65 73 20 61 6e 64 20 47 65 6e 74 6c Ladies and Gentl
1195 016 65 6d 65 6e 20 6f 66 20 74 68 65 20 63 6c 61 73 emen of the clas
1196 032 73 20 6f 66 20 27 39 39 3a 20 49 66 20 49 20 63 s of '99: If I c
1197 048 6f 75 6c 64 20 6f 66 66 65 72 20 79 6f 75 20 6f ould offer you o
1198 064 6e 6c 79 20 6f 6e 65 20 74 69 70 20 66 6f 72 20 nly one tip for
1199 080 74 68 65 20 66 75 74 75 72 65 2c 20 73 75 6e 73 the future, suns
1200 096 63 72 65 65 6e 20 77 6f 75 6c 64 20 62 65 20 69 creen would be i
1201 112 74 2e t.
1202
1203 AAD:
1204 000 50 51 52 53 c0 c1 c2 c3 c4 c5 c6 c7 PQRS........
1205
1206 Key:
1207 000 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................
1208 016 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................
1209
1210 IV:
1211 000 40 41 42 43 44 45 46 47 @ABCDEFG
1212
1213 32-bit fixed-common part:
1214 000 07 00 00 00 ....
1215
1216 Setup for generating Poly1305 one-time key (sender id=7):
1217 61707865 3320646e 79622d32 6b206574
1218 83828180 87868584 8b8a8988 8f8e8d8c
1219 93929190 97969594 9b9a9998 9f9e9d9c
1220 00000000 00000007 43424140 47464544
1221
1222
1223
1224
1225
1226
1227
1228 <span class="grey">Nir &amp; Langley Informational [Page 22]</span></pre>
1229 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-23" ></span>
1230 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1231
1232
1233 After generating Poly1305 one-time key:
1234 252bac7b af47b42d 557ab609 8455e9a4
1235 73d6e10a ebd97510 7875932a ff53d53e
1236 decc7ea2 b44ddbad e49c17d1 d8430bc9
1237 8c94b7bc 8b7d4b4b 3927f67d 1669a432
1238
1239 Poly1305 Key:
1240 000 7b ac 2b 25 2d b4 47 af 09 b6 7a 55 a4 e9 55 84 {.+%-.G...zU..U.
1241 016 0a e1 d6 73 10 75 d9 eb 2a 93 75 78 3e d5 53 ff ...s.u..*.ux&gt;.S.
1242
1243 Poly1305 r = 455e9a4057ab6080f47b42c052bac7b
1244 Poly1305 s = ff53d53e7875932aebd9751073d6e10a
1245
1246 keystream bytes:
1247 9f:7b:e9:5d:01:fd:40:ba:15:e2:8f:fb:36:81:0a:ae:
1248 c1:c0:88:3f:09:01:6e:de:dd:8a:d0:87:55:82:03:a5:
1249 4e:9e:cb:38:ac:8e:5e:2b:b8:da:b2:0f:fa:db:52:e8:
1250 75:04:b2:6e:be:69:6d:4f:60:a4:85:cf:11:b8:1b:59:
1251 fc:b1:c4:5f:42:19:ee:ac:ec:6a:de:c3:4e:66:69:78:
1252 8e:db:41:c4:9c:a3:01:e1:27:e0:ac:ab:3b:44:b9:cf:
1253 5c:86:bb:95:e0:6b:0d:f2:90:1a:b6:45:e4:ab:e6:22:
1254 15:38
1255
1256 Ciphertext:
1257 000 d3 1a 8d 34 64 8e 60 db 7b 86 af bc 53 ef 7e c2 ...4d.`.{...S.~.
1258 016 a4 ad ed 51 29 6e 08 fe a9 e2 b5 a7 36 ee 62 d6 ...Q)n......6.b.
1259 032 3d be a4 5e 8c a9 67 12 82 fa fb 69 da 92 72 8b =..^..g....i..r.
1260 <a href="#section-048">048</a> 1a 71 de 0a 9e 06 0b 29 05 d6 a5 b6 7e cd 3b 36 .q.....)....~.;6
1261 064 92 dd bd 7f 2d 77 8b 8c 98 03 ae e3 28 09 1b 58 ....-w......(..X
1262 080 fa b3 24 e4 fa d6 75 94 55 85 80 8b 48 31 d7 bc ..$...u.U...H1..
1263 096 3f f4 de f0 8e 4b 7a 9d e5 76 d2 65 86 ce c6 4b ?....Kz..v.e...K
1264 112 61 16 a.
1265
1266 AEAD Construction for Poly1305:
1267 000 50 51 52 53 c0 c1 c2 c3 c4 c5 c6 c7 00 00 00 00 PQRS............
1268 016 d3 1a 8d 34 64 8e 60 db 7b 86 af bc 53 ef 7e c2 ...4d.`.{...S.~.
1269 032 a4 ad ed 51 29 6e 08 fe a9 e2 b5 a7 36 ee 62 d6 ...Q)n......6.b.
1270 048 3d be a4 5e 8c a9 67 12 82 fa fb 69 da 92 72 8b =..^..g....i..r.
1271 <a href="#section-064">064</a> 1a 71 de 0a 9e 06 0b 29 05 d6 a5 b6 7e cd 3b 36 .q.....)....~.;6
1272 080 92 dd bd 7f 2d 77 8b 8c 98 03 ae e3 28 09 1b 58 ....-w......(..X
1273 096 fa b3 24 e4 fa d6 75 94 55 85 80 8b 48 31 d7 bc ..$...u.U...H1..
1274 112 3f f4 de f0 8e 4b 7a 9d e5 76 d2 65 86 ce c6 4b ?....Kz..v.e...K
1275 128 61 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 a...............
1276 144 0c 00 00 00 00 00 00 00 72 00 00 00 00 00 00 00 ........r.......
1277
1278 Note the four zero bytes in line 000 and the 14 zero bytes in line
1279 128
1280
1281
1282
1283
1284 <span class="grey">Nir &amp; Langley Informational [Page 23]</span></pre>
1285 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-24" ></span>
1286 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1287
1288
1289 Tag:
1290 1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91
1291
1292 <span class="h2"><a class="selflink" id="section-3" href="#section-3">3</a>. Implementation Advice</span>
1293
1294 Each block of ChaCha20 involves 16 move operations and one increment
1295 operation for loading the state, 80 each of XOR, addition and Roll
1296 operations for the rounds, 16 more add operations and 16 XOR
1297 operations for protecting the plaintext. <a href="#section-2.3">Section 2.3</a> describes the
1298 ChaCha block function as "adding the original input words". This
1299 implies that before starting the rounds on the ChaCha state, we copy
1300 it aside, only to add it in later. This is correct, but we can save
1301 a few operations if we instead copy the state and do the work on the
1302 copy. This way, for the next block you don't need to recreate the
1303 state, but only to increment the block counter. This saves
1304 approximately 5.5% of the cycles.
1305
1306 It is not recommended to use a generic big number library such as the
1307 one in OpenSSL for the arithmetic operations in Poly1305. Such
1308 libraries use dynamic allocation to be able to handle an integer of
1309 any size, but that flexibility comes at the expense of performance as
1310 well as side-channel security. More efficient implementations that
1311 run in constant time are available, one of them in D. J. Bernstein's
1312 own library, NaCl ([<a href="#ref-NaCl" title="&quot;NaCl: Networking and Cryptography library&quot;">NaCl</a>]). A constant-time but not optimal approach
1313 would be to naively implement the arithmetic operations for 288-bit
1314 integers, because even a naive implementation will not exceed 2^288
1315 in the multiplication of (acc+block) and r. An efficient constant-
1316 time implementation can be found in the public domain library
1317 poly1305-donna ([<a href="#ref-Poly1305_Donna">Poly1305_Donna</a>]).
1318
1319 <span class="h2"><a class="selflink" id="section-4" href="#section-4">4</a>. Security Considerations</span>
1320
1321 The ChaCha20 cipher is designed to provide 256-bit security.
1322
1323 The Poly1305 authenticator is designed to ensure that forged messages
1324 are rejected with a probability of 1-(n/(2^102)) for a 16n-byte
1325 message, even after sending 2^64 legitimate messages, so it is
1326 SUF-CMA (strong unforgeability against chosen-message attacks) in the
1327 terminology of [<a href="#ref-AE" title="&quot;Authenticated Encryption: Relations among notions and analysis of the generic composition paradigm&quot;">AE</a>].
1328
1329 Proving the security of either of these is beyond the scope of this
1330 document. Such proofs are available in the referenced academic
1331 papers ([<a href="#ref-ChaCha" title="&quot;ChaCha, a variant of Salsa20&quot;">ChaCha</a>], [<a href="#ref-Poly1305" title="&quot;The Poly1305-AES message-authentication code&quot;">Poly1305</a>], [<a href="#ref-LatinDances">LatinDances</a>], [<a href="#ref-LatinDances2">LatinDances2</a>], and
1332 [<a href="#ref-Zhenqing2012">Zhenqing2012</a>]).
1333
1334 The most important security consideration in implementing this
1335 document is the uniqueness of the nonce used in ChaCha20. Counters
1336 and LFSRs are both acceptable ways of generating unique nonces, as is
1337
1338
1339
1340 <span class="grey">Nir &amp; Langley Informational [Page 24]</span></pre>
1341 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-25" ></span>
1342 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1343
1344
1345 encrypting a counter using a 64-bit cipher such as DES. Note that it
1346 is not acceptable to use a truncation of a counter encrypted with a
1347 128-bit or 256-bit cipher, because such a truncation may repeat after
1348 a short time.
1349
1350 Consequences of repeating a nonce: If a nonce is repeated, then both
1351 the one-time Poly1305 key and the keystream are identical between the
1352 messages. This reveals the XOR of the plaintexts, because the XOR of
1353 the plaintexts is equal to the XOR of the ciphertexts.
1354
1355 The Poly1305 key MUST be unpredictable to an attacker. Randomly
1356 generating the key would fulfill this requirement, except that
1357 Poly1305 is often used in communications protocols, so the receiver
1358 should know the key. Pseudorandom number generation such as by
1359 encrypting a counter is acceptable. Using ChaCha with a secret key
1360 and a nonce is also acceptable.
1361
1362 The algorithms presented here were designed to be easy to implement
1363 in constant time to avoid side-channel vulnerabilities. The
1364 operations used in ChaCha20 are all additions, XORs, and fixed
1365 rotations. All of these can and should be implemented in constant
1366 time. Access to offsets into the ChaCha state and the number of
1367 operations do not depend on any property of the key, eliminating the
1368 chance of information about the key leaking through the timing of
1369 cache misses.
1370
1371 For Poly1305, the operations are addition, multiplication. and
1372 modulus, all on numbers with greater than 128 bits. This can be done
1373 in constant time, but a naive implementation (such as using some
1374 generic big number library) will not be constant time. For example,
1375 if the multiplication is performed as a separate operation from the
1376 modulus, the result will sometimes be under 2^256 and sometimes be
1377 above 2^256. Implementers should be careful about timing side-
1378 channels for Poly1305 by using the appropriate implementation of
1379 these operations.
1380
1381 Validating the authenticity of a message involves a bitwise
1382 comparison of the calculated tag with the received tag. In most use
1383 cases, nonces and AAD contents are not "used up" until a valid
1384 message is received. This allows an attacker to send multiple
1385 identical messages with different tags until one passes the tag
1386 comparison. This is hard if the attacker has to try all 2^128
1387 possible tags one by one. However, if the timing of the tag
1388 comparison operation reveals how long a prefix of the calculated and
1389 received tags is identical, the number of messages can be reduced
1390 significantly. For this reason, with online protocols,
1391
1392
1393
1394
1395
1396 <span class="grey">Nir &amp; Langley Informational [Page 25]</span></pre>
1397 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-26" ></span>
1398 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1399
1400
1401 implementation MUST use a constant-time comparison function rather
1402 than relying on optimized but insecure library functions such as the
1403 C language's memcmp().
1404
1405 <span class="h2"><a class="selflink" id="section-5" href="#section-5">5</a>. IANA Considerations</span>
1406
1407 IANA has assigned an entry in the "Authenticated Encryption with
1408 Associated Data (AEAD) Parameters" registry with 29 as the Numeric
1409 ID, "AEAD_CHACHA20_POLY1305" as the name, and this document as
1410 reference.
1411
1412 <span class="h2"><a class="selflink" id="section-6" href="#section-6">6</a>. References</span>
1413
1414 <span class="h3"><a class="selflink" id="section-6.1" href="#section-6.1">6.1</a>. Normative References</span>
1415
1416 [<a id="ref-ChaCha">ChaCha</a>] Bernstein, D., "ChaCha, a variant of Salsa20", January
1417 2008, &lt;<a href="http://cr.yp.to/chacha/chacha-20080128.pdf">http://cr.yp.to/chacha/chacha-20080128.pdf</a>&gt;.
1418
1419 [<a id="ref-Poly1305">Poly1305</a>] Bernstein, D., "The Poly1305-AES message-authentication
1420 code", March 2005,
1421 &lt;<a href="http://cr.yp.to/mac/poly1305-20050329.pdf">http://cr.yp.to/mac/poly1305-20050329.pdf</a>&gt;.
1422
1423 [<a id="ref-RFC2119">RFC2119</a>] Bradner, S., "Key words for use in RFCs to Indicate
1424 Requirement Levels", <a href="https://www.rfc-editor.org/bcp/bcp14">BCP 14</a>, <a href="./rfc2119">RFC 2119</a>,
1425 DOI 10.17487/RFC2119, March 1997,
1426 &lt;<a href="http://www.rfc-editor.org/info/rfc2119">http://www.rfc-editor.org/info/rfc2119</a>&gt;.
1427
1428 <span class="h3"><a class="selflink" id="section-6.2" href="#section-6.2">6.2</a>. Informative References</span>
1429
1430 [<a id="ref-AE">AE</a>] Bellare, M. and C. Namprempre, "Authenticated Encryption:
1431 Relations among notions and analysis of the generic
1432 composition paradigm", September 2008,
1433 &lt;<a href="http://dl.acm.org/citation.cfm?id=1410269">http://dl.acm.org/citation.cfm?id=1410269</a>&gt;.
1434
1435 [<a id="ref-Cache-Collisions">Cache-Collisions</a>]
1436 Bonneau, J. and I. Mironov, "Cache-Collision Timing
1437 Attacks Against AES", 2006,
1438 &lt;<a href="http://research.microsoft.com/pubs/64024/aes-timing.pdf">http://research.microsoft.com/pubs/64024/aes-timing.pdf</a>&gt;.
1439
1440 [<a id="ref-FIPS-197">FIPS-197</a>] National Institute of Standards and Technology, "Advanced
1441 Encryption Standard (AES)", FIPS PUB 197, November 2001,
1442 &lt;<a href="http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf">http://csrc.nist.gov/publications/fips/fips197/</a>
1443 <a href="http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf">fips-197.pdf</a>&gt;.
1444
1445
1446
1447
1448
1449
1450
1451
1452 <span class="grey">Nir &amp; Langley Informational [Page 26]</span></pre>
1453 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-27" ></span>
1454 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1455
1456
1457 [<a id="ref-LatinDances">LatinDances</a>]
1458 Aumasson, J., Fischer, S., Khazaei, S., Meier, W., and C.
1459 Rechberger, "New Features of Latin Dances: Analysis of
1460 Salsa, ChaCha, and Rumba", December 2007,
1461 &lt;<a href="http://cr.yp.to/rumba20/newfeatures-20071218.pdf">http://cr.yp.to/rumba20/newfeatures-20071218.pdf</a>&gt;.
1462
1463 [<a id="ref-LatinDances2">LatinDances2</a>]
1464 Ishiguro, T., Kiyomoto, S., and Y. Miyake, "Modified
1465 version of 'Latin Dances Revisited: New Analytic Results
1466 of Salsa20 and ChaCha'", February 2012,
1467 &lt;<a href="https://eprint.iacr.org/2012/065.pdf">https://eprint.iacr.org/2012/065.pdf</a>&gt;.
1468
1469 [<a id="ref-NaCl">NaCl</a>] Bernstein, D., Lange, T., and P. Schwabe, "NaCl:
1470 Networking and Cryptography library", July 2012,
1471 &lt;<a href="http://nacl.cr.yp.to">http://nacl.cr.yp.to</a>&gt;.
1472
1473 [<a id="ref-Poly1305_Donna">Poly1305_Donna</a>]
1474 Floodyberry, A., "poly1305-donna", February 2014,
1475 &lt;<a href="https://github.com/floodyberry/poly1305-donna">https://github.com/floodyberry/poly1305-donna</a>&gt;.
1476
1477 [<a id="ref-Procter">Procter</a>] Procter, G., "A Security Analysis of the Composition of
1478 ChaCha20 and Poly1305", August 2014,
1479 &lt;<a href="http://eprint.iacr.org/2014/613.pdf">http://eprint.iacr.org/2014/613.pdf</a>&gt;.
1480
1481 [<a id="ref-RFC4868">RFC4868</a>] Kelly, S. and S. Frankel, "Using HMAC-SHA-256, HMAC-SHA-
1482 384, and HMAC-SHA-512 with IPsec", <a href="./rfc4868">RFC 4868</a>,
1483 DOI 10.17487/RFC4868, May 2007,
1484 &lt;<a href="http://www.rfc-editor.org/info/rfc4868">http://www.rfc-editor.org/info/rfc4868</a>&gt;.
1485
1486 [<a id="ref-RFC5116">RFC5116</a>] McGrew, D., "An Interface and Algorithms for Authenticated
1487 Encryption", <a href="./rfc5116">RFC 5116</a>, DOI 10.17487/RFC5116, January 2008,
1488 &lt;<a href="http://www.rfc-editor.org/info/rfc5116">http://www.rfc-editor.org/info/rfc5116</a>&gt;.
1489
1490 [<a id="ref-RFC7296">RFC7296</a>] Kaufman, C., Hoffman, P., Nir, Y., Eronen, P., and T.
1491 Kivinen, "Internet Key Exchange Protocol Version 2
1492 (IKEv2)", STD 79, <a href="./rfc7296">RFC 7296</a>, DOI 10.17487/RFC7296, October
1493 2014, &lt;<a href="http://www.rfc-editor.org/info/rfc7296">http://www.rfc-editor.org/info/rfc7296</a>&gt;.
1494
1495 [<a id="ref-SP800-67">SP800-67</a>] National Institute of Standards and Technology,
1496 "Recommendation for the Triple Data Encryption Algorithm
1497 (TDEA) Block Cipher", NIST 800-67, January 2012,
1498 &lt;<a href="http://csrc.nist.gov/publications/nistpubs/800-67-Rev1/SP-800-67-Rev1.pdf">http://csrc.nist.gov/publications/nistpubs/800-67-Rev1/</a>
1499 <a href="http://csrc.nist.gov/publications/nistpubs/800-67-Rev1/SP-800-67-Rev1.pdf">SP-800-67-Rev1.pdf</a>&gt;.
1500
1501
1502
1503
1504
1505
1506
1507
1508 <span class="grey">Nir &amp; Langley Informational [Page 27]</span></pre>
1509 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-28" ></span>
1510 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1511
1512
1513 [<a id="ref-Standby-Cipher">Standby-Cipher</a>]
1514 McGrew, D., Grieco, A., and Y. Sheffer, "Selection of
1515 Future Cryptographic Standards", Work in Progress,
1516 <a href="./draft-mcgrew-standby-cipher-00">draft-mcgrew-standby-cipher-00</a>, January 2013.
1517
1518 [<a id="ref-Zhenqing2012">Zhenqing2012</a>]
1519 Zhenqing, S., Bin, Z., Dengguo, F., and W. Wenling,
1520 "Improved Key Recovery Attacks on Reduced-Round Salsa20
1521 and ChaCha*", 2012.
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564 <span class="grey">Nir &amp; Langley Informational [Page 28]</span></pre>
1565 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-29" ></span>
1566 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1567
1568
1569 <span class="h2"><a class="selflink" id="appendix-A" href="#appendix-A">Appendix A</a>. Additional Test Vectors</span>
1570
1571 The subsections of this appendix contain more test vectors for the
1572 algorithms in the sub-sections of <a href="#section-2">Section 2</a>.
1573
1574 <span class="h3"><a class="selflink" id="appendix-A.1" href="#appendix-A.1">A.1</a>. The ChaCha20 Block Functions</span>
1575
1576 Test Vector #1:
1577 ==============
1578
1579 Key:
1580 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1581 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1582
1583 Nonce:
1584 000 00 00 00 00 00 00 00 00 00 00 00 00 ............
1585
1586 Block Counter = 0
1587
1588 ChaCha state at the end
1589 ade0b876 903df1a0 e56a5d40 28bd8653
1590 b819d2bd 1aed8da0 ccef36a8 c70d778b
1591 7c5941da 8d485751 3fe02477 374ad8b8
1592 f4b8436a 1ca11815 69b687c3 8665eeb2
1593
1594 Keystream:
1595 000 76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 v.....=.@]j.S..(
1596 016 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 .........6...w..
1597 <a href="#section-032">032</a> da 41 59 7c 51 57 48 8d 77 24 e0 3f b8 d8 4a 37 .AY|QWH.w$.?..J7
1598 048 6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86 jC.........i..e.
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620 <span class="grey">Nir &amp; Langley Informational [Page 29]</span></pre>
1621 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-30" ></span>
1622 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1623
1624
1625 Test Vector #2:
1626 ==============
1627
1628 Key:
1629 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1630 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1631
1632 Nonce:
1633 000 00 00 00 00 00 00 00 00 00 00 00 00 ............
1634
1635 Block Counter = 1
1636
1637 ChaCha state at the end
1638 bee7079f 7a385155 7c97ba98 0d082d73
1639 a0290fcb 6965e348 3e53c612 ed7aee32
1640 7621b729 434ee69c b03371d5 d539d874
1641 281fed31 45fb0a51 1f0ae1ac 6f4d794b
1642
1643 Keystream:
1644 000 9f 07 e7 be 55 51 38 7a 98 ba 97 7c 73 2d 08 0d ....UQ8z...|s-..
1645 016 cb 0f 29 a0 48 e3 65 69 12 c6 53 3e 32 ee 7a ed ..).H.ei..S&gt;2.z.
1646 032 29 b7 21 76 9c e6 4e 43 d5 71 33 b0 74 d8 39 d5 ).!v..NC.q3.t.9.
1647 048 31 ed 1f 28 51 0a fb 45 ac e1 0a 1f 4b 79 4d 6f 1..(Q..E....KyMo
1648
1649 Test Vector #3:
1650 ==============
1651
1652 Key:
1653 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1654 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................
1655
1656 Nonce:
1657 000 00 00 00 00 00 00 00 00 00 00 00 00 ............
1658
1659 Block Counter = 1
1660
1661 ChaCha state at the end
1662 2452eb3a 9249f8ec 8d829d9b ddd4ceb1
1663 e8252083 60818b01 f38422b8 5aaa49c9
1664 bb00ca8e da3ba7b4 c4b592d1 fdf2732f
1665 4436274e 2561b3c8 ebdd4aa6 a0136c00
1666
1667 Keystream:
1668 000 3a eb 52 24 ec f8 49 92 9b 9d 82 8d b1 ce d4 dd :.R$..I.........
1669 016 83 20 25 e8 01 8b 81 60 b8 22 84 f3 c9 49 aa 5a . %....`."...I.Z
1670 032 8e ca 00 bb b4 a7 3b da d1 92 b5 c4 2f 73 f2 fd ......;...../s..
1671 048 4e 27 36 44 c8 b3 61 25 a6 4a dd eb 00 6c 13 a0 N'6D..a%.J...l..
1672
1673
1674
1675
1676 <span class="grey">Nir &amp; Langley Informational [Page 30]</span></pre>
1677 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-31" ></span>
1678 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1679
1680
1681 Test Vector #4:
1682 ==============
1683
1684 Key:
1685 000 00 ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1686 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1687
1688 Nonce:
1689 000 00 00 00 00 00 00 00 00 00 00 00 00 ............
1690
1691 Block Counter = 2
1692
1693 ChaCha state at the end
1694 fb4dd572 4bc42ef1 df922636 327f1394
1695 a78dea8f 5e269039 a1bebbc1 caf09aae
1696 a25ab213 48a6b46c 1b9d9bcb 092c5be6
1697 546ca624 1bec45d5 87f47473 96f0992e
1698
1699 Keystream:
1700 <a href="#section-000">000</a> 72 d5 4d fb f1 2e c4 4b 36 26 92 df 94 13 7f 32 r.M....K6&amp;.....<a href="#page-2">2</a>
1701 016 8f ea 8d a7 39 90 26 5e c1 bb be a1 ae 9a f0 ca ....9.&amp;^........
1702 032 13 b2 5a a2 6c b4 a6 48 cb 9b 9d 1b e6 5b 2c 09 ..Z.l..H.....[,.
1703 048 24 a6 6c 54 d5 45 ec 1b 73 74 f4 87 2e 99 f0 96 $.lT.E..st......
1704
1705 Test Vector #5:
1706 ==============
1707
1708 Key:
1709 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1710 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1711
1712 Nonce:
1713 000 00 00 00 00 00 00 00 00 00 00 00 02 ............
1714
1715 Block Counter = 0
1716
1717 ChaCha state at the end
1718 374dc6c2 3736d58c b904e24a cd3f93ef
1719 88228b1a 96a4dfb3 5b76ab72 c727ee54
1720 0e0e978a f3145c95 1b748ea8 f786c297
1721 99c28f5f 628314e8 398a19fa 6ded1b53
1722
1723 Keystream:
1724 000 c2 c6 4d 37 8c d5 36 37 4a e2 04 b9 ef 93 3f cd ..M7..67J.....?.
1725 016 1a 8b 22 88 b3 df a4 96 72 ab 76 5b 54 ee 27 c7 ..".....r.v[T.'.
1726 032 8a 97 0e 0e 95 5c 14 f3 a8 8e 74 1b 97 c2 86 f7 .....\....t.....
1727 048 5f 8f c2 99 e8 14 83 62 fa 19 8a 39 53 1b ed 6d _......b...9S..m
1728
1729
1730
1731
1732 <span class="grey">Nir &amp; Langley Informational [Page 31]</span></pre>
1733 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-32" ></span>
1734 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1735
1736
1737 <span class="h3"><a class="selflink" id="appendix-A.2" href="#appendix-A.2">A.2</a>. ChaCha20 Encryption</span>
1738
1739 Test Vector #1:
1740 ==============
1741
1742 Key:
1743 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1744 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1745
1746 Nonce:
1747 000 00 00 00 00 00 00 00 00 00 00 00 00 ............
1748
1749 Initial Block Counter = 0
1750
1751 Plaintext:
1752 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1753 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1754 032 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1755 048 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1756
1757 Ciphertext:
1758 000 76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 v.....=.@]j.S..(
1759 016 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 .........6...w..
1760 <a href="#section-032">032</a> da 41 59 7c 51 57 48 8d 77 24 e0 3f b8 d8 4a 37 .AY|QWH.w$.?..J7
1761 048 6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86 jC.........i..e.
1762
1763 Test Vector #2:
1764 ==============
1765
1766 Key:
1767 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1768 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................
1769
1770 Nonce:
1771 000 00 00 00 00 00 00 00 00 00 00 00 02 ............
1772
1773 Initial Block Counter = 1
1774
1775 Plaintext:
1776 000 41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 Any submission t
1777 016 6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e o the IETF inten
1778 032 64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 ded by the Contr
1779 048 69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 ibutor for publi
1780 064 63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 cation as all or
1781 080 20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 part of an IETF
1782 096 20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 Internet-Draft
1783 112 6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 or RFC and any s
1784 128 74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 tatement made wi
1785
1786
1787
1788 <span class="grey">Nir &amp; Langley Informational [Page 32]</span></pre>
1789 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-33" ></span>
1790 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1791
1792
1793 144 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 thin the context
1794 160 20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 of an IETF acti
1795 176 76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 vity is consider
1796 192 65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 ed an "IETF Cont
1797 208 72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 ribution". Such
1798 224 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 statements inclu
1799 240 64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e de oral statemen
1800 256 74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 ts in IETF sessi
1801 272 6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 ons, as well as
1802 288 77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 written and elec
1803 304 74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 tronic communica
1804 320 74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e tions made at an
1805 336 79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c y time or place,
1806 352 20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 which are addre
1807 368 73 73 65 64 20 74 6f ssed to
1808
1809 Ciphertext:
1810 000 a3 fb f0 7d f3 fa 2f de 4f 37 6c a2 3e 82 73 70 ...}../.O7l.&gt;.sp
1811 016 41 60 5d 9f 4f 4f 57 bd 8c ff 2c 1d 4b 79 55 ec A`].OOW...,.KyU.
1812 032 2a 97 94 8b d3 72 29 15 c8 f3 d3 37 f7 d3 70 05 *....r)....7..p.
1813 048 0e 9e 96 d6 47 b7 c3 9f 56 e0 31 ca 5e b6 25 0d ....G...V.1.^.%.
1814 064 40 42 e0 27 85 ec ec fa 4b 4b b5 e8 ea d0 44 0e @B.'....KK....D.
1815 080 20 b6 e8 db 09 d8 81 a7 c6 13 2f 42 0e 52 79 50 ........./B.RyP
1816 096 42 bd fa 77 73 d8 a9 05 14 47 b3 29 1c e1 41 1c B..ws....G.)..A.
1817 112 68 04 65 55 2a a6 c4 05 b7 76 4d 5e 87 be a8 5a h.eU*....vM^...Z
1818 128 d0 0f 84 49 ed 8f 72 d0 d6 62 ab 05 26 91 ca 66 ...I..r..b..&amp;..f
1819 144 42 4b c8 6d 2d f8 0e a4 1f 43 ab f9 37 d3 25 9d BK.m-....C..7.%.
1820 160 c4 b2 d0 df b4 8a 6c 91 39 dd d7 f7 69 66 e9 28 ......l.9...if.(
1821 176 e6 35 55 3b a7 6c 5c 87 9d 7b 35 d4 9e b2 e6 2b .5U;.l\..{5....+
1822 192 08 71 cd ac 63 89 39 e2 5e 8a 1e 0e f9 d5 28 0f .q..c.9.^.....(.
1823 208 a8 ca 32 8b 35 1c 3c 76 59 89 cb cf 3d aa 8b 6c ..2.5.&lt;vY...=..l
1824 224 cc 3a af 9f 39 79 c9 2b 37 20 fc 88 dc 95 ed 84 .:..9y.+7 ......
1825 240 a1 be 05 9c 64 99 b9 fd a2 36 e7 e8 18 b0 4b 0b ....d....6....K.
1826 256 c3 9c 1e 87 6b 19 3b fe 55 69 75 3f 88 12 8c c0 ....k.;.Uiu?....
1827 272 8a aa 9b 63 d1 a1 6f 80 ef 25 54 d7 18 9c 41 1f ...c..o..%T...A.
1828 288 58 69 ca 52 c5 b8 3f a3 6f f2 16 b9 c1 d3 00 62 Xi.R..?.o......b
1829 304 be bc fd 2d c5 bc e0 91 19 34 fd a7 9a 86 f6 e6 ...-.....4......
1830 320 98 ce d7 59 c3 ff 9b 64 77 33 8f 3d a4 f9 cd 85 ...Y...dw3.=....
1831 336 14 ea 99 82 cc af b3 41 b2 38 4d d9 02 f3 d1 ab .......A.8M.....
1832 352 7a c6 1d d2 9c 6f 21 ba 5b 86 2f 37 30 e3 7c fd z....o!.[./70.|.
1833 368 c4 fd 80 6c 22 f2 21 ...l".!
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844 <span class="grey">Nir &amp; Langley Informational [Page 33]</span></pre>
1845 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-34" ></span>
1846 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1847
1848
1849 Test Vector #3:
1850 ==============
1851
1852 Key:
1853 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 [email protected]......
1854 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu.
1855
1856 Nonce:
1857 000 00 00 00 00 00 00 00 00 00 00 00 02 ............
1858
1859 Initial Block Counter = 42
1860
1861 Plaintext:
1862 000 27 54 77 61 73 20 62 72 69 6c 6c 69 67 2c 20 61 'Twas brillig, a
1863 016 6e 64 20 74 68 65 20 73 6c 69 74 68 79 20 74 6f nd the slithy to
1864 032 76 65 73 0a 44 69 64 20 67 79 72 65 20 61 6e 64 ves.Did gyre and
1865 048 20 67 69 6d 62 6c 65 20 69 6e 20 74 68 65 20 77 gimble in the w
1866 064 61 62 65 3a 0a 41 6c 6c 20 6d 69 6d 73 79 20 77 abe:.All mimsy w
1867 080 65 72 65 20 74 68 65 20 62 6f 72 6f 67 6f 76 65 ere the borogove
1868 096 73 2c 0a 41 6e 64 20 74 68 65 20 6d 6f 6d 65 20 s,.And the mome
1869 112 72 61 74 68 73 20 6f 75 74 67 72 61 62 65 2e raths outgrabe.
1870
1871 Ciphertext:
1872 000 62 e6 34 7f 95 ed 87 a4 5f fa e7 42 6f 27 a1 df b.4....._..Bo'..
1873 016 5f b6 91 10 04 4c 0d 73 11 8e ff a9 5b 01 e5 cf _....L.s....[...
1874 032 16 6d 3d f2 d7 21 ca f9 b2 1e 5f b1 4c 61 68 71 .m=..!...._.Lahq
1875 048 fd 84 c5 4f 9d 65 b2 83 19 6c 7f e4 f6 05 53 eb ...O.e...l....S.
1876 064 f3 9c 64 02 c4 22 34 e3 2a 35 6b 3e 76 43 12 a6 ..d.."4.*5k&gt;vC..
1877 080 1a 55 32 05 57 16 ea d6 96 25 68 f8 7d 3f 3f 77 .U2.W....%h.}??w
1878 096 04 c6 a8 d1 bc d1 bf 4d 50 d6 15 4b 6d a7 31 b1 .......MP..Km.1.
1879 112 87 b5 8d fd 72 8a fa 36 75 7a 79 7a c1 88 d1 ....r..6uzyz...
1880
1881 <span class="h3"><a class="selflink" id="appendix-A.3" href="#appendix-A.3">A.3</a>. Poly1305 Message Authentication Code</span>
1882
1883 Notice how, in test vector #2, r is equal to zero. The part of the
1884 Poly1305 algorithm where the accumulator is multiplied by r means
1885 that with r equal zero, the tag will be equal to s regardless of the
1886 content of the text. Fortunately, all the proposed methods of
1887 generating r are such that getting this particular weak key is very
1888 unlikely.
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900 <span class="grey">Nir &amp; Langley Informational [Page 34]</span></pre>
1901 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-35" ></span>
1902 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1903
1904
1905 Test Vector #1:
1906 ==============
1907
1908 One-time Poly1305 Key:
1909 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1910 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1911
1912 Text to MAC:
1913 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1914 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1915 032 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1916 048 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1917
1918 Tag:
1919 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956 <span class="grey">Nir &amp; Langley Informational [Page 35]</span></pre>
1957 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-36" ></span>
1958 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
1959
1960
1961 Test Vector #2:
1962 ==============
1963
1964 One-time Poly1305 Key:
1965 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
1966 016 36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e 6.....`p...."z.&gt;
1967
1968 Text to MAC:
1969 000 41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 Any submission t
1970 016 6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e o the IETF inten
1971 032 64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 ded by the Contr
1972 048 69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 ibutor for publi
1973 064 63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 cation as all or
1974 080 20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 part of an IETF
1975 096 20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 Internet-Draft
1976 112 6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 or RFC and any s
1977 128 74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 tatement made wi
1978 144 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 thin the context
1979 160 20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 of an IETF acti
1980 176 76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 vity is consider
1981 192 65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 ed an "IETF Cont
1982 208 72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 ribution". Such
1983 224 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 statements inclu
1984 240 64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e de oral statemen
1985 256 74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 ts in IETF sessi
1986 272 6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 ons, as well as
1987 288 77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 written and elec
1988 304 74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 tronic communica
1989 320 74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e tions made at an
1990 336 79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c y time or place,
1991 352 20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 which are addre
1992 368 73 73 65 64 20 74 6f ssed to
1993
1994 Tag:
1995 000 36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e 6.....`p...."z.&gt;
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012 <span class="grey">Nir &amp; Langley Informational [Page 36]</span></pre>
2013 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-37" ></span>
2014 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2015
2016
2017 Test Vector #3:
2018 ==============
2019
2020 One-time Poly1305 Key:
2021 000 36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e 6.....`p...."z.&gt;
2022 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
2023
2024 Text to MAC:
2025 000 41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 Any submission t
2026 016 6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e o the IETF inten
2027 032 64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 ded by the Contr
2028 048 69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 ibutor for publi
2029 064 63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 cation as all or
2030 080 20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 part of an IETF
2031 096 20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 Internet-Draft
2032 112 6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 or RFC and any s
2033 128 74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 tatement made wi
2034 144 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 thin the context
2035 160 20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 of an IETF acti
2036 176 76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 vity is consider
2037 192 65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 ed an "IETF Cont
2038 208 72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 ribution". Such
2039 224 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 statements inclu
2040 240 64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e de oral statemen
2041 256 74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 ts in IETF sessi
2042 272 6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 ons, as well as
2043 288 77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 written and elec
2044 304 74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 tronic communica
2045 320 74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e tions made at an
2046 336 79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c y time or place,
2047 352 20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 which are addre
2048 368 73 73 65 64 20 74 6f ssed to
2049
2050 Tag:
2051 000 f3 47 7e 7c d9 54 17 af 89 a6 b8 79 4c 31 0c f0 .G~|.T.....yL1..
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068 <span class="grey">Nir &amp; Langley Informational [Page 37]</span></pre>
2069 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-38" ></span>
2070 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2071
2072
2073 Test Vector #4:
2074 ==============
2075
2076 One-time Poly1305 Key:
2077 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 [email protected]......
2078 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu.
2079
2080 Text to MAC:
2081 000 27 54 77 61 73 20 62 72 69 6c 6c 69 67 2c 20 61 'Twas brillig, a
2082 016 6e 64 20 74 68 65 20 73 6c 69 74 68 79 20 74 6f nd the slithy to
2083 032 76 65 73 0a 44 69 64 20 67 79 72 65 20 61 6e 64 ves.Did gyre and
2084 048 20 67 69 6d 62 6c 65 20 69 6e 20 74 68 65 20 77 gimble in the w
2085 064 61 62 65 3a 0a 41 6c 6c 20 6d 69 6d 73 79 20 77 abe:.All mimsy w
2086 080 65 72 65 20 74 68 65 20 62 6f 72 6f 67 6f 76 65 ere the borogove
2087 096 73 2c 0a 41 6e 64 20 74 68 65 20 6d 6f 6d 65 20 s,.And the mome
2088 112 72 61 74 68 73 20 6f 75 74 67 72 61 62 65 2e raths outgrabe.
2089
2090 Tag:
2091 000 45 41 66 9a 7e aa ee 61 e7 08 dc 7c bc c5 eb 62 EAf.~..a...|...b
2092
2093 Test Vector #5: If one uses 130-bit partial reduction, does the code
2094 handle the case where partially reduced final result is not fully
2095 reduced?
2096
2097 R:
2098 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2099 S:
2100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2101 data:
2102 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2103 tag:
2104 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2105
2106 Test Vector #6: What happens if addition of s overflows modulo 2^128?
2107
2108 R:
2109 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2110 S:
2111 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2112 data:
2113 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2114 tag:
2115 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2116
2117
2118
2119
2120
2121
2122
2123
2124 <span class="grey">Nir &amp; Langley Informational [Page 38]</span></pre>
2125 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-39" ></span>
2126 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2127
2128
2129 Test Vector #7: What happens if data limb is all ones and there is
2130 carry from lower limb?
2131
2132 R:
2133 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2134 S:
2135 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2136 data:
2137 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2138 F0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2139 11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2140 tag:
2141 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2142
2143 Test Vector #8: What happens if final result from polynomial part is
2144 exactly 2^130-5?
2145
2146 R:
2147 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2148 S:
2149 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2150 data:
2151 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2152 FB FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE
2153 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
2154 tag:
2155 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2156
2157 Test Vector #9: What happens if final result from polynomial part is
2158 exactly 2^130-6?
2159
2160 R:
2161 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2162 S:
2163 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2164 data:
2165 FD FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2166 tag:
2167 FA FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180 <span class="grey">Nir &amp; Langley Informational [Page 39]</span></pre>
2181 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-40" ></span>
2182 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2183
2184
2185 Test Vector #10: What happens if 5*H+L-type reduction produces
2186 131-bit intermediate result?
2187
2188 R:
2189 01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00
2190 S:
2191 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2192 data:
2193 E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00
2194 33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00
2195 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2196 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2197 tag:
2198 14 00 00 00 00 00 00 00 55 00 00 00 00 00 00 00
2199
2200 Test Vector #11: What happens if 5*H+L-type reduction produces
2201 131-bit final result?
2202
2203 R:
2204 01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00
2205 S:
2206 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2207 data:
2208 E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00
2209 33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00
2210 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2211 tag:
2212 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2213
2214 <span class="h3"><a class="selflink" id="appendix-A.4" href="#appendix-A.4">A.4</a>. Poly1305 Key Generation Using ChaCha20</span>
2215
2216 Test Vector #1:
2217 ==============
2218
2219 The key:
2220 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
2221 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
2222
2223 The nonce:
2224 000 00 00 00 00 00 00 00 00 00 00 00 00 ............
2225
2226 Poly1305 one-time key:
2227 000 76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 v.....=.@]j.S..(
2228 016 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 .........6...w..
2229
2230
2231
2232
2233
2234
2235
2236 <span class="grey">Nir &amp; Langley Informational [Page 40]</span></pre>
2237 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-41" ></span>
2238 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2239
2240
2241 Test Vector #2:
2242 ==============
2243
2244 The key:
2245 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
2246 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................
2247
2248 The nonce:
2249 000 00 00 00 00 00 00 00 00 00 00 00 02 ............
2250
2251 Poly1305 one-time key:
2252 000 ec fa 25 4f 84 5f 64 74 73 d3 cb 14 0d a9 e8 76 ..%O._dts......<a href="#page-v">v</a>
2253 <a href="#section-016">016</a> 06 cb 33 06 6c 44 7b 87 bc 26 66 dd e3 fb b7 39 ..3.lD{..&amp;f....<a href="#page-9">9</a>
2254
2255 Test Vector #3:
2256 ==============
2257
2258 The key:
2259 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 [email protected]......
2260 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu.
2261
2262 The nonce:
2263 000 00 00 00 00 00 00 00 00 00 00 00 02 ............
2264
2265 Poly1305 one-time key:
2266 000 96 5e 3b c6 f9 ec 7e d9 56 08 08 f4 d2 29 f9 4b .^;...~.V....).K
2267 016 13 7f f2 75 ca 9b 3f cb dd 59 de aa d2 33 10 ae ...u..?..Y...3..
2268
2269 <span class="h3"><a class="selflink" id="appendix-A.5" href="#appendix-A.5">A.5</a>. ChaCha20-Poly1305 AEAD Decryption</span>
2270
2271 Below we see decrypting a message. We receive a ciphertext, a nonce,
2272 and a tag. We know the key. We will check the tag and then
2273 (assuming that it validates) decrypt the ciphertext. In this
2274 particular protocol, we'll assume that there is no padding of the
2275 plaintext.
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292 <span class="grey">Nir &amp; Langley Informational [Page 41]</span></pre>
2293 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-42" ></span>
2294 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2295
2296
2297 The key:
2298 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 [email protected]......
2299 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu.
2300
2301 Ciphertext:
2302 000 64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd d...u...`.b...C.
2303 016 5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2 ^.\.4\....g..l..
2304 032 4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0 Ll..u]C....N8-&amp;.
2305 048 bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf ...&lt;2.....;.5X..
2306 064 33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81 3/..q......J....
2307 080 14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55 ...n..3.`....7.U
2308 <a href="#section-096">096</a> 97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38 ...n...a..2N+5.8
2309 112 36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4 6..{j|.....{S.g.
2310 128 b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9 ..lv{.MF..R.....
2311 144 90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e [email protected]"^.....lR&gt;
2312 160 af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a .E4..?..[.Gq..Tj
2313 176 0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a ..+..VN..B"s.H'.
2314 192 0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e ..1`S.v..U..1YCN
2315 208 ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10 ..NFm.Z.s.rv'.z.
2316 <a href="#section-224">224</a> 49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30 I....6...h..w.q0
2317 240 30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29 0[.......{qMlo,)
2318 256 a6 ad 5c b4 02 2b 02 70 9b ..\..+.p.
2319
2320 The nonce:
2321 000 00 00 00 00 01 02 03 04 05 06 07 08 ............
2322
2323 The AAD:
2324 000 f3 33 88 86 00 00 00 00 00 00 4e 91 .3........N.
2325
2326 Received Tag:
2327 <a href="#section-000">000</a> ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38 ...g..."9#6....<a href="#page-8">8</a>
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348 <span class="grey">Nir &amp; Langley Informational [Page 42]</span></pre>
2349 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-43" ></span>
2350 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2351
2352
2353 First, we calculate the one-time Poly1305 key
2354
2355 @@@ ChaCha state with key setup
2356 61707865 3320646e 79622d32 6b206574
2357 a540921c 8ad355eb 868833f3 f0b5f604
2358 c1173947 09802b40 bc5cca9d c0757020
2359 00000000 00000000 04030201 08070605
2360
2361 @@@ ChaCha state after 20 rounds
2362 a94af0bd 89dee45c b64bb195 afec8fa1
2363 508f4726 63f554c0 1ea2c0db aa721526
2364 11b1e514 a0bacc0f 828a6015 d7825481
2365 e8a4a850 d9dcbbd6 4c2de33a f8ccd912
2366
2367 @@@ out bytes:
2368 bd:f0:4a:a9:5c:e4:de:89:95:b1:4b:b6:a1:8f:ec:af:
2369 26:47:8f:50:c0:54:f5:63:db:c0:a2:1e:26:15:72:aa
2370
2371 Poly1305 one-time key:
2372 000 bd f0 4a a9 5c e4 de 89 95 b1 4b b6 a1 8f ec af ..J.\.....K.....
2373 016 26 47 8f 50 c0 54 f5 63 db c0 a2 1e 26 15 72 aa &amp;G.P.T.c....&amp;.r.
2374
2375 Next, we construct the AEAD buffer
2376
2377 Poly1305 Input:
2378 000 f3 33 88 86 00 00 00 00 00 00 4e 91 00 00 00 00 .3........N.....
2379 016 64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd d...u...`.b...C.
2380 032 5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2 ^.\.4\....g..l..
2381 048 4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0 Ll..u]C....N8-&amp;.
2382 064 bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf ...&lt;2.....;.5X..
2383 080 33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81 3/..q......J....
2384 096 14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55 ...n..3.`....7.U
2385 <a href="#section-112">112</a> 97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38 ...n...a..2N+5.8
2386 128 36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4 6..{j|.....{S.g.
2387 144 b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9 ..lv{.MF..R.....
2388 160 90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e [email protected]"^.....lR&gt;
2389 176 af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a .E4..?..[.Gq..Tj
2390 192 0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a ..+..VN..B"s.H'.
2391 208 0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e ..1`S.v..U..1YCN
2392 224 ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10 ..NFm.Z.s.rv'.z.
2393 <a href="#section-240">240</a> 49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30 I....6...h..w.q0
2394 256 30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29 0[.......{qMlo,)
2395 272 a6 ad 5c b4 02 2b 02 70 9b 00 00 00 00 00 00 00 ..\..+.p........
2396 288 0c 00 00 00 00 00 00 00 09 01 00 00 00 00 00 00 ................
2397
2398
2399
2400
2401
2402
2403
2404 <span class="grey">Nir &amp; Langley Informational [Page 43]</span></pre>
2405 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-44" ></span>
2406 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2407
2408
2409 We calculate the Poly1305 tag and find that it matches
2410
2411 Calculated Tag:
2412 <a href="#section-000">000</a> ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38 ...g..."9#6....<a href="#page-8">8</a>
2413
2414 Finally, we decrypt the ciphertext
2415
2416 Plaintext::
2417 000 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 73 20 Internet-Drafts
2418 016 61 72 65 20 64 72 61 66 74 20 64 6f 63 75 6d 65 are draft docume
2419 032 6e 74 73 20 76 61 6c 69 64 20 66 6f 72 20 61 20 nts valid for a
2420 048 6d 61 78 69 6d 75 6d 20 6f 66 20 73 69 78 20 6d maximum of six m
2421 064 6f 6e 74 68 73 20 61 6e 64 20 6d 61 79 20 62 65 onths and may be
2422 080 20 75 70 64 61 74 65 64 2c 20 72 65 70 6c 61 63 updated, replac
2423 096 65 64 2c 20 6f 72 20 6f 62 73 6f 6c 65 74 65 64 ed, or obsoleted
2424 112 20 62 79 20 6f 74 68 65 72 20 64 6f 63 75 6d 65 by other docume
2425 128 6e 74 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e nts at any time.
2426 144 20 49 74 20 69 73 20 69 6e 61 70 70 72 6f 70 72 It is inappropr
2427 160 69 61 74 65 20 74 6f 20 75 73 65 20 49 6e 74 65 iate to use Inte
2428 176 72 6e 65 74 2d 44 72 61 66 74 73 20 61 73 20 72 rnet-Drafts as r
2429 192 65 66 65 72 65 6e 63 65 20 6d 61 74 65 72 69 61 eference materia
2430 208 6c 20 6f 72 20 74 6f 20 63 69 74 65 20 74 68 65 l or to cite the
2431 224 6d 20 6f 74 68 65 72 20 74 68 61 6e 20 61 73 20 m other than as
2432 240 2f e2 80 9c 77 6f 72 6b 20 69 6e 20 70 72 6f 67 /...work in prog
2433 256 72 65 73 73 2e 2f e2 80 9d ress./...
2434
2435 <span class="h2"><a class="selflink" id="appendix-B" href="#appendix-B">Appendix B</a>. Performance Measurements of ChaCha20</span>
2436
2437 The following measurements were made by Adam Langley for a blog post
2438 published on February 27th, 2014. The original blog post was
2439 available at the time of this writing at
2440 &lt;<a href="https://www.imperialviolet.org/2014/02/27/tlssymmetriccrypto.html">https://www.imperialviolet.org/2014/02/27/tlssymmetriccrypto.html</a>&gt;.
2441
2442 +----------------------------+-------------+-------------------+
2443 | Chip | AES-128-GCM | ChaCha20-Poly1305 |
2444 +----------------------------+-------------+-------------------+
2445 | OMAP 4460 | 24.1 MB/s | 75.3 MB/s |
2446 | Snapdragon S4 Pro | 41.5 MB/s | 130.9 MB/s |
2447 | Sandy Bridge Xeon (AES-NI) | 900 MB/s | 500 MB/s |
2448 +----------------------------+-------------+-------------------+
2449
2450 Table 1: Speed Comparison
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460 <span class="grey">Nir &amp; Langley Informational [Page 44]</span></pre>
2461 <hr class='noprint'/><!--NewPage--><pre class='newpage'><span id="page-45" ></span>
2462 <span class="grey"><a href="./rfc7539">RFC 7539</a> ChaCha20 &amp; Poly1305 May 2015</span>
2463
2464
2465 Acknowledgements
2466
2467 ChaCha20 and Poly1305 were invented by Daniel J. Bernstein. The AEAD
2468 construction and the method of creating the one-time Poly1305 key
2469 were invented by Adam Langley.
2470
2471 Thanks to Robert Ransom, Watson Ladd, Stefan Buhler, Dan Harkins, and
2472 Kenny Paterson for their helpful comments and explanations. Thanks
2473 to Niels Moller for suggesting the more efficient AEAD construction
2474 in this document. Special thanks to Ilari Liusvaara for providing
2475 extra test vectors, helpful comments, and for being the first to
2476 attempt an implementation from this document. Thanks to Sean
2477 Parkinson for suggesting improvements to the examples and the
2478 pseudocode. Thanks to David Ireland for pointing out a bug in the
2479 pseudocode, and to Stephen Farrell and Alyssa Rowan for pointing out
2480 missing advise in the security considerations.
2481
2482 Special thanks goes to Gordon Procter for performing a security
2483 analysis of the composition and publishing [<a href="#ref-Procter" title="&quot;A Security Analysis of the Composition of ChaCha20 and Poly1305&quot;">Procter</a>].
2484
2485 Authors' Addresses
2486
2487 Yoav Nir
2488 Check Point Software Technologies, Ltd.
2489 5 Hasolelim St.
2490 Tel Aviv 6789735
2491 Israel
2492
2493 EMail: [email protected]
2494
2495
2496 Adam Langley
2497 Google, Inc.
2498
2499 EMail: [email protected]
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516 Nir &amp; Langley Informational [Page 45]
2517 </pre>