Browse Source
UDS: Stub SendTo to generate the unencrypted data frame with the right headers.
nce_cpp
UDS: Stub SendTo to generate the unencrypted data frame with the right headers.
nce_cpp
4 changed files with 261 additions and 1 deletions
-
2src/core/CMakeLists.txt
-
68src/core/hle/service/nwm/nwm_uds.cpp
-
112src/core/hle/service/nwm/uds_data.cpp
-
80src/core/hle/service/nwm/uds_data.h
@ -0,0 +1,112 @@ |
|||||
|
// Copyright 2017 Citra Emulator Project
|
||||
|
// Licensed under GPLv2 or any later version
|
||||
|
// Refer to the license.txt file included.
|
||||
|
|
||||
|
#include <cstring>
|
||||
|
|
||||
|
#include "core/hle/service/nwm/nwm_uds.h"
|
||||
|
#include "core/hle/service/nwm/uds_data.h"
|
||||
|
#include "core/hw/aes/key.h"
|
||||
|
|
||||
|
#include <cryptopp/aes.h>
|
||||
|
#include <cryptopp/md5.h>
|
||||
|
#include <cryptopp/modes.h>
|
||||
|
|
||||
|
namespace Service { |
||||
|
namespace NWM { |
||||
|
|
||||
|
// AES Keyslot used to generate the UDS data frame CCMP key.
|
||||
|
constexpr size_t UDSDataCryptoAESKeySlot = 0x2D; |
||||
|
|
||||
|
/*
|
||||
|
* Generates a SNAP-enabled 802.2 LLC header for the specified protocol. |
||||
|
* @returns a buffer with the bytes of the generated header. |
||||
|
*/ |
||||
|
static std::vector<u8> GenerateLLCHeader(EtherType protocol) { |
||||
|
LLCHeader header{}; |
||||
|
header.protocol = static_cast<u16>(protocol); |
||||
|
|
||||
|
std::vector<u8> buffer(sizeof(header)); |
||||
|
memcpy(buffer.data(), &header, sizeof(header)); |
||||
|
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
* Generates a Nintendo UDS SecureData header with the specified parameters. |
||||
|
* @returns a buffer with the bytes of the generated header. |
||||
|
*/ |
||||
|
static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 dest_node_id, |
||||
|
u16 src_node_id, u16 sequence_number) { |
||||
|
SecureDataHeader header{}; |
||||
|
header.protocol_size = data_size + sizeof(SecureDataHeader); |
||||
|
// TODO(Subv): It is likely that the first 4 bytes of this header are actually a decorator for another protocol.
|
||||
|
header.securedata_size = data_size + sizeof(SecureDataHeader) - 4; |
||||
|
header.is_management = 0; // Frames sent by the emulated application are never UDS management frames
|
||||
|
header.data_channel = channel; |
||||
|
header.sequence_number = sequence_number; |
||||
|
header.dest_node_id = dest_node_id; |
||||
|
header.src_node_id = src_node_id; |
||||
|
|
||||
|
std::vector<u8> buffer(sizeof(header)); |
||||
|
memcpy(buffer.data(), &header, sizeof(header)); |
||||
|
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
* Calculates the CTR used for the AES-CTR process that calculates |
||||
|
* the CCMP crypto key for data frames. |
||||
|
* @returns The CTR used for data frames crypto key generation. |
||||
|
*/ |
||||
|
static std::array<u8, CryptoPP::MD5::DIGESTSIZE> GetDataCryptoCTR(const NetworkInfo& network_info) { |
||||
|
DataFrameCryptoCTR data{}; |
||||
|
|
||||
|
data.host_mac = network_info.host_mac_address; |
||||
|
data.wlan_comm_id = network_info.wlan_comm_id; |
||||
|
data.id = network_info.id; |
||||
|
data.network_id = network_info.network_id; |
||||
|
|
||||
|
std::array<u8, CryptoPP::MD5::DIGESTSIZE> hash; |
||||
|
CryptoPP::MD5().CalculateDigest(hash.data(), reinterpret_cast<u8*>(&data), sizeof(data)); |
||||
|
|
||||
|
return hash; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
* Generates the key used for encrypting the 802.11 data frames generated by UDS. |
||||
|
* @returns The key used for data frames crypto. |
||||
|
*/ |
||||
|
static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey(const std::vector<u8>& passphrase, |
||||
|
const NetworkInfo& network_info) { |
||||
|
// Calculate the MD5 hash of the input passphrase.
|
||||
|
std::array<u8, CryptoPP::MD5::DIGESTSIZE> passphrase_hash; |
||||
|
CryptoPP::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(), passphrase.size()); |
||||
|
|
||||
|
std::array<u8, CryptoPP::AES::BLOCKSIZE> ccmp_key; |
||||
|
|
||||
|
// The CCMP key is the result of encrypting the MD5 hash of the passphrase with AES-CTR using keyslot 0x2D.
|
||||
|
using CryptoPP::AES; |
||||
|
std::array<u8, CryptoPP::MD5::DIGESTSIZE> counter = GetDataCryptoCTR(network_info); |
||||
|
std::array<u8, AES::BLOCKSIZE> key = HW::AES::GetNormalKey(UDSDataCryptoAESKeySlot); |
||||
|
CryptoPP::CTR_Mode<AES>::Encryption aes; |
||||
|
aes.SetKeyWithIV(key.data(), AES::BLOCKSIZE, counter.data()); |
||||
|
aes.ProcessData(ccmp_key.data(), passphrase_hash.data(), passphrase_hash.size()); |
||||
|
|
||||
|
return ccmp_key; |
||||
|
} |
||||
|
|
||||
|
std::vector<u8> GenerateDataFrame(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, u16 sequence_number) { |
||||
|
std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData); |
||||
|
std::vector<u8> securedata_header = GenerateSecureDataHeader(data.size(), channel, dest_node, src_node, sequence_number); |
||||
|
|
||||
|
buffer.insert(buffer.end(), securedata_header.begin(), securedata_header.end()); |
||||
|
buffer.insert(buffer.end(), data.begin(), data.end()); |
||||
|
// TODO(Subv): Encrypt the frame.
|
||||
|
// TODO(Subv): Prepend CCMP initialization vector (sequence_number).
|
||||
|
// TODO(Subv): Encapsulate the frame in an 802.11 data frame.
|
||||
|
return buffer; |
||||
|
} |
||||
|
|
||||
|
} // namespace NWM
|
||||
|
} // namespace Service
|
||||
@ -0,0 +1,80 @@ |
|||||
|
// Copyright 2017 Citra Emulator Project |
||||
|
// Licensed under GPLv2 or any later version |
||||
|
// Refer to the license.txt file included. |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
#include "common/common_types.h" |
||||
|
#include "common/swap.h" |
||||
|
#include "core/hle/service/service.h" |
||||
|
|
||||
|
namespace Service { |
||||
|
namespace NWM { |
||||
|
|
||||
|
enum class SAP : u8 { |
||||
|
SNAPExtensionUsed = 0xAA |
||||
|
}; |
||||
|
|
||||
|
enum class PDUControl : u8 { |
||||
|
UnnumberedInformation = 3 |
||||
|
}; |
||||
|
|
||||
|
enum class EtherType : u16 { |
||||
|
SecureData = 0x876D, |
||||
|
EAPoL = 0x888E |
||||
|
}; |
||||
|
|
||||
|
/* |
||||
|
* 802.2 header, UDS packets always use SNAP for these headers, |
||||
|
* which means the dsap and ssap are always SNAPExtensionUsed (0xAA) |
||||
|
* and the OUI is always 0. |
||||
|
*/ |
||||
|
struct LLCHeader { |
||||
|
u8 dsap = static_cast<u8>(SAP::SNAPExtensionUsed); |
||||
|
u8 ssap = static_cast<u8>(SAP::SNAPExtensionUsed); |
||||
|
u8 control = static_cast<u8>(PDUControl::UnnumberedInformation); |
||||
|
std::array<u8, 3> OUI = {}; |
||||
|
u16_be protocol; |
||||
|
}; |
||||
|
|
||||
|
static_assert(sizeof(LLCHeader) == 8, "LLCHeader has the wrong size"); |
||||
|
|
||||
|
/* |
||||
|
* Nintendo SecureData header, every UDS packet contains one, |
||||
|
* it is used to store metadata about the transmission such as |
||||
|
* the source and destination network node ids. |
||||
|
*/ |
||||
|
struct SecureDataHeader { |
||||
|
u16_be protocol_size; |
||||
|
INSERT_PADDING_BYTES(2); |
||||
|
u16_be securedata_size; |
||||
|
u8 is_management; |
||||
|
u8 data_channel; |
||||
|
u16_be sequence_number; |
||||
|
u16_be dest_node_id; |
||||
|
u16_be src_node_id; |
||||
|
}; |
||||
|
|
||||
|
static_assert(sizeof(SecureDataHeader) == 14, "SecureDataHeader has the wrong size"); |
||||
|
|
||||
|
/* |
||||
|
* The raw bytes of this structure are the CTR used in the encryption (AES-CTR) |
||||
|
* process used to generate the CCMP key for data frame encryption. |
||||
|
*/ |
||||
|
struct DataFrameCryptoCTR { |
||||
|
u32_le wlan_comm_id; |
||||
|
u32_le network_id; |
||||
|
std::array<u8, 6> host_mac; |
||||
|
u16_le id; |
||||
|
}; |
||||
|
|
||||
|
static_assert(sizeof(DataFrameCryptoCTR) == 16, "DataFrameCryptoCTR has the wrong size"); |
||||
|
|
||||
|
/** |
||||
|
* Generates an encrypted 802.11 data frame starting at the CCMP IV. |
||||
|
* @returns The generated frame. |
||||
|
*/ |
||||
|
std::vector<u8> GenerateDataFrame(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, u16 sequence_number); |
||||
|
|
||||
|
} // namespace NWM |
||||
|
} // namespace Service |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue