Exemplo n.º 1
0
        /// <remarks>
        /// Create keying material using a two stage generator
        /// </remarks>
        private byte[] GetBlock()
        {
            // generate seed; 2x input block size per NIST sp800-90b.

            // since the generator has already been initialized, so long as the sum extraction length is less
            // than the generators maximum output length, using unique seed material on each key is worth the computational expense.
            byte[] seed = m_seedEngine.GetBytes((m_hashEngine.BlockSize * 2) - m_ctrLength);
            // rotate the counter at 32 bit intervals
            Rotate(m_ctrVector);
            // prepend the counter to the seed
            seed = ArrayUtils.Concat(m_ctrVector, seed);

            // special case for sha-2
            if (m_dgtType == Digests.SHA256 || m_dgtType == Digests.SHA512)
            {
                // hmac key size is digest hash size: rfc 2104
                byte[] key = m_seedEngine.GetBytes(m_hashEngine.DigestSize);

                // set hmac to *not* dispose of underlying digest
                using (HMAC mac = new HMAC(m_hashEngine, key, false))
                    return(mac.ComputeMac(seed));
            }
            else
            {
                // other implemented digests do not require hmac
                return(m_hashEngine.ComputeHash(seed));
            }
        }
Exemplo n.º 2
0
        private bool MacTest3(byte[] IKm)
        {
            byte[] data = new CSPPrng().GetBytes(33033);
            byte[] hash1;
            byte[] hash2;

            using (MacStream mac1 = new MacStream(new HMAC(new SHA512(), IKm)))
            {
                mac1.Initialize(new MemoryStream(data));
                mac1.IsConcurrent = false;
                hash1             = mac1.ComputeMac();
            }

            using (HMAC mac2 = new HMAC(new SHA512(), IKm))
                hash2 = mac2.ComputeMac(data);

            return(Evaluate.AreEqual(hash1, hash2));
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
0
        private void HmacDescriptionTest()
        {
            CSPPrng rng = new CSPPrng();

            byte[] data = rng.GetBytes(rng.Next(100, 400));
            byte[] key  = rng.GetBytes(64);
            HMAC   mac  = new HMAC(Digests.SHA256);

            mac.Initialize(key);
            byte[]         c1  = mac.ComputeMac(data);
            MacDescription mds = new MacDescription(64, Digests.SHA256);
            MacStream      mst = new MacStream(mds, new KeyParams(key));

            mst.Initialize(new MemoryStream(data));
            byte[] c2 = mst.ComputeMac();

            if (!Evaluate.AreEqual(c1, c2))
            {
                throw new Exception("MacStreamTest: HMAC code arrays are not equal!");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Test the MacStream 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 (MacStream sm = new MacStream(new HMAC(new SHA512(), key)))
            {
                sm.Initialize(instrm);
                code1 = sm.ComputeMac();
            }

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

            // compare the hash codes
            if (!Evaluate.AreEqual(code1, code2))
            {
                throw new Exception();
            }
        }
Exemplo n.º 6
0
        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(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 (!Evaluate.AreEqual(Out256, trnHash))
                {
                    throw new Exception("HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(trnHash));
                }
            }
            else
            {
                if (!Evaluate.AreEqual(Out256, hash))
                {
                    throw new Exception("HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(hash));
                }
            }

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

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

                if (!Evaluate.AreEqual(Out256, trnHash))
                {
                    throw new Exception("HMAC is not equal! Expected: " + HexConverter.ToString(Out256) + " Received: " + HexConverter.ToString(trnHash));
                }
            }
            else
            {
                if (!Evaluate.AreEqual(Out256, hash))
                {
                    throw new Exception("HMAC 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(Key);
            hash = hmac1.ComputeMac(Input);

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

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

            // test 4: 512 hmac
            using (HMAC hmac3 = new HMAC(new SHA512()))
            {
                hmac3.Initialize(Key);
                hash = hmac3.ComputeMac(Input);
            }

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

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