Skip to content

Commit

Permalink
Merge pull request #25 from 3DSGuy/unstable
Browse files Browse the repository at this point in the history
makerom 0.13 and ctrtool fixes
  • Loading branch information
applestash committed Sep 22, 2014
2 parents 666ed4d + c7f8b18 commit 4fcc466
Show file tree
Hide file tree
Showing 23 changed files with 713 additions and 686 deletions.
18 changes: 18 additions & 0 deletions ctrtool/exheader.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ void exheader_set_programid(exheader_context* ctx, u8 programid[8])
memcpy(ctx->programid, programid, 8);
}

void exheader_set_hash(exheader_context* ctx, u8 hash[32])
{
memcpy(ctx->hash, hash, 32);
}

void exheader_set_counter(exheader_context* ctx, u8 counter[16])
{
memcpy(ctx->counter, counter, 16);
Expand Down Expand Up @@ -94,6 +99,19 @@ void exheader_read(exheader_context* ctx, u32 actions)
}
}

int exheader_hash_valid(exheader_context* ctx)
{
u8 hash[32];
ctr_sha_256((u8*)&ctx->header, 0x400, hash);

if(memcmp(ctx->hash,hash,0x20)){
fprintf(stderr, "Error, exheader hash mismatch. Wrong key?\n");
return 0;
}

return 1;
}

int exheader_programid_valid(exheader_context* ctx)
{
if (!settings_get_ignore_programid(ctx->usersettings))
Expand Down
3 changes: 3 additions & 0 deletions ctrtool/exheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ typedef struct
settings* usersettings;
u8 partitionid[8];
u8 programid[8];
u8 hash[32];
u8 counter[16];
u8 key[16];
u32 offset;
Expand All @@ -134,6 +135,7 @@ void exheader_set_size(exheader_context* ctx, u32 size);
void exheader_set_partitionid(exheader_context* ctx, u8 partitionid[8]);
void exheader_set_counter(exheader_context* ctx, u8 counter[16]);
void exheader_set_programid(exheader_context* ctx, u8 programid[8]);
void exheader_set_hash(exheader_context* ctx, u8 hash[32]);
void exheader_set_encrypted(exheader_context* ctx, u32 encrypted);
void exheader_set_key(exheader_context* ctx, u8 key[16]);
void exheader_set_usersettings(exheader_context* ctx, settings* usersettings);
Expand All @@ -143,6 +145,7 @@ int exheader_process(exheader_context* ctx, u32 actions);
const char* exheader_getvalidstring(int valid);
void exheader_print(exheader_context* ctx);
void exheader_verify(exheader_context* ctx);
int exheader_hash_valid(exheader_context* ctx);
int exheader_programid_valid(exheader_context* ctx);
void exheader_determine_key(exheader_context* ctx, u32 actions);

Expand Down
3 changes: 2 additions & 1 deletion ctrtool/ncch.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ void ncch_process(ncch_context* ctx, u32 actions)
exheader_set_usersettings(&ctx->exheader, ctx->usersettings);
exheader_set_partitionid(&ctx->exheader, ctx->header.partitionid);
exheader_set_programid(&ctx->exheader, ctx->header.programid);
exheader_set_hash(&ctx->exheader, ctx->header.extendedheaderhash);
exheader_set_counter(&ctx->exheader, exheadercounter);
exheader_set_key(&ctx->exheader, ctx->key);
exheader_set_encrypted(&ctx->exheader, ctx->encrypted);
Expand Down Expand Up @@ -361,7 +362,7 @@ void ncch_process(ncch_context* ctx, u32 actions)

if (result && ncch_get_exheader_size(ctx))
{
if (!exheader_programid_valid(&ctx->exheader))
if (!exheader_hash_valid(&ctx->exheader))
return;

result = exheader_process(&ctx->exheader, actions);
Expand Down
10 changes: 9 additions & 1 deletion ctrtool/tmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void tmd_print(tmd_context* ctx)
ctr_tmd_header_2048* header2048 = 0;
ctr_tmd_body* body = 0;
unsigned int contentcount = 0;
unsigned int savesize = 0;
unsigned int i;

if (type == TMD_RSA_2048_SHA256 || type == TMD_RSA_2048_SHA1)
Expand All @@ -104,7 +105,8 @@ void tmd_print(tmd_context* ctx)
body = tmd_get_body(ctx);

contentcount = getbe16(body->contentcount);

savesize = getle32(body->savedatasize);

fprintf(stdout, "\nTMD header:\n");
fprintf(stdout, "Signature type: %s\n", tmd_get_type_string(type));
fprintf(stdout, "Issuer: %s\n", body->issuer);
Expand All @@ -115,6 +117,12 @@ void tmd_print(tmd_context* ctx)
memdump(stdout, "Title id: ", body->titleid, 8);
fprintf(stdout, "Title type: %08x\n", getbe32(body->titletype));
fprintf(stdout, "Group id: %04x\n", getbe16(body->groupid));
if(savesize < sizeKB)
fprintf(stdout, "Save Size: %08x\n", savesize);
else if(savesize < sizeMB)
fprintf(stdout, "Save Size: %dKB (%08x)\n", savesize/sizeKB, savesize);
else
fprintf(stdout, "Save Size: %dMB (%08x)\n", savesize/sizeMB, savesize);
fprintf(stdout, "Access rights: %08x\n", getbe32(body->accessrights));
fprintf(stdout, "Title version: %04x\n", getbe16(body->titleversion));
fprintf(stdout, "Content count: %04x\n", getbe16(body->contentcount));
Expand Down
8 changes: 6 additions & 2 deletions ctrtool/tmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ typedef struct
unsigned char titleid[8];
unsigned char titletype[4];
unsigned char groupid[2];
unsigned char padding3[62];
unsigned char savedatasize[4];
unsigned char privsavedatasize[4];
unsigned char padding3[4];
unsigned char twlflag;
unsigned char padding4[0x31];
unsigned char accessrights[4];
unsigned char titleversion[2];
unsigned char contentcount[2];
unsigned char bootcontent[2];
unsigned char padding4[2];
unsigned char padding5[2];
unsigned char hash[32];
unsigned char contentinfo[36*64];
} ctr_tmd_body;
Expand Down
2 changes: 1 addition & 1 deletion makerom/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CC = gcc
# MAKEROM Build Settings
MAKEROM_BUILD_FLAGS = #-DDEBUG
VER_MAJOR = 0
VER_MINOR = 12
VER_MINOR = 13
OUTPUT = makerom

main: build
Expand Down
42 changes: 41 additions & 1 deletion makerom/accessdesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,40 @@ void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk,
*cxiPubk = (u8*)app_fw1D_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)app_fw1D_prod_acexsig;
*cxiPubk = (u8*)app_fw1D_prod_hdrpub;
*cxiPvtk = (u8*)app_fw1D_prod_hdrpub;
}
break;
case 0x1E:
if(keys->keyset == pki_PRODUCTION){
*accessDescSig = (u8*)app_fw1E_prod_acexsig;
*cxiPubk = (u8*)app_fw1E_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)app_fw1E_prod_acexsig;
*cxiPubk = (u8*)app_fw1E_prod_hdrpub;
*cxiPvtk = (u8*)app_fw1E_prod_hdrpub;
}
break;
case 0x20:
if(keys->keyset == pki_DEVELOPMENT){
*accessDescSig = (u8*)app_fw20_dev_acexsig;
*cxiPubk = (u8*)app_fw20_dev_hdrpub;
*cxiPvtk = NULL;
}
else if(keys->keyset == pki_PRODUCTION){
if(keys->keyset == pki_PRODUCTION){
*accessDescSig = (u8*)app_fw20_prod_acexsig;
*cxiPubk = (u8*)app_fw20_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)app_fw20_prod_acexsig;
*cxiPubk = (u8*)app_fw20_prod_hdrpub;
*cxiPvtk = (u8*)app_fw20_prod_hdrpub;
}
break;
case 0x21:
if(keys->keyset == pki_DEVELOPMENT){
Expand All @@ -338,6 +353,11 @@ void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk,
*cxiPubk = (u8*)app_fw21_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)app_fw21_prod_acexsig;
*cxiPubk = (u8*)app_fw21_prod_hdrpub;
*cxiPvtk = (u8*)app_fw21_prod_hdrpub;
}
break;
case 0x23:
if(keys->keyset == pki_DEVELOPMENT){
Expand All @@ -350,13 +370,23 @@ void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk,
*cxiPubk = (u8*)app_fw23_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)app_fw23_prod_acexsig;
*cxiPubk = (u8*)app_fw23_prod_hdrpub;
*cxiPvtk = (u8*)app_fw23_prod_hdrpub;
}
break;
case 0x27:
if(keys->keyset == pki_PRODUCTION){
*accessDescSig = (u8*)app_fw27_prod_acexsig;
*cxiPubk = (u8*)app_fw27_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)app_fw27_prod_acexsig;
*cxiPubk = (u8*)app_fw27_prod_hdrpub;
*cxiPvtk = (u8*)app_fw27_prod_hdrpub;
}
break;

}
Expand All @@ -369,13 +399,23 @@ void accessdesc_GetPresetSigData(u8 **accessDescSig, u8 **cxiPubk, u8 **cxiPvtk,
*cxiPubk = (u8*)ecapp_fw20_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)ecapp_fw20_prod_acexsig;
*cxiPubk = (u8*)ecapp_fw20_prod_hdrpub;
*cxiPvtk = (u8*)ecapp_fw20_prod_hdrpub;
}
break;
case 0x23:
if(keys->keyset == pki_PRODUCTION){
*accessDescSig = (u8*)ecapp_fw23_prod_acexsig;
*cxiPubk = (u8*)ecapp_fw23_prod_hdrpub;
*cxiPvtk = NULL;
}
if(keys->keyset == pki_GATEWAY3DS){
*accessDescSig = (u8*)ecapp_fw23_prod_acexsig;
*cxiPubk = (u8*)ecapp_fw23_prod_hdrpub;
*cxiPvtk = (u8*)ecapp_fw23_prod_hdrpub;
}
break;
}
}
Expand Down
8 changes: 6 additions & 2 deletions makerom/cardinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ int SetCardInfoBitmask(cardinfo_hdr *hdr, cci_settings *set)
}

str = set->rsf->CardInfo.CryptoType;
if(!str)
bitmask |= 0;//(3*0x40);
if(!str) {
if(set->options.useExternalSdkCardInfo)
bitmask |= (3*0x40);
else
bitmask |= 0;
}
else{
int val = strtol(str,NULL,10);
if(val < 0 || val > 3) {
Expand Down
12 changes: 4 additions & 8 deletions makerom/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,13 @@ u32 GetSigHashLen(u32 sig_type)
{
switch(sig_type){
case RSA_4096_SHA1:
return 0x14;
case RSA_4096_SHA256:
return 0x20;
case RSA_2048_SHA1:
return 0x14;
case RSA_2048_SHA256:
return 0x20;
case ECC_SHA1:
return 0x14;
return SHA_1_LEN;
case RSA_4096_SHA256:
case RSA_2048_SHA256:
case ECC_SHA256:
return 0x20;
return SHA_256_LEN;
}
return 0;
}
Expand Down
6 changes: 6 additions & 0 deletions makerom/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ typedef enum
CTR_SHA_256,
} ctr_sha_modes;

typedef enum
{
SHA_1_LEN = 0x14,
SHA_256_LEN = 0x20,
} sha_hash_len;

typedef enum
{
RSA_4096_PUBK = 0,
Expand Down
2 changes: 1 addition & 1 deletion makerom/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef struct

typedef struct
{
u16 *name;
fs_romfs_char *name;
u32 name_len;

void *dir; // treated as type 'fs_dir'. This officially type 'void' to prevent self referencing problems
Expand Down
Loading

0 comments on commit 4fcc466

Please sign in to comment.