From a073ee35d8f8041e0e568d8c754f066ee5ad83bb Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Fri, 3 Apr 2015 07:19:46 +0500 Subject: [PATCH] Add more encryption tests --- tests/encryption/priv_key.bin | Bin 0 -> 610 bytes tests/encryption/pub_key.bin | Bin 0 -> 162 bytes tests/test_encryption.py | 66 +++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/encryption/priv_key.bin create mode 100644 tests/encryption/pub_key.bin diff --git a/tests/encryption/priv_key.bin b/tests/encryption/priv_key.bin new file mode 100644 index 0000000000000000000000000000000000000000..90a0235aedfe4cad41593a384ee28f713a3fd3a4 GIT binary patch literal 610 zcmV-o0-gOZf&yLw0RRGlfdH+i1qYg%Yu8}m#u>>FbMtgV^=#$~XHT};2#_=OcsZaA zLQKLpH@4YvSC+zewlYq#dyn-oUEa94vdwAh4=^a=YD|2(zaqq6LY&fM!?m+Ek4G!K zKCw7~z)~8FF0ikA;Vu?~^LEKimpV5Rg+nzs04$s!&3zaW*zdbn`3(XC0RRC4fq+_d z(=6`b+N8`EQ1?Z`h-99Ccx_HON{m1lytP6ccI>V1x<&dnfs=z8gd#~3%bC$#F!=!` z!d$PWEc(peVtzbcEn4vW5i>UDAz5`x{5ba6FRIYLY=>MlPmQPJ{p~mu%RE8(9~>b7 zX~>3qBD*}B1ocUUvi=a^SnnI7?12J70M%VIchL;AM!D>|^WA&FtZ8x)t*I&r@%dv~ zd{Z#(O&lawrm`|$X(EAN^l0}UTrElU*z3G@4 z0zm+|aLB1zh%hHeaQF-R^b_Dk#a@^Y?)bIVivyYpv-^vQY7E3x8NBFUalo1A-ns?~ zeHhugLM$G3_YM+dt>OU!K>(Xi%cFQ*qfwlpxY3vV3^j!EPH*{z5e_#(F0I$kgNG@$ zmt-Y+?c;(g5{HR11ON_g)4>TloS(0HcRZAn>!_?@fwahXEmxa}=eU wvorAM2}3H1$a-hHj2l{ce~|tBj|q~i>J(SkzgB~_7f&7cwkv^@=W^61KiRt<2mk;8 literal 0 HcmV?d00001 diff --git a/tests/encryption/pub_key.bin b/tests/encryption/pub_key.bin new file mode 100644 index 0000000000000000000000000000000000000000..369734d9b1d3ccf682ea676ea7fed9563728675c GIT binary patch literal 162 zcmV;T0A2qufuAr91_>&LNQUP8v!gsbZPO^KC^)X%ExVf^;Y3mO#DB@~N ze7e6P#9u<3(qzN6vo?=ME4)6jIDx=Y8jLQmuY2Jx7K8J4$xW9!Hxh+IH8=n)oFL78 Q7!ug;yI1)Q0s{d60f8Vz9{>OV literal 0 HcmV?d00001 diff --git a/tests/test_encryption.py b/tests/test_encryption.py index 9f41761..f9ab452 100644 --- a/tests/test_encryption.py +++ b/tests/test_encryption.py @@ -1,6 +1,22 @@ +import os import unittest import hashlib -from minecraft.networking.encryption import minecraft_sha1_hash_digest +from io import BytesIO +from minecraft.networking.encryption import ( + minecraft_sha1_hash_digest, + encrypt_token_and_secret, + generate_shared_secret, + generate_verification_hash, + create_AES_cipher, + EncryptedFileObjectWrapper, +) + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 +from cryptography.hazmat.primitives.serialization import load_der_private_key + +KEY_LOCATION = os.path.join(os.path.dirname(os.path.realpath(__file__)), + "encryption") class Hashing(unittest.TestCase): @@ -13,3 +29,51 @@ class Hashing(unittest.TestCase): sha1_hash = hashlib.sha1() sha1_hash.update(input_value.encode('utf-8')) self.assertEquals(minecraft_sha1_hash_digest(sha1_hash), result) + + +class Encryption(unittest.TestCase): + + def setUp(self): + with open(os.path.join(KEY_LOCATION, "priv_key.bin"), "rb") as f: + self.private_key = f.read() + self.private_key = load_der_private_key(self.private_key, None, + default_backend()) + + with open(os.path.join(KEY_LOCATION, "pub_key.bin"), "rb") as f: + self.public_key = f.read() + self.token = generate_shared_secret() + + def test_token_secret_encryption(self): + secret = generate_shared_secret() + token, encrypted_secret = encrypt_token_and_secret(self.public_key, + self.token, secret) + decrypted_token = self.private_key.decrypt(token, + PKCS1v15()) + decrypted_secret = self.private_key.decrypt(encrypted_secret, + PKCS1v15()) + + self.assertEquals(self.token, decrypted_token) + self.assertEquals(secret, decrypted_secret) + + def generate_hash_test(self): + verification_hash = generate_verification_hash( + b"", "secret".encode('utf-8'), self.public_key) + self.assertEquals("1f142e737a84a974a5f2a22f6174a78d80fd97f5", + verification_hash) + + def test_file_object_wrapper(self): + cipher = create_AES_cipher(generate_shared_secret()) + encryptor = cipher.encryptor() + decryptor = cipher.decryptor() + + test_data = "hello".encode('utf-8') + io = BytesIO() + io.write(encryptor.update(test_data)) + io.seek(0) + + file_object_wrapper = EncryptedFileObjectWrapper(io, decryptor) + decrypted_data = file_object_wrapper.read(len(test_data)) + + self.assertEqual(test_data, decrypted_data) + + # TODO: test for the socket wrapper