def test_round_trip_failure(self): salt, iv, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, salt, iv, enc, LONG_SYMMETRIC) salt, iv, enc = cipher.encrypt(NULL, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, salt, iv, enc, LONG_SYMMETRIC)
def test_bad_decrypt_final(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) decrypt_final = cipher.evp.EVP_DecryptFinal_ex cipher.evp.EVP_DecryptFinal_ex = lambda a, b, c: None self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_DecryptFinal_ex = decrypt_final
def test_bad_ctx_new(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) new_ctx = cipher.evp.EVP_CIPHER_CTX_new cipher.evp.EVP_CIPHER_CTX_new = lambda: None self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_CIPHER_CTX_new = new_ctx
def test_bad_rand_bytes_1(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) rand_bytes = cipher.evp.RAND_bytes cipher.evp.RAND_bytes = lambda a, b: 0 self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) cipher.evp.RAND_bytes = rand_bytes
def test_bad_decrypt_update(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) decrypt_update = cipher.evp.EVP_DecryptUpdate cipher.evp.EVP_DecryptUpdate = lambda a, b, c, d, e: None self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_DecryptUpdate = decrypt_update
def test_bad_bytes_to_key(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) bytes_to_key = cipher.evp.EVP_BytesToKey cipher.evp.EVP_BytesToKey = lambda a,b,c,d,e,f,g,h: 0 self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_BytesToKey = bytes_to_key
def test_bad_hash_by_name(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) hash_by_name = cipher.evp.EVP_get_digestbyname cipher.evp.EVP_get_digestbyname = lambda a: None self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) cipher.evp.EVP_get_digestbyname = hash_by_name
def test_bad_decrypt_init(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) decrypt_init = cipher.evp.EVP_DecryptInit_ex cipher.evp.EVP_DecryptInit_ex = lambda a, b, c, d, e: None self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_DecryptInit_ex = decrypt_init
def test_bad_cipher_object(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) cipher_getter = cipher.evp.EVP_aes_192_cbc cipher.evp.EVP_aes_192_cbc= lambda: None self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_aes_192_cbc = cipher_getter
def test_bad_cipher_object(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) cipher_getter = cipher.evp.EVP_aes_192_cbc cipher.evp.EVP_aes_192_cbc = lambda: None self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_aes_192_cbc = cipher_getter
def test_bad_bytes_to_key(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) bytes_to_key = cipher.evp.EVP_BytesToKey cipher.evp.EVP_BytesToKey = lambda a, b, c, d, e, f, g, h: 0 self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_BytesToKey = bytes_to_key
def test_round_trip_no_password(self): iv, salt, enc = cipher.encrypt(UNICODE, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.encrypt, UNICODE, '') self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, '')
def test_long_iv(self): salt, iv, enc = cipher.encrypt(UNICODE, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, salt, iv+iv[:-1], enc, SHORT_SYMMETRIC)
def test_long_iv(self): salt, iv, enc = cipher.encrypt(UNICODE, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, salt, iv + iv[:-1], enc, SHORT_SYMMETRIC)
def round_trip(self, key, text): salt, iv, enc = cipher.encrypt(text, key) output = cipher.decrypt(salt, iv, enc, key) self.assertEqual(output, text, "Failed to round trip")
def test_bad_rand_bytes_2(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) rand_bytes = cipher.evp.RAND_bytes cipher.evp.RAND_bytes = run_n_times(cipher.evp.RAND_bytes, lambda a,b:0, 1) self.assertRaises(cipher.CipherError, cipher.encrypt, SHORT, SHORT_SYMMETRIC) cipher.evp.RAND_bytes = rand_bytes
def test_bad_decrypt_update(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) decrypt_update = cipher.evp.EVP_DecryptUpdate cipher.evp.EVP_DecryptUpdate = lambda a,b,c,d,e: None self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_DecryptUpdate = decrypt_update
def test_bad_decrypt_init(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) decrypt_init = cipher.evp.EVP_DecryptInit_ex cipher.evp.EVP_DecryptInit_ex = lambda a,b,c,d,e: None self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_DecryptInit_ex = decrypt_init
def test_no_data(self): iv, salt, enc = cipher.encrypt(UNICODE, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, '', SHORT_SYMMETRIC)
def test_bad_decrypt_final(self): iv, salt, enc = cipher.encrypt(SHORT, SHORT_SYMMETRIC) decrypt_final = cipher.evp.EVP_DecryptFinal_ex cipher.evp.EVP_DecryptFinal_ex = lambda a,b,c: None self.assertRaises(cipher.CipherError, cipher.decrypt, iv, salt, enc, SHORT_SYMMETRIC) cipher.evp.EVP_DecryptFinal_ex = decrypt_final
def test_short_salt(self): salt, iv, enc = cipher.encrypt(UNICODE, SHORT_SYMMETRIC) self.assertRaises(cipher.CipherError, cipher.decrypt, salt[:-1], iv, enc, SHORT_SYMMETRIC)