enet-validator/scripts/session/test_g.ps1

48 lines
2.2 KiB
PowerShell
Raw Permalink Normal View History

[Console]::OutputEncoding = [Text.Encoding]::UTF8
# Craft AECH with fresh random nonce -> .7 (local G) -> verify resp32 == G(nonce)
$TABLE = @(0x1008de00,0xe3b77ff6,0x31183e1c,0x90fb53c8,0xff10ba5c,0x030bc4b6,0xe22935c8,0x10e8ede1,
0x55f409a0,0x3eb0b9a3,0xf7333308,0xedf07810,0x8abf4078,0x60ec0545,0x1f4645c8,0x54094148,
0x04c0f500,0xfabe50bb,0xf3f3e54c,0x6c2a02cb,0xa2546e60,0x4f6ae10e,0x51dfe8d8,0x2eedb0ec,
0x74006928,0x48fade70,0xe5346e9c,0x1c3fe0bc,0xf6700700,0x6b7f1ba0,0xa1143cf4,0x3be8426c)
function G([uint32]$x) {
$big = [int64]$x * 13
$m = [uint32]($big % 4294967296)
$idx = [int]((($x % 32) + 1 + ($m % 30)) % 32)
return $x -bxor $TABLE[$idx]
}
$raw = [IO.File]::ReadAllBytes("$env:TEMP\opencode\cold2.tsv")
$text = if ($raw[0] -eq 0xFF -and $raw[1] -eq 0xFE) { [Text.Encoding]::Unicode.GetString($raw) } else { [Text.Encoding]::UTF8.GetString($raw) }
$hex = $null
foreach ($l in ($text -split "`r?`n")) {
$p = $l -split "`t"
if ($p.Count -lt 5 -or -not $p[4]) { continue }
$h = $p[4] -replace " ", ""
if ($h.Length -eq 1104 -and $h.Substring(0x28 * 2, 8) -eq "41454348") { $hex = $h; break }
}
if (-not $hex) { throw "no AECH template" }
$n = $hex.Length / 2
$req = New-Object byte[] $n
for ($i = 0; $i -lt $n; $i++) { $req[$i] = [Convert]::ToByte($hex.Substring(2 * $i, 2), 16) }
$rnd = New-Object Random
$udp = New-Object System.Net.Sockets.UdpClient
$udp.Client.ReceiveTimeout = 3000
$ep = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse("192.168.58.7"), 31772)
$pass = 0
for ($t = 0; $t -lt 5; $t++) {
$nonce = [uint32]($rnd.NextDouble() * 4294967295)
$nb = [BitConverter]::GetBytes($nonce)
[Array]::Copy($nb, 0, $req, 0x88, 4)
$expect = G $nonce
[void]$udp.Send($req, $req.Length, $ep)
try {
$rep = $udp.Receive([ref]$ep)
$got = [BitConverter]::ToUInt32($rep, 0xC0)
$ok = $got -eq $expect
if ($ok) { $pass++ }
Write-Host ("#{0} nonce={1:x8} expect={2:x8} got={3:x8} {4} len={5}" -f ($t+1), $nonce, $expect, $got, $(if ($ok) {"PASS"} else {"FAIL"}), $rep.Length)
} catch { Write-Host ("#{0} no reply" -f ($t+1)) }
}
Write-Host "== $pass/5 PASS =="
$udp.Close()