From 5dddeb3f10b6643618a74cbb8a9242cd158145bb Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 21 Oct 2020 09:46:05 +0000 Subject: [PATCH] remove random shuffle function for systemrandom equivalent --- .../downloadblocks/__init__.py | 9 +++--- .../uploadblocks/__init__.py | 6 ++-- .../uploadblocks/mixmate/pool.py | 5 ++-- src/onionrcrypto/cryptoutils/__init__.py | 3 +- src/onionrcrypto/cryptoutils/randomshuffle.py | 6 ---- src/runtests/__init__.py | 4 +-- src/utils/netutils.py | 6 ++-- tests/test_highlevelcrypto.py | 29 +++++++------------ 8 files changed, 28 insertions(+), 40 deletions(-) delete mode 100644 src/onionrcrypto/cryptoutils/randomshuffle.py diff --git a/src/communicatorutils/downloadblocks/__init__.py b/src/communicatorutils/downloadblocks/__init__.py index 3060364c..7752418b 100755 --- a/src/communicatorutils/downloadblocks/__init__.py +++ b/src/communicatorutils/downloadblocks/__init__.py @@ -1,9 +1,10 @@ -""" - Onionr - Private P2P Communication +"""Onionr - Private P2P Communication. - Download blocks using the communicator instance +Download blocks using the communicator instance. """ from typing import TYPE_CHECKING +from secrets import SystemRandom + if TYPE_CHECKING: from communicator import OnionrCommunicatorDaemon from deadsimplekv import DeadSimpleKV @@ -82,7 +83,7 @@ def download_blocks_from_communicator(comm_inst: "OnionrCommunicatorDaemon"): except onionrexceptions.OnlinePeerNeeded: continue else: - blockPeers = onionrcrypto.cryptoutils.random_shuffle(blockPeers) + SystemRandom().shuffle(blockPeers) peerUsed = blockPeers.pop(0) if not kv.get('shutdown') and peerUsed.strip() != '': diff --git a/src/communicatorutils/uploadblocks/__init__.py b/src/communicatorutils/uploadblocks/__init__.py index 280e29d9..265f678f 100755 --- a/src/communicatorutils/uploadblocks/__init__.py +++ b/src/communicatorutils/uploadblocks/__init__.py @@ -5,6 +5,7 @@ Upload blocks in the upload queue to peers from the communicator from typing import TYPE_CHECKING from time import sleep from threading import Thread +from secrets import SystemRandom from . import sessionmanager @@ -14,7 +15,6 @@ from communicatorutils import proxypicker import onionrexceptions from onionrblocks import onionrblockapi as block from onionrutils import stringvalidators, basicrequests -import onionrcrypto from communicator import onlinepeers if TYPE_CHECKING: from deadsimplekv import DeadSimpleKV @@ -47,8 +47,8 @@ def upload_blocks_from_communicator(comm_inst: 'OnionrCommunicatorDaemon'): sessionmanager.BlockUploadSessionManager) tried_peers: UserID = [] finishedUploads = [] - kv.put('blocksToUpload', onionrcrypto.cryptoutils.random_shuffle( - kv.get('blocksToUpload'))) + + SystemRandom().shuffle(kv.get('blocksToUpload')) def remove_from_hidden(bl): sleep(60) diff --git a/src/communicatorutils/uploadblocks/mixmate/pool.py b/src/communicatorutils/uploadblocks/mixmate/pool.py index 87e98747..72c3803b 100644 --- a/src/communicatorutils/uploadblocks/mixmate/pool.py +++ b/src/communicatorutils/uploadblocks/mixmate/pool.py @@ -3,10 +3,10 @@ Upload pool """ from typing import List +from secrets import SystemRandom import onionrutils import onionrtypes -from onionrcrypto import cryptoutils """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -62,7 +62,8 @@ class UploadPool: """Get the hash pool in secure random order.""" if len(self._pool) != self._pool_size: raise PoolNotReady - final_pool: List[onionrtypes.BlockHash] = cryptoutils.random_shuffle( + + final_pool: List[onionrtypes.BlockHash] = SystemRandom().shuffle( list(self._pool)) self._pool.clear() diff --git a/src/onionrcrypto/cryptoutils/__init__.py b/src/onionrcrypto/cryptoutils/__init__.py index e90854b8..0f6251d4 100644 --- a/src/onionrcrypto/cryptoutils/__init__.py +++ b/src/onionrcrypto/cryptoutils/__init__.py @@ -1,8 +1,7 @@ -from . import safecompare, replayvalidation, randomshuffle, verifypow +from . import safecompare, replayvalidation, verifypow from . import getpubfrompriv replay_validator = replayvalidation.replay_timestamp_validation -random_shuffle = randomshuffle.random_shuffle safe_compare = safecompare.safe_compare verify_POW = verifypow.verify_POW get_pub_key_from_priv = getpubfrompriv.get_pub_key_from_priv diff --git a/src/onionrcrypto/cryptoutils/randomshuffle.py b/src/onionrcrypto/cryptoutils/randomshuffle.py deleted file mode 100644 index 64dbf086..00000000 --- a/src/onionrcrypto/cryptoutils/randomshuffle.py +++ /dev/null @@ -1,6 +0,0 @@ -from random import SystemRandom - -def random_shuffle(theList): - myList = list(theList) - SystemRandom().shuffle(myList) - return myList \ No newline at end of file diff --git a/src/runtests/__init__.py b/src/runtests/__init__.py index 10694b70..a2a993fe 100644 --- a/src/runtests/__init__.py +++ b/src/runtests/__init__.py @@ -3,10 +3,10 @@ Test Onionr as it is running """ import os +from secrets import SystemRandom import logger from onionrutils import epoch -from onionrcrypto.cryptoutils.randomshuffle import random_shuffle from . import uicheck, inserttest, stresstest from . import ownnode @@ -55,7 +55,7 @@ class OnionrRunTestManager: self.run_date: int = 0 def run_tests(self): - tests = random_shuffle(RUN_TESTS) + tests = SystemRandom.shuffle(list(RUN_TESTS)) cur_time = epoch.get_epoch() logger.info(f"Doing runtime tests at {cur_time}") diff --git a/src/utils/netutils.py b/src/utils/netutils.py index c49cd524..8d428b04 100755 --- a/src/utils/netutils.py +++ b/src/utils/netutils.py @@ -2,9 +2,10 @@ NetUtils offers various useful functions to Onionr networking. """ +from random import SystemRandom + from onionrutils import basicrequests from .readstatic import read_static -from onionrcrypto.cryptoutils import random_shuffle """ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,8 +27,7 @@ def check_network(torPort=0) -> bool: success = False connect_urls = [] try: - connect_urls = random_shuffle( - read_static('connect-check.txt').split(',')) + connect_urls = SystemRandom().shuffle(read_static('connect-check.txt').split(',')) for url in connect_urls: if basicrequests.do_get_request( diff --git a/tests/test_highlevelcrypto.py b/tests/test_highlevelcrypto.py index 9317f720..dd83a239 100644 --- a/tests/test_highlevelcrypto.py +++ b/tests/test_highlevelcrypto.py @@ -14,7 +14,7 @@ from onionrutils import stringvalidators, mnemonickeys import onionrcrypto as crypto, onionrexceptions class OnionrCryptoTests(unittest.TestCase): - + def test_blake2b(self): self.assertEqual(crypto.hashers.blake2b_hash('test'), crypto.hashers.blake2b_hash(b'test')) self.assertEqual(crypto.hashers.blake2b_hash(b'test'), crypto.hashers.blake2b_hash(b'test')) @@ -26,9 +26,9 @@ class OnionrCryptoTests(unittest.TestCase): pass else: self.assertTrue(False) - + self.assertEqual(nacl.hash.blake2b(b'test'), crypto.hashers.blake2b_hash(b'test')) - + def test_sha3256(self): hasher = hashlib.sha3_256() self.assertEqual(crypto.hashers.sha3_hash('test'), crypto.hashers.sha3_hash(b'test')) @@ -41,18 +41,18 @@ class OnionrCryptoTests(unittest.TestCase): pass else: self.assertTrue(False) - + hasher.update(b'test') normal = hasher.hexdigest() self.assertEqual(crypto.hashers.sha3_hash(b'test'), normal) - + def valid_default_id(self): self.assertTrue(stringvalidators.validate_pub_key(crypto.pub_key)) - + def test_human_readable_length(self): human = mnemonickeys.get_human_readable_ID() self.assertTrue(len(human.split('-')) == 16) - + def test_safe_compare(self): self.assertTrue(crypto.cryptoutils.safe_compare('test', 'test')) self.assertTrue(crypto.cryptoutils.safe_compare('test', b'test')) @@ -63,13 +63,6 @@ class OnionrCryptoTests(unittest.TestCase): pass else: self.assertTrue(False) - - def test_random_shuffle(self): - # Small chance that the randomized list will be same. Rerun test a couple times if it fails - startList = ['cat', 'dog', 'moose', 'rabbit', 'monkey', 'crab', 'human', 'dolphin', 'whale', 'etc'] * 10 - - self.assertNotEqual(startList, list(crypto.cryptoutils.random_shuffle(startList))) - self.assertTrue(len(list(crypto.cryptoutils.random_shuffle(startList))) == len(startList)) def test_asymmetric(self): keyPair = crypto.generate() @@ -87,12 +80,12 @@ class OnionrCryptoTests(unittest.TestCase): pass else: self.assertTrue(False) - + blankMessage = crypto.encryption.pub_key_encrypt('', keyPair2[0]) self.assertTrue('' == crypto.encryption.pub_key_decrypt(blankMessage, privkey=keyPair2[1], encodedData=False).decode()) # Try to encrypt arbitrary bytes crypto.encryption.pub_key_encrypt(os.urandom(32), keyPair2[0]) - + def test_pub_from_priv(self): priv = nacl.signing.SigningKey.generate().encode(encoder=nacl.encoding.Base32Encoder) pub = crypto.cryptoutils.getpubfrompriv.get_pub_key_from_priv(priv) @@ -114,9 +107,9 @@ class OnionrCryptoTests(unittest.TestCase): pass else: self.assertFalse(True) - + gen = crypto.generate_deterministic('weakpassword', bypassCheck=True) - + password = base64.b64encode(os.urandom(32)) gen1 = crypto.generate_deterministic(password) gen2 = crypto.generate_deterministic(password)