Exemplo n.º 1
0
    def test_encrypt_key(self):
        key = Random.new().read(32)
        cipher = PKCS1_OAEP.new(self.recipient_key_private)
        encryption = Encryption(self.sender_key, self.recipient_key)

        encrypted_key = encryption.encrypt_key(key)

        self.assertEqual(cipher.decrypt(encrypted_key), key)
Exemplo n.º 2
0
    def test_sign_message(self):
        message = Random.new().read(2000)
        encryption = Encryption(self.sender_key, self.recipient_key)

        signed_message = encryption.sign_message(message)

        public_key = self.sender_key.publickey()
        hash = SHA512.new(message)
        verifier = PKCS1_v1_5.new(public_key)
        self.assertTrue(verifier.verify(hash, signed_message))
Exemplo n.º 3
0
    def test_sign_message(self):
        message = Random.new().read(2000)
        encryption = Encryption(self.sender_key, self.recipient_key)

        signed_message = encryption.sign_message(message)

        public_key = self.sender_key.publickey()
        hash = SHA512.new(message)
        verifier = PKCS1_v1_5.new(public_key)
        self.assertTrue(verifier.verify(hash, signed_message))
Exemplo n.º 4
0
    def test_encrypt_key(self):
        key = Random.new().read(32)
        cipher = PKCS1_OAEP.new(self.recipient_key_private)
        encryption = Encryption(self.sender_key, self.recipient_key)

        encrypted_key = encryption.encrypt_key(key)

        self.assertEqual(
            cipher.decrypt(encrypted_key),
            key
        )
Exemplo n.º 5
0
    def test_encrypt_message(self):
        encryption = Encryption(self.sender_key, self.recipient_key)
        key = encryption.get_key()
        iv = encryption.get_iv()
        message = Random.new().read(10)
        signed_message = encryption.sign_message(message)

        encrypted = encryption.encrypt_message(signed_message, message, key, iv)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        unpad = lambda s: s[:-ord(s[len(s) - 1:])]
        decrypted = unpad(cipher.decrypt(encrypted))

        self.assertEqual(decrypted, str(signed_message) + str(message))
Exemplo n.º 6
0
    def test_encrypt(self, sign_message, encrypt_message, encrypt_key, get_key, get_iv):
        message = Random.new().read(2000)
        encryption = Encryption(self.sender_key, self.recipient_key)
        sender_key_bytes = self.sender_key.publickey().n.bit_length() / 8
        encrypt_message.return_value = Random.new().read(len(message))
        encrypt_key.return_value = Random.new().read(sender_key_bytes)
        get_iv.return_value = Random.new().read(16)

        encrypted_message = encryption.encrypt(message)

        data = base64.b64decode(encrypted_message)
        iv = data[0:16]
        encrypted_key = data[16:(16 + sender_key_bytes)]
        encrypted_message = data[(16 + sender_key_bytes):]

        self.assertEqual(encrypt_message.return_value, encrypted_message)
        self.assertEqual(encrypt_key.return_value, encrypted_key)
        self.assertEqual(get_iv.return_value, iv)
        encrypt_key.assert_called_once_with(get_key.return_value)
        encrypt_message.assert_called_once_with(
            sign_message.return_value, message, get_key.return_value, get_iv.return_value
        )
        sign_message.assert_called_once_with(message)
Exemplo n.º 7
0
    def test_encrypt(self, sign_message, encrypt_message, encrypt_key, get_key,
                     get_iv):
        message = Random.new().read(2000)
        encryption = Encryption(self.sender_key, self.recipient_key)
        sender_key_bytes = int(self.sender_key.publickey().n.bit_length() / 8)
        encrypt_message.return_value = Random.new().read(len(message))
        encrypt_key.return_value = Random.new().read(sender_key_bytes)
        get_iv.return_value = Random.new().read(16)

        encrypted_message = encryption.encrypt(message)

        data = base64.b64decode(encrypted_message)
        iv = data[0:16]
        encrypted_key = data[16:(16 + sender_key_bytes)]
        encrypted_message = data[(16 + sender_key_bytes):]

        self.assertEqual(encrypt_message.return_value, encrypted_message)
        self.assertEqual(encrypt_key.return_value, encrypted_key)
        self.assertEqual(get_iv.return_value, iv)
        encrypt_key.assert_called_once_with(get_key.return_value)
        encrypt_message.assert_called_once_with(sign_message.return_value,
                                                message, get_key.return_value,
                                                get_iv.return_value)
        sign_message.assert_called_once_with(message)
Exemplo n.º 8
0
    def test_encrypt_message(self):
        encryption = Encryption(self.sender_key, self.recipient_key)
        key = encryption.get_key()
        iv = encryption.get_iv()
        message = Random.new().read(10)
        signed_message = encryption.sign_message(message)

        encrypted = encryption.encrypt_message(signed_message, message, key,
                                               iv)

        cipher = AES.new(key, AES.MODE_CBC, iv)
        unpad = lambda s: s[:-ord(s[len(s) - 1:])]
        decrypted = unpad(cipher.decrypt(encrypted))

        self.assertEqual(decrypted, signed_message + message)
Exemplo n.º 9
0
    def test_get_key(self, Random):
        expected = Random.new.return_value.read.return_value
        encryption = Encryption(self.sender_key, self.recipient_key)

        self.assertEqual(encryption.get_key(), expected)
        Random.new.return_value.read.assert_called_once_with(32)
Exemplo n.º 10
0
    def test_get_key(self, Random):
        expected = Random.new.return_value.read.return_value
        encryption = Encryption(self.sender_key, self.recipient_key)

        self.assertEqual(encryption.get_key(), expected)
        Random.new.return_value.read.assert_called_once_with(32)
Exemplo n.º 11
0
    def test_encrypt_not_binary(self):
        message = "not binary"
        encryption = Encryption(self.sender_key, self.recipient_key)

        six.assertRaisesRegex(self, EncryptionError, "Message must be binary.",
                              encryption.encrypt, message)
Exemplo n.º 12
0
    def test_init(self):
        encryption = Encryption(self.sender_key, self.recipient_key)

        self.assertEqual(self.sender_key, encryption.sender_key)
        self.assertEqual(self.recipient_key, encryption.recipient_key)