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
omtls / tests / drv / chacha.om
| 1 | note omtls test driver chacha: ChaCha20 (quarter round, block, stream), Poly1305, the Poly1305 key, and |
| 2 | note AEAD_CHACHA20_POLY1305 through ot_ae_seal / ot_ae_open (alg 3). |
| 3 | note Records: tests/vectors/chacha/rfc8439-sym.txt (Q B E P K D, see its header) and Wycheproof |
| 4 | note "A KN AAD MSG CT||TAG CODE" (code 0 valid, 1 invalid, 2 nonce not 12 bytes: the API takes only a 12-byte |
| 5 | note nonce, so such a case cannot be expressed and counts as refused). Usage: chacha FILE... |
| 6 | text ot_syc_area "chacha" |
| 7 | text ot_syc_q "Q" |
| 8 | text ot_syc_b "B" |
| 9 | text ot_syc_e "E" |
| 10 | text ot_syc_p "P" |
| 11 | text ot_syc_k "K" |
| 12 | text ot_syc_d "D" |
| 13 | text ot_syc_a "A" |
| 14 | fn ot_syc_w b view uchar * st edit ulong 10 to uint |
| 15 | t = new uchar 4 0 |
| 16 | n = ot_sy_nhex b st t |
| 17 | v = shl uint get t 0 24 |
| 18 | v = or v shl uint get t 1 16 |
| 19 | v = or v shl uint get t 2 8 |
| 20 | ret or v uint get t 3 |
| 21 | end |
| 22 | fn ot_syc_qr b view uchar * st edit ulong 10 to bool |
| 23 | q = new uint 4 0 |
| 24 | for i ulong 4 unroll |
| 25 | put q i ot_syc_w b st |
| 26 | end |
| 27 | ot_cc_qr q |
| 28 | ok = true |
| 29 | for i ulong 4 unroll |
| 30 | ok = and ok eq get q i ot_syc_w b st |
| 31 | end |
| 32 | ret ok |
| 33 | end |
| 34 | note B KN CTR BLOCK |
| 35 | fn ot_syc_bk b view uchar * st edit ulong 10 to bool |
| 36 | kn = new uchar 44 0 |
| 37 | o = new uchar 64 0 |
| 38 | ex = new uchar 64 0 |
| 39 | n = ot_sy_nhex b st kn |
| 40 | c = uint ot_sy_ndec b st |
| 41 | m = ot_sy_nhex b st ex |
| 42 | ot_cc_block kn c o |
| 43 | ret and eq n 44 and eq m 64 ot_sy_eq o ex 64 |
| 44 | end |
| 45 | note E KN CTR PT CT |
| 46 | fn ot_syc_en b view uchar * st edit ulong 10 p edit uchar 65536 c edit uchar 65536 to bool |
| 47 | kn = new uchar 44 0 |
| 48 | n = ot_sy_nhex b st kn |
| 49 | ctr = uint ot_sy_ndec b st |
| 50 | pn = ot_sy_nhex b st p |
| 51 | cn = ot_sy_nhex b st c |
| 52 | if or ne n 44 or lt pn 0 ne pn cn |
| 53 | ret false |
| 54 | end |
| 55 | ot_cc_xor kn ctr part p 0 ulong pn |
| 56 | ret ot_sy_eq p c ulong pn |
| 57 | end |
| 58 | note P KEY MSG TAG |
| 59 | fn ot_syc_po b view uchar * st edit ulong 10 p edit uchar 65536 to bool |
| 60 | k = new uchar 32 0 |
| 61 | t = new uchar 16 0 |
| 62 | ex = new uchar 16 0 |
| 63 | n = ot_sy_nhex b st k |
| 64 | pn = ot_sy_nhex b st p |
| 65 | m = ot_sy_nhex b st ex |
| 66 | if or ne n 32 or lt pn 0 ne m 16 |
| 67 | ret false |
| 68 | end |
| 69 | ot_p1_mac k view part p 0 ulong pn t |
| 70 | ret ot_sy_eq t ex 16 |
| 71 | end |
| 72 | note K KN OTK |
| 73 | fn ot_syc_ok b view uchar * st edit ulong 10 to bool |
| 74 | kn = new uchar 44 0 |
| 75 | o = new uchar 32 0 |
| 76 | ex = new uchar 32 0 |
| 77 | n = ot_sy_nhex b st kn |
| 78 | m = ot_sy_nhex b st ex |
| 79 | ot_cc_pk kn o |
| 80 | ret and eq n 44 and eq m 32 ot_sy_eq o ex 32 |
| 81 | end |
| 82 | note D KN AAD PT CT TAG (RFC) or A KN AAD MSG CT||TAG CODE (Wycheproof): fields into a, p, c and f. |
| 83 | fn ot_syc_ae b view uchar * st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool |
| 84 | kn = new uchar 44 0 |
| 85 | g = new long 8 0 |
| 86 | put g 0 ot_sy_nhex b st kn |
| 87 | put g 1 ot_sy_nhex b st a |
| 88 | put g 2 ot_sy_nhex b st p |
| 89 | put g 3 ot_sy_nhex b st c |
| 90 | if eq get st 9 1 |
| 91 | if or lt get g 3 0 gt get g 3 65520 |
| 92 | ret false |
| 93 | end |
| 94 | cl = ulong get g 3 |
| 95 | put g 4 ot_sy_nhex b st part c cl 16 |
| 96 | if ne get g 4 16 |
| 97 | ret false |
| 98 | end |
| 99 | put g 3 add get g 3 16 |
| 100 | put g 5 0 |
| 101 | else |
| 102 | put g 4 0 |
| 103 | put g 5 long ot_sy_ndec b st |
| 104 | end |
| 105 | ret ot_syc_ae2 kn a p c g |
| 106 | end |
| 107 | note g: 0 KN length, 1 aad, 2 pt, 3 ct with its tag, 4 the separate tag length (D records), 5 Wycheproof code. |
| 108 | fn ot_syc_ae2 kn view uchar 44 a edit uchar 65536 p view uchar 65536 c edit uchar 65536 g view long 8 to bool |
| 109 | f = new ulong 8 0 |
| 110 | if eq get g 5 2 |
| 111 | ret ne get g 0 44 |
| 112 | end |
| 113 | bad = or lt get g 1 0 lt get g 2 0 |
| 114 | bad = or bad or lt get g 3 0 lt get g 4 0 |
| 115 | if or bad ne get g 0 44 |
| 116 | ret false |
| 117 | end |
| 118 | put f 0 ulong get g 1 |
| 119 | put f 1 ulong get g 2 |
| 120 | put f 2 ulong get g 3 |
| 121 | put f 3 1 |
| 122 | if eq get g 5 1 |
| 123 | put f 3 0 |
| 124 | end |
| 125 | put f 4 1 |
| 126 | ret ot_sye_run 3 kn a p c f |
| 127 | end |
| 128 | fn ot_syc_rec b view uchar * st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool |
| 129 | if ot_sy_is b st ot_syc_q |
| 130 | ret ot_syc_qr b st |
| 131 | end |
| 132 | if ot_sy_is b st ot_syc_b |
| 133 | ret ot_syc_bk b st |
| 134 | end |
| 135 | if ot_sy_is b st ot_syc_e |
| 136 | ret ot_syc_en b st p c |
| 137 | end |
| 138 | if ot_sy_is b st ot_syc_p |
| 139 | ret ot_syc_po b st p |
| 140 | end |
| 141 | if ot_sy_is b st ot_syc_k |
| 142 | ret ot_syc_ok b st |
| 143 | end |
| 144 | put st 9 0 |
| 145 | if ot_sy_is b st ot_syc_d |
| 146 | put st 9 1 |
| 147 | end |
| 148 | ret ot_syc_ae b st a p c |
| 149 | end |
| 150 | fn ot_syc_kind b view uchar * st view ulong 10 to bool |
| 151 | r = or or ot_sy_is b st ot_syc_q ot_sy_is b st ot_syc_b ot_sy_is b st ot_syc_e |
| 152 | r = or r or ot_sy_is b st ot_syc_p ot_sy_is b st ot_syc_k |
| 153 | ret or r or ot_sy_is b st ot_syc_d ot_sy_is b st ot_syc_a |
| 154 | end |
| 155 | fn ot_syc_file fd int b edit uchar * r edit ulong 4 to void |
| 156 | st = new ulong 10 0 |
| 157 | a = new uchar 65536 0 |
| 158 | p = new uchar 65536 0 |
| 159 | c = new uchar 65536 0 |
| 160 | while ot_sy_line fd b st bound 9223372036854775807 |
| 161 | n = ot_sy_tok b st |
| 162 | if ot_syc_kind b st |
| 163 | ot_sy_res r ot_syc_rec b st a p c ot_syc_area st |
| 164 | end |
| 165 | end |
| 166 | if eq get st 2 2 |
| 167 | put r 1 add get r 1 1 |
| 168 | end |
| 169 | end |
| 170 | fn main to int |
| 171 | b = new uchar 1048576 0 |
| 172 | pth = new uchar 4096 0 |
| 173 | r = new ulong 4 0 |
| 174 | st = new ulong 10 0 |
| 175 | na = argcnt |
| 176 | i = ulong 1 |
| 177 | while lt i ulong na bound 4096 |
| 178 | fd = ot_sy_open i pth |
| 179 | if lt fd 0 |
| 180 | put r 1 add get r 1 1 |
| 181 | ot_sy_bad ot_syc_area i st |
| 182 | else |
| 183 | ot_syc_file fd b r |
| 184 | cl = fclose fd |
| 185 | end |
| 186 | i = add i 1 |
| 187 | end |
| 188 | ret ot_sy_done ot_syc_area get r 0 get r 1 |
| 189 | end |