Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SaSi_AesFinish Corrupts Context Variable

Setup:
nRF52840 DK
nRF52 SDK v16.0.0
s140 v7.0.1

When I attempt to use the nrf_crypto library for AES 128bit ECB encryption I keep getting 

NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED

out of 

nrf_crypto_aes_crypt

Even when I initialize the context before making the call I get the same error(although this is unnecessary given that this function initializes the context for you). I went through the function mentioned above step by step, keeping an eye on 

p_ctx->ecb_context.header.init_value

which has the value of 

#define NRF_CRYPTO_AES_INIT_MAGIC_VALUE     (0x53454163)    // ASCII "cAES"

until the library function call 

result = SaSi_AesFinish(&p_ctx->any.context,
                            data_size,
                            p_data_in  + offset,
                            data_size,
                            p_data_out + offset,
                            p_data_out_size);

After this function call the init_value gets truncated to 

p_ctx->ecb_context.header.init_value = 0x53000000

but the result variable indicates that the function was a success.


Here is my code for producing the error:

uint32_t cc310_aes_encrypt(const uint8_t * p_key, const uint8_t * p_plaintext, uint8_t * p_result, uint8_t data_len)
{
    nrf_crypto_aes_context_t aes_ctx;
    uint32_t ret_val;
    ret_val = nrf_crypto_aes_init(&aes_ctx, &g_nrf_crypto_aes_ecb_128_info, NRF_CRYPTO_ENCRYPT);
    AES_ERROR_CHECK(ret_val);
    
    uint8_t in_len = data_len;
    uint8_t out_len = data_len;
    /* Encrypt text */
    ret_val = nrf_crypto_aes_crypt(&aes_ctx,
                                    &g_nrf_crypto_aes_ecb_128_info,
                                    NRF_CRYPTO_ENCRYPT,
                                    p_key,
                                    NULL,
                                    p_plaintext,
                                    in_len,
                                    p_result,
                                    &out_len);
    AES_ERROR_CHECK(ret_val);

    ret_val = nrf_crypto_aeas_uninit(&aes_ctx);
    AES_ERROR_CHECK(ret_val);
    return ret_val;
} 

Error = 0x8502
The context was not initialized prior to this call or it was corrupted. Please call the corresponding init function for the algorithm to initialize it
Parents Reply Children
No Data
Related