aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / agora / tls / chacha.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note chacha.om: ChaCha20, Poly1305 and AEAD_CHACHA20_POLY1305 exactly as RFC 8439 (module CC). |
| 3 | note OM-2 library unit (no om2 line, no main): the build writes `om2`, then the library files. |
| 4 | note |
| 5 | note API (0 = success; reject codes are documented per function): |
| 6 | note chacha20_xor kn ctr buf kn = key(32) || nonce(12); buf ^= keystream from block counter ctr. |
| 7 | note 2: len kn != 44, ctr above 2^32 - 1, or the last block would |
| 8 | note need a counter above 2^32 - 1 (ctr + blocks > 2^32). |
| 9 | note poly1305_mac kd m o o[0..16) = Poly1305(kd one-time key, m). 2: len kd != 32 or len o < 16. |
| 10 | note aead_chacha_seal kn aad buf buf = plaintext || 16 bytes of tag room; encrypts in place and |
| 11 | note writes the tag at buf[n-16..n). 2: len kn != 44, len buf < 16, |
| 12 | note plaintext over the counter range (2^32 - 1 blocks). |
| 13 | note aead_chacha_open kn aad buf buf = ciphertext || tag. 0: tag valid, buf[0..n-16) decrypted in |
| 14 | note place. 1: tag invalid, buf[0..n-16) zeroed (no unauthenticated |
| 15 | note plaintext is ever released). 2: bad lengths as for seal; buf untouched. |
| 16 | note |
| 17 | note Constant time: no branch, loop count or array index depends on key, nonce, plaintext, keystream, |
| 18 | note Poly1305 accumulator or tag bytes; they depend only on lengths, the public counter and the |
| 19 | note public verdict of the tag comparison (accumulated with or over all 16 bytes). Poly1305 uses |
| 20 | note 26-bit limbs in ulong (products under 2^59) and a branch-free final reduction mod 2^130 - 5 |
| 21 | note (mask select) and addition of s mod 2^128. Compiler-level caveat: omc emits plain AArch64; we |
| 22 | note rely on MUL/ADD/EOR/AND/ORR/shift being data-independent; the DIT bit is not set. Run-time bounds |
| 23 | note checks compare public indices only. Keystream, ChaCha state, one-time key and Poly1305 state |
| 24 | note arrays are zeroed before return. |
| 25 | |
| 26 | note little-endian 32-bit load of a[o .. o+4) |
| 27 | fn chacha20_ld32 a view uchar * o ulong to uint inline |
| 28 | x = uint get a add o 3 |
| 29 | x = or shl x 8 uint get a add o 2 |
| 30 | x = or shl x 8 uint get a add o 1 |
| 31 | ret or shl x 8 uint get a o |
| 32 | end |
| 33 | |
| 34 | note RFC 8439 2.1 / 2.2: QUARTERROUND(a, b, c, d) on the state x; the four indices are the bytes |
| 35 | note of q, a in bits 0..7 up to d in bits 24..31 (one parameter keeps the inlined body in 16 registers) |
| 36 | fn chacha20_qr x edit uint 16 q ulong to void inline |
| 37 | a = get x and q 15 |
| 38 | b = get x and shr q 8 15 |
| 39 | c = get x and shr q 16 15 |
| 40 | d = get x and shr q 24 15 |
| 41 | a = add a b |
| 42 | d = rotl xor d a 16 |
| 43 | c = add c d |
| 44 | b = rotl xor b c 12 |
| 45 | a = add a b |
| 46 | d = rotl xor d a 8 |
| 47 | c = add c d |
| 48 | b = rotl xor b c 7 |
| 49 | put x and q 15 a |
| 50 | put x and shr q 8 15 b |
| 51 | put x and shr q 16 15 c |
| 52 | put x and shr q 24 15 d |
| 53 | end |
| 54 | |
| 55 | note one column round and one diagonal round (RFC 8439 2.3); i is a constant in each unrolled copy, |
| 56 | note so q folds to a number |
| 57 | fn chacha20_dround x edit uint 16 to void |
| 58 | for i ulong 4 unroll |
| 59 | chacha20_qr x or or or i shl add i 4 8 shl add i 8 16 shl add i 12 24 |
| 60 | end |
| 61 | for i ulong 4 unroll |
| 62 | chacha20_qr x or or or i shl add and add i 1 3 4 8 shl add and add i 2 3 8 16 shl add and add i 3 3 12 24 |
| 63 | end |
| 64 | end |
| 65 | |
| 66 | note RFC 8439 2.3 state: constants, key words, counter, nonce words (len kn = 44, ctr < 2^32) |
| 67 | fn chacha20_init s edit uint 16 kn view uchar * ctr ulong to void |
| 68 | put s 0 0x61707865 |
| 69 | put s 1 0x3320646e |
| 70 | put s 2 0x79622d32 |
| 71 | put s 3 0x6b206574 |
| 72 | for i ulong 8 unroll |
| 73 | put s add i 4 chacha20_ld32 kn mul i 4 |
| 74 | end |
| 75 | put s 12 uint ctr |
| 76 | for i ulong 3 unroll |
| 77 | put s add i 13 chacha20_ld32 kn add mul i 4 32 |
| 78 | end |
| 79 | end |
| 80 | |
| 81 | note ks = serialize(20 rounds(s) + s); w is the working state |
| 82 | fn chacha20_block s view uint 16 w edit uint 16 ks edit uchar 64 to void |
| 83 | x = uint 0 |
| 84 | for i ulong 16 unroll |
| 85 | put w i get s i |
| 86 | end |
| 87 | for r ulong 10 |
| 88 | chacha20_dround w |
| 89 | end |
| 90 | for i ulong 16 unroll |
| 91 | x = add get w i get s i |
| 92 | put ks mul i 4 uchar and x 0xff |
| 93 | put ks add mul i 4 1 uchar and shr x 8 0xff |
| 94 | put ks add mul i 4 2 uchar and shr x 16 0xff |
| 95 | put ks add mul i 4 3 uchar shr x 24 |
| 96 | end |
| 97 | end |
| 98 | |
| 99 | note one keystream block of s into buf[o .. min(o + 64, len buf)); s[12] += 1; returns the new o |
| 100 | fn chacha20_step s edit uint 16 w edit uint 16 ks edit uchar 64 buf edit uchar * o ulong to ulong |
| 101 | chacha20_block s w ks |
| 102 | k = sub len buf o |
| 103 | if gt k 64 |
| 104 | k = 64 |
| 105 | end |
| 106 | for i ulong k |
| 107 | q = add o i |
| 108 | put buf q xor get buf q get ks i |
| 109 | end |
| 110 | put s 12 add get s 12 1 |
| 111 | ret add o k |
| 112 | end |
| 113 | |
| 114 | note buf ^= keystream blocks of s, s[12] counting up from its initial value; w, ks scratch |
| 115 | fn chacha20_run s edit uint 16 w edit uint 16 ks edit uchar 64 buf edit uchar * to void |
| 116 | n = len buf |
| 117 | o = ulong 0 |
| 118 | for h ulong add shr n 22 1 |
| 119 | for l ulong 65536 |
| 120 | if ge o n |
| 121 | break |
| 122 | end |
| 123 | o = chacha20_step s w ks buf o |
| 124 | end |
| 125 | end |
| 126 | end |
| 127 | |
| 128 | note RFC 8439 2.4: buf ^= ChaCha20 keystream from block counter ctr |
| 129 | fn chacha20_xor kn view uchar * ctr ulong buf edit uchar * to ulong |
| 130 | s = new uint 16 0 |
| 131 | w = new uint 16 0 |
| 132 | ks = new uchar 64 0 |
| 133 | if ne len kn 44 |
| 134 | ret 2 |
| 135 | end |
| 136 | if gt ctr 0xffffffff |
| 137 | ret 2 |
| 138 | end |
| 139 | if gt shr add len buf 63 6 sub 0x100000000 ctr |
| 140 | ret 2 |
| 141 | end |
| 142 | chacha20_init s kn ctr |
| 143 | chacha20_run s w ks buf |
| 144 | for i ulong 16 unroll |
| 145 | put s i 0 |
| 146 | put w i 0 |
| 147 | end |
| 148 | for i ulong 64 unroll |
| 149 | put ks i 0 |
| 150 | end |
| 151 | ret 0 |
| 152 | end |
| 153 | |
| 154 | note Poly1305 state p (ulong 24): 0..4 r limbs (26 bits), 5..8 5*r1..5*r4, 9..13 accumulator h |
| 155 | note limbs, 14..18 scratch, 19..22 s as four 32-bit words. |
| 156 | note Load the one-time key kd (32 bytes): r clamped as RFC 8439 2.5 (bytes 3, 7, 11, 15 and 15; |
| 157 | note bytes 4, 8, 12 and 252), then cut into 26-bit limbs; s little-endian words; h = 0. |
| 158 | fn poly1305_key p edit ulong 24 kd view uchar * to void |
| 159 | c = new uchar 16 0 |
| 160 | for i ulong 16 unroll |
| 161 | put c i get kd i |
| 162 | end |
| 163 | put c 3 and get c 3 15 |
| 164 | put c 7 and get c 7 15 |
| 165 | put c 11 and get c 11 15 |
| 166 | put c 15 and get c 15 15 |
| 167 | put c 4 and get c 4 252 |
| 168 | put c 8 and get c 8 252 |
| 169 | put c 12 and get c 12 252 |
| 170 | put p 0 and ulong chacha20_ld32 c 0 0x3ffffff |
| 171 | put p 1 and shr ulong chacha20_ld32 c 3 2 0x3ffffff |
| 172 | put p 2 and shr ulong chacha20_ld32 c 6 4 0x3ffffff |
| 173 | put p 3 and shr ulong chacha20_ld32 c 9 6 0x3ffffff |
| 174 | put p 4 shr ulong chacha20_ld32 c 12 8 |
| 175 | for i ulong 4 unroll |
| 176 | put p add i 5 mul get p add i 1 5 |
| 177 | put p add i 19 ulong chacha20_ld32 kd add mul i 4 16 |
| 178 | end |
| 179 | for i ulong 5 unroll |
| 180 | put p add i 9 0 |
| 181 | end |
| 182 | for i ulong 16 unroll |
| 183 | put c i 0 |
| 184 | end |
| 185 | end |
| 186 | |
| 187 | note h = (h + block b + hib * 2^128) * r mod 2^130 - 5 (partial: limbs below 2^26 + 2^6) |
| 188 | fn poly1305_block p edit ulong 24 b view uchar 16 hib ulong to void |
| 189 | h0 = add get p 9 and ulong chacha20_ld32 b 0 0x3ffffff |
| 190 | h1 = add get p 10 and shr ulong chacha20_ld32 b 3 2 0x3ffffff |
| 191 | h2 = add get p 11 and shr ulong chacha20_ld32 b 6 4 0x3ffffff |
| 192 | h3 = add get p 12 and shr ulong chacha20_ld32 b 9 6 0x3ffffff |
| 193 | h4 = add get p 13 or shr ulong chacha20_ld32 b 12 8 shl hib 24 |
| 194 | d = mul h0 get p 0 |
| 195 | d = add d mul h1 get p 8 |
| 196 | d = add d mul h2 get p 7 |
| 197 | d = add d mul h3 get p 6 |
| 198 | put p 14 add d mul h4 get p 5 |
| 199 | d = mul h0 get p 1 |
| 200 | d = add d mul h1 get p 0 |
| 201 | d = add d mul h2 get p 8 |
| 202 | d = add d mul h3 get p 7 |
| 203 | put p 15 add d mul h4 get p 6 |
| 204 | d = mul h0 get p 2 |
| 205 | d = add d mul h1 get p 1 |
| 206 | d = add d mul h2 get p 0 |
| 207 | d = add d mul h3 get p 8 |
| 208 | put p 16 add d mul h4 get p 7 |
| 209 | d = mul h0 get p 3 |
| 210 | d = add d mul h1 get p 2 |
| 211 | d = add d mul h2 get p 1 |
| 212 | d = add d mul h3 get p 0 |
| 213 | put p 17 add d mul h4 get p 8 |
| 214 | d = mul h0 get p 4 |
| 215 | d = add d mul h1 get p 3 |
| 216 | d = add d mul h2 get p 2 |
| 217 | d = add d mul h3 get p 1 |
| 218 | put p 18 add d mul h4 get p 0 |
| 219 | c = ulong 0 |
| 220 | for i ulong 5 unroll |
| 221 | d = add get p add i 14 c |
| 222 | put p add i 9 and d 0x3ffffff |
| 223 | c = shr d 26 |
| 224 | end |
| 225 | d = add get p 9 mul c 5 |
| 226 | put p 9 and d 0x3ffffff |
| 227 | put p 10 add get p 10 shr d 26 |
| 228 | end |
| 229 | |
| 230 | note absorb m: whole 16-byte blocks with the 2^128 bit; a final partial block either RFC 8439 2.5 |
| 231 | note style (pad 1: append 0x01, zero fill, no 2^128 bit) or zero padded to a whole block as the |
| 232 | note AEAD construction of 2.8 requires (pad 0). b is the caller's 16-byte block scratch. |
| 233 | fn poly1305_feed p edit ulong 24 b edit uchar 16 m view uchar * pad ulong to void |
| 234 | n = len m |
| 235 | o = ulong 0 |
| 236 | for h ulong add shr n 20 1 |
| 237 | for l ulong 65536 |
| 238 | if gt add o 16 n |
| 239 | break |
| 240 | end |
| 241 | for i ulong 16 unroll |
| 242 | put b i get m add o i |
| 243 | end |
| 244 | poly1305_block p b 1 |
| 245 | o = add o 16 |
| 246 | end |
| 247 | end |
| 248 | r = sub n o |
| 249 | if eq r 0 |
| 250 | ret |
| 251 | end |
| 252 | for i ulong 16 unroll |
| 253 | put b i 0 |
| 254 | end |
| 255 | for i ulong r |
| 256 | put b i get m add o i |
| 257 | end |
| 258 | if eq pad 1 |
| 259 | put b r 1 |
| 260 | poly1305_block p b 0 |
| 261 | else |
| 262 | poly1305_block p b 1 |
| 263 | end |
| 264 | end |
| 265 | |
| 266 | note full carry, then h = h - (2^130 - 5) when that is not negative, by mask select (RFC 8439 2.5) |
| 267 | fn poly1305_reduce p edit ulong 24 to void |
| 268 | c = ulong 0 |
| 269 | d = ulong 0 |
| 270 | for i ulong 4 unroll |
| 271 | d = add get p add i 10 c |
| 272 | put p add i 10 and d 0x3ffffff |
| 273 | c = shr d 26 |
| 274 | end |
| 275 | d = add get p 9 mul c 5 |
| 276 | put p 9 and d 0x3ffffff |
| 277 | put p 10 add get p 10 shr d 26 |
| 278 | c = 5 |
| 279 | for i ulong 4 unroll |
| 280 | d = add get p add i 9 c |
| 281 | put p add i 14 and d 0x3ffffff |
| 282 | c = shr d 26 |
| 283 | end |
| 284 | d = sub add get p 13 c 0x4000000 |
| 285 | put p 18 d |
| 286 | m = sub shr d 63 1 |
| 287 | for i ulong 5 unroll |
| 288 | put p add i 9 or and get p add i 9 not m and get p add i 14 m |
| 289 | end |
| 290 | end |
| 291 | |
| 292 | note o[0..16) = (h + s) mod 2^128, little-endian (h fully reduced, limbs below 2^26) |
| 293 | fn poly1305_finish p edit ulong 24 o edit uchar * to void |
| 294 | poly1305_reduce p |
| 295 | a = add get p 9 shl get p 10 26 |
| 296 | f = add and a 0xffffffff get p 19 |
| 297 | put p 14 and f 0xffffffff |
| 298 | a = add shr a 32 shl get p 11 20 |
| 299 | f = add add and a 0xffffffff get p 20 shr f 32 |
| 300 | put p 15 and f 0xffffffff |
| 301 | a = add shr a 32 shl get p 12 14 |
| 302 | f = add add and a 0xffffffff get p 21 shr f 32 |
| 303 | put p 16 and f 0xffffffff |
| 304 | a = add shr a 32 shl get p 13 8 |
| 305 | f = add add and a 0xffffffff get p 22 shr f 32 |
| 306 | put p 17 and f 0xffffffff |
| 307 | x = ulong 0 |
| 308 | for i ulong 4 unroll |
| 309 | x = get p add i 14 |
| 310 | put o mul i 4 uchar and x 0xff |
| 311 | put o add mul i 4 1 uchar and shr x 8 0xff |
| 312 | put o add mul i 4 2 uchar and shr x 16 0xff |
| 313 | put o add mul i 4 3 uchar and shr x 24 0xff |
| 314 | end |
| 315 | end |
| 316 | |
| 317 | note RFC 8439 2.5: o[0..16) = Poly1305(kd, m) |
| 318 | fn poly1305_mac kd view uchar * m view uchar * o edit uchar * to ulong |
| 319 | p = new ulong 24 0 |
| 320 | b = new uchar 16 0 |
| 321 | if ne len kd 32 |
| 322 | ret 2 |
| 323 | end |
| 324 | if lt len o 16 |
| 325 | ret 2 |
| 326 | end |
| 327 | poly1305_key p kd |
| 328 | poly1305_feed p b m 1 |
| 329 | poly1305_finish p o |
| 330 | for i ulong 24 unroll |
| 331 | put p i 0 |
| 332 | end |
| 333 | for i ulong 16 unroll |
| 334 | put b i 0 |
| 335 | end |
| 336 | ret 0 |
| 337 | end |
| 338 | |
| 339 | note RFC 8439 2.8 tag over aad, pad16, buf[0..n-16), pad16, le64 len aad, le64 (n-16), keyed by the |
| 340 | note one-time key pk; the tag replaces pk[0..16) and pk[16..32) is zeroed. |
| 341 | fn aead_chacha_mac pk edit uchar 32 aad view uchar * buf view uchar * to void |
| 342 | p = new ulong 24 0 |
| 343 | b = new uchar 16 0 |
| 344 | poly1305_key p pk |
| 345 | poly1305_feed p b aad 0 |
| 346 | n = sub len buf 16 |
| 347 | poly1305_feed p b part buf 0 n 0 |
| 348 | a = len aad |
| 349 | for i ulong 8 unroll |
| 350 | put b i uchar and shr a mul i 8 0xff |
| 351 | put b add i 8 uchar and shr n mul i 8 0xff |
| 352 | end |
| 353 | poly1305_block p b 1 |
| 354 | poly1305_finish p pk |
| 355 | for i ulong 16 unroll |
| 356 | put pk add i 16 0 |
| 357 | end |
| 358 | for i ulong 24 unroll |
| 359 | put p i 0 |
| 360 | end |
| 361 | for i ulong 16 unroll |
| 362 | put b i 0 |
| 363 | end |
| 364 | end |
| 365 | |
| 366 | note 0 when kn and buf have AEAD lengths (len kn 44, len buf >= 16, ciphertext within the counter |
| 367 | note range from block 1), else 2 |
| 368 | fn aead_chacha_lens kn view uchar * buf view uchar * to ulong |
| 369 | if ne len kn 44 |
| 370 | ret 2 |
| 371 | end |
| 372 | if lt len buf 16 |
| 373 | ret 2 |
| 374 | end |
| 375 | if gt shr add sub len buf 16 63 6 0xffffffff |
| 376 | ret 2 |
| 377 | end |
| 378 | ret 0 |
| 379 | end |
| 380 | |
| 381 | note zero a, any length (two loop levels: one rolled loop traps past 1000000 passes) |
| 382 | fn aead_chacha_zero a edit uchar * to void |
| 383 | n = len a |
| 384 | o = ulong 0 |
| 385 | for h ulong add shr n 20 1 |
| 386 | for l ulong 65536 |
| 387 | if ge o n |
| 388 | break |
| 389 | end |
| 390 | k = sub n o |
| 391 | if gt k 16 |
| 392 | k = 16 |
| 393 | end |
| 394 | for i ulong k |
| 395 | put a add o i 0 |
| 396 | end |
| 397 | o = add o k |
| 398 | end |
| 399 | end |
| 400 | end |
| 401 | |
| 402 | note RFC 8439 2.8 AEAD_CHACHA20_POLY1305 encryption, in place |
| 403 | fn aead_chacha_seal kn view uchar * aad view uchar * buf edit uchar * to ulong |
| 404 | pk = new uchar 32 0 |
| 405 | if ne aead_chacha_lens kn buf 0 |
| 406 | ret 2 |
| 407 | end |
| 408 | m = sub len buf 16 |
| 409 | r = chacha20_xor kn 0 pk |
| 410 | r = or r chacha20_xor kn 1 part buf 0 m |
| 411 | aead_chacha_mac pk aad buf |
| 412 | for i ulong 16 unroll |
| 413 | put buf add m i get pk i |
| 414 | end |
| 415 | for i ulong 32 unroll |
| 416 | put pk i 0 |
| 417 | end |
| 418 | ret r |
| 419 | end |
| 420 | |
| 421 | note RFC 8439 2.8 AEAD_CHACHA20_POLY1305 decryption, in place; the tag is checked before any |
| 422 | note plaintext is produced, with a constant-time or-accumulated comparison |
| 423 | fn aead_chacha_open kn view uchar * aad view uchar * buf edit uchar * to ulong |
| 424 | pk = new uchar 32 0 |
| 425 | if ne aead_chacha_lens kn buf 0 |
| 426 | ret 2 |
| 427 | end |
| 428 | m = sub len buf 16 |
| 429 | r = chacha20_xor kn 0 pk |
| 430 | aead_chacha_mac pk aad buf |
| 431 | d = uchar 0 |
| 432 | for i ulong 16 unroll |
| 433 | d = or d xor get pk i get buf add m i |
| 434 | end |
| 435 | for i ulong 32 unroll |
| 436 | put pk i 0 |
| 437 | end |
| 438 | if ne d 0 |
| 439 | aead_chacha_zero part buf 0 m |
| 440 | ret 1 |
| 441 | end |
| 442 | ret or r chacha20_xor kn 1 part buf 0 m |
| 443 | end |