[ctrtool] Re-added "--seed" option, simplified/corrected common-key selection logic.

This commit is contained in:
jakcron
2017-06-05 11:31:18 +08:00
parent 3cca6a7a93
commit 56ef8b41d1
8 changed files with 111 additions and 91 deletions
+16 -30
View File
@@ -32,9 +32,9 @@ void tik_set_usersettings(tik_context* ctx, settings* usersettings)
ctx->usersettings = usersettings;
}
void tik_get_titlekey(tik_context* ctx, u8 key[0x10])
const unsigned char* tik_get_titlekey(tik_context* ctx)
{
memcpy(key, ctx->titlekey, 0x10);
return ctx->titlekey.valid ? ctx->titlekey.data : NULL;
}
void tik_get_titleid(tik_context* ctx, u8 titleid[8])
@@ -48,38 +48,24 @@ void tik_get_iv(tik_context* ctx, u8 iv[16])
memcpy(iv, ctx->tik.title_id, 8);
}
void tik_decrypt_titlekey(tik_context* ctx, u8 decryptedkey[0x10])
int tik_decrypt_titlekey(tik_context* ctx, u8 decryptedkey[0x10])
{
u8 iv[16];
u8* keyX = settings_get_common_keyX(ctx->usersettings);
const u8 keyYs[6][16] = {
// application titles (eShop titles)
{0xd0, 0x7b, 0x33, 0x7f, 0x9c, 0xa4, 0x38, 0x59, 0x32, 0xa2, 0xe2, 0x57, 0x23, 0x23, 0x2e, 0xb9},
// system titles
{0x0c, 0x76, 0x72, 0x30, 0xf0, 0x99, 0x8f, 0x1c, 0x46, 0x82, 0x82, 0x02, 0xfa, 0xac, 0xbe, 0x4c},
// these are unused
{0xc4, 0x75, 0xcb, 0x3a, 0xb8, 0xc7, 0x88, 0xbb, 0x57, 0x5e, 0x12, 0xa1, 0x09, 0x07, 0xb8, 0xa4},
{0xe4, 0x86, 0xee, 0xe3, 0xd0, 0xc0, 0x9c, 0x90, 0x2f, 0x66, 0x86, 0xd4, 0xc0, 0x6f, 0x64, 0x9f},
{0xed, 0x31, 0xba, 0x9c, 0x04, 0xb0, 0x67, 0x50, 0x6c, 0x44, 0x97, 0xa3, 0x5b, 0x78, 0x04, 0xfc},
{0x5e, 0x66, 0x99, 0x8a, 0xb4, 0xe8, 0x93, 0x16, 0x06, 0x85, 0x0f, 0xd7, 0xa1, 0x6d, 0xd7, 0x55},
};
u8 key[16];
u8* commonkey = settings_get_common_key(ctx->usersettings, ctx->tik.commonkey_idx);
memset(decryptedkey, 0, 0x10);
if (!keyX)
if (!commonkey)
{
fprintf(stdout, "Warning, could not read common key.\n");
fprintf(stdout, "Error, could not read common key.\n");
return 1;
}
else
{
ctr_aes_keygen(keyX, keyYs[(ctx->tik.title_id[3] & 0x10) ? 1 : 0], key);
memset(iv, 0, 0x10);
memcpy(iv, ctx->tik.title_id, 8);
memset(iv, 0, 0x10);
memcpy(iv, ctx->tik.title_id, 8);
ctr_init_cbc_decrypt(&ctx->aes, key, iv);
ctr_decrypt_cbc(&ctx->aes, ctx->tik.encrypted_title_key, decryptedkey, 0x10);
}
ctr_init_cbc_decrypt(&ctx->aes, commonkey, iv);
ctr_decrypt_cbc(&ctx->aes, ctx->tik.encrypted_title_key, decryptedkey, 0x10);
return 0;
}
void tik_process(tik_context* ctx, u32 actions)
@@ -93,7 +79,7 @@ void tik_process(tik_context* ctx, u32 actions)
fseeko64(ctx->file, ctx->offset, SEEK_SET);
fread((u8*)&ctx->tik, 1, sizeof(eticket), ctx->file);
tik_decrypt_titlekey(ctx, ctx->titlekey);
ctx->titlekey.valid = tik_decrypt_titlekey(ctx, ctx->titlekey.data) == 0 ? 1 : 0;
if (actions & InfoFlag)
{
@@ -122,8 +108,8 @@ void tik_print(tik_context* ctx)
memdump(stdout, "Encrypted Titlekey: ", tik->encrypted_title_key, 0x10);
if (settings_get_common_keyX(ctx->usersettings))
memdump(stdout, "Decrypted Titlekey: ", ctx->titlekey, 0x10);
if (ctx->titlekey.valid)
memdump(stdout, "Decrypted Titlekey: ", ctx->titlekey.data, 0x10);
memdump(stdout, "Ticket ID: ", tik->ticket_id, 0x08);
fprintf(stdout, "Ticket Version: %d\n", getle16(tik->ticket_version));