Exemplo n.º 1
0
        private void OnModeChanged(object sender, EventArgs e)
        {
            CipherModes cmode = CipherModes.CTR;

            Enum.TryParse <CipherModes>(((ComboBox)sender).Text, out cmode);
            _cipherType = cmode;
        }
Exemplo n.º 2
0
        private ICipherMode GetCipherMode(CipherModes CipherType, BlockCiphers EngineType, int BlockSize, int RoundCount, Digests KdfEngine)
        {
            IBlockCipher engine = GetBlockCipher(EngineType, BlockSize, RoundCount, KdfEngine);

            try
            {
                return(CipherModeFromName.GetInstance(CipherType, engine));
            }
            catch (Exception ex)
            {
                throw new CryptoProcessingException("CipherStream:GetCipherMode", ex);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initialize the class as a Block Cipher
        /// </summary>
        /// <param name="Cipher">Block Cipher instance</param>
        /// <param name="KeyParam">Key and vector material</param>
        /// <param name="Mode">Cipher mode</param>
        /// <param name="Padding">Padding type</param>
        public Transform(IBlockCipher Cipher, KeyParams KeyParam, CipherModes Mode = CipherModes.CTR, PaddingModes Padding = PaddingModes.X923)
        {
            this.KeyParam = KeyParam;

            if (Mode == CipherModes.CBC)
                this.CipherMode = new CBC(Cipher);
            else
                this.CipherMode = new CTR(Cipher);

            if (Padding == PaddingModes.PKCS7)
                this.Padding = new PKCS7();
            else if (Padding == PaddingModes.X923)
                this.Padding = new X923();

            this.IsStream = false;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a volume key file using a manual description of the cipher parameters.
        /// </summary>
        ///
        /// <param name="KeyCount">The number of key sets associated with this volume key</param>
        /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">Engine</see> type</param>
        /// <param name="KeySize">The cipher Key Size in bytes</param>
        /// <param name="IvSize">Size of the cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.IVSizes">Initialization Vector</see></param>
        /// <param name="CipherType">The type of <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">Cipher Mode</see></param>
        /// <param name="PaddingType">The type of cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">Padding Mode</see></param>
        /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param>
        /// <param name="Rounds">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param>
        /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param>
        ///
        /// <returns>A populated VolumeKey</returns>
        public MemoryStream CreateKey(int KeyCount, SymmetricEngines EngineType, int KeySize, IVSizes IvSize,
                                      CipherModes CipherType, PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts Rounds, Digests KdfEngine)
        {
            CipherDescription dsc = new CipherDescription()
            {
                EngineType  = (int)EngineType,
                KeySize     = KeySize,
                IvSize      = (int)IvSize,
                CipherType  = (int)CipherType,
                PaddingType = (int)PaddingType,
                BlockSize   = (int)BlockSize,
                RoundCount  = (int)Rounds,
                KdfEngine   = (int)KdfEngine,
            };

            return(CreateKey(dsc, KeyCount));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get an Cipher Mode instance by name using default parameters
        /// </summary>
        ///
        /// <param name="CipherType">The cipher mode enumeration name</param>
        /// <param name="Engine">The block cipher instance</param>
        ///
        /// <returns>An initialized digest</returns>
        ///
        /// <exception cref="CryptoProcessingException">Thrown if the enumeration name is not supported</exception>
        public static ICipherMode GetInstance(CipherModes CipherType, IBlockCipher Engine)
        {
            switch (CipherType)
            {
            case CipherModes.CTR:
                return(new CTR(Engine));

            case CipherModes.CBC:
                return(new CBC(Engine));

            case CipherModes.CFB:
                return(new CFB(Engine));

            case CipherModes.OFB:
                return(new OFB(Engine));

            default:
                throw new CryptoProcessingException("CipherModeFromName:GetInstance", "The cipher mode is not supported!");
            }
        }
Exemplo n.º 6
0
        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];
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a single use Key file using a manual description of the cipher parameters.
        /// </summary>
        ///
        /// <param name="KeyParam">An initialized and populated key material container</param>
        /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">Engine</see> type</param>
        /// <param name="KeySize">The cipher Key Size in bytes</param>
        /// <param name="IvSize">Size of the cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.IVSizes">Initialization Vector</see></param>
        /// <param name="CipherType">The type of <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">Cipher Mode</see></param>
        /// <param name="PaddingType">The type of cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">Padding Mode</see></param>
        /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param>
        /// <param name="Rounds">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param>
        /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param>
        /// <param name="MacSize">The size of the HMAC message authentication code; a zeroed parameter means authentication is not enabled with this key</param>
        /// <param name="MacEngine">The HMAC <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param>
        ///
        /// <exception cref="System.ArgumentNullException">Thrown if a KeyParams member is null, but specified in the Header</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if a Header parameter does not match a KeyParams value</exception>
        public void Create(KeyParams KeyParam, SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType,
                           PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts Rounds, Digests KdfEngine, int MacSize, Digests MacEngine)
        {
            CipherDescription dsc = new CipherDescription()
            {
                EngineType  = (int)EngineType,
                KeySize     = KeySize,
                IvSize      = (int)IvSize,
                CipherType  = (int)CipherType,
                PaddingType = (int)PaddingType,
                BlockSize   = (int)BlockSize,
                RoundCount  = (int)Rounds,
                KdfEngine   = (int)KdfEngine,
                MacEngine   = (int)MacEngine,
                MacKeySize  = MacSize
            };

            Create(dsc, KeyParam);
        }
Exemplo n.º 8
0
 private ICipherMode GetCipher(CipherModes CipherType, SymmetricEngines EngineType, int RoundCount, int BlockSize, Digests KdfEngine)
 {
     if (CipherType == CipherModes.CBC)
         return new CBC(GetBlockEngine(EngineType, RoundCount, BlockSize, KdfEngine));
     else if (CipherType == CipherModes.CFB)
         return new CFB(GetBlockEngine(EngineType, RoundCount, BlockSize, KdfEngine), BlockSize * 8);
     else if (CipherType == CipherModes.OFB)
         return new OFB(GetBlockEngine(EngineType, RoundCount, BlockSize, KdfEngine));
     else
         return new CTR(GetBlockEngine(EngineType, RoundCount, BlockSize, KdfEngine));
 }
Exemplo n.º 9
0
 /// <summary>
 /// CipherDescription constructor
 /// </summary>
 ///
 /// <param name="EngineType">The Cryptographic <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.SymmetricEngines">Engine</see> type</param>
 /// <param name="KeySize">The cipher Key Size in bytes</param>
 /// <param name="IvSize">Size of the cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.IVSizes">Initialization Vector</see></param>
 /// <param name="CipherType">The type of <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.CipherModes">Cipher Mode</see></param>
 /// <param name="PaddingType">The type of cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.PaddingModes">Padding Mode</see></param>
 /// <param name="BlockSize">The cipher <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.BlockSizes">Block Size</see></param>
 /// <param name="RoundCount">The number of diffusion <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.RoundCounts">Rounds</see></param>
 /// <param name="KdfEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> digest engine used to power the key schedule Key Derivation Function</param>
 /// <param name="MacKeySize">The size of the HMAC key in bytes; a zeroed parameter means authentication is not enabled with this key</param>
 /// <param name="MacEngine">The HMAC <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param>
 ///
 /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid KeyId, MessageKey, or ExtensionKey is used</exception>
 public CipherDescription(SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType,
                          BlockSizes BlockSize, RoundCounts RoundCount, Digests KdfEngine = Digests.None, int MacKeySize = 0, Digests MacEngine = Digests.None)
 {
     this.EngineType  = (int)EngineType;
     this.KeySize     = KeySize;
     this.IvSize      = (int)IvSize;
     this.CipherType  = (int)CipherType;
     this.PaddingType = (int)PaddingType;
     this.BlockSize   = (int)BlockSize;
     this.RoundCount  = (int)RoundCount;
     this.KdfEngine   = (int)KdfEngine;
     this.MacKeySize  = MacKeySize;
     this.MacEngine   = (int)MacEngine;
 }
Exemplo n.º 10
0
        /// <summary>
        /// Create a volume key file using a manual description of the cipher parameters.
        /// </summary>
        /// 
        /// <param name="KeyCount">The number of key sets associated with this volume key</param>
        /// <param name="EngineType">The Cryptographic <see cref="SymmetricEngines">Engine</see> type</param>
        /// <param name="KeySize">The cipher Key Size in bytes</param>
        /// <param name="IvSize">Size of the cipher <see cref="IVSizes">Initialization Vector</see></param>
        /// <param name="CipherType">The type of <see cref="CipherModes">Cipher Mode</see></param>
        /// <param name="PaddingType">The type of cipher <see cref="PaddingModes">Padding Mode</see></param>
        /// <param name="BlockSize">The cipher <see cref="BlockSizes">Block Size</see></param>
        /// <param name="Rounds">The number of diffusion <see cref="RoundCounts">Rounds</see></param>
        /// <param name="KdfEngine">The <see cref="Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param>
        /// <param name="MacSize">The size of the HMAC message authentication code; a zeroed parameter means authentication is not enabled with this key</param>
        /// <param name="MacEngine">The HMAC <see cref="Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param>
        /// 
        /// <exception cref="System.IO.FileLoadException">A key file exists at the path specified</exception>
        /// <exception cref="System.UnauthorizedAccessException">The key file path is read only</exception>
        public void Create(int KeyCount, SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType,
            PaddingModes PaddingType, BlockSizes BlockSize, RoundCounts Rounds, Digests KdfEngine, int MacSize, Digests MacEngine)
        {
            CipherDescription dsc = new CipherDescription()
            {
                EngineType = (int)EngineType,
                KeySize = KeySize,
                IvSize = (int)IvSize,
                CipherType = (int)CipherType,
                PaddingType = (int)PaddingType,
                BlockSize = (int)BlockSize,
                RoundCount = (int)Rounds,
                KdfEngine = (int)KdfEngine,
                MacEngine = (int)MacEngine,
                MacSize = MacSize
            };

            Create(dsc, KeyCount);
        }
Exemplo n.º 11
0
 private void OnModeChanged(object sender, EventArgs e)
 {
     CipherModes cmode = CipherModes.CTR;
     Enum.TryParse<CipherModes>(((ComboBox)sender).Text, out cmode);
     _cipherType = cmode;
 }
Exemplo n.º 12
0
        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];
        }
Exemplo n.º 13
0
 /// <summary>
 /// CipherDescription constructor
 /// </summary>
 /// 
 /// <param name="EngineType">The Cryptographic <see cref="SymmetricEngines">Engine</see> type</param>
 /// <param name="KeySize">The cipher Key Size in bytes</param>
 /// <param name="IvSize">Size of the cipher <see cref="IVSizes">Initialization Vector</see></param>
 /// <param name="CipherType">The type of <see cref="CipherModes">Cipher Mode</see></param>
 /// <param name="PaddingType">The type of cipher <see cref="PaddingModes">Padding Mode</see></param>
 /// <param name="BlockSize">The cipher <see cref="BlockSizes">Block Size</see></param>
 /// <param name="RoundCount">The number of diffusion <see cref="RoundCounts">Rounds</see></param>
 /// <param name="KdfEngine">The <see cref="Digests">Digest</see> engine used to power the key schedule Key Derivation Function in HX and M series ciphers</param>
 /// <param name="MacSize">The size of the HMAC message authentication code; a zeroed parameter means authentication is not enabled with this key</param>
 /// <param name="MacEngine">The HMAC <see cref="Digests">Digest</see> engine used to authenticate a message file encrypted with this key</param>
 /// 
 /// <exception cref="System.ArgumentOutOfRangeException">Thrown if an invalid KeyId, MessageKey, or ExtensionKey is used</exception>
 public CipherDescription(SymmetricEngines EngineType, int KeySize, IVSizes IvSize, CipherModes CipherType, PaddingModes PaddingType,
     BlockSizes BlockSize, RoundCounts RoundCount, Digests KdfEngine = Digests.SHA512, int MacSize = 64, Digests MacEngine = Digests.SHA512)
 {
     this.EngineType = (Int32)EngineType;
     this.KeySize = KeySize;
     this.IvSize = (Int32)IvSize;
     this.CipherType = (Int32)CipherType;
     this.PaddingType = (Int32)PaddingType;
     this.BlockSize = (Int32)BlockSize;
     this.RoundCount = (Int32)RoundCount;
     this.KdfEngine = (Int32)KdfEngine;
     this.MacSize = MacSize;
     this.MacEngine = (Int32)MacEngine;
 }