diff --git a/SabreTools.Wrappers/N3DS.Encryption.cs b/SabreTools.Wrappers/N3DS.Encryption.cs index 979e005b..d719e4fc 100644 --- a/SabreTools.Wrappers/N3DS.Encryption.cs +++ b/SabreTools.Wrappers/N3DS.Encryption.cs @@ -980,7 +980,7 @@ namespace SabreTools.Wrappers #region Helper Classes /// - /// Encryption settings to be passed around during processing + /// 3DS Encryption settings to be passed around during processing /// private class EncryptionSettings { @@ -1003,18 +1003,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedKeyX0x18)) - return; - - // Assign the validated value - field = value; + if (ValidateKeyX0x18(value)) + field = value; } } = []; @@ -1026,18 +1016,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedDevKeyX0x18)) - return; - - // Assign the validated value - field = value; + if (ValidateDevKeyX0x18(value)) + field = value; } } = []; @@ -1049,18 +1029,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedKeyX0x1B)) - return; - - // Assign the validated value - field = value; + if (ValidateKeyX0x1B(value)) + field = value; } } = []; @@ -1072,18 +1042,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedDevKeyX0x1B)) - return; - - // Assign the validated value - field = value; + if (ValidateDevKeyX0x1B(value)) + field = value; } } = []; @@ -1095,18 +1055,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedKeyX0x25)) - return; - - // Assign the validated value - field = value; + if (ValidateKeyX0x25(value)) + field = value; } } = []; @@ -1118,18 +1068,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedDevKeyX0x25)) - return; - - // Assign the validated value - field = value; + if (ValidateDevKeyX0x25(value)) + field = value; } } = []; @@ -1141,18 +1081,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedKeyX0x2C)) - return; - - // Assign the validated value - field = value; + if (ValidateKeyX0x2C(value)) + field = value; } } = []; @@ -1164,18 +1094,8 @@ namespace SabreTools.Wrappers get; set { - // Ignore missing key data - if (value.Length == 0) - return; - - // Validate the key data - var cipher = AESCTR.CreateEncryptionCipher(value, TestIV); - byte[] actual = cipher.ProcessBytes(TestPattern); - if (!actual.EqualsExactly(ExpectedDevKeyX0x2C)) - return; - - // Assign the validated value - field = value; + if (ValidateDevKeyX0x2C(value)) + field = value; } } = []; @@ -1326,6 +1246,146 @@ namespace SabreTools.Wrappers ]; #endregion + + #region Validation Methods + + /// + /// Validate a possible KeyX0x18 value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateKeyX0x18(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedKeyX0x18); + } + + /// + /// Validate a possible DevKeyX0x18 value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateDevKeyX0x18(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedDevKeyX0x18); + } + + /// + /// Validate a possible KeyX0x1B value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateKeyX0x1B(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedKeyX0x1B); + } + + /// + /// Validate a possible DevKeyX0x1B value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateDevKeyX0x1B(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedDevKeyX0x1B); + } + + /// + /// Validate a possible KeyX0x25 value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateKeyX0x25(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedKeyX0x25); + } + + /// + /// Validate a possible DevKeyX0x25 value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateDevKeyX0x25(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedDevKeyX0x25); + } + + /// + /// Validate a possible KeyX0x2C value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateKeyX0x2C(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedKeyX0x2C); + } + + /// + /// Validate a possible DevKeyX0x2C value + /// + /// Key to test + /// True if the key was valid, false otherwise + public bool ValidateDevKeyX0x2C(byte[]? key) + { + // Missing key data is considered invalid + if (key is null || key.Length == 0) + return false; + + // Validate the key data + var cipher = AESCTR.CreateEncryptionCipher(key, TestIV); + byte[] actual = cipher.ProcessBytes(TestPattern); + return actual.EqualsExactly(ExpectedDevKeyX0x2C); + } + + #endregion } ///