Added XOR encryption

This commit is contained in:
Maddox Werts 2024-09-09 00:41:04 -04:00
parent fce394fd59
commit c9680982c8
6 changed files with 91 additions and 10 deletions

View file

@ -37,7 +37,7 @@ char** split_string(const char* str, char delimiter, int* count) {
}
void manager_loadnames(Manager* manager){
// Loading the names of all passwords
char* data = store_load("Data");
char* data = secure_storage_load(manager->secure_storage, "Data");
// Do we not have any passwords?
if (!data) {
@ -90,6 +90,7 @@ Manager* manager_init() {
Manager* result = malloc(sizeof(Manager));
// Setting variables
result->secure_storage = secure_storage_init("password");
result->count = 0;
// Loading all names on start
@ -100,9 +101,9 @@ Manager* manager_init() {
}
// Functions
void manager_savepass(Password* pass) {
void manager_savepass(Manager* manager, Password* pass) {
// Our resulting string
char* result = store_load("Data");
char* result = secure_storage_load(manager->secure_storage, "Data");
// Is the result empty?
if(!result) {
@ -115,7 +116,7 @@ void manager_savepass(Password* pass) {
// Saving the data
char* data = malloc(sizeof(char) * furi_string_size(n_result));
strcpy(data, furi_string_get_cstr(n_result));
store_save("Data", data);
secure_storage_save(manager->secure_storage, "Data", data);
// Cleanup
furi_string_free(n_result);
@ -130,7 +131,7 @@ void manager_savepass(Password* pass) {
// Saving the data
char* data = malloc(sizeof(char) * furi_string_size(new_result));
strcpy(data, furi_string_get_cstr(new_result));
store_save("Data", data);
secure_storage_save(manager->secure_storage, "Data", data);
// Cleanup
furi_string_free(new_result);
@ -151,7 +152,7 @@ void manager_loadpass(Manager* manager, char* name) {
FURI_LOG_D(TAG, "Loading password of name %s", name);
// Loading password string
char* data = store_load("Data");
char* data = secure_storage_load(manager->secure_storage, "Data");
// Splitting string
int count = 0;

View file

@ -7,13 +7,14 @@
#include "base.h"
#include "pass.h"
#include "store.h"
#include "secure.h"
// Structures
typedef struct {
Password* current;
Password* new;
SecureStorage* secure_storage;
char** names;
int count;
} Manager;
@ -22,7 +23,7 @@ typedef struct {
Manager* manager_init();
// Functions
void manager_savepass(Password* pass);
void manager_savepass(Manager* manager, Password* pass);
void manager_loadpass(Manager* manager, char* name);
void manager_free(Manager* manager);

View file

@ -3,7 +3,6 @@
#define H_PASS
// Libraries
#include "store.h"
// Structures
typedef struct {

59
project/backend/secure.c Normal file
View file

@ -0,0 +1,59 @@
// Header
#include "secure.h"
// Constructors
SecureStorage* secure_storage_init(char* key){
// Creating memory for secure storage
SecureStorage* result = malloc(sizeof(SecureStorage));
// Setting key
result->key = key;
// Return result
return result;
}
// Functions
void xor_encrypt_decrypt(char* data, char* key){
size_t data_len = strlen(data);
size_t key_len = strlen(key);
for (size_t i = 0; i < data_len; ++i) {
data[i] ^= key[i % key_len];
}
}
char* secure_storage_load(SecureStorage* secure_storage, char* tag){
/*
Currently, We will use XOR encryption & XOR decryption.
Not too secure but it's simple.
*/
// Loading data from storage
char* data = store_load(tag);
// Is the data empty?
if (!data) {
FURI_LOG_D(TAG, "Decrypting... Nothing?");
return NULL;
}
// Decrypting it
xor_encrypt_decrypt(data, secure_storage->key);
FURI_LOG_D(TAG, "Decrypted: %s", data);
// Returning result
return data;
}
void secure_storage_save(SecureStorage* secure_storage, char* tag, char* data){
// Encrypting data
xor_encrypt_decrypt(data, secure_storage->key);
// Saving it
store_save(tag, data);
}
void secure_storage_free(SecureStorage* secure_storage){
free(secure_storage->key);
free(secure_storage);
}

21
project/backend/secure.h Normal file
View file

@ -0,0 +1,21 @@
// Define once
#ifndef H_SECURE
#define H_SECURE
// Libraries
#include "store.h"
// Structures
typedef struct {
char* key;
} SecureStorage;
// Constructors
SecureStorage* secure_storage_init(char* key);
// Functions
char* secure_storage_load(SecureStorage* secure_storage, char* tag);
void secure_storage_save(SecureStorage* secure_storage, char* tag, char* data);
void secure_storage_free(SecureStorage* secure_storage);
#endif

View file

@ -134,7 +134,7 @@ bool FP_Scene_Event_Create(void* context, SceneManagerEvent event) {
break;
case FP_Scene_Create_Event_Done:
// Save the password
manager_savepass(app->manager->new);
manager_savepass(app->manager, app->manager->new);
// Reload manager
manager_free(app->manager);