Describe the bug
When using MbedTLS library
Code: Select all
mbedtls_pk_encrypt
Code: Select all
mbedtls_pk_decrypt
To Reproduce:
Code: Select all
int ret = 0;
mbedtls_pk_context pk;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_pk_init(&pk);
mbedtls_ctr_drbg_init(&ctr_drbg);
/*
* Read the RSA public key
*/
if ((ret = mbedtls_pk_parse_public_key(&pk, ConstParams::RSA_PUB_KEY, 452)) != 0)
{
printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret);
}
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
size_t olen = 0;
/*
* Calculate the RSA encryption of the data.
*/
printf("\n . Generating the encrypted value");
const unsigned char to_encrypt[] = "Hello World!";
const size_t to_encrypt_len = sizeof(to_encrypt);
if ((ret = mbedtls_pk_encrypt(&pk, to_encrypt, to_encrypt_len + 1,
buf, &olen, sizeof(buf),
mbedtls_ctr_drbg_random, &ctr_drbg)) != 0)
{
printf(" failed\n ! mbedtls_pk_encrypt returned %d\n", -ret);
}
ESP_LOG_BUFFER_HEX("encrypted", buf, olen);
Code: Select all
ConstParams::RSA_PUB_KEY
Expected behavior
Correct encryption and decryption of any given input.