Example #1
0
        public Nax0(Keyset keyset, Stream stream, string sdPath, bool keepOpen)
        {
            stream.Position = 0;
            KeepOpen        = keepOpen;
            ReadHeader(stream);
            DeriveKeys(keyset, sdPath, stream);

            stream.Position = 0x4000;
            Xts xts = XtsAes128.Create(Keys[0], Keys[1]);

            Stream = new RandomAccessSectorStream(new XtsSectorStream(stream, xts, 0x4000, 0x4000), keepOpen);
        }
Example #2
0
        // Thanks, Falo!
        public static byte[] AES_XTS(byte[] Key1, byte[] Key2, int SectorSize, byte[] Data, ulong Sector)
        {
            byte[] BlockData;
            Xts    XTS128 = XtsAes128.Create(Key1, Key2, true);
            int    Blocks;
            var    MemStrm         = new MemoryStream();
            var    Writer          = new BinaryWriter(MemStrm);
            var    CryptoTransform = XTS128.CreateEncryptor();

            BlockData = new byte[SectorSize];
            Blocks    = Data.Length / SectorSize;
            for (int i = 0; i < Blocks; i++)
            {
                CryptoTransform.TransformBlock(Data, i * SectorSize, SectorSize, BlockData, 0, Sector++);
                Writer.Write(BlockData);
            }
            return(MemStrm.ToArray());
        }
Example #3
0
 public static byte[] DecryptNCAHeader(string selectedFile, long offset)
 {
     byte[] array = new byte[3072];
     if (File.Exists(selectedFile))
     {
         FileStream fileStream = new FileStream(selectedFile, FileMode.Open, FileAccess.Read);
         fileStream.Position = offset;
         fileStream.Read(array, 0, 3072);
         File.WriteAllBytes(selectedFile + ".tmp", array);
         Xts xts = XtsAes128.Create(Form1.NcaHeaderEncryptionKey1_Prod, Form1.NcaHeaderEncryptionKey2_Prod);
         using (BinaryReader binaryReader = new BinaryReader(File.OpenRead(selectedFile + ".tmp"))) {
             using (XtsStream xtsStream = new XtsStream(binaryReader.BaseStream, xts, 512)) {
                 xtsStream.Read(array, 0, 3072);
             }
         }
         File.Delete(selectedFile + ".tmp");
         fileStream.Close();
     }
     return(array);
 }
Example #4
0
        /* Switched to MemoryStream and simplified code
         * No idea if anything broke
         */
        public static byte[] DecryptNCAHeader(string selectedFile, long offset)
        {
            byte[] array = new byte[3072];
            if (File.Exists(selectedFile))
            {
                using (var stream = new MemoryStream()) {
                    FileStream fs = new FileStream(selectedFile, FileMode.Open, FileAccess.Read);
                    fs.Position = offset;
                    fs.Read(array, 0, 3072);
                    fs.Close();

                    stream.Write(array, 0, array.Length);
                    stream.Position = 0;

                    Xts xts = XtsAes128.Create(Form1.NcaHeaderEncryptionKey1_Prod, Form1.NcaHeaderEncryptionKey2_Prod);
                    using (XtsStream xtsStream = new XtsStream(stream, xts, 512)) {
                        xtsStream.Read(array, 0, 3072);
                    }
                }
            }
            return(array);
        }
Example #5
0
 public void SetXts(Xts xts)
 {
     Xts = xts;
 }
Example #6
0
        protected override async Task InitMetadataImplAsync(CancellationToken cancel = default)
        {
            Memory <byte> firstSectorData = new byte[XtsAesMetaDataSize];

            // ヘッダの読み込みを試行する
            int readSize = await this.PhysicalReadAsync(0, firstSectorData, cancel);

            if (readSize == XtsAesMetaDataSize)
            {
                var metaDataParseResult = TryParseMetaData(firstSectorData);

                metaDataParseResult.ThrowIfException();

                var metaData = metaDataParseResult.Value !;

                // パスワード検査
                if (Secure.VeritySaltedPassword(metaData.SaltedPassword, this.CurrentPassword) == false)
                {
                    throw new CoresException("XtsAesRandomAccess: Incorrect password.");
                }

                // 秘密鍵解読
                var decrypted = ChaChaPoly.EasyDecryptWithPassword(metaData.MasterKeyEncryptedByPassword._GetHexBytes(), this.CurrentPassword);
                decrypted.ThrowIfException();

                // 秘密鍵サイズ検査
                if (decrypted.Value.Length != XtsAesKeySize)
                {
                    throw new CoresException("XtsAesRandomAccess: decrypted.Value.Length != XtsAesKeySize");
                }

                this.CurrentMasterKey = decrypted.Value;

                this.CurrentMetaData = metaData;
            }
            else if (readSize == 0)
            {
                // ファイルの内容が存在しない

                // マスターキーを新規作成する
                this.CurrentMasterKey = Secure.Rand(XtsAesKeySize);

                // メタデータを新規作成する
                var metaData = new XtsAesRandomAccessMetaData
                {
                    Version        = 1,
                    VirtualSize    = 0,
                    SaltedPassword = Secure.SaltPassword(this.CurrentPassword),
                    MasterKeyEncryptedByPassword = ChaChaPoly.EasyEncryptWithPassword(this.CurrentMasterKey, this.CurrentPassword)._GetHexString(),
                };

                this.CurrentMetaData = metaData;

                // メタデータを書き込みする
                await WriteMetaDataAsync(cancel);
            }
            else
            {
                // 不正 ここには来ないはず
                throw new CoresException($"XtsAesRandomAccess: Invalid readSize: {readSize}");
            }

            // XTS を作成
            this.CurrentXts        = XtsAes256.Create(this.CurrentMasterKey.ToArray());
            this.CurrentEncrypter  = this.CurrentXts.CreateEncryptor();
            this.CurrentDescrypter = this.CurrentXts.CreateDecryptor();
        }
Example #7
0
 public VideoCacheDownloadOperationOutputWithEncryption(StorageFile destinationFile, XTSSharp.Xts xts)
 {
     _destinationFile = destinationFile;
     _xts             = xts;
 }