Beispiel #1
0
 def test_sign(self):
     sig = Crypto.sign(b"test", self.key)
     assert_equal(bytes, sig.__class__)
     sig2 = Crypto.sign(b"tesa", self.key)
     assert_equal(bytes, sig2.__class__)
     assert_not_equal(
         sig, sig2,
         'signatures for two different strings should return different values'
     )
Beispiel #2
0
 def setUp(self):
     self.key = Crypto.generate_key()
     self.addr_port_pair = ('127.0.0.1', 35341)
     self.client = KnockClient(socket.AF_INET, self.addr_port_pair)
     self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     self.server.bind(self.addr_port_pair)
     self.expected_time = int(time.time())
Beispiel #3
0
 def test_create_encrypted_knock_request(self):
     expected_visit_duration = 12600
     KnockClient.create_encrypted_knock_request
     encrypted_knock_request = KnockClient.create_encrypted_knock_request(
         expected_visit_duration, self.key)
     knock_request = Crypto.decrypt(encrypted_knock_request, self.key)
     assert_is_not_none(knock_request)
     self.check_knock_request(knock_request, expected_visit_duration)
Beispiel #4
0
 def test_knock(self):
     expected_visit_duration = 111
     self.client.knock(expected_visit_duration, self.key)
     data, addr = self.server.recvfrom(2048)
     assert_is_not_none(data)
     decrypted = Crypto.decrypt(data, self.key)
     assert_is_not_none(decrypted)
     self.check_knock_request(decrypted, expected_visit_duration)
Beispiel #5
0
    def handle(self, data, addr):
        decrypted_data = Crypto.decrypt(data, self.key)
        if decrypted_data is None:
            return None

        knock_request = self.parse(decrypted_data)
        if not self.validate(knock_request):
            return None

        return self.execute(knock_request)
Beispiel #6
0
 def test_decrypt_wrong_key(self):
     assert_equal(
         None,
         Crypto.decrypt(Crypto.encrypt(self.message, self.key),
                        self.wrong_key))
Beispiel #7
0
 def test_decrypt(self):
     result = Crypto.decrypt(Crypto.encrypt(self.message, self.key),
                             self.key)
     assert_equal(result, self.message)
Beispiel #8
0
 def test_encrypt_different_key(self):
     enc1 = Crypto.encrypt(self.message, self.key, iv=self.iv)
     enc2 = Crypto.encrypt(self.message, self.wrong_key, iv=self.iv)
     assert_equal(bytes, enc1.__class__)
     assert_equal(bytes, enc2.__class__)
     assert_not_equal(enc1, enc2)
Beispiel #9
0
 def check_pad(self, message):
     padded_message = Crypto.pad(message)
     assert_greater_equal(len(padded_message), len(padded_message))
     assert_equal(0, len(padded_message) % Crypto.BLOCK_SIZE)
 def test_invalid_key(self):
     assert_raises(ValueError, KnockRequestHandler, Crypto.generate_key()[:-1])
Beispiel #11
0
 def test_verify_fail_wrong_signature(self):
     sig = Crypto.sign(self.message, self.wrong_key)
     assert_false(Crypto.verify(self.message + b"2", self.key, sig))
Beispiel #12
0
 def test_verify(self):
     sig = Crypto.sign(self.message, self.key)
     assert_true(Crypto.verify(self.message, self.key, sig))
Beispiel #13
0
 def test_extract_crypted(self):
     assert_equal(self.fake_crypted,
                  Crypto.extract_crypted(self.fake_encrypted))
Beispiel #14
0
 def test_extract_signature(self):
     assert_equal(self.fake_sig,
                  Crypto.extract_signature(self.fake_encrypted))
Beispiel #15
0
 def test_extract_iv(self):
     assert_equal(self.fake_iv, Crypto.extract_iv(self.fake_encrypted))
Beispiel #16
0
 def check_unpad(self, padded_message):
     assert_equal(padded_message, Crypto.unpad(Crypto.pad(padded_message)))
Beispiel #17
0
 def test_encrypt_same_iv(self):
     enc1 = Crypto.encrypt(self.message, self.key, iv=self.iv)
     enc2 = Crypto.encrypt(self.message, self.key, iv=self.iv)
     assert_equal(bytes, enc1.__class__)
     assert_equal(bytes, enc2.__class__)
     assert_equal(enc1, enc2)
Beispiel #18
0
 def test_encrypt_same_iv_different_message(self):
     enc1 = Crypto.encrypt(self.message + b"a", self.key, iv=self.iv)
     enc2 = Crypto.encrypt(self.message + b"b", self.key, iv=self.iv)
     assert_equal(bytes, enc1.__class__)
     assert_equal(bytes, enc2.__class__)
     assert_not_equal(enc1, enc2)
 def setUp(self):
     self.handler = KnockRequestHandler(Crypto.generate_key())
     self.req_invalid_knock_time = KnockRequest(0, 100)
     self.req_invalid_visit_duration = KnockRequest(0, 1000000)
     self.request_valid = KnockRequest.create(100)
Beispiel #20
0
 def test_generate_key(self):
     key = Crypto.generate_key()
     assert_equals(Crypto.KEY_SIZE, len(key))
     assert_is_instance(key, bytes)