-
-
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.
- Loading branch information
1 parent
01d3cba
commit 1e538a7
Showing
3 changed files
with
63 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "secret-store.h" | ||
|
||
#include <esp_hmac.h> | ||
#include <mbedtls/aes.h> | ||
|
||
constexpr static unsigned BITS = 256; // do not change | ||
|
||
namespace Secret { | ||
|
||
String encrypt(String key, String in) | ||
{ | ||
mbedtls_aes_context aes; | ||
mbedtls_aes_init(&aes); | ||
|
||
const auto kb = key.c_str(); | ||
const auto kl = key.length(); | ||
{ | ||
uint8_t hmac[BITS / 8]; | ||
esp_hmac_calculate(HMAC_KEY0, kb, kl, hmac); | ||
mbedtls_aes_setkey_enc(&aes, hmac, BITS); | ||
} | ||
|
||
char out[in.length()]; | ||
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, | ||
reinterpret_cast<const uint8_t *>(in.c_str()), | ||
reinterpret_cast<uint8_t *>(out)); | ||
return out; | ||
} | ||
|
||
String decrypt(String key, String in) | ||
{ | ||
mbedtls_aes_context aes; | ||
mbedtls_aes_init(&aes); | ||
|
||
const auto kb = key.c_str(); | ||
const auto kl = key.length(); | ||
{ | ||
uint8_t hmac[BITS / 8]; | ||
esp_hmac_calculate(HMAC_KEY0, kb, kl, hmac); | ||
mbedtls_aes_setkey_dec(&aes, hmac, BITS); | ||
} | ||
|
||
char out[in.length()]; | ||
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, | ||
reinterpret_cast<const uint8_t *>(in.c_str()), | ||
reinterpret_cast<uint8_t *>(out)); | ||
return out; | ||
} | ||
|
||
} // namespace Secret | ||
|
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,42 +1,13 @@ | ||
#include <esp_hmac.h> | ||
#include <mbedtls/aes.h> | ||
#ifndef SECRET_STORE_H | ||
#define SECRET_STORE_H | ||
|
||
class Secret | ||
{ | ||
constexpr static int BITS = 256; // do not change | ||
mbedtls_aes_context aes; | ||
uint8_t hmac[BITS / 8]; | ||
|
||
bool generateKey(String key) { | ||
const auto result = esp_hmac_calculate(HMAC_KEY4, key.c_str(), key.length(), hmac); | ||
return result == ESP_OK; | ||
} | ||
|
||
String process(String in, int mode) { | ||
uint8_t out[64] = {0}; | ||
mbedtls_aes_crypt_ecb(&aes, mode, (const uint8_t *)in.c_str(), out); | ||
return String((char *)out); | ||
} | ||
|
||
public: | ||
Secret(String key) { | ||
mbedtls_aes_init(&aes); | ||
generateKey(key); | ||
} | ||
#include <WString.h> | ||
|
||
~Secret() { | ||
mbedtls_aes_free(&aes); | ||
} | ||
|
||
String encrypt(String in) { | ||
mbedtls_aes_setkey_enc(&aes, hmac, BITS); | ||
return process(in, MBEDTLS_AES_ENCRYPT); | ||
} | ||
|
||
String decrypt(String in) { | ||
mbedtls_aes_setkey_dec(&aes, hmac, BITS); | ||
return process(in, MBEDTLS_AES_DECRYPT); | ||
} | ||
}; | ||
namespace Secret | ||
{ | ||
String encrypt(String key, String in); | ||
String decrypt(String key, String in); | ||
} | ||
|
||
#endif // SECRET_STORE_H | ||
|