private void getHashKeys(int hashFlag, byte[] calculatedHash, byte[] hash)
        {
            uint mode = (uint)hashFlag & 0xF0000000;

            switch (mode)
            {
            case 0x10000000:
                ToolsImpl.aescbcDecrypt(EDATKeys.EDATKEY, EDATKeys.EDATIV, hash, 0, calculatedHash, 0, calculatedHash.Length);
                Debug.WriteLine("MODE: Encrypted HASHKEY");
                break;

            case 0x20000000:
                ConversionUtils.arraycopy(EDATKeys.EDATHASH, 0, calculatedHash, 0, calculatedHash.Length);
                Debug.WriteLine("MODE: Default HASHKEY");
                break;

            case 0x00000000:
                ConversionUtils.arraycopy(hash, 0, calculatedHash, 0, calculatedHash.Length);
                Debug.WriteLine("MODE: Unencrypted HASHKEY");
                break;

            default:
                throw new Exception("Hash mode is not valid: Undefined keys calculator");
            }
        }
Beispiel #2
0
        public byte[] getrifKey(string rifIn, string actIn, string idps)
        {
            if ((rifIn == null) || (actIn == null))
            {
                return(null);
            }
            byte[]     o      = null;
            FileStream stream = File.Open(rifIn, FileMode.Open);

            byte[] buffer2 = new byte[0x10];
            byte[] buffer3 = new byte[0x10];
            byte[] buffer  = new byte[0x10];
            byte[] buffer5 = new byte[0x10];
            stream.Seek(0x40L, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            stream.Read(buffer5, 0, buffer5.Length);
            stream.Close();
            ToolsImpl.aesecbDecrypt(RIFKEY, buffer, 0, buffer2, 0, 0x10);
            long num = ConversionUtils.be32(buffer2, 12);

            if (num < 0x80L)
            {
                byte[] src  = decryptACTDAT(actIn, idps);
                byte[] dest = new byte[0x10];
                o = new byte[0x10];
                ConversionUtils.arraycopy(src, ((int)num) * 0x10, dest, 0L, 0x10);
                ToolsImpl.aesecbDecrypt(dest, buffer5, 0, o, 0, 0x10);
            }
            return(o);
        }
Beispiel #3
0
        private void getCryptoKeys(int cryptoFlag, byte[] calculatedKey, byte[] calculatedIV, byte[] key, byte[] iv)
        {
            switch (((uint)(cryptoFlag & -268435456)))
            {
            case 0:
                ConversionUtils.arraycopy(key, 0, calculatedKey, 0L, calculatedKey.Length);
                ConversionUtils.arraycopy(iv, 0, calculatedIV, 0L, calculatedIV.Length);
                Debug.WriteLine("MODE: Unencrypted ERK");
                break;

            case 0x10000000:
                ToolsImpl.aescbcDecrypt(EDATKeys.EDATKEY, EDATKeys.EDATIV, key, 0, calculatedKey, 0, calculatedKey.Length);
                ConversionUtils.arraycopy(iv, 0, calculatedIV, 0L, calculatedIV.Length);
                Debug.WriteLine("MODE: Encrypted ERK");
                break;

            case 0x20000000:
                ConversionUtils.arraycopy(EDATKeys.EDATKEY, 0, calculatedKey, 0L, calculatedKey.Length);
                ConversionUtils.arraycopy(EDATKeys.EDATIV, 0, calculatedIV, 0L, calculatedIV.Length);
                Debug.WriteLine("MODE: Default ERK");
                break;

            default:
                throw new Exception("Crypto mode is not valid: Undefined keys calculator");
            }
        }
Beispiel #4
0
        public override void doUpdate(byte[] i, int inOffset, int len)
        {
            byte[] data;
            if (nonProcessed != null)
            {
                int totalLen = len + nonProcessed.Length;
                data = new byte[totalLen];
                ConversionUtils.arraycopy(nonProcessed, 0, data, 0, nonProcessed.Length);
                ConversionUtils.arraycopy(i, inOffset, data, nonProcessed.Length, len);
            }
            else
            {
                data = new byte[len];
                ConversionUtils.arraycopy(i, inOffset, data, 0, len);
            }
            int count = 0;

            while (count < data.Length - 0x10)
            {
                byte[] aux = new byte[0x10];
                ConversionUtils.arraycopy(data, count, aux, 0, aux.Length);
                ToolsImpl.XOR(aux, aux, previous);
                ToolsImpl.aesecbEncrypt(key, aux, 0, previous, 0, aux.Length);
                count += 0x10;
            }
            nonProcessed = new byte[data.Length - count];
            ConversionUtils.arraycopy(data, count, nonProcessed, 0, nonProcessed.Length);
        }
Beispiel #5
0
        public override void doUpdate(byte[] i, int inOffset, int len)
        {
            byte[] buffer;
            if (this.nonProcessed != null)
            {
                int num = len + this.nonProcessed.Length;
                buffer = new byte[num];
                ConversionUtils.arraycopy(this.nonProcessed, 0, buffer, 0L, this.nonProcessed.Length);
                ConversionUtils.arraycopy(i, inOffset, buffer, (long)this.nonProcessed.Length, len);
            }
            else
            {
                buffer = new byte[len];
                ConversionUtils.arraycopy(i, inOffset, buffer, 0L, len);
            }
            int srcPos = 0;

            while (srcPos < (buffer.Length - 0x10))
            {
                byte[] dest = new byte[0x10];
                ConversionUtils.arraycopy(buffer, srcPos, dest, 0L, dest.Length);
                ToolsImpl.XOR(dest, dest, this.previous);
                ToolsImpl.aesecbEncrypt(this.key, dest, 0, this.previous, 0, dest.Length);
                srcPos += 0x10;
            }
            this.nonProcessed = new byte[buffer.Length - srcPos];
            ConversionUtils.arraycopy(buffer, srcPos, this.nonProcessed, 0L, this.nonProcessed.Length);
        }
Beispiel #6
0
        public byte[] getrifKey(String rifIn, String actIn, String idps)
        {
            if (rifIn == null || actIn == null)
            {
                return(null);
            }
            byte[]     result  = null;
            FileStream rifFile = File.Open(rifIn, FileMode.Open);

            byte[] rif0x40    = new byte[0x10];
            byte[] rif0x50    = new byte[0x10];
            byte[] encrif0x40 = new byte[0x10];
            byte[] encrif0x50 = new byte[0x10];
            rifFile.Seek(0x40, SeekOrigin.Begin);
            rifFile.Read(encrif0x40, 0, encrif0x40.Length);
            rifFile.Read(encrif0x50, 0, encrif0x50.Length);
            rifFile.Close();
            ToolsImpl.aesecbDecrypt(RIFKEY, encrif0x40, 0x00, rif0x40, 0, 0x10);
            long index = ConversionUtils.be32(rif0x40, 0xC); //

            if (index < 0x80)
            {
                byte[] actDat = decryptACTDAT(actIn, idps);
                byte[] datKey = new byte[0x10];
                result = new byte[0x10];
                ConversionUtils.arraycopy(actDat, (int)index * 16, datKey, 0, 0x10);
                ToolsImpl.aesecbDecrypt(datKey, encrif0x50, 0, result, 0, 0x10);
            }
            return(result);
        }
Beispiel #7
0
        private static byte[] getPerConsoleKey(String IDPSFile)
        {
            FileStream raf = File.Open(IDPSFile, FileMode.Open);

            byte[] idps = new byte[0x10];
            raf.Read(idps, 0, idps.Length);
            raf.Close();
            byte[] result = new byte[0x10];
            ToolsImpl.aesecbEncrypt(idps, ACTDAT_KEY, 0, result, 0, ACTDAT_KEY.Length);
            return(result);
        }
Beispiel #8
0
        private static byte[] getPerConsoleKey(string IDPSFile)
        {
            FileStream stream = File.Open(IDPSFile, FileMode.Open);

            byte[] buffer = new byte[0x10];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            byte[] o = new byte[0x10];
            ToolsImpl.aesecbEncrypt(buffer, ACTDAT_KEY, 0, o, 0, ACTDAT_KEY.Length);
            return(o);
        }
Beispiel #9
0
        private static byte[] decryptACTDAT(string actIn, string IDPSFile)
        {
            FileStream stream = File.Open(actIn, FileMode.Open);

            byte[] buffer = new byte[0x800];
            byte[] o      = new byte[buffer.Length];
            stream.Seek(0x10L, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            ToolsImpl.aesecbDecrypt(getPerConsoleKey(IDPSFile), buffer, 0, o, 0, buffer.Length);
            return(o);
        }
Beispiel #10
0
 private byte[] createNPDHash2(byte[] klicensee, byte[] npd)
 {
     byte[] output = new byte[0x10];
     ToolsImpl.XOR(output, klicensee, EDATKeys.npdrm_omac_key2);
     byte[] src = ToolsImpl.CMAC128(output, npd, 0, 0x60);
     ConversionUtils.arraycopy(src, 0, npd, 0x60L, 0x10);
     if (this.compareBytes(src, 0, npd, 0x60, 0x10))
     {
         return(src);
     }
     return(null);
 }
Beispiel #11
0
        private static byte[] decryptACTDAT(String actIn, String IDPSFile)
        {
            FileStream actFile = File.Open(actIn, FileMode.Open);

            byte[] actdat = new byte[0x800];
            byte[] result = new byte[actdat.Length];
            actFile.Seek(0x10, SeekOrigin.Begin);
            actFile.Read(actdat, 0, actdat.Length);
            actFile.Close();
            byte[] key = getPerConsoleKey(IDPSFile);
            ToolsImpl.aesecbDecrypt(key, actdat, 0, result, 0, actdat.Length);
            return(result);
        }
Beispiel #12
0
        private bool checkNPDHash2(byte[] klicensee, byte[] npd)
        {
            byte[] output = new byte[0x10];
            ToolsImpl.XOR(output, klicensee, EDATKeys.npdrm_omac_key2);
            byte[] buffer2 = ToolsImpl.CMAC128(output, npd, 0, 0x60);
            bool   flag    = this.compareBytes(buffer2, 0, npd, 0x60, 0x10);

            if (flag)
            {
                Console.WriteLine("NPD hash 2 is valid (" + ConversionUtils.getHexString(buffer2) + ")");
            }
            return(flag);
        }
Beispiel #13
0
        private bool checkNPDHash2(byte[] klicensee, byte[] npd)
        {
            byte[] xoredKey = new byte[0x10];
            ToolsImpl.XOR(xoredKey, klicensee, EDATKeys.npdrm_omac_key2);
            byte[] calculated = ToolsImpl.CMAC128(xoredKey, npd, 0, 0x60);
            bool   result2    = compareBytes(calculated, 0, npd, 0x60, 0x10);

            if (result2)
            {
                Console.WriteLine("NPD hash 2 is valid (" + ConversionUtils.getHexString(calculated) + ")");
            }
            return(result2);
        }
Beispiel #14
0
        private byte[] createNPDHash2(byte[] klicensee, byte[] npd)
        {
            byte[] xoredKey = new byte[0x10];
            ToolsImpl.XOR(xoredKey, klicensee, EDATKeys.npdrm_omac_key2);
            byte[] calculated = ToolsImpl.CMAC128(xoredKey, npd, 0, 0x60);
            ConversionUtils.arraycopy(calculated, 0, npd, 0x60, 0x10);
            bool result2 = compareBytes(calculated, 0, npd, 0x60, 0x10);

            if (result2)
            {
                return(calculated);
            }
            return(null);
        }
Beispiel #15
0
 private byte[] createNPDHash1(string filename, byte[] npd)
 {
     byte[] src  = ConversionUtils.charsToByte(filename.ToCharArray());
     byte[] dest = new byte[0x30 + src.Length];
     ConversionUtils.arraycopy(npd, 0x10, dest, 0L, 0x30);
     ConversionUtils.arraycopy(src, 0, dest, 0x30L, src.Length);
     byte[] buffer3 = ToolsImpl.CMAC128(EDATKeys.npdrm_omac_key3, dest, 0, dest.Length);
     ConversionUtils.arraycopy(buffer3, 0, npd, 80L, 0x10);
     if (this.compareBytes(buffer3, 0, npd, 80, 0x10))
     {
         return(buffer3);
     }
     return(null);
 }
Beispiel #16
0
        private bool checkNPDHash1(String filename, byte[] npd)
        {
            byte[] fileBytes = ConversionUtils.charsToByte(filename.ToCharArray());
            byte[] data1     = new byte[0x30 + fileBytes.Length];
            ConversionUtils.arraycopy(npd, 0x10, data1, 0, 0x30);
            ConversionUtils.arraycopy(fileBytes, 0x00, data1, 0x30, fileBytes.Length);
            byte[] hash1   = ToolsImpl.CMAC128(EDATKeys.npdrm_omac_key3, data1, 0, data1.Length);
            bool   result1 = compareBytes(hash1, 0, npd, 0x50, 0x10);

            if (result1)
            {
                Console.WriteLine("NPD hash 1 is valid (" + ConversionUtils.getHexString(hash1) + ")");
            }
            return(result1);
        }
Beispiel #17
0
        private bool checkNPDHash1(string filename, byte[] npd)
        {
            byte[] src  = ConversionUtils.charsToByte(filename.ToCharArray());
            byte[] dest = new byte[0x30 + src.Length];
            ConversionUtils.arraycopy(npd, 0x10, dest, 0L, 0x30);
            ConversionUtils.arraycopy(src, 0, dest, 0x30L, src.Length);
            byte[] buffer3 = ToolsImpl.CMAC128(EDATKeys.npdrm_omac_key3, dest, 0, dest.Length);
            bool   flag    = this.compareBytes(buffer3, 0, npd, 80, 0x10);

            if (flag)
            {
                Console.WriteLine("NPD hash 1 is valid (" + ConversionUtils.getHexString(buffer3) + ")");
            }
            return(flag);
        }
Beispiel #18
0
        private byte[] createNPDHash1(String filename, byte[] npd)
        {
            byte[] fileBytes = ConversionUtils.charsToByte(filename.ToCharArray());
            byte[] data1     = new byte[0x30 + fileBytes.Length];
            ConversionUtils.arraycopy(npd, 0x10, data1, 0, 0x30);
            ConversionUtils.arraycopy(fileBytes, 0x00, data1, 0x30, fileBytes.Length);
            byte[] hash1 = ToolsImpl.CMAC128(EDATKeys.npdrm_omac_key3, data1, 0, data1.Length);
            ConversionUtils.arraycopy(hash1, 0, npd, 0x50, 0x10);
            bool result1 = compareBytes(hash1, 0, npd, 0x50, 0x10);

            if (result1)
            {
                return(hash1);
            }
            return(null);
        }
        private static void calculateSubkey(byte[] key, byte[] K1, byte[] K2)
        {
            byte[] zero = new byte[0x10];
            byte[] L    = new byte[0x10];
            ToolsImpl.aesecbEncrypt(key, zero, 0, L, 0, zero.Length);
            BigInteger aux = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(L));

            if ((L[0] & 0x80) != 0)
            {
                //Case MSB is set
                aux = (aux << 1) ^ (new BigInteger(0x87));
            }
            else
            {
                aux = aux << 1;
            }
            byte[] aux1 = ConversionUtils.reverseByteWithSizeFIX(aux.ToByteArray());
            if (aux1.Length >= 0x10)
            {
                ConversionUtils.arraycopy(aux1, aux1.Length - 0x10, K1, 0, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(zero, 0, K1, 0, zero.Length);
                ConversionUtils.arraycopy(aux1, 0, K1, 0x10 - aux1.Length, aux1.Length);
            }
            aux = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(K1));

            if ((K1[0] & 0x80) != 0)
            {
                aux = (aux << 1) ^ (new BigInteger(0x87));
            }
            else
            {
                aux = aux << 1;
            }
            aux1 = ConversionUtils.reverseByteWithSizeFIX(aux.ToByteArray());
            if (aux1.Length >= 0x10)
            {
                ConversionUtils.arraycopy(aux1, aux1.Length - 0x10, K2, 0, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(zero, 0, K2, 0, zero.Length);
                ConversionUtils.arraycopy(aux1, 0, K2, 0x10 - aux1.Length, aux1.Length);
            }
        }
Beispiel #20
0
 public override bool doFinal(byte[] generateHash)
 {
     byte[] dest = new byte[0x10];
     ConversionUtils.arraycopy(this.nonProcessed, 0, dest, 0L, this.nonProcessed.Length);
     if (this.nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(dest, dest, this.K1);
     }
     else
     {
         dest[this.nonProcessed.Length] = 0x80;
         ToolsImpl.XOR(dest, dest, this.K2);
     }
     ToolsImpl.XOR(dest, dest, this.previous);
     ToolsImpl.aesecbEncrypt(this.key, dest, 0, generateHash, 0, dest.Length);
     return(true);
 }
Beispiel #21
0
 public override bool doFinal(byte[] generateHash)
 {
     byte[] aux = new byte[0x10];
     ConversionUtils.arraycopy(nonProcessed, 0, aux, 0, nonProcessed.Length);
     if (nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(aux, aux, K1);
     }
     else
     {
         aux[nonProcessed.Length] = (byte)0x80;
         ToolsImpl.XOR(aux, aux, K2);
     }
     ToolsImpl.XOR(aux, aux, previous);
     ToolsImpl.aesecbEncrypt(key, aux, 0, generateHash, 0, aux.Length);
     return(true);
 }
Beispiel #22
0
        private int encryptData(FileStream ii, FileStream o, NPD npd, EDATData data, byte[] rifkey)
        {
            int num = (int)(((data.getFileLen() + data.getBlockSize()) - 1) / data.getBlockSize());

            byte[] dest    = new byte[num * 0x10];
            byte[] buffer2 = new byte[ii.Length + 15L];
            for (int i = 0; i < num; i++)
            {
                long offset = i * data.getBlockSize();
                ii.Seek(offset, SeekOrigin.Begin);
                int length = (int)data.getBlockSize();
                if (i == (num - 1))
                {
                    length = (int)(data.getFileLen() % new BigInteger(data.getBlockSize()));
                }
                int num5 = length;
                length = (length + 15) & -16;
                byte[] buffer3 = new byte[length];
                byte[] buffer4 = new byte[length];
                for (int j = num5; j > 0; j -= ii.Read(buffer4, num5 - j, j))
                {
                }
                for (int k = num5; k < length; k++)
                {
                    buffer4[k] = 0;
                }
                byte[] buffer5 = new byte[0x10];
                byte[] buffer6 = new byte[0x10];
                byte[] buffer7 = this.calculateBlockKey(i, npd);
                ToolsImpl.aesecbEncrypt(rifkey, buffer7, 0, buffer5, 0, buffer7.Length);
                ConversionUtils.arraycopy(buffer5, 0, buffer6, 0L, buffer5.Length);
                int cryptoFlag                 = 2;
                int hashFlag                   = 2;
                AppLoaderReverse reverse       = new AppLoaderReverse();
                byte[]           iv            = npd.getDigest();
                byte[]           generatedHash = new byte[0x10];
                reverse.doAll(hashFlag, cryptoFlag, buffer4, 0, buffer3, 0, buffer4.Length, buffer5, iv, buffer6, generatedHash, 0);
                ConversionUtils.arraycopy(buffer3, 0, buffer2, offset, length);
                ConversionUtils.arraycopy(generatedHash, 0, dest, (long)(i * 0x10), 0x10);
            }
            byte[] buffer = ConversionUtils.getByteArray("4D6164652062792052325220546F6F6C");
            o.Write(dest, 0, dest.Length);
            o.Write(buffer2, 0, buffer2.Length - 15);
            o.Write(buffer, 0, buffer.Length);
            return(STATUS_OK);
        }
Beispiel #23
0
        private void calculateSubkey(byte[] key, byte[] K1, byte[] K2)
        {
            byte[] i = new byte[0x10];
            byte[] o = new byte[0x10];
            ToolsImpl.aesecbEncrypt(key, i, 0, o, 0, i.Length);
            BigInteger integer = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(o));

            if ((o[0] & 0x80) != 0)
            {
                integer = (integer << 1) ^ new BigInteger(0x87);
            }
            else
            {
                integer = integer << 1;
            }
            byte[] src = ConversionUtils.reverseByteWithSizeFIX(integer.ToByteArray());
            if (src.Length >= 0x10)
            {
                ConversionUtils.arraycopy(src, src.Length - 0x10, K1, 0L, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(i, 0, K1, 0L, i.Length);
                ConversionUtils.arraycopy(src, 0, K1, (long)(0x10 - src.Length), src.Length);
            }
            integer = new BigInteger(ConversionUtils.reverseByteWithSizeFIX(K1));
            if ((K1[0] & 0x80) != 0)
            {
                integer = (integer << 1) ^ new BigInteger(0x87);
            }
            else
            {
                integer = integer << 1;
            }
            src = ConversionUtils.reverseByteWithSizeFIX(integer.ToByteArray());
            if (src.Length >= 0x10)
            {
                ConversionUtils.arraycopy(src, src.Length - 0x10, K2, 0L, 0x10);
            }
            else
            {
                ConversionUtils.arraycopy(i, 0, K2, 0L, i.Length);
                ConversionUtils.arraycopy(src, 0, K2, (long)(0x10 - src.Length), src.Length);
            }
        }
Beispiel #24
0
 public override bool doFinal(byte[] expectedhash, int hashOffset, bool hashDebug)
 {
     byte[] dest = new byte[0x10];
     ConversionUtils.arraycopy(this.nonProcessed, 0, dest, 0L, this.nonProcessed.Length);
     if (this.nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(dest, dest, this.K1);
     }
     else
     {
         dest[this.nonProcessed.Length] = 0x80;
         ToolsImpl.XOR(dest, dest, this.K2);
     }
     ToolsImpl.XOR(dest, dest, this.previous);
     byte[] o = new byte[0x10];
     ToolsImpl.aesecbEncrypt(this.key, dest, 0, o, 0, dest.Length);
     return(hashDebug || base.compareBytes(expectedhash, hashOffset, o, 0, this.hashLen));
 }
Beispiel #25
0
 public override bool doFinal(byte[] expectedhash, int hashOffset, bool hashDebug)
 {
     byte[] aux = new byte[0x10];
     ConversionUtils.arraycopy(nonProcessed, 0, aux, 0, nonProcessed.Length);
     if (nonProcessed.Length == 0x10)
     {
         ToolsImpl.XOR(aux, aux, K1);
     }
     else
     {
         aux[nonProcessed.Length] = (byte)0x80;
         ToolsImpl.XOR(aux, aux, K2);
     }
     ToolsImpl.XOR(aux, aux, previous);
     byte[] calculatedhash = new byte[0x10];
     ToolsImpl.aesecbEncrypt(key, aux, 0, calculatedhash, 0, aux.Length);
     return(hashDebug || compareBytes(expectedhash, hashOffset, calculatedhash, 0, hashLen));
 }
Beispiel #26
0
 private byte[] getKey(NPD npd, EDATData data, byte[] devKLic, byte[] keyFromRif)
 {
     byte[] output = null;
     if ((data.getFlags() & FLAG_SDAT) != 0L)
     {
         output = new byte[0x10];
         ToolsImpl.XOR(output, npd.getDevHash(), EDATKeys.SDATKEY);
         return(output);
     }
     if (npd.getLicense() == 3L)
     {
         return(devKLic);
     }
     if (npd.getLicense() == 2L)
     {
         output = keyFromRif;
     }
     return(output);
 }
Beispiel #27
0
        public byte[] getKey(string rapFile)
        {
            BinaryReader reader = new BinaryReader(File.OpenRead(rapFile));

            byte[] i = reader.ReadBytes(0x10);
            reader.Close();
            byte[] o = new byte[i.Length];
            ToolsImpl.aesecbDecrypt(rapKey, i, 0, o, 0, i.Length);
            for (int j = 0; j < 5; j++)
            {
                int num3;
                int index = 0;
                while (index < 0x10)
                {
                    num3    = indexTable[index];
                    o[num3] = (byte)(o[num3] ^ key1[num3]);
                    index++;
                }
                index = 15;
                while (index > 0)
                {
                    int num4 = indexTable[index];
                    int num5 = indexTable[index - 1];
                    o[num4] = (byte)(o[num4] ^ o[num5]);
                    index--;
                }
                int num6 = 0;
                for (index = 0; index < 0x10; index++)
                {
                    num3 = indexTable[index];
                    byte num7 = (byte)(o[num3] - num6);
                    o[num3] = num7;
                    if ((num6 != 1) || (num7 != 0xff))
                    {
                        int num8 = num7 & 0xff;
                        int num9 = key2[num3] & 0xff;
                        num6 = (num8 < num9) ? 1 : 0;
                    }
                    o[num3] = (byte)(num7 - key2[num3]);
                }
            }
            return(o);
        }
        public static byte[] CMAC128(byte[] key, byte[] i, int inOffset, int len)
        {
            byte[] K1 = new byte[0x10];
            byte[] K2 = new byte[0x10];
            calculateSubkey(key, K1, K2);
            byte[] input         = new byte[0x10];
            byte[] previous      = new byte[0x10];
            int    currentOffset = inOffset;
            int    remaining     = len;

            //System.out.println(ConversionUtils.getHexString(key));
            while (remaining > 0x10)
            {
                ConversionUtils.arraycopy(i, currentOffset, input, 0, 0x10);
                //System.out.println(ConversionUtils.getHexString(previous) + " - " + ConversionUtils.getHexString(input));
                XOR(input, input, previous);

                ToolsImpl.aesecbEncrypt(key, input, 0, previous, 0, input.Length);
                currentOffset += 0x10;
                remaining     -= 0x10;
            }
            //System.out.println(ConversionUtils.getHexString(previous));
            input = new byte[0x10]; //Memset 0
            ConversionUtils.arraycopy(i, currentOffset, input, 0, remaining);
            if (remaining == 0x10)
            {
                XOR(input, input, previous);
                XOR(input, input, K1);
            }
            else
            {
                input[remaining] = (byte)0x80;
                XOR(input, input, previous);
                XOR(input, input, K2);
            }
            //System.out.println(" - " + ConversionUtils.getHexString(input));
            ToolsImpl.aesecbEncrypt(key, input, 0, previous, 0, input.Length);
            return(previous);
        }
Beispiel #29
0
        public byte[] getKey(String rapFile)
        {
            BinaryReader raf = new BinaryReader(File.OpenRead(rapFile));

            byte[] read = raf.ReadBytes(0x10);
            raf.Close();
            byte[] intermediate = new byte[read.Length];
            ToolsImpl.aesecbDecrypt(rapKey, read, 0, intermediate, 0, read.Length);
            for (int loop = 0; loop < 5; loop++)
            {
                for (int loop2 = 0; loop2 < 0x10; loop2++)
                {
                    int index = indexTable[loop2];
                    intermediate[index] = (byte)(intermediate[index] ^ key1[index]);
                }
                for (int loop2 = 0xF; loop2 > 0; loop2--)
                {
                    int index1 = indexTable[loop2];
                    int index2 = indexTable[loop2 - 1];
                    intermediate[index1] = (byte)(intermediate[index1] ^ intermediate[index2]);
                }
                int acum = 0;
                for (int loop2 = 0; loop2 < 0x10; loop2++)
                {
                    int  index   = indexTable[loop2];
                    byte current = (byte)(intermediate[index] - acum);
                    intermediate[index] = current;
                    if (acum != 1 || current != 0xFF)
                    {
                        int aux1 = current & 0xFF;
                        int aux2 = key2[index] & 0xFF;
                        acum = (aux1 < aux2) ? 1 : 0;
                    }
                    current             = (byte)(current - key2[index]);
                    intermediate[index] = current;
                }
            }
            return(intermediate);
        }
Beispiel #30
0
 private byte[] getKey(NPD npd, EDATData data, byte[] devKLic, byte[] keyFromRif)
 {
     byte[] result = null;
     if ((data.getFlags() & FLAG_SDAT) != 0)
     {
         //Case SDAT
         result = new byte[0x10];
         ToolsImpl.XOR(result, npd.getDevHash(), EDATKeys.SDATKEY);
     }
     else
     {
         //Case EDAT
         if (npd.getLicense() == 0x03)
         {
             result = devKLic;
         }
         else if (npd.getLicense() == 0x02)
         {
             result = keyFromRif;
         }
     }
     return(result);
 }