Rename Aes class

This commit is contained in:
Alex Barney 2019-11-18 18:20:21 -07:00
parent 4b2c4d9553
commit 488bd8e682
14 changed files with 112 additions and 102 deletions

View file

@ -2,13 +2,14 @@
using System; using System;
#if HAS_INTRINSICS #if HAS_INTRINSICS
using System.Runtime.Intrinsics.X86;
using LibHac.Crypto2.Detail; using LibHac.Crypto2.Detail;
using AesNi = System.Runtime.Intrinsics.X86.Aes;
#endif #endif
namespace LibHac.Crypto2 namespace LibHac.Crypto2
{ {
public static class AesCrypto public static class Aes
{ {
public const int KeySize128 = 0x10; public const int KeySize128 = 0x10;
public const int BlockSize = 0x10; public const int BlockSize = 0x10;
@ -16,7 +17,7 @@ namespace LibHac.Crypto2
public static bool IsAesNiSupported() public static bool IsAesNiSupported()
{ {
#if HAS_INTRINSICS #if HAS_INTRINSICS
return Aes.IsSupported; return AesNi.IsSupported;
#else #else
return false; return false;
#endif #endif

View file

@ -1,5 +1,6 @@
#if HAS_INTRINSICS #if HAS_INTRINSICS
using System; using System;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Intrinsics; using System.Runtime.Intrinsics;
@ -17,6 +18,8 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv, bool isDecrypting) public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv, bool isDecrypting)
{ {
Debug.Assert(iv.Length == Aes.BlockSize);
_aesCore.Initialize(key, isDecrypting); _aesCore.Initialize(key, isDecrypting);
_iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv)); _iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv));

View file

@ -12,10 +12,10 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv, CipherMode mode, bool isDecrypting) public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv, CipherMode mode, bool isDecrypting)
{ {
Debug.Assert(key.Length == AesCrypto.KeySize128); Debug.Assert(key.Length == Aes.KeySize128);
Debug.Assert(iv.IsEmpty || iv.Length == AesCrypto.BlockSize); 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"); if (aes == null) throw new CryptographicException("Unable to create AES object");
aes.Key = key.ToArray(); aes.Key = key.ToArray();

View file

@ -1,9 +1,12 @@
#if NETCOREAPP #if NETCOREAPP
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Intrinsics; using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86; using System.Runtime.Intrinsics.X86;
using System;
using System.Runtime.CompilerServices; using AesNi = System.Runtime.Intrinsics.X86.Aes;
namespace LibHac.Crypto2.Detail namespace LibHac.Crypto2.Detail
{ {
@ -17,6 +20,8 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key, bool isDecrypting) public void Initialize(ReadOnlySpan<byte> key, bool isDecrypting)
{ {
Debug.Assert(key.Length == Aes.KeySize128);
KeyExpansion(key, MemoryMarshal.CreateSpan(ref _roundKeys, RoundKeyCount), isDecrypting); KeyExpansion(key, MemoryMarshal.CreateSpan(ref _roundKeys, RoundKeyCount), isDecrypting);
} }
@ -63,16 +68,16 @@ namespace LibHac.Crypto2.Detail
ReadOnlySpan<Vector128<byte>> keys = RoundKeys; ReadOnlySpan<Vector128<byte>> keys = RoundKeys;
Vector128<byte> b = Sse2.Xor(input, keys[0]); Vector128<byte> b = Sse2.Xor(input, keys[0]);
b = Aes.Encrypt(b, keys[1]); b = AesNi.Encrypt(b, keys[1]);
b = Aes.Encrypt(b, keys[2]); b = AesNi.Encrypt(b, keys[2]);
b = Aes.Encrypt(b, keys[3]); b = AesNi.Encrypt(b, keys[3]);
b = Aes.Encrypt(b, keys[4]); b = AesNi.Encrypt(b, keys[4]);
b = Aes.Encrypt(b, keys[5]); b = AesNi.Encrypt(b, keys[5]);
b = Aes.Encrypt(b, keys[6]); b = AesNi.Encrypt(b, keys[6]);
b = Aes.Encrypt(b, keys[7]); b = AesNi.Encrypt(b, keys[7]);
b = Aes.Encrypt(b, keys[8]); b = AesNi.Encrypt(b, keys[8]);
b = Aes.Encrypt(b, keys[9]); b = AesNi.Encrypt(b, keys[9]);
return Aes.EncryptLast(b, keys[10]); return AesNi.EncryptLast(b, keys[10]);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -81,16 +86,16 @@ namespace LibHac.Crypto2.Detail
ReadOnlySpan<Vector128<byte>> keys = RoundKeys; ReadOnlySpan<Vector128<byte>> keys = RoundKeys;
Vector128<byte> b = Sse2.Xor(input, keys[10]); Vector128<byte> b = Sse2.Xor(input, keys[10]);
b = Aes.Decrypt(b, keys[9]); b = AesNi.Decrypt(b, keys[9]);
b = Aes.Decrypt(b, keys[8]); b = AesNi.Decrypt(b, keys[8]);
b = Aes.Decrypt(b, keys[7]); b = AesNi.Decrypt(b, keys[7]);
b = Aes.Decrypt(b, keys[6]); b = AesNi.Decrypt(b, keys[6]);
b = Aes.Decrypt(b, keys[5]); b = AesNi.Decrypt(b, keys[5]);
b = Aes.Decrypt(b, keys[4]); b = AesNi.Decrypt(b, keys[4]);
b = Aes.Decrypt(b, keys[3]); b = AesNi.Decrypt(b, keys[3]);
b = Aes.Decrypt(b, keys[2]); b = AesNi.Decrypt(b, keys[2]);
b = Aes.Decrypt(b, keys[1]); b = AesNi.Decrypt(b, keys[1]);
return Aes.DecryptLast(b, keys[0]); return AesNi.DecryptLast(b, keys[0]);
} }
[MethodImpl(MethodImplOptions.AggressiveOptimization)] [MethodImpl(MethodImplOptions.AggressiveOptimization)]
@ -99,41 +104,41 @@ namespace LibHac.Crypto2.Detail
var curKey = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(key)); var curKey = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(key));
roundKeys[0] = curKey; roundKeys[0] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x01)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x01));
roundKeys[1] = curKey; roundKeys[1] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x02)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x02));
roundKeys[2] = curKey; roundKeys[2] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x04)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x04));
roundKeys[3] = curKey; roundKeys[3] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x08)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x08));
roundKeys[4] = curKey; roundKeys[4] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x10)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x10));
roundKeys[5] = curKey; roundKeys[5] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x20)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x20));
roundKeys[6] = curKey; roundKeys[6] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x40)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x40));
roundKeys[7] = curKey; roundKeys[7] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x80)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x80));
roundKeys[8] = curKey; roundKeys[8] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x1b)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x1b));
roundKeys[9] = curKey; roundKeys[9] = curKey;
curKey = KeyExpansion(curKey, Aes.KeygenAssist(curKey, 0x36)); curKey = KeyExpansion(curKey, AesNi.KeygenAssist(curKey, 0x36));
roundKeys[10] = curKey; roundKeys[10] = curKey;
if (isDecrypting) if (isDecrypting)
{ {
for (int i = 1; i < 10; i++) for (int i = 1; i < 10; i++)
{ {
roundKeys[i] = Aes.InverseMixColumns(roundKeys[i]); roundKeys[i] = AesNi.InverseMixColumns(roundKeys[i]);
} }
} }
} }

View file

@ -14,7 +14,7 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv) 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 = new AesCore();
_aesCore.Initialize(key, ReadOnlySpan<byte>.Empty, CipherMode.ECB, false); _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) public void Transform(ReadOnlySpan<byte> input, Span<byte> output)
{ {
int blockCount = Util.DivideByRoundUp(input.Length, AesCrypto.BlockSize); int blockCount = Util.DivideByRoundUp(input.Length, Aes.BlockSize);
int length = blockCount * AesCrypto.BlockSize; int length = blockCount * Aes.BlockSize;
using var counterBuffer = new RentedArray<byte>(length); using var counterBuffer = new RentedArray<byte>(length);
FillDecryptedCounter(_counter, counterBuffer.Span); FillDecryptedCounter(_counter, counterBuffer.Span);

View file

@ -1,5 +1,6 @@
#if HAS_INTRINSICS #if HAS_INTRINSICS
using System; using System;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Intrinsics; using System.Runtime.Intrinsics;
@ -17,6 +18,8 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv) public void Initialize(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)
{ {
Debug.Assert(iv.Length == Aes.BlockSize);
_aesCore.Initialize(key, false); _aesCore.Initialize(key, false);
_iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv)); _iv = Unsafe.ReadUnaligned<Vector128<byte>>(ref MemoryMarshal.GetReference(iv));

View file

@ -15,7 +15,7 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key1, ReadOnlySpan<byte> key2, ReadOnlySpan<byte> iv, bool isDecrypting) 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(); _dataAesCore = new AesCore();
_tweakAesCore = new AesCore(); _tweakAesCore = new AesCore();
@ -33,18 +33,18 @@ namespace LibHac.Crypto2.Detail
int leftover = length & 0xF; int leftover = length & 0xF;
// Data units must be at least 1 block long. // Data units must be at least 1 block long.
if (length < AesCrypto.BlockSize) if (length < Aes.BlockSize)
throw new ArgumentException(); throw new ArgumentException();
var tweak = new Buffer16(); var tweak = new Buffer16();
_tweakAesCore.Encrypt(_iv, tweak); _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)); tweak = FillTweakBuffer(tweak, MemoryMarshal.Cast<byte, Buffer16>(tweakBuffer.Span));
Util.XorArrays(output, input, 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); Util.XorArrays(output, output, tweakBuffer.Array);
if (leftover != 0) if (leftover != 0)
@ -65,7 +65,7 @@ namespace LibHac.Crypto2.Detail
tmp[i] = inBlock[i]; tmp[i] = inBlock[i];
} }
for (int i = leftover; i < AesCrypto.BlockSize; i++) for (int i = leftover; i < Aes.BlockSize; i++)
{ {
tmp[i] = prevOutBlock[i]; tmp[i] = prevOutBlock[i];
} }
@ -83,7 +83,7 @@ namespace LibHac.Crypto2.Detail
int leftover = length & 0xF; int leftover = length & 0xF;
// Data units must be at least 1 block long. // Data units must be at least 1 block long.
if (length < AesCrypto.BlockSize) if (length < Aes.BlockSize)
throw new ArgumentException(); throw new ArgumentException();
if (leftover != 0) blockCount--; if (leftover != 0) blockCount--;
@ -94,11 +94,11 @@ namespace LibHac.Crypto2.Detail
if (blockCount > 0) 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)); tweak = FillTweakBuffer(tweak, MemoryMarshal.Cast<byte, Buffer16>(tweakBuffer.Span));
Util.XorArrays(output, input, 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); Util.XorArrays(output, output, tweakBuffer.Span);
} }
@ -128,7 +128,7 @@ namespace LibHac.Crypto2.Detail
tmp[i] = finalInBlock[i]; tmp[i] = finalInBlock[i];
} }
for (int i = leftover; i < AesCrypto.BlockSize; i++) for (int i = leftover; i < Aes.BlockSize; i++)
{ {
tmp[i] = outBlock[i]; tmp[i] = outBlock[i];
} }

View file

@ -20,9 +20,7 @@ namespace LibHac.Crypto2.Detail
public void Initialize(ReadOnlySpan<byte> key1, ReadOnlySpan<byte> key2, ReadOnlySpan<byte> iv, bool decrypting) public void Initialize(ReadOnlySpan<byte> key1, ReadOnlySpan<byte> key2, ReadOnlySpan<byte> iv, bool decrypting)
{ {
Debug.Assert(key1.Length == AesCrypto.KeySize128); Debug.Assert(iv.Length == Aes.KeySize128);
Debug.Assert(key2.Length == AesCrypto.KeySize128);
Debug.Assert(iv.Length == AesCrypto.KeySize128);
_dataAesCore.Initialize(key1, decrypting); _dataAesCore.Initialize(key1, decrypting);
_tweakAesCore.Initialize(key2, false); _tweakAesCore.Initialize(key2, false);

View file

@ -186,13 +186,13 @@ namespace hactoolnet
logger.LogMessage(string.Empty); logger.LogMessage(string.Empty);
logger.LogMessage(label); logger.LogMessage(label);
if (AesCrypto.IsAesNiSupported()) if (Aes.IsAesNiSupported())
CipherBenchmark(srcData, dstDataLh, cipherLibHac, Iterations, "LibHac impl: ", logger); CipherBenchmark(srcData, dstDataLh, cipherLibHac, Iterations, "LibHac impl: ", logger);
CipherBenchmark(srcData, dstDataNet, cipherNet, Iterations, ".NET impl: ", logger); CipherBenchmark(srcData, dstDataNet, cipherNet, Iterations, ".NET impl: ", logger);
if (benchBlocked) if (benchBlocked)
{ {
if (AesCrypto.IsAesNiSupported()) if (Aes.IsAesNiSupported())
CipherBenchmarkBlocked(srcData, dstDataBlockedLh, cipherLibHac, Iterations / 5, CipherBenchmarkBlocked(srcData, dstDataBlockedLh, cipherLibHac, Iterations / 5,
"LibHac impl (blocked): ", logger); "LibHac impl (blocked): ", logger);
@ -202,7 +202,7 @@ namespace hactoolnet
if (function != null) if (function != null)
{ {
if (AesCrypto.IsAesNiSupported()) if (Aes.IsAesNiSupported())
CipherBenchmarkSeparate(srcData, dstDataSeparateLh, function, Iterations / 5, CipherBenchmarkSeparate(srcData, dstDataSeparateLh, function, Iterations / 5,
"LibHac impl (separate): ", false, logger); "LibHac impl (separate): ", false, logger);
@ -210,7 +210,7 @@ namespace hactoolnet
".NET impl (separate): ", true, logger); ".NET impl (separate): ", true, logger);
} }
if (AesCrypto.IsAesNiSupported()) if (Aes.IsAesNiSupported())
{ {
logger.LogMessage($"{dstDataLh.SequenceEqual(dstDataNet)}"); logger.LogMessage($"{dstDataLh.SequenceEqual(dstDataNet)}");
@ -279,17 +279,17 @@ namespace hactoolnet
case "aesecbnew": case "aesecbnew":
{ {
Func<ICipher> encryptorNet = () => AesCrypto.CreateEcbEncryptor(new byte[0x10], true); Func<ICipher> encryptorNet = () => Aes.CreateEcbEncryptor(new byte[0x10], true);
Func<ICipher> encryptorLh = () => AesCrypto.CreateEcbEncryptor(new byte[0x10]); Func<ICipher> encryptorLh = () => Aes.CreateEcbEncryptor(new byte[0x10]);
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) => 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); RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, true, "AES-ECB encrypt", ctx.Logger);
Func<ICipher> decryptorNet = () => AesCrypto.CreateEcbDecryptor(new byte[0x10], true); Func<ICipher> decryptorNet = () => Aes.CreateEcbDecryptor(new byte[0x10], true);
Func<ICipher> decryptorLh = () => AesCrypto.CreateEcbDecryptor(new byte[0x10]); Func<ICipher> decryptorLh = () => Aes.CreateEcbDecryptor(new byte[0x10]);
CipherTaskSeparate decrypt = (input, output, key1, key2, iv, crypto) => 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); RunCipherBenchmark(decryptorNet, decryptorLh, decrypt, true, "AES-ECB decrypt", ctx.Logger);
@ -297,17 +297,17 @@ namespace hactoolnet
} }
case "aescbcnew": case "aescbcnew":
{ {
Func<ICipher> encryptorNet = () => AesCrypto.CreateCbcEncryptor(new byte[0x10], new byte[0x10], true); Func<ICipher> encryptorNet = () => Aes.CreateCbcEncryptor(new byte[0x10], new byte[0x10], true);
Func<ICipher> encryptorLh = () => AesCrypto.CreateCbcEncryptor(new byte[0x10], new byte[0x10]); Func<ICipher> encryptorLh = () => Aes.CreateCbcEncryptor(new byte[0x10], new byte[0x10]);
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) => 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); RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, true, "AES-CBC encrypt", ctx.Logger);
Func<ICipher> decryptorNet = () => AesCrypto.CreateCbcDecryptor(new byte[0x10], new byte[0x10], true); Func<ICipher> decryptorNet = () => Aes.CreateCbcDecryptor(new byte[0x10], new byte[0x10], true);
Func<ICipher> decryptorLh = () => AesCrypto.CreateCbcDecryptor(new byte[0x10], new byte[0x10]); Func<ICipher> decryptorLh = () => Aes.CreateCbcDecryptor(new byte[0x10], new byte[0x10]);
CipherTaskSeparate decrypt = (input, output, key1, key2, iv, crypto) => 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); RunCipherBenchmark(decryptorNet, decryptorLh, decrypt, true, "AES-CBC decrypt", ctx.Logger);
@ -316,10 +316,10 @@ namespace hactoolnet
case "aesctrnew": case "aesctrnew":
{ {
Func<ICipher> encryptorNet = () => AesCrypto.CreateCtrEncryptor(new byte[0x10], new byte[0x10], true); Func<ICipher> encryptorNet = () => Aes.CreateCtrEncryptor(new byte[0x10], new byte[0x10], true);
Func<ICipher> encryptorLh = () => AesCrypto.CreateCtrEncryptor(new byte[0x10], new byte[0x10]); Func<ICipher> encryptorLh = () => Aes.CreateCtrEncryptor(new byte[0x10], new byte[0x10]);
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) => 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); RunCipherBenchmark(encryptorNet, encryptorLh, encrypt, true, "AES-CTR", ctx.Logger);
@ -327,17 +327,17 @@ namespace hactoolnet
} }
case "aesxtsnew": case "aesxtsnew":
{ {
Func<ICipher> encryptorNet = () => AesCrypto.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10], true); Func<ICipher> encryptorNet = () => Aes.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> encryptorLh = () => Aes.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10]);
CipherTaskSeparate encrypt = (input, output, key1, key2, iv, crypto) => 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); 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> decryptorNet = () => Aes.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> decryptorLh = () => Aes.CreateXtsDecryptor(new byte[0x10], new byte[0x10], new byte[0x10]);
CipherTaskSeparate decrypt = (input, output, key1, key2, iv, crypto) => 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); RunCipherBenchmark(decryptorNet, decryptorLh, decrypt, false, "AES-XTS decrypt", ctx.Logger);

View file

@ -21,56 +21,56 @@ namespace LibHac.Tests.CryptoTests
[MemberData(nameof(EncryptTestVectors))] [MemberData(nameof(EncryptTestVectors))]
public static void Encrypt(EncryptionTestVector tv) 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] [Theory]
[MemberData(nameof(DecryptTestVectors))] [MemberData(nameof(DecryptTestVectors))]
public static void Decrypt(EncryptionTestVector tv) 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] [Theory]
[MemberData(nameof(EncryptMultiTestVectors))] [MemberData(nameof(EncryptMultiTestVectors))]
public static void EncryptMulti(EncryptionTestVector tv) 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] [Theory]
[MemberData(nameof(DecryptMultiTestVectors))] [MemberData(nameof(DecryptMultiTestVectors))]
public static void DecryptMulti(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(EncryptTestVectors))] [MemberData(nameof(EncryptTestVectors))]
public static void EncryptIntrinsics(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(DecryptTestVectors))] [MemberData(nameof(DecryptTestVectors))]
public static void DecryptIntrinsics(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(EncryptMultiTestVectors))] [MemberData(nameof(EncryptMultiTestVectors))]
public static void EncryptMultiIntrinsics(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(DecryptMultiTestVectors))] [MemberData(nameof(DecryptMultiTestVectors))]
public static void DecryptMultiIntrinsics(EncryptionTestVector tv) 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));
} }
} }
} }

View file

@ -11,14 +11,14 @@ namespace LibHac.Tests.CryptoTests
[MemberData(nameof(TestVectors))] [MemberData(nameof(TestVectors))]
public static void Transform(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(TestVectors))] [MemberData(nameof(TestVectors))]
public static void TransformIntrinsics(EncryptionTestVector tv) 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));
} }
} }
} }

View file

@ -21,56 +21,56 @@ namespace LibHac.Tests.CryptoTests
[MemberData(nameof(EncryptTestVectors))] [MemberData(nameof(EncryptTestVectors))]
public static void Encrypt(EncryptionTestVector tv) 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] [Theory]
[MemberData(nameof(DecryptTestVectors))] [MemberData(nameof(DecryptTestVectors))]
public static void Decrypt(EncryptionTestVector tv) 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] [Theory]
[MemberData(nameof(EncryptMultiTestVectors))] [MemberData(nameof(EncryptMultiTestVectors))]
public static void EncryptMulti(EncryptionTestVector tv) 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] [Theory]
[MemberData(nameof(DecryptMultiTestVectors))] [MemberData(nameof(DecryptMultiTestVectors))]
public static void DecryptMulti(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(EncryptTestVectors))] [MemberData(nameof(EncryptTestVectors))]
public static void EncryptIntrinsics(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(DecryptTestVectors))] [MemberData(nameof(DecryptTestVectors))]
public static void DecryptIntrinsics(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(EncryptMultiTestVectors))] [MemberData(nameof(EncryptMultiTestVectors))]
public static void EncryptMultiIntrinsics(EncryptionTestVector tv) 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] [AesIntrinsicsRequiredTheory]
[MemberData(nameof(DecryptMultiTestVectors))] [MemberData(nameof(DecryptMultiTestVectors))]
public static void DecryptMultiIntrinsics(EncryptionTestVector tv) 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));
} }
} }
} }

View file

@ -7,7 +7,7 @@ namespace LibHac.Tests.CryptoTests
{ {
public AesIntrinsicsRequiredTheoryAttribute() public AesIntrinsicsRequiredTheoryAttribute()
{ {
if (!AesCrypto.IsAesNiSupported()) if (!Aes.IsAesNiSupported())
{ {
Skip = "AES intrinsics required"; Skip = "AES intrinsics required";
} }

View file

@ -39,7 +39,7 @@ namespace LibHac.Tests.CryptoTests
Span<byte> key1 = tv.Key.AsSpan(0, 0x10); Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 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] [Theory]
@ -49,7 +49,7 @@ namespace LibHac.Tests.CryptoTests
Span<byte> key1 = tv.Key.AsSpan(0, 0x10); Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 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] [AesIntrinsicsRequiredTheory]
@ -59,7 +59,7 @@ namespace LibHac.Tests.CryptoTests
Span<byte> key1 = tv.Key.AsSpan(0, 0x10); Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 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] [AesIntrinsicsRequiredTheory]
@ -69,7 +69,7 @@ namespace LibHac.Tests.CryptoTests
Span<byte> key1 = tv.Key.AsSpan(0, 0x10); Span<byte> key1 = tv.Key.AsSpan(0, 0x10);
Span<byte> key2 = tv.Key.AsSpan(0x10, 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));
} }
} }
} }