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 / aesgcm.om
| 1 | note omtls test driver aesgcm: AES-128/256 (FIPS 197 C.1/C.3 block vectors and an S-box check of all 256 inputs |
| 2 | note against a direct GF(2^8) reference), AES-GCM through ot_ae_seal / ot_ae_open (alg 1, 2) on the NIST CAVP |
| 3 | note gcmEncryptExtIV / gcmDecrypt files (the subset with 96-bit IV, 128-bit tag, 128/256-bit keys; other groups |
| 4 | note are skipped and not counted) and Wycheproof "W TCID V|I KEY IV AAD MSG CT TAG" (cases with a 12-byte IV and |
| 5 | note a 16- or 32-byte key; other IV and key sizes are skipped and not counted). Usage: aesgcm FILE... |
| 6 | note A line "S" in any file (tests/vectors/aesgcm/sbox-check.txt) runs the S-box check as one case. |
| 7 | text ot_syg_area "aesgcm" |
| 8 | text ot_syg_b "B" |
| 9 | text ot_syg_w "W" |
| 10 | text ot_syg_s "S" |
| 11 | text ot_syg_cnt "Count" |
| 12 | text ot_syg_key "Key" |
| 13 | text ot_syg_iv "IV" |
| 14 | text ot_syg_pt "PT" |
| 15 | text ot_syg_aad "AAD" |
| 16 | text ot_syg_ct "CT" |
| 17 | text ot_syg_tag "Tag" |
| 18 | text ot_syg_fail "FAIL" |
| 19 | text ot_syg_kl "[Keylen" |
| 20 | text ot_syg_ivl "[IVlen" |
| 21 | text ot_syg_tl "[Taglen" |
| 22 | note Reference S-box (public, slow, test only): inverse by search, then the FIPS 197 5.1.1 affine map. |
| 23 | fn ot_syg_gm a ulong b ulong to ulong |
| 24 | r = ulong 0 |
| 25 | x = a |
| 26 | y = b |
| 27 | for i ulong 8 |
| 28 | if ne and y 1 0 |
| 29 | r = xor r x |
| 30 | end |
| 31 | x = shl x 1 |
| 32 | if ne and x 256 0 |
| 33 | x = xor x 283 |
| 34 | end |
| 35 | y = shr y 1 |
| 36 | end |
| 37 | ret r |
| 38 | end |
| 39 | fn ot_syg_ref v ulong to ulong |
| 40 | inv = ulong 0 |
| 41 | for c ulong 256 |
| 42 | if eq ot_syg_gm v c 1 |
| 43 | inv = c |
| 44 | end |
| 45 | end |
| 46 | r = ulong 99 |
| 47 | for k ulong 8 |
| 48 | bit = xor shr inv k shr inv and add k 4 7 |
| 49 | bit = xor bit shr inv and add k 5 7 |
| 50 | bit = xor bit shr inv and add k 6 7 |
| 51 | bit = and xor bit shr inv and add k 7 7 1 |
| 52 | r = xor r shl bit k |
| 53 | end |
| 54 | ret r |
| 55 | end |
| 56 | fn ot_syg_sbox to bool |
| 57 | s = new ulong 64 0 |
| 58 | w = new ulong 64 0 |
| 59 | ok = true |
| 60 | for q ulong 4 |
| 61 | for i ulong 64 |
| 62 | put s i add mul q 64 i |
| 63 | end |
| 64 | ot_aes_sb s w |
| 65 | for i ulong 64 |
| 66 | if ne get s i ot_syg_ref add mul q 64 i |
| 67 | ok = false |
| 68 | end |
| 69 | end |
| 70 | end |
| 71 | ret ok |
| 72 | end |
| 73 | note B KEY PT CT (AES-192 records are skipped: st 9 = 1 means not counted). |
| 74 | fn ot_syg_blk b view uchar * st edit ulong 10 to bool |
| 75 | k = new uchar 32 0 |
| 76 | p = new uchar 16 0 |
| 77 | c = new uchar 16 0 |
| 78 | o = new uchar 16 0 |
| 79 | kl = ot_sy_nhex b st k |
| 80 | pl = ot_sy_nhex b st p |
| 81 | cl = ot_sy_nhex b st c |
| 82 | if eq kl 24 |
| 83 | put st 9 1 |
| 84 | ret true |
| 85 | end |
| 86 | if or ne pl 16 ne cl 16 |
| 87 | ret false |
| 88 | end |
| 89 | if eq kl 16 |
| 90 | ot_aes_enc1 false k p o |
| 91 | else |
| 92 | if ne kl 32 |
| 93 | ret false |
| 94 | end |
| 95 | ot_aes_enc1 true k p o |
| 96 | end |
| 97 | ret ot_sy_eq o c 16 |
| 98 | end |
| 99 | note The tail of a (from 49152) holds the record's key (64 bytes), IV (1024) and tag (64); aad stays below 49152. |
| 100 | note kn = key || IV (AES-128 keys use bytes 0..15). |
| 101 | fn ot_syg_kn a view uchar 65536 kn edit uchar 44 to void |
| 102 | for i ulong 32 |
| 103 | put kn i get a add 49152 i |
| 104 | end |
| 105 | for i ulong 12 |
| 106 | put kn add 32 i get a add 49216 i |
| 107 | end |
| 108 | end |
| 109 | note W TCID V|I KEY IV AAD MSG CT TAG. g: 0 key, 1 iv, 2 aad, 3 msg, 4 ct, 5 tag lengths, 6 valid. |
| 110 | fn ot_syg_wy b view uchar * st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool |
| 111 | g = new long 8 0 |
| 112 | n = ot_sy_tok b st |
| 113 | n = ot_sy_tok b st |
| 114 | put g 6 0 |
| 115 | if eq get b get st 6 86 |
| 116 | put g 6 1 |
| 117 | end |
| 118 | put g 0 ot_sy_nhex b st part a 49152 64 |
| 119 | put g 1 ot_sy_nhex b st part a 49216 1024 |
| 120 | put g 2 ot_sy_nhex b st part a 0 49152 |
| 121 | put g 3 ot_sy_nhex b st p |
| 122 | put g 4 ot_sy_nhex b st part c 0 65520 |
| 123 | if lt get g 4 0 |
| 124 | ret false |
| 125 | end |
| 126 | cl = ulong get g 4 |
| 127 | put g 5 ot_sy_nhex b st part c cl 16 |
| 128 | put g 7 get g 6 |
| 129 | sk = and ne get g 0 16 ne get g 0 32 |
| 130 | if or sk ne get g 1 12 |
| 131 | put st 9 1 |
| 132 | ret true |
| 133 | end |
| 134 | ret ot_syg_go a p c g |
| 135 | end |
| 136 | note Seal/open one AES-GCM case: g as above; g 7 1 adds the derived negative checks. |
| 137 | fn ot_syg_go a edit uchar 65536 p view uchar 65536 c view uchar 65536 g view long 8 to bool |
| 138 | kn = new uchar 44 0 |
| 139 | f = new ulong 8 0 |
| 140 | bad = or lt get g 2 0 lt get g 3 0 |
| 141 | bad = or bad or lt get g 4 0 ne get g 5 16 |
| 142 | if bad |
| 143 | ret false |
| 144 | end |
| 145 | ot_syg_kn a kn |
| 146 | put f 0 ulong get g 2 |
| 147 | put f 1 ulong get g 3 |
| 148 | put f 2 add ulong get g 4 16 |
| 149 | put f 3 ulong get g 6 |
| 150 | put f 4 ulong get g 7 |
| 151 | alg = ulong 1 |
| 152 | if eq get g 0 32 |
| 153 | alg = 2 |
| 154 | end |
| 155 | ret ot_sye_run alg kn a p c f |
| 156 | end |
| 157 | note CAVP state, kept as 8-byte little-endian values in a from 51200 (test metadata only): 0 Keylen, 1 IVlen, |
| 158 | note 2 Taglen (bits), 3 PT seen, 4 Tag seen, 5 key, 6 iv, 7 pt, 8 aad, 9 ct, 10 tag byte lengths, 11 FAIL record. |
| 159 | fn ot_syg_cg a view uchar 65536 i ulong to long |
| 160 | v = ulong 0 |
| 161 | o = add 51200 mul i 8 |
| 162 | for j ulong 8 |
| 163 | x = ulong get a add o j |
| 164 | v = or v shl x mul j 8 |
| 165 | end |
| 166 | ret bitcast long v |
| 167 | end |
| 168 | fn ot_syg_cp a edit uchar 65536 i ulong v long to void |
| 169 | u = bitcast ulong v |
| 170 | o = add 51200 mul i 8 |
| 171 | for j ulong 8 |
| 172 | x = and shr u mul j 8 255 |
| 173 | put a add o j uchar x |
| 174 | end |
| 175 | end |
| 176 | note The value after "NAME =" as hex into o; an empty value is 0 bytes. |
| 177 | fn ot_syg_val b view uchar * st edit ulong 10 o edit uchar * to long |
| 178 | n = ot_sy_tok b st |
| 179 | r = ot_sy_nhex b st o |
| 180 | if eq r -3 |
| 181 | ret 0 |
| 182 | end |
| 183 | ret r |
| 184 | end |
| 185 | fn ot_syg_hdr b view uchar * st edit ulong 10 a edit uchar 65536 to void |
| 186 | k = ulong 9 |
| 187 | if ot_sy_is b st ot_syg_kl |
| 188 | k = 0 |
| 189 | end |
| 190 | if ot_sy_is b st ot_syg_ivl |
| 191 | k = 1 |
| 192 | end |
| 193 | if ot_sy_is b st ot_syg_tl |
| 194 | k = 2 |
| 195 | end |
| 196 | n = ot_sy_tok b st |
| 197 | v = long ot_sy_ndec b st |
| 198 | if lt k 3 |
| 199 | ot_syg_cp a k v |
| 200 | end |
| 201 | end |
| 202 | note Evaluate a complete CAVP record (subset: 128/256-bit key, 96-bit IV, 128-bit tag; else not counted). |
| 203 | fn ot_syg_cav st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool |
| 204 | g = new long 8 0 |
| 205 | kl = ot_syg_cg a 0 |
| 206 | if or ne ot_syg_cg a 1 96 or ne ot_syg_cg a 2 128 and ne kl 128 ne kl 256 |
| 207 | put st 9 1 |
| 208 | ret true |
| 209 | end |
| 210 | put g 0 ot_syg_cg a 5 |
| 211 | put g 1 ot_syg_cg a 6 |
| 212 | put g 2 ot_syg_cg a 8 |
| 213 | put g 4 ot_syg_cg a 9 |
| 214 | put g 3 get g 4 |
| 215 | if eq ot_syg_cg a 11 0 |
| 216 | put g 3 ot_syg_cg a 7 |
| 217 | put g 6 1 |
| 218 | end |
| 219 | put g 5 ot_syg_cg a 10 |
| 220 | if or lt get g 4 0 gt get g 4 65520 |
| 221 | ret false |
| 222 | end |
| 223 | cl = ulong get g 4 |
| 224 | for i ulong 16 |
| 225 | put c add cl i get a add 50240 i |
| 226 | end |
| 227 | ret ot_syg_go a p c g |
| 228 | end |
| 229 | note One line. Gives true when it closed a case; st 9 = 1 for a case that is not counted. |
| 230 | fn ot_syg_line b view uchar * st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool |
| 231 | if ot_sy_is b st ot_syg_s |
| 232 | ret ot_syg_sbox |
| 233 | end |
| 234 | if ot_sy_is b st ot_syg_b |
| 235 | ret ot_syg_blk b st |
| 236 | end |
| 237 | if ot_sy_is b st ot_syg_w |
| 238 | ret ot_syg_wy b st a p c |
| 239 | end |
| 240 | put st 9 1 |
| 241 | if eq get b get st 6 91 |
| 242 | ot_syg_hdr b st a |
| 243 | ret true |
| 244 | end |
| 245 | if ot_sy_is b st ot_syg_cnt |
| 246 | ot_syg_cp a 3 0 |
| 247 | ot_syg_cp a 4 0 |
| 248 | ot_syg_cp a 11 0 |
| 249 | ret true |
| 250 | end |
| 251 | if ot_sy_is b st ot_syg_key |
| 252 | ot_syg_cp a 5 ot_syg_val b st part a 49152 64 |
| 253 | ret true |
| 254 | end |
| 255 | if ot_sy_is b st ot_syg_iv |
| 256 | ot_syg_cp a 6 ot_syg_val b st part a 49216 1024 |
| 257 | ret true |
| 258 | end |
| 259 | if ot_sy_is b st ot_syg_aad |
| 260 | ot_syg_cp a 8 ot_syg_val b st part a 0 49152 |
| 261 | ret true |
| 262 | end |
| 263 | if ot_sy_is b st ot_syg_ct |
| 264 | ot_syg_cp a 9 ot_syg_val b st part c 0 65520 |
| 265 | ret true |
| 266 | end |
| 267 | if ot_sy_is b st ot_syg_pt |
| 268 | ot_syg_cp a 7 ot_syg_val b st p |
| 269 | ot_syg_cp a 3 1 |
| 270 | if eq ot_syg_cg a 4 1 |
| 271 | put st 9 0 |
| 272 | ret ot_syg_cav st a p c |
| 273 | end |
| 274 | ret true |
| 275 | end |
| 276 | if ot_sy_is b st ot_syg_tag |
| 277 | ot_syg_cp a 10 ot_syg_val b st part a 50240 64 |
| 278 | ot_syg_cp a 4 1 |
| 279 | if eq ot_syg_cg a 3 1 |
| 280 | put st 9 0 |
| 281 | ret ot_syg_cav st a p c |
| 282 | end |
| 283 | ret true |
| 284 | end |
| 285 | if ot_sy_is b st ot_syg_fail |
| 286 | ot_syg_cp a 11 1 |
| 287 | put st 9 0 |
| 288 | ret ot_syg_cav st a p c |
| 289 | end |
| 290 | ret true |
| 291 | end |
| 292 | fn ot_syg_file fd int b edit uchar * r edit ulong 4 to void |
| 293 | st = new ulong 10 0 |
| 294 | a = new uchar 65536 0 |
| 295 | p = new uchar 65536 0 |
| 296 | c = new uchar 65536 0 |
| 297 | while ot_sy_line fd b st bound 9223372036854775807 |
| 298 | n = ot_sy_tok b st |
| 299 | if gt n 0 |
| 300 | if ne get b get st 6 35 |
| 301 | put st 9 0 |
| 302 | ok = ot_syg_line b st a p c |
| 303 | if eq get st 9 0 |
| 304 | ot_sy_res r ok ot_syg_area st |
| 305 | end |
| 306 | end |
| 307 | end |
| 308 | end |
| 309 | if eq get st 2 2 |
| 310 | put r 1 add get r 1 1 |
| 311 | end |
| 312 | end |
| 313 | fn main to int |
| 314 | b = new uchar 1048576 0 |
| 315 | pth = new uchar 4096 0 |
| 316 | r = new ulong 4 0 |
| 317 | st = new ulong 10 0 |
| 318 | na = argcnt |
| 319 | i = ulong 1 |
| 320 | while lt i ulong na bound 4096 |
| 321 | fd = ot_sy_open i pth |
| 322 | if lt fd 0 |
| 323 | put r 1 add get r 1 1 |
| 324 | ot_sy_bad ot_syg_area i st |
| 325 | else |
| 326 | ot_syg_file fd b r |
| 327 | cl = fclose fd |
| 328 | end |
| 329 | i = add i 1 |
| 330 | end |
| 331 | ret ot_sy_done ot_syg_area get r 0 get r 1 |
| 332 | end |