diff --git a/build/tools/sctools/auto_preinst_rom/Makefile b/build/tools/sctools/auto_preinst_rom/Makefile index 6135573..7b3cd70 100644 --- a/build/tools/sctools/auto_preinst_rom/Makefile +++ b/build/tools/sctools/auto_preinst_rom/Makefile @@ -30,6 +30,10 @@ SRCS = main.c key.c font.c text.c mprintf.c logprintf.c \ myimport.c pre_install.c \ my_rsa_sign.c my_aes.c my_sign.c +# my_rsa.c + + + # hatamotolib.cpp @@ -42,12 +46,90 @@ ES_DIR = $(ROOT)/add-ins/es ES_LATEST = $(shell ls -d $(ES_DIR)/es-sdk-*|sort|tail -n 1) ES_ROOT = $(ES_LATEST) + +BSAFE_DIR = $(TWLSDK_ROOT)/build/libraries/crypto/common/mes + + LINCLUDES = ../common/src ../copy_dst/src \ $(TWLSDK_ROOT)/build/libraries/lcfg/ARM9.TWL/include \ $(TWLSDK_ROOT)/build/libraries/nam/ARM9.TWL/include \ $(TWLSDK_ROOT)/build/libraries/ese/common/include \ + $(BSAFE_DIR)/src \ + $(BSAFE_DIR)/include/r_com \ + $(BSAFE_DIR)/include \ + $(BSAFE_DIR)/bn \ + $(BSAFE_DIR)/bio \ + $(BSAFE_DIR)/pubkey/pk \ + $(BSAFE_DIR)/pubkey/rsa \ + $(BSAFE_DIR)/pubkey/pk_op \ + $(BSAFE_DIR)/pubkey/pk_data \ + $(BSAFE_DIR)/ber \ + $(BSAFE_DIR)/berobj \ + $(BSAFE_DIR)/buffer \ + $(BSAFE_DIR)/data/eitems \ + $(BSAFE_DIR)/data/flag \ + $(BSAFE_DIR)/data/lhash \ + $(BSAFE_DIR)/data/op \ + $(BSAFE_DIR)/data/reslist \ + $(BSAFE_DIR)/data/stack \ + $(BSAFE_DIR)/data/strtbl \ + $(BSAFE_DIR)/err \ + $(BSAFE_DIR)/evp \ + $(BSAFE_DIR)/library \ + $(BSAFE_DIR)/objects \ + $(BSAFE_DIR)/os/cpuid \ + $(BSAFE_DIR)/os/lock \ + $(BSAFE_DIR)/os/memory \ + $(BSAFE_DIR)/os/perf \ + $(BSAFE_DIR)/os/rand \ + $(BSAFE_DIR)/os/surr \ + $(BSAFE_DIR)/digest/sha \ + $(BSAFE_DIR)/random \ + $(BSAFE_DIR)/cipher/aes \ + $(BSAFE_DIR)/module/crypto \ + $(BSAFE_DIR)/module/crypto/nat_meth \ + $(BSAFE_DIR)/key \ $(ES_ROOT)/twl/include $(ES_ROOT)/common/lib/ec/include + + + + + + +MACRO_FLAGS += \ + -DOPT_32_BIT \ + -DRCOM_LEAN \ + -DR_LIBRARY_BUILD \ + -DFLAT_INC \ + -DNO_SOCK \ + -DNO_SPLIT \ + -DNO_FP_API \ + -DNO_R_DIAG \ + -DNO_STDIO_H \ + -DNO_STDLIB_H \ + -DNO_PK_TO_OP_FUNCS \ + -DNO_PKCS8 \ + -DNO_CRYPTO \ + -DNO_CERT \ + -DNO_HDW \ + -DNO_TIME_H \ + -DNO_SHA224 \ + -DNO_SHA256 \ + -DNO_SHA384 \ + -DNO_SHA512 \ + -DNO_FP_API \ + -DNO_BIO \ +# -DBN_LIBRARY_SMALL \ +# -DNO_RSA -DNO_SHA1 \ +# -DNO_R_LOCK \ + +MACRO_FLAGS += -DSMALL_CODE_SIZE +MACRO_FLAGS += -DOPT_BN_ASM +MACRO_FLAGS += -DOPT_SHA1_ASM -DOPT_SHA1_ARM +# MACRO_FLAGS += -include subst_external_symbols.h + + LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) ../wifilib/$(TWL_BUILDTYPE) # LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) diff --git a/build/tools/sctools/auto_preinst_rom/src/main.c b/build/tools/sctools/auto_preinst_rom/src/main.c index e7a6ee9..d35c4dd 100644 --- a/build/tools/sctools/auto_preinst_rom/src/main.c +++ b/build/tools/sctools/auto_preinst_rom/src/main.c @@ -487,10 +487,7 @@ OS_TPrintf("%s %s %d\n", __FILE__,__FUNCTION__,__LINE__ ); (void)start_my_thread(THREAD_COMMAND_DOWNLOAD_APP); } - - - mfprintf(tc[1], "\fAuto Pre-install Tool"); - + mfprintf(tc[1], "\fAuto Pre-install Tool\n"); /* ユニークIDは表示しなくていい? */ m_set_palette(tc[1], M_TEXT_COLOR_LIGHTBLUE ); diff --git a/build/tools/sctools/common/src/my_fs_util.c b/build/tools/sctools/common/src/my_fs_util.c index b67ac2d..f35ea49 100644 --- a/build/tools/sctools/common/src/my_fs_util.c +++ b/build/tools/sctools/common/src/my_fs_util.c @@ -2029,81 +2029,6 @@ BOOL MydataSaveEncrypt(const char *path, void *pData, int size, FSFile *log_fd) } -#if 0 -BOOL MydataLoad(const char *path, void *pBuffer, int size, FSFile *log_fd) -{ - FSFile f; - BOOL bSuccess; - // u32 fileSize; - s32 readSize = 0; - - FS_InitFile(&f); - - bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_R); - if( ! bSuccess ) { - miya_log_fprintf(log_fd, "Failed Open File %s\n",__FUNCTION__); - miya_log_fprintf(log_fd, " path=%s\n", path ); - miya_log_fprintf(log_fd, " res=%s\n", my_fs_util_get_fs_result_word( FS_GetArchiveResultCode(path) )); - return FALSE; - } - readSize = FS_ReadFile(&f, pBuffer, (s32)size); - if( readSize != size ) { - miya_log_fprintf(log_fd, "Failed Read File: %s\n",path); - } - bSuccess = FS_CloseFile(&f); - if( ! bSuccess ) { - miya_log_fprintf(log_fd, "Failed Close File\n"); - miya_log_fprintf(log_fd, " %s\n", my_fs_util_get_fs_result_word( FS_GetArchiveResultCode(path))); - } - - return TRUE; -} -#endif - -#if 0 -BOOL MydataSave(const char *path, void *pData, int size, FSFile *log_fd) -{ - - FSFile f; - // BOOL flag; - BOOL bSuccess; - FSResult res; - FSResult fsResult; - s32 writtenSize; - - FS_InitFile(&f); - - bSuccess = FS_OpenFileEx(&f, path, FS_FILEMODE_W); - if( ! bSuccess ) { - FS_CreateFileAuto( path, FS_PERMIT_W|FS_PERMIT_R); - bSuccess = FS_OpenFileEx(&f, path , FS_FILEMODE_W ); - if( ! bSuccess ) { - res = FS_GetArchiveResultCode( path ); - miya_log_fprintf(NULL, "%s file open error %s\n", __FUNCTION__,path ); - miya_log_fprintf(NULL, " Failed open file:%s\n", my_fs_util_get_fs_result_word( res )); - return FALSE; - } - } - - fsResult = FS_SetFileLength(&f, 0); - if( fsResult != FS_RESULT_SUCCESS ) { - } - - writtenSize = FS_WriteFile(&f, pData, (s32)size); - if( writtenSize != size ) { - return FALSE; - } - - FS_FlushFile(&f); - bSuccess = FS_CloseFile(&f); - if( bSuccess ) { - - } - return TRUE; -} -#endif - -//BOOL TitleIDLoad(const char *path, u64 **pBuffer, int *count, char *log_file_name) BOOL TitleIDLoad(const char *path, MY_USER_APP_TID **pBuffer, int *count, char *log_file_name) { FSFile f; @@ -2265,7 +2190,6 @@ BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_ if( ( pData != NULL ) && ( count != 0 ) ) { /* 16文字だから */ - // if( (count*sizeof(u64)) != FS_WriteFile(&f, pData, (s32)(count*sizeof(u64)) )) { if( (count*sizeof(MY_USER_APP_TID)) != FS_WriteFile(&f, pData, (s32)(count*sizeof(MY_USER_APP_TID)) )) { res = FS_GetArchiveResultCode( path ); miya_log_fprintf(log_fd, "%s file write error %s\n", __FUNCTION__,path ); @@ -2275,7 +2199,6 @@ BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_ } else { int j; - // u64 *ptr = pData; MY_USER_APP_TID *ptr = pData; if( ptr != NULL && count > 0 ) { @@ -2307,7 +2230,7 @@ BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_ } -BOOL TitleIDLoadETicketOnly(const char *path, u64 **pBuffer, int *count, char *log_file_name) +BOOL TitleIDLoadETicketOnly(const char *path, MY_USER_ETICKET_TID **pBuffer, int *count, char *log_file_name) { FSFile f; BOOL bSuccess; @@ -2348,9 +2271,9 @@ BOOL TitleIDLoadETicketOnly(const char *path, u64 **pBuffer, int *count, char *l } *count = id_count; - size = (int)sizeof(u64) * id_count; + size = (int)sizeof(MY_USER_ETICKET_TID) * id_count; - *pBuffer = (u64 *)OS_Alloc( (u32)size ); + *pBuffer = (MY_USER_ETICKET_TID *)OS_Alloc( (u32)size ); if( *pBuffer == NULL ) { ret_flag = FALSE; miya_log_fprintf(log_fd, "%s Failed memory alloc size %d\n",__FUNCTION__, size); @@ -2379,7 +2302,7 @@ BOOL TitleIDLoadETicketOnly(const char *path, u64 **pBuffer, int *count, char *l return ret_flag; } -BOOL TitleIDSaveETicketOnly(const char *path, u64 *pData, int count, char *log_file_name ) +BOOL TitleIDSaveETicketOnly(const char *path, MY_USER_ETICKET_TID *pData, int count, char *log_file_name ) { FSFile f; BOOL bSuccess; @@ -2449,20 +2372,20 @@ BOOL TitleIDSaveETicketOnly(const char *path, u64 *pData, int count, char *log_f if( ( pData != NULL ) && ( count != 0 ) ) { /* 16文字だから */ - if( (count*sizeof(u64)) != FS_WriteFile(&f, pData, (s32)(count*sizeof(u64)) )) { + if( (count*sizeof(MY_USER_ETICKET_TID)) != FS_WriteFile(&f, pData, (s32)(count*sizeof(MY_USER_ETICKET_TID)) )) { res = FS_GetArchiveResultCode( path ); miya_log_fprintf(log_fd, "%s file write error %s\n", __FUNCTION__,path ); - miya_log_fprintf(log_fd, " Failed write file:%s\n", my_fs_util_get_fs_result_word( res )); + miya_log_fprintf(log_fd, " Failed write file:%s\n", my_fs_util_get_fs_result_word(res)); ret_flag = FALSE; goto function_end; } else { int j; - u64 *ptr = pData; + MY_USER_ETICKET_TID *ptr = pData; if( ptr != NULL && count > 0 ) { for( j = 0 ; j < count ; j++ ) { - miya_log_fprintf(log_fd,"No. %d 0x%016llx\n",j, *ptr); + miya_log_fprintf(log_fd,"No. %d 0x%016llx\n",j, ptr->tid); ptr++; } } @@ -3312,87 +3235,3 @@ int copy_r( MY_DIR_ENTRY_LIST **headp, const char *path_dst, const char *path_sr return ret_value; } - -void write_debug_data(void) -{ - // CopyFile( dst <= src ); - CopyFile("sdmc:/m00.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m01.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m02.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m03.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m04.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m05.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m06.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m07.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m08.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m09.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m10.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m11.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m12.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m13.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m14.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m15.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m16.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m17.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m18.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m19.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m20.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m22.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m23.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m24.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m25.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m26.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m27.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m28.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m29.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m30.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m31.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m32.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m33.sav" , "nand:/sys/log/sysmenu.log", NULL); - CopyFile("sdmc:/m34.sav" , "nand:/sys/log/sysmenu.log", NULL); - - /* - PrintDirEntryListBackword-----Start - src name = nand:/title - dst name = sdmc:/miya_find_title_save - src name = nand:/title/0003000f - dst name = sdmc:/miya_find_title_save/0003000f - src name = nand:/title/0003000f/484e4341 - dst name = sdmc:/miya_find_title_save/0003000f/484e4341 - src name = nand:/title/0003000f/484e4341/data - dst name = sdmc:/miya_find_title_save/0003000f/484e4341/data --- src name = nand:/title/0003000f/484e4341/data/private.sav - dst name = sdmc:/miya_find_title_save/0003000f/484e4341/data/private.sav - src name = nand:/title/00030017 - dst name = sdmc:/miya_find_title_save/00030017 - src name = nand:/title/00030017/484e4141 - dst name = sdmc:/miya_find_title_save/00030017/484e4141 - src name = nand:/title/00030017/484e4141/data - dst name = sdmc:/miya_find_title_save/00030017/484e4141/data --- src name = nand:/title/00030017/484e4141/data/public.sav - dst name = sdmc:/miya_find_title_save/00030017/484e4141/data/public.sav - PrintDirEntryListBackward-----End - */ - -#if 0 - -nand:/title/0003000f -nand:/title/0003000f/484e4341 -nand:/title/0003000f/484e4341/content -nand:/title/0003000f/484e4841 -nand:/title/0003000f/484e4841/data -nand:/title/0003000f/484e4841/content -nand:/title/0003000f/484e4c41 -nand:/title/0003000f/484e4c41/data - -nand:/title/00030015 -nand:/title/00030015/484e4241 -nand:/title/00030015/484e4241/data -nand:/title/00030015/484e4241/content - CopyFile("nand:/title/00030017/ - CopyFile("nand:/ticket/public.sav" , "nand:/sys/log/sysmenu.log"); - CopyFile("nand:/ticket/00030015 - CopyFile("nand:/ticket/00030015/miya.sav" , "nand:/sys/log/sysmenu.log"); -#endif -} - diff --git a/build/tools/sctools/common/src/my_fs_util.h b/build/tools/sctools/common/src/my_fs_util.h index ee12e1c..273549d 100644 --- a/build/tools/sctools/common/src/my_fs_util.h +++ b/build/tools/sctools/common/src/my_fs_util.h @@ -25,6 +25,12 @@ typedef struct { BOOL install_success_flag; } MY_USER_APP_TID; +typedef struct { + u64 tid; + int es_version; + int ticket_version; +} MY_USER_ETICKET_TID; + BOOL my_fs_Tid_To_GameCode(u64 tid, char *gcode); @@ -75,8 +81,8 @@ BOOL MydataSaveEncrypt(const char *path, void *pData, int size, FSFile *log_fd); BOOL TitleIDSave(const char *path, MY_USER_APP_TID *pData, int count, char *log_file_name); BOOL TitleIDLoad(const char *path, MY_USER_APP_TID **pBuffer, int *count, char *log_file_name); -BOOL TitleIDSaveETicketOnly(const char *path, u64 *pData, int count, char *log_file_name ); -BOOL TitleIDLoadETicketOnly(const char *path, u64 **pBuffer, int *count, char *log_file_name); +BOOL TitleIDSaveETicketOnly(const char *path, MY_USER_ETICKET_TID *pData, int count, char *log_file_name ); +BOOL TitleIDLoadETicketOnly(const char *path, MY_USER_ETICKET_TID **pBuffer, int *count, char *log_file_name); BOOL CopyFile(const char *dst_path, const char *src_path, FSFile *log_fd ); diff --git a/build/tools/sctools/common/src/my_rsa_sign.c b/build/tools/sctools/common/src/my_rsa_sign.c index 45b9abd..2aa2757 100644 --- a/build/tools/sctools/common/src/my_rsa_sign.c +++ b/build/tools/sctools/common/src/my_rsa_sign.c @@ -4,6 +4,9 @@ #include #include + + + #define MY_RSA_SIGN_DEBUG 1 @@ -15,49 +18,139 @@ #include "my_rsa_sign.h" -static const unsigned char rsa512_sec[]={ - 0x30,0x82,0x02,0x5c,0x02,0x01,0x00,0x02,0x81,0x81,0x00,0xeb,0x72,0xe2,0x05,0x45, - 0x37,0xd9,0x78,0x35,0xb5,0x8d,0x70,0x9a,0xe7,0x17,0x42,0xe9,0xf3,0x26,0x73,0x4e, - 0xdf,0x5e,0x96,0x5f,0xcf,0xf2,0xf1,0x2d,0xc0,0x41,0x31,0xb6,0x3b,0xe8,0xa4,0xd7, - 0x70,0xdb,0x3c,0xfd,0x66,0x0d,0xea,0x2f,0xb8,0x7b,0xf0,0x2d,0x70,0xe0,0xf1,0x05, - 0x55,0xe6,0x33,0x8f,0x3a,0xde,0x79,0xce,0xd0,0x11,0xbf,0xda,0x78,0xe1,0xef,0x8b, - 0x0e,0x2e,0xa7,0xe2,0x61,0x88,0x58,0x90,0x1d,0x0c,0x6d,0x5b,0x40,0xbf,0x6f,0xc7, - 0x18,0xde,0xe8,0xfd,0xd9,0xd9,0x1e,0xb9,0xe4,0xa3,0x4d,0x04,0x39,0x4b,0x8f,0x5b, - 0x13,0xad,0x14,0x0b,0xf8,0x53,0xbc,0xae,0x72,0x91,0x6b,0xcd,0xf9,0x39,0x8d,0x17, - 0x3d,0xc8,0xee,0xc9,0xcc,0x95,0x35,0x38,0xb0,0x80,0x53,0x02,0x03,0x01,0x00,0x01, - 0x02,0x81,0x80,0x52,0x5d,0x37,0xef,0xfc,0x0c,0xd3,0x88,0x97,0xd1,0x51,0x0f,0x49, - 0x40,0xfa,0x04,0x30,0x6f,0x1c,0xce,0x4d,0x93,0x1a,0x35,0xa4,0x82,0xcc,0x4b,0xce, - 0x63,0x68,0xeb,0x09,0x02,0xe0,0x19,0x7a,0x3c,0x42,0x95,0x8d,0x57,0x7c,0x1a,0xa6, - 0x62,0xf5,0x08,0x49,0xfd,0x28,0xd3,0x67,0xb8,0x6a,0xf5,0x32,0x6f,0xb7,0x90,0x08, - 0xe7,0x04,0xad,0xda,0xc8,0x70,0x10,0x3a,0x61,0x92,0x9b,0x03,0x96,0x8e,0xc7,0x59, - 0x0c,0x47,0xcb,0xbd,0xb6,0x6a,0x18,0xfc,0x29,0x69,0x80,0x17,0x82,0x90,0x59,0x1b, - 0xc6,0x3d,0x89,0x64,0x76,0x4d,0x73,0x63,0x7f,0xa5,0x3a,0xf8,0x81,0x7e,0x85,0xf0, - 0x4b,0xf3,0x9e,0x0c,0x9f,0x0f,0x59,0x91,0x25,0x32,0x84,0x5f,0x01,0xb8,0xa9,0xa2, - 0xad,0x05,0x31,0x02,0x41,0x00,0xff,0xa1,0x02,0xce,0x09,0xcb,0xde,0x21,0x8c,0xe9, - 0xa1,0xe6,0x6d,0x2a,0x94,0xee,0x7a,0x66,0xc7,0x9e,0x4e,0xea,0x7c,0xb7,0xcf,0xcf, - 0x1e,0x48,0xa9,0x67,0xfe,0x0d,0xc7,0xeb,0x45,0xdc,0x86,0x4d,0x12,0x79,0xcd,0x47, - 0x4a,0x82,0xf3,0x72,0x2b,0xdf,0x56,0x74,0x32,0x3f,0x13,0x83,0x2a,0xa5,0x1a,0xdd, - 0xc8,0x32,0x31,0x7a,0x6f,0xad,0x02,0x41,0x00,0xeb,0xca,0x5f,0x89,0x5e,0x94,0x1b, - 0xb9,0x40,0x14,0x11,0x9d,0xb5,0xa7,0xaa,0x41,0xf5,0xcc,0x3c,0x4c,0x0d,0x06,0x3b, - 0xac,0x77,0xb0,0x1a,0xf0,0x03,0x7f,0x1c,0x1b,0x19,0xea,0x71,0x0e,0x2f,0x08,0x7a, - 0x30,0xdc,0x16,0x5e,0xd5,0x4a,0x83,0x4a,0x2b,0x34,0x88,0x0b,0x34,0xba,0x78,0x17, - 0xc8,0x5c,0x1f,0x3e,0x83,0x8f,0x74,0xaf,0xff,0x02,0x41,0x00,0x9a,0x75,0xff,0xeb, - 0xdf,0x9f,0x80,0x29,0x65,0xac,0x06,0x57,0xe5,0xbe,0xc5,0x1f,0x14,0x76,0xe6,0x47, - 0x92,0x07,0xbe,0x34,0x93,0x2c,0xe3,0x6f,0xa4,0x6d,0x61,0xe5,0x91,0xd7,0x67,0xef, - 0x48,0x77,0x69,0xa2,0x8f,0x97,0x74,0x3e,0x8c,0x3b,0x80,0xa7,0x02,0xe6,0x53,0x36, - 0x29,0xe4,0x27,0xf0,0x7b,0xbb,0x67,0x2c,0x6d,0x19,0x3e,0x41,0x02,0x40,0x1a,0x2f, - 0x59,0x38,0xba,0x26,0x1c,0x86,0x10,0x15,0xaa,0x77,0x27,0x2b,0x3d,0x7f,0x21,0xba, - 0xd2,0x9f,0x67,0x7a,0xdf,0xb9,0xa1,0x79,0x0f,0x24,0xc4,0x5e,0xa3,0x52,0x67,0x28, - 0x5f,0xf1,0xc1,0x4b,0x61,0xb6,0x77,0x5a,0x8c,0xa1,0x87,0x2a,0xd9,0x5b,0xbe,0xf3, - 0xb4,0xba,0xe0,0x52,0x61,0xe1,0xc0,0x49,0x3f,0xfa,0x00,0x3e,0x67,0x25,0x02,0x40, - 0x58,0x37,0xc2,0x52,0x77,0x1b,0xd1,0xc9,0x05,0x62,0x18,0x12,0x5e,0xb4,0xad,0x0a, - 0x9e,0x15,0x29,0x3e,0xa5,0x25,0xdc,0x6c,0x83,0x12,0x8c,0x72,0x47,0xa2,0x1e,0x01, - 0xb9,0xc2,0xb9,0xa1,0xe9,0x83,0x34,0xb0,0xf0,0xc2,0xf7,0xfd,0x58,0x5a,0xfa,0xf7, - 0xbc,0x17,0x77,0x20,0x77,0xfa,0xcb,0xfd,0x2f,0xe4,0xf3,0x9f,0xea,0x10,0x5c,0x02 +#include +#include +#include +#include +#include +#include + +/* + */ +int R_CDECL CRYPTOi__RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to, + RSA *pkey, int padding); + +PK_METH * R_CDECL CRYPTOi__PK_METH_rsa_pkcs1_public(void); + + +//#define RSA_PublicKey_from_binary d2i_RSAPublicKey +RSA * R_CDECL CRYPTOi__d2i_RSAPublicKey(RSA **key, unsigned char **buf, long length); + + +typedef struct CRYPTORSAContext_local +{ + RSA *key; +} +CRYPTORSAContext_local; + + +static s32 miya_CRYPTO_RSA_DecryptInit(CRYPTORSAContext *context, + CRYPTORSADecryptInitParam *param) +{ + CRYPTORSAContext_local *lc; + + unsigned char *p; + + // 型サイズチェック + // ここでエラーが発生した場合はCRYPTO_RSA_CONTEXT_SIZEを修正すること{ + SDK_ASSERT(sizeof(CRYPTORSAContext) == sizeof(CRYPTORSAContext_local)); + + lc = (CRYPTORSAContext_local*)context; + + p = (unsigned char *)param->key; //test_keys->test_key; + + lc->key = CRYPTOi__d2i_RSAPublicKey(NULL, &p, (long)param->key_len); + if(lc->key == NULL) + { + return -1; + } + + return 0; +} + + +static int get_dec_shift_len(void *buf, int buf_len) +{ + u8 *p; + + for(p = buf; p < (u8*)buf + buf_len; p++) + { + if(*p != '\0') + { + return (int)(p - buf); + } + } + + return -1; +} + +static s32 miya_CRYPTO_RSA_Decrypt(CRYPTORSAContext *context, + CRYPTORSADecryptParam *param) +{ + // int len, shift; + int len; + CRYPTORSAContext_local *lc; + + + + + lc = (CRYPTORSAContext_local*)context; + + lc->key->rsa_meth = CRYPTOi__PK_METH_rsa_pkcs1_public(); + +#if (RSA_GENERATE_TEST == 1) + if (PK_CTX_set(lc->key->pk_ctx, lc->key->rsa_meth, lc->key->key, 0, 0) != 0) + { + return -1; + } +#endif + + memset(param->out, 0, param->out_size); + + + if ((len = CRYPTOi__RSA_public_decrypt((int)param->in_len, param->in, param->out, lc->key, RSA_PKCS1_PADDING)) <= 0) + { + return -1; + } + + // 復号文字列の後にもデータが残っているため、直後の値を0にする + *((u8*)param->out + len) = 0; + + return len; +} + + +#if 1 +static const unsigned char rsa512_pub[]={ + 0x30,0x81,0x89,0x02,0x81,0x81,0x00,0xeb,0x72,0xe2,0x05,0x45,0x37,0xd9,0x78,0x35, + 0xb5,0x8d,0x70,0x9a,0xe7,0x17,0x42,0xe9,0xf3,0x26,0x73,0x4e,0xdf,0x5e,0x96,0x5f, + 0xcf,0xf2,0xf1,0x2d,0xc0,0x41,0x31,0xb6,0x3b,0xe8,0xa4,0xd7,0x70,0xdb,0x3c,0xfd, + 0x66,0x0d,0xea,0x2f,0xb8,0x7b,0xf0,0x2d,0x70,0xe0,0xf1,0x05,0x55,0xe6,0x33,0x8f, + 0x3a,0xde,0x79,0xce,0xd0,0x11,0xbf,0xda,0x78,0xe1,0xef,0x8b,0x0e,0x2e,0xa7,0xe2, + 0x61,0x88,0x58,0x90,0x1d,0x0c,0x6d,0x5b,0x40,0xbf,0x6f,0xc7,0x18,0xde,0xe8,0xfd, + 0xd9,0xd9,0x1e,0xb9,0xe4,0xa3,0x4d,0x04,0x39,0x4b,0x8f,0x5b,0x13,0xad,0x14,0x0b, + 0xf8,0x53,0xbc,0xae,0x72,0x91,0x6b,0xcd,0xf9,0x39,0x8d,0x17,0x3d,0xc8,0xee,0xc9, + 0xcc,0x95,0x35,0x38,0xb0,0x80,0x53,0x02,0x03,0x01,0x00,0x01 +}; +#else + +static const unsigned char rsa512_pub[]={ + 0x30,0x81,0x9f,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01, + 0x05,0x00,0x03,0x81,0x8d,0x00,0x30,0x81,0x89,0x02,0x81,0x81,0x00,0xeb,0x72,0xe2, + 0x05,0x45,0x37,0xd9,0x78,0x35,0xb5,0x8d,0x70,0x9a,0xe7,0x17,0x42,0xe9,0xf3,0x26, + 0x73,0x4e,0xdf,0x5e,0x96,0x5f,0xcf,0xf2,0xf1,0x2d,0xc0,0x41,0x31,0xb6,0x3b,0xe8, + 0xa4,0xd7,0x70,0xdb,0x3c,0xfd,0x66,0x0d,0xea,0x2f,0xb8,0x7b,0xf0,0x2d,0x70,0xe0, + 0xf1,0x05,0x55,0xe6,0x33,0x8f,0x3a,0xde,0x79,0xce,0xd0,0x11,0xbf,0xda,0x78,0xe1, + 0xef,0x8b,0x0e,0x2e,0xa7,0xe2,0x61,0x88,0x58,0x90,0x1d,0x0c,0x6d,0x5b,0x40,0xbf, + 0x6f,0xc7,0x18,0xde,0xe8,0xfd,0xd9,0xd9,0x1e,0xb9,0xe4,0xa3,0x4d,0x04,0x39,0x4b, + 0x8f,0x5b,0x13,0xad,0x14,0x0b,0xf8,0x53,0xbc,0xae,0x72,0x91,0x6b,0xcd,0xf9,0x39, + 0x8d,0x17,0x3d,0xc8,0xee,0xc9,0xcc,0x95,0x35,0x38,0xb0,0x80,0x53,0x02,0x03,0x01, + 0x00,0x01 }; +#endif -static const int rsa512_sec_len = sizeof rsa512_sec; +static const int rsa512_pub_len = sizeof rsa512_pub; static void* MyAlloc(u32 size) @@ -146,21 +239,19 @@ int RsaTestDecrypt(char *input, int in_len, char *output, int outlen) メンバ keyで指定したアドレスにはDER形式のRSA秘密鍵イメージ全体を格納してください。 */ - - key = (void *)OS_Alloc(rsa512_sec_len); + key = (void *)OS_Alloc(rsa512_pub_len); if( key == NULL ) { OS_TPrintf("Error:alloc %s %d",__FUNCTION__,__LINE__); return 0; } - memcpy(key, (void*)rsa512_sec, rsa512_sec_len); - + memcpy(key, (void*)rsa512_pub, rsa512_pub_len); // decinitparam.key = (void*)rsa512_sec; decinitparam.key = key; - decinitparam.key_len = rsa512_sec_len; - result = CRYPTO_RSA_DecryptInit(&context, &decinitparam); - CheckAndPrintErr(result, "CRYPTO_RSA_DecryptInit"); + decinitparam.key_len = rsa512_pub_len; + result = miya_CRYPTO_RSA_DecryptInit(&context, &decinitparam); + CheckAndPrintErr(result, "CRYPTO_RSA_DecryptInit"); /* typedef struct CRYPTORSADecryptParam { @@ -188,12 +279,12 @@ int RsaTestDecrypt(char *input, int in_len, char *output, int outlen) decparam.out = output; decparam.out_size = (u32)outlen; - outcount = 0; - - outcount = CRYPTO_RSA_Decrypt(&context, &decparam); + // outcount = 0; + outcount = miya_CRYPTO_RSA_Decrypt(&context, &decparam); CheckAndPrintErr(result, "CRYPTO_RSA_Decrypt"); + result = CRYPTO_RSA_DecryptTerminate(&context); CheckAndPrintErr(result, "CRYPTO_RSA_DecryptTerminate"); diff --git a/build/tools/sctools/common/src/my_sign.c b/build/tools/sctools/common/src/my_sign.c index 89087da..23d26d4 100644 --- a/build/tools/sctools/common/src/my_sign.c +++ b/build/tools/sctools/common/src/my_sign.c @@ -48,7 +48,7 @@ static BOOL my_sign_check(MY_SIGN_SIGNATURE *encrypted_sign, u8 *buf, int buf_si // OS_TPrintf("decrypt outlen = %d\n",outlen); if( outlen < MY_SIGN_HASH_SIZE ) { - OS_TPrintf("line=%d RSA Decrypt error! outlen=%d\n",__LINE__,outlen); + OS_TPrintf("%s %d:RSA Decrypt error! outlen=%d\n",__FUNCTION__, __LINE__,outlen); return FALSE; } diff --git a/build/tools/sctools/common/src/mydata.h b/build/tools/sctools/common/src/mydata.h index 723899f..3516ce8 100644 --- a/build/tools/sctools/common/src/mydata.h +++ b/build/tools/sctools/common/src/mydata.h @@ -3,7 +3,7 @@ #define MY_DATA_VERSION_MAJOR 1 -#define MY_DATA_VERSION_MINOR 6 +#define MY_DATA_VERSION_MINOR 7 typedef struct { u8 version_major; diff --git a/build/tools/sctools/common/src/myimport.c b/build/tools/sctools/common/src/myimport.c index 1388a8d..5f96a35 100644 --- a/build/tools/sctools/common/src/myimport.c +++ b/build/tools/sctools/common/src/myimport.c @@ -56,6 +56,11 @@ typedef struct NAMTadInfo NAMTadInfo; +c:/twlsdk/add-ins/es/es-sdk-20090216/twl/include/estypes.h + +typedef u8 ESVersion; // 8-bit data structure version +typedef u16 ESTitleVersion; // 16-bit title version +typedef ESTitleId ESSysVersion; // 64-bit system software version */ diff --git a/build/tools/sctools/common/src/pre_install.c b/build/tools/sctools/common/src/pre_install.c index 6c82983..3fce560 100644 --- a/build/tools/sctools/common/src/pre_install.c +++ b/build/tools/sctools/common/src/pre_install.c @@ -24,6 +24,7 @@ static PRE_INSTALL_FILE *pre_install_file_list = NULL; + #if 1 //char *pre_install_search_tid(u64 tid, FSFile *log_fd); static BOOL pre_install_discard_list(void); @@ -40,8 +41,10 @@ BOOL pre_install_get_version(u64 tid, u16 *version) if (tid == titleInfoTmp.titleId) { if( version != NULL ) { *version = titleInfoTmp.version; +#if 0 OS_TPrintf( "tid=0x%08x%08x version = %d\n", (u32)(titleInfoTmp.titleId >> 32), (u32)(titleInfoTmp.titleId & 0xffffffff), titleInfoTmp.version); +#endif return TRUE; } } @@ -50,13 +53,38 @@ BOOL pre_install_get_version(u64 tid, u16 *version) } -BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, FSFile *log_fd) +BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, u8 *es_ver, u16 *ticket_ver, FSFile *log_fd) { + +#if 0 +typedef u64 ESTitleId; /* 64-bit title identity */ +typedef u64 ESTicketId; /* 64-bit ticket id */ +typedef u8 ESVersion; /* 8-bit data structure version */ +typedef u16 ESTitleVersion; /* 16-bit title version */ +typedef ESTitleId ESSysVersion; /* 64-bit system software version */ + +typedef struct { + ESVersion version; /* eTicket data structure version */ + ESTicketId ticketId; /* eTicket ID */ + ESId deviceId; /* device ID */ + ESTitleId titleId; /* title ID */ + ESSysAccessMask sysAccessMask; /* 16 bit cidx mask */ + u16 ticketVersion; /* 16 bit ticket version */ + u32 accessTitleId; /* 32 bit title id for access control*/ + u32 accessTitleMask; /* 32 bit title id mask */ + u8 licenseType; /* for infrastructure use */ + ESTicketReserved reserved; /* 48 bytes reserved info */ + u8 audit; /* for infrastructure use */ + ESCidxMask cidxMask; /* 512 bits of cidx mask */ + ESLpEntry limits[ES_MAX_LIMIT_TYPE]; /* limit algorithm and limit */ +} ESTicketView; +#endif + ESError rv; ESTicketView *ticketViews; u32 numTickets; u32 i; - + if( flag == NULL ) { return FALSE; } @@ -109,11 +137,15 @@ BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, FSFile *log_f /* common */ *flag = 1; /* 1個でもcommonがあればcommon扱い!? */ + *es_ver = ticketViews[i].version; + *ticket_ver = ticketViews[i].ticketVersion; break; } else { /* personalized */ *flag = 2; + *es_ver = ticketViews[i].version; + *ticket_ver = ticketViews[i].ticketVersion; } } @@ -836,7 +868,7 @@ BOOL pre_install_debug(FSFile *log_fd, BOOL development_version_flag ) } BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int title_id_count, - u64 *ticket_id_array, int ticket_id_count, BOOL development_version_flag ) + MY_USER_ETICKET_TID *ticket_id_array, int ticket_id_count, BOOL development_version_flag ) { char *tad_file_name; int i; @@ -860,7 +892,7 @@ BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int /* チケットだけのインストール */ for( i = 0 ; i < ticket_id_count ; i++ ) { - tid = ticket_id_array[i]; + tid = ticket_id_array[i].tid; (void)my_fs_Tid_To_GameCode(tid, game_code_buf); mprintf(" TO %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); miya_log_fprintf(log_fd, " TO %08X %08X [%s] ", (u32)(tid >> 32), (u32)tid, game_code_buf); diff --git a/build/tools/sctools/common/src/pre_install.h b/build/tools/sctools/common/src/pre_install.h index 91e9fe9..b6ff9d2 100644 --- a/build/tools/sctools/common/src/pre_install.h +++ b/build/tools/sctools/common/src/pre_install.h @@ -17,9 +17,9 @@ extern "C" { BOOL pre_install_Cleanup_User_Titles( FSFile *log_fd ); BOOL pre_install_process( FSFile *log_fd, MY_USER_APP_TID *title_id_buf_ptr, int tile_id_count, - u64 *ticket_id_array, int ticket_id_count ,BOOL development_version_flag ); + MY_USER_ETICKET_TID *ticket_id_array, int ticket_id_count ,BOOL development_version_flag ); -BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, FSFile *log_fd); +BOOL pre_install_check_download_or_pre_install(u64 tid, int *flag, u8 *es_ver, u16 *ticket_ver, FSFile *log_fd); BOOL pre_install_get_version(u64 tid, u16 *version); BOOL pre_install_debug(FSFile *log_fd, BOOL development_version_flag ); diff --git a/build/tools/sctools/copy_dst/Makefile b/build/tools/sctools/copy_dst/Makefile index 6610068..3c285da 100644 --- a/build/tools/sctools/copy_dst/Makefile +++ b/build/tools/sctools/copy_dst/Makefile @@ -40,15 +40,37 @@ ES_DIR = $(ROOT)/add-ins/es ES_LATEST = $(shell ls -d $(ES_DIR)/es-sdk-*|sort|tail -n 1) ES_ROOT = $(ES_LATEST) +BSAFE_DIR = $(TWLSDK_ROOT)/build/libraries/crypto/common/mes + + LINCLUDES = $(TWLSDK_ROOT)/build/libraries/lcfg/ARM9.TWL/include ../common/src \ $(TWLSDK_ROOT)/build/libraries/nam/ARM9.TWL/include \ $(TWLSDK_ROOT)/build/libraries/ese/common/include \ + $(BSAFE_DIR)/src $(BSAFE_DIR)/include/r_com \ + $(BSAFE_DIR)/include $(BSAFE_DIR)/bn \ + $(BSAFE_DIR)/bio $(BSAFE_DIR)/pubkey/pk \ + $(BSAFE_DIR)/pubkey/rsa $(BSAFE_DIR)/pubkey/pk_op \ + $(BSAFE_DIR)/pubkey/pk_data $(BSAFE_DIR)/ber \ + $(BSAFE_DIR)/berobj $(BSAFE_DIR)/buffer \ + $(BSAFE_DIR)/data/eitems $(BSAFE_DIR)/data/flag \ + $(BSAFE_DIR)/data/lhash $(BSAFE_DIR)/data/op \ + $(BSAFE_DIR)/data/reslist $(BSAFE_DIR)/data/stack \ + $(BSAFE_DIR)/data/strtbl $(BSAFE_DIR)/err \ + $(BSAFE_DIR)/evp $(BSAFE_DIR)/library \ + $(BSAFE_DIR)/objects $(BSAFE_DIR)/os/cpuid \ + $(BSAFE_DIR)/os/lock $(BSAFE_DIR)/os/memory \ + $(BSAFE_DIR)/os/perf $(BSAFE_DIR)/os/rand \ + $(BSAFE_DIR)/os/surr $(BSAFE_DIR)/digest/sha \ + $(BSAFE_DIR)/random $(BSAFE_DIR)/cipher/aes \ + $(BSAFE_DIR)/module/crypto $(BSAFE_DIR)/module/crypto/nat_meth \ + $(BSAFE_DIR)/key \ $(ES_ROOT)/twl/include $(ES_ROOT)/common/lib/ec/include LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) ../wifilib/$(TWL_BUILDTYPE) # LLIBRARY_DIRS += $(ES_ROOT)/twl/lib/$(TWL_BUILDTYPE) + LLIBRARIES += libecx$(TWL_LIBSUFFIX).a \ liblcfg$(TWL_LIBSUFFIX).a \ libnhttp.nssl$(TWL_LIBSUFFIX).a \ @@ -62,6 +84,37 @@ LLIBRARIES += libecx$(TWL_LIBSUFFIX).a \ libsfs$(TWL_LIBSUFFIX).a \ libna$(TWL_LIBSUFFIX).a \ +MACRO_FLAGS += \ + -DOPT_32_BIT \ + -DRCOM_LEAN \ + -DR_LIBRARY_BUILD \ + -DFLAT_INC \ + -DNO_SOCK \ + -DNO_SPLIT \ + -DNO_FP_API \ + -DNO_R_DIAG \ + -DNO_STDIO_H \ + -DNO_STDLIB_H \ + -DNO_PK_TO_OP_FUNCS \ + -DNO_PKCS8 \ + -DNO_CRYPTO \ + -DNO_CERT \ + -DNO_HDW \ + -DNO_TIME_H \ + -DNO_SHA224 \ + -DNO_SHA256 \ + -DNO_SHA384 \ + -DNO_SHA512 \ + -DNO_FP_API \ + -DNO_BIO \ +# -DBN_LIBRARY_SMALL \ +# -DNO_RSA -DNO_SHA1 \ +# -DNO_R_LOCK \ + +MACRO_FLAGS += -DSMALL_CODE_SIZE +MACRO_FLAGS += -DOPT_BN_ASM +MACRO_FLAGS += -DOPT_SHA1_ASM -DOPT_SHA1_ARM + MAKEROM_ROMROOT = ../files diff --git a/build/tools/sctools/copy_dst/src/main.c b/build/tools/sctools/copy_dst/src/main.c index 0a3f093..59687df 100644 --- a/build/tools/sctools/copy_dst/src/main.c +++ b/build/tools/sctools/copy_dst/src/main.c @@ -642,7 +642,7 @@ static BOOL RestoreFromSDCard7(void) char game_code_buf[5]; int is_personalized; u64 tid; - u64 *eticket_only_id_buf = NULL; + MY_USER_ETICKET_TID *eticket_only_id_buf = NULL; int num_of_eticket_only_titles = 0; title_id_count = 0; @@ -886,6 +886,10 @@ static BOOL RestoreFromSDCard7(void) development_console_flag ) ) { ret_flag = FALSE; } + + if( eticket_only_id_buf ) { + OS_Free(eticket_only_id_buf ); + } } hatamotolib_log_end(); diff --git a/build/tools/sctools/copy_org/src/main.c b/build/tools/sctools/copy_org/src/main.c index cd2b1e3..a0000b2 100644 --- a/build/tools/sctools/copy_org/src/main.c +++ b/build/tools/sctools/copy_org/src/main.c @@ -99,10 +99,10 @@ static LCFGTWLHWSecureInfo hws_info; #define NAM_TITLE_ID_S 128 -static NAMTitleId array_eticket_only_titles[NAM_TITLE_ID_S]; -// static int array_eticket_only_titles_version[NAM_TITLE_ID_S]; + + +static MY_USER_ETICKET_TID array_eticket_only_titles[NAM_TITLE_ID_S]; static NAMTitleId array_app_titles[NAM_TITLE_ID_S]; -// static int array_app_titles_version[NAM_TITLE_ID_S]; static int num_of_eticket_only_titles = 0; static int num_of_app_titles = 0; @@ -171,6 +171,8 @@ static int Check_User_Titles_ETicket_Only(void) char game_code[5]; int common_or_personalized_flag; // u16 version = 0; + u8 es_version; + u16 ticket_version; // num = NAM_GetNumTitles(); num = NAM_GetNumInstalledTitles(); @@ -213,22 +215,15 @@ static int Check_User_Titles_ETicket_Only(void) /* user app. */ // OS_TPrintf(" usr.:%3d:0x%llx %s\n", i, id, game_code); common_or_personalized_flag = 1; - if( FALSE == pre_install_check_download_or_pre_install(id, &common_or_personalized_flag, NULL) ) { + if( FALSE == pre_install_check_download_or_pre_install(id, &common_or_personalized_flag, &es_version, &ticket_version, NULL) ) { OS_TPrintf(" pre_install_check_download_or_pre_install failed\n"); } else { if( common_or_personalized_flag == 1 ) { 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; - } - else { - array_eticket_only_titles_version[user_title_count] = -1; - } -#endif + array_eticket_only_titles[user_title_count].tid = id; + array_eticket_only_titles[user_title_count].es_version = (int)es_version; + array_eticket_only_titles[user_title_count].ticket_version = (int)ticket_version; user_title_count++; } else { @@ -565,6 +560,8 @@ static BOOL SDBackupToSDCard7(void) BOOL flag = TRUE; int common_or_presonalized_flag; u16 version; + u8 es_version; + u16 ticket_version; /* タイトルリストの生成 */ /* @@ -628,17 +625,16 @@ static BOOL SDBackupToSDCard7(void) if( ptr != NULL && count != 0 ) { for( j = 0 ; j < count ; j++ ) { - // OS_TPrintf("No. %d 0x%016llx\n",j, *ptr); - // mfprintf(tc[2],"No. %d 0x%016llx\n",j,*ptr); OS_TPrintf("No. %d 0x%016llx",j, ptr->tid); - mfprintf(tc[2],"No. %d 0x%016llx",j,ptr->tid); + // mfprintf(tc[2],"No. %d 0x%08x%08x",j, (ptr->tid) >> 32, (ptr->tid & 0xffffffff)); + mfprintf(tc[2],"No. %d 0x%016llx",j, ptr->tid); if( no_sd_clean_flag == TRUE ) { - mprintf("No. %d 0x%016llx",j,ptr->tid); + // mprintf("No. %d 0x%08x%08x",j, (ptr->tid) >> 32, (ptr->tid & 0xffffffff)); + mprintf("No. %d 0x%016llx",j, ptr->tid); } - if( TRUE == pre_install_check_download_or_pre_install(ptr->tid, &common_or_presonalized_flag, NULL) ) { - + if( TRUE == pre_install_check_download_or_pre_install(ptr->tid, &common_or_presonalized_flag, &es_version, &ticket_version, NULL) ) { if( TRUE == pre_install_get_version(ptr->tid, &version) ) { ptr->version = (int)version; } diff --git a/build/tools/sctools/cryptopc/main.c b/build/tools/sctools/cryptopc/main.c index 8f4fc49..e4c74b2 100644 --- a/build/tools/sctools/cryptopc/main.c +++ b/build/tools/sctools/cryptopc/main.c @@ -24,6 +24,8 @@ typedef unsigned long u32; typedef unsigned long long u64; +#define MAX_OUTPUT_FILE_NAME_LEN (1024*4) + #include "my_sign.h" /* データのハッシュの単位は32KB @@ -89,6 +91,7 @@ static int my_sign_make(MY_SIGN_SIGNATURE *encrypted_sign, u8 *buf, int buf_size MY_SIGN_SIGNATURE temp_sign; int outlen = 0; RSA *rsa_key; + int ret_flag = 0; // int i; if( 0 != fseek(fp_key, 0 , SEEK_SET) ) { @@ -112,7 +115,8 @@ static int my_sign_make(MY_SIGN_SIGNATURE *encrypted_sign, u8 *buf, int buf_size } else { fprintf(stderr, "Error:d2i_RSAPrivateKey_fp(read key) NULL!\n"); - return -1; + ret_flag = -1; + goto end; } @@ -143,21 +147,32 @@ static int my_sign_make(MY_SIGN_SIGNATURE *encrypted_sign, u8 *buf, int buf_size #define RSA_PKCS1_PADDING_SIZE 11 #endif + #if 1 - if(rsaSize != (outlen = RSA_public_encrypt(rsaSize - RSA_PKCS1_PADDING_SIZE, - (u8 *)&temp_sign, (u8 *)encrypted_sign, - rsa_key, RSA_PKCS1_PADDING ))) { + + if(rsaSize != (outlen = +#if 0 + RSA_public_encrypt +#else + RSA_private_encrypt +#endif + (rsaSize - RSA_PKCS1_PADDING_SIZE, + (u8 *)&temp_sign, (u8 *)encrypted_sign, + rsa_key, RSA_PKCS1_PADDING ))) { fprintf(stderr,"encrypt error rsaSize=%d outlen=%d\n",rsaSize, outlen); - return -1; + ret_flag = -1; + goto end; } #else if(rsaSize != (outlen = RSA_public_encrypt(rsaSize, (u8 *)&temp_sign, (u8 *)encrypted_sign, rsa_key, RSA_NO_PADDING ))) { fprintf(stderr,"encrypt error rsaSize=%d outlen=%d\n",rsaSize, outlen); - return -1; + ret_flag = -1; + goto end; } #endif + end: RSA_free(rsa_key); return 0; @@ -183,290 +198,250 @@ int main(int ac, char *argv[]) u8 block_buf_in[MY_SIGN_BLOCK_SIZE]; u8 block_buf_out[MY_SIGN_BLOCK_SIZE]; + char output_file_name[MAX_OUTPUT_FILE_NAME_LEN]; u8 aes_key_buf[AES_KEY_BYTE_LEN]; AES_KEY aes_key; unsigned char aes_iv[ AES_BLOCK_SIZE ]; - - printf("hash offset = %d\n",offsetof(MY_SIGN_SIGNATURE, hash)); + // printf("hash offset = %d\n",offsetof(MY_SIGN_SIGNATURE, hash)); ERR_load_crypto_strings(); - if( ac == 4 ) { - if( 0 != stat( argv[2], &st_buf) ) { - fprintf(stderr, "failed to stat %s\n",argv[2]); - goto end; - } + if( ac != 3 ) { + fprintf(stderr,"Invalid argument!\n"); + fprintf(stderr,"Usage: %s xx.der tadfile\n", argv[0]); + return -1; + } - if( (fp_key = fopen( argv[1], "rb" )) == NULL ) { - fprintf(stderr, "failed to fopen %s\n",argv[1]); - goto end; - } - else { -#if 0 - rsa_key = RSA_new(); - - if( rsa_key == NULL ) { - fprintf(stdout,"Error:RSA_new(key alloc) NULL!\n"); - } - d2i_RSAPrivateKey_fp(fp_key, &rsa_key); - // d2i_RSAPublicKey_fp(fp_key, &key); - - if( rsa_key != NULL ) { - // RSA_print_fp(stdout, key, 0); - rsaSize = RSA_size( rsa_key ); - printf("rsaSize = %d bit\n",rsaSize * 8); - } - else { - fprintf(stderr, "Error:d2i_RSAPrivateKey_fp(read key) NULL!\n"); - goto end; - } -#endif - } - - if( (fp_in = fopen( argv[2], "rb" )) == NULL ) { - fprintf(stderr, "Error:failed to fopen input file(%s)\n",argv[2]); - goto end; - } - - if( (fp_out = fopen( argv[3], "wb+" )) == NULL ) { - fprintf(stderr, "failed to fopen output file(%s)\n",argv[3]); - goto end; - } + if( 0 != stat( argv[2], &st_buf) ) { + fprintf(stderr, "failed to stat %s\n",argv[2]); + goto end; + } - /* - FILE format: - ----- - MY_SIGN_HEADER header; - MY_SIGN_SIGNATURE header_sign; - L2_sign_table[0]; - L2_sign_table[1]; - L2_sign_table[2]; - L2_sign_table[ ]; - . - . - aes_enc_data_block[0](32KB) - aes_enc_data_block[1](32KB) - aes_enc_data_block[2](32KB) - . - . - . - . + if( (fp_key = fopen( argv[1], "rb" )) == NULL ) { + fprintf(stderr, "failed to fopen %s\n",argv[1]); + goto end; + } + + if( strlen( argv[2] ) >= (MAX_OUTPUT_FILE_NAME_LEN - 3) /* "en_"の分 */ ) { + fprintf(stderr, "Error: too long - input file name(%s)\n",argv[2]); + goto end; + } + + if( (fp_in = fopen( argv[2], "rb" )) == NULL ) { + fprintf(stderr, "Error:failed to fopen input file(%s)\n",argv[2]); + goto end; + } + + memset( output_file_name, 0, MAX_OUTPUT_FILE_NAME_LEN); + + strcpy( output_file_name, "en_"); + strcat( output_file_name, argv[2] ); + if( (fp_out = fopen( output_file_name, "wb+" )) == NULL ) { + fprintf(stderr, "failed to fopen output file(%s)\n",output_file_name); + goto end; + } + + /* + FILE format: + ----- + MY_SIGN_HEADER header; + MY_SIGN_SIGNATURE header_sign; + L2_sign_table[0]; + L2_sign_table[1]; + L2_sign_table[2]; + L2_sign_table[ ]; + . + . + aes_enc_data_block[0](32KB) + aes_enc_data_block[1](32KB) + aes_enc_data_block[2](32KB) + . + . + . + . - #define RSA_SIZE 128 - #define HASH_SIZE 0x20 - #define BLOCK_SIZE (32*1024) + #define RSA_SIZE 128 + #define HASH_SIZE 0x20 + #define BLOCK_SIZE (32*1024) - typedef struct { - u8 hash[HASH_SIZE]; - u8 dummy[RSA_SIZE - HASH_SIZE]; - } SIGNATURE; + typedef struct { + u8 hash[HASH_SIZE]; + u8 dummy[RSA_SIZE - HASH_SIZE]; + } SIGNATURE; - typedef struct { - u32 magic_code; - u32 org_file_size; - u32 num_of_block; - u32 file_offset_L2_sign_table; - u32 file_offset_data_block; - SIGNATURE L2_sign; - } HEADER; + typedef struct { + u32 magic_code; + u32 org_file_size; + u32 num_of_block; + u32 file_offset_L2_sign_table; + u32 file_offset_data_block; + SIGNATURE L2_sign; + } HEADER; - */ + */ - memset(&header, 0 , sizeof(MY_SIGN_HEADER)); - header.magic_code = 0xdeadbeef; - header.org_file_size = st_buf.st_size; - header.num_of_block = st_buf.st_size / MY_SIGN_BLOCK_SIZE; - if( st_buf.st_size % MY_SIGN_BLOCK_SIZE ) { - header.num_of_block += 1; - } + memset(&header, 0 , sizeof(MY_SIGN_HEADER)); + header.magic_code = 0xdeadbeef; + header.org_file_size = st_buf.st_size; + header.num_of_block = st_buf.st_size / MY_SIGN_BLOCK_SIZE; + if( st_buf.st_size % MY_SIGN_BLOCK_SIZE ) { + header.num_of_block += 1; + } - header.file_offset_L2_sign_table = sizeof(MY_SIGN_HEADER) + sizeof(MY_SIGN_SIGNATURE); + header.file_offset_L2_sign_table = sizeof(MY_SIGN_HEADER) + sizeof(MY_SIGN_SIGNATURE); - L2_sign_table_size = sizeof(MY_SIGN_SIGNATURE) * header.num_of_block; + L2_sign_table_size = sizeof(MY_SIGN_SIGNATURE) * header.num_of_block; - header.file_offset_data_block = header.file_offset_L2_sign_table + L2_sign_table_size; + header.file_offset_data_block = header.file_offset_L2_sign_table + L2_sign_table_size; - printf("header.org_file_size = %d\n",(int)header.org_file_size); - printf("header.num_of_block = %d\n",(int)header.num_of_block); + printf("header.org_file_size = %d\n",(int)header.org_file_size); + printf("header.num_of_block = %d\n",(int)header.num_of_block); - L2_sign_table = (MY_SIGN_SIGNATURE *)malloc( L2_sign_table_size ); - if( L2_sign_table == NULL ) { - fprintf(stderr,"L2_sign_table malloc error %s %d\n",__FUNCTION__,__LINE__); - goto end; - } - memset(L2_sign_table, 0 , L2_sign_table_size ); - L2_sign_table_temp = L2_sign_table; - - -#if 1 - if( 0 != fseek(fp_out, header.file_offset_data_block , SEEK_SET) ) { - fprintf(stderr,"fseek error %s %d\n",__FUNCTION__,__LINE__); - goto end; - } -#else - if( sizeof(MY_SIGN_HEADER) != (outlen = fwrite( &header, 1, sizeof(MY_SIGN_HEADER), fp_out)) ) { - fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); - rsa_error_flag = 1; - goto end; - } - - - /* ヘッダーの署名を出力ファイルに書き込み */ - - if( sizeof(MY_SIGN_SIGNATURE) != (outlen = fwrite( &header_sign, 1, sizeof(MY_SIGN_SIGNATURE), fp_out)) ) { - fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); - rsa_error_flag = 1; - goto end; - } - - /* L2_signテーブルを出力ファイルに書き込み */ - -#endif - - - block_no = 0; - - while( (readlen = fread(block_buf_in, 1, MY_SIGN_BLOCK_SIZE, fp_in)) > 0 ) { - - if( readlen < MY_SIGN_BLOCK_SIZE ) { - for( i = readlen ; i < MY_SIGN_BLOCK_SIZE ; i++ ) { - block_buf_in[i] = 0; /* padding.. */ - } - } - - - /* 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_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++ ) { - // AES_encrypt( &(block_buf_in[AES_BLOCK_SIZE*i]), &(block_buf_out[AES_BLOCK_SIZE*i]),&aes_key); - AES_cbc_encrypt( &(block_buf_in[AES_BLOCK_SIZE*i]), &(block_buf_out[AES_BLOCK_SIZE*i]), - AES_BLOCK_SIZE, &aes_key, aes_iv, AES_ENCRYPT ); - - } - - if( MY_SIGN_BLOCK_SIZE != (outlen = fwrite( block_buf_out, 1, MY_SIGN_BLOCK_SIZE, fp_out)) ) { - fprintf(stderr,"Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); - fprintf(stderr,"%s\n", ERR_error_string(ERR_get_error(), NULL)); - rsa_error_flag = 1; - goto end; - } - - /* データブロックの署名計算 */ - if( 0 != my_sign_make( L2_sign_table_temp , block_buf_out, MY_SIGN_BLOCK_SIZE ) ) { - fprintf(stderr,"make Data Block signature error line=%d\n",__LINE__); - rsa_error_flag = 1; - goto end; - } - - - - L2_sign_table_temp++; - block_no++; - } - - printf("last block no = %d\n",block_no); - - - /* L2signテーブルの署名計算 */ - if( 0 != my_sign_make(&header.L2_sign, (u8 *)L2_sign_table, L2_sign_table_size ) ) { - fprintf(stderr,"make L2_sign signature error line=%d\n",__LINE__); - rsa_error_flag = 1; - goto end; - } - - - /* ヘッダーの署名計算 */ - if( 0 != my_sign_make(&header_sign, (u8 *)&header, sizeof(MY_SIGN_HEADER)) ) { - fprintf(stderr,"make signature error line=%d\n",__LINE__); - rsa_error_flag = 1; - goto end; - } - - - - - /* ヘッダーを出力ファイルに書き込み */ - if( 0 != fseek(fp_out, 0 , SEEK_SET) ) { - fprintf(stderr,"fseek error %s %d\n",__FUNCTION__,__LINE__); - goto end; - } - - if( sizeof(MY_SIGN_HEADER) != (outlen = fwrite( &header, 1, sizeof(MY_SIGN_HEADER), fp_out)) ) { - fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); - rsa_error_flag = 1; - goto end; - } - - - /* ヘッダーの署名を出力ファイルに書き込み */ - - if( sizeof(MY_SIGN_SIGNATURE) != (outlen = fwrite( &header_sign, 1, sizeof(MY_SIGN_SIGNATURE), fp_out)) ) { - fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); - rsa_error_flag = 1; - goto end; - } - - /* L2_signテーブルを出力ファイルに書き込み */ - if( L2_sign_table_size != (outlen = fwrite( (u8 *)L2_sign_table, 1, L2_sign_table_size, fp_out)) ) { - fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); - rsa_error_flag = 1; - goto end; - } - - - if( rsa_error_flag == 1 ) { - printf("Error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__); - } - - end: - - if( L2_sign_table != NULL ) { - free( L2_sign_table ); - } - - if( fp_in ) { - fclose(fp_in); - } - - if( fp_out ) { - fclose(fp_out); - } - - if( fp_key ) { - fclose(fp_key); - } - - if( rsa_error_flag == 0 ) { - printf("success\n"); - } - - // fprintf(stderr,"Invalid argument!\n"); - // fprintf(stderr,"Usage: %s\n", argv[0]); - // fprintf(stderr,"Usage: %s SerialNo.(32bit) filename.dat\n", argv[0]); - + L2_sign_table = (MY_SIGN_SIGNATURE *)malloc( L2_sign_table_size ); + if( L2_sign_table == NULL ) { + fprintf(stderr,"L2_sign_table malloc error %s %d\n",__FUNCTION__,__LINE__); + goto end; } + memset(L2_sign_table, 0 , L2_sign_table_size ); + L2_sign_table_temp = L2_sign_table; + + + + if( 0 != fseek(fp_out, header.file_offset_data_block , SEEK_SET) ) { + fprintf(stderr,"fseek error %s %d\n",__FUNCTION__,__LINE__); + goto end; + } + + block_no = 0; + + while( (readlen = fread(block_buf_in, 1, MY_SIGN_BLOCK_SIZE, fp_in)) > 0 ) { + + if( readlen < MY_SIGN_BLOCK_SIZE ) { + for( i = readlen ; i < MY_SIGN_BLOCK_SIZE ; i++ ) { + block_buf_in[i] = 0; /* padding.. */ + } + } + + + /* AESキーのセット */ + 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]; + } + + 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++ ) { + // AES_encrypt( &(block_buf_in[AES_BLOCK_SIZE*i]), &(block_buf_out[AES_BLOCK_SIZE*i]),&aes_key); + AES_cbc_encrypt( &(block_buf_in[AES_BLOCK_SIZE*i]), &(block_buf_out[AES_BLOCK_SIZE*i]), + AES_BLOCK_SIZE, &aes_key, aes_iv, AES_ENCRYPT ); + + } + + if( MY_SIGN_BLOCK_SIZE != (outlen = fwrite( block_buf_out, 1, MY_SIGN_BLOCK_SIZE, fp_out)) ) { + fprintf(stderr,"Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); + fprintf(stderr,"%s\n", ERR_error_string(ERR_get_error(), NULL)); + rsa_error_flag = 1; + goto end; + } + + /* データブロックの署名計算 */ + if( 0 != my_sign_make( L2_sign_table_temp , block_buf_out, MY_SIGN_BLOCK_SIZE ) ) { + fprintf(stderr,"make Data Block signature error line=%d\n",__LINE__); + rsa_error_flag = 1; + goto end; + } + + + + L2_sign_table_temp++; + block_no++; + } + + printf("last block no = %d\n",block_no); + + + /* L2signテーブルの署名計算 */ + if( 0 != my_sign_make(&header.L2_sign, (u8 *)L2_sign_table, L2_sign_table_size ) ) { + fprintf(stderr,"make L2_sign signature error line=%d\n",__LINE__); + rsa_error_flag = 1; + goto end; + } + + + /* ヘッダーの署名計算 */ + if( 0 != my_sign_make(&header_sign, (u8 *)&header, sizeof(MY_SIGN_HEADER)) ) { + fprintf(stderr,"make signature error line=%d\n",__LINE__); + rsa_error_flag = 1; + goto end; + } + + + + + /* ヘッダーを出力ファイルに書き込み */ + if( 0 != fseek(fp_out, 0 , SEEK_SET) ) { + fprintf(stderr,"fseek error %s %d\n",__FUNCTION__,__LINE__); + goto end; + } + + if( sizeof(MY_SIGN_HEADER) != (outlen = fwrite( &header, 1, sizeof(MY_SIGN_HEADER), fp_out)) ) { + fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); + rsa_error_flag = 1; + goto end; + } + + + /* ヘッダーの署名を出力ファイルに書き込み */ + + if( sizeof(MY_SIGN_SIGNATURE) != (outlen = fwrite( &header_sign, 1, sizeof(MY_SIGN_SIGNATURE), fp_out)) ) { + fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); + rsa_error_flag = 1; + goto end; + } + + /* L2_signテーブルを出力ファイルに書き込み */ + if( L2_sign_table_size != (outlen = fwrite( (u8 *)L2_sign_table, 1, L2_sign_table_size, fp_out)) ) { + fprintf(stderr, "Error:fwrite line=%d outlen=%d\n", __LINE__,outlen); + rsa_error_flag = 1; + goto end; + } + + + if( rsa_error_flag == 1 ) { + printf("Error: %s %s %d\n",__FILE__,__FUNCTION__,__LINE__); + } + + end: + + if( L2_sign_table != NULL ) { + free( L2_sign_table ); + } + + if( fp_in ) { + fclose(fp_in); + } + + if( fp_out ) { + fclose(fp_out); + } + + if( fp_key ) { + fclose(fp_key); + } + + if( rsa_error_flag == 0 ) { + printf("success\n"); + } + return 0; }