mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-07-08 17:57:02 +00:00
Port Wii decryption logic from Serialization
This commit is contained in:
@@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Collections.Exte
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Collections.Extensions.Test", "SabreTools.Collections.Extensions.Test\SabreTools.Collections.Extensions.Test.csproj", "{C32AA0AE-46ED-49CE-9E4F-84B45130D2CE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SabreTools.Security.Cryptography.Test", "SabreTools.Security.Cryptography.Test\SabreTools.Security.Cryptography.Test.csproj", "{C9634E58-302A-433E-AC74-883C591EFB94}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -405,6 +407,18 @@ Global
|
||||
{C32AA0AE-46ED-49CE-9E4F-84B45130D2CE}.Release|x64.Build.0 = Release|Any CPU
|
||||
{C32AA0AE-46ED-49CE-9E4F-84B45130D2CE}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{C32AA0AE-46ED-49CE-9E4F-84B45130D2CE}.Release|x86.Build.0 = Release|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Release|x64.Build.0 = Release|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{C9634E58-302A-433E-AC74-883C591EFB94}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
|
||||
<IsPackable>false</IsPackable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<WarningsNotAsErrors>CS0618</WarningsNotAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SabreTools.Security.Cryptography\SabreTools.Security.Cryptography.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
111
SabreTools.Security.Cryptography.Test/WiiDecrypterTests.cs
Normal file
111
SabreTools.Security.Cryptography.Test/WiiDecrypterTests.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Security.Cryptography.Test
|
||||
{
|
||||
public class WiiDecrypterTests
|
||||
{
|
||||
#region DecryptTitleKey
|
||||
|
||||
[Fact]
|
||||
public void DecryptTitleKey_EmptyKeys_Null()
|
||||
{
|
||||
var decrypter = new WiiDecrypter
|
||||
{
|
||||
RetailCommonKey = [],
|
||||
KoreanCommonKey = [],
|
||||
};
|
||||
|
||||
Assert.Null(decrypter.DecryptTitleKey(new byte[16], new byte[8], 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecryptTitleKey_NullEncKey_Null()
|
||||
{
|
||||
var decrypter = new WiiDecrypter
|
||||
{
|
||||
RetailCommonKey = new byte[16],
|
||||
KoreanCommonKey = new byte[16],
|
||||
};
|
||||
|
||||
Assert.Null(decrypter.DecryptTitleKey(null, new byte[8], 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecryptTitleKey_WrongLengthEncKey_Null()
|
||||
{
|
||||
var decrypter = new WiiDecrypter
|
||||
{
|
||||
RetailCommonKey = new byte[16],
|
||||
KoreanCommonKey = new byte[16],
|
||||
};
|
||||
|
||||
Assert.Null(decrypter.DecryptTitleKey(new byte[8], new byte[8], 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecryptTitleKey_NullTitleId_Null()
|
||||
{
|
||||
var decrypter = new WiiDecrypter
|
||||
{
|
||||
RetailCommonKey = new byte[16],
|
||||
KoreanCommonKey = new byte[16],
|
||||
};
|
||||
|
||||
Assert.Null(decrypter.DecryptTitleKey(new byte[16], null, 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecryptTitleKey_WrongLengthTitleId_Null()
|
||||
{
|
||||
var decrypter = new WiiDecrypter
|
||||
{
|
||||
RetailCommonKey = new byte[16],
|
||||
KoreanCommonKey = new byte[16],
|
||||
};
|
||||
|
||||
Assert.Null(decrypter.DecryptTitleKey(new byte[16], new byte[4], 0));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1)]
|
||||
[InlineData(2)]
|
||||
public void DecryptTitleKey_UnknownIndex_Null(int index)
|
||||
{
|
||||
var decrypter = new WiiDecrypter
|
||||
{
|
||||
RetailCommonKey = new byte[16],
|
||||
KoreanCommonKey = new byte[16],
|
||||
};
|
||||
|
||||
Assert.Null(decrypter.DecryptTitleKey(new byte[16], new byte[8], index));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DecryptTitleKey_WithInjectedKey_RoundTrips()
|
||||
{
|
||||
byte[] commonKey =
|
||||
[
|
||||
0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xF0, 0x0D,
|
||||
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
|
||||
];
|
||||
byte[] plainTitleKey =
|
||||
[
|
||||
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
|
||||
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
|
||||
];
|
||||
byte[] titleId = [0x00, 0x01, 0x00, 0x45, 0x52, 0x53, 0x42, 0x00];
|
||||
|
||||
byte[] iv = new byte[16];
|
||||
Array.Copy(titleId, 0, iv, 0, 8);
|
||||
byte[] encTitleKey = AESCBC.Encrypt(plainTitleKey, commonKey, iv)
|
||||
?? throw new InvalidOperationException("AesCbc.Encrypt returned null");
|
||||
|
||||
byte[]? decrypted = WiiDecrypter.DecryptTitleKey(encTitleKey, titleId, commonKey);
|
||||
Assert.NotNull(decrypted);
|
||||
Assert.Equal(plainTitleKey, decrypted);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -130,8 +130,8 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] TestIV =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
];
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Pattern to use for key validation tests
|
||||
@@ -139,14 +139,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] TestPattern =
|
||||
[
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
];
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08,
|
||||
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for KeyX0x18
|
||||
@@ -154,14 +154,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedKeyX0x18 =
|
||||
[
|
||||
0x06, 0xF1, 0xB2, 0x3B, 0x12, 0xAD, 0x80, 0xC1,
|
||||
0x13, 0xC6, 0x18, 0x3D, 0x27, 0xB8, 0xB9, 0x95,
|
||||
0x49, 0x73, 0x59, 0x82, 0xEF, 0xFE, 0x16, 0x48,
|
||||
0x91, 0x2A, 0x89, 0x55, 0x9A, 0xDC, 0x3C, 0xA0,
|
||||
0x84, 0x46, 0x14, 0xE0, 0x16, 0x59, 0x8E, 0x4F,
|
||||
0xC2, 0x6C, 0x52, 0xA4, 0x7D, 0xAD, 0x4F, 0x23,
|
||||
0xF1, 0xC6, 0x99, 0x44, 0x39, 0xB7, 0x42, 0xF0,
|
||||
0x1F, 0xBB, 0x02, 0xF6, 0x0A, 0x8A, 0xC2, 0x9A,
|
||||
];
|
||||
0x13, 0xC6, 0x18, 0x3D, 0x27, 0xB8, 0xB9, 0x95,
|
||||
0x49, 0x73, 0x59, 0x82, 0xEF, 0xFE, 0x16, 0x48,
|
||||
0x91, 0x2A, 0x89, 0x55, 0x9A, 0xDC, 0x3C, 0xA0,
|
||||
0x84, 0x46, 0x14, 0xE0, 0x16, 0x59, 0x8E, 0x4F,
|
||||
0xC2, 0x6C, 0x52, 0xA4, 0x7D, 0xAD, 0x4F, 0x23,
|
||||
0xF1, 0xC6, 0x99, 0x44, 0x39, 0xB7, 0x42, 0xF0,
|
||||
0x1F, 0xBB, 0x02, 0xF6, 0x0A, 0x8A, 0xC2, 0x9A,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for DevKeyX0x18
|
||||
@@ -169,14 +169,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedDevKeyX0x18 =
|
||||
[
|
||||
0x99, 0x6E, 0x3C, 0x54, 0x97, 0x3C, 0xEA, 0xE8,
|
||||
0xBA, 0xAE, 0x18, 0x5C, 0x93, 0x27, 0x65, 0x50,
|
||||
0xF6, 0x6D, 0x67, 0xD7, 0xEF, 0xBD, 0x7C, 0xCB,
|
||||
0x8A, 0xC1, 0x1A, 0x54, 0xFC, 0x3B, 0x8B, 0x3A,
|
||||
0x0E, 0xE5, 0xEF, 0x27, 0x4A, 0x73, 0x7E, 0x0A,
|
||||
0x2E, 0x2E, 0x9D, 0xAF, 0x6C, 0x03, 0xF2, 0x91,
|
||||
0xC4, 0xFA, 0x73, 0xFD, 0x6B, 0xA0, 0x07, 0xD4,
|
||||
0x75, 0x5B, 0x6F, 0x2E, 0x8B, 0x68, 0x4C, 0xD1,
|
||||
];
|
||||
0xBA, 0xAE, 0x18, 0x5C, 0x93, 0x27, 0x65, 0x50,
|
||||
0xF6, 0x6D, 0x67, 0xD7, 0xEF, 0xBD, 0x7C, 0xCB,
|
||||
0x8A, 0xC1, 0x1A, 0x54, 0xFC, 0x3B, 0x8B, 0x3A,
|
||||
0x0E, 0xE5, 0xEF, 0x27, 0x4A, 0x73, 0x7E, 0x0A,
|
||||
0x2E, 0x2E, 0x9D, 0xAF, 0x6C, 0x03, 0xF2, 0x91,
|
||||
0xC4, 0xFA, 0x73, 0xFD, 0x6B, 0xA0, 0x07, 0xD4,
|
||||
0x75, 0x5B, 0x6F, 0x2E, 0x8B, 0x68, 0x4C, 0xD1,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for KeyX0x1B
|
||||
@@ -184,29 +184,29 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedKeyX0x1B =
|
||||
[
|
||||
0x0A, 0xE4, 0x79, 0x02, 0x1B, 0xFA, 0x25, 0x4B,
|
||||
0x2D, 0x92, 0x4F, 0xA8, 0x41, 0x59, 0xCE, 0x10,
|
||||
0x09, 0xE6, 0x08, 0x61, 0x23, 0xC7, 0xD2, 0x30,
|
||||
0x84, 0x37, 0xD5, 0x49, 0x42, 0x94, 0xB2, 0x70,
|
||||
0x6A, 0xF3, 0x75, 0xB0, 0x1F, 0x4F, 0xA1, 0xCE,
|
||||
0x03, 0xA2, 0x6A, 0x19, 0x5D, 0x32, 0x0D, 0xB5,
|
||||
0x79, 0xCD, 0xFD, 0xF0, 0xDE, 0x49, 0x26, 0x2D,
|
||||
0x29, 0x36, 0x30, 0x69, 0x8B, 0x45, 0xE1, 0xFC,
|
||||
];
|
||||
0x2D, 0x92, 0x4F, 0xA8, 0x41, 0x59, 0xCE, 0x10,
|
||||
0x09, 0xE6, 0x08, 0x61, 0x23, 0xC7, 0xD2, 0x30,
|
||||
0x84, 0x37, 0xD5, 0x49, 0x42, 0x94, 0xB2, 0x70,
|
||||
0x6A, 0xF3, 0x75, 0xB0, 0x1F, 0x4F, 0xA1, 0xCE,
|
||||
0x03, 0xA2, 0x6A, 0x19, 0x5D, 0x32, 0x0D, 0xB5,
|
||||
0x79, 0xCD, 0xFD, 0xF0, 0xDE, 0x49, 0x26, 0x2D,
|
||||
0x29, 0x36, 0x30, 0x69, 0x8B, 0x45, 0xE1, 0xFC,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for DevKeyX0x1B
|
||||
/// </summary>
|
||||
private static readonly byte[] ExpectedDevKeyX0x1B =
|
||||
[
|
||||
0x16, 0x4F, 0xD9, 0x58, 0xC9, 0x20, 0xB3, 0xED,
|
||||
0xC4, 0xEB, 0x57, 0x39, 0x10, 0xEF, 0xA8, 0xCC,
|
||||
0xE5, 0x49, 0xBF, 0x52, 0x10, 0xA9, 0xCC, 0xE1,
|
||||
0x65, 0x3B, 0x2D, 0x51, 0x45, 0xFB, 0x60, 0x52,
|
||||
0x3E, 0x29, 0xEB, 0xEB, 0x3F, 0xF2, 0x76, 0x08,
|
||||
0x00, 0x05, 0x7F, 0x64, 0x29, 0x4A, 0x17, 0x22,
|
||||
0x56, 0x7F, 0x49, 0x94, 0x1A, 0x8C, 0x56, 0x35,
|
||||
0x38, 0xBE, 0xA4, 0x2E, 0x58, 0xD3, 0x81, 0x8C,
|
||||
];
|
||||
0x16, 0x4F, 0xD9, 0x58, 0xC9, 0x20, 0xB3, 0xED,
|
||||
0xC4, 0xEB, 0x57, 0x39, 0x10, 0xEF, 0xA8, 0xCC,
|
||||
0xE5, 0x49, 0xBF, 0x52, 0x10, 0xA9, 0xCC, 0xE1,
|
||||
0x65, 0x3B, 0x2D, 0x51, 0x45, 0xFB, 0x60, 0x52,
|
||||
0x3E, 0x29, 0xEB, 0xEB, 0x3F, 0xF2, 0x76, 0x08,
|
||||
0x00, 0x05, 0x7F, 0x64, 0x29, 0x4A, 0x17, 0x22,
|
||||
0x56, 0x7F, 0x49, 0x94, 0x1A, 0x8C, 0x56, 0x35,
|
||||
0x38, 0xBE, 0xA4, 0x2E, 0x58, 0xD3, 0x81, 0x8C,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for KeyX0x25
|
||||
@@ -214,14 +214,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedKeyX0x25 =
|
||||
[
|
||||
0x37, 0xBC, 0x73, 0xD6, 0xEE, 0x73, 0xE0, 0x94,
|
||||
0x42, 0x84, 0x74, 0xE5, 0xD8, 0xFB, 0x5F, 0x65,
|
||||
0xF4, 0xCF, 0x2E, 0xC1, 0x43, 0x48, 0x6C, 0xAA,
|
||||
0xC8, 0xF9, 0x96, 0xE6, 0x33, 0xDD, 0xE7, 0xBF,
|
||||
0xD2, 0x21, 0x89, 0x39, 0x13, 0xD1, 0xEC, 0xCA,
|
||||
0x1D, 0x5D, 0x1F, 0x77, 0x95, 0xD2, 0x8B, 0x27,
|
||||
0x92, 0x79, 0xC5, 0x1D, 0x72, 0xA7, 0x28, 0x57,
|
||||
0x41, 0x0E, 0x46, 0xB8, 0x80, 0x7B, 0x7C, 0x0D,
|
||||
];
|
||||
0x42, 0x84, 0x74, 0xE5, 0xD8, 0xFB, 0x5F, 0x65,
|
||||
0xF4, 0xCF, 0x2E, 0xC1, 0x43, 0x48, 0x6C, 0xAA,
|
||||
0xC8, 0xF9, 0x96, 0xE6, 0x33, 0xDD, 0xE7, 0xBF,
|
||||
0xD2, 0x21, 0x89, 0x39, 0x13, 0xD1, 0xEC, 0xCA,
|
||||
0x1D, 0x5D, 0x1F, 0x77, 0x95, 0xD2, 0x8B, 0x27,
|
||||
0x92, 0x79, 0xC5, 0x1D, 0x72, 0xA7, 0x28, 0x57,
|
||||
0x41, 0x0E, 0x46, 0xB8, 0x80, 0x7B, 0x7C, 0x0D,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for DevKeyX0x25
|
||||
@@ -229,14 +229,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedDevKeyX0x25 =
|
||||
[
|
||||
0x71, 0x65, 0x30, 0xF2, 0x68, 0xEC, 0x65, 0x0A,
|
||||
0x8C, 0x9E, 0xC5, 0x5A, 0xFA, 0x37, 0x8E, 0xDA,
|
||||
0x7B, 0x58, 0x3B, 0x66, 0x7C, 0x9D, 0x16, 0xD9,
|
||||
0x2D, 0x8F, 0xCF, 0x04, 0x66, 0x7F, 0x27, 0x41,
|
||||
0xBF, 0x5F, 0x1E, 0x11, 0x4C, 0xD6, 0xB9, 0x0A,
|
||||
0xC5, 0x42, 0xCF, 0x2B, 0x87, 0x6B, 0xD4, 0x72,
|
||||
0x4D, 0x9C, 0x29, 0x2E, 0xF8, 0xB0, 0x6F, 0x22,
|
||||
0x35, 0x5B, 0x96, 0x83, 0xD1, 0xE4, 0x5E, 0xDB,
|
||||
];
|
||||
0x8C, 0x9E, 0xC5, 0x5A, 0xFA, 0x37, 0x8E, 0xDA,
|
||||
0x7B, 0x58, 0x3B, 0x66, 0x7C, 0x9D, 0x16, 0xD9,
|
||||
0x2D, 0x8F, 0xCF, 0x04, 0x66, 0x7F, 0x27, 0x41,
|
||||
0xBF, 0x5F, 0x1E, 0x11, 0x4C, 0xD6, 0xB9, 0x0A,
|
||||
0xC5, 0x42, 0xCF, 0x2B, 0x87, 0x6B, 0xD4, 0x72,
|
||||
0x4D, 0x9C, 0x29, 0x2E, 0xF8, 0xB0, 0x6F, 0x22,
|
||||
0x35, 0x5B, 0x96, 0x83, 0xD1, 0xE4, 0x5E, 0xDB,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for KeyX0x2C
|
||||
@@ -244,14 +244,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedKeyX0x2C =
|
||||
[
|
||||
0xAE, 0x44, 0x20, 0xDB, 0xA5, 0x96, 0xDC, 0xF3,
|
||||
0xD8, 0x23, 0x9E, 0x3C, 0x44, 0x73, 0x3D, 0xCD,
|
||||
0x07, 0xD5, 0xF8, 0xD0, 0xC6, 0xB3, 0x5A, 0x80,
|
||||
0xB5, 0x5A, 0x55, 0x30, 0x5D, 0x4A, 0xBE, 0x61,
|
||||
0xBF, 0xEF, 0x64, 0x17, 0x28, 0xD6, 0x26, 0x52,
|
||||
0x42, 0x4D, 0x8F, 0x1C, 0xBC, 0x63, 0xD3, 0x91,
|
||||
0x7D, 0xA6, 0x4F, 0xAF, 0x26, 0x38, 0x60, 0xEE,
|
||||
0x79, 0x92, 0x2F, 0xD8, 0xCA, 0x4E, 0xE7, 0xEC,
|
||||
];
|
||||
0xD8, 0x23, 0x9E, 0x3C, 0x44, 0x73, 0x3D, 0xCD,
|
||||
0x07, 0xD5, 0xF8, 0xD0, 0xC6, 0xB3, 0x5A, 0x80,
|
||||
0xB5, 0x5A, 0x55, 0x30, 0x5D, 0x4A, 0xBE, 0x61,
|
||||
0xBF, 0xEF, 0x64, 0x17, 0x28, 0xD6, 0x26, 0x52,
|
||||
0x42, 0x4D, 0x8F, 0x1C, 0xBC, 0x63, 0xD3, 0x91,
|
||||
0x7D, 0xA6, 0x4F, 0xAF, 0x26, 0x38, 0x60, 0xEE,
|
||||
0x79, 0x92, 0x2F, 0xD8, 0xCA, 0x4E, 0xE7, 0xEC,
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Expected output value for DevKeyX0x2C
|
||||
@@ -259,14 +259,14 @@ namespace SabreTools.Security.Cryptography
|
||||
private static readonly byte[] ExpectedDevKeyX0x2C =
|
||||
[
|
||||
0x5F, 0x73, 0xD5, 0x9A, 0x67, 0xFF, 0x8C, 0x12,
|
||||
0x31, 0x58, 0x0B, 0x58, 0x46, 0xFE, 0x05, 0x16,
|
||||
0x92, 0xE4, 0x84, 0x06, 0x18, 0x9B, 0x58, 0x91,
|
||||
0xE7, 0xF8, 0xCD, 0xA9, 0x95, 0xAC, 0x07, 0xCD,
|
||||
0x43, 0x20, 0x7A, 0x8C, 0xCC, 0xAB, 0x48, 0x50,
|
||||
0x29, 0x2F, 0x96, 0x73, 0xB0, 0xD9, 0xE5, 0xCB,
|
||||
0xE6, 0x9A, 0x0D, 0xF7, 0xD0, 0x1E, 0xC2, 0xEC,
|
||||
0xC1, 0xE2, 0x8E, 0xEE, 0x89, 0xB9, 0xB1, 0x97,
|
||||
];
|
||||
0x31, 0x58, 0x0B, 0x58, 0x46, 0xFE, 0x05, 0x16,
|
||||
0x92, 0xE4, 0x84, 0x06, 0x18, 0x9B, 0x58, 0x91,
|
||||
0xE7, 0xF8, 0xCD, 0xA9, 0x95, 0xAC, 0x07, 0xCD,
|
||||
0x43, 0x20, 0x7A, 0x8C, 0xCC, 0xAB, 0x48, 0x50,
|
||||
0x29, 0x2F, 0x96, 0x73, 0xB0, 0xD9, 0xE5, 0xCB,
|
||||
0xE6, 0x9A, 0x0D, 0xF7, 0xD0, 0x1E, 0xC2, 0xEC,
|
||||
0xC1, 0xE2, 0x8E, 0xEE, 0x89, 0xB9, 0xB1, 0x97,
|
||||
];
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
156
SabreTools.Security.Cryptography/WiiDecrypter.cs
Normal file
156
SabreTools.Security.Cryptography/WiiDecrypter.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using SabreTools.Hashing;
|
||||
|
||||
namespace SabreTools.Security.Cryptography
|
||||
{
|
||||
public class WiiDecrypter
|
||||
{
|
||||
/// <summary>
|
||||
/// Retail common key (index 0)
|
||||
/// </summary>
|
||||
public byte[] RetailCommonKey
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
if (ValidateRetailCommonKey(value))
|
||||
field = value;
|
||||
}
|
||||
} = [];
|
||||
|
||||
/// <summary>
|
||||
/// Korean common key (index 1)
|
||||
/// </summary>
|
||||
public byte[] KoreanCommonKey
|
||||
{
|
||||
get;
|
||||
set
|
||||
{
|
||||
if (ValidateKoreanCommonKey(value))
|
||||
field = value;
|
||||
}
|
||||
} = [];
|
||||
|
||||
#region Internal Test Values
|
||||
|
||||
/// <summary>
|
||||
/// Korean common key SHA-256 hash
|
||||
/// </summary>
|
||||
private const string KoreanCommonKeySHA256 = "b9f42ca27a1e178f0f14ebf1a05d486fa8db8d08875336c4e6e8dfae29f2901c";
|
||||
|
||||
/// <summary>
|
||||
/// Retail common key SHA-256 hash
|
||||
/// </summary>
|
||||
private const string RetailCommonKeySHA256 = "de38aeab4fe0c36d828a47e6fd315100e7ce234d3b00aa25e6ad6f5ff2824af8";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Decryption
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt a Wii partition title key from the ticket data.
|
||||
/// </summary>
|
||||
/// <param name="encryptedTitleKey">16-byte encrypted title key from ticket offset 0x1BF</param>
|
||||
/// <param name="titleId">8-byte title ID from ticket offset 0x1DC (big-endian)</param>
|
||||
/// <param name="commonKeyIndex">Common key index to use for decryption</param>
|
||||
/// <returns>Decrypted 16-byte title key, or null if no key is available for the given index</returns>
|
||||
public byte[]? DecryptTitleKey(byte[]? encryptedTitleKey, byte[]? titleId, int commonKeyIndex)
|
||||
{
|
||||
if (encryptedTitleKey is null || encryptedTitleKey.Length != 16)
|
||||
return null;
|
||||
if (titleId is null || titleId.Length != 8)
|
||||
return null;
|
||||
|
||||
byte[] commonKey;
|
||||
if (commonKeyIndex == 0)
|
||||
commonKey = RetailCommonKey;
|
||||
else if (commonKeyIndex == 1)
|
||||
commonKey = KoreanCommonKey;
|
||||
else
|
||||
return null;
|
||||
|
||||
return DecryptTitleKey(encryptedTitleKey, titleId, commonKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt a Wii partition title key from the ticket data.
|
||||
/// </summary>
|
||||
/// <param name="encryptedTitleKey">16-byte encrypted title key from ticket offset 0x1BF</param>
|
||||
/// <param name="titleId">8-byte title ID from ticket offset 0x1DC (big-endian)</param>
|
||||
/// <param name="commonKey">Common key to use for decryption</param>
|
||||
/// <returns>Decrypted 16-byte title key, or null if no key is available for the given index</returns>
|
||||
public static byte[]? DecryptTitleKey(byte[]? encryptedTitleKey, byte[]? titleId, byte[] commonKey)
|
||||
{
|
||||
if (encryptedTitleKey is null || encryptedTitleKey.Length != 16)
|
||||
return null;
|
||||
if (titleId is null || titleId.Length != 8)
|
||||
return null;
|
||||
|
||||
if (commonKey.Length != 16)
|
||||
return null;
|
||||
|
||||
// IV is the 8-byte title ID padded with zeros to 16 bytes
|
||||
byte[] iv = new byte[16];
|
||||
Array.Copy(titleId, 0, iv, 0, 8);
|
||||
|
||||
return AESCBC.Decrypt(encryptedTitleKey, commonKey, iv);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt one Wii block of data (0x7C00 bytes) using AES-128-CBC.
|
||||
/// </summary>
|
||||
/// <param name="encryptedData">0x7C00 bytes of encrypted block data</param>
|
||||
/// <param name="titleKey">16-byte partition title key</param>
|
||||
/// <param name="iv">16-byte initialization vector (last 16 bytes of the preceding hash block)</param>
|
||||
/// <returns>Decrypted 0x7C00-byte block data, or null on error</returns>
|
||||
public static byte[]? DecryptBlock(byte[] encryptedData, byte[] titleKey, byte[] iv)
|
||||
{
|
||||
if (encryptedData is null || encryptedData.Length != 0x7C00)
|
||||
return null;
|
||||
if (titleKey is null || titleKey.Length != 16)
|
||||
return null;
|
||||
if (iv is null || iv.Length != 16)
|
||||
return null;
|
||||
|
||||
return AESCBC.Decrypt(encryptedData, titleKey, iv);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation Methods
|
||||
|
||||
/// <summary>
|
||||
/// Validate the Korean common key based on hash and length
|
||||
/// </summary>
|
||||
/// <param name="commonKey">Korean common key to validate</param>
|
||||
/// <returns>True if the key was valid, false otherwise</returns>
|
||||
public static bool ValidateKoreanCommonKey(byte[]? commonKey)
|
||||
{
|
||||
// Ignore invalid values
|
||||
if (commonKey is null || commonKey.Length != 16)
|
||||
return false;
|
||||
|
||||
// Hash the key and compare
|
||||
string? actualHash = HashTool.GetByteArrayHash(commonKey, HashType.SHA256);
|
||||
return string.Equals(actualHash, KoreanCommonKeySHA256, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate the retail common key based on hash and length
|
||||
/// </summary>
|
||||
/// <param name="commonKey">Retail common key to validate</param>
|
||||
/// <returns>True if the key was valid, false otherwise</returns>
|
||||
public static bool ValidateRetailCommonKey(byte[]? commonKey)
|
||||
{
|
||||
// Ignore invalid values
|
||||
if (commonKey is null || commonKey.Length != 16)
|
||||
return false;
|
||||
|
||||
// Hash the key and compare
|
||||
string? actualHash = HashTool.GetByteArrayHash(commonKey, HashType.SHA256);
|
||||
return string.Equals(actualHash, RetailCommonKeySHA256, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user