diff --git a/lbrynet/tests/functional/test_misc.py b/lbrynet/tests/functional/test_misc.py index e806da5c2..34e0d80c9 100644 --- a/lbrynet/tests/functional/test_misc.py +++ b/lbrynet/tests/functional/test_misc.py @@ -7,8 +7,7 @@ import sys import random import unittest -from Crypto import Random -from Crypto.Hash import MD5 +from hashlib import md5 from lbrynet import conf from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager from lbrynet.core.Session import Session @@ -98,9 +97,6 @@ class LbryUploader(object): from twisted.internet import reactor self.reactor = reactor logging.debug("Starting the uploader") - Random.atfork() - r = random.Random() - r.seed("start_lbry_uploader") wallet = FakeWallet() peer_manager = PeerManager() peer_finder = FakePeerFinder(5553, peer_manager, 1) @@ -191,10 +187,6 @@ def start_lbry_reuploader(sd_hash, kill_event, dead_event, logging.debug("Starting the uploader") - Random.atfork() - - r = random.Random() - r.seed("start_lbry_reuploader") wallet = FakeWallet() peer_port = 5553 + n @@ -297,7 +289,6 @@ def start_blob_uploader(blob_hash_queue, kill_event, dead_event, slow, is_genero logging.debug("Starting the uploader") - Random.atfork() wallet = FakeWallet() peer_manager = PeerManager() @@ -515,7 +506,7 @@ class TestTransfer(TestCase): def check_md5_sum(): f = open(os.path.join(db_dir, 'test_file')) - hashsum = MD5.new() + hashsum = md5() hashsum.update(f.read()) self.assertEqual(hashsum.hexdigest(), "4ca2aafb4101c1e42235aad24fbb83be") @@ -688,7 +679,7 @@ class TestTransfer(TestCase): def check_md5_sum(): f = open(os.path.join(db_dir, 'test_file')) - hashsum = MD5.new() + hashsum = md5() hashsum.update(f.read()) self.assertEqual(hashsum.hexdigest(), "4ca2aafb4101c1e42235aad24fbb83be") @@ -811,7 +802,7 @@ class TestTransfer(TestCase): def check_md5_sum(): f = open('test_file') - hashsum = MD5.new() + hashsum = md5() hashsum.update(f.read()) self.assertEqual(hashsum.hexdigest(), "e5941d615f53312fd66638239c1f90d5") diff --git a/lbrynet/tests/functional/test_streamify.py b/lbrynet/tests/functional/test_streamify.py index c84630272..cda06758b 100644 --- a/lbrynet/tests/functional/test_streamify.py +++ b/lbrynet/tests/functional/test_streamify.py @@ -2,7 +2,7 @@ import os import shutil import tempfile -from Crypto.Hash import MD5 +from hashlib import md5 from twisted.trial.unittest import TestCase from twisted.internet import defer, threads @@ -127,7 +127,7 @@ class TestStreamify(TestCase): self.assertTrue(lbry_file.sd_hash, sd_hash) yield lbry_file.start() f = open('test_file') - hashsum = MD5.new() + hashsum = md5() hashsum.update(f.read()) self.assertEqual(hashsum.hexdigest(), "68959747edc73df45e45db6379dd7b3b") diff --git a/lbrynet/tests/mocks.py b/lbrynet/tests/mocks.py index d2bce3730..7de4927c7 100644 --- a/lbrynet/tests/mocks.py +++ b/lbrynet/tests/mocks.py @@ -1,7 +1,10 @@ +import base64 import struct import io -from Crypto.PublicKey import RSA +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives import serialization from twisted.internet import defer, error from twisted.python.failure import Failure @@ -15,6 +18,12 @@ from lbrynet import conf from util import debug_kademlia_packet KB = 2**10 +PUBLIC_EXPOENT = 65537 # http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html + + +def decode_rsa_key(pem_key): + decoded = base64.b64decode(''.join(pem_key.splitlines()[1:-1])) + return serialization.load_der_public_key(decoded, default_backend()) class FakeLBRYFile(object): @@ -137,9 +146,9 @@ class PointTraderKeyQueryHandler(object): if self.query_identifiers[0] in queries: new_encoded_pub_key = queries[self.query_identifiers[0]] try: - RSA.importKey(new_encoded_pub_key) + decode_rsa_key(new_encoded_pub_key) except (ValueError, TypeError, IndexError): - return defer.fail(Failure(ValueError("Client sent an invalid public key"))) + return defer.fail(Failure(ValueError("Client sent an invalid public key: {}".format(new_encoded_pub_key)))) self.public_key = new_encoded_pub_key self.wallet.set_public_key_for_peer(self.peer, self.public_key) fields = {'public_key': self.wallet.encoded_public_key} @@ -152,8 +161,10 @@ class PointTraderKeyQueryHandler(object): class Wallet(object): def __init__(self): - self.private_key = RSA.generate(1024) - self.encoded_public_key = self.private_key.publickey().exportKey() + self.private_key = rsa.generate_private_key(public_exponent=PUBLIC_EXPOENT, + key_size=1024, backend=default_backend()) + self.encoded_public_key = self.private_key.public_key().public_bytes(serialization.Encoding.PEM, + serialization.PublicFormat.PKCS1) self._config = None self.network = None self.wallet = None diff --git a/lbrynet/tests/unit/cryptstream/test_cryptblob.py b/lbrynet/tests/unit/cryptstream/test_cryptblob.py index 2378c5770..90719166e 100644 --- a/lbrynet/tests/unit/cryptstream/test_cryptblob.py +++ b/lbrynet/tests/unit/cryptstream/test_cryptblob.py @@ -5,11 +5,13 @@ from lbrynet.blob.blob_file import MAX_BLOB_SIZE from lbrynet.tests.mocks import mock_conf_settings -from Crypto import Random -from Crypto.Cipher import AES +from cryptography.hazmat.primitives.ciphers.algorithms import AES import random import string import StringIO +import os + +AES_BLOCK_SIZE_BYTES = AES.block_size / 8 class MocBlob(object): def __init__(self): @@ -44,8 +46,8 @@ class TestCryptBlob(unittest.TestCase): # max blob size is 2*2**20 -1 ( -1 due to required padding in the end ) blob = MocBlob() blob_num = 0 - key = Random.new().read(AES.block_size) - iv = Random.new().read(AES.block_size) + key = os.urandom(AES_BLOCK_SIZE_BYTES) + iv = os.urandom(AES_BLOCK_SIZE_BYTES) maker = CryptBlob.CryptStreamBlobMaker(key, iv, blob_num, blob) write_size = size_of_data string_to_encrypt = random_string(size_of_data) @@ -54,7 +56,7 @@ class TestCryptBlob(unittest.TestCase): done, num_bytes = maker.write(string_to_encrypt) yield maker.close() self.assertEqual(size_of_data, num_bytes) - expected_encrypted_blob_size = ((size_of_data / AES.block_size) + 1) * AES.block_size + expected_encrypted_blob_size = ((size_of_data / AES_BLOCK_SIZE_BYTES) + 1) * AES_BLOCK_SIZE_BYTES self.assertEqual(expected_encrypted_blob_size, len(blob.data)) if size_of_data < MAX_BLOB_SIZE-1: diff --git a/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py b/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py index 07ad7e87f..6a4dcc8fd 100644 --- a/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py +++ b/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from Crypto.Cipher import AES +from cryptography.hazmat.primitives.ciphers.algorithms import AES import mock from twisted.trial import unittest from twisted.internet import defer @@ -18,7 +18,7 @@ MB = 2**20 def iv_generator(): while True: - yield '3' * AES.block_size + yield '3' * (AES.block_size / 8) class CreateEncryptedFileTest(unittest.TestCase): @@ -47,7 +47,7 @@ class CreateEncryptedFileTest(unittest.TestCase): @defer.inlineCallbacks def create_file(self, filename): handle = mocks.GenFile(3*MB, '1') - key = '2'*AES.block_size + key = '2' * (AES.block_size / 8) out = yield EncryptedFileCreator.create_lbry_file(self.session, self.file_manager, filename, handle, key, iv_generator()) defer.returnValue(out)