public ByteData PullData(long offset, int size)
        {
            ByteData byteData = this.m_source.PullData(offset, size);

            if (byteData.Buffer.Count % this.BlockSize != 0)
            {
                throw new InvalidOperationException();
            }
            if (offset % (long)this.BlockSize != 0L)
            {
                throw new InvalidOperationException();
            }
            if (byteData.Buffer.Count == 0)
            {
                return(new ByteData(new ArraySegment <byte>()));
            }
            byte[] numArray = new byte[byteData.Buffer.Count];
            int    num1     = 0;
            ArraySegment <byte> buffer;

            while (true)
            {
                int num2 = num1;
                buffer = byteData.Buffer;
                int count = buffer.Count;
                if (num2 < count)
                {
                    IXtsModeEncryptor crypto       = this.m_crypto;
                    byte[]            initialTweak = this.GetInitialTweak(offset + (long)num1);
                    buffer = byteData.Buffer;
                    byte[] array = buffer.Array;
                    buffer = byteData.Buffer;
                    int    srcOffset = buffer.Offset + num1;
                    int    blockSize = this.BlockSize;
                    byte[] dst       = numArray;
                    int    dstOffset = num1;
                    crypto.EncryptBlock(initialTweak, array, srcOffset, blockSize, dst, dstOffset);
                    num1 += this.BlockSize;
                }
                else
                {
                    break;
                }
            }
            byte[] array1  = numArray;
            int    offset1 = 0;

            buffer = byteData.Buffer;
            int count1 = buffer.Count;

            return(new ByteData(new ArraySegment <byte>(array1, offset1, count1)));
        }
 public Aes128XtsEncryptedSource(ISource source, IXtsModeEncryptor encryptor)
 {
     this.m_crypto  = encryptor;
     this.BlockSize = 512;
     this.m_source  = (ISource) new AlignedSource(source, this.BlockSize);
 }
Ejemplo n.º 3
0
 private void SetCryptor(KeyConfiguration config)
 {
     if (this.m_fileSystemInfo.isProdEncryption)
     {
         this.m_bodyEncryptionKeyEncryptor = this.m_keyConfig.GetProdKeyAreaEncryptionKey() == null ? (IEncryptor) new HsmAes128CryptoDriver(Aes128KeyIndex.NcaContentKey) : (IEncryptor) new Aes128CryptoDriver(this.m_keyConfig.GetProdKeyAreaEncryptionKey().Key);
         byte[][] numArray1 = new byte[2][]
         {
             new byte[16],
             new byte[16]
         };
         byte[][] numArray2 = new byte[2][]
         {
             new byte[16]
             {
                 (byte)90,
                 (byte)62,
                 (byte)216,
                 (byte)79,
                 (byte)222,
                 (byte)192,
                 (byte)216,
                 (byte)38,
                 (byte)49,
                 (byte)247,
                 (byte)226,
                 (byte)93,
                 (byte)25,
                 (byte)123,
                 (byte)245,
                 (byte)208
             },
             new byte[16]
             {
                 (byte)28,
                 (byte)155,
                 (byte)123,
                 (byte)250,
                 (byte)246,
                 (byte)40,
                 (byte)24,
                 (byte)61,
                 (byte)113,
                 (byte)246,
                 (byte)77,
                 (byte)115,
                 (byte)241,
                 (byte)80,
                 (byte)185,
                 (byte)210
             }
         };
         IEncryptor encryptor = this.m_keyConfig.GetProdNcaHeaderEncryptionKek() == null ? (IEncryptor) new HsmAes128CryptoDriver(Aes128KeyIndex.NcaHeader) : (IEncryptor) new Aes128CryptoDriver(this.m_keyConfig.GetProdNcaHeaderEncryptionKek().Key);
         encryptor.DecryptBlock(numArray2[0], 0, 16, numArray1[0], 0);
         encryptor.DecryptBlock(numArray2[1], 0, 16, numArray1[1], 0);
         this.m_headerEncryptor = (IXtsModeEncryptor) new Aes128XtsCryptoDriver(numArray1[0], numArray1[1]);
         if (this.m_keyConfig.GetNcaHeader1SignKey() != null)
         {
             RsaKey ncaHeader1SignKey = this.m_keyConfig.GetNcaHeader1SignKey();
             this.m_header1Signer = (ISigner) new Rsa2048PssSha256SignCryptoDriver(ncaHeader1SignKey.KeyModulus, ncaHeader1SignKey.KeyPublicExponent, ncaHeader1SignKey.KeyPrivateExponent);
         }
         else
         {
             this.m_header1Signer = (ISigner) new HsmRsa2048PssSha256SignCryptoDriver(Rsa2048PssSha256KeyIndex.NcaHeader1);
         }
         if (this.m_fileSystemInfo.contentType != (byte)0)
         {
             this.m_header2Signer = (ISigner)null;
         }
         else
         {
             RsaKey randomRsaKey = this.m_keyConfig.GetRandomRsaKey();
             this.m_header2Signer = (ISigner) new Rsa2048PssSha256SignCryptoDriver(randomRsaKey.KeyModulus, randomRsaKey.KeyPublicExponent, randomRsaKey.KeyPrivateExponent);
         }
         if (this.m_keyConfig.GetAcidSignKey() != null)
         {
             RsaKey acidSignKey = this.m_keyConfig.GetAcidSignKey();
             this.m_acidSigner = (ISigner) new Rsa2048PssSha256SignCryptoDriver(acidSignKey.KeyModulus, acidSignKey.KeyPublicExponent, acidSignKey.KeyPrivateExponent);
         }
         else
         {
             this.m_acidSigner = (ISigner) new HsmRsa2048PssSha256SignCryptoDriver(Rsa2048PssSha256KeyIndex.Acid);
         }
         RsaKey randomRsaKey1 = this.m_keyConfig.GetRandomRsaKey();
         this.m_nrrSigner = (ISigner) new Rsa2048PssSha256SignCryptoDriver(randomRsaKey1.KeyModulus, randomRsaKey1.KeyPublicExponent, randomRsaKey1.KeyPrivateExponent);
         if (this.m_keyConfig.GetNrrCertificateSignKey() != null)
         {
             RsaKey certificateSignKey = this.m_keyConfig.GetNrrCertificateSignKey();
             this.m_nrrCertificateSigner = (ISigner) new Rsa2048PssSha256SignCryptoDriver(certificateSignKey.KeyModulus, certificateSignKey.KeyPublicExponent, certificateSignKey.KeyPrivateExponent);
         }
         else
         {
             this.m_nrrCertificateSigner = (ISigner) new HsmRsa2048PssSha256SignCryptoDriver(Rsa2048PssSha256KeyIndex.NrrCertificate);
         }
     }
     else
     {
         this.m_bodyEncryptionKeyEncryptor = (IEncryptor) new Aes128CryptoDriver(this.m_keyConfig.GetKeyAreaEncryptionKey((uint)this.m_fileSystemInfo.keyAreaEncryptionKeyIndex).Key);
         this.m_headerEncryptor            = (IXtsModeEncryptor) new Aes128XtsCryptoDriver(Aes128XtsCryptoDriver.GetDefaultKey(2), Aes128XtsCryptoDriver.GetDefaultKey(3));
         this.m_header1Signer        = (ISigner) new Rsa2048PssSha256SignCryptoDriver(Rsa2048PssSha256KeyIndex.NcaHeader1);
         this.m_header2Signer        = this.m_fileSystemInfo.contentType != (byte)0 || this.m_fileSystemInfo.header2SignKeyModulus == null || (this.m_fileSystemInfo.header2SignKeyPublicExponent == null || this.m_fileSystemInfo.header2SignKeyPrivateExponent == null) ? (ISigner)null : (ISigner) new Rsa2048PssSha256SignCryptoDriver(this.m_fileSystemInfo.header2SignKeyModulus, this.m_fileSystemInfo.header2SignKeyPublicExponent, this.m_fileSystemInfo.header2SignKeyPrivateExponent);
         this.m_acidSigner           = (ISigner)null;
         this.m_nrrSigner            = (ISigner) new Rsa2048PssSha256SignCryptoDriver(Rsa2048PssSha256KeyIndex.NrrCertificate);
         this.m_nrrCertificateSigner = (ISigner) new Rsa2048PssSha256SignCryptoDriver(Rsa2048PssSha256KeyIndex.NrrCertificate);
     }
 }