Hi guys,
ESP_Angus, thanks for moving the subject to a new topic.
So here is my sample:
Code: Select all
#include "mbedtls/aes.h"
mbedtls_aes_context aes;
size_t _length = 16;
unsigned char iv[16] = "0123456789abcde";
unsigned char key[] = "F56C041F990E5374A1E78B333DAEBEB1";
unsigned char input[16] = "abcdefghijklmno";
unsigned char encrypt_output[16];
unsigned char decrypt_output[16];
static void hex_print(const void* pv, size_t len)
{
const unsigned char * p = (const unsigned char*)pv;
if (NULL == pv)
printf("NULL");
else
{
size_t i = 0;
for (; i<len;++i)
printf("%02X ", *p++);
}
printf("\n");
}
void _aes_encrypt(unsigned char *iv, size_t crypt_len, const unsigned char *input, unsigned char *output)
{
size_t iv_offset = 0;
mbedtls_aes_setkey_enc(&aes, key, 128);
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, input, output);
//mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_ENCRYPT, crypt_len, iv, input, output);
//mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, crypt_len, &iv_offset, iv, input, output);
}
void _aes_decrypt(unsigned char *iv, size_t crypt_len, const unsigned char *input, unsigned char *output)
{
size_t iv_offset = 0;
mbedtls_aes_setkey_dec(&aes, key, 128);
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, input, output);
//mbedtls_aes_crypt_cfb8(&aes, MBEDTLS_AES_DECRYPT, crypt_len, iv, input, output);
//mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_DECRYPT, crypt_len, &iv_offset, iv, input, output);
}
void _security_init(void)
{
mbedtls_aes_init(&aes);
}
void _security_deinit(void)
{
mbedtls_aes_free(&aes);
}
void setup() {
Serial.begin(115200);
_security_init();
_aes_encrypt(iv, _length, input, encrypt_output);
_aes_decrypt(iv, _length, encrypt_output, decrypt_output);
printf("original:\t");
hex_print(input, sizeof(input));
printf("encrypted:\t");
hex_print(encrypt_output, sizeof(encrypt_output));
printf("decrypted:\t");
hex_print(decrypt_output, sizeof(decrypt_output));
_security_deinit();
}
void loop() {
}
The crypt and decrypt is working with "mbedtls_aes_crypt_ecb", but with
http://aes.online-domain-tools.com/ I don't get the same encrypted result.
I even try with "mbedtls_aes_crypt_cfb8" and "mbedtls_aes_crypt_cfb128", but it's even worse, decrypted value doesn't match with the original. But for that, I think I'm not fully understanding the process.