···11**Emma's Xbox 360 Research Notes - Networking**
2233-Updated 23rd October 2024.
33+Updated 24th October 2024.
4455Stub page.
66···1313This article also applies to Games for Windows - LIVE, in sections discussing
1414cross-platform system link.
15151616+Some information on this page was referenced from Xenia's partial
1717+[XNet Implementation](https://github.com/xenia-project/xenia/blob/systemlink/src/xenia/kernel/xnet.cc).
1818+1619## Encryption Key Initialisation
17201821When a title initialises WinSock and XNet, `CXnIp::IpInit` initialises several
1919-encryption keys, likely for three different cryptography types, AES, 3DES and
2020-DES. *(TODO: Check)* Pseudocode for the derivation process is as follows:
2222+encryption keys, for HMAC validation, DES3 packet encryption, and a feed value
2323+for using DES3 in CBC mode.
2424+2525+These keys are initialised by taking the LAN key from either the optional XEX
2626+header value 0x40404 (16 byte fixed-sized structure with ID 0x0404) on Xbox 360,
2727+or the `lankey` value from the .cfg XML next to the game executable on GfWL,
2828+and using that to build 3 keys forming a buffer of size 0x3C bytes.
2929+3030+**Key Buffer:**
3131+3232+When building the key buffer, numbers 0 through 2 are prefixed at the start of
3333+the LAN key and then hashed with either HMAC SHA-1 with the roamable obfuscation
3434+key (Xbox 360), or encrypted* and then a regular SHA-1 (GfWL / cross-platform)
3535+performed on it.
3636+3737+*\* The LAN key is encrypted - the prefix byte remains unencrypted.*
3838+3939+| Offset | Size | Description |
4040+| ------ | ------ | -------------------------------------- |
4141+| `0x00` | `0x14` | (HMAC) SHA-1 result for 0x00 + LAN key |
4242+| `0x14` | `0x14` | (HMAC) SHA-1 result for 0x01 + LAN key |
4343+| `0x28` | `0x14` | (HMAC) SHA-1 result for 0x02 + LAN key |
4444+4545+This buffer is then sliced up into 3 final keys for the encryption process.
4646+4747+| Offset | Size | Description |
4848+| ------ | ------ | ------------------------------------ |
4949+| `0x00` | `0x10` | HMAC SHA-1 key for packet validation |
5050+| `0x10` | `0x18` | 3DES encryption key for packet data |
5151+| `0x28` | `0x8` | Key for building the CBC feed |
5252+5353+**Pseudocode:**
21542255(Note that this is pseudocode of just key initialisation - it is not C that can
2356be compiled nor is it any specific CXnIp function)
···70103 XeCryptSha(&config_buffer, 0x11, NULL, 0, NULL, 0, key_buffer.key3, 0x14);
71104 }
721057373- // use first 0x10 bytes of key1 for some 0x10 byte key, likely AES
7474- memcpy(this->lan_aes_key, key_buffer.key1, sizeof(this->lan_aes_key)); // 0x10
106106+ // use first 0x10 bytes of key1 for a 0x10 byte HMAC-SHA1 key
107107+ memcpy(this->lan_hmac_key, key_buffer.key1, sizeof(this->lan_hmac_key)); // 0x10
751087676- // use the next 0x4 bytes of key1 and all of key2 for some 0x18 byte key, likely 3DES
109109+ // use the next 0x4 bytes of key1 and all of key2 for a 0x18 byte 3DES key
77110 memcpy(this->lan_3des_key, key_buffer + 0x10, sizeof(this->lan_3des_key)); // 0x18
78111 XeCryptDesParity(this->lan_3des_key, sizeof(this->lan_3des_key), this->lan_3des_key);
791128080- // use the first 0x8 bytes of key3 for some 0x8 byte key, likely DES
8181- memcpy(this->lan_des_key, key_buffer.key3, sizeof(this->lan_des_key));
113113+ // use the first 0x8 bytes of key3 for some 0x8 byte key for seeding CBC mode
114114+ memcpy(this->lan_cbc_feed, key_buffer.key3, sizeof(this->lan_cbc_feed)); // 0x8
82115}
83116```
117117+118118+## Packet Structure
119119+120120+*(TODO: This is for broadcast on 360 packets, but what about P2P/xplat? Check)*
121121+122122+All values are in little endian. Why?
123123+124124+**Xbox 360:** Broadcast messages are sent as IPv4 UDP packets over port 3074,
125125+with a source IP of 0.0.0.1 and a destination IP address of 255.255.255.255.
126126+The destination MAC address is FF:FF:FF:FF:FF:FF.
127127+128128+**Cross-Platform:** TODO
129129+130130+### General Structure
131131+132132+| Offset | Type / Size | Description |
133133+| -------- | ----------- | ----------------------------------- |
134134+| `0x0` | uint32 | Header flags *(TODO: Check?)* |
135135+| `0x4` | variable | Encrypted packet data |
136136+| variable | Footer | Metadata about the packet |
137137+138138+Note that parts of the footer will be encrypted depending on the length of the
139139+packet data.
140140+141141+### Footer
142142+143143+| Offset | Type / Size | Description |
144144+| ------ | ----------- | ----------------------------- |
145145+| `0x0` | uint16 | Source port |
146146+| `0x2` | uint16 | Destination port |
147147+| `0x4` | uint32 | Title ID |
148148+| `0x8` | uint32 | Title version |
149149+| `0xC` | uint32 | System kernel version |
150150+| `0x10` | uint8 | Bytes encrypted divided by 8 |
151151+| `0x11` | uint16 | Seed value for the CBC feed |
152152+| `0x13` | byte[0xA] | HMAC SHA-1 checksum |
153153+154154+### Encryption
155155+156156+TODO