private static byte[] DoubleLu(byte[] input) { byte[] array = new byte[input.Length]; int num = CMac.ShiftLeft(input, array); int num2 = (input.Length == 16) ? 135 : 27; byte[] expr_2D_cp_0 = array; int expr_2D_cp_1 = input.Length - 1; expr_2D_cp_0[expr_2D_cp_1] ^= (byte)(num2 >> (1 - num << 3)); return(array); }
public void Init(ICipherParameters parameters) { if (parameters is KeyParameter) { this.cipher.Init(true, parameters); this.L = new byte[this.ZEROES.Length]; this.cipher.ProcessBlock(this.ZEROES, 0, this.L, 0); this.Lu = CMac.DoubleLu(this.L); this.Lu2 = CMac.DoubleLu(this.Lu); } else if (parameters != null) { throw new ArgumentException("CMac mode only permits key to be set.", "parameters"); } this.Reset(); }
public override void PerformTest() { IBlockCipher cipher = new AesFastEngine(); 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)); } }
private void TestExceptions() { try { CMac mac = new CMac(new AesEngine()); mac.Init(new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); Fail("CMac does not accept IV"); } catch(ArgumentException) { // Expected } }