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 HMAC (RFC 2104) over SHA-256 (alg 0, vendored), SHA-384 (1), SHA-512 (2). |
| 2 | note Internal helpers keep two contexts (c5 for SHA-384/512, c2 for SHA-256) and dispatch on the public alg. |
| 3 | fn ot_hm_bs alg ulong to ulong |
| 4 | if eq alg 0 |
| 5 | ret 64 |
| 6 | end |
| 7 | if or eq alg 1 eq alg 2 |
| 8 | ret 128 |
| 9 | end |
| 10 | ret 0 |
| 11 | end |
| 12 | fn ot_hm_hl alg ulong to ulong |
| 13 | if eq alg 0 |
| 14 | ret 32 |
| 15 | end |
| 16 | if eq alg 1 |
| 17 | ret 48 |
| 18 | end |
| 19 | if eq alg 2 |
| 20 | ret 64 |
| 21 | end |
| 22 | ret 0 |
| 23 | end |
| 24 | fn ot_hm_hi alg ulong c5 edit ulong 32 c2 edit uchar 104 to void |
| 25 | if eq alg 0 |
| 26 | sha256_init c2 |
| 27 | else |
| 28 | ot_s5_init c5 eq alg 1 |
| 29 | end |
| 30 | end |
| 31 | fn ot_hm_hu alg ulong c5 edit ulong 32 c2 edit uchar 104 d view uchar * to void |
| 32 | if eq alg 0 |
| 33 | sha256_update c2 d |
| 34 | else |
| 35 | ot_s5_update c5 d |
| 36 | end |
| 37 | end |
| 38 | fn ot_hm_hf alg ulong c5 edit ulong 32 c2 edit uchar 104 o edit uchar 64 to void |
| 39 | t = new uchar 32 0 |
| 40 | if eq alg 0 |
| 41 | sha256_final c2 t |
| 42 | for i ulong 32 unroll |
| 43 | put o i get t i |
| 44 | put t i 0 |
| 45 | put o add 32 i 0 |
| 46 | end |
| 47 | else |
| 48 | ot_s5_final c5 o |
| 49 | end |
| 50 | end |
| 51 | fn ot_hm_zc c5 edit ulong 32 c2 edit uchar 104 to void |
| 52 | for i ulong 32 unroll |
| 53 | put c5 i 0 |
| 54 | end |
| 55 | for i ulong 104 |
| 56 | put c2 i 0 |
| 57 | end |
| 58 | end |
| 59 | note The key block: the key, or its hash when longer than the block size, zero-padded to 128 bytes. |
| 60 | fn ot_hm_kb alg ulong key view uchar * kb edit uchar 128 to void |
| 61 | for i ulong 128 |
| 62 | put kb i 0 |
| 63 | end |
| 64 | n = len key |
| 65 | t = new uchar 64 0 |
| 66 | if gt n ot_hm_bs alg |
| 67 | hl = ot_hash alg key t |
| 68 | for i ulong 64 unroll |
| 69 | put kb i get t i |
| 70 | put t i 0 |
| 71 | end |
| 72 | ret |
| 73 | end |
| 74 | for i ulong n |
| 75 | put kb i get key i |
| 76 | end |
| 77 | end |
| 78 | note Start the inner hash: H(K xor ipad || ...). |
| 79 | fn ot_hm_beg alg ulong kb view uchar 128 c5 edit ulong 32 c2 edit uchar 104 to void |
| 80 | pad = new uchar 128 0 |
| 81 | for i ulong 128 |
| 82 | put pad i xor get kb i 54 |
| 83 | end |
| 84 | ot_hm_hi alg c5 c2 |
| 85 | ot_hm_hu alg c5 c2 view part pad 0 ot_hm_bs alg |
| 86 | for i ulong 128 |
| 87 | put pad i 0 |
| 88 | end |
| 89 | end |
| 90 | note Finish: inner digest, then H(K xor opad || inner) into o (bytes past the digest length zero). |
| 91 | fn ot_hm_end alg ulong kb view uchar 128 c5 edit ulong 32 c2 edit uchar 104 o edit uchar 64 to void |
| 92 | pad = new uchar 128 0 |
| 93 | ib = new uchar 64 0 |
| 94 | ot_hm_hf alg c5 c2 ib |
| 95 | for i ulong 128 |
| 96 | put pad i xor get kb i 92 |
| 97 | end |
| 98 | ot_hm_hi alg c5 c2 |
| 99 | ot_hm_hu alg c5 c2 view part pad 0 ot_hm_bs alg |
| 100 | ot_hm_hu alg c5 c2 view part ib 0 ot_hm_hl alg |
| 101 | ot_hm_hf alg c5 c2 o |
| 102 | for i ulong 128 |
| 103 | put pad i 0 |
| 104 | end |
| 105 | for i ulong 64 unroll |
| 106 | put ib i 0 |
| 107 | end |
| 108 | ot_hm_zc c5 c2 |
| 109 | end |
| 110 | note HMAC(key, msg) into o; gives the tag length (32/48/64), or 0 with o all zero for an unknown alg. |
| 111 | export fn ot_hm_mac alg ulong key view uchar * msg view uchar * o edit uchar 64 to ulong |
| 112 | hl = ot_hm_hl alg |
| 113 | kb = new uchar 128 0 |
| 114 | c5 = new ulong 32 0 |
| 115 | c2 = new uchar 104 0 |
| 116 | for i ulong 64 unroll |
| 117 | put o i 0 |
| 118 | end |
| 119 | if eq hl 0 |
| 120 | ret 0 |
| 121 | end |
| 122 | ot_hm_kb alg key kb |
| 123 | ot_hm_beg alg kb c5 c2 |
| 124 | ot_hm_hu alg c5 c2 msg |
| 125 | ot_hm_end alg kb c5 c2 o |
| 126 | for i ulong 128 |
| 127 | put kb i 0 |
| 128 | end |
| 129 | ret hl |
| 130 | end |