Skip to content

Commit

Permalink
encryption with protected hmac key
Browse files Browse the repository at this point in the history
  • Loading branch information
tcsullivan committed Apr 3, 2024
1 parent 01d3cba commit 1e538a7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
6 changes: 3 additions & 3 deletions noisemeter-device/noisemeter-device.ino
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ void saveNetworkCreds(WebServer& httpServer) {
// Confirm that the form was actually submitted.
if (httpServer.hasArg("ssid") && httpServer.hasArg("psk")) {
const auto id = String(buildDeviceId());
const auto ssid = Secret(id).encrypt(httpServer.arg("ssid"));
const auto psk = Secret(id).encrypt(httpServer.arg("psk"));
const auto ssid = Secret::encrypt(id, httpServer.arg("ssid"));
const auto psk = Secret::encrypt(id, httpServer.arg("psk"));

// Confirm that the given credentials will fit in the allocated EEPROM space.
if (!ssid.isEmpty() && Creds.canStore(ssid) && Creds.canStore(psk)) {
Expand Down Expand Up @@ -323,7 +323,7 @@ int tryWifiConnection()

WiFi.mode(WIFI_STA);
const auto id = String(buildDeviceId());
const auto stat = WiFi.begin(Secret(id).decrypt(ssid).c_str(), Secret(id).decrypt(psk).c_str());
const auto stat = WiFi.begin(Secret::decrypt(id, ssid).c_str(), Secret::decrypt(id, psk).c_str());
if (stat == WL_CONNECT_FAILED)
return -1;

Expand Down
51 changes: 51 additions & 0 deletions noisemeter-device/secret-store.cpp
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

47 changes: 9 additions & 38 deletions noisemeter-device/secret-store.h
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

0 comments on commit 1e538a7

Please sign in to comment.