/usr/bin/avos:AOS compare SIGx checksum()

From ArchosDocs

Jump to: navigation, search

Description

Decrypt the SIGx (x = 0 to 4) block, calculate the md5 checksum of the file on disk and then compare this checksum to the checksum contained in the SIGx block after decryption. If they don't match, the file is deemed invalid.

Notice that AOS_decipher_SIGx_block() can fail silently.

References To

The Code

int AOS_SIGx_key_id;
 
int AOS_compare_SIGx_checksum(int fd)
{
uint32_t sign; // -0x424C
char bigbuf[0x4000]; // -0x4240
char md5sum[16]; // -0x240
char SIGx[136]; // -0x230
int read; // -0x1C
uint32_t pos; // -0x18
int remaining; // -0x14
int i; // -0x10
 
read = fread(fd, &SIGx, 136);
if(read != 136)
return 205; // failure
 
pos = (uint32_t)ftell64(fd);
sign = *(uint32_t *)&SIGx[0];
 
switch(sign) {
case 0x53494730: // SIG0
{
AOS_decipher_SIGx_block(&SIGx[8], &Keyring_GetRelMPK);
AOS_SIGx_key_id = 1;
break;
}
case 0x53494731: // SIG1
{
AOS_decipher_SIGx_block(&SIGx[8], &Keyring_GetDevMPK);
AOS_SIGx_key_id = 2;
break;
}
case 0x53494732: // SIG2
{
AOS_decipher_SIGx_block(&SIGx[8], &Keyring_GetPlugMPK);
AOS_SIGx_key_id = 3;
break;
}
case 0x53494733: // SIG3
{
AOS_decipher_SIGx_block(&SIGx[8], &Keyring_GetHDDMPK);
AOS_SIGx_key_id = 4;
break;
}
case 0x53494734: // SIG4
{
AOS_decipher_SIGx_block(&SIGx[8], &Keyring_GetGAMESMPK);
AOS_SIGx_key_id = 5;
break;
}
case 0x5349474E: // SIGN
default: // not recognized
{
return 204; // failure
}
}
 
MD5Digest(1, 0, 0, NULL);
 
i = (AOS_file_length - pos);
 
while(i != 0) {
remaining = min(i, 0x4000); // returns the minimum between i and 0x4000
 
read = fread(fd, &bigbuf, remaining);
if(remaining != read)
return 206; // failure
 
MD5Digest(2, &bigbuf, read, NULL);
 
i = i-remaining;
}
 
MD5Digest(3, 0, 0, &md5sum);
if(memcmp(&md5sum, &SIGx[8], 16) != 0)
return 207; // failure
 
fseek64(fd, SEEK_SET, pos);
 
return 0;
}
Personal tools