Skip to content

Commit

Permalink
Add SHA1 calculation for DS mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketRobz committed Feb 12, 2024
1 parent 457a824 commit a52576b
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 183 deletions.
5 changes: 3 additions & 2 deletions arm9/source/crypto.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdint.h>
#include "../mbedtls/aes.h"
#include "sha1.h"
#include "crypto.h"
//#include "ticket0.h"
#include "utils.h"
Expand Down Expand Up @@ -117,7 +118,7 @@ static void dsi_aes_set_key(uint32_t *rk, const uint32_t *console_id, key_mode_t

int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len) {
uint8_t digest[SHA1_LEN];
my_swiSHA1Calc(digest, data, len);
SHA1((char*)digest, data, len);
// return type of swiSHA1Verify() is declared void, so how exactly should we use it?
int ret = memcmp(digest, digest_verify, SHA1_LEN);
if (ret != 0) {
Expand Down Expand Up @@ -153,7 +154,7 @@ void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int i
aes_set_key_enc_128_be(boot2_rk, (uint8_t*)DSi_BOOT2_KEY);

uint32_t digest[SHA1_LEN / sizeof(uint32_t)];
my_swiSHA1Calc(digest, emmc_cid, 16);
SHA1((char*)digest, (char*)emmc_cid, 16);
nand_ctr_iv[0] = digest[0];
nand_ctr_iv[1] = digest[1];
nand_ctr_iv[2] = digest[2];
Expand Down
3 changes: 0 additions & 3 deletions arm9/source/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ typedef enum {
ES
} key_mode_t;

// don't want to include nds.h just for this
void my_swiSHA1Calc(void *digest, const void *buf, size_t len);

int dsi_sha1_verify(const void *digest_verify, const void *data, unsigned len);

void dsi_crypt_init(const uint8_t *console_id_be, const uint8_t *emmc_cid, int is3DS);
Expand Down
14 changes: 7 additions & 7 deletions arm9/source/fileOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <dirent.h>
#include <vector>

#include "my_sha1.h"
#include "sha1.h"
#include "file_browse.h"
#include "font.h"
#include "ndsheaderbanner.h"
Expand All @@ -15,7 +15,8 @@
#define copyBufSize 0x8000
#define shaChunkSize 0x10000

u8* copyBuf = (u8*)0x02004000;
// u8* copyBuf = (u8*)0x02004000;
u8 copyBuf[copyBufSize];

std::vector<ClipboardFile> clipboard;
bool clipboardOn = false;
Expand Down Expand Up @@ -88,9 +89,8 @@ bool calculateSHA1(const char *fileName, u8 *sha1) {
return false;
}
memset(sha1, 0, 20);
swiSHA1context_t ctx;
ctx.sha_block=0; //this is weird but it has to be done
my_swiSHA1Init(&ctx);
SHA1_CTX ctx;
SHA1Init(&ctx);

font->clear(false);
font->printf(firstCol, 0, false, alignStart, Palette::white, STR_CALCULATING_SHA1.c_str(), fileName);
Expand All @@ -105,7 +105,7 @@ bool calculateSHA1(const char *fileName, u8 *sha1) {
while (true) {
size_t ret = fread(buf, 1, shaChunkSize, fp);
if (!ret) break;
my_swiSHA1Update(&ctx, buf, ret);
SHA1Update(&ctx, buf, ret);
scanKeys();
int keys = keysHeld();
if (keys & KEY_START) {
Expand All @@ -121,7 +121,7 @@ bool calculateSHA1(const char *fileName, u8 *sha1) {
font->printf(firstCol, nameHeight + 6, false, alignStart, Palette::white, STR_N_OF_N_BYTES_PROCESSED.c_str(), ftell(fp), fsize);
font->update(false);
}
my_swiSHA1Final(sha1, &ctx);
SHA1Final(sha1, &ctx);
free(buf);
fclose(fp);
return true;
Expand Down
7 changes: 1 addition & 6 deletions arm9/source/file_browse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,7 @@ FileOperation fileBrowse_A(DirEntry* entry, const char *curdir) {
}

operations.push_back(FileOperation::hexEdit);

// The bios SHA1 functions are only available on the DSi
// https://problemkaputt.de/gbatek.htm#biossha1functionsdsionly
if (bios9iEnabled) {
operations.push_back(FileOperation::calculateSHA1);
}
operations.push_back(FileOperation::calculateSHA1);
}

operations.push_back(FileOperation::showInfo);
Expand Down
12 changes: 6 additions & 6 deletions arm9/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool screenSwapped = false;

bool arm7SCFGLocked = false;
bool isRegularDS = true;
bool bios9iEnabled = false;
// bool bios9iEnabled = false;
bool is3DS = false;
int ownNitroFSMounted;
std::string prevTime;
Expand Down Expand Up @@ -124,7 +124,7 @@ int main(int argc, char **argv) {
fifoSendValue32(FIFO_USER_07, 0);

if (isDSiMode()) {
bios9iEnabled = true;
// bios9iEnabled = true;
if (!arm7SCFGLocked) {
//font->print(-2, -4, false, " Held - Disable NAND access", Alignment::right);
font->print(-2, -3, false, " Held - Disable cart access", Alignment::right);
Expand Down Expand Up @@ -183,7 +183,7 @@ int main(int argc, char **argv) {
is3DS = fifoGetValue32(FIFO_USER_05) != 0xD2;
}

FILE* bios = fopen("sd:/_nds/bios9i.bin", "rb");
/* FILE* bios = fopen("sd:/_nds/bios9i.bin", "rb");
if (!bios) {
bios = fopen("sd:/_nds/bios9i_part1.bin", "rb");
}
Expand Down Expand Up @@ -229,10 +229,10 @@ int main(int argc, char **argv) {
}
setVectorBase(0);
bios9iEnabled = true;
bios9iEnabled = true; */

nandMount(); // Returns corrupt data for some reason
}
nandMount();
// }
} else if (isRegularDS && (io_dldi_data->ioInterface.features & FEATURE_SLOT_NDS)) {
ramdriveMount(false);
}
Expand Down
2 changes: 1 addition & 1 deletion arm9/source/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern bool screenSwapped;

extern bool arm7SCFGLocked;
extern bool isRegularDS;
extern bool bios9iEnabled;
// extern bool bios9iEnabled;
extern bool is3DS;
extern int ownNitroFSMounted;

Expand Down
65 changes: 0 additions & 65 deletions arm9/source/my_sha1.c

This file was deleted.

87 changes: 0 additions & 87 deletions arm9/source/my_sha1.h

This file was deleted.

Loading

0 comments on commit a52576b

Please sign in to comment.