Beispiel #1
0
    def test_raises_after_finalize(self, backend):
        key = b"2b7e151628aed2a6abf7158809cf4f3c"
        cmac = CMAC(AES(key), backend)
        cmac.finalize()

        with pytest.raises(AlreadyFinalized):
            cmac.update(b"foo")

        with pytest.raises(AlreadyFinalized):
            cmac.copy()

        with pytest.raises(AlreadyFinalized):
            cmac.finalize()
Beispiel #2
0
    def test_raises_after_finalize(self, backend):
        key = b"2b7e151628aed2a6abf7158809cf4f3c"
        cmac = CMAC(AES(key), backend)
        cmac.finalize()

        with pytest.raises(AlreadyFinalized):
            cmac.update(b"foo")

        with pytest.raises(AlreadyFinalized):
            cmac.copy()

        with pytest.raises(AlreadyFinalized):
            cmac.finalize()
Beispiel #3
0
 def test_buffer_protocol(self, backend):
     key = bytearray(b"2b7e151628aed2a6abf7158809cf4f3c")
     cmac = CMAC(AES(key), backend)
     cmac.update(b"6bc1bee22e409f96e93d7e117393172a")
     assert cmac.finalize() == binascii.unhexlify(
         b"a21e6e647bfeaf5ca0a5e1bcd957dfad"
     )
Beispiel #4
0
    def test_aes_generate(self, backend, params):
        key = params["key"]
        message = params["message"]
        output = params["output"]

        cmac = CMAC(AES(binascii.unhexlify(key)), backend)
        cmac.update(binascii.unhexlify(message))
        assert binascii.hexlify(cmac.finalize()) == output
Beispiel #5
0
    def test_aes_generate(self, backend, params):
        key = params["key"]
        message = params["message"]
        output = params["output"]

        cmac = CMAC(AES(binascii.unhexlify(key)), backend)
        cmac.update(binascii.unhexlify(message))
        assert binascii.hexlify(cmac.finalize()) == output
Beispiel #6
0
    def test_3des_generate(self, backend, params):
        key1 = params["key1"]
        key2 = params["key2"]
        key3 = params["key3"]

        key = key1 + key2 + key3

        message = params["message"]
        output = params["output"]

        cmac = CMAC(TripleDES(binascii.unhexlify(key)), backend)
        cmac.update(binascii.unhexlify(message))
        assert binascii.hexlify(cmac.finalize()) == output
Beispiel #7
0
    def test_3des_generate(self, backend, params):
        key1 = params["key1"]
        key2 = params["key2"]
        key3 = params["key3"]

        key = key1 + key2 + key3

        message = params["message"]
        output = params["output"]

        cmac = CMAC(TripleDES(binascii.unhexlify(key)), backend)
        cmac.update(binascii.unhexlify(message))
        assert binascii.hexlify(cmac.finalize()) == output
Beispiel #8
0
class _KSP1_encryptor:
    """
    DO NOT CALL
    
    Use KSP1().encryptor() instead
    """
    def __init__(self, key, nonce, mac_key):
        self._aes = Cipher(algorithms.AES(key), modes.CTR(nonce),
                           default_backend()).encryptor()
        self._mac = CMAC(algorithms.AES(mac_key), default_backend())

    def update(self, data):
        data = self._aes.update(data)
        self._mac.update(data)
        return data

    def finalize(self):
        return self._mac.finalize()
Beispiel #9
0
def mac(key, msg):
    """
    Default MAC function (CMAC using AES-128).

    Args:
        key: key for MAC creation.
        msg: Plaintext to be MACed, as a bytes object.

    Returns:
        MAC output, as a bytes object.

    Raises:
        ValueError: An error occurred when key is NULL or ciphertext is NULL.
    """
    if key is None:
        raise ValueError('Key is NULL.')
    elif msg is None:
        raise ValueError('Message is NULL.')
    else:
        cobj = CMAC(AES(key), backend=default_backend())
        cobj.update(msg)
        return cobj.finalize()
Beispiel #10
0
 def test_copy_with_backend(self, backend):
     key = b"2b7e151628aed2a6abf7158809cf4f3c"
     cmac = CMAC(AES(key), backend)
     cmac.update(b"6bc1bee22e409f96e93d7e117393172a")
     copy_cmac = cmac.copy()
     assert cmac.finalize() == copy_cmac.finalize()
Beispiel #11
0
 def test_copy_with_backend(self, backend):
     key = b"2b7e151628aed2a6abf7158809cf4f3c"
     cmac = CMAC(AES(key), backend)
     cmac.update(b"6bc1bee22e409f96e93d7e117393172a")
     copy_cmac = cmac.copy()
     assert cmac.finalize() == copy_cmac.finalize()
Beispiel #12
0
def aes_cmac(key, data):
    backend = default_backend()
    cmac = CMAC(algorithms.AES(key), backend=backend)
    cmac.update(data)
    return cmac.finalize()