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

Regarding SHA256

Hi. I'm using nRF52832, SDK13.0.0.

I need SHA256.

My code is

NRF_CRYPTO_HASH_CREATE(init_packet_hash, SHA256);

const nrf_crypto_hash_info_t hash_info_sha256 =
{
    .hash_type = NRF_CRYPTO_HASH_TYPE_SHA256,
    .endian_type = NRF_CRYPTO_ENDIAN_LE
};


void hash_test()
{
 uint8_t p_init_cmd[32];
 uint32_t init_cmd_len = 32;

 for(int i=0;i<32;i++) p_init_cmd[i]=i;

 SEGGER_RTT_printf_log_Hex(0, RTT_CTRL_TEXT_CYAN"p_init_cmd     : ", p_init_cmd, 32);

    ret_code_t                                       err_code = NRF_SUCCESS;
 err_code = nrf_crypto_hash_compute(hash_info_sha256, p_init_cmd, init_cmd_len, &init_packet_hash);

 SEGGER_RTT_printf_log(0, "hash_info_sha256, err_code=0x%x\n", err_code);
 SEGGER_RTT_printf_log_Hex(0, "init_packet_hash     : ", init_packet_hash.p_value, 32);
}

static void crypto_init(void)
{
    ret_code_t err_code = nrf_crypto_init();
    APP_ERROR_CHECK(err_code);
}

int main(void)
{

......

 crypto_init();

hash_test();

}

in nrf_crypto_sw_hash.c

uint32_t nrf_crypto_hash_compute(nrf_crypto_hash_info_t    hash_info,
                                 uint8_t           const * p_data,
                                 uint32_t                  len,
                                 nrf_value_length_t      * p_hash)
{
    uint32_t            ret_val;
    uint32_t            hash_size;
    sha256_context_t    hash_context;
    nrf_value_length_t  hash_context_desc =
    {
        .p_value = (uint8_t*)&hash_context,
        .length  = sizeof(hash_context)
    };

    // Basic parameter testing.
    if (p_hash == NULL || p_data == NULL)
    {
        return NRF_ERROR_NULL;
    }

    if (p_hash->p_value == NULL)
    {
        return NRF_ERROR_INVALID_ADDR;
    }

    // Currently SHA256 is the only available hash algorithm
    if (hash_info.hash_type != NRF_CRYPTO_HASH_TYPE_SHA256)
    {
        return NRF_ERROR_NOT_SUPPORTED;
    }

    // Check if hash is correct size.
    ret_val = nrf_crypto_hash_size_get(hash_info.hash_type, &hash_size);
    if (ret_val != NRF_SUCCESS)
    {
        return ret_val;
    }

    if (hash_size != p_hash->length)
    {
        return NRF_ERROR_INVALID_LENGTH;
    }

    ret_val = nrf_crypto_hash_init(hash_info, &hash_context_desc);
    if (ret_val != NRF_SUCCESS)
    {
        return ret_val;
    }

    ret_val = nrf_crypto_hash_update(&hash_context_desc, p_data, len);
    if (ret_val != NRF_SUCCESS)
    {
        return ret_val;
    }

    ret_val = nrf_crypto_hash_finalize(hash_info, &hash_context_desc, p_hash);
    return ret_val;
}

But result is not same as SHA-256 Calculator(http://www.xorbin.com/tools/sha256-hash-calculator).

What could be the problem?

I need your help.

Related