Hi,
I am using nrf52832 module, sdk 17.0.2.
I have ble nus service added and device works properly had no issues till i add the AES related files and defines.
Can you please help me how to resolve this issue?
Hi,
I am using nrf52832 module, sdk 17.0.2.
I have ble nus service added and device works properly had no issues till i add the AES related files and defines.
Can you please help me how to resolve this issue?
I am using softdevice S132. And i added aes logic from aes cbc with padding example to encrypt and decrypt.
Hi Swetha,
Could you let me know what exact issue you have when adding AES ? Do you have any log or screenshot when you get into the error ?
Where do you init AES ? Which crypto backend do you use ?
First of all... I would like to know if nrf52832 support aes cbc along with padding with ble communication.
This is the code that i have in my main.c
/* Maximum allowed key = 256 bit */
static uint8_t m_key[16] = {'N', 'O', 'R', 'D', 'I', 'C', ' ',
'S', 'E', 'M', 'I', 'C', 'O', 'N', 'D', 'U', 'C', 'T', 'O', 'R',
'A', 'E', 'S', ' ', 'C', 'B', 'C', ' ', 'T', 'E', 'S', 'T'};
static char encrypted_text[NRF_CRYPTO_EXAMPLE_AES_MAX_TEXT_SIZE];
static char decrypted_text[NRF_CRYPTO_EXAMPLE_AES_MAX_TEXT_SIZE];
static void crypt_cbc(void)
{
uint8_t iv[16];
ret_code_t ret_val;
size_t len_in;
size_t len_out;
static nrf_crypto_aes_context_t cbc_encr_128_ctx; // AES CBC encryption context
static nrf_crypto_aes_context_t cbc_decr_128_ctx; // AES CBC decryption context
memset(encrypted_text, 0, sizeof(encrypted_text));
memset(decrypted_text, 0, sizeof(decrypted_text));
//
// Encryption phase
//
/* Init encryption context for 128 bit key and PKCS7 padding mode */
ret_val = nrf_crypto_aes_init(&cbc_encr_128_ctx,
&g_nrf_crypto_aes_cbc_128_pad_pkcs7_info,
NRF_CRYPTO_ENCRYPT);
AES_ERROR_CHECK(ret_val);
/* Set key for encryption context - only first 128 key bits will be used */
ret_val = nrf_crypto_aes_key_set(&cbc_encr_128_ctx, m_key);
AES_ERROR_CHECK(ret_val);
memset(iv, 0, sizeof(iv));
/* Set IV for encryption context */
ret_val = nrf_crypto_aes_iv_set(&cbc_encr_128_ctx, iv);
AES_ERROR_CHECK(ret_val);
len_in = strlen(m_plain_text);
len_out = sizeof(encrypted_text);
/* Encrypt text
When padding is selected m_encrypted_text buffer shall be at least 16 bytes larger
than text_len. */
ret_val = nrf_crypto_aes_finalize(&cbc_encr_128_ctx,
(uint8_t *)m_plain_text,
len_in,
(uint8_t *)encrypted_text,
&len_out);
AES_ERROR_CHECK(ret_val);
//
// Decryption phase
//
/* Init decryption context for 128 bit key and PKCS7 padding mode */
ret_val = nrf_crypto_aes_init(&cbc_decr_128_ctx,
&g_nrf_crypto_aes_cbc_128_pad_pkcs7_info,
NRF_CRYPTO_DECRYPT);
AES_ERROR_CHECK(ret_val);
/* Set key for decryption context - only first 128 key bits will be used */
ret_val = nrf_crypto_aes_key_set(&cbc_decr_128_ctx, m_key);
AES_ERROR_CHECK(ret_val);
memset(iv, 0, sizeof(iv));
/* Set IV for decryption context */
ret_val = nrf_crypto_aes_iv_set(&cbc_decr_128_ctx, iv);
AES_ERROR_CHECK(ret_val);
/* Decrypt text */
ret_val = nrf_crypto_aes_finalize(&cbc_decr_128_ctx,
(uint8_t *)encrypted_text,
len_out,
(uint8_t *)decrypted_text,
&len_out);
AES_ERROR_CHECK(ret_val);
/* trim padding */
decrypted_text[len_out] = '\0';
NRF_LOG_FLUSH();
if (memcmp(m_plain_text, decrypted_text, strlen(m_plain_text)) == 0)
{
NRF_LOG_RAW_INFO("AES CBC example with padding executed successfully.\r\n");
}
else
{
NRF_LOG_RAW_INFO("AES CBC example with padding failed!!!\r\n");
}
}
/**@brief Application main function.
*/
int main(void)
{
uint32_t err_code;
// Initialize the async SV CI interface to bootloader before any interrupts are enabled.
err_code = ble_dfu_buttonless_async_svci_init();
log_init();
err_code = nrf_crypto_init();
APP_ERROR_CHECK(err_code);
#if NRF_CRYPTO_BACKEND_MBEDTLS_ENABLED
err_code = nrf_mem_init();
APP_ERROR_CHECK(err_code);
#endif
timers_init();
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
// Start execution.
advertising_start();
crypt_cbc();
while(1)
{
NRF_LOG_FLUSH();
idle_state_handle();
}
}
