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 SHA-384 / SHA-512 driver layer (FIPS 180-4) and the one-shot ot_hash dispatch. |
| 2 | note Context c (ulong 32): 0..7 chaining value, 8..23 the pending block as 16 big-endian words (bytes past the |
| 3 | note pending count are zero), 24 bytes absorbed (mod 2^64), 25 digest length (48 SHA-384, 64 SHA-512), 26..31 zero. |
| 4 | note Branches and indices here depend only on lengths and counts; message and state bytes only flow through |
| 5 | note arithmetic and the CT core. |
| 6 | fn ot_s5_ins c edit ulong 32 k ulong v uchar to void |
| 7 | wi = add 8 shr k 3 |
| 8 | x = shl ulong v shl sub 7 and k 7 3 |
| 9 | put c wi or get c wi x |
| 10 | end |
| 11 | fn ot_s5_wd d view uchar * p ulong m edit ulong 16 to void |
| 12 | for i ulong 16 unroll |
| 13 | v = ulong 0 |
| 14 | for j ulong 8 unroll |
| 15 | v = or shl v 8 ulong get d add p add mul i 8 j |
| 16 | end |
| 17 | put m i v |
| 18 | end |
| 19 | end |
| 20 | fn ot_s5_z8 a edit ulong 8 to void |
| 21 | for i ulong 8 unroll |
| 22 | put a i 0 |
| 23 | end |
| 24 | end |
| 25 | fn ot_s5_z16 a edit ulong 16 to void |
| 26 | for i ulong 16 unroll |
| 27 | put a i 0 |
| 28 | end |
| 29 | end |
| 30 | fn ot_s5_z80 a edit ulong 80 to void |
| 31 | for i ulong 80 |
| 32 | put a i 0 |
| 33 | end |
| 34 | end |
| 35 | note Hash the full pending block of c into h and clear it. |
| 36 | fn ot_s5_flush c edit ulong 32 h edit ulong 8 m edit ulong 16 w edit ulong 80 to void |
| 37 | for i ulong 16 unroll |
| 38 | put m i get c add 8 i |
| 39 | put c add 8 i 0 |
| 40 | end |
| 41 | ot_s5_blk h m w |
| 42 | end |
| 43 | export fn ot_s5_init c edit ulong 32 is384 bool to void |
| 44 | h = new ulong 8 0 |
| 45 | for i ulong 32 unroll |
| 46 | put c i 0 |
| 47 | end |
| 48 | if is384 |
| 49 | ot_s5_iv3 h |
| 50 | put c 25 48 |
| 51 | else |
| 52 | ot_s5_iv5 h |
| 53 | put c 25 64 |
| 54 | end |
| 55 | for i ulong 8 unroll |
| 56 | put c i get h i |
| 57 | end |
| 58 | end |
| 59 | export fn ot_s5_update c edit ulong 32 d view uchar * to void |
| 60 | h = new ulong 8 0 |
| 61 | m = new ulong 16 0 |
| 62 | w = new ulong 80 0 |
| 63 | for i ulong 8 unroll |
| 64 | put h i get c i |
| 65 | end |
| 66 | n = len d |
| 67 | k = and get c 24 127 |
| 68 | p = ulong 0 |
| 69 | if gt k 0 |
| 70 | while and lt p n lt k 128 bound 128 |
| 71 | ot_s5_ins c k get d p |
| 72 | k = add k 1 |
| 73 | p = add p 1 |
| 74 | end |
| 75 | if eq k 128 |
| 76 | ot_s5_flush c h m w |
| 77 | k = 0 |
| 78 | end |
| 79 | end |
| 80 | while ge sub n p 128 bound 9223372036854775807 |
| 81 | ot_s5_wd d p m |
| 82 | ot_s5_blk h m w |
| 83 | p = add p 128 |
| 84 | end |
| 85 | while lt p n bound 128 |
| 86 | ot_s5_ins c k get d p |
| 87 | k = add k 1 |
| 88 | p = add p 1 |
| 89 | end |
| 90 | put c 24 add get c 24 n |
| 91 | for i ulong 8 unroll |
| 92 | put c i get h i |
| 93 | end |
| 94 | ot_s5_z8 h |
| 95 | ot_s5_z16 m |
| 96 | ot_s5_z80 w |
| 97 | end |
| 98 | note Pad (0x80, zeros, 128-bit big-endian bit length) and write the digest; c is not changed. |
| 99 | export fn ot_s5_final c view ulong 32 o edit uchar 64 to void |
| 100 | h = new ulong 8 0 |
| 101 | m = new ulong 16 0 |
| 102 | w = new ulong 80 0 |
| 103 | for i ulong 8 unroll |
| 104 | put h i get c i |
| 105 | end |
| 106 | for i ulong 16 unroll |
| 107 | put m i get c add 8 i |
| 108 | end |
| 109 | cnt = get c 24 |
| 110 | k = and cnt 127 |
| 111 | wi = shr k 3 |
| 112 | x = shl sub 7 and k 7 3 |
| 113 | x = shl ulong 128 x |
| 114 | put m wi or get m wi x |
| 115 | if gt k 111 |
| 116 | ot_s5_blk h m w |
| 117 | ot_s5_z16 m |
| 118 | end |
| 119 | put m 14 shr cnt 61 |
| 120 | put m 15 shl cnt 3 |
| 121 | ot_s5_blk h m w |
| 122 | for j ulong 8 unroll |
| 123 | for i ulong 8 unroll |
| 124 | put o add mul j 8 i ot_ct_lo8 shr get h j sub 56 mul i 8 |
| 125 | end |
| 126 | end |
| 127 | if eq get c 25 48 |
| 128 | for i ulong 16 unroll |
| 129 | put o add 48 i 0 |
| 130 | end |
| 131 | end |
| 132 | ot_s5_z8 h |
| 133 | ot_s5_z16 m |
| 134 | ot_s5_z80 w |
| 135 | end |
| 136 | export fn ot_sha384 d view uchar * o edit uchar 48 to void |
| 137 | c = new ulong 32 0 |
| 138 | t = new uchar 64 0 |
| 139 | ot_s5_init c true |
| 140 | ot_s5_update c d |
| 141 | ot_s5_final c t |
| 142 | for i ulong 48 unroll |
| 143 | put o i get t i |
| 144 | end |
| 145 | for i ulong 64 unroll |
| 146 | put t i 0 |
| 147 | end |
| 148 | for i ulong 32 unroll |
| 149 | put c i 0 |
| 150 | end |
| 151 | end |
| 152 | export fn ot_sha512 d view uchar * o edit uchar 64 to void |
| 153 | c = new ulong 32 0 |
| 154 | ot_s5_init c false |
| 155 | ot_s5_update c d |
| 156 | ot_s5_final c o |
| 157 | for i ulong 32 unroll |
| 158 | put c i 0 |
| 159 | end |
| 160 | end |
| 161 | note One-shot hash: alg 0 SHA-256 (vendored), 1 SHA-384, 2 SHA-512. Gives the digest length and zeroes o past it; |
| 162 | note an unknown alg gives 0 with o all zero. |
| 163 | export fn ot_hash alg ulong d view uchar * o edit uchar 64 to ulong |
| 164 | t = new uchar 32 0 |
| 165 | c = new ulong 32 0 |
| 166 | if eq alg 0 |
| 167 | sha256_hash d t |
| 168 | for i ulong 64 unroll |
| 169 | put o i 0 |
| 170 | end |
| 171 | for i ulong 32 unroll |
| 172 | put o i get t i |
| 173 | put t i 0 |
| 174 | end |
| 175 | ret 32 |
| 176 | end |
| 177 | if eq alg 1 |
| 178 | ot_s5_init c true |
| 179 | ot_s5_update c d |
| 180 | ot_s5_final c o |
| 181 | for i ulong 32 unroll |
| 182 | put c i 0 |
| 183 | end |
| 184 | ret 48 |
| 185 | end |
| 186 | if eq alg 2 |
| 187 | ot_sha512 d o |
| 188 | ret 64 |
| 189 | end |
| 190 | for i ulong 64 unroll |
| 191 | put o i 0 |
| 192 | end |
| 193 | ret 0 |
| 194 | end |