mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Rename Aes class
This commit is contained in:
parent
4b2c4d9553
commit
488bd8e682
14 changed files with 112 additions and 102 deletions
|
@ -2,13 +2,14 @@
|
|||
using System;
|
||||
|
||||
#if HAS_INTRINSICS
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using LibHac.Crypto2.Detail;
|
||||
|
||||
using AesNi = System.Runtime.Intrinsics.X86.Aes;
|
||||
#endif
|
||||
|
||||
namespace LibHac.Crypto2
|
||||
{
|
||||
public static class AesCrypto
|
||||
public static class Aes
|
||||
{
|
||||
public const int KeySize128 = 0x10;
|
||||
public const int BlockSize = 0x10;
|
||||
|
@ -16,7 +17,7 @@ namespace LibHac.Crypto2
|
|||
public static bool IsAesNiSupported()
|
||||
{
|
||||
#if HAS_INTRINSICS
|
||||
return Aes.IsSupported;
|
||||
return AesNi.IsSupported;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#if HAS_INTRINSICS
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
|
@ -17,6 +18,8 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv, bool isDecrypting)
|
||||
{
|
||||
Debug.Assert(iv.Length == Aes.BlockSize);
|
||||
|
||||
_aesCore.Initialize(key, isDecrypting);
|
||||
|
||||
_iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv));
|
||||
|
|
|
@ -12,10 +12,10 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv, CipherMode mode, bool isDecrypting)
|
||||
{
|
||||
Debug.Assert(key.Length == AesCrypto.KeySize128);
|
||||
Debug.Assert(iv.IsEmpty || iv.Length == AesCrypto.BlockSize);
|
||||
Debug.Assert(key.Length == Aes.KeySize128);
|
||||
Debug.Assert(iv.IsEmpty || iv.Length == Aes.BlockSize);
|
||||
|
||||
Aes aes = Aes.Create();
|
||||
System.Security.Cryptography.Aes aes = System.Security.Cryptography.Aes.Create();
|
||||
|
||||
if (aes == null) throw new CryptographicException("Unable to create AES object");
|
||||
aes.Key = key.ToArray();
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#if NETCOREAPP
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using AesNi = System.Runtime.Intrinsics.X86.Aes;
|
||||
|
||||
namespace LibHac.Crypto2.Detail
|
||||
{
|
||||
|
@ -17,6 +20,8 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key, bool isDecrypting)
|
||||
{
|
||||
Debug.Assert(key.Length == Aes.KeySize128);
|
||||
|
||||
KeyExpansion(key, MemoryMarshal.CreateSpan(ref _roundKeys, RoundKeyCount), isDecrypting);
|
||||
}
|
||||
|
||||
|
@ -63,16 +68,16 @@ namespace LibHac.Crypto2.Detail
|
|||
ReadOnlySpan<Vector128<byte>> keys = RoundKeys;
|
||||
|
||||
Vector128<byte> b = Sse2.Xor(input, keys[0]);
|
||||
b = Aes.Encrypt(b, keys[1]);
|
||||
b = Aes.Encrypt(b, keys[2]);
|
||||
b = Aes.Encrypt(b, keys[3]);
|
||||
b = Aes.Encrypt(b, keys[4]);
|
||||
b = Aes.Encrypt(b, keys[5]);
|
||||
b = Aes.Encrypt(b, keys[6]);
|
||||
b = Aes.Encrypt(b, keys[7]);
|
||||
b = Aes.Encrypt(b, keys[8]);
|
||||
b = Aes.Encrypt(b, keys[9]);
|
||||
return Aes.EncryptLast(b, keys[10]);
|
||||
b = AesNi.Encrypt(b, keys[1]);
|
||||
b = AesNi.Encrypt(b, keys[2]);
|
||||
b = AesNi.Encrypt(b, keys[3]);
|
||||
b = AesNi.Encrypt(b, keys[4]);
|
||||
b = AesNi.Encrypt(b, keys[5]);
|
||||
b = AesNi.Encrypt(b, keys[6]);
|
||||
b = AesNi.Encrypt(b, keys[7]);
|
||||
b = AesNi.Encrypt(b, keys[8]);
|
||||
b = AesNi.Encrypt(b, keys[9]);
|
||||
return AesNi.EncryptLast(b, keys[10]);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
@ -81,16 +86,16 @@ namespace LibHac.Crypto2.Detail
|
|||
ReadOnlySpan<Vector128<byte>> keys = RoundKeys;
|
||||
|
||||
Vector128<byte> b = Sse2.Xor(input, keys[10]);
|
||||
b = Aes.Decrypt(b, keys[9]);
|
||||
b = Aes.Decrypt(b, keys[8]);
|
||||
b = Aes.Decrypt(b, keys[7]);
|
||||
b = Aes.Decrypt(b, keys[6]);
|
||||
b = Aes.Decrypt(b, keys[5]);
|
||||
b = Aes.Decrypt(b, keys[4]);
|
||||
b = Aes.Decrypt(b, keys[3]);
|
||||
b = Aes.Decrypt(b, keys[2]);
|
||||
b = Aes.Decrypt(b, keys[1]);
|
||||
return Aes.DecryptLast(b, keys[0]);
|
||||
b = AesNi.Decrypt(b, keys[9]);
|
||||
b = AesNi.Decrypt(b, keys[8]);
|
||||
b = AesNi.Decrypt(b, keys[7]);
|
||||
b = AesNi.Decrypt(b, keys[6]);
|
||||
b = AesNi.Decrypt(b, keys[5]);
|
||||
b = AesNi.Decrypt(b, keys[4]);
|
||||
b = AesNi.Decrypt(b, keys[3]);
|
||||
b = AesNi.Decrypt(b, keys[2]);
|
||||
b = AesNi.Decrypt(b, keys[1]);
|
||||
return AesNi.DecryptLast(b, keys[0]);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
|
@ -99,41 +104,41 @@ namespace LibHac.Crypto2.Detail
|
|||
var curKey = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(key));
|
||||
roundKeys[0] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x01));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x01));
|
||||
roundKeys[1] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x02));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x02));
|
||||
roundKeys[2] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x04));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x04));
|
||||
roundKeys[3] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x08));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x08));
|
||||
roundKeys[4] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x10));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x10));
|
||||
roundKeys[5] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x20));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x20));
|
||||
roundKeys[6] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x40));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x40));
|
||||
roundKeys[7] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x80));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x80));
|
||||
roundKeys[8] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x1b));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x1b));
|
||||
roundKeys[9] = curKey;
|
||||
|
||||
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x36));
|
||||
curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x36));
|
||||
roundKeys[10] = curKey;
|
||||
|
||||
if (isDecrypting)
|
||||
{
|
||||
for (int i = 1; i < 10; i++)
|
||||
{
|
||||
roundKeys[i] = Aes.InverseMixColumns(roundKeys[i]);
|
||||
roundKeys[i] = AesNi.InverseMixColumns(roundKeys[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
|
||||
{
|
||||
Debug.Assert(iv.Length == AesCrypto.BlockSize);
|
||||
Debug.Assert(iv.Length == Aes.BlockSize);
|
||||
|
||||
_aesCore = new AesCore();
|
||||
_aesCore.Initialize(key, ReadOnlySpan<byte>.Empty, CipherMode.ECB, false);
|
||||
|
@ -24,8 +24,8 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Transform(ReadOnlySpan<byte> input, Span<byte> output)
|
||||
{
|
||||
int blockCount = Util.DivideByRoundUp(input.Length, AesCrypto.BlockSize);
|
||||
int length = blockCount * AesCrypto.BlockSize;
|
||||
int blockCount = Util.DivideByRoundUp(input.Length, Aes.BlockSize);
|
||||
int length = blockCount * Aes.BlockSize;
|
||||
|
||||
using var counterBuffer = new RentedArray<byte>(length);
|
||||
FillDecryptedCounter(_counter, counterBuffer.Span);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#if HAS_INTRINSICS
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics;
|
||||
|
@ -17,6 +18,8 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
|
||||
{
|
||||
Debug.Assert(iv.Length == Aes.BlockSize);
|
||||
|
||||
_aesCore.Initialize(key, false);
|
||||
|
||||
_iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv));
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key1, ReadOnlySpan<byte> key2, ReadOnlySpan<byte> iv, bool isDecrypting)
|
||||
{
|
||||
Debug.Assert(iv.Length == AesCrypto.BlockSize);
|
||||
Debug.Assert(iv.Length == Aes.BlockSize);
|
||||
|
||||
_dataAesCore = new AesCore();
|
||||
_tweakAesCore = new AesCore();
|
||||
|
@ -33,18 +33,18 @@ namespace LibHac.Crypto2.Detail
|
|||
int leftover = length & 0xF;
|
||||
|
||||
// Data units must be at least 1 block long.
|
||||
if (length < AesCrypto.BlockSize)
|
||||
if (length < Aes.BlockSize)
|
||||
throw new ArgumentException();
|
||||
|
||||
var tweak = new Buffer16();
|
||||
|
||||
_tweakAesCore.Encrypt(_iv, tweak);
|
||||
|
||||
using var tweakBuffer = new RentedArray<byte>(blockCount * AesCrypto.BlockSize);
|
||||
using var tweakBuffer = new RentedArray<byte>(blockCount * Aes.BlockSize);
|
||||
tweak = FillTweakBuffer(tweak, MemoryMarshal.Cast<byte, Buffer16>(tweakBuffer.Span));
|
||||
|
||||
Util.XorArrays(output, input, tweakBuffer.Span);
|
||||
_dataAesCore.Encrypt(output.Slice(0, blockCount * AesCrypto.BlockSize), output);
|
||||
_dataAesCore.Encrypt(output.Slice(0, blockCount * Aes.BlockSize), output);
|
||||
Util.XorArrays(output, output, tweakBuffer.Array);
|
||||
|
||||
if (leftover != 0)
|
||||
|
@ -65,7 +65,7 @@ namespace LibHac.Crypto2.Detail
|
|||
tmp[i] = inBlock[i];
|
||||
}
|
||||
|
||||
for (int i = leftover; i < AesCrypto.BlockSize; i++)
|
||||
for (int i = leftover; i < Aes.BlockSize; i++)
|
||||
{
|
||||
tmp[i] = prevOutBlock[i];
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ namespace LibHac.Crypto2.Detail
|
|||
int leftover = length & 0xF;
|
||||
|
||||
// Data units must be at least 1 block long.
|
||||
if (length < AesCrypto.BlockSize)
|
||||
if (length < Aes.BlockSize)
|
||||
throw new ArgumentException();
|
||||
|
||||
if (leftover != 0) blockCount--;
|
||||
|
@ -94,11 +94,11 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
if (blockCount > 0)
|
||||
{
|
||||
using var tweakBuffer = new RentedArray<byte>(blockCount * AesCrypto.BlockSize);
|
||||
using var tweakBuffer = new RentedArray<byte>(blockCount * Aes.BlockSize);
|
||||
tweak = FillTweakBuffer(tweak, MemoryMarshal.Cast<byte, Buffer16>(tweakBuffer.Span));
|
||||
|
||||
Util.XorArrays(output, input, tweakBuffer.Span);
|
||||
_dataAesCore.Decrypt(output.Slice(0, blockCount * AesCrypto.BlockSize), output);
|
||||
_dataAesCore.Decrypt(output.Slice(0, blockCount * Aes.BlockSize), output);
|
||||
Util.XorArrays(output, output, tweakBuffer.Span);
|
||||
}
|
||||
|
||||
|
@ -128,7 +128,7 @@ namespace LibHac.Crypto2.Detail
|
|||
tmp[i] = finalInBlock[i];
|
||||
}
|
||||
|
||||
for (int i = leftover; i < AesCrypto.BlockSize; i++)
|
||||
for (int i = leftover; i < Aes.BlockSize; i++)
|
||||
{
|
||||
tmp[i] = outBlock[i];
|
||||
}
|
||||
|
|
|
@ -20,9 +20,7 @@ namespace LibHac.Crypto2.Detail
|
|||
|
||||
public void Initialize(ReadOnlySpan<byte> key1, ReadOnlySpan<byte> key2, ReadOnlySpan<byte> iv, bool decrypting)
|
||||
{
|
||||
Debug.Assert(key1.Length == AesCrypto.KeySize128);
|
||||
Debug.Assert(key2.Length == AesCrypto.KeySize128);
|
||||
Debug.Assert(iv.Length == AesCrypto.KeySize128);
|
||||
Debug.Assert(iv.Length == Aes.KeySize128);
|
||||
|
||||
_dataAesCore.Initialize(key1, decrypting);
|
||||
_tweakAesCore.Initialize(key2, false);
|
||||
|
|
|
@ -186,13 +186,13 @@ namespace hactoolnet
|
|||
logger.LogMessage(string.Empty);
|
||||
logger.LogMessage(label);
|
||||
|
||||
if (AesCrypto.IsAesNiSupported())
|
||||
if (Aes.IsAesNiSupported())
|
||||
CipherBenchmark(srcData, dstDataLh, cipherLibHac, Iterations, "LibHac impl: ", logger);
|
||||
CipherBenchmark(srcData, dstDataNet, cipherNet, Iterations, ".NET impl: ", logger);
|
||||
|
||||
if (benchBlocked)
|
||||
{
|
||||
if (AesCrypto.IsAesNiSupported())
|
||||
if (Aes.IsAesNiSupported())
|
||||
CipherBenchmarkBlocked(srcData, dstDataBlockedLh, cipherLibHac, Iterations / 5,
|
||||
"LibHac impl (blocked): ", logger);
|
||||
|
||||
|
@ -202,7 +202,7 @@ namespace hactoolnet
|
|||
|
||||
if (function != null)
|
||||
{
|
||||
if (AesCrypto.IsAesNiSupported())
|
||||
if (Aes.IsAesNiSupported())
|
||||
CipherBenchmarkSeparate(srcData, dstDataSeparateLh, function, Iterations / 5,
|
||||
"LibHac impl (separate): ", false, logger);
|
||||
|
||||
|
@ -210,7 +210,7 @@ namespace hactoolnet
|
|||
".NET impl (separate): ", true, logger);
|
||||
}
|
||||
|
||||
if (AesCrypto.IsAesNiSupported())
|
||||
if (Aes.IsAesNiSupported())
|
||||
{
|
||||
logger.LogMessage($"{dstDataLh.SequenceEqual(dstDataNet)}");
|
||||
|
||||
|
@ -279,17 +279,17 @@ namespace hactoolnet
|
|||
|
||||
case "aesecbnew":
|
||||
{
|
||||
Func<ICipher> encryptorNet = () => AesCrypto.CreateEcbEncryptor(new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => AesCrypto.CreateEcbEncryptor(new byte[0x10]);
|
||||
Func<ICipher> encryptorNet = () => Aes.CreateEcbEncryptor(new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => Aes.CreateEcbEncryptor(new byte[0x10]);
|
||||
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.EncryptEcb128(input, output, key1, crypto);
|
||||
Aes.EncryptEcb128(input, output, key1, crypto);
|
||||
|
||||
RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, true, "AES-ECB encrypt", ctx.Logger);
|
||||
|
||||
Func<ICipher> decryptorNet = () => AesCrypto.CreateEcbDecryptor(new byte[0x10], true);
|
||||
Func<ICipher> decryptorLh = () => AesCrypto.CreateEcbDecryptor(new byte[0x10]);
|
||||
Func<ICipher> decryptorNet = () => Aes.CreateEcbDecryptor(new byte[0x10], true);
|
||||
Func<ICipher> decryptorLh = () => Aes.CreateEcbDecryptor(new byte[0x10]);
|
||||
CipherTaskSeparate decrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.DecryptEcb128(input, output, key1, crypto);
|
||||
Aes.DecryptEcb128(input, output, key1, crypto);
|
||||
|
||||
RunCipherBenchmark(decryptorNet, decryptorLh, decrypt, true, "AES-ECB decrypt", ctx.Logger);
|
||||
|
||||
|
@ -297,17 +297,17 @@ namespace hactoolnet
|
|||
}
|
||||
case "aescbcnew":
|
||||
{
|
||||
Func<ICipher> encryptorNet = () => AesCrypto.CreateCbcEncryptor(new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => AesCrypto.CreateCbcEncryptor(new byte[0x10], new byte[0x10]);
|
||||
Func<ICipher> encryptorNet = () => Aes.CreateCbcEncryptor(new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => Aes.CreateCbcEncryptor(new byte[0x10], new byte[0x10]);
|
||||
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.EncryptCbc128(input, output, key1, iv, crypto);
|
||||
Aes.EncryptCbc128(input, output, key1, iv, crypto);
|
||||
|
||||
RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, true, "AES-CBC encrypt", ctx.Logger);
|
||||
|
||||
Func<ICipher> decryptorNet = () => AesCrypto.CreateCbcDecryptor(new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> decryptorLh = () => AesCrypto.CreateCbcDecryptor(new byte[0x10], new byte[0x10]);
|
||||
Func<ICipher> decryptorNet = () => Aes.CreateCbcDecryptor(new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> decryptorLh = () => Aes.CreateCbcDecryptor(new byte[0x10], new byte[0x10]);
|
||||
CipherTaskSeparate decrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.DecryptCbc128(input, output, key1, iv, crypto);
|
||||
Aes.DecryptCbc128(input, output, key1, iv, crypto);
|
||||
|
||||
RunCipherBenchmark(decryptorNet, decryptorLh, decrypt, true, "AES-CBC decrypt", ctx.Logger);
|
||||
|
||||
|
@ -316,10 +316,10 @@ namespace hactoolnet
|
|||
|
||||
case "aesctrnew":
|
||||
{
|
||||
Func<ICipher> encryptorNet = () => AesCrypto.CreateCtrEncryptor(new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => AesCrypto.CreateCtrEncryptor(new byte[0x10], new byte[0x10]);
|
||||
Func<ICipher> encryptorNet = () => Aes.CreateCtrEncryptor(new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => Aes.CreateCtrEncryptor(new byte[0x10], new byte[0x10]);
|
||||
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.EncryptCtr128(input, output, key1, iv, crypto);
|
||||
Aes.EncryptCtr128(input, output, key1, iv, crypto);
|
||||
|
||||
RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, true, "AES-CTR", ctx.Logger);
|
||||
|
||||
|
@ -327,17 +327,17 @@ namespace hactoolnet
|
|||
}
|
||||
case "aesxtsnew":
|
||||
{
|
||||
Func<ICipher> encryptorNet = () => AesCrypto.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => AesCrypto.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10]);
|
||||
Func<ICipher> encryptorNet = () => Aes.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> encryptorLh = () => Aes.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10]);
|
||||
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.EncryptXts128(input, output, key1, key2, iv, crypto);
|
||||
Aes.EncryptXts128(input, output, key1, key2, iv, crypto);
|
||||
|
||||
RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, false, "AES-XTS encrypt", ctx.Logger);
|
||||
|
||||
Func<ICipher> decryptorNet = () => AesCrypto.CreateXtsDecryptor(new byte[0x10], new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> decryptorLh = () => AesCrypto.CreateXtsDecryptor(new byte[0x10], new byte[0x10], new byte[0x10]);
|
||||
Func<ICipher> decryptorNet = () => Aes.CreateXtsDecryptor(new byte[0x10], new byte[0x10], new byte[0x10], true);
|
||||
Func<ICipher> decryptorLh = () => Aes.CreateXtsDecryptor(new byte[0x10], new byte[0x10], new byte[0x10]);
|
||||
CipherTaskSeparate decrypt = (input, output, key1, key2, iv, crypto) =>
|
||||
AesCrypto.DecryptXts128(input, output, key1, key2, iv, crypto);
|
||||
Aes.DecryptXts128(input, output, key1, key2, iv, crypto);
|
||||
|
||||
RunCipherBenchmark(decryptorNet, decryptorLh, decrypt, false, "AES-XTS decrypt", ctx.Logger);
|
||||
|
||||
|
|
|
@ -21,56 +21,56 @@ namespace LibHac.Tests.CryptoTests
|
|||
[MemberData(nameof(EncryptTestVectors))]
|
||||
public static void Encrypt(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateCbcEncryptor(tv.Key, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateCbcEncryptor(tv.Key, tv.Iv, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DecryptTestVectors))]
|
||||
public static void Decrypt(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateCbcDecryptor(tv.Key, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateCbcDecryptor(tv.Key, tv.Iv, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(EncryptMultiTestVectors))]
|
||||
public static void EncryptMulti(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateCbcEncryptor(tv.Key, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateCbcEncryptor(tv.Key, tv.Iv, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DecryptMultiTestVectors))]
|
||||
public static void DecryptMulti(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateCbcDecryptor(tv.Key, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateCbcDecryptor(tv.Key, tv.Iv, true));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(EncryptTestVectors))]
|
||||
public static void EncryptIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateCbcEncryptor(tv.Key, tv.Iv));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateCbcEncryptor(tv.Key, tv.Iv));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(DecryptTestVectors))]
|
||||
public static void DecryptIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateCbcDecryptor(tv.Key, tv.Iv));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateCbcDecryptor(tv.Key, tv.Iv));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(EncryptMultiTestVectors))]
|
||||
public static void EncryptMultiIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateCbcEncryptor(tv.Key, tv.Iv));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateCbcEncryptor(tv.Key, tv.Iv));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(DecryptMultiTestVectors))]
|
||||
public static void DecryptMultiIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateCbcDecryptor(tv.Key, tv.Iv));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateCbcDecryptor(tv.Key, tv.Iv));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,14 +11,14 @@ namespace LibHac.Tests.CryptoTests
|
|||
[MemberData(nameof(TestVectors))]
|
||||
public static void Transform(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateCtrEncryptor(tv.Key, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateCtrEncryptor(tv.Key, tv.Iv, true));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(TestVectors))]
|
||||
public static void TransformIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateCtrEncryptor(tv.Key, tv.Iv));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateCtrEncryptor(tv.Key, tv.Iv));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,56 +21,56 @@ namespace LibHac.Tests.CryptoTests
|
|||
[MemberData(nameof(EncryptTestVectors))]
|
||||
public static void Encrypt(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateEcbEncryptor(tv.Key, true));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateEcbEncryptor(tv.Key, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DecryptTestVectors))]
|
||||
public static void Decrypt(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateEcbDecryptor(tv.Key, true));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateEcbDecryptor(tv.Key, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(EncryptMultiTestVectors))]
|
||||
public static void EncryptMulti(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateEcbEncryptor(tv.Key, true));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateEcbEncryptor(tv.Key, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DecryptMultiTestVectors))]
|
||||
public static void DecryptMulti(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateEcbDecryptor(tv.Key, true));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateEcbDecryptor(tv.Key, true));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(EncryptTestVectors))]
|
||||
public static void EncryptIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateEcbEncryptor(tv.Key));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateEcbEncryptor(tv.Key));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(DecryptTestVectors))]
|
||||
public static void DecryptIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateEcbDecryptor(tv.Key));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateEcbDecryptor(tv.Key));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(EncryptMultiTestVectors))]
|
||||
public static void EncryptMultiIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateEcbEncryptor(tv.Key));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateEcbEncryptor(tv.Key));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
[MemberData(nameof(DecryptMultiTestVectors))]
|
||||
public static void DecryptMultiIntrinsics(EncryptionTestVector tv)
|
||||
{
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateEcbDecryptor(tv.Key));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateEcbDecryptor(tv.Key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace LibHac.Tests.CryptoTests
|
|||
{
|
||||
public AesIntrinsicsRequiredTheoryAttribute()
|
||||
{
|
||||
if (!AesCrypto.IsAesNiSupported())
|
||||
if (!Aes.IsAesNiSupported())
|
||||
{
|
||||
Skip = "AES intrinsics required";
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ namespace LibHac.Tests.CryptoTests
|
|||
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
|
||||
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
|
||||
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateXtsEncryptor(key1, key2, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateXtsEncryptor(key1, key2, tv.Iv, true));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
@ -49,7 +49,7 @@ namespace LibHac.Tests.CryptoTests
|
|||
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
|
||||
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
|
||||
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateXtsDecryptor(key1, key2, tv.Iv, true));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateXtsDecryptor(key1, key2, tv.Iv, true));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
|
@ -59,7 +59,7 @@ namespace LibHac.Tests.CryptoTests
|
|||
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
|
||||
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
|
||||
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, AesCrypto.CreateXtsEncryptor(key1, key2, tv.Iv));
|
||||
Common.CipherTestCore(tv.PlainText, tv.CipherText, Aes.CreateXtsEncryptor(key1, key2, tv.Iv));
|
||||
}
|
||||
|
||||
[AesIntrinsicsRequiredTheory]
|
||||
|
@ -69,7 +69,7 @@ namespace LibHac.Tests.CryptoTests
|
|||
Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
|
||||
Span<byte> key2 = tv.Key.AsSpan(0x10, 0x10);
|
||||
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, AesCrypto.CreateXtsDecryptor(key1, key2, tv.Iv));
|
||||
Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateXtsDecryptor(key1, key2, tv.Iv));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue