diff --git a/encryption/encryption.c b/encryption/encryption.c new file mode 100644 index 0000000..153d4f1 --- /dev/null +++ b/encryption/encryption.c @@ -0,0 +1,33 @@ +/* + * Wrapper around mbedtls to provide AES encryption + * + * Needs esp32 arduino platform libraries + * + * Hesham T. Banafa + * May 9th, 2022 + * + */ +#include +#include "encryption.h" + +static int valid_time(long long time_epoch); + +extern void aes_init(aes_t *ctx, char *key) +{ + mbedtls_aes_init(&ctx->aes_ctx); + //mbedtls_aes_setkey_enc(ctx->aes_ctx, (const unsigned char*)key, strlen(key) * 8 ); + ctx->psk_key = key; // Save key ptr +} + +extern void aes_encrypt(aes_t *ctx, char *plain_text, char *out_buf) +{ + if (ctx == NULL) return; // What are you doing? out_buf remains as is. + mbedtls_aes_setkey_enc(&ctx->aes_ctx, (const unsigned char*)ctx->psk_key, strlen(ctx->psk_key) * 8 ); + mbedtls_aes_crypt_ecb(&ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, (const unsigned char*)plain_text, (unsigned char*)out_buf); +} + +extern void aes_decrypt(aes_t *ctx, char *cipher_text, char *out_buf) +{ + +} + diff --git a/encryption/encryption.h b/encryption/encryption.h new file mode 100644 index 0000000..c83620f --- /dev/null +++ b/encryption/encryption.h @@ -0,0 +1,24 @@ +/* + * Wrapper around mbedtls to provide AES encryption + * + * Needs esp32 arduino platform libraries + * + * Hesham T. Banafa + * May 9th, 2022 + * + */ + +#include "mbedtls/aes.h" + +typedef struct aes_t +{ + const char *psk_key; + mbedtls_aes_context aes_ctx; +} aes_t; + +static int valid_time(long long time_epoch); + +extern void aes_init(aes_t *ctx, char *key); +extern void aes_encrypt(aes_t *ctx, char *plain_text, char *out_buf); +extern void aes_decrypt(aes_t *ctx, char *cipher_text, char *out_buf); +