Example #1
0
        private void ParallelDecrypt(ICipherMode Cipher, IPadding Padding, byte[] Input, int InOffset, byte[] Output, int OutOffset)
        {
            int  blkSize = Cipher.ParallelBlockSize;
            long inpSize = (Input.Length - InOffset);
            long alnSize = (inpSize / blkSize) * blkSize;
            long count   = 0;

            Cipher.IsParallel        = true;
            Cipher.ParallelBlockSize = blkSize;

            // parallel
            while (count != alnSize)
            {
                Cipher.Transform(Input, InOffset, Output, OutOffset);
                InOffset  += blkSize;
                OutOffset += blkSize;
                count     += blkSize;
            }

            if (alnSize != inpSize)
            {
                int cnkSize = (int)(inpSize - alnSize);
                BlockDecrypt(Cipher, Padding, Input, InOffset, ref Output, OutOffset);
            }
        }
Example #2
0
        /// <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;
                }
            }
        }
Example #3
0
        private void BlockEncrypt(ICipherMode Cipher, IPadding Padding, byte[] Input, int InOffset, ref byte[] Output, int OutOffset)
        {
            int  blkSize = Cipher.BlockSize;
            long inpSize = (Input.Length - InOffset);
            long alnSize = inpSize - (inpSize % blkSize);
            long count   = 0;

            Cipher.IsParallel = false;

            while (count != alnSize)
            {
                Cipher.Transform(Input, InOffset, Output, OutOffset);
                InOffset  += blkSize;
                OutOffset += blkSize;
                count     += blkSize;
            }

            // partial
            if (alnSize != inpSize)
            {
                int    fnlSize   = (int)(inpSize - alnSize);
                byte[] inpBuffer = new byte[blkSize];
                Buffer.BlockCopy(Input, InOffset, inpBuffer, 0, fnlSize);
                Padding.AddPadding(inpBuffer, fnlSize);
                byte[] outBuffer = new byte[blkSize];
                Cipher.Transform(inpBuffer, 0, outBuffer, 0);
                if (Output.Length != OutOffset + blkSize)
                {
                    Array.Resize(ref Output, OutOffset + blkSize);
                }
                Buffer.BlockCopy(outBuffer, 0, Output, OutOffset, blkSize);
                count += blkSize;
            }
        }
Example #4
0
        private void BlockDecrypt(ICipherMode Cipher, IPadding Padding, byte[] Input, int InOffset, ref byte[] Output, int OutOffset)
        {
            int  blkSize = Cipher.BlockSize;
            long inpSize = (Input.Length - InOffset);
            long alnSize = inpSize - blkSize;
            long count   = 0;

            Cipher.IsParallel = false;

            while (count != alnSize)
            {
                Cipher.Transform(Input, InOffset, Output, OutOffset);
                InOffset  += blkSize;
                OutOffset += blkSize;
                count     += blkSize;
            }

            // last block
            byte[] inpBuffer = new byte[blkSize];
            Buffer.BlockCopy(Input, InOffset, inpBuffer, 0, blkSize);
            byte[] outBuffer = new byte[blkSize];
            Cipher.Transform(inpBuffer, 0, outBuffer, 0);
            int fnlSize = blkSize - Padding.GetPaddingLength(outBuffer, 0);

            Buffer.BlockCopy(outBuffer, 0, Output, OutOffset, fnlSize);
            OutOffset += fnlSize;

            if (Output.Length != OutOffset)
            {
                Array.Resize(ref Output, OutOffset);
            }
        }
Example #5
0
        public static byte[] Extend(byte[] key, IPadding padding)
        {
            if (key.Length == 16 ||
                key.Length == 24 ||
                key.Length == 32)
            {
                return(key);
            }

            int length = 0;

            if (key.Length < 16)
            {
                length = 16;
            }
            else if (key.Length < 24)
            {
                length = 24;
            }
            else if (key.Length < 32)
            {
                length = 32;
            }

            var extended = new byte[length];

            Array.Copy(key, extended, key.Length);
            padding.Add(extended, key.Length - 1);
            return(extended);
        }
Example #6
0
        void CompareOutput(IPadding Padding)
        {
            CSPPrng rng = new CSPPrng();

            byte[] fill = new byte[16];
            rng.GetBytes(fill);
            const int BLOCK = 16;

            for (int i = 0; i < BLOCK; i++)
            {
                byte[] data = new byte[BLOCK];
                // fill with rand
                if (i > 0)
                {
                    Array.Copy(fill, data, BLOCK - i);
                }

                // pad array
                Padding.AddPadding(data, i);
                // verify length
                int len = Padding.GetPaddingLength(data);

                if (len == 0 && i != 0)
                {
                    throw new Exception("PaddingTest: Failed the padding value return check!");
                }
                else if (i != 0 && len != BLOCK - i)
                {
                    throw new Exception("PaddingTest: Failed the padding value return check!");
                }

                // test offset method
                if (i > 0 && i < 15)
                {
                    len = Padding.GetPaddingLength(data, i);

                    if (len == 0 && i != 0)
                    {
                        throw new Exception("PaddingTest: Failed the padding value return check!");
                    }
                    else if (i != 0 && len != BLOCK - i)
                    {
                        throw new Exception("PaddingTest: Failed the padding value return check!");
                    }
                }
            }

            rng.Dispose();
        }
Example #7
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;
        }
Example #8
0
        public CBC(ICipher cipher, IPadding padding, byte[] IV) : base(cipher, padding)
        {
            if (IV == null)
            {
                throw new ArgumentNullException(nameof(IV), "Initialization vector IV is null.");
            }
            if (IV.Length == 0)
            {
                throw new ArgumentNullException(nameof(IV), "Initialization vector IV is empty.");
            }
            if (IV.Length != cipher.BlockSize)
            {
                throw new ArgumentNullException(nameof(IV), "Initialization vector IV length has to be equal to block size.");
            }

            this.IV = IV;
        }
Example #9
0
        void StreamModesTest(ICipherMode Cipher, IPadding Padding)
        {
            if (Cipher.Engine.LegalKeySizes[0] > 32)
            {
                AllocateRandom(ref _key, 192);
            }
            else
            {
                AllocateRandom(ref _key, 32);
            }

            AllocateRandom(ref _iv, 16);
            // we are testing padding modes, make sure input size is random, but -not- block aligned..
            AllocateRandom(ref _plnText, 0, Cipher.BlockSize);

            KeyParams    kp   = new KeyParams(_key, _iv);
            MemoryStream mIn  = new MemoryStream(_plnText);
            MemoryStream mOut = new MemoryStream();
            MemoryStream mRes = new MemoryStream();

            CipherStream cs = new CipherStream(Cipher, Padding);

            cs.Initialize(true, kp);
            cs.Write(mIn, mOut);

            cs.Initialize(false, kp);
            mOut.Seek(0, SeekOrigin.Begin);
            cs.Write(mOut, mRes);

            int pos = (int)mRes.Position;

            byte[] res = new byte[_plnText.Length];
            Buffer.BlockCopy(mRes.ToArray(), 0, res, 0, pos);

            if (!Evaluate.AreEqual(res, _plnText))
            {
                throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
            }

            Cipher.Dispose();
        }
Example #10
0
        /// <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;
            }
        }
Example #11
0
 protected Encryptor(ICipher cipher, IPadding padding)
 {
     Cipher  = cipher;
     Padding = padding;
 }
Example #12
0
 private void Dispose(bool Disposing)
 {
     if (!_isDisposed && Disposing)
     {
         try
         {
             if (_disposeEngine)
             {
                 if (_cipherEngine != null)
                 {
                     _cipherEngine.Dispose();
                     _cipherEngine = null;
                 }
                 if (_cipherPadding != null)
                 {
                     _cipherPadding = null;
                 }
                 if (_streamCipher != null)
                 {
                     _streamCipher.Dispose();
                     _streamCipher = null;
                 }
             }
         }
         finally
         {
             _isDisposed = true;
         }
     }
 }
Example #13
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="Header">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 StreamCipher(bool Encryption, CipherDescription Header, KeyParams KeyParam)
        {
            if (!CipherDescription.IsValid(Header))
                throw new CryptoProcessingException("StreamCipher:CTor", "The key Header is invalid!", new ArgumentException());
            if (KeyParam == null)
                throw new CryptoProcessingException("StreamCipher:CTor", "KeyParam can not be null!", new ArgumentNullException());

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

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

                if (_streamCipher.GetType().Equals(typeof(Fusion)))
                {
                    if (_isParallel = ((Fusion)_streamCipher).IsParallel)
                        _blockSize = ((Fusion)_streamCipher).ParallelBlockSize;
                }
            }
            else
            {
                _cipherEngine = GetCipher((CipherModes)Header.CipherType, (SymmetricEngines)Header.EngineType, Header.RoundCount, Header.BlockSize, (Digests)Header.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;
                    }
                    _cipherPadding = GetPadding((PaddingModes)Header.PaddingType);
                }
            }
        }
Example #14
0
 public static void UpdatePadding(this Button platformControl, IPadding padding, Thickness?defaultPadding = null) =>
 UpdatePadding(platformControl, padding.Padding, defaultPadding);
Example #15
0
 /// <summary>
 /// Initialize the class with a Block Cipher wrapped in a <see cref="VTDev.Libraries.CEXEngine.Crypto.Cipher.Symmetric.Block.Mode.ICipherMode">Cipher Mode</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 <see cref="VTDev.Libraries.CEXEngine.Crypto.Cipher.Symmetric.Block.Mode.ICipherMode">Cipher</see> mode</param>
 /// <param name="Padding">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Cipher.Symmetric.Block.Padding.IPadding">Padding</see> instance</param>
 ///
 /// <exception cref="System.ArgumentNullException">Thrown if a null Cipher is used</exception>
 /// <exception cref="System.ArgumentException">Thrown if an uninitialized Cipher is used</exception>
 public CompressionCipher(ICipherMode Cipher, IPadding Padding = null) :
     base(Cipher, Padding)
 {
 }
Example #16
0
 public static void UpdatePadding(this Control nativeControl, IPadding padding, UI.Xaml.Thickness?defaultThickness = null) =>
 nativeControl.UpdatePadding(padding.Padding, defaultThickness);
Example #17
0
 /// <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="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="Compress">The volume cipher is in compression mode (encrypt)</param>
 /// <param name="Cipher">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Enumeration.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 Dispose() on this class is called</param>
 /// 
 /// <exception cref="System.ArgumentNullException">Thrown if a null <see cref="ICipherMode">Cipher</see> is used</exception>
 /// <exception cref="System.ArgumentException">Thrown if an uninitialized Cipher is used</exception>
 public CompressionCipher(bool Compress, ICipherMode Cipher, IPadding Padding = null, bool DisposeEngine = false)
     : base(Cipher, Padding, DisposeEngine)
 {
     _isCompression = Compress;
 }
Example #18
0
        internal Transform(string KeyPath)
        {
            if (!File.Exists(KeyPath)) return;
            // get key and algorithm
            this.ProgressInterval = PRG_INTV;
            this.KeyPath = KeyPath;
            this.Engine = KeyHeader.GetEngineType(KeyPath);
            this.Key = GetKey(KeyPath);
            int rounds = GetRoundsSize(KeyPath);

            // stream ciphers
            if (this.Engine == Engines.ChaCha)
                StreamCipher = new ChaCha(rounds);
            else if (this.Engine == Engines.DCS)
                StreamCipher = new DCS();
            if (this.Engine == Engines.Salsa)
                StreamCipher = new Salsa20(rounds);
            if (this.Engine == Engines.Fusion)
                StreamCipher = new Fusion(rounds);

            this.BlockSize = 64;

            // get iv
            if (this.Engine != Engines.DCS )
                this.IV = GetIV(KeyPath);
            else
                this.BlockSize = (DCS_BLOCK * 4);

            // dcs, chacha and salsa are stream ciphers
            if (this.Engine == Engines.DCS || this.Engine == Engines.ChaCha || this.Engine == Engines.Salsa || this.Engine == Engines.Fusion)
                return;

            this.IsParallel = Environment.ProcessorCount > 1;

            // set params from key data
            this.BlockSize = KeyHeader.GetBlockSize(KeyPath) == BlockSizes.B128 ? 16 : 32;
            this.CipherMode = KeyHeader.GetCipherType(KeyPath);
            this.PaddingMode = KeyHeader.GetPaddingType(KeyPath);

            // block size
            if (this.IV != null && this.IV.Length > 0)
                this.BlockSize = this.IV.Length;
            else
                this.CipherMode = CipherModes.ECB;

            // padding selection
            if (this.PaddingMode == PaddingModes.PKCS7)
                Padding = new PKCS7();
            else if (this.PaddingMode == PaddingModes.X923)
                Padding = new X923();
            else if (this.PaddingMode == PaddingModes.Zeros)
                Padding = new ZeroPad();

            // create engine
            if (this.Engine == Engines.RDX)
                this.BlockCipher = new RDX(this.BlockSize);
            else if (this.Engine == Engines.RSM)
                this.BlockCipher = new RSM(rounds, this.BlockSize);
            else if (this.Engine == Engines.RSX)
                this.BlockCipher = new RSX(this.BlockSize);
            else if (this.Engine == Engines.RHX)
                this.BlockCipher = new RHX(rounds, this.BlockSize);
            else if (this.Engine == Engines.SPX)
                this.BlockCipher = new SPX(rounds);
            else if (this.Engine == Engines.SHX)
                this.BlockCipher = new SHX(rounds);
            else if (this.Engine == Engines.TFX)
                this.BlockCipher = new TFX(rounds);
            else if (this.Engine == Engines.THX)
                this.BlockCipher = new THX(rounds);
            else if (this.Engine == Engines.TSM)
                this.BlockCipher = new TSM(rounds);

            // create cipher
            if (this.CipherMode == CipherModes.CBC)
                this.Mode = new CBC(this.BlockCipher);
            else if (this.CipherMode == CipherModes.CTR)
                this.Mode = new CTR(this.BlockCipher);
            else if (this.CipherMode == CipherModes.ECB)
                this.Mode = new ECB(this.BlockCipher);
        }
Example #19
0
 public Paddings(IPadding left, IPadding right)
 {
     Left  = left;
     Right = right;
 }
Example #20
0
        internal Transform(string KeyPath)
        {
            if (!File.Exists(KeyPath))
            {
                return;
            }
            // get key and algorithm
            this.ProgressInterval = PRG_INTV;
            this.KeyPath          = KeyPath;
            this.Engine           = KeyHeader.GetEngineType(KeyPath);
            this.Key = GetKey(KeyPath);
            int rounds = GetRoundsSize(KeyPath);

            // stream ciphers
            if (this.Engine == Engines.ChaCha)
            {
                StreamCipher = new ChaCha(rounds);
            }
            else if (this.Engine == Engines.DCS)
            {
                StreamCipher = new DCS();
            }
            if (this.Engine == Engines.Salsa)
            {
                StreamCipher = new Salsa20(rounds);
            }
            if (this.Engine == Engines.Fusion)
            {
                StreamCipher = new Fusion(rounds);
            }

            this.BlockSize = 64;

            // get iv
            if (this.Engine != Engines.DCS)
            {
                this.IV = GetIV(KeyPath);
            }
            else
            {
                this.BlockSize = (DCS_BLOCK * 4);
            }

            // dcs, chacha and salsa are stream ciphers
            if (this.Engine == Engines.DCS || this.Engine == Engines.ChaCha || this.Engine == Engines.Salsa || this.Engine == Engines.Fusion)
            {
                return;
            }

            this.IsParallel = Environment.ProcessorCount > 1;

            // set params from key data
            this.BlockSize   = KeyHeader.GetBlockSize(KeyPath) == BlockSizes.B128 ? 16 : 32;
            this.CipherMode  = KeyHeader.GetCipherType(KeyPath);
            this.PaddingMode = KeyHeader.GetPaddingType(KeyPath);

            // block size
            if (this.IV != null && this.IV.Length > 0)
            {
                this.BlockSize = this.IV.Length;
            }
            else
            {
                this.CipherMode = CipherModes.ECB;
            }

            // padding selection
            if (this.PaddingMode == PaddingModes.PKCS7)
            {
                Padding = new PKCS7();
            }
            else if (this.PaddingMode == PaddingModes.X923)
            {
                Padding = new X923();
            }
            else if (this.PaddingMode == PaddingModes.Zeros)
            {
                Padding = new ZeroPad();
            }

            // create engine
            if (this.Engine == Engines.RDX)
            {
                this.BlockCipher = new RDX(this.BlockSize);
            }
            else if (this.Engine == Engines.RSM)
            {
                this.BlockCipher = new RSM(rounds, this.BlockSize);
            }
            else if (this.Engine == Engines.RSX)
            {
                this.BlockCipher = new RSX(this.BlockSize);
            }
            else if (this.Engine == Engines.RHX)
            {
                this.BlockCipher = new RHX(rounds, this.BlockSize);
            }
            else if (this.Engine == Engines.SPX)
            {
                this.BlockCipher = new SPX(rounds);
            }
            else if (this.Engine == Engines.SHX)
            {
                this.BlockCipher = new SHX(rounds);
            }
            else if (this.Engine == Engines.TFX)
            {
                this.BlockCipher = new TFX(rounds);
            }
            else if (this.Engine == Engines.THX)
            {
                this.BlockCipher = new THX(rounds);
            }
            else if (this.Engine == Engines.TSM)
            {
                this.BlockCipher = new TSM(rounds);
            }

            // create cipher
            if (this.CipherMode == CipherModes.CBC)
            {
                this.Mode = new CBC(this.BlockCipher);
            }
            else if (this.CipherMode == CipherModes.CTR)
            {
                this.Mode = new CTR(this.BlockCipher);
            }
            else if (this.CipherMode == CipherModes.ECB)
            {
                this.Mode = new ECB(this.BlockCipher);
            }
        }
Example #21
0
 public static void UpdatePadding(this Button nativeButton, IPadding padding) =>
 nativeButton.UpdatePadding(padding, nativeButton.GetResource <UI.Xaml.Thickness>("ButtonPadding"));
Example #22
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);
                }
            }
        }
Example #23
0
 public static void UpdatePadding(this Button platformButton, IPadding padding) =>
 platformButton.UpdatePadding(padding, platformButton.GetResource <UI.Xaml.Thickness>("ButtonPadding"));
Example #24
0
 public ECB(ICipher cipher, IPadding padding) : base(cipher, padding)
 {
 }