diff --git a/build/libraries_sysmenu/hotsw/ARM7/include/blowfish.h b/build/libraries_sysmenu/hotsw/ARM7/include/blowfish.h index 357da56e..4799d036 100644 --- a/build/libraries_sysmenu/hotsw/ARM7/include/blowfish.h +++ b/build/libraries_sysmenu/hotsw/ARM7/include/blowfish.h @@ -15,6 +15,7 @@ #include +#include #include #ifdef __cplusplus @@ -26,18 +27,10 @@ extern BLOWFISH_CTX HotSwBlowfishInitTableBufDS; extern BLOWFISH_CTX HotSwBlowfishInitTableTWL; // Function Prototype ------------------------------------------------------------------------ -// Blowfish 初期化 -void InitBlowfish(BLOWFISH_CTX *ctx, const unsigned char *key, int keyLen); // Blowfish KeyとTableの初期化 void InitBlowfishKeyAndTableDS(BLOWFISH_CTX *ctx, u32 *keyBufp, s32 keyLen); -// Blowfish 復号化 -void EncryptByBlowfish(const BLOWFISH_CTX *ctx, u32 *xl, u32 *xr); - -// Blowfish 暗号化 -void DecryptByBlowfish(const BLOWFISH_CTX *ctx, u32 *xl, u32 *xr); - // Key Table の生成 void MakeBlowfishTableDS(CardBootData *cbd, s32 keyLen); diff --git a/build/libraries_sysmenu/hotsw/ARM7/src/Blowfish.c b/build/libraries_sysmenu/hotsw/ARM7/src/Blowfish.c new file mode 100644 index 00000000..fefac1c5 --- /dev/null +++ b/build/libraries_sysmenu/hotsw/ARM7/src/Blowfish.c @@ -0,0 +1,148 @@ +/*-------------------------------------------------------------------- +! +! Blowfish.c +! +! Copyright 2002 by MegaChips Corporation +! +!--------------------------------------------------------------------- +! PROJECT: IRIS Demo +! CATEGORY: C source +! COMMENT: Blowfish subroutine +! $Author: kawamura.kumiko $ +! $Date: 2002/12/02 08:13:54 $ +! $Revision: 1.2 $ +!-- +*/ +#include "blowfish.h" + + +#define MAXKEYBYTES 56 /* 448 bits */ +#define N 16 + + +static u32 F(const BLOWFISH_CTX *ctx, u32 x); + +void InitBlowfish(BLOWFISH_CTX *ctx, const unsigned char *key, int keyLen) +{ + int i, j, k; + u32 data, datal, datar; + + j = 0; + for (i = 0; i < N + 2; ++i) { + data = 0x00000000; + for (k = 0; k < 4; ++k) { + data = (data << 8) | key[j]; + j = j + 1; + if (j >= keyLen) + j = 0; + } + ctx->P[i] = ctx->P[i] ^ data; + } + + datal = 0x00000000; + datar = 0x00000000; + + for (i = 0; i < N + 2; i += 2) { + EncryptByBlowfish(ctx, &datal, &datar); + ctx->P[i] = datal; + ctx->P[i + 1] = datar; + } + + for (i = 0; i < 4; ++i) { + for (j = 0; j < 256; j += 2) { + EncryptByBlowfish(ctx, &datal, &datar); + ctx->S[i][j] = datal; + ctx->S[i][j + 1] = datar; + } + } + +} + + +void EncryptByBlowfish(const BLOWFISH_CTX *ctx, u32 *xl, u32 *xr) +{ + u32 Xl; + u32 Xr; + u32 temp; + int i; + + Xl = *xl; + Xr = *xr; + + for (i = 0; i < N; ++i) { + Xl = Xl ^ ctx->P[i]; + Xr = F(ctx, Xl) ^ Xr; + + temp = Xl; + + Xl = Xr; + Xr = temp; + } + + temp = Xl; + Xl = Xr; + Xr = temp; + + Xr = Xr ^ ctx->P[N]; + Xl = Xl ^ ctx->P[N + 1]; + + *xl = Xl; + *xr = Xr; +} + +void DecryptByBlowfish(const BLOWFISH_CTX *ctx, u32 *xl, u32 *xr) +{ + u32 Xl; + u32 Xr; + u32 temp; + int i; + + + Xl = *xl; + Xr = *xr; + + for (i = N + 1; i > 1; --i) { + Xl = Xl ^ ctx->P[i]; + Xr = F(ctx, Xl) ^ Xr; + + temp = Xl; + Xl = Xr; + Xr = temp; + } + + temp = Xl; + Xl = Xr; + Xr = temp; + + Xr = Xr ^ ctx->P[1]; + Xl = Xl ^ ctx->P[0]; + + *xl = Xl; + *xr = Xr; +} + + +static u32 F(const BLOWFISH_CTX *ctx, u32 x) { + u32 a, b, c, d; + u32 y; + + d = x & 0x00FF; + x >>= 8; + + c = x & 0x00FF; + x >>= 8; + + b = x & 0x00FF; + x >>= 8; + + a = x & 0x00FF; + + y = ctx->S[0][a] + ctx->S[1][b]; + + y = y ^ ctx->S[2][c]; + y = y + ctx->S[3][d]; + + return y; +} + + diff --git a/build/libraries_sysmenu/hotsw/ARM7/src/hotswBlowfish.c b/build/libraries_sysmenu/hotsw/ARM7/src/hotswBlowfish.c new file mode 100644 index 00000000..c6b99a4e --- /dev/null +++ b/build/libraries_sysmenu/hotsw/ARM7/src/hotswBlowfish.c @@ -0,0 +1,102 @@ +/*---------------------------------------------------------------------------* + Project: TwlIPL + File: blowfish.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. + *---------------------------------------------------------------------------*/ +#include +#include +#include +#include + +// Function Prototype ------------------------------------------------------- + + +/*---------------------------------------------------------------------------* + Name: MakeBlowfishTableDS + + Description: KeyTableの生成 + *---------------------------------------------------------------------------*/ +void MakeBlowfishTableDS(CardBootData *cbd, s32 keyLen) +{ + const BLOWFISH_CTX *initTable = &HotSwBlowfishInitTableBufDS; + + u32 *RomHeaderGameCode = (u32 *)cbd->pBootSegBuf->rh.s.game_code; + u32 *keyBuf = cbd->keyBuf; + BLOWFISH_CTX *ctx = &cbd->keyTable; + + MI_CpuCopyFast((void *)initTable, (void *)ctx, sizeof(BLOWFISH_CTX)); + + keyBuf[0] = *RomHeaderGameCode; + keyBuf[1] = *RomHeaderGameCode >> 1; + keyBuf[2] = *RomHeaderGameCode << 1; + + InitBlowfishKeyAndTableDS(ctx, keyBuf, keyLen); + InitBlowfishKeyAndTableDS(ctx, keyBuf, keyLen); +} + + +/*---------------------------------------------------------------------------* + Name: MakeBlowfishTableTWL + + Description: KeyTable2の生成 + *---------------------------------------------------------------------------*/ +void MakeBlowfishTableTWL(CardBootData *cbd, s32 keyLen, u16 bondingOp) +{ + u32 *RomHeaderGameCode = (u32 *)cbd->pBootSegBuf->rh.s.game_code; + u32 *keyBuf = cbd->keyBuf2; + BLOWFISH_CTX *ctx = &cbd->keyTable2; + void *tempCTX; + + + // 製品機の場合 + if(bondingOp == SCFG_OP_PRODUCT){ + MI_CpuCopyFast((void *)((OSFromFirm7Buf *)HW_FIRM_FROM_FIRM_BUF)->twl_blowfish, (void *)ctx, sizeof(BLOWFISH_CTX)); + } + // 開発機の場合 + else{ + MI_CpuCopyFast(&HotSwBlowfishInitTableTWL, (void *)ctx, sizeof(BLOWFISH_CTX)); + + + // スタック領域がオーバーフローするから、ヒープ領域から領域を確保する。 + tempCTX = OS_AllocFromSubPrivWram( sizeof(BLOWFISH_CTX) ); + + if(tempCTX != NULL){ + ctx = tempCTX; + } + else{ + return; + } + } + + keyBuf[0] = *RomHeaderGameCode; + keyBuf[1] = *RomHeaderGameCode >> 1; + keyBuf[2] = *RomHeaderGameCode << 1; + + InitBlowfishKeyAndTableDS(ctx, keyBuf, keyLen); + + // Heapの開放 + if(bondingOp != SCFG_OP_PRODUCT){ + OS_FreeToSubPrivWram( tempCTX ); + } +} + + +/*---------------------------------------------------------------------------* + Name: InitBlowfishKeyAndTableDS + + Description: + *---------------------------------------------------------------------------*/ +void InitBlowfishKeyAndTableDS(BLOWFISH_CTX *ctx, u32 *keyBufp, s32 keyLen) +{ + EncryptByBlowfish(ctx, (void*)&(keyBufp)[2], (void*)&(keyBufp)[1]); + EncryptByBlowfish(ctx, (void*)&(keyBufp)[1], (void*)&(keyBufp)[0]); + InitBlowfish(ctx, (u8 *)keyBufp, keyLen); +} +