Exemplo n.º 1
0
 public static int Memcmp(byte *str1, byte *str2, int count) => PointerUtils.Memcmp(str1, str2, count);
Exemplo n.º 2
0
        protected byte[] DecryptPrx2(byte[] pbIn, bool showInfo = false)
        {
            var size  = pbIn.Length;
            var pbOut = new byte[size];

            pbIn.CopyTo(pbOut, 0);

            var tmp1Bytes = new byte[0x150];
            var tmp2Bytes = new byte[0x90 + 0x14];
            var tmp3Bytes = new byte[0x60 + 0x14];

            fixed(byte *inbuf = pbIn)
            fixed(byte *outbuf = pbOut)
            fixed(byte *tmp1   = tmp1Bytes)
            fixed(byte *tmp2   = tmp2Bytes)
            fixed(byte *tmp3   = tmp3Bytes)
            {
                //var headerPointer = (HeaderStruct*) inbuf;
                Header = *(HeaderStruct *)inbuf;
                var pti = GetTagInfo2(Header.Tag);

                Console.WriteLine("{0}", pti);

                var retsize = *(int *)&inbuf[0xB0];

                PointerUtils.Memset(tmp1Bytes, 0, 0x150);
                PointerUtils.Memset(tmp2Bytes, 0, 0x90 + 0x14);
                PointerUtils.Memset(tmp3Bytes, 0, 0x60 + 0x14);

                PointerUtils.Memcpy(outbuf, inbuf, size);

                if (size < 0x160)
                {
                    throw new InvalidDataException("buffer not big enough, ");
                }

                if (size - 0x150 < retsize)
                {
                    throw new InvalidDataException("not enough data, ");
                }

                PointerUtils.Memcpy(tmp1, outbuf, 0x150);

                int i, j;

                //byte *p = tmp2+0x14;

                for (i = 0; i < 9; i++)
                {
                    for (j = 0; j < 0x10; j++)
                    {
                        tmp2Bytes[0x14 + (i << 4) + j] = pti.Key[j];
                    }

                    tmp2Bytes[0x14 + (i << 4)] = (byte)i;
                }

                if (Scramble((uint *)tmp2, 0x90, pti.Code) < 0)
                {
                    throw new InvalidDataException("error in Scramble#1, ");
                }

                PointerUtils.Memcpy(outbuf, tmp1 + 0xD0, 0x5C);
                PointerUtils.Memcpy(outbuf + 0x5C, tmp1 + 0x140, 0x10);
                PointerUtils.Memcpy(outbuf + 0x6C, tmp1 + 0x12C, 0x14);
                PointerUtils.Memcpy(outbuf + 0x80, tmp1 + 0x080, 0x30);
                PointerUtils.Memcpy(outbuf + 0xB0, tmp1 + 0x0C0, 0x10);
                PointerUtils.Memcpy(outbuf + 0xC0, tmp1 + 0x0B0, 0x10);
                PointerUtils.Memcpy(outbuf + 0xD0, tmp1 + 0x000, 0x80);

                PointerUtils.Memcpy(tmp3 + 0x14, outbuf + 0x5C, 0x60);

                if (Scramble((uint *)tmp3, 0x60, pti.Code) < 0)
                {
                    throw new InvalidDataException("error in Scramble#2, ");
                }

                PointerUtils.Memcpy(outbuf + 0x5C, tmp3, 0x60);
                PointerUtils.Memcpy(tmp3, outbuf + 0x6C, 0x14);
                PointerUtils.Memcpy(outbuf + 0x70, outbuf + 0x5C, 0x10);
                PointerUtils.Memset(outbuf + 0x18, 0, 0x58);
                PointerUtils.Memcpy(outbuf + 0x04, outbuf, 0x04);

                *((uint *)outbuf) = 0x014C;
                PointerUtils.Memcpy(outbuf + 0x08, tmp2, 0x10);

                /* sha-1 */
                if (_kirk.HleUtilsBufferCopyWithRange(outbuf, 3000000, outbuf, 3000000,
                                                      Kirk.CommandEnum.PspKirkCmdSha1Hash) !=
                    Kirk.ResultEnum.Ok)
                {
                    throw new InvalidDataException("error in sceUtilsBufferCopyWithRange 0xB, ");
                }

                if (PointerUtils.Memcmp(outbuf, tmp3, 0x14) != 0)
                {
                    throw new InvalidDataException("WARNING (SHA-1 incorrect), ");
                }

                int iXor;

                for (iXor = 0; iXor < 0x40; iXor++)
                {
                    tmp3[iXor + 0x14] = (byte)(outbuf[iXor + 0x80] ^ tmp2Bytes[iXor + 0x10]);
                }

                if (Scramble((uint *)tmp3, 0x40, pti.Code) != 0)
                {
                    throw new InvalidDataException("error in Scramble#3, ");
                }

                for (iXor = 0x3F; iXor >= 0; iXor--)
                {
                    outbuf[iXor + 0x40] = (byte)(tmp3Bytes[iXor] ^ tmp2Bytes[iXor + 0x50]);  // uns 8
                }

                PointerUtils.Memset(outbuf + 0x80, 0, 0x30);
                *(uint *)&outbuf[0xA0] = 1;

                PointerUtils.Memcpy(outbuf + 0xB0, outbuf + 0xC0, 0x10);
                PointerUtils.Memset(outbuf + 0xC0, 0, 0x10);

                // the real decryption
                var ret = _kirk.HleUtilsBufferCopyWithRange(outbuf, size, outbuf + 0x40, size - 0x40,
                                                            Kirk.CommandEnum.PspKirkCmdDecryptPrivate);

                if (ret != 0)
                {
                    throw new InvalidDataException(
                              $"error in sceUtilsBufferCopyWithRange 0x1 (0x{ret:X}), ");
                }

                if (retsize < 0x150)
                {
                    // Fill with 0
                    PointerUtils.Memset(outbuf + retsize, 0, 0x150 - retsize);
                }

                return(pbOut.Slice(0, retsize).ToArray());
            }
        }