Esempio n. 1
0
        /// <summary>
        /// Returns the DtmForwardKeyStruct as an encoded MemoryStream
        /// </summary>
        ///
        /// <returns>The serialized DtmForwardKeyStruct</returns>
        public MemoryStream ToStream()
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            KeyParams.Serialize(Key).CopyTo(stream);
            writer.Write(SessionParams.ToBytes());
            writer.Write((long)LifeSpan);
            writer.Write((short)Instruction);
            writer.Write((long)OptionsFlag);

            stream.Seek(0, SeekOrigin.Begin);

            return(stream);
        }
Esempio n. 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!");
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Create a single use key file using a <see cref="KeyParams"/> containing the key material, and a <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription"/> containing the cipher implementation details
        /// </summary>
        ///
        /// <param name="Description">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription">Cipher Description</see> containing the cipher details</param>
        /// <param name="KeyParam">An initialized and populated key material container; must include a 16 byte populated ExtKey property</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if a KeyParams member is null, but specified in the Header or a Header parameter does not match a KeyParams value</exception>
        public void Create(CipherDescription Description, KeyParams KeyParam)
        {
            if (KeyParam.Key == null)
            {
                throw new CryptoProcessingException("KeyFactory:Create", "The key can not be null!", new ArgumentNullException());
            }

            if (KeyParam.Key.Length != Description.KeySize)
            {
                throw new CryptoProcessingException("KeyFactory:Create", "The key parameter does not match the key size specified in the Header!", new ArgumentOutOfRangeException());
            }

            if (Description.IvSize > 0 && KeyParam.IV != null)
            {
                if (KeyParam.IV.Length != Description.IvSize)
                {
                    throw new CryptoProcessingException("KeyFactory:Create", "The KeyParam IV size does not align with the IVSize setting in the Header!", new ArgumentOutOfRangeException());
                }
            }
            if (Description.MacKeySize > 0)
            {
                if (KeyParam.IKM == null)
                {
                    throw new CryptoProcessingException("KeyFactory:Create", "Digest key is specified in the header MacSize, but is null in KeyParam!", new ArgumentNullException());
                }
                if (KeyParam.IKM.Length != Description.MacKeySize)
                {
                    throw new CryptoProcessingException("KeyFactory:Create", "Header MacSize does not align with the size of the KeyParam IKM!", new ArgumentOutOfRangeException());
                }
            }

            byte[] hdr = new CipherKey(Description).ToBytes();
            m_keyStream.Write(hdr, 0, hdr.Length);
            byte[] key = ((MemoryStream)KeyParams.Serialize(KeyParam)).ToArray();
            m_keyStream.Write(key, 0, key.Length);
        }