From fb98fa96f8104f6a1dac0ae65b64921a769fb79b Mon Sep 17 00:00:00 2001 From: miya Date: Mon, 27 Jul 2009 06:45:15 +0000 Subject: [PATCH] git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlToolsRED@386 7061adef-622a-194b-ae81-725974e89856 --- build/tools/sctools/common/src/my_sign.c | 192 +++++++++--------- build/tools/sctools/common/src/my_sign.h | 3 + build/tools/sctools/common/src/pre_install.c | 3 + build/tools/sctools/copy_org/src/main.c | 1 + build/tools/sctools/cryptopc/Makefile | 4 +- build/tools/sctools/cryptopc/main.c | 21 +- .../sctools/make_tad_table/make_tad_table.c | 34 +++- 7 files changed, 154 insertions(+), 104 deletions(-) diff --git a/build/tools/sctools/common/src/my_sign.c b/build/tools/sctools/common/src/my_sign.c index 9acd35e..89087da 100644 --- a/build/tools/sctools/common/src/my_sign.c +++ b/build/tools/sctools/common/src/my_sign.c @@ -8,6 +8,16 @@ #define AES_KEY_BIT_LEN 256 #define AES_KEY_BYTE_LEN (AES_KEY_BIT_LEN/8) +static u8 my_sign_aes_key[AES_KEY_BYTE_LEN] = { + 0x02,0xB6,0x01,0xD8,0x01,0x80,0x01,0x77,0xB4,0x01,0xCB,0x01,0xBD,0x5F,0x18,0x0F, + 0xF6,0x39,0x9C,0xC6,0x90,0xAC,0xC1,0x0D,0x03,0x74,0x6E,0x8D,0xD1,0xBA,0x37,0x46 +}; + +static u8 my_sign_aes_iv[AES_BLOCK_SIZE] = { + 0xC3,0x85,0x93,0xFE,0xA8,0x2D,0xBF,0xFB,0xED,0x42,0xE0,0x42,0xFD,0x17,0x04,0xB0 +}; + + static BOOL my_sign_check(MY_SIGN_SIGNATURE *encrypted_sign, u8 *buf, int buf_size) { @@ -113,9 +123,6 @@ BOOL my_sign_FS_OpenFile(MY_SIGN_File *msfile, char *path) return FALSE; } - // OS_TPrintf("hash offset = %d\n",offsetof(MY_SIGN_SIGNATURE, hash)); - - RsaTestInit(); FS_InitFile(&(msfile->f)); @@ -229,32 +236,13 @@ int my_sign_FS_ReadFile(MY_SIGN_File *msfile, u8 *buf, int buf_size) u8 *user_buf_ptr; int num = 0; int readlen; + int temp_size; + BOOL cache_valid_flag; if( msfile->open_flag != TRUE ) { return -1; /* error */ } - // msfile->pos = 0; /* original file pos */ - // MY_SIGN_BLOCK_SIZE - /* - typedef struct { - u32 magic_code; - u32 org_file_size; - u32 num_of_block; - u32 file_offset_L2_sign_table; - u32 file_offset_data_block; - u32 dummy[3]; - MY_SIGN_SIGNATURE L2_sign; - } MY_SIGN_HEADER; - */ - - // header.file_offset_data_block; - - - // OS_TPrintf("%s %d\n",__FUNCTION__, __LINE__); - - - user_buf_size = buf_size; user_buf_ptr = buf; @@ -262,79 +250,97 @@ int my_sign_FS_ReadFile(MY_SIGN_File *msfile, u8 *buf, int buf_size) return 0; } + while( user_buf_size > 0 ) { + block_no = msfile->pos / MY_SIGN_BLOCK_SIZE; + block_buf_out_pos = msfile->pos % MY_SIGN_BLOCK_SIZE; - loop_0: - - block_no = msfile->pos / MY_SIGN_BLOCK_SIZE; - block_buf_out_pos = msfile->pos % MY_SIGN_BLOCK_SIZE; - - if( block_no >= (int)msfile->header.num_of_block ) { - return -1; - } - - - // OS_TPrintf("%s %d pos = %d block_no = %d\n",__FUNCTION__, __LINE__,msfile->pos, block_no); - - - enc_file_pos = (int)msfile->header.file_offset_data_block + block_no * MY_SIGN_BLOCK_SIZE; - if( FALSE == FS_SeekFile(&(msfile->f), enc_file_pos , FS_SEEK_SET) ) { - return -1; - } - - - readlen = FS_ReadFile(&(msfile->f), msfile->block_buf_in, MY_SIGN_BLOCK_SIZE); - if( readlen != MY_SIGN_BLOCK_SIZE ) { - OS_TPrintf("%s %d Failed read File readlen=%d\n",__FUNCTION__, __LINE__, readlen); - return -1; - } - - - - - L2_sign_table_temp = msfile->L2_sign_table + block_no; - - /* データブロックの署名チェック */ - if( FALSE == my_sign_check( L2_sign_table_temp, msfile->block_buf_in, MY_SIGN_BLOCK_SIZE) ) { - OS_TPrintf("Data Hash check Error!\n"); - return -1; - } - - /* AESキーのセット */ - for( i = 0 ; i < AES_KEY_BYTE_LEN ; i++ ) { - aes_key_buf[i] = (u8)i; - } - AES_set_decrypt_key(aes_key_buf, AES_KEY_BIT_LEN, &aes_key); - - for( i = 0 ; i < AES_BLOCK_SIZE ; i++ ) { - aes_iv[i] = (u8)i; - } - - memset(msfile->block_buf_out, 0 , MY_SIGN_BLOCK_SIZE); - - /* AES復号化 */ - for( i = 0 ; i < (MY_SIGN_BLOCK_SIZE / AES_BLOCK_SIZE) ; i++ ) { - AES_cbc_encrypt( &(msfile->block_buf_in[AES_BLOCK_SIZE*i]), &(msfile->block_buf_out[AES_BLOCK_SIZE*i]), - AES_BLOCK_SIZE, &aes_key, aes_iv, AES_DECRYPT ); - - } - - // block_no - while( user_buf_size ) { - if( msfile->pos >= (int)msfile->header.org_file_size ) { - goto end; - } - if( block_buf_out_pos >= MY_SIGN_BLOCK_SIZE ) { + if( block_no >= (int)msfile->header.num_of_block ) { + OS_TPrintf("%s %d pos = %d block_no = %d\n",__FUNCTION__, __LINE__,msfile->pos, block_no); break; } - *user_buf_ptr++ = msfile->block_buf_out[block_buf_out_pos]; - block_buf_out_pos++; - msfile->pos++; - user_buf_size--; - num++; - } - if( user_buf_size > 0 ) { - goto loop_0; + + // OS_TPrintf("%s %d pos = %d block_no = %d\n",__FUNCTION__, __LINE__,msfile->pos, block_no); + + enc_file_pos = (int)msfile->header.file_offset_data_block + block_no * MY_SIGN_BLOCK_SIZE; + + cache_valid_flag = FALSE; + if( msfile->now_cache == TRUE ) { + if( msfile->cache_msfile == (void *)msfile ) { + if( msfile->cache_block_no == block_no ) { + cache_valid_flag = TRUE; + } + } + } + + if( cache_valid_flag == FALSE ) { + if( FALSE == FS_SeekFile(&(msfile->f), enc_file_pos , FS_SEEK_SET) ) { + OS_TPrintf("%s %d Failed seek File pos=%d\n",__FUNCTION__, __LINE__, enc_file_pos ); + return -1; + } + + readlen = FS_ReadFile(&(msfile->f), msfile->block_buf_in, MY_SIGN_BLOCK_SIZE); + if( readlen != MY_SIGN_BLOCK_SIZE ) { + OS_TPrintf("%s %d Failed read File readlen=%d\n",__FUNCTION__, __LINE__, readlen); + return -1; + } + + L2_sign_table_temp = msfile->L2_sign_table + block_no; + + /* データブロックの署名チェック */ + if( FALSE == my_sign_check( L2_sign_table_temp, msfile->block_buf_in, MY_SIGN_BLOCK_SIZE) ) { + OS_TPrintf("Data Hash check Error!\n"); + return -1; + } + + /* AESキーのセット */ +#if 1 + for( i = 0 ; i < AES_KEY_BYTE_LEN ; i++ ) { + aes_key_buf[i] = my_sign_aes_key[i]; + } + for( i = 0 ; i < AES_BLOCK_SIZE ; i++ ) { + aes_iv[i] = my_sign_aes_iv[i]; + } +#else + for( i = 0 ; i < AES_KEY_BYTE_LEN ; i++ ) { + aes_key_buf[i] = (u8)i; + } + for( i = 0 ; i < AES_BLOCK_SIZE ; i++ ) { + aes_iv[i] = (u8)i; + } +#endif + AES_set_decrypt_key(aes_key_buf, AES_KEY_BIT_LEN, &aes_key); + + memset(msfile->block_buf_out, 0 , MY_SIGN_BLOCK_SIZE); + + /* AES復号化 */ + for( i = 0 ; i < (MY_SIGN_BLOCK_SIZE / AES_BLOCK_SIZE) ; i++ ) { + AES_cbc_encrypt( &(msfile->block_buf_in[AES_BLOCK_SIZE*i]), &(msfile->block_buf_out[AES_BLOCK_SIZE*i]), + AES_BLOCK_SIZE, &aes_key, aes_iv, AES_DECRYPT ); + + } + msfile->now_cache = TRUE; + msfile->cache_msfile = (void *)msfile; + msfile->cache_block_no = block_no; + // OS_TPrintf("%s %d cache change\n",__FUNCTION__, __LINE__); + } + + temp_size = (MY_SIGN_BLOCK_SIZE - block_buf_out_pos); + if( (msfile->pos + temp_size) >= (int)msfile->header.org_file_size ) { + temp_size = (int)msfile->header.org_file_size - msfile->pos; + } + if( temp_size > user_buf_size ) { + temp_size = user_buf_size; + } + + memcpy(user_buf_ptr, &(msfile->block_buf_out[block_buf_out_pos]), (u32)temp_size ); + user_buf_ptr += temp_size; + block_buf_out_pos += temp_size; + msfile->pos += temp_size; + user_buf_size -= temp_size; + num += temp_size; + } + // OS_TPrintf("%s %d num=%d\n",__FUNCTION__, __LINE__,num); end: return num; } diff --git a/build/tools/sctools/common/src/my_sign.h b/build/tools/sctools/common/src/my_sign.h index 1af604b..527c3e0 100644 --- a/build/tools/sctools/common/src/my_sign.h +++ b/build/tools/sctools/common/src/my_sign.h @@ -39,6 +39,9 @@ typedef struct { MY_SIGN_HEADER header; u8 *block_buf_in; u8 *block_buf_out; + BOOL now_cache; + void *cache_msfile; + int cache_block_no; } MY_SIGN_File; BOOL my_sign_FS_OpenFile(MY_SIGN_File *msfile, char *path); diff --git a/build/tools/sctools/common/src/pre_install.c b/build/tools/sctools/common/src/pre_install.c index edca138..6c82983 100644 --- a/build/tools/sctools/common/src/pre_install.c +++ b/build/tools/sctools/common/src/pre_install.c @@ -475,6 +475,9 @@ static BOOL pre_install_load_sd_file(char *path, FSFile *log_fd) int temp_filename_count; char c; + + OS_TPrintf("%s %d path=%s\n", __FUNCTION__,__LINE__,path); + my_sign_FS_InitFile(&file); bSuccess = my_sign_FS_OpenFile(&file, path); diff --git a/build/tools/sctools/copy_org/src/main.c b/build/tools/sctools/copy_org/src/main.c index 026fb78..cd2b1e3 100644 --- a/build/tools/sctools/copy_org/src/main.c +++ b/build/tools/sctools/copy_org/src/main.c @@ -221,6 +221,7 @@ static int Check_User_Titles_ETicket_Only(void) OS_TPrintf(" usr.:%3d:0x%llx %s common\n", i, id, game_code); array_eticket_only_titles[user_title_count] = id; #if 0 + /* 失敗するに決まってる、NAM_ReadTitleInfoが失敗するので。 */ if( TRUE == pre_install_get_version(id, &version) ) { array_eticket_only_titles_version[user_title_count] = (int)version; } diff --git a/build/tools/sctools/cryptopc/Makefile b/build/tools/sctools/cryptopc/Makefile index 0bd45ed..c049f79 100644 --- a/build/tools/sctools/cryptopc/Makefile +++ b/build/tools/sctools/cryptopc/Makefile @@ -2,8 +2,8 @@ PACKAGE = cryptopc OPENSSL_DIR = c:/OpenSSL/openssl-1.0.0-beta2 -# SRCS = main.c -SRCS = m.c +SRCS = main.c +# SRCS = m.c HEADS = my_sign.h diff --git a/build/tools/sctools/cryptopc/main.c b/build/tools/sctools/cryptopc/main.c index 80a333e..8f4fc49 100644 --- a/build/tools/sctools/cryptopc/main.c +++ b/build/tools/sctools/cryptopc/main.c @@ -74,6 +74,16 @@ typedef unsigned long long u64; static FILE *fp_key = NULL; static int rsaSize; +static u8 my_sign_aes_key[AES_KEY_BYTE_LEN] = { + 0x02,0xB6,0x01,0xD8,0x01,0x80,0x01,0x77,0xB4,0x01,0xCB,0x01,0xBD,0x5F,0x18,0x0F, + 0xF6,0x39,0x9C,0xC6,0x90,0xAC,0xC1,0x0D,0x03,0x74,0x6E,0x8D,0xD1,0xBA,0x37,0x46 +}; + +static u8 my_sign_aes_iv[AES_BLOCK_SIZE] = { + 0xC3,0x85,0x93,0xFE,0xA8,0x2D,0xBF,0xFB,0xED,0x42,0xE0,0x42,0xFD,0x17,0x04,0xB0 +}; + + static int my_sign_make(MY_SIGN_SIGNATURE *encrypted_sign, u8 *buf, int buf_size) { MY_SIGN_SIGNATURE temp_sign; @@ -331,13 +341,22 @@ int main(int ac, char *argv[]) /* AESキーのセット */ +#if 1 + for( i = 0 ; i < AES_KEY_BYTE_LEN ; i++ ) { + aes_key_buf[i] = my_sign_aes_key[i]; + } + for( i = 0 ; i < AES_BLOCK_SIZE ; i++ ) { + aes_iv[i] = my_sign_aes_iv[i]; + } +#else for( i = 0 ; i < AES_KEY_BYTE_LEN ; i++ ) { aes_key_buf[i] = (u8)i; } - AES_set_encrypt_key(aes_key_buf, AES_KEY_BIT_LEN, &aes_key); for( i = 0 ; i < AES_BLOCK_SIZE ; i++ ) { aes_iv[i] = (u8)i; } +#endif + AES_set_encrypt_key(aes_key_buf, AES_KEY_BIT_LEN, &aes_key); memset(block_buf_out, 0 , MY_SIGN_BLOCK_SIZE); for( i = 0 ; i < (MY_SIGN_BLOCK_SIZE / AES_BLOCK_SIZE) ; i++ ) { diff --git a/build/tools/sctools/make_tad_table/make_tad_table.c b/build/tools/sctools/make_tad_table/make_tad_table.c index 370b804..c41d008 100644 --- a/build/tools/sctools/make_tad_table/make_tad_table.c +++ b/build/tools/sctools/make_tad_table/make_tad_table.c @@ -1,6 +1,6 @@ /* [ROM用] make_tad_table.exe -dir . -o table_file.txt -var MAKEROM_TAD_ROMFILES -fdir tads -mk Makefile.inc - [SD用] make_tad_table.exe -dir . -o table_file.txt -fdir tads_sd + [SD用] make_tad_table.exe -sd -dir . -o table_file.txt -fdir sdtads */ #include @@ -201,7 +201,7 @@ static write_tad_table_form(FILE *fp, u32 tid_hi, u32 tid_lo, char *filename) } #endif -static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out, FILE *fp_mk) +static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out, FILE *fp_mk, BOOL is_sd ) { FILE *fp_in = NULL; TAD_INFO tad_info; @@ -214,7 +214,8 @@ static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out fprintf(stderr, "error: file open %s\n",path); goto end_file; } - + + /* TADファイルかどうかチェック */ if( FALSE == read_tad_info(fp_in, &tad_info) ) { // fprintf(stderr, "error:%s %d\n",__FUNCTION__,__LINE__); goto end_file; @@ -239,13 +240,23 @@ static void read_file_and_print_titleid( char *path , char *d_name, FILE *fp_out } if( fp_out ) { - fprintf(fp_out, "0x%08x%08x, %d , %d , rom:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + if( is_sd == FALSE ) { + fprintf(fp_out, "0x%08x%08x, %d , %d , rom:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + } + else { + fprintf(fp_out, "0x%08x%08x, %d , %d , sdmc:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + } if( fp_mk ) { fprintf(fp_mk, "\t\t%s \\\n", d_name); } } else { - printf("0x%08x%08x, %d , %d , rom:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + if( is_sd == FALSE ) { + printf("0x%08x%08x, %d , %d , rom:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + } + else { + printf("0x%08x%08x, %d , %d , sdmc:/%s,\n", titleid_hi, titleid_lo, 0 , 0 , d_name); + } } @@ -278,6 +289,7 @@ int main(int argc, char **argv) BOOL mk_file_flag = FALSE; BOOL var_name_flag = FALSE; BOOL file_dir_flag = FALSE; + BOOL sd_flag = FALSE; char *prog; int badops = 0; @@ -340,6 +352,9 @@ int main(int argc, char **argv) else if (strcmp(*argv,"-d") == 0 ) { debug_print_flag = TRUE; } + else if (strcmp(*argv,"-sd") == 0 ) { + sd_flag = TRUE; + } else if ( !read_file_flag ) { infile = *argv; read_file_flag = TRUE; @@ -395,7 +410,7 @@ int main(int argc, char **argv) - + /* ディレクトリ中のTADファイルをリストにして表示する。 */ while( (dr = readdir(dir)) != NULL ) { if (!strcmp(dr->d_name, ".") || !strcmp(dr->d_name, "..")) { continue; @@ -425,11 +440,14 @@ int main(int argc, char **argv) if( file_dir_flag ) { strcpy( rom_file_full_path, file_dir); strcat( rom_file_full_path, "/"); + if( sd_flag == TRUE ) { + strcat( rom_file_full_path, "en_"); + } strcat( rom_file_full_path, dr->d_name); - read_file_and_print_titleid( full_path ,rom_file_full_path, fp_out , fp_mk ); + read_file_and_print_titleid( full_path ,rom_file_full_path, fp_out , fp_mk , sd_flag); } else { - read_file_and_print_titleid( full_path ,dr->d_name, fp_out , fp_mk ); + read_file_and_print_titleid( full_path ,dr->d_name, fp_out , fp_mk , sd_flag); } } }