NTR_Launcher/BootLoader/source/modcrypt/u128_math.c
ApacheThunder de31f9cb2b 1.3 Refactor
* Added DSOnei kernel to included nds files for Stage2 menu.
* Added N-Card rom dump to included nds files for Stage2 menu.
* Added CycloDS, and DSTWo bootloader dumps to included nds files for
Stage2 menu.
* DSTwo now boots correctly from cart launcher.
* R4 SDHC Gold and other similar DEMON time bomb DSTTi clones now boot
correctly from cart launcher.
* Added back option for enabling/disabling TWL ram.
* Added fixes to allow DS only carts to run with TWL ram enabled.
* Initial modcrypt code added for TWL carts. Currently works in
emulation however TWL carts will fail to boot on hardware (when twl
mode, ram, etc is enabled).
* If TWL mode and ram is enabled, cart loader will now load the DSi
extended binaries into ram. Currently however they will only boot in
emulation. Have not resolved why it's not working on hardware yet.
* Stage2 menu now allowed to load dsi extended binaries of SRLs if TWL
mode and TWL ram is enabled. Booting rom dumps as a method of booting
into TWL carts is confirmed working. At least for System Flaw it does.
:D
* Despite the improvents Acekard 2i still appears to require using the
stage2 menu to boot into.
* Fixes that allowed Demon timebomb carts to boot from cart
launcher/autoboot may allow other non working carts to work. Further
testing needed.
2024-11-27 21:50:32 -06:00

106 lines
2.4 KiB
C

// u128_math
#include "u128_math.h"
#include <string.h>
// rotate a 128bit, little endian by shift bits in direction of increasing significance.
void u128_lrot(uint8_t *num, uint32_t shift)
{
uint8_t tmp[16];
for (int i=0;i<16;i++)
{
// rot: rotate to more significant.
// LSB is tmp[0], MSB is tmp[15]
const uint32_t byteshift = shift / 8 ;
const uint32_t bitshift = shift % 8;
tmp[(i+byteshift) % 16] = (num[i] << bitshift)
| ((num[(i+16-1) % 16] >> (8-bitshift)) & 0xff);
}
memcpy(num, tmp, 16) ;
}
// rotate a 128bit, little endian by shift bits in direction of decreasing significance.
void u128_rrot(uint8_t *num, uint32_t shift)
{
uint8_t tmp[16];
for (int i=0;i<16;i++)
{
// rot: rotate to less significant.
// LSB is tmp[0], MSB is tmp[15]
const uint32_t byteshift = shift / 8 ;
const uint32_t bitshift = shift % 8;
tmp[i] = (num[(i+byteshift) % 16] >> bitshift)
| ((num[(i+byteshift+1) % 16] << (8-bitshift)) & 0xff);
}
memcpy(num, tmp, 16) ;
}
// xor two 128bit, little endian values and store the result into the first
void u128_xor(uint8_t *a, const uint8_t *b)
{
for (int i=0;i<16;i++)
{
a[i] = a[i] ^ b[i] ;
}
}
// or two 128bit, little endian values and store the result into the first
void u128_or(uint8_t *a, const uint8_t *b)
{
for (int i=0;i<16;i++)
{
a[i] = a[i] | b[i] ;
}
}
// and two 128bit, little endian values and store the result into the first
void u128_and(uint8_t *a, const uint8_t *b)
{
for (int i=0;i<16;i++)
{
a[i] = a[i] & b[i] ;
}
}
// add two 128bit, little endian values and store the result into the first
void u128_add(uint8_t *a, const uint8_t *b)
{
uint8_t carry = 0 ;
for (int i=0;i<16;i++)
{
uint16_t sum = a[i] + b[i] + carry ;
a[i] = sum & 0xff ;
carry = sum >> 8 ;
}
}
// add two 128bit, little endian values and store the result into the first
void u128_add32(uint8_t *a, const uint32_t b)
{
uint8_t _b[16];
memset(_b, 0, sizeof(_b)) ;
for (int i=0;i<4;i++)
_b[i] = b >> (i*8) ;
u128_add(a, _b);
}
// sub two 128bit, little endian values and store the result into the first
void u128_sub(uint8_t *a, const uint8_t *b)
{
uint8_t carry = 0 ;
for (int i=0;i<16;i++)
{
uint16_t sub = a[i] - b[i] - (carry & 1);
a[i] = sub & 0xff ;
carry = sub >> 8 ;
}
}
void u128_swap(uint8_t *out, const uint8_t *in)
{
for (int i=0;i<16;i++)
{
out[15-i] = in[i];
}
}