Beispiel #1
0
        public void TestSha1()
        {
            var Kirk = new Kirk();
            Kirk.kirk_init();

            var Input = new byte[] {
                // Size
                0x20, 0x00, 0x00, 0x00,
                // Data
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            };

            var ExpectedOutput = new byte[]
            {
                0xDE, 0x8A, 0x84, 0x7B, 0xFF, 0x8C, 0x34, 0x3D, 0x69, 0xB8, 0x53, 0xA2,
                0x15, 0xE6, 0xEE, 0x77, 0x5E, 0xF2, 0xEF, 0x96
            };

            var Output = new byte[0x14];

            Assert.AreEqual(0x24, Input.Length);

            fixed (byte* OutputPtr = Output)
            fixed (byte* InputPtr = Input)
            {
                Kirk.KirkSha1(OutputPtr, InputPtr, Input.Length);
            }

            CollectionAssert.AreEqual(ExpectedOutput, Output);
            //Console.WriteLine(BitConverter.ToString(Hash));
        }
Beispiel #2
0
        public void TestCmd7()
        {
            var Kirk = new Kirk();
            Kirk.kirk_init();

            var _src = new byte[0x20 + 0x14];
            var _dst = new byte[0x20];

            var ExpectedOutput = new byte[]
            {
                0xE4, 0x8B, 0x57, 0x4C, 0x6E, 0xAF, 0x62, 0x51, 0x5C, 0x44, 0x52, 0x1D,
                0xBA, 0x7D, 0xD4, 0x32, 0xE4, 0x8B, 0x57, 0x4C, 0x6E, 0xAF, 0x62, 0x51,
                0x5C, 0x44, 0x52, 0x1D, 0xBA, 0x7D, 0xD4, 0x32
            };

            fixed (byte* src = _src)
            fixed (byte* dst = _dst)
            {
                *(uint*)(&src[0x00]) = 5; // Mode
                *(uint*)(&src[0x0C]) = 0x03; // KeySeed
                *(uint*)(&src[0x10]) = 1; // DataSize

                Kirk.kirk_CMD7(dst, src, 0x20);

                Console.WriteLine("_src: {0}", BitConverter.ToString(_src));
                Console.WriteLine("_dst: {0}", BitConverter.ToString(_dst));

                CollectionAssert.AreEqual(ExpectedOutput, _dst);
            }
        }
Beispiel #3
0
        public void TestCmd1()
        {
            var Kirk = new Kirk();
            Kirk.kirk_init();

            var _src = new byte[0x100];
            var _dst = new byte[0x10];

            var ExpectedOutput = new byte[]
            {
                0xE3, 0x17, 0x49, 0x84, 0xAE, 0xB9, 0xB5, 0xAF, 0x7D, 0x9F, 0x73, 0xAD,
                0x93, 0x66, 0x62, 0xD5
            };

            fixed (byte* src = _src)
            fixed (byte* dst = _dst)
            {
                *(uint*)(&src[0x60]) = 1; // Mode
                *(uint*)(&src[0x70]) = 0x10; // DataSize
                *(uint*)(&src[0x74]) = 0; // DataOffset

                Kirk.kirk_CMD1(dst, src, 0x100, false);

                //Console.WriteLine(BitConverter.ToString(_src));
                //Console.WriteLine(BitConverter.ToString(_dst));

                CollectionAssert.AreEqual(ExpectedOutput, _dst);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_pbIn"></param>
        /// <param name="ShowInfo"></param>
        public byte[] Decrypt(byte[] _pbIn, bool ShowInfo = false)
        {
            this.Kirk = new Kirk();
            this.Kirk.kirk_init();

            return DecryptPRX(_pbIn, ShowInfo);
        }
Beispiel #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="IplData"></param>
        /// <param name="OutputStream"></param>
        /// <param name="ToMemoryAddress"></param>
        /// <returns></returns>
        public static IplInfo DecryptIplToMemory(byte[] IplData, Stream OutputStream, bool ToMemoryAddress = true)
        {
            var buffer = new byte[0x1000];
            var IplInfo = default(IplInfo);

            //ArrayUtils.HexDump(IplData);

            fixed (byte* IplPtr = IplData)
            fixed (byte* bufferPtr = buffer)
            {
                for (int n = 0; n < IplData.Length; n += 0x1000)
                {
                    var Ptr = IplPtr + n;

                    var Header = *(Kirk.AES128CMACHeader*)Ptr;
                    //Console.WriteLine(Header.DataSize);
                    var Kirk = new Kirk();
                    Kirk.kirk_init();
                    Kirk.kirk_CMD1(bufferPtr, Ptr, 0x1000, do_check: false);
                    var IplBlock = *(IplBlock*)bufferPtr;
                    //Console.WriteLine(IplBlock.ToStringDefault());
                    if (ToMemoryAddress)
                    {
                        OutputStream.Position = IplBlock.LoadAddress;
                        Console.WriteLine("IplBlock.LoadAddress: 0x{0:X8}", IplBlock.LoadAddress);
                    }
                    OutputStream.WriteBytes(PointerUtils.PointerToByteArray(&IplBlock.BlockData, (int)IplBlock.BlockSize));
                    if (IplBlock.EntryFunction != 0)
                    {
                        IplInfo.EntryFunction = IplBlock.EntryFunction;
                    }
                }
            }

            return IplInfo;
        }
Beispiel #6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Stream"></param>
        public void Load(byte[] inbuf)
        {
            var Kirk = new Kirk();
            Kirk.kirk_init();
            var outbuf = new byte[(int)inbuf.Length];
            inbuf.CopyTo(outbuf, 0);

            fixed (byte* _inbuf = inbuf)
            fixed (byte* _outbuf = outbuf)
            {
                var HeaderPointer = (HeaderStruct*)_inbuf;
                this.Header = *(HeaderStruct*)_inbuf;
                var pti = g_oldTagInfo.Where(Tag => Tag.tag == this.Header.tag).Single();

                for (int i = 0; i < 0x150; i++)
                {
                    outbuf[i] = 0;
                }
                for (int i = 0; i < 0x40; i++)
                {
                    outbuf[i] = 0x55;
                }

                //HeaderPointer->psp_size = 5;
                //HeaderPointer->boot_entry = 0;

                var h7_header = (Kirk.KIRK_AES128CBC_HEADER*)&_outbuf[0x2C];
                h7_header->Mode = 5;
                h7_header->Unknown4 = 0;
                h7_header->Unknown8 = 0;
                h7_header->KeySeed = pti.code;
                h7_header->Datasize = 0x70;

                byte[] header = new byte[0x150];

                Array.Copy(inbuf, 0xD0, header, 0x00, 0x80);
                Array.Copy(inbuf, 0x80, header, 0x80, 0x50);
                Array.Copy(inbuf, 0x00, header, 0xD0, 0x80);

                if (pti.codeExtra != 0)
                {
                    //ScramblePRXV2(header, (byte)pti.codeExtra);
                    throw (new NotImplementedException());
                }

                Array.Copy(header, 0x40, outbuf, 0x40, 0x40);

                for (int iXOR = 0; iXOR < 0x70; iXOR++) outbuf[0x40 + iXOR] = (byte)(outbuf[0x40 + iXOR] ^ (byte)pti.key[0x14 + iXOR]);
                for (int k = 0; k < 0x30; k++) outbuf[k + 0x80] = 0;

                // Scramble the data by calling CMD7.
                var bScrambleOut = new byte[outbuf.Length];
                var bScrambleIn = outbuf;
                fixed (byte* _bScrambleOut = bScrambleOut)
                {
                    Kirk.hleUtilsBufferCopyWithRange(_bScrambleOut, 0x70, &_outbuf[0x2C], 0x70, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT);
                }

                for (int iXOR = 0x6F; iXOR >= 0; iXOR--)
                {
                    outbuf[0x40 + iXOR] = (byte)(outbuf[0x2C + iXOR] ^ (byte)pti.key[0x20 + iXOR]);
                }

                for (int k = 0; k < 0x30; k++)
                {
                    outbuf[k + 0x80] = 0;
                }

                var KirkHeader = (Kirk.AES128CMACHeader *)&_outbuf[0x40];
                KirkHeader->Mode = (uint)Kirk.KIRK_MODE_CMD1;

                Array.Copy(inbuf, 0x40 + 0x70, outbuf, 0x40 + 0x70, 0x20);
                Array.Copy(inbuf, 0, outbuf, 0x40 + 0x90, 0x80);

                // Call KIRK CMD1 for final decryption.
                /*
                ByteBuffer bDataOut = ByteBuffer.wrap(outbuf);
                ByteBuffer bHeaderIn = bDataOut.duplicate();
                bHeaderIn.position(0x40);
                hleUtilsBufferCopyWithRange(bDataOut, size, bHeaderIn, size, 0x1);
                */

            //#if true
                {
                    int size = outbuf.Length;
                    var Result = Kirk.hleUtilsBufferCopyWithRange(&_outbuf[0x00], size, (byte *)KirkHeader, size, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT_PRIVATE, true);
                    Console.WriteLine(Result);
                }
            //#endif
                File.WriteAllBytes("../../../TestInput/temp.bin", outbuf);

                //File.write
                //outbuf

                //Kirk.kirk_CMD1();
                //Console.WriteLine("{0:X}", this.Header.tag);
            }
        }
Beispiel #7
0
 public LleKirk(DebugPspMemory Memory)
 {
     this.Memory = Memory;
     Kirk = new Kirk();
     Kirk.kirk_init();
 }