Permalink
Browse files

Useful tools to compare different algorithms and implementations

  • Loading branch information...
1 parent 5dd90d4 commit 6af4a803f49defdf7b872196cedf66c2a748aa72 @spouliot spouliot committed Jan 14, 2012
Showing with 244 additions and 0 deletions.
  1. +12 −0 tools/Makefile
  2. +2 −0 tools/README
  3. +134 −0 tools/cryptperf.cs
  4. +96 −0 tools/hashperf.cs
View
@@ -0,0 +1,12 @@
+CSC=mcs
+
+all: hashperf.exe cryptperf.exe
+
+hashperf.exe: hashperf.cs
+ $(CSC) -debug hashperf.cs
+
+cryptperf.exe: cryptperf.cs
+ $(CSC) -debug cryptperf.cs
+
+clean:
+ rm *.exe*
View
@@ -0,0 +1,2 @@
+Useful tools to compare different algorithms and implementations
+
View
@@ -0,0 +1,134 @@
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+class Program {
+
+ static void Test (SymmetricAlgorithm cipher)
+ {
+ if (cipher is Rijndael || cipher is Aes) {
+ Console.WriteLine ("Testing results wrt FIPS 197 test vectors");
+ FIPS197_AppendixB (cipher);
+ FIPS197_AppendixC1 (cipher);
+ FIPS197_AppendixC2 (cipher);
+ FIPS197_AppendixC3 (cipher);
+ } else {
+ Console.WriteLine ("No test vectors were found.");
+ return;
+ }
+ }
+
+ static void FIPS197_AppendixB (SymmetricAlgorithm cipher)
+ {
+ byte[] key = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
+ byte[] iv = new byte[16]; // empty - not used for ECB
+ byte[] input = { 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34 };
+ byte[] expected = { 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 };
+ Console.WriteLine ("FIPS 197 B: {0}",
+ Test (cipher, key, iv, input, expected) ? "PASS" : "FAIL");
+ }
+
+ // FIPS197 C.1 AES-128 (Nk=4, Nr=10)
+ static void FIPS197_AppendixC1 (SymmetricAlgorithm cipher)
+ {
+ byte[] key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
+ byte[] iv = new byte[16]; // empty - not used for ECB
+ byte[] input = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
+ byte[] expected = { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a };
+ Console.WriteLine ("FIPS 197 C1: {0}",
+ Test (cipher, key, iv, input, expected) ? "PASS" : "FAIL");
+ }
+
+ // FIPS197 C.2 AES-192 (Nk=6, Nr=12)
+ static void FIPS197_AppendixC2 (SymmetricAlgorithm cipher)
+ {
+ byte[] key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
+ byte[] iv = new byte[16]; // empty - not used for ECB
+ byte[] input = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
+ byte[] expected = { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 };
+ Console.WriteLine ("FIPS 197 C2: {0}",
+ Test (cipher, key, iv, input, expected) ? "PASS" : "FAIL");
+ }
+
+ // C.3 AES-256 (Nk=8, Nr=14)
+ static void FIPS197_AppendixC3 (SymmetricAlgorithm cipher)
+ {
+ byte[] key = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
+ byte[] iv = new byte[16]; // empty - not used for ECB
+ byte[] input = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
+ byte[] expected = { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 };
+ Console.WriteLine ("FIPS 197 C3: {0}",
+ Test (cipher, key, iv, input, expected) ? "PASS" : "FAIL");
+ }
+
+ static bool Test (SymmetricAlgorithm cipher, byte[] key, byte[] iv, byte[] input, byte[] expected)
+ {
+ cipher.Mode = CipherMode.ECB;
+ cipher.KeySize = key.Length * 8;
+ cipher.Padding = PaddingMode.Zeros;
+
+ byte[] output = new byte [input.Length];
+ ICryptoTransform encryptor = cipher.CreateEncryptor (key, iv);
+ encryptor.TransformBlock (input, 0, input.Length, output, 0);
+ if (!Compare (output, expected))
+ return false;
+
+ byte[] original = new byte [output.Length];
+ ICryptoTransform decryptor = cipher.CreateDecryptor (key, iv);
+ decryptor.TransformBlock (output, 0, output.Length, original, 0);
+ return Compare (original, input);
+ }
+
+ static bool Compare (byte[] actual, byte[] expected)
+ {
+ if (actual == null)
+ return (expected == null);
+ if (expected == null)
+ return false;
+ if (actual.Length != expected.Length)
+ return false;
+ for (int i=0; i < actual.Length; i++) {
+ if (actual [i] != expected [i])
+ return false;
+ }
+ return true;
+ }
+
+
+ static void Perf (SymmetricAlgorithm cipher)
+ {
+ Console.WriteLine ("Performance tests for different block sizes, 30 seconds each");
+ int block = cipher.BlockSize;
+ while (block <= 64 * 1024 + 1) {
+ Speed (cipher, block);
+ block <<= 2;
+ }
+ }
+
+ static void Speed (SymmetricAlgorithm cipher, int block)
+ {
+ byte[] input = new byte [block];
+ byte[] output = new byte [block];
+ DateTime now = DateTime.UtcNow;
+ long size = 0;
+ ICryptoTransform transform = cipher.CreateEncryptor ();
+ while ((DateTime.UtcNow - now).TotalSeconds < 30) {
+ transform.TransformBlock (input, 0, input.Length, output, 0);
+ size += input.Length;
+ }
+ transform.TransformFinalBlock (input, 0, input.Length);
+ size += input.Length;
+ double speed = size / (DateTime.UtcNow - now).TotalSeconds;
+ Console.WriteLine ("{0}: {1}: {2} Mbytes/sec", block, cipher, speed / 1024 / 1024);
+ }
+
+ static void Main (string[] args)
+ {
+ foreach (string arg in args) {
+ SymmetricAlgorithm cipher = (SymmetricAlgorithm) CryptoConfig.CreateFromName (arg);
+ Console.WriteLine ("{0}: {1}", arg, cipher);
+ Test (cipher);
+ Perf (cipher);
+ }
+ }
+}
View
@@ -0,0 +1,96 @@
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+class Program {
+
+ static void Test (HashAlgorithm digest)
+ {
+ byte[] result1, result2, result3;
+
+ if (digest is SHA1) {
+ Console.WriteLine ("Testing results wrt FIPS 180-1 test vectors");
+ result1 = new byte [] { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
+ 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d };
+ result2 = new byte [] { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
+ 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 };
+ result3 = new byte [] { 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
+ 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f };
+ } else if (digest is SHA256) {
+ Console.WriteLine ("Testing results wrt FIPS 180-2 test vectors");
+ result1 = new byte [] { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
+ 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
+ 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
+ 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad };
+ result2 = new byte [] { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
+ 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
+ 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
+ 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 };
+ result3 = new byte [] { 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92,
+ 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67,
+ 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
+ 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 };
+ } else {
+ Console.WriteLine ("No test vectors were found.");
+ return;
+ }
+ string input1 = "abc";
+ string input2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+ byte[] input = Encoding.Default.GetBytes (input1);
+ byte[] output = digest.ComputeHash (input);
+ Console.WriteLine ("FIPS 180 Test 1: {0}",
+ BitConverter.ToString (result1) != BitConverter.ToString (output) ?
+ "FAIL" : "PASS");
+
+ input = Encoding.Default.GetBytes (input2);
+ output = digest.ComputeHash (input);
+ Console.WriteLine ("FIPS 180 Test 2: {0}",
+ BitConverter.ToString (result2) != BitConverter.ToString (output) ?
+ "FAIL" : "PASS");
+
+ input = new byte [1000000];
+ for (int i = 0; i < 1000000; i++)
+ input[i] = 0x61; // a
+ output = digest.ComputeHash (input);
+ Console.WriteLine ("FIPS 180 Test 3: {0}",
+ BitConverter.ToString (result3) != BitConverter.ToString (output) ?
+ "FAIL" : "PASS");
+ }
+
+ static void Perf (HashAlgorithm digest)
+ {
+ Console.WriteLine ("Performance tests for different block sizes, 30 seconds each");
+ int block = 1;
+ while (block <= 64 * 1024) {
+ Speed (digest, block);
+ block <<= 2;
+ }
+ }
+
+ static void Speed (HashAlgorithm digest, int block)
+ {
+ byte[] input = new byte [block];
+ byte[] output = new byte [block];
+ DateTime now = DateTime.UtcNow;
+ long size = 0;
+ while ((DateTime.UtcNow - now).TotalSeconds < 30) {
+ digest.TransformBlock (input, 0, input.Length, output, 0);
+ size += input.Length;
+ }
+ digest.TransformFinalBlock (input, 0, input.Length);
+ size += input.Length;
+ double speed = size / (DateTime.UtcNow - now).TotalSeconds;
+ Console.WriteLine ("{0}: {1}: {2} Mbytes/sec", block, digest, speed / 1024 / 1024);
+ }
+
+ static void Main (string[] args)
+ {
+ foreach (string arg in args) {
+ HashAlgorithm hash = (HashAlgorithm) CryptoConfig.CreateFromName (arg);
+ Console.WriteLine ("{0}: {1}", arg, hash);
+ Test (hash);
+ Perf (hash);
+ }
+ }
+}

0 comments on commit 6af4a80

Please sign in to comment.