public static void ARIA_test() { byte[] p = new byte[16]; byte[] c = new byte[16]; byte[] mk = new byte[32]; bool flag = false; TextWriter writer = Console.Out; ARIAEngine instance = new ARIAEngine(256); for (int i = 0; i < 32; i++) { mk[i] = 0; } for (int i = 0; i < 16; i++) { p[i] = 0; } writer.WriteLine("BEGIN testing the roundtrip..."); writer.WriteLine("For key size of 256 bits, starting with " + "the zero plaintext and the zero key, let's see if " + "we may recover the plaintext by decrypting the " + "encrypted ciphertext."); instance.SetKey(mk); instance.SetupRoundKeys(); writer.Write("plaintext : "); PrintBlock(writer, p); writer.WriteLine(); instance.Encrypt(p, 0, c, 0); writer.Write("ciphertext: "); PrintBlock(writer, c); writer.WriteLine(); instance.Decrypt(c, 0, p, 0); writer.Write("decrypted : "); PrintBlock(writer, p); writer.WriteLine(); flag = false; for (int i = 0; i < 16; i++) { if (p[i] != 0) { flag = true; } } if (flag) { writer.WriteLine("The result is incorrect!"); } else { writer.WriteLine("Okay. The result is correct."); } writer.WriteLine("END testing the roundtrip.\n"); int TEST_NUM = 0x800000; writer.WriteLine("BEGIN speed measurement..."); for (int i = 0; i < 16; i++) { mk[i] = (byte)i; } writer.WriteLine(" First, EncKeySetup():"); writer.Write(" masterkey: "); PrintBlock(writer, mk); writer.WriteLine(); instance.Reset(); instance.SetKeySize(128); instance.SetKey(mk); for (int i = 0; i < 1000; i++) { instance.SetupEncRoundKeys(); // allow the CPU to settle down } DateTime start = DateTime.Now; for (int i = 0; i < TEST_NUM; i++) { instance.SetupEncRoundKeys(); } DateTime fin = DateTime.Now; float lapse = (float)((fin - start).Milliseconds) / 1000; writer.Write(" time lapsed: "); writer.Write(lapse); writer.WriteLine(" sec."); writer.Write(" speed : "); writer.Write(TEST_NUM * 128 / (lapse * 1024 * 1024)); writer.WriteLine(" megabits/sec.\n"); writer.WriteLine(" Next, Crypt():"); for (int i = 0; i < 16; i++) { p[i] = (byte)((i << 4) ^ i); } writer.Write(" plaintext : "); PrintBlock(writer, p); writer.WriteLine(); for (int i = 0; i < 1000; i++) { instance.Encrypt(p, 0, c, 0); } start = DateTime.Now; for (int i = 0; i < TEST_NUM; i++) { instance.Encrypt(p, 0, c, 0); } fin = DateTime.Now; writer.Write(" ciphertext: "); PrintBlock(writer, c); writer.WriteLine(); lapse = (float)((fin - start).Milliseconds) / 1000; writer.Write(" time lapsed: "); writer.Write(lapse); writer.WriteLine(" sec."); writer.Write(" speed : "); writer.Write(TEST_NUM * 128 / (lapse * 1024 * 1024)); writer.WriteLine(" megabits/sec.\n"); writer.WriteLine(" Finally, DecKeySetup():"); for (int i = 0; i < 1000; i++) { instance.SetupDecRoundKeys(); // allow the CPU to settle down } start = DateTime.Now; for (int i = 0; i < TEST_NUM; i++) { instance.SetupDecRoundKeys(); } fin = DateTime.Now; lapse = (float)((fin - start).Milliseconds) / 1000; writer.Write(" time lapsed: "); writer.Write(lapse); writer.WriteLine(" sec."); writer.Write(" speed : "); writer.Write(TEST_NUM * 128 / (lapse * 1024 * 1024)); writer.WriteLine(" megabits/sec."); writer.WriteLine("END speed measurement."); }
private void Init(byte[] key) { engine = new ARIAEngine(key.Length * 8); engine.SetKey(key); engine.SetupRoundKeys(); }