Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions include/aes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifdef RB3E_WII

#ifndef _AES_H_
#define _AES_H_

#include <stdint.h>
#include <stddef.h>

// #define the macros below to 1/0 to enable/disable the mode of operation.
//
// CBC enables AES encryption in CBC-mode of operation.
// CTR enables encryption in counter-mode.
// ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.

// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
#ifndef CBC
#define CBC 1
#endif

#ifndef ECB
#define ECB 1
#endif

#ifndef CTR
#define CTR 0
#endif


#define AES128 1
//#define AES192 1
//#define AES256 1

#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only

#if defined(AES256) && (AES256 == 1)
#define AES_KEYLEN 32
#define AES_keyExpSize 240
#elif defined(AES192) && (AES192 == 1)
#define AES_KEYLEN 24
#define AES_keyExpSize 208
#else
#define AES_KEYLEN 16 // Key length in bytes
#define AES_keyExpSize 176
#endif

struct AES_ctx
{
uint8_t RoundKey[AES_keyExpSize];
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
uint8_t Iv[AES_BLOCKLEN];
#endif
};

void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
#endif

#if defined(ECB) && (ECB == 1)
// buffer size is exactly AES_BLOCKLEN bytes;
// you need only AES_init_ctx as IV is not used in ECB
// NB: ECB is considered insecure for most uses
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);

#endif // #if defined(ECB) && (ECB == !)


#if defined(CBC) && (CBC == 1)
// buffer size MUST be mutile of AES_BLOCKLEN;
// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
// no IV should ever be reused with the same key
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);

#endif // #if defined(CBC) && (CBC == 1)


#if defined(CTR) && (CTR == 1)

// Same function for encrypting as for decrypting.
// IV is incremented for every block, and used after encryption as XOR-compliment for output
// Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
// no IV should ever be reused with the same key
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);

#endif // #if defined(CTR) && (CTR == 1)


#endif // _AES_H_

#endif // RB3E_WII
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct _RB3E_Config
// [Wii]
char NASServer[RB3E_MAX_DOMAIN];
char LegacySDMode;
char ModernSDMode;
#endif
#ifdef RB3EDEBUG
// [Debug]
Expand Down
6 changes: 6 additions & 0 deletions include/ports_wii.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
#define PORT_DATAREGISTERFUNC 0x8031b2b8 // DataRegisterFunc
#define PORT_FILEISLOCAL 0x802fb548 // FileIsLocal
#define PORT_FILEISDLC 0x802fb54c // FileIsDLC
#define PORT_SDMODECHECK 0x802F5638 // WiiContentMgr::SDModeCheck
// instance addresses
#define PORT_MODIFIERMGR_POINTER 0x808fda68 // pointer to ModifierManager
#define PORT_ROCKCENTRALGATEWAY 0x80900870 // address of RockCentralGateway
Expand Down Expand Up @@ -141,6 +142,11 @@
#define PORT_PPCHALT 0x80706390 // PPCHalt
#define PORT_OSRETURNTOMENU 0x8076a610 // OSReturnToMenu
#define PORT_OSREADROM 0x8076ae20 // OSReadROM
#define PORT_ARCINITHANDLE 0x806ffdb0 // ARCInitHandle
#define PORT_CONTENTINITHANDLETITLENAND 0x80733d60 // contentInitHandleTitleNAND
#define PORT_CNTRELEASEHANDLE 0x80734620 // CNTReleaseHandle
#define PORT_CNTREAD 0x807349d0 // CNTRead
#define PORT_ECGETCONTENTINFOS 0x807a9680 // EC_GetContentInfos

// define logging functions
#define RB3E_PRINT printf
Expand Down
6 changes: 6 additions & 0 deletions include/ports_wii_bank8.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
#define PORT_DATAREGISTERFUNC 0x804545e0 // DataRegisterFunc
#define PORT_FILEISLOCAL 0x80422560 // FileIsLocal
#define PORT_FILEISDLC 0x80422570 // FileIsDLC
#define PORT_SDMODECHECK 0x8041a790 // WiiContentMgr::SDModeCheck
// instance addresses
#define PORT_MODIFIERMGR_POINTER 0x80c904a8 // pointer to ModifierManager
#define PORT_ROCKCENTRALGATEWAY 0x80c91818 // address of RockCentralGateway
Expand Down Expand Up @@ -143,6 +144,11 @@
#define PORT_PPCHALT 0x80a49600 // PPCHalt
#define PORT_OSRETURNTOMENU 0x80aad970 // OSReturnToMenu
#define PORT_OSREADROM 0x80aae180 // OSReadROM
#define PORT_ARCINITHANDLE 0x80a43020 // ARCInitHandle
#define PORT_CONTENTINITHANDLETITLENAND 0x80a76fd0 // contentInitHandleTitleNAND
#define PORT_CNTRELEASEHANDLE 0x80a77890 // CNTReleaseHandle
#define PORT_CNTREAD 0x80a77c40 // CNTRead
#define PORT_ECGETCONTENTINFOS 0x80aed870 // EC_GetContentInfos
// bank8 specific stuff
#define PORT_BANK8_MEM2_RSO_ASSERT1 0x804428e8
#define PORT_BANK8_MEM2_RSO_ASSERT2 0x80442940
Expand Down
57 changes: 51 additions & 6 deletions include/rvl/cnt.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef _RVL_CNT_H
#define _RVL_CNT_H

#include <stdint.h>
#include <rvl/mem.h>
#include "emvolution/dvd.h"

// -- ARC/U8, on-disk --

// 'U.8-'
Expand All @@ -13,7 +17,7 @@ typedef struct _u8_header_t {
int first_node;
int nodes_size;
int data_offset;
int reserved[4];
unsigned int reserved[4];
} u8_header_t; // 0x20

// the types of nodes within an ARC
Expand Down Expand Up @@ -46,9 +50,21 @@ typedef struct _u8_node_t {
// -- ARC, internally --

typedef struct _arc_handle_t {
unsigned char backing[0x1C];
u8_header_t *header;
u8_node_t *nodes;
uint8_t *file;
unsigned int count;
const char *strings;
unsigned int fstSize;
int entrynum;
} arc_handle_t;

typedef struct _arc_file_info_t {
arc_handle_t *handle;
unsigned int offset;
unsigned int size;
} arc_file_info_t;

typedef struct _arc_entry_t {
arc_handle_t *handle;
unsigned int path;
Expand All @@ -63,8 +79,21 @@ typedef enum _cnt_handle_type {
cnthandle_dvd = 2
} cnt_handle_type;

typedef struct _cnt_handle_nand {
arc_handle_t ArcHandle;
long FileDescriptor;
void *allocator;
} cnt_handle_nand;

typedef struct _cnt_handle_dvd {
unsigned char backing[0x24];
} cnt_handle_dvd;

typedef struct _cnt_handle {
unsigned char backing[0x28]; // TODO: split between ARC/NAND and DVD
union {
cnt_handle_nand nand;
cnt_handle_dvd dvd;
};
unsigned char type;
} cnt_handle;

Expand All @@ -79,14 +108,28 @@ typedef struct _cnt_dir_entry {
};
unsigned char type;
} cnt_dir_entry;
int a = sizeof(cnt_dir_entry);

typedef struct _cnt_file_info_nand {
cnt_handle_nand *CntHandle;
unsigned int startoffset;
unsigned int length;
long readOffset;
} cnt_file_info_nand;

typedef struct _cnt_file_info_dvd {
DVDFileInfo fileInfo;
long readOffset;
} cnt_file_info_dvd;

typedef struct _cnt_file_info {
unsigned char backing[0x40];
union {
cnt_file_info_nand nand;
cnt_file_info_dvd dvd;
};
unsigned char type;
} cnt_file_info;

cnt_handle *contentInitHandleTitleNAND(unsigned long long title_id, unsigned int content_index, cnt_handle *handle, void *allocator);
int contentInitHandleTitleNAND(unsigned long long title_id, unsigned int content_index, cnt_handle *handle, MEMAllocator *allocator);
int CNTReleaseHandle(cnt_handle *handle);

int CNTOpen(cnt_handle *handle, const char *path, cnt_file_info *file_info);
Expand All @@ -99,4 +142,6 @@ int CNTOpenDir(cnt_handle *handle, const char *path, cnt_dir *dir);
int CNTReadDir(cnt_dir *dir, cnt_dir_entry *entry);
int CNTCloseDir(cnt_dir *dir);

int ARCInitHandle(void *bin, arc_handle_t *handle);

#endif // _RVL_CNT_H
20 changes: 20 additions & 0 deletions include/rvl/mem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _RVL_MEM_H

typedef struct MEMAllocator;

typedef void* (*MEMAllocatorAllocFunc)(struct MEMAllocator* allocator, unsigned int size);
typedef void (*MEMAllocatorFreeFunc)(struct MEMAllocator* allocator, void* block);

typedef struct _MEMAllocatorFuncs {
MEMAllocatorAllocFunc allocFunc;
MEMAllocatorFreeFunc freeFunc;
} MEMAllocatorFuncs;

typedef struct _MEMAllocator {
MEMAllocatorFuncs *funcs;
void *heap;
unsigned int heapParam1;
unsigned int heapParam2;
} MEMAllocator;

#endif // _RVL_MEM_H
29 changes: 29 additions & 0 deletions include/wii_cnt_crypt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
RB3Enhanced - wii_cnt_crypt.h
*/

#ifdef RB3E_WII

#include <stdint.h>

typedef struct _RB3E_CNTFileSD {
int fd; // 0x0
uint32_t titleId; // 0x4
int contentLength; // 0x8
int startOffset; // 0xC
uint16_t contentIndex; // 0x10
uint16_t padding; // 0x12 - probably not needed
struct AES_ctx *aesCtx; // 0x14
uint8_t *arcHeader; // 0x18
int lastBlockIndex; // 0x1c
uint8_t aesKey[0x10]; // 0x20
uint8_t lastBlock[0x10]; // 0x30
uint8_t lastBlockEnc[0x10]; // 0x40
} RB3E_CNTFileSD;

RB3E_CNTFileSD *RB3E_OpenCNTFileSD(const char *filepath, unsigned long long titleid, unsigned int index);
void RB3E_CloseCNTFileSD(RB3E_CNTFileSD *file);
void RB3E_CNTFileRead(RB3E_CNTFileSD *file, int offset, uint8_t *buffer, int length);
void TryToLoadPRNGKeyFromFile();

#endif // RB3E_WII
9 changes: 9 additions & 0 deletions include/wii_cnt_hooks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
RB3Enhanced - wii_cnt_hooks.h
*/

#ifdef RB3E_WII

void InitCNTHooks();

#endif
6 changes: 6 additions & 0 deletions source/_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ RB3E_STUB(OSFatal)
RB3E_STUB(OSSetErrorHandler)
RB3E_STUB(PPCHalt)
RB3E_STUB(OSReturnToMenu)
RB3E_STUB(ARCInitHandle)
RB3E_STUB(contentInitHandleTitleNAND)
RB3E_STUB(CNTReleaseHandle)
RB3E_STUB(CNTRead)
RB3E_STUB(EC_GetContentInfos)
RB3E_STUB(CNTOpen)
#endif

RB3E_STUB(RB3EStubEnd);
Loading