Python Crypto.Cipher.AES.MODE_CCM() Examples

The following are code examples for showing how to use Crypto.Cipher.AES.MODE_CCM(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_mac_len(self):
        # Invalid MAC length
        for mac_len in range(3, 17 + 1, 2):
            self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                              nonce=self.nonce_96, mac_len=mac_len)

        # Valid MAC length
        for mac_len in range(4, 16 + 1, 2):
            cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
        _, mac = cipher.encrypt_and_digest(self.data_128)
        self.assertEqual(len(mac), 16) 
Example 2
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_output_param_neg(self):

        pt = b'5' * 16
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        ct = cipher.encrypt(pt)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
        
        cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) 
Example 3
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_valid_init_update_digest_verify(self):
        # No plaintext, fixed authenticated data
        for assoc_len in (None, len(self.data_128)):
            for msg_len in (None, 0):
                # Verify path INIT->UPDATE->DIGEST
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                mac = cipher.digest()

                # Verify path INIT->UPDATE->VERIFY
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                cipher.verify(mac) 
Example 4
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_valid_full_path(self):
        # Fixed authenticated data, fixed plaintext
        for assoc_len in (None, len(self.data_128)):
            for msg_len in (None, len(self.data_128)):
                # Verify path INIT->UPDATE->ENCRYPT->DIGEST
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                ct = cipher.encrypt(self.data_128)
                mac = cipher.digest()

                # Verify path INIT->UPDATE->DECRYPT->VERIFY
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                cipher.decrypt(ct)
                cipher.verify(mac) 
Example 5
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_valid_multiple_encrypt_or_decrypt(self):
        # Only possible if msg_len is declared in advance
        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_CCM,
                                 nonce=self.nonce_96,
                                 msg_len=64,
                                 assoc_len=assoc_len)
                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 6
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_invalid_decrypt_or_update_after_verify(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, 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_CCM, nonce=self.nonce_96)
            cipher.decrypt_and_verify(ct, mac)
            self.assertRaises(TypeError, getattr(cipher, method_name),
                              self.data_128) 
Example 7
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 6 votes vote down vote up
def test_mac_len(self):
        # Invalid MAC length
        for mac_len in range(3, 17 + 1, 2):
            self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                              nonce=self.nonce_96, mac_len=mac_len)

        # Valid MAC length
        for mac_len in range(4, 16 + 1, 2):
            cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
        _, mac = cipher.encrypt_and_digest(self.data_128)
        self.assertEqual(len(mac), 16) 
Example 8
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 6 votes vote down vote up
def test_output_param_neg(self):

        pt = b'5' * 16
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        ct = cipher.encrypt(pt)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
        
        cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) 
Example 9
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 6 votes vote down vote up
def test_valid_init_update_digest_verify(self):
        # No plaintext, fixed authenticated data
        for assoc_len in (None, len(self.data_128)):
            for msg_len in (None, 0):
                # Verify path INIT->UPDATE->DIGEST
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                mac = cipher.digest()

                # Verify path INIT->UPDATE->VERIFY
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                cipher.verify(mac) 
Example 10
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 6 votes vote down vote up
def test_valid_full_path(self):
        # Fixed authenticated data, fixed plaintext
        for assoc_len in (None, len(self.data_128)):
            for msg_len in (None, len(self.data_128)):
                # Verify path INIT->UPDATE->ENCRYPT->DIGEST
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                ct = cipher.encrypt(self.data_128)
                mac = cipher.digest()

                # Verify path INIT->UPDATE->DECRYPT->VERIFY
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.update(self.data_128)
                cipher.decrypt(ct)
                cipher.verify(mac) 
Example 11
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 6 votes vote down vote up
def test_valid_multiple_encrypt_or_decrypt(self):
        # Only possible if msg_len is declared in advance
        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_CCM,
                                 nonce=self.nonce_96,
                                 msg_len=64,
                                 assoc_len=assoc_len)
                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 12
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 6 votes vote down vote up
def test_invalid_decrypt_or_update_after_verify(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, 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_CCM, nonce=self.nonce_96)
            cipher.decrypt_and_verify(ct, mac)
            self.assertRaises(TypeError, getattr(cipher, method_name),
                              self.data_128) 
Example 13
Project: geofire-python   Author: ininex   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_mac_len(self):
        # Invalid MAC length
        for mac_len in xrange(3, 17 + 1, 2):
            self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                              nonce=self.nonce_96, mac_len=mac_len)

        # Valid MAC length
        for mac_len in xrange(4, 16 + 1, 2):
            cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
        _, mac = cipher.encrypt_and_digest(self.data_128)
        self.assertEqual(len(mac), 16) 
Example 14
Project: geofire-python   Author: ininex   File: test_CCM.py    MIT License 6 votes vote down vote up
def test_valid_init_encrypt_decrypt_digest_verify(self):
        # No authenticated data, fixed plaintext
        for assoc_len in (None, 0):
            for msg_len in (None, len(self.data_128)):
                # Verify path INIT->ENCRYPT->DIGEST
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                ct = cipher.encrypt(self.data_128)
                mac = cipher.digest()

                # Verify path INIT->DECRYPT->VERIFY
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 assoc_len=assoc_len,
                                 msg_len=msg_len)
                cipher.decrypt(ct)
                cipher.verify(mac) 
Example 15
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_loopback_128(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        pt = get_tag_random("plaintext", 16 * 100)
        ct = cipher.encrypt(pt)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        pt2 = cipher.decrypt(ct)
        self.assertEqual(pt, pt2) 
Example 16
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_nonce(self):
        # If not passed, the nonce is created randomly
        cipher = AES.new(self.key_128, AES.MODE_CCM)
        nonce1 = cipher.nonce
        cipher = AES.new(self.key_128, AES.MODE_CCM)
        nonce2 = cipher.nonce
        self.assertEqual(len(nonce1), 11)
        self.assertNotEqual(nonce1, nonce2)

        cipher = AES.new(self.key_128, AES.MODE_CCM, self.nonce_96)
        ct = cipher.encrypt(self.data_128)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertEquals(ct, cipher.encrypt(self.data_128)) 
Example 17
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_nonce_must_be_bytes(self):
        self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=u'test12345678') 
Example 18
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_nonce_length(self):
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=b"")
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=bchr(1) * 6)
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=bchr(1) * 14)
        for x in range(7, 13 + 1):
            AES.new(self.key_128, AES.MODE_CCM, nonce=bchr(1) * x) 
Example 19
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_block_size(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertEqual(cipher.block_size, AES.block_size) 
Example 20
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_unknown_parameters(self):
        self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
                          self.nonce_96, 7)
        self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
                          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_CCM, nonce=self.nonce_96,
                use_aesni=False) 
Example 21
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_null_encryption_decryption(self):
        for func in "encrypt", "decrypt":
            cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
            result = getattr(cipher, func)(b"")
            self.assertEqual(result, b"") 
Example 22
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_either_encrypt_or_decrypt(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.encrypt(b"")
        self.assertRaises(TypeError, cipher.decrypt, b"")

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.decrypt(b"")
        self.assertRaises(TypeError, cipher.encrypt, b"") 
Example 23
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_data_must_be_bytes(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') 
Example 24
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_hex_mac(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        mac_hex = cipher.hexdigest()
        self.assertEqual(cipher.digest(), unhexlify(mac_hex))

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.hexverify(mac_hex) 
Example 25
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_longer_assoc_data_than_declared(self):
        # More than zero
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=0)
        self.assertRaises(ValueError, cipher.update, b"1")

        # Too large
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=15)
        self.assertRaises(ValueError, cipher.update, self.data_128) 
Example 26
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_shorter_assoc_data_than_expected(self):
        # With plaintext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.encrypt, self.data_128)

        # With empty plaintext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.digest)

        # With ciphertext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.decrypt, self.data_128)

        # With empty ciphertext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.update(self.data_128)
        mac = cipher.digest()

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.verify, mac) 
Example 27
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_shorter_and_longer_plaintext_than_declared(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=17)
        cipher.encrypt(self.data_128)
        self.assertRaises(ValueError, cipher.digest)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=15)
        self.assertRaises(ValueError, cipher.encrypt, self.data_128) 
Example 28
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_shorter_ciphertext_than_declared(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        ct, mac = cipher.encrypt_and_digest(self.data_128)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=17)
        cipher.decrypt(ct)
        self.assertRaises(ValueError, cipher.verify, mac)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=15)
        self.assertRaises(ValueError, cipher.decrypt, ct) 
Example 29
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_output_param(self):

        pt = b'5' * 16
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        ct = cipher.encrypt(pt)
        tag = cipher.digest()

        output = bytearray(16)
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        res = cipher.encrypt(pt, output=output)
        self.assertEqual(ct, output)
        self.assertEqual(res, None)
        
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        res = cipher.decrypt(ct, output=output)
        self.assertEqual(pt, output)
        self.assertEqual(res, None)
        
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        res, tag_out = cipher.encrypt_and_digest(pt, output=output)
        self.assertEqual(ct, output)
        self.assertEqual(res, None)
        self.assertEqual(tag, tag_out)
        
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        res = cipher.decrypt_and_verify(ct, tag, output=output)
        self.assertEqual(pt, output)
        self.assertEqual(res, None) 
Example 30
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_output_param_memoryview(self):
        
        pt = b'5' * 16
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        ct = cipher.encrypt(pt)

        output = memoryview(bytearray(16))
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.encrypt(pt, output=output)
        self.assertEqual(ct, output)
        
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.decrypt(ct, output=output)
        self.assertEqual(pt, output) 
Example 31
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_valid_init_digest(self):
        # Verify path INIT->DIGEST
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.digest() 
Example 32
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_valid_init_verify(self):
        # Verify path INIT->VERIFY
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        mac = cipher.digest()

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.verify(mac) 
Example 33
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_valid_encrypt_and_digest_decrypt_and_verify(self):
        # encrypt_and_digest
        cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
        cipher.update(self.data_128)
        pt = cipher.decrypt_and_verify(ct, mac)
        self.assertEqual(self.data_128, pt) 
Example 34
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_invalid_multiple_encrypt_decrypt_without_msg_len(self):
        # Once per method, with or without assoc. data
        for method_name in "encrypt", "decrypt":
            for assoc_data_present in (True, False):
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96)
                if assoc_data_present:
                    cipher.update(self.data_128)
                method = getattr(cipher, method_name)
                method(self.data_128)
                self.assertRaises(TypeError, method, self.data_128) 
Example 35
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_invalid_mixing_encrypt_decrypt(self):
        # Once per method, with or without assoc. data
        for method1_name, method2_name in (("encrypt", "decrypt"),
                                           ("decrypt", "encrypt")):
            for assoc_data_present in (True, False):
                cipher = AES.new(self.key_128, AES.MODE_CCM,
                                 nonce=self.nonce_96,
                                 msg_len=32)
                if assoc_data_present:
                    cipher.update(self.data_128)
                getattr(cipher, method1_name)(self.data_128)
                self.assertRaises(TypeError, getattr(cipher, method2_name),
                                  self.data_128) 
Example 36
Project: FUTU_Stop_Loss   Author: BigtoC   File: test_CCM.py    MIT License 5 votes vote down vote up
def test_invalid_encrypt_or_update_after_digest(self):
        for method_name in "encrypt", "update":
            cipher = AES.new(self.key_128, AES.MODE_CCM, 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_CCM, nonce=self.nonce_96)
            cipher.encrypt_and_digest(self.data_128) 
Example 37
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_loopback_128(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        pt = get_tag_random("plaintext", 16 * 100)
        ct = cipher.encrypt(pt)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        pt2 = cipher.decrypt(ct)
        self.assertEqual(pt, pt2) 
Example 38
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_nonce(self):
        # If not passed, the nonce is created randomly
        cipher = AES.new(self.key_128, AES.MODE_CCM)
        nonce1 = cipher.nonce
        cipher = AES.new(self.key_128, AES.MODE_CCM)
        nonce2 = cipher.nonce
        self.assertEqual(len(nonce1), 11)
        self.assertNotEqual(nonce1, nonce2)

        cipher = AES.new(self.key_128, AES.MODE_CCM, self.nonce_96)
        ct = cipher.encrypt(self.data_128)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertEquals(ct, cipher.encrypt(self.data_128)) 
Example 39
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_nonce_must_be_bytes(self):
        self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=u'test12345678') 
Example 40
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_nonce_length(self):
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=b"")
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=bchr(1) * 6)
        self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
                          nonce=bchr(1) * 14)
        for x in range(7, 13 + 1):
            AES.new(self.key_128, AES.MODE_CCM, nonce=bchr(1) * x) 
Example 41
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_block_size(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertEqual(cipher.block_size, AES.block_size) 
Example 42
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_unknown_parameters(self):
        self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
                          self.nonce_96, 7)
        self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
                          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_CCM, nonce=self.nonce_96,
                use_aesni=False) 
Example 43
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_null_encryption_decryption(self):
        for func in "encrypt", "decrypt":
            cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
            result = getattr(cipher, func)(b"")
            self.assertEqual(result, b"") 
Example 44
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_either_encrypt_or_decrypt(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.encrypt(b"")
        self.assertRaises(TypeError, cipher.decrypt, b"")

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.decrypt(b"")
        self.assertRaises(TypeError, cipher.encrypt, b"") 
Example 45
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_data_must_be_bytes(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*') 
Example 46
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_hex_mac(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        mac_hex = cipher.hexdigest()
        self.assertEqual(cipher.digest(), unhexlify(mac_hex))

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.hexverify(mac_hex) 
Example 47
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_longer_assoc_data_than_declared(self):
        # More than zero
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=0)
        self.assertRaises(ValueError, cipher.update, b"1")

        # Too large
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=15)
        self.assertRaises(ValueError, cipher.update, self.data_128) 
Example 48
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_shorter_assoc_data_than_expected(self):
        # With plaintext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.encrypt, self.data_128)

        # With empty plaintext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.digest)

        # With ciphertext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.decrypt, self.data_128)

        # With empty ciphertext
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        cipher.update(self.data_128)
        mac = cipher.digest()

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         assoc_len=17)
        cipher.update(self.data_128)
        self.assertRaises(ValueError, cipher.verify, mac) 
Example 49
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_shorter_and_longer_plaintext_than_declared(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=17)
        cipher.encrypt(self.data_128)
        self.assertRaises(ValueError, cipher.digest)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=15)
        self.assertRaises(ValueError, cipher.encrypt, self.data_128) 
Example 50
Project: NConf-Workshop-2019   Author: chaindrium-inc   File: test_CCM.py    GNU General Public License v3.0 5 votes vote down vote up
def test_shorter_ciphertext_than_declared(self):
        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
        ct, mac = cipher.encrypt_and_digest(self.data_128)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=17)
        cipher.decrypt(ct)
        self.assertRaises(ValueError, cipher.verify, mac)

        cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
                         msg_len=15)
        self.assertRaises(ValueError, cipher.decrypt, ct)