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