Exemplo n.º 1
0
        private void KeyParamsTest()
        {
            CSPPrng      rnd = new CSPPrng();
            KeyGenerator kg  = new KeyGenerator();

            for (int i = 0; i < 10; ++i)
            {
                // out-bound funcs return pointer to obj
                KeyParams    kp1 = kg.GetKeyParams(rnd.Next(1, 1024), rnd.Next(1, 128), rnd.Next(1, 128));
                MemoryStream m   = (MemoryStream)KeyParams.Serialize(kp1);
                KeyParams    kp2 = KeyParams.DeSerialize(m);

                if (!kp1.Equals(kp2))
                {
                    throw new Exception("KeyFactoryTest: KeyParams serialization test has failed!");
                }
                if (kp1.GetHashCode() != kp2.GetHashCode())
                {
                    throw new Exception("KeyFactoryTest: KeyAuthority hash code test has failed!");
                }
            }
        }
Exemplo n.º 2
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!");
            }
        }