Skip to content

Commit b31354f

Browse files
replace cryptography with pycryptodomex
1 parent 43f52a3 commit b31354f

File tree

9 files changed

+48
-77
lines changed

9 files changed

+48
-77
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ Key features
1616
* `WebAuth <http://steam.readthedocs.io/en/latest/api/steam.webauth.html>`_ - authentication for access to ``store.steampowered.com`` and ``steamcommunity.com``
1717

1818
Checkout the `User guide <http://steam.readthedocs.io/en/latest/user_guide.html>`_ for examples,
19-
or the `API Reference <http://steam.readthedocs.io/en/latest/api/index.html>`_ for details.
19+
or the `API Reference <http://steam.readthedocs.io/en/latest/api/steam.html>`_ for details.
2020

2121
For questions, issues or general curiosity visit the repo at `https://github.com/ValvePython/steam <https://github.com/ValvePython/steam>`_.
2222

2323
Quick install
2424
-------------
2525

26-
For details on require system packages, see `Full Installation <http://steam.readthedocs.io/en/latest/install.html>`_.
26+
For system specific details, see `Install Details <http://steam.readthedocs.io/en/latest/install.html>`_.
2727

2828
Install latest version from PYPI
2929

Vagrantfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ Vagrant.configure("2") do |config|
1515
# box.vm.synced_folder "../csgo-python/csgo/", "/home/vagrant/csgo"
1616

1717
box.vm.provision "shell", inline: <<-SHELL
18+
set -x
1819
apt-get update
19-
apt-get -y install build-essential libssl-dev libffi-dev python-dev
2020
apt-get -y install python-pip python-virtualenv
2121
SHELL
2222

2323
box.vm.provision "shell", privileged: false, inline: <<-SHELL
24+
set -x
2425
virtualenv -p python2 venv2
2526
source venv2/bin/activate
2627
pip install -r /vagrant/requirements.txt ipython

docs/install.rst

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@ Steps assume that ``python`` and ``pip`` are already installed.
1414
in order to keep you system packages untouched.
1515

1616

17-
Ubuntu/Debian
18-
^^^^^^^^^^^^^
19-
20-
Replace ``python-dev`` with ``python3-dev`` for Python 3.
21-
22-
.. code-block:: console
23-
24-
$ sudo apt-get install build-essential libssl-dev libffi-dev python-dev
25-
26-
RHEL-based
27-
^^^^^^^^^^
28-
29-
.. code-block:: console
30-
31-
$ sudo yum install gcc libffi-devel python-devel openssl-devel
32-
33-
3417
Windows
3518
-------
3619

@@ -41,13 +24,7 @@ Cygwin
4124

4225
2. During the setup select these additional packages
4326
- ``python3``
44-
- ``python3-devel``
4527
- ``python3-setuptools``
46-
- ``gcc-core``
47-
- ``gcc-g++``
48-
- ``libffi6``
49-
- ``libffi-devel``
50-
- ``openssl-devel``
5128

5229
4. Install pip
5330
- Open cygwin terminal

docs/intro.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ Key features
2020
* :doc:`WebAPI <api/steam.webapi>` - simple API for Steam's Web API with automatic population of interfaces
2121
* :doc:`WebAuth <api/steam.webauth>` - authentication for access to ``store.steampowered.com`` and ``steamcommunity.com``
2222

23-
Checkout the :doc:`user_guide` for examples, or the :doc:`api/index` for details.
23+
Checkout the :doc:`user_guide` for examples, or the :doc:`api/steam` for details.
2424

2525
For questions, issues, or general curiosity, visit the repo at `https://github.com/ValvePython/steam <https://github.com/ValvePython/steam>`_.
2626

2727
Quick install
2828
=============
2929

30-
For details on require system packages, see :doc:`install`.
30+
For system specific details, see :doc:`install`.
3131

3232
Install latest version from PYPI::
3333

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
six>=1.10.0
2-
cryptography>=1.3
2+
pycryptodomex>=3.7.0
33
requests>=2.9.1
44
vdf>=2.0
55
gevent>=1.2.0

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
install_requires = [
1818
'six>=1.10',
19-
'cryptography>=1.3',
19+
'pycryptodomex>=3.7.0',
2020
'requests>=2.9.1',
2121
'vdf>=2.0',
2222
'gevent>=1.2.0',
@@ -50,6 +50,7 @@
5050
'Programming Language :: Python :: 3.4',
5151
'Programming Language :: Python :: 3.5',
5252
'Programming Language :: Python :: 3.6',
53+
'Programming Language :: Python :: Implementation :: PyPy',
5354
],
5455
keywords='valve steam steamid api webapi steamcommunity',
5556
packages=['steam'] + ['steam.'+x for x in find_packages(where='steam')],

steam/core/crypto.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55
from os import urandom as random_bytes
66
from struct import pack
77
from base64 import b64decode
8-
from cryptography.hazmat.primitives.hmac import HMAC
9-
from cryptography.hazmat.primitives.hashes import Hash, SHA1
10-
from cryptography.hazmat.primitives.asymmetric.padding import PSS, OAEP, MGF1
11-
from cryptography.hazmat.primitives.ciphers import Cipher
12-
from cryptography.hazmat.primitives.ciphers.algorithms import AES
13-
from cryptography.hazmat.primitives.ciphers.modes import CBC, ECB
14-
import cryptography.hazmat.backends
15-
backend = cryptography.hazmat.backends.default_backend()
8+
9+
from Cryptodome.Hash import SHA1, HMAC
10+
from Cryptodome.PublicKey.RSA import import_key as rsa_import_key, construct as rsa_construct
11+
from Cryptodome.Cipher import PKCS1_OAEP, PKCS1_v1_5
12+
from Cryptodome.Cipher import AES as AES
1613

1714

1815
class UniverseKey(object):
1916
"""Public keys for Universes"""
2017

21-
Public = backend.load_der_public_key(b64decode("""
18+
Public = rsa_import_key(b64decode("""
2219
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDf7BrWLBBmLBc1OhSwfFkRf53T
2320
2Ct64+AVzRkeRuh7h3SiGEYxqQMUeYKO6UWiSRKpI2hzic9pobFhRr3Bvr/WARvY
2421
gdTckPv+T1JzZsuVcNfFjrocejN1oWI0Rrtgt4Bo+hOneoo3S57G9F1fOpn5nsQ6
@@ -42,9 +39,9 @@ def generate_session_key(hmac_secret=b''):
4239
:rtype: :class:`tuple`
4340
"""
4441
session_key = random_bytes(32)
45-
encrypted_session_key = UniverseKey.Public.encrypt(session_key + hmac_secret,
46-
OAEP(MGF1(SHA1()), SHA1(), None)
47-
)
42+
encrypted_session_key = PKCS1_OAEP.new(UniverseKey.Public, SHA1)\
43+
.encrypt(session_key + hmac_secret)
44+
4845
return (session_key, encrypted_session_key)
4946

5047
def symmetric_encrypt(message, key):
@@ -58,13 +55,11 @@ def symmetric_encrypt_HMAC(message, key, hmac_secret):
5855
return symmetric_encrypt_with_iv(message, key, iv)
5956

6057
def symmetric_encrypt_iv(iv, key):
61-
encryptor = Cipher(AES(key), ECB(), backend).encryptor()
62-
return encryptor.update(iv) + encryptor.finalize()
58+
return AES.new(key, AES.MODE_ECB).encrypt(iv)
6359

6460
def symmetric_encrypt_with_iv(message, key, iv):
6561
encrypted_iv = symmetric_encrypt_iv(iv, key)
66-
encryptor = Cipher(AES(key), CBC(iv), backend).encryptor()
67-
cyphertext = encryptor.update(pad(message)) + encryptor.finalize()
62+
cyphertext = AES.new(key, AES.MODE_CBC, iv).encrypt(pad(message))
6863
return encrypted_iv + cyphertext
6964

7065
def symmetric_decrypt(cyphertext, key):
@@ -84,19 +79,19 @@ def symmetric_decrypt_HMAC(cyphertext, key, hmac_secret):
8479
return message
8580

8681
def symmetric_decrypt_iv(cyphertext, key):
87-
decryptor = Cipher(AES(key), ECB(), backend).decryptor()
88-
return decryptor.update(cyphertext[:BS]) + decryptor.finalize()
82+
return AES.new(key, AES.MODE_ECB).decrypt(cyphertext[:BS])
8983

9084
def symmetric_decrypt_with_iv(cyphertext, key, iv):
91-
decryptor = Cipher(AES(key), CBC(iv), backend).decryptor()
92-
return unpad(decryptor.update(cyphertext[BS:]) + decryptor.finalize())
85+
return unpad(AES.new(key, AES.MODE_CBC, iv).decrypt(cyphertext[BS:]))
9386

9487
def hmac_sha1(secret, data):
95-
hmac = HMAC(secret, SHA1(), backend)
96-
hmac.update(data)
97-
return hmac.finalize()
88+
return HMAC.new(secret, data, SHA1).digest()
9889

9990
def sha1_hash(data):
100-
sha = Hash(SHA1(), backend)
101-
sha.update(data)
102-
return sha.finalize()
91+
return SHA1.new(data).digest()
92+
93+
def rsa_publickey(mod, exp):
94+
return rsa_construct((mod, exp))
95+
96+
def pkcs1v15_encrypt(key, message):
97+
return PKCS1_v1_5.new(key).encrypt(message)

steam/webauth.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@
6060
from base64 import b64encode
6161
import requests
6262

63-
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers
64-
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
65-
from steam.core.crypto import backend
66-
6763
from steam import SteamID, webapi
6864
from steam.util.web import make_requests_session, generate_session_id
65+
from steam.core.crypto import rsa_publickey, pkcs1v15_encrypt
6966

7067
if sys.version_info < (3,):
7168
intBase = long
@@ -108,12 +105,12 @@ def get_rsa_key(self, username):
108105
"""
109106
try:
110107
resp = self.session.post('https://steamcommunity.com/login/getrsakey/',
111-
timeout=15,
112-
data={
113-
'username': username,
114-
'donotchache': int(time() * 1000),
115-
},
116-
).json()
108+
timeout=15,
109+
data={
110+
'username': username,
111+
'donotchache': int(time() * 1000),
112+
},
113+
).json()
117114
except requests.exceptions.RequestException as e:
118115
raise HTTPError(str(e))
119116

@@ -123,17 +120,15 @@ def _load_key(self):
123120
if not self.key:
124121
resp = self.get_rsa_key(self.username)
125122

126-
nums = RSAPublicNumbers(intBase(resp['publickey_exp'], 16),
127-
intBase(resp['publickey_mod'], 16),
128-
)
129-
130-
self.key = backend.load_rsa_public_numbers(nums)
123+
self.key = rsa_publickey(intBase(resp['publickey_mod'], 16),
124+
intBase(resp['publickey_exp'], 16),
125+
)
131126
self.timestamp = resp['timestamp']
132127

133128
def _send_login(self, captcha='', email_code='', twofactor_code=''):
134129
data = {
135-
'username' : self.username,
136-
"password": b64encode(self.key.encrypt(self.password.encode('ascii'), PKCS1v15())),
130+
'username': self.username,
131+
"password": b64encode(pkcs1v15_encrypt(self.key, self.password.encode('ascii'))),
137132
"emailauth": email_code,
138133
"emailsteamid": str(self.steam_id) if email_code else '',
139134
"twofactorcode": twofactor_code,

tests/test_core_crypto.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ def setUp(self):
4545
# self.assertEqual(key, expected_key)
4646
# self.assertEqual(ekey, expected_ekey)
4747

48-
def test_encryption(self):
48+
def test_encryption_legacy(self):
4949
message = b'My secret message'
5050
key = b'9' * 32
51-
hmac = b'3' * 16
5251

53-
# legacy
5452
cyphertext = crypto.symmetric_encrypt(message, key)
5553
dmessage = crypto.symmetric_decrypt(cyphertext, key)
5654

5755
self.assertEqual(message, dmessage)
5856

59-
# with HMAC
57+
def test_encryption_hmac(self):
58+
message = b'My secret message'
59+
key = b'9' * 32
60+
hmac = b'3' * 16
61+
6062
cyphertext = crypto.symmetric_encrypt_HMAC(message, key, hmac)
6163
dmessage = crypto.symmetric_decrypt_HMAC(cyphertext, key, hmac)
6264

0 commit comments

Comments
 (0)