uds_data.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include "core/hle/service/nwm/nwm_uds.h"
  6. #include "core/hle/service/nwm/uds_data.h"
  7. #include "core/hw/aes/key.h"
  8. #include <cryptopp/aes.h>
  9. #include <cryptopp/md5.h>
  10. #include <cryptopp/modes.h>
  11. namespace Service {
  12. namespace NWM {
  13. // AES Keyslot used to generate the UDS data frame CCMP key.
  14. constexpr size_t UDSDataCryptoAESKeySlot = 0x2D;
  15. /*
  16. * Generates a SNAP-enabled 802.2 LLC header for the specified protocol.
  17. * @returns a buffer with the bytes of the generated header.
  18. */
  19. static std::vector<u8> GenerateLLCHeader(EtherType protocol) {
  20. LLCHeader header{};
  21. header.protocol = static_cast<u16>(protocol);
  22. std::vector<u8> buffer(sizeof(header));
  23. memcpy(buffer.data(), &header, sizeof(header));
  24. return buffer;
  25. }
  26. /*
  27. * Generates a Nintendo UDS SecureData header with the specified parameters.
  28. * @returns a buffer with the bytes of the generated header.
  29. */
  30. static std::vector<u8> GenerateSecureDataHeader(u16 data_size, u8 channel, u16 dest_node_id,
  31. u16 src_node_id, u16 sequence_number) {
  32. SecureDataHeader header{};
  33. header.protocol_size = data_size + sizeof(SecureDataHeader);
  34. // TODO(Subv): It is likely that the first 4 bytes of this header are actually a decorator for another protocol.
  35. header.securedata_size = data_size + sizeof(SecureDataHeader) - 4;
  36. header.is_management = 0; // Frames sent by the emulated application are never UDS management frames
  37. header.data_channel = channel;
  38. header.sequence_number = sequence_number;
  39. header.dest_node_id = dest_node_id;
  40. header.src_node_id = src_node_id;
  41. std::vector<u8> buffer(sizeof(header));
  42. memcpy(buffer.data(), &header, sizeof(header));
  43. return buffer;
  44. }
  45. /*
  46. * Calculates the CTR used for the AES-CTR process that calculates
  47. * the CCMP crypto key for data frames.
  48. * @returns The CTR used for data frames crypto key generation.
  49. */
  50. static std::array<u8, CryptoPP::MD5::DIGESTSIZE> GetDataCryptoCTR(const NetworkInfo& network_info) {
  51. DataFrameCryptoCTR data{};
  52. data.host_mac = network_info.host_mac_address;
  53. data.wlan_comm_id = network_info.wlan_comm_id;
  54. data.id = network_info.id;
  55. data.network_id = network_info.network_id;
  56. std::array<u8, CryptoPP::MD5::DIGESTSIZE> hash;
  57. CryptoPP::MD5().CalculateDigest(hash.data(), reinterpret_cast<u8*>(&data), sizeof(data));
  58. return hash;
  59. }
  60. /*
  61. * Generates the key used for encrypting the 802.11 data frames generated by UDS.
  62. * @returns The key used for data frames crypto.
  63. */
  64. static std::array<u8, CryptoPP::AES::BLOCKSIZE> GenerateDataCCMPKey(const std::vector<u8>& passphrase,
  65. const NetworkInfo& network_info) {
  66. // Calculate the MD5 hash of the input passphrase.
  67. std::array<u8, CryptoPP::MD5::DIGESTSIZE> passphrase_hash;
  68. CryptoPP::MD5().CalculateDigest(passphrase_hash.data(), passphrase.data(), passphrase.size());
  69. std::array<u8, CryptoPP::AES::BLOCKSIZE> ccmp_key;
  70. // The CCMP key is the result of encrypting the MD5 hash of the passphrase with AES-CTR using keyslot 0x2D.
  71. using CryptoPP::AES;
  72. std::array<u8, CryptoPP::MD5::DIGESTSIZE> counter = GetDataCryptoCTR(network_info);
  73. std::array<u8, AES::BLOCKSIZE> key = HW::AES::GetNormalKey(UDSDataCryptoAESKeySlot);
  74. CryptoPP::CTR_Mode<AES>::Encryption aes;
  75. aes.SetKeyWithIV(key.data(), AES::BLOCKSIZE, counter.data());
  76. aes.ProcessData(ccmp_key.data(), passphrase_hash.data(), passphrase_hash.size());
  77. return ccmp_key;
  78. }
  79. std::vector<u8> GenerateDataFrame(const std::vector<u8>& data, u8 channel, u16 dest_node, u16 src_node, u16 sequence_number) {
  80. std::vector<u8> buffer = GenerateLLCHeader(EtherType::SecureData);
  81. std::vector<u8> securedata_header = GenerateSecureDataHeader(data.size(), channel, dest_node, src_node, sequence_number);
  82. buffer.insert(buffer.end(), securedata_header.begin(), securedata_header.end());
  83. buffer.insert(buffer.end(), data.begin(), data.end());
  84. // TODO(Subv): Encrypt the frame.
  85. // TODO(Subv): Prepend CCMP initialization vector (sequence_number).
  86. // TODO(Subv): Encapsulate the frame in an 802.11 data frame.
  87. return buffer;
  88. }
  89. } // namespace NWM
  90. } // namespace Service