omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / acvp / acvp.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note acvp.om: ACVP response engine for the om/tls library. Build (run.sh does this): the program is |
| 3 | note acvpio.om || acvp.om || acvpmain.om, composed after the library by sh om/tls/build.sh OMC PROG OUT. |
| 4 | note It computes, it does not grade: every record is answered with the library's own result and the |
| 5 | note comparison against NIST's expectedResults.json happens afterwards (om2acvp.py). Exception: the |
| 6 | note KDA validation (VAL) records, where the IUT itself must say whether a supplied DKM is right, and |
| 7 | note the AES-GCM decrypt records, where the library's tag check decides; both decisions are made here. |
| 8 | note Input on stdin (at most 16 MiB), one record per line, fields separated by one space, byte strings |
| 9 | note in hex ("-" = empty), ID a decimal record number echoed as the first output field: |
| 10 | note H ID alg MSG sha2_digest -> ID MD |
| 11 | note M ID alg ver SEED SHA-2 Monte Carlo, ver 0 standard, 1 alternate (ACVP SHA |
| 12 | note spec / ACVP-Server StandardSizeShaMct, AlternateSizeShaMct), |
| 13 | note 100 outer rounds of 1000 -> ID MD1 .. MD100 |
| 14 | note L ID alg CONTENT NBYTES large-data test: CONTENT repeated to NBYTES bytes, streamed |
| 15 | note through sha2_update in slices of whole CONTENT copies of at |
| 16 | note most 512 KiB -> ID MD |
| 17 | note T ID alg KEY MSG MACBYTES hmac_init/update/final, first MACBYTES bytes -> ID MAC |
| 18 | note G ID KEY IV AAD PT TAGBYTES aes_gcm_seal -> ID RC CT TAG |
| 19 | note D ID KEY IV AAD CT TAG aes_gcm_open -> ID RC PT (PT "-" unless RC 0) |
| 20 | note E ID crv alg MSG PUB SIG hash MSG with alg, ecdsa_verify_raw crv (PUB = 04||X||Y, |
| 21 | note SIG = r||s) -> ID RC |
| 22 | note R ID alg KEY MSG SIG hash MSG, rsa_verify_pkcs1 (KEY = DER RSAPublicKey) -> ID RC |
| 23 | note K ID alg SALT IKM N FI1 L1 .. FIN LN hkdf_extract once, hkdf_expand per (FI, L bytes) |
| 24 | note -> ID DKM1 .. DKMN OK (1 when every hkdf_expand returned 0) |
| 25 | note V ID alg SALT IKM N FI1 L1 DKM1 .. as K, then the DKMs are compared here -> ID OK (1/0) |
| 26 | note S ID alg PSK DHE CH SH SF CF RFC 8446 7.1 key schedule from hkdf_extract/hkdf_expand and |
| 27 | note sha2_digest ("-" PSK or DHE = Hash.length zero bytes) |
| 28 | note -> ID CETS EEMS CHTS SHTS CATS SATS EMS RMS |
| 29 | note N ID PSK DHE CH SH SF the same through tls.om's own tls_xtr/tls_drv (SHA-256, 32-byte |
| 30 | note PSK and DHE only) -> ID RC CHTS SHTS CATS SATS |
| 31 | note X ID PRIV PEER x25519_base, x25519_scalarmult -> ID RC1 PUB RC2 Z |
| 32 | note B ID KEY PT AES ECB encryption (aes_setkey, aes_enc4) -> ID CT |
| 33 | note C ID KEY PT AES ECB encrypt Monte Carlo (ACVP AES MCT) |
| 34 | note -> ID (KEY PT CT) x 100 |
| 35 | note A malformed record prints "ID BAD" (or "0 BAD" before the ID is read). Exit 0 when no record |
| 36 | note was malformed, 1 otherwise, 2 when stdin cannot be read or exceeds 16 MiB. |
| 37 | |
| 38 | text S13CET "c e traffic" |
| 39 | text S13EEM "e exp master" |
| 40 | text S13EXP "exp master" |
| 41 | text S13RES "res master" |
| 42 | |
| 43 | note digest length of alg, 0 (and the record malformed) when alg is not 256, 384 or 512 |
| 44 | fn drdl alg ulong to ulong |
| 45 | if eq alg 256 |
| 46 | ret 32 |
| 47 | end |
| 48 | if eq alg 384 |
| 49 | ret 48 |
| 50 | end |
| 51 | if eq alg 512 |
| 52 | ret 64 |
| 53 | end |
| 54 | drbad |
| 55 | ret 0 |
| 56 | end |
| 57 | |
| 58 | note ---- SHA-2 ---- |
| 59 | |
| 60 | fn rech a edit uchar * to void |
| 61 | dg = new uchar 64 0 |
| 62 | drid |
| 63 | alg = drnum |
| 64 | dl = drdl alg |
| 65 | n = drhex a |
| 66 | dreor |
| 67 | if isbad |
| 68 | ret |
| 69 | end |
| 70 | k = sha2_digest alg part a 0 n dg |
| 71 | ohex part dg 0 k |
| 72 | end |
| 73 | |
| 74 | note m = x0 || x1 || x2 (x slot j at 1024 j, length q[j]); ver q[4] 1: m cut or zero-padded to q[3] |
| 75 | fn mcmsg m edit uchar * x view uchar 3072 q view ulong 8 to ulong |
| 76 | n = ulong 0 |
| 77 | for j ulong 3 |
| 78 | for i ulong get q j |
| 79 | put m n get x add mul j 1024 i |
| 80 | n = add n 1 |
| 81 | end |
| 82 | end |
| 83 | if eq get q 4 1 |
| 84 | sl = get q 3 |
| 85 | while lt n sl bound 4096 |
| 86 | put m n 0 |
| 87 | n = add n 1 |
| 88 | end |
| 89 | n = sl |
| 90 | end |
| 91 | ret n |
| 92 | end |
| 93 | |
| 94 | note x0 = x1, x1 = x2, x2 = dg[0..dl) |
| 95 | fn mcshift x edit uchar 3072 q edit ulong 8 dg view uchar 64 dl ulong to void |
| 96 | for j ulong 2 |
| 97 | put q j get q add j 1 |
| 98 | for i ulong get q j |
| 99 | put x add mul j 1024 i get x add mul add j 1 1024 i |
| 100 | end |
| 101 | end |
| 102 | put q 2 dl |
| 103 | for i ulong dl |
| 104 | put x add 2048 i get dg i |
| 105 | end |
| 106 | end |
| 107 | |
| 108 | note x1 = x2 = x0, all of length n |
| 109 | fn mcseed x edit uchar 3072 q edit ulong 8 n ulong to void |
| 110 | for i ulong n |
| 111 | put x add 1024 i get x i |
| 112 | put x add 2048 i get x i |
| 113 | end |
| 114 | put q 0 n |
| 115 | put q 1 n |
| 116 | put q 2 n |
| 117 | end |
| 118 | |
| 119 | fn recm a edit uchar * to void |
| 120 | x = new uchar 3072 0 |
| 121 | q = new ulong 8 0 |
| 122 | dg = new uchar 64 0 |
| 123 | drid |
| 124 | alg = drnum |
| 125 | dl = drdl alg |
| 126 | ver = drnum |
| 127 | sl = drhex part x 0 1024 |
| 128 | dreor |
| 129 | if gt ver 1 |
| 130 | drbad |
| 131 | end |
| 132 | if eq ver 0 |
| 133 | if ne sl dl |
| 134 | drbad |
| 135 | end |
| 136 | end |
| 137 | if isbad |
| 138 | ret |
| 139 | end |
| 140 | put q 3 sl |
| 141 | put q 4 ver |
| 142 | mcseed x q sl |
| 143 | for j ulong 100 |
| 144 | for c ulong 1000 |
| 145 | n = mcmsg a x q |
| 146 | k = sha2_digest alg part a 0 n dg |
| 147 | mcshift x q dg dl |
| 148 | end |
| 149 | ohex part dg 0 dl |
| 150 | for i ulong dl |
| 151 | put x i get dg i |
| 152 | end |
| 153 | mcseed x q dl |
| 154 | end |
| 155 | end |
| 156 | |
| 157 | fn recl a edit uchar * to void |
| 158 | c = new ulong 40 0 |
| 159 | dg = new uchar 64 0 |
| 160 | drid |
| 161 | alg = drnum |
| 162 | dl = drdl alg |
| 163 | cl = drhex part a 0 4096 |
| 164 | full = drnum |
| 165 | dreor |
| 166 | if eq cl 0 |
| 167 | drbad |
| 168 | end |
| 169 | if isbad |
| 170 | ret |
| 171 | end |
| 172 | bn = mul div 524288 cl cl |
| 173 | for i ulong sub bn cl |
| 174 | put a add cl i get a i |
| 175 | end |
| 176 | sha2_init c alg |
| 177 | q = ulong 0 |
| 178 | while lt q full bound 999999 |
| 179 | k = bn |
| 180 | if gt k sub full q |
| 181 | k = sub full q |
| 182 | end |
| 183 | sha2_update c part a 0 k |
| 184 | q = add q k |
| 185 | end |
| 186 | k = sha2_fin c dg |
| 187 | ohex part dg 0 k |
| 188 | end |
| 189 | |
| 190 | note ---- HMAC ---- |
| 191 | |
| 192 | fn rect a edit uchar * b edit uchar * to void |
| 193 | c = new ulong 96 0 |
| 194 | dg = new uchar 64 0 |
| 195 | drid |
| 196 | alg = drnum |
| 197 | dl = drdl alg |
| 198 | kl = drhex a |
| 199 | ml = drhex b |
| 200 | tl = drnum |
| 201 | dreor |
| 202 | if gt tl dl |
| 203 | drbad |
| 204 | end |
| 205 | if isbad |
| 206 | ret |
| 207 | end |
| 208 | hmac_init alg c part a 0 kl |
| 209 | hmac_update c part b 0 ml |
| 210 | k = hmac_final c dg |
| 211 | ohex part dg 0 tl |
| 212 | end |
| 213 | |
| 214 | note ---- AES-GCM ---- |
| 215 | |
| 216 | note kia = KEY || IV || AAD into a; lengths into q[0..3) |
| 217 | fn gckia a edit uchar * q edit ulong 8 to void |
| 218 | put q 0 drhex a |
| 219 | k = get q 0 |
| 220 | put q 1 drhex part a k sub len a k |
| 221 | k = add k get q 1 |
| 222 | put q 2 drhex part a k sub len a k |
| 223 | end |
| 224 | |
| 225 | fn recg a edit uchar * b edit uchar * to void |
| 226 | q = new ulong 8 0 |
| 227 | drid |
| 228 | gckia a q |
| 229 | pl = drhex b |
| 230 | tl = drnum |
| 231 | dreor |
| 232 | if gt tl 255 |
| 233 | drbad |
| 234 | end |
| 235 | if isbad |
| 236 | ret |
| 237 | end |
| 238 | kia = add add get q 0 get q 1 get q 2 |
| 239 | ctl = add add get q 0 shl tl 8 shl get q 1 16 |
| 240 | rc = aes_gcm_seal ctl part a 0 kia part b 0 add pl tl |
| 241 | ofld rc |
| 242 | if ne rc 0 |
| 243 | ohex part b 0 0 |
| 244 | ohex part b 0 0 |
| 245 | ret |
| 246 | end |
| 247 | ohex part b 0 pl |
| 248 | ohex part b pl tl |
| 249 | end |
| 250 | |
| 251 | fn recd a edit uchar * b edit uchar * to void |
| 252 | q = new ulong 8 0 |
| 253 | drid |
| 254 | gckia a q |
| 255 | cl = drhex b |
| 256 | tl = drhex part b cl sub len b cl |
| 257 | dreor |
| 258 | if gt tl 255 |
| 259 | drbad |
| 260 | end |
| 261 | if isbad |
| 262 | ret |
| 263 | end |
| 264 | kia = add add get q 0 get q 1 get q 2 |
| 265 | ctl = add add get q 0 shl tl 8 shl get q 1 16 |
| 266 | rc = aes_gcm_open ctl part a 0 kia part b 0 add cl tl |
| 267 | ofld rc |
| 268 | if ne rc 0 |
| 269 | ohex part b 0 0 |
| 270 | ret |
| 271 | end |
| 272 | ohex part b 0 cl |
| 273 | end |
| 274 | |
| 275 | note ---- signatures ---- |
| 276 | |
| 277 | fn rece a edit uchar * b edit uchar * to void |
| 278 | drid |
| 279 | crv = drnum |
| 280 | alg = drnum |
| 281 | dl = drdl alg |
| 282 | ml = drhex a |
| 283 | pl = drhex part a ml sub len a ml |
| 284 | sl = drhex part b dl sub len b dl |
| 285 | dreor |
| 286 | if isbad |
| 287 | ret |
| 288 | end |
| 289 | k = sha2_digest alg part a 0 ml part b 0 dl |
| 290 | rc = ecdsa_verify_raw crv part a ml pl part b 0 add dl sl dl |
| 291 | ofld rc |
| 292 | end |
| 293 | |
| 294 | fn recr a edit uchar * b edit uchar * to void |
| 295 | drid |
| 296 | alg = drnum |
| 297 | dl = drdl alg |
| 298 | kl = drhex a |
| 299 | ml = drhex part a kl sub len a kl |
| 300 | sl = drhex part b dl sub len b dl |
| 301 | dreor |
| 302 | if isbad |
| 303 | ret |
| 304 | end |
| 305 | k = sha2_digest alg part a kl ml part b 0 dl |
| 306 | rc = rsa_verify_pkcs1 alg part a 0 kl part b 0 add dl sl dl |
| 307 | ofld rc |
| 308 | end |
| 309 | |
| 310 | note ---- KDA HKDF ---- |
| 311 | |
| 312 | note b = PRK || FI: reads FI into b[dl..) and L, expands into c[0..L); returns rc, L in q[0] |
| 313 | fn kdaexp alg ulong b edit uchar * c edit uchar * q edit ulong 8 to ulong |
| 314 | dl = shr alg 3 |
| 315 | fl = drhex part b dl sub len b dl |
| 316 | l = drnum |
| 317 | put q 0 l |
| 318 | if gt l len c |
| 319 | drbad |
| 320 | ret 9 |
| 321 | end |
| 322 | if isbad |
| 323 | ret 9 |
| 324 | end |
| 325 | ret hkdf_expand alg part b 0 add dl fl dl part c 0 l |
| 326 | end |
| 327 | |
| 328 | note 1 when a[0..n) equals b[0..n), else 0 (or-accumulated) |
| 329 | fn dreq a view uchar * b view uchar * n ulong to ulong |
| 330 | if gt n len a |
| 331 | ret 0 |
| 332 | end |
| 333 | if gt n len b |
| 334 | ret 0 |
| 335 | end |
| 336 | d = uchar 0 |
| 337 | for i ulong n |
| 338 | d = or d xor get a i get b i |
| 339 | end |
| 340 | if eq d 0 |
| 341 | ret 1 |
| 342 | end |
| 343 | ret 0 |
| 344 | end |
| 345 | |
| 346 | note per expansion: K prints the DKM, V reads the expected DKM and folds the comparison into ok; |
| 347 | note both end with ok (1 when every expansion returned 0 and, for V, every DKM matched) |
| 348 | fn kdaloop b edit uchar * c edit uchar * q edit ulong 8 to void |
| 349 | e = new uchar 65536 0 |
| 350 | ok = ulong 1 |
| 351 | for j ulong get q 2 |
| 352 | rc = kdaexp get q 1 b c q |
| 353 | if isbad |
| 354 | ret |
| 355 | end |
| 356 | if eq get st 9 0 |
| 357 | ohex part c 0 get q 0 |
| 358 | else |
| 359 | el = drhex e |
| 360 | if ne el get q 0 |
| 361 | ok = 0 |
| 362 | end |
| 363 | ok = and ok dreq c e get q 0 |
| 364 | end |
| 365 | if ne rc 0 |
| 366 | ok = 0 |
| 367 | end |
| 368 | end |
| 369 | dreor |
| 370 | if isbad |
| 371 | ret |
| 372 | end |
| 373 | ofld ok |
| 374 | end |
| 375 | |
| 376 | fn reck a edit uchar * b edit uchar * c edit uchar * to void |
| 377 | q = new ulong 8 0 |
| 378 | drid |
| 379 | alg = drnum |
| 380 | dl = drdl alg |
| 381 | sl = drhex a |
| 382 | il = drhex part a sl sub len a sl |
| 383 | n = drnum |
| 384 | if isbad |
| 385 | ret |
| 386 | end |
| 387 | k = hkdf_extract alg part a 0 add sl il sl b |
| 388 | if ne k dl |
| 389 | drbad |
| 390 | ret |
| 391 | end |
| 392 | put q 1 alg |
| 393 | put q 2 n |
| 394 | kdaloop b c q |
| 395 | end |
| 396 | |
| 397 | note ---- TLS 1.3 key schedule (ACVP TLS-v1.3-KDF-RFC8446) ---- |
| 398 | |
| 399 | note w[0..dl) = HKDF-Extract(salt w[0..dl), ikm) |
| 400 | fn s13x w edit uchar 256 dl ulong ikm view uchar * to void |
| 401 | si = new uchar 4096 0 |
| 402 | p = new uchar 64 0 |
| 403 | for i ulong dl |
| 404 | put si i get w i |
| 405 | end |
| 406 | for i ulong len ikm |
| 407 | put si add dl i get ikm i |
| 408 | end |
| 409 | k = hkdf_extract shl dl 3 part si 0 add dl len ikm dl p |
| 410 | for i ulong dl |
| 411 | put w i get p i |
| 412 | end |
| 413 | end |
| 414 | |
| 415 | note p[dl..) = uint16 dl || uint8 len || "tls13 " || lab (HkdfLabel up to the context); returns its end |
| 416 | fn s13lab p edit uchar 256 dl ulong lab view uchar * to ulong |
| 417 | put p dl uchar shr dl 8 |
| 418 | put p add dl 1 uchar and dl 255 |
| 419 | ll = add 6 len lab |
| 420 | put p add dl 2 uchar ll |
| 421 | for i ulong 6 |
| 422 | put p add add dl 3 i get TLL13 i |
| 423 | end |
| 424 | for i ulong len lab |
| 425 | put p add add dl 9 i get lab i |
| 426 | end |
| 427 | ret add add dl 3 ll |
| 428 | end |
| 429 | |
| 430 | note o[0..dl) = HKDF-Expand(PRK p[0..dl), info p[dl..n), dl) |
| 431 | fn s13e p view uchar 256 n ulong dl ulong o edit uchar 64 to ulong |
| 432 | ret hkdf_expand shl dl 3 part p 0 n dl part o 0 dl |
| 433 | end |
| 434 | |
| 435 | note o = HKDF-Expand-Label(w[0..dl), lab, w[64..64+dl), dl) (RFC 8446 7.1) |
| 436 | fn s13d w view uchar 256 dl ulong lab view uchar * o edit uchar 64 to void |
| 437 | p = new uchar 256 0 |
| 438 | for i ulong dl |
| 439 | put p i get w i |
| 440 | end |
| 441 | c = s13lab p dl lab |
| 442 | put p c uchar dl |
| 443 | for i ulong dl |
| 444 | put p add add c 1 i get w add 64 i |
| 445 | end |
| 446 | r = s13e p add add c 1 dl dl o |
| 447 | end |
| 448 | |
| 449 | note w[64..) = transcript hash j (slot 64 j of h) |
| 450 | fn s13c w edit uchar 256 h view uchar 320 j ulong dl ulong to void |
| 451 | for i ulong dl |
| 452 | put w add 64 i get h add mul j 64 i |
| 453 | end |
| 454 | end |
| 455 | |
| 456 | note w[0..dl) = o[0..dl) |
| 457 | fn s13s w edit uchar 256 o view uchar 64 dl ulong to void |
| 458 | for i ulong dl |
| 459 | put w i get o i |
| 460 | end |
| 461 | end |
| 462 | |
| 463 | note h slot j = Hash(a[0..e_j)), e = 0, CH, CH||SH, CH||SH||SF, CH||SH||SF||CF |
| 464 | fn s13h a view uchar * q view ulong 16 h edit uchar 320 to void |
| 465 | d = new uchar 64 0 |
| 466 | alg = get q 1 |
| 467 | for j ulong 5 |
| 468 | k = sha2_digest alg part a 0 get q add 4 j d |
| 469 | for i ulong k |
| 470 | put h add mul j 64 i get d i |
| 471 | end |
| 472 | end |
| 473 | end |
| 474 | |
| 475 | fn s13run a view uchar * b view uchar * q view ulong 16 to void |
| 476 | w = new uchar 256 0 |
| 477 | h = new uchar 320 0 |
| 478 | o = new uchar 64 0 |
| 479 | z = new uchar 64 0 |
| 480 | dl = get q 2 |
| 481 | s13h a q h |
| 482 | if eq get q 9 0 |
| 483 | s13x w dl part z 0 dl |
| 484 | else |
| 485 | s13x w dl part b 0 get q 9 |
| 486 | end |
| 487 | s13c w h 1 dl |
| 488 | s13d w dl S13CET o |
| 489 | ohex part o 0 dl |
| 490 | s13d w dl S13EEM o |
| 491 | ohex part o 0 dl |
| 492 | s13c w h 0 dl |
| 493 | s13d w dl TLLDER o |
| 494 | s13s w o dl |
| 495 | if eq get q 10 0 |
| 496 | s13x w dl part z 0 dl |
| 497 | else |
| 498 | s13x w dl part b 1024 get q 10 |
| 499 | end |
| 500 | s13c w h 2 dl |
| 501 | s13d w dl TLLCHS o |
| 502 | ohex part o 0 dl |
| 503 | s13d w dl TLLSHS o |
| 504 | ohex part o 0 dl |
| 505 | s13c w h 0 dl |
| 506 | s13d w dl TLLDER o |
| 507 | s13s w o dl |
| 508 | s13x w dl part z 0 dl |
| 509 | s13c w h 3 dl |
| 510 | s13d w dl TLLCAP o |
| 511 | ohex part o 0 dl |
| 512 | s13d w dl TLLSAP o |
| 513 | ohex part o 0 dl |
| 514 | s13d w dl S13EXP o |
| 515 | ohex part o 0 dl |
| 516 | s13c w h 4 dl |
| 517 | s13d w dl S13RES o |
| 518 | ohex part o 0 dl |
| 519 | end |
| 520 | |
| 521 | note PSK into b[0..), DHE into b[1024..), CH SH SF CF back to back in a; q: 1 alg, 2 dl, 4..8 ends |
| 522 | note (q 4 = 0), 9 PSK length, 10 DHE length |
| 523 | fn s13in a edit uchar * b edit uchar * q edit ulong 16 nm ulong to void |
| 524 | put q 9 drhex part b 0 1024 |
| 525 | put q 10 drhex part b 1024 1024 |
| 526 | put q 4 0 |
| 527 | e = ulong 0 |
| 528 | for j ulong nm |
| 529 | k = drhex part a e sub len a e |
| 530 | e = add e k |
| 531 | put q add 5 j e |
| 532 | end |
| 533 | dreor |
| 534 | end |
| 535 | |
| 536 | fn recs a edit uchar * b edit uchar * to void |
| 537 | q = new ulong 16 0 |
| 538 | drid |
| 539 | put q 1 drnum |
| 540 | put q 2 drdl get q 1 |
| 541 | s13in a b q 4 |
| 542 | if isbad |
| 543 | ret |
| 544 | end |
| 545 | s13run a b q |
| 546 | end |
| 547 | |
| 548 | note ---- the same schedule through tls.om's tls_xtr / tls_drv (SHA-256 only) ---- |
| 549 | |
| 550 | fn s13tls a view uchar * b view uchar * q view ulong 16 to void |
| 551 | c = new uchar TLCSZ 0 |
| 552 | h = new uchar 32 0 |
| 553 | z = new uchar 32 0 |
| 554 | tls_h0 z |
| 555 | for i ulong 32 |
| 556 | put c add TLTMP i get b i |
| 557 | put c add TLSS i get b add 1024 i |
| 558 | end |
| 559 | r = tls_xtr c or TLHS shl TLTMP 32 |
| 560 | r = or r tls_drv c tls_sel TLHS TLHS 32 TLIDER z |
| 561 | r = or r tls_xtr c or TLHS or shl TLHS 16 shl TLSS 32 |
| 562 | k = sha2_digest 256 part a 0 get q 6 h |
| 563 | r = or r tls_drv c tls_sel TLCHS TLHS 32 TLICHS h |
| 564 | r = or r tls_drv c tls_sel TLSHS TLHS 32 TLISHS h |
| 565 | r = or r tls_drv c tls_sel TLMS TLHS 32 TLIDER z |
| 566 | r = or r tls_xtr c or TLMS shl TLMS 16 |
| 567 | k = sha2_digest 256 part a 0 get q 7 h |
| 568 | r = or r tls_drv c tls_sel TLCAP TLMS 32 TLICAP h |
| 569 | r = or r tls_drv c tls_sel TLSAP TLMS 32 TLISAP h |
| 570 | ofld r |
| 571 | ohex part c TLCHS 32 |
| 572 | ohex part c TLSHS 32 |
| 573 | ohex part c TLCAP 32 |
| 574 | ohex part c TLSAP 32 |
| 575 | end |
| 576 | |
| 577 | fn recn a edit uchar * b edit uchar * to void |
| 578 | q = new ulong 16 0 |
| 579 | drid |
| 580 | put q 1 256 |
| 581 | put q 2 32 |
| 582 | s13in a b q 3 |
| 583 | if ne get q 9 32 |
| 584 | drbad |
| 585 | end |
| 586 | if ne get q 10 32 |
| 587 | drbad |
| 588 | end |
| 589 | if isbad |
| 590 | ret |
| 591 | end |
| 592 | s13tls a b q |
| 593 | end |
| 594 | |
| 595 | note ---- X25519 ---- |
| 596 | |
| 597 | fn recx to void |
| 598 | k = new uchar 32 0 |
| 599 | u = new uchar 32 0 |
| 600 | o = new uchar 32 0 |
| 601 | drid |
| 602 | kl = drhex k |
| 603 | ul = drhex u |
| 604 | dreor |
| 605 | if ne kl 32 |
| 606 | drbad |
| 607 | end |
| 608 | if ne ul 32 |
| 609 | drbad |
| 610 | end |
| 611 | if isbad |
| 612 | ret |
| 613 | end |
| 614 | r = x25519_base o k |
| 615 | ofld r |
| 616 | ohex o |
| 617 | r = x25519_scalarmult o k u |
| 618 | ofld r |
| 619 | ohex o |
| 620 | end |
| 621 | |
| 622 | note ---- AES block cipher (aesgcm.om's aes_setkey / aes_enc4, encrypt direction only) ---- |
| 623 | |
| 624 | note block b[o..o+16) into lane 0 of s (s[150] bytes 0..7, s[151] bytes 8..15, big-endian) |
| 625 | fn aesld s edit ulong AGSN b view uchar * o ulong to void |
| 626 | x = ulong 0 |
| 627 | y = ulong 0 |
| 628 | for i ulong 8 |
| 629 | x = or shl x 8 ulong get b add o i |
| 630 | y = or shl y 8 ulong get b add add o 8 i |
| 631 | end |
| 632 | put s 150 x |
| 633 | put s 151 y |
| 634 | end |
| 635 | |
| 636 | fn aesst s view ulong AGSN b edit uchar * o ulong to void |
| 637 | for i ulong 16 |
| 638 | v = get s add 150 shr i 3 |
| 639 | put b add o i uchar and shr v shl sub 7 and i 7 3 255 |
| 640 | end |
| 641 | end |
| 642 | |
| 643 | note b[o..o+16) = AES-Encrypt(k, b[o..o+16)) |
| 644 | fn aesenc k view ulong AGKN s edit ulong AGSN b edit uchar * o ulong to void |
| 645 | aesld s b o |
| 646 | aes_enc4 k s |
| 647 | aesst s b o |
| 648 | end |
| 649 | |
| 650 | fn aeskl kl ulong to void |
| 651 | if eq kl 16 |
| 652 | ret |
| 653 | end |
| 654 | if eq kl 24 |
| 655 | ret |
| 656 | end |
| 657 | if eq kl 32 |
| 658 | ret |
| 659 | end |
| 660 | drbad |
| 661 | end |
| 662 | |
| 663 | note B ID KEY PT: ECB encryption of every 16-byte block -> ID CT |
| 664 | fn recb a edit uchar * b edit uchar * to void |
| 665 | k = new ulong AGKN 0 |
| 666 | s = new ulong AGSN 0 |
| 667 | drid |
| 668 | kl = drhex a |
| 669 | pl = drhex b |
| 670 | dreor |
| 671 | aeskl kl |
| 672 | if ne and pl 15 0 |
| 673 | drbad |
| 674 | end |
| 675 | if isbad |
| 676 | ret |
| 677 | end |
| 678 | aes_setkey k s part a 0 kl |
| 679 | for i ulong shr pl 4 |
| 680 | aesenc k s b shl i 4 |
| 681 | end |
| 682 | ohex part b 0 pl |
| 683 | end |
| 684 | |
| 685 | note MCT key update (ACVP AES MCT, ECB): key ^= the last kl bytes of ct998 || ct999 (u = 32 bytes: |
| 686 | note ct998 at 0, ct999 at 16) |
| 687 | fn aesmku key edit uchar 32 kl ulong u view uchar 32 to void |
| 688 | o = sub 32 kl |
| 689 | for i ulong kl |
| 690 | put key i xor get key i get u add o i |
| 691 | end |
| 692 | end |
| 693 | |
| 694 | note C ID KEY PT: ECB encrypt Monte Carlo, 100 rounds of 1000 -> ID (KEY PT CT) x 100 |
| 695 | fn recc a edit uchar * to void |
| 696 | k = new ulong AGKN 0 |
| 697 | s = new ulong AGSN 0 |
| 698 | key = new uchar 32 0 |
| 699 | u = new uchar 32 0 |
| 700 | drid |
| 701 | kl = drhex key |
| 702 | pl = drhex part u 16 16 |
| 703 | dreor |
| 704 | aeskl kl |
| 705 | if ne pl 16 |
| 706 | drbad |
| 707 | end |
| 708 | if isbad |
| 709 | ret |
| 710 | end |
| 711 | for r ulong 100 |
| 712 | ohex part key 0 kl |
| 713 | ohex part u 16 16 |
| 714 | aes_setkey k s part key 0 kl |
| 715 | for j ulong 1000 |
| 716 | for i ulong 16 |
| 717 | put u i get u add 16 i |
| 718 | end |
| 719 | aesenc k s u 16 |
| 720 | end |
| 721 | ohex part u 16 16 |
| 722 | aesmku key kl u |
| 723 | end |
| 724 | end |
| 725 | |
| 726 | fn drrec a edit uchar * b edit uchar * c edit uchar * to void |
| 727 | op = drpeek |
| 728 | dradv |
| 729 | if eq op 72 |
| 730 | rech a |
| 731 | ret |
| 732 | end |
| 733 | if eq op 77 |
| 734 | recm a |
| 735 | ret |
| 736 | end |
| 737 | if eq op 76 |
| 738 | recl a |
| 739 | ret |
| 740 | end |
| 741 | if eq op 84 |
| 742 | rect a b |
| 743 | ret |
| 744 | end |
| 745 | if eq op 71 |
| 746 | recg a b |
| 747 | ret |
| 748 | end |
| 749 | if eq op 68 |
| 750 | recd a b |
| 751 | ret |
| 752 | end |
| 753 | if eq op 69 |
| 754 | rece a b |
| 755 | ret |
| 756 | end |
| 757 | if eq op 82 |
| 758 | recr a b |
| 759 | ret |
| 760 | end |
| 761 | if eq op 75 |
| 762 | put st 9 0 |
| 763 | reck a b c |
| 764 | ret |
| 765 | end |
| 766 | if eq op 86 |
| 767 | put st 9 1 |
| 768 | reck a b c |
| 769 | ret |
| 770 | end |
| 771 | if eq op 83 |
| 772 | recs a b |
| 773 | ret |
| 774 | end |
| 775 | if eq op 78 |
| 776 | recn a b |
| 777 | ret |
| 778 | end |
| 779 | if eq op 66 |
| 780 | recb a b |
| 781 | ret |
| 782 | end |
| 783 | if eq op 67 |
| 784 | recc a |
| 785 | ret |
| 786 | end |
| 787 | if eq op 88 |
| 788 | recx |
| 789 | ret |
| 790 | end |
| 791 | drbad |
| 792 | end |
| 793 |