private void CipherDescriptionTest()
        {
            CipherDescription cd1 = new CipherDescription(
                SymmetricEngines.RHX,
                192, IVSizes.V128,
                CipherModes.CTR,
                PaddingModes.None,
                BlockSizes.B128,
                RoundCounts.R22);

            byte[]            bcd = cd1.ToBytes();
            CipherDescription cd2 = new CipherDescription(bcd);

            if (!cd1.Equals(cd2))
            {
                throw new Exception("KeyFactoryTest: CipherDescription serialization has failed!");
            }
            MemoryStream      mcd = cd2.ToStream();
            CipherDescription cd3 = new CipherDescription(mcd);

            if (!cd1.Equals(cd3))
            {
                throw new Exception("KeyFactoryTest: CipherDescription serialization has failed!");
            }

            int x = cd1.GetHashCode();

            if (x != cd2.GetHashCode() || x != cd3.GetHashCode())
            {
                throw new Exception("KeyFactoryTest: CipherDescription hash code test has failed!");
            }
        }
Beispiel #2
0
        void SerializeStructTest()
        {
            CipherDescription cd = new CipherDescription(
                SymmetricEngines.RHX,
                192,
                IVSizes.V128,
                CipherModes.CTR,
                PaddingModes.PKCS7,
                BlockSizes.B128,
                RoundCounts.R22,
                Digests.Skein512,
                64,
                Digests.SHA512);

            CipherDescription cy = new CipherDescription(cd.ToStream());

            if (!cy.Equals(cd))
            {
                throw new Exception("CipherStreamTest: CipherDescriptions are not equal!");
            }

            cy.KeySize = 0;
            if (cy.Equals(cd))
            {
                throw new Exception("CipherStreamTest: CipherDescriptionsare not equal!");
            }
        }
        private void VolumeKeyTest()
        {
            CipherDescription cd1 = new CipherDescription(
                SymmetricEngines.RHX,
                192, IVSizes.V128,
                CipherModes.CTR,
                PaddingModes.None,
                BlockSizes.B128,
                RoundCounts.R22);

            MemoryStream mk;

            using (VolumeCipher vc = new VolumeCipher())
                mk = vc.CreateKey(cd1, 100);

            VolumeKey         vk1 = new VolumeKey(mk);
            CipherDescription cd2 = vk1.Description;

            if (!cd1.Equals(cd2))
            {
                throw new Exception("KeyFactoryTest: VolumeKey serialization has failed!");
            }

            VolumeKey vk2 = new VolumeKey(mk.ToArray());

            if (!vk1.Equals(vk2))
            {
                throw new Exception("KeyFactoryTest: VolumeKey serialization has failed!");
            }
            if (vk1.GetHashCode() != vk2.GetHashCode())
            {
                throw new Exception("KeyFactoryTest: VolumeKey hash code test has failed!");
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a temporary PackageKey on disk, extracts and compares the copy
        /// <para>Throws an Exception on failure</</para>
        /// </summary>
        public static void PackageFactoryTest()
        {
            string       path = GetTempPath();
            KeyGenerator kgen = new KeyGenerator();
            // populate a KeyAuthority structure
            KeyAuthority authority = new KeyAuthority(kgen.GetBytes(16), kgen.GetBytes(16), kgen.GetBytes(16), kgen.GetBytes(32), 0);

            // cipher paramaters
            CipherDescription desc = new CipherDescription(
                SymmetricEngines.RHX, 32,
                IVSizes.V128,
                CipherModes.CTR,
                PaddingModes.X923,
                BlockSizes.B128,
                RoundCounts.R14,
                Digests.Keccak512,
                64,
                Digests.Keccak512);

            // create the package key
            PackageKey pkey = new PackageKey(authority, desc, 10);

            // write a key file
            using (PackageFactory pf = new PackageFactory(new FileStream(path, FileMode.Open, FileAccess.ReadWrite), authority))
                pf.Create(pkey);

            for (int i = 0; i < pkey.SubKeyCount; i++)
            {
                CipherDescription desc2;
                KeyParams         kp1;
                KeyParams         kp2;
                byte[]            ext;
                byte[]            id = pkey.SubKeyID[i];

                // get at index
                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    kp2 = PackageKey.AtIndex(stream, i);

                // read the package from id
                using (PackageFactory pf = new PackageFactory(new FileStream(path, FileMode.Open, FileAccess.ReadWrite), authority))
                    pf.Extract(id, out desc2, out kp1);

                // compare key material
                if (!Evaluate.AreEqual(kp1.Key, kp2.Key))
                {
                    throw new Exception();
                }
                if (!Evaluate.AreEqual(kp1.IV, kp2.IV))
                {
                    throw new Exception();
                }
                if (!desc.Equals(desc2))
                {
                    throw new Exception();
                }
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
Beispiel #5
0
        private void CipherKeyCreateExtractTest()
        {
            KeyGenerator kg = new KeyGenerator();
            KeyParams    kp = kg.GetKeyParams(192, 16, 64);

            CipherDescription ds = new CipherDescription(
                SymmetricEngines.RHX,
                192,
                IVSizes.V128,
                CipherModes.CTR,
                PaddingModes.PKCS7,
                BlockSizes.B128,
                RoundCounts.R22,
                Digests.Skein512,
                64,
                Digests.SHA512);

            // in/out use a pointer
            MemoryStream m  = new MemoryStream();
            KeyFactory   kf = new KeyFactory(m);

            kf.Create(ds, kp);
            // init new instance w/ populated stream
            m.Seek(0, SeekOrigin.Begin);
            KeyFactory kf2 = new KeyFactory(m);
            // extract key and desc from stream
            CipherKey ck;
            KeyParams kp2;

            kf2.Extract(out ck, out kp2);

            if (!ds.Equals(ck.Description))
            {
                throw new Exception("KeyFactoryTest: Description extraction has failed!");
            }
            if (!kp.Equals(kp2))
            {
                throw new Exception("KeyFactoryTest: Key extraction has failed!");
            }

            MemoryStream m2  = new MemoryStream();
            KeyFactory   kf3 = new KeyFactory(m2);

            // test other create func
            kf3.Create(kp, SymmetricEngines.RHX, 192, IVSizes.V128, CipherModes.CTR, PaddingModes.PKCS7,
                       BlockSizes.B128, RoundCounts.R22, Digests.Skein512, 64, Digests.SHA512);

            m2.Seek(0, SeekOrigin.Begin);
            KeyFactory kf4 = new KeyFactory(m2);

            kf4.Extract(out ck, out kp2);

            if (!ds.Equals(ck.Description))
            {
                throw new Exception("KeyFactoryTest: Description extraction has failed!");
            }
            if (!kp.Equals(kp2))
            {
                throw new Exception("KeyFactoryTest: Key extraction has failed!");
            }
        }