From 066715a61b399bc644cf5a4c9a40ac2da9a3bdf9 Mon Sep 17 00:00:00 2001 From: LukeFZ <17146677+LukeFZ@users.noreply.github.com> Date: Thu, 17 Aug 2023 01:00:14 +0200 Subject: [PATCH] Initial commit --- .github/workflows/build.yml | 51 + .gitignore | 353 +++++ LICENSE | 340 +++++ LibXboxOne.Tests/AesCipherTests.cs | 54 + LibXboxOne.Tests/AesXtsTests.cs | 94 ++ LibXboxOne.Tests/BCryptImportTests.cs | 167 +++ LibXboxOne.Tests/DurangoKeysTests.cs | 36 + LibXboxOne.Tests/HashUtilsTests.cs | 98 ++ LibXboxOne.Tests/LibXboxOne.Tests.csproj | 20 + .../Resources/DataBlobs/xbfs_header.bin | Bin 0 -> 1024 bytes .../Resources/DataBlobs/xts_cipher.bin | Bin 0 -> 12288 bytes .../Resources/DataBlobs/xts_plain.bin | 1 + .../RsaKeys/RSA_1024_CAPIPRIVATEBLOB.bin | Bin 0 -> 596 bytes .../RsaKeys/RSA_1024_CAPIPUBLICBLOB.bin | Bin 0 -> 148 bytes .../RsaKeys/RSA_1024_RSAFULLPRIVATEBLOB.bin | Bin 0 -> 603 bytes .../RsaKeys/RSA_1024_RSAPRIVATEBLOB.bin | Bin 0 -> 283 bytes .../RsaKeys/RSA_1024_RSAPUBLICBLOB.bin | Bin 0 -> 155 bytes .../RsaKeys/RSA_2048_CAPIPRIVATEBLOB.bin | Bin 0 -> 1172 bytes .../RsaKeys/RSA_2048_CAPIPUBLICBLOB.bin | Bin 0 -> 276 bytes .../RsaKeys/RSA_2048_RSAFULLPRIVATEBLOB.bin | Bin 0 -> 1179 bytes .../RsaKeys/RSA_2048_RSAPRIVATEBLOB.bin | Bin 0 -> 539 bytes .../RsaKeys/RSA_2048_RSAPUBLICBLOB.bin | Bin 0 -> 283 bytes .../RsaKeys/RSA_3072_CAPIPRIVATEBLOB.bin | Bin 0 -> 1748 bytes .../RsaKeys/RSA_3072_CAPIPUBLICBLOB.bin | Bin 0 -> 404 bytes .../RsaKeys/RSA_3072_RSAFULLPRIVATEBLOB.bin | Bin 0 -> 1755 bytes .../RsaKeys/RSA_3072_RSAPRIVATEBLOB.bin | Bin 0 -> 795 bytes .../RsaKeys/RSA_3072_RSAPUBLICBLOB.bin | Bin 0 -> 411 bytes .../RsaKeys/RSA_4096_CAPIPRIVATEBLOB.bin | Bin 0 -> 2324 bytes .../RsaKeys/RSA_4096_CAPIPUBLICBLOB.bin | Bin 0 -> 532 bytes .../RsaKeys/RSA_4096_RSAFULLPRIVATEBLOB.bin | Bin 0 -> 2331 bytes .../RsaKeys/RSA_4096_RSAPRIVATEBLOB.bin | Bin 0 -> 1051 bytes .../RsaKeys/RSA_4096_RSAPUBLICBLOB.bin | Bin 0 -> 539 bytes .../RsaKeys/RSA_512_CAPIPRIVATEBLOB.bin | Bin 0 -> 308 bytes .../RsaKeys/RSA_512_CAPIPUBLICBLOB.bin | Bin 0 -> 84 bytes .../RsaKeys/RSA_512_RSAFULLPRIVATEBLOB.bin | Bin 0 -> 315 bytes .../RsaKeys/RSA_512_RSAPRIVATEBLOB.bin | Bin 0 -> 155 bytes .../RsaKeys/RSA_512_RSAPUBLICBLOB.bin | Bin 0 -> 91 bytes LibXboxOne.Tests/ResourcesProvider.cs | 57 + LibXboxOne.Tests/XbfsTests.cs | 56 + LibXboxOne.Tests/XvdFileTests.cs | 282 ++++ LibXboxOne.Tests/XvdHashBlockTests.cs | 57 + LibXboxOne/AppDirs.cs | 38 + LibXboxOne/Certificates/BootCapability.cs | 103 ++ LibXboxOne/Certificates/BootCapabilityCert.cs | 113 ++ LibXboxOne/Certificates/PspConsoleCert.cs | 120 ++ LibXboxOne/Common.cs | 13 + LibXboxOne/Crypto/AesXts.cs | 106 ++ LibXboxOne/Crypto/BCryptRsaImport.cs | 68 + LibXboxOne/Crypto/HashUtils.cs | 69 + LibXboxOne/Keys/DurangoKeys.cs | 304 ++++ LibXboxOne/Keys/KeyEntry.cs | 57 + LibXboxOne/Keys/KeyType.cs | 9 + LibXboxOne/Keys/OdkIndex.cs | 10 + LibXboxOne/LibXboxOne.csproj | 25 + LibXboxOne/MiscStructs.cs | 78 + LibXboxOne/NAND/XbfsEntry.cs | 37 + LibXboxOne/NAND/XbfsFile.cs | 304 ++++ LibXboxOne/NAND/XbfsHeader.cs | 85 ++ LibXboxOne/Shared.cs | 298 ++++ LibXboxOne/ThirdParty/ProgressBar.cs | 101 ++ .../ThirdParty/TableParserExtensions.cs | 117 ++ LibXboxOne/XVD/XVDEnums.cs | 111 ++ LibXboxOne/XVD/XVDFile.cs | 1254 +++++++++++++++++ LibXboxOne/XVD/XVDLicenseBlock.cs | 143 ++ LibXboxOne/XVD/XVDMount.cs | 80 ++ LibXboxOne/XVD/XVDStructs.cs | 643 +++++++++ LibXboxOne/XVD/XvcConstants.cs | 7 + LibXboxOne/XVD/XvcRegionId.cs | 12 + LibXboxOne/XVD/XvdFilesystem.cs | 351 +++++ LibXboxOne/XVD/XvdFilesystemStream.cs | 152 ++ LibXboxOne/XVD/XvdMath.cs | 188 +++ README.md | 61 + XVDTool.sln | 52 + XvdTool.Streaming/ConsoleLogger.cs | 21 + XvdTool.Streaming/Crypto/AesCoreNi.cs | 584 ++++++++ XvdTool.Streaming/Crypto/AesXtsCipherNi.cs | 263 ++++ XvdTool.Streaming/Crypto/AesXtsDecryptorNi.cs | 20 + XvdTool.Streaming/Crypto/Buffer16.cs | 114 ++ XvdTool.Streaming/Extensions.cs | 41 + XvdTool.Streaming/HttpFileStream.cs | 189 +++ XvdTool.Streaming/KeyManager.cs | 310 ++++ XvdTool.Streaming/Program.cs | 302 ++++ .../StreamedXvdFile.LocalImpl.cs | 319 +++++ XvdTool.Streaming/StreamedXvdFile.cs | 743 ++++++++++ XvdTool.Streaming/XvdTool.Streaming.csproj | 20 + 85 files changed, 9701 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 LibXboxOne.Tests/AesCipherTests.cs create mode 100644 LibXboxOne.Tests/AesXtsTests.cs create mode 100644 LibXboxOne.Tests/BCryptImportTests.cs create mode 100644 LibXboxOne.Tests/DurangoKeysTests.cs create mode 100644 LibXboxOne.Tests/HashUtilsTests.cs create mode 100644 LibXboxOne.Tests/LibXboxOne.Tests.csproj create mode 100644 LibXboxOne.Tests/Resources/DataBlobs/xbfs_header.bin create mode 100644 LibXboxOne.Tests/Resources/DataBlobs/xts_cipher.bin create mode 100644 LibXboxOne.Tests/Resources/DataBlobs/xts_plain.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_CAPIPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_CAPIPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_RSAFULLPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_RSAPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_RSAPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_CAPIPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_CAPIPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAFULLPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_CAPIPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_CAPIPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAFULLPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_CAPIPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_CAPIPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAFULLPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_512_CAPIPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_512_CAPIPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAFULLPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAPRIVATEBLOB.bin create mode 100644 LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAPUBLICBLOB.bin create mode 100644 LibXboxOne.Tests/ResourcesProvider.cs create mode 100644 LibXboxOne.Tests/XbfsTests.cs create mode 100644 LibXboxOne.Tests/XvdFileTests.cs create mode 100644 LibXboxOne.Tests/XvdHashBlockTests.cs create mode 100644 LibXboxOne/AppDirs.cs create mode 100644 LibXboxOne/Certificates/BootCapability.cs create mode 100644 LibXboxOne/Certificates/BootCapabilityCert.cs create mode 100644 LibXboxOne/Certificates/PspConsoleCert.cs create mode 100644 LibXboxOne/Common.cs create mode 100644 LibXboxOne/Crypto/AesXts.cs create mode 100644 LibXboxOne/Crypto/BCryptRsaImport.cs create mode 100644 LibXboxOne/Crypto/HashUtils.cs create mode 100644 LibXboxOne/Keys/DurangoKeys.cs create mode 100644 LibXboxOne/Keys/KeyEntry.cs create mode 100644 LibXboxOne/Keys/KeyType.cs create mode 100644 LibXboxOne/Keys/OdkIndex.cs create mode 100644 LibXboxOne/LibXboxOne.csproj create mode 100644 LibXboxOne/MiscStructs.cs create mode 100644 LibXboxOne/NAND/XbfsEntry.cs create mode 100644 LibXboxOne/NAND/XbfsFile.cs create mode 100644 LibXboxOne/NAND/XbfsHeader.cs create mode 100644 LibXboxOne/Shared.cs create mode 100644 LibXboxOne/ThirdParty/ProgressBar.cs create mode 100644 LibXboxOne/ThirdParty/TableParserExtensions.cs create mode 100644 LibXboxOne/XVD/XVDEnums.cs create mode 100644 LibXboxOne/XVD/XVDFile.cs create mode 100644 LibXboxOne/XVD/XVDLicenseBlock.cs create mode 100644 LibXboxOne/XVD/XVDMount.cs create mode 100644 LibXboxOne/XVD/XVDStructs.cs create mode 100644 LibXboxOne/XVD/XvcConstants.cs create mode 100644 LibXboxOne/XVD/XvcRegionId.cs create mode 100644 LibXboxOne/XVD/XvdFilesystem.cs create mode 100644 LibXboxOne/XVD/XvdFilesystemStream.cs create mode 100644 LibXboxOne/XVD/XvdMath.cs create mode 100644 README.md create mode 100644 XVDTool.sln create mode 100644 XvdTool.Streaming/ConsoleLogger.cs create mode 100644 XvdTool.Streaming/Crypto/AesCoreNi.cs create mode 100644 XvdTool.Streaming/Crypto/AesXtsCipherNi.cs create mode 100644 XvdTool.Streaming/Crypto/AesXtsDecryptorNi.cs create mode 100644 XvdTool.Streaming/Crypto/Buffer16.cs create mode 100644 XvdTool.Streaming/Extensions.cs create mode 100644 XvdTool.Streaming/HttpFileStream.cs create mode 100644 XvdTool.Streaming/KeyManager.cs create mode 100644 XvdTool.Streaming/Program.cs create mode 100644 XvdTool.Streaming/StreamedXvdFile.LocalImpl.cs create mode 100644 XvdTool.Streaming/StreamedXvdFile.cs create mode 100644 XvdTool.Streaming/XvdTool.Streaming.csproj diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..79eb9df --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,51 @@ +name: build +permissions: + contents: write + +on: + push: + tags: + - "*" + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + dotnet-version: [ '7.0.x' ] + rid: ['win-x64', 'win-x86', 'linux-x64', 'linux-arm64', 'osx-x64', 'osx-arm64'] + + steps: + - uses: actions/checkout@v3 + - name: Setup .NET SDK ${{ matrix.dotnet-version }} + uses: actions/setup-dotnet@v3 + with: + dotnet-version: ${{ matrix.dotnet-version }} + - name: Install dependencies + run: dotnet restore -r ${{ matrix.rid }} + - name: Publish + run: | + dotnet publish XvdTool.Streaming/XvdTool.Streaming.csproj -c Release --no-restore -o ./${{ matrix.rid }} -r ${{ matrix.rid }} --no-self-contained + zip -r ${{ matrix.rid }}.zip ./${{ matrix.rid }}/* + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: ${{ matrix.rid }}.zip + path: ./${{ matrix.rid }}.zip + + make-release: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + path: ./artifacts/ + - name: Make release + uses: softprops/action-gh-release@v0.1.14 + with: + files: ./artifacts/**/* + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b36e81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# macOS +.DS_Store +._.DS_Store + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ +# ASP.NET Core default setup: bower directory is configured as wwwroot/lib/ and bower restore is true +**/wwwroot/lib/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- Backup*.rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Application specific +*.rsa +*.odk +*.cik diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d6a9326 --- /dev/null +++ b/LICENSE @@ -0,0 +1,340 @@ +GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {description} + Copyright (C) {year} {fullname} + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + {signature of Ty Coon}, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. + diff --git a/LibXboxOne.Tests/AesCipherTests.cs b/LibXboxOne.Tests/AesCipherTests.cs new file mode 100644 index 0000000..9a5c1ff --- /dev/null +++ b/LibXboxOne.Tests/AesCipherTests.cs @@ -0,0 +1,54 @@ +using System.Security.Cryptography; +using Xunit; + +namespace LibXboxOne.Tests +{ + public static class AesChipherData + { + public static byte[] AesKey => new byte[]{ + 0x58,0x51,0x28,0xbc,0xcb,0xdb,0x45,0x73,0xdb,0x92,0x18,0xc5,0x64,0x92,0x86,0x15, + 0xde,0xdb,0xc8,0x29,0x99,0x32,0x81,0xd9,0x77,0xa0,0xf9,0xc9,0x4b,0xbb,0x7f,0x62}; + public static byte[] PlaintextData => new byte[]{ + 0x48,0x45,0x4c,0x4c,0x4f,0x2c,0x20,0x49,0x54,0x53,0x20,0x4d,0x45,0x2c,0x20,0x54, + 0x45,0x53,0x54,0x44,0x41,0x54,0x41,0x0a,0x01,0x11,0x21,0x31,0x41,0x51,0x61,0x71}; + + public static byte[] EncryptedData => new byte[]{ + 0x50,0x88,0xED,0x6D,0x2B,0xD6,0xDE,0xA9,0x23,0x45,0x29,0x97,0x8A,0xD1,0x8C,0xE9, + 0xED,0x65,0x53,0x0A,0xB9,0x72,0x46,0xF0,0xA7,0x49,0xE3,0x19,0x5D,0x13,0x15,0x82}; + } + + public class AesCipherTests + { + [Fact] + public void TestAesEncryption() + { + var data = AesChipherData.PlaintextData; + + byte[] nullIv = new byte[16]; + var cipher = Aes.Create(); + cipher.Mode = CipherMode.ECB; + cipher.Padding = PaddingMode.None; + + ICryptoTransform transform = cipher.CreateEncryptor(AesChipherData.AesKey, nullIv); + transform.TransformBlock(data, 0, data.Length, data, 0); + + Assert.Equal(AesChipherData.EncryptedData, data); + } + + [Fact] + public void TestAesDecryption() + { + var data = AesChipherData.EncryptedData; + + byte[] nullIv = new byte[16]; + var cipher = Aes.Create(); + cipher.Mode = CipherMode.ECB; + cipher.Padding = PaddingMode.None; + + ICryptoTransform transform = cipher.CreateDecryptor(AesChipherData.AesKey, nullIv); + transform.TransformBlock(data, 0, data.Length, data, 0); + + Assert.Equal(AesChipherData.PlaintextData, data); + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/AesXtsTests.cs b/LibXboxOne.Tests/AesXtsTests.cs new file mode 100644 index 0000000..062e79d --- /dev/null +++ b/LibXboxOne.Tests/AesXtsTests.cs @@ -0,0 +1,94 @@ +using Xunit; + +namespace LibXboxOne.Tests +{ + public static class AesXtsData + { + public static byte[] WholeKey => new byte[]{ + 0x82,0x21,0xd4,0xac,0xf4,0x31,0x9d,0x95,0xe2,0x8e,0x72,0x2e,0x82,0xa3,0xb5,0xe2, + 0x32,0xba,0x06,0xdd,0x96,0x86,0x15,0x91,0x3f,0x6b,0xf8,0x10,0xd0,0x89,0x16,0xa0}; + public static byte[] TweakKey => new byte[]{ + 0x82,0x21,0xd4,0xac,0xf4,0x31,0x9d,0x95,0xe2,0x8e,0x72,0x2e,0x82,0xa3,0xb5,0xe2}; + + public static byte[] DataKey => new byte[]{ + 0x32,0xba,0x06,0xdd,0x96,0x86,0x15,0x91,0x3f,0x6b,0xf8,0x10,0xd0,0x89,0x16,0xa0}; + + public static byte[] Tweak => new byte[]{ + 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x34,0xFF,0x87,0x90,0xCA,0xDB,0x14,0x22}; + + public static byte[] PlainData => ResourcesProvider.GetBytes("xts_plain.bin", ResourceType.DataBlobs); + public static byte[] CipherData => ResourcesProvider.GetBytes("xts_cipher.bin", ResourceType.DataBlobs); + + public static uint HeaderId => 0x1; + } + + public class AesXtsTests + { + [Fact] + public void TestEncrypt() + { + var cipher = new AesXtsTransform(AesXtsData.Tweak, AesXtsData.DataKey, AesXtsData.TweakKey, + encrypt: true); + + var plaintext = AesXtsData.PlainData; + var ciphertext = AesXtsData.CipherData; + byte[] result = new byte[0x3000]; + int transformedBytes = 0; + + for (int dataUnit=0; dataUnit < 3; dataUnit++) + { + transformedBytes += cipher.TransformDataUnit(plaintext, dataUnit * 0x1000, 0x1000, + result, dataUnit * 0x1000, (uint)dataUnit); + } + + Assert.Equal(0x3000, transformedBytes); + Assert.Equal(ciphertext, result); + Assert.NotEqual(plaintext, result); + } + + [Fact] + public void TestDecrypt() + { + var cipher = new AesXtsTransform(AesXtsData.Tweak, AesXtsData.DataKey, AesXtsData.TweakKey, + encrypt: false); + + var plaintext = AesXtsData.PlainData; + var ciphertext = AesXtsData.CipherData; + byte[] result = new byte[0x3000]; + int transformedBytes = 0; + + for (int dataUnit=0; dataUnit < 3; dataUnit++) + { + transformedBytes += cipher.TransformDataUnit(ciphertext, dataUnit * 0x1000, 0x1000, + result, dataUnit * 0x1000, (uint)dataUnit); + } + + Assert.Equal(0x3000, transformedBytes); + Assert.Equal(plaintext, result); + Assert.NotEqual(ciphertext, result); + } + + [Fact] + public void TestEncryptFail() + { + var cipher = new AesXtsTransform(AesXtsData.Tweak, AesXtsData.DataKey, AesXtsData.TweakKey, + encrypt: true); + + var plaintext = AesXtsData.PlainData; + var ciphertext = AesXtsData.CipherData; + byte[] result = new byte[0x3000]; + int transformedBytes = 0; + + for (int dataUnit=0; dataUnit < 3; dataUnit++) + { + transformedBytes += cipher.TransformDataUnit(plaintext, dataUnit * 0x1000, 0x1000, + result, dataUnit * 0x1000, + (uint)dataUnit + 1); // <- Invalid data unit + } + + Assert.Equal(0x3000, transformedBytes); + Assert.NotEqual(plaintext, result); + Assert.NotEqual(ciphertext, result); + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/BCryptImportTests.cs b/LibXboxOne.Tests/BCryptImportTests.cs new file mode 100644 index 0000000..a32642a --- /dev/null +++ b/LibXboxOne.Tests/BCryptImportTests.cs @@ -0,0 +1,167 @@ +using System.IO; +using System.Security.Cryptography; +using System.Threading.Tasks; +using Xunit; + +namespace LibXboxOne.Tests +{ + public static class BCryptRsaHelper + { + static async Task GetRsa(int keyStrength, string keyIdentifier) + { + var blob = await ResourcesProvider.GetBytesAsync($"RSA_{keyStrength}_{keyIdentifier}.bin", + ResourceType.RsaKeys); + var rsaParams = BCryptRsaImport.BlobToParameters(blob, out int resultBitLength, out bool isPrivate); + if (keyStrength != resultBitLength) + throw new InvalidDataException("Desired keyStrength does not match parsed data"); + + return rsaParams; + } + + public static Task GetRsaPublic(int keyStrength) + { + return GetRsa(keyStrength, "RSAPUBLICBLOB"); + } + + public static Task GetRsaPrivate(int keyStrength) + { + return GetRsa(keyStrength, "RSAPRIVATEBLOB"); + } + + public static Task GetRsaFullPrivate(int keyStrength) + { + return GetRsa(keyStrength, "RSAFULLPRIVATEBLOB"); + } + + public static Task GetRsaPublicCAPI(int keyStrength) + { + return GetRsa(keyStrength, "CAPIPUBLICBLOB"); + } + + public static Task GetRsaPrivateCAPI(int keyStrength) + { + return GetRsa(keyStrength, "CAPIPRIVATEBLOB"); + } + } + + public class BCryptImportTests + { + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public async void ImportRsaPublicBlob(int keyStrength) + { + RSAParameters rsaParams = await BCryptRsaHelper.GetRsaPublic(keyStrength); + + Assert.NotEmpty(rsaParams.Exponent); + Assert.NotEmpty(rsaParams.Modulus); + } + + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public async void ImportRsaPrivateBlob(int keyStrength) + { + RSAParameters rsaParams = await BCryptRsaHelper.GetRsaPrivate(keyStrength); + + Assert.NotEmpty(rsaParams.Exponent); + Assert.NotEmpty(rsaParams.Modulus); + + Assert.NotEmpty(rsaParams.P); + Assert.NotEmpty(rsaParams.Q); + } + + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public async void ImportRsaFullPrivateBlob(int keyStrength) + { + RSAParameters rsaParams = await BCryptRsaHelper.GetRsaFullPrivate(keyStrength); + + Assert.NotEmpty(rsaParams.Exponent); + Assert.NotEmpty(rsaParams.Modulus); + + Assert.NotEmpty(rsaParams.P); + Assert.NotEmpty(rsaParams.Q); + + Assert.NotEmpty(rsaParams.DP); + Assert.NotEmpty(rsaParams.DQ); + Assert.NotEmpty(rsaParams.InverseQ); + Assert.NotEmpty(rsaParams.D); + } + + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public async void ImportAndComparePrivateBlobs(int keyStrength) + { + RSAParameters rsaParams = await BCryptRsaHelper.GetRsaPrivate(keyStrength); + RSAParameters rsaParamsFull = await BCryptRsaHelper.GetRsaFullPrivate(keyStrength); + + Assert.NotEmpty(rsaParams.Exponent); + Assert.NotEmpty(rsaParams.Modulus); + + Assert.Equal(rsaParams.Exponent, rsaParamsFull.Exponent); + Assert.Equal(rsaParams.Modulus, rsaParamsFull.Modulus); + + Assert.Equal(rsaParams.P, rsaParamsFull.P); + Assert.Equal(rsaParams.Q, rsaParamsFull.Q); + } + + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public async void ImportAndComparePublicBlobs(int keyStrength) + { + RSAParameters rsaParamsPublic = await BCryptRsaHelper.GetRsaPublic(keyStrength); + RSAParameters rsaParamsFullprivate = await BCryptRsaHelper.GetRsaFullPrivate(keyStrength); + + Assert.NotEmpty(rsaParamsPublic.Exponent); + Assert.NotEmpty(rsaParamsPublic.Modulus); + + Assert.Equal(rsaParamsPublic.Exponent, rsaParamsFullprivate.Exponent); + Assert.Equal(rsaParamsPublic.Modulus, rsaParamsFullprivate.Modulus); + } + + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public void ImportInvalidPublicBlobCAPI(int keyStrength) + { + Assert.ThrowsAsync(async () => + await BCryptRsaHelper.GetRsaPublicCAPI(keyStrength) + ); + } + + [Theory] + [InlineData(512)] + [InlineData(1024)] + [InlineData(2048)] + [InlineData(3072)] + [InlineData(4096)] + public void ImportInvalidPrivateBlobCAPI(int keyStrength) + { + Assert.ThrowsAsync(async () => + await BCryptRsaHelper.GetRsaPrivateCAPI(keyStrength) + ); + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/DurangoKeysTests.cs b/LibXboxOne.Tests/DurangoKeysTests.cs new file mode 100644 index 0000000..b507ee1 --- /dev/null +++ b/LibXboxOne.Tests/DurangoKeysTests.cs @@ -0,0 +1,36 @@ +using Xunit; +using LibXboxOne.Keys; + +namespace LibXboxOne.Tests +{ + public class DurangoKeysTests + { + [Fact] + public void TestOdkIndexEnumConversion() + { + var redSuccess0 = DurangoKeys.GetOdkIndexFromString("RedOdk", out OdkIndex redOdk); + var redSuccess1 = DurangoKeys.GetOdkIndexFromString("redodk", out OdkIndex redOdkLower); + var redSuccess2 = DurangoKeys.GetOdkIndexFromString("2", out OdkIndex redOdkNumber); + var standardSuccess = DurangoKeys.GetOdkIndexFromString("0", out OdkIndex standardOdkNumber); + var unknownNumberSuccess = DurangoKeys.GetOdkIndexFromString("42", out OdkIndex unknownOdkNumber); + + var invalidNameFail = DurangoKeys.GetOdkIndexFromString("redodkblabla", out OdkIndex nameFail); + + Assert.True(redSuccess0); + Assert.True(redSuccess1); + Assert.True(redSuccess2); + Assert.True(standardSuccess); + Assert.True(unknownNumberSuccess); + + Assert.False(invalidNameFail); + + Assert.Equal(OdkIndex.RedOdk, redOdk); + Assert.Equal(OdkIndex.RedOdk, redOdkLower); + Assert.Equal(OdkIndex.RedOdk, redOdkNumber); + Assert.Equal((OdkIndex)42, unknownOdkNumber); + + Assert.Equal(OdkIndex.Invalid, nameFail); + + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/HashUtilsTests.cs b/LibXboxOne.Tests/HashUtilsTests.cs new file mode 100644 index 0000000..907f721 --- /dev/null +++ b/LibXboxOne.Tests/HashUtilsTests.cs @@ -0,0 +1,98 @@ +using System.Security.Cryptography; +using Xunit; + +namespace LibXboxOne.Tests +{ + public static class HashUtilsData + { + public static byte[] Data => new byte[]{ + 0x48,0x45,0x4c,0x4c,0x4f,0x2c,0x20,0x49,0x54,0x53,0x20,0x4d,0x45,0x2c,0x20,0x54, + 0x45,0x53,0x54,0x44,0x41,0x54,0x41,0x0a,0x01,0x11,0x21,0x31,0x41,0x51,0x61,0x71}; + + public static byte[] Sha256Hash => new byte[]{ + 0x3C,0x47,0xF6,0x32,0x57,0x72,0xCC,0x80,0xAE,0x72,0x0D,0xA2,0x4C,0x60,0xD8,0x1A, + 0xBF,0xD1,0x7E,0x8A,0x63,0xA8,0xCC,0xDE,0x4B,0xD9,0xF2,0xB8,0xBB,0xC3,0x82,0x0F}; + + public static byte[] Sha1Hash => new byte[]{ + 0xD7,0x3E,0x77,0x3C,0xE0,0x56,0x82,0x03,0xE8,0x92,0xFD,0xDD,0xBC,0xD8,0xAA,0x3C, + 0x94,0xE2,0x37,0xC2}; + + public static byte[] RsaSignature => new byte[]{ + 0x52,0xDA,0x68,0x6B,0x69,0x32,0x48,0x01,0x28,0x7A,0x38,0x24,0x84,0xA7,0xC2,0xCD, + 0x9F,0xDC,0xDC,0xA5,0x98,0x59,0x35,0x9D,0x5E,0x76,0xCF,0x4A,0xA7,0xDB,0xF0,0xEE, + 0x12,0x19,0xA6,0x87,0x0A,0x6F,0xF6,0xE1,0x44,0x57,0xC7,0xBE,0x0E,0x36,0x77,0x70, + 0xA3,0xD3,0x95,0xA0,0x07,0xA3,0x59,0x7F,0xCD,0xEF,0xC2,0x8D,0x79,0x4D,0x7A,0x9C, + 0xC0,0xB8,0x12,0xEC,0x1D,0xF1,0x58,0x4C,0x46,0xC5,0x0A,0xEA,0x8D,0x6C,0x51,0xFC, + 0x21,0xFA,0x25,0x76,0xE7,0xAC,0x51,0xD0,0xDC,0x87,0x82,0x89,0x70,0x41,0x79,0x0D, + 0x04,0xF5,0x75,0xD6,0x6B,0x3D,0xE0,0x77,0x79,0x86,0xF4,0xD5,0x72,0x3B,0x0F,0xC5, + 0xDF,0x68,0xC6,0x88,0x08,0x71,0x98,0x64,0x20,0xAC,0xE1,0x4A,0x4A,0xE7,0xD9,0xF4}; + } + + public class HashUtilsTests + { + [Fact] + public void TestComputeSha256() + { + var result = HashUtils.ComputeSha256(HashUtilsData.Data); + + Assert.Equal(HashUtilsData.Sha256Hash.Length, result.Length); + Assert.Equal(HashUtilsData.Sha256Hash, result); + } + + [Fact] + public void TestComputeSha1() + { + var result = HashUtils.ComputeSha1(HashUtilsData.Data); + + Assert.Equal(HashUtilsData.Sha1Hash.Length, result.Length); + Assert.Equal(HashUtilsData.Sha1Hash, result); + } + + byte[] GetRsaBlob(string blobType, int bits) + { + return ResourcesProvider.GetBytes($"RSA_{bits}_{blobType}.bin", ResourceType.RsaKeys); + } + + [Theory] + // [InlineData(1024, "RSAPRIVATEBLOB")] + [InlineData(1024, "RSAFULLPRIVATEBLOB")] + public void TestSignData(int bits, string blobType) + { + var rsaBlob = GetRsaBlob(blobType, bits); + bool result = HashUtils.SignData(rsaBlob, blobType, HashUtilsData.Data, + out byte[] signature); + Assert.True(result); + Assert.NotEqual(HashUtilsData.RsaSignature, signature); // Cant be same, due to PSS + + + result = HashUtils.VerifySignature(rsaBlob, signature, + HashUtilsData.Data); + Assert.True(result); + } + + [Fact] + public void TestSignDataFailPubKey() + { + var rsaBlob = GetRsaBlob("RSAPUBLICBLOB", 1024); + + Assert.Throws(() => + { + HashUtils.SignData(rsaBlob, "RSAPUBLICBLOB", HashUtilsData.Data, + out byte[] signature); + }); + } + + [Theory] + [InlineData(1024, "RSAPUBLICBLOB")] + [InlineData(1024, "RSAPRIVATEBLOB")] + [InlineData(1024, "RSAFULLPRIVATEBLOB")] + public void TestVerifySignature(int bits, string blobType) + { + var rsaBlob = GetRsaBlob(blobType, bits); + bool success = HashUtils.VerifySignature(rsaBlob, HashUtilsData.RsaSignature, + HashUtilsData.Data); + + Assert.True(success); + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/LibXboxOne.Tests.csproj b/LibXboxOne.Tests/LibXboxOne.Tests.csproj new file mode 100644 index 0000000..11c0b5a --- /dev/null +++ b/LibXboxOne.Tests/LibXboxOne.Tests.csproj @@ -0,0 +1,20 @@ + + + + net7.0 + true + false + false + + + + + + + + + + + + + diff --git a/LibXboxOne.Tests/Resources/DataBlobs/xbfs_header.bin b/LibXboxOne.Tests/Resources/DataBlobs/xbfs_header.bin new file mode 100644 index 0000000000000000000000000000000000000000..cf36449844c922d6296e70f56737e1013a27cec1 GIT binary patch literal 1024 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 C7C~MB literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_RSAFULLPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_1024_RSAFULLPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..4cbfffea52bc94de4710c95b4b2f556be2223857 GIT binary patch literal 603 zcmV-h0;K&?Q$aHT1ONa70001h0000$0000$0000100G=9c}WG>LDOqrzfG=9fYuBw zvvaZL{Wpj`*0@(#IfKE;t8CLF!yGAL^Rg+U^99--+2j0cR9SytAUaQV;gmK)y_b>Y z174MI*}Z8CenQv6s=-8Q!?9e_QB)IqS+7p7V}IsBt@eDF?<-1H92O^! zyqPUHKS%tuiYUIr%UPrMXJ2qUKRBv+^K1MqL@aW@X9*qLo7?J`PAShDow!NbMLShH z*D+`tM-H}~gQ3-&K@C%@u=k=-0`F zms)2a;n!Mw+`qLgAzK6PI>HqCp<*`Pgt^R2(dqzMqw<2#j^McK@v~&h%xe$8oABFg z9tD`Mi2OT76b78QL`RRX64!1pvXGbVkkVcbWtRcYrIf@D(_2IWlyL4yHj4i-r$c>M pt$%~?n-U8Iu@|WjnMLDOqrzfG=9fYuBw zvvaZL{Wpj`*0@(#IfKE;t8CLF!yGAL^Rg+U^99--+2j0cR9SytAUaQV;gmK)y_b>Y z174MI*}Z8CenQv6s=-8Q!?9e_QB)IqS+7p7V}IsB)M-@p5+je&2Pz4cB;dHWzPv_Sd|__S%l{ z2+QV!Cs*fORz57FnfQ5==Hkz+x8!a<{*xUNSs$-p)%4{waQ6c{309l73beRBP?`uDLj%?*#HROpv*KV&O!+LUFhKFiEx;?2W5 E0V_U1H2?qr literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_CAPIPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_CAPIPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..dbe8c55df4c70554033719fed3378ad6479450ea GIT binary patch literal 1172 zcmV;F1Z(>T0ssI2qyPX?Q$aES2mk;90098igCM=^Ho44p^x&S~oV6;34PzUfgcZY_ zd%@^-LED^NDOjuJ2%np2ER5cuoOlLHds5fB9>i{&tWr*JIR^!ID_TO7HGeIZ4k8NZHp#X z{&_8;`+{NjtJ>7UTVPj#)4H|TxnRs|V>z+OV(uM1mg~8Zc3I!*u#2jOfOzm+nR4~1 z;Q3K*VSwPhFs_7CM?o^8^;Ug%Z-!zc_1pO@*9x1K9JW8qamM7D!v+OKDxM8GZ}OH0 zN0B1k>Cvb}jBIX;1TZ%brW393x?}>N?Y)H{tA&J4OVxBal^Ns9EoCW5vL*e$#p><~ zwx_A`9SZPt_QbgQx^>-2b;jY-7Oy9X$oO4Da) z`ctoJ^W%;$n~&>#81lK9tG^f^q#48_%D@`+jT_i=w-^bnakI0$xi!`xK&_( z-{?&;n?P%c#WLF&^g_Y3b>$CL-;`K$B{$-}mbn zhZ~4jyNkBUaVepgEkMZVW@ZhYRuvcl_v^!ao00%ZSPB%ndrU5Zsi9K<*}l{#!Sg&< z`vL2o2*SJ!*~ihfr4_x3& zD#cRS<>^#`Rb;^BQANR}H1dp-drgET)-#R)kzdP39}zMEAlH0_K^87%YrtExvB1hs z8%N39gxI}oS&Wth(>gteNR_&a3IWy88RTVcff?xyL#WAki}q`-eOORDT*7SqNbK&( z3N9oq+)uZ<6}EIu+05gFL!3IOTwS_JoU^YDwBzV(mP;0#vBY;mx49;6s!RJ0$UU!J z5rmX+l;oyuDgu)6?g{W_qr*_uI^R?st5O=8_)$e6H<=Bnd$Z*(P*eTI@WV$;AaPX@ z7DUC}x@Iy-x?LVFdw4`olTTw3osrQ~JqwL}a6=&ds`Au5t-ikh!*2}Pp=10e-t$1# mMFxCnhw8%RT^U?PF+r$(FUb=jFrS3SFQ~7l>~!pJ&ZQmO8b{;+ literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_CAPIPUBLICBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_CAPIPUBLICBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..7b0544d1a07ad2146519ab792d05e5cd5df60a96 GIT binary patch literal 276 zcmV+v0qgz-0ssI2qyPX?Q$aBR2mk;90098igCM=^Ho44p^x&S~oV6;34PzUfgcZY_ zd%@^-LED^NDOjuJ2%np2ER5cuoOlLHds5fB9>i{&tWr*JIR^!ID_TO7HGeIZ4k8NZHp#X a{&_8;`+{NjtJ>7UTVPj#)4H|TxnRtw0ECzT literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAFULLPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAFULLPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..1b803aea9737470e191ea549cc4c791d2edf7d09 GIT binary patch literal 1179 zcmV;M1Z4YCQ$aHT2mk;B000000RRAi0001h0000100GQkx!AS3(}7oDTf)@ZtM_4o z`=Tv*{`U4^Cx~{mP{?lYQ~*j~iMsgjNX@Przd*;{5-G4;p#@}ZkG&^BGnhWrWES(< z&4`%s3%ba*v7?Nm_zL}SBAoeZMx;4TdVUoZp_{^mfd-HtfA1gVp^dvPmgrEz9E>l{s|POHPD^t00BF z?Vti=y6~+NrVlqT1dDELj6|r>>D?lcM+cVjZ#oU0Dn$hb!N zV{6W?Rktq^(#~H%zgB(%G{lFXgL?mM8RSi8NC$i^=zA0hl|sBQGVvrDhjy`95^;&q zq=+KFtC_j-7=7!Hn=g*z^J=eC`e|p=O8f4z={^p(vqa@rJ&U?GM1y=juI2dK+Tc8| z7SrLzbxGZIy85`p_H^(H9rCHCwhHd*#lP3fnYPMh=<7sv+k$pE84w=gKc(WltYcY* z`r!@vkoKvG@@3V<46(lB>Z3}-V>EIV%7I6Ci~0C9^6Lngt_yldNeGR!X$N~7X;wG4 z_%C@DcOXOz=ewtXLME!oWyJt?t#H)7Q$#84-%4!x)!*`Swj#B~!FB2!`}2u&EpNy` zEtsJxamu!fyH|)ChZyVk->hnrK*a4LCavk|X&C1oG0;gF!Xef}MgahDHt2r|`ifL= zwMgpP;8YOGK_}rLhn7@r-*5#&xuQdIVSd>!uc!|-Y0_EZJNP6!f-eUZrM1z= z*$lkG2%hT!`&T^k!6($d*#J|ase&#{d%6?~SV{nrn|#CT_W>9cR-FxIX6PNI&Ts5= z?5D4&FUN$RFd-AkFMX&%F-BY&UFE{+hiQBUMb<#`-X{EGq1g;?!~ed%tv%H8s{J5C zaD9ynJyX$4TdVUoZp_{^mfd-HtfA1gVp^dvPmgrEz9E>l{s|POHPD^t00BF z?Vti=y6~+NrVlqT1dDELj6|r>>D?lcM+cVjZ#oU0Dn$hb!N zV{6W?Rktq^(#~H%zgB(%G{lFXgL?mM8RSi8NC$i^=zA0hl|sBQGVvrDhjy`95^;&q zq=+KFtC_j-7=7!Hn=g*z^J=eC`e|p=O8f4z={^p(vqa@rJ&U?GM1y=juI2dK+Tc8| d7SrLzbxGZIy85`p_H^(H9rCHCwhHd*#lH_n3tj*K literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAPUBLICBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_2048_RSAPUBLICBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..e2a6e96e6fb83e2f033a8246792bbb62541f1242 GIT binary patch literal 283 zcmV+$0p$KtQ$aBR2mk;B000000RR91000000000100GQkx!AS3(}7oDTf)@ZtM_4o z`=Tv*{`U4^Cx~{mP{?lYQ~*j~iMsgjNX@Przd*;{5-G4;p#@}ZkG&^BGnhWrWES(< z&4`%s3%ba*v7?Nm_zL}SBAoeZMx;4TdVUoZp_{^mfd-HtfA1gVodMgYW2+fZ-MaORVX>ZgiS3hO)h5dfP z_Yh+^*I3JH)$r$+^!=k4VGAs21NR+*$X_x)Ody|myx>F_=`~H7e7$R=LS(C6tW{Y+ zE1jM$gjQz&945C45hJ)Eo3Las7t~_IyM^dEp1e~+CQwSs@Ak*wAS17aG(h)^jE{e zkogZQAxl*k>CZ!#P($9zoaS6h+Rrupn?H6q6Wx77&~J7z1kj z2n_z8j;QYw<=reAn3Cc?{^Jzm+8(AuZfeX;9h_pT;uo{m~&gD7Z}XF9Jy+ag*qlb{7>nYT?Ke`n{DiAv;g z@yBS4WeOo)Utas~{ZFWRR)6r++MRS9z8LUs7$GITHhGKv742Omx#u@K0D-RNXgWJf z9+l0}c~A-;f=SF)f7Koq=MK_SD2V}po*;iQuf&EFby~gs9?eNQtuG1w2BV%q)76~0 zTx56%PRo!vXvBmbsl9tKY`zvnpTcg+Ni5r`B}4Azoc|0F8FA@q>q}&|{F;YKhL_CRfqo z17{b6P%mStrulAbDck7%L@JFq^09)010%eb2+K^cU1sj3&dvZZeoDq~kw^X%x}9Y1 z_sRF_1N(d}^g<|ihPwD!0{M<)I0kNLqEFL%`BD<;-M}#6f6Q<8_h@Q#( zGHs^!5#BJG4mM|_iAv$-32WQx7$2=?tF(7Um`j|rY5r^Y6p*UkRXkN$acyS&W-Emn zY|>(OCELyTK%?~Yo=;VVT#RqI)lH z>ovZHbwDTS2_f2IM_+AmSvbS@9j3Jd$1qGhK9vGQCOX>_)z^iACA(BI{!zm&fQAZ& zcB0x}0Y`klo q>ZA7bJbF7)!x#cxVQ|o$K5c0rupks$;3Ch3q6pac=btd>ja@(Ugkz=v literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_CAPIPUBLICBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_CAPIPUBLICBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..c246ee3dc9b8f120a500b66cd73d3ef80c1db63c GIT binary patch literal 404 zcmV;F0c-vS0ssI2qyPX?Q$aBR3;+ND0098$b_jm>2+fZ-MaORVX>ZgiS3hO)h5dfP z_Yh+^*I3JH)$r$+^!=k4VGAs21NR+*$X_x)Ody|myx>F_=`~H7e7$R=LS(C6tW{Y+ zE1jM$gjQz&945C45hJ)Eo3Las7t~_IyM^dEp1e~+CQwSs@Ak*wAS17aG(h)^jE{e zkogZQAxl*k>CZ!#P($9zoaS6h+Rrupn?H6q6Wx77&~J7z1kj z2n_z8j;QYw<=reAn3Cc?{^Jzm+8(AuZfeX;9h_pT;uo{m~&gD7Z}XF9Jy+ag*qlb{7>nYT?Ke`n{j=D>ac literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAFULLPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAFULLPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..ce58e807b8af0215d9161efdcb8ee00fb580ca9a GIT binary patch literal 1755 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 Z-@%$d0O0cAa2yPgdB6U-TNBb4fZhNA literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAPUBLICBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_3072_RSAPUBLICBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..05a17c3253d77618affe7f85b18aba35d9c578d3 GIT binary patch literal 411 zcmV;M0c8GCQ$aBR3;+NF0001h0RR91000000000100HM`e;`e_nP&x{lQCK%+d!{6 zXOw6tgJoKdo@*wq_hsv`ebuMh(306+JtKMMb-&Nt;Wf)Fj!ZAN%quk9ng+@Or01qC zMkIfaT82g(L7JC?z6t&dA01X}87UmN6 zdSr}0oBcJ<+DlyKoXXxqP?tl`=@(T?AuA90kix@P^ePFUa0?|>))4`FsKzIjwWi8* zhK%ME(Kk3|$!m-`Q1MIA>haM@5)M0CPGuZ zo;m1+yTfAC7cgY7n<2O(5el~^8~|rlgf5<)D?nLQtX`{RLZoZGe40%)=@>-dym_A> zOg}PT$budB18FP^VHl(R^q1%G)oROF*EnMk_rZSsh4N)TS1HtQY4~o(MK_Pl2>5;o FcIlHEz*YbN literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_CAPIPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_CAPIPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..7bf81884cf107fb6934b2defe2f29f750b7db831 GIT binary patch literal 2324 zcmV+v3G4O;0ssI2qyPX?Q$aES5C8xH0097T;yq7*Pi{U01wt$cG41hnUaF1@VCesu za9_D~#2l1hfo2|tuxun6z<86@L0Wvt>#TWm5E!0jmL^3(>6nw>nJ+B*dzN|^FA$Ts zEA2escNvNiBo2V|6TS|lb*V1&yDrxH z`<{|(ZfTrnU$jXzt&rG9!p?Cp+4vFXA?X44r`QjXk3A);`B===8{|K~r zk?7S2u+3myd+y7m+mW1-%qUXsFf5}KljITa@tbzzG#mHFY5TB)f$W4{$K+o-0Y#{1H*`AYAFDwLuPMB29&q@gJ<)mf~}_U25V|B3Q$wd{bj%WiT{^0_!7*EyohhM;PW;K1HT z_#ElqUosK3Csfx^f_^VN#=g5d#8Y)Hzn#Q_C4Fg{n65wM7xfgFi2FCMLNi=H{V}oz z+A}A5>^Lc}UoVd9%`{^hFm7$n|EFZtJtHc7tFn*dEB~qk38Ogen=25KudmIz#Q?Fe zUu%)+H(KFg5*m9^xCK@iRqKuh8YtUhp>rfG5?xM{jj@+$9J##2c>F?7|V`r*NmvZMRb3s6C4#gB{o{@Bt z4))`Wm1b`B5<|p2Z@cTPz;R(Azau_$s<8{Naye|g{-()8o;qVw;an|3W59L>*-{E@ zBnGv9jRe;Lu@%W7bo)3Ts_GJ!cce;SkzScz;pSwTD`_@P-Cb7uMJy?Ot1pD#%vVD> zih9#mslSa-9y8)GHWg{H#^lja9;|vD-At#c(LJ(kUs)H%RqXF0a4}Y!7vmCW*~O&r}FA;w1Sg-(cPz{N#$9C=HPq60)9xM6lgoN zp5cz(YadKiQVWT0O7|pDCrq}kDkUp*bB&N--~J`KBa4~6kJQ?0N?Py*1A#{WTp(GF z9PckX&i`8CtNq7K3QXc@FxawYO;c(|+yCVc^sRBO>qy&NJL|J>IQklY0KVY3W&ANJH;@>APT0E?-Ji_>8m54i8G_k^p56t&z5#P5 zgR-ZX-_x3R6uxQhPYZ_QnYQ-ZDT+I>N)Z^ia*>s?PkY^d7T&M@(jAd(4%4pJDEny_ zfCSIM$#B71Oc(JfOFP?iIf+!s+`$CD=bsM~qrm~-m`DU=tItc&%(3IkI}n63#g6LP ze?z0luj7a~QdG~L3%?VL9E!YwNMH&b{jADMb2aEMO_Z*!f)AEobDsc@_BH~QJm?i) zCWNrLche@or>5Ur=X`3&KC^pf-iue(Yz;lV`MQI~v{$2$Wb z_!(x_S0m_RDE!@iN+sa7kN_6!^6*%NNs33S_^L=_|4ly=6r zu&EXo;2NhoI_Rl^-~~IMkp42TrHr#n8lv_QHyCL4UIEK`NGz0`_n!wPu&Yx!#yLr4 zln7_qLSk~ZLI3N(nDK@qifVE13`V0;uCyH}0WQv`#ohgJ7s1rQE2W+K^_cj^d77+S zKAbJNtJ)tQs@voQpZRum1#0l_?;;R3wYYyZSUiQ&XtTFi1=b5Ud%O&tI*f(`4mCP^ z=}^@Y!SJn+MT!vVi!Dn>U>?;)RD0$kS4oZqQxZ8H+?&Po@4NMj?%T`~?~cfPGO%wm zq`Mh5g>B5%g)5_@KXi57jjW5%X+?|Bq7V>>36!{&^b(FU2S5J$69A13h$P1KRaIM3 zAG(1>su!p$KC%d>s`+lRXB1@*wOd{D>4g`)Xco0P3O@z&91e5D1Q{j1ZY?$iJq}2quZOBG50%U7So^mD6g^ULd?d> zrHVx*`#$b3S|J68=&%IU!g*27oNl185ph&*htufShm%kwdaA+8cIn`^N*IoU_&C#G u`u~qH#cw}!D_kV%B0N;9+8r9LN00&Mm9tQ0Vv~WMn$NO4t{DWtC#rIyvVLv= literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_CAPIPUBLICBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_CAPIPUBLICBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..fe41e1c0d226613a0e5d7dabde4214fe03c89e9d GIT binary patch literal 532 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 BeaQd- literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..0a3e462d28d556e1f9a51066675a45c1b4fd3973 GIT binary patch literal 1051 zcmV+$1myctQ$aES5C8xJ000000ssI20RR910RR9200Ezws^Ksep=v!J#;J*J3GeBg zD4q|L7x_e#?-az#I>JZn2+wC)0B&om-piT@szU-Y8OV0`~ zDRhmR70#_&bjtLDiFZz#1fMu!bIHfnbyz#C5q} zaGC$;U<;0_UUl*9F$gR|1q42BPk>K7;&H_LW^g-*c5<;GXk4c^#QAMGXyH`q4%g*p z$#rW1TI(~jj+0JZ5-cQhp<>%88U~K*RTx$UxKVo=5@F$5H|ddUU$C(N#k$R}uaXce zo9#HG2?MJCE8~x{t9&XWJ=J8V|Icl1FdJhu&FhXYU#}@R?0P3N+6J;Q{XbkYLa#Ub zh?o@h7vw*#n3`#QC4$7Azb75~C$$kWU*G8*_($Hr;ErmbhRmWl z*CIH%@=tPZ%d~*(wQll>|DR0e_ROqV)h{Wbq!zc@L=B>pDppAC*CbrnaMYvzAp4C2 zx@=<_{fijC;VnIdKGK1TmXVXoKl9IP5-2xLp6DNo0*>g0<7g7&7n@cwa3k;RRmK-t zUu?2H(W$3Q-5q+Y9#PTc#<6J?HZkHe9#DafMf+A=-A*=X zE1G2H;a-_ukzh)sca{?BsvkJ}bRo$Vu>scvjefNTBy0*&*#>sNV?r%l;ZtKeo zV^7oX=}jneJw>hDj;h@l0N2$yLbd3I$>N<2FsS6h*osdRcD5IfI^|J(t8$(@q?10i Vjnk1A$b9X&qkZ2EIJVSr5exLV`?CN5 literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAPUBLICBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_4096_RSAPUBLICBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..d9ed738b53a3e3f9a897bb5e0fafb96a6631dd47 GIT binary patch literal 539 zcmV+$0_6QtQ$aBR5C8xJ000000ssI2000000000100Ezws^Ksep=v!J#;J*J3GeBg zD4q|L7x_e#?-az#I>JZn2+wC)0B&om-piT@szU-Y8OV0`~ zDRhmR70#_&bjtLDiFZz#1fMu!bIHfnbyz#C5q} daGC$;U<;0_UUl*9F$gR|1q42BPk>K7;&Fk%{{;X5 literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_512_CAPIPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_512_CAPIPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..234b2b92c97177077a7b33fff4ed1d0570ab3adb GIT binary patch literal 308 zcmV-40n7de0ssI2qyPX?Q$aES0ssI30096IO3s3Ax&Boz7}@|#_5M>B8nn=#7vdN$M>JL5Ux=Qoj1wb)hEwsR5alWS}bdUWHSPxpx1>ZtJmaN35m11_NT}7%t_F8aDV)CXG2TtlqU*% n?siQ+u&qIN?QU^J-zbrNV&!bdUbbf_btzRJ@tJz``1g$fI-ekZ literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAFULLPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAFULLPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..b69c79c6c9b213d55222ecb745fdcf96db06459b GIT binary patch literal 315 zcmV-B0mS}NQ$aHT0ssI50000$0000W0000W0000100FW0$HkRQ#Csx&B4~%{#s+s5 zz7$$bAs4%?Er7PblZxHEAmo&Vgn+r_<3~U%V4RFzXOBe%7ZN&dtgEEZN)hY-U_TDO12)03RlIup9k{w|Ud*_v@G;yH z?{InrmWRMut#kps9z1z4`yyUj*Q|;R60`vEm|6duzXngZJEbt(d_(!Ghgi6_Hq|;C zjc!n3Rjp&7fTLa?YMD0bjk0mnRSZB31X1`2r4yD7Lwp{n+^4e+m6dS&s~#WR;l#sr)9Pb=dm8Qneq6LE^wo NBC}uJV*W(zi6K;Qku(4R literal 0 HcmV?d00001 diff --git a/LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAPRIVATEBLOB.bin b/LibXboxOne.Tests/Resources/RsaKeys/RSA_512_RSAPRIVATEBLOB.bin new file mode 100644 index 0000000000000000000000000000000000000000..ff411d6746ae17548b4e9eb1e3a005753e2c42d4 GIT binary patch literal 155 zcmWFvb~IvOVqjoqU|?_nVg)E>WMJI*{rJ(TK1ZsRx|A~7UmjyC7uzQi<*O*Zd#!H6 zwgZ#9?(R`|GNrYpVdvAw?he`sb9&;^`(0VZg{<<{tX^`#OYjZjc1DNt|u@d2~!->mkSwMt|8LGA>5zWvN*2RDW8sroIuV^>bxnH{S>7~T GetBytesAsync(string fileName, ResourceType type = ResourceType.Misc) + { + var file = $"{ResourcePath}/{type}/{fileName}"; + if (File.Exists(file)) + { + using (FileStream stream = File.OpenRead(file)) + { + byte[] result = new byte[stream.Length]; + await stream.ReadAsync(result, 0, (int)stream.Length); + return result; + } + } + throw new FileNotFoundException(file); + } + public static string GetString(string fileName, ResourceType type = ResourceType.Misc) + { + var file = $"{ResourcePath}/{type}/{fileName}"; + if (File.Exists(file)) + { + return System.Text.Encoding.UTF8.GetString( + File.ReadAllBytes(file) + ); + } + throw new FileNotFoundException(file); + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/XbfsTests.cs b/LibXboxOne.Tests/XbfsTests.cs new file mode 100644 index 0000000..08a489f --- /dev/null +++ b/LibXboxOne.Tests/XbfsTests.cs @@ -0,0 +1,56 @@ +using Xunit; +using LibXboxOne.Nand; + +namespace LibXboxOne.Tests +{ + public class XbfsTests + { + public static XbfsHeader GetHeader() + { + var data = ResourcesProvider.GetBytes("xbfs_header.bin", ResourceType.DataBlobs); + return Shared.BytesToStruct(data); + } + + [Fact] + public void TestXbfsHeaderParsing() + { + XbfsHeader header = GetHeader(); + + Assert.True(header.IsValid); + Assert.True(header.IsHashValid); + Assert.Equal(1, header.FormatVersion); + Assert.Equal(1, header.SequenceNumber); + Assert.Equal(9, header.LayoutVersion); + Assert.Equal((ulong)0, header.Reserved08); + Assert.Equal((ulong)0, header.Reserved10); + Assert.Equal((ulong)0, header.Reserved18); + } + + [Fact] + + public void TestXbfsHeaderRehash() + { + XbfsHeader header = GetHeader(); + header.Files[0].BlockCount = 123; + + Assert.False(header.IsHashValid); + header.Rehash(); + Assert.True(header.IsHashValid); + } + + [Fact] + public void TestXbfsOutOfBounds() + { + XbfsHeader header = GetHeader(); + + // Write a file entry past the known filenames array + header.Files[XbfsFile.XbfsFilenames.Length + 2] = new XbfsEntry(){ + LBA=0, BlockCount=1, Reserved=0 + }; + + // Call to ToString will print filenames and should + // encouter a file that we don't know the filename of + header.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne.Tests/XvdFileTests.cs b/LibXboxOne.Tests/XvdFileTests.cs new file mode 100644 index 0000000..db0a226 --- /dev/null +++ b/LibXboxOne.Tests/XvdFileTests.cs @@ -0,0 +1,282 @@ +using System.IO; +using Xunit; + +namespace LibXboxOne.Tests +{ + public class XvdFileTests + { + [Fact(Skip="Relies on xvd data blob")] + public void Dev_Signed_ValidHash_Test() + { + using (var file = new XvdFile(@"F:\XBone\XVDs\TestXVDs\xvd1")) + { + Assert.True(file.Load()); + /* + if(XvdFile.SignKeyLoaded) + Assert.True(file.Header.IsSignedWithRedKey); + */ + Assert.True(file.IsEncrypted); + Assert.True(file.IsDataIntegrityEnabled); + Assert.True(file.HashTreeValid); + Assert.True(file.DataHashTreeValid); + } + } + + [Fact(Skip="Relies on xvd data blob")] + public void Dev_Signed_InvalidHashTree_Test() + { + using (var file = new XvdFile(@"F:\XBone\XVDs\TestXVDs\xvd1_brokehash")) + { + Assert.True(file.Load()); + /* + if (XvdFile.SignKeyLoaded) + Assert.True(file.Header.IsSignedWithRedKey); + */ + Assert.True(file.IsEncrypted); + Assert.True(file.IsDataIntegrityEnabled); + Assert.False(file.HashTreeValid); + } + } + + [Fact(Skip="Relies on xvd data blob")] + public void Dev_Signed_InvalidDataHashTree_Test() + { + using (var file = new XvdFile(@"F:\XBone\XVDs\TestXVDs\xvd1_brokedatahash")) + { + Assert.True(file.Load()); + /* + if (XvdFile.SignKeyLoaded) + Assert.True(file.Header.IsSignedWithRedKey); + */ + Assert.True(file.IsEncrypted); + Assert.True(file.IsDataIntegrityEnabled); + Assert.True(file.HashTreeValid); + Assert.False(file.DataHashTreeValid); + } + } + + [Fact(Skip="Relies on xvd data blob")] + public void Dev_Signed_XVC_Decrypt_Test() + { + const string dest = @"F:\XBone\XVDs\TestXVDs\xvd1_decrypted_temp"; + const string fileToCompare = @"F:\XBone\XVDs\TestXVDs\xvd1_decrypted"; + if (File.Exists(dest)) + File.Delete(dest); + + File.Copy(@"F:\XBone\XVDs\TestXVDs\xvd1", dest); + using (var file = new XvdFile(dest)) + { + Assert.True(file.Load()); + /* + Assert.True(file.Header.IsSignedWithRedKey); + */ + Assert.True(file.IsEncrypted); + Assert.True(file.IsDataIntegrityEnabled); + Assert.True(file.HashTreeValid); + Assert.True(file.DataHashTreeValid); + Assert.True(file.Decrypt()); + Assert.False(file.IsEncrypted); + ulong[] invalid = file.VerifyDataHashTree(); + Assert.True(invalid.Length == 0); + Assert.True(file.VerifyHashTree()); + + byte[] ntfsString = file.ReadBytes(0x87003, 4); + byte[] expectedString = { 0x4E, 0x54, 0x46, 0x53 }; + Assert.True(ntfsString.IsEqualTo(expectedString)); + } + + byte[] generatedHash; + using (FileStream stream = File.OpenRead(dest)) + { + generatedHash = HashUtils.ComputeSha256(stream); + } + + File.Delete(dest); + + byte[] expectedHash; + using (FileStream stream = File.OpenRead(fileToCompare)) + { + expectedHash = HashUtils.ComputeSha256(stream); + } + + Assert.True(generatedHash.IsEqualTo(expectedHash)); + } + + [Fact(Skip="Relies on xvd data blob")] + public void Dev_Signed_XVC_Encrypt_Test() + { + const string dest = @"F:\XBone\XVDs\TestXVDs\xvd1_encrypted_temp"; + const string fileToCompare = @"F:\XBone\XVDs\TestXVDs\xvd1"; + if (File.Exists(dest)) + File.Delete(dest); + + File.Copy(@"F:\XBone\XVDs\TestXVDs\xvd1_decrypted", dest); + using (var file = new XvdFile(dest)) + { + Assert.True(file.Load()); + Assert.False(file.IsEncrypted); + Assert.True(file.IsDataIntegrityEnabled); + Assert.True(file.HashTreeValid); + Assert.True(file.DataHashTreeValid); + // Assert.True(file.Encrypt()); + Assert.True(file.IsEncrypted); + + ulong[] invalid = file.VerifyDataHashTree(); + Assert.True(invalid.Length == 0); + Assert.True(file.VerifyHashTree()); + } + + byte[] generatedHash; + using (FileStream stream = File.OpenRead(dest)) + { + generatedHash = HashUtils.ComputeSha256(stream); + } + + File.Delete(dest); + + byte[] expectedHash; + using (FileStream stream = File.OpenRead(fileToCompare)) + { + expectedHash = HashUtils.ComputeSha256(stream); + } + + Assert.True(generatedHash.IsEqualTo(expectedHash)); + } + + [Fact(Skip="Relies on xvd data blob")] + public void Unsigned_XVD_Decrypt_Test() + { + const string dest = @"F:\XBone\XVDs\TestXVDs\xvd2_decrypted_temp"; + const string fileToCompare = @"F:\XBone\XVDs\TestXVDs\xvd2_decrypted"; + if (File.Exists(dest)) + File.Delete(dest); + + File.Copy(@"F:\XBone\XVDs\TestXVDs\xvd2", dest); + using (var file = new XvdFile(dest)) + { + Assert.True(file.Load()); + /* + Assert.True(file.Header.IsSignedWithRedKey); + */ + Assert.True(file.IsEncrypted); + Assert.True(file.IsDataIntegrityEnabled); + Assert.True(file.HashTreeValid); + Assert.True(file.DataHashTreeValid); + Assert.True(file.Decrypt()); + Assert.False(file.IsEncrypted); + + ulong[] invalid = file.VerifyDataHashTree(); + Assert.True(invalid.Length == 0); + Assert.True(file.VerifyHashTree()); + + byte[] ntfsString = file.ReadBytes(0x75003, 4); + byte[] expectedString = { 0x4E, 0x54, 0x46, 0x53 }; + Assert.True(ntfsString.IsEqualTo(expectedString)); + } + + byte[] generatedHash; + using (FileStream stream = File.OpenRead(dest)) + { + generatedHash = HashUtils.ComputeSha256(stream); + } + + File.Delete(dest); + + byte[] expectedHash; + using (FileStream stream = File.OpenRead(fileToCompare)) + { + expectedHash = HashUtils.ComputeSha256(stream); + } + + Assert.True(generatedHash.IsEqualTo(expectedHash)); + } + + [Fact(Skip="Relies on xvd data blob")] + public void Unsigned_XVD_Encrypt_Test() + { + const string dest = @"F:\XBone\XVDs\TestXVDs\xvd2_encrypted_temp"; + const string fileToCompare = @"F:\XBone\XVDs\TestXVDs\xvd2"; + if (File.Exists(dest)) + File.Delete(dest); + + File.Copy(@"F:\XBone\XVDs\TestXVDs\xvd2_decrypted_orig_mod", dest); // modded with CIK used in xvd2 + using (var file = new XvdFile(dest)) + { + Assert.True(file.Load()); + /* + Assert.False(file.Header.IsSignedWithRedKey); + */ + Assert.False(file.IsEncrypted); + Assert.False(file.IsDataIntegrityEnabled); + // Assert.True(file.Encrypt()); + Assert.True(file.IsEncrypted); + + // copy values from file being compared so the hashes match + file.Header.PDUID = new byte[] {0xEA, 0xC8, 0xE2, 0x82, 0x2F, 0x58, 0x32, 0x4F, 0x92, 0x29, 0xE1, 0xAB, 0x6E, 0x8F, 0x91, 0x63}; + using (FileStream stream = File.OpenRead(fileToCompare)) + { + stream.Position = 0; + stream.Read(file.Header.Signature, 0, 0x200); + } + + Assert.True(file.AddHashTree()); + + ulong[] invalid = file.VerifyDataHashTree(); + Assert.True(invalid.Length == 0); + Assert.True(file.VerifyHashTree()); + } + + byte[] generatedHash; + using (FileStream stream = File.OpenRead(dest)) + { + generatedHash = HashUtils.ComputeSha256(stream); + } + + File.Delete(dest); + + byte[] expectedHash; + using (FileStream stream = File.OpenRead(fileToCompare)) + { + expectedHash = HashUtils.ComputeSha256(stream); + } + + Assert.True(generatedHash.IsEqualTo(expectedHash)); + } + + /* + [Fact(Skip="Relies on xvd data blob")] + public void XvdSign_Key_Extract() + { + var sdkVersions = new List { "XDK_11785" }; + var versionsTextFile = @"F:\Xbone\Research\xdk_versions.txt"; + if (File.Exists(versionsTextFile)) + { + string[] sdkVersionArr = File.ReadAllLines(versionsTextFile); + foreach (string ver in sdkVersionArr) + if (!sdkVersions.Contains(ver.ToUpper())) + sdkVersions.Add(ver.ToUpper()); + } + + foreach (string ver in sdkVersions) + { + string path = Path.Combine(@"F:\Xbone\Research\", ver); + string binPath = Path.Combine(path, "bin"); + if (Directory.Exists(binPath)) + path = binPath; + if (!File.Exists(Path.Combine(path, "xvdsign.exe"))) + continue; + + XvdFile.CikFileLoaded = false; + XvdFile.OdkKeyLoaded = false; + XvdFile.SignKeyLoaded = false; + XvdFile.LoadKeysFromSdk(path); + Assert.True(XvdFile.CikFileLoaded && XvdFile.OdkKeyLoaded && XvdFile.SignKeyLoaded); + } + + XvdFile.CikFileLoaded = false; + XvdFile.OdkKeyLoaded = false; + XvdFile.SignKeyLoaded = false; + } + */ + } +} diff --git a/LibXboxOne.Tests/XvdHashBlockTests.cs b/LibXboxOne.Tests/XvdHashBlockTests.cs new file mode 100644 index 0000000..1dcadde --- /dev/null +++ b/LibXboxOne.Tests/XvdHashBlockTests.cs @@ -0,0 +1,57 @@ +using Xunit; + +namespace LibXboxOne.Tests +{ + public class XvdHashBlockTests + { + [Theory] + [InlineData(0x4000, 1, 0x1)] + [InlineData(0x5427, 1, 0x1)] + [InlineData(0x20001, 1, 0x5)] + [InlineData(0x20001, 2, 0x1)] + [InlineData(0xA43E3, 1, 0x18)] + [InlineData(0xA43E3, 2, 0x1)] + [InlineData(0xC2BFF, 1, 0x1C)] + [InlineData(0xC2BFF, 2, 0x1)] + public void TestNumHashBlockCalculation(ulong size, ulong index, ulong expected) + { + ulong actual = XvdMath.CalculateNumHashBlocksInLevel(size, index, false); + + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData((XvdType)0, 0x2, 0x4000, 0xE02, 0x0, 0x10, 0x16)] + [InlineData((XvdType)1, 0x3, 0xC001, 0x150A, 0x0, 0x74, 0x22)] + [InlineData((XvdType)1, 0x3, 0x20001, 0x3772, 0x0, 0x54, 0x59)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0x5F604, 0x0, 0x0, 0x909)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0x5F604, 0x1, 0x58, 0xE)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0x5BB94, 0x1, 0x0, 0xE)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0x5BB94, 0x2, 0xD, 0x0)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0xAB2, 0x0, 0x12, 0x1F)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0xF20B, 0x0, 0x53, 0x17B)] + [InlineData((XvdType)1, 0x3, 0x5F653, 0xF60A, 0x0, 0x56, 0x181)] + public void TestCalculateHashBlockNumForBlockNum(XvdType xvdType, ulong hashTreeLevels, ulong xvdDataBlockCount, + ulong blockNum, uint index, + ulong expectedEntryNum, ulong expectedResult) + { + ulong result = XvdMath.CalculateHashBlockNumForBlockNum(xvdType, hashTreeLevels, xvdDataBlockCount, + blockNum, index, out ulong entryNumInBlock); + + Assert.Equal(expectedEntryNum, entryNumInBlock); + Assert.Equal(expectedResult, result); + } + + [Theory] + [InlineData(0x6401, 0x97)] + [InlineData(0x5427, 0x7F)] + [InlineData(0x20001, 0x304)] + [InlineData(0x5F653, 0x8FB)] + public void TestCalculateHashTreeBlockCount(ulong xvdDataBlockCount, ulong expected) + { + ulong result = XvdMath.PagesToBlocks(xvdDataBlockCount); + + Assert.Equal(expected, result); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/AppDirs.cs b/LibXboxOne/AppDirs.cs new file mode 100644 index 0000000..28571b2 --- /dev/null +++ b/LibXboxOne/AppDirs.cs @@ -0,0 +1,38 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace LibXboxOne +{ + public static class AppDirs + { + internal static string GetApplicationBaseDirectory() + { + return Environment.GetFolderPath( + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + /* + * Windows + * Result: C:\Users\\AppData\Local + */ + ? Environment.SpecialFolder.LocalApplicationData + /* + * Mac OS X + * Result: /Users//.config + * + * Linux + * Result: /home//.config + */ + : Environment.SpecialFolder.ApplicationData); + } + + public static string GetApplicationConfigDirectory(string appName) + { + /* + * Windows: C:\Users\\AppData\Local\ + * Linux: /home//.config/ + * Mac OS X: /Users//.config/ + */ + return Path.Combine(GetApplicationBaseDirectory(), appName); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Certificates/BootCapability.cs b/LibXboxOne/Certificates/BootCapability.cs new file mode 100644 index 0000000..2da3745 --- /dev/null +++ b/LibXboxOne/Certificates/BootCapability.cs @@ -0,0 +1,103 @@ +using System; +using System.Runtime.InteropServices; + +namespace LibXboxOne.Certificates +{ + public enum BootCapability : ushort + { + CERT_CAP_NONE = 0x0000, + CERT_CAP_DELETED = 0xFFFF, + + CERT_CAP_SRA_DEVKIT = 0x2001, + CERT_CAP_SRA_DEVKIT_DEBUG = 0x2002, + CERT_CAP_SRA_FILE_IO = 0x2003, + CERT_CAP_SRA_STREAM = 0x2004, + CERT_CAP_SRA_PUSH_DEPLOY = 0x2005, + CERT_CAP_SRA_PULL_DEPLOY = 0x2006, + CERT_CAP_SRA_PROFILING = 0x2007, + CERT_CAP_SRA_JS_PROFILING = 0x2008, + CERT_CAP_RECOVERY = 0x3001, + CERT_CAP_VS_CRASH_DUMP = 0x3002, + CERT_CAP_CRASH_DUMP = 0x3003, + CERT_CAP_REMOTE_MGMT = 0x3004, + CERT_CAP_VIEW_TRACING = 0x3005, + CERT_CAP_TCR_TOOL = 0x3006, + CERT_CAP_XSTUDIO = 0x3007, + CERT_CAP_GESTURE_BUILDER = 0x3008, + CERT_CAP_SPEECH_LAB = 0x3009, + CERT_CAP_SMARTGLASS_STUDIO = 0x300A, + CERT_CAP_NETWORK_FIDDLER = 0x300B, + CERT_CAP_ERA_DEVKIT = 0x4001, + CERT_CAP_HW_BERSINGSEA_DEBUG = 0x4002, + CERT_CAP_ERA_DEVKIT_DEBUG = 0x4003, + CERT_CAP_ERA_FILE_IO = 0x4004, + CERT_CAP_ERA_STREAM = 0x4005, + CERT_CAP_ERA_PUSH_DEPLOY = 0x4006, + CERT_CAP_ERA_PULL_DEPLOY = 0x4007, + CERT_CAP_ERA_EXTRA_MEM = 0x4008, + CERT_CAP_ERA_PROFILING = 0x4009, + CERT_CAP_MS_DEVKIT = 0x6001, + CERT_CAP_HW_CPU_DEBUG = 0x6002, + CERT_CAP_HW_FW_DEBUG = 0x6003, + CERT_CAP_HW_POWER_DEBUG = 0x6004, + CERT_CAP_MEMENC_DISABLED = 0x6005, + CERT_CAP_MEMENC_FIXED_KEY = 0x6006, + CERT_CAP_MEMENC_KEY_0 = 0x6007, + CERT_CAP_CERT_MTE_BOOST = 0x6008, + CERT_CAP_CERT_RIO_BOOST = 0x6009, + CERT_CAP_CERT_TEST_BOOST = 0x600A, + CERT_CAP_UPDATE_TESTER = 0x600B, + CERT_CAP_RED_CPU_CODE = 0x600C, + CERT_CAP_OS_PREVIEW = 0x600D, + CERT_CAP_RETAIL_DEBUGGER = 0x600E, + CERT_CAP_OFFLINE = 0x600F, + CERT_CAP_IGNORE_UPDATESEQUENCE = 0x6010, + CERT_CAP_CERT_QASLT = 0x6011, + CERT_CAP_GPU_FENCE_DEBUG = 0x6013, + CERT_CAP_HW_EN_MEM_SPEED_RETEST = 0x6014, + CERT_CAP_HW_EN_MEM_1GB_RETEST = 0x6015, + CERT_CAP_HW_EN_FUSE_READ = 0x6016, + CERT_CAP_HW_EN_POST_CODE_SECURE = 0x6017, + CERT_CAP_HW_EN_FUSE_OVR_RETEST = 0x6018, + CERT_CAP_HW_EN_MEM_UNLIM_RETEST = 0x6019, + CERT_CAP_ICT_TESTER = 0x601A, + CERT_CAP_LOADXVD_TESTER = 0x601B, + CERT_CAP_WIDE_THERMAL_THRESHOLDS = 0x601C, + CERT_CAP_MS_TESTLAB = 0x601D, + CERT_CAP_ALLOW_DISK_LICENSE = 0x601E, + CERT_CAP_ALLOW_SYSTEM_DOWNGRADE = 0x601F, + CERT_CAP_WIFI_TESTER = 0x6020, + CERT_CAP_GREEN_FIDDLER = 0x6021, + CERT_CAP_KIOSK_MODE = 0x6022, + CERT_CAP_FULL_MEDIA_AUTH = 0x6023, + CERT_CAP_HW_DEVTEST = 0x6024, + CERT_CAP_ALLOW_FUSE_FA = 0x6025, + CERT_CAP_ALLOW_SERIAL_CERT_UPLOAD = 0x6026, + CERT_CAP_ALLOW_INSTRUMENTATION = 0x6027, + CERT_CAP_WIFI_TESTER_DFS = 0x6028, + CERT_CAP_HOSTOS_HW_TEST = 0x6029, + CERT_CAP_HOSTOS_ODD_TEST = 0x602A, + CERT_CAP_REDUCE_MODE_TESTER = 0x7002, + CERT_CAP_SP_DEVKIT = 0x8001, + CERT_CAP_HW_SP_DEBUG = 0x8002, + CERT_CAP_SCP_DEBUG = 0x8003, + CERT_CAP_HW_SDF = 0x8004, + CERT_CAP_HW_ALL_DEBUG = 0x8005, + CERT_CAP_HW_AEB_DEBUG = 0x8006, + CERT_CAP_HW_BTG = 0x8007, + CERT_CAP_SP_TESTER = 0x8008, + CERT_CAP_SP_DEBUG_BUILD = 0x8009, + CERT_CAP_NO_FUSE_BLOW = 0x800A, + CERT_CAP_HW_DIS_N_CALIB_RETEST = 0x800B, + CERT_CAP_HW_DIS_CRIT_FUSE_CHK_RETEST = 0x800C, + CERT_CAP_GREEN_SRA_DEBUG = 0x800D, + CERT_CAP_GREEN_ERA_DEBUG = 0x800E, + CERT_CAP_HW_DIS_RNG_CHK_SECURE = 0x800F, + CERT_CAP_HW_EN_FUSE_OVRD_SECURE = 0x8010, + CERT_CAP_GREEN_HOST_DEBUG = 0x8011, + CERT_CAP_GREEN_ALLOW_DISK_LICENSES = 0x8012, + CERT_CAP_VIRT_CAP_DEVKIT_ANY_EQUIV = 0xF001, + CERT_CAP_VIRT_CAP_DEVKIT_INTERNAL_EQUIV = 0xF002, + CERT_CAP_VIRT_CAP_SP_DEVKIT_EQUIV = 0xF003 + } +} \ No newline at end of file diff --git a/LibXboxOne/Certificates/BootCapabilityCert.cs b/LibXboxOne/Certificates/BootCapabilityCert.cs new file mode 100644 index 0000000..db57abb --- /dev/null +++ b/LibXboxOne/Certificates/BootCapabilityCert.cs @@ -0,0 +1,113 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace LibXboxOne.Certificates +{ + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct BootCapabilityCert + { + public ushort StructId; + public ushort Size; + public ushort ProtocolVersion; + public ushort IssuerKeyId; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + public byte[] IssueDate; + + public DateTime IssueDateTime + { + get + { + var filetime = BitConverter.ToInt64(IssueDate, 0); + return DateTime.FromFileTime(filetime); + } + } + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + public byte[] SocId; + + public ushort GenerationId; + public byte AllowedStates; + public byte LastCapability; + public uint Flags; + public byte ExpireCentury; + public byte ExpireYear; + public byte ExpireMonth; + public byte ExpireDayOfMonth; + public byte ExpireHour; + public byte ExpireMinute; + public byte ExpireSecond; + public byte MinimumSpVersion; + public ulong Minimum2blVersion; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + public byte[] Nonce; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x38)] + public byte[] Reserved; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)] + public BootCapability[] Capabilities; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x180)] + public byte[] RsaSignature; + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + string fmt = formatted ? " " : ""; + + var b = new StringBuilder(); + b.AppendLineSpace("BootCapabilityCert:"); + + if (StructId == 0x00) + { + b.AppendLineSpace(fmt + "! No or invalid certificate (StructId: 0)\n"); + return b.ToString(); + } + + b.AppendLineSpace(fmt + $"StructId: 0x{StructId:X}"); + b.AppendLineSpace(fmt + $"Size: {Size} (0x{Size:X})"); + b.AppendLineSpace(fmt + $"IssuerKeyId: {IssuerKeyId} (0x{IssuerKeyId:X})"); + b.AppendLineSpace(fmt + $"IssueDate: {IssueDateTime}"); + b.AppendLineSpace(fmt + $"SocId: {Environment.NewLine}{fmt}{SocId.ToHexString()}"); + b.AppendLineSpace(fmt + $"GenerationId: {GenerationId} (0x{GenerationId:X})"); + b.AppendLineSpace(fmt + $"AllowedStates: {AllowedStates} (0x{AllowedStates:X})"); + b.AppendLineSpace(fmt + $"LastCapability: {LastCapability} (0x{LastCapability:X})"); + b.AppendLineSpace(fmt + $"Flags: {Flags} (0x{Flags:X})"); + + var expiry = $"{ExpireCentury}{ExpireYear:00}-{ExpireMonth:00}-{ExpireDayOfMonth:00} {ExpireHour:00}:{ExpireMinute:00}:{ExpireSecond:00}"; + if (ExpireCentury == 255) + expiry = "never!"; + b.AppendLineSpace(fmt + $"Expiration date: {expiry}"); + + b.AppendLineSpace(fmt + $"MinimumSpVersion: {MinimumSpVersion} (0x{MinimumSpVersion:X})"); + b.AppendLineSpace(fmt + $"Minimum2blVersion: {Minimum2blVersion} (0x{Minimum2blVersion:X})"); + + b.AppendLineSpace(fmt + $"Nonce: {Environment.NewLine}{fmt}{Nonce.ToHexString()}"); + b.AppendLineSpace(fmt + $"Reserved: {Environment.NewLine}{fmt}{Reserved.ToHexString()}"); + b.AppendLineSpace(fmt + $"RsaSignature: {Environment.NewLine}{fmt}{RsaSignature.ToHexString()}"); + + b.AppendLineSpace(fmt + "Boot Capabilities:"); + for(int i = 0; i < Capabilities.Length; i++) + { + BootCapability cap = Capabilities[i]; + + if (cap == BootCapability.CERT_CAP_NONE + || cap == BootCapability.CERT_CAP_DELETED) + { + continue; + } + b.AppendLine($"{fmt}-{cap}"); + } + + b.AppendLine(); + return b.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Certificates/PspConsoleCert.cs b/LibXboxOne/Certificates/PspConsoleCert.cs new file mode 100644 index 0000000..d53d1db --- /dev/null +++ b/LibXboxOne/Certificates/PspConsoleCert.cs @@ -0,0 +1,120 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace LibXboxOne.Certificates +{ + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct PspConsoleCert + { + public UInt16 StructID; // 0x4343 (ASCII: CC = ConsoleCert?) + + public UInt16 Size; + + public UInt16 IssuerKeyId; // Key Version + + public UInt16 ProtocolVer; // unknown + + public UInt32 IssueDate; // POSIX time + + public DateTime IssueDateTime + { + get + { + return DateTime.UnixEpoch.AddSeconds(IssueDate); + } + } + + public UInt32 PspRevisionId; // PSP Version + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + public byte[] SocId; + + public UInt16 GenerationId; + + public byte ConsoleRegion; + + public byte ReservedByte; // 0 + + public UInt32 ReservedDword; // 0 + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x8)] + public byte[] VendorId; // size of 8 + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)] + public byte[] AttestationPubKey; // Public key that is used by the Xbox One ChalResp system + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)] + public byte[] ReservedPublicKey; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xC)] + public char[] ConsoleSerialNumber; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x8)] + public byte[] ConsoleSku; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + public byte[] ConsoleSettingsHash; // Hash of factory settings (SHA-256) + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xC)] + public char[] ConsolePartNumber; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + public byte[] SomeData; // unknown + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x180)] + public byte[] RsaSignature; + + public string ConsoleSerialNumberString + { + get + { + return new string(ConsoleSerialNumber); + } + } + + public string ConsolePartNumberString + { + get + { + return new string(ConsolePartNumber); + } + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + string fmt = formatted ? " " : ""; + + var b = new StringBuilder(); + b.AppendLineSpace("PspConsoleCert:"); + b.AppendLineSpace(fmt + $"StructId: 0x{StructID:X}"); + b.AppendLineSpace(fmt + $"Size: {Size} (0x{Size:X})"); + b.AppendLineSpace(fmt + $"IssuerKeyId: {IssuerKeyId} (0x{IssuerKeyId:X})"); + b.AppendLineSpace(fmt + $"ProtocolVer: {ProtocolVer} (0x{ProtocolVer:X})"); + b.AppendLineSpace(fmt + $"IssueDateTime: {IssueDateTime} ({IssueDate})"); + b.AppendLineSpace(fmt + $"PspRevisionId: {PspRevisionId} (0x{PspRevisionId:X})"); + b.AppendLineSpace(fmt + $"SocId: {SocId.ToHexString()}"); + b.AppendLineSpace(fmt + $"GenerationId: {GenerationId} (0x{GenerationId:X})"); + b.AppendLineSpace(fmt + $"ConsoleRegion: {ConsoleRegion} (0x{ConsoleRegion:X})"); + b.AppendLineSpace(fmt + $"ReservedByte: {ReservedByte} (0x{ReservedByte:X})"); + b.AppendLineSpace(fmt + $"ReservedDword: {ReservedDword} (0x{ReservedDword:X})"); + b.AppendLineSpace(fmt + $"VendorId: {VendorId.ToHexString()}"); + b.AppendLineSpace(fmt + $"AttestationPubKey: {Environment.NewLine}{fmt}{AttestationPubKey.ToHexString()}"); + b.AppendLineSpace(fmt + $"ReservedPublicKey: {Environment.NewLine}{fmt}{ReservedPublicKey.ToHexString()}"); + b.AppendLineSpace(fmt + $"ConsoleSerialNumberString: {ConsoleSerialNumberString}"); + b.AppendLineSpace(fmt + $"ConsoleSku: {ConsoleSku.ToHexString()}"); + b.AppendLineSpace(fmt + $"ConsoleSettingsHash: {ConsoleSettingsHash.ToHexString(String.Empty)}"); + b.AppendLineSpace(fmt + $"ConsolePartNumberString: {ConsolePartNumberString}"); + b.AppendLineSpace(fmt + $"SomeData: {SomeData.ToHexString()}"); + b.AppendLineSpace(fmt + $"RsaSignature: {Environment.NewLine}{fmt}{RsaSignature.ToHexString()}"); + + b.AppendLine(); + return b.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Common.cs b/LibXboxOne/Common.cs new file mode 100644 index 0000000..854e600 --- /dev/null +++ b/LibXboxOne/Common.cs @@ -0,0 +1,13 @@ +using System; +using System.Reflection; + +namespace LibXboxOne +{ + public static class Common + { + public static string AppVersion => + Assembly.GetExecutingAssembly() + .GetCustomAttribute() + .InformationalVersion; + } +} \ No newline at end of file diff --git a/LibXboxOne/Crypto/AesXts.cs b/LibXboxOne/Crypto/AesXts.cs new file mode 100644 index 0000000..67350b9 --- /dev/null +++ b/LibXboxOne/Crypto/AesXts.cs @@ -0,0 +1,106 @@ +using System; +using System.IO; +using System.Security.Cryptography; + +namespace LibXboxOne +{ + class AesXtsTransform : IDisposable + { + public int BlockSize; + private readonly byte[] _tweakBytes; + private readonly ICryptoTransform _tweakEncryptor; + private readonly ICryptoTransform _dataTransform; + private readonly SymmetricAlgorithm _symmetricAlgorithm; + + public AesXtsTransform(byte[] tweakBytes, byte[] dataAesKey, byte[] tweakAesKey, bool encrypt) + { + if (tweakBytes == null) throw new InvalidDataException("Tweak bytes not provided"); + if (dataAesKey == null) throw new InvalidDataException("Data AES key not provided"); + if (tweakAesKey == null) throw new InvalidDataException("Tweak AES key not provided"); + if (tweakBytes.Length != 16) throw new InvalidDataException("Tweak bytes not 16 bytes"); + if (dataAesKey.Length != 16) throw new InvalidDataException("Data AES key not 16 bytes"); + if (tweakAesKey.Length != 16) throw new InvalidDataException("Tweak AES not 16 bytes"); + + _tweakBytes = tweakBytes; + + _symmetricAlgorithm = Aes.Create(); + _symmetricAlgorithm.Padding = PaddingMode.None; + _symmetricAlgorithm.Mode = CipherMode.ECB; + + byte[] nullIv = new byte[16]; + _tweakEncryptor = _symmetricAlgorithm.CreateEncryptor(tweakAesKey, nullIv); + _dataTransform = encrypt ? _symmetricAlgorithm.CreateEncryptor(dataAesKey, nullIv) : + _symmetricAlgorithm.CreateDecryptor(dataAesKey, nullIv); + + BlockSize = _symmetricAlgorithm.BlockSize / 8; + } + + internal static byte[] MultiplyTweak(byte[] tweak) + { + byte dl = 0; + var newTweak = new byte[0x10]; + + for (int i = 0; i < 0x10; i++) + { + byte cl = tweak[i]; + byte al = cl; + al = (byte)(al + al); + al = (byte)(al | dl); + dl = cl; + newTweak[i] = al; + dl = (byte)(dl >> 7); + } + if (dl != 0) + newTweak[0] = (byte)(newTweak[0] ^ 0x87); + return newTweak; + } + + public int TransformDataUnit(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset, uint dataUnit) + { + byte[] encryptedTweak = new byte[0x10]; + byte[] tweak = _tweakBytes; + + // Update tweak with data unit number + Array.Copy(BitConverter.GetBytes(dataUnit), tweak, 4); + + // Encrypt tweak + _tweakEncryptor.TransformBlock(tweak, 0, tweak.Length, encryptedTweak, 0); + + byte[] encryptedTweakOrig = new byte[0x10]; + Array.Copy(encryptedTweak, encryptedTweakOrig, 0x10); + + int blocks = inputCount / BlockSize; + + // Apply first part of tweak (input-tweak) to input buffer all at once + for (int i = 0; i < blocks; i++) + { + for (int y = 0; y < BlockSize; y++) + outputBuffer[outputOffset + (i * BlockSize) + y] = (byte)(inputBuffer[inputOffset + (i * BlockSize) + y] ^ encryptedTweak[y % encryptedTweak.Length]); + + encryptedTweak = MultiplyTweak(encryptedTweak); + } + + // AES transform the data... + var transformedBytes = _dataTransform.TransformBlock(outputBuffer, outputOffset, inputCount, outputBuffer, outputOffset); + + // Reset tweak back to original encrypted tweak and then apply output-tweak + Array.Copy(encryptedTweakOrig, encryptedTweak, 0x10); + for (int i = 0; i < blocks; i++) + { + for (int y = 0; y < BlockSize; y++) + outputBuffer[outputOffset + (i * BlockSize) + y] = (byte)(outputBuffer[outputOffset + (i * BlockSize) + y] ^ encryptedTweak[y % encryptedTweak.Length]); + + encryptedTweak = MultiplyTweak(encryptedTweak); + } + + return transformedBytes; + } + + public void Dispose() + { + _tweakEncryptor.Dispose(); + _dataTransform.Dispose(); + _symmetricAlgorithm.Dispose(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Crypto/BCryptRsaImport.cs b/LibXboxOne/Crypto/BCryptRsaImport.cs new file mode 100644 index 0000000..0bfbda9 --- /dev/null +++ b/LibXboxOne/Crypto/BCryptRsaImport.cs @@ -0,0 +1,68 @@ +using System.IO; +using System.Runtime.InteropServices; +using System.Security.Cryptography; + +namespace LibXboxOne +{ + public enum BCRYPT_RSABLOB_MAGIC : uint + { + // The key is an RSA public key. + RSAPUBLIC = 0x31415352, + // The key is an RSA private key. + RSAPRIVATE = 0x32415352, + // The key is a full RSA private key. + RSAFULLPRIVATE = 0x33415352 + }; + + [StructLayout(LayoutKind.Sequential)] + public struct BCRYPT_RSAKEY_BLOB + { + public BCRYPT_RSABLOB_MAGIC Magic; + public uint BitLength; + public uint cbPublicExp; + public uint cbModulus; + public uint cbPrime1; + public uint cbPrime2; + }; + + public sealed class BCryptRsaImport + { + public static RSAParameters BlobToParameters(byte[] blobData, out int bitLength, out bool isPrivate) + { + var parameters = new RSAParameters(); + var reader = new BinaryReader(new MemoryStream(blobData)); + + BCRYPT_RSAKEY_BLOB header = reader.ReadStruct(); + if (header.Magic == BCRYPT_RSABLOB_MAGIC.RSAPUBLIC) + isPrivate = false; + else if (header.Magic == BCRYPT_RSABLOB_MAGIC.RSAPRIVATE || + header.Magic == BCRYPT_RSABLOB_MAGIC.RSAFULLPRIVATE) + isPrivate = true; + else + throw new InvalidDataException("Unexpected RSA keyblob"); + + bitLength = (int)header.BitLength; + + parameters.Exponent = reader.ReadBytes((int)header.cbPublicExp); + parameters.Modulus = reader.ReadBytes((int)header.cbModulus); + + if (header.Magic == BCRYPT_RSABLOB_MAGIC.RSAPUBLIC) + return parameters; + + // RSAPRIVATEBLOB + parameters.P = reader.ReadBytes((int)header.cbPrime1); + parameters.Q = reader.ReadBytes((int)header.cbPrime2); + + if (header.Magic == BCRYPT_RSABLOB_MAGIC.RSAPRIVATE) + return parameters; + + // RSAFULLPRIVATEBLOB + parameters.DP = reader.ReadBytes((int)header.cbPrime1); + parameters.DQ = reader.ReadBytes((int)header.cbPrime2); + parameters.InverseQ = reader.ReadBytes((int)header.cbPrime1); + parameters.D = reader.ReadBytes((int)header.cbModulus); + + return parameters; + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Crypto/HashUtils.cs b/LibXboxOne/Crypto/HashUtils.cs new file mode 100644 index 0000000..d4d79ed --- /dev/null +++ b/LibXboxOne/Crypto/HashUtils.cs @@ -0,0 +1,69 @@ +using System.IO; +using System.Security.Cryptography; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Security; + +namespace LibXboxOne +{ + public static class HashUtils + { + public static byte[] ComputeSha256(byte[] data) + { + return ComputeSha256(data, 0, data.Length); + } + + public static byte[] ComputeSha256(Stream stream) + { + return SHA256.Create().ComputeHash(stream); + } + + public static byte[] ComputeSha256(byte[] data, int offset, int length) + { + return SHA256.Create().ComputeHash(data, offset, length); + } + + public static byte[] ComputeSha1(byte[] data) + { + return ComputeSha1(data, 0, data.Length); + } + + public static byte[] ComputeSha1(Stream stream) + { + return SHA1.Create().ComputeHash(stream); + } + + public static byte[] ComputeSha1(byte[] data, int offset, int length) + { + return SHA1.Create().ComputeHash(data, offset, length); + } + + public static bool SignData(byte[] key, string keyType, byte[] data, out byte[] signature) // keyType = RSAFULLPRIVATEBLOB, RSAPRIVATEBLOB, RSAPUBLICBLOB + { + if (keyType != "RSAFULLPRIVATEBLOB") + throw new CryptographicException("Only RSAFULLPRIVATEBLOB can be used for signing"); + + var rsaParams = BCryptRsaImport.BlobToParameters(key, out int bitLength, out bool isPrivate); + var rsaKey = DotNetUtilities.GetRsaKeyPair(rsaParams).Private; + ISigner s = SignerUtilities.GetSigner("SHA256withRSA/PSS"); + + s.Init(true, new ParametersWithRandom(rsaKey)); + s.BlockUpdate(data, 0, data.Length); + + signature = s.GenerateSignature(); + return true; + } + + public static bool VerifySignature(byte[] key, byte[] signature, byte[] data) // keyType = RSAFULLPRIVATEBLOB, RSAPRIVATEBLOB, RSAPUBLICBLOB + { + var rsaParams = BCryptRsaImport.BlobToParameters(key, out int bitLength, out bool isPrivate); + var rsaKey = DotNetUtilities.GetRsaPublicKey(rsaParams); + ISigner s = SignerUtilities.GetSigner("SHA256withRSA/PSS"); + + s.Init(false, new ParametersWithRandom(rsaKey)); + s.BlockUpdate(data, 0, data.Length); + + return s.VerifySignature(signature); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Keys/DurangoKeys.cs b/LibXboxOne/Keys/DurangoKeys.cs new file mode 100644 index 0000000..70aaea2 --- /dev/null +++ b/LibXboxOne/Keys/DurangoKeys.cs @@ -0,0 +1,304 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace LibXboxOne.Keys +{ + public static class DurangoKeys + { + public static Guid TestCIK => new Guid("33EC8436-5A0E-4F0D-B1CE-3F29C3955039"); + public static string RedXvdPrivateKey => "RedXvdPrivateKey"; + + static readonly Dictionary SignkeyStorage = new Dictionary(){ + // Xvd signing keys + {"RedXvdPrivateKey", new DurangoKeyEntry(KeyType.XvdSigningKey, sha256Hash: "8E2B60377006D87EE850334C42FC200081386A838C65D96D1EA52032AA9628C5", dataSize: 0x91B)}, + {"GreenXvdPublicKey", new DurangoKeyEntry(KeyType.XvdSigningKey, sha256Hash: "618C5FB1193040AF8BC1C0199B850B4B5C42E43CE388129180284E4EF0B18082", dataSize: 0x21B)}, + {"GreenGamesPublicKey", new DurangoKeyEntry(KeyType.XvdSigningKey, sha256Hash: "183F0AE05431E4AD91554E88946967C872997227DBE6C85116F5FD2FD2D1229E", dataSize: 0x21B)}, + }; + + static readonly Dictionary OdkStorage = new Dictionary(){ + {OdkIndex.RedOdk, new DurangoKeyEntry(KeyType.Odk, sha256Hash: "CA37132DFB4B811506AE4DC45F45970FED8FE5E58C1BACB259F1B96145B0EBC6", dataSize: 0x20)} + }; + + static readonly Dictionary CikStorage = new Dictionary() + { + {new Guid("33EC8436-5A0E-4F0D-B1CE-3F29C3955039"), new DurangoKeyEntry(KeyType.Cik, sha256Hash: "6786C11B788ED5CCE3C7695425CB82970347180650893D1B5613B2EFB33F9F4E", dataSize: 0x20)}, // TestCIK + {new Guid("F0522B7C-7FC1-D806-43E3-68A5DAAB06DA"), new DurangoKeyEntry(KeyType.Cik, sha256Hash: "B767CE5F83224375E663A1E01044EA05E8022C033D96BED952475D87F0566642", dataSize: 0x20)}, + }; + + static DurangoKeys() + { + } + + public static KeyValuePair[] GetAllXvdSigningKeys() + { + return SignkeyStorage.ToArray(); + } + + public static KeyValuePair[] GetAllODK() + { + return OdkStorage.ToArray(); + } + + public static KeyValuePair[] GetAllCIK() + { + return CikStorage.ToArray(); + } + + public static bool KnowsSignKeySHA256(byte[] sha256Hash, out string keyName) + { + foreach (var kvp in SignkeyStorage) + { + if (!kvp.Value.SHA256Hash.IsEqualTo(sha256Hash)) + continue; + + keyName = kvp.Key; + return true; + } + keyName = ""; + return false; + } + + public static bool KnowsOdkSHA256(byte[] sha256Hash, out OdkIndex keyId) + { + foreach (var kvp in OdkStorage) + { + if (!kvp.Value.SHA256Hash.IsEqualTo(sha256Hash)) + continue; + + keyId = kvp.Key; + return true; + } + keyId = OdkIndex.Invalid; + return false; + } + + public static bool KnowsCikSHA256(byte[] sha256Hash, out Guid keyGuid) + { + foreach (var kvp in CikStorage) + { + if (!kvp.Value.SHA256Hash.IsEqualTo(sha256Hash)) + continue; + + keyGuid = kvp.Key; + return true; + } + keyGuid = Guid.Empty; + return false; + } + + public static bool GetOdkIndexFromString(string name, out OdkIndex odkIndex) + { + // First, try to convert to know Enum values + var success = Enum.TryParse(name, true, out odkIndex); + if (success) + return true; + + odkIndex = OdkIndex.Invalid; + success = UInt32.TryParse(name, out uint odkUint); + if (success) + // Odk Id is valid uint but we don't know its Enum name yet + odkIndex = (OdkIndex)odkUint; + + return success; + } + + public static int LoadCikKeys(byte[] keyData, out Guid[] loadedKeys) + { + int foundCount = 0; + if (keyData.Length < 0x30 || keyData.Length % 0x30 != 0) + throw new Exception("Misaligned CIK, expecting array of 0x30 bytes: 0x10 bytes: GUID, 0x20 bytes: Key"); + + int cikKeyCount = keyData.Length / 0x30; + loadedKeys = new Guid[cikKeyCount]; + using (BinaryReader br = new BinaryReader(new MemoryStream(keyData))) + { + for (int keyIndex = 0; keyIndex < cikKeyCount; keyIndex++) + { + var guid = new Guid(br.ReadBytes(0x10)); + var cikKeyData = br.ReadBytes(0x20); + var sha256Hash = HashUtils.ComputeSha256(cikKeyData); + bool hashMatches = KnowsCikSHA256(sha256Hash, out Guid verifyGuid); + var hashString = sha256Hash.ToHexString(""); + + if (hashMatches && verifyGuid != guid) + { + Console.WriteLine($"CIK {guid} with hash {hashString} is known as {verifyGuid}"); + continue; + } + + if (hashMatches && CikStorage[guid].HasKeyData) + { + // Duplicate key, already loaded + Console.WriteLine($"CIK {guid} is already loaded"); + continue; + } + + CikStorage[guid] = new DurangoKeyEntry(KeyType.Cik, cikKeyData); + foundCount++; + } + } + return foundCount; + } + + public static bool LoadOdkKey(OdkIndex keyId, byte[] keyData, out bool isNewKey) + { + isNewKey = false; + byte[] sha256Hash = HashUtils.ComputeSha256(keyData); + DurangoKeyEntry existingKey = GetOdkById(keyId); + + if (existingKey != null) + { + bool hashMatches = KnowsOdkSHA256(sha256Hash, out OdkIndex verifyKeyId); + if (hashMatches && verifyKeyId != keyId) + { + var hashString = sha256Hash.ToHexString(""); + Console.WriteLine($"ODK {keyId} with hash {hashString} is known as ODK {verifyKeyId}"); + return false; + } + + if (hashMatches && OdkStorage[keyId].HasKeyData) + { + // Duplicate key, already loaded + Console.WriteLine($"ODK {keyId} is already loaded"); + return false; + } + + if (!hashMatches) + { + Console.WriteLine($"ODK {keyId} does not match expected hash"); + return false; + } + + OdkStorage[keyId].SetKey(keyData); + return true; + } + + isNewKey = true; + OdkStorage[keyId] = new DurangoKeyEntry(KeyType.Odk, keyData); + return true; + } + + public static bool LoadSignKey(string desiredKeyName, byte[] keyData, out bool isNewKey, out string keyName) + { + isNewKey = false; + byte[] sha256Hash = HashUtils.ComputeSha256(keyData); + + bool hashMatches = KnowsSignKeySHA256(sha256Hash, out keyName); + if (hashMatches && SignkeyStorage[keyName].HasKeyData) + { + // Duplicate key, already loaded + Console.WriteLine($"SignKey {keyName} is already loaded"); + return false; + } + + if (hashMatches) + { + SignkeyStorage[keyName].SetKey(keyData); + return true; + } + + // New key, using user-set keyname + isNewKey = true; + SignkeyStorage[desiredKeyName] = new DurangoKeyEntry(KeyType.Odk, keyData); + return true; + } + + public static bool LoadKey(KeyType keyType, string keyFilePath) + { + if (keyFilePath == String.Empty) + return false; + + bool success; + var isNewKey = false; + var keyName = String.Empty; + var filename = Path.GetFileNameWithoutExtension(keyFilePath); + var keyBytes = File.ReadAllBytes(keyFilePath); + + switch(keyType) + { + case KeyType.XvdSigningKey: + success = LoadSignKey(filename, keyBytes, out isNewKey, out keyName); + break; + case KeyType.Odk: + success = GetOdkIndexFromString(filename, out OdkIndex odkIndex); + if (!success) + { + Console.WriteLine($"Could not get OdkIndex from filename: {filename}"); + break; + } + success = LoadOdkKey(odkIndex, keyBytes, out isNewKey); + break; + case KeyType.Cik: + var keyCount = LoadCikKeys(keyBytes, out Guid[] loadedCiks); + success = keyCount > 0; + break; + default: + throw new InvalidOperationException("Invalid KeyType supplied"); + } + + return success; + } + + public static int LoadKeysRecursive(string basePath) + { + int foundCount = 0; + foreach (KeyType keyType in Enum.GetValues(typeof(KeyType))) + { + var keyDirectory = Path.Combine(basePath, keyType.ToString()); + if (!Directory.Exists(keyDirectory)) + { + Console.WriteLine($"Key directory \"{keyDirectory}\" was not found!"); + continue; + } + + var keyFiles = Directory.GetFiles(keyDirectory); + foreach (var keyFilePath in keyFiles) + { + if (LoadKey(keyType, keyFilePath)) + { + Console.WriteLine($"Loaded key {keyType} from {keyFilePath}"); + foundCount++; + } + else + Console.WriteLine($"Unable to load key from \"{keyFilePath}\""); + } + } + return foundCount; + } + + public static DurangoKeyEntry GetSignkeyByName(string keyName) + { + return SignkeyStorage.ContainsKey(keyName) ? SignkeyStorage[keyName] : null; + } + + public static DurangoKeyEntry GetCikByGuid(Guid keyId) + { + return CikStorage.ContainsKey(keyId) ? CikStorage[keyId] : null; + } + + public static DurangoKeyEntry GetOdkById(OdkIndex keyId) + { + return OdkStorage.ContainsKey(keyId) ? OdkStorage[keyId] : null; + } + + public static bool IsSignkeyLoaded(string keyName) + { + var key = GetSignkeyByName(keyName); + return key != null && key.HasKeyData; + } + + public static bool IsCikLoaded(Guid keyId) + { + var key = GetCikByGuid(keyId); + return key != null && key.HasKeyData; + } + + public static bool IsOdkLoaded(OdkIndex keyId) + { + var key = GetOdkById(keyId); + return key != null && key.HasKeyData; + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Keys/KeyEntry.cs b/LibXboxOne/Keys/KeyEntry.cs new file mode 100644 index 0000000..09887f3 --- /dev/null +++ b/LibXboxOne/Keys/KeyEntry.cs @@ -0,0 +1,57 @@ +using System; + +namespace LibXboxOne.Keys +{ + public interface IKeyEntry + { + bool HasKeyData { get; } + + int DataSize { get; } + byte[] SHA256Hash { get; } + byte[] KeyData { get; } + KeyType KeyType { get; } + + void SetKey(byte[] keyData); + } + + public class DurangoKeyEntry : IKeyEntry + { + public bool HasKeyData => KeyData != null && KeyData.Length == DataSize; + public byte[] SHA256Hash { get; } + public int DataSize { get; } + public byte[] KeyData { get; private set; } + public KeyType KeyType { get; } + + public DurangoKeyEntry(KeyType keyType, string sha256Hash, int dataSize) + { + KeyType = keyType; + SHA256Hash = sha256Hash.ToBytes(); + DataSize = dataSize; + if (SHA256Hash.Length != 0x20) + throw new DataMisalignedException("Invalid length for SHA256Hash"); + } + + public DurangoKeyEntry(KeyType keyType, byte[] keyData) + { + KeyType = keyType; + SHA256Hash = HashUtils.ComputeSha256(keyData); + DataSize = keyData.Length; + KeyData = keyData; + } + + public void SetKey(byte[] newKeyData) + { + if (HasKeyData) + throw new InvalidOperationException("KeyData is already filled!"); + if (newKeyData.Length != DataSize) + throw new InvalidProgramException($"Unexpected keydata of length: {newKeyData.Length} bytes"); + + KeyData = newKeyData; + } + + public override string ToString() + { + return $"Loaded: {HasKeyData} Hash: {SHA256Hash.ToHexString("")} Size: {DataSize}"; + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Keys/KeyType.cs b/LibXboxOne/Keys/KeyType.cs new file mode 100644 index 0000000..206ddf0 --- /dev/null +++ b/LibXboxOne/Keys/KeyType.cs @@ -0,0 +1,9 @@ +namespace LibXboxOne.Keys +{ + public enum KeyType + { + XvdSigningKey, + Odk, // Offline Distribution Key, AES256 (32 bytes) key + Cik // Content Instance Key, Guid (16 bytes) + AES256 (32 bytes) key + } +} \ No newline at end of file diff --git a/LibXboxOne/Keys/OdkIndex.cs b/LibXboxOne/Keys/OdkIndex.cs new file mode 100644 index 0000000..1a942fc --- /dev/null +++ b/LibXboxOne/Keys/OdkIndex.cs @@ -0,0 +1,10 @@ +namespace LibXboxOne.Keys +{ + public enum OdkIndex : uint + { + StandardOdk = 0, + GreenOdk = 1, + RedOdk = 2, + Invalid = 0xFFFFFFFF + } +} diff --git a/LibXboxOne/LibXboxOne.csproj b/LibXboxOne/LibXboxOne.csproj new file mode 100644 index 0000000..a3761e0 --- /dev/null +++ b/LibXboxOne/LibXboxOne.csproj @@ -0,0 +1,25 @@ + + + + net7.0 + + + + + + + + + + + + + + + + + + <_Parameter1>$(MSBuildProjectName).Tests + + + diff --git a/LibXboxOne/MiscStructs.cs b/LibXboxOne/MiscStructs.cs new file mode 100644 index 0000000..236225b --- /dev/null +++ b/LibXboxOne/MiscStructs.cs @@ -0,0 +1,78 @@ +using System.Runtime.InteropServices; + +namespace LibXboxOne +{ + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct XviHeader + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x240)] + /* 0x0 */ + public byte[] Unknown1; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x240 */ + public byte[] ContentId; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x250 */ + public byte[] VDUID; + /* 0x260 */ + public ulong Unknown4; + /* 0x268 */ + public uint Unknown5; // should be 1 + /* 0x26C */ + public uint Unknown6; // should be r9d + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct EkbFile + { // EKBs also seem to follow the XvcLicenseBlock format, but with 2 byte block ids instead of 4 byte. + // note: this is just here for reference as EKB files are written sequentially like this, but EKB files should be loaded with the XvcLicenseBlock class now since it's assumed that's how the xbox reads them + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x4)] + /* 0x0 */ + public byte[] Magic; // EKB1 + + /* 0x4 */ + public uint HeaderLength; // (length of data proceeding this) + /* 0x8 */ + public ushort Unknown1; // 1? + /* 0xA */ + public uint Unknown2; // 2? + /* 0xE */ + public ushort Unknown3; // 5? + /* 0x10 */ + public ushort Unknown4; // 2? + /* 0x12 */ + public uint Unknown5; // 0x20? + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x16 */ + public byte[] KeyIdHexChars; + + /* 0x36 */ + public ushort Unknown7; // 3? + /* 0x38 */ + public uint Unknown8; // 2? + /* 0x3C */ + public ushort Unknown9; // 6? lots of calcs for this + /* 0x3E */ + public ushort Unknown10; // 4? + /* 0x40 */ + public uint Unknown11; // 1? + /* 0x44 */ + public byte Unknown12; // 0x31? + /* 0x45 */ + public ushort Unknown13; // 5? + /* 0x47 */ + public uint Unknown14; // 2? + /* 0x4B */ + public ushort Unknown15; // 1? + /* 0x4D */ + public ushort Unknown16; // 7? + /* 0x4F */ + public uint EncryptedDataLength; // EncryptedDataLength + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x180)] + /* 0x53 */ + public byte[] EncryptedData; + } +} diff --git a/LibXboxOne/NAND/XbfsEntry.cs b/LibXboxOne/NAND/XbfsEntry.cs new file mode 100644 index 0000000..465a21e --- /dev/null +++ b/LibXboxOne/NAND/XbfsEntry.cs @@ -0,0 +1,37 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace LibXboxOne.Nand +{ + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct XbfsEntry + { + /* 0x0 */ public uint LBA; + /* 0x4 */ public uint BlockCount; + /* 0x8 */ public ulong Reserved; + + public long Length => BlockCount * XbfsFile.BlockSize; + + public long Offset(XbfsFlavor flavor) { + var offset = LBA * XbfsFile.BlockSize; + if (flavor == XbfsFlavor.XboxSeries && offset >= XbfsFile.SeriesOffsetDiff) + offset -= XbfsFile.SeriesOffsetDiff; + return offset; + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.Append($"LBA: 0x{LBA:X} (One: 0x{Offset(XbfsFlavor.XboxOne):X} Series: 0x{Offset(XbfsFlavor.XboxSeries):X}), "); + b.Append($"Length: 0x{BlockCount:X} (0x{Length:X}), "); + b.Append($"Reserved: 0x{Reserved:X}"); + + return b.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/NAND/XbfsFile.cs b/LibXboxOne/NAND/XbfsFile.cs new file mode 100644 index 0000000..b9a0c81 --- /dev/null +++ b/LibXboxOne/NAND/XbfsFile.cs @@ -0,0 +1,304 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Linq; + +namespace LibXboxOne.Nand +{ + public enum XbfsFlavor { + XboxOne, + XboxSeries, + Invalid, + } + + public enum NandSize: ulong + { + EMMC_LOGICAL = 0x13B00_0000, + EMMC_PHYSICAL = 0x13C00_0000, + NVME_SERIES = 0x4000_0000, + } + + public class XbfsFile + { + public static readonly int SeriesOffsetDiff = 0x6000; + public static readonly int BlockSize = 0x1000; + public static readonly int[] XbfsOffsetsXboxOne = { 0x1_0000, 0x81_0000, 0x82_0000 }; + public static readonly int[] XbfsOffsetsXboxSeries = { 0x0, 0x1800_8000 }; + public static string[] XbfsFilenames = + { + "1smcbl_a.bin", // 0 + "header.bin", // 1 + "devkit.ini", // 2 + "mtedata.cfg", // 3 + "certkeys.bin", // 4 + "smcerr.log", // 5 + "system.xvd", // 6 + "$sospf.xvd", // 7, formerly $sosrst.xvd + "download.xvd", // 8 + "smc_s.cfg", // 9 + "sp_s.cfg", // 10, keyvault? has serial/partnum/osig, handled by psp.sys (/Device/psp) + "os_s.cfg", // 11 + "smc_d.cfg", // 12 + "sp_d.cfg", // 13 + "os_d.cfg", // 14 + "smcfw.bin", // 15 + "boot.bin", // 16 + "host.xvd", // 17 + "settings.xvd", // 18 + "1smcbl_b.bin", // 19 + "bootanim.dat", // 20, this entry and ones below it are only in retail 97xx and above? + "obsolete.001", // 21, formerly sostmpl.xvd + "update.cfg", // 22 + "obsolete.002", // 23, formerly sosinit.xvd + "hwinit.cfg", // 24 + "qaslt.xvd", // 25 + "sp_s.bak", // 26, keyvault backup? has serial/partnum/osig + "update2.cfg", // 27 + "obsolete.003", // 28 + "dump.lng", // 29 + "os_d_dev.cfg", // 30 + "os_glob.cfg", // 31 + "sp_s.alt", // 32 + "sysauxf.xvd", // 33 + }; + + private readonly IO _io; + + public XbfsFlavor Flavor = XbfsFlavor.Invalid; + public int[] HeaderOffsets; + public List XbfsHeaders; + + public readonly string FilePath; + public long FileSize => _io.Stream.Length; + + public XbfsFile(string path) + { + FilePath = path; + _io = new IO(path); + } + + public static XbfsFlavor FlavorFromSize(long size) { + return size switch + { + (long)NandSize.EMMC_LOGICAL or (long)NandSize.EMMC_PHYSICAL => XbfsFlavor.XboxOne, + (long)NandSize.NVME_SERIES => XbfsFlavor.XboxSeries, + _ => XbfsFlavor.Invalid, + }; + } + + /// + /// Get Filename for XBFS filename from index + /// + /// Index of file + /// + public static string GetFilenameForIndex(int index) + { + if (index >= XbfsFilenames.Length) { + return null; + } + + return XbfsFilenames[index]; + } + + /// + /// Get index for XBFS filename from filename + /// + /// Filename + /// Returns >= 0 if file is found, -1 otherwise + public static int GetFileindexForName(string name) + { + return Array.IndexOf(XbfsFilenames, name); + } + + public Certificates.PspConsoleCert? ReadPspConsoleCertificate() + { + if (XbfsHeaders == null || XbfsHeaders.Count == 0) + return null; + + long spDataSize = SeekToFile("sp_s.cfg"); + if (spDataSize <= 0) + return null; + + // SP_S.cfg: (secure processor secured config? there's also a blank sp_d.cfg which is probably secure processor decrypted config) + // 0x0 - 0x200 - signature? + // 0x200 - 0x5200 - encrypted data? maybe loaded and decrypted into PSP memory? + // 0x5200 - 0x5400 - blank + // 0x5400 - 0x5800 - console certificate + // 0x5800 - 0x6000 - unknown data, looks like it has some hashes and the OSIG of the BR drive + // 0x6000 - 0x6600 - encrypted data? + // 0x6600 - 0x7400 - blank + // 0x7400 - 0x7410 - unknown data, hash maybe + // 0x7410 - 0x40000 - blank + + _io.Stream.Position += 0x5400; // seek to start of unencrypted data in sp_s (console certificate) + return _io.Reader.ReadStruct(); + } + + public Certificates.BootCapabilityCert? ReadBootcapCertificate() + { + if (XbfsHeaders == null || XbfsHeaders.Count == 0) + return null; + + long spDataSize = SeekToFile("certkeys.bin"); + if (spDataSize <= 0) + return null; + + return _io.Reader.ReadStruct(); + } + + public bool Load() + { + Flavor = FlavorFromSize(FileSize); + + HeaderOffsets = Flavor switch { + XbfsFlavor.XboxOne => XbfsOffsetsXboxOne, + XbfsFlavor.XboxSeries => XbfsOffsetsXboxSeries, + _ => throw new InvalidDataException($"Invalid xbfs filesize: {FileSize:X}"), + }; + + // read each XBFS header + XbfsHeaders = new List(); + foreach (int offset in HeaderOffsets) + { + _io.Stream.Position = offset; + var header = _io.Reader.ReadStruct(); + XbfsHeaders.Add(header); + } + return true; + } + + // returns the size of the file if found + public long SeekToFile(string fileName) + { + int idx = GetFileindexForName(fileName); + if (idx < 0) + return 0; + + long size = 0; + for (int i = 0; i < XbfsHeaders.Count; i++) + { + if (!XbfsHeaders[i].IsValid) + continue; + if (idx >= XbfsHeaders[i].Files.Length) + continue; + var ent = XbfsHeaders[i].Files[idx]; + if (ent.BlockCount == 0) + continue; + _io.Stream.Position = ent.Offset(Flavor); + size = ent.Length; + } + return size; + } + + public string GetXbfsInfo() + { + var info = new Dictionary(); + for (int i = 0; i < XbfsHeaders.Count; i++) + { + if (!XbfsHeaders[i].IsValid) + continue; + for (int y = 0; y < XbfsHeaders[i].Files.Length; y++) + { + var ent = XbfsHeaders[i].Files[y]; + if (ent.Length == 0) + continue; + long start = ent.Offset(Flavor); + long length = ent.Length; + long end = start + length; + string addInfo = $"{end:X} {i}_{y}"; + if (info.ContainsKey(start)) + info[start] += $" {addInfo}"; + else + info.Add(start, addInfo); + } + } + string infoStr = String.Empty; + var keys = info.Keys.ToList(); + keys.Sort(); + foreach (var key in keys) + infoStr += $"{key:X} - {info[key]}{Environment.NewLine}"; + + return infoStr; + } + + public void ExtractXbfsData(string folderPath) + { + if (!Directory.Exists(folderPath)) { + Console.WriteLine($"Creating output folder for xbfs extraction '{folderPath}'"); + Directory.CreateDirectory(folderPath); + } + + var doneAddrs = new List(); + for (int i = 0; i < XbfsHeaders.Count; i++) + { + if (!XbfsHeaders[i].IsValid) + continue; + for (int y = 0; y < XbfsHeaders[i].Files.Length; y++) + { + var ent = XbfsHeaders[i].Files[y]; + if (ent.BlockCount == 0) + continue; + + var xbfsFilename = GetFilenameForIndex(y); + string fileName = $"{ent.Offset(Flavor):X}_{ent.Length:X}_{i}_{y}_{xbfsFilename ?? "unknown"}"; + + long read = 0; + long total = ent.Length; + _io.Stream.Position = ent.Offset(Flavor); + + bool writeFile = true; + if (doneAddrs.Contains(_io.Stream.Position)) + { + writeFile = false; + fileName = $"DUPE_{fileName}"; + } + doneAddrs.Add(_io.Stream.Position); + + + if (_io.Stream.Position + total > _io.Stream.Length) + continue; + + using (var fileIo = new IO(Path.Combine(folderPath, fileName), FileMode.Create)) + { + if (!writeFile) // create empty file for DUPE_* files + continue; + + while (read < total) + { + int toRead = 0x4000; + if (total - read < toRead) + toRead = (int) (total - read); + byte[] data = _io.Reader.ReadBytes(toRead); + fileIo.Writer.Write(data); + read += toRead; + } + } + } + } + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine("XbfsFile"); + b.AppendLine($"Flavor: {Flavor}"); + b.AppendLine($"Size: 0x{FileSize:X} ({FileSize / 1024 / 1024} MB)"); + b.AppendLine(); + for (int i = 0; i < XbfsHeaders.Count; i++) + { + if(!XbfsHeaders[i].IsValid) + continue; + + b.AppendLine($"XbfsHeader slot {i}: (0x{HeaderOffsets[i]:X})"); + b.Append(XbfsHeaders[i].ToString(formatted)); + } + return b.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/NAND/XbfsHeader.cs b/LibXboxOne/NAND/XbfsHeader.cs new file mode 100644 index 0000000..62c1109 --- /dev/null +++ b/LibXboxOne/NAND/XbfsHeader.cs @@ -0,0 +1,85 @@ +using System; +using System.Text; +using System.Runtime.InteropServices; + +namespace LibXboxOne.Nand +{ + // XBFS header, can be at 0x10000, 0x810000 or 0x820000 + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct XbfsHeader + { + public static readonly int DataToHash = 0x3E0; + public static readonly string XbfsMagic = "SFBX"; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + /* 0x0 */ public char[] Magic; // SFBX + + /* 0x4 */ public byte FormatVersion; + /* 0x5 */ public byte SequenceNumber; // Indicates latest filesystem, wraps around: 0xFF -> 0x00 + /* 0x6 */ public ushort LayoutVersion; // 3 + /* 0x8 */ public ulong Reserved08; // 0 + /* 0x10 */ public ulong Reserved10; // 0 + /* 0x18 */ public ulong Reserved18; // 0 + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3A)] + /* 0x20 */ public XbfsEntry[] Files; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x3C0 */ public byte[] Reserved3C0; + + /* 0x3D0 */ public Guid SystemXVID; // GUID + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x3E0 */ public byte[] XbfsHash; // SHA256 hash of 0x0 - 0x3E0 + + public string MagicString => new string(Magic); + + public bool IsValid => MagicString == XbfsMagic; + + public bool IsHashValid => XbfsHash.IsEqualTo(CalculateHash()); + + byte[] CalculateHash() + { + byte[] data = Shared.StructToBytes(this); + return HashUtils.ComputeSha256(data, 0, DataToHash); + } + + public void Rehash() + { + XbfsHash = CalculateHash(); + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + string fmt = formatted ? " " : ""; + + var b = new StringBuilder(); + b.AppendLineSpace(fmt + $"Magic: {new string(Magic)}"); + b.AppendLineSpace(fmt + $"Format Version: 0x{FormatVersion:X}"); + b.AppendLineSpace(fmt + $"Sequence Number: 0x{SequenceNumber:X}"); + b.AppendLineSpace(fmt + $"Layout Version: 0x{LayoutVersion:X}"); + b.AppendLineSpace(fmt + $"Reserved08: 0x{Reserved08:X}"); + b.AppendLineSpace(fmt + $"Reserved10: 0x{Reserved10:X}"); + b.AppendLineSpace(fmt + $"Reserved18: 0x{Reserved18:X}"); + b.AppendLineSpace(fmt + $"Reserved3C0: {Reserved3C0.ToHexString()}"); + b.AppendLineSpace(fmt + $"System XVID: {SystemXVID}"); + b.AppendLineSpace(fmt + $"XBFS header hash: {Environment.NewLine}{fmt}{XbfsHash.ToHexString()} (Valid: {IsHashValid})"); + b.AppendLine(); + + for(int i = 0; i < Files.Length; i++) + { + XbfsEntry entry = Files[i]; + if (entry.Length == 0) + continue; + b.AppendLine($"File {i}: {XbfsFile.GetFilenameForIndex(i) ?? ""} {entry.ToString(formatted)}"); + } + + return b.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/Shared.cs b/LibXboxOne/Shared.cs new file mode 100644 index 0000000..a56f833 --- /dev/null +++ b/LibXboxOne/Shared.cs @@ -0,0 +1,298 @@ +using System; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; + +namespace LibXboxOne +{ + public class Natives + { + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdMount(out IntPtr hDiskHandle, + out int mountedDiskNum, + IntPtr hXvdHandle, + [MarshalAs(UnmanagedType.LPWStr)] string pszXvdPath, + long unknown, + [MarshalAs(UnmanagedType.LPWStr)] string pszMountPoint, + int mountFlags); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdMountContentType(out IntPtr hDiskHandle, + out int mountedDiskNum, + IntPtr hXvdHandle, + [MarshalAs(UnmanagedType.LPWStr)] string pszXvdPath, + long xvdContentType, + long unknown, + [MarshalAs(UnmanagedType.LPWStr)] string pszMountPoint, + int mountFlags); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdVmMount(out IntPtr hDiskHandle, + IntPtr hXvdHandle, + long vmNumber, + [MarshalAs(UnmanagedType.LPWStr)] string pszXvdPath, + long unknown, + [MarshalAs(UnmanagedType.LPWStr)] string pszMountPoint, + int mountFlags); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdUnmountDiskNumber(IntPtr hXvdHandle, + int diskNum); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdUnmountFile(IntPtr hXvdHandle, + [MarshalAs(UnmanagedType.LPWStr)] string pszXvdPath); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdVmUnmountFile(IntPtr hXvdHandle, + long vmId, + [MarshalAs(UnmanagedType.LPWStr)] string pszXvdPath); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdOpenAdapter(out IntPtr phXvdHandle); + + [DllImport("xsapi.dll", SetLastError = true)] + public static extern uint XvdCloseAdapter(IntPtr phXvdHandle); + + [DllImport("kernel32.dll")] + public static extern int DeviceIoControl(IntPtr hDevice, int + dwIoControlCode, ref short lpInBuffer, int nInBufferSize, IntPtr + lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr + lpOverlapped); + } +// ReSharper disable once InconsistentNaming + public class IO : IDisposable + { + public BinaryReader Reader; + public BinaryWriter Writer; + public Stream Stream; + + public IO(string filePath) + { + Stream = new FileStream(filePath, FileMode.Open); + InitIo(); + } + + public IO(string filePath, FileMode mode) + { + Stream = new FileStream(filePath, mode); + InitIo(); + } + + public IO(Stream baseStream) + { + Stream = baseStream; + InitIo(); + } + + public void Dispose() + { + Stream.Dispose(); + Reader.Dispose(); + Writer.Dispose(); + } + + public bool AddBytes(long numBytes) + { + const int blockSize = 0x1000; + + long startPos = Stream.Position; + long startSize = Stream.Length; + long endPos = startPos + numBytes; + long endSize = Stream.Length + numBytes; + + Stream.SetLength(endSize); + + long totalWrite = startSize - startPos; + + while (totalWrite > 0) + { + int toRead = totalWrite < blockSize ? (int)totalWrite : blockSize; + + Stream.Position = startPos + (totalWrite - toRead); + var data = Reader.ReadBytes(toRead); + + Stream.Position = startPos + (totalWrite - toRead); + var blankData = new byte[toRead]; + Writer.Write(blankData); + + Stream.Position = endPos + (totalWrite - toRead); + Writer.Write(data); + + totalWrite -= toRead; + } + + Stream.Position = startPos; + + return true; + } + + public bool DeleteBytes(long numBytes) + { + if (Stream.Position + numBytes > Stream.Length) + return false; + + const int blockSize = 0x1000; + + long startPos = Stream.Position; + long endPos = startPos + numBytes; + long endSize = Stream.Length - numBytes; + long i = 0; + + while (i < endSize) + { + long totalRemaining = endSize - i; + int toRead = totalRemaining < blockSize ? (int)totalRemaining : blockSize; + + Stream.Position = endPos + i; + byte[] data = Reader.ReadBytes(toRead); + + Stream.Position = startPos + i; + Writer.Write(data); + + i += toRead; + } + + Stream.SetLength(endSize); + return true; + } + + private void InitIo() + { + Reader = new BinaryReader(Stream); + Writer = new BinaryWriter(Stream); + } + } + public static class Shared + { + public static string FindFile(string fileName) + { + string test = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); + if (File.Exists(test)) + return test; + string[] drives = Directory.GetLogicalDrives(); + foreach (string drive in drives) + { + test = Path.Combine(drive, fileName); + if (File.Exists(test)) + return test; + } + return String.Empty; + } + + /// + /// Reads in a block from a file and converts it to the struct + /// type specified by the template parameter + /// + /// + /// + /// + public static T ReadStruct(this BinaryReader reader) + { + var size = Marshal.SizeOf(typeof (T)); + // Read in a byte array + var bytes = reader.ReadBytes(size); + + return BytesToStruct(bytes); + } + + public static bool WriteStruct(this BinaryWriter writer, T structure) + { + byte[] bytes = StructToBytes(structure); + + writer.Write(bytes); + + return true; + } + + public static T BytesToStruct(byte[] bytes) + { + // Pin the managed memory while, copy it out the data, then unpin it + var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); + var theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); + handle.Free(); + + return theStructure; + } + + public static byte[] StructToBytes(T structure) + { + var bytes = new byte[Marshal.SizeOf(typeof(T))]; + + // Pin the managed memory while, copy in the data, then unpin it + var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); + Marshal.StructureToPtr(structure, handle.AddrOfPinnedObject(), true); + handle.Free(); + + return bytes; + } + + public static string ToHexString(this byte[] bytes, string seperator = " ") + { + return bytes.Aggregate("", (current, b) => $"{current}{b:X2}{seperator}"); + } + + public static string ToHexString(this uint[] array, string seperator = " ") + { + return array.Aggregate("", (current, b) => $"{current}0x{b:X8}{seperator}"); + } + + public static string ToHexString(this ushort[] array, string seperator = " ") + { + return array.Aggregate("", (current, b) => $"{current}0x{b:X4}{seperator}"); + } + + public static byte[] ToBytes(this string hexString) + { + hexString = hexString.Replace(" ", ""); + + byte[] retval = new byte[hexString.Length / 2]; + for (int i = 0; i < hexString.Length; i += 2) + retval[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16); + return retval; + } + + public static bool IsArrayEmpty(this byte[] bytes) + { + return bytes.All(b => b == 0); + } + public static bool IsEqualTo(this byte[] byte1, byte[] byte2) + { + if (byte1.Length != byte2.Length) + return false; + + for (int i = 0; i < byte1.Length; i++) + if (byte1[i] != byte2[i]) + return false; + + return true; + } + + public static void AppendLineSpace(this StringBuilder b, string str) + { + b.AppendLine(str + " "); + } + + public static ushort EndianSwap(this ushort num) + { + byte[] data = BitConverter.GetBytes(num); + Array.Reverse(data); + return BitConverter.ToUInt16(data, 0); + } + + public static uint EndianSwap(this uint num) + { + byte[] data = BitConverter.GetBytes(num); + Array.Reverse(data); + return BitConverter.ToUInt32(data, 0); + } + + public static ulong EndianSwap(this ulong num) + { + byte[] data = BitConverter.GetBytes(num); + Array.Reverse(data); + return BitConverter.ToUInt64(data, 0); + } + } +} diff --git a/LibXboxOne/ThirdParty/ProgressBar.cs b/LibXboxOne/ThirdParty/ProgressBar.cs new file mode 100644 index 0000000..23eeb74 --- /dev/null +++ b/LibXboxOne/ThirdParty/ProgressBar.cs @@ -0,0 +1,101 @@ +using System; +using System.Text; +using System.Threading; + +/// +/// An ASCII progress bar +/// +namespace LibXboxOne +{ + // Source: https://gist.github.com/DanielSWolf/0ab6a96899cc5377bf54 + // License: MIT + public class ProgressBar : IDisposable, IProgress + { + private const int blockCount = 30; + private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8); + private const string animation = @"|/-\"; + + private readonly Timer timer; + + private long totalItems = 0; + private long currentItem = 0; + private string description = String.Empty; + private string currentText = string.Empty; + private bool disposed = false; + private int animationIndex = 0; + + public ProgressBar(long items, string unitDescription="") { + totalItems = items; + description = unitDescription; + timer = new Timer(TimerHandler); + + // A progress bar is only for temporary display in a console window. + // If the console output is redirected to a file, draw nothing. + // Otherwise, we'll end up with a lot of garbage in the target file. + if (!Console.IsOutputRedirected) { + ResetTimer(); + } + } + + public void Report(long current) { + Interlocked.Exchange(ref currentItem, current); + } + + private void TimerHandler(object state) { + lock (timer) { + if (disposed) return; + + double progress = (double)(currentItem + 1) / totalItems; + int progressBlockCount = (int) (progress * blockCount); + int percent = (int) (progress * 100); + string text = string.Format("{0,3}% [{1}{2}] {3}/{4} {5} {6}", + percent, + new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount), + currentItem, + totalItems, + description, + animation[animationIndex++ % animation.Length]); + UpdateText(text); + + ResetTimer(); + } + } + + private void UpdateText(string text) { + // Get length of common portion + int commonPrefixLength = 0; + int commonLength = Math.Min(currentText.Length, text.Length); + while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength]) { + commonPrefixLength++; + } + + // Backtrack to the first differing character + StringBuilder outputBuilder = new StringBuilder(); + outputBuilder.Append('\b', currentText.Length - commonPrefixLength); + + // Output new suffix + outputBuilder.Append(text.Substring(commonPrefixLength)); + + // If the new text is shorter than the old one: delete overlapping characters + int overlapCount = currentText.Length - text.Length; + if (overlapCount > 0) { + outputBuilder.Append(' ', overlapCount); + outputBuilder.Append('\b', overlapCount); + } + + Console.Write(outputBuilder); + currentText = text; + } + + private void ResetTimer() { + timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1)); + } + + public void Dispose() { + lock (timer) { + disposed = true; + UpdateText(string.Empty); + } + } + } +} \ No newline at end of file diff --git a/LibXboxOne/ThirdParty/TableParserExtensions.cs b/LibXboxOne/ThirdParty/TableParserExtensions.cs new file mode 100644 index 0000000..616a868 --- /dev/null +++ b/LibXboxOne/ThirdParty/TableParserExtensions.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; + +namespace LibXboxOne.ThirdParty +{ + // TableParserExtensions class from https://github.com/Robert-McGinley/TableParser + public static class TableParserExtensions + { + public static string ToStringTable(this IEnumerable values, string[] columnHeaders, params Func[] valueSelectors) + { + return ToStringTable(values.ToArray(), columnHeaders, valueSelectors); + } + + public static string ToStringTable(this T[] values, string[] columnHeaders, params Func[] valueSelectors) + { + Debug.Assert(columnHeaders.Length == valueSelectors.Length); + + var arrValues = new string[values.Length + 1, valueSelectors.Length]; + + // Fill headers + for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++) + { + arrValues[0, colIndex] = columnHeaders[colIndex]; + } + + // Fill table rows + for (int rowIndex = 1; rowIndex < arrValues.GetLength(0); rowIndex++) + { + for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++) + { + object value = valueSelectors[colIndex].Invoke(values[rowIndex - 1]); + + arrValues[rowIndex, colIndex] = value != null ? value.ToString() : "null"; + } + } + + return ToStringTable(arrValues); + } + + public static string ToStringTable(this string[,] arrValues) + { + int[] maxColumnsWidth = GetMaxColumnsWidth(arrValues); + var headerSpliter = new string('-', maxColumnsWidth.Sum(i => i + 3) - 1); + + var sb = new StringBuilder(); + for (int rowIndex = 0; rowIndex < arrValues.GetLength(0); rowIndex++) + { + for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++) + { + // Print cell + string cell = arrValues[rowIndex, colIndex]; + cell = cell.PadRight(maxColumnsWidth[colIndex]); + sb.Append(" | "); + sb.Append(cell); + } + + // Print end of line + sb.Append(" | "); + sb.AppendLine(); + + // Print splitter + if (rowIndex == 0) + { + sb.AppendFormat(" |{0}| ", headerSpliter); + sb.AppendLine(); + } + } + + return sb.ToString(); + } + + private static int[] GetMaxColumnsWidth(string[,] arrValues) + { + var maxColumnsWidth = new int[arrValues.GetLength(1)]; + for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++) + { + for (int rowIndex = 0; rowIndex < arrValues.GetLength(0); rowIndex++) + { + int newLength = arrValues[rowIndex, colIndex].Length; + int oldLength = maxColumnsWidth[colIndex]; + + if (newLength > oldLength) + { + maxColumnsWidth[colIndex] = newLength; + } + } + } + + return maxColumnsWidth; + } + + public static string ToStringTable(this IEnumerable values, params Expression>[] valueSelectors) + { + var headers = valueSelectors.Select(func => GetProperty(func).Name).ToArray(); + var selectors = valueSelectors.Select(exp => exp.Compile()).ToArray(); + return ToStringTable(values, headers, selectors); + } + + private static PropertyInfo GetProperty(Expression> expresstion) + { + switch (expresstion.Body) + { + case UnaryExpression expression when expression.Operand is MemberExpression memberExpression: + return memberExpression.Member as PropertyInfo; + case MemberExpression expression1: + return expression1.Member as PropertyInfo; + default: + return null; + } + } + } +} diff --git a/LibXboxOne/XVD/XVDEnums.cs b/LibXboxOne/XVD/XVDEnums.cs new file mode 100644 index 0000000..8810b71 --- /dev/null +++ b/LibXboxOne/XVD/XVDEnums.cs @@ -0,0 +1,111 @@ +using System; + +namespace LibXboxOne +{ + public enum XvdType : uint + { + Fixed = 0, + Dynamic = 1 + } + + public enum XvdContentType : uint + { + Data = 0, + Title = 1, + SystemOS = 2, + EraOS = 3, + Scratch = 4, + ResetData = 5, + Application = 6, + HostOS = 7, + X360STFS = 8, + X360FATX = 9, + X360GDFX = 0xA, + Updater = 0xB, + OfflineUpdater = 0xC, + Template = 0xD, + MteHost = 0xE, + MteApp = 0xF, + MteTitle = 0x10, + MteEraOS = 0x11, + EraTools = 0x12, + SystemTools = 0x13, + SystemAux = 0x14, + AcousticModel = 0x15, + SystemCodecsVolume = 0x16, + QasltPackage = 0x17, + AppDlc = 0x18, + TitleDlc = 0x19, + UniversalDlc = 0x1A, + SystemDataVolume = 0x1B, + TestVolume = 0x1C, + HardwareTestVolume = 0x1D, + KioskContent = 0x1E, + HostProfiler = 0x20, + Uwa = 0x21, + Unknown22 = 0x22, + Unknown23 = 0x23, + Unknown24 = 0x24, + ServerAgent = 0x25 + } + + [Flags] + public enum XvcRegionFlags : uint + { + Resident = 1, + InitialPlay = 2, + Preview = 4, + FileSystemMetadata = 8, + Present = 0x10, + OnDemand = 0x20, + Available = 0x40, + } + + [Flags] + public enum XvdVolumeFlags : uint + { + ReadOnly = 1, + EncryptionDisabled = 2, // data decrypted, no encrypted CIKs + DataIntegrityDisabled = 4, // unsigned and unhashed + LegacySectorSize = 8, + ResiliencyEnabled = 0x10, + SraReadOnly = 0x20, + RegionIdInXts = 0x40, + EraSpecific = 0x80 + } + + [Flags] + public enum XvcRegionPresenceInfo : byte + { + IsPresent = 1, // not set = "not present" + IsAvailable = 2, // not set = "unavailable" + + //value >> 4 = discnum + Disc1 = 0x10, + Disc2 = 0x20, + Disc3 = 0x30, + Disc4 = 0x40, + Disc5 = 0x50, + Disc6 = 0x60, + Disc7 = 0x70, + Disc8 = 0x80, + Disc9 = 0x90, + Disc10 = 0xA0, + Disc11 = 0xB0, + Disc12 = 0xC0, + Disc13 = 0xD0, + Disc14 = 0xE0, + Disc15 = 0xF0 + } + + public enum XvdUserDataType : uint + { + PackageFiles = 0 + } + + [Flags] + public enum XvdSegmentMetadataSegmentFlags : ushort + { + KeepEncryptedOnDisk = 1, + } +} diff --git a/LibXboxOne/XVD/XVDFile.cs b/LibXboxOne/XVD/XVDFile.cs new file mode 100644 index 0000000..90b9d25 --- /dev/null +++ b/LibXboxOne/XVD/XVDFile.cs @@ -0,0 +1,1254 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Xml; +using LibXboxOne.Keys; +using LibXboxOne.ThirdParty; + +namespace LibXboxOne +{ + public class XvdFile : IDisposable + { + #region Constants + + public static readonly ulong HEADER_SIGNATURE_SIZE = 0x200; + public static readonly ulong XVD_HEADER_SIZE = 0x2E00; + public static readonly ulong XVD_HEADER_INCL_SIGNATURE_SIZE = HEADER_SIGNATURE_SIZE + XVD_HEADER_SIZE; + + public static readonly uint PAGE_SIZE = 0x1000; + public static readonly uint BLOCK_SIZE = 0xAA000; + public static readonly uint SECTOR_SIZE = 4096; + public static readonly uint LEGACY_SECTOR_SIZE = 512; + public static readonly uint INVALID_SECTOR = 0xFFFFFFFF; + + public static readonly uint HASH_ENTRY_LENGTH = 0x18; + public static readonly uint HASH_ENTRY_LENGTH_ENCRYPTED = 0x14; + + public static readonly uint HASH_ENTRIES_IN_PAGE = PAGE_SIZE / HASH_ENTRY_LENGTH; // 0xAA + public static readonly uint PAGES_PER_BLOCK = BLOCK_SIZE / PAGE_SIZE; // 0xAA + + public static readonly uint DATA_BLOCKS_IN_LEVEL0_HASHTREE = HASH_ENTRIES_IN_PAGE; // 0xAA + public static readonly uint DATA_BLOCKS_IN_LEVEL1_HASHTREE = HASH_ENTRIES_IN_PAGE * DATA_BLOCKS_IN_LEVEL0_HASHTREE; // 0x70E4 + public static readonly uint DATA_BLOCKS_IN_LEVEL2_HASHTREE = HASH_ENTRIES_IN_PAGE * DATA_BLOCKS_IN_LEVEL1_HASHTREE; // 0x4AF768 + public static readonly uint DATA_BLOCKS_IN_LEVEL3_HASHTREE = HASH_ENTRIES_IN_PAGE * DATA_BLOCKS_IN_LEVEL2_HASHTREE; // 0x31C84B10 + #endregion + + public static bool DisableDataHashChecking = false; + public static bool PrintProgressToConsole = false; + public static bool DisableSaveAfterModification = false; + + public XvdHeader Header; + public XvcInfo XvcInfo; + + public List RegionHeaders; + public List UpdateSegments; + public List RegionSpecifiers; + public List RegionPresenceInfo; + + public bool HashTreeValid; + public bool DataHashTreeValid; + public bool XvcDataHashValid; + + public bool CikIsDecrypted; + + public static XvdContentType[] XvcContentTypes = + { // taken from bit test 0x07018042 (00000111000000011000000001000010) + // idx of each set bit (from right to left) is an XVC-enabled content type + XvdContentType.Title, + XvdContentType.Application, + XvdContentType.MteApp, + XvdContentType.MteTitle, + XvdContentType.AppDlc, + XvdContentType.TitleDlc, + XvdContentType.UniversalDlc + }; + + public OdkIndex OverrideOdk { get; set; } + public XvdFilesystem Filesystem { get; private set; } + + private readonly IO _io; + + public readonly string FilePath; + + public long FileSize => _io.Stream.Length; + + public DateTime TimeCreated => DateTime.FromFileTime(Header.FileTimeCreated); + + public bool IsXvcFile => XvcContentTypes.Contains(Header.ContentType); + + public bool IsEncrypted => !Header.VolumeFlags.HasFlag(XvdVolumeFlags.EncryptionDisabled); + + public bool IsDataIntegrityEnabled => !Header.VolumeFlags.HasFlag(XvdVolumeFlags.DataIntegrityDisabled); + + public bool IsResiliencyEnabled => Header.VolumeFlags.HasFlag(XvdVolumeFlags.ResiliencyEnabled); + + public bool UsesLegacySectorSize => Header.VolumeFlags.HasFlag(XvdVolumeFlags.LegacySectorSize); + + public ulong EmbeddedXvdOffset => XVD_HEADER_INCL_SIGNATURE_SIZE; + + public ulong MduOffset => XvdMath.PageNumberToOffset(Header.EmbeddedXvdPageCount) + + EmbeddedXvdOffset; + + public ulong HashTreeOffset => Header.MutableDataLength + MduOffset; + + public ulong HashTreePageCount => + XvdMath.CalculateNumberHashPages(out _, + Header.NumberOfHashedPages, + IsResiliencyEnabled); + + public ulong HashTreeLevels { + get + { + XvdMath.CalculateNumberHashPages(out ulong tmpHashTreeLevels, + Header.NumberOfHashedPages, + IsResiliencyEnabled); + return tmpHashTreeLevels; + } + } + + public ulong UserDataOffset => + (IsDataIntegrityEnabled ? XvdMath.PageNumberToOffset(HashTreePageCount) : 0) + + HashTreeOffset; + + public ulong XvcInfoOffset => + XvdMath.PageNumberToOffset(Header.UserDataPageCount) + + UserDataOffset; + + public ulong DynamicHeaderOffset => + XvdMath.PageNumberToOffset(Header.XvcInfoPageCount) + + XvcInfoOffset; + + public ulong DriveDataOffset => + XvdMath.PageNumberToOffset(Header.DynamicHeaderPageCount) + + DynamicHeaderOffset; + + public uint DataHashEntryLength => IsEncrypted ? HASH_ENTRY_LENGTH_ENCRYPTED : HASH_ENTRY_LENGTH; + + // Used to calculate absolute offset for dynamic data + public ulong DynamicBaseOffset => XvcInfoOffset; + + public ulong StaticDataLength { get; private set; } + + public XvdFile(string path) + { + FilePath = path; + _io = new IO(path); + OverrideOdk = OdkIndex.Invalid; + } + + public uint ReadBat(ulong requestedBlock) + { + ulong absoluteAddress = DynamicHeaderOffset + (requestedBlock * sizeof(uint)); + if (absoluteAddress > (DynamicHeaderOffset + Header.DynamicHeaderLength - sizeof(uint))) + { + throw new InvalidDataException( + $"Out-of-bounds block 0x{requestedBlock:X} requested, addr: 0x{absoluteAddress:X}. " + + $"Dynamic header range: 0x{DynamicHeaderOffset:X}-0x{DynamicHeaderOffset+Header.DynamicHeaderLength:X}"); + } + + _io.Stream.Position = (long)absoluteAddress; + return _io.Reader.ReadUInt32(); + } + + public uint ReadUInt32(long offset) + { + _io.Stream.Seek(offset, SeekOrigin.Begin); + return _io.Reader.ReadUInt32(); + } + + public byte[] ReadBytes(long offset, int length) + { + _io.Stream.Seek(offset, SeekOrigin.Begin); + return _io.Reader.ReadBytes(length); + } + + public int WriteBytes(long offset, byte[] data) + { + _io.Stream.Seek(offset, SeekOrigin.Begin); + _io.Writer.Write(data); + return data.Length; + } + + public byte[] ReadPage(ulong pageNumber) + { + return ReadBytes((long)XvdMath.PageNumberToOffset(pageNumber), (int)PAGE_SIZE); + } + + public int WritePage(ulong pageNumber, byte[] data) + { + return WriteBytes((long)XvdMath.PageNumberToOffset(pageNumber), data); + } + + public bool VirtualToLogicalDriveOffset(ulong virtualOffset, out ulong logicalOffset) + { + logicalOffset = 0; + + if (virtualOffset >= Header.DriveSize) + throw new InvalidOperationException( + $"Virtual offset 0x{virtualOffset:X} is outside drivedata length 0x{Header.DriveSize:X}"); + if (Header.Type > XvdType.Dynamic) + throw new NotSupportedException($"Xvd type {Header.Type} is unhandled"); + + + if (Header.Type == XvdType.Dynamic) + { + var dataStartOffset = virtualOffset + XvdMath.PageNumberToOffset(Header.NumberOfMetadataPages); + var pageNumber = XvdMath.OffsetToPageNumber(dataStartOffset); + var inBlockOffset = XvdMath.InBlockOffset(dataStartOffset); + var firstDynamicPage = XvdMath.QueryFirstDynamicPage(Header.NumberOfMetadataPages); + + if (pageNumber >= firstDynamicPage) + { + var firstDynamicPageBytes = XvdMath.PageNumberToOffset(firstDynamicPage); + var blockNumber = XvdMath.OffsetToBlockNumber(dataStartOffset - firstDynamicPageBytes); + ulong allocatedBlock = ReadBat(blockNumber); + if (allocatedBlock == INVALID_SECTOR) + return false; + + dataStartOffset = XvdMath.PageNumberToOffset(allocatedBlock) + inBlockOffset; + pageNumber = XvdMath.OffsetToPageNumber(dataStartOffset); + } + + var dataBackingBlockNum = XvdMath.ComputeDataBackingPageNumber(Header.Type, + HashTreeLevels, + HashTreePageCount, + pageNumber); + logicalOffset = XvdMath.PageNumberToOffset(dataBackingBlockNum); + logicalOffset += XvdMath.InPageOffset(dataStartOffset); + logicalOffset += XvdMath.PageNumberToOffset(Header.EmbeddedXvdPageCount); + logicalOffset += Header.MutableDataLength; + logicalOffset += XVD_HEADER_INCL_SIGNATURE_SIZE; + logicalOffset += PAGE_SIZE; + } + else + { // Xvd type fixed + logicalOffset = virtualOffset; + logicalOffset += XvdMath.PageNumberToOffset(Header.EmbeddedXvdPageCount); + logicalOffset += Header.MutableDataLength; + logicalOffset += XvdMath.PageNumberToOffset(Header.NumberOfMetadataPages); + logicalOffset += XVD_HEADER_INCL_SIGNATURE_SIZE; + logicalOffset += PAGE_SIZE; + } + + return true; + } + + uint[] GetAllBATEntries() + { + var batEntryCount = XvdMath.BytesToBlocks(Header.DriveSize); + uint[] BatEntries = new uint[batEntryCount]; + + for (ulong i=0; i < batEntryCount; i++) + BatEntries[i] = ReadBat(i); + + return BatEntries; + } + + ulong CalculateStaticDataLength() + { + switch (Header.Type) + { + case XvdType.Dynamic: + { + var smallestPage = GetAllBATEntries().Min(); + return XvdMath.PageNumberToOffset(smallestPage) + - XvdMath.PageNumberToOffset(Header.DynamicHeaderPageCount) + - XvdMath.PageNumberToOffset(Header.XvcInfoPageCount); + } + case XvdType.Fixed: + return 0; + + default: + throw new InvalidProgramException("Unsupported XvdType"); + } + } + + private void CryptHeaderCik(bool encrypt) + { + if ((!encrypt && CikIsDecrypted) || IsXvcFile) + { + CikIsDecrypted = true; + return; + } + + var odkToUse = OverrideOdk == OdkIndex.Invalid ? Header.ODKKeyslotID : OverrideOdk; + + var odkKey = DurangoKeys.GetOdkById(odkToUse); + if (odkKey == null) + throw new InvalidOperationException( + $"ODK with Id \'{Header.ODKKeyslotID}\' not found! Cannot crypt CIK in header"); + + if (!odkKey.HasKeyData) + throw new InvalidOperationException( + $"ODK with Id \'{Header.ODKKeyslotID}\' is known but not loaded! Cannot crypt CIK in header"); + + byte[] nullIv = new byte[16]; + + var cipher = Aes.Create(); + cipher.Mode = CipherMode.ECB; + cipher.Padding = PaddingMode.None; + + var transform = encrypt ? cipher.CreateEncryptor(odkKey.KeyData, nullIv) : + cipher.CreateDecryptor(odkKey.KeyData, nullIv); + + transform.TransformBlock(Header.KeyMaterial, 0, Header.KeyMaterial.Length, Header.KeyMaterial, 0); + + CikIsDecrypted = !encrypt; + } + + private bool CryptXvcRegion(int regionIdx, bool encrypt) + { + if (XvcInfo.ContentID == null || !XvcInfo.IsAnyKeySet || regionIdx > XvcInfo.RegionCount || RegionHeaders == null || regionIdx >= RegionHeaders.Count) + return false; + + XvcRegionHeader header = RegionHeaders[regionIdx]; + if (encrypt && header.KeyId == XvcConstants.XVC_KEY_NONE) + header.KeyId = 0; + + if (header.Length <= 0 || header.Offset <= 0 || header.KeyId == XvcConstants.XVC_KEY_NONE || header.KeyId + 1 > XvcInfo.KeyCount) + return false; + + GetXvcKey(header.KeyId, out var key); + + if (key == null) + return false; + + return CryptSectionXts(encrypt, key, (uint)header.Id, header.Offset, header.Length); + } + + internal bool CryptSectionXts(bool encrypt, byte[] key, uint headerId, ulong offset, ulong length) + { + var startPage = XvdMath.OffsetToPageNumber(offset - UserDataOffset); + ulong numPages = XvdMath.BytesToPages(length); + + // Pre-read data unit numbers to minimize needing to seek around the file + List dataUnits = null; + if (IsDataIntegrityEnabled) + { + dataUnits = new List(); + for (uint page = 0; page < numPages; page++) + { + // fetch dataUnit from hash table entry for this page + // TODO: seems we'll have to insert dataUnit when re-adding hashtables... + + // last 4 bytes of hash entry = dataUnit + _io.Stream.Position = (long)CalculateHashEntryOffsetForBlock(startPage + page, 0) + 0x14; + dataUnits.Add(_io.Reader.ReadUInt32()); + } + } + + var tweakAesKey = new byte[0x10]; + var dataAesKey = new byte[0x10]; + + var tweak = new byte[0x10]; + + // Split tweak- / Data AES key + Array.Copy(key, tweakAesKey, 0x10); + Array.Copy(key, 0x10, dataAesKey, 0, 0x10); + + // Copy VDUID and header Id as tweak + var headerIdBytes = BitConverter.GetBytes(headerId); + Array.Copy(Header.VDUID, 0, tweak, 0x8, 0x8); + Array.Copy(headerIdBytes, 0, tweak, 0x4, 0x4); + + var cipher = new AesXtsTransform(tweak, dataAesKey, tweakAesKey, encrypt); + + // Perform crypto! + if (PrintProgressToConsole) + Console.WriteLine($"\r\nCrypting section 0x{offset:X} - 0x{offset + length:X} (HeaderId: 0x{headerId:X}):"); + + _io.Stream.Position = (long)offset; + + using (var progress = new ProgressBar((long)numPages, "pages")) + { + for (uint page = 0; page < numPages; page++) + { + if (PrintProgressToConsole) + progress.Report((long)page); + + var transformedData = new byte[PAGE_SIZE]; + + var pageOffset = _io.Stream.Position; + var origData = _io.Reader.ReadBytes((int)PAGE_SIZE); + + cipher.TransformDataUnit(origData, 0, origData.Length, transformedData, 0, dataUnits?[(int)page] ?? page); + + _io.Stream.Position = pageOffset; + _io.Writer.Write(transformedData); + } + } + + return true; + } + + public byte[] ExtractEmbeddedXvd() + { + if (Header.EmbeddedXVDLength == 0) + return null; + _io.Stream.Position = (long)EmbeddedXvdOffset; + return _io.Reader.ReadBytes((int)Header.EmbeddedXVDLength); + } + + public byte[] ExtractUserData() + { + if (Header.UserDataLength == 0) + return null; + _io.Stream.Position = (long)UserDataOffset; + return _io.Reader.ReadBytes((int)Header.UserDataLength); + } + + public bool Decrypt() + { + if (!IsEncrypted) + return true; + + CryptHeaderCik(false); + if (!CikIsDecrypted) + return false; + + bool success; + + if (IsXvcFile) + { + for (int i = 0; i < RegionHeaders.Count; i++) + { + XvcRegionHeader header = RegionHeaders[i]; + if (header.Length <= 0 || header.Offset <= 0 || header.KeyId == XvcConstants.XVC_KEY_NONE || header.KeyId + 1 > XvcInfo.KeyCount) + continue; + if (!CryptXvcRegion(i, false)) + return false; + } + success = true; + } + else + { + // todo: check with more non-xvc xvds and see if they use any other headerId besides 0x1 + success = CryptSectionXts(false, Header.KeyMaterial, 0x1, UserDataOffset, + (ulong)_io.Stream.Length - UserDataOffset); + } + + if (!success) + return false; + + Header.VolumeFlags ^= XvdVolumeFlags.EncryptionDisabled; + + if (!DisableSaveAfterModification) + return Save(); + + return true; + } + + public bool Encrypt(Guid cikKeyId) + { + if (IsEncrypted) + return true; + + bool success; + + if (!IsXvcFile) + { + if (Header.KeyMaterial.IsArrayEmpty()) + { + // generate a new CIK if there's none specified + var rng = new Random(); + Header.KeyMaterial = new byte[0x20]; + rng.NextBytes(Header.KeyMaterial); + } + + // todo: check with more non-xvc xvds and see if they use any other headerId besides 0x1 + success = CryptSectionXts(true, Header.KeyMaterial, 0x1, UserDataOffset, + (ulong)_io.Stream.Length - UserDataOffset); + } + else + { + if (cikKeyId != Guid.Empty) // if cikKeyId is set, set the XvcInfo key accordingly + { + var key = DurangoKeys.GetCikByGuid(cikKeyId); + if (key == null) + { + throw new InvalidOperationException($"Desired CIK with GUID {cikKeyId} is unknown"); + } + + if (!key.HasKeyData) + { + throw new InvalidOperationException($"Desired CIK with GUID {cikKeyId} is known but not loaded"); + } + + XvcInfo.EncryptionKeyIds[0].KeyId = cikKeyId.ToByteArray(); + } + + for (int i = 0; i < RegionHeaders.Count; i++) + { + var header = RegionHeaders[i]; + if (header.Length <= 0 || header.Offset <= 0 || header.KeyId + 1 > XvcInfo.KeyCount && header.KeyId != XvcConstants.XVC_KEY_NONE) + continue; + + if (header.Id == XvcRegionId.Header || + header.Id == XvcRegionId.EmbeddedXvd || + header.Id == XvcRegionId.MetadataXvc) + continue; // skip XVD header / EXVD / XVC info + + if (!CryptXvcRegion(i, true)) + return false; + } + success = true; + } + + if (!success) + return false; + + CryptHeaderCik(true); + + Header.VolumeFlags ^= XvdVolumeFlags.EncryptionDisabled; + + // seems the readonly flag gets set when encrypting + if (!Header.VolumeFlags.HasFlag(XvdVolumeFlags.ReadOnly)) + { + Header.VolumeFlags ^= XvdVolumeFlags.ReadOnly; + } + + if (!DisableSaveAfterModification) + return Save(); + + return true; + } + + public bool Save() + { + if (Header.XvcDataLength > 0 && IsXvcFile) + { + _io.Stream.Position = (long)XvcInfoOffset; + + XvcInfo.RegionCount = (uint)RegionHeaders.Count; + XvcInfo.UpdateSegmentCount = (uint)UpdateSegments.Count; + if (RegionSpecifiers != null) + XvcInfo.RegionSpecifierCount = (uint)RegionSpecifiers.Count; + + _io.Writer.WriteStruct(XvcInfo); + + for (int i = 0; i < XvcInfo.RegionCount; i++) + _io.Writer.WriteStruct(RegionHeaders[i]); + + for (int i = 0; i < XvcInfo.UpdateSegmentCount; i++) + _io.Writer.WriteStruct(UpdateSegments[i]); + + if (RegionSpecifiers != null) + for (int i = 0; i < XvcInfo.RegionSpecifierCount; i++) + _io.Writer.WriteStruct(RegionSpecifiers[i]); + } + + if (IsDataIntegrityEnabled) + { + if (PrintProgressToConsole) + Console.WriteLine("Calculating data hashes:"); + +// ReSharper disable once UnusedVariable + ulong[] invalidBlocks = VerifyDataHashTree(true); +// ReSharper disable once UnusedVariable + bool hashTreeValid = CalculateHashTree(); + } + + _io.Stream.Position = 0; + _io.Writer.WriteStruct(Header); + + return true; + } + + public bool Load() + { + _io.Stream.Position = 0; + Header = _io.Reader.ReadStruct(); + + CikIsDecrypted = !IsEncrypted; + + if (DriveDataOffset >= (ulong)_io.Stream.Length) + return false; + + if (Header.XvcDataLength > 0 && IsXvcFile) + { + _io.Stream.Position = (long)XvcInfoOffset; + + XvcInfo = _io.Reader.ReadStruct(); + + if (XvcInfo.Version >= 1) + { + RegionHeaders = new List(); + for (int i = 0; i < XvcInfo.RegionCount; i++) + RegionHeaders.Add(_io.Reader.ReadStruct()); + + UpdateSegments = new List(); + for (int i = 0; i < XvcInfo.UpdateSegmentCount; i++) + UpdateSegments.Add(_io.Reader.ReadStruct()); + + if(XvcInfo.Version >= 2) // RegionSpecifiers / RegionPresenseInfo only seems to be used on XvcInfo v2 + { + RegionSpecifiers = new List(); + for (int i = 0; i < XvcInfo.RegionSpecifierCount; i++) + RegionSpecifiers.Add(_io.Reader.ReadStruct()); + + if(Header.MutableDataPageCount > 0) + { + RegionPresenceInfo = new List(); + _io.Stream.Position = (long)MduOffset; + for (int i = 0; i < XvcInfo.RegionCount; i++) + RegionPresenceInfo.Add((XvcRegionPresenceInfo)_io.Reader.ReadByte()); + } + } + } + } + + DataHashTreeValid = true; + HashTreeValid = true; + + if (IsDataIntegrityEnabled) + { + if (!DisableDataHashChecking) + { + if(PrintProgressToConsole) + Console.WriteLine("Verifying data hashes, use -nd to disable:"); + + ulong[] invalidBlocks = VerifyDataHashTree(); + DataHashTreeValid = invalidBlocks.Length <= 0; + } + HashTreeValid = VerifyHashTree(); + } + + XvcDataHashValid = VerifyXvcHash(); + StaticDataLength = CalculateStaticDataLength(); + Filesystem = new XvdFilesystem(this); + return true; + } + + public bool VerifyXvcHash(bool rehash = false) + { + if (!IsXvcFile) + return true; + + ulong hashTreeSize = HashTreePageCount * PAGE_SIZE; + + var ms = new MemoryStream(); + var msIo = new IO(ms); + msIo.Writer.WriteStruct(XvcInfo); + + // fix region headers to match pre-hashtable + for (int i = 0; i < XvcInfo.RegionCount; i++) + { + var region = RegionHeaders[i]; + region.Hash = 0; + + if (IsDataIntegrityEnabled) + { + if (HashTreeOffset >= region.Offset && region.Offset + region.Length > HashTreeOffset) + region.Length -= hashTreeSize; + else if (region.Offset > HashTreeOffset) + region.Offset -= hashTreeSize; + } + + msIo.Writer.WriteStruct(region); + } + + for (int i = 0; i < XvcInfo.UpdateSegmentCount; i++) + { + var segment = UpdateSegments[i]; + + var hashTreeEnd = XvdMath.BytesToPages(HashTreeOffset) + HashTreePageCount; + if (segment.PageNum >= hashTreeEnd) + segment.PageNum -= (uint)HashTreePageCount; + + segment.Hash = 0; + + msIo.Writer.WriteStruct(segment); + } + + if (RegionSpecifiers != null) + for (int i = 0; i < XvcInfo.RegionSpecifierCount; i++) + msIo.Writer.WriteStruct(RegionSpecifiers[i]); + + if(Header.XvcDataLength > msIo.Stream.Length) + msIo.Stream.SetLength(Header.XvcDataLength); + + if (IsDataIntegrityEnabled) + { + // remove hash table offset from the special regions + if (XvcInfo.InitialPlayOffset > HashTreeOffset) + { + msIo.Stream.Position = 0xD28; + msIo.Writer.Write(XvcInfo.InitialPlayOffset - hashTreeSize); + } + + if (XvcInfo.PreviewOffset > HashTreeOffset) + { + msIo.Stream.Position = 0xD40; + msIo.Writer.Write(XvcInfo.PreviewOffset - hashTreeSize); + } + } + + byte[] xvcData = ms.ToArray(); + msIo.Dispose(); + byte[] hash = HashUtils.ComputeSha256(xvcData); + bool isValid = Header.OriginalXvcDataHash.IsEqualTo(hash); + + if (rehash) + Header.OriginalXvcDataHash = hash; + + return isValid; //todo: investigate why this gets the correct hash for dev XVCs but fails for retail ones, might be to do with retail XVC data having a content ID that doesn't match with VDUID/UDUID + } + + public bool AddHashTree() + { + if (IsDataIntegrityEnabled) + return true; + + if (!AddData(HashTreeOffset, (uint)HashTreePageCount)) + return false; + + Header.VolumeFlags ^= XvdVolumeFlags.DataIntegrityDisabled; + + // todo: calculate hash tree + + if (!DisableSaveAfterModification) + return Save(); + + return true; + } + + internal bool AddData(ulong offset, ulong numPages) + { + var page = XvdMath.OffsetToPageNumber(offset); + var length = numPages * PAGE_SIZE; + + _io.Stream.Position = (long)offset; + if (!_io.AddBytes((long)length)) + return false; + + if (!IsXvcFile) + return true; + + if (XvcInfo.InitialPlayOffset > offset) + XvcInfo.InitialPlayOffset += length; + + if (XvcInfo.PreviewOffset > offset) + XvcInfo.PreviewOffset += length; + + for (int i = 0; i < RegionHeaders.Count; i++) + { + var region = RegionHeaders[i]; + region.Hash = 0; // ??? + + if (offset >= region.Offset && region.Offset + region.Length > offset) + region.Length += length; // offset is part of region, add to length + else if (region.Offset > offset) + region.Offset += length; // offset is before region, add to offset + + RegionHeaders[i] = region; + } + + for (int i = 0; i < UpdateSegments.Count; i++) + { + var segment = UpdateSegments[i]; + if (segment.PageNum < page) + continue; + + segment.PageNum += (uint)numPages; + UpdateSegments[i] = segment; + } + + return true; + } + + internal bool RemoveData(ulong offset, ulong numPages) + { + var page = XvdMath.OffsetToPageNumber(offset); + var length = numPages * PAGE_SIZE; + + _io.Stream.Position = (long)offset; + if (!_io.DeleteBytes((long)length)) + return false; + + if (!IsXvcFile) + return true; + + if (XvcInfo.InitialPlayOffset > offset) + XvcInfo.InitialPlayOffset -= length; + + if (XvcInfo.PreviewOffset > offset) + XvcInfo.PreviewOffset -= length; + + for (int i = 0; i < RegionHeaders.Count; i++) + { + var region = RegionHeaders[i]; + region.Hash = 0; // ??? + + if (offset >= region.Offset && region.Offset + region.Length > offset) + region.Length -= length; // offset is part of region, reduce length + else if (region.Offset > offset) + region.Offset -= length; // offset is before region, reduce offset + + RegionHeaders[i] = region; // region is a copy instead of a reference due to it being a struct, so we have to replace the original data ourselves + } + + for(int i = 0; i < UpdateSegments.Count; i++) + { + var segment = UpdateSegments[i]; + if (segment.PageNum < page) + continue; + + segment.PageNum -= (uint)numPages; + UpdateSegments[i] = segment; + } + + return true; + } + + public bool AddMutableData() + { + if (!IsXvcFile || XvcInfo.ContentID == null || RegionHeaders == null) + { + Console.WriteLine("Package is not XVC or has no Xvc data"); + return false; + } + + ulong pageCount = XvdMath.BytesToPages((ulong)RegionHeaders.Count); + byte[] mutableData = new byte[XvdMath.PageNumberToOffset(pageCount)]; + + if (Header.MutableDataPageCount > 0) + return true; + + if (!AddData(MduOffset, pageCount)) + return false; + + for (int i=0; i < RegionHeaders.Count; i++) + { + XvcRegionHeader region = RegionHeaders[i]; + bool isPresent = (long)(region.Offset + region.Length) <= FileSize; + // Assume that every Xvc region is available (probably means downloadable) + mutableData[i] = (byte)(XvcRegionPresenceInfo.IsAvailable + | (isPresent ? XvcRegionPresenceInfo.IsPresent : 0)); + } + + + WriteBytes((long)MduOffset, mutableData); + Header.MutableDataPageCount = (byte)pageCount; + + if (!DisableSaveAfterModification) + return Save(); + + return true; + } + + public bool RemoveMutableData() + { + if (Header.MutableDataPageCount <= 0) + return true; + + if (!RemoveData(MduOffset, Header.MutableDataPageCount)) + return false; + + Header.MutableDataPageCount = 0; + + if (!DisableSaveAfterModification) + return Save(); + + return true; + } + + public bool RemoveHashTree() + { + if (!IsDataIntegrityEnabled) + return true; + + if (!RemoveData(HashTreeOffset, HashTreePageCount)) + return false; + + Header.VolumeFlags ^= XvdVolumeFlags.DataIntegrityDisabled; + + for (int i = 0; i < Header.TopHashBlockHash.Length; i++) + Header.TopHashBlockHash[i] = 0; + + if (!DisableSaveAfterModification) + return Save(); + + return true; + } + + public ulong CalculateHashEntryOffsetForBlock(ulong blockNum, uint hashLevel) + { + var hashBlock = XvdMath.CalculateHashBlockNumForBlockNum(Header.Type, HashTreeLevels, Header.NumberOfHashedPages, blockNum, hashLevel, out var entryNum); + return HashTreeOffset + XvdMath.PageNumberToOffset(hashBlock) + (entryNum * HASH_ENTRY_LENGTH); + } + + public ulong[] VerifyDataHashTree(bool rehash = false) + { + ulong dataBlockCount = XvdMath.OffsetToPageNumber((ulong)_io.Stream.Length - UserDataOffset); + var invalidBlocks = new List(); + + using (var progress = new ProgressBar((long)dataBlockCount, "blocks")) + { + for (ulong i = 0; i < dataBlockCount; i++) + { + if (PrintProgressToConsole) + progress.Report((long)i); + + var hashEntryOffset = CalculateHashEntryOffsetForBlock(i, 0); + _io.Stream.Position = (long)hashEntryOffset; + + byte[] oldhash = _io.Reader.ReadBytes((int)DataHashEntryLength); + + var dataToHashOffset = XvdMath.PageNumberToOffset(i) + UserDataOffset; + _io.Stream.Position = (long)dataToHashOffset; + + byte[] data = _io.Reader.ReadBytes((int)PAGE_SIZE); + byte[] hash = HashUtils.ComputeSha256(data); + Array.Resize(ref hash, (int)DataHashEntryLength); + + if (hash.IsEqualTo(oldhash)) + continue; + + invalidBlocks.Add(i); + if (!rehash) + continue; + _io.Stream.Position = (long)hashEntryOffset; + _io.Writer.Write(hash); + } + } + + return invalidBlocks.ToArray(); + } + + public bool CalculateHashTree() + { + uint blocksPerLevel = 0xAA; + uint hashTreeLevel = 1; + while (hashTreeLevel < HashTreeLevels) + { + uint dataBlockNum = 0; + if (Header.NumberOfHashedPages != 0) + { + while (dataBlockNum < Header.NumberOfHashedPages) + { + _io.Stream.Position = (long)CalculateHashEntryOffsetForBlock(dataBlockNum, hashTreeLevel - 1); + byte[] blockHash = HashUtils.ComputeSha256(_io.Reader.ReadBytes((int)PAGE_SIZE)); + Array.Resize(ref blockHash, (int)HASH_ENTRY_LENGTH); + + _io.Stream.Position = (long)CalculateHashEntryOffsetForBlock(dataBlockNum, hashTreeLevel); + + byte[] oldHash = _io.Reader.ReadBytes((int)HASH_ENTRY_LENGTH); + if (!blockHash.IsEqualTo(oldHash)) + { + _io.Stream.Position -= (int)HASH_ENTRY_LENGTH; // todo: maybe return a list of blocks that needed rehashing + _io.Writer.Write(blockHash); + } + + dataBlockNum += blocksPerLevel; + } + } + hashTreeLevel++; + blocksPerLevel = blocksPerLevel * 0xAA; + } + _io.Stream.Position = (long)HashTreeOffset; + byte[] hash = HashUtils.ComputeSha256(_io.Reader.ReadBytes((int)PAGE_SIZE)); + Header.TopHashBlockHash = hash; + + return true; + } + + public bool VerifyHashTree() + { + if (!IsDataIntegrityEnabled) + return true; + + _io.Stream.Position = (long)HashTreeOffset; + byte[] hash = HashUtils.ComputeSha256(_io.Reader.ReadBytes((int)PAGE_SIZE)); + if (!Header.TopHashBlockHash.IsEqualTo(hash)) + return false; + + if (HashTreeLevels == 1) + return true; + + var blocksPerLevel = 0xAA; + ulong topHashTreeBlock = 0; + uint hashTreeLevel = 1; + while (hashTreeLevel < HashTreeLevels) + { + uint dataBlockNum = 0; + if (Header.NumberOfHashedPages != 0) + { + while (dataBlockNum < Header.NumberOfHashedPages) + { + _io.Stream.Position = (long)CalculateHashEntryOffsetForBlock(dataBlockNum, hashTreeLevel - 1); + byte[] blockHash = HashUtils.ComputeSha256(_io.Reader.ReadBytes((int)PAGE_SIZE)); + Array.Resize(ref blockHash, (int)HASH_ENTRY_LENGTH); + + var upperHashBlockOffset = CalculateHashEntryOffsetForBlock(dataBlockNum, hashTreeLevel); + topHashTreeBlock = XvdMath.OffsetToPageNumber(upperHashBlockOffset - HashTreeOffset); + _io.Stream.Position = (long)upperHashBlockOffset; + + byte[] expectedHash = _io.Reader.ReadBytes((int)HASH_ENTRY_LENGTH); + if (!expectedHash.IsEqualTo(blockHash)) + { + // wrong hash + return false; + } + dataBlockNum += (uint)blocksPerLevel; + } + } + hashTreeLevel++; + blocksPerLevel = blocksPerLevel * 0xAA; + } + if (topHashTreeBlock != 0) + { + Console.WriteLine(@"Top level hash page calculated to be at {0}, should be 0!", topHashTreeBlock); + } + return true; + } + + public bool GetXvcKey(ushort keyIndex, out byte[] keyOutput) + { + keyOutput = null; + if (XvcInfo.EncryptionKeyIds == null || XvcInfo.EncryptionKeyIds.Length < 1 || XvcInfo.KeyCount == 0) + return false; + + XvcEncryptionKeyId xvcKeyEntry = XvcInfo.EncryptionKeyIds[keyIndex]; + if (xvcKeyEntry.IsKeyNulled) + return false; + + return GetXvcKeyByGuid(new Guid(xvcKeyEntry.KeyId), out keyOutput); + } + + public bool GetXvcKeyByGuid(Guid keyGuid, out byte[] keyOutput) + { + keyOutput = null; + + if (XvcInfo.EncryptionKeyIds == null || XvcInfo.EncryptionKeyIds.Length < 1 || XvcInfo.KeyCount == 0) + return false; + + bool keyFound = false; + foreach(var xvcKey in XvcInfo.EncryptionKeyIds) + { + if (new Guid(xvcKey.KeyId) == keyGuid) + { + keyFound = true; + } + } + + if (!keyFound) + { + Console.WriteLine($"Key {keyGuid} is not used by this XVC"); + return false; + } + + if(DurangoKeys.IsCikLoaded(keyGuid)) + { + keyOutput = DurangoKeys.GetCikByGuid(keyGuid).KeyData; + return true; + } + + Console.WriteLine($"Did not find CIK {keyGuid} loaded in Keystorage"); + Console.WriteLine("Checking for XML licenses..."); + + string licenseFolder = Path.GetDirectoryName(FilePath); + if (Path.GetFileName(licenseFolder) == "MSXC") + licenseFolder = Path.GetDirectoryName(licenseFolder); + + if (String.IsNullOrEmpty(licenseFolder)) + return false; + + licenseFolder = Path.Combine(licenseFolder, "Licenses"); + + if (!Directory.Exists(licenseFolder)) + return false; + + foreach (string file in Directory.GetFiles(licenseFolder, "*.xml")) + { + var xml = new XmlDocument(); + xml.Load(file); + + var xmlns = new XmlNamespaceManager(xml.NameTable); + xmlns.AddNamespace("resp", "http://schemas.microsoft.com/xboxlive/security/clas/LicResp/v1"); + + XmlNode licenseNode = xml.SelectSingleNode("//resp:SignedLicense", xmlns); + if (licenseNode == null) + continue; + + string signedLicense = licenseNode.InnerText; + byte[] signedLicenseBytes = Convert.FromBase64String(signedLicense); + + var licenseXml = new XmlDocument(); + licenseXml.LoadXml(Encoding.ASCII.GetString(signedLicenseBytes)); + + var xmlns2 = new XmlNamespaceManager(licenseXml.NameTable); + xmlns2.AddNamespace("resp", "http://schemas.microsoft.com/xboxlive/security/clas/LicResp/v1"); + + XmlNode keyIdNode = licenseXml.SelectSingleNode("//resp:KeyId", xmlns2); + if (keyIdNode == null) + continue; + + if (keyGuid != new Guid(keyIdNode.InnerText)) + continue; + + XmlNode licenseBlockNode = licenseXml.SelectSingleNode("//resp:SPLicenseBlock", xmlns2); + if (licenseBlockNode == null) + continue; + + string licenseBlock = licenseBlockNode.InnerText; + byte[] licenseBlockBytes = Convert.FromBase64String(licenseBlock); + + var block = new XvcLicenseBlock(licenseBlockBytes); + var keyIdBlock = block.GetBlockWithId(XvcLicenseBlockId.KeyId); + if (keyIdBlock == null) + continue; + + if (!(new Guid(keyIdBlock.BlockData) == keyGuid)) + continue; + + var decryptKeyBlock = block.GetBlockWithId(XvcLicenseBlockId.EncryptedCik); + if (decryptKeyBlock == null) + continue; + + keyOutput = decryptKeyBlock.BlockData; + Console.WriteLine($"Xvd CIK key found in {file}"); + // todo: decrypt/deobfuscate the key + + return true; + } + return false; + } + + #region ToString + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + var b = new StringBuilder(); + + string fmt = formatted ? " " : ""; + + b.AppendLine("XvdMiscInfo:"); + b.AppendLineSpace(fmt + $"Page Count: 0x{Header.NumberOfHashedPages:X}"); + b.AppendLineSpace(fmt + $"Embedded XVD Offset: 0x{EmbeddedXvdOffset:X}"); + b.AppendLineSpace(fmt + $"MDU Offset: 0x{MduOffset:X}"); + b.AppendLineSpace(fmt + $"HashTree Offset: 0x{HashTreeOffset:X}"); + b.AppendLineSpace(fmt + $"User Data Offset: 0x{UserDataOffset:X}"); + b.AppendLineSpace(fmt + $"XVC Data Offset: 0x{XvcInfoOffset:X}"); + b.AppendLineSpace(fmt + $"Dynamic Header Offset: 0x{DynamicHeaderOffset:X}"); + b.AppendLineSpace(fmt + $"Drive Data Offset: 0x{DriveDataOffset:X}"); + + if (IsDataIntegrityEnabled) + { + b.AppendLineSpace(fmt + $"Hash Tree Page Count: 0x{HashTreePageCount:X}"); + b.AppendLineSpace(fmt + $"Hash Tree Levels: 0x{HashTreeLevels:X}"); + b.AppendLineSpace(fmt + $"Hash Tree Valid: {HashTreeValid}"); + + if (!DisableDataHashChecking) + b.AppendLineSpace(fmt + $"Data Hash Tree Valid: {DataHashTreeValid}"); + } + + if(IsXvcFile) + b.AppendLineSpace(fmt + $"XVC Data Hash Valid: {XvcDataHashValid}"); + + b.AppendLine(); + b.Append(Header.ToString(formatted)); + + if (IsXvcFile && XvcInfo.ContentID != null) + { + b.AppendLine(); + bool xvcKeyFound = GetXvcKey(0, out var decryptKey); + if (xvcKeyFound) + { + b.AppendLine($"Decrypt key for xvc keyslot 0: {decryptKey.ToHexString()}"); + b.AppendLine("(key is wrong though until the obfuscation/encryption on it is figured out)"); + b.AppendLine(); + } + + b.AppendLine(XvcInfo.ToString(formatted)); + } + + if(RegionHeaders != null) + for (int i = 0; i < RegionHeaders.Count; i++) + { + b.AppendLine(); + string presenceInfo = ""; + if (RegionPresenceInfo != null && RegionPresenceInfo.Count > i) + { + var presenceFlags = RegionPresenceInfo[i]; + presenceInfo = " ("; + presenceInfo += (presenceFlags.HasFlag(XvcRegionPresenceInfo.IsPresent) ? "present" : "not present") + ", "; + presenceInfo += presenceFlags.HasFlag(XvcRegionPresenceInfo.IsAvailable) ? "available" : "unavailable"; + if (((int)presenceFlags & 0xF0) != 0) + { + presenceInfo += $", on disc {(int)presenceFlags >> 4}"; + } + presenceInfo += ")"; + } + b.AppendLine($"Region {i}{presenceInfo}"); + b.Append(RegionHeaders[i].ToString(formatted)); + } + + if (UpdateSegments != null && UpdateSegments.Count > 0) + { + // have to add segments to a seperate List so we can store the index of them... + var segments = new List>(); + for (int i = 0; i < UpdateSegments.Count; i++) + { + if (UpdateSegments[i].Hash == 0) + break; + segments.Add(Tuple.Create(i, UpdateSegments[i])); + } + + b.AppendLine(); + b.AppendLine("Update Segments:"); + b.AppendLine(); + b.AppendLine(segments.ToStringTable( + new[] { "Id", "PageNum (Offset)", "Hash" }, + a => a.Item1, a => $"0x{a.Item2.PageNum:X} (0x{XvdMath.PageNumberToOffset(a.Item2.PageNum):X})", a => $"0x{a.Item2.Hash:X}")); + } + + + if (RegionSpecifiers != null && RegionSpecifiers.Count > 0) + { + // have to add specifiers to a seperate List so we can store the index of them... + var specs = new List>(); + for (int i = 0; i < RegionSpecifiers.Count; i++) + { + specs.Add(Tuple.Create(i, RegionSpecifiers[i])); + } + + b.AppendLine(); + b.AppendLine("Region Specifiers:"); + b.AppendLine(); + b.AppendLine(specs.ToStringTable( + new[] { "Id", "RegionId", "Key", "Value" }, + a => a.Item1, a => $"0x{a.Item2.RegionId:X}", a => a.Item2.Key, a => a.Item2.Value)); + } + + if (!IsEncrypted) + { + b.AppendLine(); + try + { + b.Append(Filesystem.ToString(formatted)); + } + catch (Exception e) + { + b.AppendLine($"Failed to get XvdFilesystem info, error: {e}"); + } + } + else + b.AppendLine($"Cannot get XvdFilesystem from encrypted package"); + + return b.ToString(); + } + #endregion + + public void Dispose() + { + _io.Dispose(); + } + } +} diff --git a/LibXboxOne/XVD/XVDLicenseBlock.cs b/LibXboxOne/XVD/XVDLicenseBlock.cs new file mode 100644 index 0000000..20d2e3b --- /dev/null +++ b/LibXboxOne/XVD/XVDLicenseBlock.cs @@ -0,0 +1,143 @@ +using System; + +namespace LibXboxOne +{ + public enum XvcLicenseBlockId : uint + { + // EKB block IDs follow + + EkbUnknown1 = 1, // length 0x2, value 0x5 + EkbKeyIdString = 2, // length 0x20 + EkbUnknown2 = 3, // length 0x2, value 0x7 + EkbUnknown4 = 4, // length 0x1, value 0x31 + EkbUnknown5 = 5, // length 0x2, value 0x1 + EkbEncryptedCik = 7, // length 0x180 + + // SPLicenseBlock block IDs follow + + LicenseSection = 0x14, + Unknown1 = 0x15, + Unknown2 = 0x16, + UplinkKeyId = 0x18, + KeyId = 0x1A, + EncryptedCik = 0x1B, + DiscIdSection = 0x1C, + Unknown3 = 0x1E, + Unknown4 = 0x24, + DiscId = 0x25, + SignatureSection = 0x28, + Unknown5 = 0x29, + Unknown6 = 0x2C, + PossibleHash = 0x2A, + PossibleSignature = 0x2B, // length 0x100, could be the encrypted CIK if it's encrypted in the same way as EKB files + + // UWP SPLicenseBlock block IDs follow + + UwpUnknown0 = 0x0, + EncryptedDeviceKey = 0x1, + DeviceLicenseDeviceId = 0x2, + LicenseExpirationTime = 0x20, + LicenseInformation = 0xC9, + PackedContentKeys = 0xCA, + LicenseId = 0xCB, + SignatureBlock = 0xCC, + LicenseEntryIds = 0xCD, + PackageFullName = 0xCE, + UwpUnknown1 = 0xCF, + HardwareId = 0xD0, + UwpUnknown2 = 0xD1, + LicenseDeviceId = 0xD2, + PollingTime = 0xD3, + LicensePolicies = 0xD4, + UwpUnknown3 = 0xD5, + KeyholderPublicSigningKey = 0xDC, + KeyholderPolicies = 0xDD, + KeyholderKeyLicenseId = 0xDE, + ClepSignState = 0x12D, + UwpUnknown4 = 0x12E, + + } + + // XvcLicenseBlock can load in SPLicenseBlock data and EKB data + public class XvcLicenseBlock + { + public XvcLicenseBlockId BlockId; + public uint BlockSize; + public byte[] BlockData; + + public XvcLicenseBlock BlockDataAsBlock => BlockData.Length < MinBlockLength ? null : new XvcLicenseBlock(BlockData, IsEkbFile); + + public byte[] NextBlockData; + public XvcLicenseBlock NextBlockDataAsBlock => NextBlockData.Length < MinBlockLength ? null : new XvcLicenseBlock(NextBlockData, IsEkbFile); + + public bool IsEkbFile; + + public int MinBlockLength = 8; + + public XvcLicenseBlock(byte[] data, bool isEkbFile = false) + { + IsEkbFile = isEkbFile; + int idLen = IsEkbFile ? 2 : 4; + int szLen = 4; + + if (data.Length >= idLen) + BlockId = (XvcLicenseBlockId)(isEkbFile ? BitConverter.ToUInt16(data, 0) : BitConverter.ToUInt32(data, 0)); + + if (isEkbFile && BlockId == (XvcLicenseBlockId)0x4b45) + { + // if its an ekb file and we read the magic of the EKB skip 2 bytes + idLen = 4; + BlockId = (XvcLicenseBlockId)BitConverter.ToUInt16(data, 2); + } + + if (!isEkbFile && BlockId == (XvcLicenseBlockId)0x31424b45) + { + // if we're not an EKB file but we read the EKB magic act like its an EKB + IsEkbFile = true; + idLen = 4; + BlockId = (XvcLicenseBlockId)BitConverter.ToUInt16(data, 2); + } + + if (data.Length >= idLen + szLen) + BlockSize = BitConverter.ToUInt32(data, idLen); + + if (data.Length < BlockSize + (idLen + szLen)) + return; + + BlockData = new byte[BlockSize]; + Array.Copy(data, idLen + szLen, BlockData, 0, BlockSize); + + if (data.Length - (BlockSize + (idLen + szLen)) <= 0) + return; + + NextBlockData = new byte[data.Length - (BlockSize + (idLen + szLen))]; + Array.Copy(data, BlockSize + (idLen + szLen), NextBlockData, 0, NextBlockData.Length); + + idLen = IsEkbFile ? 2 : 4; + szLen = 4; + MinBlockLength = idLen + szLen; + } + public XvcLicenseBlock GetBlockWithId(XvcLicenseBlockId id) + { + int idLen = IsEkbFile ? 2 : 4; + int szLen = 4; + + if (BlockId == id) + return this; + + XvcLicenseBlock block; + if (BlockSize > idLen + szLen && BlockSize >= idLen + szLen + BlockDataAsBlock.BlockSize) + { + block = BlockDataAsBlock.GetBlockWithId(id); + if (block != null) + return block; + } + + if (NextBlockData.Length <= idLen + szLen || NextBlockData.Length < idLen + szLen + NextBlockDataAsBlock.BlockSize) + return null; + + block = NextBlockDataAsBlock.GetBlockWithId(id); + return block; + } + } +} diff --git a/LibXboxOne/XVD/XVDMount.cs b/LibXboxOne/XVD/XVDMount.cs new file mode 100644 index 0000000..342d2f5 --- /dev/null +++ b/LibXboxOne/XVD/XVDMount.cs @@ -0,0 +1,80 @@ +using System; + +namespace LibXboxOne +{ + [Flags] + public enum XvdMountFlags + { + None = 0x0, + ReadOnly = 0x1, + Boot = 0x2, // Used with XvdVmMount + MountEmbeddedXvd = 0x8, + AsRemovable = 0x20 + } + + public class XvdMount + { + public static bool MountXvd(string filepath, string mountPoint, XvdMountFlags flags=0) + { + // Setup Xvd handle + ulong result = Natives.XvdOpenAdapter(out var pHandle); + + if (result != 0x10000000) + { + Console.WriteLine("XvdOpenAdapter failed. Result: 0x{0:X}", result); + return false; + } + + result = Natives.XvdMount(out var pDiskHandle, out var diskNum, pHandle, filepath, 0, mountPoint, (int)flags); + + // Check for errors + if (result == 0x80070002) + { + Console.WriteLine("Failed to find XVD file!"); + } + else if (result == 0xC0000043) + { + Console.WriteLine("Xvd file is already mounted or being used by another process!"); + } + else if (result != 0) + { + Console.WriteLine("XvdMount error: 0x{0:X}", result); + } + else + { + Console.WriteLine($"Package {filepath} attached as disk number {diskNum}"); + } + + Natives.XvdCloseAdapter(pHandle); + return result == 0; + } + + public static bool UnmountXvd(string filepath) + { + // Setup XVD Handle + ulong result = Natives.XvdOpenAdapter(out var pHandle); + + if (result != 0x10000000) + { + Console.WriteLine("XvdOpenAdapter failed. Result: 0x{0:X}", result); + return false; + } + + // UnMount XVD file + result = Natives.XvdUnmountFile(pHandle, filepath); + + //Check for errors + if (result == 0x80070002) + { + Console.WriteLine("Failed to find XVD file!"); + } + else if (result != 0) + { + Console.WriteLine("XvdMount error: 0x{0:X}", result); + } + + Natives.XvdCloseAdapter(pHandle); + return result == 0; + } + } +} diff --git a/LibXboxOne/XVD/XVDStructs.cs b/LibXboxOne/XVD/XVDStructs.cs new file mode 100644 index 0000000..1139522 --- /dev/null +++ b/LibXboxOne/XVD/XVDStructs.cs @@ -0,0 +1,643 @@ +using System; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using LibXboxOne.Keys; + +namespace LibXboxOne +{ + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct XvdHeader + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x200)] + /* 0x0 */ public byte[] Signature; // RSA signature of the hash of 0x200-0xe00 + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + /* 0x0 (from signature) */ public char[] Magic; // msft-xvd + + /* 0x8 */ public XvdVolumeFlags VolumeFlags; + /* 0xC */ public uint FormatVersion; // 3 in latest xvds + /* 0x10 */ public long FileTimeCreated; + /* 0x18 */ public ulong DriveSize; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x20 */ public byte[] VDUID; // DriveID / ContentID + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x30 */ public byte[] UDUID; // UserID + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x40 */ public byte[] TopHashBlockHash; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x60 */ public byte[] OriginalXvcDataHash; // hash of XVC data pre-hashtables, with no PDUIDs + + /* 0x80 */ public XvdType Type; + /* 0x84 */ public XvdContentType ContentType; // if above 0x1A = not an XVC + /* 0x88 */ public uint EmbeddedXVDLength; + /* 0x8C */ public uint UserDataLength; // aka Persistent Local Storage ? + /* 0x90 */ public uint XvcDataLength; + /* 0x94 */ public uint DynamicHeaderLength; + /* 0x98 */ public uint BlockSize; // always 0x000AA000, value seems to be used (staticly built into the exe) during xvdsign en/decryption + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x4)] + /* 0x9C */ public XvdExtEntry[] ExtEntries; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x8)] + /* 0xFC */ public ushort[] Capabilities; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x10C */ public byte[] PECatalogHash; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x12C */ public byte[] EmbeddedXVD_PDUID; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x13C */ public byte[] Reserved13C; + + // encrypted CIK is only used in non-XVC XVDs, field is decrypted with ODK and then used as the CIK to decrypt data blocks + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x14C */ public byte[] KeyMaterial; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x20)] + /* 0x16C */ public byte[] UserDataHash; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x18C */ public char[] SandboxId; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x19C */ public byte[] ProductId; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x1AC */ public byte[] PDUID; // BuildID, changed with every XVD package creation + + /* 0x1BC */ public ushort PackageVersion1; + /* 0x1BE */ public ushort PackageVersion2; + /* 0x1C0 */ public ushort PackageVersion3; + /* 0x1C2 */ public ushort PackageVersion4; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x1C4 */ public ushort[] PECatalogCaps; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x80)] + /* 0x1E4 */ public byte[] PECatalogs; + + /* 0x264 */ public uint WriteableExpirationDate; + /* 0x268 */ public uint WriteablePolicyFlags; + /* 0x26C */ public uint PersistentLocalStorageSize; + + // NEW FIELDS: only seen in SoDTest windows-XVC! + /* 0x270 */ public byte MutableDataPageCount; + /* 0x271 */ public byte Unknown271; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x272 */ public byte[] Unknown272; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xA)] + /* 0x282 */ public byte[] Reserved282; + /* 0x28C */ public long SequenceNumber; + /* 0x294 */ public ushort RequiredSystemVersion1; + /* 0x296 */ public ushort RequiredSystemVersion2; + /* 0x298 */ public ushort RequiredSystemVersion3; + /* 0x29A */ public ushort RequiredSystemVersion4; + + /* 0x29C */ public OdkIndex ODKKeyslotID; // 0x2 for test ODK, 0x0 for retail ODK? (makepkg doesn't set this for test ODK crypted packages?) + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xB54)] + /* 0x2A0 */ public byte[] Reserved2A0; + /* 0xDF4 */ public ulong ResilientDataOffset; + /* 0xDFC */ public uint ResilientDataLength; + + /* 0xE00 = END */ + + public ulong MutableDataLength => XvdMath.PageNumberToOffset(MutableDataPageCount); + public ulong UserDataPageCount => XvdMath.BytesToPages(UserDataLength); + public ulong XvcInfoPageCount => XvdMath.BytesToPages(XvcDataLength); + public ulong EmbeddedXvdPageCount => XvdMath.BytesToPages(EmbeddedXVDLength); + public ulong DynamicHeaderPageCount => XvdMath.BytesToPages(DynamicHeaderLength); + public ulong DrivePageCount => XvdMath.BytesToPages(DriveSize); + public ulong NumberOfHashedPages => DrivePageCount + UserDataPageCount + XvcInfoPageCount + DynamicHeaderPageCount; + public ulong NumberOfMetadataPages => UserDataPageCount + XvcInfoPageCount + DynamicHeaderPageCount; + public ulong SectorSize => VolumeFlags.HasFlag(XvdVolumeFlags.LegacySectorSize) ? + XvdFile.LEGACY_SECTOR_SIZE : + XvdFile.SECTOR_SIZE; + + public bool IsSigned + { + get + { + // Some console-signed xvds only have 0x10/0x20 bytes of signature area filled + // -> Consider such packages unsigned + var data = new byte[Signature.Length - 0x20]; + Array.Copy(Signature, 0x20, data, 0, data.Length); + return !data.IsArrayEmpty(); + } + } + + byte[] GetHeaderWithoutSignature() + { + var rawHeader = Shared.StructToBytes(this); + byte[] headerData = new byte[rawHeader.Length - Signature.Length]; + // Copy headerdata, skipping signature + Array.Copy(rawHeader, Signature.Length, headerData, 0, headerData.Length); + return headerData; + } + + public string SignedBy + { + get + { + if (!IsSigned) + return ""; + + var headerData = GetHeaderWithoutSignature(); + foreach(var signKey in DurangoKeys.GetAllXvdSigningKeys()) + { + if(signKey.Value.KeyData != null && HashUtils.VerifySignature(signKey.Value.KeyData, Signature, headerData)) + return signKey.Key; + } + return ""; + } + } + + public bool Resign(byte[] key, string keyType) + { + var headerData = GetHeaderWithoutSignature(); + return HashUtils.SignData(key, keyType, headerData, out Signature); + } + + public bool ResignWithRedKey() + { + var key = DurangoKeys.GetSignkeyByName("RedXvdPrivateKey"); + if (key == null || !key.HasKeyData) + throw new InvalidOperationException("Private Xvd Red key is not loaded, cannot resign xvd header"); + + return Resign(key.KeyData, "RSAFULLPRIVATEBLOB"); + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine("XvdHeader:"); + + string fmt = formatted ? " " : ""; + + if (!Enum.IsDefined(typeof(XvdContentType), ContentType)) + b.AppendLineSpace(fmt + $"Unknown content type 0x{ContentType:X}"); + + b.AppendLineSpace(fmt + $"Signed by: {SignedBy}"); + + b.AppendLineSpace(fmt + $"Using ODK keyslot: {ODKKeyslotID}"); + + b.AppendLineSpace(fmt + $"Read-only flag {(VolumeFlags.HasFlag(XvdVolumeFlags.ReadOnly) ? "set" : "not set")}"); + + b.AppendLineSpace(fmt + (VolumeFlags.HasFlag(XvdVolumeFlags.EncryptionDisabled) + ? "Decrypted" + : "Encrypted")); + + b.AppendLineSpace(fmt + (VolumeFlags.HasFlag(XvdVolumeFlags.DataIntegrityDisabled) + ? "Data integrity disabled (doesn't use hash tree)" + : "Data integrity enabled (uses hash tree)")); + + b.AppendLineSpace(fmt + (VolumeFlags.HasFlag(XvdVolumeFlags.LegacySectorSize) + ? "Legacy Sector Size (512 bytes)" + : "Sector Size (4096 bytes)")); + + b.AppendLineSpace(fmt + $"ResiliencyEnabled {(VolumeFlags.HasFlag(XvdVolumeFlags.ResiliencyEnabled) ? "set" : "not set")}"); + b.AppendLineSpace(fmt + $"SraReadOnly {(VolumeFlags.HasFlag(XvdVolumeFlags.SraReadOnly) ? "set" : "not set")}"); + + b.AppendLineSpace(fmt + $"RegionIdInXts {(VolumeFlags.HasFlag(XvdVolumeFlags.RegionIdInXts) ? "set" : "not set")}"); + b.AppendLineSpace(fmt + $"EraSpecific {(VolumeFlags.HasFlag(XvdVolumeFlags.EraSpecific) ? "set" : "not set")}"); + + b.AppendLine(); + + b.AppendLineSpace(fmt + $"Magic: {new string(Magic)}"); + b.AppendLineSpace(fmt + $"Volume Flags: 0x{VolumeFlags:X}"); + b.AppendLineSpace(fmt + $"Format Version: 0x{FormatVersion:X}"); + + b.AppendLineSpace(fmt + $"File Time Created: {DateTime.FromFileTime(FileTimeCreated)}"); + b.AppendLineSpace(fmt + $"Drive Size: 0x{DriveSize:X}"); + + b.AppendLineSpace(fmt + $"VDUID / Drive Id: {new Guid(VDUID)}"); + b.AppendLineSpace(fmt + $"UDUID / User Id: {new Guid(UDUID)}"); + + b.AppendLineSpace(fmt + $"Top Hash Block Hash: {Environment.NewLine}{fmt}{TopHashBlockHash.ToHexString()}"); + b.AppendLineSpace(fmt + $"Original XVC Data Hash: {Environment.NewLine}{fmt}{OriginalXvcDataHash.ToHexString()}"); + + b.AppendLineSpace(fmt + $"XvdType: {Type}"); + b.AppendLineSpace(fmt + $"Content Type: 0x{ContentType:X} ({ContentType})"); + b.AppendLineSpace(fmt + $"Embedded XVD PDUID/Build Id: {new Guid(EmbeddedXVD_PDUID)}"); + b.AppendLineSpace(fmt + $"Embedded XVD Length: 0x{EmbeddedXVDLength:X}"); + b.AppendLineSpace(fmt + $"User Data Length: 0x{UserDataLength:X}"); + b.AppendLineSpace(fmt + $"XVC Data Length: 0x{XvcDataLength:X}"); + b.AppendLineSpace(fmt + $"Dynamic Header Length: 0x{DynamicHeaderLength:X}"); + b.AppendLineSpace(fmt + $"BlockSize: 0x{BlockSize:X}"); + b.AppendLineSpace(fmt + $"Ext Entries: {ExtEntries.Count(e => !e.IsEmpty)}"); + foreach(XvdExtEntry entry in ExtEntries.Where(e => !e.IsEmpty)) + b.AppendLineSpace(fmt + entry.ToString(true)); + + b.AppendLineSpace(fmt + $"Capabilities: {Capabilities.ToHexString()}"); + + b.AppendLineSpace(fmt + $"PE Catalog Hash: {PECatalogHash.ToHexString()}"); + b.AppendLineSpace(fmt + $"Userdata hash: {UserDataHash.ToHexString()}"); + + b.AppendLineSpace(fmt + $"PE Catalog Caps: {PECatalogCaps.ToHexString()}"); + b.AppendLineSpace(fmt + $"PE Catalogs: {PECatalogs.ToHexString()}"); + + b.AppendLineSpace(fmt + $"Writeable Expiration Date: 0x{WriteableExpirationDate:X}"); + b.AppendLineSpace(fmt + $"Writeable Policy flags: 0x{WriteablePolicyFlags:X}"); + b.AppendLineSpace(fmt + $"Persistent Local storage length: 0x{PersistentLocalStorageSize:X}"); + b.AppendLineSpace(fmt + $"Mutable data page count: 0x{MutableDataPageCount:X} (0x{MutableDataLength:X} bytes)"); + + b.AppendLineSpace(fmt + $"Sandbox Id: {new string(SandboxId).Replace("\0", "")}"); + b.AppendLineSpace(fmt + $"Product Id: {new Guid(ProductId)}"); + b.AppendLineSpace(fmt + $"PDUID/Build Id: {new Guid(PDUID)}"); + b.AppendLineSpace(fmt + $"Sequence Number: {SequenceNumber}"); + b.AppendLineSpace(fmt + $"Package Version: {PackageVersion4}.{PackageVersion3}.{PackageVersion2}.{PackageVersion1}"); + b.AppendLineSpace(fmt + $"Required System Version: {RequiredSystemVersion4}.{RequiredSystemVersion3}.{RequiredSystemVersion2}.{RequiredSystemVersion1}"); + b.AppendLineSpace(fmt + $"ODK Keyslot ID: {ODKKeyslotID}"); + b.AppendLineSpace(fmt + $"KeyMaterial: {Environment.NewLine}{fmt}{KeyMaterial.ToHexString()}"); + b.AppendLineSpace(fmt + $"Resilient Data offset: 0x{ResilientDataOffset:X}"); + b.AppendLineSpace(fmt + $"Resilient Data length: 0x{ResilientDataLength:X}"); + + if (Unknown271 != 0) + b.AppendLineSpace(fmt + $"Unknown271: 0x{Unknown271:X}"); + + if (!Unknown272.IsArrayEmpty()) + b.AppendLineSpace(fmt + $"Unknown272: {Unknown272.ToHexString()}"); + + if (!Reserved13C.IsArrayEmpty()) + b.AppendLineSpace(fmt + $"Reserved13C: {Environment.NewLine}{fmt}{Reserved13C.ToHexString()}"); + if (!Reserved13C.IsArrayEmpty()) + b.AppendLineSpace(fmt + $"Reserved13C: {Environment.NewLine}{fmt}{Reserved13C.ToHexString()}"); + if (!Reserved2A0.IsArrayEmpty()) + b.AppendLineSpace(fmt + $"Reserved2A0: {Environment.NewLine}{fmt}{Reserved2A0.ToHexString()}"); + + return b.ToString(); + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvdExtEntry + { + /* 0x00 */ public uint Code; + /* 0x04 */ public uint Length; + /* 0x08 */ public ulong Offset; + /* 0x10 */ public uint DataLength; + /* 0x14 */ public uint Reserved; + + /* 0x18 = END */ + + public bool IsEmpty => Code == 0 && Length == 0 && Offset == 0 && DataLength == 0 && Reserved == 0; + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + string fmt = formatted ? " " : ""; + return fmt + $"XvdExtEntry: Code: {Code:X}, Length: {Length:X}, Offset: {Offset:X}, DataLength: {DataLength:X}"; + } + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct XvcUpdateSegment + { + /* 0x0 */ public uint PageNum; + /* 0x4 */ public ulong Hash; + + /* 0xC = END */ + + public override string ToString() + { + return ToString(false); + } + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine("XvcUpdateSegment"); + b.AppendLine(); + + string fmt = formatted ? " " : ""; + + b.AppendLineSpace(fmt + $"PageNum: 0x{PageNum:X} (@ 0x{XvdMath.PageNumberToOffset(PageNum)})"); + b.AppendLineSpace(fmt + $"Hash: 0x{Hash:X}"); + + return b.ToString(); + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvcRegionSpecifier + { + /* 0x0 */ public XvcRegionId RegionId; + /* 0x4 */ public uint Padding4; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)] + /* 0x8 */ public string Key; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)] + /* 0x88 */ public string Value; + + /* 0x188 = END */ + + public override string ToString() + { + return ToString(false); + } + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine("XvcRegionSpecifier"); + b.AppendLine(); + + string fmt = formatted ? " " : ""; + + b.AppendLineSpace(fmt + $"Region ID: 0x{(uint)RegionId:X} {RegionId})"); + b.AppendLineSpace(fmt + $"Key: {Key}"); + b.AppendLineSpace(fmt + $"Value: {Value}"); + + if (Padding4 != 0) + b.AppendLineSpace(fmt + $"Padding4: 0x{Padding4:X}"); + + return b.ToString(); + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvcRegionHeader + { + /* 0x0 */ public XvcRegionId Id; + /* 0x4 */ public ushort KeyId; + /* 0x6 */ public ushort Padding6; + /* 0x8 */ public XvcRegionFlags Flags; + /* 0xC */ public uint FirstSegmentIndex; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)] + /* 0x10 */ public string Description; // XVC-HD = Header, XVC-EXVD = Embedded XVD, XVC-MD = XVC metadata, FS-MD = FileSystem metadata + + /* 0x50 */ public ulong Offset; + /* 0x58 */ public ulong Length; + /* 0x60 */ public ulong Hash; // aka RegionPDUID + + /* 0x68 */ public ulong Unknown68; + /* 0x70 */ public ulong Unknown70; + /* 0x78 */ public ulong Unknown78; + + /* 0x80 = END */ + + public override string ToString() + { + return ToString(false); + } + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine($"XvcRegionHeader (ID/EncryptionIV: 0x{(uint)Id:X} {Id}):"); + + string fmt = formatted ? " " : ""; + + if (Padding6 != 0) + b.AppendLineSpace(fmt + "Padding6 != 0"); + if (Unknown68 != 0) + b.AppendLineSpace(fmt + "Unknown68 != 0"); + if (Unknown70 != 0) + b.AppendLineSpace(fmt + "Unknown70 != 0"); + if (Unknown78 != 0) + b.AppendLineSpace(fmt + "Unknown78 != 0"); + + string keyid = KeyId.ToString("X"); + if (KeyId == XvcConstants.XVC_KEY_NONE) + keyid += " (not encrypted)"; + b.AppendLineSpace(fmt + $"Description: {Description.Replace("\0", "")}"); + b.AppendLineSpace(fmt + $"Key ID: 0x{keyid}"); + b.AppendLineSpace(fmt + $"Flags: 0x{(uint)Flags:X}"); + if (Flags.HasFlag(XvcRegionFlags.Resident)) + b.AppendLineSpace(fmt + " - Resident"); + if (Flags.HasFlag(XvcRegionFlags.InitialPlay)) + b.AppendLineSpace(fmt + " - InitialPlay"); + if (Flags.HasFlag(XvcRegionFlags.Preview)) + b.AppendLineSpace(fmt + " - Preview"); + if (Flags.HasFlag(XvcRegionFlags.FileSystemMetadata)) + b.AppendLineSpace(fmt + " - FileSystemMetadata"); + if (Flags.HasFlag(XvcRegionFlags.Present)) + b.AppendLineSpace(fmt + " - Present"); + if (Flags.HasFlag(XvcRegionFlags.OnDemand)) + b.AppendLineSpace(fmt + " - OnDemand"); + if (Flags.HasFlag(XvcRegionFlags.Available)) + b.AppendLineSpace(fmt + " - Available"); + + b.AppendLineSpace(fmt + $"Offset: 0x{Offset:X}"); + b.AppendLineSpace(fmt + $"Length: 0x{Length:X}"); + b.AppendLineSpace(fmt + $"Hash: 0x{Hash:X}"); + b.AppendLineSpace(fmt + $"First Segment Index: {FirstSegmentIndex}"); + b.AppendLine(); + + if (Unknown68 != 0) + b.AppendLineSpace(fmt + $"Unknown68: 0x{Unknown68:X}"); + if (Unknown70 != 0) + b.AppendLineSpace(fmt + $"Unknown70: 0x{Unknown70:X}"); + if (Unknown78 != 0) + b.AppendLineSpace(fmt + $"Unknown78: 0x{Unknown78:X}"); + if (Padding6 != 0) + b.AppendLineSpace(fmt + $"Padding6: 0x{Padding6:X}"); + + return b.ToString(); + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct XvcEncryptionKeyId + { + public bool IsKeyNulled => KeyId == null || KeyId.IsArrayEmpty(); + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + public byte[] KeyId; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] + public struct XvcInfo + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x0 */ public byte[] ContentID; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0xC0)] + /* 0x10 */ public XvcEncryptionKeyId[] EncryptionKeyIds; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x100)] + /* 0xC10 */ public byte[] Description; // unicode? + + /* 0xD10 */ public uint Version; + /* 0xD14 */ public uint RegionCount; + /* 0xD18 */ public uint Flags; + /* 0xD1C */ public ushort PaddingD1C; + /* 0xD1E */ public ushort KeyCount; + /* 0xD20 */ public uint UnknownD20; + /* 0xD24 */ public uint InitialPlayRegionId; + /* 0xD28 */ public ulong InitialPlayOffset; + /* 0xD30 */ public long FileTimeCreated; + /* 0xD38 */ public uint PreviewRegionId; + /* 0xD3C */ public uint UpdateSegmentCount; + /* 0xD40 */ public ulong PreviewOffset; + /* 0xD48 */ public ulong UnusedSpace; + /* 0xD50 */ public uint RegionSpecifierCount; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x54)] + /* 0xD54 */ public byte[] ReservedD54; + + /* 0xDA8 = END (actually 0x2000 but rest is read in XVDFile class) */ + + public bool IsUsingTestCik + { + get + { + Guid testCik = new Guid("33EC8436-5A0E-4F0D-B1CE-3F29C3955039"); + return EncryptionKeyIds != null && + EncryptionKeyIds.Length > 0 && + EncryptionKeyIds[0].KeyId.IsEqualTo(testCik.ToByteArray()); + } + } + + public bool IsAnyKeySet + { + get { return EncryptionKeyIds.Any(keyId => !keyId.IsKeyNulled); } + } + + public override string ToString() + { + return ToString(false); + } + + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine("XvcInfo:"); + + string fmt = formatted ? " " : ""; + + if (Flags != 0) + b.AppendLineSpace(fmt + "Flags != 0"); + if (PaddingD1C != 0) + b.AppendLineSpace(fmt + "PaddingD1C != 0"); + if (UnknownD20 != 0) + b.AppendLineSpace(fmt + "UnknownD20 != 0"); + if (PreviewRegionId != 0) + b.AppendLineSpace(fmt + "PreviewRegionId != 0"); + if (PreviewOffset != 0) + b.AppendLineSpace(fmt + "PreviewOffset != 0"); + if (UnusedSpace != 0) + b.AppendLineSpace(fmt + "UnusedSpace != 0"); + if (!ReservedD54.IsArrayEmpty()) + b.AppendLineSpace(fmt + "Reserved != null"); + + var signType = "Unsigned/not crypted (/LU)"; + if (KeyCount > 0) + if (IsUsingTestCik) + signType = "Test-crypted (/LT)"; + else if (!IsAnyKeySet) + signType = "Unsigned with KeyCount > 0"; + else + signType = "Submission-crypted (not using test key) (/L)"; + + b.AppendLineSpace(fmt + signType); + b.AppendLineSpace(fmt + $"/updcompat type {(UpdateSegmentCount == 0 ? "1" : "2")}"); + + b.AppendLine(); + + b.AppendLineSpace(fmt + $"ContentID: {new Guid(ContentID)}"); + for(int i = 0; i < EncryptionKeyIds.Length; i++) + if(!EncryptionKeyIds[i].IsKeyNulled) + b.AppendLineSpace(fmt + $"Encryption Key {i} GUID: {new Guid(EncryptionKeyIds[i].KeyId)}"); + + b.AppendLine(); + + b.AppendLineSpace(fmt + $"Description: {Encoding.Unicode.GetString(Description).Replace("\0", "")}"); + b.AppendLineSpace(fmt + $"Version: 0x{Version:X}"); + b.AppendLineSpace(fmt + $"Region Count: 0x{RegionCount:X}"); + b.AppendLineSpace(fmt + $"Flags: 0x{Flags:X}"); + b.AppendLineSpace(fmt + $"Key Count: 0x{KeyCount:X}"); + b.AppendLineSpace(fmt + $"InitialPlay Region Id: 0x{InitialPlayRegionId:X}"); + b.AppendLineSpace(fmt + $"InitialPlay Offset: 0x{InitialPlayOffset:X}"); + b.AppendLineSpace(fmt + $"File Time Created: {DateTime.FromFileTime(FileTimeCreated)}"); + b.AppendLineSpace(fmt + $"Preview Region Id: 0x{PreviewRegionId:X}"); + b.AppendLineSpace(fmt + $"Update Segment Count: {UpdateSegmentCount}"); + b.AppendLineSpace(fmt + $"Preview Offset: 0x{PreviewOffset:X}"); + b.AppendLineSpace(fmt + $"Unused Space: 0x{UnusedSpace:X}"); + b.AppendLine(); + + if (PaddingD1C != 0) + b.AppendLineSpace(fmt + $"PaddingD1C: 0x{PaddingD1C:X}"); + + if (UnknownD20 != 0) + b.AppendLineSpace(fmt + $"UnknownD20: 0x{UnknownD20:X}"); + + if (!ReservedD54.IsArrayEmpty()) + b.AppendLineSpace(fmt + $"ReservedD54: {Environment.NewLine}{fmt}{ReservedD54.ToHexString()}"); + + return b.ToString(); + } + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvdUserDataHeader + { + /* 0x0 */ public uint Length; + /* 0x4 */ public uint Version; + /* 0x8 */ public XvdUserDataType Type; // Only 0? + /* 0xc */ public uint Unknown; // thought this was data length, but apparently it isn't + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvdUserDataPackageFilesHeader + { + /* 0x0 */ public uint Version; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + /* 0x4 */ public string PackageFullName; + /* 0x108 */ public uint FileCount; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvdUserDataPackageFileEntry + { + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] + /* 0x0 */ public string FilePath; + /* 0x104 */ public uint Size; + /* 0x108 */ public uint Offset; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)] + public struct XvdSegmentMetadataHeader + { + /* 0x0 */ public uint Magic; // 20 50 46 58 (' PFX' / 'XFP ') + /* 0x4 */ public uint Version0; // 4 is currently used + /* 0x8 */ public uint Version1; + /* 0xc */ public uint HeaderLength; + /* 0x10 */ public uint SegmentCount; + /* 0x14 */ public uint FilePathsLength; // BlockSize? + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)] + /* 0x18 */ public byte[] PDUID; + + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x3c)] + /* 0x28 */ + public byte[] Unknown; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1, Size = 0x10)] + public struct XvdSegmentMetadataSegment + { + /* 0x0 */ public XvdSegmentMetadataSegmentFlags Flags; + /* 0x2 */ public ushort PathLength; + /* 0x4 */ public uint PathOffset; + /* 0x8 */ public ulong FileSize; + } +} diff --git a/LibXboxOne/XVD/XvcConstants.cs b/LibXboxOne/XVD/XvcConstants.cs new file mode 100644 index 0000000..f56f9b3 --- /dev/null +++ b/LibXboxOne/XVD/XvcConstants.cs @@ -0,0 +1,7 @@ +namespace LibXboxOne +{ + public static class XvcConstants + { + public const ushort XVC_KEY_NONE = 0xFFFF; + } +} \ No newline at end of file diff --git a/LibXboxOne/XVD/XvcRegionId.cs b/LibXboxOne/XVD/XvcRegionId.cs new file mode 100644 index 0000000..88dba24 --- /dev/null +++ b/LibXboxOne/XVD/XvcRegionId.cs @@ -0,0 +1,12 @@ +namespace LibXboxOne +{ + public enum XvcRegionId : uint + { + MetadataXvc = 0x40000001, + MetadataFilesystem = 0x40000002, + Unknown = 0x40000003, + EmbeddedXvd = 0x40000004, + Header = 0x40000005, + MutableData = 0x40000006 + } +} \ No newline at end of file diff --git a/LibXboxOne/XVD/XvdFilesystem.cs b/LibXboxOne/XVD/XvdFilesystem.cs new file mode 100644 index 0000000..adeb086 --- /dev/null +++ b/LibXboxOne/XVD/XvdFilesystem.cs @@ -0,0 +1,351 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Diagnostics; +using DiscUtils; +using DiscUtils.Ntfs; +using DiscUtils.Partitions; +using DiscUtils.Streams; + +namespace LibXboxOne +{ + public class XvdFilesystem + { + XvdFile _xvdFile { get; } + XvdFilesystemStream _fs { get; } + + XvdType XvdFsType => _xvdFile.Header.Type; + uint SectorSize => (uint)_xvdFile.Header.SectorSize; + ulong FilesystemSize => _xvdFile.Header.DriveSize; + Geometry DiskGeometry => + Geometry.FromCapacity((long)FilesystemSize, (int)SectorSize); + + const long CHUNK_SIZE = (16 * 1024 * 1024); // 16 MB + + public XvdFilesystem(XvdFile file) + { + _xvdFile = file; + _fs = new XvdFilesystemStream(_xvdFile); + } + + bool GetGptPartitionTable(out GuidPartitionTable partitionTable) + { + try + { + partitionTable = new GuidPartitionTable(_fs, DiskGeometry); + } + catch (Exception) + { + partitionTable = null; + } + + // CHECKME: Count refers to the number of PARTITION TABLES, not partitions? + if (partitionTable == null || partitionTable.Count <= 0) + { + Debug.WriteLine("No GPT partition table detected"); + return false; + } + + return true; + } + + bool GetMbrPartitionTable(out BiosPartitionTable partitionTable) + { + try + { + partitionTable = new BiosPartitionTable(_fs, DiskGeometry); + } + catch (Exception) + { + partitionTable = null; + } + + // CHECKME: Count refers to the number of PARTITION TABLES, not partitions? + if (partitionTable == null || partitionTable.Count <= 0) + { + Debug.WriteLine("No MBR partition table detected"); + return false; + } + + return true; + } + + PartitionTable OpenDisk() + { + // Gathering partitions manually so Geometry can be provided + // explicitly. This ensures that 4k sectors are properly set. + PartitionTable partitionTable; + if (GetGptPartitionTable(out GuidPartitionTable gptTable)) + { + partitionTable = (PartitionTable)gptTable; + } + else if (GetMbrPartitionTable(out BiosPartitionTable mbrTable)) + { + partitionTable = (PartitionTable)mbrTable; + } + else + { + Debug.WriteLine("No valid partition table detected"); + return null; + } + + if (partitionTable.Partitions == null || partitionTable.Partitions.Count <= 0) + { + Debug.WriteLine("Partition table holds no partitions"); + return null; + } + + return partitionTable; + } + + IEnumerable IterateFilesystem(int partitionNumber) + { + IEnumerable IterateSubdir(DiscUtils.DiscDirectoryInfo subdir) + { + foreach(var dir in subdir.GetDirectories()) + { + foreach(var subfile in IterateSubdir(dir)) + { + yield return subfile; + } + } + + var files = subdir.GetFiles(); + foreach(var f in files) + { + yield return f; + } + } + + PartitionTable disk = OpenDisk(); + if (disk == null) + { + Debug.WriteLine("IterateFilesystem: Failed to open disk"); + yield break; + } + else if (disk.Partitions.Count - 1 < partitionNumber) + { + Debug.WriteLine($"IterateFilesystem: Partition {partitionNumber} does not exist"); + yield break; + } + + using (var partitionStream = disk.Partitions[partitionNumber].Open()) + { + NtfsFileSystem fs = new DiscUtils.Ntfs.NtfsFileSystem(partitionStream); + + foreach(var file in IterateSubdir(fs.Root)) + { + yield return file; + } + + fs.Dispose(); + partitionStream.Dispose(); + } + } + + public bool ExtractFilesystem(string outputDirectory, int partitionNumber=0) + { + foreach (var file in IterateFilesystem(partitionNumber)) + { + Console.WriteLine(file.DirectoryName + file.Name); + /* Assemble destination path and create directory */ + var destDir = outputDirectory; + var parentDirs = file.DirectoryName.Split('\\'); + foreach(var pd in parentDirs) + { + destDir = Path.Combine(destDir, pd); + } + Directory.CreateDirectory(destDir); + + /* Write out file */ + var destFile = Path.Combine(destDir, file.Name); + using(var srcFile = file.OpenRead()) + { + using(var dstFs = File.Create(destFile)) + { + while(dstFs.Length < srcFile.Length) + { + var readSize = Math.Min(CHUNK_SIZE, srcFile.Length - srcFile.Position); + var data = new byte[readSize]; + if (srcFile.Read(data, 0, data.Length) != data.Length) + throw new InvalidOperationException( + $"Failed to read {srcFile} from raw image"); + + dstFs.Write(data, 0, data.Length); + } + } + } + } + + return true; + } + + public bool ExtractFilesystemImage(string targetFile, bool createVhd) + { + using (var destFs = File.Open(targetFile, FileMode.Create)) + { + byte[] buffer = new byte[XvdFile.PAGE_SIZE]; + + for (long offset = 0; offset < _fs.Length; offset += XvdFile.PAGE_SIZE) + { + _fs.Read(buffer, 0, buffer.Length); + destFs.Write(buffer, 0, buffer.Length); + } + } + return true; + } + + void InitializeVhdManually(DiscUtils.Vhd.Disk vhdDisk) + { + BiosPartitionTable.Initialize(vhdDisk, WellKnownPartitionType.WindowsNtfs); + // GuidPartitionTable.Initialize(vhdDisk, WellKnownPartitionType.WindowsNtfs); + + var volMgr = new VolumeManager(vhdDisk); + var logicalVolume = volMgr.GetLogicalVolumes()[0]; + + var label = $"XVDTool conversion"; + + using (var destNtfs = NtfsFileSystem.Format(logicalVolume, label, new NtfsFormatOptions())) + { + destNtfs.NtfsOptions.ShortNameCreation = ShortFileNameOption.Disabled; + + // NOTE: For VHD creation we just assume a single partition + foreach (var file in IterateFilesystem(partitionNumber: 0)) + { + var fh = file.OpenRead(); + + if (!destNtfs.Exists(file.DirectoryName)) + { + destNtfs.CreateDirectory(file.DirectoryName); + } + + using (Stream dest = destNtfs.OpenFile(file.FullName, FileMode.Create, + FileAccess.ReadWrite)) + { + fh.CopyTo(dest); + dest.Flush(); + } + + fh.Close(); + } + } + } + + void InitializeVhdViaPump(DiscUtils.Vhd.Disk vhdDisk) + { + if (SectorSize != XvdFile.LEGACY_SECTOR_SIZE) + { + throw new InvalidOperationException( + "Initializing VHD via pump is only supported for 512 byte sectors"); + } + + var pump = new StreamPump(_fs, vhdDisk.Content, (int)XvdFile.LEGACY_SECTOR_SIZE); + pump.Run(); + } + + // Source: https://github.com/DiscUtils/DiscUtils/issues/137 + public bool ConvertToVhd(string outputFile) + { + using (FileStream destVhdFs = File.Open(outputFile, FileMode.Create, FileAccess.ReadWrite)) + { + DiscUtils.Vhd.Disk vhdDisk; + if (XvdFsType == XvdType.Fixed) + { + Console.WriteLine("Initializing fixed VHD..."); + vhdDisk = DiscUtils.Vhd.Disk.InitializeFixed(destVhdFs, + Ownership.None, + (long)FilesystemSize + + (long)(FilesystemSize / 10)); + } + else if (XvdFsType == XvdType.Dynamic) + { + Console.WriteLine("Initializing dynamic VHD..."); + vhdDisk = DiscUtils.Vhd.Disk.InitializeDynamic(destVhdFs, + Ownership.None, + (long)FilesystemSize + + (long)(FilesystemSize / 10)); + } + else + throw new InvalidOperationException(); + + if (SectorSize == XvdFile.LEGACY_SECTOR_SIZE) + { + Console.WriteLine("Pumping data as-is to vhd (legacy sector size)"); + InitializeVhdViaPump(vhdDisk); + } + else + { + Console.WriteLine("Creating vhd manually (4k sectors)"); + InitializeVhdManually(vhdDisk); + } + } + return true; + } + + public string FileInfoToString(DiscFileInfo info) + { + return $"{info.FullName} ({info.Length} bytes)"; + } + + public override string ToString() + { + return ToString(true); + } + + public string ToString(bool formatted) + { + var b = new StringBuilder(); + b.AppendLine("XvdFilesystem:"); + + string fmt = formatted ? " " : ""; + + if (_xvdFile.IsEncrypted) + { + b.AppendLineSpace(fmt + "Cannot get XvdFilesystem info from encrypted package"); + return b.ToString(); + } + + var disk = OpenDisk(); + if (disk == null) + { + b.AppendLineSpace(fmt + "No partition table found on disk!"); + return b.ToString(); + } + + b.AppendLineSpace(fmt + "Partitions:"); + var partitions = disk.Partitions; + for (int i = 0; i < partitions.Count; i++) + { + var part = partitions[i]; + b.AppendLineSpace(fmt + fmt + $"- Partition {i}:"); + + b.AppendLineSpace(fmt + fmt + fmt + $" BIOS-type: {part.TypeAsString} ({part.BiosType} / 0x{part.BiosType:X})"); + b.AppendLineSpace(fmt + fmt + fmt + $" GUID-type: {part.GuidType}"); + b.AppendLineSpace(fmt + fmt + fmt + $" First sector: {part.FirstSector} (0x{part.FirstSector:X})"); + b.AppendLineSpace(fmt + fmt + fmt + $" Last sector: {part.LastSector} (0x{part.LastSector:X})"); + b.AppendLineSpace(fmt + fmt + fmt + $" Sector count: {part.SectorCount} (0x{part.SectorCount:X})"); + b.AppendLine(); + } + + b.AppendLineSpace(fmt + "Filesystem content:"); + try + { + for (int partitionNumber = 0; partitionNumber < partitions.Count; partitionNumber++) + { + b.AppendLineSpace(fmt + fmt + $":: Partition {partitionNumber}:"); + foreach (var file in IterateFilesystem(partitionNumber)) + { + b.AppendLineSpace(fmt + fmt + FileInfoToString(file)); + } + } + } + catch (Exception e) + { + b.AppendLineSpace(fmt + fmt + $"Failed to list filesystem content. Error: {e}"); + } + + return b.ToString(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/XVD/XvdFilesystemStream.cs b/LibXboxOne/XVD/XvdFilesystemStream.cs new file mode 100644 index 0000000..0c82ac2 --- /dev/null +++ b/LibXboxOne/XVD/XvdFilesystemStream.cs @@ -0,0 +1,152 @@ +using System; +using System.Diagnostics; +using System.IO; + +namespace LibXboxOne +{ + public class XvdFilesystemStream : Stream + { + readonly XvdFile _xvdFile; + + public override bool CanRead => true; + public override bool CanSeek => true; + // Disable writing for now + public override bool CanWrite => false; + + public override long Length => (long)_xvdFile.Header.DriveSize; + public override long Position { get; set; } + + // Absolute offset in Xvd of DriveData start + long DriveBaseOffset => (long)_xvdFile.DriveDataOffset; + // Absolute offset to use for calculation BAT target offset + long DynamicBaseOffset => (long)_xvdFile.DynamicBaseOffset; + // Length of static data for XvdType.Dynamic + long StaticDataLength => (long)_xvdFile.StaticDataLength; + + public XvdFilesystemStream(XvdFile file) + { + _xvdFile = file; + Position = 0; + } + + public override void Flush() + { + } + + byte[] InternalRead(int count) + { + var offset = DriveBaseOffset + Position; + return InternalReadAbsolute(offset, count); + } + + byte[] InternalReadAbsolute(long offset, int count) + { + Debug.WriteLine($"InternalReadAbsolute: Reading 0x{count:X} @ 0x{offset:X}"); + var data = _xvdFile.ReadBytes(offset, count); + if (data.Length <= 0) + throw new IOException("InternalReadAbsolute got nothing..."); + + // Debug.WriteLine($"Got {data.Length:X} bytes: {data.ToHexString("")}"); + Position += data.Length; + return data; + } + + byte[] ReadDynamic(int count) + { + int positionInBuffer = 0; + int bytesRemaining = count; + byte[] destBuffer = new byte[count]; + + while (positionInBuffer < count) + { + byte[] data = new byte[0]; + if (Position < StaticDataLength) + { + // Read a chunk from non-dynamic area, next iteration will read dynamic data + int maxReadLength = (int)(StaticDataLength - Position); + int length = bytesRemaining > maxReadLength ? maxReadLength : bytesRemaining; + data = InternalRead(length); + } + else + { + // Lookup block allocation table for real data offset + var targetVirtualOffset = (ulong)(Position - StaticDataLength); + ulong blockNumber = XvdMath.OffsetToBlockNumber(targetVirtualOffset); + long inBlockOffset = (long)XvdMath.InBlockOffset(targetVirtualOffset); + int maxReadLength = (int)(XvdFile.BLOCK_SIZE - inBlockOffset); + int length = bytesRemaining > maxReadLength ? maxReadLength : bytesRemaining; + + var targetPage = _xvdFile.ReadBat(blockNumber); + if (targetPage == XvdFile.INVALID_SECTOR) + { + data = new byte[length]; + // Advance stream position cause we are not actually reading data + Position += length; + } + else + { + long targetPhysicalOffset = DynamicBaseOffset + + (long)XvdMath.PageNumberToOffset(targetPage) + + inBlockOffset; + + data = InternalReadAbsolute(targetPhysicalOffset, length); + } + } + + Array.Copy(data, 0, destBuffer, positionInBuffer, data.Length); + positionInBuffer += data.Length; + bytesRemaining -= data.Length; + } + + return destBuffer; + } + + public override int Read(byte[] buffer, int offset, int count) + { + byte[] dataRead; + + if (Position + count > Length) + throw new IOException("Desired range out-of-bounds for stream"); + else if (offset + count > buffer.Length) + throw new IOException("Target buffer to small to hold read data"); + + else if (_xvdFile.Header.Type == XvdType.Fixed) + dataRead = InternalRead(count); + else if (_xvdFile.Header.Type == XvdType.Dynamic) + dataRead = ReadDynamic(count); + + else + throw new IOException($"Unsupported XvdType: {_xvdFile.Header.Type}"); + + Array.Copy(dataRead, 0, buffer, offset, count); + return dataRead.Length; + } + + public override long Seek(long offset, SeekOrigin origin) + { + switch (origin) + { + case SeekOrigin.Begin: + Position = offset; + break; + case SeekOrigin.Current: + Position += offset; + break; + case SeekOrigin.End: + Position = Length + offset; + break; + } + return Position; + } + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/LibXboxOne/XVD/XvdMath.cs b/LibXboxOne/XVD/XvdMath.cs new file mode 100644 index 0000000..0639314 --- /dev/null +++ b/LibXboxOne/XVD/XvdMath.cs @@ -0,0 +1,188 @@ +using System; + +namespace LibXboxOne +{ + public static class XvdMath + { + public static bool PagesAligned(ulong page) + { + return (page & (XvdFile.PAGE_SIZE - 1)) == 0; + } + + public static ulong PageAlign(ulong offset) + { + return offset & 0xFFFFFFFFFFFFF000; + } + + public static ulong InBlockOffset(ulong offset) + { + return offset - ((offset / XvdFile.BLOCK_SIZE) * XvdFile.BLOCK_SIZE); + } + + public static ulong InPageOffset(ulong offset) + { + return offset & (XvdFile.PAGE_SIZE - 1); + } + + public static ulong BlockNumberToOffset(ulong blockNumber) + { + return blockNumber * XvdFile.BLOCK_SIZE; + } + + public static ulong PageNumberToOffset(ulong pageNumber) + { + return pageNumber * XvdFile.PAGE_SIZE; + } + + public static ulong BytesToBlocks(ulong bytes) + { + return (bytes + XvdFile.BLOCK_SIZE - 1) / XvdFile.BLOCK_SIZE; + } + + public static ulong PagesToBlocks(ulong pages) + { + return (pages + XvdFile.PAGES_PER_BLOCK - 1) / XvdFile.PAGES_PER_BLOCK; + } + + public static ulong BytesToPages(ulong bytes) + { + return (bytes + XvdFile.PAGE_SIZE - 1) / XvdFile.PAGE_SIZE; + } + + public static ulong OffsetToBlockNumber(ulong offset) + { + return offset / XvdFile.BLOCK_SIZE; + } + + public static ulong OffsetToPageNumber(ulong offset) + { + return offset / XvdFile.PAGE_SIZE; + } + + public static ulong SectorsToBytes(ulong sectors) + { + return sectors * XvdFile.SECTOR_SIZE; + } + + public static ulong LegacySectorsToBytes(ulong sectors) + { + return sectors * XvdFile.LEGACY_SECTOR_SIZE; + } + + public static ulong ComputePagesSpanned(ulong startOffset, ulong lengthBytes) + { + return OffsetToPageNumber(startOffset + lengthBytes - 1) - + OffsetToPageNumber(lengthBytes) + 1; + } + + public static ulong QueryFirstDynamicPage(ulong metaDataPagesCount) + { + return XvdFile.PAGES_PER_BLOCK * PagesToBlocks(metaDataPagesCount); + } + + public static ulong ComputeDataBackingPageNumber(XvdType type, ulong numHashLevels, ulong hashPageCount, ulong dataPageNumber) + { + if (type > XvdType.Dynamic) // Invalid Xvd Type! + return dataPageNumber; + + return dataPageNumber + hashPageCount; + } + + public static ulong CalculateHashBlockNumForBlockNum(XvdType type, ulong hashTreeLevels, ulong numberOfHashedPages, + ulong blockNum, uint hashLevel, out ulong entryNumInBlock, + bool resilient=false, bool unknown=false) + { + ulong HashBlockExponent(ulong blockCount) + { + return (ulong)Math.Pow(0xAA, blockCount); + } + + ulong result = 0xFFFF; + entryNumInBlock = 0; + + if ((uint)type > 1 || hashLevel > 3) + return result; // Invalid data + + if (hashLevel == 0) + entryNumInBlock = blockNum % 0xAA; + else + entryNumInBlock = blockNum / HashBlockExponent(hashLevel) % 0xAA; + + if (hashLevel == 3) + return 0; + + result = blockNum / HashBlockExponent(hashLevel + 1); + hashTreeLevels -= hashLevel + 1; + + if (hashLevel == 0 && hashTreeLevels > 0) + { + result += (numberOfHashedPages + HashBlockExponent(2) - 1) / HashBlockExponent(2); + hashTreeLevels--; + } + + if ((hashLevel == 0 || hashLevel == 1) && hashTreeLevels > 0) + { + result += (numberOfHashedPages + HashBlockExponent(3) - 1) / HashBlockExponent(3); + hashTreeLevels--; + } + + if (hashTreeLevels > 0) + result += (numberOfHashedPages + HashBlockExponent(4) - 1) / HashBlockExponent(4); + + if (resilient) + result *= 2; + if (unknown) + result++; + + return result; + } + + public static ulong CalculateNumHashBlocksInLevel(ulong size, ulong hashLevel, bool resilient) + { + ulong hashBlocks = 0; + + switch (hashLevel) + { + case 0: + hashBlocks = (size + XvdFile.DATA_BLOCKS_IN_LEVEL0_HASHTREE - 1) / XvdFile.DATA_BLOCKS_IN_LEVEL0_HASHTREE; + break; + case 1: + hashBlocks = (size + XvdFile.DATA_BLOCKS_IN_LEVEL1_HASHTREE - 1) / XvdFile.DATA_BLOCKS_IN_LEVEL1_HASHTREE; + break; + case 2: + hashBlocks = (size + XvdFile.DATA_BLOCKS_IN_LEVEL2_HASHTREE - 1) / XvdFile.DATA_BLOCKS_IN_LEVEL2_HASHTREE; + break; + case 3: + hashBlocks = (size + XvdFile.DATA_BLOCKS_IN_LEVEL3_HASHTREE - 1) / XvdFile.DATA_BLOCKS_IN_LEVEL3_HASHTREE; + break; + } + + if (resilient) + hashBlocks *= 2; + + return hashBlocks; + } + + public static ulong CalculateNumberHashPages(out ulong hashTreeLevels, ulong hashedPagesCount, bool resilient) + { + ulong hashTreePageCount = (hashedPagesCount + XvdFile.HASH_ENTRIES_IN_PAGE - 1) / XvdFile.HASH_ENTRIES_IN_PAGE; + hashTreeLevels = 1; + + if (hashTreePageCount > 1) + { + ulong result = 2; + while (result > 1) + { + result = CalculateNumHashBlocksInLevel(hashedPagesCount, hashTreeLevels, false); + hashTreeLevels += 1; + hashTreePageCount += result; + } + } + + if (resilient) + hashTreePageCount *= 2; + + return hashTreePageCount; + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..8c9cbef --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# XvdTool.Streaming + +Rewritten and optimized version of XVDTool that lets you view information and extract files from streamed (remote, by URL) XVC/XVD files. +Also allows for very fast extraction/decryption/hash-verification of local XVC/XVD files. + +Commands supported for both local and streamed types: +- `info` + - Lets you view detailed information (headers, regions, segments, files) for a given file. +- `extract` + - Lets you extract and decrypted the embedded files contained within a XVC. + *Note: Only supports the newer type of XVC which do not just contain a disk partition. (SegmentMetadata.bin)* + +Commands only supported by local files: +- `verify` + - Validates the embedded hashes to check for any corruption. +- `decrypt` + - Decrypts the file contents. + +Some speed estimates on an NVMe drive: +- File extraction from local file (Hash Check enabled): ~200MB/s +- File extraction from local file (Hash Check disbaled): ~800MB/s +- Local file decryption: ~1GB/s + +Please note that you still need to acquire the respective CIK for a package before you are able to extract or decrypt it. +For further information on that, check out [CikExtractor](https://github.com/LukeFZ/CikExtractor). + +For further information about XVC/XVD files in general, check out the original [XVDTool repository](https://github.com/emoose/xvdtool). + +Thanks to [emoose](https://github.com/emoose), [tuxuser](https://github.com/tuxuser) & contributors for developing the original [XVDTool](https://github.com/emoose/xvdtool). + +### Usage +``` +USAGE: + XvdTool.Streaming.exe [OPTIONS] + +EXAMPLES: + XvdTool.Streaming.exe info c:/file.msixvc + XvdTool.Streaming.exe info c:/file.msixvc -o log.txt + XvdTool.Streaming.exe info https://assets1.xboxlive.com/... + XvdTool.Streaming.exe extract c:/file.msixvc + XvdTool.Streaming.exe extract c:/file.msixvc -o c:/output + +OPTIONS: + -h, --help Prints help information + -v, --version Prints version information + +COMMANDS: + info Prints information about a given file + extract Decrypts and extracts the files contained in a given file + verify Checks the integrity of the given file. (Local only) + decrypt Decrypts the given file. (Local only) +``` + +### Third party libraries used +- LibXboxOne (modified, from [regular XVDTool](https://github.com/emoose/xvdtool)): + * [DiscUtils](https://github.com/DiscUtils/DiscUtils) +- This tool: + * [BouncyCastle](https://bouncycastle.org) + * [DotNext](https://github.com/dotnet/dotnext) + * [Spectre.Console](https://spectreconsole.net) + * Slightly modified fast AES-XTS Implementation from [Thealexbarney's](https://github.com/thealexbarney) [LibHac](https://github.com/thealexbarney/libhac) diff --git a/XVDTool.sln b/XVDTool.sln new file mode 100644 index 0000000..942574a --- /dev/null +++ b/XVDTool.sln @@ -0,0 +1,52 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.33913.275 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibXboxOne", "LibXboxOne\LibXboxOne.csproj", "{7C009949-7098-42DB-9F0B-9A0BA68EED92}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibXboxOne.Tests", "LibXboxOne.Tests\LibXboxOne.Tests.csproj", "{960C5852-85E7-49BA-AF9E-818DF1C29F9F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XvdTool.Streaming", "XvdTool.Streaming\XvdTool.Streaming.csproj", "{E48A9A51-3340-436D-AA7B-B5988D95B42E}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GitHub Workflows", "GitHub Workflows", "{045E5C0E-B232-46ED-B075-CF1BE3EB9805}" + ProjectSection(SolutionItems) = preProject + .github\workflows\build.yml = .github\workflows\build.yml + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7C009949-7098-42DB-9F0B-9A0BA68EED92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C009949-7098-42DB-9F0B-9A0BA68EED92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C009949-7098-42DB-9F0B-9A0BA68EED92}.Debug|x64.ActiveCfg = Debug|Any CPU + {7C009949-7098-42DB-9F0B-9A0BA68EED92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C009949-7098-42DB-9F0B-9A0BA68EED92}.Release|Any CPU.Build.0 = Release|Any CPU + {7C009949-7098-42DB-9F0B-9A0BA68EED92}.Release|x64.ActiveCfg = Release|Any CPU + {960C5852-85E7-49BA-AF9E-818DF1C29F9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {960C5852-85E7-49BA-AF9E-818DF1C29F9F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {960C5852-85E7-49BA-AF9E-818DF1C29F9F}.Debug|x64.ActiveCfg = Debug|Any CPU + {960C5852-85E7-49BA-AF9E-818DF1C29F9F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {960C5852-85E7-49BA-AF9E-818DF1C29F9F}.Release|Any CPU.Build.0 = Release|Any CPU + {960C5852-85E7-49BA-AF9E-818DF1C29F9F}.Release|x64.ActiveCfg = Release|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Debug|x64.ActiveCfg = Debug|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Debug|x64.Build.0 = Debug|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Release|Any CPU.Build.0 = Release|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Release|x64.ActiveCfg = Release|Any CPU + {E48A9A51-3340-436D-AA7B-B5988D95B42E}.Release|x64.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F2B36754-DA0C-45C6-A402-0DF8DE43E993} + EndGlobalSection +EndGlobal diff --git a/XvdTool.Streaming/ConsoleLogger.cs b/XvdTool.Streaming/ConsoleLogger.cs new file mode 100644 index 0000000..3877912 --- /dev/null +++ b/XvdTool.Streaming/ConsoleLogger.cs @@ -0,0 +1,21 @@ +using Spectre.Console; + +namespace XvdTool.Streaming; + +public static class ConsoleLogger +{ + public static void WriteInfoLine(string line) + { + AnsiConsole.MarkupLine($"[white]INFO:[/] {line}"); + } + + public static void WriteWarnLine(string line) + { + AnsiConsole.MarkupLine($"[orange1]WARN:[/] {line}"); + } + + public static void WriteErrLine(string line) + { + AnsiConsole.MarkupLine($"[red]ERR:[/] {line}"); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/Crypto/AesCoreNi.cs b/XvdTool.Streaming/Crypto/AesCoreNi.cs new file mode 100644 index 0000000..a756ea4 --- /dev/null +++ b/XvdTool.Streaming/Crypto/AesCoreNi.cs @@ -0,0 +1,584 @@ +// source: https://github.com/Thealexbarney/LibHac/blob/master/src/LibHac/Crypto/Impl/AesCoreNi.cs + +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; +using AesNi = System.Runtime.Intrinsics.X86.Aes; + +namespace XvdTool.Streaming.Crypto; + +[StructLayout(LayoutKind.Sequential, Size = RoundKeyCount * RoundKeySize)] +public struct AesCoreNi +{ + private const int RoundKeyCount = 11; + private const int RoundKeySize = 0x10; + + private Vector128 _roundKeys; + + // An Initialize method is used instead of a constructor because it prevents the runtime + // from zeroing out the structure's memory when creating it. + // When processing a single block, doing this can increase performance by 20-40% + // depending on the context. + public void Initialize(ReadOnlySpan key, bool isDecrypting) + { + Debug.Assert(key.Length == 16); + + KeyExpansion(key, isDecrypting); + } + + public readonly ReadOnlySpan> RoundKeys => + MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in _roundKeys), RoundKeyCount); + + public readonly void Encrypt(ReadOnlySpan input, Span output) + { + int blockCount = Math.Min(input.Length, output.Length) >> 4; + + ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); + ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); + + for (int i = 0; i < blockCount; i++) + { + outBlock = EncryptBlock(inBlock); + + inBlock = ref Unsafe.Add(ref inBlock, 1); + outBlock = ref Unsafe.Add(ref outBlock, 1); + } + } + + public readonly void Decrypt(ReadOnlySpan input, Span output) + { + int blockCount = Math.Min(input.Length, output.Length) >> 4; + + ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); + ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); + + for (int i = 0; i < blockCount; i++) + { + outBlock = DecryptBlock(inBlock); + + inBlock = ref Unsafe.Add(ref inBlock, 1); + outBlock = ref Unsafe.Add(ref outBlock, 1); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Vector128 EncryptBlock(Vector128 input) + { + ReadOnlySpan> keys = RoundKeys; + + Vector128 b = Sse2.Xor(input, keys[0]); + b = AesNi.Encrypt(b, keys[1]); + b = AesNi.Encrypt(b, keys[2]); + b = AesNi.Encrypt(b, keys[3]); + b = AesNi.Encrypt(b, keys[4]); + b = AesNi.Encrypt(b, keys[5]); + b = AesNi.Encrypt(b, keys[6]); + b = AesNi.Encrypt(b, keys[7]); + b = AesNi.Encrypt(b, keys[8]); + b = AesNi.Encrypt(b, keys[9]); + return AesNi.EncryptLast(b, keys[10]); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly Vector128 DecryptBlock(Vector128 input) + { + ReadOnlySpan> keys = RoundKeys; + + Vector128 b = Sse2.Xor(input, keys[10]); + b = AesNi.Decrypt(b, keys[9]); + b = AesNi.Decrypt(b, keys[8]); + b = AesNi.Decrypt(b, keys[7]); + b = AesNi.Decrypt(b, keys[6]); + b = AesNi.Decrypt(b, keys[5]); + b = AesNi.Decrypt(b, keys[4]); + b = AesNi.Decrypt(b, keys[3]); + b = AesNi.Decrypt(b, keys[2]); + b = AesNi.Decrypt(b, keys[1]); + return AesNi.DecryptLast(b, keys[0]); + } + + public readonly int EncryptInterleaved8(ReadOnlySpan input, Span output) + { + int remainingBlocks = Math.Min(input.Length, output.Length) >> 4; + int length = remainingBlocks << 4; + + ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); + ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); + + while (remainingBlocks > 7) + { + EncryptBlocks8( + Unsafe.Add(ref inBlock, 0), + Unsafe.Add(ref inBlock, 1), + Unsafe.Add(ref inBlock, 2), + Unsafe.Add(ref inBlock, 3), + Unsafe.Add(ref inBlock, 4), + Unsafe.Add(ref inBlock, 5), + Unsafe.Add(ref inBlock, 6), + Unsafe.Add(ref inBlock, 7), + out Unsafe.Add(ref outBlock, 0), + out Unsafe.Add(ref outBlock, 1), + out Unsafe.Add(ref outBlock, 2), + out Unsafe.Add(ref outBlock, 3), + out Unsafe.Add(ref outBlock, 4), + out Unsafe.Add(ref outBlock, 5), + out Unsafe.Add(ref outBlock, 6), + out Unsafe.Add(ref outBlock, 7)); + + inBlock = ref Unsafe.Add(ref inBlock, 8); + outBlock = ref Unsafe.Add(ref outBlock, 8); + remainingBlocks -= 8; + } + + while (remainingBlocks > 0) + { + outBlock = EncryptBlock(inBlock); + + inBlock = ref Unsafe.Add(ref inBlock, 1); + outBlock = ref Unsafe.Add(ref outBlock, 1); + remainingBlocks -= 1; + } + + return length; + } + + public readonly int DecryptInterleaved8(ReadOnlySpan input, Span output) + { + int remainingBlocks = Math.Min(input.Length, output.Length) >> 4; + int length = remainingBlocks << 4; + + ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); + ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); + + while (remainingBlocks > 7) + { + DecryptBlocks8( + Unsafe.Add(ref inBlock, 0), + Unsafe.Add(ref inBlock, 1), + Unsafe.Add(ref inBlock, 2), + Unsafe.Add(ref inBlock, 3), + Unsafe.Add(ref inBlock, 4), + Unsafe.Add(ref inBlock, 5), + Unsafe.Add(ref inBlock, 6), + Unsafe.Add(ref inBlock, 7), + out Unsafe.Add(ref outBlock, 0), + out Unsafe.Add(ref outBlock, 1), + out Unsafe.Add(ref outBlock, 2), + out Unsafe.Add(ref outBlock, 3), + out Unsafe.Add(ref outBlock, 4), + out Unsafe.Add(ref outBlock, 5), + out Unsafe.Add(ref outBlock, 6), + out Unsafe.Add(ref outBlock, 7)); + + inBlock = ref Unsafe.Add(ref inBlock, 8); + outBlock = ref Unsafe.Add(ref outBlock, 8); + remainingBlocks -= 8; + } + + while (remainingBlocks > 0) + { + outBlock = DecryptBlock(inBlock); + + inBlock = ref Unsafe.Add(ref inBlock, 1); + outBlock = ref Unsafe.Add(ref outBlock, 1); + remainingBlocks -= 1; + } + + return length; + } + + // When inlining this function, RyuJIT will almost make the + // generated code the same as if it were manually inlined + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void EncryptBlocks8( + Vector128 in0, + Vector128 in1, + Vector128 in2, + Vector128 in3, + Vector128 in4, + Vector128 in5, + Vector128 in6, + Vector128 in7, + out Vector128 out0, + out Vector128 out1, + out Vector128 out2, + out Vector128 out3, + out Vector128 out4, + out Vector128 out5, + out Vector128 out6, + out Vector128 out7) + { + ReadOnlySpan> keys = RoundKeys; + + Vector128 key = keys[0]; + Vector128 b0 = Sse2.Xor(in0, key); + Vector128 b1 = Sse2.Xor(in1, key); + Vector128 b2 = Sse2.Xor(in2, key); + Vector128 b3 = Sse2.Xor(in3, key); + Vector128 b4 = Sse2.Xor(in4, key); + Vector128 b5 = Sse2.Xor(in5, key); + Vector128 b6 = Sse2.Xor(in6, key); + Vector128 b7 = Sse2.Xor(in7, key); + + key = keys[1]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[2]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[3]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[4]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[5]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[6]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[7]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[8]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[9]; + b0 = AesNi.Encrypt(b0, key); + b1 = AesNi.Encrypt(b1, key); + b2 = AesNi.Encrypt(b2, key); + b3 = AesNi.Encrypt(b3, key); + b4 = AesNi.Encrypt(b4, key); + b5 = AesNi.Encrypt(b5, key); + b6 = AesNi.Encrypt(b6, key); + b7 = AesNi.Encrypt(b7, key); + + key = keys[10]; + out0 = AesNi.EncryptLast(b0, key); + out1 = AesNi.EncryptLast(b1, key); + out2 = AesNi.EncryptLast(b2, key); + out3 = AesNi.EncryptLast(b3, key); + out4 = AesNi.EncryptLast(b4, key); + out5 = AesNi.EncryptLast(b5, key); + out6 = AesNi.EncryptLast(b6, key); + out7 = AesNi.EncryptLast(b7, key); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly void DecryptBlocks8( + Vector128 in0, + Vector128 in1, + Vector128 in2, + Vector128 in3, + Vector128 in4, + Vector128 in5, + Vector128 in6, + Vector128 in7, + out Vector128 out0, + out Vector128 out1, + out Vector128 out2, + out Vector128 out3, + out Vector128 out4, + out Vector128 out5, + out Vector128 out6, + out Vector128 out7) + { + ReadOnlySpan> keys = RoundKeys; + + Vector128 key = keys[10]; + Vector128 b0 = Sse2.Xor(in0, key); + Vector128 b1 = Sse2.Xor(in1, key); + Vector128 b2 = Sse2.Xor(in2, key); + Vector128 b3 = Sse2.Xor(in3, key); + Vector128 b4 = Sse2.Xor(in4, key); + Vector128 b5 = Sse2.Xor(in5, key); + Vector128 b6 = Sse2.Xor(in6, key); + Vector128 b7 = Sse2.Xor(in7, key); + + key = keys[9]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[8]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[7]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[6]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[5]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[4]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[3]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[2]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[1]; + b0 = AesNi.Decrypt(b0, key); + b1 = AesNi.Decrypt(b1, key); + b2 = AesNi.Decrypt(b2, key); + b3 = AesNi.Decrypt(b3, key); + b4 = AesNi.Decrypt(b4, key); + b5 = AesNi.Decrypt(b5, key); + b6 = AesNi.Decrypt(b6, key); + b7 = AesNi.Decrypt(b7, key); + + key = keys[0]; + out0 = AesNi.DecryptLast(b0, key); + out1 = AesNi.DecryptLast(b1, key); + out2 = AesNi.DecryptLast(b2, key); + out3 = AesNi.DecryptLast(b3, key); + out4 = AesNi.DecryptLast(b4, key); + out5 = AesNi.DecryptLast(b5, key); + out6 = AesNi.DecryptLast(b6, key); + out7 = AesNi.DecryptLast(b7, key); + } + + public static Vector128 EncryptBlock(Vector128 input, Vector128 key) + { + Vector128 curKey = key; + Vector128 b = Sse2.Xor(input, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x01)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x02)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x04)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x08)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x10)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x20)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x40)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x80)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x1b)); + b = AesNi.Encrypt(b, curKey); + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x36)); + return AesNi.EncryptLast(b, curKey); + } + + public static Vector128 DecryptBlock(Vector128 input, Vector128 key) + { + Vector128 key0 = key; + Vector128 key1 = KeyExpansion(key0, AesNi.KeygenAssist(key0, 0x01)); + Vector128 key2 = KeyExpansion(key1, AesNi.KeygenAssist(key1, 0x02)); + Vector128 key3 = KeyExpansion(key2, AesNi.KeygenAssist(key2, 0x04)); + Vector128 key4 = KeyExpansion(key3, AesNi.KeygenAssist(key3, 0x08)); + Vector128 key5 = KeyExpansion(key4, AesNi.KeygenAssist(key4, 0x10)); + Vector128 key6 = KeyExpansion(key5, AesNi.KeygenAssist(key5, 0x20)); + Vector128 key7 = KeyExpansion(key6, AesNi.KeygenAssist(key6, 0x40)); + Vector128 key8 = KeyExpansion(key7, AesNi.KeygenAssist(key7, 0x80)); + Vector128 key9 = KeyExpansion(key8, AesNi.KeygenAssist(key8, 0x1b)); + Vector128 key10 = KeyExpansion(key9, AesNi.KeygenAssist(key9, 0x36)); + + Vector128 b = input; + + b = Sse2.Xor(b, key10); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key9)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key8)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key7)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key6)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key5)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key4)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key3)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key2)); + b = AesNi.Decrypt(b, AesNi.InverseMixColumns(key1)); + return AesNi.DecryptLast(b, key0); + } + + private void KeyExpansion(ReadOnlySpan key, bool isDecrypting) + { + Span> roundKeys = MemoryMarshal.CreateSpan(ref _roundKeys, RoundKeyCount); + + var curKey = Unsafe.ReadUnaligned>(ref MemoryMarshal.GetReference(key)); + roundKeys[0] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x01)); + roundKeys[1] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x02)); + roundKeys[2] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x04)); + roundKeys[3] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x08)); + roundKeys[4] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x10)); + roundKeys[5] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x20)); + roundKeys[6] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x40)); + roundKeys[7] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x80)); + roundKeys[8] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x1b)); + roundKeys[9] = curKey; + + curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x36)); + roundKeys[10] = curKey; + + if (isDecrypting) + { + roundKeys[1] = AesNi.InverseMixColumns(roundKeys[1]); + roundKeys[2] = AesNi.InverseMixColumns(roundKeys[2]); + roundKeys[3] = AesNi.InverseMixColumns(roundKeys[3]); + roundKeys[4] = AesNi.InverseMixColumns(roundKeys[4]); + roundKeys[5] = AesNi.InverseMixColumns(roundKeys[5]); + roundKeys[6] = AesNi.InverseMixColumns(roundKeys[6]); + roundKeys[7] = AesNi.InverseMixColumns(roundKeys[7]); + roundKeys[8] = AesNi.InverseMixColumns(roundKeys[8]); + roundKeys[9] = AesNi.InverseMixColumns(roundKeys[9]); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128 KeyExpansion(Vector128 s, Vector128 t) + { + t = Sse2.Shuffle(t.AsUInt32(), 0xFF).AsByte(); + + s = Sse2.Xor(s, Sse2.ShiftLeftLogical128BitLane(s, 4)); + s = Sse2.Xor(s, Sse2.ShiftLeftLogical128BitLane(s, 8)); + + return Sse2.Xor(s, t); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/Crypto/AesXtsCipherNi.cs b/XvdTool.Streaming/Crypto/AesXtsCipherNi.cs new file mode 100644 index 0000000..f059d18 --- /dev/null +++ b/XvdTool.Streaming/Crypto/AesXtsCipherNi.cs @@ -0,0 +1,263 @@ +// source: https://github.com/Thealexbarney/LibHac/blob/master/src/LibHac/Crypto/Impl/AesXtsCipherNi.cs +// Only changes are making .Decrypt take the iv instead of the constructor + +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; + +namespace XvdTool.Streaming.Crypto; + +public struct AesXtsCipherNi +{ + private AesCoreNi _dataAesCore; + private AesCoreNi _tweakAesCore; + + public void Initialize(ReadOnlySpan key1, ReadOnlySpan key2, bool decrypting) + { + _dataAesCore.Initialize(key1, decrypting); + _tweakAesCore.Initialize(key2, false); + } + + public int Encrypt(ReadOnlySpan input, Span output, ReadOnlySpan tweakIv) + { + Debug.Assert(tweakIv.Length == 16); + + var iv = Unsafe.ReadUnaligned>(ref MemoryMarshal.GetReference(tweakIv)); + + int length = Math.Min(input.Length, output.Length); + int remainingBlocks = length >> 4; + int leftover = length & 0xF; + + Debug.Assert(remainingBlocks > 0); + + ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); + ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); + + Vector128 mask = Vector128.Create(0x87, 1).AsByte(); + + Vector128 tweak = _tweakAesCore.EncryptBlock(iv); + + while (remainingBlocks > 7) + { + Vector128 b0 = Sse2.Xor(tweak, Unsafe.Add(ref inBlock, 0)); + + Vector128 tweak1 = Gf128Mul(tweak, mask); + Vector128 b1 = Sse2.Xor(tweak1, Unsafe.Add(ref inBlock, 1)); + + Vector128 tweak2 = Gf128Mul(tweak1, mask); + Vector128 b2 = Sse2.Xor(tweak2, Unsafe.Add(ref inBlock, 2)); + + Vector128 tweak3 = Gf128Mul(tweak2, mask); + Vector128 b3 = Sse2.Xor(tweak3, Unsafe.Add(ref inBlock, 3)); + + Vector128 tweak4 = Gf128Mul(tweak3, mask); + Vector128 b4 = Sse2.Xor(tweak4, Unsafe.Add(ref inBlock, 4)); + + Vector128 tweak5 = Gf128Mul(tweak4, mask); + Vector128 b5 = Sse2.Xor(tweak5, Unsafe.Add(ref inBlock, 5)); + + Vector128 tweak6 = Gf128Mul(tweak5, mask); + Vector128 b6 = Sse2.Xor(tweak6, Unsafe.Add(ref inBlock, 6)); + + Vector128 tweak7 = Gf128Mul(tweak6, mask); + Vector128 b7 = Sse2.Xor(tweak7, Unsafe.Add(ref inBlock, 7)); + + _dataAesCore.EncryptBlocks8(b0, b1, b2, b3, b4, b5, b6, b7, + out b0, out b1, out b2, out b3, out b4, out b5, out b6, out b7); + + Unsafe.Add(ref outBlock, 0) = Sse2.Xor(tweak, b0); + Unsafe.Add(ref outBlock, 1) = Sse2.Xor(tweak1, b1); + Unsafe.Add(ref outBlock, 2) = Sse2.Xor(tweak2, b2); + Unsafe.Add(ref outBlock, 3) = Sse2.Xor(tweak3, b3); + Unsafe.Add(ref outBlock, 4) = Sse2.Xor(tweak4, b4); + Unsafe.Add(ref outBlock, 5) = Sse2.Xor(tweak5, b5); + Unsafe.Add(ref outBlock, 6) = Sse2.Xor(tweak6, b6); + Unsafe.Add(ref outBlock, 7) = Sse2.Xor(tweak7, b7); + + tweak = Gf128Mul(tweak7, mask); + + inBlock = ref Unsafe.Add(ref inBlock, 8); + outBlock = ref Unsafe.Add(ref outBlock, 8); + remainingBlocks -= 8; + } + + while (remainingBlocks > 0) + { + Vector128 tmp = Sse2.Xor(inBlock, tweak); + tmp = _dataAesCore.EncryptBlock(tmp); + outBlock = Sse2.Xor(tmp, tweak); + + tweak = Gf128Mul(tweak, mask); + + inBlock = ref Unsafe.Add(ref inBlock, 1); + outBlock = ref Unsafe.Add(ref outBlock, 1); + remainingBlocks--; + } + + if (leftover != 0) + { + EncryptPartialFinalBlock(ref inBlock, ref outBlock, tweak, leftover); + } + + return length; + } + + public int Decrypt(ReadOnlySpan input, Span output, ReadOnlySpan tweakIv) + { + Debug.Assert(tweakIv.Length == 16); + + var iv = Unsafe.ReadUnaligned>(ref MemoryMarshal.GetReference(tweakIv)); + + int length = Math.Min(input.Length, output.Length); + int remainingBlocks = length >> 4; + int leftover = length & 0xF; + + Debug.Assert(remainingBlocks > 0); + + if (leftover != 0) remainingBlocks--; + + ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); + ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); + + Vector128 mask = Vector128.Create(0x87, 1).AsByte(); + + Vector128 tweak = _tweakAesCore.EncryptBlock(iv); + + while (remainingBlocks > 7) + { + Vector128 b0 = Sse2.Xor(tweak, Unsafe.Add(ref inBlock, 0)); + + Vector128 tweak1 = Gf128Mul(tweak, mask); + Vector128 b1 = Sse2.Xor(tweak1, Unsafe.Add(ref inBlock, 1)); + + Vector128 tweak2 = Gf128Mul(tweak1, mask); + Vector128 b2 = Sse2.Xor(tweak2, Unsafe.Add(ref inBlock, 2)); + + Vector128 tweak3 = Gf128Mul(tweak2, mask); + Vector128 b3 = Sse2.Xor(tweak3, Unsafe.Add(ref inBlock, 3)); + + Vector128 tweak4 = Gf128Mul(tweak3, mask); + Vector128 b4 = Sse2.Xor(tweak4, Unsafe.Add(ref inBlock, 4)); + + Vector128 tweak5 = Gf128Mul(tweak4, mask); + Vector128 b5 = Sse2.Xor(tweak5, Unsafe.Add(ref inBlock, 5)); + + Vector128 tweak6 = Gf128Mul(tweak5, mask); + Vector128 b6 = Sse2.Xor(tweak6, Unsafe.Add(ref inBlock, 6)); + + Vector128 tweak7 = Gf128Mul(tweak6, mask); + Vector128 b7 = Sse2.Xor(tweak7, Unsafe.Add(ref inBlock, 7)); + + _dataAesCore.DecryptBlocks8(b0, b1, b2, b3, b4, b5, b6, b7, + out b0, out b1, out b2, out b3, out b4, out b5, out b6, out b7); + + Unsafe.Add(ref outBlock, 0) = Sse2.Xor(tweak, b0); + Unsafe.Add(ref outBlock, 1) = Sse2.Xor(tweak1, b1); + Unsafe.Add(ref outBlock, 2) = Sse2.Xor(tweak2, b2); + Unsafe.Add(ref outBlock, 3) = Sse2.Xor(tweak3, b3); + Unsafe.Add(ref outBlock, 4) = Sse2.Xor(tweak4, b4); + Unsafe.Add(ref outBlock, 5) = Sse2.Xor(tweak5, b5); + Unsafe.Add(ref outBlock, 6) = Sse2.Xor(tweak6, b6); + Unsafe.Add(ref outBlock, 7) = Sse2.Xor(tweak7, b7); + + tweak = Gf128Mul(tweak7, mask); + + inBlock = ref Unsafe.Add(ref inBlock, 8); + outBlock = ref Unsafe.Add(ref outBlock, 8); + remainingBlocks -= 8; + } + + while (remainingBlocks > 0) + { + Vector128 tmp = Sse2.Xor(inBlock, tweak); + tmp = _dataAesCore.DecryptBlock(tmp); + outBlock = Sse2.Xor(tmp, tweak); + + tweak = Gf128Mul(tweak, mask); + + inBlock = ref Unsafe.Add(ref inBlock, 1); + outBlock = ref Unsafe.Add(ref outBlock, 1); + remainingBlocks--; + } + + if (leftover != 0) + { + Debug.Assert(false, "We should never attempt to decrypt a non 0x1000 aligned block"); + + DecryptPartialFinalBlock(ref inBlock, ref outBlock, tweak, mask, leftover); + } + + return length; + } + + // ReSharper disable once RedundantAssignment + private void DecryptPartialFinalBlock(ref Vector128 input, ref Vector128 output, + Vector128 tweak, Vector128 mask, int finalBlockLength) + { + Vector128 finalTweak = Gf128Mul(tweak, mask); + + Vector128 tmp = Sse2.Xor(input, finalTweak); + tmp = _dataAesCore.DecryptBlock(tmp); + output = Sse2.Xor(tmp, finalTweak); + + var x = new Buffer16(); + ref Buffer16 outBuf = ref Unsafe.As, Buffer16>(ref output); + Buffer16 nextInBuf = Unsafe.As, Buffer16>(ref Unsafe.Add(ref input, 1)); + ref Buffer16 nextOutBuf = ref Unsafe.As, Buffer16>(ref Unsafe.Add(ref output, 1)); + + for (int i = 0; i < finalBlockLength; i++) + { + nextOutBuf[i] = outBuf[i]; + x[i] = nextInBuf[i]; + } + + for (int i = finalBlockLength; i < 16; i++) + { + x[i] = outBuf[i]; + } + + tmp = Sse2.Xor(x.As>(), tweak); + tmp = _dataAesCore.DecryptBlock(tmp); + output = Sse2.Xor(tmp, tweak); + } + + private void EncryptPartialFinalBlock(ref Vector128 input, ref Vector128 output, + Vector128 tweak, int finalBlockLength) + { + ref Vector128 prevOutBlock = ref Unsafe.Subtract(ref output, 1); + + var x = new Buffer16(); + ref Buffer16 outBuf = ref Unsafe.As, Buffer16>(ref output); + Buffer16 inBuf = Unsafe.As, Buffer16>(ref input); + ref Buffer16 prevOutBuf = ref Unsafe.As, Buffer16>(ref prevOutBlock); + + for (int i = 0; i < finalBlockLength; i++) + { + outBuf[i] = prevOutBuf[i]; + x[i] = inBuf[i]; + } + + for (int i = finalBlockLength; i < 16; i++) + { + x[i] = prevOutBuf[i]; + } + + Vector128 tmp = Sse2.Xor(x.As>(), tweak); + tmp = _dataAesCore.EncryptBlock(tmp); + prevOutBlock = Sse2.Xor(tmp, tweak); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static Vector128 Gf128Mul(Vector128 iv, Vector128 mask) + { + Vector128 tmp1 = Sse2.Add(iv.AsUInt64(), iv.AsUInt64()).AsByte(); + + Vector128 tmp2 = Sse2.Shuffle(iv.AsInt32(), 0x13).AsByte(); + tmp2 = Sse2.ShiftRightArithmetic(tmp2.AsInt32(), 31).AsByte(); + tmp2 = Sse2.And(mask, tmp2); + + return Sse2.Xor(tmp1, tmp2); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/Crypto/AesXtsDecryptorNi.cs b/XvdTool.Streaming/Crypto/AesXtsDecryptorNi.cs new file mode 100644 index 0000000..a78e8ea --- /dev/null +++ b/XvdTool.Streaming/Crypto/AesXtsDecryptorNi.cs @@ -0,0 +1,20 @@ +namespace XvdTool.Streaming.Crypto; + +// Everything in here and /Crypto taken from Thealexbarney's LibHac (https://github.dev/Thealexbarney/LibHac) +// Only changes are making .Decrypt take the iv instead of the constructor + +public class AesXtsDecryptorNi +{ + private AesXtsCipherNi _cipher; + + public AesXtsDecryptorNi(ReadOnlySpan data, ReadOnlySpan tweak) + { + _cipher = new AesXtsCipherNi(); + _cipher.Initialize(data, tweak, true); + } + + public int Transform(ReadOnlySpan input, Span output, ReadOnlySpan tweakIv) + { + return _cipher.Decrypt(input, output, tweakIv); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/Crypto/Buffer16.cs b/XvdTool.Streaming/Crypto/Buffer16.cs new file mode 100644 index 0000000..d6d8c58 --- /dev/null +++ b/XvdTool.Streaming/Crypto/Buffer16.cs @@ -0,0 +1,114 @@ +// source: https://github.com/Thealexbarney/LibHac/blob/master/src/LibHac/Common/Buffer.cs + +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +namespace XvdTool.Streaming.Crypto; + +/// +/// Represents a buffer of 16 bytes. +/// Contains functions that assist with common operations on small buffers. +/// +[StructLayout(LayoutKind.Sequential, Size = 16)] +public struct Buffer16 +{ + [DebuggerBrowsable(DebuggerBrowsableState.Never)] private ulong _dummy0; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] private ulong _dummy1; + + public byte this[int i] + { + get => Bytes[i]; + set => Bytes[i] = value; + } + + [UnscopedRef] public Span Bytes => SpanHelpers.AsByteSpan(ref this); + + // Prevent a defensive copy by changing the read-only in reference to a reference with Unsafe.AsRef() + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator Span(in Buffer16 value) + { + return SpanHelpers.AsByteSpan(ref Unsafe.AsRef(in value)); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator ReadOnlySpan(in Buffer16 value) + { + return SpanHelpers.AsReadOnlyByteSpan(in value); + } + + [UnscopedRef, MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref T As() where T : unmanaged + { + if (Unsafe.SizeOf() > (uint)Unsafe.SizeOf()) + { + throw new ArgumentException(); + } + + return ref MemoryMarshal.GetReference(AsSpan()); + } + + [UnscopedRef, MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span AsSpan() where T : unmanaged + { + return SpanHelpers.AsSpan(ref this); + } + + [UnscopedRef, MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly ReadOnlySpan AsReadOnlySpan() where T : unmanaged + { + return SpanHelpers.AsReadOnlySpan(in this); + } +} + +// source: https://github.com/Thealexbarney/LibHac/blob/master/src/LibHac/Common/SpanHelpers + +file class SpanHelpers +{ + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span CreateSpan(ref T reference, int length) + { + return MemoryMarshal.CreateSpan(ref reference, length); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span AsSpan(ref TStruct reference) + where TStruct : unmanaged where TSpan : unmanaged + { + return CreateSpan(ref Unsafe.As(ref reference), + Unsafe.SizeOf() / Unsafe.SizeOf()); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span AsByteSpan(ref T reference) where T : unmanaged + { + return CreateSpan(ref Unsafe.As(ref reference), Unsafe.SizeOf()); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan CreateReadOnlySpan(in T reference, int length) + { + return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in reference), length); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlySpan(in T reference) where T : unmanaged + { + return new ReadOnlySpan(in reference); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlySpan(in TStruct reference) + where TStruct : unmanaged where TSpan : unmanaged + { + return CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in reference)), + Unsafe.SizeOf() / Unsafe.SizeOf()); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlyByteSpan(in T reference) where T : unmanaged + { + return CreateReadOnlySpan(in Unsafe.As(ref Unsafe.AsRef(in reference)), Unsafe.SizeOf()); + } +} diff --git a/XvdTool.Streaming/Extensions.cs b/XvdTool.Streaming/Extensions.cs new file mode 100644 index 0000000..48b0584 --- /dev/null +++ b/XvdTool.Streaming/Extensions.cs @@ -0,0 +1,41 @@ +using System.Runtime.InteropServices; + +namespace XvdTool.Streaming; + +public static class Extensions +{ + public static T ReadStruct(this BinaryReader reader) + { + var size = Marshal.SizeOf(typeof(T)); + // Read in a byte array + var bytes = reader.ReadBytes(size); + + // Pin the managed memory while, copy it out the data, then unpin it + var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); + var theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T))!; + handle.Free(); + + return theStructure; + } + + public static T[] ReadStructArray(this BinaryReader reader, int count) where T : struct + { + var size = Marshal.SizeOf(typeof(T)); + // Read in a byte array + var bytes = reader.ReadBytes(size * count); + + // Pin the managed memory while, copy it out the data, then unpin it + var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); + var handleAddr = handle.AddrOfPinnedObject(); + + var array = new T[count]; + for (int i = 0; i < count; i++) + { + array[i] = (T) Marshal.PtrToStructure(handleAddr + size * i, typeof(T))!; + } + + handle.Free(); + + return array; + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/HttpFileStream.cs b/XvdTool.Streaming/HttpFileStream.cs new file mode 100644 index 0000000..89e5483 --- /dev/null +++ b/XvdTool.Streaming/HttpFileStream.cs @@ -0,0 +1,189 @@ +using System.Diagnostics; +using System.Net; +using System.Net.Http.Headers; +using System; + +namespace XvdTool.Streaming; + +public sealed class HttpFileStream : Stream +{ + public string Url { get; } + + private readonly HttpClient _httpClient = new(); + private long _contentLength = -1; + + private HttpFileStream(string url) + { + Url = url; + } + + public static HttpFileStream Open(string url) + { + var stream = new HttpFileStream(url); + stream.GetFileLength(); + + return stream; + } + + private void GetFileLength() + { + var request = new HttpRequestMessage(HttpMethod.Get, Url); + request.Headers.Range = new RangeHeaderValue(0, 0); + + var response = _httpClient.Send(request); + response.EnsureSuccessStatusCode(); + + var contentRange = response.Content.Headers.ContentRange; + + if (contentRange == null || contentRange.Unit == "none") + throw new InvalidOperationException("URL does not support 'Range:' header."); + + if (contentRange.Unit != "bytes") + throw new InvalidOperationException( + $"URL supports 'Range:' header but uses invalid unit {contentRange.Unit}."); + + if (!contentRange.HasLength) + throw new InvalidOperationException("URL supports 'Range:' header but did not respond with content length."); + + _contentLength = contentRange.Length!.Value; + } + + private Stream GetRangeStream(int count) + { + var actualLength = Math.Min(Position + count, _contentLength); + + var header = new RangeHeaderValue(Position, actualLength); + +#if DEBUG + Console.WriteLine($"Sending request to read from {Position} to {actualLength} (0x{actualLength - Position:x8})"); +#endif + + var request = new HttpRequestMessage(HttpMethod.Get, Url); + request.Headers.Range = header; + +#if DEBUG + var stop = Stopwatch.StartNew(); +#endif + + var response = _httpClient.Send(request); + response.EnsureSuccessStatusCode(); + +#if DEBUG + stop.Stop(); + Console.WriteLine($"Request took {stop.ElapsedMilliseconds} ms"); +#endif + + Position = actualLength; + + return response.Content.ReadAsStream(); + } + + private async Task GetRangeStreamAsync(int count, CancellationToken cancellationToken) + { + var actualLength = Math.Min(Position + count, _contentLength); + + var header = new RangeHeaderValue(Position, actualLength); + +#if DEBUG + Console.WriteLine($"Sending request to read from {Position} to {actualLength}"); +#endif + + var request = new HttpRequestMessage(HttpMethod.Get, Url); + request.Headers.Range = header; + + var response = await _httpClient.SendAsync(request, cancellationToken); + response.EnsureSuccessStatusCode(); + + Position = actualLength; + + return await response.Content.ReadAsStreamAsync(cancellationToken); + } + + public override int Read(byte[] buffer, int offset, int count) + { + if (Position >= _contentLength) + return 0; + + using var responseStream = GetRangeStream(count); + return responseStream.Read(buffer, offset, count); + } + + public override int Read(Span buffer) + { + if (Position >= _contentLength) + return 0; + + using var responseStream = GetRangeStream(buffer.Length); + return responseStream.Read(buffer); + } + + public override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + if (Position >= _contentLength) + return 0; + + await using var responseStream = await GetRangeStreamAsync(count, cancellationToken); + return await responseStream.ReadAsync(buffer.AsMemory(offset, count), cancellationToken); + } + + public override async ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) + { + if (Position >= _contentLength) + return 0; + + await using var responseStream = await GetRangeStreamAsync(buffer.Length, cancellationToken); + return await responseStream.ReadAsync(buffer, cancellationToken); + } + + public override long Seek(long offset, SeekOrigin origin) + { + var newPosition = origin switch + { + SeekOrigin.Begin => offset, + SeekOrigin.Current => Position + offset, + SeekOrigin.End => _contentLength - offset, + _ => throw new UnreachableException() + }; + + if (newPosition >= _contentLength) + throw new IOException("Cannot seek past file end"); + + Position = newPosition; + return Position; + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _httpClient.Dispose(); + } + + base.Dispose(disposing); + } + + public override bool CanRead => true; + public override bool CanSeek => true; + public override bool CanWrite => false; + public override long Length => _contentLength; + public override long Position { get; set; } + + #region Unimplemented Methods + + public override void SetLength(long value) + { + throw new NotImplementedException(); + } + + public override void Flush() + { + throw new NotImplementedException(); + } + + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotImplementedException(); + } + + #endregion +} \ No newline at end of file diff --git a/XvdTool.Streaming/KeyManager.cs b/XvdTool.Streaming/KeyManager.cs new file mode 100644 index 0000000..6dca605 --- /dev/null +++ b/XvdTool.Streaming/KeyManager.cs @@ -0,0 +1,310 @@ +using System.Diagnostics; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text; +using System.Xml.Linq; +using LibXboxOne; +using LibXboxOne.Keys; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Parameters; +using Spectre.Console; +using XvdTool.Streaming.Crypto; +using Aes = System.Security.Cryptography.Aes; + +namespace XvdTool.Streaming; + +public class KeyManager +{ + private AesWrapEngine? _uwpKeyWrapEngine; + + private readonly Dictionary _keys = new(); + private readonly Dictionary _odks = new(); + + private const string AppName = "xvdtool"; // Use the same name as xvdtool to support the same directories + + private const string OdkDirectory = "Odk"; + private const string CikDirectory = "Cik"; + private const string LicenseDirectory = "License"; + + public void LoadDeviceKey(byte[] deviceKey) + { + _uwpKeyWrapEngine = new AesWrapEngine(); + _uwpKeyWrapEngine.Init(false, new KeyParameter(deviceKey)); + } + + public bool TryGetKey(Guid keyId, out KeyEntry entry) + { + return _keys.TryGetValue(keyId, out entry); + } + + public void LoadCachedKeys() + { + var globalBaseDirectory = AppDirs.GetApplicationConfigDirectory(AppName); + LoadFromDirectory(globalBaseDirectory); + + var currentBaseDirectory = Directory.GetCurrentDirectory(); + LoadFromDirectory(currentBaseDirectory); + } + + private void LoadFromDirectory(string directory) + { + var odkPath = Path.Combine(directory, OdkDirectory); + Directory.CreateDirectory(odkPath); + + foreach (var file in Directory.GetFiles(odkPath, "*.odk", SearchOption.TopDirectoryOnly)) + { + var filename = Path.GetFileNameWithoutExtension(file); + + if (!Enum.TryParse(filename, true, out OdkIndex odkIndex)) + { + if (!int.TryParse(filename, out var odkId)) + { + ConsoleLogger.WriteWarnLine($"Could not get ODK index from file [white]{file}[/]. Skipping."); + continue; + } + + odkIndex = (OdkIndex) odkId; + } + + var odk = File.ReadAllBytes(file); + + if (odk.Length != 0x20) + { + ConsoleLogger.WriteWarnLine($"ODK file [white]{file}[/] was not 0x20 bytes in length, assuming corrupted. Skipping."); + continue; + } + + _odks[odkIndex] = odk; + } + + var cikPath = Path.Combine(directory, CikDirectory); + Directory.CreateDirectory(cikPath); + + foreach (var file in Directory.GetFiles(cikPath, "*.cik", SearchOption.TopDirectoryOnly)) + { + var entry = LoadCik(file); + if (entry.Id != Guid.Empty) + { + _keys[entry.Id] = entry; + } + } + + var licensePath = Path.Combine(directory, LicenseDirectory); + Directory.CreateDirectory(licensePath); + + foreach (var file in Directory.GetFiles(licensePath, "*.*", SearchOption.TopDirectoryOnly)) + { + LoadLicense(file); + } + } + + public KeyEntry LoadCik(string path) + { + var cikContents = File.ReadAllBytes(path).AsSpan(); + if (cikContents.Length != KeyEntry.Size) + { + ConsoleLogger.WriteWarnLine($"CIK file [white]{path}[/] has an unexpected length of [white bold]0x{cikContents.Length}[/], assuming corrupted. Skipping."); + return default; + } + + return new KeyEntry(cikContents); + } + + public void LoadLicense(string path) + { + var xmlLicense = File.ReadAllText(path); + try + { + var document = XDocument.Parse(xmlLicense); + + var uwpSpLicenseBlock = + document.Descendants( + XName.Get("SPLicenseBlock", "urn:schemas-microsoft-com:windows:store:licensing:ls")) + .FirstOrDefault(); + + if (uwpSpLicenseBlock != null) + { + var spLicense = new XvcLicenseBlock(Convert.FromBase64String(uwpSpLicenseBlock.Value)); + + var contentKeyBlock = spLicense.GetBlockWithId(XvcLicenseBlockId.PackedContentKeys); + if (contentKeyBlock != null) + { + if (_uwpKeyWrapEngine != null) + { + using var keyBlockReader = new BinaryReader(new MemoryStream(contentKeyBlock.BlockData)); + + while (keyBlockReader.BaseStream.Position != keyBlockReader.BaseStream.Length) + { + var keyIdSize = keyBlockReader.ReadUInt16(); + Debug.Assert(keyIdSize == 0x20, "keyIdSize == 0x20"); + + var keyEntrySize = keyBlockReader.ReadUInt16(); + Debug.Assert(keyEntrySize == 0x28, "keyEntrySize == 0x28"); + + var keyId1 = new Guid(keyBlockReader.ReadBytes(0x10)); + var keyId2 = new Guid(keyBlockReader.ReadBytes(0x10)); + + var packedKey = keyBlockReader.ReadBytes(0x28); + + var unwrappedKey = _uwpKeyWrapEngine.Unwrap(packedKey, 0, packedKey.Length).AsSpan(); + + _keys[keyId1] = new KeyEntry(keyId1, unwrappedKey); + _keys[keyId2] = new KeyEntry(keyId2, unwrappedKey); + } + } + else + { + ConsoleLogger.WriteWarnLine( + $"SPLicenseBlock from file [white]{path}[/] contained a content key, but no device key was provided for decryption. Skipping."); + } + } + else + { + ConsoleLogger.WriteWarnLine( + $"SPLicenseBlock from file [white]{path}[/] did not contain a content key. Skipping."); + } + } + else + { + // This might be an xbox license + + const string xboxNamespace = "http://schemas.microsoft.com/xboxlive/security/clas/LicResp/v1"; + + var xboxLicense = document.Descendants(XName.Get("SignedLicense", xboxNamespace)).FirstOrDefault(); + if (xboxLicense == null) + { + ConsoleLogger.WriteWarnLine( + $"License file [white]{path}[/] did not contain any known license type, assuming corrupted. Skipping."); + + return; + } + + var xboxSignedLicenseString = Encoding.UTF8.GetString(Convert.FromBase64String(xboxLicense.Value)); + var xboxSignedLicense = XDocument.Parse(xboxSignedLicenseString); + + var spLicenseBlock = xboxSignedLicense.Descendants(XName.Get("SPLicenseBlock", xboxNamespace)).FirstOrDefault(); + if (spLicenseBlock == null) + { + ConsoleLogger.WriteWarnLine( + $"Xbox license file [white]{path}[/] did not contain a SPLicenseBlock, assuming corrupted. Skipping."); + + return; + } + + var spLicense = new XvcLicenseBlock(Convert.FromBase64String(spLicenseBlock.Value)); + + var keyIdBlock = spLicense.GetBlockWithId(XvcLicenseBlockId.KeyId); + if (keyIdBlock == null) + { + ConsoleLogger.WriteWarnLine( + $"SPLicenseBlock from file [white]{path}[/] did not contain a key ID. Skipping."); + + return; + } + + var encryptedCikBlock = spLicense.GetBlockWithId(XvcLicenseBlockId.EncryptedCik); + + if (encryptedCikBlock == null) + { + ConsoleLogger.WriteWarnLine( + $"SPLicenseBlock from file [white]{path}[/] did not contain an encrypted CIK. Skipping."); + + return; + } + + var uplinkKeyIdBlock = spLicense.GetBlockWithId(XvcLicenseBlockId.UplinkKeyId); + if (uplinkKeyIdBlock == null) + { + ConsoleLogger.WriteWarnLine( + $"SPLicenseBlock from file [white]{path}[/] did not contain an uplink key ID. Skipping."); + + return; + } + + byte[] cikEncryptionKey; + + if (uplinkKeyIdBlock.BlockData.Skip(1).Sum(x => x) == 0) + { + var odkId = (OdkIndex)uplinkKeyIdBlock.BlockData[0]; + + if (!_odks.TryGetValue(odkId, out var odk)) + { + ConsoleLogger.WriteWarnLine( + $"Xbox license from file [white]{path}[/] contains a CIK that is encrypted by ODK [white]{odkId}[/] which is not currently loaded. Skipping."); + + return; + } + + cikEncryptionKey = odk; + } + else + { + var uplinkKeyId = new Guid(uplinkKeyIdBlock.BlockData); + + ConsoleLogger.WriteWarnLine( + $"Xbox license from file [white]{path}[/] contains a CIK that is encrypted by the key [white]{uplinkKeyId}[/]. Skipping as this is not currently supported."); + + return; + } + + var keyId = new Guid(keyIdBlock.BlockData); + + var aes = Aes.Create(); + aes.Key = cikEncryptionKey; + aes.IV = new byte[16]; + + var cik = aes.DecryptEcb(encryptedCikBlock.BlockData, PaddingMode.None); + + _keys[keyId] = new KeyEntry(keyId, cik); + } + } + catch (Exception ex) + { + ConsoleLogger.WriteWarnLine($"License file [white]{path}[/] was not valid XML, assuming corrupted. Skipping"); + #if DEBUG + AnsiConsole.WriteException(ex); + #endif + } + } +} + +public struct KeyEntry +{ + public const int Size = 0x30; + + public readonly Guid Id; + public readonly Buffer16 TweakKey; + public readonly Buffer16 DataKey; + + public KeyEntry(ReadOnlySpan cik) + { + Debug.Assert(cik.Length == 0x30, "Invalid cik length (!= 0x30)"); + Debug.Assert(BitConverter.IsLittleEndian); + + Id = new Guid(cik[..0x10]); + TweakKey = MemoryMarshal.Read(cik[0x10..0x20]); + DataKey = MemoryMarshal.Read(cik[0x20..]); + } + + public KeyEntry(Guid id, ReadOnlySpan key) + { + Debug.Assert(key.Length != 0x20, "Invalid key length (!= 0x20)"); + Debug.Assert(BitConverter.IsLittleEndian); + + Id = id; + TweakKey = MemoryMarshal.Read(key[..0x10]); + DataKey = MemoryMarshal.Read(key[0x10..]); + } + + public void Save(string path) + { + Debug.Assert(BitConverter.IsLittleEndian); + + using var fileStream = File.OpenWrite(path); + + var buffer = (stackalloc byte[Size]); + MemoryMarshal.Write(buffer, ref this); + + fileStream.Write(buffer); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/Program.cs b/XvdTool.Streaming/Program.cs new file mode 100644 index 0000000..013443d --- /dev/null +++ b/XvdTool.Streaming/Program.cs @@ -0,0 +1,302 @@ +using System.ComponentModel; +using System.Diagnostics; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace XvdTool.Streaming; + +internal class Program +{ + static void Main(string[] args) + { + var app = new CommandApp(); + app.Configure(config => + { + //config.PropagateExceptions(); + + config.AddCommand("info") + .WithDescription("Prints information about a given file.") + .WithExample("info", "c:/file.msixvc") + .WithExample("info", "c:/file.msixvc", "-o log.txt") + .WithExample("info", "https://assets1.xboxlive.com/..."); + + config.AddCommand("extract") + .WithDescription("Decrypts and extracts the files contained in a given file.") + .WithExample("extract", "c:/file.msixvc") + .WithExample("extract", "c:/file.msixvc", "-o c:/output") + .WithExample("extract", "https://assets1.xboxlive.com/..."); + + config.AddCommand("verify") + .WithDescription("Checks the integrity of the given file. (Local only)") + .WithExample("verify", "c:/file.msixvc"); + + config.AddCommand("decrypt") + .WithDescription("Decrypts the given file. (Local only)") + .WithExample("decrypt", "c:/file.msixvc"); + }); + + app.Run(args); + } +} + +internal abstract class XvdCommandSettings : CommandSettings +{ + [Description("File Path / URL to the XVC.")] + [CommandArgument(0, "")] + public string? XvcPath { get; init; } +} + +internal abstract class CryptoCommandSettings : XvdCommandSettings +{ + [Description("Path to the .cik file to be used regardless of the header key ID.")] + [CommandOption("-c|--cik")] + public string? CikPath { get; init; } + + [Description("Device key used to decrypt UWP licenses.")] + [CommandOption("-d|--device-key")] + public string? DeviceKey { get; init; } +} + +internal abstract class XvdCommand : Command where T : XvdCommandSettings +{ + protected StreamedXvdFile XvdFile = default!; + + protected void Initialize(XvdCommandSettings settings) + { + Debug.Assert(settings.XvcPath != null, "settings.XvcPath != null"); + + var path = settings.XvcPath; + + XvdFile = path.StartsWith("http") + ? StreamedXvdFile.OpenFromUrl(path) + : StreamedXvdFile.OpenFromFile(path); + + XvdFile.Parse(); + } + + public override ValidationResult Validate(CommandContext context, T settings) + { + if (settings.XvcPath != null && !settings.XvcPath.StartsWith("http") && !File.Exists(settings.XvcPath)) + return ValidationResult.Error("Provided file does not exist."); + + return ValidationResult.Success(); + } +} + +internal abstract class CryptoCommand : XvdCommand where T : CryptoCommandSettings +{ + protected KeyManager KeyManager = default!; + + protected bool Initialize(CryptoCommandSettings settings, out KeyEntry entry) + { + base.Initialize(settings); + + Debug.Assert(XvdFile != null, "XvdFile != null"); + + entry = default; + + KeyManager = new KeyManager(); + + if (settings.DeviceKey != null) + { + KeyManager.LoadDeviceKey(Convert.FromHexString(settings.DeviceKey)); + } + + if (settings.CikPath != null) + { + entry = KeyManager.LoadCik(settings.CikPath); + } + else + { + KeyManager.LoadCachedKeys(); + + var keyId = XvdFile.GetKeyId(); + if (keyId != Guid.Empty) + { + if (!KeyManager.TryGetKey(keyId, out entry)) + { + ConsoleLogger.WriteErrLine($"Could not find key [bold]{keyId}[/] loaded in key storage."); + + return false; + } + } + } + + return true; + } + + public override ValidationResult Validate(CommandContext context, T settings) + { + var result = base.Validate(context, settings); + + if (!result.Successful) + return result; + + if (settings.CikPath != null && !File.Exists(settings.CikPath)) + return ValidationResult.Error("Provided .cik file does not exist."); + + if (settings.DeviceKey != null && (settings.DeviceKey.Length != 32 || + settings.DeviceKey.All("0123456789ABCDEFabcdef".Contains))) + return ValidationResult.Error("Provided device key is invalid. Must be 32 hex characters long."); + + return ValidationResult.Success(); + } +} + +internal sealed class InfoCommand : XvdCommand +{ + public sealed class Settings : XvdCommandSettings + { + [Description("File path to save the output into.")] + [CommandOption("-o|--output")] + public string? OutputPath { get; init; } + } + + public override int Execute(CommandContext context, Settings settings) + { + Initialize(settings); + + Debug.Assert(XvdFile != null, "XvdFile != null"); + + using (XvdFile) + { + var infoOutput = XvdFile.PrintInfo(); + if (settings.OutputPath != null) + { + var directory = Path.GetDirectoryName(settings.OutputPath); + if (!string.IsNullOrEmpty(directory)) + Directory.CreateDirectory(directory); + + File.WriteAllText(settings.OutputPath, infoOutput); + } + } + + return 0; + } +} + +internal sealed class ExtractCommand : CryptoCommand +{ + public sealed class Settings : CryptoCommandSettings + { + [DefaultValue("output")] + [Description("Output directory to extract the files into.")] + [CommandOption("-o|--output")] + public string? OutputDirectory { get; init; } + + [Description("List of regions to skip downloading. Defaults to none.")] + [CommandOption("-r|--skip-region")] + public uint[]? SkippedRegions { get; init; } + + [Description("Skips performing hash verification on the pages prior to decryption.\nMassively improves performance at the cost of integrity.\nOnly use this if you know the file is not corrupt!")] + [CommandOption("-n|--no-hash-check")] + public bool SkipHashCheck { get; init; } + } + + public override int Execute(CommandContext context, Settings settings) + { + Debug.Assert(settings.OutputDirectory != null, "settings.OutputDirectory != null"); + + if (!Initialize(settings, out var keyEntry)) + { + return -1; + } + + var outputPath = Path.GetFullPath(settings.OutputDirectory); + + var hashStatus = settings.SkipHashCheck ? "[red]disabled[/]" : "[green]enabled[/]"; + + ConsoleLogger.WriteInfoLine($"Extracting files into [green bold]{outputPath}[/]. (Hash check {hashStatus})"); + + using (XvdFile) + { + XvdFile.ExtractFiles(outputPath, keyEntry, settings.SkipHashCheck, settings.SkippedRegions); + } + + ConsoleLogger.WriteInfoLine("[green bold]Successfully[/] extracted files."); + + return 0; + } +} + +internal sealed class VerifyCommand : XvdCommand +{ + public sealed class Settings : XvdCommandSettings; + + public override int Execute(CommandContext context, Settings settings) + { + Initialize(settings); + + Debug.Assert(XvdFile != null, "XvdFile != null"); + + using (XvdFile) + { + var result = XvdFile.VerifyDataHashes(); + + ConsoleLogger.WriteInfoLine(result + ? "Integrity check [green bold]successful[/]." + : "Integrity check [red bold]failed[/]."); + } + + return 0; + } + + public override ValidationResult Validate(CommandContext context, Settings settings) + { + base.Validate(context, settings); + + Debug.Assert(settings.XvcPath != null, "settings.XvcPath != null"); + + if (settings.XvcPath.StartsWith("http")) + return ValidationResult.Error("Only local files are supported for integrity verification."); + + if (!File.Exists(settings.XvcPath)) + return ValidationResult.Error("Provided file does not exist."); + + return ValidationResult.Success(); + } +} + +internal sealed class DecryptCommand : CryptoCommand +{ + public sealed class Settings : CryptoCommandSettings + { + [Description("Skips recalculating the hashes after decryption.\nSpeeds up the process, but makes subsequent hash checks on the file fail.")] + [CommandOption("-n|--no-hash-calc")] + public bool SkipHashCalculation { get; init; } + } + + public override int Execute(CommandContext context, Settings settings) + { + if (!Initialize(settings, out var keyEntry)) + { + return -1; + } + + using (XvdFile) + { + XvdFile.DecryptData(keyEntry, false); + } + + return 0; + } + + public override ValidationResult Validate(CommandContext context, Settings settings) + { + base.Validate(context, settings); + + Debug.Assert(settings.XvcPath != null, "settings.XvcPath != null"); + + if (!settings.SkipHashCalculation) + return ValidationResult.Error( + "Hash recalculation is not yet supported. Please use the 'extract' command instead, or specify '--no-hash-calc' to skip recomputing the hash table."); + + if (settings.XvcPath.StartsWith("http")) + return ValidationResult.Error("Only local files are supported for integrity verification."); + + if (!File.Exists(settings.XvcPath)) + return ValidationResult.Error("Provided file does not exist."); + + return ValidationResult.Success(); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/StreamedXvdFile.LocalImpl.cs b/XvdTool.Streaming/StreamedXvdFile.LocalImpl.cs new file mode 100644 index 0000000..d488729 --- /dev/null +++ b/XvdTool.Streaming/StreamedXvdFile.LocalImpl.cs @@ -0,0 +1,319 @@ +using System.Diagnostics; +using System.IO.MemoryMappedFiles; +using DotNext.IO.MemoryMappedFiles; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using LibXboxOne; +using Spectre.Console; +using XvdTool.Streaming.Crypto; + +namespace XvdTool.Streaming; + +// Everything here is optimized for working with local (on-disk) files +public partial class StreamedXvdFile +{ + // Used in both VerifyDataPageHashes and CacheDataUnits + private const int PageCountPerCache = 16; + + // ReSharper disable AccessToDisposedClosure + private void LocalDecryptData(in KeyEntry key, bool recalculateHashes) + { + Debug.Assert(_stream is FileStream, "_stream is FileStream"); + + var cipher = new AesXtsDecryptorNi(key.DataKey.AsReadOnlySpan(), key.TweakKey.AsReadOnlySpan()); + + Action decryptor; + + using var memoryFile = MemoryMappedFile.CreateFromFile((FileStream)_stream, null, 0, + MemoryMappedFileAccess.ReadWrite, + HandleInheritability.None, true); + + if (_hasXvcInfo) + { + if (_xvcInfo.ContentID == null || !_xvcInfo.IsAnyKeySet) + return; + + var decryptableRegionList = + _xvcRegions + .Where(region => + region.KeyId != XvcConstants.XVC_KEY_NONE + && _xvcInfo.KeyCount > region.KeyId - 1) + .ToArray(); + + Debug.Assert(decryptableRegionList.Length > 0, "decryptableRegionList.Length > 0"); + + decryptor = ctx => + { + var tasks = new ProgressTask[decryptableRegionList.Length]; + + for (int i = 0; i < decryptableRegionList.Length; i++) + { + var region = decryptableRegionList[i]; + + Debug.Assert(region.KeyId == 0x0, "region.KeyId == 0x0"); + + tasks[i] = ctx.AddTask( + $"Decrypting region [white bold]0x{(uint) region.Id:x8}[/]", + autoStart: false, + maxValue: region.Length); + } + + for (int i = 0; i < tasks.Length; i++) + { + var region = decryptableRegionList[i]; + var task = tasks[i]; + + task.StartTask(); + + LocalDecryptSection(task, memoryFile, cipher, (uint) region.Id, region.Offset, region.Length); + + ctx.Refresh(); + + task.StopTask(); + } + }; + } + else + { + decryptor = ctx => + { + var total = _stream.Length - (long) _userDataOffset; + + var task = ctx.AddTask("Decrypting data", autoStart: false, maxValue: total); + + task.StartTask(); + + LocalDecryptSection(task, memoryFile, cipher, 0x1, _userDataOffset, (ulong) total); + + ctx.Refresh(); + + task.StopTask(); + }; + } + + AnsiConsole.Progress() + .Columns( + new TaskDescriptionColumn(), + new ProgressBarColumn(), + new PercentageColumn(), + new TransferSpeedColumn(), + new DownloadedColumn(), + new RemainingTimeColumn(), + new SpinnerColumn()) + .Start(decryptor); + + ConsoleLogger.WriteInfoLine("[green bold]Successfully[/] decrypted regions."); + + using var headerAccessor = memoryFile.CreateViewAccessor(0, (long) XvdFile.XVD_HEADER_INCL_SIGNATURE_SIZE, + MemoryMappedFileAccess.Write); + + var header = _header; + header.VolumeFlags ^= XvdVolumeFlags.EncryptionDisabled; + + var headerBytes = Shared.StructToBytes(header); + + headerAccessor.WriteArray(0, headerBytes, 0, headerBytes.Length); + + if (recalculateHashes) + { + // TODO + } + + memoryFile.Dispose(); + } + // ReSharper restore AccessToDisposedClosure + + private void LocalDecryptSection(ProgressTask progressTask, MemoryMappedFile memoryFile, AesXtsDecryptorNi cipher, uint headerId, ulong offset, ulong length) + { + var startPage = XvdMath.OffsetToPageNumber(offset - _userDataOffset); + var totalPageCount = XvdMath.OffsetToPageNumber(length); + + var dataUnits = Span.Empty; + + if (_dataIntegrity) + { + dataUnits = CacheDataUnits(startPage, totalPageCount); + } + + var tweakIv = (stackalloc byte[16]); + + // + + MemoryMarshal.Cast(tweakIv)[1] = headerId; + _header.VDUID.AsSpan(0, 8).CopyTo(tweakIv[8..]); + + const int pagesPerMaxInt = int.MaxValue / 0x1000; + + var remainingPages = totalPageCount; + + var transformedSpan = (stackalloc byte[0x1000]); + + for (ulong mappedPageOffset = 0; mappedPageOffset < totalPageCount; mappedPageOffset += pagesPerMaxInt) + { + var pageCountThisOffset = Math.Min(remainingPages, pagesPerMaxInt); + remainingPages -= pageCountThisOffset; + + using var directAccessor = memoryFile.CreateDirectAccessor( + (long)(offset + mappedPageOffset * XvdFile.PAGE_SIZE), + (long)pageCountThisOffset * XvdFile.PAGE_SIZE, + MemoryMappedFileAccess.ReadWrite); + + for (uint i = 0; i < pageCountThisOffset; i++) + { + var currentTotalPage = i + (long)mappedPageOffset; + + progressTask.Increment((int)XvdFile.PAGE_SIZE); + + var currentPageSpan = + directAccessor.Bytes.Slice((int)(i * XvdFile.PAGE_SIZE), (int)XvdFile.PAGE_SIZE); + + if (dataUnits.Length > 0) + { + MemoryMarshal.Cast(tweakIv)[0] = dataUnits[(int) currentTotalPage]; + } + + cipher.Transform(currentPageSpan, transformedSpan, tweakIv); + + transformedSpan.CopyTo(currentPageSpan); + } + } + } + + private Span CacheDataUnits(ulong startPage, ulong count) + { + var dataUnits = new uint[count].AsSpan(); + + var hashPageOffset = (long)CalculateHashEntryBlockOffset(startPage, + out var currentHashPageEntry); + + var currentHashPageOffset = 0; + var currentHashPage = 0; + + var refreshCache = true; + + var pageCache = (stackalloc byte[(int)XvdFile.PAGE_SIZE * PageCountPerCache]); + + _stream.Position = hashPageOffset; + + int read; + for (ulong i = 0; i < count; i++) + { + if (refreshCache) + { + refreshCache = false; + + read = _stream.Read(pageCache); + Debug.Assert(read == pageCache.Length, "read == pageCache.Length"); + } + + dataUnits[(int)i] = MemoryMarshal.Cast(pageCache.Slice(currentHashPageOffset + 0x14, 4))[0]; + + currentHashPageOffset += 0x18; + currentHashPageEntry++; + if (currentHashPageEntry == 0xAA) + { + currentHashPageOffset += 0x10; + currentHashPageEntry = 0; + currentHashPage++; + if (currentHashPage == PageCountPerCache) + { + refreshCache = true; + currentHashPage = 0; + currentHashPageOffset = 0; + } + } + } + + return dataUnits; + } + + private bool LocalVerifyDataHashes() + { + if (!_dataIntegrity) + return true; + + return AnsiConsole.Progress() + .Columns( + new TaskDescriptionColumn(), + new ProgressBarColumn(), + new PercentageColumn(), + new TransferSpeedColumn(), + new DownloadedColumn(), + new RemainingTimeColumn(), + new SpinnerColumn()) + .Start(LocalVerifyDataHashesTask); + } + + private bool LocalVerifyDataHashesTask(ProgressContext ctx) + { + var valid = true; + + var cachedHashPages = (stackalloc byte[0x1000 * PageCountPerCache]); + var calculatedHash = (stackalloc byte[32]); + var pageData = (stackalloc byte[0x1000]); + + var dataBlockCount = XvdMath.OffsetToPageNumber((ulong)_stream.Length - _userDataOffset); + + var hashPageOffset = (long)CalculateHashEntryOffset(0); + var dataToHashOffset = _userDataOffset; + + var currentHashPageOffset = 0; + var currentHashPageEntry = 0; + var currentHashPage = 0; + + var refreshCache = true; + + var fileHandle = (_stream as FileStream)!.SafeFileHandle; + + _stream.Position = (long)dataToHashOffset; + + var task = ctx.AddTask("Verifying hashes", maxValue: (long) dataBlockCount * (int) XvdFile.PAGE_SIZE); + + int read; + for (ulong i = 0; i < dataBlockCount; i++) + { + task.Increment(XvdFile.PAGE_SIZE); + + if (refreshCache) + { + refreshCache = false; + + read = RandomAccess.Read(fileHandle, cachedHashPages, hashPageOffset); + Debug.Assert(read == cachedHashPages.Length, "read == cachedHashPages.Length"); + + hashPageOffset += cachedHashPages.Length; + } + + read = _stream.Read(pageData); + Debug.Assert(read == pageData.Length, "read == pageData.Length"); + + SHA256.HashData(pageData, calculatedHash); + + if (!cachedHashPages + .Slice(currentHashPageOffset, _hashEntryLength) + .SequenceEqual(calculatedHash[.._hashEntryLength])) + { + ConsoleLogger.WriteErrLine($"Page [bold]0x{i:x16}[i] has an [red bold]invalid[/] hash."); + + valid = false; + } + + currentHashPageOffset += 0x18; + currentHashPageEntry++; + if (currentHashPageEntry == 0xAA) + { + currentHashPageOffset += 0x10; + currentHashPageEntry = 0; + currentHashPage++; + if (currentHashPage == PageCountPerCache) + { + refreshCache = true; + currentHashPage = 0; + currentHashPageOffset = 0; + } + } + } + + return valid; + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/StreamedXvdFile.cs b/XvdTool.Streaming/StreamedXvdFile.cs new file mode 100644 index 0000000..f810553 --- /dev/null +++ b/XvdTool.Streaming/StreamedXvdFile.cs @@ -0,0 +1,743 @@ +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using LibXboxOne; +using Spectre.Console; +using Spectre.Console.Rendering; +using XvdTool.Streaming.Crypto; +using Aes = System.Runtime.Intrinsics.X86.Aes; + +namespace XvdTool.Streaming; + +public partial class StreamedXvdFile : IDisposable +{ + private readonly Stream _stream; + private readonly BinaryReader _reader; + + private XvdHeader _header; + + private bool _hasXvcInfo; + private XvcInfo _xvcInfo; + private XvcRegionHeader[] _xvcRegions; + private XvcUpdateSegment[] _xvcUpdateSegments; + private XvcRegionSpecifier[] _xvcRegionSpecifiers; + private XvcRegionPresenceInfo[] _xvcRegionPresenceInfo; + + private bool _hasUserData; + private XvdUserDataHeader _userDataHeader; + private XvdUserDataPackageFilesHeader _userDataPackageFilesHeader; + private readonly Dictionary _userDataPackages; + private readonly Dictionary _userDataPackageContents; + + private bool _hasSegmentMetadata; + private XvdSegmentMetadataHeader _segmentMetadataHeader; + private XvdSegmentMetadataSegment[] _segments; + private string[] _segmentPaths; + + // XVD header extracted infos + private bool _isXvc; + private bool _dataIntegrity; + private bool _resiliency; + private bool _encrypted; + + private int _hashEntryLength; + private ulong _hashTreePageCount; + private ulong _hashTreeLevels; + + private readonly ulong _embeddedXvdOffset = XvdFile.XVD_HEADER_INCL_SIGNATURE_SIZE; + private ulong _mutableDataOffset; + private ulong _hashTreeOffset; + private ulong _userDataOffset; + private ulong _xvcInfoOffset; + + private const string SegmentMetadataFilename = "SegmentMetadata.bin"; + + private StreamedXvdFile(Stream stream) + { + _stream = stream; + _reader = new BinaryReader(stream); + + _xvcRegions = Array.Empty(); + _xvcUpdateSegments = Array.Empty(); + _xvcRegionSpecifiers = Array.Empty(); + _xvcRegionPresenceInfo = Array.Empty(); + + _userDataPackages = new Dictionary(); + _userDataPackageContents = new Dictionary(); + + _segments = Array.Empty(); + _segmentPaths = Array.Empty(); + } + + public static StreamedXvdFile OpenFromUrl(string url) + { + return new StreamedXvdFile(HttpFileStream.Open(url)); + } + + public static StreamedXvdFile OpenFromFile(string filePath) + { + if (!File.Exists(filePath)) + throw new InvalidOperationException("File does not exist"); + + return new StreamedXvdFile(File.Open(filePath, FileMode.Open, FileAccess.ReadWrite)); + } + + public void Parse() + { + ParseHeader(); + + if (_header.UserDataLength > 0) + { + ParseUserData(); + + if (_userDataPackageContents.ContainsKey(SegmentMetadataFilename)) + ParseSegmentMetadata(); + } + + if (_isXvc && _header.XvcDataLength > 0) + { + ParseXvcInfo(); + } + } + + private void ParseHeader() + { + _stream.Position = 0; + + _header = _reader.ReadStruct(); + + _isXvc = XvdFile.XvcContentTypes.Contains(_header.ContentType); + _dataIntegrity = !_header.VolumeFlags.HasFlag(XvdVolumeFlags.DataIntegrityDisabled); + _resiliency = _header.VolumeFlags.HasFlag(XvdVolumeFlags.ResiliencyEnabled); + _encrypted = !_header.VolumeFlags.HasFlag(XvdVolumeFlags.EncryptionDisabled); + + _hashTreePageCount = + XvdMath.CalculateNumberHashPages(out _hashTreeLevels, _header.NumberOfHashedPages, _resiliency); + _hashEntryLength = _encrypted ? (int)XvdFile.HASH_ENTRY_LENGTH_ENCRYPTED : (int)XvdFile.HASH_ENTRY_LENGTH; + + _mutableDataOffset = XvdMath.PageNumberToOffset(_header.EmbeddedXvdPageCount) + _embeddedXvdOffset; + _hashTreeOffset = _header.MutableDataLength + _mutableDataOffset; + _userDataOffset = (_dataIntegrity ? XvdMath.PageNumberToOffset(_hashTreePageCount) : 0) + _hashTreeOffset; + _xvcInfoOffset = XvdMath.PageNumberToOffset(_header.UserDataPageCount) + _userDataOffset; + } + + private void ParseUserData() + { + _stream.Position = (long)_userDataOffset; + + var userData = new byte[_header.UserDataLength]; + var read = _stream.Read(userData.AsSpan()); + Debug.Assert(read == userData.Length, "read == userData.Length"); + + using var userDataReader = new BinaryReader(new MemoryStream(userData)); + + _userDataHeader = userDataReader.ReadStruct(); + _hasUserData = true; + + userDataReader.BaseStream.Position = _userDataHeader.Length; + + if (_userDataHeader.Type == XvdUserDataType.PackageFiles) + { + _userDataPackageFilesHeader = userDataReader.ReadStruct(); + Debug.Assert(int.MaxValue > _userDataPackageFilesHeader.FileCount, "int.MaxValue > _userDataPackageFilesHeader.FileCount"); + + var fileCount = (int)_userDataPackageFilesHeader.FileCount; + + _userDataPackages.EnsureCapacity(fileCount); + + foreach (var entry in userDataReader.ReadStructArray(fileCount)) + { + userDataReader.BaseStream.Position = _userDataHeader.Length + entry.Offset; + var contents = new byte[entry.Size]; + read = userDataReader.BaseStream.Read(contents.AsSpan()); + Debug.Assert(read == entry.Size, "read == entry.Size"); + + _userDataPackages[entry.FilePath] = entry; + _userDataPackageContents[entry.FilePath] = contents; + } + } + } + + private void ParseSegmentMetadata() + { + var segmentMetadata = _userDataPackageContents[SegmentMetadataFilename]; + + using var segmentMetadataReader = new BinaryReader(new MemoryStream(segmentMetadata)); + + _segmentMetadataHeader = segmentMetadataReader.ReadStruct(); + _hasSegmentMetadata = true; + + Debug.Assert(int.MaxValue > _segmentMetadataHeader.SegmentCount, "int.MaxValue > _segmentMetadataHeader.SegmentCount"); + + _segments = + segmentMetadataReader.ReadStructArray((int) _segmentMetadataHeader.SegmentCount); + + _segmentPaths = new string[_segmentMetadataHeader.SegmentCount]; + + var segmentPathsOffset = + _segmentMetadataHeader.HeaderLength + + _segmentMetadataHeader.SegmentCount * 0x10 /* segment size */; + + for (int i = 0; i < _segments.Length; i++) + { + var segment = _segments[i]; + + segmentMetadataReader.BaseStream.Position = segmentPathsOffset + segment.PathOffset; + + var stringSpan = segmentMetadataReader.ReadBytes(segment.PathLength * 2).AsSpan(); + + _segmentPaths[i] = new string(MemoryMarshal.Cast(stringSpan)); + } + } + + private void ParseXvcInfo() + { + _stream.Position = (long)_xvcInfoOffset; + + var xvcInfo = new byte[_header.XvcDataLength]; + var xvcInfoSpan = xvcInfo.AsSpan(); + + var read = _stream.Read(xvcInfo.AsSpan()); + Debug.Assert(read == xvcInfoSpan.Length, "read == xvcInfoSpan.Length"); + + using var xvcInfoReader = new BinaryReader(new MemoryStream(xvcInfo)); + + _xvcInfo = xvcInfoReader.ReadStruct(); + _hasXvcInfo = true; + + if (_xvcInfo.Version >= 1) + { + Debug.Assert(int.MaxValue > _xvcInfo.RegionCount, "int.MaxValue > _xvcInfo.RegionCount"); + _xvcRegions = xvcInfoReader.ReadStructArray((int)_xvcInfo.RegionCount); + + Debug.Assert(int.MaxValue > _xvcInfo.UpdateSegmentCount, "int.MaxValue > _xvcInfo.UpdateSegmentCount"); + _xvcUpdateSegments = xvcInfoReader.ReadStructArray((int) _xvcInfo.UpdateSegmentCount); + + if (_xvcInfo.Version >= 2) + { + Debug.Assert(int.MaxValue > _xvcInfo.RegionSpecifierCount, "int.MaxValue > _xvcInfo.RegionSpecifierCount"); + _xvcRegionSpecifiers = xvcInfoReader.ReadStructArray((int) _xvcInfo.RegionSpecifierCount); + + if (_header.MutableDataPageCount > 0) + { + var presenceInfo = xvcInfoSpan.Slice((int)xvcInfoReader.BaseStream.Position, (int)_xvcInfo.RegionCount); + + _xvcRegionPresenceInfo = MemoryMarshal.Cast(presenceInfo).ToArray(); + } + } + } + } + + public Guid GetKeyId() + { + if (!_encrypted || !_hasXvcInfo || !_xvcInfo.IsAnyKeySet) + return Guid.Empty; + + foreach (var key in _xvcInfo.EncryptionKeyIds) + { + if (key.IsKeyNulled) + continue; + + return new Guid(key.KeyId); + } + + throw new UnreachableException("IsAnyKeySet == false but no key was not nulled"); + } + + private ulong CalculateHashEntryOffset(ulong blockNo, uint hashLevel = 0) + { + var hashBlockPage = XvdMath.CalculateHashBlockNumForBlockNum(_header.Type, _hashTreeLevels, + _header.NumberOfHashedPages, blockNo, hashLevel, out var hashEntryNum, _resiliency); + + return + _hashTreeOffset + + XvdMath.PageNumberToOffset(hashBlockPage) + + hashEntryNum * XvdFile.HASH_ENTRY_LENGTH; + } + + private ulong CalculateHashEntryBlockOffset(ulong blockNo, out ulong hashEntryId) + { + var hashBlockPage = XvdMath.CalculateHashBlockNumForBlockNum(_header.Type, _hashTreeLevels, + _header.NumberOfHashedPages, blockNo, 0, out hashEntryId, _resiliency); + + return + _hashTreeOffset + + XvdMath.PageNumberToOffset(hashBlockPage); + } + + public void DecryptData(in KeyEntry key, bool recalculateHashes) + { + if (_stream is not FileStream) + { + ConsoleLogger.WriteErrLine("Decryption is only supported for [bold]local[/] files."); + ConsoleLogger.WriteErrLine("[bold red]Streamed[/] files can only be extracted."); + return; + } + + if (!_encrypted) + { + ConsoleLogger.WriteInfoLine("Skipping decryption as the file is [green bold]not encrypted[/]."); + //return; + } + + LocalDecryptData(key, recalculateHashes); + } + + public bool VerifyDataHashes() + { + if (_stream is not FileStream) + { + ConsoleLogger.WriteErrLine("Hash verification is only supported for local files."); + ConsoleLogger.WriteErrLine("Streamed files can only be extracted."); + return false; + } + + if (!_dataIntegrity) + { + ConsoleLogger.WriteInfoLine("Skipping hash verification as this file does not have data integrity enabled."); + return true; + } + + return LocalVerifyDataHashes(); + } + + public void ExtractFiles(string outputPath, in KeyEntry key, bool skipHashCheck = false, uint[]? skippedRegionList = null) + { + if (!_hasXvcInfo) + { + ConsoleLogger.WriteErrLine("Cannot extract non-XVC files."); + return; + } + + if (!_hasSegmentMetadata) + { + ConsoleLogger.WriteErrLine("Cannot extract files that do not contain a segment->file map (SegmentMetadata.bin)."); + return; + } + + if (0 >= _segments.Length) + { + ConsoleLogger.WriteWarnLine("XVC does not contain any segments/files."); + return; + } + + AesXtsDecryptorNi cipher = default!; + + if (_encrypted) + { + if (!Aes.IsSupported) + { + ConsoleLogger.WriteErrLine("Your CPU does not support AES-NI."); + ConsoleLogger.WriteErrLine("Either upgrade to a newer CPU or use the regular XVDTool to extract this file."); + return; + } + + cipher = new AesXtsDecryptorNi(key.DataKey.AsReadOnlySpan(), key.TweakKey.AsReadOnlySpan()); + } + + var fullOutputPath = Path.GetFullPath(outputPath); // Expand path to prevent issues later + + var firstSegmentOffset = XvdMath.PageNumberToOffset(_xvcUpdateSegments[0].PageNum); + + var extractableRegionList = + _xvcRegions + .Where(region => + (skippedRegionList == null || !skippedRegionList.Contains((uint)region.Id)) + && (region.FirstSegmentIndex != 0 || firstSegmentOffset == region.Offset)) + .ToArray(); + + // Fancy console version + AnsiConsole.Progress() + .Columns( + new TaskDescriptionColumn(), + new ProgressBarColumn(), + new PercentageColumn(), + new TransferSpeedColumn(), + new DownloadedColumn(), + new RemainingTimeColumn(), + new SpinnerColumn()) + .Start(ctx => + { + var tasks = new ProgressTask[extractableRegionList.Length]; + for (int i = 0; i < extractableRegionList.Length; i++) + { + var region = extractableRegionList[i]; + var totalValue = region.Length; + + tasks[i] = ctx.AddTask( + $"Extracting region [white bold]0x{(uint) region.Id:x8}[/]", + autoStart: false, + maxValue: totalValue); + } + + for (int i = 0; i < tasks.Length; i++) + { + var region = extractableRegionList[i]; + var task = tasks[i]; + + task.StartTask(); + + ExtractRegion( + task, + fullOutputPath, + cipher, + (uint)region.Id, + region.Offset, + region.Length, + region.FirstSegmentIndex, + _encrypted && region.KeyId != XvcConstants.XVC_KEY_NONE, + skipHashCheck + ); + + task.StopTask(); + } + }); + } + + private void ExtractRegion( + ProgressTask progressTask, + string output, + AesXtsDecryptorNi cipher, + uint headerId, + ulong regionOffset, + ulong regionLength, + uint startSegment, + bool shouldDecrypt, + bool skipHashCheck = false + ) + { + Debug.Assert(_xvcUpdateSegments[(int)startSegment].PageNum == XvdMath.OffsetToPageNumber(regionOffset)); + + var tweakIv = (stackalloc byte[16]); + + if (shouldDecrypt) + { + MemoryMarshal.Cast(tweakIv)[1] = headerId; + _header.VDUID.AsSpan(0, 8).CopyTo(tweakIv[8..]); + } + + // Page Cache + var refreshPageCache = true; + var totalPageCacheOffset = (long)regionOffset; + + var pageCacheOffset = 0; + + //var pageCache = (stackalloc byte[0x10000]); + var pageCache = new byte[0x100000].AsSpan(); + + // Hash Cache + var refreshHashCache = _dataIntegrity; + var totalHashCacheOffset = + (long) CalculateHashEntryBlockOffset(XvdMath.OffsetToPageNumber(regionOffset - _userDataOffset), + out var hashCacheEntry); + + var hashCacheOffset = (int)(hashCacheEntry * XvdFile.HASH_ENTRY_LENGTH); + + // var hashCache = (stackalloc byte[0x10000]); + var hashCache = new byte[0x100000].AsSpan(); + + // Buffer for calculated hash + var calculatedHash = (stackalloc byte[SHA256.HashSizeInBytes]); + + // Progression tracking + var currentSegment = startSegment; + var currentPageNumber = 0; + var totalPageNumber = (long) XvdMath.OffsetToPageNumber(regionLength); + + int read; + while (_segments.Length > currentSegment && totalPageNumber > currentPageNumber) + { + var fileSize = _segments[currentSegment].FileSize; + var filePath = _segmentPaths[currentSegment]; + + var outputPath = Path.Join(output, filePath); + var outputDirectory = Path.GetDirectoryName(outputPath); + if (outputDirectory != null) + Directory.CreateDirectory(outputDirectory); + + using var fileStream = File.OpenWrite(outputPath); + + var remainingFileSize = fileSize; + + while (remainingFileSize > 0) + { + var currentFileSectionLength = (int)Math.Min(remainingFileSize, XvdFile.PAGE_SIZE); + + if (refreshHashCache) + { + _stream.Position = totalHashCacheOffset; + + read = _stream.Read(hashCache); + Debug.Assert(read == hashCache.Length); + + refreshHashCache = false; + } + + if (refreshPageCache) + { + _stream.Position = totalPageCacheOffset; + + read = _stream.Read(pageCache); + Debug.Assert(read == pageCache.Length || (uint)pageCache.Length > remainingFileSize); + + refreshPageCache = false; + } + + var currentPage = pageCache.Slice(pageCacheOffset, (int) XvdFile.PAGE_SIZE); + + if (_dataIntegrity) + { + var currentHashEntry = hashCache.Slice(hashCacheOffset, (int) XvdFile.HASH_ENTRY_LENGTH); + + if (!skipHashCheck) + { + SHA256.HashData(currentPage, calculatedHash); + + if (!currentHashEntry[.._hashEntryLength].SequenceEqual(calculatedHash[.._hashEntryLength])) + { + ConsoleLogger.WriteErrLine($"Page 0x{currentPageNumber:x} has an invalid hash, retrying."); + + // This could be corruption during the download, refresh caches and retry + refreshHashCache = true; + refreshPageCache = true; + continue; + } + } + + if (shouldDecrypt) + { + MemoryMarshal.Cast(tweakIv)[0] = + MemoryMarshal.Cast(currentHashEntry.Slice(_hashEntryLength, 4))[0]; + } + + hashCacheOffset += (int) XvdFile.HASH_ENTRY_LENGTH; + hashCacheEntry++; + if (hashCacheEntry == XvdFile.HASH_ENTRIES_IN_PAGE) + { + hashCacheEntry = 0; + hashCacheOffset += 0x10; // Alignment for page boundaries (0xff0 -> 0x1000) + } + + if (hashCacheOffset == hashCache.Length) + { + totalHashCacheOffset += hashCacheOffset; + hashCacheOffset = 0; + hashCacheEntry = 0; + refreshHashCache = true; + } + } + + if (shouldDecrypt) + { + cipher.Transform(currentPage, currentPage, tweakIv); + } + + fileStream.Write(currentPage[..currentFileSectionLength]); + + remainingFileSize -= (uint) currentFileSectionLength; + + pageCacheOffset += (int) XvdFile.PAGE_SIZE; + if (pageCacheOffset == pageCache.Length) + { + totalPageCacheOffset += pageCacheOffset; + pageCacheOffset = 0; + refreshPageCache = true; + } + + currentPageNumber++; + progressTask.Increment(XvdFile.PAGE_SIZE); + } + + currentSegment++; + } + } + + public string PrintInfo() + { + AnsiConsole.Record(); + + static Rows StringToRows(string text) + { + return new Rows(text.Split(Environment.NewLine).Select(x => new Text(x.Trim()))); + } + + var xvdHeaderPanel = new Panel(StringToRows(_header.ToString(false))) + .Header("XVD Header") + .RoundedBorder() + .Expand(); + + AnsiConsole.Write(xvdHeaderPanel); + + if (_hasXvcInfo) + { + var xvcInfoPanel = new Panel(StringToRows(_xvcInfo.ToString(false))) + .Header("XVC Info") + .RoundedBorder() + .Expand(); + + AnsiConsole.Write(xvcInfoPanel); + + if (_xvcInfo.Version >= 1) + { + var regionSpecifiersPerRegion = _xvcRegionSpecifiers + .ToLookup(x => x.RegionId) + .ToDictionary(x => x.Key, x => x.ToList()); + + var regionTable = new Table() + .Title("XVC Regions") + .AddColumns( + new TableColumn("Region ID"), + new TableColumn("Flags"), + new TableColumn("Key ID"), + new TableColumn("Offset"), + new TableColumn("Length"), + new TableColumn("Hash"), + new TableColumn("First Segment"), + //new TableColumn("Region Status"), + new TableColumn("Region Specifiers") + ) + .RoundedBorder() + .Expand(); + + foreach (var region in _xvcRegions) + { + //var regionPresenceElement = new Text(_xvcRegionPresenceInfo.Length > 0 + // ? _xvcRegionPresenceInfo[i].ToString() + // : ""); + + IRenderable regionSpecifierElement; + + if (regionSpecifiersPerRegion.TryGetValue(region.Id, out var specifiers)) + { + var regionSpecifierTable = new Table() + .AddColumn("Key") + .AddColumn("Value") + .RoundedBorder() + .Expand(); + + foreach (var specifier in specifiers) + regionSpecifierTable.AddRow(specifier.Key, specifier.Value); + + regionSpecifierElement = regionSpecifierTable; + } + else + { + regionSpecifierElement = new Text(""); + } + + regionTable.AddRow( + new Text(region.Id.ToString()), + new Text(region.Flags.ToString()), + new Text($"0x{region.KeyId:x}"), + new Text($"0x{region.Offset:x}"), + new Text($"0x{region.Length:x}"), + new Text($"0x{region.Hash:x}"), + new Text($"0x{region.FirstSegmentIndex:x}"), + //regionPresenceElement, + regionSpecifierElement + ); + } + + AnsiConsole.Write(regionTable); + } + } + + if (_hasUserData) + { + var userDataPanel = new Panel( + new Rows( + new Markup($"[white]Type[/]: [green bold]{_userDataHeader.Type}[/]"), + new Markup($"[white]Version[/]: [green bold]{_userDataHeader.Version}[/]"), + new Markup($"[white]Length[/]: [green bold]{_userDataHeader.Length}[/]"), + new Markup($"[white]Unknown[/]: [green bold]{_userDataHeader.Unknown}[/]") + )) + .Header("User Data") + .RoundedBorder() + .Expand(); + + AnsiConsole.Write(userDataPanel); + + if (_userDataHeader.Type == XvdUserDataType.PackageFiles) + { + var packageFilesTable = new Table() + .Title("Package Files") + .AddColumns( + new TableColumn("Offset"), + new TableColumn("Size"), + new TableColumn("File Path") + ) + .RoundedBorder() + .Expand(); + + foreach (var (_, entry) in _userDataPackages) + { + packageFilesTable.AddRow( + new Markup($"[green bold]0x{entry.Offset:x8}[/]"), + new Markup($"[green bold]0x{entry.Size:x8}[/]"), + new Markup($"[aqua underline]{entry.FilePath}[/]") + ); + } + + AnsiConsole.Write(packageFilesTable); + } + + if (_hasSegmentMetadata) + { + var segmentMetadataPanel = new Panel( + new Rows( + new Markup($"[white]Segment Count[/]: [green]{_segmentMetadataHeader.SegmentCount}[/]"), + new Markup($"[white]Version0[/]: [green]{_segmentMetadataHeader.Version0}[/]"), + new Markup($"[white]Version1[/]: [green]{_segmentMetadataHeader.Version1}[/]") + )) + .Header("Segment Metadata") + .RoundedBorder() + .Expand(); + + AnsiConsole.Write(segmentMetadataPanel); + + var segmentTable = new Table() + .Title("Segment Files") + .AddColumns( + new TableColumn("Start Page"), + new TableColumn("File Size"), + new TableColumn("Hash"), + new TableColumn("Flags"), + new TableColumn("File Path") + ) + .RoundedBorder() + .Expand(); + + for (int i = 0; i < Math.Min(_segments.Length, 0x1000); i++) + { + var segment = _segments[i]; + var updateSegment = _xvcUpdateSegments[i]; + + segmentTable.AddRow( + new Markup($"[green]0x{updateSegment.PageNum:x8}[/]"), + new Markup($"[green]0x{segment.FileSize:x16}[/]"), + new Markup($"[green]0x{updateSegment.Hash:x16}[/]"), + new Markup(segment.Flags != 0x0 ? $"[fuchsia]0x{(byte)segment.Flags:x2}[/]" : $"0x{segment.Flags}"), + new Markup($"[aqua underline]{_segmentPaths[i]}[/]") + ); + + if (_segments.Length > 0x1000) + { + segmentTable.AddEmptyRow(); + segmentTable.AddRow(new Markup("[red bold][/]")); + } + } + + AnsiConsole.Write(segmentTable); + } + } + + return AnsiConsole.ExportText(); + } + + public void Dispose() + { + _reader.Dispose(); + _stream.Dispose(); + } +} \ No newline at end of file diff --git a/XvdTool.Streaming/XvdTool.Streaming.csproj b/XvdTool.Streaming/XvdTool.Streaming.csproj new file mode 100644 index 0000000..ca4d336 --- /dev/null +++ b/XvdTool.Streaming/XvdTool.Streaming.csproj @@ -0,0 +1,20 @@ + + + + Exe + net7.0 + enable + enable + preview + + + + + + + + + + + +