ctrtool: updated cia code

now supports plaintext cia content, and the plaintext flag works with
cia content.
This commit is contained in:
3DSGuy
2014-04-14 12:36:31 +08:00
parent 7dbb107a81
commit 00a2d35e12
3 changed files with 28 additions and 13 deletions
+25 -12
View File
@@ -40,6 +40,8 @@ void cia_save(cia_context* ctx, u32 type, u32 flags)
{ {
u32 offset; u32 offset;
u32 size; u32 size;
u16 contentflags;
u8 docrypto;
filepath* path = 0; filepath* path = 0;
ctr_tmd_body *body; ctr_tmd_body *body;
ctr_tmd_contentchunk *chunk; ctr_tmd_contentchunk *chunk;
@@ -102,15 +104,20 @@ void cia_save(cia_context* ctx, u32 type, u32 flags)
sprintf(tmpname, "%s.%04x.%08x", path->pathname, getbe16(chunk->index), getbe32(chunk->id)); sprintf(tmpname, "%s.%04x.%08x", path->pathname, getbe16(chunk->index), getbe32(chunk->id));
fprintf(stdout, "Saving content #%04x to %s\n", getbe16(chunk->index), tmpname); fprintf(stdout, "Saving content #%04x to %s\n", getbe16(chunk->index), tmpname);
ctx->iv[0] = (getbe16(chunk->index) >> 8) & 0xff; contentflags = getbe16(chunk->type);
ctx->iv[1] = getbe16(chunk->index) & 0xff; docrypto = contentflags & 1 && !(flags & PlainFlag);
ctr_init_cbc_decrypt(&ctx->aes, ctx->titlekey, ctx->iv); if(docrypto) // Decrypt if needed
{
ctx->iv[0] = (getbe16(chunk->index) >> 8) & 0xff;
ctx->iv[1] = getbe16(chunk->index) & 0xff;
cia_save_blob(ctx, tmpname, offset, getbe64(chunk->size) & 0xffffffff, 1); ctr_init_cbc_decrypt(&ctx->aes, ctx->titlekey, ctx->iv);
}
cia_save_blob(ctx, tmpname, offset, getbe64(chunk->size) & 0xffffffff, docrypto);
offset += getbe64(chunk->size) & 0xffffffff; offset += getbe64(chunk->size) & 0xffffffff;
chunk++; chunk++;
} }
@@ -218,7 +225,7 @@ void cia_process(cia_context* ctx, u32 actions)
if (actions & VerifyFlag) if (actions & VerifyFlag)
{ {
cia_verify_contents(ctx); cia_verify_contents(ctx, actions);
} }
if (actions & InfoFlag || actions & VerifyFlag) if (actions & InfoFlag || actions & VerifyFlag)
@@ -237,8 +244,9 @@ clean:
return; return;
} }
void cia_verify_contents(cia_context *ctx) void cia_verify_contents(cia_context *ctx, u32 actions)
{ {
u16 contentflags;
ctr_tmd_body *body; ctr_tmd_body *body;
ctr_tmd_contentchunk *chunk; ctr_tmd_contentchunk *chunk;
u8 *verify_buf; u8 *verify_buf;
@@ -254,15 +262,20 @@ void cia_verify_contents(cia_context *ctx)
{ {
content_size = getbe64(chunk->size) & 0xffffffff; content_size = getbe64(chunk->size) & 0xffffffff;
ctx->iv[0] = (getbe16(chunk->index) >> 8) & 0xff; contentflags = getbe16(chunk->type);
ctx->iv[1] = getbe16(chunk->index) & 0xff;
ctr_init_cbc_decrypt(&ctx->aes, ctx->titlekey, ctx->iv);
verify_buf = malloc(content_size); verify_buf = malloc(content_size);
fread(verify_buf, content_size, 1, ctx->file); fread(verify_buf, content_size, 1, ctx->file);
ctr_decrypt_cbc(&ctx->aes, verify_buf, verify_buf, content_size); if(contentflags & 1 && !(actions & PlainFlag)) // Decrypt if needed
{
ctx->iv[0] = (getbe16(chunk->index) >> 8) & 0xff;
ctx->iv[1] = getbe16(chunk->index) & 0xff;
ctr_init_cbc_decrypt(&ctx->aes, ctx->titlekey, ctx->iv);
ctr_decrypt_cbc(&ctx->aes, verify_buf, verify_buf, content_size);
}
if (ctr_sha_256_verify(verify_buf, content_size, chunk->hash) == Good) if (ctr_sha_256_verify(verify_buf, content_size, chunk->hash) == Good)
ctx->tmd.content_hash_stat[i] = 1; ctx->tmd.content_hash_stat[i] = 1;
+1 -1
View File
@@ -67,6 +67,6 @@ void cia_print(cia_context* ctx);
void cia_save(cia_context* ctx, u32 type, u32 flags); void cia_save(cia_context* ctx, u32 type, u32 flags);
void cia_process(cia_context* ctx, u32 actions); void cia_process(cia_context* ctx, u32 actions);
void cia_save_blob(cia_context *ctx, char *out_path, u32 offset, u32 size, int do_cbc); void cia_save_blob(cia_context *ctx, char *out_path, u32 offset, u32 size, int do_cbc);
void cia_verify_contents(cia_context *ctx); void cia_verify_contents(cia_context *ctx, u32 actions);
#endif // _CIA_H_ #endif // _CIA_H_
+2
View File
@@ -41,10 +41,12 @@ void tmd_process(tmd_context* ctx, u32 actions)
fseek(ctx->file, ctx->offset, SEEK_SET); fseek(ctx->file, ctx->offset, SEEK_SET);
fread(ctx->buffer, 1, ctx->size, ctx->file); fread(ctx->buffer, 1, ctx->size, ctx->file);
/*
if (actions & InfoFlag) if (actions & InfoFlag)
{ {
tmd_print(ctx); tmd_print(ctx);
} }
*/
} }
} }