Added XOR encryption
This commit is contained in:
parent
fce394fd59
commit
c9680982c8
6 changed files with 91 additions and 10 deletions
|
@ -37,7 +37,7 @@ char** split_string(const char* str, char delimiter, int* count) {
|
||||||
}
|
}
|
||||||
void manager_loadnames(Manager* manager){
|
void manager_loadnames(Manager* manager){
|
||||||
// Loading the names of all passwords
|
// 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?
|
// Do we not have any passwords?
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
@ -90,6 +90,7 @@ Manager* manager_init() {
|
||||||
Manager* result = malloc(sizeof(Manager));
|
Manager* result = malloc(sizeof(Manager));
|
||||||
|
|
||||||
// Setting variables
|
// Setting variables
|
||||||
|
result->secure_storage = secure_storage_init("password");
|
||||||
result->count = 0;
|
result->count = 0;
|
||||||
|
|
||||||
// Loading all names on start
|
// Loading all names on start
|
||||||
|
@ -100,9 +101,9 @@ Manager* manager_init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
void manager_savepass(Password* pass) {
|
void manager_savepass(Manager* manager, Password* pass) {
|
||||||
// Our resulting string
|
// Our resulting string
|
||||||
char* result = store_load("Data");
|
char* result = secure_storage_load(manager->secure_storage, "Data");
|
||||||
|
|
||||||
// Is the result empty?
|
// Is the result empty?
|
||||||
if(!result) {
|
if(!result) {
|
||||||
|
@ -115,7 +116,7 @@ void manager_savepass(Password* pass) {
|
||||||
// Saving the data
|
// Saving the data
|
||||||
char* data = malloc(sizeof(char) * furi_string_size(n_result));
|
char* data = malloc(sizeof(char) * furi_string_size(n_result));
|
||||||
strcpy(data, furi_string_get_cstr(n_result));
|
strcpy(data, furi_string_get_cstr(n_result));
|
||||||
store_save("Data", data);
|
secure_storage_save(manager->secure_storage, "Data", data);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
furi_string_free(n_result);
|
furi_string_free(n_result);
|
||||||
|
@ -130,7 +131,7 @@ void manager_savepass(Password* pass) {
|
||||||
// Saving the data
|
// Saving the data
|
||||||
char* data = malloc(sizeof(char) * furi_string_size(new_result));
|
char* data = malloc(sizeof(char) * furi_string_size(new_result));
|
||||||
strcpy(data, furi_string_get_cstr(new_result));
|
strcpy(data, furi_string_get_cstr(new_result));
|
||||||
store_save("Data", data);
|
secure_storage_save(manager->secure_storage, "Data", data);
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
furi_string_free(new_result);
|
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);
|
FURI_LOG_D(TAG, "Loading password of name %s", name);
|
||||||
|
|
||||||
// Loading password string
|
// Loading password string
|
||||||
char* data = store_load("Data");
|
char* data = secure_storage_load(manager->secure_storage, "Data");
|
||||||
|
|
||||||
// Splitting string
|
// Splitting string
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
|
@ -7,13 +7,14 @@
|
||||||
|
|
||||||
#include "base.h"
|
#include "base.h"
|
||||||
#include "pass.h"
|
#include "pass.h"
|
||||||
#include "store.h"
|
#include "secure.h"
|
||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Password* current;
|
Password* current;
|
||||||
Password* new;
|
Password* new;
|
||||||
|
|
||||||
|
SecureStorage* secure_storage;
|
||||||
char** names;
|
char** names;
|
||||||
int count;
|
int count;
|
||||||
} Manager;
|
} Manager;
|
||||||
|
@ -22,7 +23,7 @@ typedef struct {
|
||||||
Manager* manager_init();
|
Manager* manager_init();
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
void manager_savepass(Password* pass);
|
void manager_savepass(Manager* manager, Password* pass);
|
||||||
void manager_loadpass(Manager* manager, char* name);
|
void manager_loadpass(Manager* manager, char* name);
|
||||||
void manager_free(Manager* manager);
|
void manager_free(Manager* manager);
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
#define H_PASS
|
#define H_PASS
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include "store.h"
|
|
||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
59
project/backend/secure.c
Normal file
59
project/backend/secure.c
Normal 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
21
project/backend/secure.h
Normal 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
|
|
@ -134,7 +134,7 @@ bool FP_Scene_Event_Create(void* context, SceneManagerEvent event) {
|
||||||
break;
|
break;
|
||||||
case FP_Scene_Create_Event_Done:
|
case FP_Scene_Create_Event_Done:
|
||||||
// Save the password
|
// Save the password
|
||||||
manager_savepass(app->manager->new);
|
manager_savepass(app->manager, app->manager->new);
|
||||||
|
|
||||||
// Reload manager
|
// Reload manager
|
||||||
manager_free(app->manager);
|
manager_free(app->manager);
|
||||||
|
|
Loading…
Reference in a new issue