public NcaBodySection(long mediaOffset, long mediaLength, NcaSectionCrypto sectionCrypto, byte[] baseSectionCtr) { if (mediaOffset < 6) { throw new ArgumentOutOfRangeException(nameof(mediaOffset)); } if (mediaLength <= 0) { throw new ArgumentOutOfRangeException(nameof(mediaLength)); } if (baseSectionCtr == null) { throw new ArgumentNullException(nameof(baseSectionCtr)); } if (baseSectionCtr.Length != 0x10) { throw new InvalidOperationException("Base section ctr needs a length of 0x10 bytes."); } MediaOffset = mediaOffset; MediaLength = mediaLength; SectionCrypto = sectionCrypto; BaseSectionCtr = new byte[0x10]; Array.Copy(baseSectionCtr, BaseSectionCtr, 0x10); }
public NcaBodyStream(Stream input, NcaSectionCrypto sectionCryptoType, byte[] iv, byte[] decKeyArea, byte[] decTitleKey) { if (sectionCryptoType == NcaSectionCrypto.TitleKey) { if (decTitleKey == null) { throw new ArgumentNullException(nameof(decTitleKey)); } _baseStream = new CtrStream(input, decTitleKey, iv, false); } else { switch (sectionCryptoType) { case NcaSectionCrypto.NoCrypto: _baseStream = input; break; case NcaSectionCrypto.Xts: var keyAreaKey = new byte[0x20]; Array.Copy(decKeyArea, keyAreaKey, 0x20); _baseStream = new XtsStream(input, keyAreaKey, iv, true, false, 0x200); break; case NcaSectionCrypto.Ctr: keyAreaKey = new byte[0x10]; Array.Copy(decKeyArea, 0x20, keyAreaKey, 0, 0x10); _baseStream = new CtrStream(input, keyAreaKey, iv, false); break; case NcaSectionCrypto.Bktr: //BKTR, some CTR //stub // TODO: Implement BKTR cryptography throw new NotSupportedException($"This section crypto is not supported."); } } }