Example #1
0
        // ReSharper disable once UnusedParameter.Local
        private static void RegisterAesSingleBlockBenchmarks(MultiBenchmark bench)
        {
            var input  = new byte[SingleBlockCipherBenchSize];
            var output = new byte[SingleBlockCipherBenchSize];

            Func <double, string> resultPrinter = time => GetPerformanceString(time, SingleBlockCipherBenchSize);

            bench.Register("AES single-block encrypt", () => { }, EncryptBlocks, resultPrinter);
            bench.Register("AES single-block decrypt", () => { }, DecryptBlocks, resultPrinter);

            bench.DefaultRunsNeeded = 1000;

            void EncryptBlocks()
            {
                ref byte inBlock  = ref MemoryMarshal.GetReference(input.AsSpan());
                ref byte outBlock = ref MemoryMarshal.GetReference(output.AsSpan());
Example #2
0
        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);
            }
        }