Exemple #1
0
        /// <summary>
        /// Compare this object instance with another
        /// </summary>
        ///
        /// <param name="Obj">Object to compare</param>
        ///
        /// <returns>True if equal, otherwise false</returns>
        public override bool Equals(Object Obj)
        {
            if (!(Obj is CipherKey))
            {
                return(false);
            }

            CipherKey other = (CipherKey)Obj;

            if (Description.GetHashCode() != other.Description.GetHashCode())
            {
                return(false);
            }
            if (!Compare.IsEqual(KeyId, other.KeyId))
            {
                return(false);
            }
            if (!Compare.IsEqual(ExtensionKey, other.ExtensionKey))
            {
                return(false);
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Extract a KeyParams and CipherKey
        /// </summary>
        /// 
        /// <param name="KeyHeader">The <see cref="CipherKey"/> that receives the cipher description, key id, and extension key</param>
        /// <param name="KeyParam">The <see cref="KeyParams"/> container that receives the key material from the file</param>
        /// 
        /// <exception cref="CryptoProcessingException">Thrown if the key file could not be found or a Header parameter does not match the keystream length</exception>
        public void Extract(out CipherKey KeyHeader, out KeyParams KeyParam)
        {
            if (!string.IsNullOrEmpty(_keyPath))
            {
                if (!File.Exists(_keyPath))
                    throw new CryptoProcessingException("KeyFactory:Extract", "The key file could not be found! Check the path.", new FileNotFoundException());
            }

            if (_keyStream == null)
                _keyStream = new FileStream(_keyPath, FileMode.Open, FileAccess.Read);

            KeyHeader = new CipherKey(_keyStream);
            CipherDescription dsc = KeyHeader.Description;

            if (_keyStream.Length < dsc.KeySize + dsc.IvSize + dsc.MacSize + CipherKey.GetHeaderSize())
                throw new CryptoProcessingException("KeyFactory:Extract", "The size of the key file does not align with the CipherKey sizes! Key is corrupt.", new ArgumentOutOfRangeException());

            _keyStream.Position = CipherKey.GetHeaderSize();
            KeyParam = KeyParams.DeSerialize(_keyStream);
        }
Exemple #3
0
        /// <summary>
        /// Create a single use key file using a <see cref="KeyParams"/> containing the key material, and a <see cref="CipherDescription"/> containing the cipher implementation details
        /// </summary>
        /// 
        /// <param name="Description">The <see cref="CipherDescription">Cipher Description</see> containing the cipher details</param>
        /// <param name="KeyParam">An initialized and populated key material container</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.MacSize > 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.MacSize)
                    throw new CryptoProcessingException("KeyFactory:Create", "Header MacSize does not align with the size of the KeyParam IKM!", new ArgumentOutOfRangeException());
            }

            if (_keyStream == null)
                _keyStream = new FileStream(_keyPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);

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