Ejemplo n.º 1
0
        // Encrypt with AESCBC128 using keys from table.
        private int executeKIRKCmd5(ByteBuffer @out, ByteBuffer @in, int size)
        {
            // Return an error if the crypto engine hasn't been initialized.
            if (!CryptoEngine.CryptoEngineStatus)
            {
                return(PSP_KIRK_NOT_INIT);
            }

            int outPosition = @out.position();

            // Read in the CMD4 format header.
            AES128_CBC_Header header = new AES128_CBC_Header(@in);

            if (header.mode != PSP_KIRK_CMD_MODE_ENCRYPT_CBC)
            {
                return(PSP_KIRK_INVALID_MODE);                // Only valid for mode ENCRYPT_CBC.
            }

            if (header.dataSize == 0)
            {
                return(PSP_KIRK_DATA_SIZE_IS_ZERO);
            }

            sbyte[] key = null;
            if (header.keySeed == 0x100)
            {
                key = priv_iv;
            }
            else
            {
                return(PSP_KIRK_INVALID_SIZE);                // Dummy.
            }

            sbyte[] encKey = new sbyte[16];
            for (int i = 0; i < encKey.Length; i++)
            {
                encKey[i] = (sbyte)key[i];
            }

            AES128 aes = new AES128("AES/CBC/NoPadding");

            sbyte[] inBuf = new sbyte[header.dataSize];
            @in.get(inBuf, 0, header.dataSize);
            sbyte[] outBuf = aes.encrypt(inBuf, encKey, priv_iv);

            @out.position(outPosition);
            // The header is kept in the output and the header.mode is even updated from
            // PSP_KIRK_CMD_MODE_ENCRYPT_CBC to PSP_KIRK_CMD_MODE_DECRYPT_CBC.
            @out.putInt(PSP_KIRK_CMD_MODE_DECRYPT_CBC);
            @out.putInt(header.unk1);
            @out.putInt(header.unk2);
            @out.putInt(header.keySeed);
            @out.putInt(header.dataSize);
            @out.put(outBuf);
            @in.clear();

            return(0);
        }
Ejemplo n.º 2
0
        // Decrypt with AESCBC128 using keys from table.
        private int executeKIRKCmd7(ByteBuffer @out, ByteBuffer @in, int size)
        {
            // Return an error if the crypto engine hasn't been initialized.
            if (!CryptoEngine.CryptoEngineStatus)
            {
                return(PSP_KIRK_NOT_INIT);
            }

            int outPosition = @out.position();

            // Read in the CMD7 format header.
            AES128_CBC_Header header = new AES128_CBC_Header(@in);

            if (header.mode != PSP_KIRK_CMD_MODE_DECRYPT_CBC)
            {
                return(PSP_KIRK_INVALID_MODE);                // Only valid for mode DECRYPT_CBC.
            }

            if (header.dataSize == 0)
            {
                return(PSP_KIRK_DATA_SIZE_IS_ZERO);
            }

            int[] key = getAESKeyFromSeed(header.keySeed);
            if (key == null)
            {
                return(PSP_KIRK_INVALID_SEED);
            }

            sbyte[] decKey = new sbyte[16];
            for (int i = 0; i < decKey.Length; i++)
            {
                decKey[i] = (sbyte)key[i];
            }

            AES128 aes = new AES128("AES/CBC/NoPadding");

            sbyte[] inBuf = new sbyte[header.dataSize];
            @in.get(inBuf, 0, header.dataSize);
            sbyte[] outBuf = aes.decrypt(inBuf, decKey, priv_iv);

            @out.position(outPosition);
            @out.put(outBuf);
            @in.clear();

            return(0);
        }