From 69521b0beea5e30adef9f7205271a0a2587932b5 Mon Sep 17 00:00:00 2001 From: scbroede <32247405+scbroede@users.noreply.github.com> Date: Wed, 11 Mar 2026 17:18:08 -0400 Subject: [PATCH] decompile crypto utils (#994) --- .../crypto}/asm/include/sign.inc | 0 .../crypto}/asm/macros/function.inc | 0 .../libcrypto => lib/crypto}/asm/rc4-arm4cw.s | 0 .../libcrypto => lib/crypto}/asm/rc4.s | 0 .../crypto}/asm/rc4s-arm4cw.s | 0 .../libcrypto => lib/crypto}/asm/sign.s | 0 .../libcrypto => lib/crypto}/include/crypto.h | 4 +- .../crypto}/include/crypto/rc4.h | 10 +-- .../crypto}/include/crypto/sign.h | 0 lib/crypto/include/crypto/util.h | 16 +++++ .../libcrypto => lib/crypto}/meson.build | 17 +++-- lib/crypto/src/crypto_internal.h | 4 ++ lib/crypto/src/util.c | 33 ++++++++++ lib/meson.build | 1 + meson.build | 2 +- subprojects/libcrypto/asm/include/util.inc | 2 - subprojects/libcrypto/asm/util.s | 66 ------------------- subprojects/libcrypto/include/crypto/util.h | 14 ---- tools/devtools/gen_compile_commands.py | 32 +-------- tools/m2ctx/m2ctx.sh | 2 +- 20 files changed, 78 insertions(+), 125 deletions(-) rename {subprojects/libcrypto => lib/crypto}/asm/include/sign.inc (100%) rename {subprojects/libcrypto => lib/crypto}/asm/macros/function.inc (100%) rename {subprojects/libcrypto => lib/crypto}/asm/rc4-arm4cw.s (100%) rename {subprojects/libcrypto => lib/crypto}/asm/rc4.s (100%) rename {subprojects/libcrypto => lib/crypto}/asm/rc4s-arm4cw.s (100%) rename {subprojects/libcrypto => lib/crypto}/asm/sign.s (100%) rename {subprojects/libcrypto => lib/crypto}/include/crypto.h (100%) rename {subprojects/libcrypto => lib/crypto}/include/crypto/rc4.h (70%) rename {subprojects/libcrypto => lib/crypto}/include/crypto/sign.h (100%) create mode 100644 lib/crypto/include/crypto/util.h rename {subprojects/libcrypto => lib/crypto}/meson.build (68%) create mode 100644 lib/crypto/src/crypto_internal.h create mode 100644 lib/crypto/src/util.c delete mode 100644 subprojects/libcrypto/asm/include/util.inc delete mode 100644 subprojects/libcrypto/asm/util.s delete mode 100644 subprojects/libcrypto/include/crypto/util.h diff --git a/subprojects/libcrypto/asm/include/sign.inc b/lib/crypto/asm/include/sign.inc similarity index 100% rename from subprojects/libcrypto/asm/include/sign.inc rename to lib/crypto/asm/include/sign.inc diff --git a/subprojects/libcrypto/asm/macros/function.inc b/lib/crypto/asm/macros/function.inc similarity index 100% rename from subprojects/libcrypto/asm/macros/function.inc rename to lib/crypto/asm/macros/function.inc diff --git a/subprojects/libcrypto/asm/rc4-arm4cw.s b/lib/crypto/asm/rc4-arm4cw.s similarity index 100% rename from subprojects/libcrypto/asm/rc4-arm4cw.s rename to lib/crypto/asm/rc4-arm4cw.s diff --git a/subprojects/libcrypto/asm/rc4.s b/lib/crypto/asm/rc4.s similarity index 100% rename from subprojects/libcrypto/asm/rc4.s rename to lib/crypto/asm/rc4.s diff --git a/subprojects/libcrypto/asm/rc4s-arm4cw.s b/lib/crypto/asm/rc4s-arm4cw.s similarity index 100% rename from subprojects/libcrypto/asm/rc4s-arm4cw.s rename to lib/crypto/asm/rc4s-arm4cw.s diff --git a/subprojects/libcrypto/asm/sign.s b/lib/crypto/asm/sign.s similarity index 100% rename from subprojects/libcrypto/asm/sign.s rename to lib/crypto/asm/sign.s diff --git a/subprojects/libcrypto/include/crypto.h b/lib/crypto/include/crypto.h similarity index 100% rename from subprojects/libcrypto/include/crypto.h rename to lib/crypto/include/crypto.h index f7a9dbf43d..6d23043562 100644 --- a/subprojects/libcrypto/include/crypto.h +++ b/lib/crypto/include/crypto.h @@ -5,9 +5,9 @@ extern "C" { #endif -#include -#include #include +#include +#include #ifdef __cplusplus } diff --git a/subprojects/libcrypto/include/crypto/rc4.h b/lib/crypto/include/crypto/rc4.h similarity index 70% rename from subprojects/libcrypto/include/crypto/rc4.h rename to lib/crypto/include/crypto/rc4.h index 9b26d77817..1bd7c48ddc 100644 --- a/subprojects/libcrypto/include/crypto/rc4.h +++ b/lib/crypto/include/crypto/rc4.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + typedef struct CRYPTORC4Context_t { u8 i, j; u8 padd[2]; @@ -21,24 +23,24 @@ void CRYPTO_RC4Encrypt(CRYPTORC4Context *, const void *, u32, void *); void CRYPTO_RC4FastInit(CRYPTORC4FastContext *, const void *, u32); void CRYPTO_RC4FastEncrypt(CRYPTORC4FastContext *, const void *, u32, void *); -static inline void CRYPTO_RC4Decrypt (CRYPTORC4Context * param0, const void * param1, u32 param2, void * param3) +static inline void CRYPTO_RC4Decrypt(CRYPTORC4Context *param0, const void *param1, u32 param2, void *param3) { CRYPTO_RC4Encrypt(param0, param1, param2, param3); } -static inline void CRYPTO_RC4 (const void * param0, u32 param1, void * param2, u32 param3) +static inline void CRYPTO_RC4(const void *param0, u32 param1, void *param2, u32 param3) { CRYPTORC4Context v0; CRYPTO_RC4Init(&v0, param0, param1); CRYPTO_RC4Encrypt(&v0, param2, param3, param2); } -static inline void CRYPTO_RC4FastDecrypt (CRYPTORC4FastContext * param0, const void * param1, u32 param2, void * param3) +static inline void CRYPTO_RC4FastDecrypt(CRYPTORC4FastContext *param0, const void *param1, u32 param2, void *param3) { CRYPTO_RC4FastEncrypt(param0, param1, param2, param3); } -static inline void CRYPTO_RC4Fast (const void * param0, u32 param1, void * param2, u32 param3) +static inline void CRYPTO_RC4Fast(const void *param0, u32 param1, void *param2, u32 param3) { CRYPTORC4FastContext v0; CRYPTO_RC4FastInit(&v0, param0, param1); diff --git a/subprojects/libcrypto/include/crypto/sign.h b/lib/crypto/include/crypto/sign.h similarity index 100% rename from subprojects/libcrypto/include/crypto/sign.h rename to lib/crypto/include/crypto/sign.h diff --git a/lib/crypto/include/crypto/util.h b/lib/crypto/include/crypto/util.h new file mode 100644 index 0000000000..16559c60f8 --- /dev/null +++ b/lib/crypto/include/crypto/util.h @@ -0,0 +1,16 @@ +#ifndef POKEPLATINUM_CRYPTO_UTIL_H +#define POKEPLATINUM_CRYPTO_UTIL_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +void CRYPTO_SetAllocator(void *(*allocFunc)(u32), void (*freeFunc)(void *)); + +#ifdef __cplusplus +} +#endif + +#endif // POKEPLATINUM_CRYPTO_UTIL_H diff --git a/subprojects/libcrypto/meson.build b/lib/crypto/meson.build similarity index 68% rename from subprojects/libcrypto/meson.build rename to lib/crypto/meson.build index 6939c74ad1..78d8200fd6 100644 --- a/subprojects/libcrypto/meson.build +++ b/lib/crypto/meson.build @@ -1,12 +1,16 @@ -project('libcrypto', ['c', 'nasm']) - asm_args = [ '-proc', 'arm5TE', '-16', '-gccinc' ] -public_includes = include_directories('include') +libcrypto_c_args = [ + '-nothumb', + '-wrap:sdk', '1.2/sp2p3', + '-wrap:noipa' +] + +libcrypto_public_includes = include_directories('include') libcrypto_static_srcs = files( 'asm/rc4.s', @@ -16,7 +20,7 @@ libcrypto_static_srcs = files( libcrypto_ov97_srcs = files( 'asm/rc4s-arm4cw.s', 'asm/sign.s', - 'asm/util.s' + 'src/util.c' ) libcrypto_static = static_library('crypto_static', @@ -27,8 +31,11 @@ libcrypto_static = static_library('crypto_static', libcrypto_ov97 = static_library('crypto_ov97', sources: libcrypto_ov97_srcs, + c_args: libcrypto_c_args, nasm_args: asm_args, - pic: false + pic: false, + include_directories: libcrypto_public_includes, + dependencies: nitrosdk_dep ) libcrypto_dep = declare_dependency( diff --git a/lib/crypto/src/crypto_internal.h b/lib/crypto/src/crypto_internal.h new file mode 100644 index 0000000000..6b7fa707d7 --- /dev/null +++ b/lib/crypto/src/crypto_internal.h @@ -0,0 +1,4 @@ +#include + +void *CRYPTOi_MyAlloc(u32 size); +void CRYPTOi_MyFree(void *ptr); diff --git a/lib/crypto/src/util.c b/lib/crypto/src/util.c new file mode 100644 index 0000000000..77097e87c8 --- /dev/null +++ b/lib/crypto/src/util.c @@ -0,0 +1,33 @@ +#include "crypto/util.h" + +#include +#include + +#include "crypto_internal.h" + +static void (*sFreeFunc)(void *ptr); +static void *(*sAllocFunc)(u32 size); + +void *CRYPTOi_MyAlloc(u32 size) +{ + if (sAllocFunc) { + return sAllocFunc(size); + } else { + return OS_AllocFromHeap(OS_ARENA_MAIN, OS_CURRENT_HEAP_HANDLE, size); + } +} + +void CRYPTOi_MyFree(void *ptr) +{ + if (sFreeFunc) { + sFreeFunc(ptr); + } else { + OS_FreeToHeap(OS_ARENA_MAIN, OS_CURRENT_HEAP_HANDLE, ptr); + } +} + +void CRYPTO_SetAllocator(void *(*allocFunc)(u32), void (*freeFunc)(void *)) +{ + sAllocFunc = allocFunc; + sFreeFunc = freeFunc; +} diff --git a/lib/meson.build b/lib/meson.build index ae5f650f54..86614a4442 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -1,2 +1,3 @@ +subdir('crypto') subdir('gds') subdir('spl') diff --git a/meson.build b/meson.build index a82c67297c..8c868c5f3a 100644 --- a/meson.build +++ b/meson.build @@ -128,7 +128,6 @@ nitrodwc_dep = dependency('NitroDWC', ) libvct_dep = dependency('libvct') -libcrypto_dep = dependency('libcrypto') libsyscall_dep = dependency('libsyscall') ppwlobby_dep = dependency('ppwlobby') @@ -187,6 +186,7 @@ main = executable('main', c_pch: 'include/pch/global_pch.h', include_directories: [ public_includes, + libcrypto_public_includes, libgds_public_includes, libspl_public_includes, ], diff --git a/subprojects/libcrypto/asm/include/util.inc b/subprojects/libcrypto/asm/include/util.inc deleted file mode 100644 index 63f9e8558f..0000000000 --- a/subprojects/libcrypto/asm/include/util.inc +++ /dev/null @@ -1,2 +0,0 @@ -.public OS_AllocFromHeap -.public OS_FreeToHeap \ No newline at end of file diff --git a/subprojects/libcrypto/asm/util.s b/subprojects/libcrypto/asm/util.s deleted file mode 100644 index bfb49b7d32..0000000000 --- a/subprojects/libcrypto/asm/util.s +++ /dev/null @@ -1,66 +0,0 @@ - .include "macros/function.inc" - .include "include/util.inc" - - - .text - - - arm_func_start CRYPTO_SetAllocator -CRYPTO_SetAllocator: ; 0x0223D5C8 - ldr r3, _0223D5DC ; =0x02240AF4 - ldr r2, _0223D5E0 ; =0x02240AF8 - str r0, [r3, #0] - str r1, [r2, #0] - bx lr - ; .align 2, 0 -_0223D5DC: .word 0x02240AF4 -_0223D5E0: .word 0x02240AF8 - arm_func_end CRYPTO_SetAllocator - - arm_func_start CRYPTOi_MyFree -CRYPTOi_MyFree: ; 0x0223D5E4 - stmdb sp!, {lr} - sub sp, sp, #4 - ldr r1, _0223D628 ; =0x02240AF8 - mov r2, r0 - ldr r1, [r1, #0] - cmp r1, #0 - beq _0223D610 - blx r1 - add sp, sp, #4 - ldmia sp!, {lr} - bx lr -_0223D610: - mov r0, #0 - mvn r1, #0 - bl OS_FreeToHeap - add sp, sp, #4 - ldmia sp!, {lr} - bx lr - ; .align 2, 0 -_0223D628: .word 0x02240AF8 - arm_func_end CRYPTOi_MyFree - - arm_func_start CRYPTOi_MyAlloc -CRYPTOi_MyAlloc: ; 0x0223D62C - stmdb sp!, {lr} - sub sp, sp, #4 - ldr r1, _0223D670 ; =0x02240AF4 - mov r2, r0 - ldr r1, [r1, #0] - cmp r1, #0 - beq _0223D658 - blx r1 - add sp, sp, #4 - ldmia sp!, {lr} - bx lr -_0223D658: - mov r0, #0 - mvn r1, #0 - bl OS_AllocFromHeap - add sp, sp, #4 - ldmia sp!, {lr} - bx lr - ; .align 2, 0 -_0223D670: .word 0x02240AF4 - arm_func_end CRYPTOi_MyAlloc diff --git a/subprojects/libcrypto/include/crypto/util.h b/subprojects/libcrypto/include/crypto/util.h deleted file mode 100644 index b5034d8536..0000000000 --- a/subprojects/libcrypto/include/crypto/util.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef POKEPLATINUM_UTIL_H -#define POKEPLATINUM_UTIL_H - -#ifdef __cplusplus -extern "C" { -#endif - -void CRYPTO_SetAllocator(void *(* param0)(u32), void (* param1)(void *)); - -#ifdef __cplusplus -} -#endif - -#endif // POKEPLATINUM_UTIL_H diff --git a/tools/devtools/gen_compile_commands.py b/tools/devtools/gen_compile_commands.py index 1d3f1d7d43..26d397424e 100755 --- a/tools/devtools/gen_compile_commands.py +++ b/tools/devtools/gen_compile_commands.py @@ -203,33 +203,6 @@ libvct_c_commands = [ for file in (homedir / "subprojects/libvct-1.3.1").rglob("*.c") ] -libcrypto_c_commands = [ - { - "directory": builddir, - "arguments": arm9_c_flags - + [ - f"-I{cwlibcdir}", - f"-I{cwextrasdir}", - f"-I{cwlibcarmdir}", - f"-I{cwextrasarmdir}", - f"-I{homedir}/subprojects/NitroSDK-4.2.30001/include", - f"-I{builddir}/subprojects/NitroSDK-4.2.30001/gen", - f"-I{homedir}/subprojects/NitroSystem-071126.1/include", - f"-I{homedir}/subprojects/NitroWiFi-2.1.30003/include", - f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include", - f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include/gs", - f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include/base", - f"-I{homedir}/subprojects/libvct-1.3.1/include", - f"-I{homedir}/subprojects/libcrypto/include", - "-o", - file.with_suffix(".o"), - file.resolve(), - ], - "file": file.resolve(), - } - for file in (homedir / "subprojects/libcrypto").rglob("*.c") -] - ppwlobby_c_commands = [ { "directory": builddir, @@ -247,7 +220,7 @@ ppwlobby_c_commands = [ f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include/gs", f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include/base", f"-I{homedir}/subprojects/libvct-1.3.1/include", - f"-I{homedir}/subprojects/libcrypto/include", + f"-I{homedir}/lib/crypto/include", f"-I{homedir}/subprojects/ppwlobby/include", "-o", file.with_suffix(".o"), @@ -276,8 +249,8 @@ c_commands = [ f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include/gs", f"-I{homedir}/subprojects/NitroDWC-2.2.30008/include/base", f"-I{homedir}/subprojects/libvct-1.3.1/include", - f"-I{homedir}/subprojects/libcrypto/include", f"-I{homedir}/subprojects/ppwlobby/include", + f"-I{homedir}/lib/crypto/include", f"-I{homedir}/lib/gds/include", f"-I{homedir}/lib/spl/include", f"-iquote{homedir}", @@ -346,7 +319,6 @@ with open("compile_commands.json", "w") as ofp: + nitrowifi_c_commands + nitrodwc_c_commands + libvct_c_commands - + libcrypto_c_commands + ppwlobby_c_commands + c_commands + datagen_cpp_commands diff --git a/tools/m2ctx/m2ctx.sh b/tools/m2ctx/m2ctx.sh index 55815dba5d..a010458270 100755 --- a/tools/m2ctx/m2ctx.sh +++ b/tools/m2ctx/m2ctx.sh @@ -4,7 +4,7 @@ OUT_FILE=ctx.c GCC=gcc FLAGS="-E -P -dD -undef" -INCLUDES="-Itools/cw/include -Isubprojects/NitroSDK-4.2.30001/include -Ibuild/subprojects/NitroSDK-4.2.30001/gen -Isubprojects/NitroSystem-071126.1/include -Isubprojects/NitroWiFi-2.1.30003/include -Isubprojects/NitroDWC-2.2.30008/include -Isubprojects/libvct-1.3.1/include -Isubprojects/libcrypto/include -Isubprojects/ppwlobby/include -Iinclude -Iasm -Ires -Ilib/gds/include -Ilib/spl/include -Ibuild -Ibuild/res/text -include pch/global_pch.h" +INCLUDES="-Itools/cw/include -Isubprojects/NitroSDK-4.2.30001/include -Ibuild/subprojects/NitroSDK-4.2.30001/gen -Isubprojects/NitroSystem-071126.1/include -Isubprojects/NitroWiFi-2.1.30003/include -Isubprojects/NitroDWC-2.2.30008/include -Isubprojects/libvct-1.3.1/include -Isubprojects/ppwlobby/include -Iinclude -Iasm -Ires -Ilib/crypto/include -Ilib/gds/include -Ilib/spl/include -Ibuild -Ibuild/res/text -include pch/global_pch.h" DEFINES="-DGAME_VERSION=VERSION_PLATINUM -DGAME_LANGUAGE=ENGLISH -DPM_KEEP_ASSERTS -D_NITRO -DLINK_PPWLOBBY -DNNS_FINALROM -DSDK_4M -DSDK_ARM9 -DSDK_CODE_ARM -DSDK_CW -DSDK_CW_FORCE_EXPORT_SUPPORT -DSDK_FINALROM -DSDK_TS" generate-ctx () {