Exemple #1
0
        /// <summary>
        /// Initialize the Cipher MAC.
        /// <para>Uses the Key or IKM field, and optionally the IV field of the KeyParams class.</para>
        /// </summary>
        ///
        /// <param name="MacKey">A byte array containing the cipher Key.
        /// <para>Key size must be one of the <c>LegalKeySizes</c> of the underlying cipher.</para>
        /// </param>
        /// <param name="IV">A byte array containing the cipher Initialization Vector.
        /// <para>IV size must be the ciphers blocksize.</para></param>
        ///
        /// <exception cref="CryptoMacException">Thrown if an invalid Input size is chosen</exception>
        public void Initialize(byte[] MacKey, byte[] IV)
        {
            if (MacKey == null)
            {
                throw new CryptoMacException("CMAC:Initialize", "Key can not be null!", new ArgumentNullException());
            }

            if (IV == null)
            {
                IV = new byte[m_blockSize];
            }
            if (IV.Length != m_blockSize)
            {
                Array.Resize <byte>(ref IV, m_blockSize);
            }

            m_cipherKey = new KeyParams(MacKey, IV);
            m_cipherMode.Initialize(true, m_cipherKey);
            byte[] lu   = new byte[m_blockSize];
            byte[] tmpz = new byte[m_blockSize];
            m_cipherMode.Transform(tmpz, 0, lu, 0);
            m_K1 = GenerateSubkey(lu);
            m_K2 = GenerateSubkey(m_K1);
            m_cipherMode.Initialize(true, m_cipherKey);
            m_isInitialized = true;
        }
Exemple #2
0
        /// <summary>
        /// Initialize the MAC
        /// </summary>
        ///
        /// <param name="KeyParam">A <see cref="KeyParams"/> containing Key and IV.
        /// <para>Uses the Key and IV fields of the KeyParams parameter.
        /// Key size must be one of the <c>LegalKeySizes</c> of the underlying cipher.
        /// IV size must be the ciphers blocksize.
        /// </para>
        /// </param>
        ///
        /// <exception cref="CryptoMacException">Thrown if an invalid Input size is chosen</exception>
        public void Initialize(KeyParams KeyParam)
        {
            if (KeyParam.Key == null)
            {
                throw new CryptoMacException("CMAC:Initialize", "Key can not be null!", new ArgumentNullException());
            }

            byte[] tmpIv = new byte[_blockSize];
            // convert for cipher
            KeyParams key = new KeyParams(KeyParam.Key, tmpIv);

            _cipherType.Initialize(true, key);

            _L = new byte[_tmpZeroes.Length];
            _cipherType.Transform(_tmpZeroes, 0, _L, 0);
            _LU  = DoubleLu(_L);
            _LU2 = DoubleLu(_LU);
            _cipherType.Initialize(true, key);

            _isInitialized = true;
        }
Exemple #3
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];
        }
Exemple #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="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;
                    }
                }
            }
        }
Exemple #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;
                        }
                    }
                }
            }
        }
Exemple #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.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];
        }