Beispiel #1
0
            internal override void Evaluate(EngineProvider provider)
            {
                byte[] K = Hex.Decode("404142434445464748494a4b4c4d4e4f");
                byte[] N = Hex.Decode("10111213141516");
                byte[] A = Hex.Decode("0001020304050607");
                byte[] P = Hex.Decode("20212223");
                byte[] C = Hex.Decode("7162015b4dac255d");
                byte[] T = Hex.Decode("6084341b");

                CcmBlockCipher encCipher = new CcmBlockCipher(provider.CreateEngine(EngineUsage.GENERAL));
                CcmBlockCipher decCipher = new CcmBlockCipher(provider.CreateEngine(EngineUsage.GENERAL));
                int            macSize   = T.Length * 8;

                KeyParameter keyParam = new KeyParameter(K);

                encCipher.Init(true, new AeadParameters(keyParam, macSize, N, A));

                byte[] enc = new byte[C.Length];

                int len = encCipher.ProcessBytes(P, 0, P.Length, enc, 0);

                encCipher.DoFinal(enc, len);

                if (!Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.AesCcmEnc], enc))
                {
                    Fail("encrypted stream fails to match in self test");
                }

                if (!Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.AesCcmEncTag], encCipher.GetMac()))
                {
                    Fail("MAC fails to match in self test encrypt");
                }

                decCipher.Init(false, new AeadParameters(keyParam, macSize, N, A));

                byte[] tmp = new byte[enc.Length];

                len = decCipher.ProcessBytes(enc, 0, enc.Length, tmp, 0);

                len += decCipher.DoFinal(tmp, len);

                byte[] dec = new byte[len];

                Array.Copy(tmp, 0, dec, 0, len);

                if (!Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.AesCcmDec], dec))
                {
                    Fail("decrypted stream fails to match in self test");
                }

                if (!Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.AesCcmDecTag], decCipher.GetMac()))
                {
                    Fail("MAC fails to match in self test");
                }
            }
Beispiel #2
0
        private void checkVectors(
            int count,
            CcmBlockCipher ccm,
            byte[] k,
            int macSize,
            byte[] n,
            byte[] a,
            byte[] p,
            byte[] t,
            byte[] c)
        {
            ccm.Init(true, new AeadParameters(new KeyParameter(k), macSize, n, a));

            byte[] enc = new byte[c.Length];

            int len = ccm.ProcessBytes(p, 0, p.Length, enc, 0);

            len += ccm.DoFinal(enc, len);

//			ccm.Init(true, new CcmParameters(new KeyParameter(k), macSize, n, a));
//
//			byte[] enc = ccm.ProcessPacket(p, 0, p.Length);

            if (!AreEqual(c, enc))
            {
                Fail("encrypted stream fails to match in test " + count);
            }

//			ccm.Init(false, new CcmParameters(new KeyParameter(k), macSize, n, a));
//
//			byte[] dec = ccm.ProcessPacket(enc, 0, enc.Length);

            ccm.Init(false, new AeadParameters(new KeyParameter(k), macSize, n, a));

            byte[] tmp = new byte[enc.Length];

            len = ccm.ProcessBytes(enc, 0, enc.Length, tmp, 0);

            len += ccm.DoFinal(tmp, len);

            byte[] dec = new byte[len];

            Array.Copy(tmp, 0, dec, 0, len);

            if (!AreEqual(p, dec))
            {
                Fail("decrypted stream fails to match in test " + count);
            }

            if (!AreEqual(t, ccm.GetMac()))
            {
                Fail("MAC fails to match in test " + count);
            }
        }
        private void checkVectors(
            int count,
            CcmBlockCipher ccm,
            string additionalDataType,
            byte[] k,
            int macSize,
            byte[] n,
            byte[] a,
            byte[] sa,
            byte[] p,
            byte[] t,
            byte[] c)
        {
            KeyParameter keyParam = (k == null) ? null : new KeyParameter(k);

            ccm.Init(true, new AeadParameters(keyParam, macSize, n, a));

            byte[] enc = new byte[c.Length];

            if (sa != null)
            {
                ccm.ProcessAadBytes(sa, 0, sa.Length);
            }

            int len = ccm.ProcessBytes(p, 0, p.Length, enc, 0);

            len += ccm.DoFinal(enc, len);

//			ccm.Init(true, new AeadParameters(new KeyParameter(k), macSize, n, a));
//
//			byte[] enc = ccm.ProcessPacket(p, 0, p.Length);

            if (!AreEqual(c, enc))
            {
                Fail("encrypted stream fails to match in test " + count + " with " + additionalDataType);
            }

//			ccm.Init(false, new AeadParameters(new KeyParameter(k), macSize, n, a));
//
//			byte[] dec = ccm.ProcessPacket(enc, 0, enc.Length);

            ccm.Init(false, new AeadParameters(new KeyParameter(k), macSize, n, a));

            byte[] tmp = new byte[enc.Length];

            if (sa != null)
            {
                ccm.ProcessAadBytes(sa, 0, sa.Length);
            }

            len = ccm.ProcessBytes(enc, 0, enc.Length, tmp, 0);

            len += ccm.DoFinal(tmp, len);

            byte[] dec = new byte[len];

            Array.Copy(tmp, 0, dec, 0, len);

            if (!AreEqual(p, dec))
            {
                Fail("decrypted stream fails to match in test " + count + " with " + additionalDataType,
                     Hex.ToHexString(p), Hex.ToHexString(dec));
            }

            if (!AreEqual(t, ccm.GetMac()))
            {
                Fail("MAC fails to match in test " + count + " with " + additionalDataType);
            }
        }