diff --git a/src/LibHac/CryptoOld.cs b/src/LibHac/CryptoOld.cs deleted file mode 100644 index 69a033b2..00000000 --- a/src/LibHac/CryptoOld.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.IO; -using System.Security.Cryptography; -using LibHac.Common; -using LibHac.Crypto; -using LibHac.FsSystem; - -using Aes = LibHac.Crypto.Aes; - -namespace LibHac; - -public static class CryptoOld -{ - public static void GenerateKek(byte[] key, byte[] src, byte[] dest, byte[] kekSeed, byte[] keySeed) - { - byte[] kek = new byte[Aes.KeySize128]; - byte[] srcKek = new byte[Aes.KeySize128]; - - Aes.DecryptEcb128(kekSeed, kek, key); - Aes.DecryptEcb128(src, srcKek, kek); - - if (keySeed != null) - { - Aes.DecryptEcb128(keySeed, dest, srcKek); - } - else - { - Array.Copy(srcKek, dest, Aes.KeySize128); - } - } - - public static RSAParameters DecryptRsaKey(byte[] encryptedKey, byte[] kek) - { - byte[] counter = new byte[0x10]; - Array.Copy(encryptedKey, counter, 0x10); - byte[] key = new byte[0x230]; - Array.Copy(encryptedKey, 0x10, key, 0, 0x230); - - new Aes128CtrTransform(kek, counter).TransformBlock(key); - - byte[] d = new byte[0x100]; - byte[] n = new byte[0x100]; - byte[] e = new byte[4]; - Array.Copy(key, 0, d, 0, 0x100); - Array.Copy(key, 0x100, n, 0, 0x100); - Array.Copy(key, 0x200, e, 0, 4); - - RSAParameters rsaParams = Rsa.RecoverParameters(n, e, d); - TestRsaKey(rsaParams); - return rsaParams; - } - - private static void TestRsaKey(RSAParameters keyParams) - { - var rsa = new RSACryptoServiceProvider(); - rsa.ImportParameters(keyParams); - - byte[] test = { 12, 34, 56, 78 }; - byte[] testEnc = rsa.Encrypt(test, false); - byte[] testDec = rsa.Decrypt(testEnc, false); - - if (!Utilities.ArraysEqual(test, testDec)) - { - throw new InvalidDataException("Could not verify RSA key pair"); - } - } - - public static Validity Rsa2048Pkcs1Verify(byte[] data, byte[] signature, byte[] modulus) => - Rsa.VerifyRsa2048Pkcs1Sha256(signature, modulus, new byte[] { 1, 0, 1 }, data) - ? Validity.Valid - : Validity.Invalid; - - public static Validity Rsa2048PssVerify(byte[] data, byte[] signature, byte[] modulus) => - Rsa.VerifyRsa2048PssSha256(signature, modulus, new byte[] { 1, 0, 1 }, data) - ? Validity.Valid - : Validity.Invalid; - - public static byte[] DecryptRsaOaep(byte[] data, RSAParameters rsaParams) - { - var rsa = RSA.Create(); - - rsa.ImportParameters(rsaParams); - return rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA256); - } - - public static bool DecryptRsaOaep(ReadOnlySpan data, Span destination, RSAParameters rsaParams, out int bytesWritten) - { - using (var rsa = RSA.Create()) - { - try - { - rsa.ImportParameters(rsaParams); - - return rsa.TryDecrypt(data, destination, RSAEncryptionPadding.OaepSHA256, out bytesWritten); - } - catch (CryptographicException) - { - bytesWritten = 0; - return false; - } - } - } -} diff --git a/src/LibHac/Npdm/Acid.cs b/src/LibHac/Npdm/Acid.cs index 89d45d4d..20f27155 100644 --- a/src/LibHac/Npdm/Acid.cs +++ b/src/LibHac/Npdm/Acid.cs @@ -3,6 +3,7 @@ using System; using System.IO; using LibHac.Common; using LibHac.Common.Keys; +using LibHac.Tools.Crypto; namespace LibHac.Npdm; diff --git a/src/LibHac/Tools/Crypto/CryptoOld.cs b/src/LibHac/Tools/Crypto/CryptoOld.cs new file mode 100644 index 00000000..01835cba --- /dev/null +++ b/src/LibHac/Tools/Crypto/CryptoOld.cs @@ -0,0 +1,45 @@ +using System; +using System.Security.Cryptography; +using LibHac.Common; +using LibHac.Crypto; + +namespace LibHac.Tools.Crypto; + +public static class CryptoOld +{ + public static Validity Rsa2048Pkcs1Verify(byte[] data, byte[] signature, byte[] modulus) => + Rsa.VerifyRsa2048Pkcs1Sha256(signature, modulus, new byte[] { 1, 0, 1 }, data) + ? Validity.Valid + : Validity.Invalid; + + public static Validity Rsa2048PssVerify(byte[] data, byte[] signature, byte[] modulus) => + Rsa.VerifyRsa2048PssSha256(signature, modulus, new byte[] { 1, 0, 1 }, data) + ? Validity.Valid + : Validity.Invalid; + + public static byte[] DecryptRsaOaep(byte[] data, RSAParameters rsaParams) + { + var rsa = RSA.Create(); + + rsa.ImportParameters(rsaParams); + return rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA256); + } + + public static bool DecryptRsaOaep(ReadOnlySpan data, Span destination, RSAParameters rsaParams, out int bytesWritten) + { + using (var rsa = RSA.Create()) + { + try + { + rsa.ImportParameters(rsaParams); + + return rsa.TryDecrypt(data, destination, RSAEncryptionPadding.OaepSHA256, out bytesWritten); + } + catch (CryptographicException) + { + bytesWritten = 0; + return false; + } + } + } +} \ No newline at end of file diff --git a/src/LibHac/Tools/Es/Ticket.cs b/src/LibHac/Tools/Es/Ticket.cs index 589cf5b9..892cef0a 100644 --- a/src/LibHac/Tools/Es/Ticket.cs +++ b/src/LibHac/Tools/Es/Ticket.cs @@ -2,6 +2,7 @@ using System.IO; using LibHac.Common; using LibHac.Common.Keys; +using LibHac.Tools.Crypto; using LibHac.Util; namespace LibHac.Tools.Es; diff --git a/src/LibHac/Tools/Fs/XciHeader.cs b/src/LibHac/Tools/Fs/XciHeader.cs index 8a4244c0..6dd84bbc 100644 --- a/src/LibHac/Tools/Fs/XciHeader.cs +++ b/src/LibHac/Tools/Fs/XciHeader.cs @@ -5,6 +5,7 @@ using LibHac.Common; using LibHac.Common.Keys; using LibHac.Crypto; using LibHac.Fs; +using LibHac.Tools.Crypto; namespace LibHac.Tools.Fs; diff --git a/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs b/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs index f90bdd42..3b7812a3 100644 --- a/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs +++ b/src/LibHac/Tools/FsSystem/NcaUtils/Nca.cs @@ -11,6 +11,7 @@ using LibHac.Fs; using LibHac.Fs.Fsa; using LibHac.FsSystem; using LibHac.Spl; +using LibHac.Tools.Crypto; using LibHac.Tools.FsSystem.RomFs; namespace LibHac.Tools.FsSystem.NcaUtils; diff --git a/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs b/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs index 4cc6ef8e..5c93eac3 100644 --- a/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs +++ b/src/LibHac/Tools/FsSystem/NcaUtils/NcaHeader.cs @@ -8,6 +8,7 @@ using LibHac.Crypto; using LibHac.Diag; using LibHac.Fs; using LibHac.FsSystem; +using LibHac.Tools.Crypto; using LibHac.Util; namespace LibHac.Tools.FsSystem.NcaUtils;