chovy-sign/LibChovy/VersionKey/EbootPbpMethod.cs

85 lines
3.7 KiB
C#
Raw Normal View History

2023-04-16 19:48:32 +00:00
using GameBuilder.Psp;
using Li.Utilities;
2023-04-16 19:48:32 +00:00
using PspCrypto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
2023-04-23 08:15:11 +00:00
namespace LibChovy.VersionKey
2023-04-16 19:48:32 +00:00
{
public class EbootPbpMethod
{
private static byte[] getKey(byte[] bbmac, byte[] headerBody)
{
byte[] versionKey = new byte[0x10];
Span<byte> mkey = stackalloc byte[Marshal.SizeOf<PspCrypto.AMCTRL.MAC_KEY>()];
AMCTRL.sceDrmBBMacInit(mkey, 3);
AMCTRL.sceDrmBBMacUpdate(mkey, headerBody, headerBody.Length);
AMCTRL.bbmac_getkey(mkey, bbmac, versionKey);
return versionKey;
}
2023-06-10 07:20:27 +00:00
public static NpDrmInfo GetVersionKey(Stream ebootStream, int keyIndex)
2023-04-16 19:48:32 +00:00
{
using (ebootStream)
{
StreamUtil ebootUtil = new StreamUtil(ebootStream);
ebootStream.Seek(0x1, SeekOrigin.Begin);
2023-04-17 14:41:55 +00:00
string pbpMagic = ebootUtil.ReadStrLen(0x3);
if (pbpMagic == "PBP")
2023-04-16 19:48:32 +00:00
{
int dataPspLocation = ebootUtil.ReadInt32At(0x20);
int dataPsarLocation = ebootUtil.ReadInt32At(0x24);
ebootStream.Seek(dataPsarLocation, SeekOrigin.Begin);
2023-04-17 14:41:55 +00:00
string psarMagic = ebootUtil.ReadStrLen(8);
2023-04-16 19:48:32 +00:00
2023-04-17 14:41:55 +00:00
switch (psarMagic)
2023-04-16 19:48:32 +00:00
{
case "NPUMDIMG":
2023-06-10 07:20:27 +00:00
int orginalKeyIndex = ebootUtil.ReadInt32();
2023-04-16 19:48:32 +00:00
string contentId = ebootUtil.ReadStringAt(dataPsarLocation + 0x10);
2023-04-17 14:41:55 +00:00
byte[] npUmdHdr = ebootUtil.ReadBytesAt(dataPsarLocation, 0xC0);
byte[] npUmdHeaderHash = ebootUtil.ReadBytesAt(dataPsarLocation + 0xC0, 0x10);
2023-04-16 19:48:32 +00:00
2023-04-17 14:41:55 +00:00
byte[] versionkey = getKey(npUmdHeaderHash, npUmdHdr);
2023-06-10 07:20:27 +00:00
SceNpDrm.sceNpDrmTransformVersionKey(versionkey, orginalKeyIndex, keyIndex);
return new NpDrmInfo(versionkey, contentId, keyIndex);
2023-04-16 22:14:13 +00:00
case "PSISOIMG":
2023-04-16 19:48:32 +00:00
using (DNASStream dnas = new DNASStream(ebootStream, dataPsarLocation + 0x400))
{
contentId = ebootUtil.ReadStringAt(dataPspLocation + 0x560);
2023-06-10 07:20:27 +00:00
orginalKeyIndex = dnas.KeyIndex;
2023-04-16 19:48:32 +00:00
versionkey = dnas.VersionKey;
2023-06-10 07:20:27 +00:00
SceNpDrm.sceNpDrmTransformVersionKey(versionkey, orginalKeyIndex, keyIndex);
return new NpDrmInfo(versionkey, contentId, keyIndex);
2023-04-16 19:48:32 +00:00
}
2023-04-16 22:14:13 +00:00
case "PSTITLEI":
2023-04-16 19:48:32 +00:00
using (DNASStream dnas = new DNASStream(ebootStream, dataPsarLocation + 0x200))
{
contentId = ebootUtil.ReadStringAt(dataPspLocation + 0x560);
2023-06-10 07:20:27 +00:00
orginalKeyIndex = dnas.KeyIndex;
2023-04-16 19:48:32 +00:00
versionkey = dnas.VersionKey;
2023-06-10 07:20:27 +00:00
SceNpDrm.sceNpDrmTransformVersionKey(versionkey, orginalKeyIndex, keyIndex);
return new NpDrmInfo(versionkey, contentId, keyIndex);
2023-04-16 19:48:32 +00:00
}
default:
2023-04-17 14:41:55 +00:00
throw new Exception("Cannot obtain versionkey from this EBOOT.PBP (magic:" + psarMagic + ")");
2023-04-16 19:48:32 +00:00
}
}
else
{
2023-04-17 14:41:55 +00:00
throw new Exception("Invalid PBP (got \"" + pbpMagic + "\", expected \"PBP\")");
2023-04-16 19:48:32 +00:00
}
}
}
}
}