// Plain PGD handling functions. public virtual sbyte[] DecryptPGD(sbyte[] inbuf, int size, sbyte[] key, int seed) { // Setup the crypto and keygen modes and initialize both context structs. int sdEncMode = 1; int sdGenMode = 2; pgdMacContext = new AMCTRL.BBMac_Ctx(); pgdCipherContext = new AMCTRL.BBCipher_Ctx(); // Align the buffers to 16-bytes. int alignedSize = ((size + 0xF) >> 4) << 4; sbyte[] outbuf = new sbyte[alignedSize - 0x10]; sbyte[] dataBuf = new sbyte[alignedSize]; // Fully copy the contents of the encrypted file. Array.Copy(inbuf, 0, dataBuf, 0, size); amctrl.hleDrmBBMacInit(pgdMacContext, sdEncMode); amctrl.hleDrmBBCipherInit(pgdCipherContext, sdEncMode, sdGenMode, dataBuf, key, seed); amctrl.hleDrmBBMacUpdate(pgdMacContext, dataBuf, 0x10); Array.Copy(dataBuf, 0x10, outbuf, 0, alignedSize - 0x10); amctrl.hleDrmBBMacUpdate(pgdMacContext, outbuf, alignedSize - 0x10); amctrl.hleDrmBBCipherUpdate(pgdCipherContext, outbuf, alignedSize - 0x10); return(outbuf); }
/* * sceNpDrm - npdrm.prx */ public virtual sbyte[] hleNpDrmGetFixedKey(sbyte[] hash, sbyte[] data, int mode) { // Setup the crypto and keygen modes and initialize both context structs. AMCTRL.BBMac_Ctx bbctx = new AMCTRL.BBMac_Ctx(); AES128 aes = new AES128("AES/CBC/NoPadding"); // Get the encryption key. sbyte[] encKey = new sbyte[0x10]; if ((mode & 0x1) == 0x1) { for (int i = 0; i < 0x10; i++) { encKey[i] = unchecked ((sbyte)(KeyVault.drmEncKey1[i] & 0xFF)); } } else if ((mode & 0x2) == 0x2) { for (int i = 0; i < 0x10; i++) { encKey[i] = unchecked ((sbyte)(KeyVault.drmEncKey2[i] & 0xFF)); } } else if ((mode & 0x3) == 0x3) { for (int i = 0; i < 0x10; i++) { encKey[i] = unchecked ((sbyte)(KeyVault.drmEncKey3[i] & 0xFF)); } } else { return(null); } // Get the fixed key. sbyte[] fixedKey = new sbyte[0x10]; for (int i = 0; i < 0x10; i++) { fixedKey[i] = unchecked ((sbyte)(KeyVault.drmFixedKey[i] & 0xFF)); } // Call the BBMac functions. amctrl.hleDrmBBMacInit(bbctx, 1); amctrl.hleDrmBBMacUpdate(bbctx, data, data.Length); amctrl.hleDrmBBMacFinal(bbctx, hash, fixedKey); // Encrypt and return the hash. return(aes.encrypt(hash, encKey, iv)); }
public virtual bool CheckEDATRenameKey(sbyte[] fileName, sbyte[] hash, sbyte[] data) { // Set up MAC context. pgdMacContext = new AMCTRL.BBMac_Ctx(); // Perform hash check. amctrl.hleDrmBBMacInit(pgdMacContext, 3); amctrl.hleDrmBBMacUpdate(pgdMacContext, data, 0x30); amctrl.hleDrmBBMacUpdate(pgdMacContext, fileName, fileName.Length); // Get the fixed rename key. sbyte[] renameKey = new sbyte[0x10]; for (int i = 0; i < 0x10; i++) { renameKey[i] = unchecked ((sbyte)(KeyVault.drmRenameKey[i] & 0xFF)); } // Compare and return. return(amctrl.hleDrmBBMacFinal2(pgdMacContext, hash, renameKey) == 0); }
public virtual sbyte[] GetEDATPGDKey(sbyte[] inbuf, int size) { // Setup the crypto and keygen modes and initialize both context structs. int macEncMode; int pgdFlag = 2; pgdMacContext = new AMCTRL.BBMac_Ctx(); // Align the buffer to 16-bytes. int alignedSize = ((size + 0xF) >> 4) << 4; sbyte[] dataBuf = new sbyte[alignedSize]; // Fully copy the contents of the encrypted file. Array.Copy(inbuf, 0, dataBuf, 0, size); int keyIndex = dataBuf[0x4]; int drmType = dataBuf[0x8]; if ((drmType & 0x1) == 0x1) { macEncMode = 1; pgdFlag |= 4; if (keyIndex > 0x1) { macEncMode = 3; pgdFlag |= 8; } } else { macEncMode = 2; } // Get fixed DNAS keys. sbyte[] dnasKey = new sbyte[0x10]; if ((pgdFlag & 0x2) == 0x2) { for (int i = 0; i < KeyVault.drmDNASKey1.Length; i++) { dnasKey[i] = unchecked ((sbyte)(KeyVault.drmDNASKey1[i] & 0xFF)); } } else if ((pgdFlag & 0x1) == 0x1) { for (int i = 0; i < KeyVault.drmDNASKey2.Length; i++) { dnasKey[i] = unchecked ((sbyte)(KeyVault.drmDNASKey2[i] & 0xFF)); } } else { return(null); } // Get mac80 from input. sbyte[] macKey80 = new sbyte[0x10]; Array.Copy(dataBuf, 0x80, macKey80, 0, 0x10); // Get mac70 from input. sbyte[] macKey70 = new sbyte[0x10]; Array.Copy(dataBuf, 0x70, macKey70, 0, 0x10); // MAC_0x80 amctrl.hleDrmBBMacInit(pgdMacContext, macEncMode); amctrl.hleDrmBBMacUpdate(pgdMacContext, dataBuf, 0x80); amctrl.hleDrmBBMacFinal2(pgdMacContext, macKey80, dnasKey); // MAC_0x70 amctrl.hleDrmBBMacInit(pgdMacContext, macEncMode); amctrl.hleDrmBBMacUpdate(pgdMacContext, dataBuf, 0x70); // Get the decryption key from BBMAC. return(amctrl.GetKeyFromBBMac(pgdMacContext, macKey70)); }