diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/hle/service/nwm/nwm_uds.cpp | 7 | ||||
| -rw-r--r-- | src/core/hle/service/nwm/uds_data.cpp | 80 | ||||
| -rw-r--r-- | src/core/hle/service/nwm/uds_data.h | 19 |
3 files changed, 55 insertions, 51 deletions
diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp index d9bd9c4a4..35fa1cd77 100644 --- a/src/core/hle/service/nwm/nwm_uds.cpp +++ b/src/core/hle/service/nwm/nwm_uds.cpp | |||
| @@ -433,9 +433,8 @@ static void SendTo(Interface* self) { | |||
| 433 | 433 | ||
| 434 | // TODO(Subv): Increment the sequence number after each sent packet. | 434 | // TODO(Subv): Increment the sequence number after each sent packet. |
| 435 | u16 sequence_number = 0; | 435 | u16 sequence_number = 0; |
| 436 | std::vector<u8> data_payload = GenerateDataPayload(data, data_channel, dest_node_id, | 436 | std::vector<u8> data_payload = GenerateDataPayload( |
| 437 | connection_status.network_node_id, | 437 | data, data_channel, dest_node_id, connection_status.network_node_id, sequence_number); |
| 438 | sequence_number); | ||
| 439 | 438 | ||
| 440 | // TODO(Subv): Retrieve the MAC address of the dest_node_id and our own to encrypt | 439 | // TODO(Subv): Retrieve the MAC address of the dest_node_id and our own to encrypt |
| 441 | // and encapsulate the payload. | 440 | // and encapsulate the payload. |
| @@ -640,7 +639,7 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
| 640 | {0x00130040, nullptr, "Unbind"}, | 639 | {0x00130040, nullptr, "Unbind"}, |
| 641 | {0x001400C0, nullptr, "PullPacket"}, | 640 | {0x001400C0, nullptr, "PullPacket"}, |
| 642 | {0x00150080, nullptr, "SetMaxSendDelay"}, | 641 | {0x00150080, nullptr, "SetMaxSendDelay"}, |
| 643 | {0x00170182, SendTo, "SendTo"}, | 642 | {0x00170182, SendTo, "SendTo"}, |
| 644 | {0x001A0000, GetChannel, "GetChannel"}, | 643 | {0x001A0000, GetChannel, "GetChannel"}, |
| 645 | {0x001B0302, InitializeWithVersion, "InitializeWithVersion"}, | 644 | {0x001B0302, InitializeWithVersion, "InitializeWithVersion"}, |
| 646 | {0x001D0044, BeginHostingNetwork, "BeginHostingNetwork"}, | 645 | {0x001D0044, BeginHostingNetwork, "BeginHostingNetwork"}, |
diff --git a/src/core/hle/service/nwm/uds_data.cpp b/src/core/hle/service/nwm/uds_data.cpp index e05ca8815..fabdf67a8 100644 --- a/src/core/hle/service/nwm/uds_data.cpp +++ b/src/core/hle/service/nwm/uds_data.cpp | |||
| @@ -3,20 +3,20 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <cstring> | 5 | #include <cstring> |
| 6 | 6 | #include <cryptopp/aes.h> | |
| 7 | #include "core/hle/service/nwm/nwm_uds.h" | ||
| 8 | #include "core/hle/service/nwm/uds_beacon.h" | ||
| 9 | #include "core/hle/service/nwm/uds_data.h" | ||
| 10 | #include "core/hw/aes/key.h" | ||
| 11 | |||
| 12 | #include <cryptopp/ccm.h> | 7 | #include <cryptopp/ccm.h> |
| 13 | #include <cryptopp/filters.h> | 8 | #include <cryptopp/filters.h> |
| 14 | #include <cryptopp/md5.h> | 9 | #include <cryptopp/md5.h> |
| 15 | #include <cryptopp/modes.h> | 10 | #include <cryptopp/modes.h> |
| 11 | #include "core/hle/service/nwm/nwm_uds.h" | ||
| 12 | #include "core/hle/service/nwm/uds_data.h" | ||
| 13 | #include "core/hw/aes/key.h" | ||
| 16 | 14 | ||
| 17 | namespace Service { | 15 | namespace Service { |
| 18 | namespace NWM { | 16 | namespace NWM { |
| 19 | 17 | ||
| 18 | using MacAddress = std::array<u8, 6>; | ||
| 19 | |||
| 20 | // AES Keyslot used to generate the UDS data frame CCMP key. | 20 | // AES Keyslot used to generate the UDS data frame CCMP key. |
| 21 | constexpr size_t UDSDataCryptoAESKeySlot = 0x2D; | 21 | constexpr size_t UDSDataCryptoAESKeySlot = 0x2D; |
| 22 | 22 | ||
| @@ -39,14 +39,15 @@ static std::vector<u8> GenerateLLCHeader(EtherType protocol) { | |||
| 39 | * @returns a buffer with the bytes of the generated header. | 39 | * @returns a buffer with the bytes of the generated header. |
| 40 | */ | 40 | */ |
| 41 | static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 dest_node_id, | 41 | static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 dest_node_id, |
| 42 | u16 src_node_id, u16 sequence_number) { | 42 | u16 src_node_id, u16 sequence_number) { |
| 43 | SecureDataHeader header{}; | 43 | SecureDataHeader header{}; |
| 44 | header.protocol_size = data_size + sizeof(SecureDataHeader); | 44 | header.protocol_size = data_size + sizeof(SecureDataHeader); |
| 45 | // Note: This size includes everything except the first 4 bytes of the structure, | 45 | // Note: This size includes everything except the first 4 bytes of the structure, |
| 46 | // reinforcing the hypotheses that the first 4 bytes are actually the header of | 46 | // reinforcing the hypotheses that the first 4 bytes are actually the header of |
| 47 | // another container protocol. | 47 | // another container protocol. |
| 48 | header.securedata_size = data_size + sizeof(SecureDataHeader) - 4; | 48 | header.securedata_size = data_size + sizeof(SecureDataHeader) - 4; |
| 49 | header.is_management = 0; // Frames sent by the emulated application are never UDS management frames | 49 | // Frames sent by the emulated application are never UDS management frames |
| 50 | header.is_management = 0; | ||
| 50 | header.data_channel = channel; | 51 | header.data_channel = channel; |
| 51 | header.sequence_number = sequence_number; | 52 | header.sequence_number = sequence_number; |
| 52 | header.dest_node_id = dest_node_id; | 53 | header.dest_node_id = dest_node_id; |
| @@ -60,7 +61,7 @@ static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 d | |||
| 60 | 61 | ||
| 61 | /* | 62 | /* |
| 62 | * Calculates the CTR used for the AES-CTR process that calculates | 63 | * Calculates the CTR used for the AES-CTR process that calculates |
| 63 | * the CCMP crypto key for data frames. | 64 | * the CCMP crypto key for data frames. |
| 64 | * @returns The CTR used for data frames crypto key generation. | 65 | * @returns The CTR used for data frames crypto key generation. |
| 65 | */ | 66 | */ |
| 66 | static std::array<u8, CryptoPP::MD5::DIGESTSIZE> GetDataCryptoCTR(const NetworkInfo& network_info) { | 67 | static std::array<u8, CryptoPP::MD5::DIGESTSIZE> GetDataCryptoCTR(const NetworkInfo& network_info) { |
| @@ -81,15 +82,16 @@ static std::array<u8, CryptoPP::MD5::DIGESTSIZE> GetDataCryptoCTR(const NetworkI | |||
| 81 | * Generates the key used for encrypting the 802.11 data frames generated by UDS. | 82 | * Generates the key used for encrypting the 802.11 data frames generated by UDS. |
| 82 | * @returns The key used for data frames crypto. | 83 | * @returns The key used for data frames crypto. |
| 83 | */ | 84 | */ |
| 84 | static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey(const std::vector<u8>& passphrase, | 85 | static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey( |
| 85 | const NetworkInfo& network_info) { | 86 | const std::vector<u8>& passphrase, const NetworkInfo& network_info) { |
| 86 | // Calculate the MD5 hash of the input passphrase. | 87 | // Calculate the MD5 hash of the input passphrase. |
| 87 | std::array<u8, CryptoPP::MD5::DIGESTSIZE> passphrase_hash; | 88 | std::array<u8, CryptoPP::MD5::DIGESTSIZE> passphrase_hash; |
| 88 | CryptoPP::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(), passphrase.size()); | 89 | CryptoPP::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(), passphrase.size()); |
| 89 | 90 | ||
| 90 | std::array<u8, CryptoPP::AES::BLOCKSIZE> ccmp_key; | 91 | std::array<u8, CryptoPP::AES::BLOCKSIZE> ccmp_key; |
| 91 | 92 | ||
| 92 | // The CCMP key is the result of encrypting the MD5 hash of the passphrase with AES-CTR using keyslot 0x2D. | 93 | // The CCMP key is the result of encrypting the MD5 hash of the passphrase with AES-CTR using |
| 94 | // keyslot 0x2D. | ||
| 93 | using CryptoPP::AES; | 95 | using CryptoPP::AES; |
| 94 | std::array<u8, CryptoPP::MD5::DIGESTSIZE> counter = GetDataCryptoCTR(network_info); | 96 | std::array<u8, CryptoPP::MD5::DIGESTSIZE> counter = GetDataCryptoCTR(network_info); |
| 95 | std::array<u8, AES::BLOCKSIZE> key = HW::AES::GetNormalKey(UDSDataCryptoAESKeySlot); | 97 | std::array<u8, AES::BLOCKSIZE> key = HW::AES::GetNormalKey(UDSDataCryptoAESKeySlot); |
| @@ -139,21 +141,26 @@ static std::vector<u8> GenerateCCMPAAD(const MacAddress& sender, const MacAddres | |||
| 139 | * Decrypts the payload of an encrypted 802.11 data frame using the specified key. | 141 | * Decrypts the payload of an encrypted 802.11 data frame using the specified key. |
| 140 | * @returns The decrypted payload. | 142 | * @returns The decrypted payload. |
| 141 | */ | 143 | */ |
| 142 | static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, | 144 | static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload, |
| 143 | const MacAddress& sender, const MacAddress& receiver, u16 sequence_number) { | 145 | const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, |
| 146 | const MacAddress& sender, const MacAddress& receiver, | ||
| 147 | u16 sequence_number) { | ||
| 144 | 148 | ||
| 145 | // Reference: IEEE 802.11-2007 | 149 | // Reference: IEEE 802.11-2007 |
| 146 | 150 | ||
| 147 | std::vector<u8> aad = GenerateCCMPAAD(sender, receiver); | 151 | std::vector<u8> aad = GenerateCCMPAAD(sender, receiver); |
| 148 | 152 | ||
| 149 | std::vector<u8> packet_number{0, 0, 0, 0, | 153 | std::vector<u8> packet_number{0, |
| 154 | 0, | ||
| 155 | 0, | ||
| 156 | 0, | ||
| 150 | static_cast<u8>((sequence_number >> 8) & 0xFF), | 157 | static_cast<u8>((sequence_number >> 8) & 0xFF), |
| 151 | static_cast<u8>(sequence_number & 0xFF)}; | 158 | static_cast<u8>(sequence_number & 0xFF)}; |
| 152 | 159 | ||
| 153 | // 8.3.3.3.3 Construct CCM nonce (13 bytes) | 160 | // 8.3.3.3.3 Construct CCM nonce (13 bytes) |
| 154 | std::vector<u8> nonce; | 161 | std::vector<u8> nonce; |
| 155 | nonce.push_back(0); // priority | 162 | nonce.push_back(0); // priority |
| 156 | nonce.insert(nonce.end(), sender.begin(), sender.end()); // Address 2 | 163 | nonce.insert(nonce.end(), sender.begin(), sender.end()); // Address 2 |
| 157 | nonce.insert(nonce.end(), packet_number.begin(), packet_number.end()); // PN | 164 | nonce.insert(nonce.end(), packet_number.begin(), packet_number.end()); // PN |
| 158 | 165 | ||
| 159 | try { | 166 | try { |
| @@ -161,15 +168,17 @@ static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload | |||
| 161 | d.SetKeyWithIV(ccmp_key.data(), ccmp_key.size(), nonce.data(), nonce.size()); | 168 | d.SetKeyWithIV(ccmp_key.data(), ccmp_key.size(), nonce.data(), nonce.size()); |
| 162 | d.SpecifyDataLengths(aad.size(), encrypted_payload.size() - 8, 0); | 169 | d.SpecifyDataLengths(aad.size(), encrypted_payload.size() - 8, 0); |
| 163 | 170 | ||
| 164 | CryptoPP::AuthenticatedDecryptionFilter df(d, nullptr, | 171 | CryptoPP::AuthenticatedDecryptionFilter df( |
| 165 | CryptoPP::AuthenticatedDecryptionFilter::MAC_AT_END | | 172 | d, nullptr, CryptoPP::AuthenticatedDecryptionFilter::MAC_AT_END | |
| 166 | CryptoPP::AuthenticatedDecryptionFilter::THROW_EXCEPTION); | 173 | CryptoPP::AuthenticatedDecryptionFilter::THROW_EXCEPTION); |
| 167 | // put aad | 174 | // put aad |
| 168 | df.ChannelPut(CryptoPP::AAD_CHANNEL, aad.data(), aad.size()); | 175 | df.ChannelPut(CryptoPP::AAD_CHANNEL, aad.data(), aad.size()); |
| 169 | 176 | ||
| 170 | // put cipher with mac | 177 | // put cipher with mac |
| 171 | df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, encrypted_payload.data(), encrypted_payload.size() - 8); | 178 | df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, encrypted_payload.data(), |
| 172 | df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, encrypted_payload.data() + encrypted_payload.size() - 8, 8); | 179 | encrypted_payload.size() - 8); |
| 180 | df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, | ||
| 181 | encrypted_payload.data() + encrypted_payload.size() - 8, 8); | ||
| 173 | 182 | ||
| 174 | df.ChannelMessageEnd(CryptoPP::AAD_CHANNEL); | 183 | df.ChannelMessageEnd(CryptoPP::AAD_CHANNEL); |
| 175 | df.ChannelMessageEnd(CryptoPP::DEFAULT_CHANNEL); | 184 | df.ChannelMessageEnd(CryptoPP::DEFAULT_CHANNEL); |
| @@ -191,20 +200,25 @@ static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload | |||
| 191 | * Encrypts the payload of an 802.11 data frame using the specified key. | 200 | * Encrypts the payload of an 802.11 data frame using the specified key. |
| 192 | * @returns The encrypted payload. | 201 | * @returns The encrypted payload. |
| 193 | */ | 202 | */ |
| 194 | static std::vector<u8> EncryptDataFrame(const std::vector<u8>& payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, | 203 | static std::vector<u8> EncryptDataFrame(const std::vector<u8>& payload, |
| 195 | const MacAddress& sender, const MacAddress& receiver, u16 sequence_number) { | 204 | const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, |
| 205 | const MacAddress& sender, const MacAddress& receiver, | ||
| 206 | u16 sequence_number) { | ||
| 196 | // Reference: IEEE 802.11-2007 | 207 | // Reference: IEEE 802.11-2007 |
| 197 | 208 | ||
| 198 | std::vector<u8> aad = GenerateCCMPAAD(sender, receiver); | 209 | std::vector<u8> aad = GenerateCCMPAAD(sender, receiver); |
| 199 | 210 | ||
| 200 | std::vector<u8> packet_number{0, 0, 0, 0, | 211 | std::vector<u8> packet_number{0, |
| 201 | static_cast<u8>((sequence_number >> 8) & 0xFF), | 212 | 0, |
| 202 | static_cast<u8>(sequence_number & 0xFF)}; | 213 | 0, |
| 214 | 0, | ||
| 215 | static_cast<u8>((sequence_number >> 8) & 0xFF), | ||
| 216 | static_cast<u8>(sequence_number & 0xFF)}; | ||
| 203 | 217 | ||
| 204 | // 8.3.3.3.3 Construct CCM nonce (13 bytes) | 218 | // 8.3.3.3.3 Construct CCM nonce (13 bytes) |
| 205 | std::vector<u8> nonce; | 219 | std::vector<u8> nonce; |
| 206 | nonce.push_back(0); // priority | 220 | nonce.push_back(0); // priority |
| 207 | nonce.insert(nonce.end(), sender.begin(), sender.end()); // Address 2 | 221 | nonce.insert(nonce.end(), sender.begin(), sender.end()); // Address 2 |
| 208 | nonce.insert(nonce.end(), packet_number.begin(), packet_number.end()); // PN | 222 | nonce.insert(nonce.end(), packet_number.begin(), packet_number.end()); // PN |
| 209 | 223 | ||
| 210 | try { | 224 | try { |
| @@ -235,11 +249,11 @@ static std::vector<u8> EncryptDataFrame(const std::vector<u8>& payload, const st | |||
| 235 | return {}; | 249 | return {}; |
| 236 | } | 250 | } |
| 237 | 251 | ||
| 238 | std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, | 252 | std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, |
| 239 | u16 sequence_number) { | 253 | u16 src_node, u16 sequence_number) { |
| 240 | std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData); | 254 | std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData); |
| 241 | std::vector<u8> securedata_header = GenerateSecureDataHeader(data.size(), channel, dest_node, src_node, | 255 | std::vector<u8> securedata_header = |
| 242 | sequence_number); | 256 | GenerateSecureDataHeader(data.size(), channel, dest_node, src_node, sequence_number); |
| 243 | 257 | ||
| 244 | buffer.insert(buffer.end(), securedata_header.begin(), securedata_header.end()); | 258 | buffer.insert(buffer.end(), securedata_header.begin(), securedata_header.end()); |
| 245 | buffer.insert(buffer.end(), data.begin(), data.end()); | 259 | buffer.insert(buffer.end(), data.begin(), data.end()); |
diff --git a/src/core/hle/service/nwm/uds_data.h b/src/core/hle/service/nwm/uds_data.h index 960f13cee..a23520a41 100644 --- a/src/core/hle/service/nwm/uds_data.h +++ b/src/core/hle/service/nwm/uds_data.h | |||
| @@ -6,28 +6,18 @@ | |||
| 6 | 6 | ||
| 7 | #include <array> | 7 | #include <array> |
| 8 | #include <vector> | 8 | #include <vector> |
| 9 | |||
| 10 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 11 | #include "common/swap.h" | 10 | #include "common/swap.h" |
| 12 | #include "core/hle/service/service.h" | 11 | #include "core/hle/service/service.h" |
| 13 | 12 | ||
| 14 | #include <cryptopp/aes.h> | ||
| 15 | |||
| 16 | namespace Service { | 13 | namespace Service { |
| 17 | namespace NWM { | 14 | namespace NWM { |
| 18 | 15 | ||
| 19 | enum class SAP : u8 { | 16 | enum class SAP : u8 { SNAPExtensionUsed = 0xAA }; |
| 20 | SNAPExtensionUsed = 0xAA | ||
| 21 | }; | ||
| 22 | 17 | ||
| 23 | enum class PDUControl : u8 { | 18 | enum class PDUControl : u8 { UnnumberedInformation = 3 }; |
| 24 | UnnumberedInformation = 3 | ||
| 25 | }; | ||
| 26 | 19 | ||
| 27 | enum class EtherType : u16 { | 20 | enum class EtherType : u16 { SecureData = 0x876D, EAPoL = 0x888E }; |
| 28 | SecureData = 0x876D, | ||
| 29 | EAPoL = 0x888E | ||
| 30 | }; | ||
| 31 | 21 | ||
| 32 | /* | 22 | /* |
| 33 | * 802.2 header, UDS packets always use SNAP for these headers, | 23 | * 802.2 header, UDS packets always use SNAP for these headers, |
| @@ -81,7 +71,8 @@ static_assert(sizeof(DataFrameCryptoCTR) == 16, "DataFrameCryptoCTR has the wron | |||
| 81 | * Generates an unencrypted 802.11 data payload. | 71 | * Generates an unencrypted 802.11 data payload. |
| 82 | * @returns The generated frame payload. | 72 | * @returns The generated frame payload. |
| 83 | */ | 73 | */ |
| 84 | std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, u16 sequence_number); | 74 | std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, |
| 75 | u16 src_node, u16 sequence_number); | ||
| 85 | 76 | ||
| 86 | } // namespace NWM | 77 | } // namespace NWM |
| 87 | } // namespace Service | 78 | } // namespace Service |