예제 #1
0
        private int AllocateRandom(ref byte[] Data, int Size = 0, int NonAlign = 0)
        {
            CSPPrng rng = new CSPPrng();

            if (Size != 0)
            {
                Data = new byte[Size];
            }
            else
            {
                int sze = 0;
                if (NonAlign != 0)
                {
                    while ((sze = rng.Next(MIN_ALLOC, MAX_ALLOC)) % NonAlign == 0)
                    {
                        ;
                    }
                }
                else
                {
                    sze = rng.Next(MIN_ALLOC, MAX_ALLOC);
                }

                Data = new byte[sze];
            }

            rng.GetBytes(Data);
            return(Data.Length);
        }
예제 #2
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!");
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Initialize an empty VolumeKey structure; generates a random key tag identifier
        /// </summary>
        ///
        /// <param name="Description">The cipher description</param>
        /// <param name="Count">The number of key/vector pairs</param>
        public VolumeKey(CipherDescription Description, int Count)
        {
            this.Description = Description;
            this.Count       = Count;
            this.FileId      = new int[Count];
            this.State       = new byte[Count];
            this.Tag         = new CSPPrng().GetBytes(TAG_SIZE);
            int id = 0;

            using (CSPPrng rng = new CSPPrng())
            {
                this.Tag = rng.GetBytes(TAG_SIZE);
                id       = rng.Next();
            }

            for (int i = 0; i < Count; ++i)
            {
                this.State[i]  = (byte)VolumeKeyStates.Unassigned;
                this.FileId[i] = id + i;
            }
        }
예제 #4
0
        private void HmacDescriptionTest()
        {
            CSPPrng rng = new CSPPrng();

            byte[] data = rng.GetBytes(rng.Next(100, 400));
            byte[] key  = rng.GetBytes(64);
            HMAC   mac  = new HMAC(Digests.SHA256);

            mac.Initialize(key);
            byte[]         c1  = mac.ComputeMac(data);
            MacDescription mds = new MacDescription(64, Digests.SHA256);
            MacStream      mst = new MacStream(mds, new KeyParams(key));

            mst.Initialize(new MemoryStream(data));
            byte[] c2 = mst.ComputeMac();

            if (!Evaluate.AreEqual(c1, c2))
            {
                throw new Exception("MacStreamTest: HMAC code arrays are not equal!");
            }
        }
예제 #5
0
        private void CmacDescriptionTest()
        {
            CSPPrng rng = new CSPPrng();

            byte[] data = rng.GetBytes(rng.Next(100, 400));
            byte[] key  = rng.GetBytes(32);
            byte[] iv   = rng.GetBytes(16);
            CMAC   mac  = new CMAC(BlockCiphers.Rijndael);

            mac.Initialize(key, iv);
            byte[]         c1  = mac.ComputeMac(data);
            MacDescription mds = new MacDescription(32, BlockCiphers.Rijndael, IVSizes.V128, BlockSizes.B128, RoundCounts.R14);
            MacStream      mst = new MacStream(mds, new KeyParams(key, iv));

            mst.Initialize(new MemoryStream(data));
            byte[] c2 = mst.ComputeMac();

            if (!Evaluate.AreEqual(c1, c2))
            {
                throw new Exception("MacStreamTest: CMAC code arrays are not equal!");
            }
        }