NTR_Launcher/arm9/common/encryption.c
ApacheThunder a4e5a00409 Big changes....
* Stage2 launcher UI added. This is a heavily modified HBMenu with
ability to launch carts! This menu can be used to boot a bootloader NDS
file for flascharts that the direct cart launcher fails to boot.
* New audio files added for new menu.
* HBMenu UI loaded by default if booted with no cart inserted.
* INI file folder now located in NTR_Launcher folder instead. A default
ini file from NitroFS (if mount of nitrofs succeeds) will be copied to
SD if one is not present.
* Stage2 launchers are expected to be in NTR Launcehr folder though you
can navigate to any folder you want in the UI so stage2 launchers aren't
the only thing this can be used for. You can also use this as a means of
booting homebrew off SD in NTR mode.
2024-04-10 22:59:52 -05:00

113 lines
2.8 KiB
C

/*
NitroHax -- Cheat tool for the Nintendo DS
Copyright (C) 2008 Michael "Chishm" Chisholm
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "encryption.h"
#include "key1.h"
#include "key2.h"
#include "tonccpy.h"
#define KEYSIZE 0x1048
static u32 keycode [3];
static u32 keybuf [KEYSIZE/sizeof(u32)];
void crypt_64bit_up (u32* ptr) {
u32 x = ptr[1];
u32 y = ptr[0];
u32 z;
int i;
for (i = 0; i < 0x10; i++) {
z = keybuf[i] ^ x;
x = keybuf[0x012 + ((z>>24)&0xff)];
x = keybuf[0x112 + ((z>>16)&0xff)] + x;
x = keybuf[0x212 + ((z>> 8)&0xff)] ^ x;
x = keybuf[0x312 + ((z>> 0)&0xff)] + x;
x = y ^ x;
y = z;
}
ptr[0] = x ^ keybuf[0x10];
ptr[1] = y ^ keybuf[0x11];
}
void crypt_64bit_down (u32* ptr) {
u32 x = ptr[1];
u32 y = ptr[0];
u32 z;
int i;
for (i = 0x11; i > 0x01; i--) {
z = keybuf[i] ^ x;
x = keybuf[0x012 + ((z>>24)&0xff)];
x = keybuf[0x112 + ((z>>16)&0xff)] + x;
x = keybuf[0x212 + ((z>> 8)&0xff)] ^ x;
x = keybuf[0x312 + ((z>> 0)&0xff)] + x;
x = y ^ x;
y = z;
}
ptr[0] = x ^ keybuf[0x01];
ptr[1] = y ^ keybuf[0x00];
}
static u32 bswap_32bit (u32 in) {
u8 a,b,c,d;
a = (u8)((in >> 0) & 0xff);
b = (u8)((in >> 8) & 0xff);
c = (u8)((in >> 16) & 0xff);
d = (u8)((in >> 24) & 0xff);
u32 out = (a << 24) | (b << 16) | (c << 8) | (d << 0);
return out;
}
void apply_keycode (u32 modulo) {
u32 scratch[2];
int i;
modulo = modulo / sizeof(*keycode);
crypt_64bit_up (&keycode[1]);
crypt_64bit_up (&keycode[0]);
toncset (scratch, 0, 8);
for (i = 0; i < 0x12; i+=1) {
keybuf[i] = keybuf[i] ^ bswap_32bit (keycode[i % modulo]);
}
for (i = 0; i < 0x412; i+=2) {
crypt_64bit_up (scratch);
keybuf[i] = scratch[1];
keybuf[i+1] = scratch[0];
}
}
void init_keycode (u32 idcode, u32 level, u32 modulo, int iCardDevice) {
tonccpy ((u8*)keybuf, (iCardDevice ? gEncrDataTwl : gEncrData), KEYSIZE);
keycode[0] = idcode;
keycode[1] = idcode/2;
keycode[2] = idcode*2;
if (level >= 1) apply_keycode (modulo); // first apply (always)
if (level >= 2) apply_keycode (modulo); // second apply (optional)
keycode[1] = keycode[1] * 2;
keycode[2] = keycode[2] / 2;
if (level >= 3) apply_keycode (modulo); // third apply (optional)
}