2019-02-16 04:08:26 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys, os
|
|
|
|
sys.path.append(".")
|
2019-11-21 09:26:23 +00:00
|
|
|
sys.path.append("src/")
|
2019-02-19 22:14:06 +00:00
|
|
|
import unittest, uuid, hashlib, base64
|
2019-02-16 04:08:26 +00:00
|
|
|
import nacl.exceptions
|
|
|
|
import nacl.signing, nacl.hash, nacl.encoding
|
|
|
|
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
|
|
|
|
print("Test directory:", TEST_DIR)
|
|
|
|
os.environ["ONIONR_HOME"] = TEST_DIR
|
2019-08-05 23:09:04 +00:00
|
|
|
from utils import createdirs
|
|
|
|
createdirs.create_dirs()
|
|
|
|
from onionrutils import stringvalidators, mnemonickeys
|
2019-07-25 16:14:13 +00:00
|
|
|
import onionrcrypto as crypto, onionrexceptions
|
2019-02-16 04:08:26 +00:00
|
|
|
|
|
|
|
class OnionrCryptoTests(unittest.TestCase):
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-16 04:08:26 +00:00
|
|
|
def test_blake2b(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
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'))
|
2019-02-16 04:08:26 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertNotEqual(crypto.hashers.blake2b_hash(''), crypto.hashers.blake2b_hash(b'test'))
|
2019-02-16 04:08:26 +00:00
|
|
|
try:
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.hashers.blake2b_hash(None)
|
2019-02-16 04:08:26 +00:00
|
|
|
except nacl.exceptions.TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertEqual(nacl.hash.blake2b(b'test'), crypto.hashers.blake2b_hash(b'test'))
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-16 04:08:26 +00:00
|
|
|
def test_sha3256(self):
|
|
|
|
hasher = hashlib.sha3_256()
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertEqual(crypto.hashers.sha3_hash('test'), crypto.hashers.sha3_hash(b'test'))
|
|
|
|
self.assertEqual(crypto.hashers.sha3_hash(b'test'), crypto.hashers.sha3_hash(b'test'))
|
2019-02-16 04:08:26 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertNotEqual(crypto.hashers.sha3_hash(''), crypto.hashers.sha3_hash(b'test'))
|
2019-02-16 04:08:26 +00:00
|
|
|
try:
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.hashers.sha3_hash(None)
|
2019-02-16 04:08:26 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-16 04:08:26 +00:00
|
|
|
hasher.update(b'test')
|
|
|
|
normal = hasher.hexdigest()
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertEqual(crypto.hashers.sha3_hash(b'test'), normal)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-16 04:08:26 +00:00
|
|
|
def valid_default_id(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertTrue(stringvalidators.validate_pub_key(crypto.pub_key))
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-16 04:08:26 +00:00
|
|
|
def test_human_readable_length(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
human = mnemonickeys.get_human_readable_ID()
|
2019-09-30 01:55:59 +00:00
|
|
|
self.assertTrue(len(human.split('-')) == 16)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-16 04:08:26 +00:00
|
|
|
def test_safe_compare(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertTrue(crypto.cryptoutils.safe_compare('test', 'test'))
|
|
|
|
self.assertTrue(crypto.cryptoutils.safe_compare('test', b'test'))
|
|
|
|
self.assertFalse(crypto.cryptoutils.safe_compare('test', 'test2'))
|
2019-02-16 04:08:26 +00:00
|
|
|
try:
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.cryptoutils.safe_compare('test', None)
|
2019-02-16 04:08:26 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
|
|
|
|
|
|
|
def test_asymmetric(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
keyPair = crypto.generate()
|
|
|
|
keyPair2 = crypto.generate()
|
2019-02-16 04:08:26 +00:00
|
|
|
message = "hello world"
|
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
self.assertTrue(len(crypto.encryption.pub_key_encrypt(message, keyPair2[0], encodedData=True)) > 0)
|
|
|
|
encrypted = crypto.encryption.pub_key_encrypt(message, keyPair2[0], encodedData=False)
|
|
|
|
decrypted = crypto.encryption.pub_key_decrypt(encrypted, privkey=keyPair2[1], encodedData=False)
|
2019-02-16 04:08:26 +00:00
|
|
|
|
|
|
|
self.assertTrue(decrypted.decode() == message)
|
|
|
|
try:
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.encryption.pub_key_encrypt(None, keyPair2[0])
|
2019-02-16 04:08:26 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
blankMessage = crypto.encryption.pub_key_encrypt('', keyPair2[0])
|
|
|
|
self.assertTrue('' == crypto.encryption.pub_key_decrypt(blankMessage, privkey=keyPair2[1], encodedData=False).decode())
|
2019-02-16 04:08:26 +00:00
|
|
|
# Try to encrypt arbitrary bytes
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.encryption.pub_key_encrypt(os.urandom(32), keyPair2[0])
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-11-04 06:31:13 +00:00
|
|
|
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)
|
|
|
|
self.assertTrue(stringvalidators.validate_pub_key(pub))
|
|
|
|
|
2019-02-19 22:14:06 +00:00
|
|
|
def test_deterministic(self):
|
|
|
|
password = os.urandom(32)
|
2019-07-25 16:14:13 +00:00
|
|
|
gen = crypto.generate_deterministic(password)
|
2019-06-25 08:21:36 +00:00
|
|
|
self.assertTrue(stringvalidators.validate_pub_key(gen[0]))
|
2019-02-19 22:14:06 +00:00
|
|
|
try:
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.generate_deterministic('weakpassword')
|
2019-02-19 22:14:06 +00:00
|
|
|
except onionrexceptions.PasswordStrengthError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertFalse(True)
|
|
|
|
try:
|
2019-07-25 16:14:13 +00:00
|
|
|
crypto.generate_deterministic(None)
|
2019-02-19 22:14:06 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertFalse(True)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
gen = crypto.generate_deterministic('weakpassword', bypassCheck=True)
|
2020-10-21 09:46:05 +00:00
|
|
|
|
2019-02-19 22:14:06 +00:00
|
|
|
password = base64.b64encode(os.urandom(32))
|
2019-07-25 16:14:13 +00:00
|
|
|
gen1 = crypto.generate_deterministic(password)
|
|
|
|
gen2 = crypto.generate_deterministic(password)
|
2019-02-19 22:14:06 +00:00
|
|
|
self.assertFalse(gen == gen1)
|
|
|
|
self.assertTrue(gen1 == gen2)
|
2019-06-25 08:21:36 +00:00
|
|
|
self.assertTrue(stringvalidators.validate_pub_key(gen1[0]))
|
2019-02-16 04:08:26 +00:00
|
|
|
|
2019-09-13 02:22:25 +00:00
|
|
|
unittest.main()
|