Files
elysium/tests/test_decrypt.py
T
tomas.kracmar 4fe884e658 refactor(Extract-NTHashes): move the export into the module as Send-ElysiumHashExport
Fourth root script converted, same mechanical approach as
Test-ElysiumPasswordQuality (AST line ranges, then a line-by-line
comparison ignoring indentation that shows only these differences):

- Script body -> src/Elysium/Public/Send-ElysiumHashExport.ps1
  (exported, [CmdletBinding()], comment-based help).
- Protect-FileWithAES -> Private/Crypto.ps1 (the ELY2 format in one
  place). tests/test_decrypt.py now dot-sources that file instead of
  pulling the function out of the script's AST.
- S3 put/get/delete, Get-FileSha256Hex and Get-FileChecksum ->
  Private/Storage.ps1; Normalize-ReportPath -> Private/Settings.ps1.
- The script's own Domain<N> loop -> the shared
  Get-DomainDetailsFromSettings; the only difference was a
  Domain<N>DA value that nothing read.
- The hash export's Out-File gets an explicit -Encoding utf8 (the
  value the script-level $PSDefaultParameterValues default supplied),
  so that session-global default and $OutputEncoding are dropped.
- Transcript via the shared helper (same extract-hashes-*.log name),
  $scriptRoot -> $InstallRoot; the completion message moves to the
  wrapper.

Extract-NTHashes.ps1 is now a thin wrapper (relaunch under
powershell.exe, import module, call the cmdlet).

Verified on macOS: decrypt round trip passes against Crypto.ps1, 22/22
Pester tests pass, and the cmdlet runs through settings to its
passphrase check and writes its transcript. The AD extraction and
upload need a Windows run.
2026-11-04 13:01:26 +01:00

54 lines
1.9 KiB
Python

# ELY2 round trip: encrypt with the real Protect-FileWithAES (src/Elysium/Private/Crypto.ps1, via
# pwsh), decrypt with decrypt.py. Catches any drift between the two sides of the format.
# Run: python3 tests/test_decrypt.py (needs pwsh on PATH and pycryptodome)
import os
import subprocess
import sys
import tempfile
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
import decrypt # noqa: E402
ENCRYPT_PS1 = r'''
param($Crypto, $In, $Out, $Pass)
$ErrorActionPreference = 'Stop'
. $Crypto
Protect-FileWithAES -InputFile $In -OutputFile $Out -Passphrase $Pass 6>$null
'''
def encrypt_with_powershell(plaintext: bytes, passphrase: str) -> bytes:
with tempfile.TemporaryDirectory() as d:
src, dst, ps1 = (os.path.join(d, n) for n in ('in.zip', 'out.enc', 'encrypt.ps1'))
with open(src, 'wb') as f:
f.write(plaintext)
with open(ps1, 'w', encoding='utf-8') as f:
f.write(ENCRYPT_PS1)
subprocess.run(['pwsh', '-NoProfile', '-File', ps1,
'-Crypto', os.path.join(ROOT, 'src', 'Elysium', 'Private', 'Crypto.ps1'),
'-In', src, '-Out', dst, '-Pass', passphrase], check=True)
with open(dst, 'rb') as f:
return f.read()
def assert_rejected(blob: bytes, passphrase: str):
try:
decrypt.decrypt(blob, passphrase)
except ValueError as e:
assert 'HMAC' in str(e), e
else:
raise AssertionError('tampered/wrong-passphrase input was accepted')
plaintext = os.urandom(1000)
for passphrase in ('correct horse battery staple', 'pässwörd-€'): # ASCII + non-ASCII
blob = encrypt_with_powershell(plaintext, passphrase)
assert blob[:4] == b'ELY2', blob[:4]
assert decrypt.decrypt(blob, passphrase) == plaintext, passphrase
assert_rejected(blob, 'wrong passphrase')
assert_rejected(blob[:40] + bytes([blob[40] ^ 1]) + blob[41:], passphrase) # flipped ciphertext bit
print('ok')