|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +// This is almost entirely based on https://github.com/Ernegien/XboxEepromEditor |
| 3 | + |
| 4 | +#include "xbox_eeprom.h" |
| 5 | +#include "rc4.h" |
| 6 | +#include "sha1.h" |
| 7 | + |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +// https://github.com/xemu-project/xemu/blob/9d5cf0926aa6f8eb2221e63a2e92bd86b02afae0/hw/xbox/eeprom_generation.c#L25 |
| 13 | +static uint32_t xbox_eeprom_crc(const void *data, uint32_t len) |
| 14 | +{ |
| 15 | + uint32_t high = 0; |
| 16 | + uint32_t low = 0; |
| 17 | + for (uint32_t i = 0; i < len / 4; i++) { |
| 18 | + uint32_t val = ((uint32_t *)data)[i]; |
| 19 | + uint64_t sum = ((uint64_t)high << 32) | low; |
| 20 | + |
| 21 | + high = (sum + val) >> 32; |
| 22 | + low += val; |
| 23 | + } |
| 24 | + return ~(high + low); |
| 25 | +} |
| 26 | + |
| 27 | +static int do_eeprom_sha1_loop(const uint8_t hardware_revision, const void *data, size_t data_length, uint8_t sha1_output[20]) |
| 28 | +{ |
| 29 | + SHA1Context sha1_context; |
| 30 | + uint8_t sha1_buffer[20]; |
| 31 | + |
| 32 | + // Let's do the encryption stages |
| 33 | + const uint32_t sha1_intermediate_debug_first[] = {0x85F9E51A, 0xE04613D2, 0x6D86A50C, 0x77C32E3C, 0x4BD717A4}; |
| 34 | + const uint32_t sha1_intermediate_debug_second[] = {0x5D7A9C6B, 0xE1922BEB, 0xB82CCDBC, 0x3137AB34, 0x486B52B3}; |
| 35 | + |
| 36 | + // 1.0 |
| 37 | + const uint32_t sha1_intermedia_retail1_first[] = {0x72127625, 0x336472B9, 0xBE609BEA, 0xF55E226B, 0x99958DAC}; |
| 38 | + const uint32_t sha1_intermedia_retail1_second[] = {0x76441D41, 0x4DE82659, 0x2E8EF85E, 0xB256FACA, 0xC4FE2DE8}; |
| 39 | + |
| 40 | + // 1.1 to 1.5 |
| 41 | + const uint32_t sha1_intermedia_retail2_first[] = {0x39B06E79, 0xC9BD25E8, 0xDBC6B498, 0x40B4389D, 0x86BBD7ED}; |
| 42 | + const uint32_t sha1_intermedia_retail2_second[] = {0x9B49BED3, 0x84B430FC, 0x6B8749CD, 0xEBFE5FE5, 0xD96E7393}; |
| 43 | + |
| 44 | + // 1.6 |
| 45 | + const uint32_t sha1_intermedia_retail3_first[] = {0x8058763A, 0xF97D4E0E, 0x865A9762, 0x8A3D920D, 0x08995B2C}; |
| 46 | + const uint32_t sha1_intermedia_retail3_second[] = {0x01075307, 0xA2f1E037, 0x1186EEEA, 0x88DA9992, 0x168A5609}; |
| 47 | + |
| 48 | + // Determine which SHA1 intermediate values to use based on hardware revision |
| 49 | + const uint32_t *sha1_h_a; |
| 50 | + const uint32_t *sha1_h_b; |
| 51 | + if (hardware_revision == 0x09) { |
| 52 | + sha1_h_a = sha1_intermediate_debug_first; |
| 53 | + sha1_h_b = sha1_intermediate_debug_second; |
| 54 | + } else if (hardware_revision == 0x0A) { |
| 55 | + sha1_h_a = sha1_intermedia_retail1_first; |
| 56 | + sha1_h_b = sha1_intermedia_retail1_second; |
| 57 | + } else if (hardware_revision == 0x0B) { |
| 58 | + sha1_h_a = sha1_intermedia_retail2_first; |
| 59 | + sha1_h_b = sha1_intermedia_retail2_second; |
| 60 | + } else if (hardware_revision == 0x0C) { |
| 61 | + sha1_h_a = sha1_intermedia_retail3_first; |
| 62 | + sha1_h_b = sha1_intermedia_retail3_second; |
| 63 | + } else { |
| 64 | + return -1; |
| 65 | + } |
| 66 | + |
| 67 | + // Reset the SHA1 context with the first intermediate values |
| 68 | + sha1_fill(&sha1_context, sha1_h_a[0], sha1_h_a[1], sha1_h_a[2], sha1_h_a[3], sha1_h_a[4]); |
| 69 | + sha1_context.msg_blk_index = 0; |
| 70 | + sha1_context.computed = false; |
| 71 | + sha1_context.length = 512; |
| 72 | + |
| 73 | + sha1_input(&sha1_context, (uint8_t *)data, data_length); |
| 74 | + sha1_result(&sha1_context, sha1_buffer); |
| 75 | + |
| 76 | + sha1_fill(&sha1_context, sha1_h_b[0], sha1_h_b[1], sha1_h_b[2], sha1_h_b[3], sha1_h_b[4]); |
| 77 | + sha1_context.msg_blk_index = 0; |
| 78 | + sha1_context.computed = false; |
| 79 | + sha1_context.length = 512; |
| 80 | + |
| 81 | + sha1_input(&sha1_context, (uint8_t *)sha1_buffer, sizeof(sha1_buffer)); |
| 82 | + sha1_result(&sha1_context, sha1_buffer); |
| 83 | + |
| 84 | + memcpy(sha1_output, sha1_buffer, 20); |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +int xbox_eeprom_decrypt(const xbox_eeprom_t *encrypted_eeprom, xbox_eeprom_t *decrypted_eeprom) |
| 89 | +{ |
| 90 | + RC4Context rc4_context; |
| 91 | + SHA1Context sha1_context; |
| 92 | + |
| 93 | + if (encrypted_eeprom == NULL || decrypted_eeprom == NULL) { |
| 94 | + return -1; // Invalid arguments |
| 95 | + } |
| 96 | + |
| 97 | + // Copy the encrypted EEPROM to the decrypted EEPROM |
| 98 | + memcpy(decrypted_eeprom, encrypted_eeprom, sizeof(xbox_eeprom_t)); |
| 99 | + |
| 100 | + // We don't know what revision the EEPROM is yet, so we have to try all of them until one works. |
| 101 | + const uint8_t hardware_revision[] = {0x09, 0x0A, 0x0B, 0x0C}; |
| 102 | + |
| 103 | + for (int i = 0; i < sizeof(hardware_revision) / sizeof(hardware_revision[0]); i++) { |
| 104 | + |
| 105 | + // Determine the decryption key |
| 106 | + uint8_t decryption_key[20]; |
| 107 | + do_eeprom_sha1_loop(hardware_revision[i], &encrypted_eeprom->encrypted.sha1_hash, |
| 108 | + sizeof(encrypted_eeprom->encrypted.sha1_hash), decryption_key); |
| 109 | + |
| 110 | + // Initialise a RC4 content using the decryption key |
| 111 | + rc4_init(&rc4_context, decryption_key, sizeof(decryption_key)); |
| 112 | + |
| 113 | + // Decrypt the encrypted EEPROM section which does not include the SHA1 hash |
| 114 | + xbox_eeprom_encrypted_t decrypted_result; |
| 115 | + |
| 116 | + // First copy the encrypted data to the decrypted array |
| 117 | + memcpy(&decrypted_result, &encrypted_eeprom->encrypted, sizeof(decrypted_result)); |
| 118 | + |
| 119 | + // Then decrypt the data using RC4. The decrypted data will be written back to the same memory location. |
| 120 | + rc4_crypt(&rc4_context, XBOX_EEPROM_ENCRYPTED_START_PTR(&decrypted_result), XBOX_EEPROM_ENCRYPTED_LENGTH); |
| 121 | + |
| 122 | + // To verify it's valid, do a SHA1 over the decrypted data and verify it matches the SHA1 hash in the EEPROM. |
| 123 | + uint8_t sha1_test[20]; |
| 124 | + do_eeprom_sha1_loop(hardware_revision[i], XBOX_EEPROM_ENCRYPTED_START_PTR(&decrypted_result), XBOX_EEPROM_ENCRYPTED_LENGTH, sha1_test); |
| 125 | + if (memcmp(sha1_test, decrypted_result.sha1_hash, sizeof(decrypted_result.sha1_hash)) == 0) { |
| 126 | + // Okay we have a match, copy the decrypted result back to the decrypted EEPROM |
| 127 | + memcpy(&decrypted_eeprom->encrypted, &decrypted_result, sizeof(decrypted_eeprom->encrypted)); |
| 128 | + return hardware_revision[i]; |
| 129 | + } else { |
| 130 | + // Try the next hardware revision |
| 131 | + continue; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return -1; |
| 136 | +} |
| 137 | + |
| 138 | +int xbox_eeprom_encrypt(uint8_t xbox_revision, const xbox_eeprom_t *decrypted_eeprom, xbox_eeprom_t *encrypted_eeprom) |
| 139 | +{ |
| 140 | + if (decrypted_eeprom == NULL || encrypted_eeprom == NULL) { |
| 141 | + return -1; |
| 142 | + } |
| 143 | + |
| 144 | + if (xbox_revision < 0x09 || xbox_revision > 0x0C) { |
| 145 | + return -1; |
| 146 | + } |
| 147 | + |
| 148 | + // Copy the decrypted EEPROM to the encrypted EEPROM |
| 149 | + memcpy(encrypted_eeprom, decrypted_eeprom, sizeof(xbox_eeprom_t)); |
| 150 | + |
| 151 | + // Write out the factory and user section checksums |
| 152 | + encrypted_eeprom->factory.checksum = xbox_eeprom_crc(XBOX_EEPROM_FACTORY_START_PTR(&encrypted_eeprom->factory), |
| 153 | + XBOX_EEPROM_FACTORY_LENGTH); |
| 154 | + encrypted_eeprom->user.checksum = xbox_eeprom_crc(XBOX_EEPROM_USER_START_PTR(&encrypted_eeprom->user), |
| 155 | + XBOX_EEPROM_USER_LENGTH); |
| 156 | + |
| 157 | + // SHA1 Stage 1 - perform the first hash calculation over the currently unencrypted area (confounder, hdd key, region) |
| 158 | + // to determine the main sha1 hash |
| 159 | + uint8_t sha1_hash[20]; |
| 160 | + do_eeprom_sha1_loop(xbox_revision, XBOX_EEPROM_ENCRYPTED_START_PTR(&encrypted_eeprom->encrypted), XBOX_EEPROM_ENCRYPTED_LENGTH, sha1_hash); |
| 161 | + |
| 162 | + // SHA1 Stage 2 - perform the second hash calculation over the main sha1 hash to determine the RC4 key |
| 163 | + uint8_t rc4_key[20]; |
| 164 | + do_eeprom_sha1_loop(xbox_revision, sha1_hash, sizeof(sha1_hash), rc4_key); |
| 165 | + |
| 166 | + // Stage 3 - RC4 using the the new key to encrypt the data |
| 167 | + RC4Context rc4_context; |
| 168 | + xbox_eeprom_encrypted_t encrypted_result; |
| 169 | + rc4_init(&rc4_context, rc4_key, sizeof(rc4_key)); |
| 170 | + // First copy the decrypted data to the encrypted array |
| 171 | + memcpy(&encrypted_result, &decrypted_eeprom->encrypted, sizeof(encrypted_result)); |
| 172 | + |
| 173 | + // Encrypt the data using RC4. The encrypted data will be written back to the same memory location |
| 174 | + rc4_crypt(&rc4_context, XBOX_EEPROM_ENCRYPTED_START_PTR(&encrypted_result), XBOX_EEPROM_ENCRYPTED_LENGTH); |
| 175 | + |
| 176 | + // Stage 4 - Write it out |
| 177 | + // Copy over the sha1 hash |
| 178 | + memcpy(encrypted_result.sha1_hash, sha1_hash, sizeof(encrypted_result.sha1_hash)); |
| 179 | + |
| 180 | + // Write the encypted result back to the encrypted EEPROM |
| 181 | + memcpy(&encrypted_eeprom->encrypted, &encrypted_result, sizeof(encrypted_result)); |
| 182 | + |
| 183 | + return 0; |
| 184 | +} |
0 commit comments