diff --git a/SabreTools.Security.Cryptography/N3DSEncryptionSettings.cs b/SabreTools.Security.Cryptography/N3DSEncryptionSettings.cs index c6b8733..d038d86 100644 --- a/SabreTools.Security.Cryptography/N3DSEncryptionSettings.cs +++ b/SabreTools.Security.Cryptography/N3DSEncryptionSettings.cs @@ -26,18 +26,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -49,18 +39,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -72,18 +52,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -95,18 +65,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -118,18 +78,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -141,18 +91,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -164,18 +104,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -187,18 +117,8 @@ namespace SabreTools.Security.Cryptography 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; } } = []; @@ -349,5 +269,145 @@ namespace SabreTools.Security.Cryptography ]; #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 } }