1
0
Fork 0
forked from suyu/suyu

UDS: Run clang-format.

This commit is contained in:
Subv 2017-06-14 13:18:58 -05:00
parent 9befb8c887
commit 87168bfe8b
3 changed files with 55 additions and 51 deletions

View file

@ -433,9 +433,8 @@ static void SendTo(Interface* self) {
// TODO(Subv): Increment the sequence number after each sent packet. // TODO(Subv): Increment the sequence number after each sent packet.
u16 sequence_number = 0; u16 sequence_number = 0;
std::vector<u8> data_payload = GenerateDataPayload(data, data_channel, dest_node_id, std::vector<u8> data_payload = GenerateDataPayload(
connection_status.network_node_id, data, data_channel, dest_node_id, connection_status.network_node_id, sequence_number);
sequence_number);
// TODO(Subv): Retrieve the MAC address of the dest_node_id and our own to encrypt // TODO(Subv): Retrieve the MAC address of the dest_node_id and our own to encrypt
// and encapsulate the payload. // and encapsulate the payload.

View file

@ -3,20 +3,20 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <cstring> #include <cstring>
#include <cryptopp/aes.h>
#include "core/hle/service/nwm/nwm_uds.h"
#include "core/hle/service/nwm/uds_beacon.h"
#include "core/hle/service/nwm/uds_data.h"
#include "core/hw/aes/key.h"
#include <cryptopp/ccm.h> #include <cryptopp/ccm.h>
#include <cryptopp/filters.h> #include <cryptopp/filters.h>
#include <cryptopp/md5.h> #include <cryptopp/md5.h>
#include <cryptopp/modes.h> #include <cryptopp/modes.h>
#include "core/hle/service/nwm/nwm_uds.h"
#include "core/hle/service/nwm/uds_data.h"
#include "core/hw/aes/key.h"
namespace Service { namespace Service {
namespace NWM { namespace NWM {
using MacAddress = std::array<u8, 6>;
// AES Keyslot used to generate the UDS data frame CCMP key. // AES Keyslot used to generate the UDS data frame CCMP key.
constexpr size_t UDSDataCryptoAESKeySlot = 0x2D; constexpr size_t UDSDataCryptoAESKeySlot = 0x2D;
@ -46,7 +46,8 @@ static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 d
// reinforcing the hypotheses that the first 4 bytes are actually the header of // reinforcing the hypotheses that the first 4 bytes are actually the header of
// another container protocol. // another container protocol.
header.securedata_size = data_size + sizeof(SecureDataHeader) - 4; header.securedata_size = data_size + sizeof(SecureDataHeader) - 4;
header.is_management = 0; // Frames sent by the emulated application are never UDS management frames // Frames sent by the emulated application are never UDS management frames
header.is_management = 0;
header.data_channel = channel; header.data_channel = channel;
header.sequence_number = sequence_number; header.sequence_number = sequence_number;
header.dest_node_id = dest_node_id; header.dest_node_id = dest_node_id;
@ -81,15 +82,16 @@ static std::array<u8, CryptoPP::MD5::DIGESTSIZE> GetDataCryptoCTR(const NetworkI
* Generates the key used for encrypting the 802.11 data frames generated by UDS. * Generates the key used for encrypting the 802.11 data frames generated by UDS.
* @returns The key used for data frames crypto. * @returns The key used for data frames crypto.
*/ */
static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey(const std::vector<u8>& passphrase, static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey(
const NetworkInfo& network_info) { const std::vector<u8>& passphrase, const NetworkInfo& network_info) {
// Calculate the MD5 hash of the input passphrase. // Calculate the MD5 hash of the input passphrase.
std::array<u8, CryptoPP::MD5::DIGESTSIZE> passphrase_hash; std::array<u8, CryptoPP::MD5::DIGESTSIZE> passphrase_hash;
CryptoPP::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(), passphrase.size()); CryptoPP::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(), passphrase.size());
std::array<u8, CryptoPP::AES::BLOCKSIZE> ccmp_key; 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. // The CCMP key is the result of encrypting the MD5 hash of the passphrase with AES-CTR using
// keyslot 0x2D.
using CryptoPP::AES; using CryptoPP::AES;
std::array<u8, CryptoPP::MD5::DIGESTSIZE> counter = GetDataCryptoCTR(network_info); std::array<u8, CryptoPP::MD5::DIGESTSIZE> counter = GetDataCryptoCTR(network_info);
std::array<u8, AES::BLOCKSIZE> key = HW::AES::GetNormalKey(UDSDataCryptoAESKeySlot); std::array<u8, AES::BLOCKSIZE> key = HW::AES::GetNormalKey(UDSDataCryptoAESKeySlot);
@ -139,14 +141,19 @@ static std::vector<u8> GenerateCCMPAAD(const MacAddress& sender, const MacAddres
* Decrypts the payload of an encrypted 802.11 data frame using the specified key. * Decrypts the payload of an encrypted 802.11 data frame using the specified key.
* @returns The decrypted payload. * @returns The decrypted payload.
*/ */
static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload,
const MacAddress& sender, const MacAddress& receiver, u16 sequence_number) { const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key,
const MacAddress& sender, const MacAddress& receiver,
u16 sequence_number) {
// Reference: IEEE 802.11-2007 // Reference: IEEE 802.11-2007
std::vector<u8> aad = GenerateCCMPAAD(sender, receiver); std::vector<u8> aad = GenerateCCMPAAD(sender, receiver);
std::vector<u8> packet_number{0, 0, 0, 0, std::vector<u8> packet_number{0,
0,
0,
0,
static_cast<u8>((sequence_number >> 8) & 0xFF), static_cast<u8>((sequence_number >> 8) & 0xFF),
static_cast<u8>(sequence_number & 0xFF)}; static_cast<u8>(sequence_number & 0xFF)};
@ -161,15 +168,17 @@ static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload
d.SetKeyWithIV(ccmp_key.data(), ccmp_key.size(), nonce.data(), nonce.size()); d.SetKeyWithIV(ccmp_key.data(), ccmp_key.size(), nonce.data(), nonce.size());
d.SpecifyDataLengths(aad.size(), encrypted_payload.size() - 8, 0); d.SpecifyDataLengths(aad.size(), encrypted_payload.size() - 8, 0);
CryptoPP::AuthenticatedDecryptionFilter df(d, nullptr, CryptoPP::AuthenticatedDecryptionFilter df(
CryptoPP::AuthenticatedDecryptionFilter::MAC_AT_END | d, nullptr, CryptoPP::AuthenticatedDecryptionFilter::MAC_AT_END |
CryptoPP::AuthenticatedDecryptionFilter::THROW_EXCEPTION); CryptoPP::AuthenticatedDecryptionFilter::THROW_EXCEPTION);
// put aad // put aad
df.ChannelPut(CryptoPP::AAD_CHANNEL, aad.data(), aad.size()); df.ChannelPut(CryptoPP::AAD_CHANNEL, aad.data(), aad.size());
// put cipher with mac // put cipher with mac
df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, encrypted_payload.data(), encrypted_payload.size() - 8); df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, encrypted_payload.data(),
df.ChannelPut(CryptoPP::DEFAULT_CHANNEL, encrypted_payload.data() + encrypted_payload.size() - 8, 8); encrypted_payload.size() - 8);
df.ChannelPut(CryptoPP::DEFAULT_CHANNEL,
encrypted_payload.data() + encrypted_payload.size() - 8, 8);
df.ChannelMessageEnd(CryptoPP::AAD_CHANNEL); df.ChannelMessageEnd(CryptoPP::AAD_CHANNEL);
df.ChannelMessageEnd(CryptoPP::DEFAULT_CHANNEL); df.ChannelMessageEnd(CryptoPP::DEFAULT_CHANNEL);
@ -191,13 +200,18 @@ static std::vector<u8> DecryptDataFrame(const std::vector<u8>& encrypted_payload
* Encrypts the payload of an 802.11 data frame using the specified key. * Encrypts the payload of an 802.11 data frame using the specified key.
* @returns The encrypted payload. * @returns The encrypted payload.
*/ */
static std::vector<u8> EncryptDataFrame(const std::vector<u8>& payload, const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key, static std::vector<u8> EncryptDataFrame(const std::vector<u8>& payload,
const MacAddress& sender, const MacAddress& receiver, u16 sequence_number) { const std::array<u8, CryptoPP::AES::BLOCKSIZE>& ccmp_key,
const MacAddress& sender, const MacAddress& receiver,
u16 sequence_number) {
// Reference: IEEE 802.11-2007 // Reference: IEEE 802.11-2007
std::vector<u8> aad = GenerateCCMPAAD(sender, receiver); std::vector<u8> aad = GenerateCCMPAAD(sender, receiver);
std::vector<u8> packet_number{0, 0, 0, 0, std::vector<u8> packet_number{0,
0,
0,
0,
static_cast<u8>((sequence_number >> 8) & 0xFF), static_cast<u8>((sequence_number >> 8) & 0xFF),
static_cast<u8>(sequence_number & 0xFF)}; static_cast<u8>(sequence_number & 0xFF)};
@ -235,11 +249,11 @@ static std::vector<u8> EncryptDataFrame(const std::vector<u8>& payload, const st
return {}; return {};
} }
std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node,
u16 sequence_number) { u16 src_node, u16 sequence_number) {
std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData); std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData);
std::vector<u8> securedata_header = GenerateSecureDataHeader(data.size(), channel, dest_node, src_node, std::vector<u8> securedata_header =
sequence_number); GenerateSecureDataHeader(data.size(), channel, dest_node, src_node, sequence_number);
buffer.insert(buffer.end(), securedata_header.begin(), securedata_header.end()); buffer.insert(buffer.end(), securedata_header.begin(), securedata_header.end());
buffer.insert(buffer.end(), data.begin(), data.end()); buffer.insert(buffer.end(), data.begin(), data.end());

View file

@ -6,28 +6,18 @@
#include <array> #include <array>
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/hle/service/service.h" #include "core/hle/service/service.h"
#include <cryptopp/aes.h>
namespace Service { namespace Service {
namespace NWM { namespace NWM {
enum class SAP : u8 { enum class SAP : u8 { SNAPExtensionUsed = 0xAA };
SNAPExtensionUsed = 0xAA
};
enum class PDUControl : u8 { enum class PDUControl : u8 { UnnumberedInformation = 3 };
UnnumberedInformation = 3
};
enum class EtherType : u16 { enum class EtherType : u16 { SecureData = 0x876D, EAPoL = 0x888E };
SecureData = 0x876D,
EAPoL = 0x888E
};
/* /*
* 802.2 header, UDS packets always use SNAP for these headers, * 802.2 header, UDS packets always use SNAP for these headers,
@ -81,7 +71,8 @@ static_assert(sizeof(DataFrameCryptoCTR) == 16, "DataFrameCryptoCTR has the wron
* Generates an unencrypted 802.11 data payload. * Generates an unencrypted 802.11 data payload.
* @returns The generated frame payload. * @returns The generated frame payload.
*/ */
std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, u16 sequence_number); std::vector<u8> GenerateDataPayload(const std::vector<u8>& data, u8 channel, u16 dest_node,
u16 src_node, u16 sequence_number);
} // namespace NWM } // namespace NWM
} // namespace Service } // namespace Service