aw: set rom key in game list (mameset 0.213)

backport lr changes
This commit is contained in:
Flyinghead 2019-09-19 16:15:18 +02:00
parent e7a33e6de1
commit b693d1c054
3 changed files with 556 additions and 121 deletions

View File

@ -247,41 +247,34 @@ void AWCartridge::WriteMem(u32 address, u32 data, u32 size)
}
/*
We are using 20 bits keys with the following subfields' structure:
bits 0-15 is a 16-bits XOR
bits 17-16 is a index to the sboxes table
bits 19-18 is a index to the permutation table
We are using 8 bits keys with the following subfields' structure:
bits 0-3 is a index of 16-bits XOR (only 11 was used in known games)
bits 4-5 is a index to the sboxes table
bits 6-7 is a index to the permutation table
These subfields could be differing from the "real" ones in the following ways:
- Current keys equal to decrypted game code binary 8-bit sum (of each byte's swapped 4-bit nibbles)
- Every one of the index subfields could be suffering an arbitrary bitswap and XOR
- The 16-bits-XOR subfield could suffer an arbitrary XOR which could depend on the 4 index bits (that is: a different XOR per every index combination)
- Of course, the way in which we are mixing 3 subfields in one only key is arbitrary too.
The keys are stored as 32-bits big-endian values in a file.
*/
const u8 AWCartridge::permutation_table[4][16] =
{
{14,1,11,15,7,3,8,13,0,4,2,12,6,10,5,9},
{8,10,1,3,7,4,11,2,5,15,6,0,12,13,9,14},
{4,5,9,6,1,13,7,11,10,0,14,12,8,15,2,3},
{12,7,11,2,0,5,15,6,1,8,14,4,9,13,3,10},
{14,1,11,15,7,3,8,13,0,4,2,12,6,10,5,9}
{12,7,11,2,0,5,15,6,1,8,14,4,9,13,3,10}
};
const AWCartridge::sbox_set AWCartridge::sboxes_table[4] =
{
{
{4,12,8,14,16,30,31,0,23,29,24,21,11,22,27,5,3,20,18,26,10,7,17,1,28,6,15,13,2,9,25,19},
{13,1,0,9,5,12,4,14,3,15,2,10,11,6,8,7},
{7,13,4,6,5,9,3,2,0,15,12,10,8,11,1,14},
{4,0,1,2,5,7,3,6}
},
{
{3,0,14,17,10,15,31,20,13,2,29,28,9,18,25,27,6,19,30,22,7,12,1,16,23,11,24,4,8,26,21,5},
{2,10,6,9,11,13,4,5,3,15,7,14,12,1,0,8},
{11,8,6,25,2,7,23,28,5,10,21,20,1,26,17,19,14,27,22,30,15,4,9,24,31,3,16,12,0,18,29,13},
{13,5,9,6,4,2,11,10,12,0,8,1,3,14,15,7},
{1,13,11,3,8,7,9,10,12,15,4,14,0,5,6,2},
{6,5,0,3,7,1,4,2}
{3,0,5,6,2,4,1,7}
},
{
{9,15,28,7,13,24,2,23,21,1,22,16,18,8,17,31,27,6,30,12,4,20,5,19,0,25,3,29,10,14,11,26},
@ -290,11 +283,22 @@ const AWCartridge::sbox_set AWCartridge::sboxes_table[4] =
{1,5,6,2,4,7,3,0}
},
{
{17,3,31,2,28,10,9,29,6,25,24,8,13,1,19,15,22,0,14,20,16,7,21,4,18,26,27,5,12,23,11,30},
{4,8,11,15,3,14,7,12,1,0,9,5,6,13,2,10},
{14,0,9,11,4,1,7,5,13,6,8,12,2,3,10,15},
{2,1,0,5,4,6,7,3}
{17,25,29,27,5,11,10,21,2,8,13,0,30,3,14,16,22,1,7,15,31,18,4,20,9,19,26,24,23,28,12,6},
{15,3,2,11,7,14,6,12,1,13,0,8,9,4,10,5},
{6,12,5,7,4,8,2,3,1,14,13,11,9,10,0,15},
{5,1,0,3,4,6,2,7}
},
{
{7,21,9,20,10,28,31,11,16,15,14,30,27,23,5,25,0,22,24,2,6,17,3,18,4,12,13,19,26,1,29,8},
{10,6,5,1,13,0,9,2,15,14,7,11,8,3,12,4},
{8,6,15,13,2,7,1,3,11,0,14,10,4,5,12,9},
{6,5,4,1,0,2,3,7}
}
};
const int AWCartridge::xor_table[16] = // -1 = unknown/unused
{
0x0000, -1, 0x97CF, 0x4BE3, 0x2255, 0x8DD6, -1, 0xC6A2, 0xA1E8, 0xB3BF, 0x3B1A, 0x547A, -1, 0x935F, -1, -1
};
static u16 bitswap16(u16 in, const u8* vec)
@ -306,12 +310,12 @@ static u16 bitswap16(u16 in, const u8* vec)
return ret;
}
u16 AWCartridge::decrypt(u16 cipherText, u32 address, const u32 key)
u16 AWCartridge::decrypt(u16 cipherText, u32 address, const u8 key)
{
u8 b0,b1,b2,b3;
u16 aux;
const u8* pbox = permutation_table[key>>18];
const sbox_set* ss = &sboxes_table[(key>>16)&3];
const u8* pbox = permutation_table[key>>6];
const sbox_set* ss = &sboxes_table[(key>>4)&3];
const u8 text_swap_vec[] = {
pbox[15],pbox[14],pbox[13],pbox[12],pbox[11],pbox[10],pbox[9],pbox[8],
@ -330,21 +334,20 @@ u16 AWCartridge::decrypt(u16 cipherText, u32 address, const u32 key)
b2 = ss->S2[b2];
b3 = ss->S3[b3];
return ((b3<<13)|(b2<<9)|(b1<<5)|b0)^(key&0xffff);
return ((b3<<13)|(b2<<9)|(b1<<5)|b0)^xor_table[key&0xf];
}
void AWCartridge::Init()
{
mpr_offset = decrypt16(0x58/2) | (decrypt16(0x5a/2) << 16);
INFO_LOG(NAOMI, "AWCartridge::SetKey rombd_key %08x mpr_offset %08x", rombd_key, mpr_offset);
INFO_LOG(NAOMI, "AWCartridge::SetKey rombd_key %02x mpr_offset %08x", rombd_key, mpr_offset);
device_reset();
}
void AWCartridge::SetKey(u32 key)
{
const u8 *krp = (u8 *)&key;
rombd_key = (krp[0] << 24) | (krp[1] << 16) | (krp[2] << 8) | krp[3];
rombd_key = key;
}
void AWCartridge::device_reset()
@ -398,7 +401,7 @@ void *AWCartridge::GetDmaPtr(u32 &limit)
// printf("AWCART Decrypted data @ %08x:\n", dma_offset);
// for (int i = 0; i < 16; i++)
// {
// printf("%c %c ", decrypted_buf[i] & 0xff, decrypted_buf[i] >> 8);
// printf("%02x %02x ", decrypted_buf[i] & 0xff, decrypted_buf[i] >> 8);
// if ((i + 1) % 8 == 0)
// printf("\n");
// }

View File

@ -28,7 +28,6 @@ public:
virtual std::string GetGameId() override;
void SetKey(u32 key) override;
void SetKeyData(u8 *key) override { SetKey(key[0] | (key[1] << 8) | (key[2] << 16) | (key[3] << 24)); }
private:
virtual void device_reset();
@ -52,7 +51,8 @@ private:
static const u8 permutation_table[4][16];
static const sbox_set sboxes_table[4];
static u16 decrypt(u16 cipherText, u32 address, const u32 key);
static const int xor_table[16];
static u16 decrypt(u16 cipherText, u32 address, const u8 key);
u16 decrypt16(u32 address) { return decrypt(((u16 *)RomPtr)[address % (RomSize / 2)], address, rombd_key); }
void set_key();

File diff suppressed because it is too large Load Diff