def test_bad_ctx_new(self): iv, aes_key, enc = envelope.encrypt(SHORT, KEY_1[1]) new_ctx = envelope.evp.EVP_CIPHER_CTX_new envelope.evp.EVP_CIPHER_CTX_new = lambda: None self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, KEY_1[1]) self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, enc, KEY_1[0]) envelope.evp.EVP_CIPHER_CTX_new = new_ctx
def test_bad_open_init(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) decrypt_init = envelope.evp.EVP_OpenInit envelope.evp.EVP_OpenInit = lambda a, b, c, d, e, f: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.EVP_OpenInit = decrypt_init
def test_bad_fopen(self): iv, aes_key, enc = envelope.encrypt(SHORT, KEY_1[1]) file_open = envelope.evp.fopen envelope.evp.fopen = lambda a,b: None self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, KEY_1[1]) self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, enc, KEY_1[0]) envelope.evp.fopen = file_open
def round_trip_strings(self, keys, text): iv, sym_key, enc = envelope.encrypt(text, key=open(keys[1], 'rb').read()) return envelope.decrypt(iv, sym_key, enc, key=open(keys[0], 'rb').read())
def test_bad_decrypt_update(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) decrypt_update = envelope.evp.EVP_DecryptUpdate envelope.evp.EVP_DecryptUpdate = lambda a, b, c, d, e: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.EVP_DecryptUpdate = decrypt_update
def test_bad_read_privatekey(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) read_private_key = envelope.evp.PEM_read_PrivateKey envelope.evp.PEM_read_PrivateKey = lambda a, b, c, d: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.PEM_read_PrivateKey = read_private_key
def test_bad_call(self): # neither key nor keyfile self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT) # both self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, KEY_1[0], open(KEY_1[0], 'rb').read()) # string key instead of bytes self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, key=open(KEY_1[0], 'r').read()) # string data instead of bytes self.assertRaises(envelope.EnvelopeError, envelope.encrypt, str(SHORT), KEY_1[0], open(KEY_1[0], 'rb').read()) # get valid encryption data iv, aes_key, enc = envelope.encrypt(SHORT, KEY_1[1]) # neither key nor keyfile self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, enc) # both self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, enc, KEY_1[1], key=open(KEY_1[1], 'rb').read()) # string key instead of bytes self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, enc, key=open(KEY_1[1], 'r').read()) # string data instead of bytes self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, str(enc), KEY_1[1], key=open(KEY_1[1], 'rb').read()) # string iv instead of bytes self.assertRaises(envelope.EnvelopeError, envelope.decrypt, str(iv), aes_key, enc, KEY_1[1], key=open(KEY_1[1], 'rb').read()) # string key instead of bytes self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, str(aes_key), enc, KEY_1[1], key=open(KEY_1[1], 'rb').read())
def test_bad_cipher_object(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) cipher_getter = envelope.evp.EVP_aes_192_cbc envelope.evp.EVP_aes_192_cbc= lambda: None self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, KEY_1[1]) self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.EVP_aes_192_cbc = cipher_getter
def test_bad_decrypt_final(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) decrypt_final = envelope.evp.EVP_DecryptFinal_ex envelope.evp.EVP_DecryptFinal_ex = lambda a, b, c: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) cipher.evp.EVP_DecryptFinal_ex = decrypt_final
def test_bad_cipher_object(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) cipher_getter = envelope.evp.EVP_aes_192_cbc envelope.evp.EVP_aes_192_cbc = lambda: None self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, KEY_1[1]) self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.EVP_aes_192_cbc = cipher_getter
def test_bad_fopen(self): iv, aes_key, enc = envelope.encrypt(SHORT, KEY_1[1]) file_open = envelope.evp.fopen envelope.evp.fopen = lambda a, b: None self.assertRaises(envelope.EnvelopeError, envelope.encrypt, SHORT, KEY_1[1]) self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, aes_key, enc, KEY_1[0]) envelope.evp.fopen = file_open
def test_bad_read_bio_privatekey(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) read_private_key = envelope.evp.PEM_read_bio_PrivateKey envelope.evp.PEM_read_bio_PrivateKey = lambda a,b,c,d: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, key=open(KEY_1[0], 'rb').read()) envelope.evp.PEM_read_bio_PrivateKey = read_private_key
def test_bad_decrypt_update(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) decrypt_update = envelope.evp.EVP_DecryptUpdate envelope.evp.EVP_DecryptUpdate = lambda a,b,c,d,e: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.EVP_DecryptUpdate = decrypt_update
def test_bad_open_init(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) decrypt_init = envelope.evp.EVP_OpenInit envelope.evp.EVP_OpenInit = lambda a,b,c,d,e,f: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) envelope.evp.EVP_OpenInit = decrypt_init
def round_trip(self, keys, text): iv, sym_key, enc = envelope.encrypt(text, keys[1]) return envelope.decrypt(iv, sym_key, enc, keys[0])
def test_bad_decrypt_final(self): iv, key, enc = envelope.encrypt(SHORT, KEY_1[1]) decrypt_final = envelope.evp.EVP_DecryptFinal_ex envelope.evp.EVP_DecryptFinal_ex = lambda a,b,c: None self.assertRaises(envelope.EnvelopeError, envelope.decrypt, iv, key, enc, KEY_1[0]) cipher.evp.EVP_DecryptFinal_ex = decrypt_final