From 4aec2441985dcc9d3b03ec2cf3c49c108cd4ad24 Mon Sep 17 00:00:00 2001 From: yutaka Date: Thu, 7 Jun 2007 06:19:17 +0000 Subject: [PATCH] add AES-CTR sample git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/twl_wrapsdk/trunk@115 4ee2a332-4b2b-5046-8439-1ba90f034370 --- build/tests/aes/Makefile | 3 +- build/tests/aes/aes-2/ARM7/Makefile | 44 ++++ build/tests/aes/aes-2/ARM7/src/main.c | 303 +++++++++++++++++++++++ build/tests/aes/aes-2/ARM9/Makefile | 43 ++++ build/tests/aes/aes-2/ARM9/src/main.c | 337 ++++++++++++++++++++++++++ build/tests/aes/aes-2/Makefile | 32 +++ include/twl/aes/ARM9/aes.h | 4 +- 7 files changed, 763 insertions(+), 3 deletions(-) create mode 100644 build/tests/aes/aes-2/ARM7/Makefile create mode 100644 build/tests/aes/aes-2/ARM7/src/main.c create mode 100644 build/tests/aes/aes-2/ARM9/Makefile create mode 100644 build/tests/aes/aes-2/ARM9/src/main.c create mode 100644 build/tests/aes/aes-2/Makefile diff --git a/build/tests/aes/Makefile b/build/tests/aes/Makefile index cfe7655..4ec8c7a 100644 --- a/build/tests/aes/Makefile +++ b/build/tests/aes/Makefile @@ -20,7 +20,8 @@ include $(TWLSDK_ROOT)/build/buildtools/commondefs #---------------------------------------------------------------------------- -SUBDIRS = aes-1 +SUBDIRS = aes-1 \ + aes-2 #---------------------------------------------------------------------------- diff --git a/build/tests/aes/aes-2/ARM7/Makefile b/build/tests/aes/aes-2/ARM7/Makefile new file mode 100644 index 0000000..5d6e09a --- /dev/null +++ b/build/tests/aes/aes-2/ARM7/Makefile @@ -0,0 +1,44 @@ +#! make -f +#---------------------------------------------------------------------------- +# Project: TwlSDK - AES - demos - aes-1 +# File: Makefile +# +# Copyright 2007 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. +# +# $Log: $ +# $NoKeywords: $ +#---------------------------------------------------------------------------- + +SUBDIRS = + + +#---------------------------------------------------------------------------- + +#TWL_CODEGEN = THUMB +TWL_PROC = ARM7 + +TARGET_BIN = main.axf + +SRCS = main.c + +#SRCDIR = # using default +#LCFILE = # using default + +include $(TWLSDK_ROOT)/build/buildtools/commondefs + + +#---------------------------------------------------------------------------- + +do-build: $(TARGETS) + + +include $(TWLSDK_ROOT)/build/buildtools/modulerules + + +#===== End of Makefile ===== diff --git a/build/tests/aes/aes-2/ARM7/src/main.c b/build/tests/aes/aes-2/ARM7/src/main.c new file mode 100644 index 0000000..fbd9c06 --- /dev/null +++ b/build/tests/aes/aes-2/ARM7/src/main.c @@ -0,0 +1,303 @@ +/*---------------------------------------------------------------------------* + Project: NitroSDK - AES - demos - aes-2 + File: main.c + + Copyright 2007 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. + + $Log: main.c,v $ + $NoKeywords: $ + *---------------------------------------------------------------------------*/ +#include + +#define ENABLE_INTERRUPT_TEST + +#define PRIORITY 5 + +#define INPUT_DMA 4 +#define OUTPUT_DMA 5 + +static const u128 key = { + 0x01234567, + 0x89abcdef, + 0x01234567, + 0x89abcdef +}; +static const u128 iv = { + 0x89abcdef, + 0x01234567, + 0x89abcdef, + 0x01234567 +}; +static const u32 gs_data[] ATTRIBUTE_ALIGN(32) = { + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567 +}; + +static u8 dataA[sizeof(gs_data)] ATTRIBUTE_ALIGN(32); +static u8 dataB[sizeof(gs_data)] ATTRIBUTE_ALIGN(32); + +//================================================================================ +static void dump(const char *str, void *ptr, u32 length) +{ + u8 *data = (u8*)ptr; + int i; + OS_TPrintf("\n[%s] (%d bytes):\n\t", str, length); + for (i = 0; i < length; i++) { + OS_TPrintf("%02X", *data++); + if ((i & 0xF) == 0xF) OS_TPrintf("\n\t"); + else OS_TPrintf(" "); + } + OS_TPrintf("\n"); +} + +#define TEST0_USE_DMA_INPUT +#define TEST0_USE_DMA_OUTPUT +static void test0(void) +{ + OSTick begin; + + OS_TPrintf("\n%s() is starting.\n\n", __func__); + + // とりあえず、リセットする (鍵関係は何も変わらない (KEY_SELレジスタも消えるが意味は無い)) + AES_Reset(); + + // 鍵は0番目を使う + AES_SelectKey(0); + +#ifdef TEST0_USE_DMA_INPUT + // 入力DMA設定 + AES_DmaSendAsync(INPUT_DMA, gs_data, sizeof(gs_data)); +#endif +#ifdef TEST0_USE_DMA_OUTPUT + // 出力DMA設定 + MI_CpuClear32(dataA, sizeof(dataA)); + AES_DmaRecvAsync(OUTPUT_DMA, dataA, sizeof(gs_data)); +#endif + + begin = OS_GetTick(); + + // AES-CTRエンコードを開始する + AES_StartCtrEnc(&iv, sizeof(gs_data)); + +#ifdef TEST0_USE_DMA_INPUT +#ifdef TEST0_USE_DMA_OUTPUT + // DMA完了待ち (AES完了待ちでも良いでしょう) + MIi_WaitExDma(OUTPUT_DMA); +#else + // CPUで出力してみる + AES_CpuRecv(dataA, sizeof(gs_data)); +#endif +#else +#ifdef TEST0_USE_DMA_OUTPUT + // CPUで入力してみる + AES_CpuSend(gs_data, sizeof(gs_data)); +#else +#error "Does not support CPU input and CPU output at same time." +#endif +#endif + + OS_TPrintf("%s: %d usec.\n", __func__, (u32)OS_TicksToMicroSeconds(OS_GetTick()-begin)); + + // 出力結果の表示 + dump(__func__, dataA, sizeof(gs_data)); +} + +static void test1(void) +{ + OSTick begin; + + OS_TPrintf("\n%s() is starting.\n\n", __func__); + + // とりあえず、リセットする (鍵関係は何も変わらない (KEY_SELレジスタも消えるが意味は無い)) + AES_Reset(); + + // 鍵を設定しない (前回の値を使いまわす) + + // 入力DMA設定 + AES_DmaSendAsync(INPUT_DMA, dataA, sizeof(gs_data)); + + // 出力バッファのクリア + MI_CpuClear32(dataB, sizeof(dataB)); + + begin = OS_GetTick(); + + // AES-CTRデコードを開始する + AES_StartCtrDec(&iv, sizeof(gs_data)); + + // CPUで出力してみる + AES_CpuRecv(dataB, sizeof(gs_data)); + + OS_TPrintf("%s: %d usec.\n", __func__, (u32)OS_TicksToMicroSeconds(OS_GetTick()-begin)); + + // 出力結果の表示 + dump(__func__, dataB, sizeof(gs_data)); +} + +static void test2(void) +{ + OSTick begin; + + OS_TPrintf("\n%s() is starting.\n\n", __func__); + + // とりあえず、リセットする (鍵関係は何も変わらない (KEY_SELレジスタも消えるが意味は無い)) + AES_Reset(); + + // 鍵を設定する + AES_SelectKey(1); + + // 出力DMA設定 + MI_CpuClear32(dataB, sizeof(dataB)); + AES_DmaRecvAsync(OUTPUT_DMA, dataB, sizeof(gs_data)); + + begin = OS_GetTick(); + + // AES-CTRデコードを開始する + AES_StartCtrDec(&iv, sizeof(gs_data)); + + // CPUで入力してみる + AES_CpuSend(dataA, sizeof(gs_data)); + + OS_TPrintf("%s: %d usec.\n", __func__, (u32)OS_TicksToMicroSeconds(OS_GetTick()-begin)); + + // 出力結果の表示 + dump(__func__, dataB, sizeof(gs_data)); + + OS_TPrintf("Result: %s\n", AES_IsValid() ? "Success" : "Failed (OK!)"); +} + +static u32 intrCounter[3]; +static u8 aesID; +static u8 inputDmaID; +static u8 outputDmaID; + +static void AesIntr(void) +{ + intrCounter[aesID]++; + + //---- check interrupt flag + OS_SetIrqCheckFlag( OS_IE_AES ); +} + +static void InputDmaIntr(void) +{ + u32 ofs = INPUT_DMA - MI_EXDMA_CH_MIN; + OSIrqMask mask = OS_IE_DMA4 << ofs; + + intrCounter[inputDmaID]++; + + //---- check interrupt flag + OS_SetIrqCheckFlag( mask ); +} + +static void OutputDmaIntr(void) +{ + u32 ofs = OUTPUT_DMA - MI_EXDMA_CH_MIN; + OSIrqMask mask = OS_IE_DMA4 << ofs; + + intrCounter[outputDmaID]++; + + //---- check interrupt flag + OS_SetIrqCheckFlag( mask ); +} + +static void InitAesDmaIntr(void) +{ + u32 i_ofs = INPUT_DMA - MI_EXDMA_CH_MIN; + u32 o_ofs = OUTPUT_DMA - MI_EXDMA_CH_MIN; + OSIrqMask i_mask = OS_IE_DMA4 << i_ofs; + OSIrqMask o_mask = OS_IE_DMA4 << o_ofs; + u8 id_alloc = 0; + + BOOL ime = OS_DisableIrq(); + + aesID = id_alloc++; + inputDmaID = id_alloc++; + outputDmaID = id_alloc++; + + (void)OS_DisableIrqMask( OS_IE_AES | i_mask | o_mask ); + (void)OS_ResetRequestIrqMask( OS_IE_AES | i_mask | o_mask ); + + (void)OS_SetIrqFunction( OS_IE_AES, AesIntr ); + (void)OS_SetIrqFunction( i_mask, InputDmaIntr ); + (void)OS_SetIrqFunction( o_mask, OutputDmaIntr ); + (void)OS_EnableIrqMask( OS_IE_AES | i_mask | o_mask ); + + (void)OS_RestoreIrq( ime ); +} + +static void PrintIntrCount(void) +{ + OS_TPrintf( "\ninterrupt count: aes = %d, input_dma = %d, output_dma = %d.\n", + intrCounter[aesID], intrCounter[inputDmaID], intrCounter[outputDmaID]); +} + + +/*---------------------------------------------------------------------------* + Name: TwlMain + + Description: main + + Arguments: None + + Returns: None + *---------------------------------------------------------------------------*/ +void TwlMain() +{ + OS_Init(); + + OS_Printf("ARM7 starts.\n"); + + OS_InitTick(); + +#ifdef ENABLE_INTERRUPT_TEST + InitAesDmaIntr(); +#endif + + OS_TPrintf("Debug Info:\n"); + OS_TPrintf("\tdataA = 0x%08X\n", dataA); + OS_TPrintf("\tdataB = 0x%08X\n", dataB); + + AES_Init(PRIORITY); // ARM9側からも利用するときのみ必要 + AES_Lock(); // ARM9側からも利用するときのみ必要 + OS_EnableIrq(); // ARM9側からも利用するときのみ必要 + + // 鍵を設定しておく + AES_SetKey(0, &key); + AES_SetSeed(1, &key); + + test0(); + test1(); + test2(); + + AES_Unlock(); // ARM9側からも利用するときのみ必要 + +#ifdef ENABLE_INTERRUPT_TEST + PrintIntrCount(); +#endif + + // done + OS_TPrintf("\nARM7 ends.\n"); + //OS_Terminate(); +} diff --git a/build/tests/aes/aes-2/ARM9/Makefile b/build/tests/aes/aes-2/ARM9/Makefile new file mode 100644 index 0000000..30018bf --- /dev/null +++ b/build/tests/aes/aes-2/ARM9/Makefile @@ -0,0 +1,43 @@ +#! make -f +#---------------------------------------------------------------------------- +# Project: TwlSDK - AES - demos - aes-1 +# File: Makefile +# +# Copyright 2007 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. +# +# $Log: $ +# $NoKeywords: $ +#---------------------------------------------------------------------------- + +SUBDIRS = + + +#---------------------------------------------------------------------------- + +#TWL_CODEGEN = THUMB + +TARGET_BIN = main.axf + +SRCS = main.c + +#SRCDIR = # using default +#LCFILE = # using default + +include $(TWLSDK_ROOT)/build/buildtools/commondefs + + +#---------------------------------------------------------------------------- + +do-build: $(TARGETS) + + +include $(TWLSDK_ROOT)/build/buildtools/modulerules + + +#===== End of Makefile ===== diff --git a/build/tests/aes/aes-2/ARM9/src/main.c b/build/tests/aes/aes-2/ARM9/src/main.c new file mode 100644 index 0000000..920046a --- /dev/null +++ b/build/tests/aes/aes-2/ARM9/src/main.c @@ -0,0 +1,337 @@ +/*---------------------------------------------------------------------------* + Project: NitroSDK - AES - demos - aes-2 + File: main.c + + Copyright 2007 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. + + $Log: main.c,v $ + $NoKeywords: $ + *---------------------------------------------------------------------------*/ +#include + +#define INPUT_DMA 4 +#define OUTPUT_DMA 5 + +static const u128 key = { + 0x01234567, + 0x89abcdef, + 0x01234567, + 0x89abcdef +}; +static const u128 iv = { + 0x89abcdef, + 0x01234567, + 0x89abcdef, + 0x01234567 +}; +static const u32 gs_data[] ATTRIBUTE_ALIGN(32) = { + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, + 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567, 0x89abcdef, 0x01234567 +}; + +static u8 dataA[sizeof(gs_data) + 32] ATTRIBUTE_ALIGN(32); +static u8 dataB[sizeof(gs_data) + 32] ATTRIBUTE_ALIGN(32); + +//================================================================================ +static void dump(const char *str, void *ptr, u32 length) +{ + u8 *data = (u8*)ptr; + int i; + OS_TPrintf("\n[%s] (%d bytes):\n\t", str, length); + for (i = 0; i < length; i++) { + OS_TPrintf("%02X", *data++); + if ((i & 0xF) == 0xF) OS_TPrintf("\n\t"); + else OS_TPrintf(" "); + } + OS_TPrintf("\n"); +} + +#define TEST0_USE_DMA_INPUT +#define TEST0_USE_DMA_OUTPUT +static void test0(void) +{ + OSTick begin; + AESResult result; + + OS_TPrintf("\n%s() is starting.\n\n", __func__); + + // とりあえず、リセットする (鍵関係は何も変わらない (KEY_SELレジスタも消えるが意味は無い)) + result = AES_Reset(); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_Reset (%d).\n", __func__, result); + } + + // 鍵を設定する + result = AES_SetGeneralKey(&key); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_SetGeneralKey (%d).\n", __func__, result); + } + +#ifdef TEST0_USE_DMA_INPUT + // 入力DMA設定 + result = AES_StartDmaSend(INPUT_DMA, gs_data, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartDmaSend (%d).\n", __func__, result); + } +#endif + +#ifdef TEST0_USE_DMA_OUTPUT + // 出力DMA設定 + MI_CpuClear32(dataA, sizeof(dataA)); + DC_FlushRange(dataA, sizeof(dataA)); + result = AES_StartDmaRecv(OUTPUT_DMA, dataA, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartDmaSend (%d).\n", __func__, result); + } +#endif + + begin = OS_GetTick(); + + // AES-CTRエンコードを開始する + result = AES_StartCtrEnc(&iv, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartCtrEnc (%d).\n", __func__, result); + } + +#ifdef TEST0_USE_DMA_INPUT +#ifdef TEST0_USE_DMA_OUTPUT + // AES出力DMA完了待ち + result = AES_WaitDma(OUTPUT_DMA); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_WaitDma (%d).\n", __func__, result); + } +#else + // CPUで出力してみる + result = AES_CpuRecv(dataA, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_CpuRecv (%d).\n", __func__, result); + } +#endif +#else +#ifdef TEST0_USE_DMA_OUTPUT + // CPUで入力してみる + result = AES_CpuSend(gs_data, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_CpuSend (%d).\n", __func__, result); + } +#else +#error "Does not support CPU input and CPU output at same time." +#endif +#endif + + OS_TPrintf("%s: %d usec.\n", __func__, (u32)OS_TicksToMicroSeconds(OS_GetTick()-begin)); + + // 出力結果の表示 + dump(__func__, dataA, sizeof(gs_data)); +} + +static void test1(void) +{ + OSTick begin; + AESResult result; + + OS_TPrintf("\n%s() is starting.\n\n", __func__); + + // とりあえず、リセットする (鍵関係は何も変わらない (KEY_SELレジスタも消えるが意味は無い)) + result = AES_Reset(); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_Reset (%d).\n", __func__, result); + } + + // 鍵を設定しない (前回の値を使いまわす) + + // 入力DMA設定 + DC_FlushRange(dataA, sizeof(dataA)); + result = AES_StartDmaSend(INPUT_DMA, dataA, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartDmaSend (%d).\n", __func__, result); + } + + // 出力バッファのクリア + MI_CpuClear32(dataB, sizeof(dataB)); + DC_FlushRange(dataB, sizeof(dataB)); + + begin = OS_GetTick(); + + // AES-CTRデコードを開始する + result = AES_StartCtrDec(&iv, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartCtrDec (%d).\n", __func__, result); + } + + // CPUで出力してみる + result = AES_CpuRecv(dataB, sizeof(gs_data)); // adata + pdata + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_CpuRecv (%d).\n", __func__, result); + } + + OS_TPrintf("%s: %d usec.\n", __func__, (u32)OS_TicksToMicroSeconds(OS_GetTick()-begin)); + + // 出力結果の表示 + dump(__func__, dataB, sizeof(gs_data)); + + result = AES_IsValid(); + if (AES_RESULT_SUCCESS_TRUE != result && AES_RESULT_SUCCESS_FALSE != result) + { + OS_TPrintf("%s: Failed to call AES_IsValid (%d).\n", __func__, result); + } + else + { + OS_TPrintf("Result: %s\n", AES_RESULT_SUCCESS_TRUE == result ? "Success" : "Failed"); + } +} + +static void test2(void) +{ + OSTick begin; + AESResult result; + + OS_TPrintf("\n%s() is starting.\n\n", __func__); + + // とりあえず、リセットする (鍵関係は何も変わらない (KEY_SELレジスタも消えるが意味は無い)) + result = AES_Reset(); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_Reset (%d).\n", __func__, result); + } + + // 鍵を設定する + result = AES_SetSpecialKey(&key); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_SetSpecialKey (%d).\n", __func__, result); + } + + // 出力DMA設定 + MI_CpuClear32(dataB, sizeof(dataB)); + DC_FlushRange(dataB, sizeof(dataB)); + result = AES_StartDmaRecv(OUTPUT_DMA, dataB, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartDmaSend (%d).\n", __func__, result); + } + + begin = OS_GetTick(); + + // AES-CTRデコードを開始する + result = AES_StartCtrDec(&iv, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_StartCtrDec (%d).\n", __func__, result); + } + + // CPUで入力してみる + AES_CpuSend(dataA, sizeof(gs_data)); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_CpuSend (%d).\n", __func__, result); + } + + OS_TPrintf("%s: %d usec.\n", __func__, (u32)OS_TicksToMicroSeconds(OS_GetTick()-begin)); + + // 出力結果の表示 + dump(__func__, dataB, sizeof(gs_data)); + + result = AES_IsValid(); + if (AES_RESULT_SUCCESS_TRUE != result && AES_RESULT_SUCCESS_FALSE != result) + { + OS_TPrintf("%s: Failed to call AES_IsValid (%d).\n", __func__, result); + } + else + { + OS_TPrintf("Result: %s\n", AES_RESULT_SUCCESS_TRUE == result ? "Success" : "Failed (OK!)"); + } +} + + +/*---------------------------------------------------------------------------* + Name: TwlMain + + Description: main + + Arguments: None + + Returns: None + *---------------------------------------------------------------------------*/ +void TwlMain() +{ + AESResult result; + + OS_Init(); + + OS_Printf("ARM9 starts.\n"); + + OS_InitTick(); + + OS_TPrintf("Debug Info:\n"); + OS_TPrintf("\tdataA = 0x%08X\n", dataA); + OS_TPrintf("\tdataB = 0x%08X\n", dataB); + + AES_Init(); + OS_TPrintf("AES_Init wad done.\n"); + + OS_EnableIrq(); + + while (1) + { + result = AES_TryLock(); + if (AES_RESULT_SUCCESS_TRUE == result) + { + break; + } + if (AES_RESULT_SUCCESS_FALSE != result) + { + OS_TPrintf("%s: Failed to call AES_TryLock (%d).\n", __func__, result); + } + OS_Sleep(1); + } + OS_TPrintf("AES_TryLock wad done.\n"); + + test0(); + test1(); + test2(); + + result = AES_Unlock(); + if (AES_RESULT_SUCCESS != result) + { + OS_TPrintf("%s: Failed to call AES_Unlock (%d).\n", __func__, result); + } + + // done + OS_TPrintf("\nARM9 ends.\n"); + OS_Terminate(); +} diff --git a/build/tests/aes/aes-2/Makefile b/build/tests/aes/aes-2/Makefile new file mode 100644 index 0000000..078ec37 --- /dev/null +++ b/build/tests/aes/aes-2/Makefile @@ -0,0 +1,32 @@ +#! make -f +#---------------------------------------------------------------------------- +# Project: TwlSDK - build +# File: Makefile +# +# Copyright 2007 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. +# +# $Log: $ +# $NoKeywords: $ +#---------------------------------------------------------------------------- + +include $(TWLSDK_ROOT)/build/buildtools/commondefs + + +#---------------------------------------------------------------------------- + +SUBDIRS = \ + ARM7 \ + ARM9 \ + +#---------------------------------------------------------------------------- + +include $(TWLSDK_ROOT)/build/buildtools/modulerules + + +#===== End of Makefile ===== diff --git a/include/twl/aes/ARM9/aes.h b/include/twl/aes/ARM9/aes.h index d45e909..2ca5c12 100644 --- a/include/twl/aes/ARM9/aes.h +++ b/include/twl/aes/ARM9/aes.h @@ -684,7 +684,7 @@ static inline AESResult AES_StartCtrEncAsync(const u128 *iv, u32 length, AESCall } /*---------------------------------------------------------------------------* - Name: AES_StartEncDec + Name: AES_StartCtrEnc Description: start AES engine for AES-CTR encryption/decryption. sync version. @@ -694,7 +694,7 @@ static inline AESResult AES_StartCtrEncAsync(const u128 *iv, u32 length, AESCall Returns: AESResult *---------------------------------------------------------------------------*/ -static inline AESResult AES_StartEncDec(const u128 *iv, u32 length) +static inline AESResult AES_StartCtrEnc(const u128 *iv, u32 length) { return AES_StartCtrDec(iv, length); }