예제 #1
0
 private void Dispose(bool Disposing)
 {
     // note: digest is disposed of by hmac
     if (!_isDisposed && Disposing)
     {
         try
         {
             if (_shaHmac != null)
             {
                 _shaHmac.Dispose();
                 _shaHmac = null;
             }
         }
         finally
         {
             _isDisposed = true;
         }
     }
 }
예제 #2
0
 /// <summary>
 /// Initialize the class
 /// <para>When using this constructor, you must call <see cref="Initialize(KeyParams)"/> before processing.</para>
 /// </summary>
 /// 
 /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called</param>
 public SHA512HMAC(bool DisposeEngine = true)
 {
     _shaDigest = new SHA512();
     _shaHmac = new HMAC(_shaDigest, DisposeEngine);
 }
예제 #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="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 Key is used</exception>
        public SHA512HMAC(byte[] Key, bool DisposeEngine = true)
        {
            if (Key == null)
                throw new CryptoMacException("SHA512HMAC:Ctor", "Key can not be null!", new ArgumentNullException());

            _shaDigest = new SHA512();
            _shaHmac = new HMAC(_shaDigest, Key, DisposeEngine);
            _isInitialized = true;
        }
예제 #4
0
 /// <summary>
 /// Initialize the class
 /// <para>When using this constructor, you must call <see cref="Initialize(KeyParams)"/> before processing.</para>
 /// </summary>
 ///
 /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called</param>
 public SHA256HMAC(bool DisposeEngine = true)
 {
     _shaDigest = new SHA256();
     _shaHmac   = new HMAC(_shaDigest, DisposeEngine);
 }
예제 #5
0
        /// <summary>
        /// Test the StreamMac class implementation
        /// <para>Throws an Exception on failure</</para>
        /// </summary>
        public static void StreamMacTest()
        {
            byte[] data;
            byte[] key;
            MemoryStream instrm;
            MemoryStream outstrm = new MemoryStream();

            using (KeyGenerator kg = new KeyGenerator())
            {
                data = kg.GetBytes(512);
                key = kg.GetBytes(64);
            }

            // data to digest
            instrm = new MemoryStream(data);
            byte[] code1;
            byte[] code2;

            using (StreamMac sm = new StreamMac(new SHA512HMAC(key)))
            {
                sm.Initialize(instrm);
                code1 = sm.ComputeMac();
            }

            using (HMAC hm = new HMAC(new SHA512()))
            {
                hm.Initialize(new KeyParams(key));
                code2 = hm.ComputeMac(data);
            }

            // compare the hash codes
            if (!Compare.AreEqual(code1, code2))
                throw new Exception();
        }
예제 #6
0
        /// <remarks>
        /// Create keying material using a two stage generator
        /// </remarks>
        private byte[] GetBlock()
        {
            // generate seed; 2x input block per NIST sp800-90b
            byte[] seed = _seedEngine.GetBytes((_hashEngine.BlockSize * 2));

            if (_hashEngine.GetType().Equals(typeof(SHA512)) || _hashEngine.GetType().Equals(typeof(SHA256)))
            {
                // hmac key size is digest hash size: rfc 2104
                byte[] key = _seedEngine.GetBytes(_hashEngine.DigestSize);

                // set hmac to *not* dispose of underlying digest
                using (HMAC mac = new HMAC(_hashEngine, key, false))
                    return mac.ComputeMac(seed);
            }
            else
            {
                // other implemented digests do not require hmac
                return _hashEngine.ComputeHash(seed);
            }
        }
예제 #7
0
        private void HMACTest(IDigest Digest, String[] Expected, byte[] TruncExpected)
        {
            HMAC mac = new HMAC(Digest);
            byte[] macV2 = new byte[mac.DigestSize];

            for (int i = 0; i != _macKeys.Length; i++)
            {
                mac.Initialize(new KeyParams(_macKeys[i]));

                byte[] mData = HexConverter.Decode(_macData[i]);
                byte[] macV = new byte[mac.DigestSize];

                mac.BlockUpdate(mData, 0, mData.Length);
                mac.DoFinal(macV, 0);

                if (Compare.AreEqual(HexConverter.Decode(Expected[i]), macV) == false)
                    throw new Exception("Keccak HMAC: Expected hash is not equal! Expected: " + Expected[i] + " Received: " + HexConverter.ToString(macV));
            }

            // test truncated keys
            mac = new HMAC(Digest);
            mac.Initialize(new KeyParams(_truncKey));
            mac.BlockUpdate(_truncData, 0, _truncData.Length);
            mac.DoFinal(macV2, 0);

            for (int i = 0; i != TruncExpected.Length; i++)
            {
                if (macV2[i] != TruncExpected[i])
                    throw new Exception("Keccak HMAC: Expected hash is not equal!");
            }
        }
예제 #8
0
파일: HMacTest.cs 프로젝트: modulexcite/CEX
        private void MacTest(byte[] Key, byte[] Input, byte[] Out256, byte[] Out512)
        {
            byte[] hash = new byte[32];
            byte[] trnHash;

            // test 1: HMAC interface
            HMAC hmac1 = new HMAC(new SHA256());
            hmac1.Initialize(new KeyParams(Key));
            hash = hmac1.ComputeMac(Input);

            // truncated output, test case #5
            if (Out256.Length != 32)
            {
                trnHash = new byte[Out256.Length];
                Buffer.BlockCopy(hash, 0, trnHash, 0, Out256.Length);

                if (!Compare.AreEqual(Out256, trnHash))
                    throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(trnHash));
            }
            else
            {
                if (!Compare.AreEqual(Out256, hash))
                    throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(hash));
            }

            // test 2: 256 hmac
            using (SHA256HMAC hmac2 = new SHA256HMAC())
            {
                hmac2.Initialize(new KeyParams(Key));
                hash = hmac2.ComputeMac(Input);
            }

            if (Out256.Length != 32)
            {
                trnHash = new byte[Out256.Length];
                Buffer.BlockCopy(hash, 0, trnHash, 0, Out256.Length);

                if (!Compare.AreEqual(Out256, trnHash))
                    throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(trnHash));
            }
            else
            {
                if (!Compare.AreEqual(Out256, hash))
                    throw new Exception("SHA256HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(hash));
            }

            hash = new byte[32];

            // test 3: HMAC interface
            hmac1 = new HMAC(new SHA512());
            hmac1.Initialize(new KeyParams(Key));
            hash = hmac1.ComputeMac(Input);

            if (Out512.Length != 64)
            {
                trnHash = new byte[Out512.Length];
                Buffer.BlockCopy(hash, 0, trnHash, 0, Out512.Length);

                if (!Compare.AreEqual(Out512, trnHash))
                    throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(trnHash));
            }
            else
            {
                if (!Compare.AreEqual(Out512, hash))
                    throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(hash));
            }

            // test 4: 256 hmac
            using (SHA512HMAC hmac3 = new SHA512HMAC())
            {
                hmac3.Initialize(new KeyParams(Key));
                hash = hmac3.ComputeMac(Input);
            }

            if (Out512.Length != 64)
            {
                trnHash = new byte[Out512.Length];
                Buffer.BlockCopy(hash, 0, trnHash, 0, Out512.Length);

                if (!Compare.AreEqual(Out512, trnHash))
                    throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(trnHash));
            }
            else
            {
                if (!Compare.AreEqual(Out512, hash))
                    throw new Exception("SHA512HMAC is not equal! Expected: " + HexConverter.ToString(Out512) + " Received: " + HexConverter.ToString(hash));
            }
        }