Ejemplo n.º 1
0
        public void testStress()
        {
            int[] keySizes = { 128, 192, 256 };

            FF1 ff1 = new FF1(10, 8);

            // for each key size
            foreach (int k in keySizes)
            {
                // generate a new key in the key size
                byte[] K = new byte[k / 8];
                for (int i = 0; i < k / 8; i++)
                {
                    K[i] = (byte)i;
                }

                // init plaintext to a 1 byte array
                int[] PT = { k % 10 };

                // for each plaintext length
                for (int j = 0; j < 4; j++)
                {
                    // make plaintext eight times longer
                    PT = Common.concatenate(PT, PT);
                    PT = Common.concatenate(PT, PT);
                    PT = Common.concatenate(PT, PT);

                    // repeat the test four times
                    for (int i = 0; i < 4; i++)
                    {
                        // create a new tweak array
                        byte[] T = Common.bytestring(i, 8);

                        // encrypt the plaintext
                        int[] CT = ff1.encrypt(K, T, PT);

                        // verify decrypted ciphertext against original plaintext
                        CollectionAssert.AreEquivalent(PT, ff1.decrypt(K, T, CT));

                        // use the ciphertext as the new plaintext
                        PT = CT;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        ///<summary>
        ///Perform a single test of FF1 encryption and decryption.
        ///</summary>
        ///<param name="plaintext">The plaintext input.</param>
        ///<param name="ciphertext">The expected ciphertext output.</param>
        ///<param name="key">The AES key.</param>
        ///<param name="name">The name of the test.</param>
        ///<param name="radix">The radix used in plaintext and ciphertext arguments.</param>
        ///<param name="tweak">The tweak.</param>
        ///<param name="maxTlen">The maximum length of a tweak.</param>
        private void testFF1Iteration(String name, int radix, int maxTlen, byte[] key, byte[] tweak, int[] plaintext,
                                      int[] ciphertext)
        {
            // create an FF1 instance
            FF1 ff1 = new FF1(radix, maxTlen);

            Assert.IsNotNull(ff1);

            // create an AES key from the key data
            byte[] K = key;


            Console.WriteLine("\n==============================================================\n");
            Console.WriteLine(name + "\n");
            Console.WriteLine("FF1-AES" + key.Length * 8 + "\n");
            Console.WriteLine("Key is " + Common.byteArrayToHexString(key));
            Console.WriteLine("Radix = " + radix);
            Console.WriteLine("--------------------------------------------------------------\n");
            Console.WriteLine("PT is <" + Common.intArrayToString(plaintext) + ">\n");

            // perform the encryption
            int[] CT = ff1.encrypt(K, tweak, plaintext);

            Console.WriteLine("CT is <" + Common.intArrayToString(CT) + ">");

            // validate the ciphertext
            CollectionAssert.AreEquivalent(ciphertext, CT);

            Console.WriteLine("\n--------------------------------------------------------------\n");

            // perform the decryption
            int[] PT = ff1.decrypt(K, tweak, CT);

            Console.WriteLine("PT is <" + Common.intArrayToString(PT) + ">");

            // validate the recovered plaintext
            CollectionAssert.AreEquivalent(plaintext, PT);
        }
Ejemplo n.º 3
0
        public void testEncrypt()
        {
            int radix   = 8;
            int maxTlen = 16;

            FF1 ff1 = new FF1(radix, maxTlen);

            Assert.IsNotNull(ff1);

            // set up generic test inputs
            byte[] key = { (byte)0x2B, (byte)0x7E, (byte)0x15, (byte)0x16, (byte)0x28, (byte)0xAE, (byte)0xD2,
                           (byte)0xA6, (byte)0xAB, (byte)0xF7, (byte)0x15, (byte)0x88, (byte)0x09, (byte)0xCF,(byte)0x4F,
                           (byte)0x3C };

            int[]  plainText = { 0, 1, 2, 3, 4, 5, 6, 7 };
            byte[] K         = key;
            byte[] T         = { };
            int[]  PT        = plainText;

            // null inputs
            try
            {
                K  = null;
                PT = plainText;
                ff1.encrypt(K, T, PT);
            }
            catch (Exception e)
            {
                Assert.IsInstanceOf <NullReferenceException>(e);
            }
            try
            {
                K  = key;
                T  = null;
                PT = plainText;
                ff1.encrypt(K, T, PT);
            }
            catch (Exception e)
            {
                Assert.IsInstanceOf <NullReferenceException>(e);
            }
            try
            {
                K  = key;
                T  = new byte[0];
                PT = null;
                ff1.encrypt(K, T, PT);
            }
            catch (Exception e)
            {
                Assert.IsInstanceOf <NullReferenceException>(e);
            }

            // wrong key type
            try
            {
                K  = new byte[] { 0, 1, 2, 3, 4, 5 };
                T  = new byte[0];
                PT = plainText;
                ff1.encrypt(K, T, PT);
            }
            catch (Exception)
            {
            }

            // T is too long
            try
            {
                K  = key;
                T  = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                PT = plainText;
                ff1.encrypt(K, T, PT);
            }
            catch (Exception)
            {
            }

            // X is too short
            try
            {
                K  = key;
                T  = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                PT = new int[] { 1 };
                ff1.encrypt(K, T, PT);
            }
            catch (Exception)
            {
            }

            // X is too long
            try
            {
                K  = key;
                T  = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                PT = new int[Constants.MAXLEN + 1];
                ff1.encrypt(K, T, PT);
            }
            catch (Exception)
            {
            }

            // X is too short for radix
            try
            {
                K  = key;
                T  = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                PT = new int[] { 1, 2 };
                ff1.encrypt(K, T, PT);
            }
            catch (Exception)
            {
            }

            // d > 16
            radix   = 128;
            maxTlen = 16;

            ff1 = new FF1(radix, maxTlen);
            Assert.IsNotNull(ff1);

            K  = key;
            T  = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            PT = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
                             28, 29, 30, 31, 32 };
            int[] CT = ff1.encrypt(K, T, PT);
            CollectionAssert.AreEquivalent(PT, ff1.decrypt(K, T, CT));
        }