/*---------------------------------------------------------------------------* Project: NitroSDK - CRYPTO - demos File: main.c Copyright 2003-2008 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Date:: 2008-09-17#$ $Rev: 8556 $ $Author: okubata_ryoma $ *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* The electronic signature specific functions in the CRYPT library operation test demo *---------------------------------------------------------------------------*/ #include #include //#define PRINT_ALLOCATION static void InitializeAllocateSystem(void); static void VBlankIntr(void); static void DisplayInit(void); static void FillScreen(u16 col); static BOOL SignTest(void); static u32 GetStringLength(char* str); static void PrintBinary(u8* ptr, int len); /*---------------------------------------------------------------------------* Variable Definitions *---------------------------------------------------------------------------*/ #ifdef PRINT_ALLOCATION static u32 total_allocated_size = 0; #endif /*---------------------------------------------------------------------------* Function Definitions *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Name: MyAlloc Description: OS_Alloc wrapper function *---------------------------------------------------------------------------*/ static void* MyAlloc(u32 size) { void* ptr; #ifdef PRINT_ALLOCATION ptr = OS_Alloc(size+4); *(u32*)ptr = size; (u8*)ptr += 4; total_allocated_size += size; OS_TPrintf("Allocate %d bytes from %p: total %d bytes\n", size, ptr, total_allocated_size); #else ptr = OS_Alloc(size); #endif return ptr; } /*---------------------------------------------------------------------------* Name: MyFree Description: OS_Free wrapper function *---------------------------------------------------------------------------*/ static void MyFree(void* ptr) { #ifdef PRINT_ALLOCATION u32 size; (u8*)ptr -= 4; size = *(u32*)ptr; total_allocated_size -= size; OS_TPrintf("Free %d bytes from %p: total %d bytes\n", size, ptr, total_allocated_size); #endif OS_Free(ptr); } /*---------------------------------------------------------------------------* Name: NitroMain Description: Initialization and main loop. Arguments: None. Returns: None. *---------------------------------------------------------------------------*/ void NitroMain(void) { // Various types of initialization OS_Init(); InitializeAllocateSystem(); DisplayInit(); if (SignTest()) { // Success OS_TPrintf("------ Test Succeeded ------\n"); FillScreen(GX_RGB(0, 31, 0)); } else { // Failed OS_TPrintf("****** Test Failed ******\n"); FillScreen(GX_RGB(31, 0, 0)); } // Main loop while (TRUE) { // Waiting for the V-Blank OS_WaitVBlankIntr(); } } /*---------------------------------------------------------------------------* Name: VBlankIntr Description: V-Blank interrupt vector. Arguments: None. Returns: None. *---------------------------------------------------------------------------*/ static void VBlankIntr(void) { // Sets the IRQ check flag OS_SetIrqCheckFlag(OS_IE_V_BLANK); } /*---------------------------------------------------------------------------* Name: InitializeAllocateSystem Description: Initializes the memory allocation system within the main memory arena. Arguments: None. Returns: None. *---------------------------------------------------------------------------*/ static void InitializeAllocateSystem(void) { void *tempLo; OSHeapHandle hh; tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1); OS_SetArenaLo(OS_ARENA_MAIN, tempLo); hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi()); if (hh < 0) { OS_Panic("ARM9: Fail to create heap...\n"); } hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh); } /*---------------------------------------------------------------------------* Name: DisplayInit Description: Graphics initialization. Arguments: None. Returns: None. *---------------------------------------------------------------------------*/ static void DisplayInit(void) { GX_Init(); FX_Init(); GX_DispOff(); GXS_DispOff(); GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN); OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr); (void)OS_EnableIrqMask(OS_IE_V_BLANK); (void)GX_VBlankIntr(TRUE); // To generate V-Blank interrupt request (void)OS_EnableIrq(); GX_SetBankForLCDC(GX_VRAM_LCDC_ALL); MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE); MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE); // Clear OAM MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE); // Clear the standard palette MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE); // Clear OAM MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE); // Clear the standard palette MI_DmaFill32(3, (void *)HW_LCDC_VRAM_C, 0x7FFF7FFF, 256 * 192 * sizeof(u16)); GX_SetBankForOBJ(GX_VRAM_OBJ_256_AB); // Set VRAM-A,B for OBJ GX_SetGraphicsMode(GX_DISPMODE_VRAM_C, // VRAM mode (GXBGMode)0, // Dummy (GXBG0As)0); // Dummy GX_SetVisiblePlane(GX_PLANEMASK_OBJ); // Make OBJs visible GX_SetOBJVRamModeBmp(GX_OBJVRAMMODE_BMP_1D_128K); // 2D mapping OBJ OS_WaitVBlankIntr(); // Waiting for the end of the V-Blank interrupt GX_DispOn(); } /*---------------------------------------------------------------------------* Name: FillScreen Description: Fills the screen. Arguments: col: FillColor Returns: None. *---------------------------------------------------------------------------*/ static void FillScreen(u16 col) { MI_CpuFill16((void *)HW_LCDC_VRAM_C, col, 256 * 192 * 2); } /*---------------------------------------------------------------------------* Name: SignTest Description: Test routine for electronic signature functions. Arguments: None. Returns: TRUE if test succeeds. *---------------------------------------------------------------------------*/ #define PrintResultEq( a, b, f ) \ { OS_TPrintf( ((a) == (b)) ? "[--OK--] " : "[**NG**] " ); (f) = (f) && ((a) == (b)); } static BOOL SignTest(void) { int i; BOOL flag = TRUE; // Perform electronic signature operation test { char* d[] = { "Copyright 2003-2005 Nintendo. All rights reserved.", "Copyright 2003-2005 Nintondo. All rights reserved.", // Modified "Copyright 2003-2005 Nintendo. All rights reserved.", "Copyright 2003-2005 Nintendo. All rights reserved.", }; unsigned char sign[][128] = { { 0x75, 0xdf, 0x56, 0x82, 0x01, 0x30, 0x0c, 0x6e, 0x91, 0x27, 0x7a, 0x1a, 0x20, 0x2e, 0xd3, 0x29, 0x74, 0xb1, 0x91, 0x6c, 0xb9, 0xcd, 0x6f, 0xda, 0xc1, 0x12, 0x16, 0x5d, 0x49, 0x26, 0x9b, 0x69, 0x44, 0xab, 0xeb, 0x29, 0xee, 0x53, 0x51, 0x55, 0x05, 0x03, 0x35, 0xbf, 0xbb, 0xde, 0x2a, 0xf9, 0x37, 0xed, 0xc7, 0xe2, 0x25, 0x46, 0x26, 0xe5, 0x20, 0x6c, 0x28, 0x13, 0x8d, 0xf6, 0x92, 0x7e, 0x60, 0x35, 0x23, 0xba, 0x7c, 0x61, 0xac, 0x6b, 0xd9, 0x2f, 0x5f, 0x3c, 0x64, 0x44, 0x51, 0x11, 0x36, 0xfe, 0x6e, 0x21, 0x9c, 0x10, 0x1d, 0xe7, 0xba, 0xa1, 0x6e, 0x24, 0x63, 0x3f, 0x42, 0x4a, 0x29, 0xea, 0x3c, 0x7a, 0x8f, 0x99, 0x51, 0x09, 0xf4, 0x0b, 0x01, 0x5a, 0x05, 0x3a, 0x52, 0x53, 0xc9, 0xb7, 0xe4, 0xcc, 0x60, 0xa5, 0x53, 0x1d, 0xeb, 0x79, 0x88, 0x55, 0xc1, 0xb2, 0xfb, 0xd6, }, { 0x75, 0xdf, 0x56, 0x82, 0x01, 0x30, 0x0c, 0x6e, 0x91, 0x27, 0x7a, 0x1a, 0x20, 0x2e, 0xd3, 0x29, 0x74, 0xb1, 0x91, 0x6c, 0xb9, 0xcd, 0x6f, 0xda, 0xc1, 0x12, 0x16, 0x5d, 0x49, 0x26, 0x9b, 0x69, 0x44, 0xab, 0xeb, 0x29, 0xee, 0x53, 0x51, 0x55, 0x05, 0x03, 0x35, 0xbf, 0xbb, 0xde, 0x2a, 0xf9, 0x37, 0xed, 0xc7, 0xe2, 0x25, 0x46, 0x26, 0xe5, 0x20, 0x6c, 0x28, 0x13, 0x8d, 0xf6, 0x92, 0x7e, 0x60, 0x35, 0x23, 0xba, 0x7c, 0x61, 0xac, 0x6b, 0xd9, 0x2f, 0x5f, 0x3c, 0x64, 0x44, 0x51, 0x11, 0x36, 0xfe, 0x6e, 0x21, 0x9c, 0x10, 0x1d, 0xe7, 0xba, 0xa1, 0x6e, 0x24, 0x63, 0x3f, 0x42, 0x4a, 0x29, 0xea, 0x3c, 0x7a, 0x8f, 0x99, 0x51, 0x09, 0xf4, 0x0b, 0x01, 0x5a, 0x05, 0x3a, 0x52, 0x53, 0xc9, 0xb7, 0xe4, 0xcc, 0x60, 0xa5, 0x53, 0x1d, 0xeb, 0x79, 0x88, 0x55, 0xc1, 0xb2, 0xfb, 0xd6, }, { // Modified 0x57, 0xdf, 0x56, 0x82, 0x01, 0x30, 0x0c, 0x6e, 0x91, 0x27, 0x7a, 0x1a, 0x20, 0x2e, 0xd3, 0x29, 0x74, 0xb1, 0x91, 0x6c, 0xb9, 0xcd, 0x6f, 0xda, 0xc1, 0x12, 0x16, 0x5d, 0x49, 0x26, 0x9b, 0x69, 0x44, 0xab, 0xeb, 0x29, 0xee, 0x53, 0x51, 0x55, 0x05, 0x03, 0x35, 0xbf, 0xbb, 0xde, 0x2a, 0xf9, 0x37, 0xed, 0xc7, 0xe2, 0x25, 0x46, 0x26, 0xe5, 0x20, 0x6c, 0x28, 0x13, 0x8d, 0xf6, 0x92, 0x7e, 0x60, 0x35, 0x23, 0xba, 0x7c, 0x61, 0xac, 0x6b, 0xd9, 0x2f, 0x5f, 0x3c, 0x64, 0x44, 0x51, 0x11, 0x36, 0xfe, 0x6e, 0x21, 0x9c, 0x10, 0x1d, 0xe7, 0xba, 0xa1, 0x6e, 0x24, 0x63, 0x3f, 0x42, 0x4a, 0x29, 0xea, 0x3c, 0x7a, 0x8f, 0x99, 0x51, 0x09, 0xf4, 0x0b, 0x01, 0x5a, 0x05, 0x3a, 0x52, 0x53, 0xc9, 0xb7, 0xe4, 0xcc, 0x60, 0xa5, 0x53, 0x1d, 0xeb, 0x79, 0x88, 0x55, 0xc1, 0xb2, 0xfb, 0xd6, }, { 0x75, 0xdf, 0x56, 0x82, 0x01, 0x30, 0x0c, 0x6e, 0x91, 0x27, 0x7a, 0x1a, 0x20, 0x2e, 0xd3, 0x29, 0x74, 0xb1, 0x91, 0x6c, 0xb9, 0xcd, 0x6f, 0xda, 0xc1, 0x12, 0x16, 0x5d, 0x49, 0x26, 0x9b, 0x69, 0x44, 0xab, 0xeb, 0x29, 0xee, 0x53, 0x51, 0x55, 0x05, 0x03, 0x35, 0xbf, 0xbb, 0xde, 0x2a, 0xf9, 0x37, 0xed, 0xc7, 0xe2, 0x25, 0x46, 0x26, 0xe5, 0x20, 0x6c, 0x28, 0x13, 0x8d, 0xf6, 0x92, 0x7e, 0x60, 0x35, 0x23, 0xba, 0x7c, 0x61, 0xac, 0x6b, 0xd9, 0x2f, 0x5f, 0x3c, 0x64, 0x44, 0x51, 0x11, 0x36, 0xfe, 0x6e, 0x21, 0x9c, 0x10, 0x1d, 0xe7, 0xba, 0xa1, 0x6e, 0x24, 0x63, 0x3f, 0x42, 0x4a, 0x29, 0xea, 0x3c, 0x7a, 0x8f, 0x99, 0x51, 0x09, 0xf4, 0x0b, 0x01, 0x5a, 0x05, 0x3a, 0x52, 0x53, 0xc9, 0xb7, 0xe4, 0xcc, 0x60, 0xa5, 0x53, 0x1d, 0xeb, 0x79, 0x88, 0x55, 0xc1, 0xb2, 0xfb, 0xd6, }, }; unsigned char mod[][128] = { { 0xC4, 0xD0, 0x91, 0x44, 0x0A, 0xC3, 0x3E, 0xB1, 0xCC, 0xF7, 0x84, 0x62, 0x2E, 0xA5, 0x4B, 0xA3, 0xF8, 0xA5, 0x44, 0x38, 0x1F, 0x00, 0x5C, 0x20, 0x54, 0x8A, 0x84, 0xF2, 0x5E, 0xEB, 0x5B, 0x09, 0xA2, 0x80, 0x3F, 0x79, 0x5F, 0x11, 0x41, 0x21, 0xEA, 0x24, 0x47, 0xC5, 0xE8, 0x16, 0x46, 0x18, 0xCA, 0x75, 0x08, 0x39, 0xAC, 0xCF, 0x17, 0x20, 0x4E, 0x8F, 0x68, 0xB6, 0xD5, 0x25, 0x1F, 0x61, 0x2E, 0xD6, 0xC9, 0x63, 0x41, 0xA9, 0xC9, 0x78, 0x1D, 0x73, 0x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x20, 0x4E, 0x8F, 0x68, 0xB6, 0xD5, 0x25, 0x1F, 0x61, 0x2E, 0xD6, 0xC9, 0x63, 0x41, 0xA9, 0xC9, 0x78, 0x1D, 0x73, 0x34, 0xC9, 0x4F, 0xBB, 0x4C, 0x3C, 0xB2, 0x6C, 0xCF, 0x87, 0xB7, 0x95, 0x66, 0xD3, 0x1A, 0xDE, 0x3F, 0xF8, 0x52, 0x3E, 0xDC, 0x82, 0x35, 0x7C, 0x58, 0x7C, 0x10, 0x35, 0xB0, 0xFF, 0x5A, 0xF6, 0x1F, 0xD3, 0x0B, 0x9C, 0x49, 0x76, 0x71, 0xE2, 0x4E, 0x69, 0x58, 0x54, 0x42, 0xA3, 0x3F, 0x93, 0x78, 0x58, 0xC6, 0xE0, 0x74, 0x06, }, }; BOOL result_sign[] = { TRUE, FALSE, FALSE, FALSE, }; CRYPTO_SetAllocator(MyAlloc, MyFree); for (i = 0; i < sizeof(d) / sizeof(char*); i++) { BOOL result; result = CRYPTO_VerifySignature(d[i], (int)GetStringLength(d[i]), sign[i], mod[i]); PrintResultEq(result, result_sign[i], flag); OS_TPrintf("CRYPTO_VerifySignature: Test Case %d: %s\n", i+1, result ? "Verified" : "Verification Failure"); } } return flag; } /*---------------------------------------------------------------------------* Name: GetStringLength Description: Gets the length of a string. Arguments: str: Pointer to a string Returns: Length of string *---------------------------------------------------------------------------*/ static u32 GetStringLength(char* str) { u32 i; for (i = 0; ; i++) { if (*(str++) == '\0') { return i; } } } /*---------------------------------------------------------------------------* Name: PrintBinary Description: Binary array output. Arguments: ptr: Memory location to be output len: Output length Returns: None. *---------------------------------------------------------------------------*/ static void PrintBinary(u8* ptr, int len) { #ifndef SDK_FINALROM int i; for (i = 0; i < len; i++) { if (i != 0) { OS_PutString(":"); } OS_TPrintf("%02x", ptr[i]); } #else #pragma unused(ptr,len) #endif return; } /*---------------------------------------------------------------------------* End of file *---------------------------------------------------------------------------*/