idnits 2.17.1 draft-nir-cfrg-rfc7539bis-04.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- -- The draft header indicates that this document obsoletes RFC7539, but the abstract doesn't seem to directly say this. It does mention RFC7539 though, so this could be OK. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year == Line 265 has weird spacing: '...db886dc c9a62...' == Line 321 has weird spacing: '...ccccccc ccccc...' == Line 322 has weird spacing: '...kkkkkkk kkkkk...' == Line 323 has weird spacing: '...kkkkkkk kkkkk...' == Line 324 has weird spacing: '...bbbbbbb nnnnn...' == (9 more instances...) == Using lowercase 'not' together with uppercase 'MUST', 'SHALL', 'SHOULD', or 'RECOMMENDED' is not an accepted usage according to RFC 2119. Please use uppercase 'NOT' together with RFC 2119 keywords (if that is what you mean). Found 'MUST not' in this paragraph: o Words 13-15 are a nonce, which MUST not be repeated for the same key. The 13th word is the first 32 bits of the input nonce taken as a little-endian integer, while the 15th word is the last 32 bits. -- The document date (March 29, 2018) is 2214 days in the past. Is this intentional? -- Found something which looks like a code comment -- if you have code sections in the document, please surround them with '' and '' lines. Checking references for intended status: Informational ---------------------------------------------------------------------------- -- Looks like a reference, but probably isn't: '3' on line 614 -- Looks like a reference, but probably isn't: '7' on line 615 -- Looks like a reference, but probably isn't: '11' on line 616 -- Looks like a reference, but probably isn't: '15' on line 617 -- Looks like a reference, but probably isn't: '4' on line 618 -- Looks like a reference, but probably isn't: '8' on line 619 -- Looks like a reference, but probably isn't: '12' on line 620 -- Looks like a reference, but probably isn't: '16' on line 612 == Missing Reference: '0x01' is mentioned on line 679, but not defined -- Obsolete informational reference (is this intentional?): RFC 7539 (Obsoleted by RFC 8439) Summary: 0 errors (**), 0 flaws (~~), 9 warnings (==), 12 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 Crypto Forum Y. Nir 3 Internet-Draft Dell EMC 4 Obsoletes: 7539 (if approved) A. Langley 5 Intended status: Informational Google, Inc. 6 Expires: September 30, 2018 March 29, 2018 8 ChaCha20 and Poly1305 for IETF Protocols 9 draft-nir-cfrg-rfc7539bis-04 11 Abstract 13 This document defines the ChaCha20 stream cipher as well as the use 14 of the Poly1305 authenticator, both as stand-alone algorithms and as 15 a "combined mode", or Authenticated Encryption with Associated Data 16 (AEAD) algorithm. 18 RFC 7539, the predecessor of this document, was meant to serve as a 19 stable reference and an implementation guide. It was a product of 20 the Crypto Forum Research Group (CFRG). This document merges the 21 errata filed against RFC 7539 and adds a little text to the Security 22 Considerations section. 24 Status of This Memo 26 This Internet-Draft is submitted in full conformance with the 27 provisions of BCP 78 and BCP 79. 29 Internet-Drafts are working documents of the Internet Engineering 30 Task Force (IETF). Note that other groups may also distribute 31 working documents as Internet-Drafts. The list of current Internet- 32 Drafts is at https://datatracker.ietf.org/drafts/current/. 34 Internet-Drafts are draft documents valid for a maximum of six months 35 and may be updated, replaced, or obsoleted by other documents at any 36 time. It is inappropriate to use Internet-Drafts as reference 37 material or to cite them other than as "work in progress." 39 This Internet-Draft will expire on September 30, 2018. 41 Copyright Notice 43 Copyright (c) 2018 IETF Trust and the persons identified as the 44 document authors. All rights reserved. 46 This document is subject to BCP 78 and the IETF Trust's Legal 47 Provisions Relating to IETF Documents 48 (https://trustee.ietf.org/license-info) in effect on the date of 49 publication of this document. Please review these documents 50 carefully, as they describe your rights and restrictions with respect 51 to this document. Code Components extracted from this document must 52 include Simplified BSD License text as described in Section 4.e of 53 the Trust Legal Provisions and are provided without warranty as 54 described in the Simplified BSD License. 56 Table of Contents 58 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3 59 1.1. Conventions Used in This Document . . . . . . . . . . . . 4 60 2. The Algorithms . . . . . . . . . . . . . . . . . . . . . . . 4 61 2.1. The ChaCha Quarter Round . . . . . . . . . . . . . . . . 4 62 2.1.1. Test Vector for the ChaCha Quarter Round . . . . . . 5 63 2.2. A Quarter Round on the ChaCha State . . . . . . . . . . . 5 64 2.2.1. Test Vector for the Quarter Round on the ChaCha State 6 65 2.3. The ChaCha20 Block Function . . . . . . . . . . . . . . . 6 66 2.3.1. The ChaCha20 Block Function in Pseudocode . . . . . . 8 67 2.3.2. Test Vector for the ChaCha20 Block Function . . . . . 8 68 2.4. The ChaCha20 Encryption Algorithm . . . . . . . . . . . . 10 69 2.4.1. The ChaCha20 Encryption Algorithm in Pseudocode . . . 10 70 2.4.2. Example and Test Vector for the ChaCha20 Cipher . . . 11 71 2.5. The Poly1305 Algorithm . . . . . . . . . . . . . . . . . 13 72 2.5.1. The Poly1305 Algorithms in Pseudocode . . . . . . . . 15 73 2.5.2. Poly1305 Example and Test Vector . . . . . . . . . . 15 74 2.6. Generating the Poly1305 Key Using ChaCha20 . . . . . . . 17 75 2.6.1. Poly1305 Key Generation in Pseudocode . . . . . . . . 18 76 2.6.2. Poly1305 Key Generation Test Vector . . . . . . . . . 18 77 2.7. A Pseudorandom Function for Crypto Suites based on 78 ChaCha/Poly1305 . . . . . . . . . . . . . . . . . . . . . 18 79 2.8. AEAD Construction . . . . . . . . . . . . . . . . . . . . 19 80 2.8.1. Pseudocode for the AEAD Construction . . . . . . . . 21 81 2.8.2. Example and Test Vector for AEAD_CHACHA20_POLY1305 . 22 82 3. Implementation Advice . . . . . . . . . . . . . . . . . . . . 24 83 4. Security Considerations . . . . . . . . . . . . . . . . . . . 24 84 5. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 26 85 6. References . . . . . . . . . . . . . . . . . . . . . . . . . 26 86 6.1. Normative References . . . . . . . . . . . . . . . . . . 26 87 6.2. Informative References . . . . . . . . . . . . . . . . . 26 88 Appendix A. Additional Test Vectors . . . . . . . . . . . . . . 29 89 A.1. The ChaCha20 Block Functions . . . . . . . . . . . . . . 29 90 A.2. ChaCha20 Encryption . . . . . . . . . . . . . . . . . . . 32 91 A.3. Poly1305 Message Authentication Code . . . . . . . . . . 34 92 A.4. Poly1305 Key Generation Using ChaCha20 . . . . . . . . . 40 93 A.5. ChaCha20-Poly1305 AEAD Decryption . . . . . . . . . . . . 41 94 Appendix B. Performance Measurements of ChaCha20 . . . . . . . . 44 95 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . 44 96 Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 45 98 1. Introduction 100 The Advanced Encryption Standard (AES -- [FIPS-197]) has become the 101 gold standard in encryption. Its efficient design, widespread 102 implementation, and hardware support allow for high performance in 103 many areas. On most modern platforms, AES is anywhere from four to 104 ten times as fast as the previous most-used cipher, Triple Data 105 Encryption Standard (3DES -- [SP800-67]), which makes it not only the 106 best choice, but the only practical choice. 108 There are several problems with this. If future advances in 109 cryptanalysis reveal a weakness in AES, users will be in an 110 unenviable position. With the only other widely supported cipher 111 being the much slower 3DES, it is not feasible to reconfigure 112 deployments to use 3DES. [Standby-Cipher] describes this issue and 113 the need for a standby cipher in greater detail. Another problem is 114 that while AES is very fast on dedicated hardware, its performance on 115 platforms that lack such hardware is considerably lower. Yet another 116 problem is that many AES implementations are vulnerable to cache- 117 collision timing attacks ([Cache-Collisions]). 119 This document provides a definition and implementation guide for 120 three algorithms: 122 1. The ChaCha20 cipher. This is a high-speed cipher first described 123 in [ChaCha]. It is considerably faster than AES in software-only 124 implementations, making it around three times as fast on 125 platforms that lack specialized AES hardware. See Appendix B for 126 some hard numbers. ChaCha20 is also not sensitive to timing 127 attacks (see the security considerations in Section 4). This 128 algorithm is described in Section 2.4 130 2. The Poly1305 authenticator. This is a high-speed message 131 authentication code. Implementation is also straightforward and 132 easy to get right. The algorithm is described in Section 2.5. 134 3. The CHACHA20-POLY1305 Authenticated Encryption with Associated 135 Data (AEAD) construction, described in Section 2.8. 137 This document and its predecesor do not introduce these new 138 algorithms for the first time. They have been defined in scientific 139 papers by D. J. Bernstein. [ChaCha][Poly1305] The purpose of this 140 document is to serve as a stable reference for IETF documents making 141 use of these algorithms. 143 These algorithms have undergone rigorous analysis. Several papers 144 discuss the security of Salsa and ChaCha ([LatinDances], 145 [LatinDances2], [Zhenqing2012]). 147 This document represents the consensus of the Crypto Forum Research 148 Group (CFRG). It replaces [RFC7539]. 150 1.1. Conventions Used in This Document 152 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 153 "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and 154 "OPTIONAL" in this document are to be interpreted as described in BCP 155 14 [RFC2119] [RFC8174] when, and only when, they appear in all 156 capitals, as shown here. 158 The description of the ChaCha algorithm will at various time refer to 159 the ChaCha state as a "vector" or as a "matrix". This follows the 160 use of these terms in [ChaCha]. The matrix notation is more visually 161 convenient and gives a better notion as to why some rounds are called 162 "column rounds" while others are called "diagonal rounds". Here's a 163 diagram of how the matrices relate to vectors (using the C language 164 convention of zero being the index origin). 166 0 1 2 3 167 4 5 6 7 168 8 9 10 11 169 12 13 14 15 171 The elements in this vector or matrix are 32-bit unsigned integers. 173 The algorithm name is "ChaCha". "ChaCha20" is a specific instance 174 where 20 "rounds" (or 80 quarter rounds -- see Section 2.1) are used. 175 Other variations are defined, with 8 or 12 rounds, but in this 176 document we only describe the 20-round ChaCha, so the names "ChaCha" 177 and "ChaCha20" will be used interchangeably. 179 2. The Algorithms 181 The subsections below describe the algorithms used and the AEAD 182 construction. 184 2.1. The ChaCha Quarter Round 186 The basic operation of the ChaCha algorithm is the quarter round. It 187 operates on four 32-bit unsigned integers, denoted a, b, c, and d. 188 The operation is as follows (in C-like notation): 190 a += b; d ^= a; d <<<= 16; 191 c += d; b ^= c; b <<<= 12; 192 a += b; d ^= a; d <<<= 8; 193 c += d; b ^= c; b <<<= 7; 195 Where "+" denotes integer addition modulo 2^32, "^" denotes a bitwise 196 Exclusive OR (XOR), and "<<< n" denotes an n-bit left roll (towards 197 the high bits). 199 For example, let's see the add, XOR, and roll operations from the 200 fourth line with sample numbers: 202 a = 0x11111111 203 b = 0x01020304 204 c = 0x77777777 205 d = 0x01234567 206 c = c + d = 0x77777777 + 0x01234567 = 0x789abcde 207 b = b ^ c = 0x01020304 ^ 0x789abcde = 0x7998bfda 208 b = b <<< 7 = 0x7998bfda <<< 7 = 0xcc5fed3c 210 2.1.1. Test Vector for the ChaCha Quarter Round 212 For a test vector, we will use the same numbers as in the example, 213 adding something random for c. 215 a = 0x11111111 216 b = 0x01020304 217 c = 0x9b8d6f43 218 d = 0x01234567 220 After running a Quarter Round on these four numbers, we get these: 222 a = 0xea2a92f4 223 b = 0xcb1cf8ce 224 c = 0x4581472e 225 d = 0x5881c4bb 227 2.2. A Quarter Round on the ChaCha State 229 The ChaCha state does not have four integer numbers: it has 16. So 230 the quarter-round operation works on only four of them -- hence the 231 name. Each quarter round operates on four predetermined numbers in 232 the ChaCha state. We will denote by QUARTERROUND(x, y, z, w) a 233 quarter-round operation on the numbers at indices x, y, z, and w of 234 the ChaCha state when viewed as a vector. For example, if we apply 235 QUARTERROUND(1, 5, 9, 13) to a state, this means running the quarter- 236 round operation on the elements marked with an asterisk, while 237 leaving the others alone: 239 0 *a 2 3 240 4 *b 6 7 241 8 *c 10 11 242 12 *d 14 15 244 Note that this run of quarter round is part of what is called a 245 "column round". 247 2.2.1. Test Vector for the Quarter Round on the ChaCha State 249 For a test vector, we will use a ChaCha state that was generated 250 randomly: 252 Sample ChaCha State 254 879531e0 c5ecf37d 516461b1 c9a62f8a 255 44c20ef3 3390af7f d9fc690b 2a5f714c 256 53372767 b00a5631 974c541a 359e9963 257 5c971061 3d631689 2098d9d6 91dbd320 259 We will apply the QUARTERROUND(2, 7, 8, 13) operation to this state. 260 For obvious reasons, this one is part of what is called a "diagonal 261 round": 263 After applying QUARTERROUND(2, 7, 8, 13) 265 879531e0 c5ecf37d *bdb886dc c9a62f8a 266 44c20ef3 3390af7f d9fc690b *cfacafd2 267 *e46bea80 b00a5631 974c541a 359e9963 268 5c971061 *ccc07c79 2098d9d6 91dbd320 270 Note that only the numbers in positions 2, 7, 8, and 13 changed. 272 2.3. The ChaCha20 Block Function 274 The ChaCha block function transforms a ChaCha state by running 275 multiple quarter rounds. 277 The inputs to ChaCha20 are: 279 o A 256-bit key, treated as a concatenation of eight 32-bit little- 280 endian integers. 282 o A 96-bit nonce, treated as a concatenation of three 32-bit little- 283 endian integers. 285 o A 32-bit block count parameter, treated as a 32-bit little-endian 286 integer. 288 The output is 64 random-looking bytes. 290 The ChaCha algorithm described here uses a 256-bit key. The original 291 algorithm also specified 128-bit keys and 8- and 12-round variants, 292 but these are out of scope for this document. In this section, we 293 describe the ChaCha block function. 295 Note also that the original ChaCha had a 64-bit nonce and 64-bit 296 block count. We have modified this here to be more consistent with 297 recommendations in Section 3.2 of [RFC5116]. This limits the use of 298 a single (key,nonce) combination to 2^32 blocks, or 256 GB, but that 299 is enough for most uses. In cases where a single key is used by 300 multiple senders, it is important to make sure that they don't use 301 the same nonces. This can be assured by partitioning the nonce space 302 so that the first 32 bits are unique per sender, while the other 64 303 bits come from a counter. 305 The ChaCha20 state is initialized as follows: 307 o The first four words (0-3) are constants: 0x61707865, 0x3320646e, 308 0x79622d32, 0x6b206574. 310 o The next eight words (4-11) are taken from the 256-bit key by 311 reading the bytes in little-endian order, in 4-byte chunks. 313 o Word 12 is a block counter. Since each block is 64-byte, a 32-bit 314 word is enough for 256 gigabytes of data. 316 o Words 13-15 are a nonce, which MUST not be repeated for the same 317 key. The 13th word is the first 32 bits of the input nonce taken 318 as a little-endian integer, while the 15th word is the last 32 319 bits. 321 cccccccc cccccccc cccccccc cccccccc 322 kkkkkkkk kkkkkkkk kkkkkkkk kkkkkkkk 323 kkkkkkkk kkkkkkkk kkkkkkkk kkkkkkkk 324 bbbbbbbb nnnnnnnn nnnnnnnn nnnnnnnn 326 c=constant k=key b=blockcount n=nonce 328 ChaCha20 runs 20 rounds, alternating between "column rounds" and 329 "diagonal rounds". Each round consists of four quarter-rounds, and 330 they are run as follows. Quarter rounds 1-4 are part of a "column" 331 round, while 5-8 are part of a "diagonal" round: 333 QUARTERROUND(0, 4, 8, 12) 334 QUARTERROUND(1, 5, 9, 13) 335 QUARTERROUND(2, 6, 10, 14) 336 QUARTERROUND(3, 7, 11, 15) 337 QUARTERROUND(0, 5, 10, 15) 338 QUARTERROUND(1, 6, 11, 12) 339 QUARTERROUND(2, 7, 8, 13) 340 QUARTERROUND(3, 4, 9, 14) 342 At the end of 20 rounds (or 10 iterations of the above list), we add 343 the original input words to the output words, and serialize the 344 result by sequencing the words one-by-one in little-endian order. 346 Note: "addition" in the above paragraph is done modulo 2^32. In some 347 machine languages, this is called carryless addition on a 32-bit 348 word. 350 2.3.1. The ChaCha20 Block Function in Pseudocode 352 Note: This section and a few others contain pseudocode for the 353 algorithm explained in a previous section. Every effort was made for 354 the pseudocode to accurately reflect the algorithm as described in 355 the preceding section. If a conflict is still present, the textual 356 explanation and the test vectors are normative. 358 inner_block (state): 359 Qround(state, 0, 4, 8, 12) 360 Qround(state, 1, 5, 9, 13) 361 Qround(state, 2, 6, 10, 14) 362 Qround(state, 3, 7, 11, 15) 363 Qround(state, 0, 5, 10, 15) 364 Qround(state, 1, 6, 11, 12) 365 Qround(state, 2, 7, 8, 13) 366 Qround(state, 3, 4, 9, 14) 367 end 369 chacha20_block(key, counter, nonce): 370 state = constants | key | counter | nonce 371 initial_state = state 372 for i=1 upto 10 373 inner_block(state) 374 end 375 state += initial_state 376 return serialize(state) 377 end 379 Where the pipe character ("|") denotes concatenation. 381 2.3.2. Test Vector for the ChaCha20 Block Function 383 For a test vector, we will use the following inputs to the ChaCha20 384 block function: 386 o Key = 00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f:10:11:12:13: 387 14:15:16:17:18:19:1a:1b:1c:1d:1e:1f. The key is a sequence of 388 octets with no particular structure before we copy it into the 389 ChaCha state. 391 o Nonce = (00:00:00:09:00:00:00:4a:00:00:00:00) 393 o Block Count = 1. 395 After setting up the ChaCha state, it looks like this: 397 ChaCha state with the key setup. 399 61707865 3320646e 79622d32 6b206574 400 03020100 07060504 0b0a0908 0f0e0d0c 401 13121110 17161514 1b1a1918 1f1e1d1c 402 00000001 09000000 4a000000 00000000 404 After running 20 rounds (10 column rounds interleaved with 10 405 "diagonal rounds"), the ChaCha state looks like this: 407 ChaCha state after 20 rounds 409 837778ab e238d763 a67ae21e 5950bb2f 410 c4f2d0c7 fc62bb2f 8fa018fc 3f5ec7b7 411 335271c2 f29489f3 eabda8fc 82e46ebd 412 d19c12b4 b04e16de 9e83d0cb 4e3c50a2 414 Finally, we add the original state to the result (simple vector or 415 matrix addition), giving this: 417 ChaCha state at the end of the ChaCha20 operation 419 e4e7f110 15593bd1 1fdd0f50 c47120a3 420 c7f4d1c7 0368c033 9aaa2204 4e6cd4c3 421 466482d2 09aa9f07 05d7c214 a2028bd9 422 d19c12b5 b94e16de e883d0cb 4e3c50a2 424 After we serialize the state, we get this: 426 Serialized Block: 427 000 10 f1 e7 e4 d1 3b 59 15 50 0f dd 1f a3 20 71 c4 .....;Y.P.... q. 428 016 c7 d1 f4 c7 33 c0 68 03 04 22 aa 9a c3 d4 6c 4e ....3.h.."....lN 429 032 d2 82 64 46 07 9f aa 09 14 c2 d7 05 d9 8b 02 a2 ..dF............ 430 048 b5 12 9c d1 de 16 4e b9 cb d0 83 e8 a2 50 3c 4e ......N......P.S. 1030 Poly1305 r = 455e9a4057ab6080f47b42c052bac7b 1031 Poly1305 s = ff53d53e7875932aebd9751073d6e10a 1033 keystream bytes: 1034 9f:7b:e9:5d:01:fd:40:ba:15:e2:8f:fb:36:81:0a:ae: 1035 c1:c0:88:3f:09:01:6e:de:dd:8a:d0:87:55:82:03:a5: 1036 4e:9e:cb:38:ac:8e:5e:2b:b8:da:b2:0f:fa:db:52:e8: 1037 75:04:b2:6e:be:69:6d:4f:60:a4:85:cf:11:b8:1b:59: 1038 fc:b1:c4:5f:42:19:ee:ac:ec:6a:de:c3:4e:66:69:78: 1039 8e:db:41:c4:9c:a3:01:e1:27:e0:ac:ab:3b:44:b9:cf: 1040 5c:86:bb:95:e0:6b:0d:f2:90:1a:b6:45:e4:ab:e6:22: 1041 15:38 1043 Ciphertext: 1044 000 d3 1a 8d 34 64 8e 60 db 7b 86 af bc 53 ef 7e c2 ...4d.`.{...S.~. 1045 016 a4 ad ed 51 29 6e 08 fe a9 e2 b5 a7 36 ee 62 d6 ...Q)n......6.b. 1046 032 3d be a4 5e 8c a9 67 12 82 fa fb 69 da 92 72 8b =..^..g....i..r. 1047 048 1a 71 de 0a 9e 06 0b 29 05 d6 a5 b6 7e cd 3b 36 .q.....)....~.;6 1048 064 92 dd bd 7f 2d 77 8b 8c 98 03 ae e3 28 09 1b 58 ....-w......(..X 1049 080 fa b3 24 e4 fa d6 75 94 55 85 80 8b 48 31 d7 bc ..$...u.U...H1.. 1050 096 3f f4 de f0 8e 4b 7a 9d e5 76 d2 65 86 ce c6 4b ?....Kz..v.e...K 1051 112 61 16 a. 1053 AEAD Construction for Poly1305: 1054 000 50 51 52 53 c0 c1 c2 c3 c4 c5 c6 c7 00 00 00 00 PQRS............ 1055 016 d3 1a 8d 34 64 8e 60 db 7b 86 af bc 53 ef 7e c2 ...4d.`.{...S.~. 1056 032 a4 ad ed 51 29 6e 08 fe a9 e2 b5 a7 36 ee 62 d6 ...Q)n......6.b. 1057 048 3d be a4 5e 8c a9 67 12 82 fa fb 69 da 92 72 8b =..^..g....i..r. 1058 064 1a 71 de 0a 9e 06 0b 29 05 d6 a5 b6 7e cd 3b 36 .q.....)....~.;6 1059 080 92 dd bd 7f 2d 77 8b 8c 98 03 ae e3 28 09 1b 58 ....-w......(..X 1060 096 fa b3 24 e4 fa d6 75 94 55 85 80 8b 48 31 d7 bc ..$...u.U...H1.. 1061 112 3f f4 de f0 8e 4b 7a 9d e5 76 d2 65 86 ce c6 4b ?....Kz..v.e...K 1062 128 61 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 a............... 1063 144 0c 00 00 00 00 00 00 00 72 00 00 00 00 00 00 00 ........r....... 1065 Note the four zero bytes in line 000 and the 14 zero bytes in line 1066 128 1067 Tag: 1068 1a:e1:0b:59:4f:09:e2:6a:7e:90:2e:cb:d0:60:06:91 1070 3. Implementation Advice 1072 Each block of ChaCha20 involves 16 move operations and one increment 1073 operation for loading the state, 80 each of XOR, addition and roll 1074 operations for the rounds, 16 more add operations and 16 XOR 1075 operations for protecting the plaintext. Section 2.3 describes the 1076 ChaCha block function as "adding the original input words". This 1077 implies that before starting the rounds on the ChaCha state, we copy 1078 it aside, only to add it in later. This is correct, but we can save 1079 a few operations if we instead copy the state and do the work on the 1080 copy. This way, for the next block you don't need to recreate the 1081 state, but only to increment the block counter. This saves 1082 approximately 5.5% of the cycles. 1084 It is not recommended to use a generic big number library such as the 1085 one in OpenSSL for the arithmetic operations in Poly1305. Such 1086 libraries use dynamic allocation to be able to handle an integer of 1087 any size, but that flexibility comes at the expense of performance as 1088 well as side-channel security. More efficient implementations that 1089 run in constant time are available, one of them in D. J. Bernstein's 1090 own library, NaCl ([NaCl]). A constant-time but not optimal approach 1091 would be to naively implement the arithmetic operations for 288-bit 1092 integers, because even a naive implementation will not exceed 2^288 1093 in the multiplication of (acc+block) and r. An efficient constant- 1094 time implementation can be found in the public domain library 1095 poly1305-donna ([Poly1305_Donna]). 1097 4. Security Considerations 1099 The ChaCha20 cipher is designed to provide 256-bit security. 1101 The Poly1305 authenticator is designed to ensure that forged messages 1102 are rejected with a probability of 1-(n/(2^102)) for a 16n-byte 1103 message, even after sending 2^64 legitimate messages, so it is 1104 SUF-CMA (strong unforgeability against chosen-message attacks) in the 1105 terminology of [AE]. 1107 Proving the security of either of these is beyond the scope of this 1108 document. Such proofs are available in the referenced academic 1109 papers ([ChaCha], [Poly1305], [LatinDances], [LatinDances2], and 1110 [Zhenqing2012]). 1112 The most important security consideration in implementing this 1113 document is the uniqueness of the nonce used in ChaCha20. Counters 1114 and LFSRs are both acceptable ways of generating unique nonces, as is 1115 encrypting a counter using a block cipher with a 64-bit block size 1116 such as DES. Note that it is not acceptable to use a truncation of a 1117 counter encrypted with block ciphers with 128-bit or 256-bit blocks, 1118 because such a truncation may repeat after a short time. 1120 Consequences of repeating a nonce: If a nonce is repeated, then both 1121 the one-time Poly1305 key and the keystream are identical between the 1122 messages. This reveals the XOR of the plaintexts, because the XOR of 1123 the plaintexts is equal to the XOR of the ciphertexts. 1125 The Poly1305 key MUST be unpredictable to an attacker. Randomly 1126 generating the key would fulfill this requirement, except that 1127 Poly1305 is often used in communications protocols, so the receiver 1128 should know the key. Pseudorandom number generation such as by 1129 encrypting a counter is acceptable. Using ChaCha with a secret key 1130 and a nonce is also acceptable. 1132 The algorithms presented here were designed to be easy to implement 1133 in constant time to avoid side-channel vulnerabilities. The 1134 operations used in ChaCha20 are all additions, XORs, and fixed rolls. 1135 All of these can and should be implemented in constant time. Access 1136 to offsets into the ChaCha state and the number of operations do not 1137 depend on any property of the key, eliminating the chance of 1138 information about the key leaking through the timing of cache misses. 1140 For Poly1305, the operations are addition, multiplication. and 1141 modulus, all on numbers with greater than 128 bits. This can be done 1142 in constant time, but a naive implementation (such as using some 1143 generic big number library) will not be constant time. For example, 1144 if the multiplication is performed as a separate operation from the 1145 modulus, the result will sometimes be under 2^256 and sometimes be 1146 above 2^256. Implementers should be careful about timing side- 1147 channels for Poly1305 by using the appropriate implementation of 1148 these operations. 1150 Validating the authenticity of a message involves a bitwise 1151 comparison of the calculated tag with the received tag. In most use 1152 cases, nonces and AAD contents are not "used up" until a valid 1153 message is received. This allows an attacker to send multiple 1154 identical messages with different tags until one passes the tag 1155 comparison. This is hard if the attacker has to try all 2^128 1156 possible tags one by one. However, if the timing of the tag 1157 comparison operation reveals how long a prefix of the calculated and 1158 received tags is identical, the number of messages can be reduced 1159 significantly. For this reason, with online protocols, 1160 implementation MUST use a constant-time comparison function rather 1161 than relying on optimized but insecure library functions such as the 1162 C language's memcmp(). 1164 Additionally, any protocol using this algorithm MUST include the 1165 complete tag to minimize the opportunity for forgery. Tag truncation 1166 MUST NOT be done. 1168 5. IANA Considerations 1170 IANA has assigned an entry in the "Authenticated Encryption with 1171 Associated Data (AEAD) Parameters" registry with 29 as the Numeric 1172 ID, "AEAD_CHACHA20_POLY1305" as the name, and RFC 7539 as reference. 1174 IANA is requested to modify the registry by using this document as 1175 reference. 1177 6. References 1179 6.1. Normative References 1181 [ChaCha] Bernstein, D., "ChaCha, a variant of Salsa20", January 1182 2008, . 1184 [Poly1305] 1185 Bernstein, D., "The Poly1305-AES message-authentication 1186 code", March 2005, 1187 . 1189 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 1190 Requirement Levels", BCP 14, RFC 2119, 1191 DOI 10.17487/RFC2119, March 1997, 1192 . 1194 [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 1195 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, 1196 May 2017, . 1198 6.2. Informative References 1200 [AE] Bellare, M. and C. Namprempre, "Authenticated Encryption: 1201 Relations among notions and analysis of the generic 1202 composition paradigm", September 2008, 1203 . 1205 [Cache-Collisions] 1206 Bonneau, J. and I. Mironov, "Cache-Collision Timing 1207 Attacks Against AES", 2006, 1208 . 1210 [FIPS-197] 1211 National Institute of Standards and Technology, "Advanced 1212 Encryption Standard (AES)", FIPS PUB 197, November 2001, 1213 . 1216 [LatinDances] 1217 Aumasson, J., Fischer, S., Khazaei, S., Meier, W., and C. 1218 Rechberger, "New Features of Latin Dances: Analysis of 1219 Salsa, ChaCha, and Rumba", December 2007, 1220 . 1222 [LatinDances2] 1223 Ishiguro, T., Kiyomoto, S., and Y. Miyake, "Modified 1224 version of 'Latin Dances Revisited: New Analytic Results 1225 of Salsa20 and ChaCha'", February 2012, 1226 . 1228 [NaCl] Bernstein, D., Lange, T., and P. Schwabe, "NaCl: 1229 Networking and Cryptography library", July 2012, 1230 . 1232 [Poly1305_Donna] 1233 Floodyberry, A., "poly1305-donna", February 2014, 1234 . 1236 [Procter] Procter, G., "A Security Analysis of the Composition of 1237 ChaCha20 and Poly1305", August 2014, 1238 . 1240 [RFC5116] McGrew, D., "An Interface and Algorithms for Authenticated 1241 Encryption", RFC 5116, DOI 10.17487/RFC5116, January 2008, 1242 . 1244 [RFC7296] Kaufman, C., Hoffman, P., Nir, Y., Eronen, P., and T. 1245 Kivinen, "Internet Key Exchange Protocol Version 2 1246 (IKEv2)", STD 79, RFC 7296, DOI 10.17487/RFC7296, October 1247 2014, . 1249 [RFC7539] Nir, Y. and A. Langley, "ChaCha20 and Poly1305 for IETF 1250 Protocols", RFC 7539, DOI 10.17487/RFC7539, May 2015, 1251 . 1253 [SP800-67] 1254 National Institute of Standards and Technology, 1255 "Recommendation for the Triple Data Encryption Algorithm 1256 (TDEA) Block Cipher", NIST 800-67, January 2012, 1257 . 1260 [Standby-Cipher] 1261 McGrew, D., Grieco, A., and Y. Sheffer, "Selection of 1262 Future Cryptographic Standards", Work in Progress, draft- 1263 mcgrew-standby-cipher-00, January 2013. 1265 [Zhenqing2012] 1266 Zhenqing, S., Bin, Z., Dengguo, F., and W. Wenling, 1267 "Improved Key Recovery Attacks on Reduced-Round Salsa20 1268 and ChaCha*", 2012. 1270 Appendix A. Additional Test Vectors 1272 The subsections of this appendix contain more test vectors for the 1273 algorithms in the sub-sections of Section 2. 1275 A.1. The ChaCha20 Block Functions 1277 Test Vector #1: 1278 ============== 1280 Key: 1281 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1282 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1284 Nonce: 1285 000 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1287 Block Counter = 0 1289 ChaCha state at the end 1290 ade0b876 903df1a0 e56a5d40 28bd8653 1291 b819d2bd 1aed8da0 ccef36a8 c70d778b 1292 7c5941da 8d485751 3fe02477 374ad8b8 1293 f4b8436a 1ca11815 69b687c3 8665eeb2 1295 Keystream: 1296 000 76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 v.....=.@]j.S..( 1297 016 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 .........6...w.. 1298 032 da 41 59 7c 51 57 48 8d 77 24 e0 3f b8 d8 4a 37 .AY|QWH.w$.?..J7 1299 048 6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86 jC.........i..e. 1301 Test Vector #2: 1302 ============== 1304 Key: 1305 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1306 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1308 Nonce: 1309 000 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1311 Block Counter = 1 1313 ChaCha state at the end 1314 bee7079f 7a385155 7c97ba98 0d082d73 1315 a0290fcb 6965e348 3e53c612 ed7aee32 1316 7621b729 434ee69c b03371d5 d539d874 1317 281fed31 45fb0a51 1f0ae1ac 6f4d794b 1319 Keystream: 1320 000 9f 07 e7 be 55 51 38 7a 98 ba 97 7c 73 2d 08 0d ....UQ8z...|s-.. 1321 016 cb 0f 29 a0 48 e3 65 69 12 c6 53 3e 32 ee 7a ed ..).H.ei..S>2.z. 1322 032 29 b7 21 76 9c e6 4e 43 d5 71 33 b0 74 d8 39 d5 ).!v..NC.q3.t.9. 1323 048 31 ed 1f 28 51 0a fb 45 ac e1 0a 1f 4b 79 4d 6f 1..(Q..E....KyMo 1325 Test Vector #3: 1326 ============== 1328 Key: 1329 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1330 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ 1332 Nonce: 1333 000 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1335 Block Counter = 1 1337 ChaCha state at the end 1338 2452eb3a 9249f8ec 8d829d9b ddd4ceb1 1339 e8252083 60818b01 f38422b8 5aaa49c9 1340 bb00ca8e da3ba7b4 c4b592d1 fdf2732f 1341 4436274e 2561b3c8 ebdd4aa6 a0136c00 1343 Keystream: 1344 000 3a eb 52 24 ec f8 49 92 9b 9d 82 8d b1 ce d4 dd :.R$..I......... 1345 016 83 20 25 e8 01 8b 81 60 b8 22 84 f3 c9 49 aa 5a . %....`."...I.Z 1346 032 8e ca 00 bb b4 a7 3b da d1 92 b5 c4 2f 73 f2 fd ......;...../s.. 1347 048 4e 27 36 44 c8 b3 61 25 a6 4a dd eb 00 6c 13 a0 N'6D..a%.J...l.. 1349 Test Vector #4: 1350 ============== 1352 Key: 1353 000 00 ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1354 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1356 Nonce: 1357 000 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1359 Block Counter = 2 1361 ChaCha state at the end 1362 fb4dd572 4bc42ef1 df922636 327f1394 1363 a78dea8f 5e269039 a1bebbc1 caf09aae 1364 a25ab213 48a6b46c 1b9d9bcb 092c5be6 1365 546ca624 1bec45d5 87f47473 96f0992e 1367 Keystream: 1368 000 72 d5 4d fb f1 2e c4 4b 36 26 92 df 94 13 7f 32 r.M....K6&.....2 1369 016 8f ea 8d a7 39 90 26 5e c1 bb be a1 ae 9a f0 ca ....9.&^........ 1370 032 13 b2 5a a2 6c b4 a6 48 cb 9b 9d 1b e6 5b 2c 09 ..Z.l..H.....[,. 1371 048 24 a6 6c 54 d5 45 ec 1b 73 74 f4 87 2e 99 f0 96 $.lT.E..st...... 1373 Test Vector #5: 1374 ============== 1376 Key: 1377 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1378 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1380 Nonce: 1381 000 00 00 00 00 00 00 00 00 00 00 00 02 ............ 1383 Block Counter = 0 1385 ChaCha state at the end 1386 374dc6c2 3736d58c b904e24a cd3f93ef 1387 88228b1a 96a4dfb3 5b76ab72 c727ee54 1388 0e0e978a f3145c95 1b748ea8 f786c297 1389 99c28f5f 628314e8 398a19fa 6ded1b53 1391 Keystream: 1392 000 c2 c6 4d 37 8c d5 36 37 4a e2 04 b9 ef 93 3f cd ..M7..67J.....?. 1393 016 1a 8b 22 88 b3 df a4 96 72 ab 76 5b 54 ee 27 c7 ..".....r.v[T.'. 1394 032 8a 97 0e 0e 95 5c 14 f3 a8 8e 74 1b 97 c2 86 f7 .....\....t..... 1395 048 5f 8f c2 99 e8 14 83 62 fa 19 8a 39 53 1b ed 6d _......b...9S..m 1397 A.2. ChaCha20 Encryption 1399 Test Vector #1: 1400 ============== 1402 Key: 1403 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1404 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1406 Nonce: 1407 000 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1409 Initial Block Counter = 0 1411 Plaintext: 1412 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1413 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1414 032 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1415 048 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1417 Ciphertext: 1418 000 76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 v.....=.@]j.S..( 1419 016 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 .........6...w.. 1420 032 da 41 59 7c 51 57 48 8d 77 24 e0 3f b8 d8 4a 37 .AY|QWH.w$.?..J7 1421 048 6a 43 b8 f4 15 18 a1 1c c3 87 b6 69 b2 ee 65 86 jC.........i..e. 1423 Test Vector #2: 1424 ============== 1426 Key: 1427 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1428 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ 1430 Nonce: 1431 000 00 00 00 00 00 00 00 00 00 00 00 02 ............ 1433 Initial Block Counter = 1 1435 Plaintext: 1436 000 41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 Any submission t 1437 016 6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e o the IETF inten 1438 032 64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 ded by the Contr 1439 048 69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 ibutor for publi 1440 064 63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 cation as all or 1441 080 20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 part of an IETF 1442 096 20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 Internet-Draft 1443 112 6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 or RFC and any s 1444 128 74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 tatement made wi 1445 144 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 thin the context 1446 160 20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 of an IETF acti 1447 176 76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 vity is consider 1448 192 65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 ed an "IETF Cont 1449 208 72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 ribution". Such 1450 224 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 statements inclu 1451 240 64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e de oral statemen 1452 256 74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 ts in IETF sessi 1453 272 6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 ons, as well as 1454 288 77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 written and elec 1455 304 74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 tronic communica 1456 320 74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e tions made at an 1457 336 79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c y time or place, 1458 352 20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 which are addre 1459 368 73 73 65 64 20 74 6f ssed to 1461 Ciphertext: 1462 000 a3 fb f0 7d f3 fa 2f de 4f 37 6c a2 3e 82 73 70 ...}../.O7l.>.sp 1463 016 41 60 5d 9f 4f 4f 57 bd 8c ff 2c 1d 4b 79 55 ec A`].OOW...,.KyU. 1464 032 2a 97 94 8b d3 72 29 15 c8 f3 d3 37 f7 d3 70 05 *....r)....7..p. 1465 048 0e 9e 96 d6 47 b7 c3 9f 56 e0 31 ca 5e b6 25 0d ....G...V.1.^.%. 1466 064 40 42 e0 27 85 ec ec fa 4b 4b b5 e8 ea d0 44 0e @B.'....KK....D. 1467 080 20 b6 e8 db 09 d8 81 a7 c6 13 2f 42 0e 52 79 50 ........./B.RyP 1468 096 42 bd fa 77 73 d8 a9 05 14 47 b3 29 1c e1 41 1c B..ws....G.)..A. 1469 112 68 04 65 55 2a a6 c4 05 b7 76 4d 5e 87 be a8 5a h.eU*....vM^...Z 1470 128 d0 0f 84 49 ed 8f 72 d0 d6 62 ab 05 26 91 ca 66 ...I..r..b..&..f 1471 144 42 4b c8 6d 2d f8 0e a4 1f 43 ab f9 37 d3 25 9d BK.m-....C..7.%. 1472 160 c4 b2 d0 df b4 8a 6c 91 39 dd d7 f7 69 66 e9 28 ......l.9...if.( 1473 176 e6 35 55 3b a7 6c 5c 87 9d 7b 35 d4 9e b2 e6 2b .5U;.l\..{5....+ 1474 192 08 71 cd ac 63 89 39 e2 5e 8a 1e 0e f9 d5 28 0f .q..c.9.^.....(. 1475 208 a8 ca 32 8b 35 1c 3c 76 59 89 cb cf 3d aa 8b 6c ..2.5.vC.. 1514 080 1a 55 32 05 57 16 ea d6 96 25 68 f8 7d 3f 3f 77 .U2.W....%h.}??w 1515 096 04 c6 a8 d1 bc d1 bf 4d 50 d6 15 4b 6d a7 31 b1 .......MP..Km.1. 1516 112 87 b5 8d fd 72 8a fa 36 75 7a 79 7a c1 88 d1 ....r..6uzyz... 1518 A.3. Poly1305 Message Authentication Code 1520 Notice how, in test vector #2, r is equal to zero. The part of the 1521 Poly1305 algorithm where the accumulator is multiplied by r means 1522 that with r equal zero, the tag will be equal to s regardless of the 1523 content of the text. Fortunately, all the proposed methods of 1524 generating r are such that getting this particular weak key is very 1525 unlikely. 1527 Test Vector #1: 1528 ============== 1530 One-time Poly1305 Key: 1531 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1532 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1534 Text to MAC: 1535 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1536 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1537 032 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1538 048 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1540 Tag: 1541 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1543 Test Vector #2: 1544 ============== 1546 One-time Poly1305 Key: 1547 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1548 016 36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e 6.....`p...."z.> 1550 Text to MAC: 1551 000 41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 Any submission t 1552 016 6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e o the IETF inten 1553 032 64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 ded by the Contr 1554 048 69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 ibutor for publi 1555 064 63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 cation as all or 1556 080 20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 part of an IETF 1557 096 20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 Internet-Draft 1558 112 6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 or RFC and any s 1559 128 74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 tatement made wi 1560 144 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 thin the context 1561 160 20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 of an IETF acti 1562 176 76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 vity is consider 1563 192 65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 ed an "IETF Cont 1564 208 72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 ribution". Such 1565 224 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 statements inclu 1566 240 64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e de oral statemen 1567 256 74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 ts in IETF sessi 1568 272 6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 ons, as well as 1569 288 77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 written and elec 1570 304 74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 tronic communica 1571 320 74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e tions made at an 1572 336 79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c y time or place, 1573 352 20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 which are addre 1574 368 73 73 65 64 20 74 6f ssed to 1576 Tag: 1577 000 36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e 6.....`p...."z.> 1578 Test Vector #3: 1579 ============== 1581 One-time Poly1305 Key: 1582 000 36 e5 f6 b5 c5 e0 60 70 f0 ef ca 96 22 7a 86 3e 6.....`p...."z.> 1583 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1585 Text to MAC: 1586 000 41 6e 79 20 73 75 62 6d 69 73 73 69 6f 6e 20 74 Any submission t 1587 016 6f 20 74 68 65 20 49 45 54 46 20 69 6e 74 65 6e o the IETF inten 1588 032 64 65 64 20 62 79 20 74 68 65 20 43 6f 6e 74 72 ded by the Contr 1589 048 69 62 75 74 6f 72 20 66 6f 72 20 70 75 62 6c 69 ibutor for publi 1590 064 63 61 74 69 6f 6e 20 61 73 20 61 6c 6c 20 6f 72 cation as all or 1591 080 20 70 61 72 74 20 6f 66 20 61 6e 20 49 45 54 46 part of an IETF 1592 096 20 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 20 Internet-Draft 1593 112 6f 72 20 52 46 43 20 61 6e 64 20 61 6e 79 20 73 or RFC and any s 1594 128 74 61 74 65 6d 65 6e 74 20 6d 61 64 65 20 77 69 tatement made wi 1595 144 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 thin the context 1596 160 20 6f 66 20 61 6e 20 49 45 54 46 20 61 63 74 69 of an IETF acti 1597 176 76 69 74 79 20 69 73 20 63 6f 6e 73 69 64 65 72 vity is consider 1598 192 65 64 20 61 6e 20 22 49 45 54 46 20 43 6f 6e 74 ed an "IETF Cont 1599 208 72 69 62 75 74 69 6f 6e 22 2e 20 53 75 63 68 20 ribution". Such 1600 224 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 63 6c 75 statements inclu 1601 240 64 65 20 6f 72 61 6c 20 73 74 61 74 65 6d 65 6e de oral statemen 1602 256 74 73 20 69 6e 20 49 45 54 46 20 73 65 73 73 69 ts in IETF sessi 1603 272 6f 6e 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 ons, as well as 1604 288 77 72 69 74 74 65 6e 20 61 6e 64 20 65 6c 65 63 written and elec 1605 304 74 72 6f 6e 69 63 20 63 6f 6d 6d 75 6e 69 63 61 tronic communica 1606 320 74 69 6f 6e 73 20 6d 61 64 65 20 61 74 20 61 6e tions made at an 1607 336 79 20 74 69 6d 65 20 6f 72 20 70 6c 61 63 65 2c y time or place, 1608 352 20 77 68 69 63 68 20 61 72 65 20 61 64 64 72 65 which are addre 1609 368 73 73 65 64 20 74 6f ssed to 1611 Tag: 1612 000 f3 47 7e 7c d9 54 17 af 89 a6 b8 79 4c 31 0c f0 .G~|.T.....yL1.. 1614 Test Vector #4: 1615 ============== 1617 One-time Poly1305 Key: 1618 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 ..@..U...3...... 1619 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu. 1621 Text to MAC: 1622 000 27 54 77 61 73 20 62 72 69 6c 6c 69 67 2c 20 61 'Twas brillig, a 1623 016 6e 64 20 74 68 65 20 73 6c 69 74 68 79 20 74 6f nd the slithy to 1624 032 76 65 73 0a 44 69 64 20 67 79 72 65 20 61 6e 64 ves.Did gyre and 1625 048 20 67 69 6d 62 6c 65 20 69 6e 20 74 68 65 20 77 gimble in the w 1626 064 61 62 65 3a 0a 41 6c 6c 20 6d 69 6d 73 79 20 77 abe:.All mimsy w 1627 080 65 72 65 20 74 68 65 20 62 6f 72 6f 67 6f 76 65 ere the borogove 1628 096 73 2c 0a 41 6e 64 20 74 68 65 20 6d 6f 6d 65 20 s,.And the mome 1629 112 72 61 74 68 73 20 6f 75 74 67 72 61 62 65 2e raths outgrabe. 1631 Tag: 1632 000 45 41 66 9a 7e aa ee 61 e7 08 dc 7c bc c5 eb 62 EAf.~..a...|...b 1634 Test Vector #5: If one uses 130-bit partial reduction, does the code 1635 handle the case where partially reduced final result is not fully 1636 reduced? 1638 R: 1639 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1640 S: 1641 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1642 data: 1643 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1644 tag: 1645 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1647 Test Vector #6: What happens if addition of s overflows modulo 2^128? 1649 R: 1650 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1651 S: 1652 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1653 data: 1654 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1655 tag: 1656 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1657 Test Vector #7: What happens if data limb is all ones and there is 1658 carry from lower limb? 1660 R: 1661 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1662 S: 1663 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1664 data: 1665 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1666 F0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1667 11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1668 tag: 1669 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1671 Test Vector #8: What happens if final result from polynomial part is 1672 exactly 2^130-5? 1674 R: 1675 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1676 S: 1677 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1678 data: 1679 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1680 FB FE FE FE FE FE FE FE FE FE FE FE FE FE FE FE 1681 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 1682 tag: 1683 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1685 Test Vector #9: What happens if final result from polynomial part is 1686 exactly 2^130-6? 1688 R: 1689 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1690 S: 1691 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1692 data: 1693 FD FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1694 tag: 1695 FA FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 1696 Test Vector #10: What happens if 5*H+L-type reduction produces 1697 131-bit intermediate result? 1699 R: 1700 01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 1701 S: 1702 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1703 data: 1704 E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00 1705 33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00 1706 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1707 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1708 tag: 1709 14 00 00 00 00 00 00 00 55 00 00 00 00 00 00 00 1711 Test Vector #11: What happens if 5*H+L-type reduction produces 1712 131-bit final result? 1714 R: 1715 01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 1716 S: 1717 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1718 data: 1719 E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00 1720 33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00 1721 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1722 tag: 1723 13 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1725 A.4. Poly1305 Key Generation Using ChaCha20 1727 Test Vector #1: 1728 ============== 1730 The ChaCha20 Key: 1731 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1732 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1734 The nonce: 1735 000 00 00 00 00 00 00 00 00 00 00 00 00 ............ 1737 Poly1305 one-time key: 1738 000 76 b8 e0 ad a0 f1 3d 90 40 5d 6a e5 53 86 bd 28 v.....=.@]j.S..( 1739 016 bd d2 19 b8 a0 8d ed 1a a8 36 ef cc 8b 77 0d c7 .........6...w.. 1741 Test Vector #2: 1742 ============== 1744 The ChaCha20 Key 1745 000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 1746 016 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ 1748 The nonce: 1749 000 00 00 00 00 00 00 00 00 00 00 00 02 ............ 1751 Poly1305 one-time key: 1752 000 ec fa 25 4f 84 5f 64 74 73 d3 cb 14 0d a9 e8 76 ..%O._dts......v 1753 016 06 cb 33 06 6c 44 7b 87 bc 26 66 dd e3 fb b7 39 ..3.lD{..&f....9 1755 Test Vector #3: 1756 ============== 1758 The ChaCha20 Key 1759 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 ..@..U...3...... 1760 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu. 1762 The nonce: 1763 000 00 00 00 00 00 00 00 00 00 00 00 02 ............ 1765 Poly1305 one-time key: 1766 000 96 5e 3b c6 f9 ec 7e d9 56 08 08 f4 d2 29 f9 4b .^;...~.V....).K 1767 016 13 7f f2 75 ca 9b 3f cb dd 59 de aa d2 33 10 ae ...u..?..Y...3.. 1769 A.5. ChaCha20-Poly1305 AEAD Decryption 1771 Below we see decrypting a message. We receive a ciphertext, a nonce, 1772 and a tag. We know the key. We will check the tag and then 1773 (assuming that it validates) decrypt the ciphertext. In this 1774 particular protocol, we'll assume that there is no padding of the 1775 plaintext. 1777 The ChaCha20 Key 1778 000 1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0 ..@..U...3...... 1779 016 47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0 G9..@+....\. pu. 1781 Ciphertext: 1782 000 64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd d...u...`.b...C. 1783 016 5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2 ^.\.4\....g..l.. 1784 032 4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0 Ll..u]C....N8-&. 1785 048 bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf ...<2.....;.5X.. 1786 064 33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81 3/..q......J.... 1787 080 14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55 ...n..3.`....7.U 1788 096 97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38 ...n...a..2N+5.8 1789 112 36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4 6..{j|.....{S.g. 1790 128 b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9 ..lv{.MF..R..... 1791 144 90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e .@...3"^.....lR> 1792 160 af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a .E4..?..[.Gq..Tj 1793 176 0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a ..+..VN..B"s.H'. 1794 192 0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e ..1`S.v..U..1YCN 1795 208 ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10 ..NFm.Z.s.rv'.z. 1796 224 49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30 I....6...h..w.q0 1797 240 30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29 0[.......{qMlo,) 1798 256 a6 ad 5c b4 02 2b 02 70 9b ..\..+.p. 1800 The nonce: 1801 000 00 00 00 00 01 02 03 04 05 06 07 08 ............ 1803 The AAD: 1804 000 f3 33 88 86 00 00 00 00 00 00 4e 91 .3........N. 1806 Received Tag: 1807 000 ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38 ...g..."9#6....8 1808 First, we calculate the one-time Poly1305 key 1810 ChaCha state with key setup 1811 61707865 3320646e 79622d32 6b206574 1812 a540921c 8ad355eb 868833f3 f0b5f604 1813 c1173947 09802b40 bc5cca9d c0757020 1814 00000000 00000000 04030201 08070605 1816 ChaCha state after 20 rounds 1817 a94af0bd 89dee45c b64bb195 afec8fa1 1818 508f4726 63f554c0 1ea2c0db aa721526 1819 11b1e514 a0bacc0f 828a6015 d7825481 1820 e8a4a850 d9dcbbd6 4c2de33a f8ccd912 1822 out bytes: 1823 bd:f0:4a:a9:5c:e4:de:89:95:b1:4b:b6:a1:8f:ec:af: 1824 26:47:8f:50:c0:54:f5:63:db:c0:a2:1e:26:15:72:aa 1826 Poly1305 one-time key: 1827 000 bd f0 4a a9 5c e4 de 89 95 b1 4b b6 a1 8f ec af ..J.\.....K..... 1828 016 26 47 8f 50 c0 54 f5 63 db c0 a2 1e 26 15 72 aa &G.P.T.c....&.r. 1830 Next, we construct the AEAD buffer 1832 Poly1305 Input: 1833 000 f3 33 88 86 00 00 00 00 00 00 4e 91 00 00 00 00 .3........N..... 1834 016 64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd d...u...`.b...C. 1835 032 5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2 ^.\.4\....g..l.. 1836 048 4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0 Ll..u]C....N8-&. 1837 064 bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf ...<2.....;.5X.. 1838 080 33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81 3/..q......J.... 1839 096 14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55 ...n..3.`....7.U 1840 112 97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38 ...n...a..2N+5.8 1841 128 36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4 6..{j|.....{S.g. 1842 144 b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9 ..lv{.MF..R..... 1843 160 90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e .@...3"^.....lR> 1844 176 af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a .E4..?..[.Gq..Tj 1845 192 0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a ..+..VN..B"s.H'. 1846 208 0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e ..1`S.v..U..1YCN 1847 224 ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10 ..NFm.Z.s.rv'.z. 1848 240 49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30 I....6...h..w.q0 1849 256 30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29 0[.......{qMlo,) 1850 272 a6 ad 5c b4 02 2b 02 70 9b 00 00 00 00 00 00 00 ..\..+.p........ 1851 288 0c 00 00 00 00 00 00 00 09 01 00 00 00 00 00 00 ................ 1853 We calculate the Poly1305 tag and find that it matches 1855 Calculated Tag: 1856 000 ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38 ...g..."9#6....8 1858 Finally, we decrypt the ciphertext 1860 Plaintext:: 1861 000 49 6e 74 65 72 6e 65 74 2d 44 72 61 66 74 73 20 Internet-Drafts 1862 016 61 72 65 20 64 72 61 66 74 20 64 6f 63 75 6d 65 are draft docume 1863 032 6e 74 73 20 76 61 6c 69 64 20 66 6f 72 20 61 20 nts valid for a 1864 048 6d 61 78 69 6d 75 6d 20 6f 66 20 73 69 78 20 6d maximum of six m 1865 064 6f 6e 74 68 73 20 61 6e 64 20 6d 61 79 20 62 65 onths and may be 1866 080 20 75 70 64 61 74 65 64 2c 20 72 65 70 6c 61 63 updated, replac 1867 096 65 64 2c 20 6f 72 20 6f 62 73 6f 6c 65 74 65 64 ed, or obsoleted 1868 112 20 62 79 20 6f 74 68 65 72 20 64 6f 63 75 6d 65 by other docume 1869 128 6e 74 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e nts at any time. 1870 144 20 49 74 20 69 73 20 69 6e 61 70 70 72 6f 70 72 It is inappropr 1871 160 69 61 74 65 20 74 6f 20 75 73 65 20 49 6e 74 65 iate to use Inte 1872 176 72 6e 65 74 2d 44 72 61 66 74 73 20 61 73 20 72 rnet-Drafts as r 1873 192 65 66 65 72 65 6e 63 65 20 6d 61 74 65 72 69 61 eference materia 1874 208 6c 20 6f 72 20 74 6f 20 63 69 74 65 20 74 68 65 l or to cite the 1875 224 6d 20 6f 74 68 65 72 20 74 68 61 6e 20 61 73 20 m other than as 1876 240 2f e2 80 9c 77 6f 72 6b 20 69 6e 20 70 72 6f 67 /...work in prog 1877 256 72 65 73 73 2e 2f e2 80 9d ress./... 1879 Appendix B. Performance Measurements of ChaCha20 1881 The following measurements were made by Adam Langley for a blog post 1882 published on February 27th, 2014. The original blog post was 1883 available at the time of this writing at 1884 . 1886 +----------------------------+-------------+-------------------+ 1887 | Chip | AES-128-GCM | ChaCha20-Poly1305 | 1888 +----------------------------+-------------+-------------------+ 1889 | OMAP 4460 | 24.1 MB/s | 75.3 MB/s | 1890 | Snapdragon S4 Pro | 41.5 MB/s | 130.9 MB/s | 1891 | Sandy Bridge Xeon (AES-NI) | 900 MB/s | 500 MB/s | 1892 +----------------------------+-------------+-------------------+ 1894 Table 1: Speed Comparison 1896 Acknowledgements 1898 ChaCha20 and Poly1305 were invented by Daniel J. Bernstein. The AEAD 1899 construction and the method of creating the one-time Poly1305 key 1900 were invented by Adam Langley. 1902 Thanks to Robert Ransom, Watson Ladd, Stefan Buhler, Dan Harkins, and 1903 Kenny Paterson for their helpful comments and explanations. Thanks 1904 to Niels Moller for suggesting the more efficient AEAD construction 1905 in this document. Special thanks to Ilari Liusvaara for providing 1906 extra test vectors, helpful comments, and for being the first to 1907 attempt an implementation from this document. Thanks to Sean 1908 Parkinson for suggesting improvements to the examples and the 1909 pseudocode. Thanks to David Ireland for pointing out a bug in the 1910 pseudocode, and to Stephen Farrell and Alyssa Rowan for pointing out 1911 missing advise in the security considerations. 1913 Special thanks goes to Gordon Procter for performing a security 1914 analysis of the composition and publishing [Procter]. 1916 Jim Schaad and John Mattson provided feedback on tag truncation, and 1917 Russ Housley, Stanislav Smyshlyaev and John Mattson each provided a 1918 review of this version. 1920 Authors' Addresses 1922 Yoav Nir 1923 Dell EMC 1924 9 Andrei Sakharov St 1925 Haifa 3190500 1926 Israel 1928 EMail: ynir.ietf@gmail.com 1930 Adam Langley 1931 Google, Inc. 1933 EMail: agl@google.com