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)
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)
def create_default_mle_crypto_engine(master_key): return net_crypto.CryptoEngine(crypto_material_creator=net_crypto. MleCryptoMaterialCreator(master_key))