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 AES-GCM driver layer (NIST SP 800-38D) with the 12-byte IV only: J0 = IV || 0^31 || 1, text blocks |
| 2 | note from inc32(J0). Branches and indices depend only on lengths, counters and the public alg. |
| 3 | note Round keys: is256 false AES-128 (key bytes 0..15 of kn), true AES-256 (bytes 0..31). |
| 4 | fn ot_gcm_ks is256 bool kn view uchar 44 rk edit ulong 240 to void |
| 5 | k = new uchar 32 0 |
| 6 | s = new ulong 64 0 |
| 7 | w = new ulong 64 0 |
| 8 | for i ulong 32 unroll |
| 9 | put k i get kn i |
| 10 | end |
| 11 | if is256 |
| 12 | ot_aes_k256 k rk s w |
| 13 | else |
| 14 | ot_aes_k128 k rk s w |
| 15 | end |
| 16 | for i ulong 32 unroll |
| 17 | put k i 0 |
| 18 | end |
| 19 | ot_gcm_z64 s w |
| 20 | end |
| 21 | fn ot_gcm_z64 s edit ulong 64 w edit ulong 64 to void |
| 22 | for i ulong 64 unroll |
| 23 | put s i 0 |
| 24 | put w i 0 |
| 25 | end |
| 26 | end |
| 27 | fn ot_gcm_enc is256 bool rk view ulong 240 s edit ulong 64 w edit ulong 64 to void |
| 28 | if is256 |
| 29 | ot_aes_e256 rk s w |
| 30 | else |
| 31 | ot_aes_e128 rk s w |
| 32 | end |
| 33 | end |
| 34 | note Counter block for block q of s: the nonce, then the 32-bit big-endian counter c. |
| 35 | fn ot_gcm_cb kn view uchar 44 s edit ulong 64 q ulong c ulong to void |
| 36 | for i ulong 12 unroll |
| 37 | put s add mul q 16 i ulong get kn add 32 i |
| 38 | end |
| 39 | for i ulong 4 unroll |
| 40 | put s add add mul q 16 12 i and shr c sub 24 mul i 8 255 |
| 41 | end |
| 42 | end |
| 43 | note hj: 0..1 H = E(K, 0^128) as big-endian halves, 2..3 E(K, J0). |
| 44 | fn ot_gcm_hj is256 bool kn view uchar 44 rk view ulong 240 hj edit ulong 4 to void |
| 45 | s = new ulong 64 0 |
| 46 | w = new ulong 64 0 |
| 47 | ot_gcm_cb kn s 1 1 |
| 48 | ot_gcm_enc is256 rk s w |
| 49 | for q ulong 2 unroll |
| 50 | a = ulong 0 |
| 51 | b = ulong 0 |
| 52 | for i ulong 8 unroll |
| 53 | a = or shl a 8 get s add mul q 16 i |
| 54 | b = or shl b 8 get s add add mul q 16 8 i |
| 55 | end |
| 56 | put hj mul q 2 a |
| 57 | put hj add mul q 2 1 b |
| 58 | end |
| 59 | ot_gcm_z64 s w |
| 60 | end |
| 61 | note buf ^= the keystream from counter 2 (CTR mode, SP 800-38D 6.5), four blocks per AES call. |
| 62 | fn ot_gcm_ctr is256 bool kn view uchar 44 rk view ulong 240 buf edit uchar * to void |
| 63 | s = new ulong 64 0 |
| 64 | w = new ulong 64 0 |
| 65 | n = len buf |
| 66 | p = ulong 0 |
| 67 | c = ulong 2 |
| 68 | while lt p n bound 9223372036854775807 |
| 69 | for q ulong 4 unroll |
| 70 | ot_gcm_cb kn s q and add c q 0xffffffff |
| 71 | end |
| 72 | ot_gcm_enc is256 rk s w |
| 73 | m = sub n p |
| 74 | if gt m 64 |
| 75 | m = 64 |
| 76 | end |
| 77 | ot_gcm_x s buf p m |
| 78 | p = add p m |
| 79 | c = add c 4 |
| 80 | end |
| 81 | ot_gcm_z64 s w |
| 82 | end |
| 83 | fn ot_gcm_x s view ulong 64 buf edit uchar * p ulong m ulong to void |
| 84 | for i ulong m |
| 85 | j = add p i |
| 86 | v = xor ulong get buf j get s i |
| 87 | put buf j ot_ct_lo8 v |
| 88 | end |
| 89 | end |
| 90 | note x ^= up to 16 bytes of d from p (zero-padded) as big-endian halves. |
| 91 | fn ot_gcm_ld d view uchar * p ulong n ulong x edit ulong 2 to void |
| 92 | a = ulong 0 |
| 93 | b = ulong 0 |
| 94 | for i ulong n |
| 95 | v = ulong get d add p i |
| 96 | if lt i 8 |
| 97 | a = or a shl v sub 56 mul i 8 |
| 98 | else |
| 99 | b = or b shl v sub 56 mul sub i 8 8 |
| 100 | end |
| 101 | end |
| 102 | put x 0 xor get x 0 a |
| 103 | put x 1 xor get x 1 b |
| 104 | end |
| 105 | note GHASH of d (zero-padded to 16 bytes) into x under h. |
| 106 | fn ot_gcm_gh x edit ulong 2 h view ulong 2 d view uchar * to void |
| 107 | n = len d |
| 108 | p = ulong 0 |
| 109 | while lt p n bound 9223372036854775807 |
| 110 | m = sub n p |
| 111 | if gt m 16 |
| 112 | m = 16 |
| 113 | end |
| 114 | ot_gcm_ld d p m x |
| 115 | ot_gcm_mul x h |
| 116 | p = add p m |
| 117 | end |
| 118 | end |
| 119 | fn ot_gcm_out x view ulong 2 hj view ulong 4 tag edit uchar 16 to void |
| 120 | a = xor get x 0 get hj 2 |
| 121 | b = xor get x 1 get hj 3 |
| 122 | for i ulong 8 unroll |
| 123 | put tag i ot_ct_lo8 shr a sub 56 mul i 8 |
| 124 | put tag add 8 i ot_ct_lo8 shr b sub 56 mul i 8 |
| 125 | end |
| 126 | end |
| 127 | note tag = GHASH(aad, ct, lengths in bits) ^ E(K, J0), big-endian bytes. |
| 128 | fn ot_gcm_tag hj view ulong 4 aad view uchar * ct view uchar * tag edit uchar 16 to void |
| 129 | x = new ulong 2 0 |
| 130 | h = new ulong 2 0 |
| 131 | put h 0 get hj 0 |
| 132 | put h 1 get hj 1 |
| 133 | ot_gcm_gh x h aad |
| 134 | ot_gcm_gh x h ct |
| 135 | put x 0 xor get x 0 shl len aad 3 |
| 136 | put x 1 xor get x 1 shl len ct 3 |
| 137 | ot_gcm_mul x h |
| 138 | ot_gcm_out x hj tag |
| 139 | put x 0 0 |
| 140 | put x 1 0 |
| 141 | put h 0 0 |
| 142 | put h 1 0 |
| 143 | end |
| 144 | fn ot_gcm_seal is256 bool kn view uchar 44 aad view uchar * buf edit uchar * to void |
| 145 | rk = new ulong 240 0 |
| 146 | hj = new ulong 4 0 |
| 147 | t = new uchar 16 0 |
| 148 | m = sub len buf 16 |
| 149 | ot_gcm_ks is256 kn rk |
| 150 | ot_gcm_hj is256 kn rk hj |
| 151 | ot_gcm_ctr is256 kn rk part buf 0 m |
| 152 | ot_gcm_tag hj aad view part buf 0 m t |
| 153 | for i ulong 16 unroll |
| 154 | put buf add m i get t i |
| 155 | end |
| 156 | ot_gcm_zk rk hj |
| 157 | end |
| 158 | fn ot_gcm_zk rk edit ulong 240 hj edit ulong 4 to void |
| 159 | for i ulong 240 |
| 160 | put rk i 0 |
| 161 | end |
| 162 | for i ulong 4 unroll |
| 163 | put hj i 0 |
| 164 | end |
| 165 | end |
| 166 | fn ot_gcm_open is256 bool kn view uchar 44 aad view uchar * buf edit uchar * to bool |
| 167 | rk = new ulong 240 0 |
| 168 | hj = new ulong 4 0 |
| 169 | t = new uchar 16 0 |
| 170 | rt = new uchar 16 0 |
| 171 | m = sub len buf 16 |
| 172 | ot_gcm_ks is256 kn rk |
| 173 | ot_gcm_hj is256 kn rk hj |
| 174 | ot_gcm_tag hj aad view part buf 0 m t |
| 175 | for i ulong 16 unroll |
| 176 | put rt i get buf add m i |
| 177 | end |
| 178 | if not ot_ct_eq16 t rt |
| 179 | ot_gcm_zk rk hj |
| 180 | ot_ae_zero buf |
| 181 | ret false |
| 182 | end |
| 183 | ot_gcm_ctr is256 kn rk part buf 0 m |
| 184 | ot_gcm_zk rk hj |
| 185 | ret true |
| 186 | end |