forked from pr3y/Bruce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switched to xor encryption, added cmds in file browser and webui (pr3…
- Loading branch information
Showing
12 changed files
with
241 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,31 @@ | ||
#include "mbedtls/aes.h" | ||
#include "mbedtls/md.h" | ||
#include "mbedtls/pkcs5.h" | ||
#include <Arduino.h> | ||
|
||
#include <mbedtls/base64.h> | ||
#include <mbedtls/sha256.h> | ||
#include <mbedtls/pkcs5.h> | ||
#include <mbedtls/gcm.h> | ||
#include <mbedtls/error.h> // error code to string conversion | ||
|
||
#include <mbedtls/entropy.h> // entropy gathering | ||
#include <mbedtls/ctr_drbg.h> // CSPRNG | ||
#include <mbedtls/error.h> // error code to string conversion | ||
#include <mbedtls/md.h> // hash algorithms | ||
#include <mbedtls/pkcs5.h> // pbkdf2 KDF | ||
|
||
|
||
const size_t iterations = 1000; | ||
const size_t keyLength = 32; | ||
|
||
|
||
String generateRandom(int len) { | ||
#include <Arduino.h> | ||
#include <MD5Builder.h> | ||
|
||
|
||
String xorEncryptDecrypt(const String &input, const String &password) { | ||
uint8_t md5Hash[16]; | ||
|
||
MD5Builder md5; | ||
md5.begin(); | ||
md5.add(password); | ||
md5.calculate(); | ||
md5.getBytes(md5Hash); // Store MD5 hash in the output array | ||
|
||
String output = input; // Copy input to output for modification | ||
for (size_t i = 0; i < input.length(); i++) { | ||
output[i] = input[i] ^ md5Hash[i % 16]; // XOR each byte with the MD5 hash | ||
} | ||
|
||
return output; | ||
} | ||
|
||
|
||
String aes_encrypt(String& plaintext, const String& password_str) { | ||
// buggy? https://github.com/espressif/arduino-esp32/issues/3719 | ||
|
||
/* | ||
unsigned char* salt = generateRandom(16).c_str(); // salt size = 16 | ||
unsigned char* iv = generateRandom(12).c_str(); // iv size = 12 | ||
unsigned char* tag = generateRandom(16).c_str(); // tag size = 16 | ||
mbedtls_gcm_context ctx; | ||
mbedtls_gcm_init(&ctx); | ||
|
||
mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, 256); | ||
mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, plaintext_len, iv, 12, ad, 16, plaintext.c_str(), ciphertext, tag); | ||
int ret = mbedtls_gcm_crypt_and_tag(&aes, MBEDTLS_GCM_ENCRYPT, strlen(text), (const unsigned char*) iv, strlen(iv), NULL, NULL, (const unsigned char*) text, (unsigned char*) encrypted, strlen(tag), (unsigned char*) tag); | ||
mbedtls_gcm_free(&ctx); | ||
*/ | ||
// WIP | ||
return plaintext; | ||
String encryptString(String& plaintext, const String& password_str) { | ||
return xorEncryptDecrypt(plaintext, password_str); | ||
} | ||
|
||
|
||
String aes_decrypt(String& encryptedBase64, const String& password_str) { | ||
|
||
const char* password = password_str.c_str(); | ||
//char mbed_err[MBED_ERR_BUF]; | ||
int ret; | ||
|
||
// decode base64 | ||
size_t decodedLen; | ||
mbedtls_base64_decode(NULL, 0, &decodedLen, (const unsigned char *)encryptedBase64.c_str(), encryptedBase64.length()); | ||
unsigned char* decodedData = (unsigned char*)malloc(decodedLen); | ||
memset(decodedData, 0, decodedLen); | ||
mbedtls_base64_decode(decodedData, decodedLen, &decodedLen, (const unsigned char *)encryptedBase64.c_str(), encryptedBase64.length()); | ||
if(decodedLen<=44) { | ||
Serial.println("base64 decode error or incomplete file"); | ||
return ""; | ||
} | ||
|
||
unsigned char* salt = decodedData; | ||
unsigned char* iv = decodedData + 16; // salt size = 16 | ||
unsigned char* tag = decodedData + 28; // salt+iv size = 16+12 | ||
unsigned char* encryptedContent = decodedData + 44; // salt+iv+tag size = 16+12+16 | ||
size_t encryptedContentLen = decodedLen - 44; | ||
|
||
// Derive key using PBKDF2 | ||
unsigned char key[keyLength]; | ||
mbedtls_md_context_t md_ctx; | ||
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); | ||
|
||
mbedtls_md_init(&md_ctx); | ||
ret = mbedtls_md_setup(&md_ctx, | ||
md_info, | ||
1); // non-zero implies HMAC is going to be used (0 saves memory, but is less secure) | ||
if (md_info == NULL || ret != 0) { | ||
Serial.println("Hash algorithm setup failed"); | ||
Serial.println(ret); | ||
free(decodedData); | ||
//mbedtls_strerror(ret, mbed_err, MBED_ERR_BUF); | ||
//mbedtls_printf( "mbedTLS ERROR: %s\n", mbed_err); | ||
return ""; | ||
} | ||
|
||
ret = mbedtls_pkcs5_pbkdf2_hmac( | ||
&md_ctx, | ||
(const unsigned char*)password, | ||
strlen(password), | ||
salt, | ||
16, // salt_bytes | ||
iterations, | ||
keyLength, | ||
key | ||
); | ||
|
||
mbedtls_md_free(&md_ctx); | ||
|
||
if (ret != 0) { | ||
Serial.println("Key derivation failed"); | ||
Serial.println(ret); | ||
free(decodedData); | ||
//mbedtls_strerror(ret, mbed_err, MBED_ERR_BUF); | ||
//mbedtls_printf( "mbedTLS ERROR: %s\n", mbed_err); | ||
return ""; | ||
} | ||
|
||
// Decrypt using AES-GCM | ||
unsigned char decryptedContent[encryptedContentLen] = {0}; | ||
mbedtls_gcm_context gcm; | ||
mbedtls_gcm_init(&gcm); | ||
|
||
ret = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, key, keyLength * 8); | ||
|
||
if (ret != 0) { | ||
Serial.println("Set Key failed"); | ||
free(decodedData); | ||
Serial.println(ret); | ||
//mbedtls_strerror(ret, mbed_err, MBED_ERR_BUF); | ||
//mbedtls_printf( "mbedTLS ERROR: %s\n", mbed_err); | ||
return ""; | ||
} | ||
|
||
ret = mbedtls_gcm_auth_decrypt(&gcm, encryptedContentLen, iv, 12, NULL, 0, tag, 16, encryptedContent, decryptedContent); | ||
String r = String((char*)decryptedContent); | ||
|
||
mbedtls_gcm_free(&gcm); | ||
free(decodedData); | ||
//free(decryptedContent); | ||
|
||
if (ret == 0) { | ||
// Successfully decrypted | ||
return(r); | ||
} else { | ||
// Decryption failed | ||
Serial.println("Decryption failed"); | ||
Serial.println(ret); | ||
//mbedtls_strerror(ret, mbed_err, MBED_ERR_BUF); | ||
//mbedtls_printf( "mbedTLS ERROR: %s\n", mbed_err); | ||
return(""); | ||
} | ||
String decryptString(String& cypertext, const String& password_str) { | ||
return xorEncryptDecrypt(cypertext, password_str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.