From 1543a7f263fdf0d3282dacdafb54be33e8d11da2 Mon Sep 17 00:00:00 2001 From: Rossen Georgiev Date: Tue, 31 May 2016 01:47:54 +0100 Subject: [PATCH] added hmac_sha1 shortuct function --- steam/core/crypto.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/steam/core/crypto.py b/steam/core/crypto.py index ae16ec4..886735e 100644 --- a/steam/core/crypto.py +++ b/steam/core/crypto.py @@ -53,13 +53,8 @@ def symmetric_encrypt(message, key): def symmetric_encrypt_HMAC(message, key, hmac_secret): prefix = random_bytes(3) - - hmac = HMAC(hmac_secret, SHA1(), backend) - hmac.update(prefix) - hmac.update(message) - - iv = hmac.finalize()[:13] + prefix - + hmac = hmac_sha1(hmac_secret, prefix + message) + iv = hmac[:13] + prefix return symmetric_encrypt_with_iv(message, key, iv) def symmetric_encrypt_iv(iv, key): @@ -81,11 +76,9 @@ def symmetric_decrypt_HMAC(cyphertext, key, hmac_secret): iv = symmetric_decrypt_iv(cyphertext, key) message = symmetric_decrypt_with_iv(cyphertext, key, iv) - hmac = HMAC(hmac_secret, SHA1(), backend) - hmac.update(iv[-3:]) - hmac.update(message) + hmac = hmac_sha1(hmac_secret, iv[-3:] + message) - if iv[:13] != hmac.finalize()[:13]: + if iv[:13] != hmac[:13]: raise RuntimeError("Unable to decrypt message. HMAC does not match.") return message @@ -98,6 +91,11 @@ def symmetric_decrypt_with_iv(cyphertext, key, iv): decryptor = Cipher(AES(key), CBC(iv), backend).decryptor() return unpad(decryptor.update(cyphertext[BS:]) + decryptor.finalize()) +def hmac_sha1(secret, data): + hmac = HMAC(secret, SHA1(), backend) + hmac.update(data) + return hmac.finalize() + def sha1_hash(data): sha = Hash(SHA1(), backend) sha.update(data)