Пример #1
0
        /// <summary>
        /// Initialize the class with a <see cref="VTDev.Libraries.CEXEngine.Crypto.Cipher.Symmetric.Stream.IStreamCipher">Stream Cipher</see> instance.
        /// <para>This constructor requires a fully initialized <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">CipherStream</see> instance.</para>
        /// </summary>
        ///
        /// <param name="Cipher">The initialized Stream Cipher instance</param>
        /// <param name="DisposeEngine">Dispose of cipher engine when <see cref="Dispose()"/> on this class is called</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if a null or uninitialized Cipher is used</exception>
        public PacketCipher(IStreamCipher Cipher, bool DisposeEngine = true)
        {
            if (Cipher == null)
            {
                throw new CryptoProcessingException("PacketCipher:CTor", "The Cipher can not be null!", new ArgumentNullException());
            }
            if (!Cipher.IsInitialized)
            {
                throw new CryptoProcessingException("PacketCipher:CTor", "The Cipher has not been initialized!", new ArgumentException());
            }

            m_disposeEngine  = DisposeEngine;
            m_streamCipher   = Cipher;
            m_isStreamCipher = true;
            m_blockSize      = 1024;
            m_isCounterMode  = false;

            // set defaults
            if (m_streamCipher.GetType().Equals(typeof(ChaCha20)))
            {
                if (m_isParallel = ((ChaCha20)m_streamCipher).IsParallel)
                {
                    m_blockSize = ((ChaCha20)m_streamCipher).ParallelBlockSize;
                }
            }
            else
            {
                if (m_isParallel = ((Salsa20)m_streamCipher).IsParallel)
                {
                    m_blockSize = ((Salsa20)m_streamCipher).ParallelBlockSize;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Initialize the class with a <see cref="IStreamCipher">Stream Cipher</see> instance.
        /// <para>This constructor requires a fully initialized <see cref="SymmetricEngines">StreamCipher</see> instance.</para>
        /// </summary>
        /// 
        /// <param name="Cipher">The initialized <see cref="IStreamCipher">Stream Cipher</see> instance</param>
        /// <param name="DisposeEngine">Dispose of cipher engine when <see cref="Dispose()"/> on this class is called</param>
        /// 
        /// <exception cref="CryptoProcessingException">Thrown if a null or uninitialized <see cref="ICipherMode">Cipher</see> is used</exception>
        public PacketCipher(IStreamCipher Cipher, bool DisposeEngine = true)
        {
            if (Cipher == null)
                throw new CryptoProcessingException("PacketCipher:CTor", "The Cipher can not be null!", new ArgumentNullException());
            if (!Cipher.IsInitialized)
                throw new CryptoProcessingException("PacketCipher:CTor", "The Cipher has not been initialized!", new ArgumentException());

            _disposeEngine = DisposeEngine;
            _streamCipher = Cipher;
            _isStreamCipher = true;
            _blockSize = 1024;
            _isCounterMode = false;

            // set defaults
            if (_streamCipher.GetType().Equals(typeof(Fusion)))
            {
                if (_isParallel = ((Fusion)_streamCipher).IsParallel)
                    _blockSize = ((Fusion)_streamCipher).ParallelBlockSize;
            }
            else
            {
                _isParallel = false;
            }
        }
Пример #3
0
        /// <summary>
        /// Initialize the class with a CipherDescription Structure; containing the cipher implementation details, and a <see cref="KeyParams"/> class containing the Key material.
        /// <para>This constructor creates and configures cryptographic instances based on the cipher description contained in a CipherDescription. 
        /// Cipher modes, padding, and engines are destroyed automatically through this classes Dispose() method.</para>
        /// </summary>
        /// 
        /// <param name="Encryption">Cipher is an encryptor</param>
        /// <param name="Description">A <see cref="CipherDescription"/> containing the cipher description</param>
        /// <param name="KeyParam">A <see cref="KeyParams"/> class containing the encryption Key material</param>
        /// 
        /// <exception cref="CryptoProcessingException">Thrown if an invalid <see cref="CipherDescription">CipherDescription</see> or <see cref="KeyParams">KeyParams</see> is used</exception>
        public PacketCipher(bool Encryption, CipherDescription Description, KeyParams KeyParam)
        {
            if (!CipherDescription.IsValid(Description))
                throw new CryptoProcessingException("PacketCipher:CTor", "The key Header is invalid!", new ArgumentException());
            if (KeyParam == null)
                throw new CryptoProcessingException("PacketCipher:CTor", "KeyParam can not be null!", new ArgumentNullException());

            _disposeEngine = true;
            _isEncryption = Encryption;
            _blockSize = Description.BlockSize;
            _isParallel = false;

            if (_isStreamCipher = IsStreamCipher((SymmetricEngines)Description.EngineType))
            {
                _streamCipher = GetStreamEngine((SymmetricEngines)Description.EngineType, Description.RoundCount, (Digests)Description.KdfEngine);
                _streamCipher.Initialize(KeyParam);

                if (_streamCipher.GetType().Equals(typeof(Fusion)))
                {
                    if (_isParallel = ((Fusion)_streamCipher).IsParallel)
                        _blockSize = ((Fusion)_streamCipher).ParallelBlockSize;
                }
            }
            else
            {
                _cipherEngine = GetCipher((CipherModes)Description.CipherType, (SymmetricEngines)Description.EngineType, Description.RoundCount, Description.BlockSize, (Digests)Description.KdfEngine);
                _cipherEngine.Initialize(_isEncryption, KeyParam);

                if (_isCounterMode = _cipherEngine.GetType().Equals(typeof(CTR)))
                {
                    if (_isParallel = ((CTR)_cipherEngine).IsParallel)
                        _blockSize = ((CTR)_cipherEngine).ParallelBlockSize;
                }
                else
                {
                    if (_cipherEngine.GetType().Equals(typeof(CBC)))
                    {
                        if (_isParallel = ((CBC)_cipherEngine).IsParallel && !((CBC)_cipherEngine).IsEncryption)
                            _blockSize = ((CBC)_cipherEngine).ParallelBlockSize;
                    }
                    else if (_cipherEngine.GetType().Equals(typeof(CFB)))
                    {
                        if (_isParallel = ((CFB)_cipherEngine).IsParallel && !((CFB)_cipherEngine).IsEncryption)
                            _blockSize = ((CFB)_cipherEngine).ParallelBlockSize;
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Initialize the class with a CipherDescription Structure; containing the cipher implementation details, and a <see cref="KeyParams"/> class containing the Key material.
        /// <para>This constructor creates and configures cryptographic instances based on the cipher description contained in a CipherDescription. 
        /// Cipher modes, padding, and engines are destroyed automatically through this classes Dispose() method.</para>
        /// </summary>
        /// 
        /// <param name="Encryption">Cipher is an encryptor</param>
        /// <param name="KeyStream">A stream containing a <see cref="VolumeKey"/> and the keying material</param>
        /// 
        /// <exception cref="CryptoProcessingException">Thrown if an invalid <see cref="VolumeKey"/> is used</exception>
        public VolumeCipher(bool Encryption, Stream KeyStream)
        {
            _keyStream = KeyStream;
            _volumeKey = new VolumeKey(KeyStream);

            if (!CipherDescription.IsValid(_volumeKey.Description))
                throw new CryptoProcessingException("VolumeCipher:CTor", "The key Header is invalid!", new ArgumentException());

            _disposeEngine = true;
            _isEncryption = Encryption;
            _blockSize = _volumeKey.Description.BlockSize;
            _isParallel = false;
            CipherDescription desc = _volumeKey.Description;

            if (_isStreamCipher = IsStreamCipher((SymmetricEngines)desc.EngineType))
            {
                _streamCipher = GetStreamEngine((SymmetricEngines)desc.EngineType, desc.RoundCount, (Digests)desc.KdfEngine);

                if (_streamCipher.GetType().Equals(typeof(Fusion)))
                {
                    if (_isParallel = ((Fusion)_streamCipher).IsParallel)
                        _blockSize = ((Fusion)_streamCipher).ParallelBlockSize;
                }
            }
            else
            {
                _cipherEngine = GetCipher((CipherModes)desc.CipherType, (SymmetricEngines)desc.EngineType, desc.RoundCount, desc.BlockSize, (Digests)desc.KdfEngine);

                if (_isCounterMode = _cipherEngine.GetType().Equals(typeof(CTR)))
                {
                    if (_isParallel = ((CTR)_cipherEngine).IsParallel)
                        _blockSize = ((CTR)_cipherEngine).ParallelBlockSize;
                }
                else
                {
                    if (_cipherEngine.GetType().Equals(typeof(CBC)))
                    {
                        if (_isParallel = ((CBC)_cipherEngine).IsParallel && !((CBC)_cipherEngine).IsEncryption)
                            _blockSize = ((CBC)_cipherEngine).ParallelBlockSize;
                    }
                    else if (_cipherEngine.GetType().Equals(typeof(CFB)))
                    {
                        if (_isParallel = ((CFB)_cipherEngine).IsParallel && !((CFB)_cipherEngine).IsEncryption)
                            _blockSize = ((CFB)_cipherEngine).ParallelBlockSize;
                    }
                    _cipherPadding = GetPadding((PaddingModes)_volumeKey.Description.PaddingType);
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Initialize the class with a CipherDescription Structure; containing the cipher implementation details, and a <see cref="KeyParams"/> class containing the Key material.
        /// <para>This constructor creates and configures cryptographic instances based on the cipher description contained in a CipherDescription.
        /// Cipher modes, padding, and engines are destroyed automatically through this classes Dispose() method.</para>
        /// </summary>
        ///
        /// <param name="Encryption">Cipher is an encryptor</param>
        /// <param name="Description">A <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription"/> containing the cipher description</param>
        /// <param name="KeyParam">A <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.KeyParams"/> class containing the encryption Key material</param>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if an invalid CipherDescription or KeyParams is used</exception>
        public PacketCipher(bool Encryption, CipherDescription Description, KeyParams KeyParam)
        {
            if (!CipherDescription.IsValid(Description))
            {
                throw new CryptoProcessingException("PacketCipher:CTor", "The key Header is invalid!", new ArgumentException());
            }
            if (KeyParam == null)
            {
                throw new CryptoProcessingException("PacketCipher:CTor", "KeyParam can not be null!", new ArgumentNullException());
            }

            m_disposeEngine = true;
            m_isEncryption  = Encryption;
            m_blockSize     = Description.BlockSize;
            m_isParallel    = false;

            if (m_isStreamCipher = IsStreamCipher((SymmetricEngines)Description.EngineType))
            {
                m_streamCipher = GetStreamCipher((StreamCiphers)Description.EngineType, Description.RoundCount);
                m_streamCipher.Initialize(KeyParam);

                if (m_streamCipher.GetType().Equals(typeof(ChaCha20)))
                {
                    if (m_isParallel = ((ChaCha20)m_streamCipher).IsParallel)
                    {
                        m_blockSize = ((ChaCha20)m_streamCipher).ParallelBlockSize;
                    }
                }
                else
                {
                    if (m_isParallel = ((Salsa20)m_streamCipher).IsParallel)
                    {
                        m_blockSize = ((Salsa20)m_streamCipher).ParallelBlockSize;
                    }
                }
            }
            else
            {
                m_cipherEngine = GetCipherMode((CipherModes)Description.CipherType, (BlockCiphers)Description.EngineType, Description.BlockSize, Description.RoundCount, (Digests)Description.KdfEngine);
                m_cipherEngine.Initialize(m_isEncryption, KeyParam);

                if (m_isCounterMode = m_cipherEngine.GetType().Equals(typeof(CTR)))
                {
                    if (m_isParallel = ((CTR)m_cipherEngine).IsParallel)
                    {
                        m_blockSize = ((CTR)m_cipherEngine).ParallelBlockSize;
                    }
                }
                else
                {
                    if (m_cipherEngine.GetType().Equals(typeof(CBC)))
                    {
                        if (m_isParallel = ((CBC)m_cipherEngine).IsParallel && !((CBC)m_cipherEngine).IsEncryption)
                        {
                            m_blockSize = ((CBC)m_cipherEngine).ParallelBlockSize;
                        }
                    }
                    else if (m_cipherEngine.GetType().Equals(typeof(CFB)))
                    {
                        if (m_isParallel = ((CFB)m_cipherEngine).IsParallel && !((CFB)m_cipherEngine).IsEncryption)
                        {
                            m_blockSize = ((CFB)m_cipherEngine).ParallelBlockSize;
                        }
                    }
                }
            }
        }