Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/omega/allen/tls-crypto.om # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / omega / allen / tls-crypto.om` 628 lines ยท 11254 bytes Raw Blame History 1 note SPDX-License-Identifier: AGPL-3.0-or-later 2 note Allen bounded TLS cryptography. Concatenate vendor/sha256-core.om before this unit. 3 note SHA256 reuses the existing first-party core and its provenance/license unchanged. 4 note X25519 follows RFC7748 sections 5/6 and native/net/x25519.c's Montgomery ladder. 5 note Unsigned radix-65536 field representation follows the first-party omnet field bounds; 6 note no verification-only branches, allocations, or point routines are reused. 7 note ChaCha20/Poly1305 are implemented from RFC8439 sections 2.1/2.3/2.5/2.8 equations. 8 note uint arithmetic wraps modulo 2^32 per OMEGESE 9.3; narrowing conversions are masked. 9 note No secret-indexed accesses or secret-dependent loop counts. Compiler/machine timing is unaudited. 10 note Single caller, nonreentrant; all input/output arrays are disjoint fixed globals, no caller aliases. 11 note Caller owns key/nonce uniqueness and entropy. Public calls preserve key/private input until tcwipe. 12 note tcsha/tchmac preserve Tcbuf; tcopen verifies the full tag BEFORE touching ciphertext. 13 note All public bool functions reject invalid lengths. HMAC accepts keys of 0..32 bytes only. 14 note HKDF-Extract/Expand RFC5869 is composed by tls.om using tchmac. 15 note Scratch globals are wiped after calls. tcwipe also clears caller-visible secrets and plaintext. 16 17 global Tcbuf uchar 65536 18 global Tckey uchar 32 19 global Tciv uchar 12 20 global Tcaad uchar 32 21 global Tctag uchar 16 22 global Tcdigest uchar 32 23 global Tcxpriv uchar 32 24 global Tcxpub uchar 32 25 global Tcxpeer uchar 32 26 global Tcxshare uchar 32 27 28 global Tchi uchar 32 29 global Tcxf ulong 176 30 global Tcxt ulong 32 31 global Tcxsc uchar 32 32 global Tcxres uchar 32 33 global Tccs uint 16 34 global Tccw uint 16 35 global Tccblk uchar 64 36 global Tcmkey uchar 32 37 global Tcmblk uchar 16 38 global Tcmtag uchar 16 39 global Tcmr ulong 10 40 global Tcmh ulong 10 41 global Tcmt ulong 10 42 global Tcmg ulong 10 43 44 fn tcshwipe to void 45 for i ulong 8 46 put H i 0 47 put Vv i 0 48 end 49 for i ulong 64 50 put W i 0 51 put Hb i 0 52 end 53 put Hs 0 0 54 put Hs 1 0 55 for i ulong 32 56 put Hash i 0 57 put Tchi i 0 58 end 59 end 60 61 fn tcsha n ulong to bool 62 for i ulong 32 63 put Tcdigest i 0 64 end 65 tcshwipe 66 if gt n 65536 67 ret false 68 end 69 constants 70 hinit 71 for i ulong n 72 hbyte get Tcbuf i 73 end 74 finish 75 for i ulong 32 76 put Tcdigest i get Hash i 77 end 78 tcshwipe 79 ret true 80 end 81 82 fn tchmac keylen ulong n ulong to bool 83 for i ulong 32 84 put Tcdigest i 0 85 end 86 tcshwipe 87 if or gt keylen 32 gt n 65536 88 ret false 89 end 90 constants 91 hinit 92 for i ulong 64 93 v = uchar 54 94 if lt i keylen 95 v = xor v get Tckey i 96 end 97 hbyte v 98 end 99 for i ulong n 100 hbyte get Tcbuf i 101 end 102 finish 103 for i ulong 32 104 put Tchi i get Hash i 105 end 106 hinit 107 for i ulong 64 108 v = uchar 92 109 if lt i keylen 110 v = xor v get Tckey i 111 end 112 hbyte v 113 end 114 for i ulong 32 115 hbyte get Tchi i 116 end 117 finish 118 for i ulong 32 119 put Tcdigest i get Hash i 120 end 121 tcshwipe 122 ret true 123 end 124 125 note Field slots: x0,a16,b32,c48,d64,e80,f96,k112,inverse128,canonical144,subtract160. 126 note A mul input limb is below 2^21; folded convolution below 2^52, so ulong never overflows. 127 note Mul gives limbs <=65535 except limb0<=65573. Sub adds 8p, keeping intermediates positive. 128 fn tcxwipe to void 129 for i ulong 176 130 put Tcxf i 0 131 end 132 for i ulong 32 133 put Tcxt i 0 134 put Tcxsc i 0 135 put Tcxres i 0 136 end 137 end 138 139 fn tcfcopy o ulong a ulong to void 140 for i ulong 16 141 put Tcxf add o i get Tcxf add a i 142 end 143 end 144 145 fn tcfadd o ulong a ulong b ulong to void 146 for i ulong 16 147 v = add get Tcxf add a i get Tcxf add b i 148 put Tcxf add o i v 149 end 150 end 151 152 fn tcfprime i ulong to ulong 153 if eq i 0 154 ret 65517 155 end 156 if eq i 15 157 ret 32767 158 end 159 ret 65535 160 end 161 162 fn tcfsub o ulong a ulong b ulong to void 163 for i ulong 16 164 v = add get Tcxf add a i mul tcfprime i 8 165 put Tcxf add o i sub v get Tcxf add b i 166 end 167 end 168 169 fn tcfcarry o ulong to void 170 for i ulong 15 171 p = add o i 172 v = get Tcxf p 173 put Tcxf add p 1 add get Tcxf add p 1 shr v 16 174 put Tcxf p and v 65535 175 end 176 v = get Tcxf add o 15 177 put Tcxf add o 15 and v 65535 178 put Tcxf o add get Tcxf o mul shr v 16 38 179 end 180 181 fn tcfmul o ulong a ulong b ulong to void 182 for i ulong 32 183 put Tcxt i 0 184 end 185 for i ulong 16 186 x = get Tcxf add a i 187 for j ulong 16 188 p = add i j 189 v = mul x get Tcxf add b j 190 put Tcxt p add get Tcxt p v 191 end 192 end 193 for i ulong 15 194 v = add get Tcxt i mul 38 get Tcxt add i 16 195 put Tcxf add o i v 196 end 197 put Tcxf add o 15 get Tcxt 15 198 tcfcarry o 199 tcfcarry o 200 end 201 202 fn tcfswap a ulong b ulong bit ulong to void 203 mask = sub ulong 0 bit 204 for i ulong 16 205 x = get Tcxf add a i 206 y = get Tcxf add b i 207 v = and mask xor x y 208 put Tcxf add a i xor x v 209 put Tcxf add b i xor y v 210 end 211 end 212 213 fn tcflad bit ulong to void 214 tcfswap 16 32 bit 215 tcfswap 48 64 bit 216 tcfadd 80 16 48 217 tcfsub 16 16 48 218 tcfadd 48 32 64 219 tcfsub 32 32 64 220 tcfmul 64 80 80 221 tcfmul 96 16 16 222 tcfmul 16 48 16 223 tcfmul 48 32 80 224 tcfadd 80 16 48 225 tcfsub 16 16 48 226 tcfmul 32 16 16 227 tcfsub 48 64 96 228 tcfmul 16 48 112 229 tcfadd 16 16 64 230 tcfmul 48 48 16 231 tcfmul 16 64 96 232 tcfmul 64 32 0 233 tcfmul 32 80 80 234 tcfswap 16 32 bit 235 tcfswap 48 64 bit 236 end 237 238 fn tcfinv to void 239 tcfcopy 128 48 240 for i ulong 254 241 j = sub 253 i 242 tcfmul 128 128 128 243 note Branch uses fixed public exponent p-2, not any secret bit. 244 if and ne j 2 ne j 4 245 tcfmul 128 128 48 246 end 247 end 248 tcfmul 16 16 128 249 end 250 251 fn tcfpack to void 252 tcfcopy 144 16 253 tcfcarry 144 254 tcfcarry 144 255 tcfcarry 144 256 for j ulong 2 257 borrow = ulong 0 258 for i ulong 16 259 v = add get Tcxf add 144 i 65536 260 v = sub sub v tcfprime i borrow 261 put Tcxf add 160 i and v 65535 262 borrow = sub 1 shr v 16 263 end 264 tcfswap 144 160 sub 1 borrow 265 end 266 for i ulong 16 267 v = get Tcxf add 144 i 268 put Tcxres mul i 2 uchar and v 255 269 put Tcxres add mul i 2 1 uchar and shr v 8 255 270 end 271 end 272 273 fn tcxrun base bool to bool 274 tcxwipe 275 for i ulong 32 276 put Tcxsc i get Tcxpriv i 277 end 278 put Tcxsc 0 and get Tcxsc 0 248 279 put Tcxsc 31 or and get Tcxsc 31 127 64 280 if base 281 put Tcxf 0 9 282 else 283 for i ulong 16 284 v = ulong get Tcxpeer mul i 2 285 v = or v shl ulong get Tcxpeer add mul i 2 1 8 286 put Tcxf i v 287 end 288 put Tcxf 15 and get Tcxf 15 32767 289 end 290 tcfcopy 32 0 291 put Tcxf 16 1 292 put Tcxf 64 1 293 put Tcxf 112 56129 294 put Tcxf 113 1 295 for i ulong 255 296 j = sub 254 i 297 v = ulong get Tcxsc div j 8 298 tcflad and shr v rem j 8 1 299 end 300 tcfinv 301 tcfpack 302 acc = uchar 0 303 for i ulong 32 304 acc = or acc get Tcxres i 305 end 306 ret ne acc 0 307 end 308 309 fn tcx25519 to bool 310 ok = tcxrun false 311 for i ulong 32 312 put Tcxshare i get Tcxres i 313 end 314 tcxwipe 315 ret ok 316 end 317 318 fn tcxpublic to bool 319 ok = tcxrun true 320 for i ulong 32 321 put Tcxpub i get Tcxres i 322 end 323 tcxwipe 324 ret ok 325 end 326 327 fn tccwipe to void 328 for i ulong 16 329 put Tccs i 0 330 put Tccw i 0 331 put Tcmblk i 0 332 put Tcmtag i 0 333 end 334 for i ulong 64 335 put Tccblk i 0 336 end 337 for i ulong 32 338 put Tcmkey i 0 339 end 340 for i ulong 10 341 put Tcmr i 0 342 put Tcmh i 0 343 put Tcmt i 0 344 put Tcmg i 0 345 end 346 end 347 348 fn tclrot x uint n uint to uint 349 ret or shl x n shr x sub 32 n 350 end 351 352 fn tccqr a ulong b ulong c ulong d ulong to void 353 put Tccw a add get Tccw a get Tccw b 354 put Tccw d tclrot xor get Tccw d get Tccw a 16 355 put Tccw c add get Tccw c get Tccw d 356 put Tccw b tclrot xor get Tccw b get Tccw c 12 357 put Tccw a add get Tccw a get Tccw b 358 put Tccw d tclrot xor get Tccw d get Tccw a 8 359 put Tccw c add get Tccw c get Tccw d 360 put Tccw b tclrot xor get Tccw b get Tccw c 7 361 end 362 363 fn tccinit to void 364 put Tccs 0 `1634760805` 365 put Tccs 1 `857760878` 366 put Tccs 2 `2036477234` 367 put Tccs 3 `1797285236` 368 for i ulong 8 369 v = uint 0 370 for j ulong 4 371 b = uint get Tckey add mul i 4 j 372 v = or v shl b mul j 8 373 end 374 put Tccs add i 4 v 375 end 376 for i ulong 3 377 v = uint 0 378 for j ulong 4 379 b = uint get Tciv add mul i 4 j 380 v = or v shl b mul j 8 381 end 382 put Tccs add i 13 v 383 end 384 end 385 386 fn tccgen ctr uint to void 387 put Tccs 12 ctr 388 for i ulong 16 389 put Tccw i get Tccs i 390 end 391 for i ulong 10 392 tccqr 0 4 8 12 393 tccqr 1 5 9 13 394 tccqr 2 6 10 14 395 tccqr 3 7 11 15 396 tccqr 0 5 10 15 397 tccqr 1 6 11 12 398 tccqr 2 7 8 13 399 tccqr 3 4 9 14 400 end 401 for i ulong 16 402 v = add get Tccw i get Tccs i 403 for j ulong 4 404 put Tccblk add mul i 4 j uchar and shr v mul j 8 255 405 end 406 end 407 end 408 409 note Standalone block entry for primitive qualification; AEAD initializes the key only once. 410 fn tccblock ctr uint to void 411 tccinit 412 tccgen ctr 413 end 414 415 note At most 1024 payload blocks: counters 1..1024, never 0 or a wrapped uint. 416 fn tccxor n ulong to void 417 p = ulong 0 418 ctr = uint 1 419 while lt p n bound 1024 420 tccgen ctr 421 m = sub n p 422 if gt m 64 423 m = 64 424 end 425 for i ulong m 426 v = xor get Tcbuf add p i get Tccblk i 427 put Tcbuf add p i v 428 end 429 p = add p m 430 ctr = add ctr 1 431 end 432 end 433 434 note Poly1305 radix 2^13: 10 limbs, B^10 = 5 mod p. Folded products <2^40. 435 note Tcmkey is the one-time key, Tcmblk a block, Tcmh the accumulator. No secret tables. 436 fn tcminit to void 437 for i ulong 10 438 put Tcmr i 0 439 put Tcmh i 0 440 end 441 for i ulong 16 442 v = ulong get Tcmkey i 443 if eq rem i 4 3 444 v = and v 15 445 end 446 if and ne i 0 eq rem i 4 0 447 v = and v 252 448 end 449 j = div mul i 8 13 450 v = shl v rem mul i 8 13 451 put Tcmr j or get Tcmr j and v 8191 452 if lt j 9 453 put Tcmr add j 1 or get Tcmr add j 1 shr v 13 454 end 455 end 456 end 457 458 fn tcmcarry to void 459 for i ulong 9 460 v = get Tcmh i 461 put Tcmh i and v 8191 462 put Tcmh add i 1 add get Tcmh add i 1 shr v 13 463 end 464 v = get Tcmh 9 465 put Tcmh 9 and v 8191 466 put Tcmh 0 add get Tcmh 0 mul shr v 13 5 467 end 468 469 fn tcmblock n ulong to void 470 for i ulong n 471 j = div mul i 8 13 472 v = shl ulong get Tcmblk i rem mul i 8 13 473 put Tcmh j add get Tcmh j and v 8191 474 if lt j 9 475 put Tcmh add j 1 add get Tcmh add j 1 shr v 13 476 end 477 end 478 top = div mul n 8 13 479 v = shl ulong 1 rem mul n 8 13 480 put Tcmh top add get Tcmh top v 481 for i ulong 10 482 put Tcmt i 0 483 end 484 for i ulong 10 485 for j ulong 10 486 p = add i j 487 v = mul get Tcmh i get Tcmr j 488 if ge p 10 489 p = sub p 10 490 v = mul v 5 491 end 492 put Tcmt p add get Tcmt p v 493 end 494 end 495 for i ulong 10 496 put Tcmh i get Tcmt i 497 end 498 tcmcarry 499 tcmcarry 500 tcmcarry 501 end 502 503 fn tcmfinish to void 504 note h is normalized below B^10; h+5 overflows exactly when h >= p. 505 v = ulong 5 506 for i ulong 10 507 v = add v get Tcmh i 508 put Tcmg i and v 8191 509 v = shr v 13 510 end 511 mask = sub ulong 0 v 512 for i ulong 10 513 v = xor get Tcmg i get Tcmh i 514 put Tcmh i xor get Tcmh i and mask v 515 end 516 carry = ulong 0 517 for i ulong 16 518 j = div mul i 8 13 519 s = rem mul i 8 13 520 v = shr get Tcmh j s 521 if lt j 9 522 v = or v shl get Tcmh add j 1 sub 13 s 523 end 524 v = add and v 255 carry 525 v = add v ulong get Tcmkey add i 16 526 put Tcmtag i uchar and v 255 527 carry = shr v 8 528 end 529 end 530 531 fn tcmauth n ulong aadlen ulong to void 532 tccgen 0 533 for i ulong 32 534 put Tcmkey i get Tccblk i 535 end 536 tcminit 537 p = ulong 0 538 while lt p aadlen bound 2 539 for i ulong 16 540 put Tcmblk i 0 541 if lt add p i aadlen 542 put Tcmblk i get Tcaad add p i 543 end 544 end 545 tcmblock 16 546 p = add p 16 547 end 548 p = 0 549 while lt p n bound 4096 550 for i ulong 16 551 put Tcmblk i 0 552 if lt add p i n 553 put Tcmblk i get Tcbuf add p i 554 end 555 end 556 tcmblock 16 557 p = add p 16 558 end 559 for i ulong 8 560 put Tcmblk i uchar and shr aadlen mul i 8 255 561 put Tcmblk add i 8 uchar and shr n mul i 8 255 562 end 563 tcmblock 16 564 tcmfinish 565 end 566 567 fn tcseal n ulong aadlen ulong to bool 568 tccwipe 569 for i ulong 16 570 put Tctag i 0 571 end 572 if or gt n 65536 gt aadlen 32 573 ret false 574 end 575 tccinit 576 tccxor n 577 tcmauth n aadlen 578 for i ulong 16 579 put Tctag i get Tcmtag i 580 end 581 tccwipe 582 ret true 583 end 584 585 fn tcopen n ulong aadlen ulong to bool 586 tccwipe 587 if or gt n 65536 gt aadlen 32 588 ret false 589 end 590 tccinit 591 tcmauth n aadlen 592 v = uchar 0 593 for i ulong 16 594 v = or v xor get Tctag i get Tcmtag i 595 end 596 note Only the final accumulated authentication result branches; no partial tag comparison. 597 if ne v 0 598 tccwipe 599 ret false 600 end 601 tccxor n 602 tccwipe 603 ret true 604 end 605 606 fn tcwipe to void 607 tcshwipe 608 tcxwipe 609 tccwipe 610 for i ulong 65536 611 put Tcbuf i 0 612 end 613 for i ulong 32 614 put Tckey i 0 615 put Tcaad i 0 616 put Tcdigest i 0 617 put Tcxpriv i 0 618 put Tcxpub i 0 619 put Tcxpeer i 0 620 put Tcxshare i 0 621 end 622 for i ulong 12 623 put Tciv i 0 624 end 625 for i ulong 16 626 put Tctag i 0 627 end 628 end