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
| 1 | note omtls ChaCha20 constant-time core (RFC 8439 sections 2.1-2.3). uint arithmetic wraps mod 2^32; rotl by a |
| 2 | note number is one ROR. Every index is a constant; no branch. |
| 3 | note State: 0..3 the constants, 4..11 the key (little-endian words), 12 the block counter, 13..15 the nonce. |
| 4 | fn ot_cc_kn st edit uint 16 kn view uchar 44 ctr uint to void |
| 5 | put st 0 0x61707865 |
| 6 | put st 1 0x3320646e |
| 7 | put st 2 0x79622d32 |
| 8 | put st 3 0x6b206574 |
| 9 | for i ulong 11 unroll |
| 10 | w = uint get kn mul i 4 |
| 11 | w = or w shl uint get kn add mul i 4 1 8 |
| 12 | w = or w shl uint get kn add mul i 4 2 16 |
| 13 | w = or w shl uint get kn add mul i 4 3 24 |
| 14 | put st add 4 add i shr i 3 w |
| 15 | end |
| 16 | put st 12 ctr |
| 17 | end |
| 18 | note The block function: x = st, 20 rounds (10 column + diagonal double rounds), then x += st. |
| 19 | fn ot_cc_blk st view uint 16 x edit uint 16 to void |
| 20 | for i ulong 16 unroll |
| 21 | put x i get st i |
| 22 | end |
| 23 | for r ulong 10 unroll |
| 24 | note column rounds QUARTERROUND(i, 4+i, 8+i, 12+i) |
| 25 | for i ulong 4 unroll |
| 26 | a = get x i |
| 27 | b = get x add 4 i |
| 28 | c = get x add 8 i |
| 29 | d = get x add 12 i |
| 30 | a = add a b |
| 31 | d = rotl xor d a 16 |
| 32 | c = add c d |
| 33 | b = rotl xor b c 12 |
| 34 | a = add a b |
| 35 | d = rotl xor d a 8 |
| 36 | c = add c d |
| 37 | b = rotl xor b c 7 |
| 38 | put x i a |
| 39 | put x add 4 i b |
| 40 | put x add 8 i c |
| 41 | put x add 12 i d |
| 42 | end |
| 43 | note diagonal rounds QUARTERROUND(i, 4+(i+1)%4, 8+(i+2)%4, 12+(i+3)%4) |
| 44 | for i ulong 4 unroll |
| 45 | a = get x i |
| 46 | b = get x add 4 and add i 1 3 |
| 47 | c = get x add 8 and add i 2 3 |
| 48 | d = get x add 12 and add i 3 3 |
| 49 | a = add a b |
| 50 | d = rotl xor d a 16 |
| 51 | c = add c d |
| 52 | b = rotl xor b c 12 |
| 53 | a = add a b |
| 54 | d = rotl xor d a 8 |
| 55 | c = add c d |
| 56 | b = rotl xor b c 7 |
| 57 | put x i a |
| 58 | put x add 4 and add i 1 3 b |
| 59 | put x add 8 and add i 2 3 c |
| 60 | put x add 12 and add i 3 3 d |
| 61 | end |
| 62 | end |
| 63 | for i ulong 16 unroll |
| 64 | put x i add get x i get st i |
| 65 | end |
| 66 | end |
| 67 | note One quarter round on four words (RFC 8439 2.1), for the 2.1.1 test vector: q holds a, b, c, d. |
| 68 | fn ot_cc_qr q edit uint 4 to void |
| 69 | a = get q 0 |
| 70 | b = get q 1 |
| 71 | c = get q 2 |
| 72 | d = get q 3 |
| 73 | a = add a b |
| 74 | d = rotl xor d a 16 |
| 75 | c = add c d |
| 76 | b = rotl xor b c 12 |
| 77 | a = add a b |
| 78 | d = rotl xor d a 8 |
| 79 | c = add c d |
| 80 | b = rotl xor b c 7 |
| 81 | put q 0 a |
| 82 | put q 1 b |
| 83 | put q 2 c |
| 84 | put q 3 d |
| 85 | end |
| 86 | note b ^= the 64 keystream bytes of block x (word i/4, little-endian byte i mod 4). |
| 87 | fn ot_cc_x64 x view uint 16 b edit uchar 64 to void |
| 88 | for w ulong 16 unroll |
| 89 | v = ulong get x w |
| 90 | for j ulong 4 unroll |
| 91 | put b add mul w 4 j xor get b add mul w 4 j ot_ct_lo8 shr v mul j 8 |
| 92 | end |
| 93 | end |
| 94 | end |
| 95 | note The 64 keystream bytes of block x into b (for the 2.3.2 / A.1 block vectors and the Poly1305 key). |
| 96 | fn ot_cc_ser x view uint 16 b edit uchar 64 to void |
| 97 | for w ulong 16 unroll |
| 98 | v = ulong get x w |
| 99 | for j ulong 4 unroll |
| 100 | put b add mul w 4 j ot_ct_lo8 shr v mul j 8 |
| 101 | end |
| 102 | end |
| 103 | end |