Exemplo n.º 1
0
        public AesXtsFile(OpenMode mode, IFile baseFile, U8String path, ReadOnlySpan <byte> kekSeed, ReadOnlySpan <byte> verificationKey, int blockSize)
        {
            Mode            = mode;
            BaseFile        = baseFile;
            Path            = path;
            KekSeed         = kekSeed.ToArray();
            VerificationKey = verificationKey.ToArray();
            BlockSize       = blockSize;

            Header = new AesXtsFileHeader(BaseFile);

            baseFile.GetSize(out long fileSize).ThrowIfFailure();

            if (!Header.TryDecryptHeader(Path.ToString(), KekSeed, VerificationKey))
            {
                ThrowHelper.ThrowResult(ResultFs.AesXtsFileHeaderInvalidKeys.Value, "NAX0 key derivation failed.");
            }

            if (HeaderLength + Utilities.AlignUp(Header.Size, 0x10) > fileSize)
            {
                ThrowHelper.ThrowResult(ResultFs.AesXtsFileTooShort.Value, "NAX0 key derivation failed.");
            }

            var fileStorage = new FileStorage2(baseFile);
            var encStorage  = new SubStorage(fileStorage, HeaderLength, fileSize - HeaderLength);

            encStorage.SetResizable(true);

            BaseStorage = new CachedStorage(new Aes128XtsStorage(encStorage, Header.DecryptedKey1, Header.DecryptedKey2, BlockSize, true), 4, true);
        }
Exemplo n.º 2
0
        public Result OpenContentStorageFileSystem(out IFileSystem fileSystem, ContentStorageId storageId)
        {
            fileSystem = default;

            U8String    contentDirPath = default;
            IFileSystem baseFileSystem = default;
            bool        isEncrypted    = false;
            Result      rc;

            switch (storageId)
            {
            case ContentStorageId.System:
                rc             = OpenBisFileSystem(out baseFileSystem, string.Empty, BisPartitionId.System);
                contentDirPath = $"/{ContentDirectoryName}".ToU8String();
                break;

            case ContentStorageId.User:
                rc             = OpenBisFileSystem(out baseFileSystem, string.Empty, BisPartitionId.User);
                contentDirPath = $"/{ContentDirectoryName}".ToU8String();
                break;

            case ContentStorageId.SdCard:
                rc             = OpenSdCardFileSystem(out baseFileSystem);
                contentDirPath = $"/{NintendoDirectoryName}/{ContentDirectoryName}".ToU8String();
                isEncrypted    = true;
                break;

            default:
                rc = ResultFs.InvalidArgument.Log();
                break;
            }

            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = baseFileSystem.EnsureDirectoryExists(contentDirPath.ToString());
            if (rc.IsFailure())
            {
                return(rc);
            }

            rc = FsCreators.SubDirectoryFileSystemCreator.Create(out IFileSystem subDirFileSystem,
                                                                 baseFileSystem, contentDirPath);
            if (rc.IsFailure())
            {
                return(rc);
            }

            if (!isEncrypted)
            {
                fileSystem = subDirFileSystem;
                return(Result.Success);
            }

            return(FsCreators.EncryptedFileSystemCreator.Create(out fileSystem, subDirFileSystem,
                                                                EncryptedFsKeyId.Content, SdEncryptionSeed));
        }