Exemplo n.º 1
0
 bool xWriteHeader(RSAParams xParams)
 {
     try
     {
         DJsIO xio = new DJsIO(true);
         xHeaderData.Write(ref xio);
         xio.SetLength(0xB000);
         xio.Position = 0x340;
         xio.Write((uint)0xAD0E);
         xio.Position = 0x379;
         xio.Write(new byte[] { 0x24, 5, 5, 0x11 });
         IO.Position = 0x37D;
         xio.Write(IO.ReadBytes(20));
         xio.Write((byte)((xIsShifted ? 1 : 0) << 6));
         xio.WriteUInt24(xBlockCount);
         xio.Write(xDeviation);
         xio.Flush();
         xio.Position = 0x344;
         byte[] xHash = SHA1Quick.ComputeHash(xio.ReadBytes((int)(xio.Length - 0x344)));
         xio.Position = 0x32C;
         xio.Write(xHash);
         xio.Flush();
         xio.Position = 0x22C;
         xHash        = SHA1Quick.ComputeHash(xio.ReadBytes(0x118));
         xio.Position = 4;
         if (xParams.Type == PackageMagic.CON)
         {
             xio.Write(xParams.Certificate);
             xio.Write(ScrambleMethods.StockScramble(RSAQuick.SignatureGenerate(xParams.RSAKeys, xHash), true));
         }
         else
         {
             xio.Write(ScrambleMethods.DevScramble(RSAQuick.SignatureGenerate(xParams.RSAKeys, xHash)));
             xio.Write(new byte[0x128]);
         }
         xio.IsBigEndian = true;
         xio.Position    = 0;
         xio.Write(((uint)xParams.Type));
         xio.Flush();
         xHeaderData.xMagic = xParams.Type;
         IO.Position        = 0;
         IO.Write(xio.ReadStream());
         IO.Flush();
         xio.Dispose();
         return(true);
     }
     catch { return(false); }
 }
Exemplo n.º 2
0
        void XLoadCON(DJsIO xKV)
        {
            int xbase = 0;

            if (xKV.Length == 0x4000)
            {
                xbase = 0x10;
            }
            else if (xKV.Length != 0x3FF0)
            {
                throw CryptoExcepts.KVSize;
            }
            xM           = PackageMagic.CON;
            xKV.Position = 0x9B8 + xbase;
            xC           = xKV.ReadBytes(0x1A8);
            // D is a constant
            xK.D         = XPackage.Resources.XK0;
            xKV.Position = 0x28C + xbase;
            xK.Exponent  = xKV.ReadBytes(4);
            xKV.Position = 0x298 + xbase;
            xK.Modulus   = ScrambleMethods.StockScramble(xKV.ReadBytes(0x80), false);
            xK.P         = ScrambleMethods.StockScramble(xKV.ReadBytes(0x40), false);
            xK.Q         = ScrambleMethods.StockScramble(xKV.ReadBytes(0x40), false);
            xK.DP        = ScrambleMethods.StockScramble(xKV.ReadBytes(0x40), false);
            xK.DQ        = ScrambleMethods.StockScramble(xKV.ReadBytes(0x40), false);
            xK.InverseQ  = ScrambleMethods.StockScramble(xKV.ReadBytes(0x40), false);
            // Checks if the certificate is the same as the imported keys
            byte[] xPiece = xC.BytePiece(0x28, 0x80);
            if (BitConverter.ToString(ScrambleMethods.StockScramble(xPiece, false)) != BitConverter.ToString(xK.Modulus))
            {
                throw CryptoExcepts.CertConflict;
            }
            // Checks if keys work
            try
            {
                RSACryptoServiceProvider x = new RSACryptoServiceProvider();
                x.ImportParameters(xK);
            }
            catch (Exception x) { throw x; }
            xV = true;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Builds the package
        /// </summary>
        /// <param name="xParams"></param>
        /// <param name="ContentType"></param>
        /// <returns></returns>
        public bool Create(RSAParams xParams, PackageType ContentType)
        {
            if (BaseImage == null)
            {
                throw new Exception("No image");
            }
            if (!xParams.Valid)
            {
                throw CryptoExcepts.ParamError;
            }
            if (ContentType != PackageType.HDDInstalledGame &&
                ContentType != PackageType.OriginalXboxGame &&
                ContentType != PackageType.GamesOnDemand &&
                ContentType != PackageType.SocialTitle)
            {
                return(false);
            }
            if (xActive)
            {
                return(false);
            }
            xActive = true;
            DJsIO x = null;
            DJsIO h = null;

            try
            {
                string outlocale = OutLocation.Replace('\\', '/');
                if (outlocale[outlocale.Length - 1] == '/')
                {
                    outlocale = outlocale.Substring(0, outlocale.Length - 1);
                }
                outlocale += '/' + ((uint)ContentType).ToString("X8");
                string DataFolder = outlocale + "/" + xHeader.TitleID.ToString("X8") + ".data";
                if (!VariousFunctions.xCheckDirectory(OutLocation))
                {
                    throw IOExcepts.CreateError;
                }
                if (!VariousFunctions.xCheckDirectory(DataFolder))
                {
                    throw IOExcepts.CreateError;
                }
                uint xBlockCount    = 0;
                uint xDataFileCount = 0;
                long xDataLength    = 0;
                BaseImage.xIO.Position = BaseImage.baseoffset;
                while (BaseImage.xIO.Position < BaseImage.xIO.Length)
                {
                    if ((xBlockCount % Constants.SVODBL[1]) == 0)
                    {
                        if (x != null)
                        {
                            for (int i = 0; i < 0xCB; i++)
                            {
                                x.Position = SVODFuncs.GenerateHashOffset((uint)(i * Constants.SVODBL[0]), 0);
                                byte[] Data1 = SHA1Quick.ComputeHash(x.ReadBytes(0x1000));
                                x.Position = SVODFuncs.GenerateHashOffset((uint)(i * Constants.SVODBL[0]), 1);
                                x.Write(Data1);
                            }
                            x.Flush();
                            xDataLength += x.Length;
                            x.Close();
                        }
                        x = new DJsIO(DataFolder + SVODFuncs.formatstring(xDataFileCount), DJFileMode.Create, true);
                        xDataFileCount++;
                    }
                    x.Position = SVODFuncs.GenerateDataOffset(xBlockCount);
                    byte[] Data = BaseImage.xIO.ReadBytes(0x1000);
                    x.Write(Data);
                    x.Position = SVODFuncs.GenerateHashOffset(xBlockCount, 0);
                    x.Write(SHA1Quick.ComputeHash(Data));
                    xBlockCount++;
                }
                if (xBlockCount == 0)
                {
                    x.Close();
                    return(xActive = false);
                }
                x.Flush();
                xDataLength += x.Length;
                int lvlct = (int)((((xBlockCount % Constants.SVODBL[1]) - 1) / Constants.SVODBL[0]) + 1);
                for (int i = 0; i < lvlct; i++)
                {
                    x.Position = SVODFuncs.GenerateHashOffset((uint)(i * Constants.SVODBL[0]), 0);
                    byte[] Data1 = SHA1Quick.ComputeHash(x.ReadBytes(0x1000));
                    x.Position = SVODFuncs.GenerateHashOffset((uint)(i * Constants.SVODBL[0]), 1);
                    x.Write(Data1);
                }
                x.Flush();
                x.Close();
                byte[] Hash = null;
                for (int i = (int)(xDataFileCount - 1); i >= 0; i--)
                {
                    x = new DJsIO(DataFolder + SVODFuncs.formatstring((uint)i), DJFileMode.Open, true);
                    if (Hash != null)
                    {
                        x.Position = 0xFF0;
                        x.Write(Hash);
                        x.Flush();
                    }
                    x.Position = 0;
                    Hash       = SHA1Quick.ComputeHash(x.ReadBytes(0x1000));
                }
                xHeader.DataFileSize  = xDataLength;
                xHeader.DataFileCount = xDataFileCount;
                xHeader.xThisType     = ContentType;
                h = new DJsIO(outlocale + "/" + xHeader.TitleID.ToString("X8"), DJFileMode.Create, true);
                xHeader.Write(ref h);
                h.SetLength(0xB000);
                h.Position = 0x340;
                h.Write((uint)0xAD0E);
                h.Position = 0x379;
                h.Write(new byte[] { 0x24, 5, 5, 0x11 });
                h.Write(Hash);
                h.Write((byte)((Deviation == 0) ? 0 : 0x40));
                h.WriteUInt24(xBlockCount);
                h.Write(Deviation, false);
                h.Position = 0x344;
                byte[] xHash = SHA1Quick.ComputeHash(h.ReadBytes((int)(h.Length - 0x344)));
                h.Position = 0x32C;
                h.Write(xHash);
                h.Flush();
                h.Position = 0x22C;
                xHash      = SHA1Quick.ComputeHash(h.ReadBytes(0x118));
                h.Position = 4;
                if (xParams.Type == PackageMagic.CON)
                {
                    h.Write(xParams.Certificate);
                    h.Write(ScrambleMethods.StockScramble(RSAQuick.SignatureGenerate(xParams.RSAKeys, xHash), true));
                }
                else
                {
                    h.Write(ScrambleMethods.DevScramble(RSAQuick.SignatureGenerate(xParams.RSAKeys, xHash)));
                    h.Write(new byte[0x128]);
                }
                h.IsBigEndian = true;
                h.Position    = 0;
                h.Write(((uint)xParams.Type));
                xHeader.xMagic = xParams.Type;
                h.Flush();
                h.Close();
                return(!(xActive = false));
            }
            catch
            {
                if (x != null)
                {
                    x.Close();
                }
                if (h != null)
                {
                    h.Close();
                }
                return(xActive = false);
            }
        }