65 lines
No EOL
1.5 KiB
C
65 lines
No EOL
1.5 KiB
C
// Header
|
|
#include "secure.h"
|
|
|
|
// Constants
|
|
#define KEYBOARD_STR_LEN 64
|
|
|
|
// Constructors
|
|
SecureStorage* secure_storage_init(char* key){
|
|
// Creating memory for secure storage
|
|
SecureStorage* result = malloc(sizeof(SecureStorage));
|
|
|
|
// Setting key
|
|
result->key = malloc(sizeof(char) * KEYBOARD_STR_LEN);
|
|
|
|
// Copying key
|
|
strcpy(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, Store_File_Path path, 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(path, 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, Store_File_Path path, char* tag, char* data){
|
|
// Encrypting data
|
|
xor_encrypt_decrypt(data, secure_storage->key);
|
|
|
|
// Saving it
|
|
store_save(path, tag, data);
|
|
}
|
|
void secure_storage_free(SecureStorage* secure_storage){
|
|
free(secure_storage->key);
|
|
free(secure_storage);
|
|
} |