Browse Source

made crypto module py3 compatible

pull/18/merge
Rossen Georgiev 9 years ago
parent
commit
85477617f9
  1. 12
      steam/core/crypto.py
  2. 37
      tests/test_core_crypto.py

12
steam/core/crypto.py

@ -1,3 +1,5 @@
import sys
from struct import pack
from base64 import b64decode from base64 import b64decode
from Crypto import Random from Crypto import Random
from Crypto.Cipher import PKCS1_OAEP, AES from Crypto.Cipher import PKCS1_OAEP, AES
@ -12,11 +14,15 @@ gdTckPv+T1JzZsuVcNfFjrocejN1oWI0Rrtgt4Bo+hOneoo3S57G9F1fOpn5nsQ6
""" """
BS = AES.block_size BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) pad = lambda s: s + (BS - len(s) % BS) * pack('B', BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1])]
if sys.version_info < (3,):
unpad = lambda s: s[0:-ord(s[-1])]
else:
unpad = lambda s: s[0:-s[-1]]
def generate_session_key(hmac_secret=''):
def generate_session_key(hmac_secret=b''):
session_key = Random.new().read(32) session_key = Random.new().read(32)
cipher = PKCS1_OAEP.new(RSA.importKey(b64decode(public_key))) cipher = PKCS1_OAEP.new(RSA.importKey(b64decode(public_key)))
encrypted_session_key = cipher.encrypt(session_key + hmac_secret) encrypted_session_key = cipher.encrypt(session_key + hmac_secret)

37
tests/test_core_crypto.py

@ -1,5 +1,6 @@
import unittest import unittest
import mock import mock
from binascii import hexlify
from steam.core import crypto from steam.core import crypto
@ -8,7 +9,7 @@ class crypto_testcase(unittest.TestCase):
def setUp(self): def setUp(self):
class NotRandom: class NotRandom:
def read(self, n): def read(self, n):
return '1' * n return b'1' * n
def fakeNew(): def fakeNew():
return NotRandom() return NotRandom()
@ -20,38 +21,38 @@ class crypto_testcase(unittest.TestCase):
crypto.Random.new = self._oldnew crypto.Random.new = self._oldnew
def test_keygen(self): def test_keygen(self):
expected_key = '1' * 32 expected_key = b'1' * 32
expected_ekey = ('82a5d4d6de38e443ed3e6f0a1701a2c47bc98e0860e7883638ea5263a1744d02' expected_ekey = (b'82a5d4d6de38e443ed3e6f0a1701a2c47bc98e0860e7883638ea5263a1744d02'
'f733f09bc6b0f9b2a371bbb79b639208521f88658aab38c23e181d39a58ae39e' b'f733f09bc6b0f9b2a371bbb79b639208521f88658aab38c23e181d39a58ae39e'
'c4e207fba822d523028d3c04e812abdc2247aa8d8e6e4a89c7a65671c5bcb329' b'c4e207fba822d523028d3c04e812abdc2247aa8d8e6e4a89c7a65671c5bcb329'
'51c6d721ccf57cc2920d6ff3b69bfb2c611b1275badcd3e37fe024c9a25bf4b0' b'51c6d721ccf57cc2920d6ff3b69bfb2c611b1275badcd3e37fe024c9a25bf4b0'
) )
key, ekey = crypto.generate_session_key() key, ekey = crypto.generate_session_key()
ekey = ekey.encode('hex') ekey = hexlify(ekey)
self.assertEqual(key, expected_key) self.assertEqual(key, expected_key)
self.assertEqual(ekey, expected_ekey) self.assertEqual(ekey, expected_ekey)
def test_keygen_with_challenge(self): def test_keygen_with_challenge(self):
expected_key = '1' * 32 expected_key = b'1' * 32
expected_ekey = ('d710c55122f9bf772ec9c0f21d75c05055764d5445902577340029b4707e1725' expected_ekey = (b'd710c55122f9bf772ec9c0f21d75c05055764d5445902577340029b4707e1725'
'd61bec77f41b17faed6577d08c812cef76dca8b0b0b2329e1f33ea4cfa31f1e6' b'd61bec77f41b17faed6577d08c812cef76dca8b0b0b2329e1f33ea4cfa31f1e6'
'0babc859c55b6ac94497b5dc9b0bc89629290dc038274af4377771e088e92887' b'0babc859c55b6ac94497b5dc9b0bc89629290dc038274af4377771e088e92887'
'30d3906f6b698fd113ba36e3d28a5e1ce0283b27a1adda538df5dc5b179cf84f' b'30d3906f6b698fd113ba36e3d28a5e1ce0283b27a1adda538df5dc5b179cf84f'
) )
key, ekey = crypto.generate_session_key('5'*16) key, ekey = crypto.generate_session_key(b'5'*16)
ekey = ekey.encode('hex') ekey = hexlify(ekey)
self.assertEqual(key, expected_key) self.assertEqual(key, expected_key)
self.assertEqual(ekey, expected_ekey) self.assertEqual(ekey, expected_ekey)
def test_encryption(self): def test_encryption(self):
message = "My secret message" message = b'My secret message'
key = '9' * 32 key = b'9' * 32
hmac = '3' * 16 hmac = b'3' * 16
# legacy # legacy
cyphertext = crypto.symmetric_encrypt(message, key) cyphertext = crypto.symmetric_encrypt(message, key)
@ -67,6 +68,6 @@ class crypto_testcase(unittest.TestCase):
# failing HMAC check # failing HMAC check
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
crypto.symmetric_decrypt_HMAC(cyphertext, key, '4'*16) crypto.symmetric_decrypt_HMAC(cyphertext, key, b'4'*16)

Loading…
Cancel
Save