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-128/256 encryption constant-time core (FIPS 197), bitsliced S-box, and the GHASH multiply |
| 2 | note (NIST SP 800-38D 6.3). Every loop unrolled, every index a constant, no branch, no table lookup. |
| 3 | note State s (ulong 64): four 16-byte blocks, block q byte r + 4c at 16q + r + 4c, one byte value per word. |
| 4 | note S-box: the 64 bytes become 8 bit-planes (plane j bit i = bit j of byte i) in slot 0 of the plane scratch w |
| 5 | note (ulong 64: slot q = words 8q .. 8q+7; slot 7 is the multiplier's scratch). The GF(2^8) inverse is x^254 |
| 6 | note (0 -> 0): x2 = x x, x3 = x2 x, x6 = x3 x3, x12 = x6 x6, x15 = x12 x3, x30, x60, x120, x240 by squaring, |
| 7 | note x252 = x240 x12, x254 = x252 x2; then the FIPS 197 5.1.1 affine map and back to bytes. |
| 8 | note A product is shift-and-add over the planes: slot 7 holds A x^i (xtime on planes: x^8 = x^4 + x^3 + x + 1), |
| 9 | note added into D under the plane mask b_i. Each fixed (A, B, D) is its own function so indices stay constants |
| 10 | note (a slot passed as an argument would be a run-time index). |
| 11 | note wolfram: 1/(x^6 + x^4 + x + 1) mod (x^8 + x^4 + x^3 + x + 1) over GF(2) = 0xca (FIPS 197 4.4 example). |
| 12 | fn ot_aes_pk s view ulong 64 w edit ulong 64 to void |
| 13 | for j ulong 8 unroll |
| 14 | a = ulong 0 |
| 15 | for i ulong 64 unroll |
| 16 | a = or a shl and shr get s i j 1 i |
| 17 | end |
| 18 | put w j a |
| 19 | end |
| 20 | end |
| 21 | note Slot 7 times x on planes: (a0..a7) -> (a7, a0^a7, a1, a2^a7, a3^a7, a4, a5, a6). |
| 22 | fn ot_aes_x7 w edit ulong 64 to void |
| 23 | a3 = get w 59 |
| 24 | a7 = get w 63 |
| 25 | put w 63 get w 62 |
| 26 | put w 62 get w 61 |
| 27 | put w 61 get w 60 |
| 28 | put w 60 xor a3 a7 |
| 29 | put w 59 xor get w 58 a7 |
| 30 | put w 58 get w 57 |
| 31 | put w 57 xor get w 56 a7 |
| 32 | put w 56 a7 |
| 33 | end |
| 34 | fn ot_aes_m001 w edit ulong 64 to void |
| 35 | for k ulong 8 unroll |
| 36 | put w add 56 k get w add 0 k |
| 37 | put w add 8 k 0 |
| 38 | end |
| 39 | for i ulong 8 unroll |
| 40 | c = get w add 0 i |
| 41 | for k ulong 8 unroll |
| 42 | put w add 8 k xor get w add 8 k and get w add 56 k c |
| 43 | end |
| 44 | ot_aes_x7 w |
| 45 | end |
| 46 | end |
| 47 | fn ot_aes_m102 w edit ulong 64 to void |
| 48 | for k ulong 8 unroll |
| 49 | put w add 56 k get w add 8 k |
| 50 | put w add 16 k 0 |
| 51 | end |
| 52 | for i ulong 8 unroll |
| 53 | c = get w add 0 i |
| 54 | for k ulong 8 unroll |
| 55 | put w add 16 k xor get w add 16 k and get w add 56 k c |
| 56 | end |
| 57 | ot_aes_x7 w |
| 58 | end |
| 59 | end |
| 60 | fn ot_aes_m223 w edit ulong 64 to void |
| 61 | for k ulong 8 unroll |
| 62 | put w add 56 k get w add 16 k |
| 63 | put w add 24 k 0 |
| 64 | end |
| 65 | for i ulong 8 unroll |
| 66 | c = get w add 16 i |
| 67 | for k ulong 8 unroll |
| 68 | put w add 24 k xor get w add 24 k and get w add 56 k c |
| 69 | end |
| 70 | ot_aes_x7 w |
| 71 | end |
| 72 | end |
| 73 | fn ot_aes_m334 w edit ulong 64 to void |
| 74 | for k ulong 8 unroll |
| 75 | put w add 56 k get w add 24 k |
| 76 | put w add 32 k 0 |
| 77 | end |
| 78 | for i ulong 8 unroll |
| 79 | c = get w add 24 i |
| 80 | for k ulong 8 unroll |
| 81 | put w add 32 k xor get w add 32 k and get w add 56 k c |
| 82 | end |
| 83 | ot_aes_x7 w |
| 84 | end |
| 85 | end |
| 86 | fn ot_aes_m425 w edit ulong 64 to void |
| 87 | for k ulong 8 unroll |
| 88 | put w add 56 k get w add 32 k |
| 89 | put w add 40 k 0 |
| 90 | end |
| 91 | for i ulong 8 unroll |
| 92 | c = get w add 16 i |
| 93 | for k ulong 8 unroll |
| 94 | put w add 40 k xor get w add 40 k and get w add 56 k c |
| 95 | end |
| 96 | ot_aes_x7 w |
| 97 | end |
| 98 | end |
| 99 | fn ot_aes_m556 w edit ulong 64 to void |
| 100 | for k ulong 8 unroll |
| 101 | put w add 56 k get w add 40 k |
| 102 | put w add 48 k 0 |
| 103 | end |
| 104 | for i ulong 8 unroll |
| 105 | c = get w add 40 i |
| 106 | for k ulong 8 unroll |
| 107 | put w add 48 k xor get w add 48 k and get w add 56 k c |
| 108 | end |
| 109 | ot_aes_x7 w |
| 110 | end |
| 111 | end |
| 112 | fn ot_aes_m663 w edit ulong 64 to void |
| 113 | for k ulong 8 unroll |
| 114 | put w add 56 k get w add 48 k |
| 115 | put w add 24 k 0 |
| 116 | end |
| 117 | for i ulong 8 unroll |
| 118 | c = get w add 48 i |
| 119 | for k ulong 8 unroll |
| 120 | put w add 24 k xor get w add 24 k and get w add 56 k c |
| 121 | end |
| 122 | ot_aes_x7 w |
| 123 | end |
| 124 | end |
| 125 | fn ot_aes_m336 w edit ulong 64 to void |
| 126 | for k ulong 8 unroll |
| 127 | put w add 56 k get w add 24 k |
| 128 | put w add 48 k 0 |
| 129 | end |
| 130 | for i ulong 8 unroll |
| 131 | c = get w add 24 i |
| 132 | for k ulong 8 unroll |
| 133 | put w add 48 k xor get w add 48 k and get w add 56 k c |
| 134 | end |
| 135 | ot_aes_x7 w |
| 136 | end |
| 137 | end |
| 138 | fn ot_aes_m346 w edit ulong 64 to void |
| 139 | for k ulong 8 unroll |
| 140 | put w add 56 k get w add 24 k |
| 141 | put w add 48 k 0 |
| 142 | end |
| 143 | for i ulong 8 unroll |
| 144 | c = get w add 32 i |
| 145 | for k ulong 8 unroll |
| 146 | put w add 48 k xor get w add 48 k and get w add 56 k c |
| 147 | end |
| 148 | ot_aes_x7 w |
| 149 | end |
| 150 | end |
| 151 | fn ot_aes_m610 w edit ulong 64 to void |
| 152 | for k ulong 8 unroll |
| 153 | put w add 56 k get w add 48 k |
| 154 | put w add 0 k 0 |
| 155 | end |
| 156 | for i ulong 8 unroll |
| 157 | c = get w add 8 i |
| 158 | for k ulong 8 unroll |
| 159 | put w add 0 k xor get w add 0 k and get w add 56 k c |
| 160 | end |
| 161 | ot_aes_x7 w |
| 162 | end |
| 163 | end |
| 164 | note FIPS 197 5.1.1 affine map of slot 0 into slot 1: b_k ^ b_(k+4) ^ b_(k+5) ^ b_(k+6) ^ b_(k+7) ^ (bit k of 0x63, |
| 165 | note as an all-ones plane), then the planes of slot 1 back to the 64 bytes of s. |
| 166 | fn ot_aes_af s edit ulong 64 w edit ulong 64 to void |
| 167 | for k ulong 8 unroll |
| 168 | v = xor xor get w k get w and add k 4 7 get w and add k 5 7 |
| 169 | v = xor xor v get w and add k 6 7 get w and add k 7 7 |
| 170 | put w add 8 k xor v neg and shr 99 k 1 |
| 171 | end |
| 172 | for i ulong 64 unroll |
| 173 | a = ulong 0 |
| 174 | for j ulong 8 unroll |
| 175 | a = or a shl and shr get w add 8 j i 1 j |
| 176 | end |
| 177 | put s i a |
| 178 | end |
| 179 | end |
| 180 | note SubBytes on all 64 bytes of s. |
| 181 | fn ot_aes_sb s edit ulong 64 w edit ulong 64 to void |
| 182 | ot_aes_pk s w |
| 183 | ot_aes_m001 w |
| 184 | ot_aes_m102 w |
| 185 | ot_aes_m223 w |
| 186 | ot_aes_m334 w |
| 187 | ot_aes_m425 w |
| 188 | ot_aes_m556 w |
| 189 | ot_aes_m663 w |
| 190 | ot_aes_m336 w |
| 191 | ot_aes_m663 w |
| 192 | ot_aes_m346 w |
| 193 | ot_aes_m610 w |
| 194 | ot_aes_af s w |
| 195 | end |
| 196 | note ShiftRows (FIPS 197 5.1.2): row r of each block rotated left by r columns. |
| 197 | fn ot_aes_sr s edit ulong 64 to void |
| 198 | for q ulong 4 unroll |
| 199 | t = get s add mul q 16 1 |
| 200 | put s add mul q 16 1 get s add mul q 16 5 |
| 201 | put s add mul q 16 5 get s add mul q 16 9 |
| 202 | put s add mul q 16 9 get s add mul q 16 13 |
| 203 | put s add mul q 16 13 t |
| 204 | t = get s add mul q 16 2 |
| 205 | put s add mul q 16 2 get s add mul q 16 10 |
| 206 | put s add mul q 16 10 t |
| 207 | t = get s add mul q 16 6 |
| 208 | put s add mul q 16 6 get s add mul q 16 14 |
| 209 | put s add mul q 16 14 t |
| 210 | t = get s add mul q 16 15 |
| 211 | put s add mul q 16 15 get s add mul q 16 11 |
| 212 | put s add mul q 16 11 get s add mul q 16 7 |
| 213 | put s add mul q 16 7 get s add mul q 16 3 |
| 214 | put s add mul q 16 3 t |
| 215 | end |
| 216 | end |
| 217 | note xtime (FIPS 197 4.2.1): a * x mod p for a byte a in a ulong, the reduction selected by a mask. |
| 218 | fn ot_aes_xt a ulong to ulong inline |
| 219 | ret xor and shl a 1 255 and 27 neg shr a 7 |
| 220 | end |
| 221 | note MixColumns (FIPS 197 5.1.3): b_r = a_r ^ t ^ xtime(a_r ^ a_(r+1)), t = a0 ^ a1 ^ a2 ^ a3. |
| 222 | fn ot_aes_mc s edit ulong 64 to void |
| 223 | for q ulong 4 unroll |
| 224 | for c ulong 4 unroll |
| 225 | a0 = get s add mul q 16 mul c 4 |
| 226 | a1 = get s add add mul q 16 mul c 4 1 |
| 227 | a2 = get s add add mul q 16 mul c 4 2 |
| 228 | a3 = get s add add mul q 16 mul c 4 3 |
| 229 | t = xor xor xor a0 a1 a2 a3 |
| 230 | put s add mul q 16 mul c 4 xor xor a0 t ot_aes_xt xor a0 a1 |
| 231 | put s add add mul q 16 mul c 4 1 xor xor a1 t ot_aes_xt xor a1 a2 |
| 232 | put s add add mul q 16 mul c 4 2 xor xor a2 t ot_aes_xt xor a2 a3 |
| 233 | put s add add mul q 16 mul c 4 3 xor xor a3 t ot_aes_xt xor a3 a0 |
| 234 | end |
| 235 | end |
| 236 | end |
| 237 | note AES encryption of the 4 blocks of s with the round keys rk (16 bytes per round, 10 rounds). |
| 238 | fn ot_aes_e128 rk view ulong 240 s edit ulong 64 w edit ulong 64 to void |
| 239 | for q ulong 4 unroll |
| 240 | for i ulong 16 unroll |
| 241 | put s add mul q 16 i xor get s add mul q 16 i get rk i |
| 242 | end |
| 243 | end |
| 244 | for r ulong 9 unroll |
| 245 | ot_aes_sb s w |
| 246 | ot_aes_sr s |
| 247 | ot_aes_mc s |
| 248 | for q ulong 4 unroll |
| 249 | for i ulong 16 unroll |
| 250 | put s add mul q 16 i xor get s add mul q 16 i get rk add mul add r 1 16 i |
| 251 | end |
| 252 | end |
| 253 | end |
| 254 | ot_aes_sb s w |
| 255 | ot_aes_sr s |
| 256 | for q ulong 4 unroll |
| 257 | for i ulong 16 unroll |
| 258 | put s add mul q 16 i xor get s add mul q 16 i get rk add 160 i |
| 259 | end |
| 260 | end |
| 261 | end |
| 262 | note AES encryption of the 4 blocks of s with the round keys rk (16 bytes per round, 14 rounds). |
| 263 | fn ot_aes_e256 rk view ulong 240 s edit ulong 64 w edit ulong 64 to void |
| 264 | for q ulong 4 unroll |
| 265 | for i ulong 16 unroll |
| 266 | put s add mul q 16 i xor get s add mul q 16 i get rk i |
| 267 | end |
| 268 | end |
| 269 | for r ulong 13 unroll |
| 270 | ot_aes_sb s w |
| 271 | ot_aes_sr s |
| 272 | ot_aes_mc s |
| 273 | for q ulong 4 unroll |
| 274 | for i ulong 16 unroll |
| 275 | put s add mul q 16 i xor get s add mul q 16 i get rk add mul add r 1 16 i |
| 276 | end |
| 277 | end |
| 278 | end |
| 279 | ot_aes_sb s w |
| 280 | ot_aes_sr s |
| 281 | for q ulong 4 unroll |
| 282 | for i ulong 16 unroll |
| 283 | put s add mul q 16 i xor get s add mul q 16 i get rk add 224 i |
| 284 | end |
| 285 | end |
| 286 | end |
| 287 | note Rcon (FIPS 197 5.2) for round r = 0..9: x^r = 01 02 04 08 10 20 40 80 1b 36, as xor xor and shl 1 r 255 mul 27 and shr r 3 xor and r 1 1 mul 54 and shr r 3 and r 1. |
| 288 | note Key expansion (FIPS 197 5.2): rk bytes 4i .. 4i+3 hold word w_i; SubWord through the bitsliced S-box on lanes |
| 289 | note 0..3 of s. AES-128: Nk = 4, 44 words. |
| 290 | fn ot_aes_k128 k view uchar 32 rk edit ulong 240 s edit ulong 64 w edit ulong 64 to void |
| 291 | for i ulong 16 unroll |
| 292 | put rk i ulong get k i |
| 293 | end |
| 294 | for r ulong 10 unroll |
| 295 | for b ulong 4 unroll |
| 296 | put s b get rk add add mul r 16 12 and add b 1 3 |
| 297 | end |
| 298 | ot_aes_sb s w |
| 299 | put s 0 xor get s 0 xor xor and shl 1 r 255 mul 27 and shr r 3 xor and r 1 1 mul 54 and shr r 3 and r 1 |
| 300 | for b ulong 4 unroll |
| 301 | put rk add add mul r 16 16 b xor get rk add mul r 16 b get s b |
| 302 | end |
| 303 | for j ulong 3 unroll |
| 304 | for b ulong 4 unroll |
| 305 | put rk add add mul r 16 add 20 mul j 4 b xor get rk add add mul r 16 add 4 mul j 4 b get rk add add mul r 16 add 16 mul j 4 b |
| 306 | end |
| 307 | end |
| 308 | end |
| 309 | end |
| 310 | note AES-256: Nk = 8, 60 words. Rounds r = 0..5 make words 8r+8 .. 8r+15 (word 8r+12 is SubWord only); the |
| 311 | note last 4 words 56..59 follow with Rcon 0x40. |
| 312 | fn ot_aes_k256 k view uchar 32 rk edit ulong 240 s edit ulong 64 w edit ulong 64 to void |
| 313 | for i ulong 32 unroll |
| 314 | put rk i ulong get k i |
| 315 | end |
| 316 | for r ulong 6 unroll |
| 317 | for b ulong 4 unroll |
| 318 | put s b get rk add add mul r 32 28 and add b 1 3 |
| 319 | end |
| 320 | ot_aes_sb s w |
| 321 | put s 0 xor get s 0 xor xor and shl 1 r 255 mul 27 and shr r 3 xor and r 1 1 mul 54 and shr r 3 and r 1 |
| 322 | for b ulong 4 unroll |
| 323 | put rk add add mul r 32 32 b xor get rk add mul r 32 b get s b |
| 324 | end |
| 325 | for j ulong 3 unroll |
| 326 | for b ulong 4 unroll |
| 327 | put rk add add mul r 32 add 36 mul j 4 b xor get rk add add mul r 32 add 4 mul j 4 b get rk add add mul r 32 add 32 mul j 4 b |
| 328 | end |
| 329 | end |
| 330 | for b ulong 4 unroll |
| 331 | put s b get rk add add mul r 32 44 b |
| 332 | end |
| 333 | ot_aes_sb s w |
| 334 | for b ulong 4 unroll |
| 335 | put rk add add mul r 32 48 b xor get rk add add mul r 32 16 b get s b |
| 336 | end |
| 337 | for j ulong 3 unroll |
| 338 | for b ulong 4 unroll |
| 339 | put rk add add mul r 32 add 52 mul j 4 b xor get rk add add mul r 32 add 20 mul j 4 b get rk add add mul r 32 add 48 mul j 4 b |
| 340 | end |
| 341 | end |
| 342 | end |
| 343 | for b ulong 4 unroll |
| 344 | put s b get rk add 220 and add b 1 3 |
| 345 | end |
| 346 | ot_aes_sb s w |
| 347 | put s 0 xor get s 0 64 |
| 348 | for b ulong 4 unroll |
| 349 | put rk add 224 b xor get rk add 192 b get s b |
| 350 | end |
| 351 | for j ulong 3 unroll |
| 352 | for b ulong 4 unroll |
| 353 | put rk add add 228 mul j 4 b xor get rk add add 196 mul j 4 b get rk add add 224 mul j 4 b |
| 354 | end |
| 355 | end |
| 356 | end |
| 357 | note GHASH multiply (SP 800-38D 6.3, Algorithm 1): x = x * h in GF(2^128), x and h as two big-endian halves |
| 358 | note (word 0 holds bits 0..63, bit 0 the most significant). Each of the 128 steps selects by mask, no branch. |
| 359 | fn ot_gcm_mul x edit ulong 2 h view ulong 2 to void |
| 360 | v0 = get h 0 |
| 361 | v1 = get h 1 |
| 362 | z0 = ulong 0 |
| 363 | z1 = ulong 0 |
| 364 | a = get x 0 |
| 365 | for i ulong 64 unroll |
| 366 | m = neg and shr a sub 63 i 1 |
| 367 | z0 = xor z0 and v0 m |
| 368 | z1 = xor z1 and v1 m |
| 369 | r = neg and v1 1 |
| 370 | v1 = or shr v1 1 shl v0 63 |
| 371 | v0 = xor shr v0 1 and r 0xe100000000000000 |
| 372 | end |
| 373 | a = get x 1 |
| 374 | for i ulong 64 unroll |
| 375 | m = neg and shr a sub 63 i 1 |
| 376 | z0 = xor z0 and v0 m |
| 377 | z1 = xor z1 and v1 m |
| 378 | r = neg and v1 1 |
| 379 | v1 = or shr v1 1 shl v0 63 |
| 380 | v0 = xor shr v0 1 and r 0xe100000000000000 |
| 381 | end |
| 382 | put x 0 z0 |
| 383 | put x 1 z1 |
| 384 | end |