From c9352fcb5ada43dbc838f8381dbeeefa50e07b06 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Thu, 3 Mar 2022 16:16:08 -0700 Subject: [PATCH] Make AES crypto return the number of bytes written --- src/LibHac/Boot/Package1.cs | 2 +- src/LibHac/Crypto/Aes.cs | 58 +++++++++++--------------- src/LibHac/Crypto/AesCbcCipher.cs | 10 ++--- src/LibHac/Crypto/AesCbcCipherNi.cs | 10 ++--- src/LibHac/Crypto/AesCtrCipher.cs | 6 +-- src/LibHac/Crypto/AesCtrCipherNi.cs | 6 +-- src/LibHac/Crypto/AesEcbCipher.cs | 10 ++--- src/LibHac/Crypto/AesEcbCipherNi.cs | 10 ++--- src/LibHac/Crypto/AesXtsCipher.cs | 10 ++--- src/LibHac/Crypto/AesXtsCipherNi.cs | 10 ++--- src/LibHac/Crypto/ICipher.cs | 4 +- src/LibHac/Crypto/Impl/AesCbcMode.cs | 10 ++--- src/LibHac/Crypto/Impl/AesCbcModeNi.cs | 8 +++- src/LibHac/Crypto/Impl/AesCore.cs | 28 +++++++------ src/LibHac/Crypto/Impl/AesCoreNi.cs | 12 ++++-- src/LibHac/Crypto/Impl/AesCtrMode.cs | 6 ++- src/LibHac/Crypto/Impl/AesCtrModeNi.cs | 7 +++- src/LibHac/Crypto/Impl/AesEcbMode.cs | 10 ++--- src/LibHac/Crypto/Impl/AesEcbModeNi.cs | 10 ++--- src/LibHac/Crypto/Impl/AesXtsMode.cs | 8 +++- src/LibHac/Crypto/Impl/AesXtsModeNi.cs | 10 +++-- 21 files changed, 131 insertions(+), 114 deletions(-) diff --git a/src/LibHac/Boot/Package1.cs b/src/LibHac/Boot/Package1.cs index 03b16e05..eae84d17 100644 --- a/src/LibHac/Boot/Package1.cs +++ b/src/LibHac/Boot/Package1.cs @@ -300,7 +300,7 @@ public class Package1 return Result.Success; } - private delegate void Decryptor(ReadOnlySpan input, Span output, ReadOnlySpan key, + private delegate int Decryptor(ReadOnlySpan input, Span output, ReadOnlySpan key, ReadOnlySpan iv, bool preferDotNetCrypto = false); private bool TryFindEristaKeyRevision() diff --git a/src/LibHac/Crypto/Aes.cs b/src/LibHac/Crypto/Aes.cs index eb2bcd08..28ae12c7 100644 --- a/src/LibHac/Crypto/Aes.cs +++ b/src/LibHac/Crypto/Aes.cs @@ -102,7 +102,7 @@ public static class Aes return new AesXtsEncryptor(key1, key2, iv); } - public static void EncryptEcb128(ReadOnlySpan input, Span output, ReadOnlySpan key, + public static int EncryptEcb128(ReadOnlySpan input, Span output, ReadOnlySpan key, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -110,16 +110,15 @@ public static class Aes Unsafe.SkipInit(out AesEcbModeNi cipherNi); cipherNi.Initialize(key, false); - cipherNi.Encrypt(input, output); - return; + return cipherNi.Encrypt(input, output); } ICipher cipher = CreateEcbEncryptor(key, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void DecryptEcb128(ReadOnlySpan input, Span output, ReadOnlySpan key, + public static int DecryptEcb128(ReadOnlySpan input, Span output, ReadOnlySpan key, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -127,16 +126,15 @@ public static class Aes Unsafe.SkipInit(out AesEcbModeNi cipherNi); cipherNi.Initialize(key, true); - cipherNi.Decrypt(input, output); - return; + return cipherNi.Decrypt(input, output); } ICipher cipher = CreateEcbDecryptor(key, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void EncryptCbc128(ReadOnlySpan input, Span output, ReadOnlySpan key, + public static int EncryptCbc128(ReadOnlySpan input, Span output, ReadOnlySpan key, ReadOnlySpan iv, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -144,16 +142,15 @@ public static class Aes Unsafe.SkipInit(out AesCbcModeNi cipherNi); cipherNi.Initialize(key, iv, false); - cipherNi.Encrypt(input, output); - return; + return cipherNi.Encrypt(input, output); } ICipher cipher = CreateCbcEncryptor(key, iv, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void DecryptCbc128(ReadOnlySpan input, Span output, ReadOnlySpan key, + public static int DecryptCbc128(ReadOnlySpan input, Span output, ReadOnlySpan key, ReadOnlySpan iv, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -161,16 +158,15 @@ public static class Aes Unsafe.SkipInit(out AesCbcModeNi cipherNi); cipherNi.Initialize(key, iv, true); - cipherNi.Decrypt(input, output); - return; + return cipherNi.Decrypt(input, output); } ICipher cipher = CreateCbcDecryptor(key, iv, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void EncryptCtr128(ReadOnlySpan input, Span output, ReadOnlySpan key, + public static int EncryptCtr128(ReadOnlySpan input, Span output, ReadOnlySpan key, ReadOnlySpan iv, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -178,16 +174,15 @@ public static class Aes Unsafe.SkipInit(out AesCtrModeNi cipherNi); cipherNi.Initialize(key, iv); - cipherNi.Transform(input, output); - return; + return cipherNi.Transform(input, output); } ICipher cipher = CreateCtrEncryptor(key, iv, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void DecryptCtr128(ReadOnlySpan input, Span output, ReadOnlySpan key, + public static int DecryptCtr128(ReadOnlySpan input, Span output, ReadOnlySpan key, ReadOnlySpan iv, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -195,16 +190,15 @@ public static class Aes Unsafe.SkipInit(out AesCtrModeNi cipherNi); cipherNi.Initialize(key, iv); - cipherNi.Transform(input, output); - return; + return cipherNi.Transform(input, output); } ICipher cipher = CreateCtrDecryptor(key, iv, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void EncryptXts128(ReadOnlySpan input, Span output, ReadOnlySpan key1, + public static int EncryptXts128(ReadOnlySpan input, Span output, ReadOnlySpan key1, ReadOnlySpan key2, ReadOnlySpan iv, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -212,16 +206,15 @@ public static class Aes Unsafe.SkipInit(out AesXtsModeNi cipherNi); cipherNi.Initialize(key1, key2, iv, false); - cipherNi.Encrypt(input, output); - return; + return cipherNi.Encrypt(input, output); } ICipher cipher = CreateXtsEncryptor(key1, key2, iv, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } - public static void DecryptXts128(ReadOnlySpan input, Span output, ReadOnlySpan key1, + public static int DecryptXts128(ReadOnlySpan input, Span output, ReadOnlySpan key1, ReadOnlySpan key2, ReadOnlySpan iv, bool preferDotNetCrypto = false) { if (IsAesNiSupported() && !preferDotNetCrypto) @@ -229,13 +222,12 @@ public static class Aes Unsafe.SkipInit(out AesXtsModeNi cipherNi); cipherNi.Initialize(key1, key2, iv, true); - cipherNi.Decrypt(input, output); - return; + return cipherNi.Decrypt(input, output); } ICipher cipher = CreateXtsDecryptor(key1, key2, iv, preferDotNetCrypto); - cipher.Transform(input, output); + return cipher.Transform(input, output); } /// @@ -307,4 +299,4 @@ public static class Aes carry = (byte)((b & 0xff00) >> 8); } } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesCbcCipher.cs b/src/LibHac/Crypto/AesCbcCipher.cs index 4502d6de..1b8138b9 100644 --- a/src/LibHac/Crypto/AesCbcCipher.cs +++ b/src/LibHac/Crypto/AesCbcCipher.cs @@ -13,9 +13,9 @@ public class AesCbcEncryptor : ICipher _baseCipher.Initialize(key, iv, false); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Encrypt(input, output); + return _baseCipher.Encrypt(input, output); } } @@ -29,8 +29,8 @@ public class AesCbcDecryptor : ICipher _baseCipher.Initialize(key, iv, true); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Decrypt(input, output); + return _baseCipher.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesCbcCipherNi.cs b/src/LibHac/Crypto/AesCbcCipherNi.cs index 418bb3e0..96cc098c 100644 --- a/src/LibHac/Crypto/AesCbcCipherNi.cs +++ b/src/LibHac/Crypto/AesCbcCipherNi.cs @@ -18,9 +18,9 @@ public class AesCbcEncryptorNi : ICipherWithIv _baseCipher.Initialize(key, iv, false); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Encrypt(input, output); + return _baseCipher.Encrypt(input, output); } } @@ -36,8 +36,8 @@ public class AesCbcDecryptorNi : ICipherWithIv _baseCipher.Initialize(key, iv, true); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Decrypt(input, output); + return _baseCipher.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesCtrCipher.cs b/src/LibHac/Crypto/AesCtrCipher.cs index 6661b53e..45a3e6f8 100644 --- a/src/LibHac/Crypto/AesCtrCipher.cs +++ b/src/LibHac/Crypto/AesCtrCipher.cs @@ -16,8 +16,8 @@ public class AesCtrCipher : ICipherWithIv _baseCipher.Initialize(key, iv); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Transform(input, output); + return _baseCipher.Transform(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesCtrCipherNi.cs b/src/LibHac/Crypto/AesCtrCipherNi.cs index 6474b533..30a1b106 100644 --- a/src/LibHac/Crypto/AesCtrCipherNi.cs +++ b/src/LibHac/Crypto/AesCtrCipherNi.cs @@ -18,8 +18,8 @@ public class AesCtrCipherNi : ICipherWithIv _baseCipher.Initialize(key, iv); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Transform(input, output); + return _baseCipher.Transform(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesEcbCipher.cs b/src/LibHac/Crypto/AesEcbCipher.cs index 887acf27..0a35e7bb 100644 --- a/src/LibHac/Crypto/AesEcbCipher.cs +++ b/src/LibHac/Crypto/AesEcbCipher.cs @@ -13,9 +13,9 @@ public class AesEcbEncryptor : ICipher _baseCipher.Initialize(key, false); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Encrypt(input, output); + return _baseCipher.Encrypt(input, output); } } @@ -29,8 +29,8 @@ public class AesEcbDecryptor : ICipher _baseCipher.Initialize(key, true); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Decrypt(input, output); + return _baseCipher.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesEcbCipherNi.cs b/src/LibHac/Crypto/AesEcbCipherNi.cs index 17a9f43e..0002f8bd 100644 --- a/src/LibHac/Crypto/AesEcbCipherNi.cs +++ b/src/LibHac/Crypto/AesEcbCipherNi.cs @@ -13,9 +13,9 @@ public class AesEcbEncryptorNi : ICipher _baseCipher.Initialize(key, false); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Encrypt(input, output); + return _baseCipher.Encrypt(input, output); } } @@ -29,8 +29,8 @@ public class AesEcbDecryptorNi : ICipher _baseCipher.Initialize(key, true); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Decrypt(input, output); + return _baseCipher.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesXtsCipher.cs b/src/LibHac/Crypto/AesXtsCipher.cs index 57ddb18d..04c65419 100644 --- a/src/LibHac/Crypto/AesXtsCipher.cs +++ b/src/LibHac/Crypto/AesXtsCipher.cs @@ -16,9 +16,9 @@ public class AesXtsEncryptor : ICipherWithIv _baseCipher.Initialize(key1, key2, iv, false); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Encrypt(input, output); + return _baseCipher.Encrypt(input, output); } } @@ -34,8 +34,8 @@ public class AesXtsDecryptor : ICipherWithIv _baseCipher.Initialize(key1, key2, iv, true); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Decrypt(input, output); + return _baseCipher.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/AesXtsCipherNi.cs b/src/LibHac/Crypto/AesXtsCipherNi.cs index 6799378f..06b06dec 100644 --- a/src/LibHac/Crypto/AesXtsCipherNi.cs +++ b/src/LibHac/Crypto/AesXtsCipherNi.cs @@ -18,9 +18,9 @@ public class AesXtsEncryptorNi : ICipherWithIv _baseCipher.Initialize(key1, key2, iv, false); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Encrypt(input, output); + return _baseCipher.Encrypt(input, output); } } @@ -36,8 +36,8 @@ public class AesXtsDecryptorNi : ICipherWithIv _baseCipher.Initialize(key1, key2, iv, true); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - _baseCipher.Decrypt(input, output); + return _baseCipher.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/ICipher.cs b/src/LibHac/Crypto/ICipher.cs index 1ee75e81..5d2b6f76 100644 --- a/src/LibHac/Crypto/ICipher.cs +++ b/src/LibHac/Crypto/ICipher.cs @@ -5,10 +5,10 @@ namespace LibHac.Crypto; public interface ICipher { - void Transform(ReadOnlySpan input, Span output); + int Transform(ReadOnlySpan input, Span output); } public interface ICipherWithIv : ICipher { ref Buffer16 Iv { get; } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesCbcMode.cs b/src/LibHac/Crypto/Impl/AesCbcMode.cs index 09c7e1ea..009b453a 100644 --- a/src/LibHac/Crypto/Impl/AesCbcMode.cs +++ b/src/LibHac/Crypto/Impl/AesCbcMode.cs @@ -13,13 +13,13 @@ public struct AesCbcMode _aesCore.Initialize(key, iv, CipherMode.CBC, isDecrypting); } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { - _aesCore.Encrypt(input, output); + return _aesCore.Encrypt(input, output); } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { - _aesCore.Decrypt(input, output); + return _aesCore.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesCbcModeNi.cs b/src/LibHac/Crypto/Impl/AesCbcModeNi.cs index 81983c18..0f8efc67 100644 --- a/src/LibHac/Crypto/Impl/AesCbcModeNi.cs +++ b/src/LibHac/Crypto/Impl/AesCbcModeNi.cs @@ -22,7 +22,7 @@ public struct AesCbcModeNi Iv = Unsafe.ReadUnaligned>(ref MemoryMarshal.GetReference(iv)); } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { int blockCount = Math.Min(input.Length, output.Length) >> 4; @@ -42,9 +42,11 @@ public struct AesCbcModeNi } Iv = iv; + + return Math.Min(input.Length, output.Length) & ~0xF; } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { int remainingBlocks = Math.Min(input.Length, output.Length) >> 4; @@ -104,5 +106,7 @@ public struct AesCbcModeNi } Iv = iv; + + return Math.Min(input.Length, output.Length) & ~0xF; } } \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesCore.cs b/src/LibHac/Crypto/Impl/AesCore.cs index 548c5153..a4111997 100644 --- a/src/LibHac/Crypto/Impl/AesCore.cs +++ b/src/LibHac/Crypto/Impl/AesCore.cs @@ -31,43 +31,45 @@ public struct AesCore _isDecrypting = isDecrypting; } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { Debug.Assert(!_isDecrypting); - Transform(input, output); + return Transform(input, output); } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { Debug.Assert(_isDecrypting); - Transform(input, output); + return Transform(input, output); } - public void Encrypt(byte[] input, byte[] output, int length) + public int Encrypt(byte[] input, byte[] output, int length) { Debug.Assert(!_isDecrypting); - Transform(input, output, length); + return Transform(input, output, length); } - public void Decrypt(byte[] input, byte[] output, int length) + public int Decrypt(byte[] input, byte[] output, int length) { Debug.Assert(_isDecrypting); - Transform(input, output, length); + return Transform(input, output, length); } - private void Transform(ReadOnlySpan input, Span output) + private int Transform(ReadOnlySpan input, Span output) { using var rented = new RentedArray(input.Length); input.CopyTo(rented.Array); - Transform(rented.Array, rented.Array, input.Length); + int bytesWritten = Transform(rented.Array, rented.Array, input.Length); rented.Array.CopyTo(output); + + return bytesWritten; } - private void Transform(byte[] input, byte[] output, int length) + private int Transform(byte[] input, byte[] output, int length) { - _transform.TransformBlock(input, 0, length, output, 0); + return _transform.TransformBlock(input, 0, length, output, 0); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesCoreNi.cs b/src/LibHac/Crypto/Impl/AesCoreNi.cs index 24b34e9f..9b928495 100644 --- a/src/LibHac/Crypto/Impl/AesCoreNi.cs +++ b/src/LibHac/Crypto/Impl/AesCoreNi.cs @@ -98,9 +98,10 @@ public struct AesCoreNi return AesNi.DecryptLast(b, keys[0]); } - public readonly void EncryptInterleaved8(ReadOnlySpan input, Span output) + public readonly int EncryptInterleaved8(ReadOnlySpan input, Span output) { int remainingBlocks = Math.Min(input.Length, output.Length) >> 4; + int length = remainingBlocks << 4; ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); @@ -138,11 +139,14 @@ public struct AesCoreNi outBlock = ref Unsafe.Add(ref outBlock, 1); remainingBlocks -= 1; } + + return length; } - public readonly void DecryptInterleaved8(ReadOnlySpan input, Span output) + public readonly int DecryptInterleaved8(ReadOnlySpan input, Span output) { int remainingBlocks = Math.Min(input.Length, output.Length) >> 4; + int length = remainingBlocks << 4; ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); ref Vector128 outBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(output)); @@ -180,6 +184,8 @@ public struct AesCoreNi outBlock = ref Unsafe.Add(ref outBlock, 1); remainingBlocks -= 1; } + + return length; } // When inlining this function, RyuJIT will almost make the @@ -574,4 +580,4 @@ public struct AesCoreNi return Sse2.Xor(s, t); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesCtrMode.cs b/src/LibHac/Crypto/Impl/AesCtrMode.cs index b5ac9859..72cf111b 100644 --- a/src/LibHac/Crypto/Impl/AesCtrMode.cs +++ b/src/LibHac/Crypto/Impl/AesCtrMode.cs @@ -24,7 +24,7 @@ public struct AesCtrMode Iv = Unsafe.ReadUnaligned(ref MemoryMarshal.GetReference(iv)); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { int blockCount = BitUtil.DivideUp(input.Length, Aes.BlockSize); int length = blockCount * Aes.BlockSize; @@ -34,6 +34,8 @@ public struct AesCtrMode _aesCore.Encrypt(counterBuffer.Array, counterBuffer.Array, length); Utilities.XorArrays(output, input, counterBuffer.Span); + + return Math.Min(input.Length, output.Length); } private static void FillDecryptedCounter(Span counter, Span buffer) @@ -53,4 +55,4 @@ public struct AesCtrMode counterL[1] = BinaryPrimitives.ReverseEndianness(lo); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesCtrModeNi.cs b/src/LibHac/Crypto/Impl/AesCtrModeNi.cs index 492fd230..547aaf69 100644 --- a/src/LibHac/Crypto/Impl/AesCtrModeNi.cs +++ b/src/LibHac/Crypto/Impl/AesCtrModeNi.cs @@ -23,9 +23,10 @@ public struct AesCtrModeNi Iv = Unsafe.ReadUnaligned>(ref MemoryMarshal.GetReference(iv)); } - public void Transform(ReadOnlySpan input, Span output) + public int Transform(ReadOnlySpan input, Span output) { - int remaining = Math.Min(input.Length, output.Length); + int length = Math.Min(input.Length, output.Length); + int remaining = length; int blockCount = remaining >> 4; ref Vector128 inBlock = ref Unsafe.As>(ref MemoryMarshal.GetReference(input)); @@ -103,6 +104,8 @@ public struct AesCtrModeNi { EncryptCtrPartialBlock(input.Slice(blockCount * 0x10), output.Slice(blockCount * 0x10)); } + + return length; } private void EncryptCtrPartialBlock(ReadOnlySpan input, Span output) diff --git a/src/LibHac/Crypto/Impl/AesEcbMode.cs b/src/LibHac/Crypto/Impl/AesEcbMode.cs index 2d76d5fb..42ff39fd 100644 --- a/src/LibHac/Crypto/Impl/AesEcbMode.cs +++ b/src/LibHac/Crypto/Impl/AesEcbMode.cs @@ -13,13 +13,13 @@ public struct AesEcbMode _aesCore.Initialize(key, ReadOnlySpan.Empty, CipherMode.ECB, isDecrypting); } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { - _aesCore.Encrypt(input, output); + return _aesCore.Encrypt(input, output); } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { - _aesCore.Decrypt(input, output); + return _aesCore.Decrypt(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesEcbModeNi.cs b/src/LibHac/Crypto/Impl/AesEcbModeNi.cs index 88b5826f..b2f8085f 100644 --- a/src/LibHac/Crypto/Impl/AesEcbModeNi.cs +++ b/src/LibHac/Crypto/Impl/AesEcbModeNi.cs @@ -11,13 +11,13 @@ public struct AesEcbModeNi _aesCore.Initialize(key, isDecrypting); } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { - _aesCore.EncryptInterleaved8(input, output); + return _aesCore.EncryptInterleaved8(input, output); } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { - _aesCore.DecryptInterleaved8(input, output); + return _aesCore.DecryptInterleaved8(input, output); } -} +} \ No newline at end of file diff --git a/src/LibHac/Crypto/Impl/AesXtsMode.cs b/src/LibHac/Crypto/Impl/AesXtsMode.cs index 2fafd67e..cf70ace3 100644 --- a/src/LibHac/Crypto/Impl/AesXtsMode.cs +++ b/src/LibHac/Crypto/Impl/AesXtsMode.cs @@ -26,7 +26,7 @@ public struct AesXtsMode Iv = Unsafe.ReadUnaligned(ref MemoryMarshal.GetReference(iv)); } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { int length = Math.Min(input.Length, output.Length); int blockCount = length >> 4; @@ -74,9 +74,11 @@ public struct AesXtsMode _dataAesCore.Encrypt(tmp, tmp); XorBuffer(ref prevOutBlock, ref tmp, ref tweak); } + + return length; } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { int length = Math.Min(input.Length, output.Length); int blockCount = length >> 4; @@ -139,6 +141,8 @@ public struct AesXtsMode _dataAesCore.Decrypt(tmp, tmp); XorBuffer(ref outBlock, ref tmp, ref tweak); } + + return length; } private static Buffer16 FillTweakBuffer(Buffer16 initialTweak, Span tweakBuffer) diff --git a/src/LibHac/Crypto/Impl/AesXtsModeNi.cs b/src/LibHac/Crypto/Impl/AesXtsModeNi.cs index aa308070..ebb8c409 100644 --- a/src/LibHac/Crypto/Impl/AesXtsModeNi.cs +++ b/src/LibHac/Crypto/Impl/AesXtsModeNi.cs @@ -25,7 +25,7 @@ public struct AesXtsModeNi Iv = Unsafe.ReadUnaligned>(ref MemoryMarshal.GetReference(iv)); } - public void Encrypt(ReadOnlySpan input, Span output) + public int Encrypt(ReadOnlySpan input, Span output) { int length = Math.Min(input.Length, output.Length); int remainingBlocks = length >> 4; @@ -101,9 +101,11 @@ public struct AesXtsModeNi { EncryptPartialFinalBlock(ref inBlock, ref outBlock, tweak, leftover); } + + return length; } - public void Decrypt(ReadOnlySpan input, Span output) + public int Decrypt(ReadOnlySpan input, Span output) { int length = Math.Min(input.Length, output.Length); int remainingBlocks = length >> 4; @@ -181,6 +183,8 @@ public struct AesXtsModeNi { DecryptPartialFinalBlock(ref inBlock, ref outBlock, tweak, mask, leftover); } + + return length; } // ReSharper disable once RedundantAssignment @@ -251,4 +255,4 @@ public struct AesXtsModeNi return Sse2.Xor(tmp1, tmp2); } -} +} \ No newline at end of file