-
Notifications
You must be signed in to change notification settings - Fork 10
/
Program.cs
152 lines (133 loc) · 5.96 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace PSFSKKey
{
class Program
{
private static bool sceSblSsMemcmpConsttime(byte[] a, byte[] b, int len, int offsetA = 0, int offsetB = 0)
{
for (int i = 0; i < len; i++)
{
if (a[i + offsetA] != b[i + offsetB])
return false;
}
return true;
}
private static bool sceSblSsMemcmpConsttime(byte[] a, string b, int len, int offsetA = 0)
{
return ASCIIEncoding.ASCII.GetString(a, offsetA, len) == b;
}
private static void sceSblSsMemset(byte[] buffer, byte val, int len)
{
for (int i = 0; i < len; i++)
{
buffer[i] = val;
}
}
private static void Sha256Hmac(byte[] sha256HmacResult, byte[] enc, int datalen, byte[] sha256hmacKey, int keylen)
{
using (HMACSHA256 hmac = new HMACSHA256(sha256hmacKey))
{
var result = hmac.ComputeHash(enc, 0, datalen);
Buffer.BlockCopy(result, 0, sha256HmacResult, 0, sha256HmacResult.Length);
}
}
private static long sceSblSsDecryptSealedKey(byte[] enc, byte[] dec)
{
long errorCode = -2146499562;
if (enc != null && dec != null)
{
errorCode = -2146499532;
if (sceSblSsMemcmpConsttime(enc, "pfsSKKey", 8))
{
errorCode = -2146499538;
byte[] sha256hmacKey = new byte[16];
short keyVersion = (short) enc[8];
Console.WriteLine("Usering Keyset Version {0}", keyVersion);
var allKeybytes = File.ReadAllBytes(string.Format("savedatamasterhashkey{0}.bin", keyVersion));
Buffer.BlockCopy(allKeybytes, 0, sha256hmacKey, 0, 16);
byte[] sha256HmacResult = new byte[32];
Sha256Hmac(sha256HmacResult, enc, 0x40, sha256hmacKey, 0x10);
errorCode = -2146499531;
if (sceSblSsMemcmpConsttime(sha256HmacResult, enc, 32, 0, 64))
{
Console.WriteLine("HMAC Check... Success!");
byte[] iv = new byte[16];
Buffer.BlockCopy(enc, 16, iv, 0, iv.Length);
byte[] encryptedKey = new byte[32];
Buffer.BlockCopy(enc, 32, encryptedKey, 0, 32);
using (AesManaged aes = new AesManaged())
{
byte[] aesKey = new byte[16];
allKeybytes = File.ReadAllBytes(string.Format("savedatamasterkey{0}.bin", keyVersion));
Buffer.BlockCopy(allKeybytes, 0, aesKey, 0, 16);
aes.Mode = CipherMode.CBC;
aes.IV = iv;
aes.KeySize = 128;
aes.Key = aesKey;
aes.Padding = PaddingMode.None;
var stream = new MemoryStream();
using (var decryptor = aes.CreateDecryptor())
{
using (var cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
{
using (var writer = new BinaryWriter(cryptoStream))
{
writer.Write(encryptedKey);
}
}
}
byte[] cipherBytes = stream.ToArray();
Buffer.BlockCopy(cipherBytes, 0, dec, 0, 32);
}
errorCode = 0;
}
else
{
Console.WriteLine("HMAC Check... Failure!");
}
}
}
return errorCode;
}
static void Main(string[] args)
{
var bytes = File.ReadAllBytes(args[0]);
byte[] dec = new byte[32];
sceSblSsDecryptSealedKey(bytes, dec);
Console.WriteLine("Your PFS Key is {0}", BitConverter.ToString(dec).Replace("-", string.Empty));
var save = File.ReadAllBytes(args[1]);
byte[] iv = new byte[16];
Buffer.BlockCopy(bytes, 16, iv, 0, iv.Length);
// TODO Reverse SAMU for Decryption of PFS File System
/* using (AesManaged aes = new AesManaged())
{
aes.Mode = CipherMode.CBC;
aes.IV = iv;
aes.KeySize = 256;
aes.Key = dec;
aes.Padding = PaddingMode.None;
var stream = new MemoryStream();
using (var decryptor = aes.CreateDecryptor())
{
using (var cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
{
using (var writer = new BinaryWriter(cryptoStream))
{
writer.Write(save);
}
}
}
byte[] cipherBytes = stream.ToArray();
Console.WriteLine("PFS Save Content:");
Console.WriteLine(UTF8Encoding.UTF8.GetString(cipherBytes));
}*/
Console.ReadLine();
}
}
}