Exemplo n.º 1
0
        private void CheckTestCase(OcbBlockCipher encCipher, OcbBlockCipher decCipher, string testName,
                                   int macLengthBytes, byte[] P, byte[] C)
        {
            byte[] tag = Arrays.Copy(C, C.Length - macLengthBytes, macLengthBytes);

            {
                byte[] enc = new byte[encCipher.GetOutputSize(P.Length)];
                int    len = encCipher.ProcessBytes(P, 0, P.Length, enc, 0);
                len += encCipher.DoFinal(enc, len);

                if (enc.Length != len)
                {
                    Fail("encryption reported incorrect length: " + testName);
                }

                if (!AreEqual(C, enc))
                {
                    Fail("incorrect encrypt in: " + testName);
                }

                if (!AreEqual(tag, encCipher.GetMac()))
                {
                    Fail("getMac() not the same as the appended tag: " + testName);
                }
            }

            {
                byte[] dec = new byte[decCipher.GetOutputSize(C.Length)];
                int    len = decCipher.ProcessBytes(C, 0, C.Length, dec, 0);
                len += decCipher.DoFinal(dec, len);

                if (dec.Length != len)
                {
                    Fail("decryption reported incorrect length: " + testName);
                }

                if (!AreEqual(P, dec))
                {
                    Fail("incorrect decrypt in: " + testName);
                }

                if (!AreEqual(tag, decCipher.GetMac()))
                {
                    Fail("getMac() not the same as the appended tag: " + testName);
                }
            }
        }
Exemplo n.º 2
0
        private void RunLongerTestCase(int aesKeySize, int tagLen, byte[] expectedOutput)
        {
            KeyParameter key = new KeyParameter(new byte[aesKeySize / 8]);

            byte[] N = new byte[12];

            IAeadBlockCipher c1 = new OcbBlockCipher(new AesFastEngine(), new AesFastEngine());

            c1.Init(true, new AeadParameters(key, tagLen, N));

            IAeadBlockCipher c2 = new OcbBlockCipher(new AesFastEngine(), new AesFastEngine());

            long total = 0;

            byte[] S = new byte[128];

            for (int i = 0; i < 128; ++i)
            {
                N[11] = (byte)i;

                c2.Init(true, new AeadParameters(key, tagLen, N));

                total += UpdateCiphers(c1, c2, S, i, true, true);
                total += UpdateCiphers(c1, c2, S, i, false, true);
                total += UpdateCiphers(c1, c2, S, i, true, false);
            }

            long expectedTotal = 16256 + (48 * tagLen);

            if (total != expectedTotal)
            {
                Fail("test generated the wrong amount of input: " + total);
            }

            byte[] output = new byte[c1.GetOutputSize(0)];
            c1.DoFinal(output, 0);

            if (!AreEqual(expectedOutput, output))
            {
                Fail("incorrect encrypt in long-form test");
            }
        }