public static byte[] GetCMACDigest(byte[] data, byte[] key) { IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); KeyParameter keyParam = new KeyParameter(key); mac.Init(keyParam); mac.BlockUpdate(data, 0, data.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); return(outBytes); }
/// <summary> /// Creates a CMAC primitive using a symmetric block cipher primitive configured with default block size. /// Default block sizes (and so, output sizes) can be found by querying <see cref="Athena" />. /// </summary> /// <param name="cipherEnum"> /// Cipher primitive to use as the basis for the CMAC construction. Block size must be 64 or 128 /// bits. /// </param> /// <param name="key">Cryptographic key to use in the MAC operation.</param> /// <param name="salt">Cryptographic salt to use in the MAC operation, if any.</param> /// <returns>Pre-initialised CMAC primitive as a <see cref="IMac" />.</returns> public static IMac CreateCmacPrimitive(BlockCipher cipherEnum, byte[] key, byte[] salt = null) { int?defaultBlockSize = Athena.Cryptography.BlockCiphers[cipherEnum].DefaultBlockSizeBits; if (defaultBlockSize != 64 && defaultBlockSize != 128) { throw new NotSupportedException("CMAC/OMAC1 only supports ciphers with 64 / 128 bit block sizes."); } var macObj = new CMac(CipherFactory.CreateBlockCipher(cipherEnum, null)); macObj.Init(key); if (salt.IsNullOrZeroLength() == false) { macObj.BlockUpdate(salt, 0, salt.Length); } return(macObj); }
public override void PerformTest() { IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); //128 bytes key KeyParameter key = new KeyParameter(keyBytes128); // 0 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m0)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m0) + " got " + Hex.ToHexString(outBytes)); } // 16 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m16)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m16) + " got " + Hex.ToHexString(outBytes)); } // 40 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m40)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m40) + " got " + Hex.ToHexString(outBytes)); } // 64 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m64)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m64) + " got " + Hex.ToHexString(outBytes)); } //192 bytes key key = new KeyParameter(keyBytes192); // 0 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m0)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m0) + " got " + Hex.ToHexString(outBytes)); } // 16 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m16)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m16) + " got " + Hex.ToHexString(outBytes)); } // 40 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m40)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m40) + " got " + Hex.ToHexString(outBytes)); } // 64 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m64)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m64) + " got " + Hex.ToHexString(outBytes)); } //256 bytes key key = new KeyParameter(keyBytes256); // 0 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m0)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m0) + " got " + Hex.ToHexString(outBytes)); } // 16 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m16)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m16) + " got " + Hex.ToHexString(outBytes)); } // 40 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m40)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m40) + " got " + Hex.ToHexString(outBytes)); } // 64 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m64)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m64) + " got " + Hex.ToHexString(outBytes)); } TestExceptions(); }
public static void PerformTest() { //ISO20038 vp = new ISO20038(); //vp.runTest(); //** Console.WriteLine(" +-----+ +-----+ +-----+ +-----+ +-----+ +---+----+ "); Console.WriteLine(" | M_1 | | M_2 | | M_n | | M_1 | | M_2 | |M_n|10^i| "); Console.WriteLine(" +-----+ +-----+ +-----+ +-----+ +-----+ +---+----+ "); Console.WriteLine(" | | | +--+ | | | +--+ "); Console.WriteLine(" | +--->(+) +--->(+)<-|K1| | +--->(+) +--->(+)<-|K2| "); Console.WriteLine(" | | | | | +--+ | | | | | +--+ "); Console.WriteLine(" +-----+ | +-----+ | +-----+ +-----+ | +-----+ | +-----+ "); Console.WriteLine(" |AES_K| | |AES_K| | |AES_K| |AES_K| | |AES_K | | |AES_K| "); Console.WriteLine(" +-----+ | +-----+ | +-----+ +-----+ | +-----+ | +-----+ "); Console.WriteLine(" | | | | | | | | | | "); Console.WriteLine(" +-----+ +-----+ | +-----+ +-----+ | "); Console.WriteLine(" | | "); Console.WriteLine(" +-----+ +-----+ "); Console.WriteLine(" | T | | T | "); Console.WriteLine(" +-----+ +-----+ "); IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); Console.WriteLine("CMAC Init.. Cipher: " + cipher.AlgorithmName); Console.WriteLine("CMAC Init.. MAC BlockSize: " + 128); Console.WriteLine("----------------------------------------------------------------"); //128 bytes key KeyParameter key = new KeyParameter(keyBytes128); Console.WriteLine("Example 1:Message len = 0 bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: <empty string>"); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); // 0 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m0)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m0) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 16 bytes message - 128 bytes key Console.WriteLine("Example 2: Message len = " + input16.Length + " bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input16)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m16)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m16) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 40 bytes message - 128 bytes key Console.WriteLine("Example 3: Message len = " + input40.Length + " bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input40)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m40)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m40) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 64 bytes message - 128 bytes key Console.WriteLine("Example 4: Message len = " + input64.Length + " bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input64)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if ((!AreEqual(outBytes, output_k128_m64))) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m64) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); //192 bytes key key = new KeyParameter(keyBytes192); Console.WriteLine("Example 5: Message len = 0 bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input0)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); // 0 bytes message - 192 bytes ke mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m0)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m0) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 16 bytes message - 192 bytes key Console.WriteLine("Example 6: Message len = " + input16.Length + " bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input16)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m16)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m16) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 40 bytes message - 192 bytes key Console.WriteLine("Example 7: Message len = " + input40.Length + " bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input40)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m40)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m40) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 64 bytes message - 192 bytes key Console.WriteLine("Example 8: Message len = " + input64.Length + " bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input64)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m64)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m64) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); //256 bytes key key = new KeyParameter(keyBytes256); Console.WriteLine("Example 9: Message len = 0 bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input0)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); // 0 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m0)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m0) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 16 bytes message - 256 bytes key Console.WriteLine("Example 10: Message len = " + input16.Length + " bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input16)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m16)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m16) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 40 bytes message - 256 bytes key Console.WriteLine("Example 11: Message len = " + input40.Length + " bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input40)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m40)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m40) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 64 bytes message - 256 bytes key Console.WriteLine("Example 12: Message len = " + input64.Length + " bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input64)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m64)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m64) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); TestExceptions(); }