private Result GetDecryptor(out ICipher decryptor, long offset) { if (offset == 0) { // Use the IV directly decryptor = Aes.CreateCbcDecryptor(_key, _iv); return(Result.Success); } decryptor = default; // Need to get the output of the previous block Span <byte> prevBlock = stackalloc byte[BlockSize]; Result rc = BaseStorage.Read(offset - BlockSize, prevBlock); if (rc.IsFailure()) { return(rc); } ICipher tmpDecryptor = Aes.CreateCbcDecryptor(_key, _iv); tmpDecryptor.Transform(prevBlock, prevBlock); decryptor = tmpDecryptor; return(Result.Success); }
private static void RegisterAesSequentialBenchmarks(MultiBenchmark bench) { var input = new byte[BatchCipherBenchSize]; var output = new byte[BatchCipherBenchSize]; Func <double, string> resultPrinter = time => GetPerformanceString(time, BatchCipherBenchSize); // Skip the first benchmark set if we don't have AES-NI intrinsics for (int i = Aes.IsAesNiSupported() ? 0 : 1; i < 2; i++) { // Prefer .NET crypto on the second set string nameSuffix = i == 1 ? "built-in " : string.Empty; bool preferDotNetImpl = i == 1; RegisterCipher($"AES-ECB {nameSuffix}encrypt", () => Aes.CreateEcbEncryptor(new byte[0x10], preferDotNetImpl)); RegisterCipher($"AES-ECB {nameSuffix}decrypt", () => Aes.CreateEcbDecryptor(new byte[0x10], preferDotNetImpl)); RegisterCipher($"AES-CBC {nameSuffix}encrypt", () => Aes.CreateCbcEncryptor(new byte[0x10], new byte[0x10], preferDotNetImpl)); RegisterCipher($"AES-CBC {nameSuffix}decrypt", () => Aes.CreateCbcDecryptor(new byte[0x10], new byte[0x10], preferDotNetImpl)); RegisterCipher($"AES-CTR {nameSuffix}decrypt", () => Aes.CreateCtrDecryptor(new byte[0x10], new byte[0x10], preferDotNetImpl)); RegisterCipher($"AES-XTS {nameSuffix}encrypt", () => Aes.CreateXtsEncryptor(new byte[0x10], new byte[0x10], new byte[0x10], preferDotNetImpl)); RegisterCipher($"AES-XTS {nameSuffix}decrypt", () => Aes.CreateXtsDecryptor(new byte[0x10], new byte[0x10], new byte[0x10], preferDotNetImpl)); } void RegisterCipher(string name, Func <ICipher> cipherGenerator) { ICipher cipher = null; Action setup = () => cipher = cipherGenerator(); Action action = () => cipher.Transform(input, output); bench.Register(name, setup, action, resultPrinter); } }
//[Theory, MemberData(nameof(DecryptTestVectors_Individual))] public static void Decrypt_Individual(EncryptionTestVector tv) { Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateCbcDecryptor(tv.Key, tv.Iv, true)); }
public static void DecryptMultiIntrinsics() { Common.DecryptCipherTest(DecryptMultiTestVectors, (key, iv) => Aes.CreateCbcDecryptor(key, iv)); }
public static void Decrypt() { Common.DecryptCipherTest(DecryptTestVectors, (key, iv) => Aes.CreateCbcDecryptor(key, iv, true)); }
public static void DecryptMultiIntrinsics(EncryptionTestVector tv) { Common.CipherTestCore(tv.CipherText, tv.PlainText, Aes.CreateCbcDecryptor(tv.Key, tv.Iv)); }