Difference between revisions of "Cryptography"
(→Encryption Algorithms) |
|||
Line 67: | Line 67: | ||
return value; | return value; | ||
}</source> | }</source> | ||
+ | |||
+ | ==Encryption Algorithms== | ||
+ | |||
+ | === AES === | ||
+ | |||
+ | The encryption algorithm used for [[RPF]], [[IMG]] and [[SCO]] files is the [[Wikipedia:Advanced Encryption Standard|Advanced Encryption Standard]] (AES) in the following configuration: | ||
+ | * block size: 128 bit (16 byte) | ||
+ | * key size: 256 bit (32 byte) | ||
+ | * cypher mode: [[Wikipedia:Block cipher modes of operation#Electronic_codebook_(ECB)|electronic code book]] (ECB) | ||
+ | * repetitions: 16 times | ||
+ | |||
+ | That means all encrypted data (the cyphertext) can be split up into 16 byte blocks and decrypted independently. Decryption is done by executing the AES-128 decrypt routine 16x on each data block. If the last block is smaller than 16 byte, it is left unencrypted in Rockstar's archives. | ||
+ | |||
+ | ==== Key ==== | ||
+ | |||
+ | The 256 bit key necessary to decrypt the cyphertext can be retrieved from <code>gtaiv.exe</code> at the following file offsets: | ||
+ | |||
+ | {| {{Prettytable}} | ||
+ | !Game Version | ||
+ | !Offset | ||
+ | |- | ||
+ | |1.0 US || 0xA94204 | ||
+ | |- | ||
+ | |1.0.1 US || 0xB607C4 | ||
+ | |- | ||
+ | |1.0.2 US || 0xB56BC4 | ||
+ | |- | ||
+ | |1.0.0.1 RUS || 0xB5B65C | ||
+ | |- | ||
+ | |1.0.1.1 RUS || 0xB569F4 | ||
+ | |} | ||
+ | |||
+ | The key is the same for all game versions on the PC and even the XBOX 360. You may want to use the following '''SHA1 hash''' to verify the correctness of the retrieved key: | ||
+ | DE A3 75 EF 1E 6E F2 22 3A 12 21 C2 C5 75 C4 7B F1 7E FA 5E | ||
+ | '''''NOTE:''''' '''This is not the cypher key!''' {{Note|1}} | ||
+ | |||
+ | It is recommended to leave modified archives unencrypted. | ||
+ | |||
+ | ==== Example ==== | ||
+ | |||
+ | Program code to decrypt data from GTA IV could look like this: | ||
+ | |||
+ | <source lang="c">AES_set_decrypt_key(key, 256, context); | ||
+ | |||
+ | for (int i = 0; i < (int) (data_size/16); i++) { | ||
+ | void *p = (void *) (data_offset + i*16); // the pointer to the current block | ||
+ | for (int j = 1; j <= 16; j++) // 16 (pointless) repetitions | ||
+ | AES_decrypt_block(p, p, context); | ||
+ | }</source> | ||
+ | |||
+ | ''See also:'' [http://code.google.com/p/gtaivtools/source/browse/trunk/RageLib/Common/DataUtil.cs Decryption routine] in [[SparkIV]] (C#, [[Wikipedia:GNU_Public_License|GPL]]) | ||
==== Legal Issues ==== | ==== Legal Issues ==== | ||
− | {{ref|1}} Since the United States complying with [[Wikipedia:WIPO|WIPO]] treaties, they created the [[Wikipedia:Digital_Millennium_Copyright_Act|Digital Millenium Copyright Act]] in order to bring their copyright laws up to date in the emerging digital world. A key point in this act is the [[Wikipedia:Digital_Millennium_Copyright_Act#Anti-circumvention_exemptions|Anti-cirumvention]] clauses which tell us that it is against the law to break copyright protection mechanisms (even if it is for legal purposes). The MPAA have used this power in the past to sue the makers of De-CSS (The copy protection scheme used in early DVD's). This doesn't only apply to people in the United States though, plenty of other countries have made their own legislation to the same effect, and many others have signed Free Trade Agreements with the United States that shoehorn the necessary DMCA provisions into the countries own legislation. Countries that have taken part in Free Trade Agreements include Australia, South Korea and The United Kingdom. | + | {{ref|1}} Since the United States complying with [[Wikipedia:WIPO|WIPO]] treaties, they created the [[Wikipedia:Digital_Millennium_Copyright_Act|Digital Millenium Copyright Act]] in order to bring their copyright laws up to date in the emerging digital world. A key point in this act is the [[Wikipedia:Digital_Millennium_Copyright_Act#Anti-circumvention_exemptions|Anti-cirumvention]] clauses which tell us that it is against the law to break copyright protection mechanisms (even if it is for legal purposes). The MPAA have used this power in the past to sue the makers of De-CSS (The copy protection scheme used in early DVD's). The AES Key that Rockstar used in GTA IV is technically an anti-circumvention measure and so cannot be posted here lest GTAModding be subject to legal ramifications. However since it is available in GTA IV's own binary a lot of people have just read it directly out of the actual executable (the offsets are posted in the section above). This doesn't only apply to people in the United States though, plenty of other countries have made their own legislation to the same effect, and many others have signed Free Trade Agreements with the United States that shoehorn the necessary DMCA provisions into the countries own legislation. Countries that have taken part in Free Trade Agreements include Australia, South Korea and The United Kingdom. |
Although other laws contradict this. For example in most countries reverse engineering for the purpose of interoperability (what most people who use the key try to achieve) transcends the anti-circumvention measures proposed by the DMCA. Although the legal issue still remains unclear most modders and site owners choose to be on the side of caution. | Although other laws contradict this. For example in most countries reverse engineering for the purpose of interoperability (what most people who use the key try to achieve) transcends the anti-circumvention measures proposed by the DMCA. Although the legal issue still remains unclear most modders and site owners choose to be on the side of caution. | ||
Line 80: | Line 131: | ||
* [[Wikipedia:CRC32|CRC32]] | * [[Wikipedia:CRC32|CRC32]] | ||
* [[Wikipedia:Hash_table#Choosing_a_good_hash_function|Jenkins One-at-a-time Hash]] | * [[Wikipedia:Hash_table#Choosing_a_good_hash_function|Jenkins One-at-a-time Hash]] | ||
+ | * [[Wikipedia:Advanced_Encryption_Standard|AES Encryption]] | ||
{{GTA4-navi}} | {{GTA4-navi}} |
Revision as of 01:16, 17 February 2011
This article explains the cryptography present in GTA IV.
Contents
Hashing Algorithms
GTA IV relies on many different hash algorithms work its operation, each for a different purpose. GTA IV's new usage of hashing has allowed it to explore a more binary focused way of hiding data in files, and strings away from plain site.
SHA1
The SHA1 hashing algorithm is used when comparing the files in versions 1.0 and 1.0.1. This check was later removed in 1.0.2.
This section is incomplete. You can help by fixing and expanding it.
CRC32
The Cyclic Redundancy Check 32 bit hashing algorithm is used in the GXT file to match text codes with their counterparts. A C++ implementation of GTA IV's CRC32 hashing algorithm can be displayed as follows:
unsigned int CRC32(char* text)
{
size_t textLen = strlen(text);
int i = 0;
unsigned int retHash = 0;
if(text[0] == '"')
i = 1;
for(i;i<textLen;i++)
{
char ctext = text[i];
if(ctext == '"')
break;
if(ctext - 65 > 25)
{
if(ctext == '\\')
ctext = '/';
}
else ctext += 32;
retHash = (1025 * (retHash + ctext) >> 6) ^ 1025 * (retHash + ctext);
}
return 32769 * (9 * retHash ^ (9 * retHash >> 11));
}
As you can see it differs from most common hashing algorithms in the way it handles text (by not including " or \ characters). Also usually entry name strings in GXT text archives are stored in upper case, so it may be useful to convert them before creating the hash.
One At A Time Hash
The One At A Time hashing function was originally created by Bob Jenkins. A C++ implementation can be found here and at Wikipedia.
unsigned int oneAtATimeHash(char* inpStr)
{
unsigned int value = 0,temp = 0;
for(size_t i=0;i<strlen(inpStr);i++)
{
char ctext = tolower(inpStr[i]);
temp = hashchr;
temp += value;
value = temp << 10;
temp += value;
value = temp >> 6;
value ^= temp;
}
temp = value << 3;
temp += value;
unsigned int temp2 = temp >> 11;
temp = temp2 ^ temp;
temp2 = temp << 15;
value = temp2 + temp;
if(value < 2) value += 2;
return value;
}
Encryption Algorithms
AES
The encryption algorithm used for RPF, IMG and SCO files is the Advanced Encryption Standard (AES) in the following configuration:
- block size: 128 bit (16 byte)
- key size: 256 bit (32 byte)
- cypher mode: electronic code book (ECB)
- repetitions: 16 times
That means all encrypted data (the cyphertext) can be split up into 16 byte blocks and decrypted independently. Decryption is done by executing the AES-128 decrypt routine 16x on each data block. If the last block is smaller than 16 byte, it is left unencrypted in Rockstar's archives.
Key
The 256 bit key necessary to decrypt the cyphertext can be retrieved from gtaiv.exe
at the following file offsets:
Game Version | Offset |
---|---|
1.0 US | 0xA94204 |
1.0.1 US | 0xB607C4 |
1.0.2 US | 0xB56BC4 |
1.0.0.1 RUS | 0xB5B65C |
1.0.1.1 RUS | 0xB569F4 |
The key is the same for all game versions on the PC and even the XBOX 360. You may want to use the following SHA1 hash to verify the correctness of the retrieved key:
DE A3 75 EF 1E 6E F2 22 3A 12 21 C2 C5 75 C4 7B F1 7E FA 5E
NOTE: This is not the cypher key! ^
It is recommended to leave modified archives unencrypted.
Example
Program code to decrypt data from GTA IV could look like this:
AES_set_decrypt_key(key, 256, context);
for (int i = 0; i < (int) (data_size/16); i++) {
void *p = (void *) (data_offset + i*16); // the pointer to the current block
for (int j = 1; j <= 16; j++) // 16 (pointless) repetitions
AES_decrypt_block(p, p, context);
}
See also: Decryption routine in SparkIV (C#, GPL)
Legal Issues
[1] Since the United States complying with WIPO treaties, they created the Digital Millenium Copyright Act in order to bring their copyright laws up to date in the emerging digital world. A key point in this act is the Anti-cirumvention clauses which tell us that it is against the law to break copyright protection mechanisms (even if it is for legal purposes). The MPAA have used this power in the past to sue the makers of De-CSS (The copy protection scheme used in early DVD's). The AES Key that Rockstar used in GTA IV is technically an anti-circumvention measure and so cannot be posted here lest GTAModding be subject to legal ramifications. However since it is available in GTA IV's own binary a lot of people have just read it directly out of the actual executable (the offsets are posted in the section above). This doesn't only apply to people in the United States though, plenty of other countries have made their own legislation to the same effect, and many others have signed Free Trade Agreements with the United States that shoehorn the necessary DMCA provisions into the countries own legislation. Countries that have taken part in Free Trade Agreements include Australia, South Korea and The United Kingdom.
Although other laws contradict this. For example in most countries reverse engineering for the purpose of interoperability (what most people who use the key try to achieve) transcends the anti-circumvention measures proposed by the DMCA. Although the legal issue still remains unclear most modders and site owners choose to be on the side of caution.
External links
Links to detailed explanations of the several hash and encryption algorithms on wikipedia:
Grand Theft Auto IV | |
---|---|
File Formats | .dat • .gxt • .ide • .img • .ipl • .nod • .sco • .rpf • .rrr • .wad • .wbd/.wbn • .wdd • .wdr • .wft • .whm • .wpl • .wtd |
Documentation | Audio • Bink Video • Cryptography • Cutscenes • GXT Text • Image listing • Keycodes • Map Listing • Native functions • Paths • Radar Blips • Radio Stations • Saves • Scenarios • VTable • Weapons |
Tools | ASI Loader • ENBSeries • G-Texture • GIMS IV • Ingame WPL Editor • IV Needle • OpenIV • SparkIV • XLiveLess • WPL Manager • X Mod Installer Alice • C++ Script Hook • .NET Script Hook • Scocl |
Tutorials | Importing Textures with OpenIV • Importing Textures with SparkIV |
Modifications | GTA Connected • Gostown IV • Four Multiplayer • IV Multiplayer • CitizenMP:IV Reloaded |
Useful links | Community portal • Discussion forums • Modding forums • Mods on GTAGarage.com |