"""Verify BCrypt CNG setup - test raw key + different CFB segment sizes.""" import ctypes from ctypes import c_void_p, c_ulong, byref from pathlib import Path import struct KEY = b'kj)TGtrK>f]b[Piow.gU+nC@s""""""4' IV = b"Copyright @ OneO" bcrypt = ctypes.windll.bcrypt # Known plaintext (DX header from hook dump) dx_plain = bytes.fromhex("44580000000000005c58000000000000") # Known ciphertext (from file at offset 24, first 16 bytes) file_ct = bytes.fromhex("2e0c10c7c967f66b6d03821271115ad6") # Full file data file_data = Path("ocr_data/oneocr.onemodel").read_bytes() hook_dx = Path("frida_dump/decrypt_1_in22624_out22624.bin").read_bytes() print("=" * 70) print("BCrypt CNG CFB Segment Size Test") print("=" * 70) print(f"KEY: {KEY}") print(f"IV: {IV}") print(f"Expected PT: {dx_plain.hex()}") print(f"Expected CT: {file_ct.hex()}") print() def test_cfb(msg_block_length, use_blob=False): """Test BCrypt AES-CFB with given MessageBlockLength.""" tag = "MBL={}".format("default" if msg_block_length is None else msg_block_length) if use_blob: tag += "+blob" hAlg = c_void_p() status = bcrypt.BCryptOpenAlgorithmProvider( byref(hAlg), "AES\0".encode("utf-16-le"), None, 0 ) if status != 0: print(" [{}] OpenAlgorithm failed: {:#010x}".format(tag, status)) return None mode = "ChainingModeCFB\0".encode("utf-16-le") status = bcrypt.BCryptSetProperty( hAlg, "ChainingMode\0".encode("utf-16-le"), mode, len(mode), 0 ) if status != 0: print(" [{}] SetProperty ChainingMode failed: {:#010x}".format(tag, status)) bcrypt.BCryptCloseAlgorithmProvider(hAlg, 0) return None if msg_block_length is not None: mbl = c_ulong(msg_block_length) status = bcrypt.BCryptSetProperty( hAlg, "MessageBlockLength\0".encode("utf-16-le"), byref(mbl), 4, 0 ) if status != 0: print(" [{}] SetProperty MBL={} failed: {:#010x}".format(tag, msg_block_length, status)) bcrypt.BCryptCloseAlgorithmProvider(hAlg, 0) return None hKey = c_void_p() if use_blob: blob = struct.pack('CT: {} {} {}".format(tag, our_ct[:16].hex(), "OK" if ct_match else "FAIL", mark)) print(" [{}] Dec->PT: {} {}".format(tag, our_pt[:16].hex(), "OK DX" if pt_match else "FAIL")) bcrypt.BCryptDestroyKey(hKey) bcrypt.BCryptDestroyKey(hKey2) bcrypt.BCryptCloseAlgorithmProvider(hAlg, 0) return ct_match print("--- Raw key (correct for BCryptGenerateSymmetricKey) ---") test_cfb(None) test_cfb(1) test_cfb(16) print() print("--- Blob key (has 12-byte header prepended - wrong) ---") test_cfb(None, use_blob=True) test_cfb(1, use_blob=True) test_cfb(16, use_blob=True) print() print("--- BCryptImportKey with BCRYPT_KEY_DATA_BLOB ---") for mbl in [None, 1, 16]: tag = "Import+MBL={}".format("default" if mbl is None else mbl) hAlg = c_void_p() bcrypt.BCryptOpenAlgorithmProvider(byref(hAlg), "AES\0".encode("utf-16-le"), None, 0) mode = "ChainingModeCFB\0".encode("utf-16-le") bcrypt.BCryptSetProperty(hAlg, "ChainingMode\0".encode("utf-16-le"), mode, len(mode), 0) if mbl is not None: mbl_val = c_ulong(mbl) bcrypt.BCryptSetProperty(hAlg, "MessageBlockLength\0".encode("utf-16-le"), byref(mbl_val), 4, 0) blob = struct.pack('