Пример #1
0
 private void Dispose(bool Disposing)
 {
     if (!_isDisposed && Disposing)
     {
         try
         {
             if (_digestMac != null && _disposeEngine)
             {
                 _digestMac.Dispose();
                 _digestMac = null;
             }
             if (_macKey != null)
             {
                 _macKey.Dispose();
                 _macKey = null;
             }
             if (_Salt != null)
             {
                 Array.Clear(_Salt, 0, _Salt.Length);
                 _Salt = null;
             }
         }
         finally
         {
             _isDisposed = true;
         }
     }
 }
Пример #2
0
 /// <summary>
 /// Reset all struct members
 /// </summary>
 public void Reset()
 {
     Key.Dispose();
     SessionParams.Reset();
     LifeSpan    = 0;
     Instruction = 0;
     OptionsFlag = 0;
 }
Пример #3
0
        /// <summary>
        /// Initialize the class and working variables.
        /// <para>When this constructor is used, <see cref="Initialize(KeyParams)"/> is called automatically.</para>
        /// </summary>
        ///
        /// <param name="Digest">Message Digest instance</param>
        /// <param name="Key">HMAC Key; passed to HMAC Initialize() through constructor</param>
        /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called</param>
        ///
        /// <exception cref="CryptoMacException">Thrown if a null digest is used</exception>
        public HMAC(IDigest Digest, byte[] Key, bool DisposeEngine = true)
        {
            if (Digest == null)
            {
                throw new CryptoMacException("HMAC:Ctor", "Digest can not be null!", new ArgumentNullException());
            }

            _disposeEngine = DisposeEngine;
            _msgDigest     = Digest;
            _digestSize    = Digest.DigestSize;
            _blockSize     = Digest.BlockSize;
            _inputPad      = new byte[_blockSize];
            _outputPad     = new byte[_blockSize];

            KeyParams keyParam = new KeyParams(Key);

            Initialize(keyParam);
            keyParam.Dispose();
        }
Пример #4
0
        private void ParallelTest()
        {
            byte[]    data;
            byte[]    dec1;
            byte[]    dec2;
            byte[]    enc1;
            byte[]    enc2;
            int       blockSize;
            KeyParams keyParam = new KeyParams(new byte[32], new byte[16]);

            // CTR mode
            using (CTR cipher = new CTR(new RHX()))
            {
                data = GetBytes(1036);

                // how to calculate an ideal block size
                int plen = (data.Length / cipher.ParallelMinimumSize) * cipher.ParallelMinimumSize;
                // you can factor it up or down or use a default
                if (plen > cipher.ParallelMaximumSize)
                {
                    plen = 1024;
                }

                // set parallel block size
                cipher.ParallelBlockSize = plen;

                // parallel 1
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                enc1 = Transform2(cipher, data, blockSize);

                // linear 1
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                enc2 = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CTR: Encrypted output is not equal!");
                }

                // encrypt //
                // parallel 2
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CTR: Encrypted output is not equal!");
                }

                // linear 2
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                enc2 = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CTR: Encrypted output is not equal!");
                }

                // decrypt //
                // parallel 1
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                // parallel 2
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec2 = Transform2(cipher, enc2, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CTR: Decrypted output is not equal!");
                }

                // linear 1
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CTR: Decrypted output is not equal!");
                }

                // linear 2
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform2(cipher, enc2, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CTR: Decrypted output is not equal!");
                }
            }

            if (Evaluate.AreEqual(data, dec1) == false)
            {
                throw new Exception("Parallel CTR: Decrypted output is not equal!");
            }
            if (Evaluate.AreEqual(data, dec2) == false)
            {
                throw new Exception("Parallel CTR: Decrypted output is not equal!");
            }

            OnProgress(new TestEventArgs("Passed Parallel CTR encryption and decryption tests.."));

            // CBC mode
            using (CBC cipher = new CBC(new RHX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

                // t1: encrypt only in normal mode for cbc
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc1      = Transform1(cipher, data, blockSize);

                // t2
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc2      = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CBC: Decrypted output is not equal!");
                }

                // decrypt //
                // t1 parallel
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                // t1 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform1(cipher, enc2, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CBC: Decrypted output is not equal!");
                }

                // t2 parallel
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform2(cipher, enc2, blockSize);

                // t2 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform2(cipher, enc1, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CBC: Decrypted output is not equal!");
                }
            }

            if (Evaluate.AreEqual(dec1, data) == false)
            {
                throw new Exception("Parallel CBC: Decrypted output is not equal!");
            }
            if (Evaluate.AreEqual(dec2, data) == false)
            {
                throw new Exception("Parallel CBC: Decrypted output is not equal!");
            }

            OnProgress(new TestEventArgs("Passed Parallel CBC decryption tests.."));

            // CFB mode
            using (CFB cipher = new CFB(new RHX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

                // t1: encrypt only in normal mode for cfb
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc1      = Transform1(cipher, data, blockSize);
                // t2
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc2      = Transform2(cipher, data, blockSize);

                if (Evaluate.AreEqual(enc1, enc2) == false)
                {
                    throw new Exception("Parallel CFB: Decrypted output is not equal!");
                }

                // decrypt //
                // t1 parallel
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                // t1 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform1(cipher, enc2, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CFB: Decrypted output is not equal!");
                }

                // t2 parallel
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize         = cipher.ParallelBlockSize;
                dec1 = Transform2(cipher, enc2, blockSize);

                // t2 linear
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize         = cipher.BlockSize;
                dec2 = Transform2(cipher, enc1, blockSize);

                if (Evaluate.AreEqual(dec1, dec2) == false)
                {
                    throw new Exception("Parallel CFB: Decrypted output is not equal!");
                }
            }

            if (Evaluate.AreEqual(data, dec1) == false)
            {
                throw new Exception("Parallel CFB: Decrypted output is not equal!");
            }
            if (Evaluate.AreEqual(data, dec2) == false)
            {
                throw new Exception("Parallel CFB: Decrypted output is not equal!");
            }

            OnProgress(new TestEventArgs("Passed Parallel CFB decryption tests.."));

            // dispose container
            keyParam.Dispose();
        }