Ejemplo n.º 1
0
        public virtual ITestResult Perform()
        {
            KeyParameter key    = new KeyParameter(keyBytes);
            IBlockCipher cipher = new DesEngine();
            IMac         mac    = new CbcBlockCipherMac(cipher);

            //
            // standard DAC - zero IV
            //
            mac.Init(key);

            mac.BlockUpdate(input1, 0, input1.Length);

            byte[] outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output1))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output1) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // mac with IV.
            //
            ParametersWithIV param = new ParametersWithIV(key, ivBytes);

            mac.Init(param);

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output2))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output2) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // CFB mac with IV - 8 bit CFB mode
            //
            param = new ParametersWithIV(key, ivBytes);

            mac = new CfbBlockCipherMac(cipher);

            mac.Init(param);

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output3))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output3) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // word aligned data - zero IV
            //
            mac.Init(key);

            mac.BlockUpdate(input2, 0, input2.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output4))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output4) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // word aligned data - zero IV - CBC padding
            //
            mac = new CbcBlockCipherMac(cipher, new Pkcs7Padding());

            mac.Init(key);

            mac.BlockUpdate(input2, 0, input2.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output5))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output5) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // non-word aligned data - zero IV - CBC padding
            //
            mac.Reset();

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output6))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output6) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // non-word aligned data - zero IV - CBC padding
            //
            mac.Init(key);

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output6))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output6) + " got " + Hex.ToHexString(outBytes)));
            }

            return(new SimpleTestResult(true, Name + ": Okay"));
        }