CSPspEmu.Core.Crypto.Kirk.kirk_CMD1 C# (CSharp) Method

kirk_CMD1() public method

Cypher-Block Chaining decoding. Master decryption command, used by firmware modules. Applies CMAC checking.
public kirk_CMD1 ( byte outbuff, byte inbuff, int size, bool do_check = true ) : void
outbuff byte
inbuff byte
size int
do_check bool
return void
        public void kirk_CMD1(byte* outbuff, byte* inbuff, int size, bool do_check = true)
        {
            fixed (Crypto.AES_ctx* aes_kirk1_ptr = &_aes_kirk1)
            {
                check_initialized();
                var header = *(AES128CMACHeader*)inbuff;
                if (header.Mode != KirkMode.Cmd1)
                {
                    //Console.Error.WriteLine("ResultEnum.PSP_KIRK_INVALID_MODE");
                    Console.Error.WriteLine("{0}", header.ToStringDefault(true));
                    Console.WriteLine("Input:");
                    ArrayUtils.HexDump(PointerUtils.PointerToByteArray(inbuff, 0x100));
                    Console.WriteLine("Output:");
                    ArrayUtils.HexDump(PointerUtils.PointerToByteArray(outbuff, 0x100));
                    throw (new KirkException(ResultEnum.PSP_KIRK_INVALID_MODE, String.Format("Expected mode Cmd1 but found {0}", header.Mode)));
                }

                Console.WriteLine("Input:");
                ArrayUtils.HexDump(PointerUtils.PointerToByteArray(inbuff, 0x100));

                //Console.WriteLine("header.DataOffset = 0x{0:X8}", header.DataOffset);
                //Console.WriteLine("header.DataSize = 0x{0:X8}", header.DataSize);

                // Decrypts AES and CMAC keys.

                header_keys keys; //0-15 AES key, 16-31 CMAC key

            #if USE_DOTNET_CRYPTO
                DecryptAes(kirk1_key, inbuff, (byte*)&keys, 16 * 2); //decrypt AES & CMAC key to temp buffer
            #else
                Crypto.AES_cbc_decrypt(aes_kirk1_ptr, inbuff, (byte*)&keys, 16 * 2);
            #endif

                // HOAX WARRING! I have no idea why the hash check on last IPL block fails, so there is an option to disable checking
                if (do_check)
                {
                    kirk_CMD10(inbuff, size);
                }

                //var AES = new RijndaelManaged();

            #if USE_DOTNET_CRYPTO
                DecryptAes(
                    PointerUtils.PointerToByteArray(keys.AES, 16),
                    inbuff + sizeof(AES128CMACHeader) + header->DataOffset,
                    outbuff,
                    header->DataSize
                );
            #else
                Crypto.AES_ctx k1;
                Crypto.AES_set_key(&k1, keys.AES, 128);

                Crypto.AES_cbc_decrypt(
                    ctx: &k1,
                    src: inbuff + sizeof(AES128CMACHeader) + header.DataOffset,
                    dst: outbuff,
                    size: header.DataSize
                );
            #endif
            }
        }

Usage Example

Beispiel #1
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);
            }
        }
All Usage Examples Of CSPspEmu.Core.Crypto.Kirk::kirk_CMD1