/// <summary> /// Initialize the class with a Block <see cref="VTDev.Libraries.CEXEngine.Crypto.Digest.IDigest">Cipher</see> and optional <see cref="VTDev.Libraries.CEXEngine.Crypto.Cipher.Symmetric.Block.Padding.IPadding">Padding</see> instances. /// <para>This constructor requires a fully initialized <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">CipherMode</see> instance. /// If the <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">PaddingMode</see> parameter is null, X9.23 padding will be used if required.</para> /// </summary> /// /// <param name="Cipher">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">Block Cipher</see> wrapped in a Cipher mode</param> /// <param name="Padding">The Padding 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(ICipherMode Cipher, IPadding Padding = null, bool DisposeEngine = false) { 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_cipherEngine = Cipher; m_isStreamCipher = false; m_blockSize = m_cipherEngine.BlockSize; m_isEncryption = m_cipherEngine.IsEncryption; m_isParallel = false; 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))) { m_isParallel = ((CBC)m_cipherEngine).IsParallel && !((CBC)m_cipherEngine).IsEncryption; } } }
public EngineSpeedTest(SymmetricEngines Engine, CipherModes Mode, int DataSize, int KeySize, int Rounds, bool Encryption, bool Parallel, TestTypes TestType = TestTypes.FileIO) { _cipherType = Mode; _dataSize = DataSize; _roundCount = Rounds; _engineType = Engine; _isEncryption = Encryption; _isParallel = Parallel; _keySize = KeySize; _keyParam = GetKeyParams(); _testType = TestType; if (IsStreamCipher()) { _streamCipher = GetStreamEngine(); _streamCipher.Initialize(_keyParam); if (_isParallel && _engineType == SymmetricEngines.Fusion || _engineType == SymmetricEngines.Salsa) { if (_dataSize > MB100) _blockSize = MB100; else if (DataSize > MB10) _blockSize = MB10; else if (DataSize > MB1) _blockSize = MB1; else _blockSize = 1024; } else { _blockSize = 64000; } } else { _cipherEngine = GetCipher(); _cipherEngine.Initialize(_isEncryption, _keyParam); // set parallel if (_cipherEngine.GetType().Equals(typeof(CTR))) ((CTR)_cipherEngine).IsParallel = _isParallel; else if (_cipherEngine.GetType().Equals(typeof(CBC))) ((CBC)_cipherEngine).IsParallel = _isParallel; else if (_cipherEngine.GetType().Equals(typeof(CFB))) ((CFB)_cipherEngine).IsParallel = _isParallel; // set block if (_isParallel && (_cipherType.Equals(CipherModes.CTR) || _cipherType.Equals(CipherModes.CBC) && !_isEncryption || _cipherType.Equals(CipherModes.CFB) && !_isEncryption)) { if (_dataSize > MB100) _blockSize = MB100; else if (DataSize > MB10) _blockSize = MB10; else if (DataSize > MB1) _blockSize = MB1; else _blockSize = 1024; if (_cipherEngine.GetType().Equals(typeof(CTR))) ((CTR)_cipherEngine).ParallelBlockSize = _blockSize; else if (_cipherEngine.GetType().Equals(typeof(CBC))) ((CBC)_cipherEngine).ParallelBlockSize = _blockSize; else if (_cipherEngine.GetType().Equals(typeof(CFB))) ((CFB)_cipherEngine).ParallelBlockSize = _blockSize; } else { _blockSize = _cipherEngine.BlockSize; } } _inputBuffer = new byte[_blockSize]; _outputBuffer = new byte[_blockSize]; }
/// <summary> /// Initialize the class with a Block <see cref="ICipherMode">Cipher</see> and optional <see cref="IPadding">Padding</see> instances. /// <para>This constructor requires a fully initialized <see cref="CipherModes">CipherMode</see> instance. /// If the <see cref="PaddingModes">PaddingMode</see> parameter is null, X9.23 padding will be used if required.</para> /// </summary> /// /// <param name="Cipher">The <see cref="SymmetricEngines">Block Cipher</see> wrapped in a <see cref="ICipherMode">Cipher</see> mode</param> /// <param name="Padding">The <see cref="IPadding">Padding</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(ICipherMode Cipher, IPadding Padding = null, bool DisposeEngine = false) { 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; _cipherEngine = Cipher; _isStreamCipher = false; _blockSize = _cipherEngine.BlockSize; _isEncryption = _cipherEngine.IsEncryption; _isParallel = false; if (_isCounterMode = _cipherEngine.GetType().Equals(typeof(CTR))) { if (_isParallel = ((CTR)_cipherEngine).IsParallel) _blockSize = ((CTR)_cipherEngine).ParallelBlockSize; } else { if (_cipherEngine.GetType().Equals(typeof(CBC))) _isParallel = ((CBC)_cipherEngine).IsParallel && !((CBC)_cipherEngine).IsEncryption; } }
/// <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; } } } }
/// <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); } } }
/// <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; } } } } }
public EngineSpeedTest(SymmetricEngines Engine, CipherModes Mode, int DataSize, int KeySize, int Rounds, bool Encryption, bool Parallel, TestTypes TestType = TestTypes.FileIO) { _cipherType = Mode; _dataSize = DataSize; _roundCount = Rounds; _engineType = Engine; _isEncryption = Encryption; _isParallel = Parallel; _keySize = KeySize; _keyParam = GetKeyParams(); _testType = TestType; if (IsStreamCipher()) { _streamCipher = GetStreamEngine(); _streamCipher.Initialize(_keyParam); if (_isParallel && _engineType == SymmetricEngines.ChaCha || _engineType == SymmetricEngines.Salsa) { if (_dataSize > MB100) { _blockSize = MB100; } else if (DataSize > MB10) { _blockSize = MB10; } else if (DataSize > MB1) { _blockSize = MB1; } else { _blockSize = 1024; } // align block if (_isParallel) { _blockSize -= (_blockSize % (64 * Environment.ProcessorCount)); } } else { _blockSize = 64000; } } else { _cipherEngine = GetCipher(); _cipherEngine.Initialize(_isEncryption, _keyParam); // set parallel if (_cipherEngine.GetType().Equals(typeof(CTR))) { ((CTR)_cipherEngine).IsParallel = _isParallel; } else if (_cipherEngine.GetType().Equals(typeof(CBC))) { ((CBC)_cipherEngine).IsParallel = _isParallel; } else if (_cipherEngine.GetType().Equals(typeof(CFB))) { ((CFB)_cipherEngine).IsParallel = _isParallel; } // set block if (_isParallel && (_cipherType.Equals(CipherModes.CTR) || _cipherType.Equals(CipherModes.CBC) && !_isEncryption || _cipherType.Equals(CipherModes.CFB) && !_isEncryption)) { if (_dataSize > MB100) { _blockSize = MB100; } else if (DataSize > MB10) { _blockSize = MB10; } else if (DataSize > MB1) { _blockSize = MB1; } else { _blockSize = 1024; } // align block if (_isParallel) { _blockSize -= (_blockSize % (16 * Environment.ProcessorCount)); } if (_cipherEngine.GetType().Equals(typeof(CTR))) { ((CTR)_cipherEngine).ParallelBlockSize = _blockSize; } else if (_cipherEngine.GetType().Equals(typeof(CBC))) { ((CBC)_cipherEngine).ParallelBlockSize = _blockSize; } else if (_cipherEngine.GetType().Equals(typeof(CFB))) { ((CFB)_cipherEngine).ParallelBlockSize = _blockSize; } } else { _blockSize = _cipherEngine.BlockSize; } } _inputBuffer = new byte[_blockSize]; _outputBuffer = new byte[_blockSize]; }