Skip to content

Commit

Permalink
commit all api changes for hashlib
Browse files Browse the repository at this point in the history
  • Loading branch information
acagliano committed Apr 26, 2022
1 parent 1795250 commit 444f6d7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/asm/cemu_pipes.asm
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

section .text

public _cemu_check
public _cemu_get
public _cemu_send
Expand Down Expand Up @@ -44,4 +47,4 @@ _cemu_get:
push bc
pop hl

ret
ret
2 changes: 2 additions & 0 deletions src/asm/getkey.asm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
section .text


public _getKey
public _getKeyAsync
Expand Down
2 changes: 2 additions & 0 deletions src/asm/user_input.asm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
;user input subroutine for use with ez80 C toolchain programs
;author: Adam "beckadamtheinventor" Beckingham

section .text

public _user_input
extern _gfx_SetColor
extern _gfx_PrintChar
Expand Down
26 changes: 11 additions & 15 deletions src/lcars/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,22 @@ bool gui_Login(uint8_t* key) {
uint8_t ct[PPT_LEN];

gfx_TextClearBG("encrypting auth token...", 20, 190);
hashlib_AESLoadKey(key, &ctx, 32); // load secret key
hashlib_RandomBytes(iv, AES_BLOCKSIZE); // get IV
aes_init(key, &ctx, 32); // load secret key
csrand_fill(iv, AES_BLOCKSIZE); // get IV

// Pad plaintext
//hashlib_AESPadMessage(settings.login_key, LOGIN_TOKEN_SIZE, ppt, SCHM_DEFAULT);
// this is no longer necessary as of HASHLIB 8

// Encrypt the login token with AES-256
if(hashlib_AESEncrypt(settings.login_key, LOGIN_TOKEN_SIZE, ct, &ctx, iv, AES_MODE_CBC, SCHM_DEFAULT) != AES_OK) return false;
if(aes_encrypt(settings.login_key, LOGIN_TOKEN_SIZE, ct, &ctx, iv, AES_MODE_CBC, SCHM_DEFAULT) != AES_OK) return false;
gfx_TextClearBG("logging you in...", 20, 190);
ntwk_send(LOGIN,
PS_PTR(iv, AES_BLOCKSIZE),
PS_PTR(ct, PPT_LEN)
);

// Zero out key schedule, key used, and IV
hashlib_EraseContext(iv, AES_BLOCKSIZE);
hashlib_EraseContext(&ctx, sizeof(aes_ctx));
hashlib_EraseContext(key, 32);
hashlib_EraseContext(ct, PPT_LEN);

return true;
}
Expand All @@ -248,31 +244,31 @@ bool gui_NewGame(void) {
return ntwk_send_nodata(NEW_GAME_REQUEST);
}

void srv_request_gfx(sha256_ctx *ctx, uint8_t *mbuffer){
void srv_request_gfx(hash_ctx *ctx){
ti_var_t f;
uint8_t digest[SHA256_DIGEST_SIZE];
gfx_TextClearBG("Hashing gfx for version...", 20, 190);
hashlib_Sha256Init(ctx, mbuffer);
hash_init(ctx, SHA256);
if((f = ti_Open(gfx_appv_name, "r"))){
hashlib_Sha256Update(ctx, ti_GetDataPtr(f), ti_GetSize(f));
hash_update(ctx, ti_GetDataPtr(f), ti_GetSize(f));
ti_Close(f);
}
hashlib_Sha256Final(ctx, digest);
hash_final(ctx, digest);
gfx_TextClearBG("Comparing version w/ server...", 20, 190);
ntwk_send(GFX_REQ_UPDATE, PS_PTR(digest, SHA256_DIGEST_SIZE));
}

void srv_request_client(sha256_ctx *ctx, uint8_t *mbuffer){
void srv_request_client(hash_ctx *ctx){
ti_var_t f;
uint8_t digest[SHA256_DIGEST_SIZE];
gfx_TextClearBG("", 20, 200);
gfx_TextClearBG("Hashing client for version...", 20, 190);
hashlib_Sha256Init(ctx, mbuffer);
hash_init(ctx, SHA256);
if((f = ti_OpenVar("TITREK", "r", TI_PPRGM_TYPE))){
hashlib_Sha256Update(ctx, ti_GetDataPtr(f), ti_GetSize(f));
hash_update(ctx, ti_GetDataPtr(f), ti_GetSize(f));
ti_Close(f);
}
hashlib_Sha256Final(ctx, digest);
hash_final(ctx, digest);
gfx_TextClearBG("Comparing version w/ server...", 20, 190);
ntwk_send(MAIN_REQ_UPDATE, PS_PTR(digest, SHA256_DIGEST_SIZE));
}
4 changes: 2 additions & 2 deletions src/lcars/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void gfx_RenderMenuTitle(const char* title, uint24_t x, uint8_t y);

uint8_t prompt_for(char* prompt, char* buffer, size_t len, uint24_t x, uint8_t y, uint8_t flags);

void srv_request_gfx(sha256_ctx *ctx, uint8_t *mbuffer);
void srv_request_client(sha256_ctx *ctx, uint8_t *mbuffer);
void srv_request_gfx(hash_ctx *ctx);
void srv_request_client(hash_ctx *ctx);

#endif
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ int main(void) {
gfx_Begin();
srandom(rtc_Time());
ti_CloseAll();
csrand_init();

if((savefile = ti_Open(settingsappv, "r")) && (ti_GetSize(savefile) == sizeof(settings))){
ti_Read(&settings, sizeof(settings), 1, savefile);
ti_Close(savefile);
Expand Down
31 changes: 15 additions & 16 deletions src/network/in.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ ti_var_t gfx_fp = 0, client_fp = 0;
extern uint8_t *gfx_appv_name = "TrekGFX";
size_t gfx_dl_size, client_dl_size;
size_t gfx_bytes_written = 0, client_bytes_written = 0;
sha256_ctx gfx_hash, client_hash;
uint32_t mbuffer[64];
hash_ctx gfx_hash, client_hash;
uint8_t aes_key[AES_KEYLEN] = {0};


Expand Down Expand Up @@ -70,11 +69,11 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
uint8_t msg[256] = {0};
uint8_t encr_text[32] = {0};
gfx_TextClearBG("generating AES key...", 20, 190);
hashlib_RandomBytes(aes_key, AES_KEYLEN);
csrand_fill(aes_key, AES_KEYLEN);
sprintf(encr_text, "RSA-%u encrypting...", keylen<<3);
gfx_TextClearBG(encr_text, 20, 190);
hexdump(key, keylen, "---RSA Key---");
hashlib_RSAEncrypt(aes_key, AES_KEYLEN, msg, key, keylen);
rsa_encrypt(aes_key, AES_KEYLEN, msg, key, keylen, SHA256);
hexdump(msg, keylen, "---Encrypted---");
ntwk_send(RSA_SEND_SESSION_KEY, PS_PTR(msg, keylen));
break;
Expand All @@ -84,7 +83,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
break;
case CONNECT:
netflags.bridge_up = true;
srv_request_client(&client_hash, mbuffer);
srv_request_client(&client_hash);
break;
case DISCONNECT:
netflags.logged_in = false;
Expand All @@ -96,7 +95,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
case LOGIN:
if(response == SUCCESS) {
netflags.logged_in = true;
srv_request_gfx(&gfx_hash, mbuffer); // see lcars/gui.c
srv_request_gfx(&gfx_hash); // see lcars/gui.c
}
else if(response==INVALID){
gfx_ErrorClearBG("auth token invalid", 20, 190);
Expand Down Expand Up @@ -172,7 +171,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
engine_ref.loaded = true;
break;
case GFX_FRAME_START: // 91
hashlib_Sha256Init(&gfx_hash, mbuffer);
hash_init(&gfx_hash, SHA256);
memcpy(&gfx_dl_size, data, sizeof(size_t));
ntwk_send_nodata(GFX_FRAME_NEXT);
gfx_bytes_written = 0;
Expand All @@ -183,7 +182,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
if(!gfx_fp) {if(!(gfx_fp = ti_Open("_TrekGFX", "w"))) break;}
if(ti_Write(data, buff_size-1, 1, gfx_fp))
gfx_bytes_written += buff_size-1;
hashlib_Sha256Update(&gfx_hash, data, buff_size-1);
hash_update(&gfx_hash, data, buff_size-1);
sprintf(msg, "Gfx download: %u%%", (100*gfx_bytes_written/gfx_dl_size));
gfx_TextClearBG(msg, 20, 190);
ntwk_send_nodata(GFX_FRAME_NEXT); // 93
Expand All @@ -192,8 +191,8 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
case GFX_FRAME_DONE: // 94
if(gfx_fp){
uint8_t digest[SHA256_DIGEST_SIZE];
hashlib_Sha256Final(&gfx_hash, digest);
if(hashlib_CompareDigest(digest, data, SHA256_DIGEST_SIZE)){
hash_final(&gfx_hash, digest);
if(digest_compare(digest, data, SHA256_DIGEST_SIZE)){
ti_Delete("TrekGFX");
ti_Rename("_TrekGFX", "TrekGFX");
ti_SetArchiveStatus(true, gfx_fp);
Expand All @@ -205,7 +204,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
gameflags.gfx_error = true;
ti_Close(gfx_fp);
ti_Delete(gfx_appv_name);
srv_request_gfx(&gfx_hash, mbuffer); // see lcars/gui.c
srv_request_gfx(&gfx_hash); // see lcars/gui.c
break;
}
}
Expand All @@ -218,7 +217,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
}
break;
case MAIN_FRAME_START: // 91
hashlib_Sha256Init(&client_hash, mbuffer);
hash_init(&client_hash, SHA256);
memcpy(&client_dl_size, data, sizeof(size_t));
ntwk_send_nodata(MAIN_FRAME_NEXT);
client_bytes_written = 0;
Expand All @@ -229,7 +228,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
if(!client_fp) {if(!(client_fp = ti_OpenVar("_TITREK", "w", TI_PPRGM_TYPE))) break;}
if(ti_Write(data, buff_size-1, 1, client_fp))
client_bytes_written += buff_size-1;
hashlib_Sha256Update(&client_hash, data, buff_size-1);
hash_update(&client_hash, data, buff_size-1);
sprintf(msg, "Client download: %u%%", (100*client_bytes_written/client_dl_size));
gfx_TextClearBG(msg, 20, 190);
ntwk_send_nodata(MAIN_FRAME_NEXT); // 93
Expand All @@ -238,8 +237,8 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
case MAIN_FRAME_DONE: // 94
if(client_fp){
uint8_t digest[SHA256_DIGEST_SIZE];
hashlib_Sha256Final(&client_hash, digest);
if(hashlib_CompareDigest(digest, data, SHA256_DIGEST_SIZE)){
hash_final(&client_hash, digest);
if(digest_compare(digest, data, SHA256_DIGEST_SIZE)){
ntwk_send_nodata(DISCONNECT);
ti_Close(client_fp);
gfx_End();
Expand All @@ -251,7 +250,7 @@ void conn_HandleInput(packet_t *in_buff, size_t buff_size) {
else {
gfx_ErrorClearBG("client download error", 20, 190);
ti_Close(client_fp);
srv_request_client(&client_hash, mbuffer); // see lcars/gui.c
srv_request_client(&client_hash); // see lcars/gui.c
break;
}
}
Expand Down

0 comments on commit 444f6d7

Please sign in to comment.