コード例 #1
0
        // This function is defined as follow :
        // Func (S, i) = HMAC(S || i) | HMAC2(S || i) | ... | HMAC(iterations) (S || i)
        // where i is the block number.
        private byte[] Func()
        {
            byte[] INT_block = Utils.Int(m_block);

            m_hmacsha1.TransformBlock(m_salt, 0, m_salt.Length, null, 0);
            m_hmacsha1.TransformBlock(INT_block, 0, INT_block.Length, null, 0);
            m_hmacsha1.TransformFinalBlock(EmptyArray <Byte> .Value, 0, 0);
            byte[] temp = m_hmacsha1.HashValue;
            m_hmacsha1.Initialize();

            byte[] ret = temp;
            for (int i = 2; i <= m_iterations; i++)
            {
                m_hmacsha1.TransformBlock(temp, 0, temp.Length, null, 0);
                m_hmacsha1.TransformFinalBlock(EmptyArray <Byte> .Value, 0, 0);
                temp = m_hmacsha1.HashValue;
                for (int j = 0; j < BlockSize; j++)
                {
                    ret[j] ^= temp[j];
                }
                m_hmacsha1.Initialize();
            }

            // increment the block count.
            m_block++;
            return(ret);
        }
コード例 #2
0
ファイル: Message.cs プロジェクト: davies/Pyrolite
	/// <summary>
	/// returns the hmac of the data and the annotation chunk values (except HMAC chunk itself)
	/// </summary>
	protected byte[] hmac()
	{
		using(HMACSHA1 hmac=new HMACSHA1(Config.HMAC_KEY)) {
			hmac.TransformBlock(this.data, 0, this.data.Length, this.data, 0);
			foreach(var e in this.annotations)
			{
				if(e.Key!="HMAC")
					hmac.TransformBlock(e.Value, 0, e.Value.Length, e.Value, 0);
			}
			hmac.TransformFinalBlock(this.data, 0, 0);
			return hmac.Hash;
		}
	}
コード例 #3
0
ファイル: MessageTests.cs プロジェクト: kanzhang/Pyrolite
 public byte[] pyrohmac(byte[] data, IDictionary<string, byte[]> annotations)
 {
     using(HMACSHA1 hmac=new HMACSHA1(Config.HMAC_KEY)) {
     hmac.TransformBlock(data, 0, data.Length, data, 0);
     if(annotations!=null)
     {
         foreach(var e in annotations)
         {
             if(e.Key!="HMAC")
                 hmac.TransformBlock(e.Value, 0, e.Value.Length, e.Value, 0);
         }
     }
     hmac.TransformFinalBlock(data, 0, 0);
     return hmac.Hash;
     }
 }
コード例 #4
0
        public void GenerateSessionKey(byte[] clientSalt, byte[] serverSalt)
        {
            var hmac = new HMACSHA1(SecureRemotePassword.SessionKey);
            var wow = Encoding.ASCII.GetBytes("WoW\0");
            var wowSessionKey = new byte[0x28];

            hmac.TransformBlock(wow, 0, wow.Length, wow, 0);
            hmac.TransformBlock(clientSalt, 0, clientSalt.Length, clientSalt, 0);
            hmac.TransformFinalBlock(serverSalt, 0, serverSalt.Length);

            Buffer.BlockCopy(hmac.Hash, 0, wowSessionKey, 0, hmac.Hash.Length);

            hmac.Initialize();
            hmac.TransformBlock(wow, 0, wow.Length, wow, 0);
            hmac.TransformBlock(serverSalt, 0, serverSalt.Length, serverSalt, 0);
            hmac.TransformFinalBlock(clientSalt, 0, clientSalt.Length);

            Buffer.BlockCopy(hmac.Hash, 0, wowSessionKey, hmac.Hash.Length, hmac.Hash.Length);

            GameAccount.SessionKey = wowSessionKey.ToHexString();

            // Update SessionKey in database
            DB.Auth.Update(GameAccount);
        }
コード例 #5
0
ファイル: HMACSHA1Test.cs プロジェクト: Profit0004/mono
	public void CheckE (string testName, byte[] key, byte[] data, byte[] result) 
	{
		algo = new HMACSHA1 (key);
		byte[] copy = new byte [data.Length];
		// LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
		for (int i=0; i < data.Length - 1; i++)
			algo.TransformBlock (data, i, 1, copy, i);
		algo.TransformFinalBlock (data, data.Length - 1, 1);
		Assert.AreEqual (result, algo.Hash, testName + "e");
	}
コード例 #6
0
ファイル: CryptoHelper.cs プロジェクト: Zaharkov/SteamKit
        /// <summary>
        /// Decrypts using AES/CBC/PKCS7 with an input byte array and key, using the IV (comprised of random bytes and the HMAC-SHA1 of the random bytes and plaintext) prepended using AES/ECB/None
        /// </summary>
        public static byte[] SymmetricDecryptHMACIV( byte[] input, byte[] key, byte[] hmacSecret )
        {
            Debug.Assert( key.Length >= 16 );
            var truncatedKeyForHmac = new byte[ 16 ];
            Array.Copy( key, 0, truncatedKeyForHmac, 0, truncatedKeyForHmac.Length );

            byte[] iv;
            var plaintextData = SymmetricDecrypt( input, key, out iv );

            // validate HMAC
            byte[] hmacBytes;
            using ( var hmac = new HMACSHA1( hmacSecret ) )
            {
                hmac.TransformBlock( iv, iv.Length - 3, 3, null, 0 );
                hmac.TransformFinalBlock( plaintextData, 0, plaintextData.Length );

                hmacBytes = hmac.Hash;
            }

            if ( !hmacBytes.Take( iv.Length - 3 ).SequenceEqual( iv.Take( iv.Length - 3 ) ) )
            {
                throw new CryptographicException( string.Format( CultureInfo.InvariantCulture, "{0} was unable to decrypt packet: HMAC from server did not match computed HMAC.", nameof(NetFilterEncryption) ) );
            }

            return plaintextData;
        }
コード例 #7
0
ファイル: CryptoHelper.cs プロジェクト: Zaharkov/SteamKit
        /// <summary>
        /// Performs an encryption using AES/CBC/PKCS7 with an input byte array and key, with a IV (comprised of random bytes and the HMAC-SHA1 of the random bytes and plaintext) prepended using AES/ECB/None
        /// </summary>
        public static byte[] SymmetricEncryptWithHMACIV( byte[] input, byte[] key, byte[] hmacSecret )
        {
            // IV is HMAC-SHA1(Random(3) + Plaintext) + Random(3). (Same random values for both)
            var iv = new byte[ 16 ];
            var random = GenerateRandomBlock( 3 );
            Array.Copy( random, 0, iv, iv.Length - random.Length, random.Length );

            using ( var hmac = new HMACSHA1( hmacSecret ) )
            {
                hmac.TransformBlock( random, 0, random.Length, null, 0 );
                hmac.TransformFinalBlock( input, 0, input.Length );
                Array.Copy( hmac.Hash, iv, iv.Length - random.Length );
            }
            
            return SymmetricEncryptWithIV( input, key, iv );
        }
コード例 #8
0
ファイル: Message.cs プロジェクト: justinuang/Pyrolite
 /// <summary>
 /// returns the hmac of the data and the annotation chunk values (except HMAC chunk itself)
 /// </summary>
 public byte[] hmac(byte[] key)
 {
     using(HMACSHA1 hmac=new HMACSHA1(key)) {
     hmac.TransformBlock(this.data, 0, this.data.Length, this.data, 0);
     foreach(var e in this.annotations.OrderBy(a=>a.Key))
     {
         if(e.Key!="HMAC")
             hmac.TransformBlock(e.Value, 0, e.Value.Length, e.Value, 0);
     }
     hmac.TransformFinalBlock(this.data, 0, 0);
     return hmac.Hash;
     }
 }
コード例 #9
0
ファイル: SimpleCaptcha.cs プロジェクト: kazuki/p2pncs
 byte[] ComputeToken(byte[] hash, byte[] ascii_text)
 {
     using (HMAC hmac = new HMACSHA1 (_hmac_key, true)) {
         hmac.Initialize ();
         hmac.TransformBlock (hash, 0, hash.Length, null, 0);
         hmac.TransformBlock (ascii_text, 0, ascii_text.Length, null, 0);
         hmac.TransformFinalBlock (_salt, 0, _salt.Length);
         return hmac.Hash;
     }
 }