Python Crypto.Cipher.AES.MODE_EAX Examples
The following are 30
code examples of Crypto.Cipher.AES.MODE_EAX().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
Crypto.Cipher.AES
, or try the search function
.
Example #1
Source File: migration.py From vault with MIT License | 7 votes |
def unlock(vault_path, key): """ Unlock legacy vault and retrieve content """ f = open(vault_path, "rb") try: nonce, tag, ciphertext = [f.read(x) for x in (16, 16, -1)] finally: f.close() # Unlock Vault with key cipher = AES.new(get_hash(key), AES.MODE_EAX, nonce) data = cipher.decrypt_and_verify(ciphertext, tag) # Set vault content to class level var return json.loads(data.decode("utf-8"))
Example #2
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 6 votes |
def create_test(cls, name, factory, key_size): def test_template(self, factory=factory, key_size=key_size): cipher = factory.new(get_tag_random("cipher", key_size), factory.MODE_EAX, nonce=b"nonce") ct, mac = cipher.encrypt_and_digest(b"plaintext") cipher = factory.new(get_tag_random("cipher", key_size), factory.MODE_EAX, nonce=b"nonce") pt2 = cipher.decrypt_and_verify(ct, mac) self.assertEqual(b"plaintext", pt2) setattr(cls, "test_" + name, test_template)
Example #3
Source File: test_EAX.py From android_universal with MIT License | 6 votes |
def test_invalid_decrypt_or_update_after_verify(self): cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) ct = cipher.encrypt(self.data_128) mac = cipher.digest() for method_name in "decrypt", "update": cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.decrypt(ct) cipher.verify(mac) self.assertRaises(TypeError, getattr(cipher, method_name), self.data_128) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.decrypt_and_verify(ct, mac) self.assertRaises(TypeError, getattr(cipher, method_name), self.data_128)
Example #4
Source File: decrypt.py From Python-for-Everyday-Life with MIT License | 6 votes |
def decrypt(key, passphrase, encrypted_file_path): """ Decrypts the specified file using a RSA key and its bound passphrase :param key: an RSA key :param passphrase: str :param encrypted_file_path: str path of the file to be decrypted :return: bytes decrypted data """ print('Decrypting file {} ...'.format(encrypted_file_path)) rsa_key = RSA.import_key(key, passphrase=passphrase) with open(encrypted_file_path, 'rb') as f: # Read the encoded session key, nonce, digest and encrypted data enc_session_key, nonce, digest, ciphertext = \ [ f.read(x) for x in (rsa_key.size_in_bytes(), 16, 16, -1) ] # decode the session key cipher_rsa = PKCS1_OAEP.new(rsa_key) session_key = cipher_rsa.decrypt(enc_session_key) cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce) # finally decrypt data data = cipher_aes.decrypt_and_verify(ciphertext, digest) print('Done') return data
Example #5
Source File: test_EAX.py From android_universal with MIT License | 6 votes |
def test_decrypt(self, tv): self._id = "Wycheproof Decrypt EAX Test #" + str(tv.id) try: cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size) except ValueError as e: assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e) return cipher.update(tv.aad) try: pt = cipher.decrypt_and_verify(tv.ct, tv.tag) except ValueError: assert not tv.valid else: assert tv.valid self.assertEqual(pt, tv.msg) self.warn(tv)
Example #6
Source File: test_EAX.py From android_universal with MIT License | 6 votes |
def create_test(cls, name, factory, key_size): def test_template(self, factory=factory, key_size=key_size): cipher = factory.new(get_tag_random("cipher", key_size), factory.MODE_EAX, nonce=b"nonce") ct, mac = cipher.encrypt_and_digest(b"plaintext") cipher = factory.new(get_tag_random("cipher", key_size), factory.MODE_EAX, nonce=b"nonce") pt2 = cipher.decrypt_and_verify(ct, mac) self.assertEqual(b"plaintext", pt2) setattr(cls, "test_" + name, test_template)
Example #7
Source File: test_EAX.py From android_universal with MIT License | 6 votes |
def test_valid_multiple_encrypt_or_decrypt(self): for method_name in "encrypt", "decrypt": for auth_data in (None, b"333", self.data_128, self.data_128 + b"3"): if auth_data is None: assoc_len = None else: assoc_len = len(auth_data) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) if auth_data is not None: cipher.update(auth_data) method = getattr(cipher, method_name) method(self.data_128) method(self.data_128) method(self.data_128) method(self.data_128)
Example #8
Source File: test_EAX.py From android_universal with MIT License | 6 votes |
def test_output_param_neg(self): pt = b'5' * 16 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) ct = cipher.encrypt(pt) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) shorter_output = bytearray(15) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
Example #9
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 6 votes |
def test_mac_len(self): # Invalid MAC length self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX, nonce=self.nonce_96, mac_len=3) self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX, nonce=self.nonce_96, mac_len=16+1) # Valid MAC length for mac_len in range(5, 16 + 1): cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96, mac_len=mac_len) _, mac = cipher.encrypt_and_digest(self.data_128) self.assertEqual(len(mac), mac_len) # Default MAC length cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) _, mac = cipher.encrypt_and_digest(self.data_128) self.assertEqual(len(mac), 16)
Example #10
Source File: test_EAX.py From android_universal with MIT License | 6 votes |
def test_mac_len(self): # Invalid MAC length self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX, nonce=self.nonce_96, mac_len=3) self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX, nonce=self.nonce_96, mac_len=16+1) # Valid MAC length for mac_len in range(5, 16 + 1): cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96, mac_len=mac_len) _, mac = cipher.encrypt_and_digest(self.data_128) self.assertEqual(len(mac), mac_len) # Default MAC length cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) _, mac = cipher.encrypt_and_digest(self.data_128) self.assertEqual(len(mac), 16)
Example #11
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 6 votes |
def test_output_param_neg(self): pt = b'5' * 16 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) ct = cipher.encrypt(pt) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) shorter_output = bytearray(15) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
Example #12
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 6 votes |
def test_valid_multiple_encrypt_or_decrypt(self): for method_name in "encrypt", "decrypt": for auth_data in (None, b"333", self.data_128, self.data_128 + b"3"): if auth_data is None: assoc_len = None else: assoc_len = len(auth_data) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) if auth_data is not None: cipher.update(auth_data) method = getattr(cipher, method_name) method(self.data_128) method(self.data_128) method(self.data_128) method(self.data_128)
Example #13
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 6 votes |
def test_invalid_decrypt_or_update_after_verify(self): cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) ct = cipher.encrypt(self.data_128) mac = cipher.digest() for method_name in "decrypt", "update": cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.decrypt(ct) cipher.verify(mac) self.assertRaises(TypeError, getattr(cipher, method_name), self.data_128) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.decrypt_and_verify(ct, mac) self.assertRaises(TypeError, getattr(cipher, method_name), self.data_128)
Example #14
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 6 votes |
def test_decrypt(self, tv): self._id = "Wycheproof Decrypt EAX Test #" + str(tv.id) try: cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size) except ValueError as e: assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e) return cipher.update(tv.aad) try: pt = cipher.decrypt_and_verify(tv.ct, tv.tag) except ValueError: assert not tv.valid else: assert tv.valid self.assertEqual(pt, tv.msg) self.warn(tv)
Example #15
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_valid_init_encrypt_decrypt_digest_verify(self): # No authenticated data, fixed plaintext # Verify path INIT->ENCRYPT->DIGEST cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) ct = cipher.encrypt(self.data_128) mac = cipher.digest() # Verify path INIT->DECRYPT->VERIFY cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.decrypt(ct) cipher.verify(mac)
Example #16
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_valid_encrypt_and_digest_decrypt_and_verify(self): # encrypt_and_digest cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.update(self.data_128) ct, mac = cipher.encrypt_and_digest(self.data_128) # decrypt_and_verify cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.update(self.data_128) pt = cipher.decrypt_and_verify(ct, mac) self.assertEqual(self.data_128, pt)
Example #17
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_invalid_encrypt_or_update_after_digest(self): for method_name in "encrypt", "update": cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.encrypt(self.data_128) cipher.digest() self.assertRaises(TypeError, getattr(cipher, method_name), self.data_128) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.encrypt_and_digest(self.data_128)
Example #18
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_valid_init_update_digest_verify(self): # No plaintext, fixed authenticated data # Verify path INIT->UPDATE->DIGEST cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.update(self.data_128) mac = cipher.digest() # Verify path INIT->UPDATE->VERIFY cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.update(self.data_128) cipher.verify(mac)
Example #19
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_null_encryption_decryption(self): for func in "encrypt", "decrypt": cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) result = getattr(cipher, func)(b"") self.assertEqual(result, b"")
Example #20
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_output_param_memoryview(self): pt = b'5' * 16 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) ct = cipher.encrypt(pt) output = memoryview(bytearray(16)) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.encrypt(pt, output=output) self.assertEqual(ct, output) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) cipher.decrypt(ct, output=output) self.assertEqual(pt, output)
Example #21
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_unknown_parameters(self): self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX, self.nonce_96, 7) self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX, nonce=self.nonce_96, unknown=7) # But some are only known by the base cipher # (e.g. use_aesni consumed by the AES module) AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96, use_aesni=False)
Example #22
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_nonce_attribute(self): cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertEqual(cipher.nonce, self.nonce_96) # By default, a 16 bytes long nonce is randomly generated nonce1 = AES.new(self.key_128, AES.MODE_EAX).nonce nonce2 = AES.new(self.key_128, AES.MODE_EAX).nonce self.assertEqual(len(nonce1), 16) self.assertNotEqual(nonce1, nonce2)
Example #23
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_encrypt(self, tv): self._id = "Wycheproof Encrypt EAX Test #" + str(tv.id) try: cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size) except ValueError as e: assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e) return cipher.update(tv.aad) ct, tag = cipher.encrypt_and_digest(tv.msg) if tv.valid: self.assertEqual(ct, tv.ct) self.assertEqual(tag, tv.tag) self.warn(tv)
Example #24
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_block_size_64(self): cipher = DES3.new(self.key_192, AES.MODE_EAX, nonce=self.nonce_96) self.assertEqual(cipher.block_size, DES3.block_size)
Example #25
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_nonce_length(self): # nonce can be of any length (but not empty) self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX, nonce=b"") for x in range(1, 128): cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=bchr(1) * x) cipher.encrypt(bchr(1))
Example #26
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_nonce_must_be_bytes(self): self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX, nonce=u'test12345678')
Example #27
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_nonce(self): # If not passed, the nonce is created randomly cipher = AES.new(self.key_128, AES.MODE_EAX) nonce1 = cipher.nonce cipher = AES.new(self.key_128, AES.MODE_EAX) nonce2 = cipher.nonce self.assertEqual(len(nonce1), 16) self.assertNotEqual(nonce1, nonce2) cipher = AES.new(self.key_128, AES.MODE_EAX, self.nonce_96) ct = cipher.encrypt(self.data_128) cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96) self.assertEquals(ct, cipher.encrypt(self.data_128))
Example #28
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def test_loopback_64(self): cipher = DES3.new(self.key_192, DES3.MODE_EAX, nonce=self.nonce_96) pt = get_tag_random("plaintext", 8 * 100) ct = cipher.encrypt(pt) cipher = DES3.new(self.key_192, DES3.MODE_EAX, nonce=self.nonce_96) pt2 = cipher.decrypt(ct) self.assertEqual(pt, pt2)
Example #29
Source File: test_EAX.py From FODI with GNU General Public License v3.0 | 5 votes |
def runTest(self): for assoc_data, pt, ct, mac, key, nonce in self.test_vectors: # Encrypt cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac)) cipher.update(assoc_data) ct2, mac2 = cipher.encrypt_and_digest(pt) self.assertEqual(ct, ct2) self.assertEqual(mac, mac2) # Decrypt cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac)) cipher.update(assoc_data) pt2 = cipher.decrypt_and_verify(ct, mac) self.assertEqual(pt, pt2)
Example #30
Source File: test_EAX.py From android_universal with MIT License | 5 votes |
def runTest(self): for assoc_data, pt, ct, mac, key, nonce in self.test_vectors: # Encrypt cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac)) cipher.update(assoc_data) ct2, mac2 = cipher.encrypt_and_digest(pt) self.assertEqual(ct, ct2) self.assertEqual(mac, mac2) # Decrypt cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac)) cipher.update(assoc_data) pt2 = cipher.decrypt_and_verify(ct, mac) self.assertEqual(pt, pt2)