Move CryptoOld

This commit is contained in:
Alex Barney 2021-12-18 20:55:52 -07:00
parent 2bd1c05ed5
commit 8e5f400048
7 changed files with 50 additions and 103 deletions

View file

@ -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<byte> data, Span<byte> 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;
}
}
}
}

View file

@ -3,6 +3,7 @@ using System;
using System.IO;
using LibHac.Common;
using LibHac.Common.Keys;
using LibHac.Tools.Crypto;
namespace LibHac.Npdm;

View file

@ -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<byte> data, Span<byte> 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;
}
}
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;