This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Compiler get stuck in side nrf_crypto_hash_update() function

Hi i am using nordic nrf52840 chip. and SDK17.1 i am using.

in that under secure bootloader code i am trying to use the nrf_crypto_hash_update() function to calculate the hash of my application code.

But when compiler reached to this function then it got stuck somewhere. 

Any idea y this happened. is i need to enable something in the code for hash calculation...?

Regards

Rohit Saini

  • And also i doing the same thing in open bootloader code also, but in that i am not facing such an issue.

  • Are you calling it in a critical region? Can you post the code here?

    Where do you place this function in the secure bootloader, and is this the same place where you do it in the open bootloader?

  • Below is the main content 

    int main(void)
    {
    uint32_t ret_val;

    bsp_board_init(BSP_INIT_LEDS);

    for (int i = 0; i < 8; i++)
    {
    nrf_gpio_pin_toggle(LED_2);
    nrf_delay_ms(500);
    }

    if (IsSignatureVerified(APPLICATION) == true)
    {
    // Must happen before flash protection is applied, since it edits a protected page.
    nrf_bootloader_mbr_addrs_populate();

    // Protect MBR and bootloader code from being overwritten.
    ret_val = nrf_bootloader_flash_protect(0, MBR_SIZE);
    APP_ERROR_CHECK(ret_val);
    ret_val = nrf_bootloader_flash_protect(BOOTLOADER_START_ADDR, BOOTLOADER_SIZE);
    APP_ERROR_CHECK(ret_val);

    (void) NRF_LOG_INIT(nrf_bootloader_dfu_timer_counter_get);
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("Inside main");

    ret_val = nrf_bootloader_init(dfu_observer);
    APP_ERROR_CHECK(ret_val);

    NRF_LOG_FLUSH();

    NRF_LOG_ERROR("After main, should never be reached.");
    NRF_LOG_FLUSH();

    APP_ERROR_CHECK_BOOL(false);
    }
    else
    {
    for (int i = 0; i < 8; i++)
    {
    nrf_gpio_pin_toggle(LED_2);
    nrf_delay_ms(2000);
    }
    }
    }

    in side IsSignatureVerified() function i am calling this function. where i am using nrf_crypto_hash_update() function. 

    void capture_flashdata_into_databuffer(uint8_t u8Type)
    {

    uint32_t flashReadAddress = 0;
    uint32_t appEndAddress = 0;
    ret_code_t err_code;

    uint8_t pk_copy[sizeof(pk1)];

    nrf_crypto_hash_context_t hash_context = {0};

    err_code = nrf_crypto_init();
    ASSERT(err_code == NRF_SUCCESS);

    // Convert public key to big-endian format for use in nrf_crypto.
    nrf_crypto_internal_double_swap_endian(pk_copy, pk1, sizeof(pk1) / 2);

    err_code = nrf_crypto_ecc_public_key_from_raw(&g_nrf_crypto_ecc_secp256r1_curve_info,
    &m_public_key,
    pk_copy,
    sizeof(pk1));
    /* Hash Calculation Code */
    err_code = nrf_crypto_hash_init(&hash_context, &g_nrf_crypto_hash_sha256_info);
    APP_ERROR_CHECK(err_code);
    errorcheck = err_code;


    if(u8Type == BOOT2)
    {
    do
    {

    flashReadAddress = BL2_START_ADDRESS;
    appEndAddress = (BL2_START_ADDRESS + BL2_SIZE_ADDRESS) - 1 ;//0x2AFFF; //
    //flashReadAddress = AP_START_ADDRESS;
    //appEndAddress = (AP_START_ADDRESS + AP_SIZE_ADDRESS) - 1 ;//0x2AFFF; //

    if (flashReadAddress > appEndAddress)
    break;

    while ((appEndAddress - flashReadAddress) >(BLOCK_SIZE-1))
    {
    read_flash_data(flashReadAddress, BLOCK_SIZE);
    flashReadAddress += BLOCK_SIZE;
    err_code = nrf_crypto_hash_update(&hash_context, dataBuffer, BLOCK_SIZE);
    APP_ERROR_CHECK(err_code);
    }

    //uint32_t temp = (appEndAddress - flashReadAddress) + 1;
    dataBufferSize = (appEndAddress - flashReadAddress);
    read_flash_data(flashReadAddress, dataBufferSize);
    dataBuffer_ptr = (uint8_t*) DUMMY_START_ADDRESS;


    err_code = nrf_crypto_hash_update(&hash_context, dataBuffer, dataBufferSize);
    APP_ERROR_CHECK(err_code);
    errorcheck = err_code;

    err_code = nrf_crypto_hash_finalize(&hash_context, m_sig_hash, &hash_len);
    APP_ERROR_CHECK(err_code);
    errorcheck = err_code;

    } while (0);
    }
    else
    {
    do
    {

    flashReadAddress = AP_START_ADDRESS;
    appEndAddress = (AP_START_ADDRESS + AP_SIZE_ADDRESS) - 1 ;//0x2AFFF; //

    if (flashReadAddress > appEndAddress)
    break;

    while ((appEndAddress - flashReadAddress) >(BLOCK_SIZE-1))
    {
    read_flash_data(flashReadAddress, BLOCK_SIZE);
    flashReadAddress += BLOCK_SIZE;
    err_code = nrf_crypto_hash_update(&hash_context, dataBuffer, BLOCK_SIZE);
    APP_ERROR_CHECK(err_code);
    }

    //uint32_t temp = (appEndAddress - flashReadAddress) + 1;
    dataBufferSize = (appEndAddress - flashReadAddress);
    read_flash_data(flashReadAddress, dataBufferSize);
    dataBuffer_ptr = (uint8_t*) DUMMY_START_ADDRESS;


    err_code = nrf_crypto_hash_update(&hash_context, dataBuffer, dataBufferSize);
    APP_ERROR_CHECK(err_code);
    errorcheck = err_code;

    err_code = nrf_crypto_hash_finalize(&hash_context, m_sig_hash, &hash_len);
    APP_ERROR_CHECK(err_code);
    errorcheck = err_code;

    } while (0);
    }

    }

  • IN open bootloader its in main only but in little different way.

    int main(void)
    {
    uint32_t ret_val;

    bsp_board_init(BSP_INIT_LEDS);


    {
    for (int i = 0; i < 6; i++)
    {
    //bsp_board_led_invert(i);
    nrf_gpio_pin_toggle(LED_3);
    nrf_delay_ms(500);
    }
    }

    // Must happen before flash protection is applied, since it edits a protected page.
    nrf_bootloader_mbr_addrs_populate();

    // Protect MBR and bootloader and application code from being overwritten.
    ret_val = nrf_bootloader_flash_protect(0, MBR_SIZE);
    APP_ERROR_CHECK(ret_val);
    ret_val = nrf_bootloader_flash_protect(AP_START_ADDRESS, 0x4000);
    APP_ERROR_CHECK(ret_val);
    ret_val = nrf_bootloader_flash_protect(BL2_START_ADDRESS, (NRF_MBR_PARAMS_PAGE_ADDRESS - 0xF8000)); //ROHIT
    APP_ERROR_CHECK(ret_val);

    NRF_LOG_INFO("Open USB bootloader started");
    //NRF_LOG_FLUSH();

    if (IsSignatureVerified(BOOT2) == true)
    {
    #if 1
    //Jump_To_App();
    Jump_To_Bootloader2();
    #endif
    }

  • R_S said:
    IN open bootloader its in main only

    Does that mean the secure bootloader is performing the hash computation from other places as well?

    Also, please do not copy-and-paste large code snippets into your post. It makes it hard to read. Instead you can use the formatting tools to insert the code, or upload the c file as an attachment.

Related