This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

ChaCha-Poly encryption test function problem

static void hex_text_print(char const* p_label, char const * p_text, size_t len)
{
    NRF_LOG_RAW_INFO("---- %s (length: %u) ----\r\n", p_label, len);
    NRF_LOG_FLUSH();

    // Handle partial line (left)
    for (size_t i = 0; i < len; i++)
    {
        if (((i & 0xF) == 0) && (i > 0))
        {
            NRF_LOG_RAW_INFO("\r\n");
            NRF_LOG_FLUSH();
        }

        NRF_LOG_RAW_INFO("%02x ", p_text[i]);
        NRF_LOG_FLUSH();
    }
    NRF_LOG_RAW_INFO("\r\n");
    NRF_LOG_RAW_INFO("---- %s end ----\r\n\r\n", p_label);
    NRF_LOG_FLUSH();
}

static void text_print(char const* p_label, char const * p_text, size_t len)
{
    NRF_LOG_RAW_INFO("----%s (length: %u) ----\r\n", p_label, len);
    NRF_LOG_FLUSH();
    for(size_t i = 0; i < len; i++)
    {
        NRF_LOG_RAW_INFO("%c", p_text[i]);
        NRF_LOG_FLUSH();
    }
    NRF_LOG_RAW_INFO("\r\n");
    NRF_LOG_RAW_INFO("---- %s end ----\r\n\r\n", p_label);
    NRF_LOG_FLUSH();
}

static void encrypted_text_print(char const * p_text, size_t encrypted_len)
{
    hex_text_print("Encrypted text (hex)", p_text, encrypted_len);
}

static void decrypted_text_print(char const * p_text, size_t decrypted_len)
{
    text_print("Decrypted text", p_text, decrypted_len);
    hex_text_print("Decrypted text (hex)", p_text, decrypted_len);
}

static void mac_print(uint8_t const * p_buff, uint8_t mac_size)
{
    hex_text_print("MAC (hex)", (char const*)p_buff, mac_size);
}


void hub_testChaChaEncryption() {
    //initialise
    ret_code_t ret;
    ret = nrf_crypto_init();
    APP_ERROR_CHECK(ret);
    ret = nrf_mem_init();
    APP_ERROR_CHECK(ret);
    //encrypting
    //plain message - example of AMB event from sensor in AMP documentation
    static uint8_t plain[NRF_CRYPTO_EXAMPLE_AES_MAX_TEXT_SIZE] = {0x41, 0x54, 0x27, 0x08, 0x05, 0x80, 0x51, 0x00, 0x23, 0x01}; 
    static uint8_t mac[NRF_CRYPTO_CHACHA_POLY_MAC_SIZE];
    static uint8_t nonce[NRF_CRYPTO_CHACHA_POLY_NONCE_SIZE];
    static uint8_t adata[] = {0xAA, 0xBB, 0xCC, 0xDD}; //how to represent this in Python? Use header of a packet?
    static nrf_crypto_aead_context_t chacha_poly_ctx;
    static char encrypted[NRF_CRYPTO_EXAMPLE_AES_MAX_TEXT_SIZE];
    ret_code_t ret_val_chacha;
    uint32_t len = sizeof(plain);
    memset(mac, 0, sizeof(mac));
    memset(nonce, 0, sizeof(nonce));
    ret_val_chacha = nrf_crypto_aead_init(&chacha_poly_ctx, &g_nrf_crypto_chacha_poly_256_info, m_key);
    AES_ERROR_CHECK(ret_val_chacha);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Starting to encrypt\n");
    ret_val_chacha = nrf_crypto_aead_crypt(&chacha_poly_ctx,
                                            NRF_CRYPTO_ENCRYPT,
                                            nonce,
                                            sizeof(nonce),
                                            adata,
                                            sizeof(adata),
                                            plain,
                                            len,
                                            (uint8_t *)encrypted,
                                            mac,
                                            sizeof(mac));
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Encryption done\n");
    encrypted_text_print(encrypted, len);
    mac_print(mac, sizeof(mac));
}

I have an issue with attempting to test an encryption on a sample data made in that function and then called in int main(void).
What happens is that during the debug the CRYS_CHACHA_POLY function was being called because I did put a breakpoint there. So "Starting to encrypt" line is being printed, and then after stepping in it goes to assembly, but at that green line it throws "Softdevice assert" error from app_error_weak.c of Mesh SDK.

If I try to run the program without breakpoints, it prints "Starting to encrypt", but it doesn't print any errors or line "Send a message to hub encrypt" (line content is just an indication).

What could be the reason for not working properly? ICould it be that the drv_clock_init wasn't used?

All screenshots attached.

Parents
  • Hi,

    The reason for the SoftDevice assert is that the SoftDevice has internal checks for ensuring that timing constraints are not violated. When you pause code execution (such as pausing at breakpoints or while stepping through the code) there will be violation of timing constraints - leading to SoftDevice assert. Note that code executes as normal up until the first time you pause execution, so you can still use a breakpoint to check that code execution follows a given path. It is possible to run the high priority SoftDevice routines in the background while debugging lower-level code, using Monitor Mode Debugging, but as this may be a bit complex to set up and get running you may want to rely on other debug techniques instead.

    From your descriptions it sounds like there is a separate issue happening (which is the reason why you run the debug session in the first place.) If so, what is the nature of this other issue?

    Regards,
    Terje

  • Hi Terje,

    The issue is actually the second part of my question - when I run the program just as usual, not in debug mode, it doesn't output encrypted text, as I called it after nrf_crypto_aead_crypt, and I'm struggling to find the reason why, Except for some changes, I used the example from SDK. That's why I thought something hangs during nrf_crypto_aead_crypt. And I'm not entirely sure why that happens.

    Thanks,

    Ilias

Reply Children
No Data
Related