diff options
| author | WitherOrNot | 2025-04-24 16:13:10 +0000 |
|---|---|---|
| committer | GitHub | 2025-04-24 16:13:10 +0000 |
| commit | f81edcb3171d1f80da1c67abdf7193d871b18378 (patch) | |
| tree | 993d3ea250c9d264c61252dd388fee5e5447e218 /LibTSforge/Crypto | |
| parent | 8aa1f9078d786a7b20d2b92bbeefdd77a93dd828 (diff) | |
| parent | 912f83c11b75c93f38b7966d7b727144eee7f23d (diff) | |
| download | TSforge-f81edcb3171d1f80da1c67abdf7193d871b18378.zip | |
Merge pull request #5 from massgravel/longhorn1.1.0
Longhorn
Diffstat (limited to 'LibTSforge/Crypto')
| -rw-r--r-- | LibTSforge/Crypto/CryptoUtils.cs | 13 | ||||
| -rw-r--r-- | LibTSforge/Crypto/PhysStoreCrypto.cs | 37 |
2 files changed, 36 insertions, 14 deletions
diff --git a/LibTSforge/Crypto/CryptoUtils.cs b/LibTSforge/Crypto/CryptoUtils.cs index 4851570..11c2413 100644 --- a/LibTSforge/Crypto/CryptoUtils.cs +++ b/LibTSforge/Crypto/CryptoUtils.cs @@ -106,10 +106,21 @@ namespace LibTSforge.Crypto public static bool HMACVerify(byte[] key, byte[] data, byte[] signature) { - HMACSHA1 hmac = new HMACSHA1(key); return Enumerable.SequenceEqual(signature, HMACSign(key, data)); } + public static byte[] SaltSHASum(byte[] salt, byte[] data) + { + SHA1 sha1 = SHA1.Create(); + byte[] sha_data = salt.Concat(data).ToArray(); + return sha1.ComputeHash(sha_data); + } + + public static bool SaltSHAVerify(byte[] salt, byte[] data, byte[] checksum) + { + return Enumerable.SequenceEqual(checksum, SaltSHASum(salt, data)); + } + public static byte[] SHA256Hash(byte[] data) { using (SHA256 sha256 = SHA256.Create()) diff --git a/LibTSforge/Crypto/PhysStoreCrypto.cs b/LibTSforge/Crypto/PhysStoreCrypto.cs index 6d77897..08978e2 100644 --- a/LibTSforge/Crypto/PhysStoreCrypto.cs +++ b/LibTSforge/Crypto/PhysStoreCrypto.cs @@ -8,7 +8,7 @@ namespace LibTSforge.Crypto public static class PhysStoreCrypto { - public static byte[] DecryptPhysicalStore(byte[] data, bool production) + public static byte[] DecryptPhysicalStore(byte[] data, bool production, PSVersion version) { byte[] rsaKey = production ? Keys.PRODUCTION : Keys.TEST; BinaryReader br = new BinaryReader(new MemoryStream(data)); @@ -16,29 +16,40 @@ namespace LibTSforge.Crypto byte[] aesKeySig = br.ReadBytes(0x80); byte[] encAesKey = br.ReadBytes(0x80); - if (CryptoUtils.RSAVerifySignature(rsaKey, encAesKey, aesKeySig)) + if (!CryptoUtils.RSAVerifySignature(rsaKey, encAesKey, aesKeySig)) { - byte[] aesKey = CryptoUtils.RSADecrypt(rsaKey, encAesKey); - byte[] decData = CryptoUtils.AESDecrypt(br.ReadBytes((int)br.BaseStream.Length - 0x110), aesKey); - byte[] hmacKey = decData.Take(0x10).ToArray(); - byte[] hmacSig = decData.Skip(0x10).Take(0x14).ToArray(); - byte[] psData = decData.Skip(0x28).ToArray(); + throw new Exception("Failed to decrypt physical store."); + } + + byte[] aesKey = CryptoUtils.RSADecrypt(rsaKey, encAesKey); + byte[] decData = CryptoUtils.AESDecrypt(br.ReadBytes((int)br.BaseStream.Length - 0x110), aesKey); + byte[] hmacKey = decData.Take(0x10).ToArray(); // SHA-1 salt on Vista + byte[] hmacSig = decData.Skip(0x10).Take(0x14).ToArray(); // SHA-1 hash on Vista + byte[] psData = decData.Skip(0x28).ToArray(); + if (version != PSVersion.Vista) + { if (!CryptoUtils.HMACVerify(hmacKey, psData, hmacSig)) { - Logger.WriteLine("Warning: Failed to verify HMAC. Physical store is either corrupt or in Vista format."); + throw new InvalidDataException("Failed to verify HMAC. Physical store is corrupt."); + } + } + else + { + if (!CryptoUtils.SaltSHAVerify(hmacKey, psData, hmacSig)) + { + throw new InvalidDataException("Failed to verify checksum. Physical store is corrupt."); } - - return psData; } - throw new Exception("Failed to decrypt physical store."); + return psData; } public static byte[] EncryptPhysicalStore(byte[] data, bool production, PSVersion version) { Dictionary<PSVersion, int> versionTable = new Dictionary<PSVersion, int> { + {PSVersion.Vista, 2}, {PSVersion.Win7, 5}, {PSVersion.Win8, 1}, {PSVersion.WinBlue, 2}, @@ -52,9 +63,9 @@ namespace LibTSforge.Crypto byte[] encAesKey = CryptoUtils.RSAEncrypt(rsaKey, aesKey); byte[] aesKeySig = CryptoUtils.RSASign(rsaKey, encAesKey); - byte[] hmacSig = CryptoUtils.HMACSign(hmacKey, data); + byte[] hmacSig = version != PSVersion.Vista ? CryptoUtils.HMACSign(hmacKey, data) : CryptoUtils.SaltSHASum(hmacKey, data); - byte[] decData = new byte[] { }; + byte[] decData = { }; decData = decData.Concat(hmacKey).Concat(hmacSig).Concat(BitConverter.GetBytes(0)).Concat(data).ToArray(); byte[] encData = CryptoUtils.AESEncrypt(decData, aesKey); |
