Beispiel #1
0
    def test_should_create_key_and_nonce_and_auth_data_when_create_key_and_nonce_and_auth_data_is_called(
            self):
        # GIVEN
        message_info = common.MessageInfo()
        message_info.source_mac_address = common.MacAddress.from_eui64(
            any_eui64())

        message_info.source_ipv6 = any_ip_address()
        message_info.destination_ipv6 = any_ip_address()

        message_info.aux_sec_hdr = any_auxiliary_security_header()
        message_info.aux_sec_hdr_bytes = convert_aux_sec_hdr_to_bytearray(
            message_info.aux_sec_hdr)

        creator = net_crypto.MleCryptoMaterialCreator(master_key)

        # WHEN
        key, nonce, auth_data = creator.create_key_and_nonce_and_authenticated_data(
            message_info)

        # THEN
        self.assertEqual(
            message_info.source_mac_address.mac_address +
            struct.pack(">LB", message_info.aux_sec_hdr.frame_counter,
                        message_info.aux_sec_hdr.security_level), nonce)

        self.assertEqual(
            message_info.source_ipv6.packed +
            message_info.destination_ipv6.packed +
            message_info.aux_sec_hdr_bytes, auth_data)
Beispiel #2
0
    def test_should_create_authenticated_data_when_create_authenticated_data_method_is_called(
            self):
        """
        Only Key id mode 2.
        Length of the Auxiliary Security Header is constantly equal 10.
        """

        # GIVEN
        source_address = any_ip_address()
        destination_address = any_ip_address()
        auxiliary_security_header_bytes = convert_aux_sec_hdr_to_bytearray(
            any_auxiliary_security_header())

        creator = net_crypto.MleCryptoMaterialCreator(master_key)

        # WHEN
        authenticated_data = creator._create_authenticated_data(
            source_address, destination_address,
            auxiliary_security_header_bytes)

        # THEN
        authenticated_data_bytes = io.BytesIO(authenticated_data)

        self.assertEqual(source_address.packed,
                         authenticated_data_bytes.read(16))
        self.assertEqual(destination_address.packed,
                         authenticated_data_bytes.read(16))
        self.assertEqual(auxiliary_security_header_bytes,
                         authenticated_data_bytes.read(10))
Beispiel #3
0
    def test_should_encrypt_and_decrypt_random_data_content_when_proper_methods_are_called(
            self):
        # GIVEN
        data = any_data()

        master_key = any_master_key()

        key_id_mode = 2
        security_level = 5

        message_info = common.MessageInfo()
        message_info.source_mac_address = common.MacAddress.from_eui64(
            any_eui64())

        message_info.source_ipv6 = any_ip_address()
        message_info.destination_ipv6 = any_ip_address()

        message_info.aux_sec_hdr = net_crypto.AuxiliarySecurityHeader(
            key_id_mode=key_id_mode,
            security_level=security_level,
            frame_counter=any_frame_counter(),
            key_id=any_key_id(key_id_mode))
        message_info.aux_sec_hdr_bytes = convert_aux_sec_hdr_to_bytearray(
            message_info.aux_sec_hdr)

        net_crypto_engine = net_crypto.CryptoEngine(
            net_crypto.MleCryptoMaterialCreator(master_key))

        # WHEN
        enc_data, mic = net_crypto_engine.encrypt(data, message_info)
        dec_data = net_crypto_engine.decrypt(enc_data, mic, message_info)

        # THEN
        self.assertEqual(data, dec_data)
Beispiel #4
0
    def test_should_create_nonce_when_create_nonce_method_is_called(self):
        # GIVEN
        source_eui64 = any_eui64()
        frame_counter = any_frame_counter()
        security_level = any_security_level()

        creator = net_crypto.MleCryptoMaterialCreator(master_key)

        # WHEN
        nonce = creator._create_nonce(source_eui64, frame_counter,
                                      security_level)

        # THEN
        nonce_bytes = io.BytesIO(nonce)

        self.assertEqual(source_eui64, nonce_bytes.read(8))
        self.assertEqual(struct.pack(">L", frame_counter), nonce_bytes.read(4))
        self.assertEqual(security_level, ord(nonce_bytes.read(1)))
Beispiel #5
0
    def test_should_decrypt_bytearray_to_mle_message_when_decrypt_method_is_called(
            self):
        # GIVEN
        message_info = common.MessageInfo()
        message_info.source_mac_address = common.MacAddress.from_eui64(
            bytearray([0x00, 0x35, 0xcc, 0x94, 0xd7, 0x7a, 0x07, 0xe8]))

        message_info.source_ipv6 = "fe80::235:cc94:d77a:07e8"
        message_info.destination_ipv6 = "ff02::2"

        message_info.aux_sec_hdr = net_crypto.AuxiliarySecurityHeader(
            key_id_mode=2,
            security_level=5,
            frame_counter=262165,
            key_id=bytearray([0x00, 0x00, 0x00, 0x00, 0x01]))
        message_info.aux_sec_hdr_bytes = convert_aux_sec_hdr_to_bytearray(
            message_info.aux_sec_hdr)

        data = bytearray([
            0x9a, 0x5a, 0x9a, 0x5b, 0xba, 0x25, 0x9c, 0x5e, 0x58, 0xa2, 0x7e,
            0x75, 0x74, 0xef, 0x79, 0xbc, 0x4f, 0xa3, 0xf9, 0xae, 0xa8, 0x34,
            0xf6, 0xf2, 0x37, 0x21, 0x93, 0x60
        ])

        mic = bytearray([0xe1, 0xb5, 0xa2, 0x53])

        net_crypto_engine = net_crypto.CryptoEngine(
            net_crypto.MleCryptoMaterialCreator(master_key))

        # WHEN
        mle_msg = net_crypto_engine.decrypt(data, mic, message_info)

        # THEN
        expected_mle_msg = bytearray([
            0x04, 0x00, 0x02, 0x00, 0x00, 0x09, 0x0b, 0x8f, 0x80, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x40, 0x00, 0x01, 0xf1, 0x0b, 0x08, 0x65, 0x5e,
            0x0f, 0x83, 0x40, 0xc7, 0x83, 0x31
        ])
        self.assertEqual(expected_mle_msg, mle_msg)
Beispiel #6
0
def create_default_mle_crypto_engine(master_key):
    return net_crypto.CryptoEngine(crypto_material_creator=net_crypto.MleCryptoMaterialCreator(master_key))